Alex Clemmer is a computer programmer. Other programmers love Alex, excitedly describing him as "employed here" and "the boss's son".
Alex is also a Hacker School alum. Surely they do not at all regret admitting him!
(My batch ended on August 22, 2013, but as they say, never graduate.)
Networking is one of the classes I never had time for. Before today, I didn’t even have good answers for basic questions about IP addresses:
For starters, IP stands for Internet Protocol. This is the family of protocols used by the global IP Internet, which we typically refer to as “the Internet” — as in “the Internet with Google and Facebook and stuff.”
A protocol is just a definition for how the machines in the Internet talk to each other. e.g., “first send me a handshake message, then I send you an acknowledgement message…”, etc.
The global IP Internet (with an uppercase I) is an implementation of the general concept of internet (with a lowercase I). An internet is so-called because it is an interconnected network of routers and computers.
An IP address is an unsigned 32-bit integer used to identify a computer on the Internet.
In the backend, these IP addresses are stored in a C struct from netinet/in.h
(see line 137 of source, or manpage):
#include <netinet/in.h>
/* Internet address. */
typedef uint32_t in_addr_t;
struct in_addr
{
in_addr_t s_addr;
};
This data is always stored in network byte order, which is big-endian. This is always true, regardless of whether your machine is little-endian or not. Network byte order is also used to transmit any other integer data in the packet headers.
While an integer number is useful for computers, it’s harder for humans to deal with. In general, IP addresses are represented application-side in dotted-decimal notation. In this form, each byte in the IP address is printed as a decimal number, with each byte separated by a period. For example: the dotted-decimal representation of 0x8002c2f2
is 128.2.194.242
.
Linux systems define functions in arpa/inet.h
(see manpage) specifically for converting to and from dotted-decimal and integer representations of IP addresses:
#include <arpa/inet.h>
/* dotted-decimal -> network */
int inet_aton(const char *cp, struct in_addr *inp);
/* network -> dotted-decimal */
char *inet_ntoa(struct in_addr in);
Here ntoa
, read n
-to-a
, means “network to application”. The reverse is true for aton
.
So, how do you get an IP address? This part I’m not quite sure about. It looks like your ISP is typically allocated a range of IP addresses. Each machine, or host, on the Internet is given an IP address, and if you’re connected to an ISP, that is who assigns you your IP address. I heard that some ISPs will give you an address that might not be unique on the Internet, but will be unique on their network — not sure why they would do this, or if it’s true.
Here is some example code that uses the structs and functions above in practice. Wrote it for funsies, should give you an idea of how to use them in practice.