blob: 0be0258923e0fa6a20301896b037a4c735595e02 [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 */
62extern const pj_uint16_t PJ_AF_UNIX; /**< Unix domain socket. */
63#define PJ_AF_LOCAL PJ_AF_UNIX; /**< POSIX name for AF_UNIX */
64extern const pj_uint16_t PJ_AF_INET; /**< Internet IP protocol. */
65extern const pj_uint16_t PJ_AF_INET6; /**< IP version 6. */
66extern const pj_uint16_t PJ_AF_PACKET; /**< Packet family. */
67extern const pj_uint16_t PJ_AF_IRDA; /**< IRDA sockets. */
68
69
70/**
71 * Supported types of sockets.
72 * APPLICATION MUST USE THESE VALUES INSTEAD OF NORMAL SOCK_*, BECAUSE
73 * THE LIBRARY WILL TRANSLATE THE VALUE TO THE NATIVE VALUE.
74 */
75
76extern const pj_uint16_t PJ_SOCK_STREAM; /**< Sequenced, reliable, connection-
77 based byte streams. */
78extern const pj_uint16_t PJ_SOCK_DGRAM; /**< Connectionless, unreliable
79 datagrams of fixed maximum
80 lengths. */
81extern const pj_uint16_t PJ_SOCK_RAW; /**< Raw protocol interface. */
82extern const pj_uint16_t PJ_SOCK_RDM; /**< Reliably-delivered messages. */
83
84
85/**
86 * Socket level specified in #pj_sock_setsockopt() or #pj_sock_getsockopt().
87 * APPLICATION MUST USE THESE VALUES INSTEAD OF NORMAL SOL_*, BECAUSE
88 * THE LIBRARY WILL TRANSLATE THE VALUE TO THE NATIVE VALUE.
89 */
90extern const pj_uint16_t PJ_SOL_SOCKET; /**< Socket level. */
91extern const pj_uint16_t PJ_SOL_IP; /**< IP level. */
92extern const pj_uint16_t PJ_SOL_TCP; /**< TCP level. */
93extern const pj_uint16_t PJ_SOL_UDP; /**< UDP level. */
94extern const pj_uint16_t PJ_SOL_IPV6; /**< IP version 6 */
95
Benny Prijono87a00892007-02-01 00:33:12 +000096
97/* IP_TOS
98 *
99 * Note:
100 * TOS CURRENTLY DOES NOT WORK IN Windows 2000 and above!
101 * See http://support.microsoft.com/kb/248611
102 */
103extern const pj_uint16_t PJ_IP_TOS; /**< IP_TOS optname in setsockopt() */
104
105
106/*
107 * IP TOS related constats.
108 *
109 * Note:
110 * TOS CURRENTLY DOES NOT WORK IN Windows 2000 and above!
111 * See http://support.microsoft.com/kb/248611
112 */
113extern const pj_uint16_t PJ_IPTOS_LOWDELAY; /**< Minimize delays */
114extern const pj_uint16_t PJ_IPTOS_THROUGHPUT; /**< Optimize throughput */
115extern const pj_uint16_t PJ_IPTOS_RELIABILITY; /**< Optimize for reliability*/
116extern const pj_uint16_t PJ_IPTOS_MINCOST; /**< "filler data" where slow
117 transmission does't matter */
118
119
Benny Prijono9033e312005-11-21 02:08:39 +0000120/**
121 * Values to be specified as \c optname when calling #pj_sock_setsockopt()
122 * or #pj_sock_getsockopt().
123 */
124extern const pj_uint16_t PJ_SO_TYPE; /**< Socket type. */
125extern const pj_uint16_t PJ_SO_RCVBUF; /**< Buffer size for receive. */
126extern const pj_uint16_t PJ_SO_SNDBUF; /**< Buffer size for send. */
127
128
Benny Prijono57dc48b2006-12-25 06:39:33 +0000129/*
Benny Prijono9033e312005-11-21 02:08:39 +0000130 * Flags to be specified in #pj_sock_recv, #pj_sock_send, etc.
131 */
Benny Prijono57dc48b2006-12-25 06:39:33 +0000132extern const int PJ_MSG_OOB; /**< Out-of-band messages. */
133extern const int PJ_MSG_PEEK; /**< Peek, don't remove from buffer. */
134extern const int PJ_MSG_DONTROUTE; /**< Don't route. */
Benny Prijono9033e312005-11-21 02:08:39 +0000135
136
137/**
138 * Flag to be specified in #pj_sock_shutdown.
139 */
140typedef enum pj_socket_sd_type
141{
142 PJ_SD_RECEIVE = 0, /**< No more receive. */
143 PJ_SHUT_RD = 0, /**< Alias for SD_RECEIVE. */
144 PJ_SD_SEND = 1, /**< No more sending. */
145 PJ_SHUT_WR = 1, /**< Alias for SD_SEND. */
146 PJ_SD_BOTH = 2, /**< No more send and receive. */
Benny Prijono92ac4472006-07-22 13:42:56 +0000147 PJ_SHUT_RDWR = 2 /**< Alias for SD_BOTH. */
Benny Prijono9033e312005-11-21 02:08:39 +0000148} pj_socket_sd_type;
149
150
151
152/** Address to accept any incoming messages. */
153#define PJ_INADDR_ANY ((pj_uint32_t)0)
154
155/** Address indicating an error return */
156#define PJ_INADDR_NONE ((pj_uint32_t)0xffffffff)
157
158/** Address to send to all hosts. */
159#define PJ_INADDR_BROADCAST ((pj_uint32_t)0xffffffff)
160
161
162/**
163 * Maximum length specifiable by #pj_sock_listen().
164 * If the build system doesn't override this value, then the lowest
165 * denominator (five, in Win32 systems) will be used.
166 */
167#if !defined(PJ_SOMAXCONN)
168# define PJ_SOMAXCONN 5
169#endif
170
171
172/**
173 * Constant for invalid socket returned by #pj_sock_socket() and
174 * #pj_sock_accept().
175 */
176#define PJ_INVALID_SOCKET (-1)
177
Benny Prijonoab668ed2006-06-01 11:38:40 +0000178#undef s_addr
179
Benny Prijono9033e312005-11-21 02:08:39 +0000180/**
181 * This structure describes Internet address.
182 */
183typedef struct pj_in_addr
184{
185 pj_uint32_t s_addr; /**< The 32bit IP address. */
186} pj_in_addr;
187
188
189/**
190 * This structure describes Internet socket address.
Benny Prijonoe67d99a2006-03-20 12:39:24 +0000191 * If PJ_SOCKADDR_HAS_LEN is not zero, then sin_zero_len member is added
192 * to this struct. As far the application is concerned, the value of
193 * this member will always be zero. Internally, PJLIB may modify the value
194 * before calling OS socket API, and reset the value back to zero before
195 * returning the struct to application.
Benny Prijono9033e312005-11-21 02:08:39 +0000196 */
Benny Prijono0ca04b62005-12-30 23:50:15 +0000197struct pj_sockaddr_in
Benny Prijono9033e312005-11-21 02:08:39 +0000198{
Benny Prijonoe67d99a2006-03-20 12:39:24 +0000199#if defined(PJ_SOCKADDR_HAS_LEN) && PJ_SOCKADDR_HAS_LEN!=0
200 pj_uint8_t sin_zero_len; /**< Just ignore this. */
201 pj_uint8_t sin_family; /**< Address family. */
202#else
Benny Prijono9033e312005-11-21 02:08:39 +0000203 pj_uint16_t sin_family; /**< Address family. */
Benny Prijonoe67d99a2006-03-20 12:39:24 +0000204#endif
Benny Prijono9033e312005-11-21 02:08:39 +0000205 pj_uint16_t sin_port; /**< Transport layer port number. */
206 pj_in_addr sin_addr; /**< IP address. */
207 char sin_zero[8]; /**< Padding. */
Benny Prijono0ca04b62005-12-30 23:50:15 +0000208};
Benny Prijono9033e312005-11-21 02:08:39 +0000209
Benny Prijonob681a2f2007-03-25 18:44:51 +0000210#undef s6_addr
Benny Prijono9033e312005-11-21 02:08:39 +0000211
212/**
213 * This structure describes IPv6 address.
214 */
215typedef struct pj_in6_addr
216{
217 /** Union of address formats. */
218 union {
219 pj_uint8_t u6_addr8[16]; /**< u6_addr8 */
220 pj_uint16_t u6_addr16[8]; /**< u6_addr16 */
221 pj_uint32_t u6_addr32[4]; /**< u6_addr32 */
222 } in6_u;
223/** Shortcut to access in6_u.u6_addr8. */
224#define s6_addr in6_u.u6_addr8
225/** Shortcut to access in6_u.u6_addr16. */
226#define s6_addr16 in6_u.u6_addr16
227/** Shortcut to access in6_u.u6_addr32. */
228#define s6_addr32 in6_u.u6_addr32
229} pj_in6_addr;
230
231/** Initializer value for pj_in6_addr. */
232#define PJ_IN6ADDR_ANY_INIT { { { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 } } }
233
234/** Initializer value for pj_in6_addr. */
235#define PJ_IN6ADDR_LOOPBACK_INIT { { { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1 } } }
236
237/**
238 * This structure describes IPv6 socket address.
Benny Prijonoe67d99a2006-03-20 12:39:24 +0000239 * If PJ_SOCKADDR_HAS_LEN is not zero, then sin_zero_len member is added
240 * to this struct. As far the application is concerned, the value of
241 * this member will always be zero. Internally, PJLIB may modify the value
242 * before calling OS socket API, and reset the value back to zero before
243 * returning the struct to application.
Benny Prijono9033e312005-11-21 02:08:39 +0000244 */
245typedef struct pj_sockaddr_in6
246{
Benny Prijonoe67d99a2006-03-20 12:39:24 +0000247#if defined(PJ_SOCKADDR_HAS_LEN) && PJ_SOCKADDR_HAS_LEN!=0
248 pj_uint8_t sin_zero_len; /**< Just ignore this. */
249 pj_uint8_t sin_family; /**< Address family. */
250#else
Benny Prijono9033e312005-11-21 02:08:39 +0000251 pj_uint16_t sin6_family; /**< Address family */
Benny Prijonoe67d99a2006-03-20 12:39:24 +0000252#endif
Benny Prijono9033e312005-11-21 02:08:39 +0000253 pj_uint16_t sin6_port; /**< Transport layer port number. */
254 pj_uint32_t sin6_flowinfo; /**< IPv6 flow information */
255 pj_in6_addr sin6_addr; /**< IPv6 address. */
256 pj_uint32_t sin6_scope_id; /**< IPv6 scope-id */
257} pj_sockaddr_in6;
258
259
Benny Prijonob01897b2007-03-18 17:39:27 +0000260/**
261 * This structure describes common attributes found in transport addresses.
262 * If PJ_SOCKADDR_HAS_LEN is not zero, then sa_zero_len member is added
263 * to this struct. As far the application is concerned, the value of
264 * this member will always be zero. Internally, PJLIB may modify the value
265 * before calling OS socket API, and reset the value back to zero before
266 * returning the struct to application.
267 */
268typedef struct pj_addr_hdr
269{
270#if defined(PJ_SOCKADDR_HAS_LEN) && PJ_SOCKADDR_HAS_LEN!=0
271 pj_uint8_t sa_zero_len;
272 pj_uint8_t sa_family;
273#else
274 pj_uint16_t sa_family; /**< Common data: address family. */
275#endif
276} pj_addr_hdr;
277
278
279/**
280 * This union describes a generic socket address.
281 */
282typedef union pj_sockaddr
283{
284 pj_addr_hdr addr; /**< Generic transport address. */
285 pj_sockaddr_in ipv4; /**< IPv4 transport address. */
286 pj_sockaddr_in6 ipv6; /**< IPv6 transport address. */
287} pj_sockaddr;
288
289
Benny Prijono9033e312005-11-21 02:08:39 +0000290/*****************************************************************************
291 *
292 * SOCKET ADDRESS MANIPULATION.
293 *
294 *****************************************************************************
295 */
296
297/**
298 * Convert 16-bit value from network byte order to host byte order.
299 *
300 * @param netshort 16-bit network value.
301 * @return 16-bit host value.
302 */
303PJ_DECL(pj_uint16_t) pj_ntohs(pj_uint16_t netshort);
304
305/**
306 * Convert 16-bit value from host byte order to network byte order.
307 *
308 * @param hostshort 16-bit host value.
309 * @return 16-bit network value.
310 */
311PJ_DECL(pj_uint16_t) pj_htons(pj_uint16_t hostshort);
312
313/**
314 * Convert 32-bit value from network byte order to host byte order.
315 *
316 * @param netlong 32-bit network value.
317 * @return 32-bit host value.
318 */
319PJ_DECL(pj_uint32_t) pj_ntohl(pj_uint32_t netlong);
320
321/**
322 * Convert 32-bit value from host byte order to network byte order.
323 *
324 * @param hostlong 32-bit host value.
325 * @return 32-bit network value.
326 */
327PJ_DECL(pj_uint32_t) pj_htonl(pj_uint32_t hostlong);
328
329/**
330 * Convert an Internet host address given in network byte order
331 * to string in standard numbers and dots notation.
332 *
333 * @param inaddr The host address.
334 * @return The string address.
335 */
336PJ_DECL(char*) pj_inet_ntoa(pj_in_addr inaddr);
337
338/**
339 * This function converts the Internet host address cp from the standard
340 * numbers-and-dots notation into binary data and stores it in the structure
341 * that inp points to.
342 *
343 * @param cp IP address in standard numbers-and-dots notation.
344 * @param inp Structure that holds the output of the conversion.
345 *
346 * @return nonzero if the address is valid, zero if not.
347 */
348PJ_DECL(int) pj_inet_aton(const pj_str_t *cp, struct pj_in_addr *inp);
349
350/**
351 * Convert address string with numbers and dots to binary IP address.
352 *
353 * @param cp The IP address in numbers and dots notation.
354 * @return If success, the IP address is returned in network
355 * byte order. If failed, PJ_INADDR_NONE will be
356 * returned.
357 * @remark
358 * This is an obsolete interface to #pj_inet_aton(); it is obsolete
359 * because -1 is a valid address (255.255.255.255), and #pj_inet_aton()
360 * provides a cleaner way to indicate error return.
361 */
362PJ_DECL(pj_in_addr) pj_inet_addr(const pj_str_t *cp);
363
Benny Prijono7a4cf152006-03-22 11:48:33 +0000364/**
365 * Convert address string with numbers and dots to binary IP address.
366 *
367 * @param cp The IP address in numbers and dots notation.
368 * @return If success, the IP address is returned in network
369 * byte order. If failed, PJ_INADDR_NONE will be
370 * returned.
371 * @remark
372 * This is an obsolete interface to #pj_inet_aton(); it is obsolete
373 * because -1 is a valid address (255.255.255.255), and #pj_inet_aton()
374 * provides a cleaner way to indicate error return.
375 */
376PJ_DECL(pj_in_addr) pj_inet_addr2(const char *cp);
Benny Prijono9033e312005-11-21 02:08:39 +0000377
378/**
379 * Get the transport layer port number of an Internet socket address.
380 * The port is returned in host byte order.
381 *
382 * @param addr The IP socket address.
383 * @return Port number, in host byte order.
384 */
385PJ_INLINE(pj_uint16_t) pj_sockaddr_in_get_port(const pj_sockaddr_in *addr)
386{
387 return pj_ntohs(addr->sin_port);
388}
389
390/**
391 * Set the port number of an Internet socket address.
392 *
393 * @param addr The IP socket address.
394 * @param hostport The port number, in host byte order.
395 */
396PJ_INLINE(void) pj_sockaddr_in_set_port(pj_sockaddr_in *addr,
397 pj_uint16_t hostport)
398{
399 addr->sin_port = pj_htons(hostport);
400}
401
402/**
403 * Get the IP address of an Internet socket address.
404 * The address is returned as 32bit value in host byte order.
405 *
406 * @param addr The IP socket address.
407 * @return 32bit address, in host byte order.
408 */
409PJ_INLINE(pj_in_addr) pj_sockaddr_in_get_addr(const pj_sockaddr_in *addr)
410{
411 pj_in_addr in_addr;
412 in_addr.s_addr = pj_ntohl(addr->sin_addr.s_addr);
413 return in_addr;
Benny Prijono92ac4472006-07-22 13:42:56 +0000414}
Benny Prijono9033e312005-11-21 02:08:39 +0000415
416/**
417 * Set the IP address of an Internet socket address.
418 *
419 * @param addr The IP socket address.
420 * @param hostaddr The host address, in host byte order.
421 */
422PJ_INLINE(void) pj_sockaddr_in_set_addr(pj_sockaddr_in *addr,
423 pj_uint32_t hostaddr)
424{
425 addr->sin_addr.s_addr = pj_htonl(hostaddr);
426}
427
428/**
429 * Set the IP address of an IP socket address from string address,
430 * with resolving the host if necessary. The string address may be in a
431 * standard numbers and dots notation or may be a hostname. If hostname
432 * is specified, then the function will resolve the host into the IP
433 * address.
434 *
435 * @param addr The IP socket address to be set.
436 * @param cp The address string, which can be in a standard
437 * dotted numbers or a hostname to be resolved.
438 *
439 * @return Zero on success.
440 */
441PJ_DECL(pj_status_t) pj_sockaddr_in_set_str_addr( pj_sockaddr_in *addr,
442 const pj_str_t *cp);
443
444/**
445 * Set the IP address and port of an IP socket address.
446 * The string address may be in a standard numbers and dots notation or
447 * may be a hostname. If hostname is specified, then the function will
448 * resolve the host into the IP address.
449 *
450 * @param addr The IP socket address to be set.
451 * @param cp The address string, which can be in a standard
452 * dotted numbers or a hostname to be resolved.
453 * @param port The port number, in host byte order.
454 *
455 * @return Zero on success.
456 */
457PJ_DECL(pj_status_t) pj_sockaddr_in_init( pj_sockaddr_in *addr,
458 const pj_str_t *cp,
459 pj_uint16_t port);
460
461
462/*****************************************************************************
463 *
464 * HOST NAME AND ADDRESS.
465 *
466 *****************************************************************************
467 */
468
469/**
470 * Get system's host name.
471 *
472 * @return The hostname, or empty string if the hostname can not
473 * be identified.
474 */
475PJ_DECL(const pj_str_t*) pj_gethostname(void);
476
477/**
478 * Get host's IP address, which the the first IP address that is resolved
479 * from the hostname.
480 *
481 * @return The host's IP address, PJ_INADDR_NONE if the host
482 * IP address can not be identified.
483 */
484PJ_DECL(pj_in_addr) pj_gethostaddr(void);
485
486
487/*****************************************************************************
488 *
489 * SOCKET API.
490 *
491 *****************************************************************************
492 */
493
494/**
495 * Create new socket/endpoint for communication.
496 *
497 * @param family Specifies a communication domain; this selects the
498 * protocol family which will be used for communication.
499 * @param type The socket has the indicated type, which specifies the
500 * communication semantics.
501 * @param protocol Specifies a particular protocol to be used with the
502 * socket. Normally only a single protocol exists to support
503 * a particular socket type within a given protocol family,
504 * in which a case protocol can be specified as 0.
505 * @param sock New socket descriptor, or PJ_INVALID_SOCKET on error.
506 *
507 * @return Zero on success.
508 */
509PJ_DECL(pj_status_t) pj_sock_socket(int family,
510 int type,
511 int protocol,
512 pj_sock_t *sock);
513
514/**
515 * Close the socket descriptor.
516 *
517 * @param sockfd The socket descriptor.
518 *
519 * @return Zero on success.
520 */
521PJ_DECL(pj_status_t) pj_sock_close(pj_sock_t sockfd);
522
523
524/**
525 * This function gives the socket sockfd the local address my_addr. my_addr is
526 * addrlen bytes long. Traditionally, this is called assigning a name to
527 * a socket. When a socket is created with #pj_sock_socket(), it exists in a
528 * name space (address family) but has no name assigned.
529 *
530 * @param sockfd The socket desriptor.
531 * @param my_addr The local address to bind the socket to.
532 * @param addrlen The length of the address.
533 *
534 * @return Zero on success.
535 */
536PJ_DECL(pj_status_t) pj_sock_bind( pj_sock_t sockfd,
537 const pj_sockaddr_t *my_addr,
538 int addrlen);
539
540/**
541 * Bind the IP socket sockfd to the given address and port.
542 *
543 * @param sockfd The socket descriptor.
544 * @param addr Local address to bind the socket to, in host byte order.
545 * @param port The local port to bind the socket to, in host byte order.
546 *
547 * @return Zero on success.
548 */
549PJ_DECL(pj_status_t) pj_sock_bind_in( pj_sock_t sockfd,
550 pj_uint32_t addr,
551 pj_uint16_t port);
552
553#if PJ_HAS_TCP
554/**
555 * Listen for incoming connection. This function only applies to connection
556 * oriented sockets (such as PJ_SOCK_STREAM or PJ_SOCK_SEQPACKET), and it
557 * indicates the willingness to accept incoming connections.
558 *
559 * @param sockfd The socket descriptor.
560 * @param backlog Defines the maximum length the queue of pending
561 * connections may grow to.
562 *
563 * @return Zero on success.
564 */
565PJ_DECL(pj_status_t) pj_sock_listen( pj_sock_t sockfd,
566 int backlog );
567
568/**
569 * Accept new connection on the specified connection oriented server socket.
570 *
571 * @param serverfd The server socket.
572 * @param newsock New socket on success, of PJ_INVALID_SOCKET if failed.
573 * @param addr A pointer to sockaddr type. If the argument is not NULL,
574 * it will be filled by the address of connecting entity.
575 * @param addrlen Initially specifies the length of the address, and upon
576 * return will be filled with the exact address length.
577 *
578 * @return Zero on success, or the error number.
579 */
580PJ_DECL(pj_status_t) pj_sock_accept( pj_sock_t serverfd,
581 pj_sock_t *newsock,
582 pj_sockaddr_t *addr,
583 int *addrlen);
584#endif
585
586/**
587 * The file descriptor sockfd must refer to a socket. If the socket is of
588 * type PJ_SOCK_DGRAM then the serv_addr address is the address to which
589 * datagrams are sent by default, and the only address from which datagrams
590 * are received. If the socket is of type PJ_SOCK_STREAM or PJ_SOCK_SEQPACKET,
591 * this call attempts to make a connection to another socket. The
592 * other socket is specified by serv_addr, which is an address (of length
593 * addrlen) in the communications space of the socket. Each communications
594 * space interprets the serv_addr parameter in its own way.
595 *
596 * @param sockfd The socket descriptor.
597 * @param serv_addr Server address to connect to.
598 * @param addrlen The length of server address.
599 *
600 * @return Zero on success.
601 */
602PJ_DECL(pj_status_t) pj_sock_connect( pj_sock_t sockfd,
603 const pj_sockaddr_t *serv_addr,
604 int addrlen);
605
606/**
607 * Return the address of peer which is connected to socket sockfd.
608 *
609 * @param sockfd The socket descriptor.
610 * @param addr Pointer to sockaddr structure to which the address
611 * will be returned.
612 * @param namelen Initially the length of the addr. Upon return the value
613 * will be set to the actual length of the address.
614 *
615 * @return Zero on success.
616 */
617PJ_DECL(pj_status_t) pj_sock_getpeername(pj_sock_t sockfd,
618 pj_sockaddr_t *addr,
619 int *namelen);
620
621/**
622 * Return the current name of the specified socket.
623 *
624 * @param sockfd The socket descriptor.
625 * @param addr Pointer to sockaddr structure to which the address
626 * will be returned.
627 * @param namelen Initially the length of the addr. Upon return the value
628 * will be set to the actual length of the address.
629 *
630 * @return Zero on success.
631 */
632PJ_DECL(pj_status_t) pj_sock_getsockname( pj_sock_t sockfd,
633 pj_sockaddr_t *addr,
634 int *namelen);
635
636/**
637 * Get socket option associated with a socket. Options may exist at multiple
638 * protocol levels; they are always present at the uppermost socket level.
639 *
640 * @param sockfd The socket descriptor.
641 * @param level The level which to get the option from.
642 * @param optname The option name.
643 * @param optval Identifies the buffer which the value will be
644 * returned.
645 * @param optlen Initially contains the length of the buffer, upon
646 * return will be set to the actual size of the value.
647 *
648 * @return Zero on success.
649 */
650PJ_DECL(pj_status_t) pj_sock_getsockopt( pj_sock_t sockfd,
651 pj_uint16_t level,
652 pj_uint16_t optname,
653 void *optval,
654 int *optlen);
655/**
656 * Manipulate the options associated with a socket. Options may exist at
657 * multiple protocol levels; they are always present at the uppermost socket
658 * level.
659 *
660 * @param sockfd The socket descriptor.
661 * @param level The level which to get the option from.
662 * @param optname The option name.
663 * @param optval Identifies the buffer which contain the value.
664 * @param optlen The length of the value.
665 *
666 * @return PJ_SUCCESS or the status code.
667 */
668PJ_DECL(pj_status_t) pj_sock_setsockopt( pj_sock_t sockfd,
669 pj_uint16_t level,
670 pj_uint16_t optname,
671 const void *optval,
672 int optlen);
673
674
675/**
676 * Receives data stream or message coming to the specified socket.
677 *
678 * @param sockfd The socket descriptor.
679 * @param buf The buffer to receive the data or message.
680 * @param len On input, the length of the buffer. On return,
681 * contains the length of data received.
682 * @param flags Combination of #pj_sock_msg_flag.
683 *
684 * @return PJ_SUCCESS or the error code.
685 */
686PJ_DECL(pj_status_t) pj_sock_recv(pj_sock_t sockfd,
687 void *buf,
688 pj_ssize_t *len,
689 unsigned flags);
690
691/**
692 * Receives data stream or message coming to the specified socket.
693 *
694 * @param sockfd The socket descriptor.
695 * @param buf The buffer to receive the data or message.
696 * @param len On input, the length of the buffer. On return,
697 * contains the length of data received.
698 * @param flags Bitmask combination of #pj_sock_msg_flag.
699 * @param from If not NULL, it will be filled with the source
700 * address of the connection.
701 * @param fromlen Initially contains the length of from address,
702 * and upon return will be filled with the actual
703 * length of the address.
704 *
705 * @return PJ_SUCCESS or the error code.
706 */
707PJ_DECL(pj_status_t) pj_sock_recvfrom( pj_sock_t sockfd,
708 void *buf,
709 pj_ssize_t *len,
710 unsigned flags,
711 pj_sockaddr_t *from,
712 int *fromlen);
713
714/**
715 * Transmit data to the socket.
716 *
717 * @param sockfd Socket descriptor.
718 * @param buf Buffer containing data to be sent.
719 * @param len On input, the length of the data in the buffer.
720 * Upon return, it will be filled with the length
721 * of data sent.
722 * @param flags Bitmask combination of #pj_sock_msg_flag.
723 *
724 * @return PJ_SUCCESS or the status code.
725 */
726PJ_DECL(pj_status_t) pj_sock_send(pj_sock_t sockfd,
727 const void *buf,
728 pj_ssize_t *len,
729 unsigned flags);
730
731/**
732 * Transmit data to the socket to the specified address.
733 *
734 * @param sockfd Socket descriptor.
735 * @param buf Buffer containing data to be sent.
736 * @param len On input, the length of the data in the buffer.
737 * Upon return, it will be filled with the length
738 * of data sent.
739 * @param flags Bitmask combination of #pj_sock_msg_flag.
740 * @param to The address to send.
741 * @param tolen The length of the address in bytes.
742 *
Benny Prijonoac9d1422006-01-18 23:32:27 +0000743 * @return PJ_SUCCESS or the status code.
Benny Prijono9033e312005-11-21 02:08:39 +0000744 */
745PJ_DECL(pj_status_t) pj_sock_sendto(pj_sock_t sockfd,
746 const void *buf,
747 pj_ssize_t *len,
748 unsigned flags,
749 const pj_sockaddr_t *to,
750 int tolen);
751
752#if PJ_HAS_TCP
753/**
754 * The shutdown call causes all or part of a full-duplex connection on the
755 * socket associated with sockfd to be shut down.
756 *
757 * @param sockfd The socket descriptor.
758 * @param how If how is PJ_SHUT_RD, further receptions will be
759 * disallowed. If how is PJ_SHUT_WR, further transmissions
760 * will be disallowed. If how is PJ_SHUT_RDWR, further
761 * receptions andtransmissions will be disallowed.
762 *
763 * @return Zero on success.
764 */
765PJ_DECL(pj_status_t) pj_sock_shutdown( pj_sock_t sockfd,
766 int how);
767#endif
768
769/**
770 * @}
771 */
772
773
774PJ_END_DECL
775
776#endif /* __PJ_SOCK_H__ */
777