Created
September 3, 2014 16:54
-
-
Save digitalresistor/08c8c92df399495bcd7b to your computer and use it in GitHub Desktop.
Quick and dirty program to get addrinfo.
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
// compile: clang -o getaddrinfo getaddrinfo.c | |
#include <stdio.h> | |
#include <string.h> | |
#include <unistd.h> | |
#include <sys/types.h> | |
#include <sys/socket.h> | |
#include <netdb.h> | |
#include <errno.h> | |
#include <stdlib.h> | |
char * family_type(int af) { | |
switch (af) { | |
case AF_INET: | |
return "AF_INET"; | |
case AF_INET6: | |
return "AF_INET6"; | |
default: | |
return "UNKNOWN"; | |
} | |
} | |
int main(int argc, char* argv[]) { | |
struct addrinfo hints, *addrlist, *addr; | |
struct socket_links *head, *current; | |
memset(&hints, 0, sizeof(hints)); | |
// Ask for TCP | |
hints.ai_socktype = SOCK_STREAM; | |
// Any family works for us ... | |
hints.ai_family = AF_UNSPEC; | |
// Set some hints | |
hints.ai_flags = | |
AI_PASSIVE | // We want to use this with bind | |
AI_NUMERICSERV; // We only pass in numeric port numbers | |
int rv; | |
if ((rv = getaddrinfo(argv[1], "80", &hints, &addrlist)) != 0) { | |
fprintf(stderr, "getaddrinfo: %s", gai_strerror(rv)); | |
return 1; | |
} | |
for (addr = addrlist; addr != 0; addr = addr->ai_next) { | |
struct protoent *proto = getprotobynumber(addr->ai_protocol); | |
printf("Address family: %s (%d)\tSocket type: %s (%d)\tProtocol: %s (%d)\n", | |
family_type(addr->ai_family), | |
addr->ai_family, | |
addr->ai_socktype == SOCK_STREAM ? "SOCK_STREAM" : "other", | |
addr->ai_socktype, | |
proto->p_name, | |
addr->ai_protocol); | |
} | |
freeaddrinfo(addrlist); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment