Beispiel #1
0
/**
 * Given a portname lookup the port.
 *
 * If we can't determine the port number then return 0.
 */
static int
tds_lookup_port(const char *portname)
{
	int num = atoi(portname);
	if (!num)
		num = tds_getservice(portname);
	return num;
}
Beispiel #2
0
/* 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;
}