/* Incomplete implementation, single ipv4 addr, service does not work, hints do not work */ int tds_getaddrinfo(const char *node, const char *service, const struct tds_addrinfo *hints, struct tds_addrinfo **res) { struct tds_addrinfo *addr; struct sockaddr_in *sin = NULL; struct hostent *host; in_addr_t ipaddr; char buffer[4096]; struct hostent result; int h_errnop, port = 0; assert(node != NULL); if ((addr = (tds_addrinfo *) calloc(1, sizeof(struct tds_addrinfo))) == NULL) goto Cleanup; if ((sin = (struct sockaddr_in *) calloc(1, sizeof(struct sockaddr_in))) == NULL) goto Cleanup; addr->ai_addr = (struct sockaddr *) sin; addr->ai_addrlen = sizeof(struct sockaddr_in); addr->ai_family = AF_INET; if ((ipaddr = inet_addr(node)) == INADDR_NONE) { if ((host = tds_gethostbyname_r(node, &result, buffer, sizeof(buffer), &h_errnop)) == NULL) goto Cleanup; if (host->h_name) addr->ai_canonname = strdup(host->h_name); ipaddr = *(in_addr_t *) host->h_addr; } if (service) { port = atoi(service); if (!port) port = tds_getservice(service); } sin->sin_family = AF_INET; sin->sin_addr.s_addr = ipaddr; sin->sin_port = htons(port); *res = addr; return 0; Cleanup: if (addr != NULL) tds_freeaddrinfo(addr); return -1; }
/* TODO callers seem to set always connection info... change it */ void tds_lookup_host(const char *servername, /* (I) name of the server */ char *ip /* (O) dotted-decimal ip address of server */ ) { /* (O) port number of the service */ struct hostent *host = NULL; unsigned int ip_addr = 0; /* Storage for reentrant getaddrby* calls */ struct hostent result; char buffer[4096]; int h_errnop; /* 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) { strncpy(ip, servername, 17); return; } host = tds_gethostbyname_r(servername, &result, buffer, sizeof(buffer), &h_errnop); ip[0] = '\0'; if (host) { struct in_addr *ptr = (struct in_addr *) host->h_addr; #if defined(AF_INET) && HAVE_INET_NTOP inet_ntop(AF_INET, ptr, ip, 17); #elif HAVE_INET_NTOA_R inet_ntoa_r(*ptr, ip, 17); #else strncpy(ip, inet_ntoa(*ptr), 17); #endif } } /* tds_lookup_host() */