From kirk at 128bits.org Wed May 14 16:42:36 2003 From: kirk at 128bits.org (Kirk Bollinger) Date: Wed May 14 23:58:13 2003 Subject: [ds6-devel] gethostbyaddr() problem Message-ID: I'm not a programmer but I'm trying to learn and play with C and IPv6 at the same time so please don't laugh. Eventually I'd like to be able to contribute to some open source IPv6 projects. I initially wanted to modify traceroute6 so that when the probes return different ipv6 addresses but when they all resolve to the same thing it won't print the same names multiple times: ex: traceroute to orange.kame.net (3ffe:501:4819:2000:203:47ff:fea5:3085) from 2002:XXXX:XXXX:1:2e0:4cff:fe77:42f1, 30 hops max, 16 byte packets 3 unused.bbtransnet-range.ipv6.eurocyber.net (2001:768:f:4::1) 241.601 ms unused.bbtransnet-range.ipv6.eurocyber.net (2001:768:f:3::1) 244.956 ms unused.bbtransnet-range.ipv6.eurocyber.net (2001:768:f:4::1) 245.948 ms I found one reference that said gethostbyaddr didn't support AF_INET6 but other web sites seem to have it as supported. Here's what I have int rval; struct hostent *hp_one; char address[150]; char *hp1; struct sockaddr_in6 foo; char buf[64]; memset(&foo, 0, sizeof(foo)); hp1 = address; getAddress(address); printf("The address is: %s", address); if ((rval = inet_pton(AF_INET6, address, &foo)) > 0) { printf("\nSuccessful inet_pton() call"); if (inet_ntop(AF_INET6, &foo, buf, sizeof(buf)) != NULL) printf("\ninet6 addr: %s", buf); } else { printf("\nERROR: inet_pton() failed!"); } if ((hp_one = gethostbyaddr(&foo.sin6_addr, sizeof(&foo.sin6_addr), AF_INET6)) == NULL) { printf("\nERROR: gethostbyaddr() returned NULL!"); } else { *hp1 = hp_one->h_name; printf("\nhostname is: %s", hp_one->h_name); } the call to gethostbyaddr always returns NULL. here's the output: Enter ip address: ::1 The address is: ::1 Successful inet_pton() call inet6 addr: ::1 ERROR: gethostbyaddr() returned NULL! thanks! -kirk From chris at leishman.org Thu May 15 02:06:38 2003 From: chris at leishman.org (Chris Leishman) Date: Thu May 15 00:06:44 2003 Subject: [ds6-devel] gethostbyaddr() problem In-Reply-To: Message-ID: <55E37F5A-8658-11D7-84D2-003065F97418@leishman.org> On Thursday, May 15, 2003, at 01:42 AM, Kirk Bollinger wrote: > I found one reference that said gethostbyaddr didn't support AF_INET6 > but > other web sites seem to have it as supported. For IPv6 (and most host lookups) gethostbyaddr seems to be deprecated in favor of getaddrinfo and friends. You might like to take a look at those. Also, download the source for netcat6 and have a look in network.c for examples of getaddrinfo use. Regards, Chris From mauro at deepspace6.net Thu May 15 01:51:55 2003 From: mauro at deepspace6.net (Mauro Tortonesi) Date: Thu May 15 00:52:06 2003 Subject: [ds6-devel] gethostbyaddr() problem In-Reply-To: References: Message-ID: On Wed, 14 May 2003, Kirk Bollinger wrote: > I found one reference that said gethostbyaddr didn't support AF_INET6 but > other web sites seem to have it as supported. gethostbyaddr does __NOT__ support AF_INET6. you have to use getnameinfo, instead. see rfc2553 or the latest draft-ietf-ipngwg-rfc2553bis-*.txt at www.ietf.org. > Here's what I have [...] you should try to use getaddrinfo instead of inet_pton and getnameinfo instead of inet_ntop wherever possible. e.g.: char buf[256]; struct sockaddr_in6 sin6; struct addrinfo hints, *res; inet_ntop(AF_INET6, &sin6, buf, sizeof(buf) - 1); buf[sizeof(buf) - 1] = '\0'; becomes: getnameinfo(&sin6, sizeof(sin6), buf, sizeof(buf) - 1, NULL, 0, NI_NUMERICHOST); buf[sizeof(buf) - 1] = '\0'; and: inet_pton(AF_INET6, buf, &sin6); becomes: getaddrinfo(buf, "ftp", &hints, &res); the address you were looking for is in: ((struct sockaddr_in6 *)res->ai_addr)->sin6_addr; -- Aequam memento rebus in arduis servare mentem... Mauro Tortonesi mauro@deepspace6.net mauro@ferrara.linux.it Deep Space 6 - IPv6 on Linux http://www.deepspace6.net Ferrara Linux Users Group http://www.ferrara.linux.it From kirk at 128bits.org Wed May 28 13:30:44 2003 From: kirk at 128bits.org (Kirk Bollinger) Date: Wed May 28 21:35:32 2003 Subject: [ds6-devel] gethostbyaddr() problem In-Reply-To: Message-ID: On Thu, 15 May 2003, Mauro Tortonesi wrote: > inet_ntop(AF_INET6, &sin6, buf, sizeof(buf) - 1); > buf[sizeof(buf) - 1] = '\0'; > > becomes: > > getnameinfo(&sin6, sizeof(sin6), buf, sizeof(buf) - 1, > NULL, 0, NI_NUMERICHOST); > buf[sizeof(buf) - 1] = '\0'; > > > and: > > > inet_pton(AF_INET6, buf, &sin6); > > becomes: > > getaddrinfo(buf, "ftp", &hints, &res); > > the address you were looking for is in: > > ((struct sockaddr_in6 *)res->ai_addr)->sin6_addr; thanks for the help! now, I have one more thing I can't get to work. I want to print out the addresses in ascii. I know that inet_ntop() is discouraged but it should work. here's my code: if (res->ai_family == AF_INET6) { printf("\nAddress family: AF_INET6"); //printf("\nThe address is: %s", ((struct sockaddr_in6 *)res->ai_addr)->sin6_addr); printf("\nnet_byte_order in hex is: %x", ((struct sockaddr_in6 *)res->ai_addr)->sin6_addr); memset(&sin6, 0, sizeof(struct sockaddr_in6)); memcpy(&sin6, (struct sockaddr_in6 *)res->ai_addr, sizeof(struct sockaddr_in6)); inet_ntop(AF_INET6, &sin6.sin6_addr, buf, 16); printf("\nAddress is: %s", buf); here's the output I'm getting: Address family: AF_INET6 net_byte_order in hex is: 105fe3f Address is: I'm having fun playing and learning. -kirk From chris at leishman.org Wed May 28 23:48:32 2003 From: chris at leishman.org (Chris Leishman) Date: Wed May 28 21:48:38 2003 Subject: [ds6-devel] gethostbyaddr() problem In-Reply-To: Message-ID: <5CDF9572-9145-11D7-AA52-003065F97418@leishman.org> On Wednesday, May 28, 2003, at 10:30 PM, Kirk Bollinger wrote: > now, I have one more thing I can't get to work. I want to print out the > addresses in ascii. I know that inet_ntop() is discouraged but it > should > work. > > here's my code: > if (res->ai_family == AF_INET6) > { > printf("\nAddress family: AF_INET6"); > //printf("\nThe address is: %s", ((struct sockaddr_in6 > *)res->ai_addr)->sin6_addr); > > printf("\nnet_byte_order in hex is: %x", ((struct sockaddr_in6 > *)res->ai_addr)->sin6_addr); > memset(&sin6, 0, sizeof(struct sockaddr_in6)); > memcpy(&sin6, (struct sockaddr_in6 *)res->ai_addr, > sizeof(struct > sockaddr_in6)); > inet_ntop(AF_INET6, &sin6.sin6_addr, buf, 16); > > printf("\nAddress is: %s", buf); The function your looking for is 'getnameinfo'. If you want only the numeric name (IP address), then give it the AI_NUMERICHOST and/or the AI_NUMERICSERV flags. With the getaddrinfo family of functions you should never need to actually do any manipulations of the actual sockets, including doing any switch like statements on the family (unless your doing really esoteric stuff like comparing sockets). So your code would become something like: char hbuf[NI_MAXHOST+1]; char sbuf[NI_MAXSERV+1]; err = getnameinfo(socket, socklen, hbuf, sizeof(hbuf), sbuf, sizeof(sbuf), 0); /* handle err */ printf("address is %s %s\n", hbuf, sbuf); err = getnameinfo(socket, socklen, buf, sizeof(hbuf), sbuf, sizeof(sbuf), NI_NUMERICHOST | NI_NUMERICSERV); /* handle err */ printf("ip address is %s %s\n", hbuf, sbuf); If your trying to print out the results of getaddrinfo, then use 'res->ai_addr' for socket and 'res->ai_addrlen' for socklen. Regards, Chris From simone at deepspace6.net Wed May 28 22:50:08 2003 From: simone at deepspace6.net (simone@deepspace6.net) Date: Wed May 28 21:50:26 2003 Subject: R: Re: [ds6-devel] gethostbyaddr() problem Message-ID: <20030528195009.20EB4D7970@atropo.wseurope.com> IIRC the last parameter should be sizeof(buf) instead of 16 and buf should be large enough to hold the representation, e.g. at least 38 bytes... Check the return value to see if conversion succeded. Bye -- Sent from my wireless blackberry handheld. -----Original Message----- From: Kirk Bollinger To: ds6-devel@deepspace6.net Sent: Wed May 28 21:30:44 2003 Subject: Re: [ds6-devel] gethostbyaddr() problem On Thu, 15 May 2003, Mauro Tortonesi wrote: > inet_ntop(AF_INET6, &sin6, buf, sizeof(buf) - 1); > buf[sizeof(buf) - 1] = '\0'; > > becomes: > > getnameinfo(&sin6, sizeof(sin6), buf, sizeof(buf) - 1, > NULL, 0, NI_NUMERICHOST); > buf[sizeof(buf) - 1] = '\0'; > > > and: > > > inet_pton(AF_INET6, buf, &sin6); > > becomes: > > getaddrinfo(buf, "ftp", &hints, &res); > > the address you were looking for is in: > > ((struct sockaddr_in6 *)res->ai_addr)->sin6_addr; thanks for the help! now, I have one more thing I can't get to work. I want to print out the addresses in ascii. I know that inet_ntop() is discouraged but it should work. here's my code: if (res->ai_family == AF_INET6) { printf("\nAddress family: AF_INET6"); //printf("\nThe address is: %s", ((struct sockaddr_in6 *)res->ai_addr)->sin6_addr); printf("\nnet_byte_order in hex is: %x", ((struct sockaddr_in6 *)res->ai_addr)->sin6_addr); memset(&sin6, 0, sizeof(struct sockaddr_in6)); memcpy(&sin6, (struct sockaddr_in6 *)res->ai_addr, sizeof(struct sockaddr_in6)); inet_ntop(AF_INET6, &sin6.sin6_addr, buf, 16); printf("\nAddress is: %s", buf); here's the output I'm getting: Address family: AF_INET6 net_byte_order in hex is: 105fe3f Address is: I'm having fun playing and learning. -kirk _______________________________________________ ds6-devel mailing list ds6-devel@deepspace6.net http://lists.deepspace6.net/listinfo/ds6-devel