PageRenderTime 50ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/htdocs/ruby-stdlib/Socket.html

https://github.com/JerryWho/railsdoccompilation
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. <script type="text/javascript" charset="utf-8">
  10. relpath = '';
  11. if (relpath != '') relpath += '/';
  12. </script>
  13. <script type="text/javascript" charset="utf-8" src="js/jquery.js"></script>
  14. <script type="text/javascript" charset="utf-8" src="js/app.js"></script>
  15. </head>
  16. <body>
  17. <script type="text/javascript" charset="utf-8">
  18. if (window.top.frames.main) document.body.className = 'frames';
  19. </script>
  20. <div id="header">
  21. <div id="menu">
  22. <a href="_index.html">Index (S)</a> &raquo;
  23. <span class="title">Socket</span>
  24. <div class="noframes"><span class="title">(</span><a href="." target="_top">no frames</a><span class="title">)</span></div>
  25. </div>
  26. <div id="search">
  27. <a id="class_list_link" href="#">Class List</a>
  28. <a id="method_list_link" href="#">Method List</a>
  29. <a id ="file_list_link" href="#">File List</a>
  30. </div>
  31. <div class="clear"></div>
  32. </div>
  33. <iframe id="search_frame"></iframe>
  34. <div id="content"><h1>Class: Socket
  35. </h1>
  36. <dl class="box">
  37. <dt class="r1">Inherits:</dt>
  38. <dd class="r1">
  39. <span class="inheritName"><span class='object_link'><a href="BasicSocket.html" title="BasicSocket (class)">BasicSocket</a></span></span>
  40. <ul class="fullTree">
  41. <li><span class='object_link'><a href="Object.html" title="Object (class)">Object</a></span></li>
  42. <li class="next"><span class='object_link'><a href="IO.html" title="IO (class)">IO</a></span></li>
  43. <li class="next"><span class='object_link'><a href="BasicSocket.html" title="BasicSocket (class)">BasicSocket</a></span></li>
  44. <li class="next">Socket</li>
  45. </ul>
  46. <a href="#" class="inheritanceTree">show all</a>
  47. </dd>
  48. <dt class="r2 last">Defined in:</dt>
  49. <dd class="r2 last">lib/ipaddr.rb<span class="defines">,<br />
  50. ext/socket/socket.c</span>
  51. </dd>
  52. </dl>
  53. <div class="clear"></div>
  54. <h2>Defined Under Namespace</h2>
  55. <p class="children">
  56. <strong class="modules">Modules:</strong> <span class='object_link'><a href="Socket/Constants.html" title="Socket::Constants (module)">Constants</a></span>
  57. <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>
  58. </p>
  59. <h2>Constant Summary</h2>
  60. <dl class="constants">
  61. <dt id="AF_INET6-constant" class="">AF_INET6 =
  62. </dt>
  63. <dd><pre class="code"><span class='const'>Object</span><span class='period'>.</span><span class='id new'>new</span></pre></dd>
  64. </dl>
  65. <h2>
  66. Class Method Summary
  67. <small>(<a href="#" class="summary_toggle">collapse</a>)</small>
  68. </h2>
  69. <ul class="summary">
  70. <li class="public ">
  71. <span class="summary_signature">
  72. <a href="#getaddrinfo-class_method" title="getaddrinfo (class method)">+ (Array) <strong>getaddrinfo</strong>(nodename, servname[, family[, socktype[, protocol[, flags[, reverse_lookup]]]]]) </a>
  73. </span>
  74. <span class="summary_desc"><div class='inline'><p>
  75. Obtains address information for <em>nodename</em>:<em>servname</em>.
  76. </p>
  77. </div></span>
  78. </li>
  79. <li class="public ">
  80. <span class="summary_signature">
  81. <a href="#gethostbyaddr-class_method" title="gethostbyaddr (class method)">+ (Object) <strong>gethostbyaddr</strong>(address_string[, address_family]) </a>
  82. </span>
  83. <span class="summary_desc"><div class='inline'><p>
  84. Obtains the host information for <em>address</em>.
  85. </p>
  86. </div></span>
  87. </li>
  88. <li class="public ">
  89. <span class="summary_signature">
  90. <a href="#gethostbyname-class_method" title="gethostbyname (class method)">+ (Array) <strong>gethostbyname</strong>(hostname) </a>
  91. </span>
  92. <span class="summary_desc"><div class='inline'><p>
  93. Obtains the host information for <em>hostname</em>.
  94. </p>
  95. </div></span>
  96. </li>
  97. <li class="public ">
  98. <span class="summary_signature">
  99. <a href="#gethostname-class_method" title="gethostname (class method)">+ (Object) <strong>gethostname</strong> </a>
  100. </span>
  101. <span class="summary_desc"><div class='inline'><p>
  102. Returns the hostname.
  103. </p>
  104. </div></span>
  105. </li>
  106. <li class="public ">
  107. <span class="summary_signature">
  108. <a href="#getnameinfo-class_method" title="getnameinfo (class method)">+ (Array) <strong>getnameinfo</strong>(sockaddr[, flags]) </a>
  109. </span>
  110. <span class="summary_desc"><div class='inline'><p>
  111. Obtains name information for <em>sockaddr</em>.
  112. </p>
  113. </div></span>
  114. </li>
  115. <li class="public ">
  116. <span class="summary_signature">
  117. <a href="#getservbyname-class_method" title="getservbyname (class method)">+ (Object) <strong>getservbyname</strong> </a>
  118. </span>
  119. <span class="summary_desc"><div class='inline'><p>
  120. Obtains the port number for <em>service_name</em>.
  121. </p>
  122. </div></span>
  123. </li>
  124. <li class="public ">
  125. <span class="summary_signature">
  126. <a href="#getservbyport-class_method" title="getservbyport (class method)">+ (Object) <strong>getservbyport</strong>(port[, protocol_name]) </a>
  127. </span>
  128. <span class="summary_desc"><div class='inline'><p>
  129. Obtains the port number for <em>port</em>.
  130. </p>
  131. </div></span>
  132. </li>
  133. <li class="public ">
  134. <span class="summary_signature">
  135. <a href="#ip_address_list-class_method" title="ip_address_list (class method)">+ (Array) <strong>ip_address_list</strong> </a>
  136. </span>
  137. <span class="summary_desc"><div class='inline'><p>
  138. Returns local IP addresses as an array.
  139. </p>
  140. </div></span>
  141. </li>
  142. <li class="public ">
  143. <span class="summary_signature">
  144. <a href="#pack_sockaddr_in-class_method" title="pack_sockaddr_in (class method)">+ (Object) <strong>pack_sockaddr_in</strong> </a>
  145. </span>
  146. <span class="summary_desc"><div class='inline'><p>
  147. Packs <em>port</em> and <em>host</em> as an AF_INET/AF_INET6 sockaddr
  148. string.
  149. </p>
  150. </div></span>
  151. </li>
  152. <li class="public ">
  153. <span class="summary_signature">
  154. <a href="#pack_sockaddr_un-class_method" title="pack_sockaddr_un (class method)">+ (Object) <strong>pack_sockaddr_un</strong> </a>
  155. </span>
  156. <span class="summary_desc"><div class='inline'><p>
  157. Packs <em>path</em> as an AF_UNIX sockaddr string.
  158. </p>
  159. </div></span>
  160. </li>
  161. <li class="public ">
  162. <span class="summary_signature">
  163. <a href="#pair-class_method" title="pair (class method)">+ (Object) <strong>pair</strong> </a>
  164. </span>
  165. <span class="summary_desc"><div class='inline'><p>
  166. Creates a pair of sockets connected each other.
  167. </p>
  168. </div></span>
  169. </li>
  170. <li class="public ">
  171. <span class="summary_signature">
  172. <a href="#sockaddr_in-class_method" title="sockaddr_in (class method)">+ (Object) <strong>sockaddr_in</strong> </a>
  173. </span>
  174. <span class="summary_desc"><div class='inline'><p>
  175. Packs <em>port</em> and <em>host</em> as an AF_INET/AF_INET6 sockaddr
  176. string.
  177. </p>
  178. </div></span>
  179. </li>
  180. <li class="public ">
  181. <span class="summary_signature">
  182. <a href="#sockaddr_un-class_method" title="sockaddr_un (class method)">+ (Object) <strong>sockaddr_un</strong> </a>
  183. </span>
  184. <span class="summary_desc"><div class='inline'><p>
  185. Packs <em>path</em> as an AF_UNIX sockaddr string.
  186. </p>
  187. </div></span>
  188. </li>
  189. <li class="public ">
  190. <span class="summary_signature">
  191. <a href="#socketpair-class_method" title="socketpair (class method)">+ (Object) <strong>socketpair</strong> </a>
  192. </span>
  193. <span class="summary_desc"><div class='inline'><p>
  194. Creates a pair of sockets connected each other.
  195. </p>
  196. </div></span>
  197. </li>
  198. <li class="public ">
  199. <span class="summary_signature">
  200. <a href="#unpack_sockaddr_in-class_method" title="unpack_sockaddr_in (class method)">+ (Array) <strong>unpack_sockaddr_in</strong>(sockaddr) </a>
  201. </span>
  202. <span class="summary_desc"><div class='inline'><p>
  203. Unpacks <em>sockaddr</em> into port and ip_address.
  204. </p>
  205. </div></span>
  206. </li>
  207. <li class="public ">
  208. <span class="summary_signature">
  209. <a href="#unpack_sockaddr_un-class_method" title="unpack_sockaddr_un (class method)">+ (Object) <strong>unpack_sockaddr_un</strong>(sockaddr) </a>
  210. </span>
  211. <span class="summary_desc"><div class='inline'><p>
  212. Unpacks <em>sockaddr</em> into path.
  213. </p>
  214. </div></span>
  215. </li>
  216. </ul>
  217. <h2>
  218. Instance Method Summary
  219. <small>(<a href="#" class="summary_toggle">collapse</a>)</small>
  220. </h2>
  221. <ul class="summary">
  222. <li class="public ">
  223. <span class="summary_signature">
  224. <a href="#accept-instance_method" title="#accept (instance method)">- (Array) <strong>accept</strong> </a>
  225. </span>
  226. <span class="summary_desc"><div class='inline'><p>
  227. Accepts a next connection.
  228. </p>
  229. </div></span>
  230. </li>
  231. <li class="public ">
  232. <span class="summary_signature">
  233. <a href="#accept_nonblock-instance_method" title="#accept_nonblock (instance method)">- (Array) <strong>accept_nonblock</strong> </a>
  234. </span>
  235. <span class="summary_desc"><div class='inline'><p>
  236. Accepts an incoming connection using accept(2) after O_NONBLOCK is set for
  237. the underlying file descriptor.
  238. </p>
  239. </div></span>
  240. </li>
  241. <li class="public ">
  242. <span class="summary_signature">
  243. <a href="#bind-instance_method" title="#bind (instance method)">- (0) <strong>bind</strong>(local_sockaddr) </a>
  244. </span>
  245. <span class="summary_desc"><div class='inline'><p>
  246. Binds to the given local address.
  247. </p>
  248. </div></span>
  249. </li>
  250. <li class="public ">
  251. <span class="summary_signature">
  252. <a href="#connect-instance_method" title="#connect (instance method)">- (0) <strong>connect</strong>(remote_sockaddr) </a>
  253. </span>
  254. <span class="summary_desc"><div class='inline'><p>
  255. Requests a connection to be made on the given <tt>remote_sockaddr</tt>.
  256. </p>
  257. </div></span>
  258. </li>
  259. <li class="public ">
  260. <span class="summary_signature">
  261. <a href="#connect_nonblock-instance_method" title="#connect_nonblock (instance method)">- (0) <strong>connect_nonblock</strong>(remote_sockaddr) </a>
  262. </span>
  263. <span class="summary_desc"><div class='inline'><p>
  264. Requests a connection to be made on the given <tt>remote_sockaddr</tt>
  265. after O_NONBLOCK is set for the underlying file descriptor.
  266. </p>
  267. </div></span>
  268. </li>
  269. <li class="public ">
  270. <span class="summary_signature">
  271. <a href="#new-instance_method" title="#new (instance method)">- (Object) <strong>new</strong>(domain, socktype[, protocol]) </a>
  272. </span>
  273. <span class="note title constructor">constructor</span>
  274. <span class="summary_desc"><div class='inline'><p>
  275. Creates a new socket object.
  276. </p>
  277. </div></span>
  278. </li>
  279. <li class="public ">
  280. <span class="summary_signature">
  281. <a href="#listen-instance_method" title="#listen (instance method)">- (0) <strong>listen</strong>(int) </a>
  282. </span>
  283. <span class="summary_desc"><div class='inline'><p>
  284. Listens for connections, using the specified <tt>int</tt> as the backlog.
  285. </p>
  286. </div></span>
  287. </li>
  288. <li class="public ">
  289. <span class="summary_signature">
  290. <a href="#recvfrom-instance_method" title="#recvfrom (instance method)">- (Object) <strong>recvfrom</strong> </a>
  291. </span>
  292. <span class="summary_desc"><div class='inline'><p>
  293. Receives up to <em>maxlen</em> bytes from <tt>socket</tt>.
  294. </p>
  295. </div></span>
  296. </li>
  297. <li class="public ">
  298. <span class="summary_signature">
  299. <a href="#recvfrom_nonblock-instance_method" title="#recvfrom_nonblock (instance method)">- (Object) <strong>recvfrom_nonblock</strong> </a>
  300. </span>
  301. <span class="summary_desc"><div class='inline'><p>
  302. Receives up to <em>maxlen</em> bytes from <tt>socket</tt> using recvfrom(2)
  303. after O_NONBLOCK is set for the underlying file descriptor.
  304. </p>
  305. </div></span>
  306. </li>
  307. <li class="public ">
  308. <span class="summary_signature">
  309. <a href="#sysaccept-instance_method" title="#sysaccept (instance method)">- (Array) <strong>sysaccept</strong> </a>
  310. </span>
  311. <span class="summary_desc"><div class='inline'><p>
  312. Accepts an incoming connection returning an array containing the (integer)
  313. file descriptor for the incoming connection, <em>client_socket_fd</em>, and
  314. an Addrinfo, <em>client_addrinfo</em>.
  315. </p>
  316. </div></span>
  317. </li>
  318. </ul>
  319. <h3 class="inherited">Methods inherited from <span class='object_link'><a href="BasicSocket.html" title="BasicSocket (class)">BasicSocket</a></span></h3>
  320. <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>
  321. <h3 class="inherited">Methods inherited from <span class='object_link'><a href="IO.html" title="IO (class)">IO</a></span></h3>
  322. <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>
  323. <div id="constructor_details" class="method_details_list">
  324. <h2>Constructor Details</h2>
  325. <div class="method_details first">
  326. <p class="signature first" id="initialize-instance_method">
  327. - (<tt><span class='object_link'><a href="Object.html" title="Object (class)">Object</a></span></tt>) <strong>new</strong>(domain, socktype[, protocol])
  328. </p><div class="docstring">
  329. <div class="discussion">
  330. <p>
  331. Creates a new socket object.
  332. </p>
  333. <p>
  334. <em>domain</em> should be a communications domain such as: :INET, :INET6,
  335. :UNIX, etc.
  336. </p>
  337. <p>
  338. <em>socktype</em> should be a socket type such as: :STREAM, :DGRAM, :RAW,
  339. etc.
  340. </p>
  341. <p>
  342. <em>protocol</em> should be a protocol defined in the domain. This is
  343. optional. If it is not given, 0 is used internally.
  344. </p>
  345. <pre class="code">
  346. Socket.new(:INET, :STREAM) # TCP socket
  347. Socket.new(:INET, :DGRAM) # UDP socket
  348. Socket.new(:UNIX, :STREAM) # UNIX stream socket
  349. Socket.new(:UNIX, :DGRAM) # UNIX datagram socket</pre>
  350. </div>
  351. </div>
  352. <div class="tags">
  353. </div><table class="source_code">
  354. <tr>
  355. <td>
  356. <pre class="lines">
  357. </pre>
  358. </td>
  359. <td>
  360. <pre class="code"><span class="info file"># File 'ext/socket/socket.c'</span>
  361. /*
  362. * call-seq:
  363. * Socket.new(domain, socktype [, protocol]) =&gt; socket
  364. *
  365. * Creates a new socket object.
  366. *
  367. * _domain_ should be a communications domain such as: :INET, :INET6, :UNIX, etc.
  368. *
  369. * _socktype_ should be a socket type such as: :STREAM, :DGRAM, :RAW, etc.
  370. *
  371. * _protocol_ should be a protocol defined in the domain.
  372. * This is optional.
  373. * If it is not given, 0 is used internally.
  374. *
  375. * Socket.new(:INET, :STREAM) # TCP socket
  376. * Socket.new(:INET, :DGRAM) # UDP socket
  377. * Socket.new(:UNIX, :STREAM) # UNIX stream socket
  378. * Socket.new(:UNIX, :DGRAM) # UNIX datagram socket
  379. */
  380. static VALUE
  381. sock_initialize(int argc, VALUE *argv, VALUE sock)
  382. {
  383. VALUE domain, type, protocol;
  384. int fd;
  385. int d, t;
  386. rb_scan_args(argc, argv, &quot;21&quot;, &amp;domain, &amp;type, &amp;protocol);
  387. if (NIL_P(protocol))
  388. protocol = INT2FIX(0);
  389. rb_secure(3);
  390. setup_domain_and_type(domain, &amp;d, type, &amp;t);
  391. fd = rsock_socket(d, t, NUM2INT(protocol));
  392. if (fd &lt; 0) rb_sys_fail(&quot;socket(2)&quot;);
  393. return rsock_init_sock(sock, fd);
  394. }</pre>
  395. </td>
  396. </tr>
  397. </table>
  398. </div>
  399. </div>
  400. <div id="class_method_details" class="method_details_list">
  401. <h2>Class Method Details</h2>
  402. <div class="method_details first">
  403. <p class="signature first" id="getaddrinfo-class_method">
  404. + (<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]]]]])
  405. </p><div class="docstring">
  406. <div class="discussion">
  407. <p>
  408. Obtains address information for <em>nodename</em>:<em>servname</em>.
  409. </p>
  410. <p>
  411. <em>family</em> should be an address family such as: :INET, :INET6, :UNIX,
  412. etc.
  413. </p>
  414. <p>
  415. <em>socktype</em> should be a socket type such as: :STREAM, :DGRAM, :RAW,
  416. etc.
  417. </p>
  418. <p>
  419. <em>protocol</em> should be a protocol defined in the family. 0 is default
  420. protocol for the family.
  421. </p>
  422. <p>
  423. <em>flags</em> should be bitwise OR of Socket::AI_* constants.
  424. </p>
  425. <pre class="code">
  426. Socket.getaddrinfo(&quot;www.ruby-lang.org&quot;, &quot;http&quot;, nil, :STREAM)
  427. #=&gt; [[&quot;AF_INET&quot;, 80, &quot;carbon.ruby-lang.org&quot;, &quot;221.186.184.68&quot;, 2, 1, 6]] # PF_INET/SOCK_STREAM/IPPROTO_TCP
  428. Socket.getaddrinfo(&quot;localhost&quot;, nil)
  429. #=&gt; [[&quot;AF_INET&quot;, 0, &quot;localhost&quot;, &quot;127.0.0.1&quot;, 2, 1, 6], # PF_INET/SOCK_STREAM/IPPROTO_TCP
  430. # [&quot;AF_INET&quot;, 0, &quot;localhost&quot;, &quot;127.0.0.1&quot;, 2, 2, 17], # PF_INET/SOCK_DGRAM/IPPROTO_UDP
  431. # [&quot;AF_INET&quot;, 0, &quot;localhost&quot;, &quot;127.0.0.1&quot;, 2, 3, 0]] # PF_INET/SOCK_RAW/IPPROTO_IP
  432. </pre>
  433. <p>
  434. <em>reverse_lookup</em> directs the form of the third element, and has to
  435. be one of below. If it is ommitted, the default value is <tt>nil</tt>.
  436. </p>
  437. <pre class="code">
  438. +true+, +:hostname+: hostname is obtained from numeric address using reverse lookup, which may take a time.
  439. +false+, +:numeric+: hostname is same as numeric address.
  440. +nil+: obey to the current +do_not_reverse_lookup+ flag.
  441. </pre>
  442. <p>
  443. If Addrinfo object is preferred, use Addrinfo.getaddrinfo.
  444. </p>
  445. </div>
  446. </div>
  447. <div class="tags">
  448. <div class="docstring">
  449. <div class="discussion">
  450. </div>
  451. </div>
  452. <div class="tags">
  453. <h3>Returns:</h3>
  454. <ul class="return">
  455. <li>
  456. <span class='type'>(<tt><span class='object_link'><a href="Array.html" title="Array (class)">Array</a></span></tt>)</span>
  457. </li>
  458. </ul>
  459. </div>
  460. </div><table class="source_code">
  461. <tr>
  462. <td>
  463. <pre class="lines">
  464. </pre>
  465. </td>
  466. <td>
  467. <pre class="code"><span class="info file"># File 'ext/socket/socket.c'</span>
  468. /*
  469. * call-seq:
  470. * Socket.getaddrinfo(nodename, servname[, family[, socktype[, protocol[, flags[, reverse_lookup]]]]]) =&gt; array
  471. *
  472. * Obtains address information for _nodename_:_servname_.
  473. *
  474. * _family_ should be an address family such as: :INET, :INET6, :UNIX, etc.
  475. *
  476. * _socktype_ should be a socket type such as: :STREAM, :DGRAM, :RAW, etc.
  477. *
  478. * _protocol_ should be a protocol defined in the family.
  479. * 0 is default protocol for the family.
  480. *
  481. * _flags_ should be bitwise OR of Socket::AI_* constants.
  482. *
  483. * Socket.getaddrinfo(&quot;www.ruby-lang.org&quot;, &quot;http&quot;, nil, :STREAM)
  484. * #=&gt; [[&quot;AF_INET&quot;, 80, &quot;carbon.ruby-lang.org&quot;, &quot;221.186.184.68&quot;, 2, 1, 6]] # PF_INET/SOCK_STREAM/IPPROTO_TCP
  485. *
  486. * Socket.getaddrinfo(&quot;localhost&quot;, nil)
  487. * #=&gt; [[&quot;AF_INET&quot;, 0, &quot;localhost&quot;, &quot;127.0.0.1&quot;, 2, 1, 6], # PF_INET/SOCK_STREAM/IPPROTO_TCP
  488. * # [&quot;AF_INET&quot;, 0, &quot;localhost&quot;, &quot;127.0.0.1&quot;, 2, 2, 17], # PF_INET/SOCK_DGRAM/IPPROTO_UDP
  489. * # [&quot;AF_INET&quot;, 0, &quot;localhost&quot;, &quot;127.0.0.1&quot;, 2, 3, 0]] # PF_INET/SOCK_RAW/IPPROTO_IP
  490. *
  491. * _reverse_lookup_ directs the form of the third element, and has to
  492. * be one of below.
  493. * If it is ommitted, the default value is +nil+.
  494. *
  495. * +true+, +:hostname+: hostname is obtained from numeric address using reverse lookup, which may take a time.
  496. * +false+, +:numeric+: hostname is same as numeric address.
  497. * +nil+: obey to the current +do_not_reverse_lookup+ flag.
  498. *
  499. * If Addrinfo object is preferred, use Addrinfo.getaddrinfo.
  500. */
  501. static VALUE
  502. sock_s_getaddrinfo(int argc, VALUE *argv)
  503. {
  504. VALUE host, port, family, socktype, protocol, flags, ret, revlookup;
  505. struct addrinfo hints, *res;
  506. int norevlookup;
  507. rb_scan_args(argc, argv, &quot;25&quot;, &amp;host, &amp;port, &amp;family, &amp;socktype, &amp;protocol, &amp;flags, &amp;revlookup);
  508. MEMZERO(&amp;hints, struct addrinfo, 1);
  509. hints.ai_family = NIL_P(family) ? PF_UNSPEC : rsock_family_arg(family);
  510. if (!NIL_P(socktype)) {
  511. hints.ai_socktype = rsock_socktype_arg(socktype);
  512. }
  513. if (!NIL_P(protocol)) {
  514. hints.ai_protocol = NUM2INT(protocol);
  515. }
  516. if (!NIL_P(flags)) {
  517. hints.ai_flags = NUM2INT(flags);
  518. }
  519. if (NIL_P(revlookup) || !rsock_revlookup_flag(revlookup, &amp;norevlookup)) {
  520. norevlookup = rsock_do_not_reverse_lookup;
  521. }
  522. res = rsock_getaddrinfo(host, port, &amp;hints, 0);
  523. ret = make_addrinfo(res, norevlookup);
  524. freeaddrinfo(res);
  525. return ret;
  526. }</pre>
  527. </td>
  528. </tr>
  529. </table>
  530. </div>
  531. <div class="method_details ">
  532. <p class="signature " id="gethostbyaddr-class_method">
  533. + (<tt><span class='object_link'><a href="Object.html" title="Object (class)">Object</a></span></tt>) <strong>gethostbyaddr</strong>(address_string[, address_family])
  534. </p><div class="docstring">
  535. <div class="discussion">
  536. <p>
  537. Obtains the host information for <em>address</em>.
  538. </p>
  539. <pre class="code">
  540. p Socket.gethostbyaddr([221,186,184,68].pack(&quot;CCCC&quot;))
  541. #=&gt; [&quot;carbon.ruby-lang.org&quot;, [], 2, &quot;\xDD\xBA\xB8D&quot;]</pre>
  542. </div>
  543. </div>
  544. <div class="tags">
  545. </div><table class="source_code">
  546. <tr>
  547. <td>
  548. <pre class="lines">
  549. </pre>
  550. </td>
  551. <td>
  552. <pre class="code"><span class="info file"># File 'ext/socket/socket.c'</span>
  553. /*
  554. * call-seq:
  555. * Socket.gethostbyaddr(address_string [, address_family]) =&gt; hostent
  556. *
  557. * Obtains the host information for _address_.
  558. *
  559. * p Socket.gethostbyaddr([221,186,184,68].pack(&quot;CCCC&quot;))
  560. * #=&gt; [&quot;carbon.ruby-lang.org&quot;, [], 2, &quot;\xDD\xBA\xB8D&quot;]
  561. */
  562. static VALUE
  563. sock_s_gethostbyaddr(int argc, VALUE *argv)
  564. {
  565. VALUE addr, family;
  566. struct hostent *h;
  567. struct sockaddr *sa;
  568. char **pch;
  569. VALUE ary, names;
  570. int t = AF_INET;
  571. rb_scan_args(argc, argv, &quot;11&quot;, &amp;addr, &amp;family);
  572. sa = (struct sockaddr*)StringValuePtr(addr);
  573. if (!NIL_P(family)) {
  574. t = rsock_family_arg(family);
  575. }
  576. #ifdef AF_INET6
  577. else if (RSTRING_LEN(addr) == 16) {
  578. t = AF_INET6;
  579. }
  580. #endif
  581. h = gethostbyaddr(RSTRING_PTR(addr), RSTRING_LENINT(addr), t);
  582. if (h == NULL) {
  583. #ifdef HAVE_HSTRERROR
  584. extern int h_errno;
  585. rb_raise(rb_eSocket, &quot;%s&quot;, (char*)hstrerror(h_errno));
  586. #else
  587. rb_raise(rb_eSocket, &quot;host not found&quot;);
  588. #endif
  589. }
  590. ary = rb_ary_new();
  591. rb_ary_push(ary, rb_str_new2(h-&gt;h_name));
  592. names = rb_ary_new();
  593. rb_ary_push(ary, names);
  594. if (h-&gt;h_aliases != NULL) {
  595. for (pch = h-&gt;h_aliases; *pch; pch++) {
  596. rb_ary_push(names, rb_str_new2(*pch));
  597. }
  598. }
  599. rb_ary_push(ary, INT2NUM(h-&gt;h_addrtype));
  600. #ifdef h_addr
  601. for (pch = h-&gt;h_addr_list; *pch; pch++) {
  602. rb_ary_push(ary, rb_str_new(*pch, h-&gt;h_length));
  603. }
  604. #else
  605. rb_ary_push(ary, rb_str_new(h-&gt;h_addr, h-&gt;h_length));
  606. #endif
  607. return ary;
  608. }</pre>
  609. </td>
  610. </tr>
  611. </table>
  612. </div>
  613. <div class="method_details ">
  614. <p class="signature " id="gethostbyname-class_method">
  615. + (<tt><span class='object_link'><a href="Array.html" title="Array (class)">Array</a></span></tt>) <strong>gethostbyname</strong>(hostname)
  616. </p><div class="docstring">
  617. <div class="discussion">
  618. <p>
  619. Obtains the host information for <em>hostname</em>.
  620. </p>
  621. <pre class="code">
  622. p Socket.gethostbyname(&quot;hal&quot;) #=&gt; [&quot;localhost&quot;, [&quot;hal&quot;], 2, &quot;\x7F\x00\x00\x01&quot;]</pre>
  623. </div>
  624. </div>
  625. <div class="tags">
  626. <div class="docstring">
  627. <div class="discussion">
  628. </div>
  629. </div>
  630. <div class="tags">
  631. <h3>Returns:</h3>
  632. <ul class="return">
  633. <li>
  634. <span class='type'>(<tt><span class='object_link'><a href="Array.html" title="Array (class)">Array</a></span></tt>)</span>
  635. </li>
  636. </ul>
  637. </div>
  638. </div><table class="source_code">
  639. <tr>
  640. <td>
  641. <pre class="lines">
  642. </pre>
  643. </td>
  644. <td>
  645. <pre class="code"><span class="info file"># File 'ext/socket/socket.c'</span>
  646. /*
  647. * call-seq:
  648. * Socket.gethostbyname(hostname) =&gt; [official_hostname, alias_hostnames, address_family, *address_list]
  649. *
  650. * Obtains the host information for _hostname_.
  651. *
  652. * p Socket.gethostbyname(&quot;hal&quot;) #=&gt; [&quot;localhost&quot;, [&quot;hal&quot;], 2, &quot;\x7F\x00\x00\x01&quot;]
  653. *
  654. */
  655. static VALUE
  656. sock_s_gethostbyname(VALUE obj, VALUE host)
  657. {
  658. rb_secure(3);
  659. return rsock_make_hostent(host, rsock_addrinfo(host, Qnil, SOCK_STREAM, AI_CANONNAME), sock_sockaddr);
  660. }</pre>
  661. </td>
  662. </tr>
  663. </table>
  664. </div>
  665. <div class="method_details ">
  666. <p class="signature " id="gethostname-class_method">
  667. + (<tt><span class='object_link'><a href="Object.html" title="Object (class)">Object</a></span></tt>) <strong>gethostname</strong>
  668. </p><div class="docstring">
  669. <div class="discussion">
  670. <p>
  671. Returns the hostname.
  672. </p>
  673. <pre class="code">
  674. p Socket.gethostname #=&gt; &quot;hal&quot;
  675. </pre>
  676. <p>
  677. Note that it is not guaranteed to be able to convert to IP address using
  678. gethostbyname, getaddrinfo, etc. If you need local IP address, use
  679. Socket.ip_address_list.
  680. </p>
  681. </div>
  682. </div>
  683. <div class="tags">
  684. </div><table class="source_code">
  685. <tr>
  686. <td>
  687. <pre class="lines">
  688. </pre>
  689. </td>
  690. <td>
  691. <pre class="code"><span class="info file"># File 'ext/socket/socket.c'</span>
  692. /*
  693. * call-seq:
  694. * Socket.gethostname =&gt; hostname
  695. *
  696. * Returns the hostname.
  697. *
  698. * p Socket.gethostname #=&gt; &quot;hal&quot;
  699. *
  700. * Note that it is not guaranteed to be able to convert to IP address using gethostbyname, getaddrinfo, etc.
  701. * If you need local IP address, use Socket.ip_address_list.
  702. */
  703. static VALUE
  704. sock_gethostname(VALUE obj)
  705. {
  706. #ifndef HOST_NAME_MAX
  707. # define HOST_NAME_MAX 1024
  708. #endif
  709. char buf[HOST_NAME_MAX+1];
  710. rb_secure(3);
  711. if (gethostname(buf, (int)sizeof buf - 1) &lt; 0)
  712. rb_sys_fail(&quot;gethostname&quot;);
  713. buf[sizeof buf - 1] = '\0';
  714. return rb_str_new2(buf);
  715. }</pre>
  716. </td>
  717. </tr>
  718. </table>
  719. </div>
  720. <div class="method_details ">
  721. <p class="signature " id="getnameinfo-class_method">
  722. + (<tt><span class='object_link'><a href="Array.html" title="Array (class)">Array</a></span></tt>) <strong>getnameinfo</strong>(sockaddr[, flags])
  723. </p><div class="docstring">
  724. <div class="discussion">
  725. <p>
  726. Obtains name information for <em>sockaddr</em>.
  727. </p>
  728. <p>
  729. <em>sockaddr</em> should be one of follows.
  730. </p>
  731. <ul>
  732. <li><p>
  733. packed sockaddr string such as Socket.sockaddr_in(80,
  734. &#8220;127.0.0.1&#8221;)
  735. </p>
  736. </li>
  737. <li><p>
  738. 3-elements array such as [&#8220;AF_INET&#8221;, 80,
  739. &#8220;127.0.0.1&#8221;]
  740. </p>
  741. </li>
  742. <li><p>
  743. 4-elements array such as [&#8220;AF_INET&#8221;, 80, ignored,
  744. &#8220;127.0.0.1&#8221;]
  745. </p>
  746. </li>
  747. </ul>
  748. <p>
  749. <em>flags</em> should be bitwise OR of Socket::NI_* constants.
  750. </p>
  751. <p>
  752. Note that the last form is compatible with IPSocket#addr,peeraddr.
  753. </p>
  754. <pre class="code">
  755. Socket.getnameinfo(Socket.sockaddr_in(80, &quot;127.0.0.1&quot;)) #=&gt; [&quot;localhost&quot;, &quot;www&quot;]
  756. Socket.getnameinfo([&quot;AF_INET&quot;, 80, &quot;127.0.0.1&quot;]) #=&gt; [&quot;localhost&quot;, &quot;www&quot;]
  757. Socket.getnameinfo([&quot;AF_INET&quot;, 80, &quot;localhost&quot;, &quot;127.0.0.1&quot;]) #=&gt; [&quot;localhost&quot;, &quot;www&quot;]
  758. </pre>
  759. <p>
  760. If Addrinfo object is preferred, use Addrinfo#getnameinfo.
  761. </p>
  762. </div>
  763. </div>
  764. <div class="tags">
  765. <div class="docstring">
  766. <div class="discussion">
  767. </div>
  768. </div>
  769. <div class="tags">
  770. <h3>Returns:</h3>
  771. <ul class="return">
  772. <li>
  773. <span class='type'>(<tt><span class='object_link'><a href="Array.html" title="Array (class)">Array</a></span></tt>)</span>
  774. </li>
  775. </ul>
  776. </div>
  777. </div><table class="source_code">
  778. <tr>
  779. <td>
  780. <pre class="lines">
  781. </pre>
  782. </td>
  783. <td>
  784. <pre class="code"><span class="info file"># File 'ext/socket/socket.c'</span>
  785. /*
  786. * call-seq:
  787. * Socket.getnameinfo(sockaddr [, flags]) =&gt; [hostname, servicename]
  788. *
  789. * Obtains name information for _sockaddr_.
  790. *
  791. * _sockaddr_ should be one of follows.
  792. * - packed sockaddr string such as Socket.sockaddr_in(80, &quot;127.0.0.1&quot;)
  793. * - 3-elements array such as [&quot;AF_INET&quot;, 80, &quot;127.0.0.1&quot;]
  794. * - 4-elements array such as [&quot;AF_INET&quot;, 80, ignored, &quot;127.0.0.1&quot;]
  795. *
  796. * _flags_ should be bitwise OR of Socket::NI_* constants.
  797. *
  798. * Note that the last form is compatible with IPSocket#{addr,peeraddr}.
  799. *
  800. * Socket.getnameinfo(Socket.sockaddr_in(80, &quot;127.0.0.1&quot;)) #=&gt; [&quot;localhost&quot;, &quot;www&quot;]
  801. * Socket.getnameinfo([&quot;AF_INET&quot;, 80, &quot;127.0.0.1&quot;]) #=&gt; [&quot;localhost&quot;, &quot;www&quot;]
  802. * Socket.getnameinfo([&quot;AF_INET&quot;, 80, &quot;localhost&quot;, &quot;127.0.0.1&quot;]) #=&gt; [&quot;localhost&quot;, &quot;www&quot;]
  803. *
  804. * If Addrinfo object is preferred, use Addrinfo#getnameinfo.
  805. */
  806. static VALUE
  807. sock_s_getnameinfo(int argc, VALUE *argv)
  808. {
  809. VALUE sa, af = Qnil, host = Qnil, port = Qnil, flags, tmp;
  810. char *hptr, *pptr;
  811. char hbuf[1024], pbuf[1024];
  812. int fl;
  813. struct addrinfo hints, *res = NULL, *r;
  814. int error;
  815. struct sockaddr_storage ss;
  816. struct sockaddr *sap;
  817. sa = flags = Qnil;
  818. rb_scan_args(argc, argv, &quot;11&quot;, &amp;sa, &amp;flags);
  819. fl = 0;
  820. if (!NIL_P(flags)) {
  821. fl = NUM2INT(flags);
  822. }
  823. tmp = rb_check_sockaddr_string_type(sa);
  824. if (!NIL_P(tmp)) {
  825. sa = tmp;
  826. if (sizeof(ss) &lt; (size_t)RSTRING_LEN(sa)) {
  827. rb_raise(rb_eTypeError, &quot;sockaddr length too big&quot;);
  828. }
  829. memcpy(&amp;ss, RSTRING_PTR(sa), RSTRING_LEN(sa));
  830. if ((size_t)RSTRING_LEN(sa) != SS_LEN(&amp;ss)) {
  831. rb_raise(rb_eTypeError, &quot;sockaddr size differs - should not happen&quot;);
  832. }
  833. sap = (struct sockaddr*)&amp;ss;
  834. goto call_nameinfo;
  835. }
  836. tmp = rb_check_array_type(sa);
  837. if (!NIL_P(tmp)) {
  838. sa = tmp;
  839. MEMZERO(&amp;hints, struct addrinfo, 1);
  840. if (RARRAY_LEN(sa) == 3) {
  841. af = RARRAY_PTR(sa)[0];
  842. port = RARRAY_PTR(sa)[1];
  843. host = RARRAY_PTR(sa)[2];
  844. }
  845. else if (RARRAY_LEN(sa) &gt;= 4) {
  846. af = RARRAY_PTR(sa)[0];
  847. port = RARRAY_PTR(sa)[1];
  848. host = RARRAY_PTR(sa)[3];
  849. if (NIL_P(host)) {
  850. host = RARRAY_PTR(sa)[2];
  851. }
  852. else {
  853. /*
  854. * 4th element holds numeric form, don't resolve.
  855. * see rsock_ipaddr().
  856. */
  857. #ifdef AI_NUMERICHOST /* AIX 4.3.3 doesn't have AI_NUMERICHOST. */
  858. hints.ai_flags |= AI_NUMERICHOST;
  859. #endif
  860. }
  861. }
  862. else {
  863. rb_raise(rb_eArgError, &quot;array size should be 3 or 4, %ld given&quot;,
  864. RARRAY_LEN(sa));
  865. }
  866. /* host */
  867. if (NIL_P(host)) {
  868. hptr = NULL;
  869. }
  870. else {
  871. strncpy(hbuf, StringValuePtr(host), sizeof(hbuf));
  872. hbuf[sizeof(hbuf) - 1] = '\0';
  873. hptr = hbuf;
  874. }
  875. /* port */
  876. if (NIL_P(port)) {
  877. strcpy(pbuf, &quot;0&quot;);
  878. pptr = NULL;
  879. }
  880. else if (FIXNUM_P(port)) {
  881. snprintf(pbuf, sizeof(pbuf), &quot;%ld&quot;, NUM2LONG(port));
  882. pptr = pbuf;
  883. }
  884. else {
  885. strncpy(pbuf, StringValuePtr(port), sizeof(pbuf));
  886. pbuf[sizeof(pbuf) - 1] = '\0';
  887. pptr = pbuf;
  888. }
  889. hints.ai_socktype = (fl &amp; NI_DGRAM) ? SOCK_DGRAM : SOCK_STREAM;
  890. /* af */
  891. hints.ai_family = NIL_P(af) ? PF_UNSPEC : rsock_family_arg(af);
  892. error = rb_getaddrinfo(hptr, pptr, &amp;hints, &amp;res);
  893. if (error) goto error_exit_addr;
  894. sap = res-&gt;ai_addr;
  895. }
  896. else {
  897. rb_raise(rb_eTypeError, &quot;expecting String or Array&quot;);
  898. }
  899. call_nameinfo:
  900. error = rb_getnameinfo(sap, SA_LEN(sap), hbuf, sizeof(hbuf),
  901. pbuf, sizeof(pbuf), fl);
  902. if (error) goto error_exit_name;
  903. if (res) {
  904. for (r = res-&gt;ai_next; r; r = r-&gt;ai_next) {
  905. char hbuf2[1024], pbuf2[1024];
  906. sap = r-&gt;ai_addr;
  907. error = rb_getnameinfo(sap, SA_LEN(sap), hbuf2, sizeof(hbuf2),
  908. pbuf2, sizeof(pbuf2), fl);
  909. if (error) goto error_exit_name;
  910. if (strcmp(hbuf, hbuf2) != 0|| strcmp(pbuf, pbuf2) != 0) {
  911. freeaddrinfo(res);
  912. rb_raise(rb_eSocket, &quot;sockaddr resolved to multiple nodename&quot;);
  913. }
  914. }
  915. freeaddrinfo(res);
  916. }
  917. return rb_assoc_new(rb_str_new2(hbuf), rb_str_new2(pbuf));
  918. error_exit_addr:
  919. if (res) freeaddrinfo(res);
  920. rsock_raise_socket_error(&quot;getaddrinfo&quot;, error);
  921. error_exit_name:
  922. if (res) freeaddrinfo(res);
  923. rsock_raise_socket_error(&quot;getnameinfo&quot;, error);
  924. }</pre>
  925. </td>
  926. </tr>
  927. </table>
  928. </div>
  929. <div class="method_details ">
  930. <p class="signature " id="getservbyname-class_method">
  931. <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>
  932. <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>
  933. </p><div class="docstring">
  934. <div class="discussion">
  935. <p>
  936. Obtains the port number for <em>service_name</em>.
  937. </p>
  938. <p>
  939. If <em>protocol_name</em> is not given, &#8220;tcp&#8221; is assumed.
  940. </p>
  941. <pre class="code">
  942. Socket.getservbyname(&quot;smtp&quot;) #=&gt; 25
  943. Socket.getservbyname(&quot;shell&quot;) #=&gt; 514
  944. Socket.getservbyname(&quot;syslog&quot;, &quot;udp&quot;) #=&gt; 514</pre>
  945. </div>
  946. </div>
  947. <div class="tags">
  948. </div><table class="source_code">
  949. <tr>
  950. <td>
  951. <pre class="lines">
  952. </pre>
  953. </td>
  954. <td>
  955. <pre class="code"><span class="info file"># File 'ext/socket/socket.c'</span>
  956. /*
  957. * call-seq:
  958. * Socket.getservbyname(service_name) =&gt; port_number
  959. * Socket.getservbyname(service_name, protocol_name) =&gt; port_number
  960. *
  961. * Obtains the port number for _service_name_.
  962. *
  963. * If _protocol_name_ is not given, &quot;tcp&quot; is assumed.
  964. *
  965. * Socket.getservbyname(&quot;smtp&quot;) #=&gt; 25
  966. * Socket.getservbyname(&quot;shell&quot;) #=&gt; 514
  967. * Socket.getservbyname(&quot;syslog&quot;, &quot;udp&quot;) #=&gt; 514
  968. */
  969. static VALUE
  970. sock_s_getservbyname(int argc, VALUE *argv)
  971. {
  972. VALUE service, proto;
  973. struct servent *sp;
  974. long port;
  975. const char *servicename, *protoname = &quot;tcp&quot;;
  976. rb_scan_args(argc, argv, &quot;11&quot;, &amp;service, &amp;proto);
  977. StringValue(service);
  978. if (!NIL_P(proto)) StringValue(proto);
  979. servicename = StringValueCStr(service);
  980. if (!NIL_P(proto)) protoname = StringValueCStr(proto);
  981. sp = getservbyname(servicename, protoname);
  982. if (sp) {
  983. port = ntohs(sp-&gt;s_port);
  984. }
  985. else {
  986. char *end;
  987. port = STRTOUL(servicename, &amp;end, 0);
  988. if (*end != '\0') {
  989. rb_raise(rb_eSocket, &quot;no such service %s/%s&quot;, servicename, protoname);
  990. }
  991. }
  992. return INT2FIX(port);
  993. }</pre>
  994. </td>
  995. </tr>
  996. </table>
  997. </div>
  998. <div class="method_details ">
  999. <p class="signature " id="getservbyport-class_method">
  1000. + (<tt><span class='object_link'><a href="Object.html" title="Object (class)">Object</a></span></tt>) <strong>getservbyport</strong>(port[, protocol_name])
  1001. </p><div class="docstring">
  1002. <div class="discussion">
  1003. <p>
  1004. Obtains the port number for <em>port</em>.
  1005. </p>
  1006. <p>
  1007. If <em>protocol_name</em> is not given, &#8220;tcp&#8221; is assumed.
  1008. </p>
  1009. <pre class="code">
  1010. Socket.getservbyport(80) #=&gt; &quot;www&quot;
  1011. Socket.getservbyport(514, &quot;tcp&quot;) #=&gt; &quot;shell&quot;
  1012. Socket.getservbyport(514, &quot;udp&quot;) #=&gt; &quot;syslog&quot;</pre>
  1013. </div>
  1014. </div>
  1015. <div class="tags">
  1016. </div><table class="source_code">
  1017. <tr>
  1018. <td>
  1019. <pre class="lines">
  1020. </pre>
  1021. </td>
  1022. <td>
  1023. <pre class="code"><span class="info file"># File 'ext/socket/socket.c'</span>
  1024. /*
  1025. * call-seq:
  1026. * Socket.getservbyport(port [, protocol_name]) =&gt; service
  1027. *
  1028. * Obtains the port number for _port_.
  1029. *
  1030. * If _protocol_name_ is not given, &quot;tcp&quot; is assumed.
  1031. *
  1032. * Socket.getservbyport(80) #=&gt; &quot;www&quot;
  1033. * Socket.getservbyport(514, &quot;tcp&quot;) #=&gt; &quot;shell&quot;
  1034. * Socket.getservbyport(514, &quot;udp&quot;) #=&gt; &quot;syslog&quot;
  1035. *
  1036. */
  1037. static VALUE
  1038. sock_s_getservbyport(int argc, VALUE *argv)
  1039. {
  1040. VALUE port, proto;
  1041. struct servent *sp;
  1042. long portnum;
  1043. const char *protoname = &quot;tcp&quot;;
  1044. rb_scan_args(argc, argv, &quot;11&quot;, &amp;port, &amp;proto);
  1045. portnum = NUM2LONG(port);
  1046. if (portnum != (uint16_t)portnum) {
  1047. const char *s = portnum &gt; 0 ? &quot;big&quot; : &quot;small&quot;;
  1048. rb_raise(rb_eRangeError, &quot;integer %ld too %s to convert into `int16_t'&quot;, portnum, s);
  1049. }
  1050. if (!NIL_P(proto)) protoname = StringValueCStr(proto);
  1051. sp = getservbyport((int)htons((uint16_t)portnum), protoname);
  1052. if (!sp) {
  1053. rb_raise(rb_eSocket, &quot;no such service for port %d/%s&quot;, (int)portnum, protoname);
  1054. }
  1055. return rb_tainted_str_new2(sp-&gt;s_name);
  1056. }</pre>
  1057. </td>
  1058. </tr>
  1059. </table>
  1060. </div>
  1061. <div class="method_details ">
  1062. <p class="signature " id="ip_address_list-class_method">
  1063. + (<tt><span class='object_link'><a href="Array.html" title="Array (class)">Array</a></span></tt>) <strong>ip_address_list</strong>
  1064. </p><div class="docstring">
  1065. <div class="discussion">
  1066. <p>
  1067. Returns local IP addresses as an array.
  1068. </p>
  1069. <p>
  1070. The array contains Addrinfo objects.
  1071. </p>
  1072. <pre class="code">
  1073. pp Socket.ip_address_list
  1074. #=&gt; [#&lt;Addrinfo: 127.0.0.1&gt;,
  1075. #&lt;Addrinfo: 192.168.0.128&gt;,
  1076. #&lt;Addrinfo: ::1&gt;,
  1077. ...]</pre>
  1078. </div>
  1079. </div>
  1080. <div class="tags">
  1081. <div class="docstring">
  1082. <div class="discussion">
  1083. </div>
  1084. </div>
  1085. <div class="tags">
  1086. <h3>Returns:</h3>
  1087. <ul class="return">
  1088. <li>
  1089. <span class='type'>(<tt><span class='object_link'><a href="Array.html" title="Array (class)">Array</a></span></tt>)</span>
  1090. </li>
  1091. </ul>
  1092. </div>
  1093. </div><table class="source_code">
  1094. <tr>
  1095. <td>
  1096. <pre class="lines">
  1097. </pre>
  1098. </td>
  1099. <td>
  1100. <pre class="code"><span class="info file"># File 'ext/socket/socket.c'</span>
  1101. /*
  1102. * call-seq:
  1103. * Socket.ip_address_list =&gt; array
  1104. *
  1105. * Returns local IP addresses as an array.
  1106. *
  1107. * The array contains Addrinfo objects.
  1108. *
  1109. * pp Socket.ip_address_list
  1110. * #=&gt; [#&lt;Addrinfo: 127.0.0.1&gt;,
  1111. * #&lt;Addrinfo: 192.168.0.128&gt;,
  1112. * #&lt;Addrinfo: ::1&gt;,
  1113. * ...]
  1114. *
  1115. */
  1116. static VALUE
  1117. socket_s_ip_address_list(VALUE self)
  1118. {
  1119. #if defined(HAVE_GETIFADDRS)
  1120. struct ifaddrs *ifp = NULL;
  1121. struct ifaddrs *p;
  1122. int ret;
  1123. VALUE list;
  1124. ret = getifaddrs(&amp;ifp);
  1125. if (ret == -1) {
  1126. rb_sys_fail(&quot;getifaddrs&quot;);
  1127. }
  1128. list = rb_ary_new();
  1129. for (p = ifp; p; p = p-&gt;ifa_next) {
  1130. if (p-&gt;ifa_addr != NULL &amp;&amp; IS_IP_FAMILY(p-&gt;ifa_addr-&gt;sa_family)) {
  1131. rb_ary_push(list, sockaddr_obj(p-&gt;ifa_addr));
  1132. }
  1133. }
  1134. freeifaddrs(ifp);
  1135. return list;
  1136. #elif defined(SIOCGLIFCONF) &amp;&amp; defined(SIOCGLIFNUM) &amp;&amp; !defined(__hpux)
  1137. /* Solaris if_tcp(7P) */
  1138. /* HP-UX has SIOCGLIFCONF too. But it uses different struct */
  1139. int fd = -1;
  1140. int ret;
  1141. struct lifnum ln;
  1142. struct lifconf lc;
  1143. char *reason = NULL;
  1144. int save_errno;
  1145. int i;
  1146. VALUE list = Qnil;
  1147. lc.lifc_buf = NULL;
  1148. fd = socket(AF_INET, SOCK_DGRAM, 0);
  1149. if (fd == -1)
  1150. rb_sys_fail(&quot;socket&quot;);
  1151. memset(&amp;ln, 0, sizeof(ln));
  1152. ln.lifn_family = AF_UNSPEC;
  1153. ret = ioctl(fd, SIOCGLIFNUM, &amp;ln);
  1154. if (ret == -1) {
  1155. reason = &quot;SIOCGLIFNUM&quot;;
  1156. goto finish;
  1157. }
  1158. memset(&amp;lc, 0, sizeof(lc));
  1159. lc.lifc_family = AF_UNSPEC;
  1160. lc.lifc_flags = 0;
  1161. lc.lifc_len = sizeof(struct lifreq) * ln.lifn_count;
  1162. lc.lifc_req = xmalloc(lc.lifc_len);
  1163. ret = ioctl(fd, SIOCGLIFCONF, &amp;lc);
  1164. if (ret == -1) {
  1165. reason = &quot;SIOCGLIFCONF&quot;;
  1166. goto finish;
  1167. }
  1168. list = rb_ary_new();
  1169. for (i = 0; i &lt; ln.lifn_count; i++) {
  1170. struct lifreq *req = &amp;lc.lifc_req[i];
  1171. if (IS_IP_FAMILY(req-&gt;lifr_addr.ss_family)) {
  1172. if (req-&gt;lifr_addr.ss_family == AF_INET6 &amp;&amp;
  1173. IN6_IS_ADDR_LINKLOCAL(&amp;((struct sockaddr_in6 *)(&amp;req-&gt;lifr_addr))-&gt;sin6_addr) &amp;&amp;
  1174. ((struct sockaddr_in6 *)(&amp;req-&gt;lifr_addr))-&gt;sin6_scope_id == 0) {
  1175. struct lifreq req2;
  1176. memcpy(req2.lifr_name, req-&gt;lifr_name, LIFNAMSIZ);
  1177. ret = ioctl(fd, SIOCGLIFINDEX, &amp;req2);
  1178. if (ret == -1) {
  1179. reason = &quot;SIOCGLIFINDEX&quot;;
  1180. goto finish;
  1181. }
  1182. ((struct sockaddr_in6 *)(&amp;req-&gt;lifr_addr))-&gt;sin6_scope_id = req2.lifr_index;
  1183. }
  1184. rb_ary_push(list, sockaddr_obj((struct sockaddr *)&amp;req-&gt;lifr_addr));
  1185. }
  1186. }
  1187. finish:
  1188. save_errno = errno;
  1189. if (lc.lifc_buf != NULL)
  1190. xfree(lc.lifc_req);
  1191. if (fd != -1)
  1192. close(fd);
  1193. errno = save_errno;
  1194. if (reason)
  1195. rb_sys_fail(reason);
  1196. return list;
  1197. #elif defined(SIOCGIFCONF)
  1198. int fd = -1;
  1199. int ret;
  1200. #define EXTRA_SPACE (sizeof(struct ifconf) + sizeof(struct sockaddr_storage))
  1201. char initbuf[4096+EXTRA_SPACE];
  1202. char *buf = initbuf;
  1203. int bufsize;
  1204. struct ifconf conf;
  1205. struct ifreq *req;
  1206. VALUE list = Qnil;
  1207. const char *reason = NULL;
  1208. int save_errno;
  1209. fd = socket(AF_INET, SOCK_DGRAM, 0);
  1210. if (fd == -1)
  1211. rb_sys_fail(&quot;socket&quot;);
  1212. bufsize = sizeof(initbuf);
  1213. buf = initbuf;
  1214. retry:
  1215. conf.ifc_len = bufsize;
  1216. conf.ifc_req = (struct ifreq *)buf;
  1217. /* fprintf(stderr, &quot;bufsize: %d\n&quot;, bufsize); */
  1218. ret = ioctl(fd, SIOCGIFCONF, &amp;conf);
  1219. if (ret == -1) {
  1220. reason = &quot;SIOCGIFCONF&quot;;
  1221. goto finish;
  1222. }
  1223. /* fprintf(stderr, &quot;conf.ifc_len: %d\n&quot;, conf.ifc_len); */
  1224. if (bufsize - EXTRA_SPACE &lt; conf.ifc_len) {
  1225. if (bufsize &lt; conf.ifc_len) {
  1226. /* NetBSD returns required size for all interfaces. */
  1227. bufsize = conf.ifc_len + EXTRA_SPACE;
  1228. }
  1229. else {
  1230. bufsize = bufsize &lt;&lt; 1;
  1231. }
  1232. if (buf == initbuf)
  1233. buf = NULL;
  1234. buf = xrealloc(buf, bufsize);
  1235. goto retry;
  1236. }
  1237. close(fd);
  1238. fd = -1;
  1239. list = rb_ary_new();
  1240. req = conf.ifc_req;
  1241. while ((char*)req &lt; (char*)conf.ifc_req + conf.ifc_len) {
  1242. struct sockaddr *addr = &amp;req-&gt;ifr_addr;
  1243. if (IS_IP_FAMILY(addr-&gt;sa_family)) {
  1244. rb_ary_push(list, sockaddr_obj(addr));
  1245. }
  1246. #ifdef HAVE_SA_LEN
  1247. # ifndef _SIZEOF_ADDR_IFREQ
  1248. # define _SIZEOF_ADDR_IFREQ(r) \
  1249. (sizeof(struct ifreq) + \
  1250. (sizeof(struct sockaddr) &lt; (r).ifr_addr.sa_len ? \
  1251. (r).ifr_addr.sa_len - sizeof(struct sockaddr) : \
  1252. 0))
  1253. # endif
  1254. req = (struct ifreq *)((char*)req + _SIZEOF_ADDR_IFREQ(*req));
  1255. #else
  1256. req = (struct ifreq *)((char*)req + sizeof(struct ifreq));
  1257. #endif
  1258. }
  1259. finish:
  1260. save_errno = errno;
  1261. if (buf != initbuf)
  1262. xfree(buf);
  1263. if (fd != -1)
  1264. close(fd);
  1265. errno = save_errno;
  1266. if (reason)
  1267. rb_sys_fail(reason);
  1268. return list;
  1269. #undef EXTRA_SPACE
  1270. #elif defined(_WIN32)
  1271. typedef struct ip_adapter_unicast_address_st {
  1272. unsigned LONG_LONG dummy0;
  1273. struct ip_adapter_unicast_address_st *Next;
  1274. struct {
  1275. struct sockaddr *lpSockaddr;
  1276. int iSockaddrLength;
  1277. } Address;
  1278. int dummy1;
  1279. int dummy2;
  1280. int dummy3;
  1281. long dummy4;
  1282. long dummy5;
  1283. long dummy6;
  1284. } ip_adapter_unicast_address_t;
  1285. typedef struct ip_adapter_anycast_address_st {
  1286. unsigned LONG_LONG dummy0;
  1287. struct ip_adapter_anycast_address_st *Next;
  1288. struct {
  1289. struct sockaddr *lpSockaddr;
  1290. int iSockaddrLength;
  1291. } Address;
  1292. } ip_adapter_anycast_address_t;
  1293. typedef struct ip_adapter_addresses_st {
  1294. unsigned LONG_LONG dummy0;
  1295. struct ip_adapter_addresses_st *Next;
  1296. void *dummy1;
  1297. ip_adapter_unicast_address_t *FirstUnicastAddress;

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