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