Wednesday, September 7, 2011

inet_makeaddr in linux


using inet_makeaddr / inet_makeaddr example

With the functions inet_lnaof(3) and inet_netof(3), you have the ability to extract host and network ID values. To re-create a consolidated IP address with the network and host ID values
combined, you need to use the inet_makeaddr(3) function. Its function synopsis is as follows:

#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

struct in_addr inet_makeaddr(int net,int host);

The arguments to the inet_makeaddr(3) function are described as follows:
  1. The net argument is the network ID, right-justified and in host-endian order. This same value is returned from the function inet_netof(3). 
  2. The host argument is the host ID, which is in host-endian order. This same value is returned from the function inet_lnaof(3).
The value returned in the struct in_addr member of the sockaddr_in socket address. This value is in network-endian sequence, which is correct for the socket address.

A program has been provided in Listing 3.7 that uses the three functions inet_lnaof(3), inet_netof(3), and inet_makeaddr(3). The IP address is split apart from a sockaddr_in structure into its network and host ID parts. Then, the socket address is zeroed out and reconstructed from just the network and host ID parts.

Example

No comments:

Post a Comment