/htdocs/ruby-stdlib/Socket.html
HTML | 5176 lines | 4043 code | 1133 blank | 0 comment | 0 complexity | f30f5a039cae6e5da2887c37ac5af6ec MD5 | raw file
Large files files are truncated, but you can click here to view the full file
1<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 2 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 3<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> 4 <head> 5 <meta name="Content-Type" content="text/html; charset=utf-8" /> 6<title>Class: Socket</title> 7<link rel="stylesheet" href="css/style.css" type="text/css" media="screen" charset="utf-8" /> 8<link rel="stylesheet" href="css/common.css" type="text/css" media="screen" charset="utf-8" /> 9 10<script type="text/javascript" charset="utf-8"> 11 relpath = ''; 12 if (relpath != '') relpath += '/'; 13</script> 14<script type="text/javascript" charset="utf-8" src="js/jquery.js"></script> 15<script type="text/javascript" charset="utf-8" src="js/app.js"></script> 16 17 </head> 18 <body> 19 <script type="text/javascript" charset="utf-8"> 20 if (window.top.frames.main) document.body.className = 'frames'; 21 </script> 22 23 <div id="header"> 24 <div id="menu"> 25 26 <a href="_index.html">Index (S)</a> » 27 28 29 <span class="title">Socket</span> 30 31 32 <div class="noframes"><span class="title">(</span><a href="." target="_top">no frames</a><span class="title">)</span></div> 33</div> 34 35 <div id="search"> 36 <a id="class_list_link" href="#">Class List</a> 37 <a id="method_list_link" href="#">Method List</a> 38 <a id ="file_list_link" href="#">File List</a> 39</div> 40 41 <div class="clear"></div> 42 </div> 43 44 <iframe id="search_frame"></iframe> 45 46 <div id="content"><h1>Class: Socket 47 48 49 50</h1> 51 52<dl class="box"> 53 54 <dt class="r1">Inherits:</dt> 55 <dd class="r1"> 56 <span class="inheritName"><span class='object_link'><a href="BasicSocket.html" title="BasicSocket (class)">BasicSocket</a></span></span> 57 58 <ul class="fullTree"> 59 <li><span class='object_link'><a href="Object.html" title="Object (class)">Object</a></span></li> 60 61 <li class="next"><span class='object_link'><a href="IO.html" title="IO (class)">IO</a></span></li> 62 63 <li class="next"><span class='object_link'><a href="BasicSocket.html" title="BasicSocket (class)">BasicSocket</a></span></li> 64 65 <li class="next">Socket</li> 66 67 </ul> 68 <a href="#" class="inheritanceTree">show all</a> 69 70 </dd> 71 72 73 74 75 76 77 78 79 80 <dt class="r2 last">Defined in:</dt> 81 <dd class="r2 last">lib/ipaddr.rb<span class="defines">,<br /> 82 ext/socket/socket.c</span> 83</dd> 84 85</dl> 86<div class="clear"></div> 87 88<h2>Defined Under Namespace</h2> 89<p class="children"> 90 91 92 <strong class="modules">Modules:</strong> <span class='object_link'><a href="Socket/Constants.html" title="Socket::Constants (module)">Constants</a></span> 93 94 95 96 <strong class="classes">Classes:</strong> <span class='object_link'><a href="Socket/AncillaryData.html" title="Socket::AncillaryData (class)">AncillaryData</a></span>, <span class='object_link'><a href="Socket/Option.html" title="Socket::Option (class)">Option</a></span> 97 98 99</p> 100 101 <h2>Constant Summary</h2> 102 103 <dl class="constants"> 104 105 <dt id="AF_INET6-constant" class="">AF_INET6 = 106 107 </dt> 108 <dd><pre class="code"><span class='const'>Object</span><span class='period'>.</span><span class='id new'>new</span></pre></dd> 109 110 </dl> 111 112 113 114 115 116 117 118 119 120 121 122 123 124 <h2> 125 Class Method Summary 126 <small>(<a href="#" class="summary_toggle">collapse</a>)</small> 127 </h2> 128 129 <ul class="summary"> 130 131 <li class="public "> 132 <span class="summary_signature"> 133 134 <a href="#getaddrinfo-class_method" title="getaddrinfo (class method)">+ (Array) <strong>getaddrinfo</strong>(nodename, servname[, family[, socktype[, protocol[, flags[, reverse_lookup]]]]]) </a> 135 136 137 138 </span> 139 140 141 142 143 144 145 146 147 <span class="summary_desc"><div class='inline'><p> 148Obtains address information for <em>nodename</em>:<em>servname</em>. 149</p> 150</div></span> 151 152</li> 153 154 155 <li class="public "> 156 <span class="summary_signature"> 157 158 <a href="#gethostbyaddr-class_method" title="gethostbyaddr (class method)">+ (Object) <strong>gethostbyaddr</strong>(address_string[, address_family]) </a> 159 160 161 162 </span> 163 164 165 166 167 168 169 170 171 <span class="summary_desc"><div class='inline'><p> 172Obtains the host information for <em>address</em>. 173</p> 174</div></span> 175 176</li> 177 178 179 <li class="public "> 180 <span class="summary_signature"> 181 182 <a href="#gethostbyname-class_method" title="gethostbyname (class method)">+ (Array) <strong>gethostbyname</strong>(hostname) </a> 183 184 185 186 </span> 187 188 189 190 191 192 193 194 195 <span class="summary_desc"><div class='inline'><p> 196Obtains the host information for <em>hostname</em>. 197</p> 198</div></span> 199 200</li> 201 202 203 <li class="public "> 204 <span class="summary_signature"> 205 206 <a href="#gethostname-class_method" title="gethostname (class method)">+ (Object) <strong>gethostname</strong> </a> 207 208 209 210 </span> 211 212 213 214 215 216 217 218 219 <span class="summary_desc"><div class='inline'><p> 220Returns the hostname. 221</p> 222</div></span> 223 224</li> 225 226 227 <li class="public "> 228 <span class="summary_signature"> 229 230 <a href="#getnameinfo-class_method" title="getnameinfo (class method)">+ (Array) <strong>getnameinfo</strong>(sockaddr[, flags]) </a> 231 232 233 234 </span> 235 236 237 238 239 240 241 242 243 <span class="summary_desc"><div class='inline'><p> 244Obtains name information for <em>sockaddr</em>. 245</p> 246</div></span> 247 248</li> 249 250 251 <li class="public "> 252 <span class="summary_signature"> 253 254 <a href="#getservbyname-class_method" title="getservbyname (class method)">+ (Object) <strong>getservbyname</strong> </a> 255 256 257 258 </span> 259 260 261 262 263 264 265 266 267 <span class="summary_desc"><div class='inline'><p> 268Obtains the port number for <em>service_name</em>. 269</p> 270</div></span> 271 272</li> 273 274 275 <li class="public "> 276 <span class="summary_signature"> 277 278 <a href="#getservbyport-class_method" title="getservbyport (class method)">+ (Object) <strong>getservbyport</strong>(port[, protocol_name]) </a> 279 280 281 282 </span> 283 284 285 286 287 288 289 290 291 <span class="summary_desc"><div class='inline'><p> 292Obtains the port number for <em>port</em>. 293</p> 294</div></span> 295 296</li> 297 298 299 <li class="public "> 300 <span class="summary_signature"> 301 302 <a href="#ip_address_list-class_method" title="ip_address_list (class method)">+ (Array) <strong>ip_address_list</strong> </a> 303 304 305 306 </span> 307 308 309 310 311 312 313 314 315 <span class="summary_desc"><div class='inline'><p> 316Returns local IP addresses as an array. 317</p> 318</div></span> 319 320</li> 321 322 323 <li class="public "> 324 <span class="summary_signature"> 325 326 <a href="#pack_sockaddr_in-class_method" title="pack_sockaddr_in (class method)">+ (Object) <strong>pack_sockaddr_in</strong> </a> 327 328 329 330 </span> 331 332 333 334 335 336 337 338 339 <span class="summary_desc"><div class='inline'><p> 340Packs <em>port</em> and <em>host</em> as an AF_INET/AF_INET6 sockaddr 341string. 342</p> 343</div></span> 344 345</li> 346 347 348 <li class="public "> 349 <span class="summary_signature"> 350 351 <a href="#pack_sockaddr_un-class_method" title="pack_sockaddr_un (class method)">+ (Object) <strong>pack_sockaddr_un</strong> </a> 352 353 354 355 </span> 356 357 358 359 360 361 362 363 364 <span class="summary_desc"><div class='inline'><p> 365Packs <em>path</em> as an AF_UNIX sockaddr string. 366</p> 367</div></span> 368 369</li> 370 371 372 <li class="public "> 373 <span class="summary_signature"> 374 375 <a href="#pair-class_method" title="pair (class method)">+ (Object) <strong>pair</strong> </a> 376 377 378 379 </span> 380 381 382 383 384 385 386 387 388 <span class="summary_desc"><div class='inline'><p> 389Creates a pair of sockets connected each other. 390</p> 391</div></span> 392 393</li> 394 395 396 <li class="public "> 397 <span class="summary_signature"> 398 399 <a href="#sockaddr_in-class_method" title="sockaddr_in (class method)">+ (Object) <strong>sockaddr_in</strong> </a> 400 401 402 403 </span> 404 405 406 407 408 409 410 411 412 <span class="summary_desc"><div class='inline'><p> 413Packs <em>port</em> and <em>host</em> as an AF_INET/AF_INET6 sockaddr 414string. 415</p> 416</div></span> 417 418</li> 419 420 421 <li class="public "> 422 <span class="summary_signature"> 423 424 <a href="#sockaddr_un-class_method" title="sockaddr_un (class method)">+ (Object) <strong>sockaddr_un</strong> </a> 425 426 427 428 </span> 429 430 431 432 433 434 435 436 437 <span class="summary_desc"><div class='inline'><p> 438Packs <em>path</em> as an AF_UNIX sockaddr string. 439</p> 440</div></span> 441 442</li> 443 444 445 <li class="public "> 446 <span class="summary_signature"> 447 448 <a href="#socketpair-class_method" title="socketpair (class method)">+ (Object) <strong>socketpair</strong> </a> 449 450 451 452 </span> 453 454 455 456 457 458 459 460 461 <span class="summary_desc"><div class='inline'><p> 462Creates a pair of sockets connected each other. 463</p> 464</div></span> 465 466</li> 467 468 469 <li class="public "> 470 <span class="summary_signature"> 471 472 <a href="#unpack_sockaddr_in-class_method" title="unpack_sockaddr_in (class method)">+ (Array) <strong>unpack_sockaddr_in</strong>(sockaddr) </a> 473 474 475 476 </span> 477 478 479 480 481 482 483 484 485 <span class="summary_desc"><div class='inline'><p> 486Unpacks <em>sockaddr</em> into port and ip_address. 487</p> 488</div></span> 489 490</li> 491 492 493 <li class="public "> 494 <span class="summary_signature"> 495 496 <a href="#unpack_sockaddr_un-class_method" title="unpack_sockaddr_un (class method)">+ (Object) <strong>unpack_sockaddr_un</strong>(sockaddr) </a> 497 498 499 500 </span> 501 502 503 504 505 506 507 508 509 <span class="summary_desc"><div class='inline'><p> 510Unpacks <em>sockaddr</em> into path. 511</p> 512</div></span> 513 514</li> 515 516 517 </ul> 518 519 <h2> 520 Instance Method Summary 521 <small>(<a href="#" class="summary_toggle">collapse</a>)</small> 522 </h2> 523 524 <ul class="summary"> 525 526 <li class="public "> 527 <span class="summary_signature"> 528 529 <a href="#accept-instance_method" title="#accept (instance method)">- (Array) <strong>accept</strong> </a> 530 531 532 533 </span> 534 535 536 537 538 539 540 541 542 <span class="summary_desc"><div class='inline'><p> 543Accepts a next connection. 544</p> 545</div></span> 546 547</li> 548 549 550 <li class="public "> 551 <span class="summary_signature"> 552 553 <a href="#accept_nonblock-instance_method" title="#accept_nonblock (instance method)">- (Array) <strong>accept_nonblock</strong> </a> 554 555 556 557 </span> 558 559 560 561 562 563 564 565 566 <span class="summary_desc"><div class='inline'><p> 567Accepts an incoming connection using accept(2) after O_NONBLOCK is set for 568the underlying file descriptor. 569</p> 570</div></span> 571 572</li> 573 574 575 <li class="public "> 576 <span class="summary_signature"> 577 578 <a href="#bind-instance_method" title="#bind (instance method)">- (0) <strong>bind</strong>(local_sockaddr) </a> 579 580 581 582 </span> 583 584 585 586 587 588 589 590 591 <span class="summary_desc"><div class='inline'><p> 592Binds to the given local address. 593</p> 594</div></span> 595 596</li> 597 598 599 <li class="public "> 600 <span class="summary_signature"> 601 602 <a href="#connect-instance_method" title="#connect (instance method)">- (0) <strong>connect</strong>(remote_sockaddr) </a> 603 604 605 606 </span> 607 608 609 610 611 612 613 614 615 <span class="summary_desc"><div class='inline'><p> 616Requests a connection to be made on the given <tt>remote_sockaddr</tt>. 617</p> 618</div></span> 619 620</li> 621 622 623 <li class="public "> 624 <span class="summary_signature"> 625 626 <a href="#connect_nonblock-instance_method" title="#connect_nonblock (instance method)">- (0) <strong>connect_nonblock</strong>(remote_sockaddr) </a> 627 628 629 630 </span> 631 632 633 634 635 636 637 638 639 <span class="summary_desc"><div class='inline'><p> 640Requests a connection to be made on the given <tt>remote_sockaddr</tt> 641after O_NONBLOCK is set for the underlying file descriptor. 642</p> 643</div></span> 644 645</li> 646 647 648 <li class="public "> 649 <span class="summary_signature"> 650 651 <a href="#new-instance_method" title="#new (instance method)">- (Object) <strong>new</strong>(domain, socktype[, protocol]) </a> 652 653 654 655 </span> 656 657 <span class="note title constructor">constructor</span> 658 659 660 661 662 663 664 665 666 <span class="summary_desc"><div class='inline'><p> 667Creates a new socket object. 668</p> 669</div></span> 670 671</li> 672 673 674 <li class="public "> 675 <span class="summary_signature"> 676 677 <a href="#listen-instance_method" title="#listen (instance method)">- (0) <strong>listen</strong>(int) </a> 678 679 680 681 </span> 682 683 684 685 686 687 688 689 690 <span class="summary_desc"><div class='inline'><p> 691Listens for connections, using the specified <tt>int</tt> as the backlog. 692</p> 693</div></span> 694 695</li> 696 697 698 <li class="public "> 699 <span class="summary_signature"> 700 701 <a href="#recvfrom-instance_method" title="#recvfrom (instance method)">- (Object) <strong>recvfrom</strong> </a> 702 703 704 705 </span> 706 707 708 709 710 711 712 713 714 <span class="summary_desc"><div class='inline'><p> 715Receives up to <em>maxlen</em> bytes from <tt>socket</tt>. 716</p> 717</div></span> 718 719</li> 720 721 722 <li class="public "> 723 <span class="summary_signature"> 724 725 <a href="#recvfrom_nonblock-instance_method" title="#recvfrom_nonblock (instance method)">- (Object) <strong>recvfrom_nonblock</strong> </a> 726 727 728 729 </span> 730 731 732 733 734 735 736 737 738 <span class="summary_desc"><div class='inline'><p> 739Receives up to <em>maxlen</em> bytes from <tt>socket</tt> using recvfrom(2) 740after O_NONBLOCK is set for the underlying file descriptor. 741</p> 742</div></span> 743 744</li> 745 746 747 <li class="public "> 748 <span class="summary_signature"> 749 750 <a href="#sysaccept-instance_method" title="#sysaccept (instance method)">- (Array) <strong>sysaccept</strong> </a> 751 752 753 754 </span> 755 756 757 758 759 760 761 762 763 <span class="summary_desc"><div class='inline'><p> 764Accepts an incoming connection returning an array containing the (integer) 765file descriptor for the incoming connection, <em>client_socket_fd</em>, and 766an Addrinfo, <em>client_addrinfo</em>. 767</p> 768</div></span> 769 770</li> 771 772 773 </ul> 774 775 776 777 778 779 780 781 782 783 784 <h3 class="inherited">Methods inherited from <span class='object_link'><a href="BasicSocket.html" title="BasicSocket (class)">BasicSocket</a></span></h3> 785 <p class="inherited"><span class='object_link'><a href="BasicSocket.html#close_read-instance_method" title="BasicSocket#close_read (method)">#close_read</a></span>, <span class='object_link'><a href="BasicSocket.html#close_write-instance_method" title="BasicSocket#close_write (method)">#close_write</a></span>, <span class='object_link'><a href="BasicSocket.html#do_not_reverse_lookup-instance_method" title="BasicSocket#do_not_reverse_lookup (method)">#do_not_reverse_lookup</a></span>, <span class='object_link'><a href="BasicSocket.html#do_not_reverse_lookup-class_method" title="BasicSocket.do_not_reverse_lookup (method)">do_not_reverse_lookup</a></span>, <span class='object_link'><a href="BasicSocket.html#do_not_reverse_lookup%3D-class_method" title="BasicSocket.do_not_reverse_lookup= (method)">do_not_reverse_lookup=</a></span>, <span class='object_link'><a href="BasicSocket.html#do_not_reverse_lookup%3D-instance_method" title="BasicSocket#do_not_reverse_lookup= (method)">#do_not_reverse_lookup=</a></span>, <span class='object_link'><a href="BasicSocket.html#for_fd-class_method" title="BasicSocket.for_fd (method)">for_fd</a></span>, <span class='object_link'><a href="BasicSocket.html#getpeereid-instance_method" title="BasicSocket#getpeereid (method)">#getpeereid</a></span>, <span class='object_link'><a href="BasicSocket.html#getpeername-instance_method" title="BasicSocket#getpeername (method)">#getpeername</a></span>, <span class='object_link'><a href="BasicSocket.html#getsockname-instance_method" title="BasicSocket#getsockname (method)">#getsockname</a></span>, <span class='object_link'><a href="BasicSocket.html#getsockopt-instance_method" title="BasicSocket#getsockopt (method)">#getsockopt</a></span>, <span class='object_link'><a href="BasicSocket.html#local_address-instance_method" title="BasicSocket#local_address (method)">#local_address</a></span>, <span class='object_link'><a href="BasicSocket.html#recv-instance_method" title="BasicSocket#recv (method)">#recv</a></span>, <span class='object_link'><a href="BasicSocket.html#recv_nonblock-instance_method" title="BasicSocket#recv_nonblock (method)">#recv_nonblock</a></span>, <span class='object_link'><a href="BasicSocket.html#recvmsg-instance_method" title="BasicSocket#recvmsg (method)">#recvmsg</a></span>, <span class='object_link'><a href="BasicSocket.html#recvmsg_nonblock-instance_method" title="BasicSocket#recvmsg_nonblock (method)">#recvmsg_nonblock</a></span>, <span class='object_link'><a href="BasicSocket.html#remote_address-instance_method" title="BasicSocket#remote_address (method)">#remote_address</a></span>, <span class='object_link'><a href="BasicSocket.html#send-instance_method" title="BasicSocket#send (method)">#send</a></span>, <span class='object_link'><a href="BasicSocket.html#sendmsg-instance_method" title="BasicSocket#sendmsg (method)">#sendmsg</a></span>, <span class='object_link'><a href="BasicSocket.html#sendmsg_nonblock-instance_method" title="BasicSocket#sendmsg_nonblock (method)">#sendmsg_nonblock</a></span>, <span class='object_link'><a href="BasicSocket.html#setsockopt-instance_method" title="BasicSocket#setsockopt (method)">#setsockopt</a></span>, <span class='object_link'><a href="BasicSocket.html#shutdown-instance_method" title="BasicSocket#shutdown (method)">#shutdown</a></span></p> 786 787 788 789 790 791 792 793 794 <h3 class="inherited">Methods inherited from <span class='object_link'><a href="IO.html" title="IO (class)">IO</a></span></h3> 795 <p class="inherited"><span class='object_link'><a href="IO.html#nread-instance_method" title="IO#nread (method)">#nread</a></span>, <span class='object_link'><a href="IO.html#ready%3F-instance_method" title="IO#ready? (method)">#ready?</a></span>, <span class='object_link'><a href="IO.html#scanf-instance_method" title="IO#scanf (method)">#scanf</a></span>, <span class='object_link'><a href="IO.html#wait-instance_method" title="IO#wait (method)">#wait</a></span></p> 796<div id="constructor_details" class="method_details_list"> 797 <h2>Constructor Details</h2> 798 799 <div class="method_details first"> 800 <p class="signature first" id="initialize-instance_method"> 801 802 - (<tt><span class='object_link'><a href="Object.html" title="Object (class)">Object</a></span></tt>) <strong>new</strong>(domain, socktype[, protocol]) 803 804 805 806</p><div class="docstring"> 807 <div class="discussion"> 808 <p> 809Creates a new socket object. 810</p> 811<p> 812<em>domain</em> should be a communications domain such as: :INET, :INET6, 813:UNIX, etc. 814</p> 815<p> 816<em>socktype</em> should be a socket type such as: :STREAM, :DGRAM, :RAW, 817etc. 818</p> 819<p> 820<em>protocol</em> should be a protocol defined in the domain. This is 821optional. If it is not given, 0 is used internally. 822</p> 823<pre class="code"> 824 Socket.new(:INET, :STREAM) # TCP socket 825 Socket.new(:INET, :DGRAM) # UDP socket 826 Socket.new(:UNIX, :STREAM) # UNIX stream socket 827 Socket.new(:UNIX, :DGRAM) # UNIX datagram socket</pre> 828 829 830 </div> 831</div> 832<div class="tags"> 833 834 835 836</div><table class="source_code"> 837 <tr> 838 <td> 839 <pre class="lines"> 840 841 842</pre> 843 </td> 844 <td> 845 <pre class="code"><span class="info file"># File 'ext/socket/socket.c'</span> 846 847/* 848 * call-seq: 849 * Socket.new(domain, socktype [, protocol]) => socket 850 * 851 * Creates a new socket object. 852 * 853 * _domain_ should be a communications domain such as: :INET, :INET6, :UNIX, etc. 854 * 855 * _socktype_ should be a socket type such as: :STREAM, :DGRAM, :RAW, etc. 856 * 857 * _protocol_ should be a protocol defined in the domain. 858 * This is optional. 859 * If it is not given, 0 is used internally. 860 * 861 * Socket.new(:INET, :STREAM) # TCP socket 862 * Socket.new(:INET, :DGRAM) # UDP socket 863 * Socket.new(:UNIX, :STREAM) # UNIX stream socket 864 * Socket.new(:UNIX, :DGRAM) # UNIX datagram socket 865 */ 866static VALUE 867sock_initialize(int argc, VALUE *argv, VALUE sock) 868{ 869 VALUE domain, type, protocol; 870 int fd; 871 int d, t; 872 873 rb_scan_args(argc, argv, "21", &domain, &type, &protocol); 874 if (NIL_P(protocol)) 875 protocol = INT2FIX(0); 876 877 rb_secure(3); 878 setup_domain_and_type(domain, &d, type, &t); 879 fd = rsock_socket(d, t, NUM2INT(protocol)); 880 if (fd < 0) rb_sys_fail("socket(2)"); 881 882 return rsock_init_sock(sock, fd); 883}</pre> 884 </td> 885 </tr> 886</table> 887</div> 888 889</div> 890 891 892 <div id="class_method_details" class="method_details_list"> 893 <h2>Class Method Details</h2> 894 895 896 <div class="method_details first"> 897 <p class="signature first" id="getaddrinfo-class_method"> 898 899 + (<tt><span class='object_link'><a href="Array.html" title="Array (class)">Array</a></span></tt>) <strong>getaddrinfo</strong>(nodename, servname[, family[, socktype[, protocol[, flags[, reverse_lookup]]]]]) 900 901 902 903</p><div class="docstring"> 904 <div class="discussion"> 905 <p> 906Obtains address information for <em>nodename</em>:<em>servname</em>. 907</p> 908<p> 909<em>family</em> should be an address family such as: :INET, :INET6, :UNIX, 910etc. 911</p> 912<p> 913<em>socktype</em> should be a socket type such as: :STREAM, :DGRAM, :RAW, 914etc. 915</p> 916<p> 917<em>protocol</em> should be a protocol defined in the family. 0 is default 918protocol for the family. 919</p> 920<p> 921<em>flags</em> should be bitwise OR of Socket::AI_* constants. 922</p> 923<pre class="code"> 924 Socket.getaddrinfo("www.ruby-lang.org", "http", nil, :STREAM) 925 #=> [["AF_INET", 80, "carbon.ruby-lang.org", "221.186.184.68", 2, 1, 6]] # PF_INET/SOCK_STREAM/IPPROTO_TCP 926 927 Socket.getaddrinfo("localhost", nil) 928 #=> [["AF_INET", 0, "localhost", "127.0.0.1", 2, 1, 6], # PF_INET/SOCK_STREAM/IPPROTO_TCP 929 # ["AF_INET", 0, "localhost", "127.0.0.1", 2, 2, 17], # PF_INET/SOCK_DGRAM/IPPROTO_UDP 930 # ["AF_INET", 0, "localhost", "127.0.0.1", 2, 3, 0]] # PF_INET/SOCK_RAW/IPPROTO_IP 931</pre> 932<p> 933<em>reverse_lookup</em> directs the form of the third element, and has to 934be one of below. If it is ommitted, the default value is <tt>nil</tt>. 935</p> 936<pre class="code"> 937 +true+, +:hostname+: hostname is obtained from numeric address using reverse lookup, which may take a time. 938 +false+, +:numeric+: hostname is same as numeric address. 939 +nil+: obey to the current +do_not_reverse_lookup+ flag. 940</pre> 941<p> 942If Addrinfo object is preferred, use Addrinfo.getaddrinfo. 943</p> 944 945 946 </div> 947</div> 948<div class="tags"> 949 950 <div class="docstring"> 951 <div class="discussion"> 952 953 954 </div> 955</div> 956<div class="tags"> 957 <h3>Returns:</h3> 958<ul class="return"> 959 960 <li> 961 962 <span class='type'>(<tt><span class='object_link'><a href="Array.html" title="Array (class)">Array</a></span></tt>)</span> 963 964 965 966 967 </li> 968 969</ul> 970 971</div> 972 973</div><table class="source_code"> 974 <tr> 975 <td> 976 <pre class="lines"> 977 978 979</pre> 980 </td> 981 <td> 982 <pre class="code"><span class="info file"># File 'ext/socket/socket.c'</span> 983 984/* 985 * call-seq: 986 * Socket.getaddrinfo(nodename, servname[, family[, socktype[, protocol[, flags[, reverse_lookup]]]]]) => array 987 * 988 * Obtains address information for _nodename_:_servname_. 989 * 990 * _family_ should be an address family such as: :INET, :INET6, :UNIX, etc. 991 * 992 * _socktype_ should be a socket type such as: :STREAM, :DGRAM, :RAW, etc. 993 * 994 * _protocol_ should be a protocol defined in the family. 995 * 0 is default protocol for the family. 996 * 997 * _flags_ should be bitwise OR of Socket::AI_* constants. 998 * 999 * Socket.getaddrinfo("www.ruby-lang.org", "http", nil, :STREAM) 1000 * #=> [["AF_INET", 80, "carbon.ruby-lang.org", "221.186.184.68", 2, 1, 6]] # PF_INET/SOCK_STREAM/IPPROTO_TCP 1001 * 1002 * Socket.getaddrinfo("localhost", nil) 1003 * #=> [["AF_INET", 0, "localhost", "127.0.0.1", 2, 1, 6], # PF_INET/SOCK_STREAM/IPPROTO_TCP 1004 * # ["AF_INET", 0, "localhost", "127.0.0.1", 2, 2, 17], # PF_INET/SOCK_DGRAM/IPPROTO_UDP 1005 * # ["AF_INET", 0, "localhost", "127.0.0.1", 2, 3, 0]] # PF_INET/SOCK_RAW/IPPROTO_IP 1006 * 1007 * _reverse_lookup_ directs the form of the third element, and has to 1008 * be one of below. 1009 * If it is ommitted, the default value is +nil+. 1010 * 1011 * +true+, +:hostname+: hostname is obtained from numeric address using reverse lookup, which may take a time. 1012 * +false+, +:numeric+: hostname is same as numeric address. 1013 * +nil+: obey to the current +do_not_reverse_lookup+ flag. 1014 * 1015 * If Addrinfo object is preferred, use Addrinfo.getaddrinfo. 1016 */ 1017static VALUE 1018sock_s_getaddrinfo(int argc, VALUE *argv) 1019{ 1020 VALUE host, port, family, socktype, protocol, flags, ret, revlookup; 1021 struct addrinfo hints, *res; 1022 int norevlookup; 1023 1024 rb_scan_args(argc, argv, "25", &host, &port, &family, &socktype, &protocol, &flags, &revlookup); 1025 1026 MEMZERO(&hints, struct addrinfo, 1); 1027 hints.ai_family = NIL_P(family) ? PF_UNSPEC : rsock_family_arg(family); 1028 1029 if (!NIL_P(socktype)) { 1030 hints.ai_socktype = rsock_socktype_arg(socktype); 1031 } 1032 if (!NIL_P(protocol)) { 1033 hints.ai_protocol = NUM2INT(protocol); 1034 } 1035 if (!NIL_P(flags)) { 1036 hints.ai_flags = NUM2INT(flags); 1037 } 1038 if (NIL_P(revlookup) || !rsock_revlookup_flag(revlookup, &norevlookup)) { 1039 norevlookup = rsock_do_not_reverse_lookup; 1040 } 1041 res = rsock_getaddrinfo(host, port, &hints, 0); 1042 1043 ret = make_addrinfo(res, norevlookup); 1044 freeaddrinfo(res); 1045 return ret; 1046}</pre> 1047 </td> 1048 </tr> 1049</table> 1050</div> 1051 1052 <div class="method_details "> 1053 <p class="signature " id="gethostbyaddr-class_method"> 1054 1055 + (<tt><span class='object_link'><a href="Object.html" title="Object (class)">Object</a></span></tt>) <strong>gethostbyaddr</strong>(address_string[, address_family]) 1056 1057 1058 1059</p><div class="docstring"> 1060 <div class="discussion"> 1061 <p> 1062Obtains the host information for <em>address</em>. 1063</p> 1064<pre class="code"> 1065 p Socket.gethostbyaddr([221,186,184,68].pack("CCCC")) 1066 #=> ["carbon.ruby-lang.org", [], 2, "\xDD\xBA\xB8D"]</pre> 1067 1068 1069 </div> 1070</div> 1071<div class="tags"> 1072 1073 1074 1075</div><table class="source_code"> 1076 <tr> 1077 <td> 1078 <pre class="lines"> 1079 1080 1081</pre> 1082 </td> 1083 <td> 1084 <pre class="code"><span class="info file"># File 'ext/socket/socket.c'</span> 1085 1086/* 1087 * call-seq: 1088 * Socket.gethostbyaddr(address_string [, address_family]) => hostent 1089 * 1090 * Obtains the host information for _address_. 1091 * 1092 * p Socket.gethostbyaddr([221,186,184,68].pack("CCCC")) 1093 * #=> ["carbon.ruby-lang.org", [], 2, "\xDD\xBA\xB8D"] 1094 */ 1095static VALUE 1096sock_s_gethostbyaddr(int argc, VALUE *argv) 1097{ 1098 VALUE addr, family; 1099 struct hostent *h; 1100 struct sockaddr *sa; 1101 char **pch; 1102 VALUE ary, names; 1103 int t = AF_INET; 1104 1105 rb_scan_args(argc, argv, "11", &addr, &family); 1106 sa = (struct sockaddr*)StringValuePtr(addr); 1107 if (!NIL_P(family)) { 1108 t = rsock_family_arg(family); 1109 } 1110#ifdef AF_INET6 1111 else if (RSTRING_LEN(addr) == 16) { 1112 t = AF_INET6; 1113 } 1114#endif 1115 h = gethostbyaddr(RSTRING_PTR(addr), RSTRING_LENINT(addr), t); 1116 if (h == NULL) { 1117#ifdef HAVE_HSTRERROR 1118 extern int h_errno; 1119 rb_raise(rb_eSocket, "%s", (char*)hstrerror(h_errno)); 1120#else 1121 rb_raise(rb_eSocket, "host not found"); 1122#endif 1123 } 1124 ary = rb_ary_new(); 1125 rb_ary_push(ary, rb_str_new2(h->h_name)); 1126 names = rb_ary_new(); 1127 rb_ary_push(ary, names); 1128 if (h->h_aliases != NULL) { 1129 for (pch = h->h_aliases; *pch; pch++) { 1130 rb_ary_push(names, rb_str_new2(*pch)); 1131 } 1132 } 1133 rb_ary_push(ary, INT2NUM(h->h_addrtype)); 1134#ifdef h_addr 1135 for (pch = h->h_addr_list; *pch; pch++) { 1136 rb_ary_push(ary, rb_str_new(*pch, h->h_length)); 1137 } 1138#else 1139 rb_ary_push(ary, rb_str_new(h->h_addr, h->h_length)); 1140#endif 1141 1142 return ary; 1143}</pre> 1144 </td> 1145 </tr> 1146</table> 1147</div> 1148 1149 <div class="method_details "> 1150 <p class="signature " id="gethostbyname-class_method"> 1151 1152 + (<tt><span class='object_link'><a href="Array.html" title="Array (class)">Array</a></span></tt>) <strong>gethostbyname</strong>(hostname) 1153 1154 1155 1156</p><div class="docstring"> 1157 <div class="discussion"> 1158 <p> 1159Obtains the host information for <em>hostname</em>. 1160</p> 1161<pre class="code"> 1162 p Socket.gethostbyname("hal") #=> ["localhost", ["hal"], 2, "\x7F\x00\x00\x01"]</pre> 1163 1164 1165 </div> 1166</div> 1167<div class="tags"> 1168 1169 <div class="docstring"> 1170 <div class="discussion"> 1171 1172 1173 </div> 1174</div> 1175<div class="tags"> 1176 <h3>Returns:</h3> 1177<ul class="return"> 1178 1179 <li> 1180 1181 <span class='type'>(<tt><span class='object_link'><a href="Array.html" title="Array (class)">Array</a></span></tt>)</span> 1182 1183 1184 1185 1186 </li> 1187 1188</ul> 1189 1190</div> 1191 1192</div><table class="source_code"> 1193 <tr> 1194 <td> 1195 <pre class="lines"> 1196 1197 1198</pre> 1199 </td> 1200 <td> 1201 <pre class="code"><span class="info file"># File 'ext/socket/socket.c'</span> 1202 1203/* 1204 * call-seq: 1205 * Socket.gethostbyname(hostname) => [official_hostname, alias_hostnames, address_family, *address_list] 1206 * 1207 * Obtains the host information for _hostname_. 1208 * 1209 * p Socket.gethostbyname("hal") #=> ["localhost", ["hal"], 2, "\x7F\x00\x00\x01"] 1210 * 1211 */ 1212static VALUE 1213sock_s_gethostbyname(VALUE obj, VALUE host) 1214{ 1215 rb_secure(3); 1216 return rsock_make_hostent(host, rsock_addrinfo(host, Qnil, SOCK_STREAM, AI_CANONNAME), sock_sockaddr); 1217}</pre> 1218 </td> 1219 </tr> 1220</table> 1221</div> 1222 1223 <div class="method_details "> 1224 <p class="signature " id="gethostname-class_method"> 1225 1226 + (<tt><span class='object_link'><a href="Object.html" title="Object (class)">Object</a></span></tt>) <strong>gethostname</strong> 1227 1228 1229 1230</p><div class="docstring"> 1231 <div class="discussion"> 1232 <p> 1233Returns the hostname. 1234</p> 1235<pre class="code"> 1236 p Socket.gethostname #=> "hal" 1237</pre> 1238<p> 1239Note that it is not guaranteed to be able to convert to IP address using 1240gethostbyname, getaddrinfo, etc. If you need local IP address, use 1241Socket.ip_address_list. 1242</p> 1243 1244 1245 </div> 1246</div> 1247<div class="tags"> 1248 1249 1250 1251</div><table class="source_code"> 1252 <tr> 1253 <td> 1254 <pre class="lines"> 1255 1256 1257</pre> 1258 </td> 1259 <td> 1260 <pre class="code"><span class="info file"># File 'ext/socket/socket.c'</span> 1261 1262/* 1263 * call-seq: 1264 * Socket.gethostname => hostname 1265 * 1266 * Returns the hostname. 1267 * 1268 * p Socket.gethostname #=> "hal" 1269 * 1270 * Note that it is not guaranteed to be able to convert to IP address using gethostbyname, getaddrinfo, etc. 1271 * If you need local IP address, use Socket.ip_address_list. 1272 */ 1273static VALUE 1274sock_gethostname(VALUE obj) 1275{ 1276#ifndef HOST_NAME_MAX 1277# define HOST_NAME_MAX 1024 1278#endif 1279 char buf[HOST_NAME_MAX+1]; 1280 1281 rb_secure(3); 1282 if (gethostname(buf, (int)sizeof buf - 1) < 0) 1283 rb_sys_fail("gethostname"); 1284 1285 buf[sizeof buf - 1] = '\0'; 1286 return rb_str_new2(buf); 1287}</pre> 1288 </td> 1289 </tr> 1290</table> 1291</div> 1292 1293 <div class="method_details "> 1294 <p class="signature " id="getnameinfo-class_method"> 1295 1296 + (<tt><span class='object_link'><a href="Array.html" title="Array (class)">Array</a></span></tt>) <strong>getnameinfo</strong>(sockaddr[, flags]) 1297 1298 1299 1300</p><div class="docstring"> 1301 <div class="discussion"> 1302 <p> 1303Obtains name information for <em>sockaddr</em>. 1304</p> 1305<p> 1306<em>sockaddr</em> should be one of follows. 1307</p> 1308<ul> 1309<li><p> 1310packed sockaddr string such as Socket.sockaddr_in(80, 1311“127.0.0.1”) 1312</p> 1313</li> 1314<li><p> 13153-elements array such as [“AF_INET”, 80, 1316“127.0.0.1”] 1317</p> 1318</li> 1319<li><p> 13204-elements array such as [“AF_INET”, 80, ignored, 1321“127.0.0.1”] 1322</p> 1323</li> 1324</ul> 1325<p> 1326<em>flags</em> should be bitwise OR of Socket::NI_* constants. 1327</p> 1328<p> 1329Note that the last form is compatible with IPSocket#addr,peeraddr. 1330</p> 1331<pre class="code"> 1332 Socket.getnameinfo(Socket.sockaddr_in(80, "127.0.0.1")) #=> ["localhost", "www"] 1333 Socket.getnameinfo(["AF_INET", 80, "127.0.0.1"]) #=> ["localhost", "www"] 1334 Socket.getnameinfo(["AF_INET", 80, "localhost", "127.0.0.1"]) #=> ["localhost", "www"] 1335</pre> 1336<p> 1337If Addrinfo object is preferred, use Addrinfo#getnameinfo. 1338</p> 1339 1340 1341 </div> 1342</div> 1343<div class="tags"> 1344 1345 <div class="docstring"> 1346 <div class="discussion"> 1347 1348 1349 </div> 1350</div> 1351<div class="tags"> 1352 <h3>Returns:</h3> 1353<ul class="return"> 1354 1355 <li> 1356 1357 <span class='type'>(<tt><span class='object_link'><a href="Array.html" title="Array (class)">Array</a></span></tt>)</span> 1358 1359 1360 1361 1362 </li> 1363 1364</ul> 1365 1366</div> 1367 1368</div><table class="source_code"> 1369 <tr> 1370 <td> 1371 <pre class="lines"> 1372 1373 1374</pre> 1375 </td> 1376 <td> 1377 <pre class="code"><span class="info file"># File 'ext/socket/socket.c'</span> 1378 1379/* 1380 * call-seq: 1381 * Socket.getnameinfo(sockaddr [, flags]) => [hostname, servicename] 1382 * 1383 * Obtains name information for _sockaddr_. 1384 * 1385 * _sockaddr_ should be one of follows. 1386 * - packed sockaddr string such as Socket.sockaddr_in(80, "127.0.0.1") 1387 * - 3-elements array such as ["AF_INET", 80, "127.0.0.1"] 1388 * - 4-elements array such as ["AF_INET", 80, ignored, "127.0.0.1"] 1389 * 1390 * _flags_ should be bitwise OR of Socket::NI_* constants. 1391 * 1392 * Note that the last form is compatible with IPSocket#{addr,peeraddr}. 1393 * 1394 * Socket.getnameinfo(Socket.sockaddr_in(80, "127.0.0.1")) #=> ["localhost", "www"] 1395 * Socket.getnameinfo(["AF_INET", 80, "127.0.0.1"]) #=> ["localhost", "www"] 1396 * Socket.getnameinfo(["AF_INET", 80, "localhost", "127.0.0.1"]) #=> ["localhost", "www"] 1397 * 1398 * If Addrinfo object is preferred, use Addrinfo#getnameinfo. 1399 */ 1400static VALUE 1401sock_s_getnameinfo(int argc, VALUE *argv) 1402{ 1403 VALUE sa, af = Qnil, host = Qnil, port = Qnil, flags, tmp; 1404 char *hptr, *pptr; 1405 char hbuf[1024], pbuf[1024]; 1406 int fl; 1407 struct addrinfo hints, *res = NULL, *r; 1408 int error; 1409 struct sockaddr_storage ss; 1410 struct sockaddr *sap; 1411 1412 sa = flags = Qnil; 1413 rb_scan_args(argc, argv, "11", &sa, &flags); 1414 1415 fl = 0; 1416 if (!NIL_P(flags)) { 1417 fl = NUM2INT(flags); 1418 } 1419 tmp = rb_check_sockaddr_string_type(sa); 1420 if (!NIL_P(tmp)) { 1421 sa = tmp; 1422 if (sizeof(ss) < (size_t)RSTRING_LEN(sa)) { 1423 rb_raise(rb_eTypeError, "sockaddr length too big"); 1424 } 1425 memcpy(&ss, RSTRING_PTR(sa), RSTRING_LEN(sa)); 1426 if ((size_t)RSTRING_LEN(sa) != SS_LEN(&ss)) { 1427 rb_raise(rb_eTypeError, "sockaddr size differs - should not happen"); 1428 } 1429 sap = (struct sockaddr*)&ss; 1430 goto call_nameinfo; 1431 } 1432 tmp = rb_check_array_type(sa); 1433 if (!NIL_P(tmp)) { 1434 sa = tmp; 1435 MEMZERO(&hints, struct addrinfo, 1); 1436 if (RARRAY_LEN(sa) == 3) { 1437 af = RARRAY_PTR(sa)[0]; 1438 port = RARRAY_PTR(sa)[1]; 1439 host = RARRAY_PTR(sa)[2]; 1440 } 1441 else if (RARRAY_LEN(sa) >= 4) { 1442 af = RARRAY_PTR(sa)[0]; 1443 port = RARRAY_PTR(sa)[1]; 1444 host = RARRAY_PTR(sa)[3]; 1445 if (NIL_P(host)) { 1446 host = RARRAY_PTR(sa)[2]; 1447 } 1448 else { 1449 /* 1450 * 4th element holds numeric form, don't resolve. 1451 * see rsock_ipaddr(). 1452 */ 1453#ifdef AI_NUMERICHOST /* AIX 4.3.3 doesn't have AI_NUMERICHOST. */ 1454 hints.ai_flags |= AI_NUMERICHOST; 1455#endif 1456 } 1457 } 1458 else { 1459 rb_raise(rb_eArgError, "array size should be 3 or 4, %ld given", 1460 RARRAY_LEN(sa)); 1461 } 1462 /* host */ 1463 if (NIL_P(host)) { 1464 hptr = NULL; 1465 } 1466 else { 1467 strncpy(hbuf, StringValuePtr(host), sizeof(hbuf)); 1468 hbuf[sizeof(hbuf) - 1] = '\0'; 1469 hptr = hbuf; 1470 } 1471 /* port */ 1472 if (NIL_P(port)) { 1473 strcpy(pbuf, "0"); 1474 pptr = NULL; 1475 } 1476 else if (FIXNUM_P(port)) { 1477 snprintf(pbuf, sizeof(pbuf), "%ld", NUM2LONG(port)); 1478 pptr = pbuf; 1479 } 1480 else { 1481 strncpy(pbuf, StringValuePtr(port), sizeof(pbuf)); 1482 pbuf[sizeof(pbuf) - 1] = '\0'; 1483 pptr = pbuf; 1484 } 1485 hints.ai_socktype = (fl & NI_DGRAM) ? SOCK_DGRAM : SOCK_STREAM; 1486 /* af */ 1487 hints.ai_family = NIL_P(af) ? PF_UNSPEC : rsock_family_arg(af); 1488 error = rb_getaddrinfo(hptr, pptr, &hints, &res); 1489 if (error) goto error_exit_addr; 1490 sap = res->ai_addr; 1491 } 1492 else { 1493 rb_raise(rb_eTypeError, "expecting String or Array"); 1494 } 1495 1496 call_nameinfo: 1497 error = rb_getnameinfo(sap, SA_LEN(sap), hbuf, sizeof(hbuf), 1498 pbuf, sizeof(pbuf), fl); 1499 if (error) goto error_exit_name; 1500 if (res) { 1501 for (r = res->ai_next; r; r = r->ai_next) { 1502 char hbuf2[1024], pbuf2[1024]; 1503 1504 sap = r->ai_addr; 1505 error = rb_getnameinfo(sap, SA_LEN(sap), hbuf2, sizeof(hbuf2), 1506 pbuf2, sizeof(pbuf2), fl); 1507 if (error) goto error_exit_name; 1508 if (strcmp(hbuf, hbuf2) != 0|| strcmp(pbuf, pbuf2) != 0) { 1509 freeaddrinfo(res); 1510 rb_raise(rb_eSocket, "sockaddr resolved to multiple nodename"); 1511 } 1512 } 1513 freeaddrinfo(res); 1514 } 1515 return rb_assoc_new(rb_str_new2(hbuf), rb_str_new2(pbuf)); 1516 1517 error_exit_addr: 1518 if (res) freeaddrinfo(res); 1519 rsock_raise_socket_error("getaddrinfo", error); 1520 1521 error_exit_name: 1522 if (res) freeaddrinfo(res); 1523 rsock_raise_socket_error("getnameinfo", error); 1524}</pre> 1525 </td> 1526 </tr> 1527</table> 1528</div> 1529 1530 <div class="method_details "> 1531 <p class="signature " id="getservbyname-class_method"> 1532 1533 1534 <span class="overload">+ (<tt><span class='object_link'><a href="Object.html" title="Object (class)">Object</a></span></tt>) <strong>getservbyname</strong>(service_name) </span> 1535 1536 <span class="overload">+ (<tt><span class='object_link'><a href="Object.html" title="Object (class)">Object</a></span></tt>) <strong>getservbyname</strong>(service_name, protocol_name) </span> 1537 1538 1539 1540 1541</p><div class="docstring"> 1542 <div class="discussion"> 1543 <p> 1544Obtains the port number for <em>service_name</em>. 1545</p> 1546<p> 1547If <em>protocol_name</em> is not given, “tcp” is assumed. 1548</p> 1549<pre class="code"> 1550 Socket.getservbyname("smtp") #=> 25 1551 Socket.getservbyname("shell") #=> 514 1552 Socket.getservbyname("syslog", "udp") #=> 514</pre> 1553 1554 1555 </div> 1556</div> 1557<div class="tags"> 1558 1559</div><table class="source_code"> 1560 <tr> 1561 <td> 1562 <pre class="lines"> 1563 1564 1565</pre> 1566 </td> 1567 <td> 1568 <pre class="code"><span class="info file"># File 'ext/socket/socket.c'</span> 1569 1570/* 1571 * call-seq: 1572 * Socket.getservbyname(service_name) => port_number 1573 * Socket.getservbyname(service_name, protocol_name) => port_number 1574 * 1575 * Obtains the port number for _service_name_. 1576 * 1577 * If _protocol_name_ is not given, "tcp" is assumed. 1578 * 1579 * Socket.getservbyname("smtp") #=> 25 1580 * Socket.getservbyname("shell") #=> 514 1581 * Socket.getservbyname("syslog", "udp") #=> 514 1582 */ 1583static VALUE 1584sock_s_getservbyname(int argc, VALUE *argv) 1585{ 1586 VALUE service, proto; 1587 struct servent *sp; 1588 long port; 1589 const char *servicename, *protoname = "tcp"; 1590 1591 rb_scan_args(argc, argv, "11", &service, &proto); 1592 StringValue(service); 1593 if (!NIL_P(proto)) StringValue(proto); 1594 servicename = StringValueCStr(service); 1595 if (!NIL_P(proto)) protoname = StringValueCStr(proto); 1596 sp = getservbyname(servicename, protoname); 1597 if (sp) { 1598 port = ntohs(sp->s_port); 1599 } 1600 else { 1601 char *end; 1602 1603 port = STRTOUL(servicename, &end, 0); 1604 if (*end != '\0') { 1605 rb_raise(rb_eSocket, "no such service %s/%s", servicename, protoname); 1606 } 1607 } 1608 return INT2FIX(port); 1609}</pre> 1610 </td> 1611 </tr> 1612</table> 1613</div> 1614 1615 <div class="method_details "> 1616 <p class="signature " id="getservbyport-class_method"> 1617 1618 + (<tt><span class='object_link'><a href="Object.html" title="Object (class)">Object</a></span></tt>) <strong>getservbyport</strong>(port[, protocol_name]) 1619 1620 1621 1622</p><div class="docstring"> 1623 <div class="discussion"> 1624 <p> 1625Obtains the port number for <em>port</em>. 1626</p> 1627<p> 1628If <em>protocol_name</em> is not given, “tcp” is assumed. 1629</p> 1630<pre class="code"> 1631 Socket.getservbyport(80) #=> "www" 1632 Socket.getservbyport(514, "tcp") #=> "shell" 1633 Socket.getservbyport(514, "udp") #=> "syslog"</pre> 1634 1635 1636 </div> 1637</div> 1638<div class="tags"> 1639 1640 1641 1642</div><table class="source_code"> 1643 <tr> 1644 <td> 1645 <pre class="lines"> 1646 1647 1648</pre> 1649 </td> 1650 <td> 1651 <pre class="code"><span class="info file"># File 'ext/socket/socket.c'</span> 1652 1653/* 1654 * call-seq: 1655 * Socket.getservbyport(port [, protocol_name]) => service 1656 * 1657 * Obtains the port number for _port_. 1658 * 1659 * If _protocol_name_ is not given, "tcp" is assumed. 1660 * 1661 * Socket.getservbyport(80) #=> "www" 1662 * Socket.getservbyport(514, "tcp") #=> "shell" 1663 * Socket.getservbyport(514, "udp") #=> "syslog" 1664 * 1665 */ 1666static VALUE 1667sock_s_getservbyport(int argc, VALUE *argv) 1668{ 1669 VALUE port, proto; 1670 struct servent *sp; 1671 long portnum; 1672 const char *protoname = "tcp"; 1673 1674 rb_scan_args(argc, argv, "11", &port, &proto); 1675 portnum = NUM2LONG(port); 1676 if (portnum != (uint16_t)portnum) { 1677 const char *s = portnum > 0 ? "big" : "small"; 1678 rb_raise(rb_eRangeError, "integer %ld too %s to convert into `int16_t'", portnum, s); 1679 } 1680 if (!NIL_P(proto)) protoname = StringValueCStr(proto); 1681 1682 sp = getservbyport((int)htons((uint16_t)portnum), protoname); 1683 if (!sp) { 1684 rb_raise(rb_eSocket, "no such service for port %d/%s", (int)portnum, protoname); 1685 } 1686 return rb_tainted_str_new2(sp->s_name); 1687}</pre> 1688 </td> 1689 </tr> 1690</table> 1691</div> 1692 1693 <div class="method_details "> 1694 <p class="signature " id="ip_address_list-class_method"> 1695 1696 + (<tt><span class='object_link'><a href="Array.html" title="Array (class)">Array</a></span></tt>) <strong>ip_address_list</strong> 1697 1698 1699 1700</p><div class="docstring"> 1701 <div class="discussion"> 1702 <p> 1703Returns local IP addresses as an array. 1704</p> 1705<p> 1706The array contains Addrinfo objects. 1707</p> 1708<pre class="code"> 1709 pp Socket.ip_address_list 1710 #=> [#<Addrinfo: 127.0.0.1>, 1711 #<Addrinfo: 192.168.0.128>, 1712 #<Addrinfo: ::1>, 1713 ...]</pre> 1714 1715 1716 </div> 1717</div> 1718<div class="tags"> 1719 1720 <div class="docstring"> 1721 <div class="discussion"> 1722 1723 1724 </div> 1725</div> 1726<div class="tags"> 1727 <h3>Returns:</h3> 1728<ul class="return"> 1729 1730 <li> 1731 1732 <span class='type'>(<tt><span class='object_link'><a href="Array.html" title="Array (class)">Array</a></span></tt>)</span> 1733 1734 1735 1736 1737 </li> 1738 1739</ul> 1740 1741</div> 1742 1743</div><table class="source_code"> 1744 <tr> 1745 <td> 1746 <pre class="lines"> 1747 1748 1749</pre> 1750 </td> 1751 <td> 1752 <pre class="code"><span class="info file"># File 'ext/socket/socket.c'</span> 1753 1754/* 1755 * call-seq: 1756 * Socket.ip_address_list => array 1757 * 1758 * Returns local IP addresses as an array. 1759 * 1760 * The array contains Addrinfo objects. 1761 * 1762 * pp Socket.ip_address_list 1763 * #=> [#<Addrinfo: 127.0.0.1>, 1764 * #<Addrinfo: 192.168.0.128>, 1765 * #<Addrinfo: ::1>, 1766 * ...] 1767 * 1768 */ 1769static VALUE 1770socket_s_ip_address_list(VALUE self) 1771{ 1772#if defined(HAVE_GETIFADDRS) 1773 struct ifaddrs *ifp = NULL; 1774 struct ifaddrs *p; 1775 int ret; 1776 VALUE list; 1777 1778 ret = getifaddrs(&ifp); 1779 if (ret == -1) { 1780 rb_sys_fail("getifaddrs"); 1781 } 1782 1783 list = rb_ary_new(); 1784 for (p = ifp; p; p = p->ifa_next) { 1785 if (p->ifa_addr != NULL && IS_IP_FAMILY(p->ifa_addr->sa_family)) { 1786 rb_ary_push(list, sockaddr_obj(p->ifa_addr)); 1787 } 1788 } 1789 1790 freeifaddrs(ifp); 1791 1792 return list; 1793#elif defined(SIOCGLIFCONF) && defined(SIOCGLIFNUM) && !defined(__hpux) 1794 /* Solaris if_tcp(7P) */ 1795 /* HP-UX has SIOCGLIFCONF too. But it uses different struct */ 1796 int fd = -1; 1797 int ret; 1798 struct lifnum ln; 1799 struct lifconf lc; 1800 char *reason = NULL; 1801 int save_errno; 1802 int i; 1803 VALUE list = Qnil; 1804 1805 lc.lifc_buf = NULL; 1806 1807 fd = socket(AF_INET, SOCK_DGRAM, 0); 1808 if (fd == -1) 1809 rb_sys_fail("socket"); 1810 1811 memset(&ln, 0, sizeof(ln)); 1812 ln.lifn_family = AF_UNSPEC; 1813 1814 ret = ioctl(fd, SIOCGLIFNUM, &ln); 1815 if (ret == -1) { 1816 reason = "SIOCGLIFNUM"; 1817 goto finish; 1818 } 1819 1820 memset(&lc, 0, sizeof(lc)); 1821 lc.lifc_family = AF_UNSPEC; 1822 lc.lifc_flags = 0; 1823 lc.lifc_len = sizeof(struct lifreq) * ln.lifn_count; 1824 lc.lifc_req = xmalloc(lc.lifc_len); 1825 1826 ret = ioctl(fd, SIOCGLIFCONF, &lc); 1827 if (ret == -1) { 1828 reason = "SIOCGLIFCONF"; 1829 goto finish; 1830 } 1831 1832 list = rb_ary_new(); 1833 for (i = 0; i < ln.lifn_count; i++) { 1834 struct lifreq *req = &lc.lifc_req[i]; 1835 if (IS_IP_FAMILY(req->lifr_addr.ss_family)) { 1836 if (req->lifr_addr.ss_family == AF_INET6 && 1837 IN6_IS_ADDR_LINKLOCAL(&((struct sockaddr_in6 *)(&req->lifr_addr))->sin6_addr) && 1838 ((struct sockaddr_in6 *)(&req->lifr_addr))->sin6_scope_id == 0) { 1839 struct lifreq req2; 1840 memcpy(req2.lifr_name, req->lifr_name, LIFNAMSIZ); 1841 ret = ioctl(fd, SIOCGLIFINDEX, &req2); 1842 if (ret == -1) { 1843 reason = "SIOCGLIFINDEX"; 1844 goto finish; 1845 } 1846 ((struct sockaddr_in6 *)(&req->lifr_addr))->sin6_scope_id = req2.lifr_index; 1847 } 1848 rb_ary_push(list, sockaddr_obj((struct sockaddr *)&req->lifr_addr)); 1849 } 1850 } 1851 1852 finish: 1853 save_errno = errno; 1854 if (lc.lifc_buf != NULL) 1855 xfree(lc.lifc_req); 1856 if (fd != -1) 1857 close(fd); 1858 errno = save_errno; 1859 1860 if (reason) 1861 rb_sys_fail(reason); 1862 return list; 1863 1864#elif defined(SIOCGIFCONF) 1865 int fd = -1; 1866 int ret; 1867#define EXTRA_SPACE (sizeof(struct ifconf) + sizeof(struct sockaddr_storage)) 1868 char initbuf[4096+EXTRA_SPACE]; 1869 char *buf = initbuf; 1870 int bufsize; 1871 struct ifconf conf; 1872 struct ifreq *req; 1873 VALUE list = Qnil; 1874 const char *reason = NULL; 1875 int save_errno; 1876 1877 fd = socket(AF_INET, SOCK_DGRAM, 0); 1878 if (fd == -1) 1879 rb_sys_fail("socket"); 1880 1881 bufsize = sizeof(initbuf); 1882 buf = initbuf; 1883 1884 retry: 1885 conf.ifc_len = bufsize; 1886 conf.ifc_req = (struct ifreq *)buf; 1887 1888 /* fprintf(stderr, "bufsize: %d\n", bufsize); */ 1889 1890 ret = ioctl(fd, SIOCGIFCONF, &conf); 1891 if (ret == -1) { 1892 reason = "SIOCGIFCONF"; 1893 goto finish; 1894 } 1895 1896 /* fprintf(stderr, "conf.ifc_len: %d\n", conf.ifc_len); */ 1897 1898 if (bufsize - EXTRA_SPACE < conf.ifc_len) { 1899 if (bufsize < conf.ifc_len) { 1900 /* NetBSD returns required size for all interfaces. */ 1901 bufsize = conf.ifc_len + EXTRA_SPACE; 1902 } 1903 else { 1904 bufsize = bufsize << 1; 1905 } 1906 if (buf == initbuf) 1907 buf = NULL; 1908 buf = xrealloc(buf, bufsize); 1909 goto retry; 1910 } 1911 1912 close(fd); 1913 fd = -1; 1914 1915 list = rb_ary_new(); 1916 req = conf.ifc_req; 1917 while ((char*)req < (char*)conf.ifc_req + conf.ifc_len) { 1918 struct sockaddr *addr = &req->ifr_addr; 1919 if (IS_IP_FAMILY(addr->sa_family)) { 1920 rb_ary_push(list, sockaddr_obj(addr)); 1921 } 1922#ifdef HAVE_SA_LEN 1923# ifndef _SIZEOF_ADDR_IFREQ 1924# define _SIZEOF_ADDR_IFREQ(r) \ 1925 (sizeof(struct ifreq) + \ 1926 (sizeof(struct sockaddr) < (r).ifr_addr.sa_len ? \ 1927 (r).ifr_addr.sa_len - sizeof(struct sockaddr) : \ 1928 0)) 1929# endif 1930 req = (struct ifreq *)((char*)req + _SIZEOF_ADDR_IFREQ(*req)); 1931#else 1932 req = (struct ifreq *)((char*)req + sizeof(struct ifreq)); 1933#endif 1934 } 1935 1936 finish: 1937 1938 save_errno = errno; 1939 if (buf != initbuf) 1940 xfree(buf); 1941 if (fd != -1) 1942 close(fd); 1943 errno = save_errno; 1944 1945 if (reason) 1946 rb_sys_fail(reason); 1947 return list; 1948 1949#undef EXTRA_SPACE 1950#elif defined(_WIN32) 1951 typedef struct ip_adapter_unicast_address_st { 1952 unsigned LONG_LONG dummy0; 1953 struct ip_adapter_unicast_address_st *Next; 1954 struct { 1955 struct sockaddr *lpSockaddr; 1956 int iSockaddrLength; 1957 } Address; 1958 int dummy1; 1959 int dummy2; 1960 int dummy3; 1961 long dummy4; 1962 long dummy5; 1963 long dummy6; 1964 } ip_adapter_unicast_address_t; 1965 typedef struct ip_adapter_anycast_address_st { 1966 unsigned LONG_LONG dummy0; 1967 struct ip_adapter_anycast_address_st *Next; 1968 struct { 1969 struct sockaddr *lpSockaddr; 1970 int iSockaddrLength; 1971 } Address; 1972 } ip_adapter_anycast_address_t; 1973 typedef struct ip_adapter_addresses_st { 1974 unsigned LONG_LONG dummy0; 1975 struct ip_adapter_addresses_st *Next; 1976 void *dummy1; 1977 ip_adapter_unicast_address_t *FirstUnicastAddress; 1978…
Large files files are truncated, but you can click here to view the full file