PageRenderTime 62ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/pjproject-0.9.0/pjlib/include/pj/sock.h

https://bitbucket.org/teluu/tabikphone
C Header | 1203 lines | 288 code | 156 blank | 759 comment | 6 complexity | 98ef561a6e5c41a0e34610eef5ccbb44 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.0, LGPL-2.1, BSD-3-Clause
  1. /* $Id: sock.h 2039 2008-06-20 22:44:47Z bennylp $ */
  2. /*
  3. * Copyright (C)2003-2008 Benny Prijono <benny@prijono.org>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. #ifndef __PJ_SOCK_H__
  20. #define __PJ_SOCK_H__
  21. /**
  22. * @file sock.h
  23. * @brief Socket Abstraction.
  24. */
  25. #include <pj/types.h>
  26. PJ_BEGIN_DECL
  27. /**
  28. * @defgroup PJ_SOCK Socket Abstraction
  29. * @ingroup PJ_IO
  30. * @{
  31. *
  32. * The PJLIB socket abstraction layer is a thin and very portable abstraction
  33. * for socket API. It provides API similar to BSD socket API. The abstraction
  34. * is needed because BSD socket API is not always available on all platforms,
  35. * therefore it wouldn't be possible to create a trully portable network
  36. * programs unless we provide such abstraction.
  37. *
  38. * Applications can use this API directly in their application, just
  39. * as they would when using traditional BSD socket API, provided they
  40. * call #pj_init() first.
  41. *
  42. * \section pj_sock_examples_sec Examples
  43. *
  44. * For some examples on how to use the socket API, please see:
  45. *
  46. * - \ref page_pjlib_sock_test
  47. * - \ref page_pjlib_select_test
  48. * - \ref page_pjlib_sock_perf_test
  49. */
  50. /**
  51. * Supported address families.
  52. * APPLICATION MUST USE THESE VALUES INSTEAD OF NORMAL AF_*, BECAUSE
  53. * THE LIBRARY WILL DO TRANSLATION TO THE NATIVE VALUE.
  54. */
  55. /** Address family is unspecified. @see pj_AF_UNSPEC() */
  56. extern const pj_uint16_t PJ_AF_UNSPEC;
  57. /** Unix domain socket. @see pj_AF_UNIX() */
  58. extern const pj_uint16_t PJ_AF_UNIX;
  59. /** POSIX name for AF_UNIX */
  60. #define PJ_AF_LOCAL PJ_AF_UNIX;
  61. /** Internet IP protocol. @see pj_AF_INET() */
  62. extern const pj_uint16_t PJ_AF_INET;
  63. /** IP version 6. @see pj_AF_INET6() */
  64. extern const pj_uint16_t PJ_AF_INET6;
  65. /** Packet family. @see pj_AF_PACKET() */
  66. extern const pj_uint16_t PJ_AF_PACKET;
  67. /** IRDA sockets. @see pj_AF_IRDA() */
  68. extern const pj_uint16_t PJ_AF_IRDA;
  69. /*
  70. * Accessor functions for various address family constants. These
  71. * functions are provided because Symbian doesn't allow exporting
  72. * global variables from a DLL.
  73. */
  74. #if defined(PJ_DLL)
  75. /** Get #PJ_AF_UNSPEC value */
  76. PJ_DECL(pj_uint16_t) pj_AF_UNSPEC(void);
  77. /** Get #PJ_AF_UNIX value. */
  78. PJ_DECL(pj_uint16_t) pj_AF_UNIX(void);
  79. /** Get #PJ_AF_INET value. */
  80. PJ_DECL(pj_uint16_t) pj_AF_INET(void);
  81. /** Get #PJ_AF_INET6 value. */
  82. PJ_DECL(pj_uint16_t) pj_AF_INET6(void);
  83. /** Get #PJ_AF_PACKET value. */
  84. PJ_DECL(pj_uint16_t) pj_AF_PACKET(void);
  85. /** Get #PJ_AF_IRDA value. */
  86. PJ_DECL(pj_uint16_t) pj_AF_IRDA(void);
  87. #else
  88. /* When pjlib is not built as DLL, these accessor functions are
  89. * simply a macro to get their constants
  90. */
  91. /** Get #PJ_AF_UNSPEC value */
  92. # define pj_AF_UNSPEC() PJ_AF_UNSPEC
  93. /** Get #PJ_AF_UNIX value. */
  94. # define pj_AF_UNIX() PJ_AF_UNIX
  95. /** Get #PJ_AF_INET value. */
  96. # define pj_AF_INET() PJ_AF_INET
  97. /** Get #PJ_AF_INET6 value. */
  98. # define pj_AF_INET6() PJ_AF_INET6
  99. /** Get #PJ_AF_PACKET value. */
  100. # define pj_AF_PACKET() PJ_AF_PACKET
  101. /** Get #PJ_AF_IRDA value. */
  102. # define pj_AF_IRDA() PJ_AF_IRDA
  103. #endif
  104. /**
  105. * Supported types of sockets.
  106. * APPLICATION MUST USE THESE VALUES INSTEAD OF NORMAL SOCK_*, BECAUSE
  107. * THE LIBRARY WILL TRANSLATE THE VALUE TO THE NATIVE VALUE.
  108. */
  109. /** Sequenced, reliable, connection-based byte streams.
  110. * @see pj_SOCK_STREAM() */
  111. extern const pj_uint16_t PJ_SOCK_STREAM;
  112. /** Connectionless, unreliable datagrams of fixed maximum lengths.
  113. * @see pj_SOCK_DGRAM() */
  114. extern const pj_uint16_t PJ_SOCK_DGRAM;
  115. /** Raw protocol interface. @see pj_SOCK_RAW() */
  116. extern const pj_uint16_t PJ_SOCK_RAW;
  117. /** Reliably-delivered messages. @see pj_SOCK_RDM() */
  118. extern const pj_uint16_t PJ_SOCK_RDM;
  119. /*
  120. * Accessor functions for various constants. These functions are provided
  121. * because Symbian doesn't allow exporting global variables from a DLL.
  122. */
  123. #if defined(PJ_DLL)
  124. /** Get #PJ_SOCK_STREAM constant */
  125. PJ_DECL(int) pj_SOCK_STREAM(void);
  126. /** Get #PJ_SOCK_DGRAM constant */
  127. PJ_DECL(int) pj_SOCK_DGRAM(void);
  128. /** Get #PJ_SOCK_RAW constant */
  129. PJ_DECL(int) pj_SOCK_RAW(void);
  130. /** Get #PJ_SOCK_RDM constant */
  131. PJ_DECL(int) pj_SOCK_RDM(void);
  132. #else
  133. /** Get #PJ_SOCK_STREAM constant */
  134. # define pj_SOCK_STREAM() PJ_SOCK_STREAM
  135. /** Get #PJ_SOCK_DGRAM constant */
  136. # define pj_SOCK_DGRAM() PJ_SOCK_DGRAM
  137. /** Get #PJ_SOCK_RAW constant */
  138. # define pj_SOCK_RAW() PJ_SOCK_RAW
  139. /** Get #PJ_SOCK_RDM constant */
  140. # define pj_SOCK_RDM() PJ_SOCK_RDM
  141. #endif
  142. /**
  143. * Socket level specified in #pj_sock_setsockopt() or #pj_sock_getsockopt().
  144. * APPLICATION MUST USE THESE VALUES INSTEAD OF NORMAL SOL_*, BECAUSE
  145. * THE LIBRARY WILL TRANSLATE THE VALUE TO THE NATIVE VALUE.
  146. */
  147. /** Socket level. @see pj_SOL_SOCKET() */
  148. extern const pj_uint16_t PJ_SOL_SOCKET;
  149. /** IP level. @see pj_SOL_IP() */
  150. extern const pj_uint16_t PJ_SOL_IP;
  151. /** TCP level. @see pj_SOL_TCP() */
  152. extern const pj_uint16_t PJ_SOL_TCP;
  153. /** UDP level. @see pj_SOL_UDP() */
  154. extern const pj_uint16_t PJ_SOL_UDP;
  155. /** IP version 6. @see pj_SOL_IPV6() */
  156. extern const pj_uint16_t PJ_SOL_IPV6;
  157. /*
  158. * Accessor functions for various constants. These functions are provided
  159. * because Symbian doesn't allow exporting global variables from a DLL.
  160. */
  161. #if defined(PJ_DLL)
  162. /** Get #PJ_SOL_SOCKET constant */
  163. PJ_DECL(pj_uint16_t) pj_SOL_SOCKET(void);
  164. /** Get #PJ_SOL_IP constant */
  165. PJ_DECL(pj_uint16_t) pj_SOL_IP(void);
  166. /** Get #PJ_SOL_TCP constant */
  167. PJ_DECL(pj_uint16_t) pj_SOL_TCP(void);
  168. /** Get #PJ_SOL_UDP constant */
  169. PJ_DECL(pj_uint16_t) pj_SOL_UDP(void);
  170. /** Get #PJ_SOL_IPV6 constant */
  171. PJ_DECL(pj_uint16_t) pj_SOL_IPV6(void);
  172. #else
  173. /** Get #PJ_SOL_SOCKET constant */
  174. # define pj_SOL_SOCKET() PJ_SOL_SOCKET
  175. /** Get #PJ_SOL_IP constant */
  176. # define pj_SOL_IP() PJ_SOL_IP
  177. /** Get #PJ_SOL_TCP constant */
  178. # define pj_SOL_TCP() PJ_SOL_TCP
  179. /** Get #PJ_SOL_UDP constant */
  180. # define pj_SOL_UDP() PJ_SOL_UDP
  181. /** Get #PJ_SOL_IPV6 constant */
  182. # define pj_SOL_IPV6() PJ_SOL_IPV6
  183. #endif
  184. /* IP_TOS
  185. *
  186. * Note:
  187. * TOS CURRENTLY DOES NOT WORK IN Windows 2000 and above!
  188. * See http://support.microsoft.com/kb/248611
  189. */
  190. /** IP_TOS optname in setsockopt(). @see pj_IP_TOS() */
  191. extern const pj_uint16_t PJ_IP_TOS;
  192. /*
  193. * IP TOS related constats.
  194. *
  195. * Note:
  196. * TOS CURRENTLY DOES NOT WORK IN Windows 2000 and above!
  197. * See http://support.microsoft.com/kb/248611
  198. */
  199. /** Minimize delays. @see pj_IPTOS_LOWDELAY() */
  200. extern const pj_uint16_t PJ_IPTOS_LOWDELAY;
  201. /** Optimize throughput. @see pj_IPTOS_THROUGHPUT() */
  202. extern const pj_uint16_t PJ_IPTOS_THROUGHPUT;
  203. /** Optimize for reliability. @see pj_IPTOS_RELIABILITY() */
  204. extern const pj_uint16_t PJ_IPTOS_RELIABILITY;
  205. /** "filler data" where slow transmission does't matter.
  206. * @see pj_IPTOS_MINCOST() */
  207. extern const pj_uint16_t PJ_IPTOS_MINCOST;
  208. #if defined(PJ_DLL)
  209. /** Get #PJ_IP_TOS constant */
  210. PJ_DECL(int) pj_IP_TOS(void);
  211. /** Get #PJ_IPTOS_LOWDELAY constant */
  212. PJ_DECL(int) pj_IPTOS_LOWDELAY(void);
  213. /** Get #PJ_IPTOS_THROUGHPUT constant */
  214. PJ_DECL(int) pj_IPTOS_THROUGHPUT(void);
  215. /** Get #PJ_IPTOS_RELIABILITY constant */
  216. PJ_DECL(int) pj_IPTOS_RELIABILITY(void);
  217. /** Get #PJ_IPTOS_MINCOST constant */
  218. PJ_DECL(int) pj_IPTOS_MINCOST(void);
  219. #else
  220. /** Get #PJ_IP_TOS constant */
  221. # define pj_IP_TOS() PJ_IP_TOS
  222. /** Get #PJ_IPTOS_LOWDELAY constant */
  223. # define pj_IPTOS_LOWDELAY() PJ_IP_TOS_LOWDELAY
  224. /** Get #PJ_IPTOS_THROUGHPUT constant */
  225. # define pj_IPTOS_THROUGHPUT() PJ_IP_TOS_THROUGHPUT
  226. /** Get #PJ_IPTOS_RELIABILITY constant */
  227. # define pj_IPTOS_RELIABILITY() PJ_IP_TOS_RELIABILITY
  228. /** Get #PJ_IPTOS_MINCOST constant */
  229. # define pj_IPTOS_MINCOST() PJ_IP_TOS_MINCOST
  230. #endif
  231. /**
  232. * Values to be specified as \c optname when calling #pj_sock_setsockopt()
  233. * or #pj_sock_getsockopt().
  234. */
  235. /** Socket type. @see pj_SO_TYPE() */
  236. extern const pj_uint16_t PJ_SO_TYPE;
  237. /** Buffer size for receive. @see pj_SO_RCVBUF() */
  238. extern const pj_uint16_t PJ_SO_RCVBUF;
  239. /** Buffer size for send. @see pj_SO_SNDBUF() */
  240. extern const pj_uint16_t PJ_SO_SNDBUF;
  241. #if defined(PJ_DLL)
  242. /** Get #PJ_SO_TYPE constant */
  243. PJ_DECL(pj_uint16_t) pj_SO_TYPE(void);
  244. /** Get #PJ_SO_RCVBUF constant */
  245. PJ_DECL(pj_uint16_t) pj_SO_RCVBUF(void);
  246. /** Get #PJ_SO_SNDBUF constant */
  247. PJ_DECL(pj_uint16_t) pj_SO_SNDBUF(void);
  248. #else
  249. /** Get #PJ_SO_TYPE constant */
  250. # define pj_SO_TYPE() PJ_SO_TYPE
  251. /** Get #PJ_SO_RCVBUF constant */
  252. # define pj_SO_RCVBUF() PJ_SO_RCVBUF
  253. /** Get #PJ_SO_SNDBUF constant */
  254. # define pj_SO_SNDBUF() PJ_SO_SNDBUF
  255. #endif
  256. /*
  257. * Flags to be specified in #pj_sock_recv, #pj_sock_send, etc.
  258. */
  259. /** Out-of-band messages. @see pj_MSG_OOB() */
  260. extern const int PJ_MSG_OOB;
  261. /** Peek, don't remove from buffer. @see pj_MSG_PEEK() */
  262. extern const int PJ_MSG_PEEK;
  263. /** Don't route. @see pj_MSG_DONTROUTE() */
  264. extern const int PJ_MSG_DONTROUTE;
  265. #if defined(PJ_DLL)
  266. /** Get #PJ_MSG_OOB constant */
  267. PJ_DECL(int) pj_MSG_OOB(void);
  268. /** Get #PJ_MSG_PEEK constant */
  269. PJ_DECL(int) pj_MSG_PEEK(void);
  270. /** Get #PJ_MSG_DONTROUTE constant */
  271. PJ_DECL(int) pj_MSG_DONTROUTE(void);
  272. #else
  273. /** Get #PJ_MSG_OOB constant */
  274. # define pj_MSG_OOB() PJ_MSG_OOB
  275. /** Get #PJ_MSG_PEEK constant */
  276. # define pj_MSG_PEEK() PJ_MSG_PEEK
  277. /** Get #PJ_MSG_DONTROUTE constant */
  278. # define pj_MSG_DONTROUTE() PJ_MSG_DONTROUTE
  279. #endif
  280. /**
  281. * Flag to be specified in #pj_sock_shutdown().
  282. */
  283. typedef enum pj_socket_sd_type
  284. {
  285. PJ_SD_RECEIVE = 0, /**< No more receive. */
  286. PJ_SHUT_RD = 0, /**< Alias for SD_RECEIVE. */
  287. PJ_SD_SEND = 1, /**< No more sending. */
  288. PJ_SHUT_WR = 1, /**< Alias for SD_SEND. */
  289. PJ_SD_BOTH = 2, /**< No more send and receive. */
  290. PJ_SHUT_RDWR = 2 /**< Alias for SD_BOTH. */
  291. } pj_socket_sd_type;
  292. /** Address to accept any incoming messages. */
  293. #define PJ_INADDR_ANY ((pj_uint32_t)0)
  294. /** Address indicating an error return */
  295. #define PJ_INADDR_NONE ((pj_uint32_t)0xffffffff)
  296. /** Address to send to all hosts. */
  297. #define PJ_INADDR_BROADCAST ((pj_uint32_t)0xffffffff)
  298. /**
  299. * Maximum length specifiable by #pj_sock_listen().
  300. * If the build system doesn't override this value, then the lowest
  301. * denominator (five, in Win32 systems) will be used.
  302. */
  303. #if !defined(PJ_SOMAXCONN)
  304. # define PJ_SOMAXCONN 5
  305. #endif
  306. /**
  307. * Constant for invalid socket returned by #pj_sock_socket() and
  308. * #pj_sock_accept().
  309. */
  310. #define PJ_INVALID_SOCKET (-1)
  311. /* Must undefine s_addr because of pj_in_addr below */
  312. #undef s_addr
  313. /**
  314. * This structure describes Internet address.
  315. */
  316. typedef struct pj_in_addr
  317. {
  318. pj_uint32_t s_addr; /**< The 32bit IP address. */
  319. } pj_in_addr;
  320. /**
  321. * Maximum length of text representation of an IPv4 address.
  322. */
  323. #define PJ_INET_ADDRSTRLEN 16
  324. /**
  325. * Maximum length of text representation of an IPv6 address.
  326. */
  327. #define PJ_INET6_ADDRSTRLEN 46
  328. /**
  329. * This structure describes Internet socket address.
  330. * If PJ_SOCKADDR_HAS_LEN is not zero, then sin_zero_len member is added
  331. * to this struct. As far the application is concerned, the value of
  332. * this member will always be zero. Internally, PJLIB may modify the value
  333. * before calling OS socket API, and reset the value back to zero before
  334. * returning the struct to application.
  335. */
  336. struct pj_sockaddr_in
  337. {
  338. #if defined(PJ_SOCKADDR_HAS_LEN) && PJ_SOCKADDR_HAS_LEN!=0
  339. pj_uint8_t sin_zero_len; /**< Just ignore this. */
  340. pj_uint8_t sin_family; /**< Address family. */
  341. #else
  342. pj_uint16_t sin_family; /**< Address family. */
  343. #endif
  344. pj_uint16_t sin_port; /**< Transport layer port number. */
  345. pj_in_addr sin_addr; /**< IP address. */
  346. char sin_zero[8]; /**< Padding. */
  347. };
  348. #undef s6_addr
  349. /**
  350. * This structure describes IPv6 address.
  351. */
  352. typedef union pj_in6_addr
  353. {
  354. /* This is the main entry */
  355. pj_uint8_t s6_addr[16]; /**< 8-bit array */
  356. /* While these are used for proper alignment */
  357. pj_uint32_t u6_addr32[4];
  358. /* Do not use this with Winsock2, as this will align pj_sockaddr_in6
  359. * to 64-bit boundary and Winsock2 doesn't like it!
  360. */
  361. #if defined(PJ_HAS_INT64) && PJ_HAS_INT64!=0 && \
  362. (!defined(PJ_WIN32) || PJ_WIN32==0)
  363. pj_int64_t u6_addr64[2];
  364. #endif
  365. } pj_in6_addr;
  366. /** Initializer value for pj_in6_addr. */
  367. #define PJ_IN6ADDR_ANY_INIT { { { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 } } }
  368. /** Initializer value for pj_in6_addr. */
  369. #define PJ_IN6ADDR_LOOPBACK_INIT { { { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1 } } }
  370. /**
  371. * This structure describes IPv6 socket address.
  372. * If PJ_SOCKADDR_HAS_LEN is not zero, then sin_zero_len member is added
  373. * to this struct. As far the application is concerned, the value of
  374. * this member will always be zero. Internally, PJLIB may modify the value
  375. * before calling OS socket API, and reset the value back to zero before
  376. * returning the struct to application.
  377. */
  378. typedef struct pj_sockaddr_in6
  379. {
  380. #if defined(PJ_SOCKADDR_HAS_LEN) && PJ_SOCKADDR_HAS_LEN!=0
  381. pj_uint8_t sin6_zero_len; /**< Just ignore this. */
  382. pj_uint8_t sin6_family; /**< Address family. */
  383. #else
  384. pj_uint16_t sin6_family; /**< Address family */
  385. #endif
  386. pj_uint16_t sin6_port; /**< Transport layer port number. */
  387. pj_uint32_t sin6_flowinfo; /**< IPv6 flow information */
  388. pj_in6_addr sin6_addr; /**< IPv6 address. */
  389. pj_uint32_t sin6_scope_id; /**< Set of interfaces for a scope */
  390. } pj_sockaddr_in6;
  391. /**
  392. * This structure describes common attributes found in transport addresses.
  393. * If PJ_SOCKADDR_HAS_LEN is not zero, then sa_zero_len member is added
  394. * to this struct. As far the application is concerned, the value of
  395. * this member will always be zero. Internally, PJLIB may modify the value
  396. * before calling OS socket API, and reset the value back to zero before
  397. * returning the struct to application.
  398. */
  399. typedef struct pj_addr_hdr
  400. {
  401. #if defined(PJ_SOCKADDR_HAS_LEN) && PJ_SOCKADDR_HAS_LEN!=0
  402. pj_uint8_t sa_zero_len;
  403. pj_uint8_t sa_family;
  404. #else
  405. pj_uint16_t sa_family; /**< Common data: address family. */
  406. #endif
  407. } pj_addr_hdr;
  408. /**
  409. * This union describes a generic socket address.
  410. */
  411. typedef union pj_sockaddr
  412. {
  413. pj_addr_hdr addr; /**< Generic transport address. */
  414. pj_sockaddr_in ipv4; /**< IPv4 transport address. */
  415. pj_sockaddr_in6 ipv6; /**< IPv6 transport address. */
  416. } pj_sockaddr;
  417. /*****************************************************************************
  418. *
  419. * SOCKET ADDRESS MANIPULATION.
  420. *
  421. *****************************************************************************
  422. */
  423. /**
  424. * Convert 16-bit value from network byte order to host byte order.
  425. *
  426. * @param netshort 16-bit network value.
  427. * @return 16-bit host value.
  428. */
  429. PJ_DECL(pj_uint16_t) pj_ntohs(pj_uint16_t netshort);
  430. /**
  431. * Convert 16-bit value from host byte order to network byte order.
  432. *
  433. * @param hostshort 16-bit host value.
  434. * @return 16-bit network value.
  435. */
  436. PJ_DECL(pj_uint16_t) pj_htons(pj_uint16_t hostshort);
  437. /**
  438. * Convert 32-bit value from network byte order to host byte order.
  439. *
  440. * @param netlong 32-bit network value.
  441. * @return 32-bit host value.
  442. */
  443. PJ_DECL(pj_uint32_t) pj_ntohl(pj_uint32_t netlong);
  444. /**
  445. * Convert 32-bit value from host byte order to network byte order.
  446. *
  447. * @param hostlong 32-bit host value.
  448. * @return 32-bit network value.
  449. */
  450. PJ_DECL(pj_uint32_t) pj_htonl(pj_uint32_t hostlong);
  451. /**
  452. * Convert an Internet host address given in network byte order
  453. * to string in standard numbers and dots notation.
  454. *
  455. * @param inaddr The host address.
  456. * @return The string address.
  457. */
  458. PJ_DECL(char*) pj_inet_ntoa(pj_in_addr inaddr);
  459. /**
  460. * This function converts the Internet host address cp from the standard
  461. * numbers-and-dots notation into binary data and stores it in the structure
  462. * that inp points to.
  463. *
  464. * @param cp IP address in standard numbers-and-dots notation.
  465. * @param inp Structure that holds the output of the conversion.
  466. *
  467. * @return nonzero if the address is valid, zero if not.
  468. */
  469. PJ_DECL(int) pj_inet_aton(const pj_str_t *cp, struct pj_in_addr *inp);
  470. /**
  471. * This function converts an address in its standard text presentation form
  472. * into its numeric binary form. It supports both IPv4 and IPv6 address
  473. * conversion.
  474. *
  475. * @param af Specify the family of the address. The PJ_AF_INET and
  476. * PJ_AF_INET6 address families shall be supported.
  477. * @param src Points to the string being passed in.
  478. * @param dst Points to a buffer into which the function stores the
  479. * numeric address; this shall be large enough to hold the
  480. * numeric address (32 bits for PJ_AF_INET, 128 bits for
  481. * PJ_AF_INET6).
  482. *
  483. * @return PJ_SUCCESS if conversion was successful.
  484. */
  485. PJ_DECL(pj_status_t) pj_inet_pton(int af, const pj_str_t *src, void *dst);
  486. /**
  487. * This function converts a numeric address into a text string suitable
  488. * for presentation. It supports both IPv4 and IPv6 address
  489. * conversion.
  490. * @see pj_sockaddr_print()
  491. *
  492. * @param af Specify the family of the address. This can be PJ_AF_INET
  493. * or PJ_AF_INET6.
  494. * @param src Points to a buffer holding an IPv4 address if the af argument
  495. * is PJ_AF_INET, or an IPv6 address if the af argument is
  496. * PJ_AF_INET6; the address must be in network byte order.
  497. * @param dst Points to a buffer where the function stores the resulting
  498. * text string; it shall not be NULL.
  499. * @param size Specifies the size of this buffer, which shall be large
  500. * enough to hold the text string (PJ_INET_ADDRSTRLEN characters
  501. * for IPv4, PJ_INET6_ADDRSTRLEN characters for IPv6).
  502. *
  503. * @return PJ_SUCCESS if conversion was successful.
  504. */
  505. PJ_DECL(pj_status_t) pj_inet_ntop(int af, const void *src,
  506. char *dst, int size);
  507. /**
  508. * Converts numeric address into its text string representation.
  509. * @see pj_sockaddr_print()
  510. *
  511. * @param af Specify the family of the address. This can be PJ_AF_INET
  512. * or PJ_AF_INET6.
  513. * @param src Points to a buffer holding an IPv4 address if the af argument
  514. * is PJ_AF_INET, or an IPv6 address if the af argument is
  515. * PJ_AF_INET6; the address must be in network byte order.
  516. * @param dst Points to a buffer where the function stores the resulting
  517. * text string; it shall not be NULL.
  518. * @param size Specifies the size of this buffer, which shall be large
  519. * enough to hold the text string (PJ_INET_ADDRSTRLEN characters
  520. * for IPv4, PJ_INET6_ADDRSTRLEN characters for IPv6).
  521. *
  522. * @return The address string or NULL if failed.
  523. */
  524. PJ_DECL(char*) pj_inet_ntop2(int af, const void *src,
  525. char *dst, int size);
  526. /**
  527. * Print socket address.
  528. *
  529. * @param addr The socket address.
  530. * @param buf Text buffer.
  531. * @param size Size of buffer.
  532. * @param flags Bitmask combination of these value:
  533. * - 1: port number is included.
  534. * - 2: square bracket is included for IPv6 address.
  535. *
  536. * @return The address string.
  537. */
  538. PJ_DECL(char*) pj_sockaddr_print(const pj_sockaddr_t *addr,
  539. char *buf, int size,
  540. unsigned flags);
  541. /**
  542. * Convert address string with numbers and dots to binary IP address.
  543. *
  544. * @param cp The IP address in numbers and dots notation.
  545. * @return If success, the IP address is returned in network
  546. * byte order. If failed, PJ_INADDR_NONE will be
  547. * returned.
  548. * @remark
  549. * This is an obsolete interface to #pj_inet_aton(); it is obsolete
  550. * because -1 is a valid address (255.255.255.255), and #pj_inet_aton()
  551. * provides a cleaner way to indicate error return.
  552. */
  553. PJ_DECL(pj_in_addr) pj_inet_addr(const pj_str_t *cp);
  554. /**
  555. * Convert address string with numbers and dots to binary IP address.
  556. *
  557. * @param cp The IP address in numbers and dots notation.
  558. * @return If success, the IP address is returned in network
  559. * byte order. If failed, PJ_INADDR_NONE will be
  560. * returned.
  561. * @remark
  562. * This is an obsolete interface to #pj_inet_aton(); it is obsolete
  563. * because -1 is a valid address (255.255.255.255), and #pj_inet_aton()
  564. * provides a cleaner way to indicate error return.
  565. */
  566. PJ_DECL(pj_in_addr) pj_inet_addr2(const char *cp);
  567. /**
  568. * Initialize IPv4 socket address based on the address and port info.
  569. * The string address may be in a standard numbers and dots notation or
  570. * may be a hostname. If hostname is specified, then the function will
  571. * resolve the host into the IP address.
  572. *
  573. * @see pj_sockaddr_init()
  574. *
  575. * @param addr The IP socket address to be set.
  576. * @param cp The address string, which can be in a standard
  577. * dotted numbers or a hostname to be resolved.
  578. * @param port The port number, in host byte order.
  579. *
  580. * @return Zero on success.
  581. */
  582. PJ_DECL(pj_status_t) pj_sockaddr_in_init( pj_sockaddr_in *addr,
  583. const pj_str_t *cp,
  584. pj_uint16_t port);
  585. /**
  586. * Initialize IP socket address based on the address and port info.
  587. * The string address may be in a standard numbers and dots notation or
  588. * may be a hostname. If hostname is specified, then the function will
  589. * resolve the host into the IP address.
  590. *
  591. * @see pj_sockaddr_in_init()
  592. *
  593. * @param af Internet address family.
  594. * @param addr The IP socket address to be set.
  595. * @param cp The address string, which can be in a standard
  596. * dotted numbers or a hostname to be resolved.
  597. * @param port The port number, in host byte order.
  598. *
  599. * @return Zero on success.
  600. */
  601. PJ_DECL(pj_status_t) pj_sockaddr_init(int af,
  602. pj_sockaddr *addr,
  603. const pj_str_t *cp,
  604. pj_uint16_t port);
  605. /**
  606. * Compare two socket addresses.
  607. *
  608. * @param addr1 First address.
  609. * @param addr2 Second address.
  610. *
  611. * @return Zero on equal, -1 if addr1 is less than addr2,
  612. * and +1 if addr1 is more than addr2.
  613. */
  614. PJ_DECL(int) pj_sockaddr_cmp(const pj_sockaddr_t *addr1,
  615. const pj_sockaddr_t *addr2);
  616. /**
  617. * Get pointer to the address part of a socket address.
  618. *
  619. * @param addr Socket address.
  620. *
  621. * @return Pointer to address part (sin_addr or sin6_addr,
  622. * depending on address family)
  623. */
  624. PJ_DECL(void*) pj_sockaddr_get_addr(const pj_sockaddr_t *addr);
  625. /**
  626. * Check that a socket address contains a non-zero address part.
  627. *
  628. * @param addr Socket address.
  629. *
  630. * @return Non-zero if address is set to non-zero.
  631. */
  632. PJ_DECL(pj_bool_t) pj_sockaddr_has_addr(const pj_sockaddr_t *addr);
  633. /**
  634. * Get the address part length of a socket address, based on its address
  635. * family. For PJ_AF_INET, the length will be sizeof(pj_in_addr), and
  636. * for PJ_AF_INET6, the length will be sizeof(pj_in6_addr).
  637. *
  638. * @param addr Socket address.
  639. *
  640. * @return Length in bytes.
  641. */
  642. PJ_DECL(unsigned) pj_sockaddr_get_addr_len(const pj_sockaddr_t *addr);
  643. /**
  644. * Get the socket address length, based on its address
  645. * family. For PJ_AF_INET, the length will be sizeof(pj_sockaddr_in), and
  646. * for PJ_AF_INET6, the length will be sizeof(pj_sockaddr_in6).
  647. *
  648. * @param addr Socket address.
  649. *
  650. * @return Length in bytes.
  651. */
  652. PJ_DECL(unsigned) pj_sockaddr_get_len(const pj_sockaddr_t *addr);
  653. /**
  654. * Copy only the address part (sin_addr/sin6_addr) of a socket address.
  655. *
  656. * @param dst Destination socket address.
  657. * @param src Source socket address.
  658. *
  659. * @see @pj_sockaddr_cp()
  660. */
  661. PJ_DECL(void) pj_sockaddr_copy_addr(pj_sockaddr *dst,
  662. const pj_sockaddr *src);
  663. /**
  664. * Copy socket address. This will copy the whole structure depending
  665. * on the address family of the source socket address.
  666. *
  667. * @param dst Destination socket address.
  668. * @param src Source socket address.
  669. *
  670. * @see @pj_sockaddr_copy_addr()
  671. */
  672. PJ_DECL(void) pj_sockaddr_cp(pj_sockaddr_t *dst, const pj_sockaddr_t *src);
  673. /**
  674. * Get the IP address of an IPv4 socket address.
  675. * The address is returned as 32bit value in host byte order.
  676. *
  677. * @param addr The IP socket address.
  678. * @return 32bit address, in host byte order.
  679. */
  680. PJ_DECL(pj_in_addr) pj_sockaddr_in_get_addr(const pj_sockaddr_in *addr);
  681. /**
  682. * Set the IP address of an IPv4 socket address.
  683. *
  684. * @param addr The IP socket address.
  685. * @param hostaddr The host address, in host byte order.
  686. */
  687. PJ_DECL(void) pj_sockaddr_in_set_addr(pj_sockaddr_in *addr,
  688. pj_uint32_t hostaddr);
  689. /**
  690. * Set the IP address of an IP socket address from string address,
  691. * with resolving the host if necessary. The string address may be in a
  692. * standard numbers and dots notation or may be a hostname. If hostname
  693. * is specified, then the function will resolve the host into the IP
  694. * address.
  695. *
  696. * @see pj_sockaddr_set_str_addr()
  697. *
  698. * @param addr The IP socket address to be set.
  699. * @param cp The address string, which can be in a standard
  700. * dotted numbers or a hostname to be resolved.
  701. *
  702. * @return PJ_SUCCESS on success.
  703. */
  704. PJ_DECL(pj_status_t) pj_sockaddr_in_set_str_addr( pj_sockaddr_in *addr,
  705. const pj_str_t *cp);
  706. /**
  707. * Set the IP address of an IPv4 or IPv6 socket address from string address,
  708. * with resolving the host if necessary. The string address may be in a
  709. * standard IPv6 or IPv6 address or may be a hostname. If hostname
  710. * is specified, then the function will resolve the host into the IP
  711. * address according to the address family.
  712. *
  713. * @param af Address family.
  714. * @param addr The IP socket address to be set.
  715. * @param cp The address string, which can be in a standard
  716. * IP numbers (IPv4 or IPv6) or a hostname to be resolved.
  717. *
  718. * @return PJ_SUCCESS on success.
  719. */
  720. PJ_DECL(pj_status_t) pj_sockaddr_set_str_addr(int af,
  721. pj_sockaddr *addr,
  722. const pj_str_t *cp);
  723. /**
  724. * Get the port number of a socket address, in host byte order.
  725. * This function can be used for both IPv4 and IPv6 socket address.
  726. *
  727. * @param addr Socket address.
  728. *
  729. * @return Port number, in host byte order.
  730. */
  731. PJ_DECL(pj_uint16_t) pj_sockaddr_get_port(const pj_sockaddr_t *addr);
  732. /**
  733. * Get the transport layer port number of an Internet socket address.
  734. * The port is returned in host byte order.
  735. *
  736. * @param addr The IP socket address.
  737. * @return Port number, in host byte order.
  738. */
  739. PJ_DECL(pj_uint16_t) pj_sockaddr_in_get_port(const pj_sockaddr_in *addr);
  740. /**
  741. * Set the port number of an Internet socket address.
  742. *
  743. * @param addr The socket address.
  744. * @param hostport The port number, in host byte order.
  745. */
  746. PJ_DECL(pj_status_t) pj_sockaddr_set_port(pj_sockaddr *addr,
  747. pj_uint16_t hostport);
  748. /**
  749. * Set the port number of an IPv4 socket address.
  750. *
  751. * @see pj_sockaddr_set_port()
  752. *
  753. * @param addr The IP socket address.
  754. * @param hostport The port number, in host byte order.
  755. */
  756. PJ_DECL(void) pj_sockaddr_in_set_port(pj_sockaddr_in *addr,
  757. pj_uint16_t hostport);
  758. /*****************************************************************************
  759. *
  760. * HOST NAME AND ADDRESS.
  761. *
  762. *****************************************************************************
  763. */
  764. /**
  765. * Get system's host name.
  766. *
  767. * @return The hostname, or empty string if the hostname can not
  768. * be identified.
  769. */
  770. PJ_DECL(const pj_str_t*) pj_gethostname(void);
  771. /**
  772. * Get host's IP address, which the the first IP address that is resolved
  773. * from the hostname.
  774. *
  775. * @return The host's IP address, PJ_INADDR_NONE if the host
  776. * IP address can not be identified.
  777. */
  778. PJ_DECL(pj_in_addr) pj_gethostaddr(void);
  779. /*****************************************************************************
  780. *
  781. * SOCKET API.
  782. *
  783. *****************************************************************************
  784. */
  785. /**
  786. * Create new socket/endpoint for communication.
  787. *
  788. * @param family Specifies a communication domain; this selects the
  789. * protocol family which will be used for communication.
  790. * @param type The socket has the indicated type, which specifies the
  791. * communication semantics.
  792. * @param protocol Specifies a particular protocol to be used with the
  793. * socket. Normally only a single protocol exists to support
  794. * a particular socket type within a given protocol family,
  795. * in which a case protocol can be specified as 0.
  796. * @param sock New socket descriptor, or PJ_INVALID_SOCKET on error.
  797. *
  798. * @return Zero on success.
  799. */
  800. PJ_DECL(pj_status_t) pj_sock_socket(int family,
  801. int type,
  802. int protocol,
  803. pj_sock_t *sock);
  804. /**
  805. * Close the socket descriptor.
  806. *
  807. * @param sockfd The socket descriptor.
  808. *
  809. * @return Zero on success.
  810. */
  811. PJ_DECL(pj_status_t) pj_sock_close(pj_sock_t sockfd);
  812. /**
  813. * This function gives the socket sockfd the local address my_addr. my_addr is
  814. * addrlen bytes long. Traditionally, this is called assigning a name to
  815. * a socket. When a socket is created with #pj_sock_socket(), it exists in a
  816. * name space (address family) but has no name assigned.
  817. *
  818. * @param sockfd The socket desriptor.
  819. * @param my_addr The local address to bind the socket to.
  820. * @param addrlen The length of the address.
  821. *
  822. * @return Zero on success.
  823. */
  824. PJ_DECL(pj_status_t) pj_sock_bind( pj_sock_t sockfd,
  825. const pj_sockaddr_t *my_addr,
  826. int addrlen);
  827. /**
  828. * Bind the IP socket sockfd to the given address and port.
  829. *
  830. * @param sockfd The socket descriptor.
  831. * @param addr Local address to bind the socket to, in host byte order.
  832. * @param port The local port to bind the socket to, in host byte order.
  833. *
  834. * @return Zero on success.
  835. */
  836. PJ_DECL(pj_status_t) pj_sock_bind_in( pj_sock_t sockfd,
  837. pj_uint32_t addr,
  838. pj_uint16_t port);
  839. #if PJ_HAS_TCP
  840. /**
  841. * Listen for incoming connection. This function only applies to connection
  842. * oriented sockets (such as PJ_SOCK_STREAM or PJ_SOCK_SEQPACKET), and it
  843. * indicates the willingness to accept incoming connections.
  844. *
  845. * @param sockfd The socket descriptor.
  846. * @param backlog Defines the maximum length the queue of pending
  847. * connections may grow to.
  848. *
  849. * @return Zero on success.
  850. */
  851. PJ_DECL(pj_status_t) pj_sock_listen( pj_sock_t sockfd,
  852. int backlog );
  853. /**
  854. * Accept new connection on the specified connection oriented server socket.
  855. *
  856. * @param serverfd The server socket.
  857. * @param newsock New socket on success, of PJ_INVALID_SOCKET if failed.
  858. * @param addr A pointer to sockaddr type. If the argument is not NULL,
  859. * it will be filled by the address of connecting entity.
  860. * @param addrlen Initially specifies the length of the address, and upon
  861. * return will be filled with the exact address length.
  862. *
  863. * @return Zero on success, or the error number.
  864. */
  865. PJ_DECL(pj_status_t) pj_sock_accept( pj_sock_t serverfd,
  866. pj_sock_t *newsock,
  867. pj_sockaddr_t *addr,
  868. int *addrlen);
  869. #endif
  870. /**
  871. * The file descriptor sockfd must refer to a socket. If the socket is of
  872. * type PJ_SOCK_DGRAM then the serv_addr address is the address to which
  873. * datagrams are sent by default, and the only address from which datagrams
  874. * are received. If the socket is of type PJ_SOCK_STREAM or PJ_SOCK_SEQPACKET,
  875. * this call attempts to make a connection to another socket. The
  876. * other socket is specified by serv_addr, which is an address (of length
  877. * addrlen) in the communications space of the socket. Each communications
  878. * space interprets the serv_addr parameter in its own way.
  879. *
  880. * @param sockfd The socket descriptor.
  881. * @param serv_addr Server address to connect to.
  882. * @param addrlen The length of server address.
  883. *
  884. * @return Zero on success.
  885. */
  886. PJ_DECL(pj_status_t) pj_sock_connect( pj_sock_t sockfd,
  887. const pj_sockaddr_t *serv_addr,
  888. int addrlen);
  889. /**
  890. * Return the address of peer which is connected to socket sockfd.
  891. *
  892. * @param sockfd The socket descriptor.
  893. * @param addr Pointer to sockaddr structure to which the address
  894. * will be returned.
  895. * @param namelen Initially the length of the addr. Upon return the value
  896. * will be set to the actual length of the address.
  897. *
  898. * @return Zero on success.
  899. */
  900. PJ_DECL(pj_status_t) pj_sock_getpeername(pj_sock_t sockfd,
  901. pj_sockaddr_t *addr,
  902. int *namelen);
  903. /**
  904. * Return the current name of the specified socket.
  905. *
  906. * @param sockfd The socket descriptor.
  907. * @param addr Pointer to sockaddr structure to which the address
  908. * will be returned.
  909. * @param namelen Initially the length of the addr. Upon return the value
  910. * will be set to the actual length of the address.
  911. *
  912. * @return Zero on success.
  913. */
  914. PJ_DECL(pj_status_t) pj_sock_getsockname( pj_sock_t sockfd,
  915. pj_sockaddr_t *addr,
  916. int *namelen);
  917. /**
  918. * Get socket option associated with a socket. Options may exist at multiple
  919. * protocol levels; they are always present at the uppermost socket level.
  920. *
  921. * @param sockfd The socket descriptor.
  922. * @param level The level which to get the option from.
  923. * @param optname The option name.
  924. * @param optval Identifies the buffer which the value will be
  925. * returned.
  926. * @param optlen Initially contains the length of the buffer, upon
  927. * return will be set to the actual size of the value.
  928. *
  929. * @return Zero on success.
  930. */
  931. PJ_DECL(pj_status_t) pj_sock_getsockopt( pj_sock_t sockfd,
  932. pj_uint16_t level,
  933. pj_uint16_t optname,
  934. void *optval,
  935. int *optlen);
  936. /**
  937. * Manipulate the options associated with a socket. Options may exist at
  938. * multiple protocol levels; they are always present at the uppermost socket
  939. * level.
  940. *
  941. * @param sockfd The socket descriptor.
  942. * @param level The level which to get the option from.
  943. * @param optname The option name.
  944. * @param optval Identifies the buffer which contain the value.
  945. * @param optlen The length of the value.
  946. *
  947. * @return PJ_SUCCESS or the status code.
  948. */
  949. PJ_DECL(pj_status_t) pj_sock_setsockopt( pj_sock_t sockfd,
  950. pj_uint16_t level,
  951. pj_uint16_t optname,
  952. const void *optval,
  953. int optlen);
  954. /**
  955. * Receives data stream or message coming to the specified socket.
  956. *
  957. * @param sockfd The socket descriptor.
  958. * @param buf The buffer to receive the data or message.
  959. * @param len On input, the length of the buffer. On return,
  960. * contains the length of data received.
  961. * @param flags Flags (such as pj_MSG_PEEK()).
  962. *
  963. * @return PJ_SUCCESS or the error code.
  964. */
  965. PJ_DECL(pj_status_t) pj_sock_recv(pj_sock_t sockfd,
  966. void *buf,
  967. pj_ssize_t *len,
  968. unsigned flags);
  969. /**
  970. * Receives data stream or message coming to the specified socket.
  971. *
  972. * @param sockfd The socket descriptor.
  973. * @param buf The buffer to receive the data or message.
  974. * @param len On input, the length of the buffer. On return,
  975. * contains the length of data received.
  976. * @param flags Flags (such as pj_MSG_PEEK()).
  977. * @param from If not NULL, it will be filled with the source
  978. * address of the connection.
  979. * @param fromlen Initially contains the length of from address,
  980. * and upon return will be filled with the actual
  981. * length of the address.
  982. *
  983. * @return PJ_SUCCESS or the error code.
  984. */
  985. PJ_DECL(pj_status_t) pj_sock_recvfrom( pj_sock_t sockfd,
  986. void *buf,
  987. pj_ssize_t *len,
  988. unsigned flags,
  989. pj_sockaddr_t *from,
  990. int *fromlen);
  991. /**
  992. * Transmit data to the socket.
  993. *
  994. * @param sockfd Socket descriptor.
  995. * @param buf Buffer containing data to be sent.
  996. * @param len On input, the length of the data in the buffer.
  997. * Upon return, it will be filled with the length
  998. * of data sent.
  999. * @param flags Flags (such as pj_MSG_DONTROUTE()).
  1000. *
  1001. * @return PJ_SUCCESS or the status code.
  1002. */
  1003. PJ_DECL(pj_status_t) pj_sock_send(pj_sock_t sockfd,
  1004. const void *buf,
  1005. pj_ssize_t *len,
  1006. unsigned flags);
  1007. /**
  1008. * Transmit data to the socket to the specified address.
  1009. *
  1010. * @param sockfd Socket descriptor.
  1011. * @param buf Buffer containing data to be sent.
  1012. * @param len On input, the length of the data in the buffer.
  1013. * Upon return, it will be filled with the length
  1014. * of data sent.
  1015. * @param flags Flags (such as pj_MSG_DONTROUTE()).
  1016. * @param to The address to send.
  1017. * @param tolen The length of the address in bytes.
  1018. *
  1019. * @return PJ_SUCCESS or the status code.
  1020. */
  1021. PJ_DECL(pj_status_t) pj_sock_sendto(pj_sock_t sockfd,
  1022. const void *buf,
  1023. pj_ssize_t *len,
  1024. unsigned flags,
  1025. const pj_sockaddr_t *to,
  1026. int tolen);
  1027. #if PJ_HAS_TCP
  1028. /**
  1029. * The shutdown call causes all or part of a full-duplex connection on the
  1030. * socket associated with sockfd to be shut down.
  1031. *
  1032. * @param sockfd The socket descriptor.
  1033. * @param how If how is PJ_SHUT_RD, further receptions will be
  1034. * disallowed. If how is PJ_SHUT_WR, further transmissions
  1035. * will be disallowed. If how is PJ_SHUT_RDWR, further
  1036. * receptions andtransmissions will be disallowed.
  1037. *
  1038. * @return Zero on success.
  1039. */
  1040. PJ_DECL(pj_status_t) pj_sock_shutdown( pj_sock_t sockfd,
  1041. int how);
  1042. #endif
  1043. /**
  1044. * @}
  1045. */
  1046. PJ_END_DECL
  1047. #endif /* __PJ_SOCK_H__ */