1 #ifndef RUBY_SOCKET_H
2 #define RUBY_SOCKET_H 1
3
4 #include "ruby/ruby.h"
5 #include "ruby/io.h"
6 #include "ruby/thread.h"
7 #include "ruby/util.h"
8 #include "internal.h"
9 #include <stdio.h>
10 #include <sys/types.h>
11 #include <sys/stat.h>
12
13 #ifdef HAVE_UNISTD_H
14 # include <unistd.h>
15 #endif
16
17 #ifdef HAVE_SYS_UIO_H
18 # include <sys/uio.h>
19 #endif
20
21 #ifdef HAVE_XTI_H
22 # include <xti.h>
23 #endif
24
25 #ifdef _WIN32
26 # if defined(_MSC_VER)
27 # undef HAVE_TYPE_STRUCT_SOCKADDR_DL
28 # endif
29 /*
30 * FIXME: failures if we make nonblocking the default
31 * [ruby-core:89973] [ruby-core:89976] [ruby-core:89977] [Bug #14968]
32 */
33 # define RSOCK_NONBLOCK_DEFAULT (0)
34 #else
35 # define RSOCK_NONBLOCK_DEFAULT (0)
36 # include <sys/socket.h>
37 # include <netinet/in.h>
38 # ifdef HAVE_NETINET_IN_SYSTM_H
39 # include <netinet/in_systm.h>
40 # endif
41 # ifdef HAVE_NETINET_TCP_H
42 # include <netinet/tcp.h>
43 # endif
44 # ifdef HAVE_NETINET_TCP_FSM_H
45 # include <netinet/tcp_fsm.h>
46 # endif
47 # ifdef HAVE_NETINET_UDP_H
48 # include <netinet/udp.h>
49 # endif
50 # ifdef HAVE_ARPA_INET_H
51 # include <arpa/inet.h>
52 # endif
53 # include <netdb.h>
54 #endif
55
56 #ifdef HAVE_NETPACKET_PACKET_H
57 # include <netpacket/packet.h>
58 #endif
59 #ifdef HAVE_NET_ETHERNET_H
60 # include <net/ethernet.h>
61 #endif
62
63 #include <errno.h>
64
65 #ifdef HAVE_SYS_UN_H
66 # include <sys/un.h>
67 #endif
68
69 #if defined(HAVE_FCNTL)
70 # ifdef HAVE_SYS_SELECT_H
71 # include <sys/select.h>
72 # endif
73 # ifdef HAVE_SYS_TYPES_H
74 # include <sys/types.h>
75 # endif
76 # ifdef HAVE_SYS_TIME_H
77 # include <sys/time.h>
78 # endif
79 # ifdef HAVE_FCNTL_H
80 # include <fcntl.h>
81 # endif
82 #endif
83
84 #ifdef HAVE_IFADDRS_H
85 # ifdef __HAIKU__
86 # define _BSD_SOURCE
87 # endif
88 # include <ifaddrs.h>
89 #endif
90 #ifdef HAVE_SYS_IOCTL_H
91 # include <sys/ioctl.h>
92 #endif
93 #ifdef HAVE_SYS_SOCKIO_H
94 # include <sys/sockio.h>
95 #endif
96 #ifdef HAVE_NET_IF_H
97 # include <net/if.h>
98 #endif
99
100 #ifdef HAVE_SYS_PARAM_H
101 # include <sys/param.h>
102 #endif
103 #ifdef HAVE_SYS_UCRED_H
104 # include <sys/ucred.h>
105 #endif
106 #ifdef HAVE_UCRED_H
107 # include <ucred.h>
108 #endif
109 #ifdef HAVE_NET_IF_DL_H
110 # include <net/if_dl.h>
111 #endif
112
113 #ifndef HAVE_TYPE_SOCKLEN_T
114 typedef int socklen_t;
115 #endif
116
117 #ifdef NEED_IF_INDEXTONAME_DECL
118 char *if_indextoname(unsigned int, char *);
119 #endif
120 #ifdef NEED_IF_NAMETOINDEX_DECL
121 unsigned int if_nametoindex(const char *);
122 #endif
123
124 #define SOCKLEN_MAX \
125 (0 < (socklen_t)-1 ? \
126 ~(socklen_t)0 : \
127 (((((socklen_t)1) << (sizeof(socklen_t) * CHAR_BIT - 2)) - 1) * 2 + 1))
128
129 #ifndef RSTRING_SOCKLEN
130 # define RSTRING_SOCKLEN (socklen_t)RSTRING_LENINT
131 #endif
132
133 #ifndef EWOULDBLOCK
134 # define EWOULDBLOCK EAGAIN
135 #endif
136
137 /*
138 * workaround for NetBSD, OpenBSD and etc.
139 * The problem is since 4.4BSD-Lite.
140 * FreeBSD fix the problem at FreeBSD 2.2.0.
141 * NetBSD fix the problem at NetBSD 3.0 by kern/29624.
142 * OpenBSD fix the problem at OpenBSD 3.8.
143 */
144 #define pseudo_AF_FTIP pseudo_AF_RTIP
145
146 #ifndef HAVE_GETADDRINFO
147 # include "addrinfo.h"
148 #endif
149
150 #include "sockport.h"
151
152 #ifndef NI_MAXHOST
153 # define NI_MAXHOST 1025
154 #endif
155 #ifndef NI_MAXSERV
156 # define NI_MAXSERV 32
157 #endif
158
159 #ifdef AF_INET6
160 # define IS_IP_FAMILY(af) ((af) == AF_INET || (af) == AF_INET6)
161 #else
162 # define IS_IP_FAMILY(af) ((af) == AF_INET)
163 #endif
164
165 #ifndef IN6_IS_ADDR_UNIQUE_LOCAL
166 # define IN6_IS_ADDR_UNIQUE_LOCAL(a) (((a)->s6_addr[0] == 0xfc) || ((a)->s6_addr[0] == 0xfd))
167 #endif
168
169 #ifndef HAVE_TYPE_STRUCT_SOCKADDR_STORAGE
170 /*
171 * RFC 2553: protocol-independent placeholder for socket addresses
172 */
173 # define _SS_MAXSIZE 128
174 # define _SS_ALIGNSIZE (sizeof(double))
175 # define _SS_PAD1SIZE (_SS_ALIGNSIZE - sizeof(unsigned char) * 2)
176 # define _SS_PAD2SIZE (_SS_MAXSIZE - sizeof(unsigned char) * 2 - \
177 _SS_PAD1SIZE - _SS_ALIGNSIZE)
178
179 struct sockaddr_storage {
180 # ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
181 unsigned char ss_len; /* address length */
182 unsigned char ss_family; /* address family */
183 # else
184 unsigned short ss_family;
185 # endif
186 char __ss_pad1[_SS_PAD1SIZE];
187 double __ss_align; /* force desired structure storage alignment */
188 char __ss_pad2[_SS_PAD2SIZE];
189 };
190 #endif
191
192 typedef union {
193 struct sockaddr addr;
194 struct sockaddr_in in;
195 #ifdef AF_INET6
196 struct sockaddr_in6 in6;
197 #endif
198 #ifdef HAVE_TYPE_STRUCT_SOCKADDR_UN
199 struct sockaddr_un un;
200 #endif
201 #ifdef HAVE_TYPE_STRUCT_SOCKADDR_DL
202 struct sockaddr_dl dl; /* AF_LINK */
203 #endif
204 struct sockaddr_storage storage;
205 char place_holder[2048]; /* sockaddr_storage is not enough for Unix domain sockets on SunOS and Darwin. */
206 } union_sockaddr;
207
208 #ifdef __APPLE__
209 /*
210 * CMSG_ macros are broken on 64bit darwin, because __DARWIN_ALIGN
211 * aligns up to __darwin_size_t which is 64bit, but CMSG_DATA is
212 * 32bit-aligned.
213 */
214 # undef __DARWIN_ALIGNBYTES
215 # define __DARWIN_ALIGNBYTES (sizeof(unsigned int) - 1)
216 #endif
217
218 #if defined(_AIX)
219 # ifndef CMSG_SPACE
220 # define CMSG_SPACE(len) (_CMSG_ALIGN(sizeof(struct cmsghdr)) + _CMSG_ALIGN(len))
221 # endif
222 # ifndef CMSG_LEN
223 # define CMSG_LEN(len) (_CMSG_ALIGN(sizeof(struct cmsghdr)) + (len))
224 # endif
225 #endif
226
227 #define INET_CLIENT 0
228 #define INET_SERVER 1
229 #define INET_SOCKS 2
230
231 extern int rsock_do_not_reverse_lookup;
232 extern int rsock_cmsg_cloexec_state;
233 #define FMODE_NOREVLOOKUP 0x100
234
235 /* common socket families only */
236 #define FMODE_UNIX 0x00200000
237 #define FMODE_INET 0x00400000
238 #define FMODE_INET6 0x00800000
239 #define FMODE_SOCK (FMODE_UNIX|FMODE_INET|FMODE_INET6)
240
241 extern VALUE rb_cBasicSocket;
242 extern VALUE rb_cIPSocket;
243 extern VALUE rb_cTCPSocket;
244 extern VALUE rb_cTCPServer;
245 extern VALUE rb_cUDPSocket;
246 #ifdef HAVE_SYS_UN_H
247 extern VALUE rb_cUNIXSocket;
248 extern VALUE rb_cUNIXServer;
249 #endif
250 extern VALUE rb_cSocket;
251 extern VALUE rb_cAddrinfo;
252 extern VALUE rb_cSockOpt;
253
254 extern VALUE rb_eSocket;
255
256 #ifdef SOCKS
257 extern VALUE rb_cSOCKSSocket;
258 # ifdef SOCKS5
259 # include <socks.h>
260 # else
261 void SOCKSinit();
262 int Rconnect();
263 # endif
264 #endif
265
266 #include "constdefs.h"
267
268 #define BLOCKING_REGION_FD(func, arg) (long)rb_thread_io_blocking_region((func), (arg), (arg)->fd)
269
270 #define SockAddrStringValue(v) rsock_sockaddr_string_value(&(v))
271 #define SockAddrStringValuePtr(v) rsock_sockaddr_string_value_ptr(&(v))
272 #define SockAddrStringValueWithAddrinfo(v, rai_ret) rsock_sockaddr_string_value_with_addrinfo(&(v), &(rai_ret))
273 VALUE rsock_sockaddr_string_value(volatile VALUE *);
274 char *rsock_sockaddr_string_value_ptr(volatile VALUE *);
275 VALUE rsock_sockaddr_string_value_with_addrinfo(volatile VALUE *v, VALUE *ai_ret);
276
277 VALUE rb_check_sockaddr_string_type(VALUE);
278
279 NORETURN(void rsock_raise_socket_error(const char *, int));
280
281 int rsock_family_arg(VALUE domain);
282 int rsock_socktype_arg(VALUE type);
283 int rsock_level_arg(int family, VALUE level);
284 int rsock_optname_arg(int family, int level, VALUE optname);
285 int rsock_cmsg_type_arg(int family, int level, VALUE type);
286 int rsock_shutdown_how_arg(VALUE how);
287
288 int rsock_getfamily(rb_io_t *fptr);
289
290 struct rb_addrinfo {
291 struct addrinfo *ai;
292 int allocated_by_malloc;
293 };
294 int rb_getaddrinfo(const char *node, const char *service, const struct addrinfo *hints, struct rb_addrinfo **res);
295 void rb_freeaddrinfo(struct rb_addrinfo *ai);
296 VALUE rsock_freeaddrinfo(VALUE arg);
297 int rb_getnameinfo(const struct sockaddr *sa, socklen_t salen, char *host, size_t hostlen, char *serv, size_t servlen, int flags);
298 int rsock_fd_family(int fd);
299 struct rb_addrinfo *rsock_addrinfo(VALUE host, VALUE port, int family, int socktype, int flags);
300 struct rb_addrinfo *rsock_getaddrinfo(VALUE host, VALUE port, struct addrinfo *hints, int socktype_hack);
301 VALUE rsock_fd_socket_addrinfo(int fd, struct sockaddr *addr, socklen_t len);
302 VALUE rsock_io_socket_addrinfo(VALUE io, struct sockaddr *addr, socklen_t len);
303
304 VALUE rsock_addrinfo_new(struct sockaddr *addr, socklen_t len, int family, int socktype, int protocol, VALUE canonname, VALUE inspectname);
305 VALUE rsock_addrinfo_inspect_sockaddr(VALUE rai);
306
307 VALUE rsock_make_ipaddr(struct sockaddr *addr, socklen_t addrlen);
308 VALUE rsock_ipaddr(struct sockaddr *sockaddr, socklen_t sockaddrlen, int norevlookup);
309 VALUE rsock_make_hostent(VALUE host, struct rb_addrinfo *addr, VALUE (*ipaddr)(struct sockaddr *, socklen_t));
310 VALUE rsock_inspect_sockaddr(struct sockaddr *addr, socklen_t socklen, VALUE ret);
311 socklen_t rsock_sockaddr_len(struct sockaddr *addr);
312 VALUE rsock_sockaddr_obj(struct sockaddr *addr, socklen_t len);
313
314 int rsock_revlookup_flag(VALUE revlookup, int *norevlookup);
315
316 #ifdef HAVE_SYS_UN_H
317 VALUE rsock_unixpath_str(struct sockaddr_un *sockaddr, socklen_t len);
318 VALUE rsock_unixaddr(struct sockaddr_un *sockaddr, socklen_t len);
319 socklen_t rsock_unix_sockaddr_len(VALUE path);
320 #endif
321
322 int rsock_socket(int domain, int type, int proto);
323 int rsock_detect_cloexec(int fd);
324 VALUE rsock_init_sock(VALUE sock, int fd);
325 VALUE rsock_sock_s_socketpair(int argc, VALUE *argv, VALUE klass);
326 VALUE rsock_init_inetsock(VALUE sock, VALUE remote_host, VALUE remote_serv, VALUE local_host, VALUE local_serv, int type);
327 VALUE rsock_init_unixsock(VALUE sock, VALUE path, int server);
328
329 struct rsock_send_arg {
330 int fd, flags;
331 VALUE mesg;
332 struct sockaddr *to;
333 socklen_t tolen;
334 };
335
336 VALUE rsock_sendto_blocking(void *data);
337 VALUE rsock_send_blocking(void *data);
338 VALUE rsock_bsock_send(int argc, VALUE *argv, VALUE sock);
339
340 enum sock_recv_type {
341 RECV_RECV, /* BasicSocket#recv(no from) */
342 RECV_IP, /* IPSocket#recvfrom */
343 RECV_UNIX, /* UNIXSocket#recvfrom */
344 RECV_SOCKET /* Socket#recvfrom */
345 };
346
347 VALUE rsock_s_recvfrom_nonblock(VALUE sock, VALUE len, VALUE flg, VALUE str,
348 VALUE ex, enum sock_recv_type from);
349 VALUE rsock_s_recvfrom(VALUE sock, int argc, VALUE *argv, enum sock_recv_type from);
350
351 int rsock_connect(int fd, const struct sockaddr *sockaddr, int len, int socks);
352
353 VALUE rsock_s_accept(VALUE klass, int fd, struct sockaddr *sockaddr, socklen_t *len);
354 VALUE rsock_s_accept_nonblock(VALUE klass, VALUE ex, rb_io_t *fptr,
355 struct sockaddr *sockaddr, socklen_t *len);
356 VALUE rsock_sock_listen(VALUE sock, VALUE log);
357
358 VALUE rsock_sockopt_new(int family, int level, int optname, VALUE data);
359
360 #if defined(HAVE_SENDMSG)
361 VALUE rsock_bsock_sendmsg(VALUE sock, VALUE data, VALUE flags,
362 VALUE dest_sockaddr, VALUE controls);
363 VALUE rsock_bsock_sendmsg_nonblock(VALUE sock, VALUE data, VALUE flags,
364 VALUE dest_sockaddr, VALUE controls, VALUE ex);
365 #else
366 #define rsock_bsock_sendmsg rb_f_notimplement
367 #define rsock_bsock_sendmsg_nonblock rb_f_notimplement
368 #endif
369
370 #if defined(HAVE_RECVMSG)
371 VALUE rsock_bsock_recvmsg(VALUE sock, VALUE dlen, VALUE clen, VALUE flags,
372 VALUE scm_rights);
373 VALUE rsock_bsock_recvmsg_nonblock(VALUE sock, VALUE dlen, VALUE clen,
374 VALUE flags, VALUE scm_rights, VALUE ex);
375 ssize_t rsock_recvmsg(int socket, struct msghdr *message, int flags);
376 #else
377 #define rsock_bsock_recvmsg rb_f_notimplement
378 #define rsock_bsock_recvmsg_nonblock rb_f_notimplement
379 #endif
380
381 #ifdef HAVE_STRUCT_MSGHDR_MSG_CONTROL
382 void rsock_discard_cmsg_resource(struct msghdr *mh, int msg_peek_p);
383 #endif
384
385 void rsock_init_basicsocket(void);
386 void rsock_init_ipsocket(void);
387 void rsock_init_tcpsocket(void);
388 void rsock_init_tcpserver(void);
389 void rsock_init_sockssocket(void);
390 void rsock_init_udpsocket(void);
391 void rsock_init_unixsocket(void);
392 void rsock_init_unixserver(void);
393 void rsock_init_socket_constants(void);
394 void rsock_init_ancdata(void);
395 void rsock_init_addrinfo(void);
396 void rsock_init_sockopt(void);
397 void rsock_init_sockifaddr(void);
398 void rsock_init_socket_init(void);
399
400 NORETURN(void rsock_syserr_fail_host_port(int err, const char *, VALUE, VALUE));
401 NORETURN(void rsock_syserr_fail_path(int err, const char *, VALUE));
402 NORETURN(void rsock_syserr_fail_sockaddr(int err, const char *mesg, struct sockaddr *addr, socklen_t len));
403 NORETURN(void rsock_syserr_fail_raddrinfo(int err, const char *mesg, VALUE rai));
404 NORETURN(void rsock_syserr_fail_raddrinfo_or_sockaddr(int err, const char *mesg, VALUE addr, VALUE rai));
405
406 NORETURN(void rsock_sys_fail_host_port(const char *, VALUE, VALUE));
407 NORETURN(void rsock_sys_fail_path(const char *, VALUE));
408 NORETURN(void rsock_sys_fail_sockaddr(const char *, struct sockaddr *addr, socklen_t len));
409 NORETURN(void rsock_sys_fail_raddrinfo(const char *, VALUE rai));
410 NORETURN(void rsock_sys_fail_raddrinfo_or_sockaddr(const char *, VALUE addr, VALUE rai));
411
412 /*
413 * It is safe on Linux to attempt using a socket without waiting on it in
414 * all cases. For some syscalls (e.g. accept/accept4), blocking on the
415 * syscall instead of relying on select/poll allows the kernel to use
416 * "wake-one" behavior and avoid the thundering herd problem.
417 * This is likely safe on all other *nix-like systems, so this safe list
418 * can be expanded by interested parties.
419 */
420 #if defined(__linux__)
rsock_maybe_fd_writable(int fd)421 static inline int rsock_maybe_fd_writable(int fd) { return 1; }
rsock_maybe_wait_fd(int fd)422 static inline void rsock_maybe_wait_fd(int fd) { }
423 # ifdef MSG_DONTWAIT
424 # define MSG_DONTWAIT_RELIABLE 1
425 # endif
426 #else /* some systems (mswin/mingw) need these. ref: r36946 */
427 # define rsock_maybe_fd_writable(fd) rb_thread_fd_writable((fd))
428 # define rsock_maybe_wait_fd(fd) rb_thread_wait_fd((fd))
429 #endif
430
431 /*
432 * some OSes may support MSG_DONTWAIT inconsistently depending on socket
433 * type, we only expect Linux to support it consistently for all socket types.
434 */
435 #ifndef MSG_DONTWAIT_RELIABLE
436 # define MSG_DONTWAIT_RELIABLE 0
437 #endif
438
439 VALUE rsock_read_nonblock(VALUE sock, VALUE length, VALUE buf, VALUE ex);
440 VALUE rsock_write_nonblock(VALUE sock, VALUE buf, VALUE ex);
441
442 void rsock_make_fd_nonblock(int fd);
443
444 #if !defined HAVE_INET_NTOP && ! defined _WIN32
445 const char *inet_ntop(int, const void *, char *, size_t);
446 #elif defined __MINGW32__
447 # define inet_ntop(f,a,n,l) rb_w32_inet_ntop(f,a,n,l)
448 #elif defined _MSC_VER && RUBY_MSVCRT_VERSION < 90
449 const char *WSAAPI inet_ntop(int, const void *, char *, size_t);
450 #endif
451
452 #endif
453