Skip to content

Instantly share code, notes, and snippets.

@mattn
Created May 21, 2009 13:45
Show Gist options
  • Save mattn/115468 to your computer and use it in GitHub Desktop.
Save mattn/115468 to your computer and use it in GitHub Desktop.
void
freeaddrinfo (struct addrinfo* info)
{
free(info->ai_addr);
free(info);
}
int
getaddrinfo (const char* host, const char* port, const struct addrinfo* hint, struct addrinfo** info)
{
struct hostent* hp;
struct protoent* pp;
hp = gethostbyname(host);
pp = getprotobyname(port);
*info = (struct addrinfo*)malloc(sizeof(struct addrinfo));
memcpy(*info, hint, sizeof(struct addrinfo));
(*info)->ai_family = (hint->ai_family == AF_UNSPEC ? AF_INET : hint->ai_family);
(*info)->ai_addrlen = sizeof(struct sockaddr_in);
(*info)->ai_addr = (struct sockaddr*)malloc(sizeof(struct sockaddr_in));
((struct sockaddr_in*)(*info)->ai_addr)->sin_family = (hint->ai_family == AF_UNSPEC ? AF_INET : hint->ai_family);
if (pp) ((struct sockaddr_in*)(*info)->ai_addr)->sin_port = pp->p_proto;
else ((struct sockaddr_in*)(*info)->ai_addr)->sin_port = htons(atol(port));
((struct sockaddr_in*)(*info)->ai_addr)->sin_addr.s_addr = ((struct in_addr *)(hp->h_addr))->s_addr;
(*info)->ai_next = NULL;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment