michael@0: /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #ifndef prnetdb_h___ michael@0: #define prnetdb_h___ michael@0: michael@0: #include "prtypes.h" michael@0: #include "prio.h" michael@0: michael@0: PR_BEGIN_EXTERN_C michael@0: michael@0: michael@0: /* michael@0: ********************************************************************* michael@0: * Translate an Internet address to/from a character string michael@0: ********************************************************************* michael@0: */ michael@0: NSPR_API(PRStatus) PR_StringToNetAddr( michael@0: const char *string, PRNetAddr *addr); michael@0: michael@0: NSPR_API(PRStatus) PR_NetAddrToString( michael@0: const PRNetAddr *addr, char *string, PRUint32 size); michael@0: michael@0: /* michael@0: ** Structures returned by network data base library. All addresses are michael@0: ** supplied in host order, and returned in network order (suitable for michael@0: ** use in system calls). michael@0: */ michael@0: /* michael@0: ** Beware that WINSOCK.H defines h_addrtype and h_length as short. michael@0: ** Client code does direct struct copies of hostent to PRHostEnt and michael@0: ** hence the ifdef. michael@0: */ michael@0: typedef struct PRHostEnt { michael@0: char *h_name; /* official name of host */ michael@0: char **h_aliases; /* alias list */ michael@0: #ifdef WIN32 michael@0: PRInt16 h_addrtype; /* host address type */ michael@0: PRInt16 h_length; /* length of address */ michael@0: #else michael@0: PRInt32 h_addrtype; /* host address type */ michael@0: PRInt32 h_length; /* length of address */ michael@0: #endif michael@0: char **h_addr_list; /* list of addresses from name server */ michael@0: } PRHostEnt; michael@0: michael@0: /* A safe size to use that will mostly work... */ michael@0: #if (defined(AIX) && defined(_THREAD_SAFE)) || defined(OSF1) michael@0: #define PR_NETDB_BUF_SIZE sizeof(struct protoent_data) michael@0: #else michael@0: #define PR_NETDB_BUF_SIZE 1024 michael@0: #endif michael@0: michael@0: /*********************************************************************** michael@0: ** FUNCTION: michael@0: ** DESCRIPTION: PR_GetHostByName() michael@0: ** Lookup a host by name. michael@0: ** michael@0: ** INPUTS: michael@0: ** char *hostname Character string defining the host name of interest michael@0: ** char *buf A scratch buffer for the runtime to return result. michael@0: ** This buffer is allocated by the caller. michael@0: ** PRIntn bufsize Number of bytes in 'buf'. A recommnded value to michael@0: ** use is PR_NETDB_BUF_SIZE. michael@0: ** OUTPUTS: michael@0: ** PRHostEnt *hostentry michael@0: ** This structure is filled in by the runtime if michael@0: ** the function returns PR_SUCCESS. This structure michael@0: ** is allocated by the caller. michael@0: ** RETURN: michael@0: ** PRStatus PR_SUCCESS if the lookup succeeds. If it fails michael@0: ** the result will be PR_FAILURE and the reason michael@0: ** for the failure can be retrieved by PR_GetError(). michael@0: ***********************************************************************/ michael@0: NSPR_API(PRStatus) PR_GetHostByName( michael@0: const char *hostname, char *buf, PRIntn bufsize, PRHostEnt *hostentry); michael@0: michael@0: /*********************************************************************** michael@0: ** FUNCTION: michael@0: ** DESCRIPTION: PR_GetIPNodeByName() michael@0: ** Lookup a host by name. Equivalent to getipnodebyname(AI_DEFAULT) michael@0: ** of RFC 2553. michael@0: ** michael@0: ** INPUTS: michael@0: ** char *hostname Character string defining the host name of interest michael@0: ** PRUint16 af Address family (either PR_AF_INET or PR_AF_INET6) michael@0: ** PRIntn flags Specifies the types of addresses that are searched michael@0: ** for and the types of addresses that are returned. michael@0: ** The only supported flag is PR_AI_DEFAULT. michael@0: ** char *buf A scratch buffer for the runtime to return result. michael@0: ** This buffer is allocated by the caller. michael@0: ** PRIntn bufsize Number of bytes in 'buf'. A recommnded value to michael@0: ** use is PR_NETDB_BUF_SIZE. michael@0: ** OUTPUTS: michael@0: ** PRHostEnt *hostentry michael@0: ** This structure is filled in by the runtime if michael@0: ** the function returns PR_SUCCESS. This structure michael@0: ** is allocated by the caller. michael@0: ** RETURN: michael@0: ** PRStatus PR_SUCCESS if the lookup succeeds. If it fails michael@0: ** the result will be PR_FAILURE and the reason michael@0: ** for the failure can be retrieved by PR_GetError(). michael@0: ***********************************************************************/ michael@0: michael@0: michael@0: #define PR_AI_ALL 0x08 michael@0: #define PR_AI_V4MAPPED 0x10 michael@0: #define PR_AI_ADDRCONFIG 0x20 michael@0: #define PR_AI_NOCANONNAME 0x8000 michael@0: #define PR_AI_DEFAULT (PR_AI_V4MAPPED | PR_AI_ADDRCONFIG) michael@0: michael@0: NSPR_API(PRStatus) PR_GetIPNodeByName( michael@0: const char *hostname, michael@0: PRUint16 af, michael@0: PRIntn flags, michael@0: char *buf, michael@0: PRIntn bufsize, michael@0: PRHostEnt *hostentry); michael@0: michael@0: /*********************************************************************** michael@0: ** FUNCTION: michael@0: ** DESCRIPTION: PR_GetHostByAddr() michael@0: ** Lookup a host entry by its network address. michael@0: ** michael@0: ** INPUTS: michael@0: ** char *hostaddr IP address of host in question michael@0: ** char *buf A scratch buffer for the runtime to return result. michael@0: ** This buffer is allocated by the caller. michael@0: ** PRIntn bufsize Number of bytes in 'buf'. A recommnded value to michael@0: ** use is PR_NETDB_BUF_SIZE. michael@0: ** OUTPUTS: michael@0: ** PRHostEnt *hostentry michael@0: ** This structure is filled in by the runtime if michael@0: ** the function returns PR_SUCCESS. This structure michael@0: ** is allocated by the caller. michael@0: ** RETURN: michael@0: ** PRStatus PR_SUCCESS if the lookup succeeds. If it fails michael@0: ** the result will be PR_FAILURE and the reason michael@0: ** for the failure can be retrieved by PR_GetError(). michael@0: ***********************************************************************/ michael@0: NSPR_API(PRStatus) PR_GetHostByAddr( michael@0: const PRNetAddr *hostaddr, char *buf, PRIntn bufsize, PRHostEnt *hostentry); michael@0: michael@0: /*********************************************************************** michael@0: ** FUNCTION: PR_EnumerateHostEnt() michael@0: ** DESCRIPTION: michael@0: ** A stateless enumerator over a PRHostEnt structure acquired from michael@0: ** PR_GetHostByName() PR_GetHostByAddr() to evaluate the possible michael@0: ** network addresses. michael@0: ** michael@0: ** INPUTS: michael@0: ** PRIntn enumIndex Index of the enumeration. The enumeration starts michael@0: ** and ends with a value of zero. michael@0: ** michael@0: ** PRHostEnt *hostEnt A pointer to a host entry struct that was michael@0: ** previously returned by PR_GetHostByName() or michael@0: ** PR_GetHostByAddr(). michael@0: ** michael@0: ** PRUint16 port The port number to be assigned as part of the michael@0: ** PRNetAddr. michael@0: ** michael@0: ** OUTPUTS: michael@0: ** PRNetAddr *address A pointer to an address structure that will be michael@0: ** filled in by the call to the enumeration if the michael@0: ** result of the call is greater than zero. michael@0: ** michael@0: ** RETURN: michael@0: ** PRIntn The value that should be used for the next call michael@0: ** of the enumerator ('enumIndex'). The enumeration michael@0: ** is ended if this value is returned zero. michael@0: ** If a value of -1 is returned, the enumeration michael@0: ** has failed. The reason for the failure can be michael@0: ** retrieved by calling PR_GetError(). michael@0: ***********************************************************************/ michael@0: NSPR_API(PRIntn) PR_EnumerateHostEnt( michael@0: PRIntn enumIndex, const PRHostEnt *hostEnt, PRUint16 port, PRNetAddr *address); michael@0: michael@0: /*********************************************************************** michael@0: ** FUNCTION: PR_InitializeNetAddr(), michael@0: ** DESCRIPTION: michael@0: ** Initialize the fields of a PRNetAddr, assigning well known values as michael@0: ** appropriate. michael@0: ** michael@0: ** INPUTS michael@0: ** PRNetAddrValue val The value to be assigned to the IP Address portion michael@0: ** of the network address. This can only specify the michael@0: ** special well known values that are equivalent to michael@0: ** INADDR_ANY and INADDR_LOOPBACK. michael@0: ** michael@0: ** PRUint16 port The port number to be assigned in the structure. michael@0: ** michael@0: ** OUTPUTS: michael@0: ** PRNetAddr *addr The address to be manipulated. michael@0: ** michael@0: ** RETURN: michael@0: ** PRStatus To indicate success or failure. If the latter, the michael@0: ** reason for the failure can be retrieved by calling michael@0: ** PR_GetError(); michael@0: ***********************************************************************/ michael@0: typedef enum PRNetAddrValue michael@0: { michael@0: PR_IpAddrNull, /* do NOT overwrite the IP address */ michael@0: PR_IpAddrAny, /* assign logical INADDR_ANY to IP address */ michael@0: PR_IpAddrLoopback, /* assign logical INADDR_LOOPBACK */ michael@0: PR_IpAddrV4Mapped /* IPv4 mapped address */ michael@0: } PRNetAddrValue; michael@0: michael@0: NSPR_API(PRStatus) PR_InitializeNetAddr( michael@0: PRNetAddrValue val, PRUint16 port, PRNetAddr *addr); michael@0: michael@0: /*********************************************************************** michael@0: ** FUNCTION: PR_SetNetAddr(), michael@0: ** DESCRIPTION: michael@0: ** Set the fields of a PRNetAddr, assigning well known values as michael@0: ** appropriate. This function is similar to PR_InitializeNetAddr michael@0: ** but differs in that the address family is specified. michael@0: ** michael@0: ** INPUTS michael@0: ** PRNetAddrValue val The value to be assigned to the IP Address portion michael@0: ** of the network address. This can only specify the michael@0: ** special well known values that are equivalent to michael@0: ** INADDR_ANY and INADDR_LOOPBACK. michael@0: ** michael@0: ** PRUint16 af The address family (either PR_AF_INET or PR_AF_INET6) michael@0: ** michael@0: ** PRUint16 port The port number to be assigned in the structure. michael@0: ** michael@0: ** OUTPUTS: michael@0: ** PRNetAddr *addr The address to be manipulated. michael@0: ** michael@0: ** RETURN: michael@0: ** PRStatus To indicate success or failure. If the latter, the michael@0: ** reason for the failure can be retrieved by calling michael@0: ** PR_GetError(); michael@0: ***********************************************************************/ michael@0: NSPR_API(PRStatus) PR_SetNetAddr( michael@0: PRNetAddrValue val, PRUint16 af, PRUint16 port, PRNetAddr *addr); michael@0: michael@0: /*********************************************************************** michael@0: ** FUNCTION: michael@0: ** DESCRIPTION: PR_IsNetAddrType() michael@0: ** Determine if the network address is of the specified type. michael@0: ** michael@0: ** INPUTS: michael@0: ** const PRNetAddr *addr A network address. michael@0: ** PRNetAddrValue The type of network address michael@0: ** michael@0: ** RETURN: michael@0: ** PRBool PR_TRUE if the network address is of the michael@0: ** specified type, else PR_FALSE. michael@0: ***********************************************************************/ michael@0: NSPR_API(PRBool) PR_IsNetAddrType(const PRNetAddr *addr, PRNetAddrValue val); michael@0: michael@0: /*********************************************************************** michael@0: ** FUNCTION: michael@0: ** DESCRIPTION: PR_ConvertIPv4AddrToIPv6() michael@0: ** Convert an IPv4 addr to an (IPv4-mapped) IPv6 addr michael@0: ** michael@0: ** INPUTS: michael@0: ** PRUint32 v4addr IPv4 address michael@0: ** michael@0: ** OUTPUTS: michael@0: ** PRIPv6Addr *v6addr The converted IPv6 address michael@0: ** michael@0: ** RETURN: michael@0: ** void michael@0: ** michael@0: ***********************************************************************/ michael@0: NSPR_API(void) PR_ConvertIPv4AddrToIPv6(PRUint32 v4addr, PRIPv6Addr *v6addr); michael@0: michael@0: /*********************************************************************** michael@0: ** MACRO: michael@0: ** DESCRIPTION: PR_NetAddrFamily() michael@0: ** Get the 'family' field of a PRNetAddr union. michael@0: ** michael@0: ** INPUTS: michael@0: ** const PRNetAddr *addr A network address. michael@0: ** michael@0: ** RETURN: michael@0: ** PRUint16 The 'family' field of 'addr'. michael@0: ***********************************************************************/ michael@0: #define PR_NetAddrFamily(addr) ((addr)->raw.family) michael@0: michael@0: /*********************************************************************** michael@0: ** MACRO: michael@0: ** DESCRIPTION: PR_NetAddrInetPort() michael@0: ** Get the 'port' field of a PRNetAddr union. michael@0: ** michael@0: ** INPUTS: michael@0: ** const PRNetAddr *addr A network address. michael@0: ** michael@0: ** RETURN: michael@0: ** PRUint16 The 'port' field of 'addr'. michael@0: ***********************************************************************/ michael@0: #define PR_NetAddrInetPort(addr) \ michael@0: ((addr)->raw.family == PR_AF_INET6 ? (addr)->ipv6.port : (addr)->inet.port) michael@0: michael@0: /*********************************************************************** michael@0: ** FUNCTION: michael@0: ** DESCRIPTION: PR_GetProtoByName() michael@0: ** Lookup a protocol entry based on protocol's name michael@0: ** michael@0: ** INPUTS: michael@0: ** char *protocolname Character string of the protocol's name. michael@0: ** char *buf A scratch buffer for the runtime to return result. michael@0: ** This buffer is allocated by the caller. michael@0: ** PRIntn bufsize Number of bytes in 'buf'. A recommnded value to michael@0: ** use is PR_NETDB_BUF_SIZE. michael@0: ** OUTPUTS: michael@0: ** PRHostEnt *PRProtoEnt michael@0: ** This structure is filled in by the runtime if michael@0: ** the function returns PR_SUCCESS. This structure michael@0: ** is allocated by the caller. michael@0: ** RETURN: michael@0: ** PRStatus PR_SUCCESS if the lookup succeeds. If it fails michael@0: ** the result will be PR_FAILURE and the reason michael@0: ** for the failure can be retrieved by PR_GetError(). michael@0: ***********************************************************************/ michael@0: michael@0: typedef struct PRProtoEnt { michael@0: char *p_name; /* official protocol name */ michael@0: char **p_aliases; /* alias list */ michael@0: #ifdef WIN32 michael@0: PRInt16 p_num; /* protocol # */ michael@0: #else michael@0: PRInt32 p_num; /* protocol # */ michael@0: #endif michael@0: } PRProtoEnt; michael@0: michael@0: NSPR_API(PRStatus) PR_GetProtoByName( michael@0: const char* protocolname, char* buffer, PRInt32 bufsize, PRProtoEnt* result); michael@0: michael@0: /*********************************************************************** michael@0: ** FUNCTION: michael@0: ** DESCRIPTION: PR_GetProtoByNumber() michael@0: ** Lookup a protocol entry based on protocol's number michael@0: ** michael@0: ** INPUTS: michael@0: ** PRInt32 protocolnumber michael@0: ** Number assigned to the protocol. michael@0: ** char *buf A scratch buffer for the runtime to return result. michael@0: ** This buffer is allocated by the caller. michael@0: ** PRIntn bufsize Number of bytes in 'buf'. A recommnded value to michael@0: ** use is PR_NETDB_BUF_SIZE. michael@0: ** OUTPUTS: michael@0: ** PRHostEnt *PRProtoEnt michael@0: ** This structure is filled in by the runtime if michael@0: ** the function returns PR_SUCCESS. This structure michael@0: ** is allocated by the caller. michael@0: ** RETURN: michael@0: ** PRStatus PR_SUCCESS if the lookup succeeds. If it fails michael@0: ** the result will be PR_FAILURE and the reason michael@0: ** for the failure can be retrieved by PR_GetError(). michael@0: ***********************************************************************/ michael@0: NSPR_API(PRStatus) PR_GetProtoByNumber( michael@0: PRInt32 protocolnumber, char* buffer, PRInt32 bufsize, PRProtoEnt* result); michael@0: michael@0: /*********************************************************************** michael@0: ** FUNCTION: michael@0: ** DESCRIPTION: PR_GetAddrInfoByName() michael@0: ** Look up a host by name. Equivalent to getaddrinfo(host, NULL, ...) of michael@0: ** RFC 3493. michael@0: ** michael@0: ** INPUTS: michael@0: ** char *hostname Character string defining the host name of interest michael@0: ** PRUint16 af May be PR_AF_UNSPEC or PR_AF_INET. michael@0: ** PRIntn flags May be either PR_AI_ADDRCONFIG or michael@0: ** PR_AI_ADDRCONFIG | PR_AI_NOCANONNAME. Include michael@0: ** PR_AI_NOCANONNAME to suppress the determination of michael@0: ** the canonical name corresponding to hostname. michael@0: ** RETURN: michael@0: ** PRAddrInfo* Handle to a data structure containing the results michael@0: ** of the host lookup. Use PR_EnumerateAddrInfo to michael@0: ** inspect the PRNetAddr values stored in this object. michael@0: ** When no longer needed, this handle must be destroyed michael@0: ** with a call to PR_FreeAddrInfo. If a lookup error michael@0: ** occurs, then NULL will be returned. michael@0: ***********************************************************************/ michael@0: typedef struct PRAddrInfo PRAddrInfo; michael@0: michael@0: NSPR_API(PRAddrInfo*) PR_GetAddrInfoByName( michael@0: const char *hostname, PRUint16 af, PRIntn flags); michael@0: michael@0: /*********************************************************************** michael@0: ** FUNCTION: michael@0: ** DESCRIPTION: PR_FreeAddrInfo() michael@0: ** Destroy the PRAddrInfo handle allocated by PR_GetAddrInfoByName(). michael@0: ** michael@0: ** INPUTS: michael@0: ** PRAddrInfo *addrInfo michael@0: ** The handle resulting from a successful call to michael@0: ** PR_GetAddrInfoByName(). michael@0: ** RETURN: michael@0: ** void michael@0: ***********************************************************************/ michael@0: NSPR_API(void) PR_FreeAddrInfo(PRAddrInfo *addrInfo); michael@0: michael@0: /*********************************************************************** michael@0: ** FUNCTION: michael@0: ** DESCRIPTION: PR_EnumerateAddrInfo() michael@0: ** A stateless enumerator over a PRAddrInfo handle acquired from michael@0: ** PR_GetAddrInfoByName() to inspect the possible network addresses. michael@0: ** michael@0: ** INPUTS: michael@0: ** void *enumPtr Index pointer of the enumeration. The enumeration michael@0: ** starts and ends with a value of NULL. michael@0: ** const PRAddrInfo *addrInfo michael@0: ** The PRAddrInfo handle returned by a successful michael@0: ** call to PR_GetAddrInfoByName(). michael@0: ** PRUint16 port The port number to be assigned as part of the michael@0: ** PRNetAddr. michael@0: ** OUTPUTS: michael@0: ** PRNetAddr *result A pointer to an address structure that will be michael@0: ** filled in by the call to the enumeration if the michael@0: ** result of the call is not NULL. michael@0: ** RETURN: michael@0: ** void* The value that should be used for the next call michael@0: ** of the enumerator ('enumPtr'). The enumeration michael@0: ** is ended if this value is NULL. michael@0: ***********************************************************************/ michael@0: NSPR_API(void *) PR_EnumerateAddrInfo( michael@0: void *enumPtr, const PRAddrInfo *addrInfo, PRUint16 port, PRNetAddr *result); michael@0: michael@0: /*********************************************************************** michael@0: ** FUNCTION: michael@0: ** DESCRIPTION: PR_GetCanonNameFromAddrInfo() michael@0: ** Extracts the canonical name of the hostname passed to michael@0: ** PR_GetAddrInfoByName(). michael@0: ** michael@0: ** INPUTS: michael@0: ** const PRAddrInfo *addrInfo michael@0: ** The PRAddrInfo handle returned by a successful michael@0: ** call to PR_GetAddrInfoByName(). michael@0: ** RETURN: michael@0: ** const char * A const pointer to the canonical hostname stored michael@0: ** in the given PRAddrInfo handle. This pointer is michael@0: ** invalidated once the PRAddrInfo handle is destroyed michael@0: ** by a call to PR_FreeAddrInfo(). michael@0: ***********************************************************************/ michael@0: NSPR_API(const char *) PR_GetCanonNameFromAddrInfo( michael@0: const PRAddrInfo *addrInfo); michael@0: michael@0: /*********************************************************************** michael@0: ** FUNCTIONS: PR_ntohs, PR_ntohl, PR_ntohll, PR_htons, PR_htonl, PR_htonll michael@0: ** michael@0: ** DESCRIPTION: API entries for the common byte ordering routines. michael@0: ** michael@0: ** PR_ntohs 16 bit conversion from network to host michael@0: ** PR_ntohl 32 bit conversion from network to host michael@0: ** PR_ntohll 64 bit conversion from network to host michael@0: ** PR_htons 16 bit conversion from host to network michael@0: ** PR_htonl 32 bit conversion from host to network michael@0: ** PR_ntonll 64 bit conversion from host to network michael@0: ** michael@0: ***********************************************************************/ michael@0: NSPR_API(PRUint16) PR_ntohs(PRUint16); michael@0: NSPR_API(PRUint32) PR_ntohl(PRUint32); michael@0: NSPR_API(PRUint64) PR_ntohll(PRUint64); michael@0: NSPR_API(PRUint16) PR_htons(PRUint16); michael@0: NSPR_API(PRUint32) PR_htonl(PRUint32); michael@0: NSPR_API(PRUint64) PR_htonll(PRUint64); michael@0: michael@0: PR_END_EXTERN_C michael@0: michael@0: #endif /* prnetdb_h___ */