This is an example port to IPv6 of a simple HTTP client. This is not actually intended to be a particularly useful application; its purpose is merely to serve as a simple example of how IPv6 socket programming in C differs from IPv4. Download the three files below, and type "make" to build the two versions of the binary. Usage is "webdump[6] host [url]", where host is the name of the web server to query, and url is the optional non-default url to request from that host.
MakefileAs can be seen from the following diff of the relevant portions of these two source files, very little has changed to port to IPv6. Most notably, several data types have had a "6" appended to them. More significant, and less well-documented, is the replacement of the standard gethostbyname() function call with gethostbyname2(), which allows the caller to specify the address family being looked up. This trips some people up, because the gethostbyname(3) man page explicitly says that this function call also supports IPv6 addresses as arguments, but in point of fact it does not.
37c37
< struct sockaddr_in saddr; /* socket structure */
---
> struct sockaddr_in6 saddr; /* socket structure */
61c61
< if ((servdata = gethostbyname(host)) == NULL) {
---
> if ((servdata = gethostbyname2(host, AF_INET6)) == NULL) {
67,69c67,69
< memcpy(&saddr.sin_addr, servdata->h_addr, servdata->h_length);
< saddr.sin_family = AF_INET;
< saddr.sin_port = htons(80); /* HTTP port, currently hard-coded */
---
> memcpy(&saddr.sin6_addr, servdata->h_addr, servdata->h_length);
> saddr.sin6_family = AF_INET6;
> saddr.sin6_port = htons(80); /* HTTP port, currently hard-coded */
73c73
< if ((s = socket(PF_INET, SOCK_STREAM, 0)) < 0) {
---
> if ((s = socket(PF_INET6, SOCK_STREAM, 0)) < 0) {
This source code is released under the GNU General Public License.