~ubuntu-branches/ubuntu/vivid/sflphone/vivid

« back to all changes in this revision

Viewing changes to daemon/libs/pjproject/pjlib/include/pj/sock.h

  • Committer: Package Import Robot
  • Author(s): Mark Purcell
  • Date: 2013-06-30 11:40:56 UTC
  • mfrom: (4.1.18 saucy-proposed)
  • Revision ID: package-import@ubuntu.com-20130630114056-0np50jkyqo6vnmii
Tags: 1.2.3-2
* changeset_r92d62cfc54732bbbcfff2b1d36c096b120b981a5.diff 
  - fixes automatic endian detection 
* Update Vcs: fixes vcs-field-not-canonical

Show diffs side-by-side

added added

removed removed

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