Ruby  2.5.0dev(2017-10-22revision60238)
socket.c
Go to the documentation of this file.
1 /************************************************
2 
3  socket.c -
4 
5  created at: Thu Mar 31 12:21:29 JST 1994
6 
7  Copyright (C) 1993-2007 Yukihiro Matsumoto
8 
9 ************************************************/
10 
11 #include "rubysocket.h"
12 
13 static VALUE sym_wait_writable;
14 
15 static VALUE sock_s_unpack_sockaddr_in(VALUE, VALUE);
16 
17 void
18 rsock_sys_fail_host_port(const char *mesg, VALUE host, VALUE port)
19 {
20  rsock_syserr_fail_host_port(errno, mesg, host, port);
21 }
22 
23 void
24 rsock_syserr_fail_host_port(int err, const char *mesg, VALUE host, VALUE port)
25 {
26  VALUE message;
27 
28  message = rb_sprintf("%s for %+"PRIsVALUE" port % "PRIsVALUE"",
29  mesg, host, port);
30 
31  rb_syserr_fail_str(err, message);
32 }
33 
34 void
35 rsock_sys_fail_path(const char *mesg, VALUE path)
36 {
37  rsock_syserr_fail_path(errno, mesg, path);
38 }
39 
40 void
41 rsock_syserr_fail_path(int err, const char *mesg, VALUE path)
42 {
43  VALUE message;
44 
45  if (RB_TYPE_P(path, T_STRING)) {
46  message = rb_sprintf("%s for % "PRIsVALUE"", mesg, path);
47  rb_syserr_fail_str(err, message);
48  }
49  else {
50  rb_syserr_fail(err, mesg);
51  }
52 }
53 
54 void
55 rsock_sys_fail_sockaddr(const char *mesg, struct sockaddr *addr, socklen_t len)
56 {
57  rsock_syserr_fail_sockaddr(errno, mesg, addr, len);
58 }
59 
60 void
61 rsock_syserr_fail_sockaddr(int err, const char *mesg, struct sockaddr *addr, socklen_t len)
62 {
63  VALUE rai;
64 
65  rai = rsock_addrinfo_new(addr, len, PF_UNSPEC, 0, 0, Qnil, Qnil);
66 
67  rsock_syserr_fail_raddrinfo(err, mesg, rai);
68 }
69 
70 void
71 rsock_sys_fail_raddrinfo(const char *mesg, VALUE rai)
72 {
74 }
75 
76 void
77 rsock_syserr_fail_raddrinfo(int err, const char *mesg, VALUE rai)
78 {
79  VALUE str, message;
80 
82  message = rb_sprintf("%s for %"PRIsVALUE"", mesg, str);
83 
84  rb_syserr_fail_str(err, message);
85 }
86 
87 void
88 rsock_sys_fail_raddrinfo_or_sockaddr(const char *mesg, VALUE addr, VALUE rai)
89 {
91 }
92 
93 void
94 rsock_syserr_fail_raddrinfo_or_sockaddr(int err, const char *mesg, VALUE addr, VALUE rai)
95 {
96  if (NIL_P(rai)) {
97  StringValue(addr);
98 
100  (struct sockaddr *)RSTRING_PTR(addr),
101  (socklen_t)RSTRING_LEN(addr)); /* overflow should be checked already */
102  }
103  else
104  rsock_syserr_fail_raddrinfo(err, mesg, rai);
105 }
106 
107 static void
108 setup_domain_and_type(VALUE domain, int *dv, VALUE type, int *tv)
109 {
110  *dv = rsock_family_arg(domain);
111  *tv = rsock_socktype_arg(type);
112 }
113 
114 /*
115  * call-seq:
116  * Socket.new(domain, socktype [, protocol]) => socket
117  *
118  * Creates a new socket object.
119  *
120  * _domain_ should be a communications domain such as: :INET, :INET6, :UNIX, etc.
121  *
122  * _socktype_ should be a socket type such as: :STREAM, :DGRAM, :RAW, etc.
123  *
124  * _protocol_ is optional and should be a protocol defined in the domain.
125  * If protocol is not given, 0 is used internally.
126  *
127  * Socket.new(:INET, :STREAM) # TCP socket
128  * Socket.new(:INET, :DGRAM) # UDP socket
129  * Socket.new(:UNIX, :STREAM) # UNIX stream socket
130  * Socket.new(:UNIX, :DGRAM) # UNIX datagram socket
131  */
132 static VALUE
133 sock_initialize(int argc, VALUE *argv, VALUE sock)
134 {
135  VALUE domain, type, protocol;
136  int fd;
137  int d, t;
138 
139  rb_scan_args(argc, argv, "21", &domain, &type, &protocol);
140  if (NIL_P(protocol))
141  protocol = INT2FIX(0);
142 
143  setup_domain_and_type(domain, &d, type, &t);
144  fd = rsock_socket(d, t, NUM2INT(protocol));
145  if (fd < 0) rb_sys_fail("socket(2)");
146 
147  return rsock_init_sock(sock, fd);
148 }
149 
150 #if defined HAVE_SOCKETPAIR
151 static VALUE
152 io_call_close(VALUE io)
153 {
154  return rb_funcallv(io, rb_intern("close"), 0, 0);
155 }
156 
157 static VALUE
158 io_close(VALUE io)
159 {
160  return rb_rescue(io_call_close, io, 0, 0);
161 }
162 
163 static VALUE
164 pair_yield(VALUE pair)
165 {
166  return rb_ensure(rb_yield, pair, io_close, rb_ary_entry(pair, 1));
167 }
168 #endif
169 
170 #if defined HAVE_SOCKETPAIR
171 
172 #ifdef SOCK_CLOEXEC
173 static int
174 rsock_socketpair0(int domain, int type, int protocol, int sv[2])
175 {
176  int ret;
177  static int cloexec_state = -1; /* <0: unknown, 0: ignored, >0: working */
178 
179  if (cloexec_state > 0) { /* common path, if SOCK_CLOEXEC is defined */
180  ret = socketpair(domain, type|SOCK_CLOEXEC, protocol, sv);
181  if (ret == 0 && (sv[0] <= 2 || sv[1] <= 2)) {
182  goto fix_cloexec; /* highly unlikely */
183  }
184  goto update_max_fd;
185  }
186  else if (cloexec_state < 0) { /* usually runs once only for detection */
187  ret = socketpair(domain, type|SOCK_CLOEXEC, protocol, sv);
188  if (ret == 0) {
189  cloexec_state = rsock_detect_cloexec(sv[0]);
190  if ((cloexec_state == 0) || (sv[0] <= 2 || sv[1] <= 2))
191  goto fix_cloexec;
192  goto update_max_fd;
193  }
194  else if (ret == -1 && errno == EINVAL) {
195  /* SOCK_CLOEXEC is available since Linux 2.6.27. Linux 2.6.18 fails with EINVAL */
196  ret = socketpair(domain, type, protocol, sv);
197  if (ret != -1) {
198  /* The reason of EINVAL may be other than SOCK_CLOEXEC.
199  * So disable SOCK_CLOEXEC only if socketpair() succeeds without SOCK_CLOEXEC.
200  * Ex. Socket.pair(:UNIX, 0xff) fails with EINVAL.
201  */
202  cloexec_state = 0;
203  }
204  }
205  }
206  else { /* cloexec_state == 0 */
207  ret = socketpair(domain, type, protocol, sv);
208  }
209  if (ret == -1) {
210  return -1;
211  }
212 
213 fix_cloexec:
216 
217 update_max_fd:
218  rb_update_max_fd(sv[0]);
219  rb_update_max_fd(sv[1]);
220 
221  return ret;
222 }
223 #else /* !SOCK_CLOEXEC */
224 static int
225 rsock_socketpair0(int domain, int type, int protocol, int sv[2])
226 {
227  int ret = socketpair(domain, type, protocol, sv);
228 
229  if (ret == -1)
230  return -1;
231 
232  rb_fd_fix_cloexec(sv[0]);
233  rb_fd_fix_cloexec(sv[1]);
234  return ret;
235 }
236 #endif /* !SOCK_CLOEXEC */
237 
238 static int
239 rsock_socketpair(int domain, int type, int protocol, int sv[2])
240 {
241  int ret;
242 
243  ret = rsock_socketpair0(domain, type, protocol, sv);
244  if (ret < 0 && rb_gc_for_fd(errno)) {
245  ret = rsock_socketpair0(domain, type, protocol, sv);
246  }
247 
248  return ret;
249 }
250 
251 /*
252  * call-seq:
253  * Socket.pair(domain, type, protocol) => [socket1, socket2]
254  * Socket.socketpair(domain, type, protocol) => [socket1, socket2]
255  *
256  * Creates a pair of sockets connected each other.
257  *
258  * _domain_ should be a communications domain such as: :INET, :INET6, :UNIX, etc.
259  *
260  * _socktype_ should be a socket type such as: :STREAM, :DGRAM, :RAW, etc.
261  *
262  * _protocol_ should be a protocol defined in the domain,
263  * defaults to 0 for the domain.
264  *
265  * s1, s2 = Socket.pair(:UNIX, :STREAM, 0)
266  * s1.send "a", 0
267  * s1.send "b", 0
268  * s1.close
269  * p s2.recv(10) #=> "ab"
270  * p s2.recv(10) #=> ""
271  * p s2.recv(10) #=> ""
272  *
273  * s1, s2 = Socket.pair(:UNIX, :DGRAM, 0)
274  * s1.send "a", 0
275  * s1.send "b", 0
276  * p s2.recv(10) #=> "a"
277  * p s2.recv(10) #=> "b"
278  *
279  */
280 VALUE
282 {
283  VALUE domain, type, protocol;
284  int d, t, p, sp[2];
285  int ret;
286  VALUE s1, s2, r;
287 
288  rb_scan_args(argc, argv, "21", &domain, &type, &protocol);
289  if (NIL_P(protocol))
290  protocol = INT2FIX(0);
291 
292  setup_domain_and_type(domain, &d, type, &t);
293  p = NUM2INT(protocol);
294  ret = rsock_socketpair(d, t, p, sp);
295  if (ret < 0) {
296  rb_sys_fail("socketpair(2)");
297  }
298 
299  s1 = rsock_init_sock(rb_obj_alloc(klass), sp[0]);
300  s2 = rsock_init_sock(rb_obj_alloc(klass), sp[1]);
301  r = rb_assoc_new(s1, s2);
302  if (rb_block_given_p()) {
303  return rb_ensure(pair_yield, r, io_close, s1);
304  }
305  return r;
306 }
307 #else
308 #define rsock_sock_s_socketpair rb_f_notimplement
309 #endif
310 
311 /*
312  * call-seq:
313  * socket.connect(remote_sockaddr) => 0
314  *
315  * Requests a connection to be made on the given +remote_sockaddr+. Returns 0 if
316  * successful, otherwise an exception is raised.
317  *
318  * === Parameter
319  * * +remote_sockaddr+ - the +struct+ sockaddr contained in a string or Addrinfo object
320  *
321  * === Example:
322  * # Pull down Google's web page
323  * require 'socket'
324  * include Socket::Constants
325  * socket = Socket.new( AF_INET, SOCK_STREAM, 0 )
326  * sockaddr = Socket.pack_sockaddr_in( 80, 'www.google.com' )
327  * socket.connect( sockaddr )
328  * socket.write( "GET / HTTP/1.0\r\n\r\n" )
329  * results = socket.read
330  *
331  * === Unix-based Exceptions
332  * On unix-based systems the following system exceptions may be raised if
333  * the call to _connect_ fails:
334  * * Errno::EACCES - search permission is denied for a component of the prefix
335  * path or write access to the +socket+ is denied
336  * * Errno::EADDRINUSE - the _sockaddr_ is already in use
337  * * Errno::EADDRNOTAVAIL - the specified _sockaddr_ is not available from the
338  * local machine
339  * * Errno::EAFNOSUPPORT - the specified _sockaddr_ is not a valid address for
340  * the address family of the specified +socket+
341  * * Errno::EALREADY - a connection is already in progress for the specified
342  * socket
343  * * Errno::EBADF - the +socket+ is not a valid file descriptor
344  * * Errno::ECONNREFUSED - the target _sockaddr_ was not listening for connections
345  * refused the connection request
346  * * Errno::ECONNRESET - the remote host reset the connection request
347  * * Errno::EFAULT - the _sockaddr_ cannot be accessed
348  * * Errno::EHOSTUNREACH - the destination host cannot be reached (probably
349  * because the host is down or a remote router cannot reach it)
350  * * Errno::EINPROGRESS - the O_NONBLOCK is set for the +socket+ and the
351  * connection cannot be immediately established; the connection will be
352  * established asynchronously
353  * * Errno::EINTR - the attempt to establish the connection was interrupted by
354  * delivery of a signal that was caught; the connection will be established
355  * asynchronously
356  * * Errno::EISCONN - the specified +socket+ is already connected
357  * * Errno::EINVAL - the address length used for the _sockaddr_ is not a valid
358  * length for the address family or there is an invalid family in _sockaddr_
359  * * Errno::ENAMETOOLONG - the pathname resolved had a length which exceeded
360  * PATH_MAX
361  * * Errno::ENETDOWN - the local interface used to reach the destination is down
362  * * Errno::ENETUNREACH - no route to the network is present
363  * * Errno::ENOBUFS - no buffer space is available
364  * * Errno::ENOSR - there were insufficient STREAMS resources available to
365  * complete the operation
366  * * Errno::ENOTSOCK - the +socket+ argument does not refer to a socket
367  * * Errno::EOPNOTSUPP - the calling +socket+ is listening and cannot be connected
368  * * Errno::EPROTOTYPE - the _sockaddr_ has a different type than the socket
369  * bound to the specified peer address
370  * * Errno::ETIMEDOUT - the attempt to connect time out before a connection
371  * was made.
372  *
373  * On unix-based systems if the address family of the calling +socket+ is
374  * AF_UNIX the follow exceptions may be raised if the call to _connect_
375  * fails:
376  * * Errno::EIO - an i/o error occurred while reading from or writing to the
377  * file system
378  * * Errno::ELOOP - too many symbolic links were encountered in translating
379  * the pathname in _sockaddr_
380  * * Errno::ENAMETOOLLONG - a component of a pathname exceeded NAME_MAX
381  * characters, or an entire pathname exceeded PATH_MAX characters
382  * * Errno::ENOENT - a component of the pathname does not name an existing file
383  * or the pathname is an empty string
384  * * Errno::ENOTDIR - a component of the path prefix of the pathname in _sockaddr_
385  * is not a directory
386  *
387  * === Windows Exceptions
388  * On Windows systems the following system exceptions may be raised if
389  * the call to _connect_ fails:
390  * * Errno::ENETDOWN - the network is down
391  * * Errno::EADDRINUSE - the socket's local address is already in use
392  * * Errno::EINTR - the socket was cancelled
393  * * Errno::EINPROGRESS - a blocking socket is in progress or the service provider
394  * is still processing a callback function. Or a nonblocking connect call is
395  * in progress on the +socket+.
396  * * Errno::EALREADY - see Errno::EINVAL
397  * * Errno::EADDRNOTAVAIL - the remote address is not a valid address, such as
398  * ADDR_ANY TODO check ADDRANY TO INADDR_ANY
399  * * Errno::EAFNOSUPPORT - addresses in the specified family cannot be used with
400  * with this +socket+
401  * * Errno::ECONNREFUSED - the target _sockaddr_ was not listening for connections
402  * refused the connection request
403  * * Errno::EFAULT - the socket's internal address or address length parameter
404  * is too small or is not a valid part of the user space address
405  * * Errno::EINVAL - the +socket+ is a listening socket
406  * * Errno::EISCONN - the +socket+ is already connected
407  * * Errno::ENETUNREACH - the network cannot be reached from this host at this time
408  * * Errno::EHOSTUNREACH - no route to the network is present
409  * * Errno::ENOBUFS - no buffer space is available
410  * * Errno::ENOTSOCK - the +socket+ argument does not refer to a socket
411  * * Errno::ETIMEDOUT - the attempt to connect time out before a connection
412  * was made.
413  * * Errno::EWOULDBLOCK - the socket is marked as nonblocking and the
414  * connection cannot be completed immediately
415  * * Errno::EACCES - the attempt to connect the datagram socket to the
416  * broadcast address failed
417  *
418  * === See
419  * * connect manual pages on unix-based systems
420  * * connect function in Microsoft's Winsock functions reference
421  */
422 static VALUE
423 sock_connect(VALUE sock, VALUE addr)
424 {
425  VALUE rai;
426  rb_io_t *fptr;
427  int fd, n;
428 
430  addr = rb_str_new4(addr);
431  GetOpenFile(sock, fptr);
432  fd = fptr->fd;
433  n = rsock_connect(fd, (struct sockaddr*)RSTRING_PTR(addr), RSTRING_SOCKLEN(addr), 0);
434  if (n < 0) {
435  rsock_sys_fail_raddrinfo_or_sockaddr("connect(2)", addr, rai);
436  }
437 
438  return INT2FIX(n);
439 }
440 
441 /* :nodoc: */
442 static VALUE
443 sock_connect_nonblock(VALUE sock, VALUE addr, VALUE ex)
444 {
445  VALUE rai;
446  rb_io_t *fptr;
447  int n;
448 
450  addr = rb_str_new4(addr);
451  GetOpenFile(sock, fptr);
452  rb_io_set_nonblock(fptr);
453  n = connect(fptr->fd, (struct sockaddr*)RSTRING_PTR(addr), RSTRING_SOCKLEN(addr));
454  if (n < 0) {
455  int e = errno;
456  if (e == EINPROGRESS) {
457  if (ex == Qfalse) {
458  return sym_wait_writable;
459  }
460  rb_readwrite_syserr_fail(RB_IO_WAIT_WRITABLE, e, "connect(2) would block");
461  }
462  if (e == EISCONN) {
463  if (ex == Qfalse) {
464  return INT2FIX(0);
465  }
466  }
467  rsock_syserr_fail_raddrinfo_or_sockaddr(e, "connect(2)", addr, rai);
468  }
469 
470  return INT2FIX(n);
471 }
472 
473 /*
474  * call-seq:
475  * socket.bind(local_sockaddr) => 0
476  *
477  * Binds to the given local address.
478  *
479  * === Parameter
480  * * +local_sockaddr+ - the +struct+ sockaddr contained in a string or an Addrinfo object
481  *
482  * === Example
483  * require 'socket'
484  *
485  * # use Addrinfo
486  * socket = Socket.new(:INET, :STREAM, 0)
487  * socket.bind(Addrinfo.tcp("127.0.0.1", 2222))
488  * p socket.local_address #=> #<Addrinfo: 127.0.0.1:2222 TCP>
489  *
490  * # use struct sockaddr
491  * include Socket::Constants
492  * socket = Socket.new( AF_INET, SOCK_STREAM, 0 )
493  * sockaddr = Socket.pack_sockaddr_in( 2200, 'localhost' )
494  * socket.bind( sockaddr )
495  *
496  * === Unix-based Exceptions
497  * On unix-based based systems the following system exceptions may be raised if
498  * the call to _bind_ fails:
499  * * Errno::EACCES - the specified _sockaddr_ is protected and the current
500  * user does not have permission to bind to it
501  * * Errno::EADDRINUSE - the specified _sockaddr_ is already in use
502  * * Errno::EADDRNOTAVAIL - the specified _sockaddr_ is not available from the
503  * local machine
504  * * Errno::EAFNOSUPPORT - the specified _sockaddr_ is not a valid address for
505  * the family of the calling +socket+
506  * * Errno::EBADF - the _sockaddr_ specified is not a valid file descriptor
507  * * Errno::EFAULT - the _sockaddr_ argument cannot be accessed
508  * * Errno::EINVAL - the +socket+ is already bound to an address, and the
509  * protocol does not support binding to the new _sockaddr_ or the +socket+
510  * has been shut down.
511  * * Errno::EINVAL - the address length is not a valid length for the address
512  * family
513  * * Errno::ENAMETOOLONG - the pathname resolved had a length which exceeded
514  * PATH_MAX
515  * * Errno::ENOBUFS - no buffer space is available
516  * * Errno::ENOSR - there were insufficient STREAMS resources available to
517  * complete the operation
518  * * Errno::ENOTSOCK - the +socket+ does not refer to a socket
519  * * Errno::EOPNOTSUPP - the socket type of the +socket+ does not support
520  * binding to an address
521  *
522  * On unix-based based systems if the address family of the calling +socket+ is
523  * Socket::AF_UNIX the follow exceptions may be raised if the call to _bind_
524  * fails:
525  * * Errno::EACCES - search permission is denied for a component of the prefix
526  * path or write access to the +socket+ is denied
527  * * Errno::EDESTADDRREQ - the _sockaddr_ argument is a null pointer
528  * * Errno::EISDIR - same as Errno::EDESTADDRREQ
529  * * Errno::EIO - an i/o error occurred
530  * * Errno::ELOOP - too many symbolic links were encountered in translating
531  * the pathname in _sockaddr_
532  * * Errno::ENAMETOOLLONG - a component of a pathname exceeded NAME_MAX
533  * characters, or an entire pathname exceeded PATH_MAX characters
534  * * Errno::ENOENT - a component of the pathname does not name an existing file
535  * or the pathname is an empty string
536  * * Errno::ENOTDIR - a component of the path prefix of the pathname in _sockaddr_
537  * is not a directory
538  * * Errno::EROFS - the name would reside on a read only filesystem
539  *
540  * === Windows Exceptions
541  * On Windows systems the following system exceptions may be raised if
542  * the call to _bind_ fails:
543  * * Errno::ENETDOWN-- the network is down
544  * * Errno::EACCES - the attempt to connect the datagram socket to the
545  * broadcast address failed
546  * * Errno::EADDRINUSE - the socket's local address is already in use
547  * * Errno::EADDRNOTAVAIL - the specified address is not a valid address for this
548  * computer
549  * * Errno::EFAULT - the socket's internal address or address length parameter
550  * is too small or is not a valid part of the user space addressed
551  * * Errno::EINVAL - the +socket+ is already bound to an address
552  * * Errno::ENOBUFS - no buffer space is available
553  * * Errno::ENOTSOCK - the +socket+ argument does not refer to a socket
554  *
555  * === See
556  * * bind manual pages on unix-based systems
557  * * bind function in Microsoft's Winsock functions reference
558  */
559 static VALUE
560 sock_bind(VALUE sock, VALUE addr)
561 {
562  VALUE rai;
563  rb_io_t *fptr;
564 
566  GetOpenFile(sock, fptr);
567  if (bind(fptr->fd, (struct sockaddr*)RSTRING_PTR(addr), RSTRING_SOCKLEN(addr)) < 0)
568  rsock_sys_fail_raddrinfo_or_sockaddr("bind(2)", addr, rai);
569 
570  return INT2FIX(0);
571 }
572 
573 /*
574  * call-seq:
575  * socket.listen( int ) => 0
576  *
577  * Listens for connections, using the specified +int+ as the backlog. A call
578  * to _listen_ only applies if the +socket+ is of type SOCK_STREAM or
579  * SOCK_SEQPACKET.
580  *
581  * === Parameter
582  * * +backlog+ - the maximum length of the queue for pending connections.
583  *
584  * === Example 1
585  * require 'socket'
586  * include Socket::Constants
587  * socket = Socket.new( AF_INET, SOCK_STREAM, 0 )
588  * sockaddr = Socket.pack_sockaddr_in( 2200, 'localhost' )
589  * socket.bind( sockaddr )
590  * socket.listen( 5 )
591  *
592  * === Example 2 (listening on an arbitrary port, unix-based systems only):
593  * require 'socket'
594  * include Socket::Constants
595  * socket = Socket.new( AF_INET, SOCK_STREAM, 0 )
596  * socket.listen( 1 )
597  *
598  * === Unix-based Exceptions
599  * On unix based systems the above will work because a new +sockaddr+ struct
600  * is created on the address ADDR_ANY, for an arbitrary port number as handed
601  * off by the kernel. It will not work on Windows, because Windows requires that
602  * the +socket+ is bound by calling _bind_ before it can _listen_.
603  *
604  * If the _backlog_ amount exceeds the implementation-dependent maximum
605  * queue length, the implementation's maximum queue length will be used.
606  *
607  * On unix-based based systems the following system exceptions may be raised if the
608  * call to _listen_ fails:
609  * * Errno::EBADF - the _socket_ argument is not a valid file descriptor
610  * * Errno::EDESTADDRREQ - the _socket_ is not bound to a local address, and
611  * the protocol does not support listening on an unbound socket
612  * * Errno::EINVAL - the _socket_ is already connected
613  * * Errno::ENOTSOCK - the _socket_ argument does not refer to a socket
614  * * Errno::EOPNOTSUPP - the _socket_ protocol does not support listen
615  * * Errno::EACCES - the calling process does not have appropriate privileges
616  * * Errno::EINVAL - the _socket_ has been shut down
617  * * Errno::ENOBUFS - insufficient resources are available in the system to
618  * complete the call
619  *
620  * === Windows Exceptions
621  * On Windows systems the following system exceptions may be raised if
622  * the call to _listen_ fails:
623  * * Errno::ENETDOWN - the network is down
624  * * Errno::EADDRINUSE - the socket's local address is already in use. This
625  * usually occurs during the execution of _bind_ but could be delayed
626  * if the call to _bind_ was to a partially wildcard address (involving
627  * ADDR_ANY) and if a specific address needs to be committed at the
628  * time of the call to _listen_
629  * * Errno::EINPROGRESS - a Windows Sockets 1.1 call is in progress or the
630  * service provider is still processing a callback function
631  * * Errno::EINVAL - the +socket+ has not been bound with a call to _bind_.
632  * * Errno::EISCONN - the +socket+ is already connected
633  * * Errno::EMFILE - no more socket descriptors are available
634  * * Errno::ENOBUFS - no buffer space is available
635  * * Errno::ENOTSOC - +socket+ is not a socket
636  * * Errno::EOPNOTSUPP - the referenced +socket+ is not a type that supports
637  * the _listen_ method
638  *
639  * === See
640  * * listen manual pages on unix-based systems
641  * * listen function in Microsoft's Winsock functions reference
642  */
643 VALUE
645 {
646  rb_io_t *fptr;
647  int backlog;
648 
649  backlog = NUM2INT(log);
650  GetOpenFile(sock, fptr);
651  if (listen(fptr->fd, backlog) < 0)
652  rb_sys_fail("listen(2)");
653 
654  return INT2FIX(0);
655 }
656 
657 /*
658  * call-seq:
659  * socket.recvfrom(maxlen) => [mesg, sender_addrinfo]
660  * socket.recvfrom(maxlen, flags) => [mesg, sender_addrinfo]
661  *
662  * Receives up to _maxlen_ bytes from +socket+. _flags_ is zero or more
663  * of the +MSG_+ options. The first element of the results, _mesg_, is the data
664  * received. The second element, _sender_addrinfo_, contains protocol-specific
665  * address information of the sender.
666  *
667  * === Parameters
668  * * +maxlen+ - the maximum number of bytes to receive from the socket
669  * * +flags+ - zero or more of the +MSG_+ options
670  *
671  * === Example
672  * # In one file, start this first
673  * require 'socket'
674  * include Socket::Constants
675  * socket = Socket.new( AF_INET, SOCK_STREAM, 0 )
676  * sockaddr = Socket.pack_sockaddr_in( 2200, 'localhost' )
677  * socket.bind( sockaddr )
678  * socket.listen( 5 )
679  * client, client_addrinfo = socket.accept
680  * data = client.recvfrom( 20 )[0].chomp
681  * puts "I only received 20 bytes '#{data}'"
682  * sleep 1
683  * socket.close
684  *
685  * # In another file, start this second
686  * require 'socket'
687  * include Socket::Constants
688  * socket = Socket.new( AF_INET, SOCK_STREAM, 0 )
689  * sockaddr = Socket.pack_sockaddr_in( 2200, 'localhost' )
690  * socket.connect( sockaddr )
691  * socket.puts "Watch this get cut short!"
692  * socket.close
693  *
694  * === Unix-based Exceptions
695  * On unix-based based systems the following system exceptions may be raised if the
696  * call to _recvfrom_ fails:
697  * * Errno::EAGAIN - the +socket+ file descriptor is marked as O_NONBLOCK and no
698  * data is waiting to be received; or MSG_OOB is set and no out-of-band data
699  * is available and either the +socket+ file descriptor is marked as
700  * O_NONBLOCK or the +socket+ does not support blocking to wait for
701  * out-of-band-data
702  * * Errno::EWOULDBLOCK - see Errno::EAGAIN
703  * * Errno::EBADF - the +socket+ is not a valid file descriptor
704  * * Errno::ECONNRESET - a connection was forcibly closed by a peer
705  * * Errno::EFAULT - the socket's internal buffer, address or address length
706  * cannot be accessed or written
707  * * Errno::EINTR - a signal interrupted _recvfrom_ before any data was available
708  * * Errno::EINVAL - the MSG_OOB flag is set and no out-of-band data is available
709  * * Errno::EIO - an i/o error occurred while reading from or writing to the
710  * filesystem
711  * * Errno::ENOBUFS - insufficient resources were available in the system to
712  * perform the operation
713  * * Errno::ENOMEM - insufficient memory was available to fulfill the request
714  * * Errno::ENOSR - there were insufficient STREAMS resources available to
715  * complete the operation
716  * * Errno::ENOTCONN - a receive is attempted on a connection-mode socket that
717  * is not connected
718  * * Errno::ENOTSOCK - the +socket+ does not refer to a socket
719  * * Errno::EOPNOTSUPP - the specified flags are not supported for this socket type
720  * * Errno::ETIMEDOUT - the connection timed out during connection establishment
721  * or due to a transmission timeout on an active connection
722  *
723  * === Windows Exceptions
724  * On Windows systems the following system exceptions may be raised if
725  * the call to _recvfrom_ fails:
726  * * Errno::ENETDOWN - the network is down
727  * * Errno::EFAULT - the internal buffer and from parameters on +socket+ are not
728  * part of the user address space, or the internal fromlen parameter is
729  * too small to accommodate the peer address
730  * * Errno::EINTR - the (blocking) call was cancelled by an internal call to
731  * the WinSock function WSACancelBlockingCall
732  * * Errno::EINPROGRESS - a blocking Windows Sockets 1.1 call is in progress or
733  * the service provider is still processing a callback function
734  * * Errno::EINVAL - +socket+ has not been bound with a call to _bind_, or an
735  * unknown flag was specified, or MSG_OOB was specified for a socket with
736  * SO_OOBINLINE enabled, or (for byte stream-style sockets only) the internal
737  * len parameter on +socket+ was zero or negative
738  * * Errno::EISCONN - +socket+ is already connected. The call to _recvfrom_ is
739  * not permitted with a connected socket on a socket that is connection
740  * oriented or connectionless.
741  * * Errno::ENETRESET - the connection has been broken due to the keep-alive
742  * activity detecting a failure while the operation was in progress.
743  * * Errno::EOPNOTSUPP - MSG_OOB was specified, but +socket+ is not stream-style
744  * such as type SOCK_STREAM. OOB data is not supported in the communication
745  * domain associated with +socket+, or +socket+ is unidirectional and
746  * supports only send operations
747  * * Errno::ESHUTDOWN - +socket+ has been shutdown. It is not possible to
748  * call _recvfrom_ on a socket after _shutdown_ has been invoked.
749  * * Errno::EWOULDBLOCK - +socket+ is marked as nonblocking and a call to
750  * _recvfrom_ would block.
751  * * Errno::EMSGSIZE - the message was too large to fit into the specified buffer
752  * and was truncated.
753  * * Errno::ETIMEDOUT - the connection has been dropped, because of a network
754  * failure or because the system on the other end went down without
755  * notice
756  * * Errno::ECONNRESET - the virtual circuit was reset by the remote side
757  * executing a hard or abortive close. The application should close the
758  * socket; it is no longer usable. On a UDP-datagram socket this error
759  * indicates a previous send operation resulted in an ICMP Port Unreachable
760  * message.
761  */
762 static VALUE
763 sock_recvfrom(int argc, VALUE *argv, VALUE sock)
764 {
765  return rsock_s_recvfrom(sock, argc, argv, RECV_SOCKET);
766 }
767 
768 /* :nodoc: */
769 static VALUE
770 sock_recvfrom_nonblock(VALUE sock, VALUE len, VALUE flg, VALUE str, VALUE ex)
771 {
772  return rsock_s_recvfrom_nonblock(sock, len, flg, str, ex, RECV_SOCKET);
773 }
774 
775 /*
776  * call-seq:
777  * socket.accept => [client_socket, client_addrinfo]
778  *
779  * Accepts a next connection.
780  * Returns a new Socket object and Addrinfo object.
781  *
782  * serv = Socket.new(:INET, :STREAM, 0)
783  * serv.listen(5)
784  * c = Socket.new(:INET, :STREAM, 0)
785  * c.connect(serv.connect_address)
786  * p serv.accept #=> [#<Socket:fd 6>, #<Addrinfo: 127.0.0.1:48555 TCP>]
787  *
788  */
789 static VALUE
790 sock_accept(VALUE sock)
791 {
792  rb_io_t *fptr;
793  VALUE sock2;
795  socklen_t len = (socklen_t)sizeof buf;
796 
797  GetOpenFile(sock, fptr);
798  sock2 = rsock_s_accept(rb_cSocket,fptr->fd,&buf.addr,&len);
799 
800  return rb_assoc_new(sock2, rsock_io_socket_addrinfo(sock2, &buf.addr, len));
801 }
802 
803 /* :nodoc: */
804 static VALUE
805 sock_accept_nonblock(VALUE sock, VALUE ex)
806 {
807  rb_io_t *fptr;
808  VALUE sock2;
810  struct sockaddr *addr = &buf.addr;
811  socklen_t len = (socklen_t)sizeof buf;
812 
813  GetOpenFile(sock, fptr);
814  sock2 = rsock_s_accept_nonblock(rb_cSocket, ex, fptr, addr, &len);
815 
816  if (SYMBOL_P(sock2)) /* :wait_readable */
817  return sock2;
818  return rb_assoc_new(sock2, rsock_io_socket_addrinfo(sock2, &buf.addr, len));
819 }
820 
821 /*
822  * call-seq:
823  * socket.sysaccept => [client_socket_fd, client_addrinfo]
824  *
825  * Accepts an incoming connection returning an array containing the (integer)
826  * file descriptor for the incoming connection, _client_socket_fd_,
827  * and an Addrinfo, _client_addrinfo_.
828  *
829  * === Example
830  * # In one script, start this first
831  * require 'socket'
832  * include Socket::Constants
833  * socket = Socket.new( AF_INET, SOCK_STREAM, 0 )
834  * sockaddr = Socket.pack_sockaddr_in( 2200, 'localhost' )
835  * socket.bind( sockaddr )
836  * socket.listen( 5 )
837  * client_fd, client_addrinfo = socket.sysaccept
838  * client_socket = Socket.for_fd( client_fd )
839  * puts "The client said, '#{client_socket.readline.chomp}'"
840  * client_socket.puts "Hello from script one!"
841  * socket.close
842  *
843  * # In another script, start this second
844  * require 'socket'
845  * include Socket::Constants
846  * socket = Socket.new( AF_INET, SOCK_STREAM, 0 )
847  * sockaddr = Socket.pack_sockaddr_in( 2200, 'localhost' )
848  * socket.connect( sockaddr )
849  * socket.puts "Hello from script 2."
850  * puts "The server said, '#{socket.readline.chomp}'"
851  * socket.close
852  *
853  * Refer to Socket#accept for the exceptions that may be thrown if the call
854  * to _sysaccept_ fails.
855  *
856  * === See
857  * * Socket#accept
858  */
859 static VALUE
860 sock_sysaccept(VALUE sock)
861 {
862  rb_io_t *fptr;
863  VALUE sock2;
865  socklen_t len = (socklen_t)sizeof buf;
866 
867  GetOpenFile(sock, fptr);
868  sock2 = rsock_s_accept(0,fptr->fd,&buf.addr,&len);
869 
870  return rb_assoc_new(sock2, rsock_io_socket_addrinfo(sock2, &buf.addr, len));
871 }
872 
873 #ifdef HAVE_GETHOSTNAME
874 /*
875  * call-seq:
876  * Socket.gethostname => hostname
877  *
878  * Returns the hostname.
879  *
880  * p Socket.gethostname #=> "hal"
881  *
882  * Note that it is not guaranteed to be able to convert to IP address using gethostbyname, getaddrinfo, etc.
883  * If you need local IP address, use Socket.ip_address_list.
884  */
885 static VALUE
887 {
888 #if defined(NI_MAXHOST)
889 # define RUBY_MAX_HOST_NAME_LEN NI_MAXHOST
890 #elif defined(HOST_NAME_MAX)
891 # define RUBY_MAX_HOST_NAME_LEN HOST_NAME_MAX
892 #else
893 # define RUBY_MAX_HOST_NAME_LEN 1024
894 #endif
895 
896  long len = RUBY_MAX_HOST_NAME_LEN;
897  VALUE name;
898 
899  name = rb_str_new(0, len);
900  while (gethostname(RSTRING_PTR(name), len) < 0) {
901  int e = errno;
902  switch (e) {
903  case ENAMETOOLONG:
904 #ifdef __linux__
905  case EINVAL:
906  /* glibc before version 2.1 uses EINVAL instead of ENAMETOOLONG */
907 #endif
908  break;
909  default:
910  rb_syserr_fail(e, "gethostname(3)");
911  }
912  rb_str_modify_expand(name, len);
913  len += len;
914  }
915  rb_str_resize(name, strlen(RSTRING_PTR(name)));
916  return name;
917 }
918 #else
919 #ifdef HAVE_UNAME
920 
921 #include <sys/utsname.h>
922 
923 static VALUE
925 {
926  struct utsname un;
927 
928  uname(&un);
929  return rb_str_new2(un.nodename);
930 }
931 #else
932 #define sock_gethostname rb_f_notimplement
933 #endif
934 #endif
935 
936 static VALUE
937 make_addrinfo(struct rb_addrinfo *res0, int norevlookup)
938 {
939  VALUE base, ary;
940  struct addrinfo *res;
941 
942  if (res0 == NULL) {
943  rb_raise(rb_eSocket, "host not found");
944  }
945  base = rb_ary_new();
946  for (res = res0->ai; res; res = res->ai_next) {
947  ary = rsock_ipaddr(res->ai_addr, res->ai_addrlen, norevlookup);
948  if (res->ai_canonname) {
949  RARRAY_ASET(ary, 2, rb_str_new2(res->ai_canonname));
950  }
951  rb_ary_push(ary, INT2FIX(res->ai_family));
952  rb_ary_push(ary, INT2FIX(res->ai_socktype));
953  rb_ary_push(ary, INT2FIX(res->ai_protocol));
954  rb_ary_push(base, ary);
955  }
956  return base;
957 }
958 
959 static VALUE
960 sock_sockaddr(struct sockaddr *addr, socklen_t len)
961 {
962  char *ptr;
963 
964  switch (addr->sa_family) {
965  case AF_INET:
966  ptr = (char*)&((struct sockaddr_in*)addr)->sin_addr.s_addr;
967  len = (socklen_t)sizeof(((struct sockaddr_in*)addr)->sin_addr.s_addr);
968  break;
969 #ifdef AF_INET6
970  case AF_INET6:
971  ptr = (char*)&((struct sockaddr_in6*)addr)->sin6_addr.s6_addr;
972  len = (socklen_t)sizeof(((struct sockaddr_in6*)addr)->sin6_addr.s6_addr);
973  break;
974 #endif
975  default:
976  rb_raise(rb_eSocket, "unknown socket family:%d", addr->sa_family);
977  break;
978  }
979  return rb_str_new(ptr, len);
980 }
981 
982 /*
983  * call-seq:
984  * Socket.gethostbyname(hostname) => [official_hostname, alias_hostnames, address_family, *address_list]
985  *
986  * Use Addrinfo.getaddrinfo instead.
987  * This method is deprecated since following reasons:
988  *
989  * - The 3rd element of result is the address family of the first address.
990  * The address families of rest addresses are not returned.
991  * - Uncommon address representation:
992  * 4/16-bytes binary string to represent IPv4/IPv6 address.
993  * - gethostbyname() is may take long time and it may block other threads.
994  * (GVL cannot be released since gethostbyname() is not thread safe.)
995  * - This method uses gethostbyname() function already removed from POSIX.
996  *
997  * This method obtains the host information for _hostname_.
998  *
999  * p Socket.gethostbyname("hal") #=> ["localhost", ["hal"], 2, "\x7F\x00\x00\x01"]
1000  *
1001  */
1002 static VALUE
1003 sock_s_gethostbyname(VALUE obj, VALUE host)
1004 {
1005  struct rb_addrinfo *res =
1006  rsock_addrinfo(host, Qnil, AF_UNSPEC, SOCK_STREAM, AI_CANONNAME);
1007  return rsock_make_hostent(host, res, sock_sockaddr);
1008 }
1009 
1010 /*
1011  * call-seq:
1012  * Socket.gethostbyaddr(address_string [, address_family]) => hostent
1013  *
1014  * Use Addrinfo#getnameinfo instead.
1015  * This method is deprecated since following reasons:
1016  *
1017  * - Uncommon address representation:
1018  * 4/16-bytes binary string to represent IPv4/IPv6 address.
1019  * - gethostbyaddr() is may take long time and it may block other threads.
1020  * (GVL cannot be released since gethostbyname() is not thread safe.)
1021  * - This method uses gethostbyname() function already removed from POSIX.
1022  *
1023  * This method obtains the host information for _address_.
1024  *
1025  * p Socket.gethostbyaddr([221,186,184,68].pack("CCCC"))
1026  * #=> ["carbon.ruby-lang.org", [], 2, "\xDD\xBA\xB8D"]
1027  *
1028  * p Socket.gethostbyaddr([127,0,0,1].pack("CCCC"))
1029  * ["localhost", [], 2, "\x7F\x00\x00\x01"]
1030  * p Socket.gethostbyaddr(([0]*15+[1]).pack("C"*16))
1031  * #=> ["localhost", ["ip6-localhost", "ip6-loopback"], 10,
1032  * "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01"]
1033  *
1034  */
1035 static VALUE
1036 sock_s_gethostbyaddr(int argc, VALUE *argv)
1037 {
1038  VALUE addr, family;
1039  struct hostent *h;
1040  char **pch;
1041  VALUE ary, names;
1042  int t = AF_INET;
1043 
1044  rb_scan_args(argc, argv, "11", &addr, &family);
1045  StringValue(addr);
1046  if (!NIL_P(family)) {
1047  t = rsock_family_arg(family);
1048  }
1049 #ifdef AF_INET6
1050  else if (RSTRING_LEN(addr) == 16) {
1051  t = AF_INET6;
1052  }
1053 #endif
1054  h = gethostbyaddr(RSTRING_PTR(addr), RSTRING_SOCKLEN(addr), t);
1055  if (h == NULL) {
1056 #ifdef HAVE_HSTRERROR
1057  extern int h_errno;
1058  rb_raise(rb_eSocket, "%s", (char*)hstrerror(h_errno));
1059 #else
1060  rb_raise(rb_eSocket, "host not found");
1061 #endif
1062  }
1063  ary = rb_ary_new();
1064  rb_ary_push(ary, rb_str_new2(h->h_name));
1065  names = rb_ary_new();
1066  rb_ary_push(ary, names);
1067  if (h->h_aliases != NULL) {
1068  for (pch = h->h_aliases; *pch; pch++) {
1069  rb_ary_push(names, rb_str_new2(*pch));
1070  }
1071  }
1072  rb_ary_push(ary, INT2NUM(h->h_addrtype));
1073 #ifdef h_addr
1074  for (pch = h->h_addr_list; *pch; pch++) {
1075  rb_ary_push(ary, rb_str_new(*pch, h->h_length));
1076  }
1077 #else
1078  rb_ary_push(ary, rb_str_new(h->h_addr, h->h_length));
1079 #endif
1080 
1081  return ary;
1082 }
1083 
1084 /*
1085  * call-seq:
1086  * Socket.getservbyname(service_name) => port_number
1087  * Socket.getservbyname(service_name, protocol_name) => port_number
1088  *
1089  * Obtains the port number for _service_name_.
1090  *
1091  * If _protocol_name_ is not given, "tcp" is assumed.
1092  *
1093  * Socket.getservbyname("smtp") #=> 25
1094  * Socket.getservbyname("shell") #=> 514
1095  * Socket.getservbyname("syslog", "udp") #=> 514
1096  */
1097 static VALUE
1098 sock_s_getservbyname(int argc, VALUE *argv)
1099 {
1100  VALUE service, proto;
1101  struct servent *sp;
1102  long port;
1103  const char *servicename, *protoname = "tcp";
1104 
1105  rb_scan_args(argc, argv, "11", &service, &proto);
1106  StringValue(service);
1107  if (!NIL_P(proto)) StringValue(proto);
1108  servicename = StringValueCStr(service);
1109  if (!NIL_P(proto)) protoname = StringValueCStr(proto);
1110  sp = getservbyname(servicename, protoname);
1111  if (sp) {
1112  port = ntohs(sp->s_port);
1113  }
1114  else {
1115  char *end;
1116 
1117  port = STRTOUL(servicename, &end, 0);
1118  if (*end != '\0') {
1119  rb_raise(rb_eSocket, "no such service %s/%s", servicename, protoname);
1120  }
1121  }
1122  return INT2FIX(port);
1123 }
1124 
1125 /*
1126  * call-seq:
1127  * Socket.getservbyport(port [, protocol_name]) => service
1128  *
1129  * Obtains the port number for _port_.
1130  *
1131  * If _protocol_name_ is not given, "tcp" is assumed.
1132  *
1133  * Socket.getservbyport(80) #=> "www"
1134  * Socket.getservbyport(514, "tcp") #=> "shell"
1135  * Socket.getservbyport(514, "udp") #=> "syslog"
1136  *
1137  */
1138 static VALUE
1139 sock_s_getservbyport(int argc, VALUE *argv)
1140 {
1141  VALUE port, proto;
1142  struct servent *sp;
1143  long portnum;
1144  const char *protoname = "tcp";
1145 
1146  rb_scan_args(argc, argv, "11", &port, &proto);
1147  portnum = NUM2LONG(port);
1148  if (portnum != (uint16_t)portnum) {
1149  const char *s = portnum > 0 ? "big" : "small";
1150  rb_raise(rb_eRangeError, "integer %ld too %s to convert into `int16_t'", portnum, s);
1151  }
1152  if (!NIL_P(proto)) protoname = StringValueCStr(proto);
1153 
1154  sp = getservbyport((int)htons((uint16_t)portnum), protoname);
1155  if (!sp) {
1156  rb_raise(rb_eSocket, "no such service for port %d/%s", (int)portnum, protoname);
1157  }
1158  return rb_tainted_str_new2(sp->s_name);
1159 }
1160 
1161 /*
1162  * call-seq:
1163  * Socket.getaddrinfo(nodename, servname[, family[, socktype[, protocol[, flags[, reverse_lookup]]]]]) => array
1164  *
1165  * Obtains address information for _nodename_:_servname_.
1166  *
1167  * Note that Addrinfo.getaddrinfo provides same functionality in
1168  * object oriented style.
1169  *
1170  * _family_ should be an address family such as: :INET, :INET6, etc.
1171  *
1172  * _socktype_ should be a socket type such as: :STREAM, :DGRAM, :RAW, etc.
1173  *
1174  * _protocol_ should be a protocol defined in the family,
1175  * and defaults to 0 for the family.
1176  *
1177  * _flags_ should be bitwise OR of Socket::AI_* constants.
1178  *
1179  * Socket.getaddrinfo("www.ruby-lang.org", "http", nil, :STREAM)
1180  * #=> [["AF_INET", 80, "carbon.ruby-lang.org", "221.186.184.68", 2, 1, 6]] # PF_INET/SOCK_STREAM/IPPROTO_TCP
1181  *
1182  * Socket.getaddrinfo("localhost", nil)
1183  * #=> [["AF_INET", 0, "localhost", "127.0.0.1", 2, 1, 6], # PF_INET/SOCK_STREAM/IPPROTO_TCP
1184  * # ["AF_INET", 0, "localhost", "127.0.0.1", 2, 2, 17], # PF_INET/SOCK_DGRAM/IPPROTO_UDP
1185  * # ["AF_INET", 0, "localhost", "127.0.0.1", 2, 3, 0]] # PF_INET/SOCK_RAW/IPPROTO_IP
1186  *
1187  * _reverse_lookup_ directs the form of the third element, and has to
1188  * be one of below. If _reverse_lookup_ is omitted, the default value is +nil+.
1189  *
1190  * +true+, +:hostname+: hostname is obtained from numeric address using reverse lookup, which may take a time.
1191  * +false+, +:numeric+: hostname is same as numeric address.
1192  * +nil+: obey to the current +do_not_reverse_lookup+ flag.
1193  *
1194  * If Addrinfo object is preferred, use Addrinfo.getaddrinfo.
1195  */
1196 static VALUE
1197 sock_s_getaddrinfo(int argc, VALUE *argv)
1198 {
1199  VALUE host, port, family, socktype, protocol, flags, ret, revlookup;
1200  struct addrinfo hints;
1201  struct rb_addrinfo *res;
1202  int norevlookup;
1203 
1204  rb_scan_args(argc, argv, "25", &host, &port, &family, &socktype, &protocol, &flags, &revlookup);
1205 
1206  MEMZERO(&hints, struct addrinfo, 1);
1207  hints.ai_family = NIL_P(family) ? PF_UNSPEC : rsock_family_arg(family);
1208 
1209  if (!NIL_P(socktype)) {
1210  hints.ai_socktype = rsock_socktype_arg(socktype);
1211  }
1212  if (!NIL_P(protocol)) {
1213  hints.ai_protocol = NUM2INT(protocol);
1214  }
1215  if (!NIL_P(flags)) {
1216  hints.ai_flags = NUM2INT(flags);
1217  }
1218  if (NIL_P(revlookup) || !rsock_revlookup_flag(revlookup, &norevlookup)) {
1219  norevlookup = rsock_do_not_reverse_lookup;
1220  }
1221  res = rsock_getaddrinfo(host, port, &hints, 0);
1222 
1223  ret = make_addrinfo(res, norevlookup);
1224  rb_freeaddrinfo(res);
1225  return ret;
1226 }
1227 
1228 /*
1229  * call-seq:
1230  * Socket.getnameinfo(sockaddr [, flags]) => [hostname, servicename]
1231  *
1232  * Obtains name information for _sockaddr_.
1233  *
1234  * _sockaddr_ should be one of follows.
1235  * - packed sockaddr string such as Socket.sockaddr_in(80, "127.0.0.1")
1236  * - 3-elements array such as ["AF_INET", 80, "127.0.0.1"]
1237  * - 4-elements array such as ["AF_INET", 80, ignored, "127.0.0.1"]
1238  *
1239  * _flags_ should be bitwise OR of Socket::NI_* constants.
1240  *
1241  * Note:
1242  * The last form is compatible with IPSocket#addr and IPSocket#peeraddr.
1243  *
1244  * Socket.getnameinfo(Socket.sockaddr_in(80, "127.0.0.1")) #=> ["localhost", "www"]
1245  * Socket.getnameinfo(["AF_INET", 80, "127.0.0.1"]) #=> ["localhost", "www"]
1246  * Socket.getnameinfo(["AF_INET", 80, "localhost", "127.0.0.1"]) #=> ["localhost", "www"]
1247  *
1248  * If Addrinfo object is preferred, use Addrinfo#getnameinfo.
1249  */
1250 static VALUE
1251 sock_s_getnameinfo(int argc, VALUE *argv)
1252 {
1253  VALUE sa, af = Qnil, host = Qnil, port = Qnil, flags, tmp;
1254  char *hptr, *pptr;
1255  char hbuf[1024], pbuf[1024];
1256  int fl;
1257  struct rb_addrinfo *res = NULL;
1258  struct addrinfo hints, *r;
1259  int error, saved_errno;
1260  union_sockaddr ss;
1261  struct sockaddr *sap;
1262  socklen_t salen;
1263 
1264  sa = flags = Qnil;
1265  rb_scan_args(argc, argv, "11", &sa, &flags);
1266 
1267  fl = 0;
1268  if (!NIL_P(flags)) {
1269  fl = NUM2INT(flags);
1270  }
1272  if (!NIL_P(tmp)) {
1273  sa = tmp;
1274  if (sizeof(ss) < (size_t)RSTRING_LEN(sa)) {
1275  rb_raise(rb_eTypeError, "sockaddr length too big");
1276  }
1277  memcpy(&ss, RSTRING_PTR(sa), RSTRING_LEN(sa));
1278  if (!VALIDATE_SOCKLEN(&ss.addr, RSTRING_LEN(sa))) {
1279  rb_raise(rb_eTypeError, "sockaddr size differs - should not happen");
1280  }
1281  sap = &ss.addr;
1282  salen = RSTRING_SOCKLEN(sa);
1283  goto call_nameinfo;
1284  }
1285  tmp = rb_check_array_type(sa);
1286  if (!NIL_P(tmp)) {
1287  sa = tmp;
1288  MEMZERO(&hints, struct addrinfo, 1);
1289  if (RARRAY_LEN(sa) == 3) {
1290  af = RARRAY_AREF(sa, 0);
1291  port = RARRAY_AREF(sa, 1);
1292  host = RARRAY_AREF(sa, 2);
1293  }
1294  else if (RARRAY_LEN(sa) >= 4) {
1295  af = RARRAY_AREF(sa, 0);
1296  port = RARRAY_AREF(sa, 1);
1297  host = RARRAY_AREF(sa, 3);
1298  if (NIL_P(host)) {
1299  host = RARRAY_AREF(sa, 2);
1300  }
1301  else {
1302  /*
1303  * 4th element holds numeric form, don't resolve.
1304  * see rsock_ipaddr().
1305  */
1306 #ifdef AI_NUMERICHOST /* AIX 4.3.3 doesn't have AI_NUMERICHOST. */
1307  hints.ai_flags |= AI_NUMERICHOST;
1308 #endif
1309  }
1310  }
1311  else {
1312  rb_raise(rb_eArgError, "array size should be 3 or 4, %ld given",
1313  RARRAY_LEN(sa));
1314  }
1315  /* host */
1316  if (NIL_P(host)) {
1317  hptr = NULL;
1318  }
1319  else {
1320  strncpy(hbuf, StringValueCStr(host), sizeof(hbuf));
1321  hbuf[sizeof(hbuf) - 1] = '\0';
1322  hptr = hbuf;
1323  }
1324  /* port */
1325  if (NIL_P(port)) {
1326  strcpy(pbuf, "0");
1327  pptr = NULL;
1328  }
1329  else if (FIXNUM_P(port)) {
1330  snprintf(pbuf, sizeof(pbuf), "%ld", NUM2LONG(port));
1331  pptr = pbuf;
1332  }
1333  else {
1334  strncpy(pbuf, StringValueCStr(port), sizeof(pbuf));
1335  pbuf[sizeof(pbuf) - 1] = '\0';
1336  pptr = pbuf;
1337  }
1338  hints.ai_socktype = (fl & NI_DGRAM) ? SOCK_DGRAM : SOCK_STREAM;
1339  /* af */
1340  hints.ai_family = NIL_P(af) ? PF_UNSPEC : rsock_family_arg(af);
1341  error = rb_getaddrinfo(hptr, pptr, &hints, &res);
1342  if (error) goto error_exit_addr;
1343  sap = res->ai->ai_addr;
1344  salen = res->ai->ai_addrlen;
1345  }
1346  else {
1347  rb_raise(rb_eTypeError, "expecting String or Array");
1348  }
1349 
1350  call_nameinfo:
1351  error = rb_getnameinfo(sap, salen, hbuf, sizeof(hbuf),
1352  pbuf, sizeof(pbuf), fl);
1353  if (error) goto error_exit_name;
1354  if (res) {
1355  for (r = res->ai->ai_next; r; r = r->ai_next) {
1356  char hbuf2[1024], pbuf2[1024];
1357 
1358  sap = r->ai_addr;
1359  salen = r->ai_addrlen;
1360  error = rb_getnameinfo(sap, salen, hbuf2, sizeof(hbuf2),
1361  pbuf2, sizeof(pbuf2), fl);
1362  if (error) goto error_exit_name;
1363  if (strcmp(hbuf, hbuf2) != 0|| strcmp(pbuf, pbuf2) != 0) {
1364  rb_freeaddrinfo(res);
1365  rb_raise(rb_eSocket, "sockaddr resolved to multiple nodename");
1366  }
1367  }
1368  rb_freeaddrinfo(res);
1369  }
1370  return rb_assoc_new(rb_str_new2(hbuf), rb_str_new2(pbuf));
1371 
1372  error_exit_addr:
1373  saved_errno = errno;
1374  if (res) rb_freeaddrinfo(res);
1375  errno = saved_errno;
1376  rsock_raise_socket_error("getaddrinfo", error);
1377 
1378  error_exit_name:
1379  saved_errno = errno;
1380  if (res) rb_freeaddrinfo(res);
1381  errno = saved_errno;
1382  rsock_raise_socket_error("getnameinfo", error);
1383 
1384  UNREACHABLE;
1385 }
1386 
1387 /*
1388  * call-seq:
1389  * Socket.sockaddr_in(port, host) => sockaddr
1390  * Socket.pack_sockaddr_in(port, host) => sockaddr
1391  *
1392  * Packs _port_ and _host_ as an AF_INET/AF_INET6 sockaddr string.
1393  *
1394  * Socket.sockaddr_in(80, "127.0.0.1")
1395  * #=> "\x02\x00\x00P\x7F\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00"
1396  *
1397  * Socket.sockaddr_in(80, "::1")
1398  * #=> "\n\x00\x00P\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00"
1399  *
1400  */
1401 static VALUE
1402 sock_s_pack_sockaddr_in(VALUE self, VALUE port, VALUE host)
1403 {
1404  struct rb_addrinfo *res = rsock_addrinfo(host, port, AF_UNSPEC, 0, 0);
1405  VALUE addr = rb_str_new((char*)res->ai->ai_addr, res->ai->ai_addrlen);
1406 
1407  rb_freeaddrinfo(res);
1408  OBJ_INFECT(addr, port);
1409  OBJ_INFECT(addr, host);
1410 
1411  return addr;
1412 }
1413 
1414 /*
1415  * call-seq:
1416  * Socket.unpack_sockaddr_in(sockaddr) => [port, ip_address]
1417  *
1418  * Unpacks _sockaddr_ into port and ip_address.
1419  *
1420  * _sockaddr_ should be a string or an addrinfo for AF_INET/AF_INET6.
1421  *
1422  * sockaddr = Socket.sockaddr_in(80, "127.0.0.1")
1423  * p sockaddr #=> "\x02\x00\x00P\x7F\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00"
1424  * p Socket.unpack_sockaddr_in(sockaddr) #=> [80, "127.0.0.1"]
1425  *
1426  */
1427 static VALUE
1428 sock_s_unpack_sockaddr_in(VALUE self, VALUE addr)
1429 {
1430  struct sockaddr_in * sockaddr;
1431  VALUE host;
1432 
1433  sockaddr = (struct sockaddr_in*)SockAddrStringValuePtr(addr);
1434  if (RSTRING_LEN(addr) <
1435  (char*)&((struct sockaddr *)sockaddr)->sa_family +
1436  sizeof(((struct sockaddr *)sockaddr)->sa_family) -
1437  (char*)sockaddr)
1438  rb_raise(rb_eArgError, "too short sockaddr");
1439  if (((struct sockaddr *)sockaddr)->sa_family != AF_INET
1440 #ifdef INET6
1441  && ((struct sockaddr *)sockaddr)->sa_family != AF_INET6
1442 #endif
1443  ) {
1444 #ifdef INET6
1445  rb_raise(rb_eArgError, "not an AF_INET/AF_INET6 sockaddr");
1446 #else
1447  rb_raise(rb_eArgError, "not an AF_INET sockaddr");
1448 #endif
1449  }
1450  host = rsock_make_ipaddr((struct sockaddr*)sockaddr, RSTRING_SOCKLEN(addr));
1451  OBJ_INFECT(host, addr);
1452  return rb_assoc_new(INT2NUM(ntohs(sockaddr->sin_port)), host);
1453 }
1454 
1455 #ifdef HAVE_SYS_UN_H
1456 
1457 /*
1458  * call-seq:
1459  * Socket.sockaddr_un(path) => sockaddr
1460  * Socket.pack_sockaddr_un(path) => sockaddr
1461  *
1462  * Packs _path_ as an AF_UNIX sockaddr string.
1463  *
1464  * Socket.sockaddr_un("/tmp/sock") #=> "\x01\x00/tmp/sock\x00\x00..."
1465  *
1466  */
1467 static VALUE
1468 sock_s_pack_sockaddr_un(VALUE self, VALUE path)
1469 {
1470  struct sockaddr_un sockaddr;
1471  VALUE addr;
1472 
1473  StringValue(path);
1474  INIT_SOCKADDR_UN(&sockaddr, sizeof(struct sockaddr_un));
1475  if (sizeof(sockaddr.sun_path) < (size_t)RSTRING_LEN(path)) {
1476  rb_raise(rb_eArgError, "too long unix socket path (%"PRIuSIZE" bytes given but %"PRIuSIZE" bytes max)",
1477  (size_t)RSTRING_LEN(path), sizeof(sockaddr.sun_path));
1478  }
1479  memcpy(sockaddr.sun_path, RSTRING_PTR(path), RSTRING_LEN(path));
1480  addr = rb_str_new((char*)&sockaddr, rsock_unix_sockaddr_len(path));
1481  OBJ_INFECT(addr, path);
1482 
1483  return addr;
1484 }
1485 
1486 /*
1487  * call-seq:
1488  * Socket.unpack_sockaddr_un(sockaddr) => path
1489  *
1490  * Unpacks _sockaddr_ into path.
1491  *
1492  * _sockaddr_ should be a string or an addrinfo for AF_UNIX.
1493  *
1494  * sockaddr = Socket.sockaddr_un("/tmp/sock")
1495  * p Socket.unpack_sockaddr_un(sockaddr) #=> "/tmp/sock"
1496  *
1497  */
1498 static VALUE
1499 sock_s_unpack_sockaddr_un(VALUE self, VALUE addr)
1500 {
1501  struct sockaddr_un * sockaddr;
1502  VALUE path;
1503 
1504  sockaddr = (struct sockaddr_un*)SockAddrStringValuePtr(addr);
1505  if (RSTRING_LEN(addr) <
1506  (char*)&((struct sockaddr *)sockaddr)->sa_family +
1507  sizeof(((struct sockaddr *)sockaddr)->sa_family) -
1508  (char*)sockaddr)
1509  rb_raise(rb_eArgError, "too short sockaddr");
1510  if (((struct sockaddr *)sockaddr)->sa_family != AF_UNIX) {
1511  rb_raise(rb_eArgError, "not an AF_UNIX sockaddr");
1512  }
1513  if (sizeof(struct sockaddr_un) < (size_t)RSTRING_LEN(addr)) {
1514  rb_raise(rb_eTypeError, "too long sockaddr_un - %ld longer than %d",
1515  RSTRING_LEN(addr), (int)sizeof(struct sockaddr_un));
1516  }
1517  path = rsock_unixpath_str(sockaddr, RSTRING_SOCKLEN(addr));
1518  OBJ_INFECT(path, addr);
1519  return path;
1520 }
1521 #endif
1522 
1523 #if defined(HAVE_GETIFADDRS) || defined(SIOCGLIFCONF) || defined(SIOCGIFCONF) || defined(_WIN32)
1524 
1525 static socklen_t
1526 sockaddr_len(struct sockaddr *addr)
1527 {
1528  if (addr == NULL)
1529  return 0;
1530 
1531 #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
1532  if (addr->sa_len != 0)
1533  return addr->sa_len;
1534 #endif
1535 
1536  switch (addr->sa_family) {
1537  case AF_INET:
1538  return (socklen_t)sizeof(struct sockaddr_in);
1539 
1540 #ifdef AF_INET6
1541  case AF_INET6:
1542  return (socklen_t)sizeof(struct sockaddr_in6);
1543 #endif
1544 
1545 #ifdef HAVE_SYS_UN_H
1546  case AF_UNIX:
1547  return (socklen_t)sizeof(struct sockaddr_un);
1548 #endif
1549 
1550 #ifdef AF_PACKET
1551  case AF_PACKET:
1552  return (socklen_t)(offsetof(struct sockaddr_ll, sll_addr) + ((struct sockaddr_ll *)addr)->sll_halen);
1553 #endif
1554 
1555  default:
1556  return (socklen_t)(offsetof(struct sockaddr, sa_family) + sizeof(addr->sa_family));
1557  }
1558 }
1559 
1560 socklen_t
1561 rsock_sockaddr_len(struct sockaddr *addr)
1562 {
1563  return sockaddr_len(addr);
1564 }
1565 
1566 static VALUE
1567 sockaddr_obj(struct sockaddr *addr, socklen_t len)
1568 {
1569 #if defined(AF_INET6) && defined(__KAME__)
1570  struct sockaddr_in6 addr6;
1571 #endif
1572 
1573  if (addr == NULL)
1574  return Qnil;
1575 
1576  len = sockaddr_len(addr);
1577 
1578 #if defined(__KAME__) && defined(AF_INET6)
1579  if (addr->sa_family == AF_INET6) {
1580  /* KAME uses the 2nd 16bit word of link local IPv6 address as interface index internally */
1581  /* http://orange.kame.net/dev/cvsweb.cgi/kame/IMPLEMENTATION */
1582  /* convert fe80:1::1 to fe80::1%1 */
1583  len = (socklen_t)sizeof(struct sockaddr_in6);
1584  memcpy(&addr6, addr, len);
1585  addr = (struct sockaddr *)&addr6;
1586  if (IN6_IS_ADDR_LINKLOCAL(&addr6.sin6_addr) &&
1587  addr6.sin6_scope_id == 0 &&
1588  (addr6.sin6_addr.s6_addr[2] || addr6.sin6_addr.s6_addr[3])) {
1589  addr6.sin6_scope_id = (addr6.sin6_addr.s6_addr[2] << 8) | addr6.sin6_addr.s6_addr[3];
1590  addr6.sin6_addr.s6_addr[2] = 0;
1591  addr6.sin6_addr.s6_addr[3] = 0;
1592  }
1593  }
1594 #endif
1595 
1596  return rsock_addrinfo_new(addr, len, addr->sa_family, 0, 0, Qnil, Qnil);
1597 }
1598 
1599 VALUE
1600 rsock_sockaddr_obj(struct sockaddr *addr, socklen_t len)
1601 {
1602  return sockaddr_obj(addr, len);
1603 }
1604 
1605 #endif
1606 
1607 #if defined(HAVE_GETIFADDRS) || (defined(SIOCGLIFCONF) && defined(SIOCGLIFNUM) && !defined(__hpux)) || defined(SIOCGIFCONF) || defined(_WIN32)
1608 /*
1609  * call-seq:
1610  * Socket.ip_address_list => array
1611  *
1612  * Returns local IP addresses as an array.
1613  *
1614  * The array contains Addrinfo objects.
1615  *
1616  * pp Socket.ip_address_list
1617  * #=> [#<Addrinfo: 127.0.0.1>,
1618  * #<Addrinfo: 192.168.0.128>,
1619  * #<Addrinfo: ::1>,
1620  * ...]
1621  *
1622  */
1623 static VALUE
1625 {
1626 #if defined(HAVE_GETIFADDRS)
1627  struct ifaddrs *ifp = NULL;
1628  struct ifaddrs *p;
1629  int ret;
1630  VALUE list;
1631 
1632  ret = getifaddrs(&ifp);
1633  if (ret == -1) {
1634  rb_sys_fail("getifaddrs");
1635  }
1636 
1637  list = rb_ary_new();
1638  for (p = ifp; p; p = p->ifa_next) {
1639  if (p->ifa_addr != NULL && IS_IP_FAMILY(p->ifa_addr->sa_family)) {
1640  struct sockaddr *addr = p->ifa_addr;
1641 #if defined(AF_INET6) && defined(__sun)
1642  /*
1643  * OpenIndiana SunOS 5.11 getifaddrs() returns IPv6 link local
1644  * address with sin6_scope_id == 0.
1645  * So fill it from the interface name (ifa_name).
1646  */
1647  struct sockaddr_in6 addr6;
1648  if (addr->sa_family == AF_INET6) {
1649  socklen_t len = (socklen_t)sizeof(struct sockaddr_in6);
1650  memcpy(&addr6, addr, len);
1651  addr = (struct sockaddr *)&addr6;
1652  if (IN6_IS_ADDR_LINKLOCAL(&addr6.sin6_addr) &&
1653  addr6.sin6_scope_id == 0) {
1654  unsigned int ifindex = if_nametoindex(p->ifa_name);
1655  if (ifindex != 0) {
1656  addr6.sin6_scope_id = ifindex;
1657  }
1658  }
1659  }
1660 #endif
1661  rb_ary_push(list, sockaddr_obj(addr, sockaddr_len(addr)));
1662  }
1663  }
1664 
1665  freeifaddrs(ifp);
1666 
1667  return list;
1668 #elif defined(SIOCGLIFCONF) && defined(SIOCGLIFNUM) && !defined(__hpux)
1669  /* Solaris if_tcp(7P) */
1670  /* HP-UX has SIOCGLIFCONF too. But it uses different struct */
1671  int fd = -1;
1672  int ret;
1673  struct lifnum ln;
1674  struct lifconf lc;
1675  const char *reason = NULL;
1676  int save_errno;
1677  int i;
1678  VALUE list = Qnil;
1679 
1680  lc.lifc_buf = NULL;
1681 
1682  fd = socket(AF_INET, SOCK_DGRAM, 0);
1683  if (fd == -1)
1684  rb_sys_fail("socket(2)");
1685 
1686  memset(&ln, 0, sizeof(ln));
1687  ln.lifn_family = AF_UNSPEC;
1688 
1689  ret = ioctl(fd, SIOCGLIFNUM, &ln);
1690  if (ret == -1) {
1691  reason = "SIOCGLIFNUM";
1692  goto finish;
1693  }
1694 
1695  memset(&lc, 0, sizeof(lc));
1696  lc.lifc_family = AF_UNSPEC;
1697  lc.lifc_flags = 0;
1698  lc.lifc_len = sizeof(struct lifreq) * ln.lifn_count;
1699  lc.lifc_req = xmalloc(lc.lifc_len);
1700 
1701  ret = ioctl(fd, SIOCGLIFCONF, &lc);
1702  if (ret == -1) {
1703  reason = "SIOCGLIFCONF";
1704  goto finish;
1705  }
1706 
1707  list = rb_ary_new();
1708  for (i = 0; i < ln.lifn_count; i++) {
1709  struct lifreq *req = &lc.lifc_req[i];
1710  if (IS_IP_FAMILY(req->lifr_addr.ss_family)) {
1711  if (req->lifr_addr.ss_family == AF_INET6 &&
1712  IN6_IS_ADDR_LINKLOCAL(&((struct sockaddr_in6 *)(&req->lifr_addr))->sin6_addr) &&
1713  ((struct sockaddr_in6 *)(&req->lifr_addr))->sin6_scope_id == 0) {
1714  struct lifreq req2;
1715  memcpy(req2.lifr_name, req->lifr_name, LIFNAMSIZ);
1716  ret = ioctl(fd, SIOCGLIFINDEX, &req2);
1717  if (ret == -1) {
1718  reason = "SIOCGLIFINDEX";
1719  goto finish;
1720  }
1721  ((struct sockaddr_in6 *)(&req->lifr_addr))->sin6_scope_id = req2.lifr_index;
1722  }
1723  rb_ary_push(list, sockaddr_obj((struct sockaddr *)&req->lifr_addr, req->lifr_addrlen));
1724  }
1725  }
1726 
1727  finish:
1728  save_errno = errno;
1729  if (lc.lifc_buf != NULL)
1730  xfree(lc.lifc_req);
1731  if (fd != -1)
1732  close(fd);
1733  errno = save_errno;
1734 
1735  if (reason)
1736  rb_syserr_fail(save_errno, reason);
1737  return list;
1738 
1739 #elif defined(SIOCGIFCONF)
1740  int fd = -1;
1741  int ret;
1742 #define EXTRA_SPACE ((int)(sizeof(struct ifconf) + sizeof(union_sockaddr)))
1743  char initbuf[4096+EXTRA_SPACE];
1744  char *buf = initbuf;
1745  int bufsize;
1746  struct ifconf conf;
1747  struct ifreq *req;
1748  VALUE list = Qnil;
1749  const char *reason = NULL;
1750  int save_errno;
1751 
1752  fd = socket(AF_INET, SOCK_DGRAM, 0);
1753  if (fd == -1)
1754  rb_sys_fail("socket(2)");
1755 
1756  bufsize = sizeof(initbuf);
1757  buf = initbuf;
1758 
1759  retry:
1760  conf.ifc_len = bufsize;
1761  conf.ifc_req = (struct ifreq *)buf;
1762 
1763  /* fprintf(stderr, "bufsize: %d\n", bufsize); */
1764 
1765  ret = ioctl(fd, SIOCGIFCONF, &conf);
1766  if (ret == -1) {
1767  reason = "SIOCGIFCONF";
1768  goto finish;
1769  }
1770 
1771  /* fprintf(stderr, "conf.ifc_len: %d\n", conf.ifc_len); */
1772 
1773  if (bufsize - EXTRA_SPACE < conf.ifc_len) {
1774  if (bufsize < conf.ifc_len) {
1775  /* NetBSD returns required size for all interfaces. */
1776  bufsize = conf.ifc_len + EXTRA_SPACE;
1777  }
1778  else {
1779  bufsize = bufsize << 1;
1780  }
1781  if (buf == initbuf)
1782  buf = NULL;
1783  buf = xrealloc(buf, bufsize);
1784  goto retry;
1785  }
1786 
1787  close(fd);
1788  fd = -1;
1789 
1790  list = rb_ary_new();
1791  req = conf.ifc_req;
1792  while ((char*)req < (char*)conf.ifc_req + conf.ifc_len) {
1793  struct sockaddr *addr = &req->ifr_addr;
1794  if (IS_IP_FAMILY(addr->sa_family)) {
1795  rb_ary_push(list, sockaddr_obj(addr, sockaddr_len(addr)));
1796  }
1797 #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
1798 # ifndef _SIZEOF_ADDR_IFREQ
1799 # define _SIZEOF_ADDR_IFREQ(r) \
1800  (sizeof(struct ifreq) + \
1801  (sizeof(struct sockaddr) < (r).ifr_addr.sa_len ? \
1802  (r).ifr_addr.sa_len - sizeof(struct sockaddr) : \
1803  0))
1804 # endif
1805  req = (struct ifreq *)((char*)req + _SIZEOF_ADDR_IFREQ(*req));
1806 #else
1807  req = (struct ifreq *)((char*)req + sizeof(struct ifreq));
1808 #endif
1809  }
1810 
1811  finish:
1812 
1813  save_errno = errno;
1814  if (buf != initbuf)
1815  xfree(buf);
1816  if (fd != -1)
1817  close(fd);
1818  errno = save_errno;
1819 
1820  if (reason)
1821  rb_syserr_fail(save_errno, reason);
1822  return list;
1823 
1824 #undef EXTRA_SPACE
1825 #elif defined(_WIN32)
1826  typedef struct ip_adapter_unicast_address_st {
1827  unsigned LONG_LONG dummy0;
1828  struct ip_adapter_unicast_address_st *Next;
1829  struct {
1830  struct sockaddr *lpSockaddr;
1831  int iSockaddrLength;
1832  } Address;
1833  int dummy1;
1834  int dummy2;
1835  int dummy3;
1836  long dummy4;
1837  long dummy5;
1838  long dummy6;
1839  } ip_adapter_unicast_address_t;
1840  typedef struct ip_adapter_anycast_address_st {
1841  unsigned LONG_LONG dummy0;
1842  struct ip_adapter_anycast_address_st *Next;
1843  struct {
1844  struct sockaddr *lpSockaddr;
1845  int iSockaddrLength;
1846  } Address;
1847  } ip_adapter_anycast_address_t;
1848  typedef struct ip_adapter_addresses_st {
1849  unsigned LONG_LONG dummy0;
1850  struct ip_adapter_addresses_st *Next;
1851  void *dummy1;
1852  ip_adapter_unicast_address_t *FirstUnicastAddress;
1853  ip_adapter_anycast_address_t *FirstAnycastAddress;
1854  void *dummy2;
1855  void *dummy3;
1856  void *dummy4;
1857  void *dummy5;
1858  void *dummy6;
1859  BYTE dummy7[8];
1860  DWORD dummy8;
1861  DWORD dummy9;
1862  DWORD dummy10;
1863  DWORD IfType;
1864  int OperStatus;
1865  DWORD dummy12;
1866  DWORD dummy13[16];
1867  void *dummy14;
1868  } ip_adapter_addresses_t;
1869  typedef ULONG (WINAPI *GetAdaptersAddresses_t)(ULONG, ULONG, PVOID, ip_adapter_addresses_t *, PULONG);
1870  HMODULE h;
1871  GetAdaptersAddresses_t pGetAdaptersAddresses;
1872  ULONG len;
1873  DWORD ret;
1874  ip_adapter_addresses_t *adapters;
1875  VALUE list;
1876 
1877  h = LoadLibrary("iphlpapi.dll");
1878  if (!h)
1879  rb_notimplement();
1880  pGetAdaptersAddresses = (GetAdaptersAddresses_t)GetProcAddress(h, "GetAdaptersAddresses");
1881  if (!pGetAdaptersAddresses) {
1882  FreeLibrary(h);
1883  rb_notimplement();
1884  }
1885 
1886  ret = pGetAdaptersAddresses(AF_UNSPEC, 0, NULL, NULL, &len);
1887  if (ret != ERROR_SUCCESS && ret != ERROR_BUFFER_OVERFLOW) {
1888  errno = rb_w32_map_errno(ret);
1889  FreeLibrary(h);
1890  rb_sys_fail("GetAdaptersAddresses");
1891  }
1892  adapters = (ip_adapter_addresses_t *)ALLOCA_N(BYTE, len);
1893  ret = pGetAdaptersAddresses(AF_UNSPEC, 0, NULL, adapters, &len);
1894  if (ret != ERROR_SUCCESS) {
1895  errno = rb_w32_map_errno(ret);
1896  FreeLibrary(h);
1897  rb_sys_fail("GetAdaptersAddresses");
1898  }
1899 
1900  list = rb_ary_new();
1901  for (; adapters; adapters = adapters->Next) {
1902  ip_adapter_unicast_address_t *uni;
1903  ip_adapter_anycast_address_t *any;
1904  if (adapters->OperStatus != 1) /* 1 means IfOperStatusUp */
1905  continue;
1906  for (uni = adapters->FirstUnicastAddress; uni; uni = uni->Next) {
1907 #ifndef INET6
1908  if (uni->Address.lpSockaddr->sa_family == AF_INET)
1909 #else
1910  if (IS_IP_FAMILY(uni->Address.lpSockaddr->sa_family))
1911 #endif
1912  rb_ary_push(list, sockaddr_obj(uni->Address.lpSockaddr, uni->Address.iSockaddrLength));
1913  }
1914  for (any = adapters->FirstAnycastAddress; any; any = any->Next) {
1915 #ifndef INET6
1916  if (any->Address.lpSockaddr->sa_family == AF_INET)
1917 #else
1918  if (IS_IP_FAMILY(any->Address.lpSockaddr->sa_family))
1919 #endif
1920  rb_ary_push(list, sockaddr_obj(any->Address.lpSockaddr, any->Address.iSockaddrLength));
1921  }
1922  }
1923 
1924  FreeLibrary(h);
1925  return list;
1926 #endif
1927 }
1928 #else
1929 #define socket_s_ip_address_list rb_f_notimplement
1930 #endif
1931 
1932 void
1934 {
1936 
1937  /*
1938  * Document-class: Socket < BasicSocket
1939  *
1940  * Class +Socket+ provides access to the underlying operating system
1941  * socket implementations. It can be used to provide more operating system
1942  * specific functionality than the protocol-specific socket classes.
1943  *
1944  * The constants defined under Socket::Constants are also defined under
1945  * Socket. For example, Socket::AF_INET is usable as well as
1946  * Socket::Constants::AF_INET. See Socket::Constants for the list of
1947  * constants.
1948  *
1949  * === What's a socket?
1950  *
1951  * Sockets are endpoints of a bidirectional communication channel.
1952  * Sockets can communicate within a process, between processes on the same
1953  * machine or between different machines. There are many types of socket:
1954  * TCPSocket, UDPSocket or UNIXSocket for example.
1955  *
1956  * Sockets have their own vocabulary:
1957  *
1958  * *domain:*
1959  * The family of protocols:
1960  * * Socket::PF_INET
1961  * * Socket::PF_INET6
1962  * * Socket::PF_UNIX
1963  * * etc.
1964  *
1965  * *type:*
1966  * The type of communications between the two endpoints, typically
1967  * * Socket::SOCK_STREAM
1968  * * Socket::SOCK_DGRAM.
1969  *
1970  * *protocol:*
1971  * Typically _zero_.
1972  * This may be used to identify a variant of a protocol.
1973  *
1974  * *hostname:*
1975  * The identifier of a network interface:
1976  * * a string (hostname, IPv4 or IPv6 address or +broadcast+
1977  * which specifies a broadcast address)
1978  * * a zero-length string which specifies INADDR_ANY
1979  * * an integer (interpreted as binary address in host byte order).
1980  *
1981  * === Quick start
1982  *
1983  * Many of the classes, such as TCPSocket, UDPSocket or UNIXSocket,
1984  * ease the use of sockets comparatively to the equivalent C programming interface.
1985  *
1986  * Let's create an internet socket using the IPv4 protocol in a C-like manner:
1987  *
1988  * require 'socket'
1989  *
1990  * s = Socket.new Socket::AF_INET, Socket::SOCK_STREAM
1991  * s.connect Socket.pack_sockaddr_in(80, 'example.com')
1992  *
1993  * You could also use the TCPSocket class:
1994  *
1995  * s = TCPSocket.new 'example.com', 80
1996  *
1997  * A simple server might look like this:
1998  *
1999  * require 'socket'
2000  *
2001  * server = TCPServer.new 2000 # Server bound to port 2000
2002  *
2003  * loop do
2004  * client = server.accept # Wait for a client to connect
2005  * client.puts "Hello !"
2006  * client.puts "Time is #{Time.now}"
2007  * client.close
2008  * end
2009  *
2010  * A simple client may look like this:
2011  *
2012  * require 'socket'
2013  *
2014  * s = TCPSocket.new 'localhost', 2000
2015  *
2016  * while line = s.gets # Read lines from socket
2017  * puts line # and print them
2018  * end
2019  *
2020  * s.close # close socket when done
2021  *
2022  * === Exception Handling
2023  *
2024  * Ruby's Socket implementation raises exceptions based on the error
2025  * generated by the system dependent implementation. This is why the
2026  * methods are documented in a way that isolate Unix-based system
2027  * exceptions from Windows based exceptions. If more information on a
2028  * particular exception is needed, please refer to the Unix manual pages or
2029  * the Windows WinSock reference.
2030  *
2031  * === Convenience methods
2032  *
2033  * Although the general way to create socket is Socket.new,
2034  * there are several methods of socket creation for most cases.
2035  *
2036  * TCP client socket::
2037  * Socket.tcp, TCPSocket.open
2038  * TCP server socket::
2039  * Socket.tcp_server_loop, TCPServer.open
2040  * UNIX client socket::
2041  * Socket.unix, UNIXSocket.open
2042  * UNIX server socket::
2043  * Socket.unix_server_loop, UNIXServer.open
2044  *
2045  * === Documentation by
2046  *
2047  * * Zach Dennis
2048  * * Sam Roberts
2049  * * <em>Programming Ruby</em> from The Pragmatic Bookshelf.
2050  *
2051  * Much material in this documentation is taken with permission from
2052  * <em>Programming Ruby</em> from The Pragmatic Bookshelf.
2053  */
2055 
2057 
2058  rb_define_method(rb_cSocket, "initialize", sock_initialize, -1);
2059  rb_define_method(rb_cSocket, "connect", sock_connect, 1);
2060 
2061  /* for ext/socket/lib/socket.rb use only: */
2063  "__connect_nonblock", sock_connect_nonblock, 2);
2064 
2065  rb_define_method(rb_cSocket, "bind", sock_bind, 1);
2067  rb_define_method(rb_cSocket, "accept", sock_accept, 0);
2068 
2069  /* for ext/socket/lib/socket.rb use only: */
2071  "__accept_nonblock", sock_accept_nonblock, 1);
2072 
2073  rb_define_method(rb_cSocket, "sysaccept", sock_sysaccept, 0);
2074 
2075  rb_define_method(rb_cSocket, "recvfrom", sock_recvfrom, -1);
2076 
2077  /* for ext/socket/lib/socket.rb use only: */
2079  "__recvfrom_nonblock", sock_recvfrom_nonblock, 4);
2080 
2084  rb_define_singleton_method(rb_cSocket, "gethostbyname", sock_s_gethostbyname, 1);
2085  rb_define_singleton_method(rb_cSocket, "gethostbyaddr", sock_s_gethostbyaddr, -1);
2086  rb_define_singleton_method(rb_cSocket, "getservbyname", sock_s_getservbyname, -1);
2087  rb_define_singleton_method(rb_cSocket, "getservbyport", sock_s_getservbyport, -1);
2088  rb_define_singleton_method(rb_cSocket, "getaddrinfo", sock_s_getaddrinfo, -1);
2089  rb_define_singleton_method(rb_cSocket, "getnameinfo", sock_s_getnameinfo, -1);
2090  rb_define_singleton_method(rb_cSocket, "sockaddr_in", sock_s_pack_sockaddr_in, 2);
2091  rb_define_singleton_method(rb_cSocket, "pack_sockaddr_in", sock_s_pack_sockaddr_in, 2);
2092  rb_define_singleton_method(rb_cSocket, "unpack_sockaddr_in", sock_s_unpack_sockaddr_in, 1);
2093 #ifdef HAVE_SYS_UN_H
2094  rb_define_singleton_method(rb_cSocket, "sockaddr_un", sock_s_pack_sockaddr_un, 1);
2095  rb_define_singleton_method(rb_cSocket, "pack_sockaddr_un", sock_s_pack_sockaddr_un, 1);
2096  rb_define_singleton_method(rb_cSocket, "unpack_sockaddr_un", sock_s_unpack_sockaddr_un, 1);
2097 #endif
2098 
2100 
2101 #undef rb_intern
2102  sym_wait_writable = ID2SYM(rb_intern("wait_writable"));
2103 }
void rsock_syserr_fail_raddrinfo_or_sockaddr(int err, const char *mesg, VALUE addr, VALUE rai)
Definition: socket.c:94
#define rb_str_new4
Definition: intern.h:837
VALUE rb_ary_entry(VALUE ary, long offset)
Definition: array.c:1215
#define RARRAY_LEN(a)
Definition: ruby.h:1019
void rb_syserr_fail(int e, const char *mesg)
Definition: error.c:2391
int ioctl(int, int,...)
Definition: win32.c:2756
#define VALIDATE_SOCKLEN(addr, len)
Definition: sockport.h:16
size_t strlen(const char *)
#define Next(p, e, enc)
Definition: dir.c:214
#define INT2NUM(x)
Definition: ruby.h:1538
void rb_update_max_fd(int fd)
Definition: io.c:191
void rb_io_set_nonblock(rb_io_t *fptr)
Definition: io.c:2475
VALUE rb_cBasicSocket
Definition: init.c:13
#define NUM2INT(x)
Definition: ruby.h:684
void rb_define_singleton_method(VALUE obj, const char *name, VALUE(*func)(ANYARGS), int argc)
Defines a singleton method for obj.
Definition: class.c:1716
VALUE rsock_sockaddr_obj(struct sockaddr *addr, socklen_t len)
void rb_syserr_fail_str(int e, VALUE mesg)
Definition: error.c:2397
int rb_block_given_p(void)
Determines if the current method is given a block.
Definition: eval.c:835
void rb_raise(VALUE exc, const char *fmt,...)
Definition: error.c:2284
#define NI_DGRAM
Definition: addrinfo.h:128
VALUE rsock_make_ipaddr(struct sockaddr *addr, socklen_t addrlen)
Definition: raddrinfo.c:396
Definition: io.h:62
int rsock_socktype_arg(VALUE type)
Definition: constants.c:50
void rb_define_private_method(VALUE klass, const char *name, VALUE(*func)(ANYARGS), int argc)
Definition: class.c:1527
#define UNREACHABLE
Definition: ruby.h:46
st_table * names
Definition: encoding.c:58
VALUE rb_ary_push(VALUE ary, VALUE item)
Definition: array.c:924
VALUE rsock_init_sock(VALUE sock, int fd)
Definition: init.c:60
int rb_gc_for_fd(int err)
Definition: io.c:878
VALUE rsock_s_accept(VALUE klass, int fd, struct sockaddr *sockaddr, socklen_t *len)
Definition: init.c:595
VALUE rb_rescue(VALUE(*b_proc)(ANYARGS), VALUE data1, VALUE(*r_proc)(ANYARGS), VALUE data2)
An equivalent of rescue clause.
Definition: eval.c:967
VALUE rb_obj_alloc(VALUE)
Allocates an instance of klass.
Definition: object.c:2121
VALUE rb_check_sockaddr_string_type(VALUE val)
Definition: raddrinfo.c:2507
void Init_socket(void)
Definition: socket.c:1933
struct sockaddr * ifa_addr
Definition: win32.h:221
void rsock_syserr_fail_raddrinfo(int err, const char *mesg, VALUE rai)
Definition: socket.c:77
#define AI_CANONNAME
Definition: addrinfo.h:97
VALUE rsock_ipaddr(struct sockaddr *sockaddr, socklen_t sockaddrlen, int norevlookup)
Definition: raddrinfo.c:559
VALUE rb_ensure(VALUE(*b_proc)(ANYARGS), VALUE data1, VALUE(*e_proc)(ANYARGS), VALUE data2)
An equivalent to ensure clause.
Definition: eval.c:1035
#define EINPROGRESS
Definition: win32.h:477
#define FIXNUM_P(f)
Definition: ruby.h:365
int rb_w32_map_errno(DWORD)
Definition: win32.c:273
VALUE rsock_s_recvfrom_nonblock(VALUE sock, VALUE len, VALUE flg, VALUE str, VALUE ex, enum sock_recv_type from)
Definition: init.c:209
#define GetOpenFile(obj, fp)
Definition: io.h:120
void freeifaddrs(struct ifaddrs *)
Definition: win32.c:4191
VALUE rsock_io_socket_addrinfo(VALUE io, struct sockaddr *addr, socklen_t len)
Definition: raddrinfo.c:2534
socklen_t rsock_sockaddr_len(struct sockaddr *addr)
VALUE rb_eArgError
Definition: error.c:802
void rb_freeaddrinfo(struct rb_addrinfo *ai)
Definition: raddrinfo.c:322
VALUE rsock_make_hostent(VALUE host, struct rb_addrinfo *addr, VALUE(*ipaddr)(struct sockaddr *, socklen_t))
Definition: raddrinfo.c:704
#define RB_TYPE_P(obj, type)
Definition: ruby.h:527
VALUE rsock_addrinfo_new(struct sockaddr *addr, socklen_t len, int family, int socktype, int protocol, VALUE canonname, VALUE inspectname)
Definition: raddrinfo.c:798
VALUE rsock_s_accept_nonblock(VALUE klass, VALUE ex, rb_io_t *fptr, struct sockaddr *sockaddr, socklen_t *len)
Definition: init.c:553
void rsock_syserr_fail_host_port(int err, const char *mesg, VALUE host, VALUE port)
Definition: socket.c:24
#define MEMZERO(p, type, n)
Definition: ruby.h:1660
struct rb_addrinfo * rsock_addrinfo(VALUE host, VALUE port, int family, int socktype, int flags)
Definition: raddrinfo.c:547
VALUE rb_eRangeError
Definition: error.c:805
int getifaddrs(struct ifaddrs **)
Definition: win32.c:4104
#define RSTRING_SOCKLEN
Definition: rubysocket.h:124
struct ifaddrs * ifa_next
Definition: win32.h:218
int rsock_family_arg(VALUE domain)
Definition: constants.c:43
IUnknown DWORD
Definition: win32ole.c:32
void rsock_syserr_fail_path(int err, const char *mesg, VALUE path)
Definition: socket.c:41
VALUE rb_ary_new(void)
Definition: array.c:499
#define STRTOUL(str, endptr, base)
Definition: ruby.h:2162
int rsock_socket(int domain, int type, int proto)
Definition: init.c:357
#define snprintf
Definition: subst.h:6
#define NIL_P(v)
Definition: ruby.h:451
VALUE rb_define_class(const char *name, VALUE super)
Defines a top-level class.
Definition: class.c:646
int fd
Definition: io.h:64
char * ai_canonname
Definition: addrinfo.h:137
struct rb_addrinfo * rsock_getaddrinfo(VALUE host, VALUE port, struct addrinfo *hints, int socktype_hack)
Definition: raddrinfo.c:506
#define offsetof(p_type, field)
Definition: addrinfo.h:186
#define sock_gethostname
Definition: socket.c:932
void rb_notimplement(void)
Definition: error.c:2330
int argc
Definition: ruby.c:187
#define Qfalse
Definition: ruby.h:436
#define ALLOCA_N(type, n)
Definition: ruby.h:1593
#define rb_str_new2
Definition: intern.h:835
int err
Definition: win32.c:135
VALUE rb_cSocket
Definition: init.c:22
#define SockAddrStringValuePtr(v)
Definition: rubysocket.h:265
VALUE rb_str_resize(VALUE, long)
Definition: string.c:2644
void rb_sys_fail(const char *mesg)
Definition: error.c:2403
#define IS_IP_FAMILY(af)
Definition: rubysocket.h:156
#define RSTRING_LEN(str)
Definition: ruby.h:971
VALUE rb_yield(VALUE)
Definition: vm_eval.c:973
VALUE rsock_sock_listen(VALUE sock, VALUE log)
Definition: socket.c:644
int errno
int socklen_t
Definition: getaddrinfo.c:83
void rsock_syserr_fail_sockaddr(int err, const char *mesg, struct sockaddr *addr, socklen_t len)
Definition: socket.c:61
VALUE rsock_s_recvfrom(VALUE sock, int argc, VALUE *argv, enum sock_recv_type from)
Definition: init.c:146
VALUE rb_sprintf(const char *format,...)
Definition: sprintf.c:1452
int rb_scan_args(int argc, const VALUE *argv, const char *fmt,...)
Definition: class.c:1908
void rb_str_modify_expand(VALUE, long)
Definition: string.c:2054
unsigned char buf[MIME_BUF_SIZE]
Definition: nkf.c:4309
VALUE rb_assoc_new(VALUE car, VALUE cdr)
Definition: array.c:639
#define PRIsVALUE
Definition: ruby.h:135
#define Qnil
Definition: ruby.h:438
void rsock_init_basicsocket(void)
Definition: basicsocket.c:692
int rsock_do_not_reverse_lookup
Definition: init.c:31
#define SockAddrStringValueWithAddrinfo(v, rai_ret)
Definition: rubysocket.h:266
void rsock_sys_fail_sockaddr(const char *mesg, struct sockaddr *addr, socklen_t len)
Definition: socket.c:55
void rb_readwrite_syserr_fail(enum rb_io_wait_readwrite writable, int n, const char *mesg)
Definition: io.c:12453
void rsock_sys_fail_host_port(const char *mesg, VALUE host, VALUE port)
Definition: socket.c:18
unsigned long VALUE
Definition: ruby.h:85
int rsock_detect_cloexec(int fd)
Definition: init.c:284
#define AI_NUMERICHOST
Definition: addrinfo.h:98
VALUE rb_eTypeError
Definition: error.c:801
#define rb_tainted_str_new2
Definition: intern.h:839
#define rb_funcallv
Definition: console.c:21
register unsigned int len
Definition: zonetab.h:51
int ai_protocol
Definition: addrinfo.h:135
#define StringValueCStr(v)
Definition: ruby.h:571
int rb_getnameinfo(const struct sockaddr *sa, socklen_t salen, char *host, size_t hostlen, char *serv, size_t servlen, int flags)
Definition: raddrinfo.c:363
void rsock_sys_fail_raddrinfo(const char *mesg, VALUE rai)
Definition: socket.c:71
#define RSTRING_PTR(str)
Definition: ruby.h:975
VALUE rb_eSocket
Definition: init.c:25
#define RARRAY_ASET(a, i, v)
Definition: ruby.h:1034
#define INT2FIX(i)
Definition: ruby.h:232
#define EISCONN
Definition: win32.h:537
#define RARRAY_AREF(a, i)
Definition: ruby.h:1033
#define PF_UNSPEC
Definition: sockport.h:105
#define xmalloc
Definition: defines.h:183
int ai_socktype
Definition: addrinfo.h:134
int rsock_revlookup_flag(VALUE revlookup, int *norevlookup)
Definition: ipsocket.c:172
void rb_fd_fix_cloexec(int fd)
Definition: io.c:233
VALUE rb_check_array_type(VALUE ary)
Definition: array.c:651
void rsock_sys_fail_raddrinfo_or_sockaddr(const char *mesg, VALUE addr, VALUE rai)
Definition: socket.c:88
#define proto(p)
Definition: sdbm.h:60
void rsock_raise_socket_error(const char *reason, int error)
Definition: init.c:35
struct addrinfo * ai
Definition: rubysocket.h:285
int rsock_connect(int fd, const struct sockaddr *sockaddr, int len, int socks)
Definition: init.c:454
#define T_STRING
Definition: ruby.h:496
#define AF_UNSPEC
Definition: sockport.h:101
#define PRIuSIZE
Definition: ruby.h:177
struct rb_encoding_entry * list
Definition: encoding.c:55
#define OBJ_INFECT(x, s)
Definition: ruby.h:1302
int rb_getaddrinfo(const char *node, const char *service, const struct addrinfo *hints, struct rb_addrinfo **res)
Definition: raddrinfo.c:288
struct addrinfo * ai_next
Definition: addrinfo.h:139
const char * name
Definition: nkf.c:208
#define xrealloc
Definition: defines.h:186
#define ID2SYM(x)
Definition: ruby.h:383
void rsock_sys_fail_path(const char *mesg, VALUE path)
Definition: socket.c:35
size_t ai_addrlen
Definition: addrinfo.h:136
#define rsock_sock_s_socketpair
Definition: socket.c:308
int socketpair(int, int, int, int *)
Definition: win32.c:4037
void void xfree(void *)
#define rb_intern(str)
int ai_flags
Definition: addrinfo.h:132
#define SYMBOL_P(x)
Definition: ruby.h:382
#define NULL
Definition: _sdbm.c:102
void rsock_init_socket_init(void)
Definition: init.c:662
VALUE rsock_addrinfo_inspect_sockaddr(VALUE self)
Definition: raddrinfo.c:1517
void rb_define_method(VALUE klass, const char *name, VALUE(*func)(ANYARGS), int argc)
Definition: class.c:1515
struct sockaddr * ai_addr
Definition: addrinfo.h:138
#define NUM2LONG(x)
Definition: ruby.h:648
void rb_maygvl_fd_fix_cloexec(int fd)
Definition: io.c:210
char ** argv
Definition: ruby.c:188
#define StringValue(v)
Definition: ruby.h:569
char * ifa_name
Definition: win32.h:219
Definition: win32.h:217
struct sockaddr addr
Definition: rubysocket.h:187
#define socket_s_ip_address_list
Definition: socket.c:1929
VALUE rb_str_new(const char *, long)
Definition: string.c:737
int ai_family
Definition: addrinfo.h:133