blob: d90c91e8b5be7c7d5d55f0b601cc8cb9c2e31dd1 [file] [log] [blame]
Benny Prijono9033e312005-11-21 02:08:39 +00001/* $Id$ */
2/*
Benny Prijonoa771a512007-02-19 01:13:53 +00003 * Copyright (C)2003-2007 Benny Prijono <benny@prijono.org>
Benny Prijono9033e312005-11-21 02:08:39 +00004 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19#ifndef __PJ_SOCK_H__
20#define __PJ_SOCK_H__
21
22/**
23 * @file sock.h
24 * @brief Socket Abstraction.
25 */
26
27#include <pj/types.h>
28
29PJ_BEGIN_DECL
30
31
32/**
33 * @defgroup PJ_SOCK Socket Abstraction
34 * @ingroup PJ_IO
35 * @{
36 *
37 * The PJLIB socket abstraction layer is a thin and very portable abstraction
38 * for socket API. It provides API similar to BSD socket API. The abstraction
39 * is needed because BSD socket API is not always available on all platforms,
40 * therefore it wouldn't be possible to create a trully portable network
41 * programs unless we provide such abstraction.
42 *
43 * Applications can use this API directly in their application, just
44 * as they would when using traditional BSD socket API, provided they
45 * call #pj_init() first.
46 *
47 * \section pj_sock_examples_sec Examples
48 *
49 * For some examples on how to use the socket API, please see:
50 *
51 * - \ref page_pjlib_sock_test
52 * - \ref page_pjlib_select_test
53 * - \ref page_pjlib_sock_perf_test
54 */
55
56
57/**
58 * Supported address families.
59 * APPLICATION MUST USE THESE VALUES INSTEAD OF NORMAL AF_*, BECAUSE
60 * THE LIBRARY WILL DO TRANSLATION TO THE NATIVE VALUE.
61 */
Benny Prijono8ab968f2007-07-20 08:08:30 +000062
Benny Prijonoc16c6e32007-11-18 14:53:47 +000063/** Address family is unspecified. @see pj_AF_UNSPEC() */
64extern const pj_uint16_t PJ_AF_UNSPEC;
65
Benny Prijonod51a37a2007-07-28 02:44:55 +000066/** Unix domain socket. @see pj_AF_UNIX() */
67extern const pj_uint16_t PJ_AF_UNIX;
68
69/** POSIX name for AF_UNIX */
70#define PJ_AF_LOCAL PJ_AF_UNIX;
71
72/** Internet IP protocol. @see pj_AF_INET() */
73extern const pj_uint16_t PJ_AF_INET;
74
75/** IP version 6. @see pj_AF_INET6() */
76extern const pj_uint16_t PJ_AF_INET6;
77
78/** Packet family. @see pj_AF_PACKET() */
79extern const pj_uint16_t PJ_AF_PACKET;
80
81/** IRDA sockets. @see pj_AF_IRDA() */
82extern const pj_uint16_t PJ_AF_IRDA;
83
84/*
85 * Accessor functions for various address family constants. These
86 * functions are provided because Symbian doesn't allow exporting
87 * global variables from a DLL.
88 */
89
Benny Prijonoc16c6e32007-11-18 14:53:47 +000090/** Get #PJ_AF_UNSPEC value */
91PJ_DECL(pj_uint16_t) pj_AF_UNSPEC(void);
Benny Prijonod51a37a2007-07-28 02:44:55 +000092/** Get #PJ_AF_UNIX value. */
Benny Prijono8ab968f2007-07-20 08:08:30 +000093PJ_DECL(pj_uint16_t) pj_AF_UNIX(void);
Benny Prijonod51a37a2007-07-28 02:44:55 +000094/** Get #PJ_AF_INET value. */
Benny Prijono8ab968f2007-07-20 08:08:30 +000095PJ_DECL(pj_uint16_t) pj_AF_INET(void);
Benny Prijonod51a37a2007-07-28 02:44:55 +000096/** Get #PJ_AF_INET6 value. */
Benny Prijono8ab968f2007-07-20 08:08:30 +000097PJ_DECL(pj_uint16_t) pj_AF_INET6(void);
Benny Prijonod51a37a2007-07-28 02:44:55 +000098/** Get #PJ_AF_PACKET value. */
Benny Prijono8ab968f2007-07-20 08:08:30 +000099PJ_DECL(pj_uint16_t) pj_AF_PACKET(void);
Benny Prijonod51a37a2007-07-28 02:44:55 +0000100/** Get #PJ_AF_IRDA value. */
Benny Prijono8ab968f2007-07-20 08:08:30 +0000101PJ_DECL(pj_uint16_t) pj_AF_IRDA(void);
Benny Prijono9033e312005-11-21 02:08:39 +0000102
103
104/**
105 * Supported types of sockets.
106 * APPLICATION MUST USE THESE VALUES INSTEAD OF NORMAL SOCK_*, BECAUSE
107 * THE LIBRARY WILL TRANSLATE THE VALUE TO THE NATIVE VALUE.
108 */
109
Benny Prijonod51a37a2007-07-28 02:44:55 +0000110/** Sequenced, reliable, connection-based byte streams.
111 * @see pj_SOCK_STREAM() */
112extern const pj_uint16_t PJ_SOCK_STREAM;
113
114/** Connectionless, unreliable datagrams of fixed maximum lengths.
115 * @see pj_SOCK_DGRAM() */
116extern const pj_uint16_t PJ_SOCK_DGRAM;
117
118/** Raw protocol interface. @see pj_SOCK_RAW() */
119extern const pj_uint16_t PJ_SOCK_RAW;
120
121/** Reliably-delivered messages. @see pj_SOCK_RDM() */
122extern const pj_uint16_t PJ_SOCK_RDM;
Benny Prijono9033e312005-11-21 02:08:39 +0000123
124
Benny Prijonod51a37a2007-07-28 02:44:55 +0000125/*
126 * Accessor functions for various constants. These functions are provided
127 * because Symbian doesn't allow exporting global variables from a DLL.
128 */
129
130/** Get #PJ_SOCK_STREAM constant */
Benny Prijono8ab968f2007-07-20 08:08:30 +0000131PJ_DECL(int) pj_SOCK_STREAM(void);
Benny Prijonod51a37a2007-07-28 02:44:55 +0000132/** Get #PJ_SOCK_DGRAM constant */
Benny Prijono8ab968f2007-07-20 08:08:30 +0000133PJ_DECL(int) pj_SOCK_DGRAM(void);
Benny Prijonod51a37a2007-07-28 02:44:55 +0000134/** Get #PJ_SOCK_RAW constant */
Benny Prijono8ab968f2007-07-20 08:08:30 +0000135PJ_DECL(int) pj_SOCK_RAW(void);
Benny Prijonod51a37a2007-07-28 02:44:55 +0000136/** Get #PJ_SOCK_RDM constant */
Benny Prijono8ab968f2007-07-20 08:08:30 +0000137PJ_DECL(int) pj_SOCK_RDM(void);
138
139
Benny Prijono9033e312005-11-21 02:08:39 +0000140/**
141 * Socket level specified in #pj_sock_setsockopt() or #pj_sock_getsockopt().
142 * APPLICATION MUST USE THESE VALUES INSTEAD OF NORMAL SOL_*, BECAUSE
143 * THE LIBRARY WILL TRANSLATE THE VALUE TO THE NATIVE VALUE.
144 */
Benny Prijonod51a37a2007-07-28 02:44:55 +0000145/** Socket level. @see pj_SOL_SOCKET() */
146extern const pj_uint16_t PJ_SOL_SOCKET;
147/** IP level. @see pj_SOL_IP() */
148extern const pj_uint16_t PJ_SOL_IP;
149/** TCP level. @see pj_SOL_TCP() */
150extern const pj_uint16_t PJ_SOL_TCP;
151/** UDP level. @see pj_SOL_UDP() */
152extern const pj_uint16_t PJ_SOL_UDP;
153/** IP version 6. @see pj_SOL_IPV6() */
154extern const pj_uint16_t PJ_SOL_IPV6;
Benny Prijono9033e312005-11-21 02:08:39 +0000155
Benny Prijonod51a37a2007-07-28 02:44:55 +0000156/*
157 * Accessor functions for various constants. These functions are provided
158 * because Symbian doesn't allow exporting global variables from a DLL.
159 */
160
161/** Get #PJ_SOL_SOCKET constant */
Benny Prijono8ab968f2007-07-20 08:08:30 +0000162PJ_DECL(pj_uint16_t) pj_SOL_SOCKET(void);
Benny Prijonod51a37a2007-07-28 02:44:55 +0000163/** Get #PJ_SOL_IP constant */
Benny Prijono8ab968f2007-07-20 08:08:30 +0000164PJ_DECL(pj_uint16_t) pj_SOL_IP(void);
Benny Prijonod51a37a2007-07-28 02:44:55 +0000165/** Get #PJ_SOL_TCP constant */
Benny Prijono8ab968f2007-07-20 08:08:30 +0000166PJ_DECL(pj_uint16_t) pj_SOL_TCP(void);
Benny Prijonod51a37a2007-07-28 02:44:55 +0000167/** Get #PJ_SOL_UDP constant */
Benny Prijono8ab968f2007-07-20 08:08:30 +0000168PJ_DECL(pj_uint16_t) pj_SOL_UDP(void);
Benny Prijonod51a37a2007-07-28 02:44:55 +0000169/** Get #PJ_SOL_IPV6 constant */
Benny Prijono8ab968f2007-07-20 08:08:30 +0000170PJ_DECL(pj_uint16_t) pj_SOL_IPV6(void);
171
Benny Prijono87a00892007-02-01 00:33:12 +0000172
173/* IP_TOS
174 *
175 * Note:
176 * TOS CURRENTLY DOES NOT WORK IN Windows 2000 and above!
177 * See http://support.microsoft.com/kb/248611
178 */
Benny Prijonod51a37a2007-07-28 02:44:55 +0000179/** IP_TOS optname in setsockopt(). @see pj_IP_TOS() */
180extern const pj_uint16_t PJ_IP_TOS;
Benny Prijono87a00892007-02-01 00:33:12 +0000181
Benny Prijonod51a37a2007-07-28 02:44:55 +0000182/** Get #PJ_IP_TOS constant */
Benny Prijono8ab968f2007-07-20 08:08:30 +0000183PJ_DECL(int) pj_IP_TOS(void);
Benny Prijono87a00892007-02-01 00:33:12 +0000184
185/*
186 * IP TOS related constats.
187 *
188 * Note:
189 * TOS CURRENTLY DOES NOT WORK IN Windows 2000 and above!
190 * See http://support.microsoft.com/kb/248611
191 */
Benny Prijonod51a37a2007-07-28 02:44:55 +0000192/** Minimize delays. @see pj_IPTOS_LOWDELAY() */
193extern const pj_uint16_t PJ_IPTOS_LOWDELAY;
Benny Prijono8ab968f2007-07-20 08:08:30 +0000194
Benny Prijonod51a37a2007-07-28 02:44:55 +0000195/** Optimize throughput. @see pj_IPTOS_THROUGHPUT() */
196extern const pj_uint16_t PJ_IPTOS_THROUGHPUT;
197
198/** Optimize for reliability. @see pj_IPTOS_RELIABILITY() */
199extern const pj_uint16_t PJ_IPTOS_RELIABILITY;
200
201/** "filler data" where slow transmission does't matter.
202 * @see pj_IPTOS_MINCOST() */
203extern const pj_uint16_t PJ_IPTOS_MINCOST;
204
205
206/** Get #PJ_IPTOS_LOWDELAY constant */
Benny Prijono8ab968f2007-07-20 08:08:30 +0000207PJ_DECL(int) pj_IPTOS_LOWDELAY(void);
Benny Prijonod51a37a2007-07-28 02:44:55 +0000208
209/** Get #PJ_IPTOS_THROUGHPUT constant */
Benny Prijono8ab968f2007-07-20 08:08:30 +0000210PJ_DECL(int) pj_IPTOS_THROUGHPUT(void);
Benny Prijonod51a37a2007-07-28 02:44:55 +0000211
212/** Get #PJ_IPTOS_RELIABILITY constant */
Benny Prijono8ab968f2007-07-20 08:08:30 +0000213PJ_DECL(int) pj_IPTOS_RELIABILITY(void);
Benny Prijonod51a37a2007-07-28 02:44:55 +0000214
215/** Get #PJ_IPTOS_MINCOST constant */
Benny Prijono8ab968f2007-07-20 08:08:30 +0000216PJ_DECL(int) pj_IPTOS_MINCOST(void);
Benny Prijono87a00892007-02-01 00:33:12 +0000217
218
Benny Prijono9033e312005-11-21 02:08:39 +0000219/**
220 * Values to be specified as \c optname when calling #pj_sock_setsockopt()
221 * or #pj_sock_getsockopt().
222 */
Benny Prijono8ab968f2007-07-20 08:08:30 +0000223
Benny Prijonod51a37a2007-07-28 02:44:55 +0000224/** Socket type. @see pj_SO_TYPE() */
225extern const pj_uint16_t PJ_SO_TYPE;
226
227/** Buffer size for receive. @see pj_SO_RCVBUF() */
228extern const pj_uint16_t PJ_SO_RCVBUF;
229
230/** Buffer size for send. @see pj_SO_SNDBUF() */
231extern const pj_uint16_t PJ_SO_SNDBUF;
232
233
234/** Get #PJ_SO_TYPE constant */
Benny Prijono8ab968f2007-07-20 08:08:30 +0000235PJ_DECL(pj_uint16_t) pj_SO_TYPE(void);
Benny Prijonod51a37a2007-07-28 02:44:55 +0000236
237/** Get #PJ_SO_RCVBUF constant */
Benny Prijono8ab968f2007-07-20 08:08:30 +0000238PJ_DECL(pj_uint16_t) pj_SO_RCVBUF(void);
Benny Prijonod51a37a2007-07-28 02:44:55 +0000239
240/** Get #PJ_SO_SNDBUF constant */
Benny Prijono8ab968f2007-07-20 08:08:30 +0000241PJ_DECL(pj_uint16_t) pj_SO_SNDBUF(void);
Benny Prijono9033e312005-11-21 02:08:39 +0000242
243
Benny Prijono57dc48b2006-12-25 06:39:33 +0000244/*
Benny Prijono9033e312005-11-21 02:08:39 +0000245 * Flags to be specified in #pj_sock_recv, #pj_sock_send, etc.
246 */
Benny Prijono8ab968f2007-07-20 08:08:30 +0000247
Benny Prijonod51a37a2007-07-28 02:44:55 +0000248/** Out-of-band messages. @see pj_MSG_OOB() */
249extern const int PJ_MSG_OOB;
250
251/** Peek, don't remove from buffer. @see pj_MSG_PEEK() */
252extern const int PJ_MSG_PEEK;
253
254/** Don't route. @see pj_MSG_DONTROUTE() */
255extern const int PJ_MSG_DONTROUTE;
256
257
258/** Get #PJ_MSG_OOB constant */
Benny Prijono8ab968f2007-07-20 08:08:30 +0000259PJ_DECL(int) pj_MSG_OOB(void);
Benny Prijonod51a37a2007-07-28 02:44:55 +0000260
261/** Get #PJ_MSG_PEEK constant */
Benny Prijono8ab968f2007-07-20 08:08:30 +0000262PJ_DECL(int) pj_MSG_PEEK(void);
Benny Prijonod51a37a2007-07-28 02:44:55 +0000263
264/** Get #PJ_MSG_DONTROUTE constant */
Benny Prijono8ab968f2007-07-20 08:08:30 +0000265PJ_DECL(int) pj_MSG_DONTROUTE(void);
Benny Prijono9033e312005-11-21 02:08:39 +0000266
267
268/**
Benny Prijonod51a37a2007-07-28 02:44:55 +0000269 * Flag to be specified in #pj_sock_shutdown().
Benny Prijono9033e312005-11-21 02:08:39 +0000270 */
271typedef enum pj_socket_sd_type
272{
273 PJ_SD_RECEIVE = 0, /**< No more receive. */
274 PJ_SHUT_RD = 0, /**< Alias for SD_RECEIVE. */
275 PJ_SD_SEND = 1, /**< No more sending. */
276 PJ_SHUT_WR = 1, /**< Alias for SD_SEND. */
277 PJ_SD_BOTH = 2, /**< No more send and receive. */
Benny Prijono92ac4472006-07-22 13:42:56 +0000278 PJ_SHUT_RDWR = 2 /**< Alias for SD_BOTH. */
Benny Prijono9033e312005-11-21 02:08:39 +0000279} pj_socket_sd_type;
280
281
282
283/** Address to accept any incoming messages. */
284#define PJ_INADDR_ANY ((pj_uint32_t)0)
285
286/** Address indicating an error return */
287#define PJ_INADDR_NONE ((pj_uint32_t)0xffffffff)
288
289/** Address to send to all hosts. */
290#define PJ_INADDR_BROADCAST ((pj_uint32_t)0xffffffff)
291
292
293/**
294 * Maximum length specifiable by #pj_sock_listen().
295 * If the build system doesn't override this value, then the lowest
296 * denominator (five, in Win32 systems) will be used.
297 */
298#if !defined(PJ_SOMAXCONN)
299# define PJ_SOMAXCONN 5
300#endif
301
302
303/**
304 * Constant for invalid socket returned by #pj_sock_socket() and
305 * #pj_sock_accept().
306 */
307#define PJ_INVALID_SOCKET (-1)
308
Benny Prijonoab668ed2006-06-01 11:38:40 +0000309#undef s_addr
310
Benny Prijono9033e312005-11-21 02:08:39 +0000311/**
312 * This structure describes Internet address.
313 */
314typedef struct pj_in_addr
315{
316 pj_uint32_t s_addr; /**< The 32bit IP address. */
317} pj_in_addr;
318
319
320/**
Benny Prijonoc16c6e32007-11-18 14:53:47 +0000321 * Maximum length of text representation of an IPv4 address.
322 */
323#define PJ_INET_ADDRSTRLEN 16
324
325/**
326 * Maximum length of text representation of an IPv6 address.
327 */
328#define PJ_INET6_ADDRSTRLEN 46
329
330
331/**
Benny Prijono9033e312005-11-21 02:08:39 +0000332 * This structure describes Internet socket address.
Benny Prijonoe67d99a2006-03-20 12:39:24 +0000333 * If PJ_SOCKADDR_HAS_LEN is not zero, then sin_zero_len member is added
334 * to this struct. As far the application is concerned, the value of
335 * this member will always be zero. Internally, PJLIB may modify the value
336 * before calling OS socket API, and reset the value back to zero before
337 * returning the struct to application.
Benny Prijono9033e312005-11-21 02:08:39 +0000338 */
Benny Prijono0ca04b62005-12-30 23:50:15 +0000339struct pj_sockaddr_in
Benny Prijono9033e312005-11-21 02:08:39 +0000340{
Benny Prijonoe67d99a2006-03-20 12:39:24 +0000341#if defined(PJ_SOCKADDR_HAS_LEN) && PJ_SOCKADDR_HAS_LEN!=0
342 pj_uint8_t sin_zero_len; /**< Just ignore this. */
343 pj_uint8_t sin_family; /**< Address family. */
344#else
Benny Prijono9033e312005-11-21 02:08:39 +0000345 pj_uint16_t sin_family; /**< Address family. */
Benny Prijonoe67d99a2006-03-20 12:39:24 +0000346#endif
Benny Prijono9033e312005-11-21 02:08:39 +0000347 pj_uint16_t sin_port; /**< Transport layer port number. */
348 pj_in_addr sin_addr; /**< IP address. */
349 char sin_zero[8]; /**< Padding. */
Benny Prijono0ca04b62005-12-30 23:50:15 +0000350};
Benny Prijono9033e312005-11-21 02:08:39 +0000351
Benny Prijonoc16c6e32007-11-18 14:53:47 +0000352
Benny Prijonob681a2f2007-03-25 18:44:51 +0000353#undef s6_addr
Benny Prijono9033e312005-11-21 02:08:39 +0000354
355/**
356 * This structure describes IPv6 address.
357 */
Benny Prijonoc16c6e32007-11-18 14:53:47 +0000358typedef union pj_in6_addr
Benny Prijono9033e312005-11-21 02:08:39 +0000359{
Benny Prijonoc16c6e32007-11-18 14:53:47 +0000360 /* This is the main entry */
361 pj_uint8_t s6_addr[16]; /**< 8-bit array */
362
363 /* While these are used for proper alignment */
364 pj_uint32_t u6_addr32[4];
365#if defined(PJ_HAS_INT64) && PJ_HAS_INT64!=0
366 pj_int64_t u6_addr64[2];
367#endif
368
Benny Prijono9033e312005-11-21 02:08:39 +0000369} pj_in6_addr;
370
Benny Prijonoc16c6e32007-11-18 14:53:47 +0000371
Benny Prijono9033e312005-11-21 02:08:39 +0000372/** Initializer value for pj_in6_addr. */
373#define PJ_IN6ADDR_ANY_INIT { { { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 } } }
374
375/** Initializer value for pj_in6_addr. */
376#define PJ_IN6ADDR_LOOPBACK_INIT { { { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1 } } }
377
378/**
379 * This structure describes IPv6 socket address.
Benny Prijonoe67d99a2006-03-20 12:39:24 +0000380 * If PJ_SOCKADDR_HAS_LEN is not zero, then sin_zero_len member is added
381 * to this struct. As far the application is concerned, the value of
382 * this member will always be zero. Internally, PJLIB may modify the value
383 * before calling OS socket API, and reset the value back to zero before
384 * returning the struct to application.
Benny Prijono9033e312005-11-21 02:08:39 +0000385 */
386typedef struct pj_sockaddr_in6
387{
Benny Prijonoe67d99a2006-03-20 12:39:24 +0000388#if defined(PJ_SOCKADDR_HAS_LEN) && PJ_SOCKADDR_HAS_LEN!=0
Benny Prijonoc16c6e32007-11-18 14:53:47 +0000389 pj_uint8_t sin6_zero_len; /**< Just ignore this. */
390 pj_uint8_t sin6_family; /**< Address family. */
Benny Prijonoe67d99a2006-03-20 12:39:24 +0000391#else
Benny Prijono9033e312005-11-21 02:08:39 +0000392 pj_uint16_t sin6_family; /**< Address family */
Benny Prijonoe67d99a2006-03-20 12:39:24 +0000393#endif
Benny Prijono9033e312005-11-21 02:08:39 +0000394 pj_uint16_t sin6_port; /**< Transport layer port number. */
395 pj_uint32_t sin6_flowinfo; /**< IPv6 flow information */
396 pj_in6_addr sin6_addr; /**< IPv6 address. */
Benny Prijonoc16c6e32007-11-18 14:53:47 +0000397 pj_uint32_t sin6_scope_id; /**< Set of interfaces for a scope */
Benny Prijono9033e312005-11-21 02:08:39 +0000398} pj_sockaddr_in6;
399
400
Benny Prijonob01897b2007-03-18 17:39:27 +0000401/**
402 * This structure describes common attributes found in transport addresses.
403 * If PJ_SOCKADDR_HAS_LEN is not zero, then sa_zero_len member is added
404 * to this struct. As far the application is concerned, the value of
405 * this member will always be zero. Internally, PJLIB may modify the value
406 * before calling OS socket API, and reset the value back to zero before
407 * returning the struct to application.
408 */
409typedef struct pj_addr_hdr
410{
411#if defined(PJ_SOCKADDR_HAS_LEN) && PJ_SOCKADDR_HAS_LEN!=0
412 pj_uint8_t sa_zero_len;
413 pj_uint8_t sa_family;
414#else
415 pj_uint16_t sa_family; /**< Common data: address family. */
416#endif
417} pj_addr_hdr;
418
419
420/**
421 * This union describes a generic socket address.
422 */
423typedef union pj_sockaddr
424{
425 pj_addr_hdr addr; /**< Generic transport address. */
426 pj_sockaddr_in ipv4; /**< IPv4 transport address. */
427 pj_sockaddr_in6 ipv6; /**< IPv6 transport address. */
428} pj_sockaddr;
429
430
Benny Prijono9033e312005-11-21 02:08:39 +0000431/*****************************************************************************
432 *
433 * SOCKET ADDRESS MANIPULATION.
434 *
435 *****************************************************************************
436 */
437
438/**
439 * Convert 16-bit value from network byte order to host byte order.
440 *
441 * @param netshort 16-bit network value.
442 * @return 16-bit host value.
443 */
444PJ_DECL(pj_uint16_t) pj_ntohs(pj_uint16_t netshort);
445
446/**
447 * Convert 16-bit value from host byte order to network byte order.
448 *
449 * @param hostshort 16-bit host value.
450 * @return 16-bit network value.
451 */
452PJ_DECL(pj_uint16_t) pj_htons(pj_uint16_t hostshort);
453
454/**
455 * Convert 32-bit value from network byte order to host byte order.
456 *
457 * @param netlong 32-bit network value.
458 * @return 32-bit host value.
459 */
460PJ_DECL(pj_uint32_t) pj_ntohl(pj_uint32_t netlong);
461
462/**
463 * Convert 32-bit value from host byte order to network byte order.
464 *
465 * @param hostlong 32-bit host value.
466 * @return 32-bit network value.
467 */
468PJ_DECL(pj_uint32_t) pj_htonl(pj_uint32_t hostlong);
469
470/**
471 * Convert an Internet host address given in network byte order
472 * to string in standard numbers and dots notation.
473 *
474 * @param inaddr The host address.
475 * @return The string address.
476 */
477PJ_DECL(char*) pj_inet_ntoa(pj_in_addr inaddr);
478
479/**
480 * This function converts the Internet host address cp from the standard
481 * numbers-and-dots notation into binary data and stores it in the structure
482 * that inp points to.
483 *
484 * @param cp IP address in standard numbers-and-dots notation.
485 * @param inp Structure that holds the output of the conversion.
486 *
487 * @return nonzero if the address is valid, zero if not.
488 */
489PJ_DECL(int) pj_inet_aton(const pj_str_t *cp, struct pj_in_addr *inp);
490
491/**
Benny Prijonoc16c6e32007-11-18 14:53:47 +0000492 * This function converts an address in its standard text presentation form
493 * into its numeric binary form. It supports both IPv4 and IPv6 address
494 * conversion.
495 *
496 * @param af Specify the family of the address. The PJ_AF_INET and
497 * PJ_AF_INET6 address families shall be supported.
498 * @param src Points to the string being passed in.
499 * @param dst Points to a buffer into which the function stores the
500 * numeric address; this shall be large enough to hold the
501 * numeric address (32 bits for PJ_AF_INET, 128 bits for
502 * PJ_AF_INET6).
503 *
504 * @return PJ_SUCCESS if conversion was successful.
505 */
506PJ_DECL(pj_status_t) pj_inet_pton(int af, const pj_str_t *src, void *dst);
507
508/**
509 * This function converts a numeric address into a text string suitable
510 * for presentation. It supports both IPv4 and IPv6 address
511 * conversion.
512 *
513 * @param af Specify the family of the address. This can be PJ_AF_INET
514 * or PJ_AF_INET6.
515 * @param src Points to a buffer holding an IPv4 address if the af argument
516 * is PJ_AF_INET, or an IPv6 address if the af argument is
517 * PJ_AF_INET6; the address must be in network byte order.
518 * @param dst Points to a buffer where the function stores the resulting
519 * text string; it shall not be NULL.
520 * @param size Specifies the size of this buffer, which shall be large
521 * enough to hold the text string (PJ_INET_ADDRSTRLEN characters
522 * for IPv4, PJ_INET6_ADDRSTRLEN characters for IPv6).
523 *
524 * @return PJ_SUCCESS if conversion was successful..
525 */
526PJ_DECL(pj_status_t) pj_inet_ntop(int af, const void *src,
527 char *dst, int size);
528
529/**
Benny Prijono9033e312005-11-21 02:08:39 +0000530 * Convert address string with numbers and dots to binary IP address.
531 *
532 * @param cp The IP address in numbers and dots notation.
533 * @return If success, the IP address is returned in network
534 * byte order. If failed, PJ_INADDR_NONE will be
535 * returned.
536 * @remark
537 * This is an obsolete interface to #pj_inet_aton(); it is obsolete
538 * because -1 is a valid address (255.255.255.255), and #pj_inet_aton()
539 * provides a cleaner way to indicate error return.
540 */
541PJ_DECL(pj_in_addr) pj_inet_addr(const pj_str_t *cp);
542
Benny Prijono7a4cf152006-03-22 11:48:33 +0000543/**
544 * Convert address string with numbers and dots to binary IP address.
545 *
546 * @param cp The IP address in numbers and dots notation.
547 * @return If success, the IP address is returned in network
548 * byte order. If failed, PJ_INADDR_NONE will be
549 * returned.
550 * @remark
551 * This is an obsolete interface to #pj_inet_aton(); it is obsolete
552 * because -1 is a valid address (255.255.255.255), and #pj_inet_aton()
553 * provides a cleaner way to indicate error return.
554 */
555PJ_DECL(pj_in_addr) pj_inet_addr2(const char *cp);
Benny Prijono9033e312005-11-21 02:08:39 +0000556
557/**
558 * Get the transport layer port number of an Internet socket address.
559 * The port is returned in host byte order.
560 *
561 * @param addr The IP socket address.
562 * @return Port number, in host byte order.
563 */
564PJ_INLINE(pj_uint16_t) pj_sockaddr_in_get_port(const pj_sockaddr_in *addr)
565{
566 return pj_ntohs(addr->sin_port);
567}
568
569/**
570 * Set the port number of an Internet socket address.
571 *
572 * @param addr The IP socket address.
573 * @param hostport The port number, in host byte order.
574 */
575PJ_INLINE(void) pj_sockaddr_in_set_port(pj_sockaddr_in *addr,
576 pj_uint16_t hostport)
577{
578 addr->sin_port = pj_htons(hostport);
579}
580
581/**
582 * Get the IP address of an Internet socket address.
583 * The address is returned as 32bit value in host byte order.
584 *
585 * @param addr The IP socket address.
586 * @return 32bit address, in host byte order.
587 */
588PJ_INLINE(pj_in_addr) pj_sockaddr_in_get_addr(const pj_sockaddr_in *addr)
589{
590 pj_in_addr in_addr;
591 in_addr.s_addr = pj_ntohl(addr->sin_addr.s_addr);
592 return in_addr;
Benny Prijono92ac4472006-07-22 13:42:56 +0000593}
Benny Prijono9033e312005-11-21 02:08:39 +0000594
595/**
596 * Set the IP address of an Internet socket address.
597 *
598 * @param addr The IP socket address.
599 * @param hostaddr The host address, in host byte order.
600 */
601PJ_INLINE(void) pj_sockaddr_in_set_addr(pj_sockaddr_in *addr,
602 pj_uint32_t hostaddr)
603{
604 addr->sin_addr.s_addr = pj_htonl(hostaddr);
605}
606
607/**
608 * Set the IP address of an IP socket address from string address,
609 * with resolving the host if necessary. The string address may be in a
610 * standard numbers and dots notation or may be a hostname. If hostname
611 * is specified, then the function will resolve the host into the IP
612 * address.
613 *
614 * @param addr The IP socket address to be set.
615 * @param cp The address string, which can be in a standard
616 * dotted numbers or a hostname to be resolved.
617 *
618 * @return Zero on success.
619 */
620PJ_DECL(pj_status_t) pj_sockaddr_in_set_str_addr( pj_sockaddr_in *addr,
621 const pj_str_t *cp);
622
623/**
624 * Set the IP address and port of an IP socket address.
625 * The string address may be in a standard numbers and dots notation or
626 * may be a hostname. If hostname is specified, then the function will
627 * resolve the host into the IP address.
628 *
629 * @param addr The IP socket address to be set.
630 * @param cp The address string, which can be in a standard
631 * dotted numbers or a hostname to be resolved.
632 * @param port The port number, in host byte order.
633 *
634 * @return Zero on success.
635 */
636PJ_DECL(pj_status_t) pj_sockaddr_in_init( pj_sockaddr_in *addr,
637 const pj_str_t *cp,
638 pj_uint16_t port);
639
640
641/*****************************************************************************
642 *
643 * HOST NAME AND ADDRESS.
644 *
645 *****************************************************************************
646 */
647
648/**
649 * Get system's host name.
650 *
651 * @return The hostname, or empty string if the hostname can not
652 * be identified.
653 */
654PJ_DECL(const pj_str_t*) pj_gethostname(void);
655
656/**
657 * Get host's IP address, which the the first IP address that is resolved
658 * from the hostname.
659 *
660 * @return The host's IP address, PJ_INADDR_NONE if the host
661 * IP address can not be identified.
662 */
663PJ_DECL(pj_in_addr) pj_gethostaddr(void);
664
665
666/*****************************************************************************
667 *
668 * SOCKET API.
669 *
670 *****************************************************************************
671 */
672
673/**
674 * Create new socket/endpoint for communication.
675 *
676 * @param family Specifies a communication domain; this selects the
677 * protocol family which will be used for communication.
678 * @param type The socket has the indicated type, which specifies the
679 * communication semantics.
680 * @param protocol Specifies a particular protocol to be used with the
681 * socket. Normally only a single protocol exists to support
682 * a particular socket type within a given protocol family,
683 * in which a case protocol can be specified as 0.
684 * @param sock New socket descriptor, or PJ_INVALID_SOCKET on error.
685 *
686 * @return Zero on success.
687 */
688PJ_DECL(pj_status_t) pj_sock_socket(int family,
689 int type,
690 int protocol,
691 pj_sock_t *sock);
692
693/**
694 * Close the socket descriptor.
695 *
696 * @param sockfd The socket descriptor.
697 *
698 * @return Zero on success.
699 */
700PJ_DECL(pj_status_t) pj_sock_close(pj_sock_t sockfd);
701
702
703/**
704 * This function gives the socket sockfd the local address my_addr. my_addr is
705 * addrlen bytes long. Traditionally, this is called assigning a name to
706 * a socket. When a socket is created with #pj_sock_socket(), it exists in a
707 * name space (address family) but has no name assigned.
708 *
709 * @param sockfd The socket desriptor.
710 * @param my_addr The local address to bind the socket to.
711 * @param addrlen The length of the address.
712 *
713 * @return Zero on success.
714 */
715PJ_DECL(pj_status_t) pj_sock_bind( pj_sock_t sockfd,
716 const pj_sockaddr_t *my_addr,
717 int addrlen);
718
719/**
720 * Bind the IP socket sockfd to the given address and port.
721 *
722 * @param sockfd The socket descriptor.
723 * @param addr Local address to bind the socket to, in host byte order.
724 * @param port The local port to bind the socket to, in host byte order.
725 *
726 * @return Zero on success.
727 */
728PJ_DECL(pj_status_t) pj_sock_bind_in( pj_sock_t sockfd,
729 pj_uint32_t addr,
730 pj_uint16_t port);
731
732#if PJ_HAS_TCP
733/**
734 * Listen for incoming connection. This function only applies to connection
735 * oriented sockets (such as PJ_SOCK_STREAM or PJ_SOCK_SEQPACKET), and it
736 * indicates the willingness to accept incoming connections.
737 *
738 * @param sockfd The socket descriptor.
739 * @param backlog Defines the maximum length the queue of pending
740 * connections may grow to.
741 *
742 * @return Zero on success.
743 */
744PJ_DECL(pj_status_t) pj_sock_listen( pj_sock_t sockfd,
745 int backlog );
746
747/**
748 * Accept new connection on the specified connection oriented server socket.
749 *
750 * @param serverfd The server socket.
751 * @param newsock New socket on success, of PJ_INVALID_SOCKET if failed.
752 * @param addr A pointer to sockaddr type. If the argument is not NULL,
753 * it will be filled by the address of connecting entity.
754 * @param addrlen Initially specifies the length of the address, and upon
755 * return will be filled with the exact address length.
756 *
757 * @return Zero on success, or the error number.
758 */
759PJ_DECL(pj_status_t) pj_sock_accept( pj_sock_t serverfd,
760 pj_sock_t *newsock,
761 pj_sockaddr_t *addr,
762 int *addrlen);
763#endif
764
765/**
766 * The file descriptor sockfd must refer to a socket. If the socket is of
767 * type PJ_SOCK_DGRAM then the serv_addr address is the address to which
768 * datagrams are sent by default, and the only address from which datagrams
769 * are received. If the socket is of type PJ_SOCK_STREAM or PJ_SOCK_SEQPACKET,
770 * this call attempts to make a connection to another socket. The
771 * other socket is specified by serv_addr, which is an address (of length
772 * addrlen) in the communications space of the socket. Each communications
773 * space interprets the serv_addr parameter in its own way.
774 *
775 * @param sockfd The socket descriptor.
776 * @param serv_addr Server address to connect to.
777 * @param addrlen The length of server address.
778 *
779 * @return Zero on success.
780 */
781PJ_DECL(pj_status_t) pj_sock_connect( pj_sock_t sockfd,
782 const pj_sockaddr_t *serv_addr,
783 int addrlen);
784
785/**
786 * Return the address of peer which is connected to socket sockfd.
787 *
788 * @param sockfd The socket descriptor.
789 * @param addr Pointer to sockaddr structure to which the address
790 * will be returned.
791 * @param namelen Initially the length of the addr. Upon return the value
792 * will be set to the actual length of the address.
793 *
794 * @return Zero on success.
795 */
796PJ_DECL(pj_status_t) pj_sock_getpeername(pj_sock_t sockfd,
797 pj_sockaddr_t *addr,
798 int *namelen);
799
800/**
801 * Return the current name of the specified socket.
802 *
803 * @param sockfd The socket descriptor.
804 * @param addr Pointer to sockaddr structure to which the address
805 * will be returned.
806 * @param namelen Initially the length of the addr. Upon return the value
807 * will be set to the actual length of the address.
808 *
809 * @return Zero on success.
810 */
811PJ_DECL(pj_status_t) pj_sock_getsockname( pj_sock_t sockfd,
812 pj_sockaddr_t *addr,
813 int *namelen);
814
815/**
816 * Get socket option associated with a socket. Options may exist at multiple
817 * protocol levels; they are always present at the uppermost socket level.
818 *
819 * @param sockfd The socket descriptor.
820 * @param level The level which to get the option from.
821 * @param optname The option name.
822 * @param optval Identifies the buffer which the value will be
823 * returned.
824 * @param optlen Initially contains the length of the buffer, upon
825 * return will be set to the actual size of the value.
826 *
827 * @return Zero on success.
828 */
829PJ_DECL(pj_status_t) pj_sock_getsockopt( pj_sock_t sockfd,
830 pj_uint16_t level,
831 pj_uint16_t optname,
832 void *optval,
833 int *optlen);
834/**
835 * Manipulate the options associated with a socket. Options may exist at
836 * multiple protocol levels; they are always present at the uppermost socket
837 * level.
838 *
839 * @param sockfd The socket descriptor.
840 * @param level The level which to get the option from.
841 * @param optname The option name.
842 * @param optval Identifies the buffer which contain the value.
843 * @param optlen The length of the value.
844 *
845 * @return PJ_SUCCESS or the status code.
846 */
847PJ_DECL(pj_status_t) pj_sock_setsockopt( pj_sock_t sockfd,
848 pj_uint16_t level,
849 pj_uint16_t optname,
850 const void *optval,
851 int optlen);
852
853
854/**
855 * Receives data stream or message coming to the specified socket.
856 *
857 * @param sockfd The socket descriptor.
858 * @param buf The buffer to receive the data or message.
859 * @param len On input, the length of the buffer. On return,
860 * contains the length of data received.
Benny Prijonod51a37a2007-07-28 02:44:55 +0000861 * @param flags Flags (such as pj_MSG_PEEK()).
Benny Prijono9033e312005-11-21 02:08:39 +0000862 *
863 * @return PJ_SUCCESS or the error code.
864 */
865PJ_DECL(pj_status_t) pj_sock_recv(pj_sock_t sockfd,
866 void *buf,
867 pj_ssize_t *len,
868 unsigned flags);
869
870/**
871 * Receives data stream or message coming to the specified socket.
872 *
873 * @param sockfd The socket descriptor.
874 * @param buf The buffer to receive the data or message.
875 * @param len On input, the length of the buffer. On return,
876 * contains the length of data received.
Benny Prijonod51a37a2007-07-28 02:44:55 +0000877 * @param flags Flags (such as pj_MSG_PEEK()).
Benny Prijono9033e312005-11-21 02:08:39 +0000878 * @param from If not NULL, it will be filled with the source
879 * address of the connection.
880 * @param fromlen Initially contains the length of from address,
881 * and upon return will be filled with the actual
882 * length of the address.
883 *
884 * @return PJ_SUCCESS or the error code.
885 */
886PJ_DECL(pj_status_t) pj_sock_recvfrom( pj_sock_t sockfd,
887 void *buf,
888 pj_ssize_t *len,
889 unsigned flags,
890 pj_sockaddr_t *from,
891 int *fromlen);
892
893/**
894 * Transmit data to the socket.
895 *
896 * @param sockfd Socket descriptor.
897 * @param buf Buffer containing data to be sent.
898 * @param len On input, the length of the data in the buffer.
899 * Upon return, it will be filled with the length
900 * of data sent.
Benny Prijonod51a37a2007-07-28 02:44:55 +0000901 * @param flags Flags (such as pj_MSG_DONTROUTE()).
Benny Prijono9033e312005-11-21 02:08:39 +0000902 *
903 * @return PJ_SUCCESS or the status code.
904 */
905PJ_DECL(pj_status_t) pj_sock_send(pj_sock_t sockfd,
906 const void *buf,
907 pj_ssize_t *len,
908 unsigned flags);
909
910/**
911 * Transmit data to the socket to the specified address.
912 *
913 * @param sockfd Socket descriptor.
914 * @param buf Buffer containing data to be sent.
915 * @param len On input, the length of the data in the buffer.
916 * Upon return, it will be filled with the length
917 * of data sent.
Benny Prijonod51a37a2007-07-28 02:44:55 +0000918 * @param flags Flags (such as pj_MSG_DONTROUTE()).
Benny Prijono9033e312005-11-21 02:08:39 +0000919 * @param to The address to send.
920 * @param tolen The length of the address in bytes.
921 *
Benny Prijonoac9d1422006-01-18 23:32:27 +0000922 * @return PJ_SUCCESS or the status code.
Benny Prijono9033e312005-11-21 02:08:39 +0000923 */
924PJ_DECL(pj_status_t) pj_sock_sendto(pj_sock_t sockfd,
925 const void *buf,
926 pj_ssize_t *len,
927 unsigned flags,
928 const pj_sockaddr_t *to,
929 int tolen);
930
931#if PJ_HAS_TCP
932/**
933 * The shutdown call causes all or part of a full-duplex connection on the
934 * socket associated with sockfd to be shut down.
935 *
936 * @param sockfd The socket descriptor.
937 * @param how If how is PJ_SHUT_RD, further receptions will be
938 * disallowed. If how is PJ_SHUT_WR, further transmissions
939 * will be disallowed. If how is PJ_SHUT_RDWR, further
940 * receptions andtransmissions will be disallowed.
941 *
942 * @return Zero on success.
943 */
944PJ_DECL(pj_status_t) pj_sock_shutdown( pj_sock_t sockfd,
945 int how);
946#endif
947
948/**
949 * @}
950 */
951
952
953PJ_END_DECL
954
955#endif /* __PJ_SOCK_H__ */
956