#include <ctype.h>
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));
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) {
} 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;
--p;
*++p = 0;
- Resolv(host, ai_family, ai_flags);
+ Resolv(host, ai_socktype, ai_family, ai_flags);
}
return 0;
}