Unix Network Programming Manual
Miscellaneous Utilities
There are a number of useful utilities for operating in the network
environment.
char *inet_ntoa (struct in_addr) - convert an address to dotted
decimal format.
struct in_addr inet_addr (char *) - convert a dotted character
address to its integer form.
struct in_addr inet_network (char *) - convert a dotted character
address to its network byte order integer form.
unsigned int inet_netof (struct in_addr) - convert an address integer
to the network portion in unsigned integer form.
unsigned int inet_lnaof (struct in_addr) - convert an address integer
to the host portion in unsigned integer form.
struct in_addr inet_makeaddr (struct in_addr, struct in_addr) - convert
a network and a host address pair into an network byte order address.
An example program using these functions is shown below with the corresponding
output.
#include <stdio.h>
#include <arpa/inet.h>
#include <netdb.h>
void main ()
{
struct hostent *hostlist;
unsigned int addr_int, addr_net, addr_host;
char *addr_ch;
struct in_addr addr;
hostlist = gethostbyname ("esus.cs.montana.edu");
addr.s_addr = *(unsigned int *)hostlist -> h_addr_list [0];
addr_ch = inet_ntoa (addr);
printf ("%s\n", addr_ch);
addr_int = inet_addr (addr_ch);
printf ("%u\n", addr_int);
addr_int = inet_addr (addr_ch);
printf ("%u\n", addr_int);
addr_net = inet_netof (addr);
addr_ch = inet_ntoa (addr_net);
printf ("%s\n", addr_ch);
addr_host = inet_lnaof (addr);
addr_ch = inet_ntoa (addr_host);
printf ("%s\n", addr_ch);
addr = inet_makeaddr (addr_net, addr_host);
addr_ch = inet_ntoa (addr);
printf ("%s\n", addr_ch);
}
153.90.192.1
29383321
29383321
90.153.0.0
1.192.0.0
153.90.192.1