inet_network()

The inet_network() call constructs a network number from character strings representing numbers expressed in standard dotted-decimal notation.

Syntax

#include <types.h>
#include <arpa\inet.h>
u_long inet_network(cp)
char *cp;

Parameters

cp

Description

The inet_network() call converts an ASCII string containing a valid internet address using dotted-decimal notation (such as 120.121.122.123) to an internet address number formatted as an unsigned long value. The inet_network() call returns an error value if the application does not provide an ASCII string containing a valid internet address using dotted-decimal notation.

The input ASCII string must represent a valid internet address number, as described in Internet Address Formats. The input string must be terminated with a null terminator (0x00) or a space (0x30). The inet_network() call ignores characters that follow the terminating character.

The input string can express an internet address number in decimal, hexadecimal, or octal format. In hexadecimal format, the string must begin with 0x. The string must begin with 0 to indicate octal format. In decimal format, the string requires no prefix.

Each octet of the input string must be delimited from another by a period. The application can omit values between delimiters. The inet_network() call interprets missing values as 0.

The following examples show valid strings and their output values in both decimal and hexadecimal notation:

Examples of Valid Strings

 Input string           Output value (in       Output value (in hex)
                        decimal)

 ...1                   1                      0x00000001

 .1..                   65536                  0x00010000

 1                      256                    0x00000100

 0xFFFFFFFF             255                    0x000000FF

 1.                     16777216               0x01000000

 1.2.3.4                16909060               0x01020304

 0x01.0X2.03.004        16909060               0x01020304

 1.2. 3.4               16777218               0x01000002

 9999.1.1.1             251724033              0x0F010101

The following examples show invalid input strings and the reasons they are not valid:

Examples of Invalid Strings

 Input string     Reason string is not valid

 1.2.3.4.5        Excessive fields.

 1.2.3.4.         Excessive delimiters (and therefore fields).

 1,2              Bad delimiter.

 1p               String not terminated by null terminator nor
                  space.

 {empty string}   No field or delimiter present.

Typically, the value of each octet of an internet address cannot exceed 246. The inet_network() call can accept larger values, but it uses only the eight least significant bits for each field value. For example, if an application passes 0x1234567890.0xabcdef, the inet_network() call returns 37103 (0x000090EF).

The application must verify that the network ID and host ID for the internet address conform to class A, class B, or class C. The inet_makeaddr() call processes any nonconforming class of address as a class C address.

The inet_network() call does not check the pointer to the ASCII input string. The application must verify the validity of the address of the string.

Return Values

The network number is returned in host-byte order. The return value points to static data that subsequent API calls can modify.

For valid input strings, the inet_network() call returns an unsigned long value that comprises the bit patterns of the input fields concatenated together. The inet_network() call places the first pattern in the leftmost (most significant) position and appends subsequent patterns if they exist.

For invalid input strings, the inet_network() call returns a value of -1.

Related Calls


[Back: inet_netof()]
[Next: inet_ntoa()]