Created
June 27, 2011 10:03
-
-
Save bnoordhuis/1048609 to your computer and use it in GitHub Desktop.
uv_ip6_addr
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* I, the copyright holder of this work, hereby release it into the public domain. | |
* This applies worldwide. In case this is not legally possible, I grant any entity | |
* the right to use this work for any purpose, without any conditions, unless such | |
* conditions are required by law. | |
*/ | |
#include <sys/types.h> | |
#include <sys/socket.h> | |
#include <netdb.h> | |
#include <unistd.h> | |
struct sockaddr_in6 uv_ip6_addr(const char* host, int port) { | |
struct sockaddr_in6 sin; | |
struct addrinfo hints; | |
struct addrinfo* res; | |
int err; | |
memset(&sin, 0, sizeof sin); | |
memset(&hints, 0, sizeof hints); | |
/* Set protocol to SOCK_STREAM, stops getaddrinfo() from | |
* returning an address for each protocol. | |
*/ | |
hints.ai_protocol = SOCK_STREAM; | |
hints.ai_family = AF_INET6; | |
hints.ai_flags = AI_NUMERICHOST; | |
res = NULL; | |
err = getaddrinfo(host, NULL, &hints, &res); | |
if (err) { | |
/* TODO Signal error to caller. (But how?) */ | |
#if 0 | |
fprintf(stderr, "getaddrinfo(%s): %s\n", host, gai_strerror(err)); | |
#endif | |
sin.sin6_family = AF_UNSPEC; | |
} | |
else { | |
sin = *(struct sockaddr_in6 *) res->ai_addr; | |
sin.sin6_port = htons(port); | |
} | |
if (res != NULL) { | |
freeaddrinfo(res); | |
} | |
return sin; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment