Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 1 | /* $Id$ */ |
| 2 | /* |
| 3 | * Copyright (C)2003-2006 Benny Prijono <benny@prijono.org> |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 2 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 18 | */ |
| 19 | #ifndef __PJ_SOCK_H__ |
| 20 | #define __PJ_SOCK_H__ |
| 21 | |
| 22 | /** |
| 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 | */ |
| 62 | extern const pj_uint16_t PJ_AF_UNIX; /**< Unix domain socket. */ |
| 63 | #define PJ_AF_LOCAL PJ_AF_UNIX; /**< POSIX name for AF_UNIX */ |
| 64 | extern const pj_uint16_t PJ_AF_INET; /**< Internet IP protocol. */ |
| 65 | extern const pj_uint16_t PJ_AF_INET6; /**< IP version 6. */ |
| 66 | extern const pj_uint16_t PJ_AF_PACKET; /**< Packet family. */ |
| 67 | extern const pj_uint16_t PJ_AF_IRDA; /**< IRDA sockets. */ |
| 68 | |
| 69 | |
| 70 | /** |
| 71 | * Supported types of sockets. |
| 72 | * APPLICATION MUST USE THESE VALUES INSTEAD OF NORMAL SOCK_*, BECAUSE |
| 73 | * THE LIBRARY WILL TRANSLATE THE VALUE TO THE NATIVE VALUE. |
| 74 | */ |
| 75 | |
| 76 | extern const pj_uint16_t PJ_SOCK_STREAM; /**< Sequenced, reliable, connection- |
| 77 | based byte streams. */ |
| 78 | extern const pj_uint16_t PJ_SOCK_DGRAM; /**< Connectionless, unreliable |
| 79 | datagrams of fixed maximum |
| 80 | lengths. */ |
| 81 | extern const pj_uint16_t PJ_SOCK_RAW; /**< Raw protocol interface. */ |
| 82 | extern const pj_uint16_t PJ_SOCK_RDM; /**< Reliably-delivered messages. */ |
| 83 | |
| 84 | |
| 85 | /** |
| 86 | * Socket level specified in #pj_sock_setsockopt() or #pj_sock_getsockopt(). |
| 87 | * APPLICATION MUST USE THESE VALUES INSTEAD OF NORMAL SOL_*, BECAUSE |
| 88 | * THE LIBRARY WILL TRANSLATE THE VALUE TO THE NATIVE VALUE. |
| 89 | */ |
| 90 | extern const pj_uint16_t PJ_SOL_SOCKET; /**< Socket level. */ |
| 91 | extern const pj_uint16_t PJ_SOL_IP; /**< IP level. */ |
| 92 | extern const pj_uint16_t PJ_SOL_TCP; /**< TCP level. */ |
| 93 | extern const pj_uint16_t PJ_SOL_UDP; /**< UDP level. */ |
| 94 | extern const pj_uint16_t PJ_SOL_IPV6; /**< IP version 6 */ |
| 95 | |
| 96 | /** |
| 97 | * Values to be specified as \c optname when calling #pj_sock_setsockopt() |
| 98 | * or #pj_sock_getsockopt(). |
| 99 | */ |
| 100 | extern const pj_uint16_t PJ_SO_TYPE; /**< Socket type. */ |
| 101 | extern const pj_uint16_t PJ_SO_RCVBUF; /**< Buffer size for receive. */ |
| 102 | extern const pj_uint16_t PJ_SO_SNDBUF; /**< Buffer size for send. */ |
| 103 | |
| 104 | |
| 105 | /** |
| 106 | * Flags to be specified in #pj_sock_recv, #pj_sock_send, etc. |
| 107 | */ |
| 108 | typedef enum pj_sock_msg_flag |
| 109 | { |
| 110 | PJ_MSG_OOB = 0x01, /**< Out-of-band messages. */ |
| 111 | PJ_MSG_PEEK = 0x02, /**< Peek, don't remove from buffer. */ |
| 112 | PJ_MSG_DONTROUTE = 0x04, /**< Don't route. */ |
| 113 | } pj_sock_msg_flag; |
| 114 | |
| 115 | |
| 116 | /** |
| 117 | * Flag to be specified in #pj_sock_shutdown. |
| 118 | */ |
| 119 | typedef enum pj_socket_sd_type |
| 120 | { |
| 121 | PJ_SD_RECEIVE = 0, /**< No more receive. */ |
| 122 | PJ_SHUT_RD = 0, /**< Alias for SD_RECEIVE. */ |
| 123 | PJ_SD_SEND = 1, /**< No more sending. */ |
| 124 | PJ_SHUT_WR = 1, /**< Alias for SD_SEND. */ |
| 125 | PJ_SD_BOTH = 2, /**< No more send and receive. */ |
| 126 | PJ_SHUT_RDWR = 2, /**< Alias for SD_BOTH. */ |
| 127 | } pj_socket_sd_type; |
| 128 | |
| 129 | |
| 130 | |
| 131 | /** Address to accept any incoming messages. */ |
| 132 | #define PJ_INADDR_ANY ((pj_uint32_t)0) |
| 133 | |
| 134 | /** Address indicating an error return */ |
| 135 | #define PJ_INADDR_NONE ((pj_uint32_t)0xffffffff) |
| 136 | |
| 137 | /** Address to send to all hosts. */ |
| 138 | #define PJ_INADDR_BROADCAST ((pj_uint32_t)0xffffffff) |
| 139 | |
| 140 | |
| 141 | /** |
| 142 | * Maximum length specifiable by #pj_sock_listen(). |
| 143 | * If the build system doesn't override this value, then the lowest |
| 144 | * denominator (five, in Win32 systems) will be used. |
| 145 | */ |
| 146 | #if !defined(PJ_SOMAXCONN) |
| 147 | # define PJ_SOMAXCONN 5 |
| 148 | #endif |
| 149 | |
| 150 | |
| 151 | /** |
| 152 | * Constant for invalid socket returned by #pj_sock_socket() and |
| 153 | * #pj_sock_accept(). |
| 154 | */ |
| 155 | #define PJ_INVALID_SOCKET (-1) |
| 156 | |
| 157 | /** |
| 158 | * Structure describing a generic socket address. |
| 159 | */ |
| 160 | typedef struct pj_sockaddr |
| 161 | { |
| 162 | pj_uint16_t sa_family; /**< Common data: address family. */ |
| 163 | char sa_data[14]; /**< Address data. */ |
| 164 | } pj_sockaddr; |
| 165 | |
| 166 | |
| 167 | /** |
| 168 | * This structure describes Internet address. |
| 169 | */ |
| 170 | typedef struct pj_in_addr |
| 171 | { |
| 172 | pj_uint32_t s_addr; /**< The 32bit IP address. */ |
| 173 | } pj_in_addr; |
| 174 | |
| 175 | |
| 176 | /** |
| 177 | * This structure describes Internet socket address. |
| 178 | */ |
Benny Prijono | 0ca04b6 | 2005-12-30 23:50:15 +0000 | [diff] [blame] | 179 | struct pj_sockaddr_in |
Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 180 | { |
| 181 | pj_uint16_t sin_family; /**< Address family. */ |
| 182 | pj_uint16_t sin_port; /**< Transport layer port number. */ |
| 183 | pj_in_addr sin_addr; /**< IP address. */ |
| 184 | char sin_zero[8]; /**< Padding. */ |
Benny Prijono | 0ca04b6 | 2005-12-30 23:50:15 +0000 | [diff] [blame] | 185 | }; |
Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 186 | |
| 187 | |
| 188 | /** |
| 189 | * This structure describes IPv6 address. |
| 190 | */ |
| 191 | typedef struct pj_in6_addr |
| 192 | { |
| 193 | /** Union of address formats. */ |
| 194 | union { |
| 195 | pj_uint8_t u6_addr8[16]; /**< u6_addr8 */ |
| 196 | pj_uint16_t u6_addr16[8]; /**< u6_addr16 */ |
| 197 | pj_uint32_t u6_addr32[4]; /**< u6_addr32 */ |
| 198 | } in6_u; |
| 199 | /** Shortcut to access in6_u.u6_addr8. */ |
| 200 | #define s6_addr in6_u.u6_addr8 |
| 201 | /** Shortcut to access in6_u.u6_addr16. */ |
| 202 | #define s6_addr16 in6_u.u6_addr16 |
| 203 | /** Shortcut to access in6_u.u6_addr32. */ |
| 204 | #define s6_addr32 in6_u.u6_addr32 |
| 205 | } pj_in6_addr; |
| 206 | |
| 207 | /** Initializer value for pj_in6_addr. */ |
| 208 | #define PJ_IN6ADDR_ANY_INIT { { { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 } } } |
| 209 | |
| 210 | /** Initializer value for pj_in6_addr. */ |
| 211 | #define PJ_IN6ADDR_LOOPBACK_INIT { { { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1 } } } |
| 212 | |
| 213 | /** |
| 214 | * This structure describes IPv6 socket address. |
| 215 | */ |
| 216 | typedef struct pj_sockaddr_in6 |
| 217 | { |
| 218 | pj_uint16_t sin6_family; /**< Address family */ |
| 219 | pj_uint16_t sin6_port; /**< Transport layer port number. */ |
| 220 | pj_uint32_t sin6_flowinfo; /**< IPv6 flow information */ |
| 221 | pj_in6_addr sin6_addr; /**< IPv6 address. */ |
| 222 | pj_uint32_t sin6_scope_id; /**< IPv6 scope-id */ |
| 223 | } pj_sockaddr_in6; |
| 224 | |
| 225 | |
| 226 | /***************************************************************************** |
| 227 | * |
| 228 | * SOCKET ADDRESS MANIPULATION. |
| 229 | * |
| 230 | ***************************************************************************** |
| 231 | */ |
| 232 | |
| 233 | /** |
| 234 | * Convert 16-bit value from network byte order to host byte order. |
| 235 | * |
| 236 | * @param netshort 16-bit network value. |
| 237 | * @return 16-bit host value. |
| 238 | */ |
| 239 | PJ_DECL(pj_uint16_t) pj_ntohs(pj_uint16_t netshort); |
| 240 | |
| 241 | /** |
| 242 | * Convert 16-bit value from host byte order to network byte order. |
| 243 | * |
| 244 | * @param hostshort 16-bit host value. |
| 245 | * @return 16-bit network value. |
| 246 | */ |
| 247 | PJ_DECL(pj_uint16_t) pj_htons(pj_uint16_t hostshort); |
| 248 | |
| 249 | /** |
| 250 | * Convert 32-bit value from network byte order to host byte order. |
| 251 | * |
| 252 | * @param netlong 32-bit network value. |
| 253 | * @return 32-bit host value. |
| 254 | */ |
| 255 | PJ_DECL(pj_uint32_t) pj_ntohl(pj_uint32_t netlong); |
| 256 | |
| 257 | /** |
| 258 | * Convert 32-bit value from host byte order to network byte order. |
| 259 | * |
| 260 | * @param hostlong 32-bit host value. |
| 261 | * @return 32-bit network value. |
| 262 | */ |
| 263 | PJ_DECL(pj_uint32_t) pj_htonl(pj_uint32_t hostlong); |
| 264 | |
| 265 | /** |
| 266 | * Convert an Internet host address given in network byte order |
| 267 | * to string in standard numbers and dots notation. |
| 268 | * |
| 269 | * @param inaddr The host address. |
| 270 | * @return The string address. |
| 271 | */ |
| 272 | PJ_DECL(char*) pj_inet_ntoa(pj_in_addr inaddr); |
| 273 | |
| 274 | /** |
| 275 | * This function converts the Internet host address cp from the standard |
| 276 | * numbers-and-dots notation into binary data and stores it in the structure |
| 277 | * that inp points to. |
| 278 | * |
| 279 | * @param cp IP address in standard numbers-and-dots notation. |
| 280 | * @param inp Structure that holds the output of the conversion. |
| 281 | * |
| 282 | * @return nonzero if the address is valid, zero if not. |
| 283 | */ |
| 284 | PJ_DECL(int) pj_inet_aton(const pj_str_t *cp, struct pj_in_addr *inp); |
| 285 | |
| 286 | /** |
| 287 | * Convert address string with numbers and dots to binary IP address. |
| 288 | * |
| 289 | * @param cp The IP address in numbers and dots notation. |
| 290 | * @return If success, the IP address is returned in network |
| 291 | * byte order. If failed, PJ_INADDR_NONE will be |
| 292 | * returned. |
| 293 | * @remark |
| 294 | * This is an obsolete interface to #pj_inet_aton(); it is obsolete |
| 295 | * because -1 is a valid address (255.255.255.255), and #pj_inet_aton() |
| 296 | * provides a cleaner way to indicate error return. |
| 297 | */ |
| 298 | PJ_DECL(pj_in_addr) pj_inet_addr(const pj_str_t *cp); |
| 299 | |
| 300 | |
| 301 | /** |
| 302 | * Get the transport layer port number of an Internet socket address. |
| 303 | * The port is returned in host byte order. |
| 304 | * |
| 305 | * @param addr The IP socket address. |
| 306 | * @return Port number, in host byte order. |
| 307 | */ |
| 308 | PJ_INLINE(pj_uint16_t) pj_sockaddr_in_get_port(const pj_sockaddr_in *addr) |
| 309 | { |
| 310 | return pj_ntohs(addr->sin_port); |
| 311 | } |
| 312 | |
| 313 | /** |
| 314 | * Set the port number of an Internet socket address. |
| 315 | * |
| 316 | * @param addr The IP socket address. |
| 317 | * @param hostport The port number, in host byte order. |
| 318 | */ |
| 319 | PJ_INLINE(void) pj_sockaddr_in_set_port(pj_sockaddr_in *addr, |
| 320 | pj_uint16_t hostport) |
| 321 | { |
| 322 | addr->sin_port = pj_htons(hostport); |
| 323 | } |
| 324 | |
| 325 | /** |
| 326 | * Get the IP address of an Internet socket address. |
| 327 | * The address is returned as 32bit value in host byte order. |
| 328 | * |
| 329 | * @param addr The IP socket address. |
| 330 | * @return 32bit address, in host byte order. |
| 331 | */ |
| 332 | PJ_INLINE(pj_in_addr) pj_sockaddr_in_get_addr(const pj_sockaddr_in *addr) |
| 333 | { |
| 334 | pj_in_addr in_addr; |
| 335 | in_addr.s_addr = pj_ntohl(addr->sin_addr.s_addr); |
| 336 | return in_addr; |
| 337 | }; |
| 338 | |
| 339 | /** |
| 340 | * Set the IP address of an Internet socket address. |
| 341 | * |
| 342 | * @param addr The IP socket address. |
| 343 | * @param hostaddr The host address, in host byte order. |
| 344 | */ |
| 345 | PJ_INLINE(void) pj_sockaddr_in_set_addr(pj_sockaddr_in *addr, |
| 346 | pj_uint32_t hostaddr) |
| 347 | { |
| 348 | addr->sin_addr.s_addr = pj_htonl(hostaddr); |
| 349 | } |
| 350 | |
| 351 | /** |
| 352 | * Set the IP address of an IP socket address from string address, |
| 353 | * with resolving the host if necessary. The string address may be in a |
| 354 | * standard numbers and dots notation or may be a hostname. If hostname |
| 355 | * is specified, then the function will resolve the host into the IP |
| 356 | * address. |
| 357 | * |
| 358 | * @param addr The IP socket address to be set. |
| 359 | * @param cp The address string, which can be in a standard |
| 360 | * dotted numbers or a hostname to be resolved. |
| 361 | * |
| 362 | * @return Zero on success. |
| 363 | */ |
| 364 | PJ_DECL(pj_status_t) pj_sockaddr_in_set_str_addr( pj_sockaddr_in *addr, |
| 365 | const pj_str_t *cp); |
| 366 | |
| 367 | /** |
| 368 | * Set the IP address and port of an IP socket address. |
| 369 | * The string address may be in a standard numbers and dots notation or |
| 370 | * may be a hostname. If hostname is specified, then the function will |
| 371 | * resolve the host into the IP address. |
| 372 | * |
| 373 | * @param addr The IP socket address to be set. |
| 374 | * @param cp The address string, which can be in a standard |
| 375 | * dotted numbers or a hostname to be resolved. |
| 376 | * @param port The port number, in host byte order. |
| 377 | * |
| 378 | * @return Zero on success. |
| 379 | */ |
| 380 | PJ_DECL(pj_status_t) pj_sockaddr_in_init( pj_sockaddr_in *addr, |
| 381 | const pj_str_t *cp, |
| 382 | pj_uint16_t port); |
| 383 | |
| 384 | |
| 385 | /***************************************************************************** |
| 386 | * |
| 387 | * HOST NAME AND ADDRESS. |
| 388 | * |
| 389 | ***************************************************************************** |
| 390 | */ |
| 391 | |
| 392 | /** |
| 393 | * Get system's host name. |
| 394 | * |
| 395 | * @return The hostname, or empty string if the hostname can not |
| 396 | * be identified. |
| 397 | */ |
| 398 | PJ_DECL(const pj_str_t*) pj_gethostname(void); |
| 399 | |
| 400 | /** |
| 401 | * Get host's IP address, which the the first IP address that is resolved |
| 402 | * from the hostname. |
| 403 | * |
| 404 | * @return The host's IP address, PJ_INADDR_NONE if the host |
| 405 | * IP address can not be identified. |
| 406 | */ |
| 407 | PJ_DECL(pj_in_addr) pj_gethostaddr(void); |
| 408 | |
| 409 | |
| 410 | /***************************************************************************** |
| 411 | * |
| 412 | * SOCKET API. |
| 413 | * |
| 414 | ***************************************************************************** |
| 415 | */ |
| 416 | |
| 417 | /** |
| 418 | * Create new socket/endpoint for communication. |
| 419 | * |
| 420 | * @param family Specifies a communication domain; this selects the |
| 421 | * protocol family which will be used for communication. |
| 422 | * @param type The socket has the indicated type, which specifies the |
| 423 | * communication semantics. |
| 424 | * @param protocol Specifies a particular protocol to be used with the |
| 425 | * socket. Normally only a single protocol exists to support |
| 426 | * a particular socket type within a given protocol family, |
| 427 | * in which a case protocol can be specified as 0. |
| 428 | * @param sock New socket descriptor, or PJ_INVALID_SOCKET on error. |
| 429 | * |
| 430 | * @return Zero on success. |
| 431 | */ |
| 432 | PJ_DECL(pj_status_t) pj_sock_socket(int family, |
| 433 | int type, |
| 434 | int protocol, |
| 435 | pj_sock_t *sock); |
| 436 | |
| 437 | /** |
| 438 | * Close the socket descriptor. |
| 439 | * |
| 440 | * @param sockfd The socket descriptor. |
| 441 | * |
| 442 | * @return Zero on success. |
| 443 | */ |
| 444 | PJ_DECL(pj_status_t) pj_sock_close(pj_sock_t sockfd); |
| 445 | |
| 446 | |
| 447 | /** |
| 448 | * This function gives the socket sockfd the local address my_addr. my_addr is |
| 449 | * addrlen bytes long. Traditionally, this is called assigning a name to |
| 450 | * a socket. When a socket is created with #pj_sock_socket(), it exists in a |
| 451 | * name space (address family) but has no name assigned. |
| 452 | * |
| 453 | * @param sockfd The socket desriptor. |
| 454 | * @param my_addr The local address to bind the socket to. |
| 455 | * @param addrlen The length of the address. |
| 456 | * |
| 457 | * @return Zero on success. |
| 458 | */ |
| 459 | PJ_DECL(pj_status_t) pj_sock_bind( pj_sock_t sockfd, |
| 460 | const pj_sockaddr_t *my_addr, |
| 461 | int addrlen); |
| 462 | |
| 463 | /** |
| 464 | * Bind the IP socket sockfd to the given address and port. |
| 465 | * |
| 466 | * @param sockfd The socket descriptor. |
| 467 | * @param addr Local address to bind the socket to, in host byte order. |
| 468 | * @param port The local port to bind the socket to, in host byte order. |
| 469 | * |
| 470 | * @return Zero on success. |
| 471 | */ |
| 472 | PJ_DECL(pj_status_t) pj_sock_bind_in( pj_sock_t sockfd, |
| 473 | pj_uint32_t addr, |
| 474 | pj_uint16_t port); |
| 475 | |
| 476 | #if PJ_HAS_TCP |
| 477 | /** |
| 478 | * Listen for incoming connection. This function only applies to connection |
| 479 | * oriented sockets (such as PJ_SOCK_STREAM or PJ_SOCK_SEQPACKET), and it |
| 480 | * indicates the willingness to accept incoming connections. |
| 481 | * |
| 482 | * @param sockfd The socket descriptor. |
| 483 | * @param backlog Defines the maximum length the queue of pending |
| 484 | * connections may grow to. |
| 485 | * |
| 486 | * @return Zero on success. |
| 487 | */ |
| 488 | PJ_DECL(pj_status_t) pj_sock_listen( pj_sock_t sockfd, |
| 489 | int backlog ); |
| 490 | |
| 491 | /** |
| 492 | * Accept new connection on the specified connection oriented server socket. |
| 493 | * |
| 494 | * @param serverfd The server socket. |
| 495 | * @param newsock New socket on success, of PJ_INVALID_SOCKET if failed. |
| 496 | * @param addr A pointer to sockaddr type. If the argument is not NULL, |
| 497 | * it will be filled by the address of connecting entity. |
| 498 | * @param addrlen Initially specifies the length of the address, and upon |
| 499 | * return will be filled with the exact address length. |
| 500 | * |
| 501 | * @return Zero on success, or the error number. |
| 502 | */ |
| 503 | PJ_DECL(pj_status_t) pj_sock_accept( pj_sock_t serverfd, |
| 504 | pj_sock_t *newsock, |
| 505 | pj_sockaddr_t *addr, |
| 506 | int *addrlen); |
| 507 | #endif |
| 508 | |
| 509 | /** |
| 510 | * The file descriptor sockfd must refer to a socket. If the socket is of |
| 511 | * type PJ_SOCK_DGRAM then the serv_addr address is the address to which |
| 512 | * datagrams are sent by default, and the only address from which datagrams |
| 513 | * are received. If the socket is of type PJ_SOCK_STREAM or PJ_SOCK_SEQPACKET, |
| 514 | * this call attempts to make a connection to another socket. The |
| 515 | * other socket is specified by serv_addr, which is an address (of length |
| 516 | * addrlen) in the communications space of the socket. Each communications |
| 517 | * space interprets the serv_addr parameter in its own way. |
| 518 | * |
| 519 | * @param sockfd The socket descriptor. |
| 520 | * @param serv_addr Server address to connect to. |
| 521 | * @param addrlen The length of server address. |
| 522 | * |
| 523 | * @return Zero on success. |
| 524 | */ |
| 525 | PJ_DECL(pj_status_t) pj_sock_connect( pj_sock_t sockfd, |
| 526 | const pj_sockaddr_t *serv_addr, |
| 527 | int addrlen); |
| 528 | |
| 529 | /** |
| 530 | * Return the address of peer which is connected to socket sockfd. |
| 531 | * |
| 532 | * @param sockfd The socket descriptor. |
| 533 | * @param addr Pointer to sockaddr structure to which the address |
| 534 | * will be returned. |
| 535 | * @param namelen Initially the length of the addr. Upon return the value |
| 536 | * will be set to the actual length of the address. |
| 537 | * |
| 538 | * @return Zero on success. |
| 539 | */ |
| 540 | PJ_DECL(pj_status_t) pj_sock_getpeername(pj_sock_t sockfd, |
| 541 | pj_sockaddr_t *addr, |
| 542 | int *namelen); |
| 543 | |
| 544 | /** |
| 545 | * Return the current name of the specified socket. |
| 546 | * |
| 547 | * @param sockfd The socket descriptor. |
| 548 | * @param addr Pointer to sockaddr structure to which the address |
| 549 | * will be returned. |
| 550 | * @param namelen Initially the length of the addr. Upon return the value |
| 551 | * will be set to the actual length of the address. |
| 552 | * |
| 553 | * @return Zero on success. |
| 554 | */ |
| 555 | PJ_DECL(pj_status_t) pj_sock_getsockname( pj_sock_t sockfd, |
| 556 | pj_sockaddr_t *addr, |
| 557 | int *namelen); |
| 558 | |
| 559 | /** |
| 560 | * Get socket option associated with a socket. Options may exist at multiple |
| 561 | * protocol levels; they are always present at the uppermost socket level. |
| 562 | * |
| 563 | * @param sockfd The socket descriptor. |
| 564 | * @param level The level which to get the option from. |
| 565 | * @param optname The option name. |
| 566 | * @param optval Identifies the buffer which the value will be |
| 567 | * returned. |
| 568 | * @param optlen Initially contains the length of the buffer, upon |
| 569 | * return will be set to the actual size of the value. |
| 570 | * |
| 571 | * @return Zero on success. |
| 572 | */ |
| 573 | PJ_DECL(pj_status_t) pj_sock_getsockopt( pj_sock_t sockfd, |
| 574 | pj_uint16_t level, |
| 575 | pj_uint16_t optname, |
| 576 | void *optval, |
| 577 | int *optlen); |
| 578 | /** |
| 579 | * Manipulate the options associated with a socket. Options may exist at |
| 580 | * multiple protocol levels; they are always present at the uppermost socket |
| 581 | * level. |
| 582 | * |
| 583 | * @param sockfd The socket descriptor. |
| 584 | * @param level The level which to get the option from. |
| 585 | * @param optname The option name. |
| 586 | * @param optval Identifies the buffer which contain the value. |
| 587 | * @param optlen The length of the value. |
| 588 | * |
| 589 | * @return PJ_SUCCESS or the status code. |
| 590 | */ |
| 591 | PJ_DECL(pj_status_t) pj_sock_setsockopt( pj_sock_t sockfd, |
| 592 | pj_uint16_t level, |
| 593 | pj_uint16_t optname, |
| 594 | const void *optval, |
| 595 | int optlen); |
| 596 | |
| 597 | |
| 598 | /** |
| 599 | * Receives data stream or message coming to the specified socket. |
| 600 | * |
| 601 | * @param sockfd The socket descriptor. |
| 602 | * @param buf The buffer to receive the data or message. |
| 603 | * @param len On input, the length of the buffer. On return, |
| 604 | * contains the length of data received. |
| 605 | * @param flags Combination of #pj_sock_msg_flag. |
| 606 | * |
| 607 | * @return PJ_SUCCESS or the error code. |
| 608 | */ |
| 609 | PJ_DECL(pj_status_t) pj_sock_recv(pj_sock_t sockfd, |
| 610 | void *buf, |
| 611 | pj_ssize_t *len, |
| 612 | unsigned flags); |
| 613 | |
| 614 | /** |
| 615 | * Receives data stream or message coming to the specified socket. |
| 616 | * |
| 617 | * @param sockfd The socket descriptor. |
| 618 | * @param buf The buffer to receive the data or message. |
| 619 | * @param len On input, the length of the buffer. On return, |
| 620 | * contains the length of data received. |
| 621 | * @param flags Bitmask combination of #pj_sock_msg_flag. |
| 622 | * @param from If not NULL, it will be filled with the source |
| 623 | * address of the connection. |
| 624 | * @param fromlen Initially contains the length of from address, |
| 625 | * and upon return will be filled with the actual |
| 626 | * length of the address. |
| 627 | * |
| 628 | * @return PJ_SUCCESS or the error code. |
| 629 | */ |
| 630 | PJ_DECL(pj_status_t) pj_sock_recvfrom( pj_sock_t sockfd, |
| 631 | void *buf, |
| 632 | pj_ssize_t *len, |
| 633 | unsigned flags, |
| 634 | pj_sockaddr_t *from, |
| 635 | int *fromlen); |
| 636 | |
| 637 | /** |
| 638 | * Transmit data to the socket. |
| 639 | * |
| 640 | * @param sockfd Socket descriptor. |
| 641 | * @param buf Buffer containing data to be sent. |
| 642 | * @param len On input, the length of the data in the buffer. |
| 643 | * Upon return, it will be filled with the length |
| 644 | * of data sent. |
| 645 | * @param flags Bitmask combination of #pj_sock_msg_flag. |
| 646 | * |
| 647 | * @return PJ_SUCCESS or the status code. |
| 648 | */ |
| 649 | PJ_DECL(pj_status_t) pj_sock_send(pj_sock_t sockfd, |
| 650 | const void *buf, |
| 651 | pj_ssize_t *len, |
| 652 | unsigned flags); |
| 653 | |
| 654 | /** |
| 655 | * Transmit data to the socket to the specified address. |
| 656 | * |
| 657 | * @param sockfd Socket descriptor. |
| 658 | * @param buf Buffer containing data to be sent. |
| 659 | * @param len On input, the length of the data in the buffer. |
| 660 | * Upon return, it will be filled with the length |
| 661 | * of data sent. |
| 662 | * @param flags Bitmask combination of #pj_sock_msg_flag. |
| 663 | * @param to The address to send. |
| 664 | * @param tolen The length of the address in bytes. |
| 665 | * |
Benny Prijono | ac9d142 | 2006-01-18 23:32:27 +0000 | [diff] [blame^] | 666 | * @return PJ_SUCCESS or the status code. |
Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 667 | */ |
| 668 | PJ_DECL(pj_status_t) pj_sock_sendto(pj_sock_t sockfd, |
| 669 | const void *buf, |
| 670 | pj_ssize_t *len, |
| 671 | unsigned flags, |
| 672 | const pj_sockaddr_t *to, |
| 673 | int tolen); |
| 674 | |
| 675 | #if PJ_HAS_TCP |
| 676 | /** |
| 677 | * The shutdown call causes all or part of a full-duplex connection on the |
| 678 | * socket associated with sockfd to be shut down. |
| 679 | * |
| 680 | * @param sockfd The socket descriptor. |
| 681 | * @param how If how is PJ_SHUT_RD, further receptions will be |
| 682 | * disallowed. If how is PJ_SHUT_WR, further transmissions |
| 683 | * will be disallowed. If how is PJ_SHUT_RDWR, further |
| 684 | * receptions andtransmissions will be disallowed. |
| 685 | * |
| 686 | * @return Zero on success. |
| 687 | */ |
| 688 | PJ_DECL(pj_status_t) pj_sock_shutdown( pj_sock_t sockfd, |
| 689 | int how); |
| 690 | #endif |
| 691 | |
| 692 | /** |
| 693 | * @} |
| 694 | */ |
| 695 | |
| 696 | |
| 697 | PJ_END_DECL |
| 698 | |
| 699 | #endif /* __PJ_SOCK_H__ */ |
| 700 | |