NSPR Reference Previous Contents Next |
Network Address Types and Constants
Network Address Functions
The API described in this chapter recognizes the emergence of Internet Protocol Version 6 (IPv6). To facilitate the transition to IPv6, it is recommended that clients treat all structures containing network addresses as transparent objects and use the functions documented here to manipulate the information.
If used consistently, this API also eliminates the need to deal with the byte
ordering of network addresses. Typically, the only numeric declarations required
are the well-known port numbers that are part of the PRNetAddr
structure.
PRHostEnt
PRProtoEnt
PR_NETDB_BUF_SIZE
PR_GetHostByName
and PR_GetHostByAddr
and passed to PR_EnumerateHostEnt
.
Clients should avoid directly accessing any of the structure's fields.
#include <prnetdb.h>
typedef struct PRHostEnt {
char *h_name;
char **h_aliases;
#if defined(_WIN32)
PRInt16 h_addrtype;
PRInt16 h_length;
#else
PRInt32 h_addrtype;
PRInt32 h_length;
#endif
char **h_addr_list;
} PRHostEnt;
Use the network address functions to manipulate the PRHostEnt
structure. To
make the transition to IP version 6 easier, it's best to treat PRHostEnt
as an opaque
structure.
Note |
WINSOCK.H defines h_addrtype and h_length as a 16-bit field,
whereas other platforms treat it as a 32-bit field. The #ifdef in the
structure allows direct assignment of the PRHostEnt structure.
|
PR_GetProtoByName
and PR_GetProtoByNumber
.
#include <prnetdb.h>
typedef struct PRProtoEnt {
char *p_name;
char **p_aliases;
#if defined(_WIN32)
PRInt16 p_num;
#else
PRInt32 p_num;
#endif
} PRProtoEnt;
p_name |
Pointer to official protocol name.
|
p_aliases |
Pointer to a pointer to a list of aliases. The list is terminated with a
NULL entry.
|
p_num |
Protocol number.
|
PR_GetHostByName
,
PR_GetHostByAddr
, PR_GetProtoByName
, or PR_GetProtoByNumber
.
#include <prnetdb.h>
#if defined(AIX) || defined(OSF1)
#define PR_NETDB_BUF_SIZE sizeof(struct protoent_data)
#else
#define PR_NETDB_BUF_SIZE 1024
#endif
PR_InitializeNetAddr
facilitates the use of PRNetAddr
, the basic network
address structure, in a polymorphic manner. By using these functions with other
network address functions, clients can support either version 4 or version 6 of the
Internet Protocol transparently.
All NSPR functions that require PRNetAddr
as an argument accept either an IPv4 or
IPv6 version of the address.
#include <prnetdb.h>
PRStatus PR_InitializeNetAddr(
PRNetAddrValue val,
PRUint16 port,
PRNetAddr *addr);
val |
The value to be assigned to the IP Address portion of the network address.
This must be PR_IpAddrNull , PR_IpAddrAny , or
PR_IpAddrLoopback .
|
port |
The port number to be assigned in the network address structure. The
value is specified in host byte order.
|
addr |
A pointer to the PRNetAddr structure to be manipulated.
|
PR_SUCCESS
.
If unsuccessful, PR_FAILURE
. This may occur, for example, if the value of val
is
not within the ranges defined by PRNetAddrValue
. You can retrieve the reason
for the failure by calling PR_GetError
.
PR_InitializeNetAddr
allows the assignment of special network address values
and the port number, while also setting the state that indicates the version of the
address being used.
The special network address values are identified by the enum PRNetAddrValue
:
typedef enum PRNetAddrValue{
PR_IpAddrNull,
PR_IpAddrAny,
PR_IpAddrLoopback
} PRNetAddrValue;
The enum has the following enumerators:
#include <prnetdb.h>
PRStatus PR_StringToNetAddr(
const char *string,
PRNetAddr *addr);
string |
The string to be converted.
|
addr |
On output, the equivalent network address.
|
PR_SUCCESS
.
If unsuccessful, PR_FAILURE
. You can retrieve the reason for the failure by
calling PR_GetError
.
.
" notation. IPv6 addresses are indicated as strings using ":
" characters separating
octets, with numerous caveats for shortcutting (see RFC #1884). If the NSPR library
and the host are configured to support IPv6, both formats are supported.
Otherwise, use of anything other than IPv4 dotted notation results in an error.
#include <prnetdb.h>
PRStatus PR_NetAddrToString(
const PRNetAddr *addr,
char *string,
PRUint32 size);
addr |
A pointer to the network address to be converted.
|
string |
A buffer that will hold the converted string on output.
|
size |
The size of the result buffer (string ).
|
PR_SUCCESS
.
If unsuccessful, PR_FAILURE
. You can retrieve the reason for the failure by
calling PR_GetError
.
addr
) may be either an IPv4 or IPv6 address
structure, assuming that the NSPR library and the host system are both configured
to utilize IPv6 addressing. If addr
is an IPv4 address,
size
needs to be at least 16. If addr
is an
IPv6 address, size
needs to be at least 46.
PR_GetHostByName
PR_GetHostByAddr
PR_EnumerateHostEnt
#include <prnetdb.h>
PRStatus PR_GetHostByName(
const char *hostname,
char *buf,
PRIntn bufsize,
PRHostEnt *hostentry);
hostname |
The character string defining the host name of interest.
|
buf |
A pointer to a buffer, allocated by the caller, that is filled in
with host data on output. All of the pointers in the hostentry
structure point to data saved in this buffer. This buffer is
referenced by the runtime during a call to
PR_EnumerateHostEnt .
|
bufsize |
Number of bytes in the buf parameter. The buffer must be at
least PR_NETDB_BUF_SIZE bytes.
|
hostentry |
This structure is allocated by the caller. On output, this
structure is filled in by the runtime if the function returns
PR_SUCCESS .
|
PR_SUCCESS
.
If unsuccessful, PR_FAILURE
. You can retrieve the reason for the failure by
calling PR_GetError
.
#include <prnetdb.h>
PRStatus PR_GetHostByAddr(
const PRNetAddr *hostaddr,
char *buf,
PRIntn bufsize,
PRHostEnt *hostentry);
hostaddr |
A pointer to the IP address of host in question.
|
buf |
A pointer to a buffer, allocated by the caller, that is filled in
with host data on output. All of the pointers in the hostentry
structure point to data saved in this buffer. This buffer is
referenced by the runtime during a call to
PR_EnumerateHostEnt .
|
bufsize |
Number of bytes in the buf parameter. The buffer must be at
least PR_NETDB_BUF_SIZE bytes.
|
hostentry |
This structure is allocated by the caller. On output, this
structure is filled in by the runtime if the function returns
PR_SUCCESS .
|
PR_SUCCESS
.
If unsuccessful, PR_FAILURE
. You can retrieve the reason for the failure by
calling PR_GetError
.
PR_GetHostByAddr
is used to perform reverse lookups of network addresses. That
is, given a valid network address (of type PRNetAddr
), PR_GetHostByAddr
discovers the address' primary name, any aliases, and any other network addresses
for the same host.
PRHostEnt
structure, acquired from
PR_GetHostByName
or PR_GetHostByAddr
.
#include <prnetdb.h>
PRIntn PR_EnumerateHostEnt(
PRIntn enumIndex,
const PRHostEnt *hostEnt,
PRUint16 port,
PRNetAddr *address);
enumIndex |
The index of the enumeration. To begin an enumeration, this
argument is set to zero. To continue an enumeration (thereby
getting successive addresses from the host entry structure), the
value should be set to the function's last returned value. The
enumeration is complete when a value of zero is returned.
|
hostEnt |
A pointer to a PRHostEnt structure obtained from
PR_GetHostByName or PR_GetHostByAddr .
|
port |
The port number to be assigned as part of the PRNetAddr
structure. This parameter is not checked for validity.
|
address |
On input, a pointer to a PRNetAddr structure. On output, this
structure is filled in by the runtime if the result of the call is greater
than 0.
|
enumIndex
parameter for the next call of the enumerator. If the function
returns 0, the enumeration is ended.
If unsuccessful, the function returns -1. You can retrieve the reason for the
failure by calling PR_GetError
.
PR_EnumerateHostEnt
is a stateless enumerator. The principle input, the
PRHostEnt
structure, is not modified.
#include <prnetdb.h>
PRStatus PR_GetProtoByName(
const char* protocolname,
char* buffer,
PRInt32 bufsize,
PRProtoEnt* result);
protocolname |
A pointer to the character string of the protocol's name.
|
buffer |
A pointer to a scratch buffer for the runtime to return result.This
buffer is allocated by the caller.
|
bufsize |
Number of bytes in the buffer parameter. The buffer must
be at least PR_NETDB_BUF_SIZE bytes.
|
result |
On input, a pointer to a PRProtoEnt structure. On output, this
structure is filled in by the runtime if the function returns
PR_SUCCESS .
|
PR_SUCCESS
.
If unsuccessful, PR_FAILURE
. You can retrieve the reason for the failure by
calling PR_GetError
.
#include <prnetdb.h>
PRStatus PR_GetProtoByNumber(
PRInt32 protocolnumber,
char* buffer,
PRInt32 bufsize,
PRProtoEnt* result);
protocolnumber |
The number assigned to the protocol.
|
buffer |
A pointer to a scratch buffer for a temporary work area. This
buffer is allocated by the caller.
|
bufsize |
Number of bytes in the buffer parameter. You should specify
PR_NETDB_BUF_SIZE .
|
result |
On input, a pointer to a PRNetAddr structure. On output, this
structure is filled in by the runtime if the function returns
PR_SUCCESS .
|
PR_SUCCESS
.
If unsuccessful, PR_FAILURE
. You can retrieve the reason for the failure by
calling PR_GetError
.
#include <prnetdb.h>
PRStatus PR_SetIPv6Enable(PRBool itIs);
itIs |
PR_TRUE to turn on IPv6 addressing, PR_FALSE to turn it off.
|
PR_SUCCESS
.
If unsuccessful, PR_FAILURE
. You can retrieve the reason for the failure by
calling PR_GetError
. The error PR_PROTOCOL_NOT_SUPPORTED_ERROR
indicates that the protocol is not supported.
PR_SetIPv6Enable
to succeed, IPv6 must first be enabled for the host
platform. If IPv6 is not enabled for the host platform, PR_SetIPv6Enable
returns
PR_FAILURE
on any attempt to change the setting.
Last Updated May 18, 2001