inet_network Subroutine

Purpose

Converts an ASCII string containing an Internet network addressee in . (dot) notation to an Internet address number.

Library

Standard C Library (libc.a)

Syntax

#include <sys/socket.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
in_addr_t inet_network ( CharString)
register const char *CharString;

Description

The inet_network subroutine converts an ASCII string containing a valid Internet address using . (dot) notation (such as, 120.121.122.123) to an Internet address number formatted as an unsigned integer value. The inet_network subroutine returns an error value if the application does not provide an ASCII string containing a valid Internet address using . notation.

The input ASCII string must represent a valid Internet address number, as described in "TCP/IP addressing" in Networks and communication management. The input string must be terminated with a null terminator (0x00) or a space (0x30). The inet_network subroutine 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 subroutine 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 decimal) Output Value (in hex)
...1 1 0x00000001
.1.. 65536 0x00010000
1 1 0x1
0xFFFFFFFF 255 0x000000FF
1. 256 0x100
1.2.3.4 66048 0x010200
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
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 subroutine 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 subroutine 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 subroutine processes any nonconforming number as a class C address.

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

All applications containing the inet_network subroutine must be compiled with the _BSD macro set to a specific value. Acceptable values are 43 and 44. In addition, all socket applications must include the BSD libbsd.a library.

Parameters

Item Description
CharString Represents a string of characters in the Internet address form.

Return Values

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

For invalid input strings, the inet_network subroutine returns a value of -1.

Files

Item Description
/etc/hosts Contains host names.
/etc/networks                          Contains network names.