![](https://csdnimg.cn/release/download_crawler_static/5342886/bg13.jpg)
IP Addresses, structs, and Data Munging 15
inet_ntop(AF_INET, &(sa.sin_addr), ip4, INET_ADDRSTRLEN);
printf("The IPv4 address is: %s\n", ip4);
// IPv6:
char ip6[INET6_ADDRSTRLEN]; // space to hold the IPv6 string
struct sockaddr_in6 sa6; // pretend this is loaded with something
inet_ntop(AF_INET6, &(sa6.sin6_addr), ip6, INET6_ADDRSTRLEN);
printf("The address is: %s\n", ip6);
When you call it, you'll pass the address type (IPv4 or IPv6), the address, a pointer to a string to hold
the result, and the maximum length of that string. (Two macros conveniently hold the size of the string you'll
need to hold the largest IPv4 or IPv6 address: INET_ADDRSTRLEN and INET6_ADDRSTRLEN.)
(Another quick note to mention once again the old way of doing things: the historical function to do this
conversion was called inet_ntoa(). It's also obsolete and won't work with IPv6.)
Lastly, these functions only work with numeric IP addresses—they won't do any nameserver DNS
lookup on a hostname, like “www.example.com”. You will use getaddrinfo() to do that, as you'll see later
on.
3.4.1. Private (Or Disconnected) Networks
Lots of places have a firewall that hides the network from the rest of the world for their own protection.
And often times, the firewall translates “internal” IP addresses to “external” (that everyone else in the world
knows) IP addresses using a process called Network Address Translation, or NAT.
Are you getting nervous yet? “Where's he going with all this weird stuff?”
Well, relax and buy yourself a non-alcoholic (or alcoholic) drink, because as a beginner, you don't even
have to worry about NAT, since it's done for you transparently. But I wanted to talk about the network behind
the firewall in case you started getting confused by the network numbers you were seeing.
For instance, I have a firewall at home. I have two static IPv4 addresses allocated to me by the DSL
company, and yet I have seven computers on the network. How is this possible? Two computers can't share
the same IP address, or else the data wouldn't know which one to go to!
The answer is: they don't share the same IP addresses. They are on a private network with 24 million IP
addresses allocated to it. They are all just for me. Well, all for me as far as anyone else is concerned. Here's
what's happening:
If I log into a remote computer, it tells me I'm logged in from 192.0.2.33 which is the public IP address
my ISP has provided to me. But if I ask my local computer what it's IP address is, it says 10.0.0.5. Who is
translating the IP address from one to the other? That's right, the firewall! It's doing NAT!
10.x.x.x is one of a few reserved networks that are only to be used either on fully disconnected networks,
or on networks that are behind firewalls. The details of which private network numbers are available for you
to use are outlined in RFC 1918
15
, but some common ones you'll see are 10.x.x.x and 192.168.x.x, where x is
0-255, generally. Less common is 172.y.x.x, where y goes between 16 and 31.
Networks behind a NATing firewall don't need to be on one of these reserved networks, but they
commonly are.
(Fun fact! My external IP address isn't really 192.0.2.33. The 192.0.2.x network is reserved for make-
believe “real” IP addresses to be used in documentation, just like this guide! Wowzers!)
IPv6 has private networks, too, in a sense. They'll start with fdxx: (or maybe in the future fcXX:), as
per RFC 4193
16
. NAT and IPv6 don't generally mix, however (unless you're doing the IPv6 to IPv4 gateway
thing which is beyond the scope of this document)—in theory you'll have so many addresses at your disposal
15. http://tools.ietf.org/html/rfc1918
16. http://tools.ietf.org/html/rfc4193