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