sock.h 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579
  1. /*
  2. * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
  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. #include <pj/compat/socket.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. * - Socket test: \src{pjlib/src/pjlib-test/sock.c}
  48. * - Socket Select() test: \src{pjlib/src/pjlib-test/select.c}
  49. * - Socket Performance test: \src{pjlib/src/pjlib-test/sock_perf.c}
  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. /** The close-on-exec flag. @see pj_SOCK_CLOEXEC() */
  121. extern const int PJ_SOCK_CLOEXEC;
  122. /*
  123. * Accessor functions for various constants. These functions are provided
  124. * because Symbian doesn't allow exporting global variables from a DLL.
  125. */
  126. #if defined(PJ_DLL)
  127. /** Get #PJ_SOCK_STREAM constant */
  128. PJ_DECL(int) pj_SOCK_STREAM(void);
  129. /** Get #PJ_SOCK_DGRAM constant */
  130. PJ_DECL(int) pj_SOCK_DGRAM(void);
  131. /** Get #PJ_SOCK_RAW constant */
  132. PJ_DECL(int) pj_SOCK_RAW(void);
  133. /** Get #PJ_SOCK_RDM constant */
  134. PJ_DECL(int) pj_SOCK_RDM(void);
  135. /** Get #PJ_SOCK_CLOEXEC constant */
  136. PJ_DECL(int) pj_SOCK_CLOEXEC(void);
  137. #else
  138. /** Get #PJ_SOCK_STREAM constant */
  139. # define pj_SOCK_STREAM() PJ_SOCK_STREAM
  140. /** Get #PJ_SOCK_DGRAM constant */
  141. # define pj_SOCK_DGRAM() PJ_SOCK_DGRAM
  142. /** Get #PJ_SOCK_RAW constant */
  143. # define pj_SOCK_RAW() PJ_SOCK_RAW
  144. /** Get #PJ_SOCK_RDM constant */
  145. # define pj_SOCK_RDM() PJ_SOCK_RDM
  146. /** Get #PJ_SOCK_CLOEXEC constant */
  147. # define pj_SOCK_CLOEXEC() PJ_SOCK_CLOEXEC
  148. #endif
  149. /**
  150. * Socket level specified in #pj_sock_setsockopt() or #pj_sock_getsockopt().
  151. * APPLICATION MUST USE THESE VALUES INSTEAD OF NORMAL SOL_*, BECAUSE
  152. * THE LIBRARY WILL TRANSLATE THE VALUE TO THE NATIVE VALUE.
  153. */
  154. /** Socket level. @see pj_SOL_SOCKET() */
  155. extern const pj_uint16_t PJ_SOL_SOCKET;
  156. /** IP level. @see pj_SOL_IP() */
  157. extern const pj_uint16_t PJ_SOL_IP;
  158. /** TCP level. @see pj_SOL_TCP() */
  159. extern const pj_uint16_t PJ_SOL_TCP;
  160. /** UDP level. @see pj_SOL_UDP() */
  161. extern const pj_uint16_t PJ_SOL_UDP;
  162. /** IP version 6. @see pj_SOL_IPV6() */
  163. extern const pj_uint16_t PJ_SOL_IPV6;
  164. /*
  165. * Accessor functions for various constants. These functions are provided
  166. * because Symbian doesn't allow exporting global variables from a DLL.
  167. */
  168. #if defined(PJ_DLL)
  169. /** Get #PJ_SOL_SOCKET constant */
  170. PJ_DECL(pj_uint16_t) pj_SOL_SOCKET(void);
  171. /** Get #PJ_SOL_IP constant */
  172. PJ_DECL(pj_uint16_t) pj_SOL_IP(void);
  173. /** Get #PJ_SOL_TCP constant */
  174. PJ_DECL(pj_uint16_t) pj_SOL_TCP(void);
  175. /** Get #PJ_SOL_UDP constant */
  176. PJ_DECL(pj_uint16_t) pj_SOL_UDP(void);
  177. /** Get #PJ_SOL_IPV6 constant */
  178. PJ_DECL(pj_uint16_t) pj_SOL_IPV6(void);
  179. #else
  180. /** Get #PJ_SOL_SOCKET constant */
  181. # define pj_SOL_SOCKET() PJ_SOL_SOCKET
  182. /** Get #PJ_SOL_IP constant */
  183. # define pj_SOL_IP() PJ_SOL_IP
  184. /** Get #PJ_SOL_TCP constant */
  185. # define pj_SOL_TCP() PJ_SOL_TCP
  186. /** Get #PJ_SOL_UDP constant */
  187. # define pj_SOL_UDP() PJ_SOL_UDP
  188. /** Get #PJ_SOL_IPV6 constant */
  189. # define pj_SOL_IPV6() PJ_SOL_IPV6
  190. #endif
  191. /* IP_TOS
  192. *
  193. * Note:
  194. * TOS CURRENTLY DOES NOT WORK IN Windows 2000 and above!
  195. * See http://support.microsoft.com/kb/248611
  196. */
  197. /** IP_TOS optname in setsockopt(). @see pj_IP_TOS() */
  198. extern const pj_uint16_t PJ_IP_TOS;
  199. /*
  200. * IP TOS related constats.
  201. *
  202. * Note:
  203. * TOS CURRENTLY DOES NOT WORK IN Windows 2000 and above!
  204. * See http://support.microsoft.com/kb/248611
  205. */
  206. /** Minimize delays. @see pj_IPTOS_LOWDELAY() */
  207. extern const pj_uint16_t PJ_IPTOS_LOWDELAY;
  208. /** Optimize throughput. @see pj_IPTOS_THROUGHPUT() */
  209. extern const pj_uint16_t PJ_IPTOS_THROUGHPUT;
  210. /** Optimize for reliability. @see pj_IPTOS_RELIABILITY() */
  211. extern const pj_uint16_t PJ_IPTOS_RELIABILITY;
  212. /** "filler data" where slow transmission does't matter.
  213. * @see pj_IPTOS_MINCOST() */
  214. extern const pj_uint16_t PJ_IPTOS_MINCOST;
  215. #if defined(PJ_DLL)
  216. /** Get #PJ_IP_TOS constant */
  217. PJ_DECL(int) pj_IP_TOS(void);
  218. /** Get #PJ_IPTOS_LOWDELAY constant */
  219. PJ_DECL(int) pj_IPTOS_LOWDELAY(void);
  220. /** Get #PJ_IPTOS_THROUGHPUT constant */
  221. PJ_DECL(int) pj_IPTOS_THROUGHPUT(void);
  222. /** Get #PJ_IPTOS_RELIABILITY constant */
  223. PJ_DECL(int) pj_IPTOS_RELIABILITY(void);
  224. /** Get #PJ_IPTOS_MINCOST constant */
  225. PJ_DECL(int) pj_IPTOS_MINCOST(void);
  226. #else
  227. /** Get #PJ_IP_TOS constant */
  228. # define pj_IP_TOS() PJ_IP_TOS
  229. /** Get #PJ_IPTOS_LOWDELAY constant */
  230. # define pj_IPTOS_LOWDELAY() PJ_IP_TOS_LOWDELAY
  231. /** Get #PJ_IPTOS_THROUGHPUT constant */
  232. # define pj_IPTOS_THROUGHPUT() PJ_IP_TOS_THROUGHPUT
  233. /** Get #PJ_IPTOS_RELIABILITY constant */
  234. # define pj_IPTOS_RELIABILITY() PJ_IP_TOS_RELIABILITY
  235. /** Get #PJ_IPTOS_MINCOST constant */
  236. # define pj_IPTOS_MINCOST() PJ_IP_TOS_MINCOST
  237. #endif
  238. /** IPV6_TCLASS optname in setsockopt(). @see pj_IPV6_TCLASS() */
  239. extern const pj_uint16_t PJ_IPV6_TCLASS;
  240. #if defined(PJ_DLL)
  241. /** Get #PJ_IPV6_TCLASS constant */
  242. PJ_DECL(int) pj_IPV6_TCLASS(void);
  243. #else
  244. /** Get #PJ_IPV6_TCLASS constant */
  245. # define pj_IPV6_TCLASS() PJ_IPV6_TCLASS
  246. #endif
  247. /**
  248. * Values to be specified as \c optname when calling #pj_sock_setsockopt()
  249. * or #pj_sock_getsockopt().
  250. */
  251. /** Socket type. @see pj_SO_TYPE() */
  252. extern const pj_uint16_t PJ_SO_TYPE;
  253. /** Buffer size for receive. @see pj_SO_RCVBUF() */
  254. extern const pj_uint16_t PJ_SO_RCVBUF;
  255. /** Buffer size for send. @see pj_SO_SNDBUF() */
  256. extern const pj_uint16_t PJ_SO_SNDBUF;
  257. /** Disables the Nagle algorithm for send coalescing. @see pj_TCP_NODELAY */
  258. extern const pj_uint16_t PJ_TCP_NODELAY;
  259. /** Allows the socket to be bound to an address that is already in use.
  260. * @see pj_SO_REUSEADDR */
  261. extern const pj_uint16_t PJ_SO_REUSEADDR;
  262. /** Do not generate SIGPIPE. @see pj_SO_NOSIGPIPE */
  263. extern const pj_uint16_t PJ_SO_NOSIGPIPE;
  264. /** Set the protocol-defined priority for all packets to be sent on socket.
  265. */
  266. extern const pj_uint16_t PJ_SO_PRIORITY;
  267. /** IP multicast interface. @see pj_IP_MULTICAST_IF() */
  268. extern const pj_uint16_t PJ_IP_MULTICAST_IF;
  269. /** IP multicast ttl. @see pj_IP_MULTICAST_TTL() */
  270. extern const pj_uint16_t PJ_IP_MULTICAST_TTL;
  271. /** IP multicast loopback. @see pj_IP_MULTICAST_LOOP() */
  272. extern const pj_uint16_t PJ_IP_MULTICAST_LOOP;
  273. /** Add an IP group membership. @see pj_IP_ADD_MEMBERSHIP() */
  274. extern const pj_uint16_t PJ_IP_ADD_MEMBERSHIP;
  275. /** Drop an IP group membership. @see pj_IP_DROP_MEMBERSHIP() */
  276. extern const pj_uint16_t PJ_IP_DROP_MEMBERSHIP;
  277. #if defined(PJ_DLL)
  278. /** Get #PJ_SO_TYPE constant */
  279. PJ_DECL(pj_uint16_t) pj_SO_TYPE(void);
  280. /** Get #PJ_SO_RCVBUF constant */
  281. PJ_DECL(pj_uint16_t) pj_SO_RCVBUF(void);
  282. /** Get #PJ_SO_SNDBUF constant */
  283. PJ_DECL(pj_uint16_t) pj_SO_SNDBUF(void);
  284. /** Get #PJ_TCP_NODELAY constant */
  285. PJ_DECL(pj_uint16_t) pj_TCP_NODELAY(void);
  286. /** Get #PJ_SO_REUSEADDR constant */
  287. PJ_DECL(pj_uint16_t) pj_SO_REUSEADDR(void);
  288. /** Get #PJ_SO_NOSIGPIPE constant */
  289. PJ_DECL(pj_uint16_t) pj_SO_NOSIGPIPE(void);
  290. /** Get #PJ_SO_PRIORITY constant */
  291. PJ_DECL(pj_uint16_t) pj_SO_PRIORITY(void);
  292. /** Get #PJ_IP_MULTICAST_IF constant */
  293. PJ_DECL(pj_uint16_t) pj_IP_MULTICAST_IF(void);
  294. /** Get #PJ_IP_MULTICAST_TTL constant */
  295. PJ_DECL(pj_uint16_t) pj_IP_MULTICAST_TTL(void);
  296. /** Get #PJ_IP_MULTICAST_LOOP constant */
  297. PJ_DECL(pj_uint16_t) pj_IP_MULTICAST_LOOP(void);
  298. /** Get #PJ_IP_ADD_MEMBERSHIP constant */
  299. PJ_DECL(pj_uint16_t) pj_IP_ADD_MEMBERSHIP(void);
  300. /** Get #PJ_IP_DROP_MEMBERSHIP constant */
  301. PJ_DECL(pj_uint16_t) pj_IP_DROP_MEMBERSHIP(void);
  302. #else
  303. /** Get #PJ_SO_TYPE constant */
  304. # define pj_SO_TYPE() PJ_SO_TYPE
  305. /** Get #PJ_SO_RCVBUF constant */
  306. # define pj_SO_RCVBUF() PJ_SO_RCVBUF
  307. /** Get #PJ_SO_SNDBUF constant */
  308. # define pj_SO_SNDBUF() PJ_SO_SNDBUF
  309. /** Get #PJ_TCP_NODELAY constant */
  310. # define pj_TCP_NODELAY() PJ_TCP_NODELAY
  311. /** Get #PJ_SO_REUSEADDR constant */
  312. # define pj_SO_REUSEADDR() PJ_SO_REUSEADDR
  313. /** Get #PJ_SO_NOSIGPIPE constant */
  314. # define pj_SO_NOSIGPIPE() PJ_SO_NOSIGPIPE
  315. /** Get #PJ_SO_PRIORITY constant */
  316. # define pj_SO_PRIORITY() PJ_SO_PRIORITY
  317. /** Get #PJ_IP_MULTICAST_IF constant */
  318. # define pj_IP_MULTICAST_IF() PJ_IP_MULTICAST_IF
  319. /** Get #PJ_IP_MULTICAST_TTL constant */
  320. # define pj_IP_MULTICAST_TTL() PJ_IP_MULTICAST_TTL
  321. /** Get #PJ_IP_MULTICAST_LOOP constant */
  322. # define pj_IP_MULTICAST_LOOP() PJ_IP_MULTICAST_LOOP
  323. /** Get #PJ_IP_ADD_MEMBERSHIP constant */
  324. # define pj_IP_ADD_MEMBERSHIP() PJ_IP_ADD_MEMBERSHIP
  325. /** Get #PJ_IP_DROP_MEMBERSHIP constant */
  326. # define pj_IP_DROP_MEMBERSHIP() PJ_IP_DROP_MEMBERSHIP
  327. #endif
  328. /*
  329. * Flags to be specified in #pj_sock_recv, #pj_sock_send, etc.
  330. */
  331. /** Out-of-band messages. @see pj_MSG_OOB() */
  332. extern const int PJ_MSG_OOB;
  333. /** Peek, don't remove from buffer. @see pj_MSG_PEEK() */
  334. extern const int PJ_MSG_PEEK;
  335. /** Don't route. @see pj_MSG_DONTROUTE() */
  336. extern const int PJ_MSG_DONTROUTE;
  337. #if defined(PJ_DLL)
  338. /** Get #PJ_MSG_OOB constant */
  339. PJ_DECL(int) pj_MSG_OOB(void);
  340. /** Get #PJ_MSG_PEEK constant */
  341. PJ_DECL(int) pj_MSG_PEEK(void);
  342. /** Get #PJ_MSG_DONTROUTE constant */
  343. PJ_DECL(int) pj_MSG_DONTROUTE(void);
  344. #else
  345. /** Get #PJ_MSG_OOB constant */
  346. # define pj_MSG_OOB() PJ_MSG_OOB
  347. /** Get #PJ_MSG_PEEK constant */
  348. # define pj_MSG_PEEK() PJ_MSG_PEEK
  349. /** Get #PJ_MSG_DONTROUTE constant */
  350. # define pj_MSG_DONTROUTE() PJ_MSG_DONTROUTE
  351. #endif
  352. /**
  353. * Flag to be specified in #pj_sock_shutdown().
  354. */
  355. typedef enum pj_socket_sd_type
  356. {
  357. PJ_SD_RECEIVE = 0, /**< No more receive. */
  358. PJ_SHUT_RD = 0, /**< Alias for SD_RECEIVE. */
  359. PJ_SD_SEND = 1, /**< No more sending. */
  360. PJ_SHUT_WR = 1, /**< Alias for SD_SEND. */
  361. PJ_SD_BOTH = 2, /**< No more send and receive. */
  362. PJ_SHUT_RDWR = 2 /**< Alias for SD_BOTH. */
  363. } pj_socket_sd_type;
  364. /** Address to accept any incoming messages. */
  365. #define PJ_INADDR_ANY ((pj_uint32_t)0)
  366. /** Address indicating an error return */
  367. #define PJ_INADDR_NONE ((pj_uint32_t)0xffffffff)
  368. /** Address to send to all hosts. */
  369. #define PJ_INADDR_BROADCAST ((pj_uint32_t)0xffffffff)
  370. /**
  371. * Maximum length specifiable by #pj_sock_listen().
  372. * If the build system doesn't override this value, then the lowest
  373. * denominator (five, in Win32 systems) will be used.
  374. */
  375. #if !defined(PJ_SOMAXCONN)
  376. # define PJ_SOMAXCONN 5
  377. #endif
  378. /**
  379. * Constant for invalid socket returned by #pj_sock_socket() and
  380. * #pj_sock_accept().
  381. */
  382. #define PJ_INVALID_SOCKET (-1)
  383. /* Undefining UNIX standard library macro such as s_addr is not
  384. * recommended as it may cause build issues for anyone who uses
  385. * the macro. See #2311 for more details.
  386. */
  387. #if 0
  388. /* Must undefine s_addr because of pj_in_addr below */
  389. #undef s_addr
  390. typedef struct pj_in_addr
  391. {
  392. pj_uint32_t s_addr; /**< The 32bit IP address. */
  393. } pj_in_addr;
  394. #else
  395. /**
  396. * This structure describes Internet address.
  397. */
  398. typedef struct in_addr pj_in_addr;
  399. #endif
  400. /**
  401. * Maximum length of text representation of an IPv4 address.
  402. */
  403. #define PJ_INET_ADDRSTRLEN 16
  404. /**
  405. * Maximum length of text representation of an IPv6 address.
  406. */
  407. #define PJ_INET6_ADDRSTRLEN 46
  408. /**
  409. * The size of sin_zero field in pj_sockaddr_in structure. Most OSes
  410. * use 8, but others such as the BSD TCP/IP stack in eCos uses 24.
  411. */
  412. #ifndef PJ_SOCKADDR_IN_SIN_ZERO_LEN
  413. # define PJ_SOCKADDR_IN_SIN_ZERO_LEN 8
  414. #endif
  415. /**
  416. * This structure describes Internet socket address.
  417. * If PJ_SOCKADDR_HAS_LEN is not zero, then sin_zero_len member is added
  418. * to this struct. As far the application is concerned, the value of
  419. * this member will always be zero. Internally, PJLIB may modify the value
  420. * before calling OS socket API, and reset the value back to zero before
  421. * returning the struct to application.
  422. */
  423. struct pj_sockaddr_in
  424. {
  425. #if defined(PJ_SOCKADDR_HAS_LEN) && PJ_SOCKADDR_HAS_LEN!=0
  426. pj_uint8_t sin_zero_len; /**< Just ignore this. */
  427. pj_uint8_t sin_family; /**< Address family. */
  428. #else
  429. pj_uint16_t sin_family; /**< Address family. */
  430. #endif
  431. pj_uint16_t sin_port; /**< Transport layer port number. */
  432. pj_in_addr sin_addr; /**< IP address. */
  433. char sin_zero_pad[PJ_SOCKADDR_IN_SIN_ZERO_LEN]; /**< Padding.*/
  434. };
  435. /* Undefining C standard library macro such as s6_addr is not
  436. * recommended as it may cause build issues for anyone who uses
  437. * the macro. See #2311 for more details.
  438. */
  439. #if 0
  440. #undef s6_addr
  441. typedef union pj_in6_addr
  442. {
  443. /* This is the main entry */
  444. pj_uint8_t s6_addr[16]; /**< 8-bit array */
  445. /* While these are used for proper alignment */
  446. pj_uint32_t u6_addr32[4];
  447. /* Do not use this with Winsock2, as this will align pj_sockaddr_in6
  448. * to 64-bit boundary and Winsock2 doesn't like it!
  449. * Update 26/04/2010:
  450. * This is now disabled, see https://github.com/pjsip/pjproject/issues/1058
  451. */
  452. #if 0 && defined(PJ_HAS_INT64) && PJ_HAS_INT64!=0 && \
  453. (!defined(PJ_WIN32) || PJ_WIN32==0)
  454. pj_int64_t u6_addr64[2];
  455. #endif
  456. } pj_in6_addr;
  457. #else
  458. /**
  459. * This structure describes IPv6 address.
  460. */
  461. typedef struct in6_addr pj_in6_addr;
  462. #endif
  463. /** Initializer value for pj_in6_addr. */
  464. #define PJ_IN6ADDR_ANY_INIT { { { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 } } }
  465. /** Initializer value for pj_in6_addr. */
  466. #define PJ_IN6ADDR_LOOPBACK_INIT { { { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1 } } }
  467. /**
  468. * This structure describes IPv6 socket address.
  469. * If PJ_SOCKADDR_HAS_LEN is not zero, then sin_zero_len member is added
  470. * to this struct. As far the application is concerned, the value of
  471. * this member will always be zero. Internally, PJLIB may modify the value
  472. * before calling OS socket API, and reset the value back to zero before
  473. * returning the struct to application.
  474. */
  475. typedef struct pj_sockaddr_in6
  476. {
  477. #if defined(PJ_SOCKADDR_HAS_LEN) && PJ_SOCKADDR_HAS_LEN!=0
  478. pj_uint8_t sin6_zero_len; /**< Just ignore this. */
  479. pj_uint8_t sin6_family; /**< Address family. */
  480. #else
  481. pj_uint16_t sin6_family; /**< Address family */
  482. #endif
  483. pj_uint16_t sin6_port; /**< Transport layer port number. */
  484. pj_uint32_t sin6_flowinfo; /**< IPv6 flow information */
  485. pj_in6_addr sin6_addr; /**< IPv6 address. */
  486. pj_uint32_t sin6_scope_id; /**< Set of interfaces for a scope */
  487. } pj_sockaddr_in6;
  488. /**
  489. * This structure describes common attributes found in transport addresses.
  490. * If PJ_SOCKADDR_HAS_LEN is not zero, then sa_zero_len member is added
  491. * to this struct. As far the application is concerned, the value of
  492. * this member will always be zero. Internally, PJLIB may modify the value
  493. * before calling OS socket API, and reset the value back to zero before
  494. * returning the struct to application.
  495. */
  496. typedef struct pj_addr_hdr
  497. {
  498. #if defined(PJ_SOCKADDR_HAS_LEN) && PJ_SOCKADDR_HAS_LEN!=0
  499. pj_uint8_t sa_zero_len;
  500. pj_uint8_t sa_family;
  501. #else
  502. pj_uint16_t sa_family; /**< Common data: address family. */
  503. #endif
  504. } pj_addr_hdr;
  505. /**
  506. * This union describes a generic socket address.
  507. */
  508. typedef union pj_sockaddr
  509. {
  510. pj_addr_hdr addr; /**< Generic transport address. */
  511. pj_sockaddr_in ipv4; /**< IPv4 transport address. */
  512. pj_sockaddr_in6 ipv6; /**< IPv6 transport address. */
  513. } pj_sockaddr;
  514. /**
  515. * This structure provides multicast group information for IPv4 addresses.
  516. */
  517. typedef struct pj_ip_mreq {
  518. pj_in_addr imr_multiaddr; /**< IP multicast address of group. */
  519. pj_in_addr imr_interface; /**< local IP address of interface. */
  520. } pj_ip_mreq;
  521. /**
  522. * Options to be set for the socket.
  523. */
  524. typedef struct pj_sockopt_params
  525. {
  526. /** The number of options to be applied. */
  527. unsigned cnt;
  528. /** Array of options to be applied. */
  529. struct {
  530. /** The level at which the option is defined. */
  531. int level;
  532. /** Option name. */
  533. int optname;
  534. /** Pointer to the buffer in which the option is specified. */
  535. void *optval;
  536. /** Buffer size of the buffer pointed by optval. */
  537. int optlen;
  538. } options[PJ_MAX_SOCKOPT_PARAMS];
  539. } pj_sockopt_params;
  540. /*****************************************************************************
  541. *
  542. * SOCKET ADDRESS MANIPULATION.
  543. *
  544. *****************************************************************************
  545. */
  546. /**
  547. * Convert 16-bit value from network byte order to host byte order.
  548. *
  549. * @param netshort 16-bit network value.
  550. * @return 16-bit host value.
  551. */
  552. PJ_DECL(pj_uint16_t) pj_ntohs(pj_uint16_t netshort);
  553. /**
  554. * Convert 16-bit value from host byte order to network byte order.
  555. *
  556. * @param hostshort 16-bit host value.
  557. * @return 16-bit network value.
  558. */
  559. PJ_DECL(pj_uint16_t) pj_htons(pj_uint16_t hostshort);
  560. /**
  561. * Convert 32-bit value from network byte order to host byte order.
  562. *
  563. * @param netlong 32-bit network value.
  564. * @return 32-bit host value.
  565. */
  566. PJ_DECL(pj_uint32_t) pj_ntohl(pj_uint32_t netlong);
  567. /**
  568. * Convert 32-bit value from host byte order to network byte order.
  569. *
  570. * @param hostlong 32-bit host value.
  571. * @return 32-bit network value.
  572. */
  573. PJ_DECL(pj_uint32_t) pj_htonl(pj_uint32_t hostlong);
  574. /**
  575. * Convert an Internet host address given in network byte order
  576. * to string in standard numbers and dots notation.
  577. *
  578. * @param inaddr The host address.
  579. * @return The string address.
  580. */
  581. PJ_DECL(char*) pj_inet_ntoa(pj_in_addr inaddr);
  582. /**
  583. * This function converts the Internet host address cp from the standard
  584. * numbers-and-dots notation into binary data and stores it in the structure
  585. * that inp points to.
  586. *
  587. * @param cp IP address in standard numbers-and-dots notation.
  588. * @param inp Structure that holds the output of the conversion.
  589. *
  590. * @return nonzero if the address is valid, zero if not.
  591. */
  592. PJ_DECL(int) pj_inet_aton(const pj_str_t *cp, pj_in_addr *inp);
  593. /**
  594. * This function converts an address in its standard text presentation form
  595. * into its numeric binary form. It supports both IPv4 and IPv6 address
  596. * conversion.
  597. *
  598. * @param af Specify the family of the address. The PJ_AF_INET and
  599. * PJ_AF_INET6 address families shall be supported.
  600. * @param src Points to the string being passed in.
  601. * @param dst Points to a buffer into which the function stores the
  602. * numeric address; this shall be large enough to hold the
  603. * numeric address (32 bits for PJ_AF_INET, 128 bits for
  604. * PJ_AF_INET6).
  605. *
  606. * @return PJ_SUCCESS if conversion was successful.
  607. */
  608. PJ_DECL(pj_status_t) pj_inet_pton(int af, const pj_str_t *src, void *dst);
  609. /**
  610. * This function converts a numeric address into a text string suitable
  611. * for presentation. It supports both IPv4 and IPv6 address
  612. * conversion.
  613. * @see pj_sockaddr_print()
  614. *
  615. * @param af Specify the family of the address. This can be PJ_AF_INET
  616. * or PJ_AF_INET6.
  617. * @param src Points to a buffer holding an IPv4 address if the af argument
  618. * is PJ_AF_INET, or an IPv6 address if the af argument is
  619. * PJ_AF_INET6; the address must be in network byte order.
  620. * @param dst Points to a buffer where the function stores the resulting
  621. * text string; it shall not be NULL.
  622. * @param size Specifies the size of this buffer, which shall be large
  623. * enough to hold the text string (PJ_INET_ADDRSTRLEN characters
  624. * for IPv4, PJ_INET6_ADDRSTRLEN characters for IPv6).
  625. *
  626. * @return PJ_SUCCESS if conversion was successful.
  627. */
  628. PJ_DECL(pj_status_t) pj_inet_ntop(int af, const void *src,
  629. char *dst, int size);
  630. /**
  631. * Converts numeric address into its text string representation.
  632. * @see pj_sockaddr_print()
  633. *
  634. * @param af Specify the family of the address. This can be PJ_AF_INET
  635. * or PJ_AF_INET6.
  636. * @param src Points to a buffer holding an IPv4 address if the af argument
  637. * is PJ_AF_INET, or an IPv6 address if the af argument is
  638. * PJ_AF_INET6; the address must be in network byte order.
  639. * @param dst Points to a buffer where the function stores the resulting
  640. * text string; it shall not be NULL.
  641. * @param size Specifies the size of this buffer, which shall be large
  642. * enough to hold the text string (PJ_INET_ADDRSTRLEN characters
  643. * for IPv4, PJ_INET6_ADDRSTRLEN characters for IPv6).
  644. *
  645. * @return The address string or NULL if failed.
  646. */
  647. PJ_DECL(char*) pj_inet_ntop2(int af, const void *src,
  648. char *dst, int size);
  649. /**
  650. * Print socket address.
  651. *
  652. * @param addr The socket address.
  653. * @param buf Text buffer.
  654. * @param size Size of buffer.
  655. * @param flags Bitmask combination of these value:
  656. * - 1: port number is included.
  657. * - 2: square bracket is included for IPv6 address.
  658. *
  659. * @return The address string.
  660. */
  661. PJ_DECL(char*) pj_sockaddr_print(const pj_sockaddr_t *addr,
  662. char *buf, int size,
  663. unsigned flags);
  664. /**
  665. * Convert address string with numbers and dots to binary IP address.
  666. *
  667. * @param cp The IP address in numbers and dots notation.
  668. * @return If success, the IP address is returned in network
  669. * byte order. If failed, PJ_INADDR_NONE will be
  670. * returned.
  671. * @remark
  672. * This is an obsolete interface to #pj_inet_aton(); it is obsolete
  673. * because -1 is a valid address (255.255.255.255), and #pj_inet_aton()
  674. * provides a cleaner way to indicate error return.
  675. */
  676. PJ_DECL(pj_in_addr) pj_inet_addr(const pj_str_t *cp);
  677. /**
  678. * Convert address string with numbers and dots to binary IP address.
  679. *
  680. * @param cp The IP address in numbers and dots notation.
  681. * @return If success, the IP address is returned in network
  682. * byte order. If failed, PJ_INADDR_NONE will be
  683. * returned.
  684. * @remark
  685. * This is an obsolete interface to #pj_inet_aton(); it is obsolete
  686. * because -1 is a valid address (255.255.255.255), and #pj_inet_aton()
  687. * provides a cleaner way to indicate error return.
  688. */
  689. PJ_DECL(pj_in_addr) pj_inet_addr2(const char *cp);
  690. /**
  691. * Initialize IPv4 socket address based on the address and port info.
  692. * The string address may be in a standard numbers and dots notation or
  693. * may be a hostname. If hostname is specified, then the function will
  694. * resolve the host into the IP address.
  695. *
  696. * @see pj_sockaddr_init()
  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. * @param port The port number, in host byte order.
  702. *
  703. * @return Zero on success.
  704. */
  705. PJ_DECL(pj_status_t) pj_sockaddr_in_init( pj_sockaddr_in *addr,
  706. const pj_str_t *cp,
  707. pj_uint16_t port);
  708. /**
  709. * Initialize IP socket address based on the address and port info.
  710. * The string address may be in a standard numbers and dots notation or
  711. * may be a hostname. If hostname is specified, then the function will
  712. * resolve the host into the IP address.
  713. *
  714. * @see pj_sockaddr_in_init()
  715. *
  716. * @param af Internet address family.
  717. * @param addr The IP socket address to be set.
  718. * @param cp The address string, which can be in a standard
  719. * dotted numbers or a hostname to be resolved.
  720. * @param port The port number, in host byte order.
  721. *
  722. * @return Zero on success.
  723. */
  724. PJ_DECL(pj_status_t) pj_sockaddr_init(int af,
  725. pj_sockaddr *addr,
  726. const pj_str_t *cp,
  727. pj_uint16_t port);
  728. /**
  729. * Compare two socket addresses.
  730. *
  731. * @param addr1 First address.
  732. * @param addr2 Second address.
  733. *
  734. * @return Zero on equal, -1 if addr1 is less than addr2,
  735. * and +1 if addr1 is more than addr2.
  736. */
  737. PJ_DECL(int) pj_sockaddr_cmp(const pj_sockaddr_t *addr1,
  738. const pj_sockaddr_t *addr2);
  739. /**
  740. * Get pointer to the address part of a socket address.
  741. *
  742. * @param addr Socket address.
  743. *
  744. * @return Pointer to address part (sin_addr or sin6_addr,
  745. * depending on address family)
  746. */
  747. PJ_DECL(void*) pj_sockaddr_get_addr(const pj_sockaddr_t *addr);
  748. /**
  749. * Check that a socket address contains a non-zero address part.
  750. *
  751. * @param addr Socket address.
  752. *
  753. * @return Non-zero if address is set to non-zero.
  754. */
  755. PJ_DECL(pj_bool_t) pj_sockaddr_has_addr(const pj_sockaddr_t *addr);
  756. /**
  757. * Get the address part length of a socket address, based on its address
  758. * family. For PJ_AF_INET, the length will be sizeof(pj_in_addr), and
  759. * for PJ_AF_INET6, the length will be sizeof(pj_in6_addr).
  760. *
  761. * @param addr Socket address.
  762. *
  763. * @return Length in bytes.
  764. */
  765. PJ_DECL(unsigned) pj_sockaddr_get_addr_len(const pj_sockaddr_t *addr);
  766. /**
  767. * Get the socket address length, based on its address
  768. * family. For PJ_AF_INET, the length will be sizeof(pj_sockaddr_in), and
  769. * for PJ_AF_INET6, the length will be sizeof(pj_sockaddr_in6).
  770. *
  771. * @param addr Socket address.
  772. *
  773. * @return Length in bytes.
  774. */
  775. PJ_DECL(unsigned) pj_sockaddr_get_len(const pj_sockaddr_t *addr);
  776. /**
  777. * Copy only the address part (sin_addr/sin6_addr) of a socket address.
  778. *
  779. * @param dst Destination socket address.
  780. * @param src Source socket address.
  781. *
  782. * @see pj_sockaddr_cp()
  783. */
  784. PJ_DECL(void) pj_sockaddr_copy_addr(pj_sockaddr *dst,
  785. const pj_sockaddr *src);
  786. /**
  787. * Copy socket address. This will copy the whole structure depending
  788. * on the address family of the source socket address.
  789. *
  790. * @param dst Destination socket address.
  791. * @param src Source socket address.
  792. *
  793. * @see pj_sockaddr_copy_addr()
  794. */
  795. PJ_DECL(void) pj_sockaddr_cp(pj_sockaddr_t *dst, const pj_sockaddr_t *src);
  796. /**
  797. * If the source's and desired address family matches, copy the address,
  798. * otherwise synthesize a new address with the desired address family,
  799. * from the source address. This can be useful to generate an IPv4-mapped
  800. * IPv6 address.
  801. *
  802. * @param dst_af Desired address family.
  803. * @param dst Destination socket address, invalid if synthesis is
  804. * required and failed.
  805. * @param src Source socket address.
  806. *
  807. * @return PJ_SUCCESS on success, or the error status
  808. * if synthesis is required and failed.
  809. */
  810. PJ_DECL(pj_status_t) pj_sockaddr_synthesize(int dst_af,
  811. pj_sockaddr_t *dst,
  812. const pj_sockaddr_t *src);
  813. /**
  814. * Get the IP address of an IPv4 socket address.
  815. * The address is returned as 32bit value in host byte order.
  816. *
  817. * @param addr The IP socket address.
  818. * @return 32bit address, in host byte order.
  819. */
  820. PJ_DECL(pj_in_addr) pj_sockaddr_in_get_addr(const pj_sockaddr_in *addr);
  821. /**
  822. * Set the IP address of an IPv4 socket address.
  823. *
  824. * @param addr The IP socket address.
  825. * @param hostaddr The host address, in host byte order.
  826. */
  827. PJ_DECL(void) pj_sockaddr_in_set_addr(pj_sockaddr_in *addr,
  828. pj_uint32_t hostaddr);
  829. /**
  830. * Set the IP address of an IP socket address from string address,
  831. * with resolving the host if necessary. The string address may be in a
  832. * standard numbers and dots notation or may be a hostname. If hostname
  833. * is specified, then the function will resolve the host into the IP
  834. * address.
  835. *
  836. * @see pj_sockaddr_set_str_addr()
  837. *
  838. * @param addr The IP socket address to be set.
  839. * @param cp The address string, which can be in a standard
  840. * dotted numbers or a hostname to be resolved.
  841. *
  842. * @return PJ_SUCCESS on success.
  843. */
  844. PJ_DECL(pj_status_t) pj_sockaddr_in_set_str_addr( pj_sockaddr_in *addr,
  845. const pj_str_t *cp);
  846. /**
  847. * Set the IP address of an IPv4 or IPv6 socket address from string address,
  848. * with resolving the host if necessary. The string address may be in a
  849. * standard IPv6 or IPv6 address or may be a hostname. If hostname
  850. * is specified, then the function will resolve the host into the IP
  851. * address according to the address family.
  852. *
  853. * @param af Address family.
  854. * @param addr The IP socket address to be set.
  855. * @param cp The address string, which can be in a standard
  856. * IP numbers (IPv4 or IPv6) or a hostname to be resolved.
  857. *
  858. * @return PJ_SUCCESS on success.
  859. */
  860. PJ_DECL(pj_status_t) pj_sockaddr_set_str_addr(int af,
  861. pj_sockaddr *addr,
  862. const pj_str_t *cp);
  863. /**
  864. * Get the port number of a socket address, in host byte order.
  865. * This function can be used for both IPv4 and IPv6 socket address.
  866. *
  867. * @param addr Socket address.
  868. *
  869. * @return Port number, in host byte order.
  870. */
  871. PJ_DECL(pj_uint16_t) pj_sockaddr_get_port(const pj_sockaddr_t *addr);
  872. /**
  873. * Get the transport layer port number of an Internet socket address.
  874. * The port is returned in host byte order.
  875. *
  876. * @param addr The IP socket address.
  877. * @return Port number, in host byte order.
  878. */
  879. PJ_DECL(pj_uint16_t) pj_sockaddr_in_get_port(const pj_sockaddr_in *addr);
  880. /**
  881. * Set the port number of an Internet socket address.
  882. *
  883. * @param addr The socket address.
  884. * @param hostport The port number, in host byte order.
  885. */
  886. PJ_DECL(pj_status_t) pj_sockaddr_set_port(pj_sockaddr *addr,
  887. pj_uint16_t hostport);
  888. /**
  889. * Set the port number of an IPv4 socket address.
  890. *
  891. * @see pj_sockaddr_set_port()
  892. *
  893. * @param addr The IP socket address.
  894. * @param hostport The port number, in host byte order.
  895. */
  896. PJ_DECL(void) pj_sockaddr_in_set_port(pj_sockaddr_in *addr,
  897. pj_uint16_t hostport);
  898. /**
  899. * Parse string containing IP address and optional port into socket address,
  900. * possibly also with address family detection. This function supports both
  901. * IPv4 and IPv6 parsing, however IPv6 parsing may only be done if IPv6 is
  902. * enabled during compilation.
  903. *
  904. * This function supports parsing several formats. Sample IPv4 inputs and
  905. * their default results::
  906. * - "10.0.0.1:80": address 10.0.0.1 and port 80.
  907. * - "10.0.0.1": address 10.0.0.1 and port zero.
  908. * - "10.0.0.1:": address 10.0.0.1 and port zero.
  909. * - "10.0.0.1:0": address 10.0.0.1 and port zero.
  910. * - ":80": address 0.0.0.0 and port 80.
  911. * - ":": address 0.0.0.0 and port 0.
  912. * - "localhost": address 127.0.0.1 and port 0.
  913. * - "localhost:": address 127.0.0.1 and port 0.
  914. * - "localhost:80": address 127.0.0.1 and port 80.
  915. *
  916. * Sample IPv6 inputs and their default results:
  917. * - "[fec0::01]:80": address fec0::01 and port 80
  918. * - "[fec0::01]": address fec0::01 and port 0
  919. * - "[fec0::01]:": address fec0::01 and port 0
  920. * - "[fec0::01]:0": address fec0::01 and port 0
  921. * - "fec0::01": address fec0::01 and port 0
  922. * - "fec0::01:80": address fec0::01:80 and port 0
  923. * - "::": address zero (::) and port 0
  924. * - "[::]": address zero (::) and port 0
  925. * - "[::]:": address zero (::) and port 0
  926. * - ":::": address zero (::) and port 0
  927. * - "[::]:80": address zero (::) and port 0
  928. * - ":::80": address zero (::) and port 80
  929. *
  930. * Note: when the IPv6 socket address contains port number, the IP
  931. * part of the socket address should be enclosed with square brackets,
  932. * otherwise the port number will be included as part of the IP address
  933. * (see "fec0::01:80" example above).
  934. *
  935. * @param af Optionally specify the address family to be used. If the
  936. * address family is to be deducted from the input, specify
  937. * pj_AF_UNSPEC() here. Other supported values are
  938. * #pj_AF_INET() and #pj_AF_INET6()
  939. * @param options Additional options to assist the parsing, must be zero
  940. * for now.
  941. * @param str The input string to be parsed.
  942. * @param addr Pointer to store the result.
  943. *
  944. * @return PJ_SUCCESS if the parsing is successful.
  945. *
  946. * @see pj_sockaddr_parse2()
  947. */
  948. PJ_DECL(pj_status_t) pj_sockaddr_parse(int af, unsigned options,
  949. const pj_str_t *str,
  950. pj_sockaddr *addr);
  951. /**
  952. * This function is similar to #pj_sockaddr_parse(), except that it will not
  953. * convert the hostpart into IP address (thus possibly resolving the hostname
  954. * into a #pj_sockaddr.
  955. *
  956. * Unlike #pj_sockaddr_parse(), this function has a limitation that if port
  957. * number is specified in an IPv6 input string, the IP part of the IPv6 socket
  958. * address MUST be enclosed in square brackets, otherwise the port number will
  959. * be considered as part of the IPv6 IP address.
  960. *
  961. * @param af Optionally specify the address family to be used. If the
  962. * address family is to be deducted from the input, specify
  963. * #pj_AF_UNSPEC() here. Other supported values are
  964. * #pj_AF_INET() and #pj_AF_INET6()
  965. * @param options Additional options to assist the parsing, must be zero
  966. * for now.
  967. * @param str The input string to be parsed.
  968. * @param hostpart Optional pointer to store the host part of the socket
  969. * address, with any brackets removed.
  970. * @param port Optional pointer to store the port number. If port number
  971. * is not found, this will be set to zero upon return.
  972. * @param raf Optional pointer to store the detected address family of
  973. * the input address.
  974. *
  975. * @return PJ_SUCCESS if the parsing is successful.
  976. *
  977. * @see pj_sockaddr_parse()
  978. */
  979. PJ_DECL(pj_status_t) pj_sockaddr_parse2(int af, unsigned options,
  980. const pj_str_t *str,
  981. pj_str_t *hostpart,
  982. pj_uint16_t *port,
  983. int *raf);
  984. /*****************************************************************************
  985. *
  986. * HOST NAME AND ADDRESS.
  987. *
  988. *****************************************************************************
  989. */
  990. /**
  991. * Get system's host name.
  992. *
  993. * @return The hostname, or empty string if the hostname can not
  994. * be identified.
  995. */
  996. PJ_DECL(const pj_str_t*) pj_gethostname(void);
  997. /**
  998. * Get host's IP address, which the the first IP address that is resolved
  999. * from the hostname.
  1000. *
  1001. * @return The host's IP address, PJ_INADDR_NONE if the host
  1002. * IP address can not be identified.
  1003. */
  1004. PJ_DECL(pj_in_addr) pj_gethostaddr(void);
  1005. /*****************************************************************************
  1006. *
  1007. * SOCKET API.
  1008. *
  1009. *****************************************************************************
  1010. */
  1011. /**
  1012. * Create new socket/endpoint for communication.
  1013. *
  1014. * @param family Specifies a communication domain; this selects the
  1015. * protocol family which will be used for communication.
  1016. * @param type The socket has the indicated type, which specifies the
  1017. * communication semantics.
  1018. * @param protocol Specifies a particular protocol to be used with the
  1019. * socket. Normally only a single protocol exists to support
  1020. * a particular socket type within a given protocol family,
  1021. * in which a case protocol can be specified as 0.
  1022. * @param sock New socket descriptor, or PJ_INVALID_SOCKET on error.
  1023. *
  1024. * @return Zero on success.
  1025. */
  1026. PJ_DECL(pj_status_t) pj_sock_socket(int family,
  1027. int type,
  1028. int protocol,
  1029. pj_sock_t *sock);
  1030. /**
  1031. * Close the socket descriptor.
  1032. *
  1033. * @param sockfd The socket descriptor.
  1034. *
  1035. * @return Zero on success.
  1036. */
  1037. PJ_DECL(pj_status_t) pj_sock_close(pj_sock_t sockfd);
  1038. /**
  1039. * This function gives the socket sockfd the local address my_addr. my_addr is
  1040. * addrlen bytes long. Traditionally, this is called assigning a name to
  1041. * a socket. When a socket is created with #pj_sock_socket(), it exists in a
  1042. * name space (address family) but has no name assigned.
  1043. *
  1044. * @param sockfd The socket desriptor.
  1045. * @param my_addr The local address to bind the socket to.
  1046. * @param addrlen The length of the address.
  1047. *
  1048. * @return Zero on success.
  1049. */
  1050. PJ_DECL(pj_status_t) pj_sock_bind( pj_sock_t sockfd,
  1051. const pj_sockaddr_t *my_addr,
  1052. int addrlen);
  1053. /**
  1054. * Bind the IP socket sockfd to the given address and port.
  1055. *
  1056. * @param sockfd The socket descriptor.
  1057. * @param addr Local address to bind the socket to, in host byte order.
  1058. * @param port The local port to bind the socket to, in host byte order.
  1059. *
  1060. * @return Zero on success.
  1061. */
  1062. PJ_DECL(pj_status_t) pj_sock_bind_in( pj_sock_t sockfd,
  1063. pj_uint32_t addr,
  1064. pj_uint16_t port);
  1065. /**
  1066. * Bind the IP socket sockfd to the given address and a random port in the
  1067. * specified range.
  1068. *
  1069. * @param sockfd The socket desriptor.
  1070. * @param addr The local address and port to bind the socket to.
  1071. * @param port_range The port range, relative the to start port number
  1072. * specified in port field in addr. Note that if the
  1073. * port is zero, this param will be ignored.
  1074. * @param max_try Maximum retries.
  1075. *
  1076. * @return Zero on success.
  1077. */
  1078. PJ_DECL(pj_status_t) pj_sock_bind_random( pj_sock_t sockfd,
  1079. const pj_sockaddr_t *addr,
  1080. pj_uint16_t port_range,
  1081. pj_uint16_t max_try);
  1082. #if PJ_HAS_TCP
  1083. /**
  1084. * Listen for incoming connection. This function only applies to connection
  1085. * oriented sockets (such as PJ_SOCK_STREAM or PJ_SOCK_SEQPACKET), and it
  1086. * indicates the willingness to accept incoming connections.
  1087. *
  1088. * @param sockfd The socket descriptor.
  1089. * @param backlog Defines the maximum length the queue of pending
  1090. * connections may grow to.
  1091. *
  1092. * @return Zero on success.
  1093. */
  1094. PJ_DECL(pj_status_t) pj_sock_listen( pj_sock_t sockfd,
  1095. int backlog );
  1096. /**
  1097. * Accept new connection on the specified connection oriented server socket.
  1098. *
  1099. * @param serverfd The server socket.
  1100. * @param newsock New socket on success, of PJ_INVALID_SOCKET if failed.
  1101. * @param addr A pointer to sockaddr type. If the argument is not NULL,
  1102. * it will be filled by the address of connecting entity.
  1103. * @param addrlen Initially specifies the length of the address, and upon
  1104. * return will be filled with the exact address length.
  1105. *
  1106. * @return Zero on success, or the error number.
  1107. */
  1108. PJ_DECL(pj_status_t) pj_sock_accept( pj_sock_t serverfd,
  1109. pj_sock_t *newsock,
  1110. pj_sockaddr_t *addr,
  1111. int *addrlen);
  1112. #endif
  1113. /**
  1114. * The file descriptor sockfd must refer to a socket. If the socket is of
  1115. * type PJ_SOCK_DGRAM then the serv_addr address is the address to which
  1116. * datagrams are sent by default, and the only address from which datagrams
  1117. * are received. If the socket is of type PJ_SOCK_STREAM or PJ_SOCK_SEQPACKET,
  1118. * this call attempts to make a connection to another socket. The
  1119. * other socket is specified by serv_addr, which is an address (of length
  1120. * addrlen) in the communications space of the socket. Each communications
  1121. * space interprets the serv_addr parameter in its own way.
  1122. *
  1123. * @param sockfd The socket descriptor.
  1124. * @param serv_addr Server address to connect to.
  1125. * @param addrlen The length of server address.
  1126. *
  1127. * @return Zero on success.
  1128. */
  1129. PJ_DECL(pj_status_t) pj_sock_connect( pj_sock_t sockfd,
  1130. const pj_sockaddr_t *serv_addr,
  1131. int addrlen);
  1132. /**
  1133. * Return the address of peer which is connected to socket sockfd.
  1134. *
  1135. * @param sockfd The socket descriptor.
  1136. * @param addr Pointer to sockaddr structure to which the address
  1137. * will be returned.
  1138. * @param namelen Initially the length of the addr. Upon return the value
  1139. * will be set to the actual length of the address.
  1140. *
  1141. * @return Zero on success.
  1142. */
  1143. PJ_DECL(pj_status_t) pj_sock_getpeername(pj_sock_t sockfd,
  1144. pj_sockaddr_t *addr,
  1145. int *namelen);
  1146. /**
  1147. * Return the current name of the specified socket.
  1148. *
  1149. * @param sockfd The socket descriptor.
  1150. * @param addr Pointer to sockaddr structure to which the address
  1151. * will be returned.
  1152. * @param namelen Initially the length of the addr. Upon return the value
  1153. * will be set to the actual length of the address.
  1154. *
  1155. * @return Zero on success.
  1156. */
  1157. PJ_DECL(pj_status_t) pj_sock_getsockname( pj_sock_t sockfd,
  1158. pj_sockaddr_t *addr,
  1159. int *namelen);
  1160. /**
  1161. * Get socket option associated with a socket. Options may exist at multiple
  1162. * protocol levels; they are always present at the uppermost socket level.
  1163. *
  1164. * @param sockfd The socket descriptor.
  1165. * @param level The level which to get the option from.
  1166. * @param optname The option name.
  1167. * @param optval Identifies the buffer which the value will be
  1168. * returned.
  1169. * @param optlen Initially contains the length of the buffer, upon
  1170. * return will be set to the actual size of the value.
  1171. *
  1172. * @return Zero on success.
  1173. */
  1174. PJ_DECL(pj_status_t) pj_sock_getsockopt( pj_sock_t sockfd,
  1175. pj_uint16_t level,
  1176. pj_uint16_t optname,
  1177. void *optval,
  1178. int *optlen);
  1179. /**
  1180. * Manipulate the options associated with a socket. Options may exist at
  1181. * multiple protocol levels; they are always present at the uppermost socket
  1182. * level.
  1183. *
  1184. * @param sockfd The socket descriptor.
  1185. * @param level The level which to get the option from.
  1186. * @param optname The option name.
  1187. * @param optval Identifies the buffer which contain the value.
  1188. * @param optlen The length of the value.
  1189. *
  1190. * @return PJ_SUCCESS or the status code.
  1191. */
  1192. PJ_DECL(pj_status_t) pj_sock_setsockopt( pj_sock_t sockfd,
  1193. pj_uint16_t level,
  1194. pj_uint16_t optname,
  1195. const void *optval,
  1196. int optlen);
  1197. /**
  1198. * Set socket options associated with a socket. This method will apply all the
  1199. * options specified, and ignore any errors that might be raised.
  1200. *
  1201. * @param sockfd The socket descriptor.
  1202. * @param params The socket options.
  1203. *
  1204. * @return PJ_SUCCESS or the last error code.
  1205. */
  1206. PJ_DECL(pj_status_t) pj_sock_setsockopt_params( pj_sock_t sockfd,
  1207. const pj_sockopt_params *params);
  1208. /**
  1209. * Helper function to set socket buffer size using #pj_sock_setsockopt()
  1210. * with capability to auto retry with lower buffer setting value until
  1211. * the highest possible value is successfully set.
  1212. *
  1213. * @param sockfd The socket descriptor.
  1214. * @param optname The option name, valid values are pj_SO_RCVBUF()
  1215. * and pj_SO_SNDBUF().
  1216. * @param auto_retry Option whether auto retry with lower value is
  1217. * enabled.
  1218. * @param buf_size On input, specify the prefered buffer size setting,
  1219. * on output, the buffer size setting applied.
  1220. *
  1221. * @return PJ_SUCCESS or the status code.
  1222. */
  1223. PJ_DECL(pj_status_t) pj_sock_setsockopt_sobuf( pj_sock_t sockfd,
  1224. pj_uint16_t optname,
  1225. pj_bool_t auto_retry,
  1226. unsigned *buf_size);
  1227. /**
  1228. * Receives data stream or message coming to the specified socket.
  1229. *
  1230. * @param sockfd The socket descriptor.
  1231. * @param buf The buffer to receive the data or message.
  1232. * @param len On input, the length of the buffer. On return,
  1233. * contains the length of data received.
  1234. * @param flags Flags (such as pj_MSG_PEEK()).
  1235. *
  1236. * @return PJ_SUCCESS or the error code.
  1237. */
  1238. PJ_DECL(pj_status_t) pj_sock_recv(pj_sock_t sockfd,
  1239. void *buf,
  1240. pj_ssize_t *len,
  1241. unsigned flags);
  1242. /**
  1243. * Receives data stream or message coming to the specified socket.
  1244. *
  1245. * @param sockfd The socket descriptor.
  1246. * @param buf The buffer to receive the data or message.
  1247. * @param len On input, the length of the buffer. On return,
  1248. * contains the length of data received.
  1249. * @param flags Flags (such as pj_MSG_PEEK()).
  1250. * @param from If not NULL, it will be filled with the source
  1251. * address of the connection.
  1252. * @param fromlen Initially contains the length of from address,
  1253. * and upon return will be filled with the actual
  1254. * length of the address.
  1255. *
  1256. * @return PJ_SUCCESS or the error code.
  1257. */
  1258. PJ_DECL(pj_status_t) pj_sock_recvfrom( pj_sock_t sockfd,
  1259. void *buf,
  1260. pj_ssize_t *len,
  1261. unsigned flags,
  1262. pj_sockaddr_t *from,
  1263. int *fromlen);
  1264. /**
  1265. * Transmit data to the socket.
  1266. *
  1267. * @param sockfd Socket descriptor.
  1268. * @param buf Buffer containing data to be sent.
  1269. * @param len On input, the length of the data in the buffer.
  1270. * Upon return, it will be filled with the length
  1271. * of data sent.
  1272. * @param flags Flags (such as pj_MSG_DONTROUTE()).
  1273. *
  1274. * @return PJ_SUCCESS or the status code.
  1275. */
  1276. PJ_DECL(pj_status_t) pj_sock_send(pj_sock_t sockfd,
  1277. const void *buf,
  1278. pj_ssize_t *len,
  1279. unsigned flags);
  1280. /**
  1281. * Transmit data to the socket to the specified address.
  1282. *
  1283. * @param sockfd Socket descriptor.
  1284. * @param buf Buffer containing data to be sent.
  1285. * @param len On input, the length of the data in the buffer.
  1286. * Upon return, it will be filled with the length
  1287. * of data sent.
  1288. * @param flags Flags (such as pj_MSG_DONTROUTE()).
  1289. * @param to The address to send.
  1290. * @param tolen The length of the address in bytes.
  1291. *
  1292. * @return PJ_SUCCESS or the status code.
  1293. */
  1294. PJ_DECL(pj_status_t) pj_sock_sendto(pj_sock_t sockfd,
  1295. const void *buf,
  1296. pj_ssize_t *len,
  1297. unsigned flags,
  1298. const pj_sockaddr_t *to,
  1299. int tolen);
  1300. #if PJ_HAS_TCP
  1301. /**
  1302. * The shutdown call causes all or part of a full-duplex connection on the
  1303. * socket associated with sockfd to be shut down.
  1304. *
  1305. * @param sockfd The socket descriptor.
  1306. * @param how If how is PJ_SHUT_RD, further receptions will be
  1307. * disallowed. If how is PJ_SHUT_WR, further transmissions
  1308. * will be disallowed. If how is PJ_SHUT_RDWR, further
  1309. * receptions andtransmissions will be disallowed.
  1310. *
  1311. * @return Zero on success.
  1312. */
  1313. PJ_DECL(pj_status_t) pj_sock_shutdown( pj_sock_t sockfd,
  1314. int how);
  1315. #endif
  1316. /*****************************************************************************
  1317. *
  1318. * Utilities.
  1319. *
  1320. *****************************************************************************
  1321. */
  1322. /**
  1323. * Deep clone the socket options.
  1324. *
  1325. * @param pool The pool.
  1326. * @param dst Destination socket options.
  1327. * @param src Source socket options.
  1328. *
  1329. * @return PJ_SUCCESS on success.
  1330. */
  1331. PJ_DECL(pj_status_t) pj_sockopt_params_clone(pj_pool_t *pool,
  1332. pj_sockopt_params *dst,
  1333. const pj_sockopt_params *src);
  1334. /**
  1335. * Print socket address string. This method will enclose the address string
  1336. * with square bracket if it's IPv6 address.
  1337. *
  1338. * @param host_str The host address string.
  1339. * @param port The port address.
  1340. * @param buf Text buffer.
  1341. * @param size Size of buffer.
  1342. * @param flag Bitmask combination of these value:
  1343. * - 1: port number is included.
  1344. *
  1345. * @return The address string.
  1346. */
  1347. PJ_DECL(char *) pj_addr_str_print( const pj_str_t *host_str, int port,
  1348. char *buf, int size, unsigned flag);
  1349. /**
  1350. * Create socket pair
  1351. * @param family Specifies a communication domain; this selects the
  1352. * protocol family which will be used for communication.
  1353. * On Unix, support AF_UNIX, AF_INET and AF_INET6,
  1354. * On Win32, not support AF_UNIX.
  1355. * @param type The socket has the indicated type, which specifies the
  1356. * communication semantics.
  1357. * @param protocol Specifies a particular protocol to be used with the
  1358. * socket. Normally only a single protocol exists to support
  1359. * a particular socket type within a given protocol family,
  1360. * in which a case protocol can be specified as 0.
  1361. * @param sv The new sockets vector
  1362. *
  1363. * @return Zero on success.
  1364. *
  1365. */
  1366. PJ_DECL(pj_status_t) pj_sock_socketpair(int family,
  1367. int type,
  1368. int protocol,
  1369. pj_sock_t sv[2]);
  1370. /**
  1371. * @}
  1372. */
  1373. PJ_END_DECL
  1374. #endif /* __PJ_SOCK_H__ */