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