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