PageRenderTime 97ms CodeModel.GetById 33ms app.highlight 55ms RepoModel.GetById 1ms app.codeStats 1ms

/pjproject-1.0.2/pjlib/include/pj/sock.h

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