blob: 80d2ab7d2cbb6cbd046bcef10da886f4a5456cdc [file] [log] [blame]
Benny Prijono9033e312005-11-21 02:08:39 +00001/* $Id$ */
2/*
3 * Copyright (C)2003-2006 Benny Prijono <benny@prijono.org>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19#ifndef __PJ_SOCK_H__
20#define __PJ_SOCK_H__
21
22/**
23 * @file sock.h
24 * @brief Socket Abstraction.
25 */
26
27#include <pj/types.h>
28
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
178/**
179 * Structure describing a generic socket address.
Benny Prijonoe67d99a2006-03-20 12:39:24 +0000180 * If PJ_SOCKADDR_HAS_LEN is not zero, then sa_zero_len member is added
181 * to this struct. As far the application is concerned, the value of
182 * this member will always be zero. Internally, PJLIB may modify the value
183 * before calling OS socket API, and reset the value back to zero before
184 * returning the struct to application.
Benny Prijono9033e312005-11-21 02:08:39 +0000185 */
186typedef struct pj_sockaddr
187{
Benny Prijonoe67d99a2006-03-20 12:39:24 +0000188#if defined(PJ_SOCKADDR_HAS_LEN) && PJ_SOCKADDR_HAS_LEN!=0
189 pj_uint8_t sa_zero_len;
190 pj_uint8_t sa_family;
191#else
Benny Prijono9033e312005-11-21 02:08:39 +0000192 pj_uint16_t sa_family; /**< Common data: address family. */
Benny Prijonoe67d99a2006-03-20 12:39:24 +0000193#endif
Benny Prijono9033e312005-11-21 02:08:39 +0000194 char sa_data[14]; /**< Address data. */
195} pj_sockaddr;
196
197
Benny Prijonoab668ed2006-06-01 11:38:40 +0000198#undef s_addr
199
Benny Prijono9033e312005-11-21 02:08:39 +0000200/**
201 * This structure describes Internet address.
202 */
203typedef struct pj_in_addr
204{
205 pj_uint32_t s_addr; /**< The 32bit IP address. */
206} pj_in_addr;
207
208
209/**
210 * This structure describes Internet socket address.
Benny Prijonoe67d99a2006-03-20 12:39:24 +0000211 * If PJ_SOCKADDR_HAS_LEN is not zero, then sin_zero_len member is added
212 * to this struct. As far the application is concerned, the value of
213 * this member will always be zero. Internally, PJLIB may modify the value
214 * before calling OS socket API, and reset the value back to zero before
215 * returning the struct to application.
Benny Prijono9033e312005-11-21 02:08:39 +0000216 */
Benny Prijono0ca04b62005-12-30 23:50:15 +0000217struct pj_sockaddr_in
Benny Prijono9033e312005-11-21 02:08:39 +0000218{
Benny Prijonoe67d99a2006-03-20 12:39:24 +0000219#if defined(PJ_SOCKADDR_HAS_LEN) && PJ_SOCKADDR_HAS_LEN!=0
220 pj_uint8_t sin_zero_len; /**< Just ignore this. */
221 pj_uint8_t sin_family; /**< Address family. */
222#else
Benny Prijono9033e312005-11-21 02:08:39 +0000223 pj_uint16_t sin_family; /**< Address family. */
Benny Prijonoe67d99a2006-03-20 12:39:24 +0000224#endif
Benny Prijono9033e312005-11-21 02:08:39 +0000225 pj_uint16_t sin_port; /**< Transport layer port number. */
226 pj_in_addr sin_addr; /**< IP address. */
227 char sin_zero[8]; /**< Padding. */
Benny Prijono0ca04b62005-12-30 23:50:15 +0000228};
Benny Prijono9033e312005-11-21 02:08:39 +0000229
230
231/**
232 * This structure describes IPv6 address.
233 */
234typedef struct pj_in6_addr
235{
236 /** Union of address formats. */
237 union {
238 pj_uint8_t u6_addr8[16]; /**< u6_addr8 */
239 pj_uint16_t u6_addr16[8]; /**< u6_addr16 */
240 pj_uint32_t u6_addr32[4]; /**< u6_addr32 */
241 } in6_u;
242/** Shortcut to access in6_u.u6_addr8. */
243#define s6_addr in6_u.u6_addr8
244/** Shortcut to access in6_u.u6_addr16. */
245#define s6_addr16 in6_u.u6_addr16
246/** Shortcut to access in6_u.u6_addr32. */
247#define s6_addr32 in6_u.u6_addr32
248} pj_in6_addr;
249
250/** Initializer value for pj_in6_addr. */
251#define PJ_IN6ADDR_ANY_INIT { { { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 } } }
252
253/** Initializer value for pj_in6_addr. */
254#define PJ_IN6ADDR_LOOPBACK_INIT { { { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1 } } }
255
256/**
257 * This structure describes IPv6 socket address.
Benny Prijonoe67d99a2006-03-20 12:39:24 +0000258 * If PJ_SOCKADDR_HAS_LEN is not zero, then sin_zero_len member is added
259 * to this struct. As far the application is concerned, the value of
260 * this member will always be zero. Internally, PJLIB may modify the value
261 * before calling OS socket API, and reset the value back to zero before
262 * returning the struct to application.
Benny Prijono9033e312005-11-21 02:08:39 +0000263 */
264typedef struct pj_sockaddr_in6
265{
Benny Prijonoe67d99a2006-03-20 12:39:24 +0000266#if defined(PJ_SOCKADDR_HAS_LEN) && PJ_SOCKADDR_HAS_LEN!=0
267 pj_uint8_t sin_zero_len; /**< Just ignore this. */
268 pj_uint8_t sin_family; /**< Address family. */
269#else
Benny Prijono9033e312005-11-21 02:08:39 +0000270 pj_uint16_t sin6_family; /**< Address family */
Benny Prijonoe67d99a2006-03-20 12:39:24 +0000271#endif
Benny Prijono9033e312005-11-21 02:08:39 +0000272 pj_uint16_t sin6_port; /**< Transport layer port number. */
273 pj_uint32_t sin6_flowinfo; /**< IPv6 flow information */
274 pj_in6_addr sin6_addr; /**< IPv6 address. */
275 pj_uint32_t sin6_scope_id; /**< IPv6 scope-id */
276} pj_sockaddr_in6;
277
278
279/*****************************************************************************
280 *
281 * SOCKET ADDRESS MANIPULATION.
282 *
283 *****************************************************************************
284 */
285
286/**
287 * Convert 16-bit value from network byte order to host byte order.
288 *
289 * @param netshort 16-bit network value.
290 * @return 16-bit host value.
291 */
292PJ_DECL(pj_uint16_t) pj_ntohs(pj_uint16_t netshort);
293
294/**
295 * Convert 16-bit value from host byte order to network byte order.
296 *
297 * @param hostshort 16-bit host value.
298 * @return 16-bit network value.
299 */
300PJ_DECL(pj_uint16_t) pj_htons(pj_uint16_t hostshort);
301
302/**
303 * Convert 32-bit value from network byte order to host byte order.
304 *
305 * @param netlong 32-bit network value.
306 * @return 32-bit host value.
307 */
308PJ_DECL(pj_uint32_t) pj_ntohl(pj_uint32_t netlong);
309
310/**
311 * Convert 32-bit value from host byte order to network byte order.
312 *
313 * @param hostlong 32-bit host value.
314 * @return 32-bit network value.
315 */
316PJ_DECL(pj_uint32_t) pj_htonl(pj_uint32_t hostlong);
317
318/**
319 * Convert an Internet host address given in network byte order
320 * to string in standard numbers and dots notation.
321 *
322 * @param inaddr The host address.
323 * @return The string address.
324 */
325PJ_DECL(char*) pj_inet_ntoa(pj_in_addr inaddr);
326
327/**
328 * This function converts the Internet host address cp from the standard
329 * numbers-and-dots notation into binary data and stores it in the structure
330 * that inp points to.
331 *
332 * @param cp IP address in standard numbers-and-dots notation.
333 * @param inp Structure that holds the output of the conversion.
334 *
335 * @return nonzero if the address is valid, zero if not.
336 */
337PJ_DECL(int) pj_inet_aton(const pj_str_t *cp, struct pj_in_addr *inp);
338
339/**
340 * Convert address string with numbers and dots to binary IP address.
341 *
342 * @param cp The IP address in numbers and dots notation.
343 * @return If success, the IP address is returned in network
344 * byte order. If failed, PJ_INADDR_NONE will be
345 * returned.
346 * @remark
347 * This is an obsolete interface to #pj_inet_aton(); it is obsolete
348 * because -1 is a valid address (255.255.255.255), and #pj_inet_aton()
349 * provides a cleaner way to indicate error return.
350 */
351PJ_DECL(pj_in_addr) pj_inet_addr(const pj_str_t *cp);
352
Benny Prijono7a4cf152006-03-22 11:48:33 +0000353/**
354 * Convert address string with numbers and dots to binary IP address.
355 *
356 * @param cp The IP address in numbers and dots notation.
357 * @return If success, the IP address is returned in network
358 * byte order. If failed, PJ_INADDR_NONE will be
359 * returned.
360 * @remark
361 * This is an obsolete interface to #pj_inet_aton(); it is obsolete
362 * because -1 is a valid address (255.255.255.255), and #pj_inet_aton()
363 * provides a cleaner way to indicate error return.
364 */
365PJ_DECL(pj_in_addr) pj_inet_addr2(const char *cp);
Benny Prijono9033e312005-11-21 02:08:39 +0000366
367/**
368 * Get the transport layer port number of an Internet socket address.
369 * The port is returned in host byte order.
370 *
371 * @param addr The IP socket address.
372 * @return Port number, in host byte order.
373 */
374PJ_INLINE(pj_uint16_t) pj_sockaddr_in_get_port(const pj_sockaddr_in *addr)
375{
376 return pj_ntohs(addr->sin_port);
377}
378
379/**
380 * Set the port number of an Internet socket address.
381 *
382 * @param addr The IP socket address.
383 * @param hostport The port number, in host byte order.
384 */
385PJ_INLINE(void) pj_sockaddr_in_set_port(pj_sockaddr_in *addr,
386 pj_uint16_t hostport)
387{
388 addr->sin_port = pj_htons(hostport);
389}
390
391/**
392 * Get the IP address of an Internet socket address.
393 * The address is returned as 32bit value in host byte order.
394 *
395 * @param addr The IP socket address.
396 * @return 32bit address, in host byte order.
397 */
398PJ_INLINE(pj_in_addr) pj_sockaddr_in_get_addr(const pj_sockaddr_in *addr)
399{
400 pj_in_addr in_addr;
401 in_addr.s_addr = pj_ntohl(addr->sin_addr.s_addr);
402 return in_addr;
Benny Prijono92ac4472006-07-22 13:42:56 +0000403}
Benny Prijono9033e312005-11-21 02:08:39 +0000404
405/**
406 * Set the IP address of an Internet socket address.
407 *
408 * @param addr The IP socket address.
409 * @param hostaddr The host address, in host byte order.
410 */
411PJ_INLINE(void) pj_sockaddr_in_set_addr(pj_sockaddr_in *addr,
412 pj_uint32_t hostaddr)
413{
414 addr->sin_addr.s_addr = pj_htonl(hostaddr);
415}
416
417/**
418 * Set the IP address of an IP socket address from string address,
419 * with resolving the host if necessary. The string address may be in a
420 * standard numbers and dots notation or may be a hostname. If hostname
421 * is specified, then the function will resolve the host into the IP
422 * address.
423 *
424 * @param addr The IP socket address to be set.
425 * @param cp The address string, which can be in a standard
426 * dotted numbers or a hostname to be resolved.
427 *
428 * @return Zero on success.
429 */
430PJ_DECL(pj_status_t) pj_sockaddr_in_set_str_addr( pj_sockaddr_in *addr,
431 const pj_str_t *cp);
432
433/**
434 * Set the IP address and port of an IP socket address.
435 * The string address may be in a standard numbers and dots notation or
436 * may be a hostname. If hostname is specified, then the function will
437 * resolve the host into the IP address.
438 *
439 * @param addr The IP socket address to be set.
440 * @param cp The address string, which can be in a standard
441 * dotted numbers or a hostname to be resolved.
442 * @param port The port number, in host byte order.
443 *
444 * @return Zero on success.
445 */
446PJ_DECL(pj_status_t) pj_sockaddr_in_init( pj_sockaddr_in *addr,
447 const pj_str_t *cp,
448 pj_uint16_t port);
449
450
451/*****************************************************************************
452 *
453 * HOST NAME AND ADDRESS.
454 *
455 *****************************************************************************
456 */
457
458/**
459 * Get system's host name.
460 *
461 * @return The hostname, or empty string if the hostname can not
462 * be identified.
463 */
464PJ_DECL(const pj_str_t*) pj_gethostname(void);
465
466/**
467 * Get host's IP address, which the the first IP address that is resolved
468 * from the hostname.
469 *
470 * @return The host's IP address, PJ_INADDR_NONE if the host
471 * IP address can not be identified.
472 */
473PJ_DECL(pj_in_addr) pj_gethostaddr(void);
474
475
476/*****************************************************************************
477 *
478 * SOCKET API.
479 *
480 *****************************************************************************
481 */
482
483/**
484 * Create new socket/endpoint for communication.
485 *
486 * @param family Specifies a communication domain; this selects the
487 * protocol family which will be used for communication.
488 * @param type The socket has the indicated type, which specifies the
489 * communication semantics.
490 * @param protocol Specifies a particular protocol to be used with the
491 * socket. Normally only a single protocol exists to support
492 * a particular socket type within a given protocol family,
493 * in which a case protocol can be specified as 0.
494 * @param sock New socket descriptor, or PJ_INVALID_SOCKET on error.
495 *
496 * @return Zero on success.
497 */
498PJ_DECL(pj_status_t) pj_sock_socket(int family,
499 int type,
500 int protocol,
501 pj_sock_t *sock);
502
503/**
504 * Close the socket descriptor.
505 *
506 * @param sockfd The socket descriptor.
507 *
508 * @return Zero on success.
509 */
510PJ_DECL(pj_status_t) pj_sock_close(pj_sock_t sockfd);
511
512
513/**
514 * This function gives the socket sockfd the local address my_addr. my_addr is
515 * addrlen bytes long. Traditionally, this is called assigning a name to
516 * a socket. When a socket is created with #pj_sock_socket(), it exists in a
517 * name space (address family) but has no name assigned.
518 *
519 * @param sockfd The socket desriptor.
520 * @param my_addr The local address to bind the socket to.
521 * @param addrlen The length of the address.
522 *
523 * @return Zero on success.
524 */
525PJ_DECL(pj_status_t) pj_sock_bind( pj_sock_t sockfd,
526 const pj_sockaddr_t *my_addr,
527 int addrlen);
528
529/**
530 * Bind the IP socket sockfd to the given address and port.
531 *
532 * @param sockfd The socket descriptor.
533 * @param addr Local address to bind the socket to, in host byte order.
534 * @param port The local port to bind the socket to, in host byte order.
535 *
536 * @return Zero on success.
537 */
538PJ_DECL(pj_status_t) pj_sock_bind_in( pj_sock_t sockfd,
539 pj_uint32_t addr,
540 pj_uint16_t port);
541
542#if PJ_HAS_TCP
543/**
544 * Listen for incoming connection. This function only applies to connection
545 * oriented sockets (such as PJ_SOCK_STREAM or PJ_SOCK_SEQPACKET), and it
546 * indicates the willingness to accept incoming connections.
547 *
548 * @param sockfd The socket descriptor.
549 * @param backlog Defines the maximum length the queue of pending
550 * connections may grow to.
551 *
552 * @return Zero on success.
553 */
554PJ_DECL(pj_status_t) pj_sock_listen( pj_sock_t sockfd,
555 int backlog );
556
557/**
558 * Accept new connection on the specified connection oriented server socket.
559 *
560 * @param serverfd The server socket.
561 * @param newsock New socket on success, of PJ_INVALID_SOCKET if failed.
562 * @param addr A pointer to sockaddr type. If the argument is not NULL,
563 * it will be filled by the address of connecting entity.
564 * @param addrlen Initially specifies the length of the address, and upon
565 * return will be filled with the exact address length.
566 *
567 * @return Zero on success, or the error number.
568 */
569PJ_DECL(pj_status_t) pj_sock_accept( pj_sock_t serverfd,
570 pj_sock_t *newsock,
571 pj_sockaddr_t *addr,
572 int *addrlen);
573#endif
574
575/**
576 * The file descriptor sockfd must refer to a socket. If the socket is of
577 * type PJ_SOCK_DGRAM then the serv_addr address is the address to which
578 * datagrams are sent by default, and the only address from which datagrams
579 * are received. If the socket is of type PJ_SOCK_STREAM or PJ_SOCK_SEQPACKET,
580 * this call attempts to make a connection to another socket. The
581 * other socket is specified by serv_addr, which is an address (of length
582 * addrlen) in the communications space of the socket. Each communications
583 * space interprets the serv_addr parameter in its own way.
584 *
585 * @param sockfd The socket descriptor.
586 * @param serv_addr Server address to connect to.
587 * @param addrlen The length of server address.
588 *
589 * @return Zero on success.
590 */
591PJ_DECL(pj_status_t) pj_sock_connect( pj_sock_t sockfd,
592 const pj_sockaddr_t *serv_addr,
593 int addrlen);
594
595/**
596 * Return the address of peer which is connected to socket sockfd.
597 *
598 * @param sockfd The socket descriptor.
599 * @param addr Pointer to sockaddr structure to which the address
600 * will be returned.
601 * @param namelen Initially the length of the addr. Upon return the value
602 * will be set to the actual length of the address.
603 *
604 * @return Zero on success.
605 */
606PJ_DECL(pj_status_t) pj_sock_getpeername(pj_sock_t sockfd,
607 pj_sockaddr_t *addr,
608 int *namelen);
609
610/**
611 * Return the current name of the specified socket.
612 *
613 * @param sockfd The socket descriptor.
614 * @param addr Pointer to sockaddr structure to which the address
615 * will be returned.
616 * @param namelen Initially the length of the addr. Upon return the value
617 * will be set to the actual length of the address.
618 *
619 * @return Zero on success.
620 */
621PJ_DECL(pj_status_t) pj_sock_getsockname( pj_sock_t sockfd,
622 pj_sockaddr_t *addr,
623 int *namelen);
624
625/**
626 * Get socket option associated with a socket. Options may exist at multiple
627 * protocol levels; they are always present at the uppermost socket level.
628 *
629 * @param sockfd The socket descriptor.
630 * @param level The level which to get the option from.
631 * @param optname The option name.
632 * @param optval Identifies the buffer which the value will be
633 * returned.
634 * @param optlen Initially contains the length of the buffer, upon
635 * return will be set to the actual size of the value.
636 *
637 * @return Zero on success.
638 */
639PJ_DECL(pj_status_t) pj_sock_getsockopt( pj_sock_t sockfd,
640 pj_uint16_t level,
641 pj_uint16_t optname,
642 void *optval,
643 int *optlen);
644/**
645 * Manipulate the options associated with a socket. Options may exist at
646 * multiple protocol levels; they are always present at the uppermost socket
647 * level.
648 *
649 * @param sockfd The socket descriptor.
650 * @param level The level which to get the option from.
651 * @param optname The option name.
652 * @param optval Identifies the buffer which contain the value.
653 * @param optlen The length of the value.
654 *
655 * @return PJ_SUCCESS or the status code.
656 */
657PJ_DECL(pj_status_t) pj_sock_setsockopt( pj_sock_t sockfd,
658 pj_uint16_t level,
659 pj_uint16_t optname,
660 const void *optval,
661 int optlen);
662
663
664/**
665 * Receives data stream or message coming to the specified socket.
666 *
667 * @param sockfd The socket descriptor.
668 * @param buf The buffer to receive the data or message.
669 * @param len On input, the length of the buffer. On return,
670 * contains the length of data received.
671 * @param flags Combination of #pj_sock_msg_flag.
672 *
673 * @return PJ_SUCCESS or the error code.
674 */
675PJ_DECL(pj_status_t) pj_sock_recv(pj_sock_t sockfd,
676 void *buf,
677 pj_ssize_t *len,
678 unsigned flags);
679
680/**
681 * Receives data stream or message coming to the specified socket.
682 *
683 * @param sockfd The socket descriptor.
684 * @param buf The buffer to receive the data or message.
685 * @param len On input, the length of the buffer. On return,
686 * contains the length of data received.
687 * @param flags Bitmask combination of #pj_sock_msg_flag.
688 * @param from If not NULL, it will be filled with the source
689 * address of the connection.
690 * @param fromlen Initially contains the length of from address,
691 * and upon return will be filled with the actual
692 * length of the address.
693 *
694 * @return PJ_SUCCESS or the error code.
695 */
696PJ_DECL(pj_status_t) pj_sock_recvfrom( pj_sock_t sockfd,
697 void *buf,
698 pj_ssize_t *len,
699 unsigned flags,
700 pj_sockaddr_t *from,
701 int *fromlen);
702
703/**
704 * Transmit data to the socket.
705 *
706 * @param sockfd Socket descriptor.
707 * @param buf Buffer containing data to be sent.
708 * @param len On input, the length of the data in the buffer.
709 * Upon return, it will be filled with the length
710 * of data sent.
711 * @param flags Bitmask combination of #pj_sock_msg_flag.
712 *
713 * @return PJ_SUCCESS or the status code.
714 */
715PJ_DECL(pj_status_t) pj_sock_send(pj_sock_t sockfd,
716 const void *buf,
717 pj_ssize_t *len,
718 unsigned flags);
719
720/**
721 * Transmit data to the socket to the specified address.
722 *
723 * @param sockfd Socket descriptor.
724 * @param buf Buffer containing data to be sent.
725 * @param len On input, the length of the data in the buffer.
726 * Upon return, it will be filled with the length
727 * of data sent.
728 * @param flags Bitmask combination of #pj_sock_msg_flag.
729 * @param to The address to send.
730 * @param tolen The length of the address in bytes.
731 *
Benny Prijonoac9d1422006-01-18 23:32:27 +0000732 * @return PJ_SUCCESS or the status code.
Benny Prijono9033e312005-11-21 02:08:39 +0000733 */
734PJ_DECL(pj_status_t) pj_sock_sendto(pj_sock_t sockfd,
735 const void *buf,
736 pj_ssize_t *len,
737 unsigned flags,
738 const pj_sockaddr_t *to,
739 int tolen);
740
741#if PJ_HAS_TCP
742/**
743 * The shutdown call causes all or part of a full-duplex connection on the
744 * socket associated with sockfd to be shut down.
745 *
746 * @param sockfd The socket descriptor.
747 * @param how If how is PJ_SHUT_RD, further receptions will be
748 * disallowed. If how is PJ_SHUT_WR, further transmissions
749 * will be disallowed. If how is PJ_SHUT_RDWR, further
750 * receptions andtransmissions will be disallowed.
751 *
752 * @return Zero on success.
753 */
754PJ_DECL(pj_status_t) pj_sock_shutdown( pj_sock_t sockfd,
755 int how);
756#endif
757
758/**
759 * @}
760 */
761
762
763PJ_END_DECL
764
765#endif /* __PJ_SOCK_H__ */
766