Wednesday, September 7, 2011

inet_lnaof example

using inet_lnaof
The inet_lnaof(3) function converts the IP number contained in a socket address, which is in network byte order, to a host ID number with the network ID removed. The return value is in hostendian order. This function saves you from having to determine the class of the IP number and then extracting the host ID portion. The function synopsis for inet_lnaof(3) is given as follows:

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

unsigned long inet_lnaof(struct in_addr addr);

The input argument addr must be the struct in_addr member of the socket address that you will normally be working with. This value will be in network byte sequence, which is what the function expects. An example of how to invoke the function using a sockaddr_in address is given as follows:

struct sockaddr_in addr; /* Socket Address */
unsigned long host_id;   /* Host ID number */
host_id = inet_lnaof(addr.sin_addr);

Table below shows some example values that can be supplied to the input of inet_lnaof(3) and the values that result. To make the reasons for the results clearer, the class of each example address is included in the table.

Example Values Returned from inet_lnaof(3) (the Hexadecimal values Are Host-Endian Ordered)

IP Number
Class
Hexadecimal
Dotted-Quad
44.135.86.12
A
0087560C
0.135.86.12
127.0.0.1
A
00000001
0.0.0.1
172.16.23.95
B
0000175F
0.0.23.95
192.168.9.1
C
00000001
0.0.0.1

You should notice in the table's class A examples only the first byte is zeroed in the returned result. In the class B example, the upper 16 bits are zeroed in the returned result. Finally, the class C example in Table above zeroes out the upper 3 bytes of the address, leaving the host ID behind in the last byte.

NOTE
The input is in network-endian sequence. The returned value is in host-endian sequence.

Example
inet_lnaof example c code

See Also

No comments:

Post a Comment