Logo Search packages:      
Sourcecode: freetds version File versions

void tds_lookup_host ( const char *  servername,
const char *  portname,
char *  ip,
char *  port 
)

Given a servername and port name or number, lookup the hostname and service. The server ip will be stored in the string 'servername' in dotted-decimal notation. The service port number will be stored in string form in the 'port' parameter.

If we can't determine both the IP address and port number then 'ip' and 'port' will be set to empty strings.

Definition at line 676 of file config.c.

References tds_lookup_host().

Referenced by tds_lookup_host().

{                       /* (O) port number of the service          */
      struct hostent *host = NULL;
      struct servent *service = NULL;
      int num = 0;
      unsigned int ip_addr = 0;

      /* Storage for reentrant getaddrby* calls */
      struct hostent result;
      char buffer[4096];
      int h_errnop;

      /* Storage for reentrant getservbyname */
      struct servent serv_result;

      /* Only call gethostbyname if servername is not an ip address. 
       * This call take a while and is useless for an ip address.
       * mlilback 3/2/02 */
      ip_addr = inet_addr(servername);
      if (ip_addr == INADDR_NONE)
            host = tds_gethostbyname_r(servername, &result, buffer, sizeof(buffer), &h_errnop);

#ifndef NOREVERSELOOKUPS
/* froy@singleentry.com 12/21/2000 */
      if (host == NULL) {
      char addr[4];
      int a0, a1, a2, a3;

            sscanf(servername, "%d.%d.%d.%d", &a0, &a1, &a2, &a3);
            addr[0] = a0;
            addr[1] = a1;
            addr[2] = a2;
            addr[3] = a3;
            host = tds_gethostbyaddr_r(addr, 4, AF_INET, &result, buffer, sizeof(buffer), &h_errnop);
      }
/* end froy */
#endif
      if (!host) {
            /* if servername is ip, copy to ip. */
            if (INADDR_NONE != ip_addr)
                  strncpy(ip, servername, 17);
            else
                  ip[0] = '\0';
      } else {
      struct in_addr *ptr = (struct in_addr *) host->h_addr;

            strncpy(ip, inet_ntoa(*ptr), 17);
      }
      if (portname) {
            service = tds_getservbyname_r(portname, "tcp", &serv_result, buffer, sizeof(buffer));
            if (service == NULL) {
                  num = atoi(portname);
            } else {
                  num = ntohs(service->s_port);
            }
      }

      if (num == 0) {
            if (port)
                  port[0] = '\0';
      } else {
            sprintf(port, "%d", num);
      }
}                       /* tds_lookup_host()  */


Generated by  Doxygen 1.6.0   Back to index