PageRenderTime 55ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/pjlib/include/pj/sock.h

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