.ad 8
.bm 8
.fm 4
.bt $Copyright by   Software AG, 1993$$Page %$
.tm 12
.hm 6
.hs 3
.tt 1 $NME$Project Distributed Database System$vos43c$
.tt 2 $$$
.tt 3 $R.Roedling$socket address resolution$1997-04-28$
***********************************************************
.nf


    ========== licence begin LGPL
    Copyright (C) 2002 SAP AG

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2.1 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    ========== licence end

.fo
.nf
.sp
Module  : socket_address_resolution
=========
.sp
Purpose :
.CM *-END-* purpose -------------------------------------
.sp
.cp 3
Define  :

.CM *-END-* define --------------------------------------
.sp;.cp 3
Use     :

.CM *-END-* use -----------------------------------------
.sp;.cp 3
Synonym :

.CM *-END-* synonym -------------------------------------
.sp;.cp 3
Author  : R.Roedling
.sp
.cp 3
Created : 23.3.9
.sp
.cp 3
Version :
.sp
.cp 3
Release :  6.2 	 Date : 1997-04-28
.br
.sp
***********************************************************
.sp
.cp 10
.fo
.oc _/1
Specification:

.CM *-END-* specification -------------------------------
.sp 2
***********************************************************
.sp
.cp 10
.fo
.oc _/1
Description:

.CM *-END-* description ---------------------------------
.sp 2
***********************************************************
.sp
.cp 10
.nf
.oc _/1
Structure:

.CM *-END-* structure -----------------------------------
.sp 2
**********************************************************
.sp
.cp 10
.nf
.oc _/1
.CM -lll-
Code    :
/*PRETTY*/

/*
 * INCLUDE FILES
 */


/*
 *  DEFINES
 */
#define MOD__  "VOS43C : "
#define MF__   MOD__"UNDEFINED"

#define MX_DOT_STR   256

/*
 *  MACROS
 */


/*
 *  LOCAL TYPE AND STRUCT DEFINITIONS
 */


/*
 * EXTERNAL VARIABLES
 */


/*
 *  EXPORTED VARIABLES
 */


/*
 * LOCAL VARIABLES
 */
static LONG            fTCPIPInstalled;

/*
 * LOCAL FUNCTION PROTOTYPES
 */


/*
 * ========================== GLOBAL FUNCTIONS ================================
 */

void sqlhostname ( SQL_NODEID        host,
                   ERRORTEXT         errtext,
                   tsp_comm_error    *returncode )
  {
  #undef  MF__
  #define MF__ MOD__"sqlhostname"
  LONG            rc = NO_ERROR;
  SQL_NODEIDC     szHostName;

  rc = sql43c_get_my_host_name ( szHostName, sizeof(SQL_NODEID) );

  if ( rc != NO_ERROR )
    {
    sql46c_build_error_string ( errtext, ERRMSG_GET_HOST_NAME, rc );
    *returncode = SQLNOTOK;
    }
  else
    {
    sql47c_ctop ( host, szHostName, sizeof(SQL_NODEID) );
    *returncode = SQLOK;
    }

  DBGOUT;
  return;
  }

/*------------------------------*/

void sqlnodename ( SQL_NODEID        host,
                   SQL_NODEID        node,
                   ERRORTEXT         errtext,
                   tsp_comm_error    *returncode )
  {
  #undef  MF__
  #define MF__ MOD__"sqlnodename"
  LONG            rc = NO_ERROR;
  SQL_NODEIDC     szHostName;
  SQL_DBNAMEC     szNodeName;

  DBGIN;

  sql47c_ptoc ( szHostName, host, sizeof(SQL_NODEID) );

  rc = sql43c_get_official_node_name ( szHostName, szNodeName, sizeof(SQL_NODEID) );

  if ( rc != NO_ERROR )
    {
    sql46c_build_error_string ( errtext, ERRMSG_GET_OFFICIAL_NODE_NAME, rc );
    *returncode = SQLNOTOK;
    }
  else
    {
    sql47c_ctop ( node, szNodeName, sizeof(SQL_NODEID) );
    *returncode = SQLOK;
    }

  DBGOUT;
  return;
  }

/*------------------------------*/

BOOL sql43c_tcpip_init ( PRTE_VERSION_ID pTCPIPRTEVersion )
  {
  #undef  MF__
  #define MF__ MOD__"sql43c_tcpip_installed"
  ULONG            rc = NO_ERROR;
  RTE_VERSION_ID   RTEVersion;
  PRTE_VERSION_ID  pRTEVersion;
  #if !defined (USER)
   UCHAR           cMajorSockVersion;
   UCHAR           cMinorSockVersion;
  #endif

  DBGIN;

  if ( pTCPIPRTEVersion == NULL )
    pRTEVersion = &RTEVersion;   // - use local variable
  else
    pRTEVersion = pTCPIPRTEVersion;

  if ( fTCPIPInstalled )
    {
    DBGOUT;
    return (fTCPIPInstalled);
    }

  rc = sql40c_get_dll_version ( pRTEVersion );

  if (( rc                 != NO_ERROR )              ||
      ( sql02_this_RTE_version_is_older(pRTEVersion)) ||
      ( sql40c_sock_init() != 0 ))
    {
    DBG1 ((MF__, "TCP/IP 'sock_init' failed!"));
    fTCPIPInstalled = FALSE;
    }
  else
    {
    fTCPIPInstalled = TRUE;

    #if !defined (USER)
     sql40c_get_sock_version ( &cMajorSockVersion, &cMinorSockVersion );

     MSGCD (( INFO_WINSOCK_AND_DLL_VERSION, cMajorSockVersion,
                                            cMinorSockVersion,
                                            pRTEVersion->ucMajorVersion,
                                            pRTEVersion->ucMinorVersion,
                                            pRTEVersion->ucCorrLevel,
                                            pRTEVersion->ucPatchLevel ))
    #endif
    }

  DBGOUT;
  return (fTCPIPInstalled);
  }

/*------------------------------*/

VOID sql43c_tcpip_cleanup ( VOID )
  {
  #undef  MF__
  #define MF__ MOD__"sql43c_tcpip_cleanup"

  DBGIN;

  sql40c_sock_cleanup ();

  fTCPIPInstalled = FALSE;

  DBGOUT;
  return;
  }

/*------------------------------*/

LONG sql43c_get_my_host_name ( PCHAR  pszHostName, INT Size )
  {
  #undef  MF__
  #define MF__ MOD__"sql43c_get_my_host_name"
  PSZ             EnvResultPointer;
  LONG            rc = NO_ERROR;

  DBGIN;

  pszHostName[0]    = '\0';
  pszHostName[Size] = '\0';

  #if defined(_WIN32)
    if (!GetComputerName( pszHostName, &Size ))
      rc = GetLastError();

    EnvResultPointer = pszHostName;
  #else
    rc = DosScanEnv(HOSTNAME_ENV_VAR, &EnvResultPointer);
  #endif

  if ((rc != NO_ERROR) || (EnvResultPointer[0] == '\0'))
    {
    MSGD ((ERR_MISSING_HOSTNAME));
    DBG1 ((MF__, "'HOSTNAME' not found, rc = %d", rc));
    rc = -1;
    }
  else if (Size < (INT)strlen(EnvResultPointer))
    {
    MSGD ((ERR_HOSTNAME_TOO_LONG, Size));
    DBG1 ((MF__, "'HOSTNAME' too long, allowed are %u characters", Size));
    rc = -1;
    }
  else
    strncpy (pszHostName, EnvResultPointer, Size);


  DBGOUT;
  return (rc);
  }

/*------------------------------*/

LONG sql43c_get_official_node_name ( PCHAR pszHostName, PCHAR psNodeName, INT Size )
  {
  #undef  MF__
  #define MF__ MOD__"sql43c_get_official_node_name"
  LONG            rc = NO_ERROR;
  UCHAR           ucAddr[64];

  DBGIN;

  memset ( ucAddr, 0, sizeof(ucAddr) );

  rc = sql43c_get_host_by_name ( pszHostName, ucAddr, sizeof(ucAddr) );

  if ( rc == NO_ERROR )
    {
    rc = sql43c_get_host_by_address ( ucAddr, psNodeName, Size );
    }

  DBGOUT;
  return (rc);
  }

/*------------------------------*/

LONG sql43c_get_service_by_name ( PCHAR pszService, PUSHORT pusServicePort )
  {
  #undef  MF__
  #define MF__ MOD__"sql43c_get_service_by_name"
  LONG           rc = NO_ERROR;

  DBGIN;

  rc = sql40c_getservbyname ( pszService, pusServicePort );

  DBGOUT;
  return (rc);
  }

/*------------------------------*/

LONG  sql43c_get_host_by_name ( PCHAR pszHostName, PUCHAR pucAddr, INT Size )
  {
  #undef  MF__
  #define MF__ MOD__"sql43c_get_host_by_name"
  LONG          rc = NO_ERROR;

  DBGIN;


  memset ( pucAddr , 0 , Size );

  rc = sql40c_gethostbyname ( pszHostName, pucAddr, &Size );

  if ( rc != NO_ERROR )
    {
    if ( rc == EINVAL )
      {
      MSGD ((ERR_TCPIP_ADDR_TOO_LONG, Size));
      DBG1 ((MF__, "TCP/IP address too long, allowed size: %u ", Size));
      }
    else
      {
      DBG1 ((MF__, "TCP/IP host name '%s' not found", pszHostName));
      MSGD ((ERR_GET_HOST_BY_NAME, pszHostName));
      }
    }
  else
    {
    #ifdef DEBUG
    {
    INT     l , i ;
    CHAR    szDotAddr [ MX_DOT_STR ];

    for ( l = 0 , i = 0 ; i < Size - 1; i ++ )
      {
      sprintf ( szDotAddr + l , "%d." , pucAddr[ i ] );
      l += strlen  ( szDotAddr + l );
      }
    sprintf ( szDotAddr + l , "%d" , pucAddr[ i ] );

    DBG3((MF__, "The address of '%s' is: %s", pszHostName, szDotAddr));
    }
    #endif
    ;
    }

  DBGOUT;
  return (rc);
  }

/*------------------------------*/

LONG sql43c_get_host_by_address ( PUCHAR pucAddr, PCHAR pszHostName, INT Size )
  {
  #undef  MF__
  #define MF__ MOD__"sql43c_get_host_by_address"
  LONG             rc = NO_ERROR;
  INT              l , i;
  CHAR             szDotAddr [ MX_DOT_STR ];

  DBGIN;

  pszHostName[0]    = '\0';
  pszHostName[Size] = '\0';


  rc = sql40c_gethostbyaddr ( pszHostName, pucAddr, Size );

  if ( rc != NO_ERROR )
    {
    if ( rc == EINVAL )
      {
      MSGD ((ERR_TCPIP_HOSTNAME_TOO_LONG, Size));
      DBG1 ((MF__, "TCP/IP host name too long, allowed size: %u", Size));
      }
    else
      {
      for ( l = 0 , i = 0 ; i < INET_ADDR_SIZE - 1; i ++ )
        {
        sprintf ( szDotAddr + l , "%d.", pucAddr [ i ] & 0xFF  );
        l += strlen  ( szDotAddr + l );
        }
      sprintf ( szDotAddr + l , "%d", pucAddr [ i ] & 0xFF  );

      DBG1 ((MF__, "Error getting TCP/IP host by address: '%s'", szDotAddr));
      MSGD ((ERR_GET_HOST_BY_ADDR, szDotAddr));
      }
    }
  else
    {
    #ifdef DEBUG
    {
    for ( l = 0 , i = 0 ; i < INET_ADDR_SIZE - 1; i ++ )
      {
      sprintf ( szDotAddr + l , "%d." , pucAddr [ i ] & 0xFF );
      l += strlen  ( szDotAddr + l );
      }
    sprintf ( szDotAddr + l , "%d" , pucAddr [ i ] & 0xFF );
    DBG3((MF__, "The name of address %s is: '%s'", szDotAddr, pszHostName));
    }
    #endif
    ;
    }

  DBGOUT;
  return (rc);
  }

/*
 * ========================== LOCAL FUNCTIONS =================================
 */

.CM *-END-* code ----------------------------------------
.SP 2
***********************************************************
.PA
