prnetdb.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. #ifndef prnetdb_h___
  6. #define prnetdb_h___
  7. #include "prtypes.h"
  8. #include "prio.h"
  9. PR_BEGIN_EXTERN_C
  10. /*
  11. *********************************************************************
  12. * Translate an Internet address to/from a character string
  13. *********************************************************************
  14. */
  15. NSPR_API(PRStatus) PR_StringToNetAddr(
  16. const char *string, PRNetAddr *addr);
  17. NSPR_API(PRStatus) PR_NetAddrToString(
  18. const PRNetAddr *addr, char *string, PRUint32 size);
  19. /*
  20. ** Structures returned by network data base library. All addresses are
  21. ** supplied in host order, and returned in network order (suitable for
  22. ** use in system calls).
  23. */
  24. /*
  25. ** Beware that WINSOCK.H defines h_addrtype and h_length as short.
  26. ** Client code does direct struct copies of hostent to PRHostEnt and
  27. ** hence the ifdef.
  28. */
  29. typedef struct PRHostEnt {
  30. char *h_name; /* official name of host */
  31. char **h_aliases; /* alias list */
  32. #ifdef WIN32
  33. PRInt16 h_addrtype; /* host address type */
  34. PRInt16 h_length; /* length of address */
  35. #else
  36. PRInt32 h_addrtype; /* host address type */
  37. PRInt32 h_length; /* length of address */
  38. #endif
  39. char **h_addr_list; /* list of addresses from name server */
  40. } PRHostEnt;
  41. /* A safe size to use that will mostly work... */
  42. #if (defined(AIX) && defined(_THREAD_SAFE))
  43. #define PR_NETDB_BUF_SIZE sizeof(struct protoent_data)
  44. #else
  45. #define PR_NETDB_BUF_SIZE 1024
  46. #endif
  47. /***********************************************************************
  48. ** FUNCTION:
  49. ** DESCRIPTION: PR_GetHostByName()
  50. ** Lookup a host by name.
  51. **
  52. ** INPUTS:
  53. ** char *hostname Character string defining the host name of interest
  54. ** char *buf A scratch buffer for the runtime to return result.
  55. ** This buffer is allocated by the caller.
  56. ** PRIntn bufsize Number of bytes in 'buf'. A recommnded value to
  57. ** use is PR_NETDB_BUF_SIZE.
  58. ** OUTPUTS:
  59. ** PRHostEnt *hostentry
  60. ** This structure is filled in by the runtime if
  61. ** the function returns PR_SUCCESS. This structure
  62. ** is allocated by the caller.
  63. ** RETURN:
  64. ** PRStatus PR_SUCCESS if the lookup succeeds. If it fails
  65. ** the result will be PR_FAILURE and the reason
  66. ** for the failure can be retrieved by PR_GetError().
  67. ***********************************************************************/
  68. NSPR_API(PRStatus) PR_GetHostByName(
  69. const char *hostname, char *buf, PRIntn bufsize, PRHostEnt *hostentry);
  70. /***********************************************************************
  71. ** FUNCTION:
  72. ** DESCRIPTION: PR_GetIPNodeByName()
  73. ** Lookup a host by name. Equivalent to getipnodebyname(AI_DEFAULT)
  74. ** of RFC 2553.
  75. **
  76. ** INPUTS:
  77. ** char *hostname Character string defining the host name of interest
  78. ** PRUint16 af Address family (either PR_AF_INET or PR_AF_INET6)
  79. ** PRIntn flags Specifies the types of addresses that are searched
  80. ** for and the types of addresses that are returned.
  81. ** The only supported flag is PR_AI_DEFAULT.
  82. ** char *buf A scratch buffer for the runtime to return result.
  83. ** This buffer is allocated by the caller.
  84. ** PRIntn bufsize Number of bytes in 'buf'. A recommnded value to
  85. ** use is PR_NETDB_BUF_SIZE.
  86. ** OUTPUTS:
  87. ** PRHostEnt *hostentry
  88. ** This structure is filled in by the runtime if
  89. ** the function returns PR_SUCCESS. This structure
  90. ** is allocated by the caller.
  91. ** RETURN:
  92. ** PRStatus PR_SUCCESS if the lookup succeeds. If it fails
  93. ** the result will be PR_FAILURE and the reason
  94. ** for the failure can be retrieved by PR_GetError().
  95. ***********************************************************************/
  96. #define PR_AI_ALL 0x08
  97. #define PR_AI_V4MAPPED 0x10
  98. #define PR_AI_ADDRCONFIG 0x20
  99. #define PR_AI_NOCANONNAME 0x8000
  100. #define PR_AI_DEFAULT (PR_AI_V4MAPPED | PR_AI_ADDRCONFIG)
  101. NSPR_API(PRStatus) PR_GetIPNodeByName(
  102. const char *hostname,
  103. PRUint16 af,
  104. PRIntn flags,
  105. char *buf,
  106. PRIntn bufsize,
  107. PRHostEnt *hostentry);
  108. /***********************************************************************
  109. ** FUNCTION:
  110. ** DESCRIPTION: PR_GetHostByAddr()
  111. ** Lookup a host entry by its network address.
  112. **
  113. ** INPUTS:
  114. ** char *hostaddr IP address of host in question
  115. ** char *buf A scratch buffer for the runtime to return result.
  116. ** This buffer is allocated by the caller.
  117. ** PRIntn bufsize Number of bytes in 'buf'. A recommnded value to
  118. ** use is PR_NETDB_BUF_SIZE.
  119. ** OUTPUTS:
  120. ** PRHostEnt *hostentry
  121. ** This structure is filled in by the runtime if
  122. ** the function returns PR_SUCCESS. This structure
  123. ** is allocated by the caller.
  124. ** RETURN:
  125. ** PRStatus PR_SUCCESS if the lookup succeeds. If it fails
  126. ** the result will be PR_FAILURE and the reason
  127. ** for the failure can be retrieved by PR_GetError().
  128. ***********************************************************************/
  129. NSPR_API(PRStatus) PR_GetHostByAddr(
  130. const PRNetAddr *hostaddr, char *buf, PRIntn bufsize, PRHostEnt *hostentry);
  131. /***********************************************************************
  132. ** FUNCTION: PR_EnumerateHostEnt()
  133. ** DESCRIPTION:
  134. ** A stateless enumerator over a PRHostEnt structure acquired from
  135. ** PR_GetHostByName() PR_GetHostByAddr() to evaluate the possible
  136. ** network addresses.
  137. **
  138. ** INPUTS:
  139. ** PRIntn enumIndex Index of the enumeration. The enumeration starts
  140. ** and ends with a value of zero.
  141. **
  142. ** PRHostEnt *hostEnt A pointer to a host entry struct that was
  143. ** previously returned by PR_GetHostByName() or
  144. ** PR_GetHostByAddr().
  145. **
  146. ** PRUint16 port The port number to be assigned as part of the
  147. ** PRNetAddr.
  148. **
  149. ** OUTPUTS:
  150. ** PRNetAddr *address A pointer to an address structure that will be
  151. ** filled in by the call to the enumeration if the
  152. ** result of the call is greater than zero.
  153. **
  154. ** RETURN:
  155. ** PRIntn The value that should be used for the next call
  156. ** of the enumerator ('enumIndex'). The enumeration
  157. ** is ended if this value is returned zero.
  158. ** If a value of -1 is returned, the enumeration
  159. ** has failed. The reason for the failure can be
  160. ** retrieved by calling PR_GetError().
  161. ***********************************************************************/
  162. NSPR_API(PRIntn) PR_EnumerateHostEnt(
  163. PRIntn enumIndex, const PRHostEnt *hostEnt, PRUint16 port, PRNetAddr *address);
  164. /***********************************************************************
  165. ** FUNCTION: PR_InitializeNetAddr(),
  166. ** DESCRIPTION:
  167. ** Initialize the fields of a PRNetAddr, assigning well known values as
  168. ** appropriate.
  169. **
  170. ** INPUTS
  171. ** PRNetAddrValue val The value to be assigned to the IP Address portion
  172. ** of the network address. This can only specify the
  173. ** special well known values that are equivalent to
  174. ** INADDR_ANY and INADDR_LOOPBACK.
  175. **
  176. ** PRUint16 port The port number to be assigned in the structure.
  177. **
  178. ** OUTPUTS:
  179. ** PRNetAddr *addr The address to be manipulated.
  180. **
  181. ** RETURN:
  182. ** PRStatus To indicate success or failure. If the latter, the
  183. ** reason for the failure can be retrieved by calling
  184. ** PR_GetError();
  185. ***********************************************************************/
  186. typedef enum PRNetAddrValue
  187. {
  188. PR_IpAddrNull, /* do NOT overwrite the IP address */
  189. PR_IpAddrAny, /* assign logical INADDR_ANY to IP address */
  190. PR_IpAddrLoopback, /* assign logical INADDR_LOOPBACK */
  191. PR_IpAddrV4Mapped /* IPv4 mapped address */
  192. } PRNetAddrValue;
  193. NSPR_API(PRStatus) PR_InitializeNetAddr(
  194. PRNetAddrValue val, PRUint16 port, PRNetAddr *addr);
  195. /***********************************************************************
  196. ** FUNCTION: PR_SetNetAddr(),
  197. ** DESCRIPTION:
  198. ** Set the fields of a PRNetAddr, assigning well known values as
  199. ** appropriate. This function is similar to PR_InitializeNetAddr
  200. ** but differs in that the address family is specified.
  201. **
  202. ** INPUTS
  203. ** PRNetAddrValue val The value to be assigned to the IP Address portion
  204. ** of the network address. This can only specify the
  205. ** special well known values that are equivalent to
  206. ** INADDR_ANY and INADDR_LOOPBACK.
  207. **
  208. ** PRUint16 af The address family (either PR_AF_INET or PR_AF_INET6)
  209. **
  210. ** PRUint16 port The port number to be assigned in the structure.
  211. **
  212. ** OUTPUTS:
  213. ** PRNetAddr *addr The address to be manipulated.
  214. **
  215. ** RETURN:
  216. ** PRStatus To indicate success or failure. If the latter, the
  217. ** reason for the failure can be retrieved by calling
  218. ** PR_GetError();
  219. ***********************************************************************/
  220. NSPR_API(PRStatus) PR_SetNetAddr(
  221. PRNetAddrValue val, PRUint16 af, PRUint16 port, PRNetAddr *addr);
  222. /***********************************************************************
  223. ** FUNCTION:
  224. ** DESCRIPTION: PR_IsNetAddrType()
  225. ** Determine if the network address is of the specified type.
  226. **
  227. ** INPUTS:
  228. ** const PRNetAddr *addr A network address.
  229. ** PRNetAddrValue The type of network address
  230. **
  231. ** RETURN:
  232. ** PRBool PR_TRUE if the network address is of the
  233. ** specified type, else PR_FALSE.
  234. ***********************************************************************/
  235. NSPR_API(PRBool) PR_IsNetAddrType(const PRNetAddr *addr, PRNetAddrValue val);
  236. /***********************************************************************
  237. ** FUNCTION:
  238. ** DESCRIPTION: PR_ConvertIPv4AddrToIPv6()
  239. ** Convert an IPv4 addr to an (IPv4-mapped) IPv6 addr
  240. **
  241. ** INPUTS:
  242. ** PRUint32 v4addr IPv4 address
  243. **
  244. ** OUTPUTS:
  245. ** PRIPv6Addr *v6addr The converted IPv6 address
  246. **
  247. ** RETURN:
  248. ** void
  249. **
  250. ***********************************************************************/
  251. NSPR_API(void) PR_ConvertIPv4AddrToIPv6(PRUint32 v4addr, PRIPv6Addr *v6addr);
  252. /***********************************************************************
  253. ** MACRO:
  254. ** DESCRIPTION: PR_NetAddrFamily()
  255. ** Get the 'family' field of a PRNetAddr union.
  256. **
  257. ** INPUTS:
  258. ** const PRNetAddr *addr A network address.
  259. **
  260. ** RETURN:
  261. ** PRUint16 The 'family' field of 'addr'.
  262. ***********************************************************************/
  263. #define PR_NetAddrFamily(addr) ((addr)->raw.family)
  264. /***********************************************************************
  265. ** MACRO:
  266. ** DESCRIPTION: PR_NetAddrInetPort()
  267. ** Get the 'port' field of a PRNetAddr union.
  268. **
  269. ** INPUTS:
  270. ** const PRNetAddr *addr A network address.
  271. **
  272. ** RETURN:
  273. ** PRUint16 The 'port' field of 'addr'.
  274. ***********************************************************************/
  275. #define PR_NetAddrInetPort(addr) \
  276. ((addr)->raw.family == PR_AF_INET6 ? (addr)->ipv6.port : (addr)->inet.port)
  277. /***********************************************************************
  278. ** FUNCTION:
  279. ** DESCRIPTION: PR_GetProtoByName()
  280. ** Lookup a protocol entry based on protocol's name
  281. **
  282. ** INPUTS:
  283. ** char *protocolname Character string of the protocol's name.
  284. ** char *buf A scratch buffer for the runtime to return result.
  285. ** This buffer is allocated by the caller.
  286. ** PRIntn bufsize Number of bytes in 'buf'. A recommnded value to
  287. ** use is PR_NETDB_BUF_SIZE.
  288. ** OUTPUTS:
  289. ** PRHostEnt *PRProtoEnt
  290. ** This structure is filled in by the runtime if
  291. ** the function returns PR_SUCCESS. This structure
  292. ** is allocated by the caller.
  293. ** RETURN:
  294. ** PRStatus PR_SUCCESS if the lookup succeeds. If it fails
  295. ** the result will be PR_FAILURE and the reason
  296. ** for the failure can be retrieved by PR_GetError().
  297. ***********************************************************************/
  298. typedef struct PRProtoEnt {
  299. char *p_name; /* official protocol name */
  300. char **p_aliases; /* alias list */
  301. #ifdef WIN32
  302. PRInt16 p_num; /* protocol # */
  303. #else
  304. PRInt32 p_num; /* protocol # */
  305. #endif
  306. } PRProtoEnt;
  307. NSPR_API(PRStatus) PR_GetProtoByName(
  308. const char* protocolname, char* buffer, PRInt32 bufsize, PRProtoEnt* result);
  309. /***********************************************************************
  310. ** FUNCTION:
  311. ** DESCRIPTION: PR_GetProtoByNumber()
  312. ** Lookup a protocol entry based on protocol's number
  313. **
  314. ** INPUTS:
  315. ** PRInt32 protocolnumber
  316. ** Number assigned to the protocol.
  317. ** char *buf A scratch buffer for the runtime to return result.
  318. ** This buffer is allocated by the caller.
  319. ** PRIntn bufsize Number of bytes in 'buf'. A recommnded value to
  320. ** use is PR_NETDB_BUF_SIZE.
  321. ** OUTPUTS:
  322. ** PRHostEnt *PRProtoEnt
  323. ** This structure is filled in by the runtime if
  324. ** the function returns PR_SUCCESS. This structure
  325. ** is allocated by the caller.
  326. ** RETURN:
  327. ** PRStatus PR_SUCCESS if the lookup succeeds. If it fails
  328. ** the result will be PR_FAILURE and the reason
  329. ** for the failure can be retrieved by PR_GetError().
  330. ***********************************************************************/
  331. NSPR_API(PRStatus) PR_GetProtoByNumber(
  332. PRInt32 protocolnumber, char* buffer, PRInt32 bufsize, PRProtoEnt* result);
  333. /***********************************************************************
  334. ** FUNCTION:
  335. ** DESCRIPTION: PR_GetAddrInfoByName()
  336. ** Look up a host by name. Equivalent to getaddrinfo(host, NULL, ...) of
  337. ** RFC 3493.
  338. **
  339. ** INPUTS:
  340. ** char *hostname Character string defining the host name of interest
  341. ** PRUint16 af May be PR_AF_UNSPEC or PR_AF_INET.
  342. ** PRIntn flags May be either PR_AI_ADDRCONFIG or
  343. ** PR_AI_ADDRCONFIG | PR_AI_NOCANONNAME. Include
  344. ** PR_AI_NOCANONNAME to suppress the determination of
  345. ** the canonical name corresponding to hostname.
  346. ** RETURN:
  347. ** PRAddrInfo* Handle to a data structure containing the results
  348. ** of the host lookup. Use PR_EnumerateAddrInfo to
  349. ** inspect the PRNetAddr values stored in this object.
  350. ** When no longer needed, this handle must be destroyed
  351. ** with a call to PR_FreeAddrInfo. If a lookup error
  352. ** occurs, then NULL will be returned.
  353. ***********************************************************************/
  354. typedef struct PRAddrInfo PRAddrInfo;
  355. NSPR_API(PRAddrInfo*) PR_GetAddrInfoByName(
  356. const char *hostname, PRUint16 af, PRIntn flags);
  357. /***********************************************************************
  358. ** FUNCTION:
  359. ** DESCRIPTION: PR_FreeAddrInfo()
  360. ** Destroy the PRAddrInfo handle allocated by PR_GetAddrInfoByName().
  361. **
  362. ** INPUTS:
  363. ** PRAddrInfo *addrInfo
  364. ** The handle resulting from a successful call to
  365. ** PR_GetAddrInfoByName().
  366. ** RETURN:
  367. ** void
  368. ***********************************************************************/
  369. NSPR_API(void) PR_FreeAddrInfo(PRAddrInfo *addrInfo);
  370. /***********************************************************************
  371. ** FUNCTION:
  372. ** DESCRIPTION: PR_EnumerateAddrInfo()
  373. ** A stateless enumerator over a PRAddrInfo handle acquired from
  374. ** PR_GetAddrInfoByName() to inspect the possible network addresses.
  375. **
  376. ** INPUTS:
  377. ** void *enumPtr Index pointer of the enumeration. The enumeration
  378. ** starts and ends with a value of NULL.
  379. ** const PRAddrInfo *addrInfo
  380. ** The PRAddrInfo handle returned by a successful
  381. ** call to PR_GetAddrInfoByName().
  382. ** PRUint16 port The port number to be assigned as part of the
  383. ** PRNetAddr.
  384. ** OUTPUTS:
  385. ** PRNetAddr *result A pointer to an address structure that will be
  386. ** filled in by the call to the enumeration if the
  387. ** result of the call is not NULL.
  388. ** RETURN:
  389. ** void* The value that should be used for the next call
  390. ** of the enumerator ('enumPtr'). The enumeration
  391. ** is ended if this value is NULL.
  392. ***********************************************************************/
  393. NSPR_API(void *) PR_EnumerateAddrInfo(
  394. void *enumPtr, const PRAddrInfo *addrInfo, PRUint16 port, PRNetAddr *result);
  395. /***********************************************************************
  396. ** FUNCTION:
  397. ** DESCRIPTION: PR_GetCanonNameFromAddrInfo()
  398. ** Extracts the canonical name of the hostname passed to
  399. ** PR_GetAddrInfoByName().
  400. **
  401. ** INPUTS:
  402. ** const PRAddrInfo *addrInfo
  403. ** The PRAddrInfo handle returned by a successful
  404. ** call to PR_GetAddrInfoByName().
  405. ** RETURN:
  406. ** const char * A const pointer to the canonical hostname stored
  407. ** in the given PRAddrInfo handle. This pointer is
  408. ** invalidated once the PRAddrInfo handle is destroyed
  409. ** by a call to PR_FreeAddrInfo().
  410. ***********************************************************************/
  411. NSPR_API(const char *) PR_GetCanonNameFromAddrInfo(
  412. const PRAddrInfo *addrInfo);
  413. /***********************************************************************
  414. ** FUNCTIONS: PR_ntohs, PR_ntohl, PR_ntohll, PR_htons, PR_htonl, PR_htonll
  415. **
  416. ** DESCRIPTION: API entries for the common byte ordering routines.
  417. **
  418. ** PR_ntohs 16 bit conversion from network to host
  419. ** PR_ntohl 32 bit conversion from network to host
  420. ** PR_ntohll 64 bit conversion from network to host
  421. ** PR_htons 16 bit conversion from host to network
  422. ** PR_htonl 32 bit conversion from host to network
  423. ** PR_ntonll 64 bit conversion from host to network
  424. **
  425. ***********************************************************************/
  426. NSPR_API(PRUint16) PR_ntohs(PRUint16);
  427. NSPR_API(PRUint32) PR_ntohl(PRUint32);
  428. NSPR_API(PRUint64) PR_ntohll(PRUint64);
  429. NSPR_API(PRUint16) PR_htons(PRUint16);
  430. NSPR_API(PRUint32) PR_htonl(PRUint32);
  431. NSPR_API(PRUint64) PR_htonll(PRUint64);
  432. PR_END_EXTERN_C
  433. #endif /* prnetdb_h___ */