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