1 /*
2  * Copyright (c) 2007-2009 Marko Kreen, Skype Technologies OÜ
3  *
4  * Permission to use, copy, modify, and/or distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15  */
16 
17 /** @file
18  *
19  * Socket compat, few utils.
20  *
21  * Socket headers included:
22  * - win32: <winsock2.h>
23  * - win32: <ws2tcpip.h>
24  * - <sys/socket.h>
25  * - <sys/un.h>
26  * - <netinet/in.h>
27  * - <netinet/tcp.h>
28  * - <arpa/inet.h>
29  * - <fcntl.h>
30  * - <poll.h>
31  */
32 #ifndef _USUAL_SOCKET_H_
33 #define _USUAL_SOCKET_H_
34 
35 #include <usual/base.h>
36 
37 
38 #ifdef WIN32
39 #include <winsock2.h>
40 #include <ws2tcpip.h>
41 #include <usual/socket_win32.h>
42 #endif
43 
44 #include <fcntl.h>
45 
46 #ifdef HAVE_SYS_SOCKET_H
47 #include <sys/socket.h>
48 #endif
49 #ifdef HAVE_POLL_H
50 #include <poll.h>
51 #endif
52 #ifdef HAVE_SYS_UIO_H
53 #include <sys/uio.h>
54 #endif
55 #ifdef HAVE_SYS_UN_H
56 #include <sys/un.h>
57 #endif
58 #ifdef HAVE_NETINET_IN_H
59 #include <netinet/in.h>
60 #endif
61 #ifdef HAVE_NETINET_TCP_H
62 #include <netinet/tcp.h>
63 #endif
64 #ifdef HAVE_ARPA_INET_H
65 #include <arpa/inet.h>
66 #endif
67 
68 #ifndef INADDR_NONE
69 /** Compat: Some systems (Solaris) does not define INADDR_NONE */
70 #define INADDR_NONE ((unsigned long) -1)
71 #endif
72 
73 /**
74  * Usual socket setup.
75  *
76  * - Disallow SIGPIPE
77  * - Set close-on-exec flag
78  * - Call \ref socket_set_nonblocking() with given flag
79  */
80 bool socket_setup(int sock, bool non_block);
81 
82 /**
83  * Flip sockets non-blocking flag
84  */
85 bool socket_set_nonblocking(int sock, bool non_block);
86 
87 /**
88  * Set sockets keepalive flags.
89  *
90  * @param fd		TCP socket
91  * @param onoff		Whether to set keepalive on or off.
92  * @param keepidle	How long the socket must be idle before keepalive packets are sent
93  * @param keepintvl	How big period between consecutive keepalive packets.
94  * @param keepcnt	How many keepalive packets to send before considering socket dead.
95  */
96 bool socket_set_keepalive(int fd, int onoff, int keepidle, int keepintvl, int keepcnt);
97 
98 /**
99  * Convert struct sockaddr to stirng.
100  *
101  * Supports: ipv4, ipv5, unix sockets.
102  */
103 const char *sa2str(const struct sockaddr *sa, char *buf, size_t buflen);
104 
105 #ifndef HAVE_INET_NTOP
106 #undef inet_ntop
107 #define inet_ntop(a,b,c,d) usual_inet_ntop(a,b,c,d)
108 /** Compat: inet_ntop() */
109 const char *inet_ntop(int af, const void *src, char *dst, int cnt);
110 #endif
111 
112 #ifndef HAVE_INET_PTON
113 #undef inet_pton
114 #define inet_pton(a,b,c) usual_inet_pton(a,b,c)
115 /** Compat: inet_pton() */
116 int inet_pton(int af, const char *src, void *dst);
117 #endif
118 
119 #ifndef HAVE_GETPEEREID
120 #define getpeereid(a,b,c) compat_getpeereid(a,b,c)
121 /** Get user id of UNIX socket peer */
122 int getpeereid(int fd, uid_t *uid_p, gid_t *gid_p);
123 #endif
124 
125 #define getpeercreds(a,b,c,d) usual_getpeercreds(a,b,c,d)
126 /** Get info of UNIX socket peer */
127 int getpeercreds(int fd, uid_t *uid_p, gid_t *gid_p, pid_t *pid_p);
128 
129 #if !defined(HAVE_POLL)
130 #define POLLIN		(1 << 0)
131 #define POLLOUT		(1 << 1)
132 #define POLLHUP		(1 << 2)
133 #define POLLPRI		(1 << 3)
134 #define POLLNVAL	(1 << 4)
135 #define POLLERR		(1 << 5)
136 #define poll(a,b,c)	compat_poll(a,b,c)
137 struct pollfd {
138 	int fd;
139 	short events;
140 	short revents;
141 };
142 typedef unsigned long nfds_t;
143 /** Compat: select-based poll() */
144 int poll(struct pollfd *fds, nfds_t nfds, int timeout_ms);
145 #endif
146 
147 #ifdef WIN32
148 #define socketpair(a,b,c,d) win32_socketpair(a,b,c,d)
149 /** Compat: socketpair() for win32 */
150 int socketpair(int d, int typ, int proto, int sv[2]);
151 #endif
152 
153 #endif
154