prnetdb.h 20 KB

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