PageRenderTime 60ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/ext/socket/socket.c

http://github.com/ruby/ruby
C | 2107 lines | 1103 code | 172 blank | 832 comment | 211 complexity | 5b3dc2eb9fa32cab6061d6a16cd10bcb MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause, AGPL-3.0

Large files files are truncated, but you can click here to view the full file

  1. /************************************************
  2. socket.c -
  3. created at: Thu Mar 31 12:21:29 JST 1994
  4. Copyright (C) 1993-2007 Yukihiro Matsumoto
  5. ************************************************/
  6. #include "rubysocket.h"
  7. static VALUE sym_wait_writable;
  8. static VALUE sock_s_unpack_sockaddr_in(VALUE, VALUE);
  9. void
  10. rsock_sys_fail_host_port(const char *mesg, VALUE host, VALUE port)
  11. {
  12. rsock_syserr_fail_host_port(errno, mesg, host, port);
  13. }
  14. void
  15. rsock_syserr_fail_host_port(int err, const char *mesg, VALUE host, VALUE port)
  16. {
  17. VALUE message;
  18. message = rb_sprintf("%s for %+"PRIsVALUE" port % "PRIsVALUE"",
  19. mesg, host, port);
  20. rb_syserr_fail_str(err, message);
  21. }
  22. void
  23. rsock_sys_fail_path(const char *mesg, VALUE path)
  24. {
  25. rsock_syserr_fail_path(errno, mesg, path);
  26. }
  27. void
  28. rsock_syserr_fail_path(int err, const char *mesg, VALUE path)
  29. {
  30. VALUE message;
  31. if (RB_TYPE_P(path, T_STRING)) {
  32. message = rb_sprintf("%s for % "PRIsVALUE"", mesg, path);
  33. rb_syserr_fail_str(err, message);
  34. }
  35. else {
  36. rb_syserr_fail(err, mesg);
  37. }
  38. }
  39. void
  40. rsock_sys_fail_sockaddr(const char *mesg, struct sockaddr *addr, socklen_t len)
  41. {
  42. rsock_syserr_fail_sockaddr(errno, mesg, addr, len);
  43. }
  44. void
  45. rsock_syserr_fail_sockaddr(int err, const char *mesg, struct sockaddr *addr, socklen_t len)
  46. {
  47. VALUE rai;
  48. rai = rsock_addrinfo_new(addr, len, PF_UNSPEC, 0, 0, Qnil, Qnil);
  49. rsock_syserr_fail_raddrinfo(err, mesg, rai);
  50. }
  51. void
  52. rsock_sys_fail_raddrinfo(const char *mesg, VALUE rai)
  53. {
  54. rsock_syserr_fail_raddrinfo(errno, mesg, rai);
  55. }
  56. void
  57. rsock_syserr_fail_raddrinfo(int err, const char *mesg, VALUE rai)
  58. {
  59. VALUE str, message;
  60. str = rsock_addrinfo_inspect_sockaddr(rai);
  61. message = rb_sprintf("%s for %"PRIsVALUE"", mesg, str);
  62. rb_syserr_fail_str(err, message);
  63. }
  64. void
  65. rsock_sys_fail_raddrinfo_or_sockaddr(const char *mesg, VALUE addr, VALUE rai)
  66. {
  67. rsock_syserr_fail_raddrinfo_or_sockaddr(errno, mesg, addr, rai);
  68. }
  69. void
  70. rsock_syserr_fail_raddrinfo_or_sockaddr(int err, const char *mesg, VALUE addr, VALUE rai)
  71. {
  72. if (NIL_P(rai)) {
  73. StringValue(addr);
  74. rsock_syserr_fail_sockaddr(err, mesg,
  75. (struct sockaddr *)RSTRING_PTR(addr),
  76. (socklen_t)RSTRING_LEN(addr)); /* overflow should be checked already */
  77. }
  78. else
  79. rsock_syserr_fail_raddrinfo(err, mesg, rai);
  80. }
  81. static void
  82. setup_domain_and_type(VALUE domain, int *dv, VALUE type, int *tv)
  83. {
  84. *dv = rsock_family_arg(domain);
  85. *tv = rsock_socktype_arg(type);
  86. }
  87. /*
  88. * call-seq:
  89. * Socket.new(domain, socktype [, protocol]) => socket
  90. *
  91. * Creates a new socket object.
  92. *
  93. * _domain_ should be a communications domain such as: :INET, :INET6, :UNIX, etc.
  94. *
  95. * _socktype_ should be a socket type such as: :STREAM, :DGRAM, :RAW, etc.
  96. *
  97. * _protocol_ is optional and should be a protocol defined in the domain.
  98. * If protocol is not given, 0 is used internally.
  99. *
  100. * Socket.new(:INET, :STREAM) # TCP socket
  101. * Socket.new(:INET, :DGRAM) # UDP socket
  102. * Socket.new(:UNIX, :STREAM) # UNIX stream socket
  103. * Socket.new(:UNIX, :DGRAM) # UNIX datagram socket
  104. */
  105. static VALUE
  106. sock_initialize(int argc, VALUE *argv, VALUE sock)
  107. {
  108. VALUE domain, type, protocol;
  109. int fd;
  110. int d, t;
  111. rb_scan_args(argc, argv, "21", &domain, &type, &protocol);
  112. if (NIL_P(protocol))
  113. protocol = INT2FIX(0);
  114. setup_domain_and_type(domain, &d, type, &t);
  115. fd = rsock_socket(d, t, NUM2INT(protocol));
  116. if (fd < 0) rb_sys_fail("socket(2)");
  117. return rsock_init_sock(sock, fd);
  118. }
  119. #if defined HAVE_SOCKETPAIR
  120. static VALUE
  121. io_call_close(VALUE io)
  122. {
  123. return rb_funcallv(io, rb_intern("close"), 0, 0);
  124. }
  125. static VALUE
  126. io_close(VALUE io)
  127. {
  128. return rb_rescue(io_call_close, io, 0, 0);
  129. }
  130. static VALUE
  131. pair_yield(VALUE pair)
  132. {
  133. return rb_ensure(rb_yield, pair, io_close, rb_ary_entry(pair, 1));
  134. }
  135. #endif
  136. #if defined HAVE_SOCKETPAIR
  137. #ifdef SOCK_CLOEXEC
  138. static int
  139. rsock_socketpair0(int domain, int type, int protocol, int sv[2])
  140. {
  141. int ret;
  142. static int cloexec_state = -1; /* <0: unknown, 0: ignored, >0: working */
  143. static const int default_flags = SOCK_CLOEXEC|RSOCK_NONBLOCK_DEFAULT;
  144. if (cloexec_state > 0) { /* common path, if SOCK_CLOEXEC is defined */
  145. ret = socketpair(domain, type|default_flags, protocol, sv);
  146. if (ret == 0 && (sv[0] <= 2 || sv[1] <= 2)) {
  147. goto fix_cloexec; /* highly unlikely */
  148. }
  149. goto update_max_fd;
  150. }
  151. else if (cloexec_state < 0) { /* usually runs once only for detection */
  152. ret = socketpair(domain, type|default_flags, protocol, sv);
  153. if (ret == 0) {
  154. cloexec_state = rsock_detect_cloexec(sv[0]);
  155. if ((cloexec_state == 0) || (sv[0] <= 2 || sv[1] <= 2))
  156. goto fix_cloexec;
  157. goto update_max_fd;
  158. }
  159. else if (ret == -1 && errno == EINVAL) {
  160. /* SOCK_CLOEXEC is available since Linux 2.6.27. Linux 2.6.18 fails with EINVAL */
  161. ret = socketpair(domain, type, protocol, sv);
  162. if (ret != -1) {
  163. /* The reason of EINVAL may be other than SOCK_CLOEXEC.
  164. * So disable SOCK_CLOEXEC only if socketpair() succeeds without SOCK_CLOEXEC.
  165. * Ex. Socket.pair(:UNIX, 0xff) fails with EINVAL.
  166. */
  167. cloexec_state = 0;
  168. }
  169. }
  170. }
  171. else { /* cloexec_state == 0 */
  172. ret = socketpair(domain, type, protocol, sv);
  173. }
  174. if (ret == -1) {
  175. return -1;
  176. }
  177. fix_cloexec:
  178. rb_maygvl_fd_fix_cloexec(sv[0]);
  179. rb_maygvl_fd_fix_cloexec(sv[1]);
  180. if (RSOCK_NONBLOCK_DEFAULT) {
  181. rsock_make_fd_nonblock(sv[0]);
  182. rsock_make_fd_nonblock(sv[1]);
  183. }
  184. update_max_fd:
  185. rb_update_max_fd(sv[0]);
  186. rb_update_max_fd(sv[1]);
  187. return ret;
  188. }
  189. #else /* !SOCK_CLOEXEC */
  190. static int
  191. rsock_socketpair0(int domain, int type, int protocol, int sv[2])
  192. {
  193. int ret = socketpair(domain, type, protocol, sv);
  194. if (ret == -1)
  195. return -1;
  196. rb_fd_fix_cloexec(sv[0]);
  197. rb_fd_fix_cloexec(sv[1]);
  198. if (RSOCK_NONBLOCK_DEFAULT) {
  199. rsock_make_fd_nonblock(sv[0]);
  200. rsock_make_fd_nonblock(sv[1]);
  201. }
  202. return ret;
  203. }
  204. #endif /* !SOCK_CLOEXEC */
  205. static int
  206. rsock_socketpair(int domain, int type, int protocol, int sv[2])
  207. {
  208. int ret;
  209. ret = rsock_socketpair0(domain, type, protocol, sv);
  210. if (ret < 0 && rb_gc_for_fd(errno)) {
  211. ret = rsock_socketpair0(domain, type, protocol, sv);
  212. }
  213. return ret;
  214. }
  215. /*
  216. * call-seq:
  217. * Socket.pair(domain, type, protocol) => [socket1, socket2]
  218. * Socket.socketpair(domain, type, protocol) => [socket1, socket2]
  219. *
  220. * Creates a pair of sockets connected each other.
  221. *
  222. * _domain_ should be a communications domain such as: :INET, :INET6, :UNIX, etc.
  223. *
  224. * _socktype_ should be a socket type such as: :STREAM, :DGRAM, :RAW, etc.
  225. *
  226. * _protocol_ should be a protocol defined in the domain,
  227. * defaults to 0 for the domain.
  228. *
  229. * s1, s2 = Socket.pair(:UNIX, :STREAM, 0)
  230. * s1.send "a", 0
  231. * s1.send "b", 0
  232. * s1.close
  233. * p s2.recv(10) #=> "ab"
  234. * p s2.recv(10) #=> ""
  235. * p s2.recv(10) #=> ""
  236. *
  237. * s1, s2 = Socket.pair(:UNIX, :DGRAM, 0)
  238. * s1.send "a", 0
  239. * s1.send "b", 0
  240. * p s2.recv(10) #=> "a"
  241. * p s2.recv(10) #=> "b"
  242. *
  243. */
  244. VALUE
  245. rsock_sock_s_socketpair(int argc, VALUE *argv, VALUE klass)
  246. {
  247. VALUE domain, type, protocol;
  248. int d, t, p, sp[2];
  249. int ret;
  250. VALUE s1, s2, r;
  251. rb_scan_args(argc, argv, "21", &domain, &type, &protocol);
  252. if (NIL_P(protocol))
  253. protocol = INT2FIX(0);
  254. setup_domain_and_type(domain, &d, type, &t);
  255. p = NUM2INT(protocol);
  256. ret = rsock_socketpair(d, t, p, sp);
  257. if (ret < 0) {
  258. rb_sys_fail("socketpair(2)");
  259. }
  260. s1 = rsock_init_sock(rb_obj_alloc(klass), sp[0]);
  261. s2 = rsock_init_sock(rb_obj_alloc(klass), sp[1]);
  262. r = rb_assoc_new(s1, s2);
  263. if (rb_block_given_p()) {
  264. return rb_ensure(pair_yield, r, io_close, s1);
  265. }
  266. return r;
  267. }
  268. #else
  269. #define rsock_sock_s_socketpair rb_f_notimplement
  270. #endif
  271. /*
  272. * call-seq:
  273. * socket.connect(remote_sockaddr) => 0
  274. *
  275. * Requests a connection to be made on the given +remote_sockaddr+. Returns 0 if
  276. * successful, otherwise an exception is raised.
  277. *
  278. * === Parameter
  279. * * +remote_sockaddr+ - the +struct+ sockaddr contained in a string or Addrinfo object
  280. *
  281. * === Example:
  282. * # Pull down Google's web page
  283. * require 'socket'
  284. * include Socket::Constants
  285. * socket = Socket.new( AF_INET, SOCK_STREAM, 0 )
  286. * sockaddr = Socket.pack_sockaddr_in( 80, 'www.google.com' )
  287. * socket.connect( sockaddr )
  288. * socket.write( "GET / HTTP/1.0\r\n\r\n" )
  289. * results = socket.read
  290. *
  291. * === Unix-based Exceptions
  292. * On unix-based systems the following system exceptions may be raised if
  293. * the call to _connect_ fails:
  294. * * Errno::EACCES - search permission is denied for a component of the prefix
  295. * path or write access to the +socket+ is denied
  296. * * Errno::EADDRINUSE - the _sockaddr_ is already in use
  297. * * Errno::EADDRNOTAVAIL - the specified _sockaddr_ is not available from the
  298. * local machine
  299. * * Errno::EAFNOSUPPORT - the specified _sockaddr_ is not a valid address for
  300. * the address family of the specified +socket+
  301. * * Errno::EALREADY - a connection is already in progress for the specified
  302. * socket
  303. * * Errno::EBADF - the +socket+ is not a valid file descriptor
  304. * * Errno::ECONNREFUSED - the target _sockaddr_ was not listening for connections
  305. * refused the connection request
  306. * * Errno::ECONNRESET - the remote host reset the connection request
  307. * * Errno::EFAULT - the _sockaddr_ cannot be accessed
  308. * * Errno::EHOSTUNREACH - the destination host cannot be reached (probably
  309. * because the host is down or a remote router cannot reach it)
  310. * * Errno::EINPROGRESS - the O_NONBLOCK is set for the +socket+ and the
  311. * connection cannot be immediately established; the connection will be
  312. * established asynchronously
  313. * * Errno::EINTR - the attempt to establish the connection was interrupted by
  314. * delivery of a signal that was caught; the connection will be established
  315. * asynchronously
  316. * * Errno::EISCONN - the specified +socket+ is already connected
  317. * * Errno::EINVAL - the address length used for the _sockaddr_ is not a valid
  318. * length for the address family or there is an invalid family in _sockaddr_
  319. * * Errno::ENAMETOOLONG - the pathname resolved had a length which exceeded
  320. * PATH_MAX
  321. * * Errno::ENETDOWN - the local interface used to reach the destination is down
  322. * * Errno::ENETUNREACH - no route to the network is present
  323. * * Errno::ENOBUFS - no buffer space is available
  324. * * Errno::ENOSR - there were insufficient STREAMS resources available to
  325. * complete the operation
  326. * * Errno::ENOTSOCK - the +socket+ argument does not refer to a socket
  327. * * Errno::EOPNOTSUPP - the calling +socket+ is listening and cannot be connected
  328. * * Errno::EPROTOTYPE - the _sockaddr_ has a different type than the socket
  329. * bound to the specified peer address
  330. * * Errno::ETIMEDOUT - the attempt to connect time out before a connection
  331. * was made.
  332. *
  333. * On unix-based systems if the address family of the calling +socket+ is
  334. * AF_UNIX the follow exceptions may be raised if the call to _connect_
  335. * fails:
  336. * * Errno::EIO - an i/o error occurred while reading from or writing to the
  337. * file system
  338. * * Errno::ELOOP - too many symbolic links were encountered in translating
  339. * the pathname in _sockaddr_
  340. * * Errno::ENAMETOOLLONG - a component of a pathname exceeded NAME_MAX
  341. * characters, or an entire pathname exceeded PATH_MAX characters
  342. * * Errno::ENOENT - a component of the pathname does not name an existing file
  343. * or the pathname is an empty string
  344. * * Errno::ENOTDIR - a component of the path prefix of the pathname in _sockaddr_
  345. * is not a directory
  346. *
  347. * === Windows Exceptions
  348. * On Windows systems the following system exceptions may be raised if
  349. * the call to _connect_ fails:
  350. * * Errno::ENETDOWN - the network is down
  351. * * Errno::EADDRINUSE - the socket's local address is already in use
  352. * * Errno::EINTR - the socket was cancelled
  353. * * Errno::EINPROGRESS - a blocking socket is in progress or the service provider
  354. * is still processing a callback function. Or a nonblocking connect call is
  355. * in progress on the +socket+.
  356. * * Errno::EALREADY - see Errno::EINVAL
  357. * * Errno::EADDRNOTAVAIL - the remote address is not a valid address, such as
  358. * ADDR_ANY TODO check ADDRANY TO INADDR_ANY
  359. * * Errno::EAFNOSUPPORT - addresses in the specified family cannot be used with
  360. * with this +socket+
  361. * * Errno::ECONNREFUSED - the target _sockaddr_ was not listening for connections
  362. * refused the connection request
  363. * * Errno::EFAULT - the socket's internal address or address length parameter
  364. * is too small or is not a valid part of the user space address
  365. * * Errno::EINVAL - the +socket+ is a listening socket
  366. * * Errno::EISCONN - the +socket+ is already connected
  367. * * Errno::ENETUNREACH - the network cannot be reached from this host at this time
  368. * * Errno::EHOSTUNREACH - no route to the network is present
  369. * * Errno::ENOBUFS - no buffer space is available
  370. * * Errno::ENOTSOCK - the +socket+ argument does not refer to a socket
  371. * * Errno::ETIMEDOUT - the attempt to connect time out before a connection
  372. * was made.
  373. * * Errno::EWOULDBLOCK - the socket is marked as nonblocking and the
  374. * connection cannot be completed immediately
  375. * * Errno::EACCES - the attempt to connect the datagram socket to the
  376. * broadcast address failed
  377. *
  378. * === See
  379. * * connect manual pages on unix-based systems
  380. * * connect function in Microsoft's Winsock functions reference
  381. */
  382. static VALUE
  383. sock_connect(VALUE sock, VALUE addr)
  384. {
  385. VALUE rai;
  386. rb_io_t *fptr;
  387. int fd, n;
  388. SockAddrStringValueWithAddrinfo(addr, rai);
  389. addr = rb_str_new4(addr);
  390. GetOpenFile(sock, fptr);
  391. fd = fptr->fd;
  392. n = rsock_connect(fd, (struct sockaddr*)RSTRING_PTR(addr), RSTRING_SOCKLEN(addr), 0);
  393. if (n < 0) {
  394. rsock_sys_fail_raddrinfo_or_sockaddr("connect(2)", addr, rai);
  395. }
  396. return INT2FIX(n);
  397. }
  398. /* :nodoc: */
  399. static VALUE
  400. sock_connect_nonblock(VALUE sock, VALUE addr, VALUE ex)
  401. {
  402. VALUE rai;
  403. rb_io_t *fptr;
  404. int n;
  405. SockAddrStringValueWithAddrinfo(addr, rai);
  406. addr = rb_str_new4(addr);
  407. GetOpenFile(sock, fptr);
  408. rb_io_set_nonblock(fptr);
  409. n = connect(fptr->fd, (struct sockaddr*)RSTRING_PTR(addr), RSTRING_SOCKLEN(addr));
  410. if (n < 0) {
  411. int e = errno;
  412. if (e == EINPROGRESS) {
  413. if (ex == Qfalse) {
  414. return sym_wait_writable;
  415. }
  416. rb_readwrite_syserr_fail(RB_IO_WAIT_WRITABLE, e, "connect(2) would block");
  417. }
  418. if (e == EISCONN) {
  419. if (ex == Qfalse) {
  420. return INT2FIX(0);
  421. }
  422. }
  423. rsock_syserr_fail_raddrinfo_or_sockaddr(e, "connect(2)", addr, rai);
  424. }
  425. return INT2FIX(n);
  426. }
  427. /*
  428. * call-seq:
  429. * socket.bind(local_sockaddr) => 0
  430. *
  431. * Binds to the given local address.
  432. *
  433. * === Parameter
  434. * * +local_sockaddr+ - the +struct+ sockaddr contained in a string or an Addrinfo object
  435. *
  436. * === Example
  437. * require 'socket'
  438. *
  439. * # use Addrinfo
  440. * socket = Socket.new(:INET, :STREAM, 0)
  441. * socket.bind(Addrinfo.tcp("127.0.0.1", 2222))
  442. * p socket.local_address #=> #<Addrinfo: 127.0.0.1:2222 TCP>
  443. *
  444. * # use struct sockaddr
  445. * include Socket::Constants
  446. * socket = Socket.new( AF_INET, SOCK_STREAM, 0 )
  447. * sockaddr = Socket.pack_sockaddr_in( 2200, 'localhost' )
  448. * socket.bind( sockaddr )
  449. *
  450. * === Unix-based Exceptions
  451. * On unix-based based systems the following system exceptions may be raised if
  452. * the call to _bind_ fails:
  453. * * Errno::EACCES - the specified _sockaddr_ is protected and the current
  454. * user does not have permission to bind to it
  455. * * Errno::EADDRINUSE - the specified _sockaddr_ is already in use
  456. * * Errno::EADDRNOTAVAIL - the specified _sockaddr_ is not available from the
  457. * local machine
  458. * * Errno::EAFNOSUPPORT - the specified _sockaddr_ is not a valid address for
  459. * the family of the calling +socket+
  460. * * Errno::EBADF - the _sockaddr_ specified is not a valid file descriptor
  461. * * Errno::EFAULT - the _sockaddr_ argument cannot be accessed
  462. * * Errno::EINVAL - the +socket+ is already bound to an address, and the
  463. * protocol does not support binding to the new _sockaddr_ or the +socket+
  464. * has been shut down.
  465. * * Errno::EINVAL - the address length is not a valid length for the address
  466. * family
  467. * * Errno::ENAMETOOLONG - the pathname resolved had a length which exceeded
  468. * PATH_MAX
  469. * * Errno::ENOBUFS - no buffer space is available
  470. * * Errno::ENOSR - there were insufficient STREAMS resources available to
  471. * complete the operation
  472. * * Errno::ENOTSOCK - the +socket+ does not refer to a socket
  473. * * Errno::EOPNOTSUPP - the socket type of the +socket+ does not support
  474. * binding to an address
  475. *
  476. * On unix-based based systems if the address family of the calling +socket+ is
  477. * Socket::AF_UNIX the follow exceptions may be raised if the call to _bind_
  478. * fails:
  479. * * Errno::EACCES - search permission is denied for a component of the prefix
  480. * path or write access to the +socket+ is denied
  481. * * Errno::EDESTADDRREQ - the _sockaddr_ argument is a null pointer
  482. * * Errno::EISDIR - same as Errno::EDESTADDRREQ
  483. * * Errno::EIO - an i/o error occurred
  484. * * Errno::ELOOP - too many symbolic links were encountered in translating
  485. * the pathname in _sockaddr_
  486. * * Errno::ENAMETOOLLONG - a component of a pathname exceeded NAME_MAX
  487. * characters, or an entire pathname exceeded PATH_MAX characters
  488. * * Errno::ENOENT - a component of the pathname does not name an existing file
  489. * or the pathname is an empty string
  490. * * Errno::ENOTDIR - a component of the path prefix of the pathname in _sockaddr_
  491. * is not a directory
  492. * * Errno::EROFS - the name would reside on a read only filesystem
  493. *
  494. * === Windows Exceptions
  495. * On Windows systems the following system exceptions may be raised if
  496. * the call to _bind_ fails:
  497. * * Errno::ENETDOWN-- the network is down
  498. * * Errno::EACCES - the attempt to connect the datagram socket to the
  499. * broadcast address failed
  500. * * Errno::EADDRINUSE - the socket's local address is already in use
  501. * * Errno::EADDRNOTAVAIL - the specified address is not a valid address for this
  502. * computer
  503. * * Errno::EFAULT - the socket's internal address or address length parameter
  504. * is too small or is not a valid part of the user space addressed
  505. * * Errno::EINVAL - the +socket+ is already bound to an address
  506. * * Errno::ENOBUFS - no buffer space is available
  507. * * Errno::ENOTSOCK - the +socket+ argument does not refer to a socket
  508. *
  509. * === See
  510. * * bind manual pages on unix-based systems
  511. * * bind function in Microsoft's Winsock functions reference
  512. */
  513. static VALUE
  514. sock_bind(VALUE sock, VALUE addr)
  515. {
  516. VALUE rai;
  517. rb_io_t *fptr;
  518. SockAddrStringValueWithAddrinfo(addr, rai);
  519. GetOpenFile(sock, fptr);
  520. if (bind(fptr->fd, (struct sockaddr*)RSTRING_PTR(addr), RSTRING_SOCKLEN(addr)) < 0)
  521. rsock_sys_fail_raddrinfo_or_sockaddr("bind(2)", addr, rai);
  522. return INT2FIX(0);
  523. }
  524. /*
  525. * call-seq:
  526. * socket.listen( int ) => 0
  527. *
  528. * Listens for connections, using the specified +int+ as the backlog. A call
  529. * to _listen_ only applies if the +socket+ is of type SOCK_STREAM or
  530. * SOCK_SEQPACKET.
  531. *
  532. * === Parameter
  533. * * +backlog+ - the maximum length of the queue for pending connections.
  534. *
  535. * === Example 1
  536. * require 'socket'
  537. * include Socket::Constants
  538. * socket = Socket.new( AF_INET, SOCK_STREAM, 0 )
  539. * sockaddr = Socket.pack_sockaddr_in( 2200, 'localhost' )
  540. * socket.bind( sockaddr )
  541. * socket.listen( 5 )
  542. *
  543. * === Example 2 (listening on an arbitrary port, unix-based systems only):
  544. * require 'socket'
  545. * include Socket::Constants
  546. * socket = Socket.new( AF_INET, SOCK_STREAM, 0 )
  547. * socket.listen( 1 )
  548. *
  549. * === Unix-based Exceptions
  550. * On unix based systems the above will work because a new +sockaddr+ struct
  551. * is created on the address ADDR_ANY, for an arbitrary port number as handed
  552. * off by the kernel. It will not work on Windows, because Windows requires that
  553. * the +socket+ is bound by calling _bind_ before it can _listen_.
  554. *
  555. * If the _backlog_ amount exceeds the implementation-dependent maximum
  556. * queue length, the implementation's maximum queue length will be used.
  557. *
  558. * On unix-based based systems the following system exceptions may be raised if the
  559. * call to _listen_ fails:
  560. * * Errno::EBADF - the _socket_ argument is not a valid file descriptor
  561. * * Errno::EDESTADDRREQ - the _socket_ is not bound to a local address, and
  562. * the protocol does not support listening on an unbound socket
  563. * * Errno::EINVAL - the _socket_ is already connected
  564. * * Errno::ENOTSOCK - the _socket_ argument does not refer to a socket
  565. * * Errno::EOPNOTSUPP - the _socket_ protocol does not support listen
  566. * * Errno::EACCES - the calling process does not have appropriate privileges
  567. * * Errno::EINVAL - the _socket_ has been shut down
  568. * * Errno::ENOBUFS - insufficient resources are available in the system to
  569. * complete the call
  570. *
  571. * === Windows Exceptions
  572. * On Windows systems the following system exceptions may be raised if
  573. * the call to _listen_ fails:
  574. * * Errno::ENETDOWN - the network is down
  575. * * Errno::EADDRINUSE - the socket's local address is already in use. This
  576. * usually occurs during the execution of _bind_ but could be delayed
  577. * if the call to _bind_ was to a partially wildcard address (involving
  578. * ADDR_ANY) and if a specific address needs to be committed at the
  579. * time of the call to _listen_
  580. * * Errno::EINPROGRESS - a Windows Sockets 1.1 call is in progress or the
  581. * service provider is still processing a callback function
  582. * * Errno::EINVAL - the +socket+ has not been bound with a call to _bind_.
  583. * * Errno::EISCONN - the +socket+ is already connected
  584. * * Errno::EMFILE - no more socket descriptors are available
  585. * * Errno::ENOBUFS - no buffer space is available
  586. * * Errno::ENOTSOC - +socket+ is not a socket
  587. * * Errno::EOPNOTSUPP - the referenced +socket+ is not a type that supports
  588. * the _listen_ method
  589. *
  590. * === See
  591. * * listen manual pages on unix-based systems
  592. * * listen function in Microsoft's Winsock functions reference
  593. */
  594. VALUE
  595. rsock_sock_listen(VALUE sock, VALUE log)
  596. {
  597. rb_io_t *fptr;
  598. int backlog;
  599. backlog = NUM2INT(log);
  600. GetOpenFile(sock, fptr);
  601. if (listen(fptr->fd, backlog) < 0)
  602. rb_sys_fail("listen(2)");
  603. return INT2FIX(0);
  604. }
  605. /*
  606. * call-seq:
  607. * socket.recvfrom(maxlen) => [mesg, sender_addrinfo]
  608. * socket.recvfrom(maxlen, flags) => [mesg, sender_addrinfo]
  609. *
  610. * Receives up to _maxlen_ bytes from +socket+. _flags_ is zero or more
  611. * of the +MSG_+ options. The first element of the results, _mesg_, is the data
  612. * received. The second element, _sender_addrinfo_, contains protocol-specific
  613. * address information of the sender.
  614. *
  615. * === Parameters
  616. * * +maxlen+ - the maximum number of bytes to receive from the socket
  617. * * +flags+ - zero or more of the +MSG_+ options
  618. *
  619. * === Example
  620. * # In one file, start this first
  621. * require 'socket'
  622. * include Socket::Constants
  623. * socket = Socket.new( AF_INET, SOCK_STREAM, 0 )
  624. * sockaddr = Socket.pack_sockaddr_in( 2200, 'localhost' )
  625. * socket.bind( sockaddr )
  626. * socket.listen( 5 )
  627. * client, client_addrinfo = socket.accept
  628. * data = client.recvfrom( 20 )[0].chomp
  629. * puts "I only received 20 bytes '#{data}'"
  630. * sleep 1
  631. * socket.close
  632. *
  633. * # In another file, start this second
  634. * require 'socket'
  635. * include Socket::Constants
  636. * socket = Socket.new( AF_INET, SOCK_STREAM, 0 )
  637. * sockaddr = Socket.pack_sockaddr_in( 2200, 'localhost' )
  638. * socket.connect( sockaddr )
  639. * socket.puts "Watch this get cut short!"
  640. * socket.close
  641. *
  642. * === Unix-based Exceptions
  643. * On unix-based based systems the following system exceptions may be raised if the
  644. * call to _recvfrom_ fails:
  645. * * Errno::EAGAIN - the +socket+ file descriptor is marked as O_NONBLOCK and no
  646. * data is waiting to be received; or MSG_OOB is set and no out-of-band data
  647. * is available and either the +socket+ file descriptor is marked as
  648. * O_NONBLOCK or the +socket+ does not support blocking to wait for
  649. * out-of-band-data
  650. * * Errno::EWOULDBLOCK - see Errno::EAGAIN
  651. * * Errno::EBADF - the +socket+ is not a valid file descriptor
  652. * * Errno::ECONNRESET - a connection was forcibly closed by a peer
  653. * * Errno::EFAULT - the socket's internal buffer, address or address length
  654. * cannot be accessed or written
  655. * * Errno::EINTR - a signal interrupted _recvfrom_ before any data was available
  656. * * Errno::EINVAL - the MSG_OOB flag is set and no out-of-band data is available
  657. * * Errno::EIO - an i/o error occurred while reading from or writing to the
  658. * filesystem
  659. * * Errno::ENOBUFS - insufficient resources were available in the system to
  660. * perform the operation
  661. * * Errno::ENOMEM - insufficient memory was available to fulfill the request
  662. * * Errno::ENOSR - there were insufficient STREAMS resources available to
  663. * complete the operation
  664. * * Errno::ENOTCONN - a receive is attempted on a connection-mode socket that
  665. * is not connected
  666. * * Errno::ENOTSOCK - the +socket+ does not refer to a socket
  667. * * Errno::EOPNOTSUPP - the specified flags are not supported for this socket type
  668. * * Errno::ETIMEDOUT - the connection timed out during connection establishment
  669. * or due to a transmission timeout on an active connection
  670. *
  671. * === Windows Exceptions
  672. * On Windows systems the following system exceptions may be raised if
  673. * the call to _recvfrom_ fails:
  674. * * Errno::ENETDOWN - the network is down
  675. * * Errno::EFAULT - the internal buffer and from parameters on +socket+ are not
  676. * part of the user address space, or the internal fromlen parameter is
  677. * too small to accommodate the peer address
  678. * * Errno::EINTR - the (blocking) call was cancelled by an internal call to
  679. * the WinSock function WSACancelBlockingCall
  680. * * Errno::EINPROGRESS - a blocking Windows Sockets 1.1 call is in progress or
  681. * the service provider is still processing a callback function
  682. * * Errno::EINVAL - +socket+ has not been bound with a call to _bind_, or an
  683. * unknown flag was specified, or MSG_OOB was specified for a socket with
  684. * SO_OOBINLINE enabled, or (for byte stream-style sockets only) the internal
  685. * len parameter on +socket+ was zero or negative
  686. * * Errno::EISCONN - +socket+ is already connected. The call to _recvfrom_ is
  687. * not permitted with a connected socket on a socket that is connection
  688. * oriented or connectionless.
  689. * * Errno::ENETRESET - the connection has been broken due to the keep-alive
  690. * activity detecting a failure while the operation was in progress.
  691. * * Errno::EOPNOTSUPP - MSG_OOB was specified, but +socket+ is not stream-style
  692. * such as type SOCK_STREAM. OOB data is not supported in the communication
  693. * domain associated with +socket+, or +socket+ is unidirectional and
  694. * supports only send operations
  695. * * Errno::ESHUTDOWN - +socket+ has been shutdown. It is not possible to
  696. * call _recvfrom_ on a socket after _shutdown_ has been invoked.
  697. * * Errno::EWOULDBLOCK - +socket+ is marked as nonblocking and a call to
  698. * _recvfrom_ would block.
  699. * * Errno::EMSGSIZE - the message was too large to fit into the specified buffer
  700. * and was truncated.
  701. * * Errno::ETIMEDOUT - the connection has been dropped, because of a network
  702. * failure or because the system on the other end went down without
  703. * notice
  704. * * Errno::ECONNRESET - the virtual circuit was reset by the remote side
  705. * executing a hard or abortive close. The application should close the
  706. * socket; it is no longer usable. On a UDP-datagram socket this error
  707. * indicates a previous send operation resulted in an ICMP Port Unreachable
  708. * message.
  709. */
  710. static VALUE
  711. sock_recvfrom(int argc, VALUE *argv, VALUE sock)
  712. {
  713. return rsock_s_recvfrom(sock, argc, argv, RECV_SOCKET);
  714. }
  715. /* :nodoc: */
  716. static VALUE
  717. sock_recvfrom_nonblock(VALUE sock, VALUE len, VALUE flg, VALUE str, VALUE ex)
  718. {
  719. return rsock_s_recvfrom_nonblock(sock, len, flg, str, ex, RECV_SOCKET);
  720. }
  721. /*
  722. * call-seq:
  723. * socket.accept => [client_socket, client_addrinfo]
  724. *
  725. * Accepts a next connection.
  726. * Returns a new Socket object and Addrinfo object.
  727. *
  728. * serv = Socket.new(:INET, :STREAM, 0)
  729. * serv.listen(5)
  730. * c = Socket.new(:INET, :STREAM, 0)
  731. * c.connect(serv.connect_address)
  732. * p serv.accept #=> [#<Socket:fd 6>, #<Addrinfo: 127.0.0.1:48555 TCP>]
  733. *
  734. */
  735. static VALUE
  736. sock_accept(VALUE sock)
  737. {
  738. rb_io_t *fptr;
  739. VALUE sock2;
  740. union_sockaddr buf;
  741. socklen_t len = (socklen_t)sizeof buf;
  742. GetOpenFile(sock, fptr);
  743. sock2 = rsock_s_accept(rb_cSocket,fptr->fd,&buf.addr,&len);
  744. return rb_assoc_new(sock2, rsock_io_socket_addrinfo(sock2, &buf.addr, len));
  745. }
  746. /* :nodoc: */
  747. static VALUE
  748. sock_accept_nonblock(VALUE sock, VALUE ex)
  749. {
  750. rb_io_t *fptr;
  751. VALUE sock2;
  752. union_sockaddr buf;
  753. struct sockaddr *addr = &buf.addr;
  754. socklen_t len = (socklen_t)sizeof buf;
  755. GetOpenFile(sock, fptr);
  756. sock2 = rsock_s_accept_nonblock(rb_cSocket, ex, fptr, addr, &len);
  757. if (SYMBOL_P(sock2)) /* :wait_readable */
  758. return sock2;
  759. return rb_assoc_new(sock2, rsock_io_socket_addrinfo(sock2, &buf.addr, len));
  760. }
  761. /*
  762. * call-seq:
  763. * socket.sysaccept => [client_socket_fd, client_addrinfo]
  764. *
  765. * Accepts an incoming connection returning an array containing the (integer)
  766. * file descriptor for the incoming connection, _client_socket_fd_,
  767. * and an Addrinfo, _client_addrinfo_.
  768. *
  769. * === Example
  770. * # In one script, start this first
  771. * require 'socket'
  772. * include Socket::Constants
  773. * socket = Socket.new( AF_INET, SOCK_STREAM, 0 )
  774. * sockaddr = Socket.pack_sockaddr_in( 2200, 'localhost' )
  775. * socket.bind( sockaddr )
  776. * socket.listen( 5 )
  777. * client_fd, client_addrinfo = socket.sysaccept
  778. * client_socket = Socket.for_fd( client_fd )
  779. * puts "The client said, '#{client_socket.readline.chomp}'"
  780. * client_socket.puts "Hello from script one!"
  781. * socket.close
  782. *
  783. * # In another script, start this second
  784. * require 'socket'
  785. * include Socket::Constants
  786. * socket = Socket.new( AF_INET, SOCK_STREAM, 0 )
  787. * sockaddr = Socket.pack_sockaddr_in( 2200, 'localhost' )
  788. * socket.connect( sockaddr )
  789. * socket.puts "Hello from script 2."
  790. * puts "The server said, '#{socket.readline.chomp}'"
  791. * socket.close
  792. *
  793. * Refer to Socket#accept for the exceptions that may be thrown if the call
  794. * to _sysaccept_ fails.
  795. *
  796. * === See
  797. * * Socket#accept
  798. */
  799. static VALUE
  800. sock_sysaccept(VALUE sock)
  801. {
  802. rb_io_t *fptr;
  803. VALUE sock2;
  804. union_sockaddr buf;
  805. socklen_t len = (socklen_t)sizeof buf;
  806. GetOpenFile(sock, fptr);
  807. sock2 = rsock_s_accept(0,fptr->fd,&buf.addr,&len);
  808. return rb_assoc_new(sock2, rsock_io_socket_addrinfo(sock2, &buf.addr, len));
  809. }
  810. #ifdef HAVE_GETHOSTNAME
  811. /*
  812. * call-seq:
  813. * Socket.gethostname => hostname
  814. *
  815. * Returns the hostname.
  816. *
  817. * p Socket.gethostname #=> "hal"
  818. *
  819. * Note that it is not guaranteed to be able to convert to IP address using gethostbyname, getaddrinfo, etc.
  820. * If you need local IP address, use Socket.ip_address_list.
  821. */
  822. static VALUE
  823. sock_gethostname(VALUE obj)
  824. {
  825. #if defined(NI_MAXHOST)
  826. # define RUBY_MAX_HOST_NAME_LEN NI_MAXHOST
  827. #elif defined(HOST_NAME_MAX)
  828. # define RUBY_MAX_HOST_NAME_LEN HOST_NAME_MAX
  829. #else
  830. # define RUBY_MAX_HOST_NAME_LEN 1024
  831. #endif
  832. long len = RUBY_MAX_HOST_NAME_LEN;
  833. VALUE name;
  834. name = rb_str_new(0, len);
  835. while (gethostname(RSTRING_PTR(name), len) < 0) {
  836. int e = errno;
  837. switch (e) {
  838. case ENAMETOOLONG:
  839. #ifdef __linux__
  840. case EINVAL:
  841. /* glibc before version 2.1 uses EINVAL instead of ENAMETOOLONG */
  842. #endif
  843. break;
  844. default:
  845. rb_syserr_fail(e, "gethostname(3)");
  846. }
  847. rb_str_modify_expand(name, len);
  848. len += len;
  849. }
  850. rb_str_resize(name, strlen(RSTRING_PTR(name)));
  851. return name;
  852. }
  853. #else
  854. #ifdef HAVE_UNAME
  855. #include <sys/utsname.h>
  856. static VALUE
  857. sock_gethostname(VALUE obj)
  858. {
  859. struct utsname un;
  860. uname(&un);
  861. return rb_str_new2(un.nodename);
  862. }
  863. #else
  864. #define sock_gethostname rb_f_notimplement
  865. #endif
  866. #endif
  867. static VALUE
  868. make_addrinfo(struct rb_addrinfo *res0, int norevlookup)
  869. {
  870. VALUE base, ary;
  871. struct addrinfo *res;
  872. if (res0 == NULL) {
  873. rb_raise(rb_eSocket, "host not found");
  874. }
  875. base = rb_ary_new();
  876. for (res = res0->ai; res; res = res->ai_next) {
  877. ary = rsock_ipaddr(res->ai_addr, res->ai_addrlen, norevlookup);
  878. if (res->ai_canonname) {
  879. RARRAY_ASET(ary, 2, rb_str_new2(res->ai_canonname));
  880. }
  881. rb_ary_push(ary, INT2FIX(res->ai_family));
  882. rb_ary_push(ary, INT2FIX(res->ai_socktype));
  883. rb_ary_push(ary, INT2FIX(res->ai_protocol));
  884. rb_ary_push(base, ary);
  885. }
  886. return base;
  887. }
  888. static VALUE
  889. sock_sockaddr(struct sockaddr *addr, socklen_t len)
  890. {
  891. char *ptr;
  892. switch (addr->sa_family) {
  893. case AF_INET:
  894. ptr = (char*)&((struct sockaddr_in*)addr)->sin_addr.s_addr;
  895. len = (socklen_t)sizeof(((struct sockaddr_in*)addr)->sin_addr.s_addr);
  896. break;
  897. #ifdef AF_INET6
  898. case AF_INET6:
  899. ptr = (char*)&((struct sockaddr_in6*)addr)->sin6_addr.s6_addr;
  900. len = (socklen_t)sizeof(((struct sockaddr_in6*)addr)->sin6_addr.s6_addr);
  901. break;
  902. #endif
  903. default:
  904. rb_raise(rb_eSocket, "unknown socket family:%d", addr->sa_family);
  905. break;
  906. }
  907. return rb_str_new(ptr, len);
  908. }
  909. /*
  910. * call-seq:
  911. * Socket.gethostbyname(hostname) => [official_hostname, alias_hostnames, address_family, *address_list]
  912. *
  913. * Use Addrinfo.getaddrinfo instead.
  914. * This method is deprecated for the following reasons:
  915. *
  916. * - The 3rd element of the result is the address family of the first address.
  917. * The address families of the rest of the addresses are not returned.
  918. * - Uncommon address representation:
  919. * 4/16-bytes binary string to represent IPv4/IPv6 address.
  920. * - gethostbyname() may take a long time and it may block other threads.
  921. * (GVL cannot be released since gethostbyname() is not thread safe.)
  922. * - This method uses gethostbyname() function already removed from POSIX.
  923. *
  924. * This method obtains the host information for _hostname_.
  925. *
  926. * p Socket.gethostbyname("hal") #=> ["localhost", ["hal"], 2, "\x7F\x00\x00\x01"]
  927. *
  928. */
  929. static VALUE
  930. sock_s_gethostbyname(VALUE obj, VALUE host)
  931. {
  932. struct rb_addrinfo *res =
  933. rsock_addrinfo(host, Qnil, AF_UNSPEC, SOCK_STREAM, AI_CANONNAME);
  934. return rsock_make_hostent(host, res, sock_sockaddr);
  935. }
  936. /*
  937. * call-seq:
  938. * Socket.gethostbyaddr(address_string [, address_family]) => hostent
  939. *
  940. * Use Addrinfo#getnameinfo instead.
  941. * This method is deprecated for the following reasons:
  942. *
  943. * - Uncommon address representation:
  944. * 4/16-bytes binary string to represent IPv4/IPv6 address.
  945. * - gethostbyaddr() may take a long time and it may block other threads.
  946. * (GVL cannot be released since gethostbyname() is not thread safe.)
  947. * - This method uses gethostbyname() function already removed from POSIX.
  948. *
  949. * This method obtains the host information for _address_.
  950. *
  951. * p Socket.gethostbyaddr([221,186,184,68].pack("CCCC"))
  952. * #=> ["carbon.ruby-lang.org", [], 2, "\xDD\xBA\xB8D"]
  953. *
  954. * p Socket.gethostbyaddr([127,0,0,1].pack("CCCC"))
  955. * ["localhost", [], 2, "\x7F\x00\x00\x01"]
  956. * p Socket.gethostbyaddr(([0]*15+[1]).pack("C"*16))
  957. * #=> ["localhost", ["ip6-localhost", "ip6-loopback"], 10,
  958. * "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01"]
  959. *
  960. */
  961. static VALUE
  962. sock_s_gethostbyaddr(int argc, VALUE *argv, VALUE _)
  963. {
  964. VALUE addr, family;
  965. struct hostent *h;
  966. char **pch;
  967. VALUE ary, names;
  968. int t = AF_INET;
  969. rb_scan_args(argc, argv, "11", &addr, &family);
  970. StringValue(addr);
  971. if (!NIL_P(family)) {
  972. t = rsock_family_arg(family);
  973. }
  974. #ifdef AF_INET6
  975. else if (RSTRING_LEN(addr) == 16) {
  976. t = AF_INET6;
  977. }
  978. #endif
  979. h = gethostbyaddr(RSTRING_PTR(addr), RSTRING_SOCKLEN(addr), t);
  980. if (h == NULL) {
  981. #ifdef HAVE_HSTRERROR
  982. extern int h_errno;
  983. rb_raise(rb_eSocket, "%s", (char*)hstrerror(h_errno));
  984. #else
  985. rb_raise(rb_eSocket, "host not found");
  986. #endif
  987. }
  988. ary = rb_ary_new();
  989. rb_ary_push(ary, rb_str_new2(h->h_name));
  990. names = rb_ary_new();
  991. rb_ary_push(ary, names);
  992. if (h->h_aliases != NULL) {
  993. for (pch = h->h_aliases; *pch; pch++) {
  994. rb_ary_push(names, rb_str_new2(*pch));
  995. }
  996. }
  997. rb_ary_push(ary, INT2NUM(h->h_addrtype));
  998. #ifdef h_addr
  999. for (pch = h->h_addr_list; *pch; pch++) {
  1000. rb_ary_push(ary, rb_str_new(*pch, h->h_length));
  1001. }
  1002. #else
  1003. rb_ary_push(ary, rb_str_new(h->h_addr, h->h_length));
  1004. #endif
  1005. return ary;
  1006. }
  1007. /*
  1008. * call-seq:
  1009. * Socket.getservbyname(service_name) => port_number
  1010. * Socket.getservbyname(service_name, protocol_name) => port_number
  1011. *
  1012. * Obtains the port number for _service_name_.
  1013. *
  1014. * If _protocol_name_ is not given, "tcp" is assumed.
  1015. *
  1016. * Socket.getservbyname("smtp") #=> 25
  1017. * Socket.getservbyname("shell") #=> 514
  1018. * Socket.getservbyname("syslog", "udp") #=> 514
  1019. */
  1020. static VALUE
  1021. sock_s_getservbyname(int argc, VALUE *argv, VALUE _)
  1022. {
  1023. VALUE service, proto;
  1024. struct servent *sp;
  1025. long port;
  1026. const char *servicename, *protoname = "tcp";
  1027. rb_scan_args(argc, argv, "11", &service, &proto);
  1028. StringValue(service);
  1029. if (!NIL_P(proto)) StringValue(proto);
  1030. servicename = StringValueCStr(service);
  1031. if (!NIL_P(proto)) protoname = StringValueCStr(proto);
  1032. sp = getservbyname(servicename, protoname);
  1033. if (sp) {
  1034. port = ntohs(sp->s_port);
  1035. }
  1036. else {
  1037. char *end;
  1038. port = STRTOUL(servicename, &end, 0);
  1039. if (*end != '\0') {
  1040. rb_raise(rb_eSocket, "no such service %s/%s", servicename, protoname);
  1041. }
  1042. }
  1043. return INT2FIX(port);
  1044. }
  1045. /*
  1046. * call-seq:
  1047. * Socket.getservbyport(port [, protocol_name]) => service
  1048. *
  1049. * Obtains the port number for _port_.
  1050. *
  1051. * If _protocol_name_ is not given, "tcp" is assumed.
  1052. *
  1053. * Socket.getservbyport(80) #=> "www"
  1054. * Socket.getservbyport(514, "tcp") #=> "shell"
  1055. * Socket.getservbyport(514, "udp") #=> "syslog"
  1056. *
  1057. */
  1058. static VALUE
  1059. sock_s_getservbyport(int argc, VALUE *argv, VALUE _)
  1060. {
  1061. VALUE port, proto;
  1062. struct servent *sp;
  1063. long portnum;
  1064. const char *protoname = "tcp";
  1065. rb_scan_args(argc, argv, "11", &port, &proto);
  1066. portnum = NUM2LONG(port);
  1067. if (portnum != (uint16_t)portnum) {
  1068. const char *s = portnum > 0 ? "big" : "small";
  1069. rb_raise(rb_eRangeError, "integer %ld too %s to convert into `int16_t'", portnum, s);
  1070. }
  1071. if (!NIL_P(proto)) protoname = StringValueCStr(proto);
  1072. sp = getservbyport((int)htons((uint16_t)portnum), protoname);
  1073. if (!sp) {
  1074. rb_raise(rb_eSocket, "no such service for port %d/%s", (int)portnum, protoname);
  1075. }
  1076. return rb_str_new2(sp->s_name);
  1077. }
  1078. /*
  1079. * call-seq:
  1080. * Socket.getaddrinfo(nodename, servname[, family[, socktype[, protocol[, flags[, reverse_lookup]]]]]) => array
  1081. *
  1082. * Obtains address information for _nodename_:_servname_.
  1083. *
  1084. * Note that Addrinfo.getaddrinfo provides the same functionality in
  1085. * an object oriented style.
  1086. *
  1087. * _family_ should be an address family such as: :INET, :INET6, etc.
  1088. *
  1089. * _socktype_ should be a socket type such as: :STREAM, :DGRAM, :RAW, etc.
  1090. *
  1091. * _protocol_ should be a protocol defined in the family,
  1092. * and defaults to 0 for the family.
  1093. *
  1094. * _flags_ should be bitwise OR of Socket::AI_* constants.
  1095. *
  1096. * Socket.getaddrinfo("www.ruby-lang.org", "http", nil, :STREAM)
  1097. * #=> [["AF_INET", 80, "carbon.ruby-lang.org", "221.186.184.68", 2, 1, 6]] # PF_INET/SOCK_STREAM/IPPROTO_TCP
  1098. *
  1099. * Socket.getaddrinfo("localhost", nil)
  1100. * #=> [["AF_INET", 0, "localhost", "127.0.0.1", 2, 1, 6], # PF_INET/SOCK_STREAM/IPPROTO_TCP
  1101. * # ["AF_INET", 0, "localhost", "127.0.0.1", 2, 2, 17], # PF_INET/SOCK_DGRAM/IPPROTO_UDP
  1102. * # ["AF_INET", 0, "localhost", "127.0.0.1", 2, 3, 0]] # PF_INET/SOCK_RAW/IPPROTO_IP
  1103. *
  1104. * _reverse_lookup_ directs the form of the third element, and has to
  1105. * be one of below. If _reverse_lookup_ is omitted, the default value is +nil+.
  1106. *
  1107. * +true+, +:hostname+: hostname is obtained from numeric address using reverse lookup, which may take a time.
  1108. * +false+, +:numeric+: hostname is same as numeric address.
  1109. * +nil+: obey to the current +do_not_reverse_lookup+ flag.
  1110. *
  1111. * If Addrinfo object is preferred, use Addrinfo.getaddrinfo.
  1112. */
  1113. static VALUE
  1114. sock_s_getaddrinfo(int argc, VALUE *argv, VALUE _)
  1115. {
  1116. VALUE host, port, family, socktype, protocol, flags, ret, revlookup;
  1117. struct addrinfo hints;
  1118. struct rb_addrinfo *res;
  1119. int norevlookup;
  1120. rb_scan_args(argc, argv, "25", &host, &port, &family, &socktype, &protocol, &flags, &revlookup);
  1121. MEMZERO(&hints, struct addrinfo, 1);
  1122. hints.ai_family = NIL_P(family) ? PF_UNSPEC : rsock_family_arg(family);
  1123. if (!NIL_P(socktype)) {
  1124. hints.ai_socktype = rsock_socktype_arg(socktype);
  1125. }
  1126. if (!NIL_P(protocol)) {
  1127. hints.ai_protocol = NUM2INT(protocol);
  1128. }
  1129. if (!NIL_P(flags)) {
  1130. hints.ai_flags = NUM2INT(flags);
  1131. }
  1132. if (NIL_P(revlookup) || !rsock_revlookup_flag(revlookup, &norevlookup)) {
  1133. norevlookup = rsock_do_not_reverse_lookup;
  1134. }
  1135. res = rsock_getaddrinfo(host, port, &hints, 0);
  1136. ret = make_addrinfo(res, norevlookup);
  1137. rb_freeaddrinfo(res);
  1138. return ret;
  1139. }
  1140. /*
  1141. * call-seq:
  1142. * Socket.getnameinfo(sockaddr [, flags]) => [hostname, servicename]
  1143. *
  1144. * Obtains name information for _sockaddr_.
  1145. *
  1146. * _sockaddr_ should be one of follows.
  1147. * - packed sockaddr string such as Socket.sockaddr_in(80, "127.0.0.1")
  1148. * - 3-elements array such as ["AF_INET", 80, "127.0.0.1"]
  1149. * - 4-elements array such as ["AF_INET", 80, ignored, "127.0.0.1"]
  1150. *
  1151. * _flags_ should be bitwise OR of Socket::NI_* constants.
  1152. *
  1153. * Note:
  1154. * The last form is compatible with IPSocket#addr and IPSocket#peeraddr.
  1155. *
  1156. * Socket.getnameinfo(Socket.sockaddr_in(80, "127.0.0.1")) #=> ["localhost", "www"]
  1157. * Socket.getnameinfo(["AF_INET", 80, "127.0.0.1"]) #=> ["localhost", "www"]
  1158. * Socket.getnameinfo(["AF_INET", 80, "localhost", "127.0.0.1"]) #=> ["localhost", "www"]
  1159. *
  1160. * If Addrinfo object is preferred, use Addrinfo#getnameinfo.
  1161. */
  1162. static VALUE
  1163. sock_s_getnameinfo(int argc, VALUE *argv, VALUE _)
  1164. {
  1165. VALUE sa, af = Qnil, host = Qnil, port = Qnil, flags, tmp;
  1166. char *hptr, *pptr;
  1167. char hbuf[1024], pbuf[1024];
  1168. int fl;
  1169. struct rb_addrinfo *res = NULL;
  1170. struct addrinfo hints, *r;
  1171. int error, saved_errno;
  1172. union_sockaddr ss;
  1173. struct sockaddr *sap;
  1174. socklen_t salen;
  1175. sa = flags = Qnil;
  1176. rb_scan_args(argc, argv, "11", &sa, &flags);
  1177. fl = 0;
  1178. if (!NIL_P(flags)) {
  1179. fl = NUM2INT(flags);
  1180. }
  1181. tmp = rb_check_sockaddr_string_type(sa);
  1182. if (!NIL_P(tmp)) {
  1183. sa = tmp;
  1184. if (sizeof(ss) < (size_t)RSTRING_LEN(sa)) {
  1185. rb_raise(rb_eTypeError, "sockaddr length too big");
  1186. }
  1187. memcpy(&ss, RSTRING_PTR(sa), RSTRING_LEN(sa));
  1188. if (!VALIDATE_SOCKLEN(&ss.addr, RSTRING_LEN(sa))) {
  1189. rb_raise(rb_eTypeError, "sockaddr size differs - should not happen");
  1190. }
  1191. sap = &ss.addr;
  1192. salen = RSTRING_SOCKLEN(sa);
  1193. goto call_nameinfo;
  1194. }
  1195. tmp = rb_check_array_type(sa);
  1196. if (!NIL_P(tmp)) {
  1197. sa = tmp;
  1198. MEMZERO(&hints, struct addrinfo, 1);
  1199. if (RARRAY_LEN(sa) == 3) {
  1200. af = RARRAY_AREF(sa, 0);
  1201. port = RARRAY_AREF(sa, 1);
  1202. host = RARRAY_AREF(sa, 2);
  1203. }
  1204. else if (RARRAY_LEN(sa) >= 4) {
  1205. af = RARRAY_AREF(sa, 0);
  1206. port = RARRAY_AREF(sa, 1);
  1207. host = RARRAY_AREF(sa, 3);
  1208. if (NIL_P(host)) {
  1209. host = RARRAY_AREF(sa, 2);
  1210. }
  1211. else {
  1212. /*
  1213. * 4th element holds numeric form, don't resolve.
  1214. * see rsock_ipaddr().
  1215. */
  1216. #ifdef AI_NUMERICHOST /* AIX 4.3.3 doesn't have AI_NUMERICHOST. */
  1217. hints.ai_flags |= AI_NUMERICHOST;
  1218. #endif
  1219. }
  1220. }
  1221. else {
  1222. rb_raise(rb_eArgError, "array size should be 3 or 4, %ld given",
  1223. RARRAY_LEN(sa));
  1224. }
  1225. /* host */
  1226. if (NIL_P(host)) {
  1227. hptr = NULL;
  1228. }
  1229. else {
  1230. strncpy(hbuf, StringValueCStr(host), sizeof(hbuf));
  1231. hbuf[sizeof(hbuf) - 1] = '\0';
  1232. hptr = hbuf;
  1233. }
  1234. /* port */
  1235. if (NIL_P(port)) {
  1236. strcpy(pbuf, "0");
  1237. pptr = NULL;
  1238. }
  1239. else if (FIXNUM_P(port)) {
  1240. snprintf(pbuf, sizeof(pbuf), "%ld", NUM2LONG(port));
  1241. pptr = pbuf;
  1242. }
  1243. else {
  1244. strncpy(pbuf, StringValueCStr(port), sizeof(pbuf));
  1245. pbuf[sizeof(pbuf) - 1] = '\0';
  1246. pptr = pbuf;
  1247. }
  1248. hints.ai_socktype = (fl & NI_DGRAM) ? SOCK_DGRAM : SOCK_STREAM;
  1249. /* af */
  1250. hints.ai_family = NIL_P(af) ? PF_UNSPEC : rsock_family_arg(af);
  1251. error = rb_getaddrinfo(hptr, pptr, &hints, &res);
  1252. if (error) goto error_exit_addr;
  1253. sap = res->ai->ai_addr;
  1254. salen = res->ai->ai_addrlen;
  1255. }
  1256. else {
  1257. rb_raise(rb_eTypeError, "expecting String or Array");
  1258. }
  1259. call_nameinfo:
  1260. error = rb_getnameinfo(sap, salen, hbuf, sizeof(hbuf),
  1261. pbuf, sizeof(pbuf), fl);
  1262. if (error) goto error_exit_name;
  1263. if (res) {
  1264. for (r = res->ai->ai_next; r; r = r->ai_next) {
  1265. char hbuf2[1024], pbuf2[1024];
  1266. sap = r->ai_addr;
  1267. salen = r->ai_addrlen;
  1268. error = rb_getnameinfo(sap, salen, hbuf2, sizeof(hbuf2),
  1269. pbuf2, sizeof(pbuf2), fl);
  1270. if (error) goto error_exit_name;
  1271. if (strcmp(hbuf, hbuf2) != 0|| strcmp(pbuf, pbuf2) != 0) {
  1272. rb_freeaddrinfo(res);
  1273. rb_raise(rb_eSocket, "sockaddr resolved to multiple nodename");
  1274. }
  1275. }
  1276. rb_freeaddrinfo(res);
  1277. }
  1278. return rb_assoc_new(rb_str_new2(hbuf), rb_str_new2(pbuf));
  1279. error_exit_addr:
  1280. saved_errno = errno;
  1281. if (res) rb_freeaddrinfo(res);
  1282. errno = saved_errno;
  1283. rsock_raise_socket_error("getaddrinfo", error);
  1284. error_exit_name:
  1285. saved_errno = errno;
  1286. if (res) rb_freeaddrinfo(res);
  1287. errno = saved_errno;
  1288. rsock_raise_socket_error("getnameinfo", error);
  1289. UNREACHABLE_RETURN(Qnil);
  1290. }
  1291. /*
  1292. * call-seq:
  1293. * Socket.sockaddr_in(port, host) => sockaddr
  1294. * Socket.pack_sockaddr_in(port, host) => sockaddr
  1295. *
  1296. * Packs _port_ and _host_ as an AF_INET/AF_INET6 sockaddr string.
  1297. *
  1298. * Socket.sockaddr_in(80, "127.0.0.1")
  1299. * #=> "\x02\x00\x00P\x7F\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00"
  1300. *
  1301. * Socket.sockaddr_in(80, "::1")
  1302. * #=> "\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"
  1303. *
  1304. */
  1305. static VALUE
  1306. sock_s_pack_sockaddr_in(VALUE self, VALUE port, VALUE host)
  1307. {
  1308. struct rb_addrinfo *res = rsock_addrinfo(host, port, AF_UNSPEC, 0, 0);
  1309. VALUE addr = rb_str_new((char*)res->ai->ai_addr, res->ai->ai_addrlen);
  1310. rb_freeaddrinfo(res);
  1311. return addr;
  1312. }
  1313. /*
  1314. * call-seq:
  1315. * Socket.unpack_sockaddr_in(sockaddr) => [port, ip_address]
  1316. *
  1317. * Unpacks _sockaddr_ into port and ip_address.
  1318. *
  1319. * _sockaddr_ should be a string or an addrinfo for AF_INET/AF_INET6.
  1320. *
  1321. * sockaddr = Socket.sockaddr_in(80, "127.0.0.1")
  1322. * p sockaddr #=> "\x02\x00\x00P\x7F\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00"
  1323. * p Socket.unpack_sockaddr_in(sockaddr) #=> [80, "127.0.0.1"]
  1324. *
  1325. */
  1326. static VALUE
  1327. sock_s_unpack_sockaddr_in(VALUE self, VALUE addr)
  1328. {
  1329. struct sockaddr_in * sockaddr;
  1330. VALUE host;
  1331. sockaddr = (struct sockaddr_in*)SockAddrStringValuePtr(addr);
  1332. if (RSTRING_LEN(addr) <
  1333. (char*)&((struct sockaddr *)sockaddr)->sa_family +
  1334. sizeof(((struct sockaddr *)sockaddr)->sa_family) -
  1335. (char*)sockaddr)
  1336. rb_raise(rb_eArgError, "too short sockaddr");
  1337. if (((struct sockaddr *)sockaddr)->sa_family != AF_INET
  1338. #ifdef INET6
  1339. && ((struct sockaddr *)sockaddr)->sa_family != AF_INET6
  1340. #endif
  1341. ) {
  1342. #ifdef INET6
  1343. rb_raise(rb_eArgError, "not an AF_INET/AF_INET6 sockaddr");
  1344. #else
  1345. rb_raise(rb_eArgError, "not an AF_INET sockaddr");
  1346. #endif
  1347. }
  1348. host = rsock_make_ipaddr((struct sockaddr*)sockaddr, RSTRING_SOCKLEN(addr));
  1349. return rb_assoc_new(INT2NUM(ntohs(sockaddr->sin_port)), host);
  1350. }
  1351. #ifdef HAVE_SYS_UN_H
  1352. /*
  1353. * call-seq:
  1354. * Socket.sockaddr_un(path) => sockaddr
  1355. * Socket.pack_sockaddr_un(path) => sockaddr
  1356. *
  1357. * Packs _path_ as an AF_UNIX sockaddr string.
  1358. *
  1359. * Socket.sockaddr_un("/tmp/sock") #=> "\x01\x00/tmp/sock\x00\x00..."
  1360. *
  1361. */
  1362. static VALUE
  1363. sock_s_pack_sockaddr_un(VALUE self, VALUE path)
  1364. {
  1365. struct sockaddr_un sockaddr;
  1366. VALUE addr;
  1367. StringValue(path);
  1368. INIT_SOCKADDR_UN(&sockaddr, sizeof(struct sockaddr_un));
  1369. if (sizeof(sockaddr.sun_path) < (size_t)RSTRING_LEN(path)) {
  1370. rb_raise(rb_eArgError, "too long unix socket path (%"PRIuSIZE" bytes given but %"PRIuSIZE" bytes max)",
  1371. (size_t)RSTRING_LEN(path), sizeof(sockaddr.sun_path));
  1372. }
  1373. memcpy(sockaddr.sun_path, RSTRING_PTR(path), RSTRING_LEN(path));
  1374. addr = rb_str_new((char*)&sockaddr, rsock_unix_sockaddr_len(path));
  1375. return addr;
  1376. }
  1377. /*
  1378. * call-seq:
  1379. * Socket.unpack_sockaddr_un(sockaddr) => path
  1380. *
  1381. * Unpacks _sockaddr_ into path.
  1382. *
  1383. * _sockaddr_ should be a string or an addrinfo for AF_UNIX.
  1384. *
  1385. * sockaddr = Socket.sockaddr_un("/tmp/sock")
  1386. * p Socket.unpack_sockaddr_un(sockaddr) #=> "/tmp/sock"
  1387. *
  1388. */
  1389. static VALUE
  1390. sock_s_unpack_sockaddr_un(VALUE self, VALUE addr)
  1391. {
  1392. struct sockaddr_un * sockaddr;
  1393. VALUE path;
  1394. sockaddr = (struct sockaddr_un*)SockAddrStringValuePtr(addr);
  1395. if (RSTRING_LEN(addr) <
  1396. (char*)&((struct sockaddr *)sockaddr)->sa_family +
  1397. sizeof(((struct sockaddr *)sockaddr)->sa_family) -
  1398. (char*)sockaddr)
  1399. rb_raise(rb_eArgError, "too short sockaddr");
  1400. if (((struct sockaddr *)sockaddr)->sa_family != AF_UNIX) {
  1401. rb_raise(rb_eArgError, "not an AF_UNIX sockaddr");
  1402. }
  1403. if (sizeof(struct sockaddr_un) < (size_t)RSTRING_LEN(addr)) {
  1404. rb_raise(rb_eTypeError, "too long sockaddr_un - %ld longer than %d",
  1405. RSTRING_LEN(addr), (int)sizeof(struct sockaddr_un));
  1406. }
  1407. path = rsock_unixpath_str(sockaddr, RSTRING_SOCKLEN(addr));
  1408. return path;
  1409. }
  1410. #endif
  1411. #if defined(HAVE_GETIFADDRS) || defined(SIOCGLIFCONF) || defined(SIOCGIFCONF) || defined(_WIN32)
  1412. static socklen_t
  1413. sockaddr_len(struct sockaddr *addr)
  1414. {
  1415. if (a

Large files files are truncated, but you can click here to view the full file