From: Pat Thoyts Date: Wed, 24 Jun 2009 19:47:59 +0000 (+0100) Subject: Fixed some bugs in the unix version and added many more options X-Git-Url: http://privyetmir.co.uk/gitweb?a=commitdiff_plain;h=eba9dc981a52e57a40f96f429ee5b36e8e50abff;p=tclresolver Fixed some bugs in the unix version and added many more options --- diff --git a/unix/tcl_resolv.c b/unix/tcl_resolv.c index ed1e988..f65f960 100644 --- a/unix/tcl_resolv.c +++ b/unix/tcl_resolv.c @@ -22,20 +22,25 @@ #include static int -Resolv(const char *hostname, int ai_family, int ai_flags) +Resolv(const char *node, int ai_socktype, int ai_family, int ai_flags) { struct addrinfo hints; struct addrinfo *res, *resPtr; int r = 0; + const char *service = NULL; - memset(&hints, sizeof(hints), 0); + memset(&hints, 0, sizeof(hints)); + hints.ai_socktype = ai_socktype; hints.ai_family = ai_family; hints.ai_flags = ai_flags; - hints.ai_socktype = 0; - if (ai_flags | AI_PASSIVE && strlen(hostname) == 0) hostname = NULL; + if ((ai_flags & AI_PASSIVE) && strlen(node) == 0) { + node = NULL; + service = "0"; + hints.ai_flags |= AI_NUMERICSERV; + } - r = getaddrinfo(hostname, "", &hints, &res); + r = getaddrinfo(node, service, &hints, &res); if (r) { printf("error: "); printf(gai_strerror(r)); @@ -60,7 +65,8 @@ int main(int argc, char *const argv[]) { int ai_family = PF_UNSPEC; - int ai_flags = AI_CANONNAME; + int ai_flags = 0; + int ai_socktype = SOCK_STREAM; int n; for (n = 1; n < argc; ++n) { @@ -69,18 +75,29 @@ main(int argc, char *const argv[]) } else if (strncmp("-6", argv[n], 2) == 0) { ai_family = PF_INET6; } else if (strncmp("-passive", argv[n], 8) == 0) { - ai_flags = AI_PASSIVE; + ai_flags |= AI_PASSIVE; + } else if (strncmp("-canon", argv[n], 6) == 0) { + ai_flags |= AI_CANONNAME; + } else if (strncmp("-addrconfig", argv[n], 11) == 0) { + ai_flags |= AI_ADDRCONFIG; + } else if (strncmp("-v4mapped", argv[n], 9) == 0) { + ai_flags |= AI_V4MAPPED; + } else if (strncmp("-all", argv[n], 4) == 0) { + ai_flags |= AI_ALL | AI_V4MAPPED; + } else if (strncmp("-dgram", argv[n], 6) == 0) { + ai_socktype = SOCK_DGRAM; } else { - fprintf(stderr, "usage: tcl_resolv ?-4? ?-6?\n"); + fprintf(stderr, "usage: tcl_resolv ?-4? ?-6? ?-passive?" + " ?-canon? ?-addrconfig? ?-v4mapped? ?-all? ?-dgram?\n"); return 2; } } while (1) { - char buffer [NI_MAXHOST + 1]; + char buffer [NI_MAXHOST]; char *host, *p; - host = fgets(buffer, sizeof(buffer), stdin); + host = fgets(buffer, sizeof(buffer)-1, stdin); if (host == NULL) break; @@ -92,7 +109,7 @@ main(int argc, char *const argv[]) --p; *++p = 0; - Resolv(host, ai_family, ai_flags); + Resolv(host, ai_socktype, ai_family, ai_flags); } return 0; }