blob: b8ddb02deafd2422dd7f33a72e65e9ff5ed804ce [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
Nanang Izzuddinb76154e2008-09-16 16:11:44 +0000293/** IP multicast interface. @see pj_IP_MULTICAST_IF() */
294extern const pj_uint16_t PJ_IP_MULTICAST_IF;
295
296/** IP multicast ttl. @see pj_IP_MULTICAST_TTL() */
297extern const pj_uint16_t PJ_IP_MULTICAST_TTL;
298
299/** IP multicast loopback. @see pj_IP_MULTICAST_LOOP() */
300extern const pj_uint16_t PJ_IP_MULTICAST_LOOP;
301
302/** Add an IP group membership. @see pj_IP_ADD_MEMBERSHIP() */
303extern const pj_uint16_t PJ_IP_ADD_MEMBERSHIP;
304
305/** Drop an IP group membership. @see pj_IP_DROP_MEMBERSHIP() */
306extern const pj_uint16_t PJ_IP_DROP_MEMBERSHIP;
307
Benny Prijonod51a37a2007-07-28 02:44:55 +0000308
Benny Prijono62b86eb2007-12-01 08:52:57 +0000309#if defined(PJ_DLL)
310 /** Get #PJ_SO_TYPE constant */
311 PJ_DECL(pj_uint16_t) pj_SO_TYPE(void);
Benny Prijonod51a37a2007-07-28 02:44:55 +0000312
Benny Prijono62b86eb2007-12-01 08:52:57 +0000313 /** Get #PJ_SO_RCVBUF constant */
314 PJ_DECL(pj_uint16_t) pj_SO_RCVBUF(void);
Benny Prijonod51a37a2007-07-28 02:44:55 +0000315
Benny Prijono62b86eb2007-12-01 08:52:57 +0000316 /** Get #PJ_SO_SNDBUF constant */
317 PJ_DECL(pj_uint16_t) pj_SO_SNDBUF(void);
Nanang Izzuddinb76154e2008-09-16 16:11:44 +0000318
319 /** Get #PJ_IP_MULTICAST_IF constant */
320 PJ_DECL(pj_uint16_t) pj_IP_MULTICAST_IF(void);
321
322 /** Get #PJ_IP_MULTICAST_TTL constant */
323 PJ_DECL(pj_uint16_t) pj_IP_MULTICAST_TTL(void);
324
325 /** Get #PJ_IP_MULTICAST_LOOP constant */
326 PJ_DECL(pj_uint16_t) pj_IP_MULTICAST_LOOP(void);
327
328 /** Get #PJ_IP_ADD_MEMBERSHIP constant */
329 PJ_DECL(pj_uint16_t) pj_IP_ADD_MEMBERSHIP(void);
330
331 /** Get #PJ_IP_DROP_MEMBERSHIP constant */
332 PJ_DECL(pj_uint16_t) pj_IP_DROP_MEMBERSHIP(void);
Benny Prijono62b86eb2007-12-01 08:52:57 +0000333#else
334 /** Get #PJ_SO_TYPE constant */
335# define pj_SO_TYPE() PJ_SO_TYPE
336
337 /** Get #PJ_SO_RCVBUF constant */
338# define pj_SO_RCVBUF() PJ_SO_RCVBUF
339
340 /** Get #PJ_SO_SNDBUF constant */
341# define pj_SO_SNDBUF() PJ_SO_SNDBUF
Nanang Izzuddinb76154e2008-09-16 16:11:44 +0000342
343 /** Get #PJ_IP_MULTICAST_IF constant */
344# define pj_IP_MULTICAST_IF() PJ_IP_MULTICAST_IF
345
346 /** Get #PJ_IP_MULTICAST_TTL constant */
347# define pj_IP_MULTICAST_TTL() PJ_IP_MULTICAST_TTL
348
349 /** Get #PJ_IP_MULTICAST_LOOP constant */
350# define pj_IP_MULTICAST_LOOP() PJ_IP_MULTICAST_LOOP
351
352 /** Get #PJ_IP_ADD_MEMBERSHIP constant */
353# define pj_IP_ADD_MEMBERSHIP() PJ_IP_ADD_MEMBERSHIP
354
355 /** Get #PJ_IP_DROP_MEMBERSHIP constant */
356# define pj_IP_DROP_MEMBERSHIP() PJ_IP_DROP_MEMBERSHIP
Benny Prijono62b86eb2007-12-01 08:52:57 +0000357#endif
Benny Prijono9033e312005-11-21 02:08:39 +0000358
359
Benny Prijono57dc48b2006-12-25 06:39:33 +0000360/*
Benny Prijono9033e312005-11-21 02:08:39 +0000361 * Flags to be specified in #pj_sock_recv, #pj_sock_send, etc.
362 */
Benny Prijono8ab968f2007-07-20 08:08:30 +0000363
Benny Prijonod51a37a2007-07-28 02:44:55 +0000364/** Out-of-band messages. @see pj_MSG_OOB() */
365extern const int PJ_MSG_OOB;
366
367/** Peek, don't remove from buffer. @see pj_MSG_PEEK() */
368extern const int PJ_MSG_PEEK;
369
370/** Don't route. @see pj_MSG_DONTROUTE() */
371extern const int PJ_MSG_DONTROUTE;
372
373
Benny Prijono62b86eb2007-12-01 08:52:57 +0000374#if defined(PJ_DLL)
375 /** Get #PJ_MSG_OOB constant */
376 PJ_DECL(int) pj_MSG_OOB(void);
Benny Prijonod51a37a2007-07-28 02:44:55 +0000377
Benny Prijono62b86eb2007-12-01 08:52:57 +0000378 /** Get #PJ_MSG_PEEK constant */
379 PJ_DECL(int) pj_MSG_PEEK(void);
Benny Prijonod51a37a2007-07-28 02:44:55 +0000380
Benny Prijono62b86eb2007-12-01 08:52:57 +0000381 /** Get #PJ_MSG_DONTROUTE constant */
382 PJ_DECL(int) pj_MSG_DONTROUTE(void);
383#else
384 /** Get #PJ_MSG_OOB constant */
385# define pj_MSG_OOB() PJ_MSG_OOB
386
387 /** Get #PJ_MSG_PEEK constant */
388# define pj_MSG_PEEK() PJ_MSG_PEEK
389
390 /** Get #PJ_MSG_DONTROUTE constant */
391# define pj_MSG_DONTROUTE() PJ_MSG_DONTROUTE
392#endif
Benny Prijono9033e312005-11-21 02:08:39 +0000393
394
395/**
Benny Prijonod51a37a2007-07-28 02:44:55 +0000396 * Flag to be specified in #pj_sock_shutdown().
Benny Prijono9033e312005-11-21 02:08:39 +0000397 */
398typedef enum pj_socket_sd_type
399{
400 PJ_SD_RECEIVE = 0, /**< No more receive. */
401 PJ_SHUT_RD = 0, /**< Alias for SD_RECEIVE. */
402 PJ_SD_SEND = 1, /**< No more sending. */
403 PJ_SHUT_WR = 1, /**< Alias for SD_SEND. */
404 PJ_SD_BOTH = 2, /**< No more send and receive. */
Benny Prijono92ac4472006-07-22 13:42:56 +0000405 PJ_SHUT_RDWR = 2 /**< Alias for SD_BOTH. */
Benny Prijono9033e312005-11-21 02:08:39 +0000406} pj_socket_sd_type;
407
408
409
410/** Address to accept any incoming messages. */
411#define PJ_INADDR_ANY ((pj_uint32_t)0)
412
413/** Address indicating an error return */
414#define PJ_INADDR_NONE ((pj_uint32_t)0xffffffff)
415
416/** Address to send to all hosts. */
417#define PJ_INADDR_BROADCAST ((pj_uint32_t)0xffffffff)
418
419
420/**
421 * Maximum length specifiable by #pj_sock_listen().
422 * If the build system doesn't override this value, then the lowest
423 * denominator (five, in Win32 systems) will be used.
424 */
425#if !defined(PJ_SOMAXCONN)
426# define PJ_SOMAXCONN 5
427#endif
428
429
430/**
431 * Constant for invalid socket returned by #pj_sock_socket() and
432 * #pj_sock_accept().
433 */
434#define PJ_INVALID_SOCKET (-1)
435
Benny Prijono62b86eb2007-12-01 08:52:57 +0000436/* Must undefine s_addr because of pj_in_addr below */
Benny Prijonoab668ed2006-06-01 11:38:40 +0000437#undef s_addr
438
Benny Prijono9033e312005-11-21 02:08:39 +0000439/**
440 * This structure describes Internet address.
441 */
442typedef struct pj_in_addr
443{
444 pj_uint32_t s_addr; /**< The 32bit IP address. */
445} pj_in_addr;
446
447
448/**
Benny Prijonoc16c6e32007-11-18 14:53:47 +0000449 * Maximum length of text representation of an IPv4 address.
450 */
451#define PJ_INET_ADDRSTRLEN 16
452
453/**
454 * Maximum length of text representation of an IPv6 address.
455 */
456#define PJ_INET6_ADDRSTRLEN 46
457
458
459/**
Benny Prijono9033e312005-11-21 02:08:39 +0000460 * This structure describes Internet socket address.
Benny Prijonoe67d99a2006-03-20 12:39:24 +0000461 * If PJ_SOCKADDR_HAS_LEN is not zero, then sin_zero_len member is added
462 * to this struct. As far the application is concerned, the value of
463 * this member will always be zero. Internally, PJLIB may modify the value
464 * before calling OS socket API, and reset the value back to zero before
465 * returning the struct to application.
Benny Prijono9033e312005-11-21 02:08:39 +0000466 */
Benny Prijono0ca04b62005-12-30 23:50:15 +0000467struct pj_sockaddr_in
Benny Prijono9033e312005-11-21 02:08:39 +0000468{
Benny Prijonoe67d99a2006-03-20 12:39:24 +0000469#if defined(PJ_SOCKADDR_HAS_LEN) && PJ_SOCKADDR_HAS_LEN!=0
470 pj_uint8_t sin_zero_len; /**< Just ignore this. */
471 pj_uint8_t sin_family; /**< Address family. */
472#else
Benny Prijono9033e312005-11-21 02:08:39 +0000473 pj_uint16_t sin_family; /**< Address family. */
Benny Prijonoe67d99a2006-03-20 12:39:24 +0000474#endif
Benny Prijono9033e312005-11-21 02:08:39 +0000475 pj_uint16_t sin_port; /**< Transport layer port number. */
476 pj_in_addr sin_addr; /**< IP address. */
477 char sin_zero[8]; /**< Padding. */
Benny Prijono0ca04b62005-12-30 23:50:15 +0000478};
Benny Prijono9033e312005-11-21 02:08:39 +0000479
Benny Prijonoc16c6e32007-11-18 14:53:47 +0000480
Benny Prijonob681a2f2007-03-25 18:44:51 +0000481#undef s6_addr
Benny Prijono9033e312005-11-21 02:08:39 +0000482
483/**
484 * This structure describes IPv6 address.
485 */
Benny Prijonoc16c6e32007-11-18 14:53:47 +0000486typedef union pj_in6_addr
Benny Prijono9033e312005-11-21 02:08:39 +0000487{
Benny Prijonoc16c6e32007-11-18 14:53:47 +0000488 /* This is the main entry */
489 pj_uint8_t s6_addr[16]; /**< 8-bit array */
490
491 /* While these are used for proper alignment */
492 pj_uint32_t u6_addr32[4];
Benny Prijono62b86eb2007-12-01 08:52:57 +0000493
494 /* Do not use this with Winsock2, as this will align pj_sockaddr_in6
495 * to 64-bit boundary and Winsock2 doesn't like it!
496 */
497#if defined(PJ_HAS_INT64) && PJ_HAS_INT64!=0 && \
498 (!defined(PJ_WIN32) || PJ_WIN32==0)
Benny Prijonoc16c6e32007-11-18 14:53:47 +0000499 pj_int64_t u6_addr64[2];
500#endif
501
Benny Prijono9033e312005-11-21 02:08:39 +0000502} pj_in6_addr;
503
Benny Prijonoc16c6e32007-11-18 14:53:47 +0000504
Benny Prijono9033e312005-11-21 02:08:39 +0000505/** Initializer value for pj_in6_addr. */
506#define PJ_IN6ADDR_ANY_INIT { { { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 } } }
507
508/** Initializer value for pj_in6_addr. */
509#define PJ_IN6ADDR_LOOPBACK_INIT { { { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1 } } }
510
511/**
512 * This structure describes IPv6 socket address.
Benny Prijonoe67d99a2006-03-20 12:39:24 +0000513 * If PJ_SOCKADDR_HAS_LEN is not zero, then sin_zero_len member is added
514 * to this struct. As far the application is concerned, the value of
515 * this member will always be zero. Internally, PJLIB may modify the value
516 * before calling OS socket API, and reset the value back to zero before
517 * returning the struct to application.
Benny Prijono9033e312005-11-21 02:08:39 +0000518 */
519typedef struct pj_sockaddr_in6
520{
Benny Prijonoe67d99a2006-03-20 12:39:24 +0000521#if defined(PJ_SOCKADDR_HAS_LEN) && PJ_SOCKADDR_HAS_LEN!=0
Benny Prijonoc16c6e32007-11-18 14:53:47 +0000522 pj_uint8_t sin6_zero_len; /**< Just ignore this. */
523 pj_uint8_t sin6_family; /**< Address family. */
Benny Prijonoe67d99a2006-03-20 12:39:24 +0000524#else
Benny Prijono9033e312005-11-21 02:08:39 +0000525 pj_uint16_t sin6_family; /**< Address family */
Benny Prijonoe67d99a2006-03-20 12:39:24 +0000526#endif
Benny Prijono9033e312005-11-21 02:08:39 +0000527 pj_uint16_t sin6_port; /**< Transport layer port number. */
528 pj_uint32_t sin6_flowinfo; /**< IPv6 flow information */
529 pj_in6_addr sin6_addr; /**< IPv6 address. */
Benny Prijonoc16c6e32007-11-18 14:53:47 +0000530 pj_uint32_t sin6_scope_id; /**< Set of interfaces for a scope */
Benny Prijono9033e312005-11-21 02:08:39 +0000531} pj_sockaddr_in6;
532
533
Benny Prijonob01897b2007-03-18 17:39:27 +0000534/**
535 * This structure describes common attributes found in transport addresses.
536 * If PJ_SOCKADDR_HAS_LEN is not zero, then sa_zero_len member is added
537 * to this struct. As far the application is concerned, the value of
538 * this member will always be zero. Internally, PJLIB may modify the value
539 * before calling OS socket API, and reset the value back to zero before
540 * returning the struct to application.
541 */
542typedef struct pj_addr_hdr
543{
544#if defined(PJ_SOCKADDR_HAS_LEN) && PJ_SOCKADDR_HAS_LEN!=0
545 pj_uint8_t sa_zero_len;
546 pj_uint8_t sa_family;
547#else
548 pj_uint16_t sa_family; /**< Common data: address family. */
549#endif
550} pj_addr_hdr;
551
552
553/**
554 * This union describes a generic socket address.
555 */
556typedef union pj_sockaddr
557{
558 pj_addr_hdr addr; /**< Generic transport address. */
559 pj_sockaddr_in ipv4; /**< IPv4 transport address. */
560 pj_sockaddr_in6 ipv6; /**< IPv6 transport address. */
561} pj_sockaddr;
562
563
Nanang Izzuddinb76154e2008-09-16 16:11:44 +0000564/**
565 * This structure provides multicast group information for IPv4 addresses.
566 */
567typedef struct pj_ip_mreq {
568 pj_in_addr imr_multiaddr; /**< IP multicast address of group. */
569 pj_in_addr imr_interface; /**< local IP address of interface. */
570} pj_ip_mreq;
571
572
Benny Prijono9033e312005-11-21 02:08:39 +0000573/*****************************************************************************
574 *
575 * SOCKET ADDRESS MANIPULATION.
576 *
577 *****************************************************************************
578 */
579
580/**
581 * Convert 16-bit value from network byte order to host byte order.
582 *
583 * @param netshort 16-bit network value.
584 * @return 16-bit host value.
585 */
586PJ_DECL(pj_uint16_t) pj_ntohs(pj_uint16_t netshort);
587
588/**
589 * Convert 16-bit value from host byte order to network byte order.
590 *
591 * @param hostshort 16-bit host value.
592 * @return 16-bit network value.
593 */
594PJ_DECL(pj_uint16_t) pj_htons(pj_uint16_t hostshort);
595
596/**
597 * Convert 32-bit value from network byte order to host byte order.
598 *
599 * @param netlong 32-bit network value.
600 * @return 32-bit host value.
601 */
602PJ_DECL(pj_uint32_t) pj_ntohl(pj_uint32_t netlong);
603
604/**
605 * Convert 32-bit value from host byte order to network byte order.
606 *
607 * @param hostlong 32-bit host value.
608 * @return 32-bit network value.
609 */
610PJ_DECL(pj_uint32_t) pj_htonl(pj_uint32_t hostlong);
611
612/**
613 * Convert an Internet host address given in network byte order
614 * to string in standard numbers and dots notation.
615 *
616 * @param inaddr The host address.
617 * @return The string address.
618 */
619PJ_DECL(char*) pj_inet_ntoa(pj_in_addr inaddr);
620
621/**
622 * This function converts the Internet host address cp from the standard
623 * numbers-and-dots notation into binary data and stores it in the structure
624 * that inp points to.
625 *
626 * @param cp IP address in standard numbers-and-dots notation.
627 * @param inp Structure that holds the output of the conversion.
628 *
629 * @return nonzero if the address is valid, zero if not.
630 */
631PJ_DECL(int) pj_inet_aton(const pj_str_t *cp, struct pj_in_addr *inp);
632
633/**
Benny Prijonoc16c6e32007-11-18 14:53:47 +0000634 * This function converts an address in its standard text presentation form
635 * into its numeric binary form. It supports both IPv4 and IPv6 address
636 * conversion.
637 *
638 * @param af Specify the family of the address. The PJ_AF_INET and
639 * PJ_AF_INET6 address families shall be supported.
640 * @param src Points to the string being passed in.
641 * @param dst Points to a buffer into which the function stores the
642 * numeric address; this shall be large enough to hold the
643 * numeric address (32 bits for PJ_AF_INET, 128 bits for
644 * PJ_AF_INET6).
645 *
646 * @return PJ_SUCCESS if conversion was successful.
647 */
648PJ_DECL(pj_status_t) pj_inet_pton(int af, const pj_str_t *src, void *dst);
649
650/**
651 * This function converts a numeric address into a text string suitable
652 * for presentation. It supports both IPv4 and IPv6 address
Benny Prijono40c26332007-12-03 14:33:39 +0000653 * conversion.
654 * @see pj_sockaddr_print()
Benny Prijonoc16c6e32007-11-18 14:53:47 +0000655 *
656 * @param af Specify the family of the address. This can be PJ_AF_INET
657 * or PJ_AF_INET6.
658 * @param src Points to a buffer holding an IPv4 address if the af argument
659 * is PJ_AF_INET, or an IPv6 address if the af argument is
660 * PJ_AF_INET6; the address must be in network byte order.
661 * @param dst Points to a buffer where the function stores the resulting
662 * text string; it shall not be NULL.
663 * @param size Specifies the size of this buffer, which shall be large
664 * enough to hold the text string (PJ_INET_ADDRSTRLEN characters
665 * for IPv4, PJ_INET6_ADDRSTRLEN characters for IPv6).
666 *
Benny Prijono80025db2007-12-02 15:36:46 +0000667 * @return PJ_SUCCESS if conversion was successful.
Benny Prijonoc16c6e32007-11-18 14:53:47 +0000668 */
669PJ_DECL(pj_status_t) pj_inet_ntop(int af, const void *src,
670 char *dst, int size);
671
672/**
Benny Prijono80025db2007-12-02 15:36:46 +0000673 * Converts numeric address into its text string representation.
Benny Prijono40c26332007-12-03 14:33:39 +0000674 * @see pj_sockaddr_print()
Benny Prijono80025db2007-12-02 15:36:46 +0000675 *
676 * @param af Specify the family of the address. This can be PJ_AF_INET
677 * or PJ_AF_INET6.
678 * @param src Points to a buffer holding an IPv4 address if the af argument
679 * is PJ_AF_INET, or an IPv6 address if the af argument is
680 * PJ_AF_INET6; the address must be in network byte order.
681 * @param dst Points to a buffer where the function stores the resulting
682 * text string; it shall not be NULL.
683 * @param size Specifies the size of this buffer, which shall be large
684 * enough to hold the text string (PJ_INET_ADDRSTRLEN characters
685 * for IPv4, PJ_INET6_ADDRSTRLEN characters for IPv6).
686 *
687 * @return The address string or NULL if failed.
688 */
689PJ_DECL(char*) pj_inet_ntop2(int af, const void *src,
690 char *dst, int size);
691
Benny Prijono40c26332007-12-03 14:33:39 +0000692/**
693 * Print socket address.
694 *
695 * @param addr The socket address.
696 * @param buf Text buffer.
697 * @param size Size of buffer.
698 * @param flags Bitmask combination of these value:
699 * - 1: port number is included.
700 * - 2: square bracket is included for IPv6 address.
701 *
702 * @return The address string.
703 */
704PJ_DECL(char*) pj_sockaddr_print(const pj_sockaddr_t *addr,
705 char *buf, int size,
706 unsigned flags);
Benny Prijono80025db2007-12-02 15:36:46 +0000707
708/**
Benny Prijono9033e312005-11-21 02:08:39 +0000709 * Convert address string with numbers and dots to binary IP address.
710 *
711 * @param cp The IP address in numbers and dots notation.
712 * @return If success, the IP address is returned in network
713 * byte order. If failed, PJ_INADDR_NONE will be
714 * returned.
715 * @remark
716 * This is an obsolete interface to #pj_inet_aton(); it is obsolete
717 * because -1 is a valid address (255.255.255.255), and #pj_inet_aton()
718 * provides a cleaner way to indicate error return.
719 */
720PJ_DECL(pj_in_addr) pj_inet_addr(const pj_str_t *cp);
721
Benny Prijono7a4cf152006-03-22 11:48:33 +0000722/**
723 * Convert address string with numbers and dots to binary IP address.
724 *
725 * @param cp The IP address in numbers and dots notation.
726 * @return If success, the IP address is returned in network
727 * byte order. If failed, PJ_INADDR_NONE will be
728 * returned.
729 * @remark
730 * This is an obsolete interface to #pj_inet_aton(); it is obsolete
731 * because -1 is a valid address (255.255.255.255), and #pj_inet_aton()
732 * provides a cleaner way to indicate error return.
733 */
734PJ_DECL(pj_in_addr) pj_inet_addr2(const char *cp);
Benny Prijono9033e312005-11-21 02:08:39 +0000735
736/**
Benny Prijono62b86eb2007-12-01 08:52:57 +0000737 * Initialize IPv4 socket address based on the address and port info.
Benny Prijono9033e312005-11-21 02:08:39 +0000738 * The string address may be in a standard numbers and dots notation or
739 * may be a hostname. If hostname is specified, then the function will
740 * resolve the host into the IP address.
741 *
Benny Prijono62b86eb2007-12-01 08:52:57 +0000742 * @see pj_sockaddr_init()
743 *
Benny Prijono9033e312005-11-21 02:08:39 +0000744 * @param addr The IP socket address to be set.
745 * @param cp The address string, which can be in a standard
746 * dotted numbers or a hostname to be resolved.
747 * @param port The port number, in host byte order.
748 *
749 * @return Zero on success.
750 */
751PJ_DECL(pj_status_t) pj_sockaddr_in_init( pj_sockaddr_in *addr,
752 const pj_str_t *cp,
753 pj_uint16_t port);
754
Benny Prijono62b86eb2007-12-01 08:52:57 +0000755/**
756 * Initialize IP socket address based on the address and port info.
757 * 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 *
761 * @see pj_sockaddr_in_init()
762 *
763 * @param af Internet address family.
764 * @param addr The IP socket address to be set.
765 * @param cp The address string, which can be in a standard
766 * dotted numbers or a hostname to be resolved.
767 * @param port The port number, in host byte order.
768 *
769 * @return Zero on success.
770 */
771PJ_DECL(pj_status_t) pj_sockaddr_init(int af,
772 pj_sockaddr *addr,
773 const pj_str_t *cp,
774 pj_uint16_t port);
775
776/**
Benny Prijono40c26332007-12-03 14:33:39 +0000777 * Compare two socket addresses.
778 *
779 * @param addr1 First address.
780 * @param addr2 Second address.
781 *
782 * @return Zero on equal, -1 if addr1 is less than addr2,
783 * and +1 if addr1 is more than addr2.
784 */
785PJ_DECL(int) pj_sockaddr_cmp(const pj_sockaddr_t *addr1,
786 const pj_sockaddr_t *addr2);
787
788/**
Benny Prijono62b86eb2007-12-01 08:52:57 +0000789 * Get pointer to the address part of a socket address.
790 *
791 * @param addr Socket address.
792 *
793 * @return Pointer to address part (sin_addr or sin6_addr,
794 * depending on address family)
795 */
796PJ_DECL(void*) pj_sockaddr_get_addr(const pj_sockaddr_t *addr);
797
798/**
799 * Check that a socket address contains a non-zero address part.
800 *
801 * @param addr Socket address.
802 *
803 * @return Non-zero if address is set to non-zero.
804 */
805PJ_DECL(pj_bool_t) pj_sockaddr_has_addr(const pj_sockaddr_t *addr);
806
807/**
808 * Get the address part length of a socket address, based on its address
809 * family. For PJ_AF_INET, the length will be sizeof(pj_in_addr), and
810 * for PJ_AF_INET6, the length will be sizeof(pj_in6_addr).
811 *
812 * @param addr Socket address.
813 *
Benny Prijono80025db2007-12-02 15:36:46 +0000814 * @return Length in bytes.
Benny Prijono62b86eb2007-12-01 08:52:57 +0000815 */
816PJ_DECL(unsigned) pj_sockaddr_get_addr_len(const pj_sockaddr_t *addr);
817
818/**
Benny Prijono80025db2007-12-02 15:36:46 +0000819 * Get the socket address length, based on its address
820 * family. For PJ_AF_INET, the length will be sizeof(pj_sockaddr_in), and
821 * for PJ_AF_INET6, the length will be sizeof(pj_sockaddr_in6).
822 *
823 * @param addr Socket address.
824 *
825 * @return Length in bytes.
826 */
827PJ_DECL(unsigned) pj_sockaddr_get_len(const pj_sockaddr_t *addr);
828
Benny Prijono40c26332007-12-03 14:33:39 +0000829/**
830 * Copy only the address part (sin_addr/sin6_addr) of a socket address.
831 *
832 * @param dst Destination socket address.
833 * @param src Source socket address.
Benny Prijono842754c2008-05-11 18:11:32 +0000834 *
835 * @see @pj_sockaddr_cp()
Benny Prijono40c26332007-12-03 14:33:39 +0000836 */
837PJ_DECL(void) pj_sockaddr_copy_addr(pj_sockaddr *dst,
838 const pj_sockaddr *src);
Benny Prijono80025db2007-12-02 15:36:46 +0000839/**
Benny Prijono842754c2008-05-11 18:11:32 +0000840 * Copy socket address. This will copy the whole structure depending
841 * on the address family of the source socket address.
842 *
843 * @param dst Destination socket address.
844 * @param src Source socket address.
845 *
846 * @see @pj_sockaddr_copy_addr()
847 */
848PJ_DECL(void) pj_sockaddr_cp(pj_sockaddr_t *dst, const pj_sockaddr_t *src);
849
850/**
Benny Prijono62b86eb2007-12-01 08:52:57 +0000851 * Get the IP address of an IPv4 socket address.
852 * The address is returned as 32bit value in host byte order.
853 *
854 * @param addr The IP socket address.
855 * @return 32bit address, in host byte order.
856 */
857PJ_DECL(pj_in_addr) pj_sockaddr_in_get_addr(const pj_sockaddr_in *addr);
858
859/**
860 * Set the IP address of an IPv4 socket address.
861 *
862 * @param addr The IP socket address.
863 * @param hostaddr The host address, in host byte order.
864 */
865PJ_DECL(void) pj_sockaddr_in_set_addr(pj_sockaddr_in *addr,
866 pj_uint32_t hostaddr);
867
868/**
869 * Set the IP address of an IP socket address from string address,
870 * with resolving the host if necessary. The string address may be in a
871 * standard numbers and dots notation or may be a hostname. If hostname
872 * is specified, then the function will resolve the host into the IP
873 * address.
874 *
875 * @see pj_sockaddr_set_str_addr()
876 *
877 * @param addr The IP socket address to be set.
878 * @param cp The address string, which can be in a standard
879 * dotted numbers or a hostname to be resolved.
880 *
881 * @return PJ_SUCCESS on success.
882 */
883PJ_DECL(pj_status_t) pj_sockaddr_in_set_str_addr( pj_sockaddr_in *addr,
884 const pj_str_t *cp);
885
886/**
887 * Set the IP address of an IPv4 or IPv6 socket address from string address,
888 * with resolving the host if necessary. The string address may be in a
889 * standard IPv6 or IPv6 address or may be a hostname. If hostname
890 * is specified, then the function will resolve the host into the IP
891 * address according to the address family.
892 *
893 * @param af Address family.
894 * @param addr The IP socket address to be set.
895 * @param cp The address string, which can be in a standard
896 * IP numbers (IPv4 or IPv6) or a hostname to be resolved.
897 *
898 * @return PJ_SUCCESS on success.
899 */
900PJ_DECL(pj_status_t) pj_sockaddr_set_str_addr(int af,
901 pj_sockaddr *addr,
902 const pj_str_t *cp);
903
904/**
905 * Get the port number of a socket address, in host byte order.
906 * This function can be used for both IPv4 and IPv6 socket address.
907 *
908 * @param addr Socket address.
909 *
910 * @return Port number, in host byte order.
911 */
912PJ_DECL(pj_uint16_t) pj_sockaddr_get_port(const pj_sockaddr_t *addr);
913
914/**
915 * Get the transport layer port number of an Internet socket address.
916 * The port is returned in host byte order.
917 *
918 * @param addr The IP socket address.
919 * @return Port number, in host byte order.
920 */
921PJ_DECL(pj_uint16_t) pj_sockaddr_in_get_port(const pj_sockaddr_in *addr);
922
923/**
924 * Set the port number of an Internet socket address.
925 *
926 * @param addr The socket address.
927 * @param hostport The port number, in host byte order.
928 */
929PJ_DECL(pj_status_t) pj_sockaddr_set_port(pj_sockaddr *addr,
930 pj_uint16_t hostport);
931
932/**
933 * Set the port number of an IPv4 socket address.
934 *
935 * @see pj_sockaddr_set_port()
936 *
937 * @param addr The IP socket address.
938 * @param hostport The port number, in host byte order.
939 */
940PJ_DECL(void) pj_sockaddr_in_set_port(pj_sockaddr_in *addr,
941 pj_uint16_t hostport);
Benny Prijono9033e312005-11-21 02:08:39 +0000942
Benny Prijono0f4b9db2009-06-04 15:11:25 +0000943/**
944 * Parse string containing IP address and optional port into socket address,
945 * possibly also with address family detection. This function supports both
946 * IPv4 and IPv6 parsing, however IPv6 parsing may only be done if IPv6 is
947 * enabled during compilation.
948 *
949 * This function supports parsing several formats. Sample IPv4 inputs and
950 * their default results::
951 * - "10.0.0.1:80": address 10.0.0.1 and port 80.
952 * - "10.0.0.1": address 10.0.0.1 and port zero.
953 * - "10.0.0.1:": address 10.0.0.1 and port zero.
954 * - "10.0.0.1:0": address 10.0.0.1 and port zero.
955 * - ":80": address 0.0.0.0 and port 80.
956 * - ":": address 0.0.0.0 and port 0.
957 * - "localhost": address 127.0.0.1 and port 0.
958 * - "localhost:": address 127.0.0.1 and port 0.
959 * - "localhost:80": address 127.0.0.1 and port 80.
960 *
961 * Sample IPv6 inputs and their default results:
962 * - "[fec0::01]:80": address fec0::01 and port 80
963 * - "[fec0::01]": address fec0::01 and port 0
964 * - "[fec0::01]:": address fec0::01 and port 0
965 * - "[fec0::01]:0": address fec0::01 and port 0
966 * - "fec0::01": address fec0::01 and port 0
967 * - "fec0::01:80": address fec0::01:80 and port 0
968 * - "::": address zero (::) and port 0
969 * - "[::]": address zero (::) and port 0
970 * - "[::]:": address zero (::) and port 0
971 * - ":::": address zero (::) and port 0
972 * - "[::]:80": address zero (::) and port 0
973 * - ":::80": address zero (::) and port 80
974 *
975 * Note: when the IPv6 socket address contains port number, the IP
976 * part of the socket address should be enclosed with square brackets,
977 * otherwise the port number will be included as part of the IP address
978 * (see "fec0::01:80" example above).
979 *
980 * @param af Optionally specify the address family to be used. If the
981 * address family is to be deducted from the input, specify
982 * pj_AF_UNSPEC() here.
983 * @param options Additional options to assist the parsing, must be zero
984 * for now.
985 * @param str The input string to be parsed.
986 * @param addr Pointer to store the result.
987 *
988 * @return PJ_SUCCESS if the parsing is successful.
989 */
990PJ_DECL(pj_status_t) pj_sockaddr_parse(int af, unsigned options,
991 const pj_str_t *str,
992 pj_sockaddr *addr);
993
Benny Prijono9033e312005-11-21 02:08:39 +0000994/*****************************************************************************
995 *
996 * HOST NAME AND ADDRESS.
997 *
998 *****************************************************************************
999 */
1000
1001/**
1002 * Get system's host name.
1003 *
1004 * @return The hostname, or empty string if the hostname can not
1005 * be identified.
1006 */
1007PJ_DECL(const pj_str_t*) pj_gethostname(void);
1008
1009/**
1010 * Get host's IP address, which the the first IP address that is resolved
1011 * from the hostname.
1012 *
1013 * @return The host's IP address, PJ_INADDR_NONE if the host
1014 * IP address can not be identified.
1015 */
1016PJ_DECL(pj_in_addr) pj_gethostaddr(void);
1017
1018
1019/*****************************************************************************
1020 *
1021 * SOCKET API.
1022 *
1023 *****************************************************************************
1024 */
1025
1026/**
1027 * Create new socket/endpoint for communication.
1028 *
1029 * @param family Specifies a communication domain; this selects the
1030 * protocol family which will be used for communication.
1031 * @param type The socket has the indicated type, which specifies the
1032 * communication semantics.
1033 * @param protocol Specifies a particular protocol to be used with the
1034 * socket. Normally only a single protocol exists to support
1035 * a particular socket type within a given protocol family,
1036 * in which a case protocol can be specified as 0.
1037 * @param sock New socket descriptor, or PJ_INVALID_SOCKET on error.
1038 *
1039 * @return Zero on success.
1040 */
1041PJ_DECL(pj_status_t) pj_sock_socket(int family,
1042 int type,
1043 int protocol,
1044 pj_sock_t *sock);
1045
1046/**
1047 * Close the socket descriptor.
1048 *
1049 * @param sockfd The socket descriptor.
1050 *
1051 * @return Zero on success.
1052 */
1053PJ_DECL(pj_status_t) pj_sock_close(pj_sock_t sockfd);
1054
1055
1056/**
1057 * This function gives the socket sockfd the local address my_addr. my_addr is
1058 * addrlen bytes long. Traditionally, this is called assigning a name to
1059 * a socket. When a socket is created with #pj_sock_socket(), it exists in a
1060 * name space (address family) but has no name assigned.
1061 *
1062 * @param sockfd The socket desriptor.
1063 * @param my_addr The local address to bind the socket to.
1064 * @param addrlen The length of the address.
1065 *
1066 * @return Zero on success.
1067 */
1068PJ_DECL(pj_status_t) pj_sock_bind( pj_sock_t sockfd,
1069 const pj_sockaddr_t *my_addr,
1070 int addrlen);
1071
1072/**
1073 * Bind the IP socket sockfd to the given address and port.
1074 *
1075 * @param sockfd The socket descriptor.
1076 * @param addr Local address to bind the socket to, in host byte order.
1077 * @param port The local port to bind the socket to, in host byte order.
1078 *
1079 * @return Zero on success.
1080 */
1081PJ_DECL(pj_status_t) pj_sock_bind_in( pj_sock_t sockfd,
1082 pj_uint32_t addr,
1083 pj_uint16_t port);
1084
1085#if PJ_HAS_TCP
1086/**
1087 * Listen for incoming connection. This function only applies to connection
1088 * oriented sockets (such as PJ_SOCK_STREAM or PJ_SOCK_SEQPACKET), and it
1089 * indicates the willingness to accept incoming connections.
1090 *
1091 * @param sockfd The socket descriptor.
1092 * @param backlog Defines the maximum length the queue of pending
1093 * connections may grow to.
1094 *
1095 * @return Zero on success.
1096 */
1097PJ_DECL(pj_status_t) pj_sock_listen( pj_sock_t sockfd,
1098 int backlog );
1099
1100/**
1101 * Accept new connection on the specified connection oriented server socket.
1102 *
1103 * @param serverfd The server socket.
1104 * @param newsock New socket on success, of PJ_INVALID_SOCKET if failed.
1105 * @param addr A pointer to sockaddr type. If the argument is not NULL,
1106 * it will be filled by the address of connecting entity.
1107 * @param addrlen Initially specifies the length of the address, and upon
1108 * return will be filled with the exact address length.
1109 *
1110 * @return Zero on success, or the error number.
1111 */
1112PJ_DECL(pj_status_t) pj_sock_accept( pj_sock_t serverfd,
1113 pj_sock_t *newsock,
1114 pj_sockaddr_t *addr,
1115 int *addrlen);
1116#endif
1117
1118/**
1119 * The file descriptor sockfd must refer to a socket. If the socket is of
1120 * type PJ_SOCK_DGRAM then the serv_addr address is the address to which
1121 * datagrams are sent by default, and the only address from which datagrams
1122 * are received. If the socket is of type PJ_SOCK_STREAM or PJ_SOCK_SEQPACKET,
1123 * this call attempts to make a connection to another socket. The
1124 * other socket is specified by serv_addr, which is an address (of length
1125 * addrlen) in the communications space of the socket. Each communications
1126 * space interprets the serv_addr parameter in its own way.
1127 *
1128 * @param sockfd The socket descriptor.
1129 * @param serv_addr Server address to connect to.
1130 * @param addrlen The length of server address.
1131 *
1132 * @return Zero on success.
1133 */
1134PJ_DECL(pj_status_t) pj_sock_connect( pj_sock_t sockfd,
1135 const pj_sockaddr_t *serv_addr,
1136 int addrlen);
1137
1138/**
1139 * Return the address of peer which is connected to socket sockfd.
1140 *
1141 * @param sockfd The socket descriptor.
1142 * @param addr Pointer to sockaddr structure to which the address
1143 * will be returned.
1144 * @param namelen Initially the length of the addr. Upon return the value
1145 * will be set to the actual length of the address.
1146 *
1147 * @return Zero on success.
1148 */
1149PJ_DECL(pj_status_t) pj_sock_getpeername(pj_sock_t sockfd,
1150 pj_sockaddr_t *addr,
1151 int *namelen);
1152
1153/**
1154 * Return the current name of the specified socket.
1155 *
1156 * @param sockfd The socket descriptor.
1157 * @param addr Pointer to sockaddr structure to which the address
1158 * will be returned.
1159 * @param namelen Initially the length of the addr. Upon return the value
1160 * will be set to the actual length of the address.
1161 *
1162 * @return Zero on success.
1163 */
1164PJ_DECL(pj_status_t) pj_sock_getsockname( pj_sock_t sockfd,
1165 pj_sockaddr_t *addr,
1166 int *namelen);
1167
1168/**
1169 * Get socket option associated with a socket. Options may exist at multiple
1170 * protocol levels; they are always present at the uppermost socket level.
1171 *
1172 * @param sockfd The socket descriptor.
1173 * @param level The level which to get the option from.
1174 * @param optname The option name.
1175 * @param optval Identifies the buffer which the value will be
1176 * returned.
1177 * @param optlen Initially contains the length of the buffer, upon
1178 * return will be set to the actual size of the value.
1179 *
1180 * @return Zero on success.
1181 */
1182PJ_DECL(pj_status_t) pj_sock_getsockopt( pj_sock_t sockfd,
1183 pj_uint16_t level,
1184 pj_uint16_t optname,
1185 void *optval,
1186 int *optlen);
1187/**
1188 * Manipulate the options associated with a socket. Options may exist at
1189 * multiple protocol levels; they are always present at the uppermost socket
1190 * level.
1191 *
1192 * @param sockfd The socket descriptor.
1193 * @param level The level which to get the option from.
1194 * @param optname The option name.
1195 * @param optval Identifies the buffer which contain the value.
1196 * @param optlen The length of the value.
1197 *
1198 * @return PJ_SUCCESS or the status code.
1199 */
1200PJ_DECL(pj_status_t) pj_sock_setsockopt( pj_sock_t sockfd,
1201 pj_uint16_t level,
1202 pj_uint16_t optname,
1203 const void *optval,
1204 int optlen);
1205
1206
1207/**
1208 * Receives data stream or message coming to the specified socket.
1209 *
1210 * @param sockfd The socket descriptor.
1211 * @param buf The buffer to receive the data or message.
1212 * @param len On input, the length of the buffer. On return,
1213 * contains the length of data received.
Benny Prijonod51a37a2007-07-28 02:44:55 +00001214 * @param flags Flags (such as pj_MSG_PEEK()).
Benny Prijono9033e312005-11-21 02:08:39 +00001215 *
1216 * @return PJ_SUCCESS or the error code.
1217 */
1218PJ_DECL(pj_status_t) pj_sock_recv(pj_sock_t sockfd,
1219 void *buf,
1220 pj_ssize_t *len,
1221 unsigned flags);
1222
1223/**
1224 * Receives data stream or message coming to the specified socket.
1225 *
1226 * @param sockfd The socket descriptor.
1227 * @param buf The buffer to receive the data or message.
1228 * @param len On input, the length of the buffer. On return,
1229 * contains the length of data received.
Benny Prijonod51a37a2007-07-28 02:44:55 +00001230 * @param flags Flags (such as pj_MSG_PEEK()).
Benny Prijono9033e312005-11-21 02:08:39 +00001231 * @param from If not NULL, it will be filled with the source
1232 * address of the connection.
1233 * @param fromlen Initially contains the length of from address,
1234 * and upon return will be filled with the actual
1235 * length of the address.
1236 *
1237 * @return PJ_SUCCESS or the error code.
1238 */
1239PJ_DECL(pj_status_t) pj_sock_recvfrom( pj_sock_t sockfd,
1240 void *buf,
1241 pj_ssize_t *len,
1242 unsigned flags,
1243 pj_sockaddr_t *from,
1244 int *fromlen);
1245
1246/**
1247 * Transmit data to the socket.
1248 *
1249 * @param sockfd Socket descriptor.
1250 * @param buf Buffer containing data to be sent.
1251 * @param len On input, the length of the data in the buffer.
1252 * Upon return, it will be filled with the length
1253 * of data sent.
Benny Prijonod51a37a2007-07-28 02:44:55 +00001254 * @param flags Flags (such as pj_MSG_DONTROUTE()).
Benny Prijono9033e312005-11-21 02:08:39 +00001255 *
1256 * @return PJ_SUCCESS or the status code.
1257 */
1258PJ_DECL(pj_status_t) pj_sock_send(pj_sock_t sockfd,
1259 const void *buf,
1260 pj_ssize_t *len,
1261 unsigned flags);
1262
1263/**
1264 * Transmit data to the socket to the specified address.
1265 *
1266 * @param sockfd Socket descriptor.
1267 * @param buf Buffer containing data to be sent.
1268 * @param len On input, the length of the data in the buffer.
1269 * Upon return, it will be filled with the length
1270 * of data sent.
Benny Prijonod51a37a2007-07-28 02:44:55 +00001271 * @param flags Flags (such as pj_MSG_DONTROUTE()).
Benny Prijono9033e312005-11-21 02:08:39 +00001272 * @param to The address to send.
1273 * @param tolen The length of the address in bytes.
1274 *
Benny Prijonoac9d1422006-01-18 23:32:27 +00001275 * @return PJ_SUCCESS or the status code.
Benny Prijono9033e312005-11-21 02:08:39 +00001276 */
1277PJ_DECL(pj_status_t) pj_sock_sendto(pj_sock_t sockfd,
1278 const void *buf,
1279 pj_ssize_t *len,
1280 unsigned flags,
1281 const pj_sockaddr_t *to,
1282 int tolen);
1283
1284#if PJ_HAS_TCP
1285/**
1286 * The shutdown call causes all or part of a full-duplex connection on the
1287 * socket associated with sockfd to be shut down.
1288 *
1289 * @param sockfd The socket descriptor.
1290 * @param how If how is PJ_SHUT_RD, further receptions will be
1291 * disallowed. If how is PJ_SHUT_WR, further transmissions
1292 * will be disallowed. If how is PJ_SHUT_RDWR, further
1293 * receptions andtransmissions will be disallowed.
1294 *
1295 * @return Zero on success.
1296 */
1297PJ_DECL(pj_status_t) pj_sock_shutdown( pj_sock_t sockfd,
1298 int how);
1299#endif
1300
1301/**
1302 * @}
1303 */
1304
1305
1306PJ_END_DECL
1307
1308#endif /* __PJ_SOCK_H__ */
1309