Debian Codegraph @

Xpjproject/pjlib/include/pj/sock.h

raw symbols SCIP
1 /*
2 * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
3 * Copyright (C) 2003-2008 Benny Prijono <[email protected]>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19 #ifndef __PJ_SOCK_H__
20 #define __PJ_SOCK_H__
21
22 /**
23 * @file sock.h
24 * @brief Socket Abstraction.
25 */
26
27 #include <pj/types.h>
28 #include <pj/compat/socket.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 * - Socket test: \src{pjlib/src/pjlib-test/sock.c}
53 * - Socket Select() test: \src{pjlib/src/pjlib-test/select.c}
54 * - Socket Performance test: \src{pjlib/src/pjlib-test/sock_perf.c}
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 /** The close-on-exec flag. @see pj_SOCK_CLOEXEC() */
144 extern const int PJ_SOCK_CLOEXEC;
145
146
147 /*
148 * Accessor functions for various constants. These functions are provided
149 * because Symbian doesn't allow exporting global variables from a DLL.
150 */
151
152 #if defined(PJ_DLL)
153 /** Get #PJ_SOCK_STREAM constant */
154 PJ_DECL(int) pj_SOCK_STREAM(void);
155 /** Get #PJ_SOCK_DGRAM constant */
156 PJ_DECL(int) pj_SOCK_DGRAM(void);
157 /** Get #PJ_SOCK_RAW constant */
158 PJ_DECL(int) pj_SOCK_RAW(void);
159 /** Get #PJ_SOCK_RDM constant */
160 PJ_DECL(int) pj_SOCK_RDM(void);
161 /** Get #PJ_SOCK_CLOEXEC constant */
162 PJ_DECL(int) pj_SOCK_CLOEXEC(void);
163 #else
164 /** Get #PJ_SOCK_STREAM constant */
165 # define pj_SOCK_STREAM() PJ_SOCK_STREAM
166 /** Get #PJ_SOCK_DGRAM constant */
167 # define pj_SOCK_DGRAM() PJ_SOCK_DGRAM
168 /** Get #PJ_SOCK_RAW constant */
169 # define pj_SOCK_RAW() PJ_SOCK_RAW
170 /** Get #PJ_SOCK_RDM constant */
171 # define pj_SOCK_RDM() PJ_SOCK_RDM
172 /** Get #PJ_SOCK_CLOEXEC constant */
173 # define pj_SOCK_CLOEXEC() PJ_SOCK_CLOEXEC
174 #endif
175
176
177 /**
178 * Socket level specified in #pj_sock_setsockopt() or #pj_sock_getsockopt().
179 * APPLICATION MUST USE THESE VALUES INSTEAD OF NORMAL SOL_*, BECAUSE
180 * THE LIBRARY WILL TRANSLATE THE VALUE TO THE NATIVE VALUE.
181 */
182 /** Socket level. @see pj_SOL_SOCKET() */
183 extern const pj_uint16_t PJ_SOL_SOCKET;
184 /** IP level. @see pj_SOL_IP() */
185 extern const pj_uint16_t PJ_SOL_IP;
186 /** TCP level. @see pj_SOL_TCP() */
187 extern const pj_uint16_t PJ_SOL_TCP;
188 /** UDP level. @see pj_SOL_UDP() */
189 extern const pj_uint16_t PJ_SOL_UDP;
190 /** IP version 6. @see pj_SOL_IPV6() */
191 extern const pj_uint16_t PJ_SOL_IPV6;
192
193 /*
194 * Accessor functions for various constants. These functions are provided
195 * because Symbian doesn't allow exporting global variables from a DLL.
196 */
197
198 #if defined(PJ_DLL)
199 /** Get #PJ_SOL_SOCKET constant */
200 PJ_DECL(pj_uint16_t) pj_SOL_SOCKET(void);
201 /** Get #PJ_SOL_IP constant */
202 PJ_DECL(pj_uint16_t) pj_SOL_IP(void);
203 /** Get #PJ_SOL_TCP constant */
204 PJ_DECL(pj_uint16_t) pj_SOL_TCP(void);
205 /** Get #PJ_SOL_UDP constant */
206 PJ_DECL(pj_uint16_t) pj_SOL_UDP(void);
207 /** Get #PJ_SOL_IPV6 constant */
208 PJ_DECL(pj_uint16_t) pj_SOL_IPV6(void);
209 #else
210 /** Get #PJ_SOL_SOCKET constant */
211 # define pj_SOL_SOCKET() PJ_SOL_SOCKET
212 /** Get #PJ_SOL_IP constant */
213 # define pj_SOL_IP() PJ_SOL_IP
214 /** Get #PJ_SOL_TCP constant */
215 # define pj_SOL_TCP() PJ_SOL_TCP
216 /** Get #PJ_SOL_UDP constant */
217 # define pj_SOL_UDP() PJ_SOL_UDP
218 /** Get #PJ_SOL_IPV6 constant */
219 # define pj_SOL_IPV6() PJ_SOL_IPV6
220 #endif
221
222
223 /* IP_TOS
224 *
225 * Note:
226 * TOS CURRENTLY DOES NOT WORK IN Windows 2000 and above!
227 * See http://support.microsoft.com/kb/248611
228 */
229 /** IP_TOS optname in setsockopt(). @see pj_IP_TOS() */
230 extern const pj_uint16_t PJ_IP_TOS;
231
232 /*
233 * IP TOS related constats.
234 *
235 * Note:
236 * TOS CURRENTLY DOES NOT WORK IN Windows 2000 and above!
237 * See http://support.microsoft.com/kb/248611
238 */
239 /** Minimize delays. @see pj_IPTOS_LOWDELAY() */
240 extern const pj_uint16_t PJ_IPTOS_LOWDELAY;
241
242 /** Optimize throughput. @see pj_IPTOS_THROUGHPUT() */
243 extern const pj_uint16_t PJ_IPTOS_THROUGHPUT;
244
245 /** Optimize for reliability. @see pj_IPTOS_RELIABILITY() */
246 extern const pj_uint16_t PJ_IPTOS_RELIABILITY;
247
248 /** "filler data" where slow transmission does't matter.
249 * @see pj_IPTOS_MINCOST() */
250 extern const pj_uint16_t PJ_IPTOS_MINCOST;
251
252
253 #if defined(PJ_DLL)
254 /** Get #PJ_IP_TOS constant */
255 PJ_DECL(int) pj_IP_TOS(void);
256
257 /** Get #PJ_IPTOS_LOWDELAY constant */
258 PJ_DECL(int) pj_IPTOS_LOWDELAY(void);
259
260 /** Get #PJ_IPTOS_THROUGHPUT constant */
261 PJ_DECL(int) pj_IPTOS_THROUGHPUT(void);
262
263 /** Get #PJ_IPTOS_RELIABILITY constant */
264 PJ_DECL(int) pj_IPTOS_RELIABILITY(void);
265
266 /** Get #PJ_IPTOS_MINCOST constant */
267 PJ_DECL(int) pj_IPTOS_MINCOST(void);
268 #else
269 /** Get #PJ_IP_TOS constant */
270 # define pj_IP_TOS() PJ_IP_TOS
271
272 /** Get #PJ_IPTOS_LOWDELAY constant */
273 # define pj_IPTOS_LOWDELAY() PJ_IP_TOS_LOWDELAY
274
275 /** Get #PJ_IPTOS_THROUGHPUT constant */
276 # define pj_IPTOS_THROUGHPUT() PJ_IP_TOS_THROUGHPUT
277
278 /** Get #PJ_IPTOS_RELIABILITY constant */
279 # define pj_IPTOS_RELIABILITY() PJ_IP_TOS_RELIABILITY
280
281 /** Get #PJ_IPTOS_MINCOST constant */
282 # define pj_IPTOS_MINCOST() PJ_IP_TOS_MINCOST
283 #endif
284
285
286 /** IPV6_TCLASS optname in setsockopt(). @see pj_IPV6_TCLASS() */
287 extern const pj_uint16_t PJ_IPV6_TCLASS;
288
289
290 #if defined(PJ_DLL)
291 /** Get #PJ_IPV6_TCLASS constant */
292 PJ_DECL(int) pj_IPV6_TCLASS(void);
293 #else
294 /** Get #PJ_IPV6_TCLASS constant */
295 # define pj_IPV6_TCLASS() PJ_IPV6_TCLASS
296 #endif
297
298
299 /**
300 * Values to be specified as \c optname when calling #pj_sock_setsockopt()
301 * or #pj_sock_getsockopt().
302 */
303
304 /** Socket type. @see pj_SO_TYPE() */
305 extern const pj_uint16_t PJ_SO_TYPE;
306
307 /** Buffer size for receive. @see pj_SO_RCVBUF() */
308 extern const pj_uint16_t PJ_SO_RCVBUF;
309
310 /** Buffer size for send. @see pj_SO_SNDBUF() */
311 extern const pj_uint16_t PJ_SO_SNDBUF;
312
313 /** Disables the Nagle algorithm for send coalescing. @see pj_TCP_NODELAY */
314 extern const pj_uint16_t PJ_TCP_NODELAY;
315
316 /** Allows the socket to be bound to an address that is already in use.
317 * @see pj_SO_REUSEADDR */
318 extern const pj_uint16_t PJ_SO_REUSEADDR;
319
320 /** Do not generate SIGPIPE. @see pj_SO_NOSIGPIPE */
321 extern const pj_uint16_t PJ_SO_NOSIGPIPE;
322
323 /** Set the protocol-defined priority for all packets to be sent on socket.
324 */
325 extern const pj_uint16_t PJ_SO_PRIORITY;
326
327 /** IP multicast interface. @see pj_IP_MULTICAST_IF() */
328 extern const pj_uint16_t PJ_IP_MULTICAST_IF;
329
330 /** IP multicast ttl. @see pj_IP_MULTICAST_TTL() */
331 extern const pj_uint16_t PJ_IP_MULTICAST_TTL;
332
333 /** IP multicast loopback. @see pj_IP_MULTICAST_LOOP() */
334 extern const pj_uint16_t PJ_IP_MULTICAST_LOOP;
335
336 /** Add an IP group membership. @see pj_IP_ADD_MEMBERSHIP() */
337 extern const pj_uint16_t PJ_IP_ADD_MEMBERSHIP;
338
339 /** Drop an IP group membership. @see pj_IP_DROP_MEMBERSHIP() */
340 extern const pj_uint16_t PJ_IP_DROP_MEMBERSHIP;
341
342
343 #if defined(PJ_DLL)
344 /** Get #PJ_SO_TYPE constant */
345 PJ_DECL(pj_uint16_t) pj_SO_TYPE(void);
346
347 /** Get #PJ_SO_RCVBUF constant */
348 PJ_DECL(pj_uint16_t) pj_SO_RCVBUF(void);
349
350 /** Get #PJ_SO_SNDBUF constant */
351 PJ_DECL(pj_uint16_t) pj_SO_SNDBUF(void);
352
353 /** Get #PJ_TCP_NODELAY constant */
354 PJ_DECL(pj_uint16_t) pj_TCP_NODELAY(void);
355
356 /** Get #PJ_SO_REUSEADDR constant */
357 PJ_DECL(pj_uint16_t) pj_SO_REUSEADDR(void);
358
359 /** Get #PJ_SO_NOSIGPIPE constant */
360 PJ_DECL(pj_uint16_t) pj_SO_NOSIGPIPE(void);
361
362 /** Get #PJ_SO_PRIORITY constant */
363 PJ_DECL(pj_uint16_t) pj_SO_PRIORITY(void);
364
365 /** Get #PJ_IP_MULTICAST_IF constant */
366 PJ_DECL(pj_uint16_t) pj_IP_MULTICAST_IF(void);
367
368 /** Get #PJ_IP_MULTICAST_TTL constant */
369 PJ_DECL(pj_uint16_t) pj_IP_MULTICAST_TTL(void);
370
371 /** Get #PJ_IP_MULTICAST_LOOP constant */
372 PJ_DECL(pj_uint16_t) pj_IP_MULTICAST_LOOP(void);
373
374 /** Get #PJ_IP_ADD_MEMBERSHIP constant */
375 PJ_DECL(pj_uint16_t) pj_IP_ADD_MEMBERSHIP(void);
376
377 /** Get #PJ_IP_DROP_MEMBERSHIP constant */
378 PJ_DECL(pj_uint16_t) pj_IP_DROP_MEMBERSHIP(void);
379 #else
380 /** Get #PJ_SO_TYPE constant */
381 # define pj_SO_TYPE() PJ_SO_TYPE
382
383 /** Get #PJ_SO_RCVBUF constant */
384 # define pj_SO_RCVBUF() PJ_SO_RCVBUF
385
386 /** Get #PJ_SO_SNDBUF constant */
387 # define pj_SO_SNDBUF() PJ_SO_SNDBUF
388
389 /** Get #PJ_TCP_NODELAY constant */
390 # define pj_TCP_NODELAY() PJ_TCP_NODELAY
391
392 /** Get #PJ_SO_REUSEADDR constant */
393 # define pj_SO_REUSEADDR() PJ_SO_REUSEADDR
394
395 /** Get #PJ_SO_NOSIGPIPE constant */
396 # define pj_SO_NOSIGPIPE() PJ_SO_NOSIGPIPE
397
398 /** Get #PJ_SO_PRIORITY constant */
399 # define pj_SO_PRIORITY() PJ_SO_PRIORITY
400
401 /** Get #PJ_IP_MULTICAST_IF constant */
402 # define pj_IP_MULTICAST_IF() PJ_IP_MULTICAST_IF
403
404 /** Get #PJ_IP_MULTICAST_TTL constant */
405 # define pj_IP_MULTICAST_TTL() PJ_IP_MULTICAST_TTL
406
407 /** Get #PJ_IP_MULTICAST_LOOP constant */
408 # define pj_IP_MULTICAST_LOOP() PJ_IP_MULTICAST_LOOP
409
410 /** Get #PJ_IP_ADD_MEMBERSHIP constant */
411 # define pj_IP_ADD_MEMBERSHIP() PJ_IP_ADD_MEMBERSHIP
412
413 /** Get #PJ_IP_DROP_MEMBERSHIP constant */
414 # define pj_IP_DROP_MEMBERSHIP() PJ_IP_DROP_MEMBERSHIP
415 #endif
416
417
418 /*
419 * Flags to be specified in #pj_sock_recv, #pj_sock_send, etc.
420 */
421
422 /** Out-of-band messages. @see pj_MSG_OOB() */
423 extern const int PJ_MSG_OOB;
424
425 /** Peek, don't remove from buffer. @see pj_MSG_PEEK() */
426 extern const int PJ_MSG_PEEK;
427
428 /** Don't route. @see pj_MSG_DONTROUTE() */
429 extern const int PJ_MSG_DONTROUTE;
430
431
432 #if defined(PJ_DLL)
433 /** Get #PJ_MSG_OOB constant */
434 PJ_DECL(int) pj_MSG_OOB(void);
435
436 /** Get #PJ_MSG_PEEK constant */
437 PJ_DECL(int) pj_MSG_PEEK(void);
438
439 /** Get #PJ_MSG_DONTROUTE constant */
440 PJ_DECL(int) pj_MSG_DONTROUTE(void);
441 #else
442 /** Get #PJ_MSG_OOB constant */
443 # define pj_MSG_OOB() PJ_MSG_OOB
444
445 /** Get #PJ_MSG_PEEK constant */
446 # define pj_MSG_PEEK() PJ_MSG_PEEK
447
448 /** Get #PJ_MSG_DONTROUTE constant */
449 # define pj_MSG_DONTROUTE() PJ_MSG_DONTROUTE
450 #endif
451
452
453 /**
454 * Flag to be specified in #pj_sock_shutdown().
455 */
456 typedef enum pj_socket_sd_type
457 {
458 PJ_SD_RECEIVE = 0, /**< No more receive. */
459 PJ_SHUT_RD = 0, /**< Alias for SD_RECEIVE. */
460 PJ_SD_SEND = 1, /**< No more sending. */
461 PJ_SHUT_WR = 1, /**< Alias for SD_SEND. */
462 PJ_SD_BOTH = 2, /**< No more send and receive. */
463 PJ_SHUT_RDWR = 2 /**< Alias for SD_BOTH. */
464 } pj_socket_sd_type;
465
466
467
468 /** Address to accept any incoming messages. */
469 #define PJ_INADDR_ANY ((pj_uint32_t)0)
470
471 /** Address indicating an error return */
472 #define PJ_INADDR_NONE ((pj_uint32_t)0xffffffff)
473
474 /** Address to send to all hosts. */
475 #define PJ_INADDR_BROADCAST ((pj_uint32_t)0xffffffff)
476
477
478 /**
479 * Maximum length specifiable by #pj_sock_listen().
480 * If the build system doesn't override this value, then the lowest
481 * denominator (five, in Win32 systems) will be used.
482 */
483 #if !defined(PJ_SOMAXCONN)
484 # define PJ_SOMAXCONN 5
485 #endif
486
487
488 /**
489 * Constant for invalid socket returned by #pj_sock_socket() and
490 * #pj_sock_accept().
491 */
492 #define PJ_INVALID_SOCKET (-1)
493
494 /* Undefining UNIX standard library macro such as s_addr is not
495 * recommended as it may cause build issues for anyone who uses
496 * the macro. See #2311 for more details.
497 */
498 #if 0
499 /* Must undefine s_addr because of pj_in_addr below */
500 #undef s_addr
501
502 typedef struct pj_in_addr
503 {
504 pj_uint32_t s_addr; /**< The 32bit IP address. */
505 } pj_in_addr;
506
507 #else
508 /**
509 * This structure describes Internet address.
510 */
511 typedef struct in_addr pj_in_addr;
512 #endif
513
514 /**
515 * Maximum length of text representation of an IPv4 address.
516 */
517 #define PJ_INET_ADDRSTRLEN 16
518
519 /**
520 * Maximum length of text representation of an IPv6 address.
521 */
522 #define PJ_INET6_ADDRSTRLEN 46
523
524 /**
525 * The size of sin_zero field in pj_sockaddr_in structure. Most OSes
526 * use 8, but others such as the BSD TCP/IP stack in eCos uses 24.
527 */
528 #ifndef PJ_SOCKADDR_IN_SIN_ZERO_LEN
529 # define PJ_SOCKADDR_IN_SIN_ZERO_LEN 8
530 #endif
531
532 /**
533 * This structure describes Internet socket address.
534 * If PJ_SOCKADDR_HAS_LEN is not zero, then sin_zero_len member is added
535 * to this struct. As far the application is concerned, the value of
536 * this member will always be zero. Internally, PJLIB may modify the value
537 * before calling OS socket API, and reset the value back to zero before
538 * returning the struct to application.
539 */
540 struct pj_sockaddr_in
541 {
542 #if defined(PJ_SOCKADDR_HAS_LEN) && PJ_SOCKADDR_HAS_LEN!=0
543 pj_uint8_t sin_zero_len; /**< Just ignore this. */
544 pj_uint8_t sin_family; /**< Address family. */
545 #else
546 pj_uint16_t sin_family; /**< Address family. */
547 #endif
548 pj_uint16_t sin_port; /**< Transport layer port number. */
549 pj_in_addr sin_addr; /**< IP address. */
550 char sin_zero_pad[PJ_SOCKADDR_IN_SIN_ZERO_LEN]; /**< Padding.*/
551 };
552
553
554 /* Undefining C standard library macro such as s6_addr is not
555 * recommended as it may cause build issues for anyone who uses
556 * the macro. See #2311 for more details.
557 */
558 #if 0
559 #undef s6_addr
560
561 typedef union pj_in6_addr
562 {
563 /* This is the main entry */
564 pj_uint8_t s6_addr[16]; /**< 8-bit array */
565
566 /* While these are used for proper alignment */
567 pj_uint32_t u6_addr32[4];
568
569 /* Do not use this with Winsock2, as this will align pj_sockaddr_in6
570 * to 64-bit boundary and Winsock2 doesn't like it!
571 * Update 26/04/2010:
572 * This is now disabled, see https://github.com/pjsip/pjproject/issues/1058
573 */
574 #if 0 && defined(PJ_HAS_INT64) && PJ_HAS_INT64!=0 && \
575 (!defined(PJ_WIN32) || PJ_WIN32==0)
576 pj_int64_t u6_addr64[2];
577 #endif
578
579 } pj_in6_addr;
580 #else
581 /**
582 * This structure describes IPv6 address.
583 */
584 typedef struct in6_addr pj_in6_addr;
585 #endif
586
587
588 /** Initializer value for pj_in6_addr. */
589 #define PJ_IN6ADDR_ANY_INIT { { { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 } } }
590
591 /** Initializer value for pj_in6_addr. */
592 #define PJ_IN6ADDR_LOOPBACK_INIT { { { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1 } } }
593
594 /**
595 * This structure describes IPv6 socket address.
596 * If PJ_SOCKADDR_HAS_LEN is not zero, then sin_zero_len member is added
597 * to this struct. As far the application is concerned, the value of
598 * this member will always be zero. Internally, PJLIB may modify the value
599 * before calling OS socket API, and reset the value back to zero before
600 * returning the struct to application.
601 */
602 typedef struct pj_sockaddr_in6
603 {
604 #if defined(PJ_SOCKADDR_HAS_LEN) && PJ_SOCKADDR_HAS_LEN!=0
605 pj_uint8_t sin6_zero_len; /**< Just ignore this. */
606 pj_uint8_t sin6_family; /**< Address family. */
607 #else
608 pj_uint16_t sin6_family; /**< Address family */
609 #endif
610 pj_uint16_t sin6_port; /**< Transport layer port number. */
611 pj_uint32_t sin6_flowinfo; /**< IPv6 flow information */
612 pj_in6_addr sin6_addr; /**< IPv6 address. */
613 pj_uint32_t sin6_scope_id; /**< Set of interfaces for a scope */
614 } pj_sockaddr_in6;
615
616
617 /**
618 * This structure describes common attributes found in transport addresses.
619 * If PJ_SOCKADDR_HAS_LEN is not zero, then sa_zero_len member is added
620 * to this struct. As far the application is concerned, the value of
621 * this member will always be zero. Internally, PJLIB may modify the value
622 * before calling OS socket API, and reset the value back to zero before
623 * returning the struct to application.
624 */
625 typedef struct pj_addr_hdr
626 {
627 #if defined(PJ_SOCKADDR_HAS_LEN) && PJ_SOCKADDR_HAS_LEN!=0
628 pj_uint8_t sa_zero_len;
629 pj_uint8_t sa_family;
630 #else
631 pj_uint16_t sa_family; /**< Common data: address family. */
632 #endif
633 } pj_addr_hdr;
634
635
636 /**
637 * This union describes a generic socket address.
638 */
639 typedef union pj_sockaddr
640 {
641 pj_addr_hdr addr; /**< Generic transport address. */
642 pj_sockaddr_in ipv4; /**< IPv4 transport address. */
643 pj_sockaddr_in6 ipv6; /**< IPv6 transport address. */
644 } pj_sockaddr;
645
646
647 /**
648 * This structure provides multicast group information for IPv4 addresses.
649 */
650 typedef struct pj_ip_mreq {
651 pj_in_addr imr_multiaddr; /**< IP multicast address of group. */
652 pj_in_addr imr_interface; /**< local IP address of interface. */
653 } pj_ip_mreq;
654
655
656 /**
657 * Options to be set for the socket.
658 */
659 typedef struct pj_sockopt_params
660 {
661 /** The number of options to be applied. */
662 unsigned cnt;
663
664 /** Array of options to be applied. */
665 struct {
666 /** The level at which the option is defined. */
667 int level;
668
669 /** Option name. */
670 int optname;
671
672 /** Pointer to the buffer in which the option is specified. */
673 void *optval;
674
675 /** Buffer size of the buffer pointed by optval. */
676 int optlen;
677 } options[PJ_MAX_SOCKOPT_PARAMS];
678 } pj_sockopt_params;
679
680 /*****************************************************************************
681 *
682 * SOCKET ADDRESS MANIPULATION.
683 *
684 *****************************************************************************
685 */
686
687 /**
688 * Convert 16-bit value from network byte order to host byte order.
689 *
690 * @param netshort 16-bit network value.
691 * @return 16-bit host value.
692 */
693 PJ_DECL(pj_uint16_t) pj_ntohs(pj_uint16_t netshort);
694
695 /**
696 * Convert 16-bit value from host byte order to network byte order.
697 *
698 * @param hostshort 16-bit host value.
699 * @return 16-bit network value.
700 */
701 PJ_DECL(pj_uint16_t) pj_htons(pj_uint16_t hostshort);
702
703 /**
704 * Convert 32-bit value from network byte order to host byte order.
705 *
706 * @param netlong 32-bit network value.
707 * @return 32-bit host value.
708 */
709 PJ_DECL(pj_uint32_t) pj_ntohl(pj_uint32_t netlong);
710
711 /**
712 * Convert 32-bit value from host byte order to network byte order.
713 *
714 * @param hostlong 32-bit host value.
715 * @return 32-bit network value.
716 */
717 PJ_DECL(pj_uint32_t) pj_htonl(pj_uint32_t hostlong);
718
719 /**
720 * Convert an Internet host address given in network byte order
721 * to string in standard numbers and dots notation.
722 *
723 * @param inaddr The host address.
724 * @return The string address.
725 */
726 PJ_DECL(char*) pj_inet_ntoa(pj_in_addr inaddr);
727
728 /**
729 * This function converts the Internet host address cp from the standard
730 * numbers-and-dots notation into binary data and stores it in the structure
731 * that inp points to.
732 *
733 * @param cp IP address in standard numbers-and-dots notation.
734 * @param inp Structure that holds the output of the conversion.
735 *
736 * @return nonzero if the address is valid, zero if not.
737 */
738 PJ_DECL(int) pj_inet_aton(const pj_str_t *cp, pj_in_addr *inp);
739
740 /**
741 * This function converts an address in its standard text presentation form
742 * into its numeric binary form. It supports both IPv4 and IPv6 address
743 * conversion.
744 *
745 * @param af Specify the family of the address. The PJ_AF_INET and
746 * PJ_AF_INET6 address families shall be supported.
747 * @param src Points to the string being passed in.
748 * @param dst Points to a buffer into which the function stores the
749 * numeric address; this shall be large enough to hold the
750 * numeric address (32 bits for PJ_AF_INET, 128 bits for
751 * PJ_AF_INET6).
752 *
753 * @return PJ_SUCCESS if conversion was successful.
754 */
755 PJ_DECL(pj_status_t) pj_inet_pton(int af, const pj_str_t *src, void *dst);
756
757 /**
758 * This function converts a numeric address into a text string suitable
759 * for presentation. It supports both IPv4 and IPv6 address
760 * conversion.
761 * @see pj_sockaddr_print()
762 *
763 * @param af Specify the family of the address. This can be PJ_AF_INET
764 * or PJ_AF_INET6.
765 * @param src Points to a buffer holding an IPv4 address if the af argument
766 * is PJ_AF_INET, or an IPv6 address if the af argument is
767 * PJ_AF_INET6; the address must be in network byte order.
768 * @param dst Points to a buffer where the function stores the resulting
769 * text string; it shall not be NULL.
770 * @param size Specifies the size of this buffer, which shall be large
771 * enough to hold the text string (PJ_INET_ADDRSTRLEN characters
772 * for IPv4, PJ_INET6_ADDRSTRLEN characters for IPv6).
773 *
774 * @return PJ_SUCCESS if conversion was successful.
775 */
776 PJ_DECL(pj_status_t) pj_inet_ntop(int af, const void *src,
777 char *dst, int size);
778
779 /**
780 * Converts numeric address into its text string representation.
781 * @see pj_sockaddr_print()
782 *
783 * @param af Specify the family of the address. This can be PJ_AF_INET
784 * or PJ_AF_INET6.
785 * @param src Points to a buffer holding an IPv4 address if the af argument
786 * is PJ_AF_INET, or an IPv6 address if the af argument is
787 * PJ_AF_INET6; the address must be in network byte order.
788 * @param dst Points to a buffer where the function stores the resulting
789 * text string; it shall not be NULL.
790 * @param size Specifies the size of this buffer, which shall be large
791 * enough to hold the text string (PJ_INET_ADDRSTRLEN characters
792 * for IPv4, PJ_INET6_ADDRSTRLEN characters for IPv6).
793 *
794 * @return The address string or NULL if failed.
795 */
796 PJ_DECL(char*) pj_inet_ntop2(int af, const void *src,
797 char *dst, int size);
798
799 /**
800 * Print socket address.
801 *
802 * @param addr The socket address.
803 * @param buf Text buffer.
804 * @param size Size of buffer.
805 * @param flags Bitmask combination of these value:
806 * - 1: port number is included.
807 * - 2: square bracket is included for IPv6 address.
808 *
809 * @return The address string.
810 */
811 PJ_DECL(char*) pj_sockaddr_print(const pj_sockaddr_t *addr,
812 char *buf, int size,
813 unsigned flags);
814
815 /**
816 * Convert address string with numbers and dots to binary IP address.
817 *
818 * @param cp The IP address in numbers and dots notation.
819 * @return If success, the IP address is returned in network
820 * byte order. If failed, PJ_INADDR_NONE will be
821 * returned.
822 * @remark
823 * This is an obsolete interface to #pj_inet_aton(); it is obsolete
824 * because -1 is a valid address (255.255.255.255), and #pj_inet_aton()
825 * provides a cleaner way to indicate error return.
826 */
827 PJ_DECL(pj_in_addr) pj_inet_addr(const pj_str_t *cp);
828
829 /**
830 * Convert address string with numbers and dots to binary IP address.
831 *
832 * @param cp The IP address in numbers and dots notation.
833 * @return If success, the IP address is returned in network
834 * byte order. If failed, PJ_INADDR_NONE will be
835 * returned.
836 * @remark
837 * This is an obsolete interface to #pj_inet_aton(); it is obsolete
838 * because -1 is a valid address (255.255.255.255), and #pj_inet_aton()
839 * provides a cleaner way to indicate error return.
840 */
841 PJ_DECL(pj_in_addr) pj_inet_addr2(const char *cp);
842
843 /**
844 * Initialize IPv4 socket address based on the address and port info.
845 * The string address may be in a standard numbers and dots notation or
846 * may be a hostname. If hostname is specified, then the function will
847 * resolve the host into the IP address.
848 *
849 * @see pj_sockaddr_init()
850 *
851 * @param addr The IP socket address to be set.
852 * @param cp The address string, which can be in a standard
853 * dotted numbers or a hostname to be resolved.
854 * @param port The port number, in host byte order.
855 *
856 * @return Zero on success.
857 */
858 PJ_DECL(pj_status_t) pj_sockaddr_in_init( pj_sockaddr_in *addr,
859 const pj_str_t *cp,
860 pj_uint16_t port);
861
862 /**
863 * Initialize IP socket address based on the address and port info.
864 * The string address may be in a standard numbers and dots notation or
865 * may be a hostname. If hostname is specified, then the function will
866 * resolve the host into the IP address.
867 *
868 * @see pj_sockaddr_in_init()
869 *
870 * @param af Internet address family.
871 * @param addr The IP socket address to be set.
872 * @param cp The address string, which can be in a standard
873 * dotted numbers or a hostname to be resolved.
874 * @param port The port number, in host byte order.
875 *
876 * @return Zero on success.
877 */
878 PJ_DECL(pj_status_t) pj_sockaddr_init(int af,
879 pj_sockaddr *addr,
880 const pj_str_t *cp,
881 pj_uint16_t port);
882
883 /**
884 * Compare two socket addresses.
885 *
886 * @param addr1 First address.
887 * @param addr2 Second address.
888 *
889 * @return Zero on equal, -1 if addr1 is less than addr2,
890 * and +1 if addr1 is more than addr2.
891 */
892 PJ_DECL(int) pj_sockaddr_cmp(const pj_sockaddr_t *addr1,
893 const pj_sockaddr_t *addr2);
894
895 /**
896 * Get pointer to the address part of a socket address.
897 *
898 * @param addr Socket address.
899 *
900 * @return Pointer to address part (sin_addr or sin6_addr,
901 * depending on address family)
902 */
903 PJ_DECL(void*) pj_sockaddr_get_addr(const pj_sockaddr_t *addr);
904
905 /**
906 * Check that a socket address contains a non-zero address part.
907 *
908 * @param addr Socket address.
909 *
910 * @return Non-zero if address is set to non-zero.
911 */
912 PJ_DECL(pj_bool_t) pj_sockaddr_has_addr(const pj_sockaddr_t *addr);
913
914 /**
915 * Get the address part length of a socket address, based on its address
916 * family. For PJ_AF_INET, the length will be sizeof(pj_in_addr), and
917 * for PJ_AF_INET6, the length will be sizeof(pj_in6_addr).
918 *
919 * @param addr Socket address.
920 *
921 * @return Length in bytes.
922 */
923 PJ_DECL(unsigned) pj_sockaddr_get_addr_len(const pj_sockaddr_t *addr);
924
925 /**
926 * Get the socket address length, based on its address
927 * family. For PJ_AF_INET, the length will be sizeof(pj_sockaddr_in), and
928 * for PJ_AF_INET6, the length will be sizeof(pj_sockaddr_in6).
929 *
930 * @param addr Socket address.
931 *
932 * @return Length in bytes.
933 */
934 PJ_DECL(unsigned) pj_sockaddr_get_len(const pj_sockaddr_t *addr);
935
936 /**
937 * Copy only the address part (sin_addr/sin6_addr) of a socket address.
938 *
939 * @param dst Destination socket address.
940 * @param src Source socket address.
941 *
942 * @see pj_sockaddr_cp()
943 */
944 PJ_DECL(void) pj_sockaddr_copy_addr(pj_sockaddr *dst,
945 const pj_sockaddr *src);
946 /**
947 * Copy socket address. This will copy the whole structure depending
948 * on the address family of the source socket address.
949 *
950 * @param dst Destination socket address.
951 * @param src Source socket address.
952 *
953 * @see pj_sockaddr_copy_addr()
954 */
955 PJ_DECL(void) pj_sockaddr_cp(pj_sockaddr_t *dst, const pj_sockaddr_t *src);
956
957 /**
958 * If the source's and desired address family matches, copy the address,
959 * otherwise synthesize a new address with the desired address family,
960 * from the source address. This can be useful to generate an IPv4-mapped
961 * IPv6 address.
962 *
963 * @param dst_af Desired address family.
964 * @param dst Destination socket address, invalid if synthesis is
965 * required and failed.
966 * @param src Source socket address.
967 *
968 * @return PJ_SUCCESS on success, or the error status
969 * if synthesis is required and failed.
970 */
971 PJ_DECL(pj_status_t) pj_sockaddr_synthesize(int dst_af,
972 pj_sockaddr_t *dst,
973 const pj_sockaddr_t *src);
974
975 /**
976 * Get the IP address of an IPv4 socket address.
977 * The address is returned as 32bit value in host byte order.
978 *
979 * @param addr The IP socket address.
980 * @return 32bit address, in host byte order.
981 */
982 PJ_DECL(pj_in_addr) pj_sockaddr_in_get_addr(const pj_sockaddr_in *addr);
983
984 /**
985 * Set the IP address of an IPv4 socket address.
986 *
987 * @param addr The IP socket address.
988 * @param hostaddr The host address, in host byte order.
989 */
990 PJ_DECL(void) pj_sockaddr_in_set_addr(pj_sockaddr_in *addr,
991 pj_uint32_t hostaddr);
992
993 /**
994 * Set the IP address of an IP socket address from string address,
995 * with resolving the host if necessary. The string address may be in a
996 * standard numbers and dots notation or may be a hostname. If hostname
997 * is specified, then the function will resolve the host into the IP
998 * address.
999 *
1000 * @see pj_sockaddr_set_str_addr()
1001 *
1002 * @param addr The IP socket address to be set.
1003 * @param cp The address string, which can be in a standard
1004 * dotted numbers or a hostname to be resolved.
1005 *
1006 * @return PJ_SUCCESS on success.
1007 */
1008 PJ_DECL(pj_status_t) pj_sockaddr_in_set_str_addr( pj_sockaddr_in *addr,
1009 const pj_str_t *cp);
1010
1011 /**
1012 * Set the IP address of an IPv4 or IPv6 socket address from string address,
1013 * with resolving the host if necessary. The string address may be in a
1014 * standard IPv6 or IPv6 address or may be a hostname. If hostname
1015 * is specified, then the function will resolve the host into the IP
1016 * address according to the address family.
1017 *
1018 * @param af Address family.
1019 * @param addr The IP socket address to be set.
1020 * @param cp The address string, which can be in a standard
1021 * IP numbers (IPv4 or IPv6) or a hostname to be resolved.
1022 *
1023 * @return PJ_SUCCESS on success.
1024 */
1025 PJ_DECL(pj_status_t) pj_sockaddr_set_str_addr(int af,
1026 pj_sockaddr *addr,
1027 const pj_str_t *cp);
1028
1029 /**
1030 * Get the port number of a socket address, in host byte order.
1031 * This function can be used for both IPv4 and IPv6 socket address.
1032 *
1033 * @param addr Socket address.
1034 *
1035 * @return Port number, in host byte order.
1036 */
1037 PJ_DECL(pj_uint16_t) pj_sockaddr_get_port(const pj_sockaddr_t *addr);
1038
1039 /**
1040 * Get the transport layer port number of an Internet socket address.
1041 * The port is returned in host byte order.
1042 *
1043 * @param addr The IP socket address.
1044 * @return Port number, in host byte order.
1045 */
1046 PJ_DECL(pj_uint16_t) pj_sockaddr_in_get_port(const pj_sockaddr_in *addr);
1047
1048 /**
1049 * Set the port number of an Internet socket address.
1050 *
1051 * @param addr The socket address.
1052 * @param hostport The port number, in host byte order.
1053 */
1054 PJ_DECL(pj_status_t) pj_sockaddr_set_port(pj_sockaddr *addr,
1055 pj_uint16_t hostport);
1056
1057 /**
1058 * Set the port number of an IPv4 socket address.
1059 *
1060 * @see pj_sockaddr_set_port()
1061 *
1062 * @param addr The IP socket address.
1063 * @param hostport The port number, in host byte order.
1064 */
1065 PJ_DECL(void) pj_sockaddr_in_set_port(pj_sockaddr_in *addr,
1066 pj_uint16_t hostport);
1067
1068 /**
1069 * Parse string containing IP address and optional port into socket address,
1070 * possibly also with address family detection. This function supports both
1071 * IPv4 and IPv6 parsing, however IPv6 parsing may only be done if IPv6 is
1072 * enabled during compilation.
1073 *
1074 * This function supports parsing several formats. Sample IPv4 inputs and
1075 * their default results::
1076 * - "10.0.0.1:80": address 10.0.0.1 and port 80.
1077 * - "10.0.0.1": address 10.0.0.1 and port zero.
1078 * - "10.0.0.1:": address 10.0.0.1 and port zero.
1079 * - "10.0.0.1:0": address 10.0.0.1 and port zero.
1080 * - ":80": address 0.0.0.0 and port 80.
1081 * - ":": address 0.0.0.0 and port 0.
1082 * - "localhost": address 127.0.0.1 and port 0.
1083 * - "localhost:": address 127.0.0.1 and port 0.
1084 * - "localhost:80": address 127.0.0.1 and port 80.
1085 *
1086 * Sample IPv6 inputs and their default results:
1087 * - "[fec0::01]:80": address fec0::01 and port 80
1088 * - "[fec0::01]": address fec0::01 and port 0
1089 * - "[fec0::01]:": address fec0::01 and port 0
1090 * - "[fec0::01]:0": address fec0::01 and port 0
1091 * - "fec0::01": address fec0::01 and port 0
1092 * - "fec0::01:80": address fec0::01:80 and port 0
1093 * - "::": address zero (::) and port 0
1094 * - "[::]": address zero (::) and port 0
1095 * - "[::]:": address zero (::) and port 0
1096 * - ":::": address zero (::) and port 0
1097 * - "[::]:80": address zero (::) and port 0
1098 * - ":::80": address zero (::) and port 80
1099 *
1100 * Note: when the IPv6 socket address contains port number, the IP
1101 * part of the socket address should be enclosed with square brackets,
1102 * otherwise the port number will be included as part of the IP address
1103 * (see "fec0::01:80" example above).
1104 *
1105 * @param af Optionally specify the address family to be used. If the
1106 * address family is to be deducted from the input, specify
1107 * pj_AF_UNSPEC() here. Other supported values are
1108 * #pj_AF_INET() and #pj_AF_INET6()
1109 * @param options Additional options to assist the parsing, must be zero
1110 * for now.
1111 * @param str The input string to be parsed.
1112 * @param addr Pointer to store the result.
1113 *
1114 * @return PJ_SUCCESS if the parsing is successful.
1115 *
1116 * @see pj_sockaddr_parse2()
1117 */
1118 PJ_DECL(pj_status_t) pj_sockaddr_parse(int af, unsigned options,
1119 const pj_str_t *str,
1120 pj_sockaddr *addr);
1121
1122 /**
1123 * This function is similar to #pj_sockaddr_parse(), except that it will not
1124 * convert the hostpart into IP address (thus possibly resolving the hostname
1125 * into a #pj_sockaddr.
1126 *
1127 * Unlike #pj_sockaddr_parse(), this function has a limitation that if port
1128 * number is specified in an IPv6 input string, the IP part of the IPv6 socket
1129 * address MUST be enclosed in square brackets, otherwise the port number will
1130 * be considered as part of the IPv6 IP address.
1131 *
1132 * @param af Optionally specify the address family to be used. If the
1133 * address family is to be deducted from the input, specify
1134 * #pj_AF_UNSPEC() here. Other supported values are
1135 * #pj_AF_INET() and #pj_AF_INET6()
1136 * @param options Additional options to assist the parsing, must be zero
1137 * for now.
1138 * @param str The input string to be parsed.
1139 * @param hostpart Optional pointer to store the host part of the socket
1140 * address, with any brackets removed.
1141 * @param port Optional pointer to store the port number. If port number
1142 * is not found, this will be set to zero upon return.
1143 * @param raf Optional pointer to store the detected address family of
1144 * the input address.
1145 *
1146 * @return PJ_SUCCESS if the parsing is successful.
1147 *
1148 * @see pj_sockaddr_parse()
1149 */
1150 PJ_DECL(pj_status_t) pj_sockaddr_parse2(int af, unsigned options,
1151 const pj_str_t *str,
1152 pj_str_t *hostpart,
1153 pj_uint16_t *port,
1154 int *raf);
1155
1156 /*****************************************************************************
1157 *
1158 * HOST NAME AND ADDRESS.
1159 *
1160 *****************************************************************************
1161 */
1162
1163 /**
1164 * Get system's host name.
1165 *
1166 * @return The hostname, or empty string if the hostname can not
1167 * be identified.
1168 */
1169 PJ_DECL(const pj_str_t*) pj_gethostname(void);
1170
1171 /**
1172 * Get host's IP address, which the the first IP address that is resolved
1173 * from the hostname.
1174 *
1175 * @return The host's IP address, PJ_INADDR_NONE if the host
1176 * IP address can not be identified.
1177 */
1178 PJ_DECL(pj_in_addr) pj_gethostaddr(void);
1179
1180
1181 /*****************************************************************************
1182 *
1183 * SOCKET API.
1184 *
1185 *****************************************************************************
1186 */
1187
1188 /**
1189 * Create new socket/endpoint for communication.
1190 *
1191 * @param family Specifies a communication domain; this selects the
1192 * protocol family which will be used for communication.
1193 * @param type The socket has the indicated type, which specifies the
1194 * communication semantics.
1195 * @param protocol Specifies a particular protocol to be used with the
1196 * socket. Normally only a single protocol exists to support
1197 * a particular socket type within a given protocol family,
1198 * in which a case protocol can be specified as 0.
1199 * @param sock New socket descriptor, or PJ_INVALID_SOCKET on error.
1200 *
1201 * @return Zero on success.
1202 */
1203 PJ_DECL(pj_status_t) pj_sock_socket(int family,
1204 int type,
1205 int protocol,
1206 pj_sock_t *sock);
1207
1208 /**
1209 * Close the socket descriptor.
1210 *
1211 * @param sockfd The socket descriptor.
1212 *
1213 * @return Zero on success.
1214 */
1215 PJ_DECL(pj_status_t) pj_sock_close(pj_sock_t sockfd);
1216
1217
1218 /**
1219 * This function gives the socket sockfd the local address my_addr. my_addr is
1220 * addrlen bytes long. Traditionally, this is called assigning a name to
1221 * a socket. When a socket is created with #pj_sock_socket(), it exists in a
1222 * name space (address family) but has no name assigned.
1223 *
1224 * @param sockfd The socket desriptor.
1225 * @param my_addr The local address to bind the socket to.
1226 * @param addrlen The length of the address.
1227 *
1228 * @return Zero on success.
1229 */
1230 PJ_DECL(pj_status_t) pj_sock_bind( pj_sock_t sockfd,
1231 const pj_sockaddr_t *my_addr,
1232 int addrlen);
1233
1234 /**
1235 * Bind the IP socket sockfd to the given address and port.
1236 *
1237 * @param sockfd The socket descriptor.
1238 * @param addr Local address to bind the socket to, in host byte order.
1239 * @param port The local port to bind the socket to, in host byte order.
1240 *
1241 * @return Zero on success.
1242 */
1243 PJ_DECL(pj_status_t) pj_sock_bind_in( pj_sock_t sockfd,
1244 pj_uint32_t addr,
1245 pj_uint16_t port);
1246
1247 /**
1248 * Bind the IP socket sockfd to the given address and a random port in the
1249 * specified range.
1250 *
1251 * @param sockfd The socket desriptor.
1252 * @param addr The local address and port to bind the socket to.
1253 * @param port_range The port range, relative the to start port number
1254 * specified in port field in addr. Note that if the
1255 * port is zero, this param will be ignored.
1256 * @param max_try Maximum retries.
1257 *
1258 * @return Zero on success.
1259 */
1260 PJ_DECL(pj_status_t) pj_sock_bind_random( pj_sock_t sockfd,
1261 const pj_sockaddr_t *addr,
1262 pj_uint16_t port_range,
1263 pj_uint16_t max_try);
1264
1265 #if PJ_HAS_TCP
1266 /**
1267 * Listen for incoming connection. This function only applies to connection
1268 * oriented sockets (such as PJ_SOCK_STREAM or PJ_SOCK_SEQPACKET), and it
1269 * indicates the willingness to accept incoming connections.
1270 *
1271 * @param sockfd The socket descriptor.
1272 * @param backlog Defines the maximum length the queue of pending
1273 * connections may grow to.
1274 *
1275 * @return Zero on success.
1276 */
1277 PJ_DECL(pj_status_t) pj_sock_listen( pj_sock_t sockfd,
1278 int backlog );
1279
1280 /**
1281 * Accept new connection on the specified connection oriented server socket.
1282 *
1283 * @param serverfd The server socket.
1284 * @param newsock New socket on success, of PJ_INVALID_SOCKET if failed.
1285 * @param addr A pointer to sockaddr type. If the argument is not NULL,
1286 * it will be filled by the address of connecting entity.
1287 * @param addrlen Initially specifies the length of the address, and upon
1288 * return will be filled with the exact address length.
1289 *
1290 * @return Zero on success, or the error number.
1291 */
1292 PJ_DECL(pj_status_t) pj_sock_accept( pj_sock_t serverfd,
1293 pj_sock_t *newsock,
1294 pj_sockaddr_t *addr,
1295 int *addrlen);
1296 #endif
1297
1298 /**
1299 * The file descriptor sockfd must refer to a socket. If the socket is of
1300 * type PJ_SOCK_DGRAM then the serv_addr address is the address to which
1301 * datagrams are sent by default, and the only address from which datagrams
1302 * are received. If the socket is of type PJ_SOCK_STREAM or PJ_SOCK_SEQPACKET,
1303 * this call attempts to make a connection to another socket. The
1304 * other socket is specified by serv_addr, which is an address (of length
1305 * addrlen) in the communications space of the socket. Each communications
1306 * space interprets the serv_addr parameter in its own way.
1307 *
1308 * @param sockfd The socket descriptor.
1309 * @param serv_addr Server address to connect to.
1310 * @param addrlen The length of server address.
1311 *
1312 * @return Zero on success.
1313 */
1314 PJ_DECL(pj_status_t) pj_sock_connect( pj_sock_t sockfd,
1315 const pj_sockaddr_t *serv_addr,
1316 int addrlen);
1317
1318 /**
1319 * Return the address of peer which is connected to socket sockfd.
1320 *
1321 * @param sockfd The socket descriptor.
1322 * @param addr Pointer to sockaddr structure to which the address
1323 * will be returned.
1324 * @param namelen Initially the length of the addr. Upon return the value
1325 * will be set to the actual length of the address.
1326 *
1327 * @return Zero on success.
1328 */
1329 PJ_DECL(pj_status_t) pj_sock_getpeername(pj_sock_t sockfd,
1330 pj_sockaddr_t *addr,
1331 int *namelen);
1332
1333 /**
1334 * Return the current name of the specified socket.
1335 *
1336 * @param sockfd The socket descriptor.
1337 * @param addr Pointer to sockaddr structure to which the address
1338 * will be returned.
1339 * @param namelen Initially the length of the addr. Upon return the value
1340 * will be set to the actual length of the address.
1341 *
1342 * @return Zero on success.
1343 */
1344 PJ_DECL(pj_status_t) pj_sock_getsockname( pj_sock_t sockfd,
1345 pj_sockaddr_t *addr,
1346 int *namelen);
1347
1348 /**
1349 * Get socket option associated with a socket. Options may exist at multiple
1350 * protocol levels; they are always present at the uppermost socket level.
1351 *
1352 * @param sockfd The socket descriptor.
1353 * @param level The level which to get the option from.
1354 * @param optname The option name.
1355 * @param optval Identifies the buffer which the value will be
1356 * returned.
1357 * @param optlen Initially contains the length of the buffer, upon
1358 * return will be set to the actual size of the value.
1359 *
1360 * @return Zero on success.
1361 */
1362 PJ_DECL(pj_status_t) pj_sock_getsockopt( pj_sock_t sockfd,
1363 pj_uint16_t level,
1364 pj_uint16_t optname,
1365 void *optval,
1366 int *optlen);
1367 /**
1368 * Manipulate the options associated with a socket. Options may exist at
1369 * multiple protocol levels; they are always present at the uppermost socket
1370 * level.
1371 *
1372 * @param sockfd The socket descriptor.
1373 * @param level The level which to get the option from.
1374 * @param optname The option name.
1375 * @param optval Identifies the buffer which contain the value.
1376 * @param optlen The length of the value.
1377 *
1378 * @return PJ_SUCCESS or the status code.
1379 */
1380 PJ_DECL(pj_status_t) pj_sock_setsockopt( pj_sock_t sockfd,
1381 pj_uint16_t level,
1382 pj_uint16_t optname,
1383 const void *optval,
1384 int optlen);
1385
1386 /**
1387 * Set socket options associated with a socket. This method will apply all the
1388 * options specified, and ignore any errors that might be raised.
1389 *
1390 * @param sockfd The socket descriptor.
1391 * @param params The socket options.
1392 *
1393 * @return PJ_SUCCESS or the last error code.
1394 */
1395 PJ_DECL(pj_status_t) pj_sock_setsockopt_params( pj_sock_t sockfd,
1396 const pj_sockopt_params *params);
1397
1398 /**
1399 * Helper function to set socket buffer size using #pj_sock_setsockopt()
1400 * with capability to auto retry with lower buffer setting value until
1401 * the highest possible value is successfully set.
1402 *
1403 * @param sockfd The socket descriptor.
1404 * @param optname The option name, valid values are pj_SO_RCVBUF()
1405 * and pj_SO_SNDBUF().
1406 * @param auto_retry Option whether auto retry with lower value is
1407 * enabled.
1408 * @param buf_size On input, specify the prefered buffer size setting,
1409 * on output, the buffer size setting applied.
1410 *
1411 * @return PJ_SUCCESS or the status code.
1412 */
1413 PJ_DECL(pj_status_t) pj_sock_setsockopt_sobuf( pj_sock_t sockfd,
1414 pj_uint16_t optname,
1415 pj_bool_t auto_retry,
1416 unsigned *buf_size);
1417
1418
1419 /**
1420 * Receives data stream or message coming to the specified socket.
1421 *
1422 * @param sockfd The socket descriptor.
1423 * @param buf The buffer to receive the data or message.
1424 * @param len On input, the length of the buffer. On return,
1425 * contains the length of data received.
1426 * @param flags Flags (such as pj_MSG_PEEK()).
1427 *
1428 * @return PJ_SUCCESS or the error code.
1429 */
1430 PJ_DECL(pj_status_t) pj_sock_recv(pj_sock_t sockfd,
1431 void *buf,
1432 pj_ssize_t *len,
1433 unsigned flags);
1434
1435 /**
1436 * Receives data stream or message coming to the specified socket.
1437 *
1438 * @param sockfd The socket descriptor.
1439 * @param buf The buffer to receive the data or message.
1440 * @param len On input, the length of the buffer. On return,
1441 * contains the length of data received.
1442 * @param flags Flags (such as pj_MSG_PEEK()).
1443 * @param from If not NULL, it will be filled with the source
1444 * address of the connection.
1445 * @param fromlen Initially contains the length of from address,
1446 * and upon return will be filled with the actual
1447 * length of the address.
1448 *
1449 * @return PJ_SUCCESS or the error code.
1450 */
1451 PJ_DECL(pj_status_t) pj_sock_recvfrom( pj_sock_t sockfd,
1452 void *buf,
1453 pj_ssize_t *len,
1454 unsigned flags,
1455 pj_sockaddr_t *from,
1456 int *fromlen);
1457
1458 /**
1459 * Transmit data to the socket.
1460 *
1461 * @param sockfd Socket descriptor.
1462 * @param buf Buffer containing data to be sent.
1463 * @param len On input, the length of the data in the buffer.
1464 * Upon return, it will be filled with the length
1465 * of data sent.
1466 * @param flags Flags (such as pj_MSG_DONTROUTE()).
1467 *
1468 * @return PJ_SUCCESS or the status code.
1469 */
1470 PJ_DECL(pj_status_t) pj_sock_send(pj_sock_t sockfd,
1471 const void *buf,
1472 pj_ssize_t *len,
1473 unsigned flags);
1474
1475 /**
1476 * Transmit data to the socket to the specified address.
1477 *
1478 * @param sockfd Socket descriptor.
1479 * @param buf Buffer containing data to be sent.
1480 * @param len On input, the length of the data in the buffer.
1481 * Upon return, it will be filled with the length
1482 * of data sent.
1483 * @param flags Flags (such as pj_MSG_DONTROUTE()).
1484 * @param to The address to send.
1485 * @param tolen The length of the address in bytes.
1486 *
1487 * @return PJ_SUCCESS or the status code.
1488 */
1489 PJ_DECL(pj_status_t) pj_sock_sendto(pj_sock_t sockfd,
1490 const void *buf,
1491 pj_ssize_t *len,
1492 unsigned flags,
1493 const pj_sockaddr_t *to,
1494 int tolen);
1495
1496 #if PJ_HAS_TCP
1497 /**
1498 * The shutdown call causes all or part of a full-duplex connection on the
1499 * socket associated with sockfd to be shut down.
1500 *
1501 * @param sockfd The socket descriptor.
1502 * @param how If how is PJ_SHUT_RD, further receptions will be
1503 * disallowed. If how is PJ_SHUT_WR, further transmissions
1504 * will be disallowed. If how is PJ_SHUT_RDWR, further
1505 * receptions andtransmissions will be disallowed.
1506 *
1507 * @return Zero on success.
1508 */
1509 PJ_DECL(pj_status_t) pj_sock_shutdown( pj_sock_t sockfd,
1510 int how);
1511 #endif
1512
1513 /*****************************************************************************
1514 *
1515 * Utilities.
1516 *
1517 *****************************************************************************
1518 */
1519
1520 /**
1521 * Deep clone the socket options.
1522 *
1523 * @param pool The pool.
1524 * @param dst Destination socket options.
1525 * @param src Source socket options.
1526 *
1527 * @return PJ_SUCCESS on success.
1528 */
1529 PJ_DECL(pj_status_t) pj_sockopt_params_clone(pj_pool_t *pool,
1530 pj_sockopt_params *dst,
1531 const pj_sockopt_params *src);
1532
1533 /**
1534 * Print socket address string. This method will enclose the address string
1535 * with square bracket if it's IPv6 address.
1536 *
1537 * @param host_str The host address string.
1538 * @param port The port address.
1539 * @param buf Text buffer.
1540 * @param size Size of buffer.
1541 * @param flag Bitmask combination of these value:
1542 * - 1: port number is included.
1543 *
1544 * @return The address string.
1545 */
1546 PJ_DECL(char *) pj_addr_str_print( const pj_str_t *host_str, int port,
1547 char *buf, int size, unsigned flag);
1548
1549 /**
1550 * Create socket pair
1551 * @param family Specifies a communication domain; this selects the
1552 * protocol family which will be used for communication.
1553 * On Unix, support AF_UNIX, AF_INET and AF_INET6,
1554 * On Win32, not support AF_UNIX.
1555 * @param type The socket has the indicated type, which specifies the
1556 * communication semantics.
1557 * @param protocol Specifies a particular protocol to be used with the
1558 * socket. Normally only a single protocol exists to support
1559 * a particular socket type within a given protocol family,
1560 * in which a case protocol can be specified as 0.
1561 * @param sv The new sockets vector
1562 *
1563 * @return Zero on success.
1564 *
1565 */
1566 PJ_DECL(pj_status_t) pj_sock_socketpair(int family,
1567 int type,
1568 int protocol,
1569 pj_sock_t sv[2]);
1570
1571 /**
1572 * @}
1573 */
1574
1575
1576 PJ_END_DECL
1577
1578 #endif /* __PJ_SOCK_H__ */
1579