blob: 79f161f2a9a0c24b8db823d1f31de9ac0cba86e1 [file] [log] [blame]
Benny Prijono4766ffe2005-11-01 17:56:59 +00001/* $Id$
2 *
3 */
Benny Prijonodd859a62005-11-01 16:42:51 +00004
5#ifndef __PJ_SOCK_H__
6#define __PJ_SOCK_H__
7
8/**
9 * @file sock.h
10 * @brief Socket Abstraction.
11 */
12
13#include <pj/types.h>
14
15PJ_BEGIN_DECL
16
17
18/**
19 * @defgroup PJ_SOCK Socket Abstraction
20 * @ingroup PJ_IO
21 * @{
22 *
23 * The PJLIB socket abstraction layer is a thin and very portable abstraction
24 * for socket API. It provides API similar to BSD socket API. The abstraction
25 * is needed because BSD socket API is not always available on all platforms,
26 * therefore it wouldn't be possible to create a trully portable network
27 * programs unless we provide such abstraction.
28 *
29 * Applications can use this API directly in their application, just
30 * as they would when using traditional BSD socket API, provided they
31 * call #pj_init() first.
32 *
33 * \section pj_sock_examples_sec Examples
34 *
35 * For some examples on how to use the socket API, please see:
36 *
37 * - \ref page_pjlib_sock_test
38 * - \ref page_pjlib_select_test
39 * - \ref page_pjlib_sock_perf_test
40 */
41
42
43/**
44 * Supported address families.
45 * APPLICATION MUST USE THESE VALUES INSTEAD OF NORMAL AF_*, BECAUSE
46 * THE LIBRARY WILL DO TRANSLATION TO THE NATIVE VALUE.
47 */
48extern const pj_uint16_t PJ_AF_UNIX; /**< Unix domain socket. */
49#define PJ_AF_LOCAL PJ_AF_UNIX; /**< POSIX name for AF_UNIX */
50extern const pj_uint16_t PJ_AF_INET; /**< Internet IP protocol. */
51extern const pj_uint16_t PJ_AF_INET6; /**< IP version 6. */
52extern const pj_uint16_t PJ_AF_PACKET; /**< Packet family. */
53extern const pj_uint16_t PJ_AF_IRDA; /**< IRDA sockets. */
54
55
56/**
57 * Supported types of sockets.
58 * APPLICATION MUST USE THESE VALUES INSTEAD OF NORMAL SOCK_*, BECAUSE
59 * THE LIBRARY WILL TRANSLATE THE VALUE TO THE NATIVE VALUE.
60 */
61
62extern const pj_uint16_t PJ_SOCK_STREAM; /**< Sequenced, reliable, connection-
63 based byte streams. */
64extern const pj_uint16_t PJ_SOCK_DGRAM; /**< Connectionless, unreliable
65 datagrams of fixed maximum
66 lengths. */
67extern const pj_uint16_t PJ_SOCK_RAW; /**< Raw protocol interface. */
68extern const pj_uint16_t PJ_SOCK_RDM; /**< Reliably-delivered messages. */
69
70
71/**
72 * Socket level specified in #pj_sock_setsockopt().
73 * APPLICATION MUST USE THESE VALUES INSTEAD OF NORMAL SOL_*, BECAUSE
74 * THE LIBRARY WILL TRANSLATE THE VALUE TO THE NATIVE VALUE.
75 */
76extern const pj_uint16_t PJ_SOL_SOCKET; /**< Socket level. */
77extern const pj_uint16_t PJ_SOL_IP; /**< IP level. */
78extern const pj_uint16_t PJ_SOL_TCP; /**< TCP level. */
79extern const pj_uint16_t PJ_SOL_UDP; /**< UDP level. */
80extern const pj_uint16_t PJ_SOL_IPV6; /**< IP version 6 */
81
82/**
83 * Flags to be specified in #pj_sock_recv, #pj_sock_send, etc.
84 */
85typedef enum pj_sock_msg_flag
86{
87 PJ_MSG_OOB = 0x01, /**< Out-of-band messages. */
88 PJ_MSG_PEEK = 0x02, /**< Peek, don't remove from buffer. */
89 PJ_MSG_DONTROUTE = 0x04, /**< Don't route. */
90} pj_sock_msg_flag;
91
92
93/**
94 * Flag to be specified in #pj_sock_shutdown.
95 */
96typedef enum pj_socket_sd_type
97{
98 PJ_SD_RECEIVE = 0, /**< No more receive. */
99 PJ_SHUT_RD = 0, /**< Alias for SD_RECEIVE. */
100 PJ_SD_SEND = 1, /**< No more sending. */
101 PJ_SHUT_WR = 1, /**< Alias for SD_SEND. */
102 PJ_SD_BOTH = 2, /**< No more send and receive. */
103 PJ_SHUT_RDWR = 2, /**< Alias for SD_BOTH. */
104} pj_socket_sd_type;
105
106
107
108/** Address to accept any incoming messages. */
109#define PJ_INADDR_ANY ((pj_uint32_t)0)
110
111/** Address indicating an error return */
112#define PJ_INADDR_NONE ((pj_uint32_t)0xffffffff)
113
114/** Address to send to all hosts. */
115#define PJ_INADDR_BROADCAST ((pj_uint32_t)0xffffffff)
116
117
118/**
119 * Maximum length specifiable by #pj_sock_listen().
120 * If the build system doesn't override this value, then the lowest
121 * denominator (five, in Win32 systems) will be used.
122 */
123#if !defined(PJ_SOMAXCONN)
124# define PJ_SOMAXCONN 5
125#endif
126
127
128/**
129 * Constant for invalid socket returned by #pj_sock_socket() and
130 * #pj_sock_accept().
131 */
132#define PJ_INVALID_SOCKET (-1)
133
134/**
135 * Structure describing a generic socket address.
136 */
137typedef struct pj_sockaddr
138{
139 pj_uint16_t sa_family; /**< Common data: address family. */
140 char sa_data[14]; /**< Address data. */
141} pj_sockaddr;
142
143
144/**
145 * This structure describes Internet address.
146 */
147typedef struct pj_in_addr
148{
149 pj_uint32_t s_addr; /**< The 32bit IP address. */
150} pj_in_addr;
151
152
153/**
154 * This structure describes Internet socket address.
155 */
156typedef struct pj_sockaddr_in
157{
158 pj_uint16_t sin_family; /**< Address family. */
159 pj_uint16_t sin_port; /**< Transport layer port number. */
160 pj_in_addr sin_addr; /**< IP address. */
161 char sin_zero[8]; /**< Padding. */
162} pj_sockaddr_in;
163
164
165/**
166 * This structure describes IPv6 address.
167 */
168typedef struct pj_in6_addr
169{
170 /** Union of address formats. */
171 union {
172 pj_uint8_t u6_addr8[16]; /**< u6_addr8 */
173 pj_uint16_t u6_addr16[8]; /**< u6_addr16 */
174 pj_uint32_t u6_addr32[4]; /**< u6_addr32 */
175 } in6_u;
176/** Shortcut to access in6_u.u6_addr8. */
177#define s6_addr in6_u.u6_addr8
178/** Shortcut to access in6_u.u6_addr16. */
179#define s6_addr16 in6_u.u6_addr16
180/** Shortcut to access in6_u.u6_addr32. */
181#define s6_addr32 in6_u.u6_addr32
182} pj_in6_addr;
183
184/** Initializer value for pj_in6_addr. */
185#define PJ_IN6ADDR_ANY_INIT { { { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 } } }
186
187/** Initializer value for pj_in6_addr. */
188#define PJ_IN6ADDR_LOOPBACK_INIT { { { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1 } } }
189
190/**
191 * This structure describes IPv6 socket address.
192 */
193typedef struct pj_sockaddr_in6
194{
195 pj_uint16_t sin6_family; /**< Address family */
196 pj_uint16_t sin6_port; /**< Transport layer port number. */
197 pj_uint32_t sin6_flowinfo; /**< IPv6 flow information */
198 pj_in6_addr sin6_addr; /**< IPv6 address. */
199 pj_uint32_t sin6_scope_id; /**< IPv6 scope-id */
200} pj_sockaddr_in6;
201
202
203/*****************************************************************************
204 *
205 * SOCKET ADDRESS MANIPULATION.
206 *
207 *****************************************************************************
208 */
209
210/**
211 * Convert 16-bit value from network byte order to host byte order.
212 *
213 * @param netshort 16-bit network value.
214 * @return 16-bit host value.
215 */
216PJ_DECL(pj_uint16_t) pj_ntohs(pj_uint16_t netshort);
217
218/**
219 * Convert 16-bit value from host byte order to network byte order.
220 *
221 * @param hostshort 16-bit host value.
222 * @return 16-bit network value.
223 */
224PJ_DECL(pj_uint16_t) pj_htons(pj_uint16_t hostshort);
225
226/**
227 * Convert 32-bit value from network byte order to host byte order.
228 *
229 * @param netlong 32-bit network value.
230 * @return 32-bit host value.
231 */
232PJ_DECL(pj_uint32_t) pj_ntohl(pj_uint32_t netlong);
233
234/**
235 * Convert 32-bit value from host byte order to network byte order.
236 *
237 * @param hostlong 32-bit host value.
238 * @return 32-bit network value.
239 */
240PJ_DECL(pj_uint32_t) pj_htonl(pj_uint32_t hostlong);
241
242/**
243 * Convert an Internet host address given in network byte order
244 * to string in standard numbers and dots notation.
245 *
246 * @param inaddr The host address.
247 * @return The string address.
248 */
249PJ_DECL(char*) pj_inet_ntoa(pj_in_addr inaddr);
250
251/**
252 * This function converts the Internet host address cp from the standard
253 * numbers-and-dots notation into binary data and stores it in the structure
254 * that inp points to.
255 *
256 * @param cp IP address in standard numbers-and-dots notation.
257 * @param inp Structure that holds the output of the conversion.
258 *
259 * @return nonzero if the address is valid, zero if not.
260 */
261PJ_DECL(int) pj_inet_aton(const pj_str_t *cp, struct pj_in_addr *inp);
262
263/**
264 * Convert address string with numbers and dots to binary IP address.
265 *
266 * @param cp The IP address in numbers and dots notation.
267 * @return If success, the IP address is returned in network
268 * byte order. If failed, PJ_INADDR_NONE will be
269 * returned.
270 * @remark
271 * This is an obsolete interface to #pj_inet_aton(); it is obsolete
272 * because -1 is a valid address (255.255.255.255), and #pj_inet_aton()
273 * provides a cleaner way to indicate error return.
274 */
275PJ_DECL(pj_in_addr) pj_inet_addr(const pj_str_t *cp);
276
277
278/**
279 * Get the transport layer port number of an Internet socket address.
280 * The port is returned in host byte order.
281 *
282 * @param addr The IP socket address.
283 * @return Port number, in host byte order.
284 */
285PJ_INLINE(pj_uint16_t) pj_sockaddr_in_get_port(const pj_sockaddr_in *addr)
286{
287 return pj_ntohs(addr->sin_port);
288}
289
290/**
291 * Set the port number of an Internet socket address.
292 *
293 * @param addr The IP socket address.
294 * @param hostport The port number, in host byte order.
295 */
296PJ_INLINE(void) pj_sockaddr_in_set_port(pj_sockaddr_in *addr,
297 pj_uint16_t hostport)
298{
299 addr->sin_port = pj_htons(hostport);
300}
301
302/**
303 * Get the IP address of an Internet socket address.
304 * The address is returned as 32bit value in host byte order.
305 *
306 * @param addr The IP socket address.
307 * @return 32bit address, in host byte order.
308 */
309PJ_INLINE(pj_in_addr) pj_sockaddr_in_get_addr(const pj_sockaddr_in *addr)
310{
311 pj_in_addr in_addr;
312 in_addr.s_addr = pj_ntohl(addr->sin_addr.s_addr);
313 return in_addr;
314};
315
316/**
317 * Set the IP address of an Internet socket address.
318 *
319 * @param addr The IP socket address.
320 * @param hostaddr The host address, in host byte order.
321 */
322PJ_INLINE(void) pj_sockaddr_in_set_addr(pj_sockaddr_in *addr,
323 pj_uint32_t hostaddr)
324{
325 addr->sin_addr.s_addr = pj_htonl(hostaddr);
326}
327
328/**
329 * Set the IP address of an IP socket address from string address,
330 * with resolving the host if necessary. The string address may be in a
331 * standard numbers and dots notation or may be a hostname. If hostname
332 * is specified, then the function will resolve the host into the IP
333 * address.
334 *
335 * @param addr The IP socket address to be set.
336 * @param cp The address string, which can be in a standard
337 * dotted numbers or a hostname to be resolved.
338 *
339 * @return Zero on success.
340 */
341PJ_DECL(pj_status_t) pj_sockaddr_in_set_str_addr( pj_sockaddr_in *addr,
342 const pj_str_t *cp);
343
344/**
345 * Set the IP address and port of an IP socket address.
346 * The string address may be in a standard numbers and dots notation or
347 * may be a hostname. If hostname is specified, then the function will
348 * resolve the host into the IP address.
349 *
350 * @param addr The IP socket address to be set.
351 * @param cp The address string, which can be in a standard
352 * dotted numbers or a hostname to be resolved.
353 * @param port The port number, in host byte order.
354 *
355 * @return Zero on success.
356 */
357PJ_DECL(pj_status_t) pj_sockaddr_in_init( pj_sockaddr_in *addr,
358 const pj_str_t *cp,
359 pj_uint16_t port);
360
361
362/*****************************************************************************
363 *
364 * HOST NAME AND ADDRESS.
365 *
366 *****************************************************************************
367 */
368
369/**
370 * Get system's host name.
371 *
372 * @return The hostname, or empty string if the hostname can not
373 * be identified.
374 */
375PJ_DECL(const pj_str_t*) pj_gethostname(void);
376
377/**
378 * Get host's IP address, which the the first IP address that is resolved
379 * from the hostname.
380 *
381 * @return The host's IP address, PJ_INADDR_NONE if the host
382 * IP address can not be identified.
383 */
384PJ_DECL(pj_in_addr) pj_gethostaddr(void);
385
386
387/*****************************************************************************
388 *
389 * SOCKET API.
390 *
391 *****************************************************************************
392 */
393
394/**
395 * Create new socket/endpoint for communication.
396 *
397 * @param family Specifies a communication domain; this selects the
398 * protocol family which will be used for communication.
399 * @param type The socket has the indicated type, which specifies the
400 * communication semantics.
401 * @param protocol Specifies a particular protocol to be used with the
402 * socket. Normally only a single protocol exists to support
403 * a particular socket type within a given protocol family,
404 * in which a case protocol can be specified as 0.
405 * @param sock New socket descriptor, or PJ_INVALID_SOCKET on error.
406 *
407 * @return Zero on success.
408 */
409PJ_DECL(pj_status_t) pj_sock_socket(int family,
410 int type,
411 int protocol,
412 pj_sock_t *sock);
413
414/**
415 * Close the socket descriptor.
416 *
417 * @param sockfd The socket descriptor.
418 *
419 * @return Zero on success.
420 */
421PJ_DECL(pj_status_t) pj_sock_close(pj_sock_t sockfd);
422
423
424/**
425 * This function gives the socket sockfd the local address my_addr. my_addr is
426 * addrlen bytes long. Traditionally, this is called assigning a name to
427 * a socket. When a socket is created with #pj_sock_socket(), it exists in a
428 * name space (address family) but has no name assigned.
429 *
430 * @param sockfd The socket desriptor.
431 * @param my_addr The local address to bind the socket to.
432 * @param addrlen The length of the address.
433 *
434 * @return Zero on success.
435 */
436PJ_DECL(pj_status_t) pj_sock_bind( pj_sock_t sockfd,
437 const pj_sockaddr_t *my_addr,
438 int addrlen);
439
440/**
441 * Bind the IP socket sockfd to the given address and port.
442 *
443 * @param sockfd The socket descriptor.
444 * @param addr Local address to bind the socket to, in host byte order.
445 * @param port The local port to bind the socket to, in host byte order.
446 *
447 * @return Zero on success.
448 */
449PJ_DECL(pj_status_t) pj_sock_bind_in( pj_sock_t sockfd,
450 pj_uint32_t addr,
451 pj_uint16_t port);
452
453#if PJ_HAS_TCP
454/**
455 * Listen for incoming connection. This function only applies to connection
456 * oriented sockets (such as PJ_SOCK_STREAM or PJ_SOCK_SEQPACKET), and it
457 * indicates the willingness to accept incoming connections.
458 *
459 * @param sockfd The socket descriptor.
460 * @param backlog Defines the maximum length the queue of pending
461 * connections may grow to.
462 *
463 * @return Zero on success.
464 */
465PJ_DECL(pj_status_t) pj_sock_listen( pj_sock_t sockfd,
466 int backlog );
467
468/**
469 * Accept new connection on the specified connection oriented server socket.
470 *
471 * @param serverfd The server socket.
472 * @param newsock New socket on success, of PJ_INVALID_SOCKET if failed.
473 * @param addr A pointer to sockaddr type. If the argument is not NULL,
474 * it will be filled by the address of connecting entity.
475 * @param addrlen Initially specifies the length of the address, and upon
476 * return will be filled with the exact address length.
477 *
478 * @return Zero on success, or the error number.
479 */
480PJ_DECL(pj_status_t) pj_sock_accept( pj_sock_t serverfd,
481 pj_sock_t *newsock,
482 pj_sockaddr_t *addr,
483 int *addrlen);
484#endif
485
486/**
487 * The file descriptor sockfd must refer to a socket. If the socket is of
488 * type PJ_SOCK_DGRAM then the serv_addr address is the address to which
489 * datagrams are sent by default, and the only address from which datagrams
490 * are received. If the socket is of type PJ_SOCK_STREAM or PJ_SOCK_SEQPACKET,
491 * this call attempts to make a connection to another socket. The
492 * other socket is specified by serv_addr, which is an address (of length
493 * addrlen) in the communications space of the socket. Each communications
494 * space interprets the serv_addr parameter in its own way.
495 *
496 * @param sockfd The socket descriptor.
497 * @param serv_addr Server address to connect to.
498 * @param addrlen The length of server address.
499 *
500 * @return Zero on success.
501 */
502PJ_DECL(pj_status_t) pj_sock_connect( pj_sock_t sockfd,
503 const pj_sockaddr_t *serv_addr,
504 int addrlen);
505
506/**
507 * Return the address of peer which is connected to socket sockfd.
508 *
509 * @param sockfd The socket descriptor.
510 * @param addr Pointer to sockaddr structure to which the address
511 * will be returned.
512 * @param namelen Initially the length of the addr. Upon return the value
513 * will be set to the actual length of the address.
514 *
515 * @return Zero on success.
516 */
517PJ_DECL(pj_status_t) pj_sock_getpeername(pj_sock_t sockfd,
518 pj_sockaddr_t *addr,
519 int *namelen);
520
521/**
522 * Return the current name of the specified socket.
523 *
524 * @param sockfd The socket descriptor.
525 * @param addr Pointer to sockaddr structure to which the address
526 * will be returned.
527 * @param namelen Initially the length of the addr. Upon return the value
528 * will be set to the actual length of the address.
529 *
530 * @return Zero on success.
531 */
532PJ_DECL(pj_status_t) pj_sock_getsockname( pj_sock_t sockfd,
533 pj_sockaddr_t *addr,
534 int *namelen);
535
536/**
537 * Get socket option associated with a socket. Options may exist at multiple
538 * protocol levels; they are always present at the uppermost socket level.
539 *
540 * @param sockfd The socket descriptor.
541 * @param level The level which to get the option from.
542 * @param optname The option name, which will be passed uninterpreted
543 * by the library.
544 * @param optval Identifies the buffer which the value will be
545 * returned.
546 * @param optlen Initially contains the length of the buffer, upon
547 * return will be set to the actual size of the value.
548 *
549 * @return Zero on success.
550 */
551PJ_DECL(pj_status_t) pj_sock_getsockopt( pj_sock_t sockfd,
552 int level,
553 int optname,
554 void *optval,
555 int *optlen);
556/**
557 * Manipulate the options associated with a socket. Options may exist at
558 * multiple protocol levels; they are always present at the uppermost socket
559 * level.
560 *
561 * @param sockfd The socket descriptor.
562 * @param level The level which to get the option from.
563 * @param optname The option name, which will be passed uninterpreted
564 * by the library.
565 * @param optval Identifies the buffer which contain the value.
566 * @param optlen The length of the value.
567 *
568 * @return PJ_SUCCESS or the status code.
569 */
570PJ_DECL(pj_status_t) pj_sock_setsockopt( pj_sock_t sockfd,
571 int level,
572 int optname,
573 const void *optval,
574 int optlen);
575
576
577/**
578 * Receives data stream or message coming to the specified socket.
579 *
580 * @param sockfd The socket descriptor.
581 * @param buf The buffer to receive the data or message.
582 * @param len On input, the length of the buffer. On return,
583 * contains the length of data received.
584 * @param flags Combination of #pj_sock_msg_flag.
585 *
586 * @return PJ_SUCCESS or the error code.
587 */
588PJ_DECL(pj_status_t) pj_sock_recv(pj_sock_t sockfd,
589 void *buf,
590 pj_ssize_t *len,
591 unsigned flags);
592
593/**
594 * Receives data stream or message coming to the specified socket.
595 *
596 * @param sockfd The socket descriptor.
597 * @param buf The buffer to receive the data or message.
598 * @param len On input, the length of the buffer. On return,
599 * contains the length of data received.
600 * @param flags Bitmask combination of #pj_sock_msg_flag.
601 * @param from If not NULL, it will be filled with the source
602 * address of the connection.
603 * @param fromlen Initially contains the length of from address,
604 * and upon return will be filled with the actual
605 * length of the address.
606 *
607 * @return PJ_SUCCESS or the error code.
608 */
609PJ_DECL(pj_status_t) pj_sock_recvfrom( pj_sock_t sockfd,
610 void *buf,
611 pj_ssize_t *len,
612 unsigned flags,
613 pj_sockaddr_t *from,
614 int *fromlen);
615
616/**
617 * Transmit data to the socket.
618 *
619 * @param sockfd Socket descriptor.
620 * @param buf Buffer containing data to be sent.
621 * @param len On input, the length of the data in the buffer.
622 * Upon return, it will be filled with the length
623 * of data sent.
624 * @param flags Bitmask combination of #pj_sock_msg_flag.
625 *
626 * @return PJ_SUCCESS or the status code.
627 */
628PJ_DECL(pj_status_t) pj_sock_send(pj_sock_t sockfd,
629 const void *buf,
630 pj_ssize_t *len,
631 unsigned flags);
632
633/**
634 * Transmit data to the socket to the specified address.
635 *
636 * @param sockfd Socket descriptor.
637 * @param buf Buffer containing data to be sent.
638 * @param len On input, the length of the data in the buffer.
639 * Upon return, it will be filled with the length
640 * of data sent.
641 * @param flags Bitmask combination of #pj_sock_msg_flag.
642 * @param to The address to send.
643 * @param tolen The length of the address in bytes.
644 *
645 * @return The length of data successfully sent.
646 */
647PJ_DECL(pj_status_t) pj_sock_sendto(pj_sock_t sockfd,
648 const void *buf,
649 pj_ssize_t *len,
650 unsigned flags,
651 const pj_sockaddr_t *to,
652 int tolen);
653
654#if PJ_HAS_TCP
655/**
656 * The shutdown call causes all or part of a full-duplex connection on the
657 * socket associated with sockfd to be shut down.
658 *
659 * @param sockfd The socket descriptor.
660 * @param how If how is PJ_SHUT_RD, further receptions will be
661 * disallowed. If how is PJ_SHUT_WR, further transmissions
662 * will be disallowed. If how is PJ_SHUT_RDWR, further
663 * receptions andtransmissions will be disallowed.
664 *
665 * @return Zero on success.
666 */
667PJ_DECL(pj_status_t) pj_sock_shutdown( pj_sock_t sockfd,
668 int how);
669#endif
670
671/**
672 * @}
673 */
674
675
676PJ_END_DECL
677
678#endif /* __PJ_SOCK_H__ */
679