inet_addr Subroutine
Purpose
Converts Internet addresses to Internet numbers.
Library
Standard C Library (libc.a)
Syntax
#include <sys/socketvar.h>
#include <netinet/in.h>
#include <arpa/inet.h>
in_addr_t inet_addr ( CharString)
register const char *CharString;
Description
The inet_addr subroutine converts an ASCII string containing a valid Internet address using dot notation into an Internet address number typed as an unsigned integer value. An example of dot notation is 120.121.5.123. The inet_addr subroutine returns an error value if the Internet address notation in the ASCII string supplied by the application is not valid.
When an application gives the inet_network subroutine a string containing an Internet address value without a delimiter, the inet_network subroutine returns the logical product of the value represented by the string and 0xFF. For any other Internet address, the subroutine returns an error value of -1 if the value of the fields exceeds the previously defined limits.
All applications containing the inet_addr 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.
Sample return values for each subroutine are as follows:
Application String | inet_addr Returns | inet_network Returns |
---|---|---|
0x1234567890abcdef 0x1234567890abcdef. 256.257.258.259 | 0x090abcdef 0xFFFFFFFF (= -1) 0xFFFFFFFF (= -1) | 0x000000ef 0x0000ef00 0x00010203 |
The ASCII string for the inet_addr subroutine must conform to the following format:
string::= field | field delimited_field^1-3 | delimited_field^1-3
delimited_field::= delimiter field | delimiter
delimiter::= .
field::= 0X | 0x | 0Xhexadecimal* | 0x hexadecimal* | decimal* | 0 octal
hexadecimal::= decimal |a|b|c|d|e|f|A|B|C|D|E|F
decimal::= octal |8|9
octal::= 0|1|2|3|4|5|6|7
- ^n indicates n repetitions of a pattern.
- ^n-m indicates n to m repetitions of a pattern.
- * indicates 0 or more repetitions of a pattern, up to environmental limits.
- The Backus Naur form (BNF) description states the space character, if one is used. Text indicates text, not a BNF symbol.
The inet_addr subroutine requires an application to terminate the string with a null terminator (0x00) or a space (0x30). The string is considered invalid if the application does not end it with a null terminator or a space. The subroutine ignores characters trailing a space.
The following describes the restrictions on the field values for the inet_addr subroutine:
Format | Field Restrictions (in decimal) |
---|---|
a | Value_a < 4,294,967,296 |
a.b | Value_a < 256; Value_b < 16,777,216 |
a.b.c | Value_a < 256; Value_b < 256; Value_c < 65536 |
a.b.c.d | Value_a < 256; Value_b < 256; Value_c < 256; Value_d < 256 |
Applications that use the inet_addr subroutine can enter field values exceeding these restrictions. The subroutine accepts the least significant bits up to an integer in length, then checks whether the truncated value exceeds the maximum field value. For example, if an application enters a field value of 0x1234567890 and the system uses 16 bits per integer, then the inet_addr subroutine uses bits 0 -15. The subroutine returns 0x34567890.
Applications can omit field values between delimiters. The inet_addr subroutine interprets empty fields as 0.
- The inet_addr subroutine does not check the pointer to the ASCII string. The user must ensure the validity of the address in the ASCII string.
- The application must verify that the network and host IDs for the Internet address conform to either a Class A, B, or C Internet address. The inet_attr subroutine processes any other number as a Class C address.
Parameters
Item | Description |
---|---|
CharString | Represents a string of characters in the Internet address form. |
Return Values
For valid input strings, the inet_addr subroutine returns an unsigned integer value comprised of the bit patterns of the input fields concatenated together. The subroutine places the first pattern in the most significant position and appends any subsequent patterns to the next most significant positions.
The inet_addr subroutine returns an error value of -1 for invalid strings.
Files
Item | Description |
---|---|
/etc/hosts | Contains host names. |
/etc/networks | Contains network names. |