Wednesday, September 7, 2011

inet_netof example


using inet_netof
The inet_netof(3) function is the companion to the inet_lnaof(3) function. The inet_netof(3) function returns the network ID instead of the host ID value. In all other respects, these functions are the same. The following lists the function synopsis:

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

unsigned long inet_netof(struct in_addr addr);

Again, the input is the struct in_addr member of the socket address sockaddr_in structure that you'll normally be working with. An example of its use is given as follows:

struct sockaddr_in addr; /* Socket Address */
unsigned long net_id; /* Network ID number */
net_id = inet_netof(addr.sin_addr);

Table below shows the same example IP numbers used in Table above Table below shows the values returned for the function inet_netof(3) function, however. Table Example Values Returned from inet_netof(3) (the Hexadecimal Values Are Host- Endian Ordered)

IP Number
Class
Hexadecimal
Dotted-Quad
44.135.86.12
A
0000002C
0.0.0.44
127.0.0.1
A
0000007F
0.0.0.127
172.16.23.95
B
0000AC10
0.0.172.16
192.168.9.1
C
00C0A809
0.192.168.9

You might find the values in Table 3.6 to be a bit of a surprise. These return values are the network bits shifted right, in order to eliminate the host ID bits. What you are left with is the right-justified network ID number.

NOTE
The return values from inet_netof(3) are right-justified. The host ID bits are shifted out.

Example

No comments:

Post a Comment