Ruby 2.1.5p273(2014-11-13revision48405)

- Main Page
- Modules
- Data Structures
- Files

- File List
- Globals

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