Ruby  2.4.2p198(2017-09-14revision59899)
raddrinfo.c
Go to the documentation of this file.
1 /************************************************
2 
3  raddrinfo.c -
4 
5  created at: Thu Mar 31 12:21:29 JST 1994
6 
7  Copyright (C) 1993-2007 Yukihiro Matsumoto
8 
9 ************************************************/
10 
11 #include "rubysocket.h"
12 
13 #if defined(INET6) && (defined(LOOKUP_ORDER_HACK_INET) || defined(LOOKUP_ORDER_HACK_INET6))
14 #define LOOKUP_ORDERS (sizeof(lookup_order_table) / sizeof(lookup_order_table[0]))
15 static const int lookup_order_table[] = {
16 #if defined(LOOKUP_ORDER_HACK_INET)
17  PF_INET, PF_INET6, PF_UNSPEC,
18 #elif defined(LOOKUP_ORDER_HACK_INET6)
19  PF_INET6, PF_INET, PF_UNSPEC,
20 #else
21  /* should not happen */
22 #endif
23 };
24 
25 static int
26 ruby_getaddrinfo(const char *nodename, const char *servname,
27  const struct addrinfo *hints, struct addrinfo **res)
28 {
29  struct addrinfo tmp_hints;
30  int i, af, error;
31 
32  if (hints->ai_family != PF_UNSPEC) {
33  return getaddrinfo(nodename, servname, hints, res);
34  }
35 
36  for (i = 0; i < LOOKUP_ORDERS; i++) {
37  af = lookup_order_table[i];
38  MEMCPY(&tmp_hints, hints, struct addrinfo, 1);
39  tmp_hints.ai_family = af;
40  error = getaddrinfo(nodename, servname, &tmp_hints, res);
41  if (error) {
42  if (tmp_hints.ai_family == PF_UNSPEC) {
43  break;
44  }
45  }
46  else {
47  break;
48  }
49  }
50 
51  return error;
52 }
53 #define getaddrinfo(node,serv,hints,res) ruby_getaddrinfo((node),(serv),(hints),(res))
54 #endif
55 
56 #if defined(_AIX)
57 static int
58 ruby_getaddrinfo__aix(const char *nodename, const char *servname,
59  const struct addrinfo *hints, struct addrinfo **res)
60 {
61  int error = getaddrinfo(nodename, servname, hints, res);
62  struct addrinfo *r;
63  if (error)
64  return error;
65  for (r = *res; r != NULL; r = r->ai_next) {
66  if (r->ai_addr->sa_family == 0)
67  r->ai_addr->sa_family = r->ai_family;
68  if (r->ai_addr->sa_len == 0)
69  r->ai_addr->sa_len = r->ai_addrlen;
70  }
71  return 0;
72 }
73 #undef getaddrinfo
74 #define getaddrinfo(node,serv,hints,res) ruby_getaddrinfo__aix((node),(serv),(hints),(res))
75 static int
76 ruby_getnameinfo__aix(const struct sockaddr *sa, size_t salen,
77  char *host, size_t hostlen,
78  char *serv, size_t servlen, int flags)
79 {
80  struct sockaddr_in6 *sa6;
81  u_int32_t *a6;
82 
83  if (sa->sa_family == AF_INET6) {
84  sa6 = (struct sockaddr_in6 *)sa;
85  a6 = sa6->sin6_addr.u6_addr.u6_addr32;
86 
87  if (a6[0] == 0 && a6[1] == 0 && a6[2] == 0 && a6[3] == 0) {
88  strncpy(host, "::", hostlen);
89  snprintf(serv, servlen, "%d", sa6->sin6_port);
90  return 0;
91  }
92  }
93  return getnameinfo(sa, salen, host, hostlen, serv, servlen, flags);
94 }
95 #undef getnameinfo
96 #define getnameinfo(sa, salen, host, hostlen, serv, servlen, flags) \
97  ruby_getnameinfo__aix((sa), (salen), (host), (hostlen), (serv), (servlen), (flags))
98 #endif
99 
100 static int str_is_number(const char *);
101 
102 #if defined(__APPLE__)
103 static int
104 ruby_getaddrinfo__darwin(const char *nodename, const char *servname,
105  const struct addrinfo *hints, struct addrinfo **res)
106 {
107  /* fix [ruby-core:29427] */
108  const char *tmp_servname;
109  struct addrinfo tmp_hints;
110  int error;
111 
112  tmp_servname = servname;
113  MEMCPY(&tmp_hints, hints, struct addrinfo, 1);
114  if (nodename && servname) {
115  if (str_is_number(tmp_servname) && atoi(servname) == 0) {
116  tmp_servname = NULL;
117 #ifdef AI_NUMERICSERV
118  if (tmp_hints.ai_flags) tmp_hints.ai_flags &= ~AI_NUMERICSERV;
119 #endif
120  }
121  }
122 
123  error = getaddrinfo(nodename, tmp_servname, &tmp_hints, res);
124  if (error == 0) {
125  /* [ruby-dev:23164] */
126  struct addrinfo *r;
127  r = *res;
128  while (r) {
129  if (! r->ai_socktype) r->ai_socktype = hints->ai_socktype;
130  if (! r->ai_protocol) {
131  if (r->ai_socktype == SOCK_DGRAM) {
133  }
134  else if (r->ai_socktype == SOCK_STREAM) {
136  }
137  }
138  r = r->ai_next;
139  }
140  }
141 
142  return error;
143 }
144 #undef getaddrinfo
145 #define getaddrinfo(node,serv,hints,res) ruby_getaddrinfo__darwin((node),(serv),(hints),(res))
146 #endif
147 
148 #ifndef GETADDRINFO_EMU
150 {
151  const char *node;
152  const char *service;
153  const struct addrinfo *hints;
154  struct addrinfo **res;
155 };
156 
157 #ifdef HAVE_INET_PTON
158 static int
159 parse_numeric_port(const char *service, int *portp)
160 {
161  unsigned long u;
162 
163  if (!service) {
164  *portp = 0;
165  return 1;
166  }
167 
168  if (strspn(service, "0123456789") != strlen(service))
169  return 0;
170 
171  errno = 0;
172  u = STRTOUL(service, NULL, 10);
173  if (errno)
174  return 0;
175 
176  if (0x10000 <= u)
177  return 0;
178 
179  *portp = (int)u;
180 
181  return 1;
182 }
183 #endif
184 
185 static void *
187 {
188  int ret;
189  struct getaddrinfo_arg *ptr = arg;
190  ret = getaddrinfo(ptr->node, ptr->service, ptr->hints, ptr->res);
191 #ifdef __linux__
192  /* On Linux (mainly Ubuntu 13.04) /etc/nsswitch.conf has mdns4 and
193  * it cause getaddrinfo to return EAI_SYSTEM/ENOENT. [ruby-list:49420]
194  */
195  if (ret == EAI_SYSTEM && errno == ENOENT)
196  ret = EAI_NONAME;
197 #endif
198  return (void *)(VALUE)ret;
199 }
200 #endif
201 
202 static int
203 numeric_getaddrinfo(const char *node, const char *service,
204  const struct addrinfo *hints,
205  struct addrinfo **res)
206 {
207 #ifdef HAVE_INET_PTON
208 # if defined __MINGW64__
209 # define inet_pton(f,s,d) rb_w32_inet_pton(f,s,d)
210 # endif
211 
212  int port;
213 
214  if (node && parse_numeric_port(service, &port)) {
215  static const struct {
216  int socktype;
217  int protocol;
218  } list[] = {
219  { SOCK_STREAM, IPPROTO_TCP },
220  { SOCK_DGRAM, IPPROTO_UDP },
221  { SOCK_RAW, 0 }
222  };
223  struct addrinfo *ai = NULL;
224  int hint_family = hints ? hints->ai_family : PF_UNSPEC;
225  int hint_socktype = hints ? hints->ai_socktype : 0;
226  int hint_protocol = hints ? hints->ai_protocol : 0;
227  char ipv4addr[4];
228 #ifdef AF_INET6
229  char ipv6addr[16];
230  if ((hint_family == PF_UNSPEC || hint_family == PF_INET6) &&
231  strspn(node, "0123456789abcdefABCDEF.:") == strlen(node) &&
232  inet_pton(AF_INET6, node, ipv6addr)) {
233  int i;
234  for (i = numberof(list)-1; 0 <= i; i--) {
235  if ((hint_socktype == 0 || hint_socktype == list[i].socktype) &&
236  (hint_protocol == 0 || list[i].protocol == 0 || hint_protocol == list[i].protocol)) {
237  struct addrinfo *ai0 = xcalloc(1, sizeof(struct addrinfo));
238  struct sockaddr_in6 *sa = xmalloc(sizeof(struct sockaddr_in6));
239  INIT_SOCKADDR_IN6(sa, sizeof(struct sockaddr_in6));
240  memcpy(&sa->sin6_addr, ipv6addr, sizeof(ipv6addr));
241  sa->sin6_port = htons(port);
242  ai0->ai_family = PF_INET6;
243  ai0->ai_socktype = list[i].socktype;
244  ai0->ai_protocol = hint_protocol ? hint_protocol : list[i].protocol;
245  ai0->ai_addrlen = sizeof(struct sockaddr_in6);
246  ai0->ai_addr = (struct sockaddr *)sa;
247  ai0->ai_canonname = NULL;
248  ai0->ai_next = ai;
249  ai = ai0;
250  }
251  }
252  }
253  else
254 #endif
255  if ((hint_family == PF_UNSPEC || hint_family == PF_INET) &&
256  strspn(node, "0123456789.") == strlen(node) &&
257  inet_pton(AF_INET, node, ipv4addr)) {
258  int i;
259  for (i = numberof(list)-1; 0 <= i; i--) {
260  if ((hint_socktype == 0 || hint_socktype == list[i].socktype) &&
261  (hint_protocol == 0 || list[i].protocol == 0 || hint_protocol == list[i].protocol)) {
262  struct addrinfo *ai0 = xcalloc(1, sizeof(struct addrinfo));
263  struct sockaddr_in *sa = xmalloc(sizeof(struct sockaddr_in));
264  INIT_SOCKADDR_IN(sa, sizeof(struct sockaddr_in));
265  memcpy(&sa->sin_addr, ipv4addr, sizeof(ipv4addr));
266  sa->sin_port = htons(port);
267  ai0->ai_family = PF_INET;
268  ai0->ai_socktype = list[i].socktype;
269  ai0->ai_protocol = hint_protocol ? hint_protocol : list[i].protocol;
270  ai0->ai_addrlen = sizeof(struct sockaddr_in);
271  ai0->ai_addr = (struct sockaddr *)sa;
272  ai0->ai_canonname = NULL;
273  ai0->ai_next = ai;
274  ai = ai0;
275  }
276  }
277  }
278  if (ai) {
279  *res = ai;
280  return 0;
281  }
282  }
283 #endif
284  return EAI_FAIL;
285 }
286 
287 int
288 rb_getaddrinfo(const char *node, const char *service,
289  const struct addrinfo *hints,
290  struct rb_addrinfo **res)
291 {
292  struct addrinfo *ai;
293  int ret;
294  int allocated_by_malloc = 0;
295 
296  ret = numeric_getaddrinfo(node, service, hints, &ai);
297  if (ret == 0)
298  allocated_by_malloc = 1;
299  else {
300 #ifdef GETADDRINFO_EMU
301  ret = getaddrinfo(node, service, hints, &ai);
302 #else
303  struct getaddrinfo_arg arg;
304  MEMZERO(&arg, struct getaddrinfo_arg, 1);
305  arg.node = node;
306  arg.service = service;
307  arg.hints = hints;
308  arg.res = &ai;
310 #endif
311  }
312 
313  if (ret == 0) {
314  *res = (struct rb_addrinfo *)xmalloc(sizeof(struct rb_addrinfo));
315  (*res)->allocated_by_malloc = allocated_by_malloc;
316  (*res)->ai = ai;
317  }
318  return ret;
319 }
320 
321 void
323 {
324  if (!ai->allocated_by_malloc)
325  freeaddrinfo(ai->ai);
326  else {
327  struct addrinfo *ai1, *ai2;
328  ai1 = ai->ai;
329  while (ai1) {
330  ai2 = ai1->ai_next;
331  xfree(ai1->ai_addr);
332  xfree(ai1);
333  ai1 = ai2;
334  }
335  }
336  xfree(ai);
337 }
338 
339 #ifndef GETADDRINFO_EMU
341 {
342  const struct sockaddr *sa;
344  int flags;
345  char *host;
346  size_t hostlen;
347  char *serv;
348  size_t servlen;
349 };
350 
351 static void *
353 {
354  struct getnameinfo_arg *ptr = arg;
355  return (void *)(VALUE)getnameinfo(ptr->sa, ptr->salen,
356  ptr->host, (socklen_t)ptr->hostlen,
357  ptr->serv, (socklen_t)ptr->servlen,
358  ptr->flags);
359 }
360 #endif
361 
362 int
363 rb_getnameinfo(const struct sockaddr *sa, socklen_t salen,
364  char *host, size_t hostlen,
365  char *serv, size_t servlen, int flags)
366 {
367 #ifdef GETADDRINFO_EMU
368  return getnameinfo(sa, salen, host, hostlen, serv, servlen, flags);
369 #else
370  struct getnameinfo_arg arg;
371  int ret;
372  arg.sa = sa;
373  arg.salen = salen;
374  arg.host = host;
375  arg.hostlen = hostlen;
376  arg.serv = serv;
377  arg.servlen = servlen;
378  arg.flags = flags;
380  return ret;
381 #endif
382 }
383 
384 static void
385 make_ipaddr0(struct sockaddr *addr, socklen_t addrlen, char *buf, size_t buflen)
386 {
387  int error;
388 
389  error = rb_getnameinfo(addr, addrlen, buf, buflen, NULL, 0, NI_NUMERICHOST);
390  if (error) {
391  rsock_raise_socket_error("getnameinfo", error);
392  }
393 }
394 
395 VALUE
396 rsock_make_ipaddr(struct sockaddr *addr, socklen_t addrlen)
397 {
398  char hbuf[1024];
399 
400  make_ipaddr0(addr, addrlen, hbuf, sizeof(hbuf));
401  return rb_str_new2(hbuf);
402 }
403 
404 static void
405 make_inetaddr(unsigned int host, char *buf, size_t buflen)
406 {
407  struct sockaddr_in sin;
408 
409  INIT_SOCKADDR_IN(&sin, sizeof(sin));
410  sin.sin_addr.s_addr = host;
411  make_ipaddr0((struct sockaddr*)&sin, sizeof(sin), buf, buflen);
412 }
413 
414 static int
415 str_is_number(const char *p)
416 {
417  char *ep;
418 
419  if (!p || *p == '\0')
420  return 0;
421  ep = NULL;
422  (void)STRTOUL(p, &ep, 10);
423  if (ep && *ep == '\0')
424  return 1;
425  else
426  return 0;
427 }
428 
429 #define str_equal(ptr, len, name) \
430  ((ptr)[0] == name[0] && \
431  rb_strlen_lit(name) == (len) && memcmp(ptr, name, len) == 0)
432 #define SafeStringValueCStr(v) do {\
433  StringValueCStr(v);\
434  rb_check_safe_obj(v);\
435 } while(0)
436 
437 static char*
438 host_str(VALUE host, char *hbuf, size_t hbuflen, int *flags_ptr)
439 {
440  if (NIL_P(host)) {
441  return NULL;
442  }
443  else if (rb_obj_is_kind_of(host, rb_cInteger)) {
444  unsigned int i = NUM2UINT(host);
445 
446  make_inetaddr(htonl(i), hbuf, hbuflen);
447  if (flags_ptr) *flags_ptr |= AI_NUMERICHOST;
448  return hbuf;
449  }
450  else {
451  const char *name;
452  size_t len;
453 
454  SafeStringValueCStr(host);
455  RSTRING_GETMEM(host, name, len);
456  if (!len || str_equal(name, len, "<any>")) {
457  make_inetaddr(INADDR_ANY, hbuf, hbuflen);
458  if (flags_ptr) *flags_ptr |= AI_NUMERICHOST;
459  }
460  else if (str_equal(name, len, "<broadcast>")) {
461  make_inetaddr(INADDR_BROADCAST, hbuf, hbuflen);
462  if (flags_ptr) *flags_ptr |= AI_NUMERICHOST;
463  }
464  else if (len >= hbuflen) {
465  rb_raise(rb_eArgError, "hostname too long (%"PRIuSIZE")",
466  len);
467  }
468  else {
469  memcpy(hbuf, name, len);
470  hbuf[len] = '\0';
471  }
472  return hbuf;
473  }
474 }
475 
476 static char*
477 port_str(VALUE port, char *pbuf, size_t pbuflen, int *flags_ptr)
478 {
479  if (NIL_P(port)) {
480  return 0;
481  }
482  else if (FIXNUM_P(port)) {
483  snprintf(pbuf, pbuflen, "%ld", FIX2LONG(port));
484 #ifdef AI_NUMERICSERV
485  if (flags_ptr) *flags_ptr |= AI_NUMERICSERV;
486 #endif
487  return pbuf;
488  }
489  else {
490  const char *serv;
491  size_t len;
492 
493  SafeStringValueCStr(port);
494  RSTRING_GETMEM(port, serv, len);
495  if (len >= pbuflen) {
496  rb_raise(rb_eArgError, "service name too long (%"PRIuSIZE")",
497  len);
498  }
499  memcpy(pbuf, serv, len);
500  pbuf[len] = '\0';
501  return pbuf;
502  }
503 }
504 
505 struct rb_addrinfo*
506 rsock_getaddrinfo(VALUE host, VALUE port, struct addrinfo *hints, int socktype_hack)
507 {
508  struct rb_addrinfo* res = NULL;
509  char *hostp, *portp;
510  int error;
511  char hbuf[NI_MAXHOST], pbuf[NI_MAXSERV];
512  int additional_flags = 0;
513 
514  hostp = host_str(host, hbuf, sizeof(hbuf), &additional_flags);
515  portp = port_str(port, pbuf, sizeof(pbuf), &additional_flags);
516 
517  if (socktype_hack && hints->ai_socktype == 0 && str_is_number(portp)) {
518  hints->ai_socktype = SOCK_DGRAM;
519  }
520  hints->ai_flags |= additional_flags;
521 
522  error = rb_getaddrinfo(hostp, portp, hints, &res);
523  if (error) {
524  if (hostp && hostp[strlen(hostp)-1] == '\n') {
525  rb_raise(rb_eSocket, "newline at the end of hostname");
526  }
527  rsock_raise_socket_error("getaddrinfo", error);
528  }
529 
530  return res;
531 }
532 
533 int
535 {
536  struct sockaddr sa = { 0 };
537  socklen_t sa_len = sizeof(sa);
538 
539  if (fd < 0 || getsockname(fd, &sa, &sa_len) != 0 ||
540  (size_t)sa_len < offsetof(struct sockaddr, sa_family) + sizeof(sa.sa_family)) {
541  return AF_UNSPEC;
542  }
543  return sa.sa_family;
544 }
545 
546 struct rb_addrinfo*
547 rsock_addrinfo(VALUE host, VALUE port, int family, int socktype, int flags)
548 {
549  struct addrinfo hints;
550 
551  MEMZERO(&hints, struct addrinfo, 1);
552  hints.ai_family = family;
553  hints.ai_socktype = socktype;
554  hints.ai_flags = flags;
555  return rsock_getaddrinfo(host, port, &hints, 1);
556 }
557 
558 VALUE
559 rsock_ipaddr(struct sockaddr *sockaddr, socklen_t sockaddrlen, int norevlookup)
560 {
561  VALUE family, port, addr1, addr2;
562  VALUE ary;
563  int error;
564  char hbuf[1024], pbuf[1024];
565  ID id;
566 
567  id = rsock_intern_family(sockaddr->sa_family);
568  if (id) {
569  family = rb_str_dup(rb_id2str(id));
570  }
571  else {
572  sprintf(pbuf, "unknown:%d", sockaddr->sa_family);
573  family = rb_str_new2(pbuf);
574  }
575 
576  addr1 = Qnil;
577  if (!norevlookup) {
578  error = rb_getnameinfo(sockaddr, sockaddrlen, hbuf, sizeof(hbuf),
579  NULL, 0, 0);
580  if (! error) {
581  addr1 = rb_str_new2(hbuf);
582  }
583  }
584  error = rb_getnameinfo(sockaddr, sockaddrlen, hbuf, sizeof(hbuf),
585  pbuf, sizeof(pbuf), NI_NUMERICHOST | NI_NUMERICSERV);
586  if (error) {
587  rsock_raise_socket_error("getnameinfo", error);
588  }
589  addr2 = rb_str_new2(hbuf);
590  if (addr1 == Qnil) {
591  addr1 = addr2;
592  }
593  port = INT2FIX(atoi(pbuf));
594  ary = rb_ary_new3(4, family, port, addr1, addr2);
595 
596  return ary;
597 }
598 
599 #ifdef HAVE_SYS_UN_H
600 VALUE
601 rsock_unixpath_str(struct sockaddr_un *sockaddr, socklen_t len)
602 {
603  char *s, *e;
604  s = sockaddr->sun_path;
605  e = (char *)sockaddr + len;
606  while (s < e && *(e-1) == '\0')
607  e--;
608  if (s <= e)
609  return rb_str_new(s, e-s);
610  else
611  return rb_str_new2("");
612 }
613 
614 VALUE
615 rsock_unixaddr(struct sockaddr_un *sockaddr, socklen_t len)
616 {
617  return rb_assoc_new(rb_str_new2("AF_UNIX"),
618  rsock_unixpath_str(sockaddr, len));
619 }
620 
621 socklen_t
622 rsock_unix_sockaddr_len(VALUE path)
623 {
624 #ifdef __linux__
625  if (RSTRING_LEN(path) == 0) {
626  /* autobind; see unix(7) for details. */
627  return (socklen_t) sizeof(sa_family_t);
628  }
629  else if (RSTRING_PTR(path)[0] == '\0') {
630  /* abstract namespace; see unix(7) for details. */
631  if (SOCKLEN_MAX - offsetof(struct sockaddr_un, sun_path) < (size_t)RSTRING_LEN(path))
632  rb_raise(rb_eArgError, "Linux abstract socket too long");
633  return (socklen_t) offsetof(struct sockaddr_un, sun_path) +
634  RSTRING_SOCKLEN(path);
635  }
636  else {
637 #endif
638  return (socklen_t) sizeof(struct sockaddr_un);
639 #ifdef __linux__
640  }
641 #endif
642 }
643 #endif
644 
645 struct hostent_arg {
647  struct rb_addrinfo* addr;
648  VALUE (*ipaddr)(struct sockaddr*, socklen_t);
649 };
650 
651 static VALUE
653 {
654  VALUE host = arg->host;
655  struct addrinfo* addr = arg->addr->ai;
656  VALUE (*ipaddr)(struct sockaddr*, socklen_t) = arg->ipaddr;
657 
658  struct addrinfo *ai;
659  struct hostent *h;
660  VALUE ary, names;
661  char **pch;
662  const char* hostp;
663  char hbuf[NI_MAXHOST];
664 
665  ary = rb_ary_new();
666  if (addr->ai_canonname) {
667  hostp = addr->ai_canonname;
668  }
669  else {
670  hostp = host_str(host, hbuf, sizeof(hbuf), NULL);
671  }
672  rb_ary_push(ary, rb_str_new2(hostp));
673 
674  if (addr->ai_canonname && strlen(addr->ai_canonname) < NI_MAXHOST &&
675  (h = gethostbyname(addr->ai_canonname))) {
676  names = rb_ary_new();
677  if (h->h_aliases != NULL) {
678  for (pch = h->h_aliases; *pch; pch++) {
679  rb_ary_push(names, rb_str_new2(*pch));
680  }
681  }
682  }
683  else {
684  names = rb_ary_new2(0);
685  }
686  rb_ary_push(ary, names);
687  rb_ary_push(ary, INT2NUM(addr->ai_family));
688  for (ai = addr; ai; ai = ai->ai_next) {
689  rb_ary_push(ary, (*ipaddr)(ai->ai_addr, ai->ai_addrlen));
690  }
691 
692  return ary;
693 }
694 
695 VALUE
697 {
698  struct rb_addrinfo *addr = (struct rb_addrinfo *)arg;
699  rb_freeaddrinfo(addr);
700  return Qnil;
701 }
702 
703 VALUE
704 rsock_make_hostent(VALUE host, struct rb_addrinfo *addr, VALUE (*ipaddr)(struct sockaddr *, socklen_t))
705 {
706  struct hostent_arg arg;
707 
708  arg.host = host;
709  arg.addr = addr;
710  arg.ipaddr = ipaddr;
711  return rb_ensure(make_hostent_internal, (VALUE)&arg,
712  rsock_freeaddrinfo, (VALUE)addr);
713 }
714 
715 typedef struct {
718  int pfamily;
719  int socktype;
720  int protocol;
723 } rb_addrinfo_t;
724 
725 static void
726 addrinfo_mark(void *ptr)
727 {
728  rb_addrinfo_t *rai = ptr;
729  if (rai) {
730  rb_gc_mark(rai->inspectname);
731  rb_gc_mark(rai->canonname);
732  }
733 }
734 
735 #define addrinfo_free RUBY_TYPED_DEFAULT_FREE
736 
737 static size_t
738 addrinfo_memsize(const void *ptr)
739 {
740  return sizeof(rb_addrinfo_t);
741 }
742 
744  "socket/addrinfo",
746 };
747 
748 static VALUE
750 {
751  return TypedData_Wrap_Struct(klass, &addrinfo_type, 0);
752 }
753 
754 #define IS_ADDRINFO(obj) rb_typeddata_is_kind_of((obj), &addrinfo_type)
755 static inline rb_addrinfo_t *
757 {
758  return rb_check_typeddata(self, &addrinfo_type);
759 }
760 
761 static rb_addrinfo_t *
763 {
764  rb_addrinfo_t *rai = check_addrinfo(self);
765 
766  if (!rai) {
767  rb_raise(rb_eTypeError, "uninitialized socket address");
768  }
769  return rai;
770 }
771 
772 
773 static rb_addrinfo_t *
775 {
777  rai->inspectname = Qnil;
778  rai->canonname = Qnil;
779  return rai;
780 }
781 
782 static void
783 init_addrinfo(rb_addrinfo_t *rai, struct sockaddr *sa, socklen_t len,
784  int pfamily, int socktype, int protocol,
785  VALUE canonname, VALUE inspectname)
786 {
787  if ((socklen_t)sizeof(rai->addr) < len)
788  rb_raise(rb_eArgError, "sockaddr string too big");
789  memcpy((void *)&rai->addr, (void *)sa, len);
790  rai->sockaddr_len = len;
791 
792  rai->pfamily = pfamily;
793  rai->socktype = socktype;
794  rai->protocol = protocol;
795  rai->canonname = canonname;
796  rai->inspectname = inspectname;
797 }
798 
799 VALUE
800 rsock_addrinfo_new(struct sockaddr *addr, socklen_t len,
801  int family, int socktype, int protocol,
802  VALUE canonname, VALUE inspectname)
803 {
804  VALUE a;
805  rb_addrinfo_t *rai;
806 
808  DATA_PTR(a) = rai = alloc_addrinfo();
809  init_addrinfo(rai, addr, len, family, socktype, protocol, canonname, inspectname);
810  return a;
811 }
812 
813 static struct rb_addrinfo *
815  VALUE family, VALUE socktype, VALUE protocol, VALUE flags,
816  int socktype_hack)
817 {
818  struct addrinfo hints;
819  struct rb_addrinfo *res;
820 
821  MEMZERO(&hints, struct addrinfo, 1);
822  hints.ai_family = NIL_P(family) ? PF_UNSPEC : rsock_family_arg(family);
823 
824  if (!NIL_P(socktype)) {
825  hints.ai_socktype = rsock_socktype_arg(socktype);
826  }
827  if (!NIL_P(protocol)) {
828  hints.ai_protocol = NUM2INT(protocol);
829  }
830  if (!NIL_P(flags)) {
831  hints.ai_flags = NUM2INT(flags);
832  }
833  res = rsock_getaddrinfo(node, service, &hints, socktype_hack);
834 
835  if (res == NULL)
836  rb_raise(rb_eSocket, "host not found");
837  return res;
838 }
839 
840 static VALUE make_inspectname(VALUE node, VALUE service, struct addrinfo *res);
841 
842 static void
844  VALUE family, VALUE socktype, VALUE protocol, VALUE flags,
845  VALUE inspectnode, VALUE inspectservice)
846 {
847  struct rb_addrinfo *res = call_getaddrinfo(node, service, family, socktype, protocol, flags, 1);
848  VALUE canonname;
849  VALUE inspectname = rb_str_equal(node, inspectnode) ? Qnil : make_inspectname(inspectnode, inspectservice, res->ai);
850 
851  canonname = Qnil;
852  if (res->ai->ai_canonname) {
853  canonname = rb_tainted_str_new_cstr(res->ai->ai_canonname);
854  OBJ_FREEZE(canonname);
855  }
856 
857  init_addrinfo(rai, res->ai->ai_addr, res->ai->ai_addrlen,
858  NUM2INT(family), NUM2INT(socktype), NUM2INT(protocol),
859  canonname, inspectname);
860 
861  rb_freeaddrinfo(res);
862 }
863 
864 static VALUE
865 make_inspectname(VALUE node, VALUE service, struct addrinfo *res)
866 {
867  VALUE inspectname = Qnil;
868 
869  if (res) {
870  /* drop redundant information which also shown in address:port part. */
871  char hbuf[NI_MAXHOST], pbuf[NI_MAXSERV];
872  int ret;
873  ret = rb_getnameinfo(res->ai_addr, res->ai_addrlen, hbuf,
874  sizeof(hbuf), pbuf, sizeof(pbuf),
876  if (ret == 0) {
877  if (RB_TYPE_P(node, T_STRING) && strcmp(hbuf, RSTRING_PTR(node)) == 0)
878  node = Qnil;
879  if (RB_TYPE_P(service, T_STRING) && strcmp(pbuf, RSTRING_PTR(service)) == 0)
880  service = Qnil;
881  else if (RB_TYPE_P(service, T_FIXNUM) && atoi(pbuf) == FIX2INT(service))
882  service = Qnil;
883  }
884  }
885 
886  if (RB_TYPE_P(node, T_STRING)) {
887  inspectname = rb_str_dup(node);
888  }
889  if (RB_TYPE_P(service, T_STRING)) {
890  if (NIL_P(inspectname))
891  inspectname = rb_sprintf(":%s", StringValueCStr(service));
892  else
893  rb_str_catf(inspectname, ":%s", StringValueCStr(service));
894  }
895  else if (RB_TYPE_P(service, T_FIXNUM) && FIX2INT(service) != 0)
896  {
897  if (NIL_P(inspectname))
898  inspectname = rb_sprintf(":%d", FIX2INT(service));
899  else
900  rb_str_catf(inspectname, ":%d", FIX2INT(service));
901  }
902  if (!NIL_P(inspectname)) {
903  OBJ_INFECT(inspectname, node);
904  OBJ_INFECT(inspectname, service);
905  OBJ_FREEZE(inspectname);
906  }
907  return inspectname;
908 }
909 
910 static VALUE
911 addrinfo_firstonly_new(VALUE node, VALUE service, VALUE family, VALUE socktype, VALUE protocol, VALUE flags)
912 {
913  VALUE ret;
914  VALUE canonname;
915  VALUE inspectname;
916 
917  struct rb_addrinfo *res = call_getaddrinfo(node, service, family, socktype, protocol, flags, 0);
918 
919  inspectname = make_inspectname(node, service, res->ai);
920 
921  canonname = Qnil;
922  if (res->ai->ai_canonname) {
923  canonname = rb_tainted_str_new_cstr(res->ai->ai_canonname);
924  OBJ_FREEZE(canonname);
925  }
926 
927  ret = rsock_addrinfo_new(res->ai->ai_addr, res->ai->ai_addrlen,
928  res->ai->ai_family, res->ai->ai_socktype,
929  res->ai->ai_protocol,
930  canonname, inspectname);
931 
932  rb_freeaddrinfo(res);
933  return ret;
934 }
935 
936 static VALUE
937 addrinfo_list_new(VALUE node, VALUE service, VALUE family, VALUE socktype, VALUE protocol, VALUE flags)
938 {
939  VALUE ret;
940  struct addrinfo *r;
941  VALUE inspectname;
942 
943  struct rb_addrinfo *res = call_getaddrinfo(node, service, family, socktype, protocol, flags, 0);
944 
945  inspectname = make_inspectname(node, service, res->ai);
946 
947  ret = rb_ary_new();
948  for (r = res->ai; r; r = r->ai_next) {
949  VALUE addr;
950  VALUE canonname = Qnil;
951 
952  if (r->ai_canonname) {
953  canonname = rb_tainted_str_new_cstr(r->ai_canonname);
954  OBJ_FREEZE(canonname);
955  }
956 
957  addr = rsock_addrinfo_new(r->ai_addr, r->ai_addrlen,
958  r->ai_family, r->ai_socktype, r->ai_protocol,
959  canonname, inspectname);
960 
961  rb_ary_push(ret, addr);
962  }
963 
964  rb_freeaddrinfo(res);
965  return ret;
966 }
967 
968 
969 #ifdef HAVE_SYS_UN_H
970 static void
971 init_unix_addrinfo(rb_addrinfo_t *rai, VALUE path, int socktype)
972 {
973  struct sockaddr_un un;
974  socklen_t len;
975 
976  StringValue(path);
977 
978  if (sizeof(un.sun_path) < (size_t)RSTRING_LEN(path))
980  "too long unix socket path (%"PRIuSIZE" bytes given but %"PRIuSIZE" bytes max)",
981  (size_t)RSTRING_LEN(path), sizeof(un.sun_path));
982 
983  INIT_SOCKADDR_UN(&un, sizeof(struct sockaddr_un));
984  memcpy((void*)&un.sun_path, RSTRING_PTR(path), RSTRING_LEN(path));
985 
986  len = rsock_unix_sockaddr_len(path);
987  init_addrinfo(rai, (struct sockaddr *)&un, len,
988  PF_UNIX, socktype, 0, Qnil, Qnil);
989 }
990 #endif
991 
992 /*
993  * call-seq:
994  * Addrinfo.new(sockaddr) => addrinfo
995  * Addrinfo.new(sockaddr, family) => addrinfo
996  * Addrinfo.new(sockaddr, family, socktype) => addrinfo
997  * Addrinfo.new(sockaddr, family, socktype, protocol) => addrinfo
998  *
999  * returns a new instance of Addrinfo.
1000  * The instance contains sockaddr, family, socktype, protocol.
1001  * sockaddr means struct sockaddr which can be used for connect(2), etc.
1002  * family, socktype and protocol are integers which is used for arguments of socket(2).
1003  *
1004  * sockaddr is specified as an array or a string.
1005  * The array should be compatible to the value of IPSocket#addr or UNIXSocket#addr.
1006  * The string should be struct sockaddr as generated by
1007  * Socket.sockaddr_in or Socket.unpack_sockaddr_un.
1008  *
1009  * sockaddr examples:
1010  * - ["AF_INET", 46102, "localhost.localdomain", "127.0.0.1"]
1011  * - ["AF_INET6", 42304, "ip6-localhost", "::1"]
1012  * - ["AF_UNIX", "/tmp/sock"]
1013  * - Socket.sockaddr_in("smtp", "2001:DB8::1")
1014  * - Socket.sockaddr_in(80, "172.18.22.42")
1015  * - Socket.sockaddr_in(80, "www.ruby-lang.org")
1016  * - Socket.sockaddr_un("/tmp/sock")
1017  *
1018  * In an AF_INET/AF_INET6 sockaddr array, the 4th element,
1019  * numeric IP address, is used to construct socket address in the Addrinfo instance.
1020  * If the 3rd element, textual host name, is non-nil, it is also recorded but used only for Addrinfo#inspect.
1021  *
1022  * family is specified as an integer to specify the protocol family such as Socket::PF_INET.
1023  * It can be a symbol or a string which is the constant name
1024  * with or without PF_ prefix such as :INET, :INET6, :UNIX, "PF_INET", etc.
1025  * If omitted, PF_UNSPEC is assumed.
1026  *
1027  * socktype is specified as an integer to specify the socket type such as Socket::SOCK_STREAM.
1028  * It can be a symbol or a string which is the constant name
1029  * with or without SOCK_ prefix such as :STREAM, :DGRAM, :RAW, "SOCK_STREAM", etc.
1030  * If omitted, 0 is assumed.
1031  *
1032  * protocol is specified as an integer to specify the protocol such as Socket::IPPROTO_TCP.
1033  * It must be an integer, unlike family and socktype.
1034  * If omitted, 0 is assumed.
1035  * Note that 0 is reasonable value for most protocols, except raw socket.
1036  *
1037  */
1038 static VALUE
1040 {
1041  rb_addrinfo_t *rai;
1042  VALUE sockaddr_arg, sockaddr_ary, pfamily, socktype, protocol;
1043  int i_pfamily, i_socktype, i_protocol;
1044  struct sockaddr *sockaddr_ptr;
1045  socklen_t sockaddr_len;
1046  VALUE canonname = Qnil, inspectname = Qnil;
1047 
1048  if (check_addrinfo(self))
1049  rb_raise(rb_eTypeError, "already initialized socket address");
1050  DATA_PTR(self) = rai = alloc_addrinfo();
1051 
1052  rb_scan_args(argc, argv, "13", &sockaddr_arg, &pfamily, &socktype, &protocol);
1053 
1054  i_pfamily = NIL_P(pfamily) ? PF_UNSPEC : rsock_family_arg(pfamily);
1055  i_socktype = NIL_P(socktype) ? 0 : rsock_socktype_arg(socktype);
1056  i_protocol = NIL_P(protocol) ? 0 : NUM2INT(protocol);
1057 
1058  sockaddr_ary = rb_check_array_type(sockaddr_arg);
1059  if (!NIL_P(sockaddr_ary)) {
1060  VALUE afamily = rb_ary_entry(sockaddr_ary, 0);
1061  int af;
1062  StringValue(afamily);
1063  if (rsock_family_to_int(RSTRING_PTR(afamily), RSTRING_LEN(afamily), &af) == -1)
1064  rb_raise(rb_eSocket, "unknown address family: %s", StringValueCStr(afamily));
1065  switch (af) {
1066  case AF_INET: /* ["AF_INET", 46102, "localhost.localdomain", "127.0.0.1"] */
1067 #ifdef INET6
1068  case AF_INET6: /* ["AF_INET6", 42304, "ip6-localhost", "::1"] */
1069 #endif
1070  {
1071  VALUE service = rb_ary_entry(sockaddr_ary, 1);
1072  VALUE nodename = rb_ary_entry(sockaddr_ary, 2);
1073  VALUE numericnode = rb_ary_entry(sockaddr_ary, 3);
1074  int flags;
1075 
1076  service = INT2NUM(NUM2INT(service));
1077  if (!NIL_P(nodename))
1078  StringValue(nodename);
1079  StringValue(numericnode);
1080  flags = AI_NUMERICHOST;
1081 #ifdef AI_NUMERICSERV
1082  flags |= AI_NUMERICSERV;
1083 #endif
1084 
1085  init_addrinfo_getaddrinfo(rai, numericnode, service,
1086  INT2NUM(i_pfamily ? i_pfamily : af), INT2NUM(i_socktype), INT2NUM(i_protocol),
1087  INT2NUM(flags),
1088  nodename, service);
1089  break;
1090  }
1091 
1092 #ifdef HAVE_SYS_UN_H
1093  case AF_UNIX: /* ["AF_UNIX", "/tmp/sock"] */
1094  {
1095  VALUE path = rb_ary_entry(sockaddr_ary, 1);
1096  StringValue(path);
1097  init_unix_addrinfo(rai, path, SOCK_STREAM);
1098  break;
1099  }
1100 #endif
1101 
1102  default:
1103  rb_raise(rb_eSocket, "unexpected address family");
1104  }
1105  }
1106  else {
1107  StringValue(sockaddr_arg);
1108  sockaddr_ptr = (struct sockaddr *)RSTRING_PTR(sockaddr_arg);
1109  sockaddr_len = RSTRING_SOCKLEN(sockaddr_arg);
1110  init_addrinfo(rai, sockaddr_ptr, sockaddr_len,
1111  i_pfamily, i_socktype, i_protocol,
1112  canonname, inspectname);
1113  }
1114 
1115  return self;
1116 }
1117 
1118 static int
1119 get_afamily(struct sockaddr *addr, socklen_t len)
1120 {
1121  if ((socklen_t)((char*)&addr->sa_family + sizeof(addr->sa_family) - (char*)addr) <= len)
1122  return addr->sa_family;
1123  else
1124  return AF_UNSPEC;
1125 }
1126 
1127 static int
1129 {
1130  return get_afamily(&rai->addr.addr, rai->sockaddr_len);
1131 }
1132 
1133 static VALUE
1135 {
1136  rb_addrinfo_t *rai = get_addrinfo(addrinfo);
1137  union_sockaddr *sockaddr = &rai->addr;
1138  socklen_t socklen = rai->sockaddr_len;
1139  return rsock_inspect_sockaddr((struct sockaddr *)sockaddr, socklen, ret);
1140 }
1141 
1142 VALUE
1143 rsock_inspect_sockaddr(struct sockaddr *sockaddr_arg, socklen_t socklen, VALUE ret)
1144 {
1145  union_sockaddr *sockaddr = (union_sockaddr *)sockaddr_arg;
1146  if (socklen == 0) {
1147  rb_str_cat2(ret, "empty-sockaddr");
1148  }
1149  else if ((long)socklen < ((char*)&sockaddr->addr.sa_family + sizeof(sockaddr->addr.sa_family)) - (char*)sockaddr)
1150  rb_str_cat2(ret, "too-short-sockaddr");
1151  else {
1152  switch (sockaddr->addr.sa_family) {
1153  case AF_UNSPEC:
1154  {
1155  rb_str_cat2(ret, "UNSPEC");
1156  break;
1157  }
1158 
1159  case AF_INET:
1160  {
1161  struct sockaddr_in *addr;
1162  int port;
1163  addr = &sockaddr->in;
1164  if ((socklen_t)(((char*)&addr->sin_addr)-(char*)addr+0+1) <= socklen)
1165  rb_str_catf(ret, "%d", ((unsigned char*)&addr->sin_addr)[0]);
1166  else
1167  rb_str_cat2(ret, "?");
1168  if ((socklen_t)(((char*)&addr->sin_addr)-(char*)addr+1+1) <= socklen)
1169  rb_str_catf(ret, ".%d", ((unsigned char*)&addr->sin_addr)[1]);
1170  else
1171  rb_str_cat2(ret, ".?");
1172  if ((socklen_t)(((char*)&addr->sin_addr)-(char*)addr+2+1) <= socklen)
1173  rb_str_catf(ret, ".%d", ((unsigned char*)&addr->sin_addr)[2]);
1174  else
1175  rb_str_cat2(ret, ".?");
1176  if ((socklen_t)(((char*)&addr->sin_addr)-(char*)addr+3+1) <= socklen)
1177  rb_str_catf(ret, ".%d", ((unsigned char*)&addr->sin_addr)[3]);
1178  else
1179  rb_str_cat2(ret, ".?");
1180 
1181  if ((socklen_t)(((char*)&addr->sin_port)-(char*)addr+(int)sizeof(addr->sin_port)) < socklen) {
1182  port = ntohs(addr->sin_port);
1183  if (port)
1184  rb_str_catf(ret, ":%d", port);
1185  }
1186  else {
1187  rb_str_cat2(ret, ":?");
1188  }
1189  if ((socklen_t)sizeof(struct sockaddr_in) != socklen)
1190  rb_str_catf(ret, " (%d bytes for %d bytes sockaddr_in)",
1191  (int)socklen,
1192  (int)sizeof(struct sockaddr_in));
1193  break;
1194  }
1195 
1196 #ifdef AF_INET6
1197  case AF_INET6:
1198  {
1199  struct sockaddr_in6 *addr;
1200  char hbuf[1024];
1201  int port;
1202  int error;
1203  if (socklen < (socklen_t)sizeof(struct sockaddr_in6)) {
1204  rb_str_catf(ret, "too-short-AF_INET6-sockaddr %d bytes", (int)socklen);
1205  }
1206  else {
1207  addr = &sockaddr->in6;
1208  /* use getnameinfo for scope_id.
1209  * RFC 4007: IPv6 Scoped Address Architecture
1210  * draft-ietf-ipv6-scope-api-00.txt: Scoped Address Extensions to the IPv6 Basic Socket API
1211  */
1212  error = getnameinfo(&sockaddr->addr, socklen,
1213  hbuf, (socklen_t)sizeof(hbuf), NULL, 0,
1215  if (error) {
1216  rsock_raise_socket_error("getnameinfo", error);
1217  }
1218  if (addr->sin6_port == 0) {
1219  rb_str_cat2(ret, hbuf);
1220  }
1221  else {
1222  port = ntohs(addr->sin6_port);
1223  rb_str_catf(ret, "[%s]:%d", hbuf, port);
1224  }
1225  if ((socklen_t)sizeof(struct sockaddr_in6) < socklen)
1226  rb_str_catf(ret, "(sockaddr %d bytes too long)", (int)(socklen - sizeof(struct sockaddr_in6)));
1227  }
1228  break;
1229  }
1230 #endif
1231 
1232 #ifdef HAVE_SYS_UN_H
1233  case AF_UNIX:
1234  {
1235  struct sockaddr_un *addr = &sockaddr->un;
1236  char *p, *s, *e;
1237  s = addr->sun_path;
1238  e = (char*)addr + socklen;
1239  while (s < e && *(e-1) == '\0')
1240  e--;
1241  if (e < s)
1242  rb_str_cat2(ret, "too-short-AF_UNIX-sockaddr");
1243  else if (s == e)
1244  rb_str_cat2(ret, "empty-path-AF_UNIX-sockaddr");
1245  else {
1246  int printable_only = 1;
1247  p = s;
1248  while (p < e) {
1249  printable_only = printable_only && ISPRINT(*p) && !ISSPACE(*p);
1250  p++;
1251  }
1252  if (printable_only) { /* only printable, no space */
1253  if (s[0] != '/') /* relative path */
1254  rb_str_cat2(ret, "UNIX ");
1255  rb_str_cat(ret, s, p - s);
1256  }
1257  else {
1258  rb_str_cat2(ret, "UNIX");
1259  while (s < e)
1260  rb_str_catf(ret, ":%02x", (unsigned char)*s++);
1261  }
1262  }
1263  break;
1264  }
1265 #endif
1266 
1267 #if defined(AF_PACKET) && defined(__linux__)
1268  /* GNU/Linux */
1269  case AF_PACKET:
1270  {
1271  struct sockaddr_ll *addr;
1272  const char *sep = "[";
1273 #define CATSEP do { rb_str_cat2(ret, sep); sep = " "; } while (0);
1274 
1275  addr = (struct sockaddr_ll *)sockaddr;
1276 
1277  rb_str_cat2(ret, "PACKET");
1278 
1279  if (offsetof(struct sockaddr_ll, sll_protocol) + sizeof(addr->sll_protocol) <= (size_t)socklen) {
1280  CATSEP;
1281  rb_str_catf(ret, "protocol=%d", ntohs(addr->sll_protocol));
1282  }
1283  if (offsetof(struct sockaddr_ll, sll_ifindex) + sizeof(addr->sll_ifindex) <= (size_t)socklen) {
1284  char buf[IFNAMSIZ];
1285  CATSEP;
1286  if (if_indextoname(addr->sll_ifindex, buf) == NULL)
1287  rb_str_catf(ret, "ifindex=%d", addr->sll_ifindex);
1288  else
1289  rb_str_catf(ret, "%s", buf);
1290  }
1291  if (offsetof(struct sockaddr_ll, sll_hatype) + sizeof(addr->sll_hatype) <= (size_t)socklen) {
1292  CATSEP;
1293  rb_str_catf(ret, "hatype=%d", addr->sll_hatype);
1294  }
1295  if (offsetof(struct sockaddr_ll, sll_pkttype) + sizeof(addr->sll_pkttype) <= (size_t)socklen) {
1296  CATSEP;
1297  if (addr->sll_pkttype == PACKET_HOST)
1298  rb_str_cat2(ret, "HOST");
1299  else if (addr->sll_pkttype == PACKET_BROADCAST)
1300  rb_str_cat2(ret, "BROADCAST");
1301  else if (addr->sll_pkttype == PACKET_MULTICAST)
1302  rb_str_cat2(ret, "MULTICAST");
1303  else if (addr->sll_pkttype == PACKET_OTHERHOST)
1304  rb_str_cat2(ret, "OTHERHOST");
1305  else if (addr->sll_pkttype == PACKET_OUTGOING)
1306  rb_str_cat2(ret, "OUTGOING");
1307  else
1308  rb_str_catf(ret, "pkttype=%d", addr->sll_pkttype);
1309  }
1310  if (socklen != (socklen_t)(offsetof(struct sockaddr_ll, sll_addr) + addr->sll_halen)) {
1311  CATSEP;
1312  if (offsetof(struct sockaddr_ll, sll_halen) + sizeof(addr->sll_halen) <= (size_t)socklen) {
1313  rb_str_catf(ret, "halen=%d", addr->sll_halen);
1314  }
1315  }
1316  if (offsetof(struct sockaddr_ll, sll_addr) < (size_t)socklen) {
1317  socklen_t len, i;
1318  CATSEP;
1319  rb_str_cat2(ret, "hwaddr");
1320  len = addr->sll_halen;
1321  if ((size_t)socklen < offsetof(struct sockaddr_ll, sll_addr) + len)
1322  len = socklen - offsetof(struct sockaddr_ll, sll_addr);
1323  for (i = 0; i < len; i++) {
1324  rb_str_cat2(ret, i == 0 ? "=" : ":");
1325  rb_str_catf(ret, "%02x", addr->sll_addr[i]);
1326  }
1327  }
1328 
1329  if (socklen < (socklen_t)(offsetof(struct sockaddr_ll, sll_halen) + sizeof(addr->sll_halen)) ||
1330  (socklen_t)(offsetof(struct sockaddr_ll, sll_addr) + addr->sll_halen) != socklen) {
1331  CATSEP;
1332  rb_str_catf(ret, "(%d bytes for %d bytes sockaddr_ll)",
1333  (int)socklen, (int)sizeof(struct sockaddr_ll));
1334  }
1335 
1336  rb_str_cat2(ret, "]");
1337 #undef CATSEP
1338 
1339  break;
1340  }
1341 #endif
1342 
1343 #if defined(AF_LINK) && defined(HAVE_TYPE_STRUCT_SOCKADDR_DL)
1344  /* AF_LINK is defined in 4.4BSD derivations since Net2.
1345  link_ntoa is also defined at Net2.
1346  However Debian GNU/kFreeBSD defines AF_LINK but
1347  don't have link_ntoa. */
1348  case AF_LINK:
1349  {
1350  /*
1351  * Simple implementation using link_ntoa():
1352  * This doesn't work on Debian GNU/kFreeBSD 6.0.7 (squeeze).
1353  * Also, the format is bit different.
1354  *
1355  * rb_str_catf(ret, "LINK %s", link_ntoa(&sockaddr->dl));
1356  * break;
1357  */
1358  struct sockaddr_dl *addr = &sockaddr->dl;
1359  char *np = NULL, *ap = NULL, *endp;
1360  int nlen = 0, alen = 0;
1361  int i, off;
1362  const char *sep = "[";
1363 #define CATSEP do { rb_str_cat2(ret, sep); sep = " "; } while (0);
1364 
1365  rb_str_cat2(ret, "LINK");
1366 
1367  endp = ((char *)addr) + socklen;
1368 
1369  if (offsetof(struct sockaddr_dl, sdl_data) < socklen) {
1370  np = addr->sdl_data;
1371  nlen = addr->sdl_nlen;
1372  if (endp - np < nlen)
1373  nlen = (int)(endp - np);
1374  }
1375  off = addr->sdl_nlen;
1376 
1377  if (offsetof(struct sockaddr_dl, sdl_data) + off < socklen) {
1378  ap = addr->sdl_data + off;
1379  alen = addr->sdl_alen;
1380  if (endp - ap < alen)
1381  alen = (int)(endp - ap);
1382  }
1383 
1384  CATSEP;
1385  if (np)
1386  rb_str_catf(ret, "%.*s", nlen, np);
1387  else
1388  rb_str_cat2(ret, "?");
1389 
1390  if (ap && 0 < alen) {
1391  CATSEP;
1392  for (i = 0; i < alen; i++)
1393  rb_str_catf(ret, "%s%02x", i == 0 ? "" : ":", (unsigned char)ap[i]);
1394  }
1395 
1396  if (socklen < (socklen_t)(offsetof(struct sockaddr_dl, sdl_nlen) + sizeof(addr->sdl_nlen)) ||
1397  socklen < (socklen_t)(offsetof(struct sockaddr_dl, sdl_alen) + sizeof(addr->sdl_alen)) ||
1398  socklen < (socklen_t)(offsetof(struct sockaddr_dl, sdl_slen) + sizeof(addr->sdl_slen)) ||
1399  /* longer length is possible behavior because struct sockaddr_dl has "minimum work area, can be larger" as the last field.
1400  * cf. Net2:/usr/src/sys/net/if_dl.h. */
1401  socklen < (socklen_t)(offsetof(struct sockaddr_dl, sdl_data) + addr->sdl_nlen + addr->sdl_alen + addr->sdl_slen)) {
1402  CATSEP;
1403  rb_str_catf(ret, "(%d bytes for %d bytes sockaddr_dl)",
1404  (int)socklen, (int)sizeof(struct sockaddr_dl));
1405  }
1406 
1407  rb_str_cat2(ret, "]");
1408 #undef CATSEP
1409  break;
1410  }
1411 #endif
1412 
1413  default:
1414  {
1415  ID id = rsock_intern_family(sockaddr->addr.sa_family);
1416  if (id == 0)
1417  rb_str_catf(ret, "unknown address family %d", sockaddr->addr.sa_family);
1418  else
1419  rb_str_catf(ret, "%s address format unknown", rb_id2name(id));
1420  break;
1421  }
1422  }
1423  }
1424 
1425  return ret;
1426 }
1427 
1428 /*
1429  * call-seq:
1430  * addrinfo.inspect => string
1431  *
1432  * returns a string which shows addrinfo in human-readable form.
1433  *
1434  * Addrinfo.tcp("localhost", 80).inspect #=> "#<Addrinfo: 127.0.0.1:80 TCP (localhost)>"
1435  * Addrinfo.unix("/tmp/sock").inspect #=> "#<Addrinfo: /tmp/sock SOCK_STREAM>"
1436  *
1437  */
1438 static VALUE
1440 {
1441  rb_addrinfo_t *rai = get_addrinfo(self);
1442  int internet_p;
1443  VALUE ret;
1444 
1445  ret = rb_sprintf("#<%s: ", rb_obj_classname(self));
1446 
1447  inspect_sockaddr(self, ret);
1448 
1449  if (rai->pfamily && ai_get_afamily(rai) != rai->pfamily) {
1451  if (id)
1452  rb_str_catf(ret, " %s", rb_id2name(id));
1453  else
1454  rb_str_catf(ret, " PF_\?\?\?(%d)", rai->pfamily);
1455  }
1456 
1457  internet_p = rai->pfamily == PF_INET;
1458 #ifdef INET6
1459  internet_p = internet_p || rai->pfamily == PF_INET6;
1460 #endif
1461  if (internet_p && rai->socktype == SOCK_STREAM &&
1462  (rai->protocol == 0 || rai->protocol == IPPROTO_TCP)) {
1463  rb_str_cat2(ret, " TCP");
1464  }
1465  else if (internet_p && rai->socktype == SOCK_DGRAM &&
1466  (rai->protocol == 0 || rai->protocol == IPPROTO_UDP)) {
1467  rb_str_cat2(ret, " UDP");
1468  }
1469  else {
1470  if (rai->socktype) {
1471  ID id = rsock_intern_socktype(rai->socktype);
1472  if (id)
1473  rb_str_catf(ret, " %s", rb_id2name(id));
1474  else
1475  rb_str_catf(ret, " SOCK_\?\?\?(%d)", rai->socktype);
1476  }
1477 
1478  if (rai->protocol) {
1479  if (internet_p) {
1480  ID id = rsock_intern_ipproto(rai->protocol);
1481  if (id)
1482  rb_str_catf(ret, " %s", rb_id2name(id));
1483  else
1484  goto unknown_protocol;
1485  }
1486  else {
1487  unknown_protocol:
1488  rb_str_catf(ret, " UNKNOWN_PROTOCOL(%d)", rai->protocol);
1489  }
1490  }
1491  }
1492 
1493  if (!NIL_P(rai->canonname)) {
1494  VALUE name = rai->canonname;
1495  rb_str_catf(ret, " %s", StringValueCStr(name));
1496  }
1497 
1498  if (!NIL_P(rai->inspectname)) {
1499  VALUE name = rai->inspectname;
1500  rb_str_catf(ret, " (%s)", StringValueCStr(name));
1501  }
1502 
1503  rb_str_buf_cat2(ret, ">");
1504  return ret;
1505 }
1506 
1507 /*
1508  * call-seq:
1509  * addrinfo.inspect_sockaddr => string
1510  *
1511  * returns a string which shows the sockaddr in _addrinfo_ with human-readable form.
1512  *
1513  * Addrinfo.tcp("localhost", 80).inspect_sockaddr #=> "127.0.0.1:80"
1514  * Addrinfo.tcp("ip6-localhost", 80).inspect_sockaddr #=> "[::1]:80"
1515  * Addrinfo.unix("/tmp/sock").inspect_sockaddr #=> "/tmp/sock"
1516  *
1517  */
1518 VALUE
1520 {
1521  return inspect_sockaddr(self, rb_str_new("", 0));
1522 }
1523 
1524 /* :nodoc: */
1525 static VALUE
1527 {
1528  rb_addrinfo_t *rai = get_addrinfo(self);
1529  VALUE sockaddr, afamily, pfamily, socktype, protocol, canonname, inspectname;
1530  int afamily_int = ai_get_afamily(rai);
1531  ID id;
1532 
1534  if (id == 0)
1535  rb_raise(rb_eSocket, "unknown protocol family: %d", rai->pfamily);
1536  pfamily = rb_id2str(id);
1537 
1538  if (rai->socktype == 0)
1539  socktype = INT2FIX(0);
1540  else {
1541  id = rsock_intern_socktype(rai->socktype);
1542  if (id == 0)
1543  rb_raise(rb_eSocket, "unknown socktype: %d", rai->socktype);
1544  socktype = rb_id2str(id);
1545  }
1546 
1547  if (rai->protocol == 0)
1548  protocol = INT2FIX(0);
1549  else if (IS_IP_FAMILY(afamily_int)) {
1550  id = rsock_intern_ipproto(rai->protocol);
1551  if (id == 0)
1552  rb_raise(rb_eSocket, "unknown IP protocol: %d", rai->protocol);
1553  protocol = rb_id2str(id);
1554  }
1555  else {
1556  rb_raise(rb_eSocket, "unknown protocol: %d", rai->protocol);
1557  }
1558 
1559  canonname = rai->canonname;
1560 
1561  inspectname = rai->inspectname;
1562 
1563  id = rsock_intern_family(afamily_int);
1564  if (id == 0)
1565  rb_raise(rb_eSocket, "unknown address family: %d", afamily_int);
1566  afamily = rb_id2str(id);
1567 
1568  switch(afamily_int) {
1569 #ifdef HAVE_SYS_UN_H
1570  case AF_UNIX:
1571  {
1572  struct sockaddr_un *su = &rai->addr.un;
1573  char *s, *e;
1574  s = su->sun_path;
1575  e = (char*)su + rai->sockaddr_len;
1576  while (s < e && *(e-1) == '\0')
1577  e--;
1578  sockaddr = rb_str_new(s, e-s);
1579  break;
1580  }
1581 #endif
1582 
1583  default:
1584  {
1585  char hbuf[NI_MAXHOST], pbuf[NI_MAXSERV];
1586  int error;
1587  error = getnameinfo(&rai->addr.addr, rai->sockaddr_len,
1588  hbuf, (socklen_t)sizeof(hbuf), pbuf, (socklen_t)sizeof(pbuf),
1590  if (error) {
1591  rsock_raise_socket_error("getnameinfo", error);
1592  }
1593  sockaddr = rb_assoc_new(rb_str_new_cstr(hbuf), rb_str_new_cstr(pbuf));
1594  break;
1595  }
1596  }
1597 
1598  return rb_ary_new3(7, afamily, sockaddr, pfamily, socktype, protocol, canonname, inspectname);
1599 }
1600 
1601 /* :nodoc: */
1602 static VALUE
1604 {
1605  VALUE v;
1606  VALUE canonname, inspectname;
1607  int afamily, pfamily, socktype, protocol;
1608  union_sockaddr ss;
1609  socklen_t len;
1610  rb_addrinfo_t *rai;
1611 
1612  if (check_addrinfo(self))
1613  rb_raise(rb_eTypeError, "already initialized socket address");
1614 
1615  ary = rb_convert_type(ary, T_ARRAY, "Array", "to_ary");
1616 
1617  v = rb_ary_entry(ary, 0);
1618  StringValue(v);
1619  if (rsock_family_to_int(RSTRING_PTR(v), RSTRING_LEN(v), &afamily) == -1)
1620  rb_raise(rb_eTypeError, "unexpected address family");
1621 
1622  v = rb_ary_entry(ary, 2);
1623  StringValue(v);
1624  if (rsock_family_to_int(RSTRING_PTR(v), RSTRING_LEN(v), &pfamily) == -1)
1625  rb_raise(rb_eTypeError, "unexpected protocol family");
1626 
1627  v = rb_ary_entry(ary, 3);
1628  if (v == INT2FIX(0))
1629  socktype = 0;
1630  else {
1631  StringValue(v);
1632  if (rsock_socktype_to_int(RSTRING_PTR(v), RSTRING_LEN(v), &socktype) == -1)
1633  rb_raise(rb_eTypeError, "unexpected socktype");
1634  }
1635 
1636  v = rb_ary_entry(ary, 4);
1637  if (v == INT2FIX(0))
1638  protocol = 0;
1639  else {
1640  StringValue(v);
1641  if (IS_IP_FAMILY(afamily)) {
1642  if (rsock_ipproto_to_int(RSTRING_PTR(v), RSTRING_LEN(v), &protocol) == -1)
1643  rb_raise(rb_eTypeError, "unexpected protocol");
1644  }
1645  else {
1646  rb_raise(rb_eTypeError, "unexpected protocol");
1647  }
1648  }
1649 
1650  v = rb_ary_entry(ary, 5);
1651  if (NIL_P(v))
1652  canonname = Qnil;
1653  else {
1654  StringValue(v);
1655  canonname = v;
1656  }
1657 
1658  v = rb_ary_entry(ary, 6);
1659  if (NIL_P(v))
1660  inspectname = Qnil;
1661  else {
1662  StringValue(v);
1663  inspectname = v;
1664  }
1665 
1666  v = rb_ary_entry(ary, 1);
1667  switch(afamily) {
1668 #ifdef HAVE_SYS_UN_H
1669  case AF_UNIX:
1670  {
1671  struct sockaddr_un uaddr;
1672  INIT_SOCKADDR_UN(&uaddr, sizeof(struct sockaddr_un));
1673 
1674  StringValue(v);
1675  if (sizeof(uaddr.sun_path) < (size_t)RSTRING_LEN(v))
1677  "too long AF_UNIX path (%"PRIuSIZE" bytes given but %"PRIuSIZE" bytes max)",
1678  (size_t)RSTRING_LEN(v), sizeof(uaddr.sun_path));
1679  memcpy(uaddr.sun_path, RSTRING_PTR(v), RSTRING_LEN(v));
1680  len = (socklen_t)sizeof(uaddr);
1681  memcpy(&ss, &uaddr, len);
1682  break;
1683  }
1684 #endif
1685 
1686  default:
1687  {
1688  VALUE pair = rb_convert_type(v, T_ARRAY, "Array", "to_ary");
1689  struct rb_addrinfo *res;
1690  int flags = AI_NUMERICHOST;
1691 #ifdef AI_NUMERICSERV
1692  flags |= AI_NUMERICSERV;
1693 #endif
1694  res = call_getaddrinfo(rb_ary_entry(pair, 0), rb_ary_entry(pair, 1),
1695  INT2NUM(pfamily), INT2NUM(socktype), INT2NUM(protocol),
1696  INT2NUM(flags), 1);
1697 
1698  len = res->ai->ai_addrlen;
1699  memcpy(&ss, res->ai->ai_addr, res->ai->ai_addrlen);
1700  rb_freeaddrinfo(res);
1701  break;
1702  }
1703  }
1704 
1705  DATA_PTR(self) = rai = alloc_addrinfo();
1706  init_addrinfo(rai, &ss.addr, len,
1707  pfamily, socktype, protocol,
1708  canonname, inspectname);
1709  return self;
1710 }
1711 
1712 /*
1713  * call-seq:
1714  * addrinfo.afamily => integer
1715  *
1716  * returns the address family as an integer.
1717  *
1718  * Addrinfo.tcp("localhost", 80).afamily == Socket::AF_INET #=> true
1719  *
1720  */
1721 static VALUE
1723 {
1724  rb_addrinfo_t *rai = get_addrinfo(self);
1725  return INT2NUM(ai_get_afamily(rai));
1726 }
1727 
1728 /*
1729  * call-seq:
1730  * addrinfo.pfamily => integer
1731  *
1732  * returns the protocol family as an integer.
1733  *
1734  * Addrinfo.tcp("localhost", 80).pfamily == Socket::PF_INET #=> true
1735  *
1736  */
1737 static VALUE
1739 {
1740  rb_addrinfo_t *rai = get_addrinfo(self);
1741  return INT2NUM(rai->pfamily);
1742 }
1743 
1744 /*
1745  * call-seq:
1746  * addrinfo.socktype => integer
1747  *
1748  * returns the socket type as an integer.
1749  *
1750  * Addrinfo.tcp("localhost", 80).socktype == Socket::SOCK_STREAM #=> true
1751  *
1752  */
1753 static VALUE
1755 {
1756  rb_addrinfo_t *rai = get_addrinfo(self);
1757  return INT2NUM(rai->socktype);
1758 }
1759 
1760 /*
1761  * call-seq:
1762  * addrinfo.protocol => integer
1763  *
1764  * returns the socket type as an integer.
1765  *
1766  * Addrinfo.tcp("localhost", 80).protocol == Socket::IPPROTO_TCP #=> true
1767  *
1768  */
1769 static VALUE
1771 {
1772  rb_addrinfo_t *rai = get_addrinfo(self);
1773  return INT2NUM(rai->protocol);
1774 }
1775 
1776 /*
1777  * call-seq:
1778  * addrinfo.to_sockaddr => string
1779  * addrinfo.to_s => string
1780  *
1781  * returns the socket address as packed struct sockaddr string.
1782  *
1783  * Addrinfo.tcp("localhost", 80).to_sockaddr
1784  * #=> "\x02\x00\x00P\x7F\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00"
1785  *
1786  */
1787 static VALUE
1789 {
1790  rb_addrinfo_t *rai = get_addrinfo(self);
1791  VALUE ret;
1792  ret = rb_str_new((char*)&rai->addr, rai->sockaddr_len);
1793  OBJ_INFECT(ret, self);
1794  return ret;
1795 }
1796 
1797 /*
1798  * call-seq:
1799  * addrinfo.canonname => string or nil
1800  *
1801  * returns the canonical name as an string.
1802  *
1803  * nil is returned if no canonical name.
1804  *
1805  * The canonical name is set by Addrinfo.getaddrinfo when AI_CANONNAME is specified.
1806  *
1807  * list = Addrinfo.getaddrinfo("www.ruby-lang.org", 80, :INET, :STREAM, nil, Socket::AI_CANONNAME)
1808  * p list[0] #=> #<Addrinfo: 221.186.184.68:80 TCP carbon.ruby-lang.org (www.ruby-lang.org)>
1809  * p list[0].canonname #=> "carbon.ruby-lang.org"
1810  *
1811  */
1812 static VALUE
1814 {
1815  rb_addrinfo_t *rai = get_addrinfo(self);
1816  return rai->canonname;
1817 }
1818 
1819 /*
1820  * call-seq:
1821  * addrinfo.ip? => true or false
1822  *
1823  * returns true if addrinfo is internet (IPv4/IPv6) address.
1824  * returns false otherwise.
1825  *
1826  * Addrinfo.tcp("127.0.0.1", 80).ip? #=> true
1827  * Addrinfo.tcp("::1", 80).ip? #=> true
1828  * Addrinfo.unix("/tmp/sock").ip? #=> false
1829  *
1830  */
1831 static VALUE
1833 {
1834  rb_addrinfo_t *rai = get_addrinfo(self);
1835  int family = ai_get_afamily(rai);
1836  return IS_IP_FAMILY(family) ? Qtrue : Qfalse;
1837 }
1838 
1839 /*
1840  * call-seq:
1841  * addrinfo.ipv4? => true or false
1842  *
1843  * returns true if addrinfo is IPv4 address.
1844  * returns false otherwise.
1845  *
1846  * Addrinfo.tcp("127.0.0.1", 80).ipv4? #=> true
1847  * Addrinfo.tcp("::1", 80).ipv4? #=> false
1848  * Addrinfo.unix("/tmp/sock").ipv4? #=> false
1849  *
1850  */
1851 static VALUE
1853 {
1854  rb_addrinfo_t *rai = get_addrinfo(self);
1855  return ai_get_afamily(rai) == AF_INET ? Qtrue : Qfalse;
1856 }
1857 
1858 /*
1859  * call-seq:
1860  * addrinfo.ipv6? => true or false
1861  *
1862  * returns true if addrinfo is IPv6 address.
1863  * returns false otherwise.
1864  *
1865  * Addrinfo.tcp("127.0.0.1", 80).ipv6? #=> false
1866  * Addrinfo.tcp("::1", 80).ipv6? #=> true
1867  * Addrinfo.unix("/tmp/sock").ipv6? #=> false
1868  *
1869  */
1870 static VALUE
1872 {
1873 #ifdef AF_INET6
1874  rb_addrinfo_t *rai = get_addrinfo(self);
1875  return ai_get_afamily(rai) == AF_INET6 ? Qtrue : Qfalse;
1876 #else
1877  return Qfalse;
1878 #endif
1879 }
1880 
1881 /*
1882  * call-seq:
1883  * addrinfo.unix? => true or false
1884  *
1885  * returns true if addrinfo is UNIX address.
1886  * returns false otherwise.
1887  *
1888  * Addrinfo.tcp("127.0.0.1", 80).unix? #=> false
1889  * Addrinfo.tcp("::1", 80).unix? #=> false
1890  * Addrinfo.unix("/tmp/sock").unix? #=> true
1891  *
1892  */
1893 static VALUE
1895 {
1896  rb_addrinfo_t *rai = get_addrinfo(self);
1897 #ifdef AF_UNIX
1898  return ai_get_afamily(rai) == AF_UNIX ? Qtrue : Qfalse;
1899 #else
1900  return Qfalse;
1901 #endif
1902 }
1903 
1904 /*
1905  * call-seq:
1906  * addrinfo.getnameinfo => [nodename, service]
1907  * addrinfo.getnameinfo(flags) => [nodename, service]
1908  *
1909  * returns nodename and service as a pair of strings.
1910  * This converts struct sockaddr in addrinfo to textual representation.
1911  *
1912  * flags should be bitwise OR of Socket::NI_??? constants.
1913  *
1914  * Addrinfo.tcp("127.0.0.1", 80).getnameinfo #=> ["localhost", "www"]
1915  *
1916  * Addrinfo.tcp("127.0.0.1", 80).getnameinfo(Socket::NI_NUMERICSERV)
1917  * #=> ["localhost", "80"]
1918  */
1919 static VALUE
1921 {
1922  rb_addrinfo_t *rai = get_addrinfo(self);
1923  VALUE vflags;
1924  char hbuf[1024], pbuf[1024];
1925  int flags, error;
1926 
1927  rb_scan_args(argc, argv, "01", &vflags);
1928 
1929  flags = NIL_P(vflags) ? 0 : NUM2INT(vflags);
1930 
1931  if (rai->socktype == SOCK_DGRAM)
1932  flags |= NI_DGRAM;
1933 
1934  error = getnameinfo(&rai->addr.addr, rai->sockaddr_len,
1935  hbuf, (socklen_t)sizeof(hbuf), pbuf, (socklen_t)sizeof(pbuf),
1936  flags);
1937  if (error) {
1938  rsock_raise_socket_error("getnameinfo", error);
1939  }
1940 
1941  return rb_assoc_new(rb_str_new2(hbuf), rb_str_new2(pbuf));
1942 }
1943 
1944 /*
1945  * call-seq:
1946  * addrinfo.ip_unpack => [addr, port]
1947  *
1948  * Returns the IP address and port number as 2-element array.
1949  *
1950  * Addrinfo.tcp("127.0.0.1", 80).ip_unpack #=> ["127.0.0.1", 80]
1951  * Addrinfo.tcp("::1", 80).ip_unpack #=> ["::1", 80]
1952  */
1953 static VALUE
1955 {
1956  rb_addrinfo_t *rai = get_addrinfo(self);
1957  int family = ai_get_afamily(rai);
1958  VALUE vflags;
1959  VALUE ret, portstr;
1960 
1961  if (!IS_IP_FAMILY(family))
1962  rb_raise(rb_eSocket, "need IPv4 or IPv6 address");
1963 
1965  ret = addrinfo_getnameinfo(1, &vflags, self);
1966  portstr = rb_ary_entry(ret, 1);
1967  rb_ary_store(ret, 1, INT2NUM(atoi(StringValueCStr(portstr))));
1968  return ret;
1969 }
1970 
1971 /*
1972  * call-seq:
1973  * addrinfo.ip_address => string
1974  *
1975  * Returns the IP address as a string.
1976  *
1977  * Addrinfo.tcp("127.0.0.1", 80).ip_address #=> "127.0.0.1"
1978  * Addrinfo.tcp("::1", 80).ip_address #=> "::1"
1979  */
1980 static VALUE
1982 {
1983  rb_addrinfo_t *rai = get_addrinfo(self);
1984  int family = ai_get_afamily(rai);
1985  VALUE vflags;
1986  VALUE ret;
1987 
1988  if (!IS_IP_FAMILY(family))
1989  rb_raise(rb_eSocket, "need IPv4 or IPv6 address");
1990 
1992  ret = addrinfo_getnameinfo(1, &vflags, self);
1993  return rb_ary_entry(ret, 0);
1994 }
1995 
1996 /*
1997  * call-seq:
1998  * addrinfo.ip_port => port
1999  *
2000  * Returns the port number as an integer.
2001  *
2002  * Addrinfo.tcp("127.0.0.1", 80).ip_port #=> 80
2003  * Addrinfo.tcp("::1", 80).ip_port #=> 80
2004  */
2005 static VALUE
2007 {
2008  rb_addrinfo_t *rai = get_addrinfo(self);
2009  int family = ai_get_afamily(rai);
2010  int port;
2011 
2012  if (!IS_IP_FAMILY(family)) {
2013  bad_family:
2014 #ifdef AF_INET6
2015  rb_raise(rb_eSocket, "need IPv4 or IPv6 address");
2016 #else
2017  rb_raise(rb_eSocket, "need IPv4 address");
2018 #endif
2019  }
2020 
2021  switch (family) {
2022  case AF_INET:
2023  if (rai->sockaddr_len != sizeof(struct sockaddr_in))
2024  rb_raise(rb_eSocket, "unexpected sockaddr size for IPv4");
2025  port = ntohs(rai->addr.in.sin_port);
2026  break;
2027 
2028 #ifdef AF_INET6
2029  case AF_INET6:
2030  if (rai->sockaddr_len != sizeof(struct sockaddr_in6))
2031  rb_raise(rb_eSocket, "unexpected sockaddr size for IPv6");
2032  port = ntohs(rai->addr.in6.sin6_port);
2033  break;
2034 #endif
2035 
2036  default:
2037  goto bad_family;
2038  }
2039 
2040  return INT2NUM(port);
2041 }
2042 
2043 static int
2045 {
2046  rb_addrinfo_t *rai = get_addrinfo(self);
2047  int family = ai_get_afamily(rai);
2048  if (family != AF_INET) return 0;
2049  *addrp = ntohl(rai->addr.in.sin_addr.s_addr);
2050  return 1;
2051 }
2052 
2053 /*
2054  * Returns true for IPv4 private address (10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16).
2055  * It returns false otherwise.
2056  */
2057 static VALUE
2059 {
2060  uint32_t a;
2061  if (!extract_in_addr(self, &a)) return Qfalse;
2062  if ((a & 0xff000000) == 0x0a000000 || /* 10.0.0.0/8 */
2063  (a & 0xfff00000) == 0xac100000 || /* 172.16.0.0/12 */
2064  (a & 0xffff0000) == 0xc0a80000) /* 192.168.0.0/16 */
2065  return Qtrue;
2066  return Qfalse;
2067 }
2068 
2069 /*
2070  * Returns true for IPv4 loopback address (127.0.0.0/8).
2071  * It returns false otherwise.
2072  */
2073 static VALUE
2075 {
2076  uint32_t a;
2077  if (!extract_in_addr(self, &a)) return Qfalse;
2078  if ((a & 0xff000000) == 0x7f000000) /* 127.0.0.0/8 */
2079  return Qtrue;
2080  return Qfalse;
2081 }
2082 
2083 /*
2084  * Returns true for IPv4 multicast address (224.0.0.0/4).
2085  * It returns false otherwise.
2086  */
2087 static VALUE
2089 {
2090  uint32_t a;
2091  if (!extract_in_addr(self, &a)) return Qfalse;
2092  if ((a & 0xf0000000) == 0xe0000000) /* 224.0.0.0/4 */
2093  return Qtrue;
2094  return Qfalse;
2095 }
2096 
2097 #ifdef INET6
2098 
2099 static struct in6_addr *
2100 extract_in6_addr(VALUE self)
2101 {
2102  rb_addrinfo_t *rai = get_addrinfo(self);
2103  int family = ai_get_afamily(rai);
2104  if (family != AF_INET6) return NULL;
2105  return &rai->addr.in6.sin6_addr;
2106 }
2107 
2108 /*
2109  * Returns true for IPv6 unspecified address (::).
2110  * It returns false otherwise.
2111  */
2112 static VALUE
2113 addrinfo_ipv6_unspecified_p(VALUE self)
2114 {
2115  struct in6_addr *addr = extract_in6_addr(self);
2116  if (addr && IN6_IS_ADDR_UNSPECIFIED(addr)) return Qtrue;
2117  return Qfalse;
2118 }
2119 
2120 /*
2121  * Returns true for IPv6 loopback address (::1).
2122  * It returns false otherwise.
2123  */
2124 static VALUE
2125 addrinfo_ipv6_loopback_p(VALUE self)
2126 {
2127  struct in6_addr *addr = extract_in6_addr(self);
2128  if (addr && IN6_IS_ADDR_LOOPBACK(addr)) return Qtrue;
2129  return Qfalse;
2130 }
2131 
2132 /*
2133  * Returns true for IPv6 multicast address (ff00::/8).
2134  * It returns false otherwise.
2135  */
2136 static VALUE
2137 addrinfo_ipv6_multicast_p(VALUE self)
2138 {
2139  struct in6_addr *addr = extract_in6_addr(self);
2140  if (addr && IN6_IS_ADDR_MULTICAST(addr)) return Qtrue;
2141  return Qfalse;
2142 }
2143 
2144 /*
2145  * Returns true for IPv6 link local address (ff80::/10).
2146  * It returns false otherwise.
2147  */
2148 static VALUE
2149 addrinfo_ipv6_linklocal_p(VALUE self)
2150 {
2151  struct in6_addr *addr = extract_in6_addr(self);
2152  if (addr && IN6_IS_ADDR_LINKLOCAL(addr)) return Qtrue;
2153  return Qfalse;
2154 }
2155 
2156 /*
2157  * Returns true for IPv6 site local address (ffc0::/10).
2158  * It returns false otherwise.
2159  */
2160 static VALUE
2161 addrinfo_ipv6_sitelocal_p(VALUE self)
2162 {
2163  struct in6_addr *addr = extract_in6_addr(self);
2164  if (addr && IN6_IS_ADDR_SITELOCAL(addr)) return Qtrue;
2165  return Qfalse;
2166 }
2167 
2168 /*
2169  * Returns true for IPv6 unique local address (fc00::/7, RFC4193).
2170  * It returns false otherwise.
2171  */
2172 static VALUE
2173 addrinfo_ipv6_unique_local_p(VALUE self)
2174 {
2175  struct in6_addr *addr = extract_in6_addr(self);
2176  if (addr && IN6_IS_ADDR_UNIQUE_LOCAL(addr)) return Qtrue;
2177  return Qfalse;
2178 }
2179 
2180 /*
2181  * Returns true for IPv4-mapped IPv6 address (::ffff:0:0/80).
2182  * It returns false otherwise.
2183  */
2184 static VALUE
2185 addrinfo_ipv6_v4mapped_p(VALUE self)
2186 {
2187  struct in6_addr *addr = extract_in6_addr(self);
2188  if (addr && IN6_IS_ADDR_V4MAPPED(addr)) return Qtrue;
2189  return Qfalse;
2190 }
2191 
2192 /*
2193  * Returns true for IPv4-compatible IPv6 address (::/80).
2194  * It returns false otherwise.
2195  */
2196 static VALUE
2197 addrinfo_ipv6_v4compat_p(VALUE self)
2198 {
2199  struct in6_addr *addr = extract_in6_addr(self);
2200  if (addr && IN6_IS_ADDR_V4COMPAT(addr)) return Qtrue;
2201  return Qfalse;
2202 }
2203 
2204 /*
2205  * Returns true for IPv6 multicast node-local scope address.
2206  * It returns false otherwise.
2207  */
2208 static VALUE
2209 addrinfo_ipv6_mc_nodelocal_p(VALUE self)
2210 {
2211  struct in6_addr *addr = extract_in6_addr(self);
2212  if (addr && IN6_IS_ADDR_MC_NODELOCAL(addr)) return Qtrue;
2213  return Qfalse;
2214 }
2215 
2216 /*
2217  * Returns true for IPv6 multicast link-local scope address.
2218  * It returns false otherwise.
2219  */
2220 static VALUE
2221 addrinfo_ipv6_mc_linklocal_p(VALUE self)
2222 {
2223  struct in6_addr *addr = extract_in6_addr(self);
2224  if (addr && IN6_IS_ADDR_MC_LINKLOCAL(addr)) return Qtrue;
2225  return Qfalse;
2226 }
2227 
2228 /*
2229  * Returns true for IPv6 multicast site-local scope address.
2230  * It returns false otherwise.
2231  */
2232 static VALUE
2233 addrinfo_ipv6_mc_sitelocal_p(VALUE self)
2234 {
2235  struct in6_addr *addr = extract_in6_addr(self);
2236  if (addr && IN6_IS_ADDR_MC_SITELOCAL(addr)) return Qtrue;
2237  return Qfalse;
2238 }
2239 
2240 /*
2241  * Returns true for IPv6 multicast organization-local scope address.
2242  * It returns false otherwise.
2243  */
2244 static VALUE
2245 addrinfo_ipv6_mc_orglocal_p(VALUE self)
2246 {
2247  struct in6_addr *addr = extract_in6_addr(self);
2248  if (addr && IN6_IS_ADDR_MC_ORGLOCAL(addr)) return Qtrue;
2249  return Qfalse;
2250 }
2251 
2252 /*
2253  * Returns true for IPv6 multicast global scope address.
2254  * It returns false otherwise.
2255  */
2256 static VALUE
2257 addrinfo_ipv6_mc_global_p(VALUE self)
2258 {
2259  struct in6_addr *addr = extract_in6_addr(self);
2260  if (addr && IN6_IS_ADDR_MC_GLOBAL(addr)) return Qtrue;
2261  return Qfalse;
2262 }
2263 
2264 /*
2265  * Returns IPv4 address of IPv4 mapped/compatible IPv6 address.
2266  * It returns nil if +self+ is not IPv4 mapped/compatible IPv6 address.
2267  *
2268  * Addrinfo.ip("::192.0.2.3").ipv6_to_ipv4 #=> #<Addrinfo: 192.0.2.3>
2269  * Addrinfo.ip("::ffff:192.0.2.3").ipv6_to_ipv4 #=> #<Addrinfo: 192.0.2.3>
2270  * Addrinfo.ip("::1").ipv6_to_ipv4 #=> nil
2271  * Addrinfo.ip("192.0.2.3").ipv6_to_ipv4 #=> nil
2272  * Addrinfo.unix("/tmp/sock").ipv6_to_ipv4 #=> nil
2273  */
2274 static VALUE
2275 addrinfo_ipv6_to_ipv4(VALUE self)
2276 {
2277  rb_addrinfo_t *rai = get_addrinfo(self);
2278  struct in6_addr *addr;
2279  int family = ai_get_afamily(rai);
2280  if (family != AF_INET6) return Qnil;
2281  addr = &rai->addr.in6.sin6_addr;
2282  if (IN6_IS_ADDR_V4MAPPED(addr) || IN6_IS_ADDR_V4COMPAT(addr)) {
2283  struct sockaddr_in sin4;
2284  INIT_SOCKADDR_IN(&sin4, sizeof(sin4));
2285  memcpy(&sin4.sin_addr, (char*)addr + sizeof(*addr) - sizeof(sin4.sin_addr), sizeof(sin4.sin_addr));
2286  return rsock_addrinfo_new((struct sockaddr *)&sin4, (socklen_t)sizeof(sin4),
2287  PF_INET, rai->socktype, rai->protocol,
2288  rai->canonname, rai->inspectname);
2289  }
2290  else {
2291  return Qnil;
2292  }
2293 }
2294 
2295 #endif
2296 
2297 #ifdef HAVE_SYS_UN_H
2298 /*
2299  * call-seq:
2300  * addrinfo.unix_path => path
2301  *
2302  * Returns the socket path as a string.
2303  *
2304  * Addrinfo.unix("/tmp/sock").unix_path #=> "/tmp/sock"
2305  */
2306 static VALUE
2307 addrinfo_unix_path(VALUE self)
2308 {
2309  rb_addrinfo_t *rai = get_addrinfo(self);
2310  int family = ai_get_afamily(rai);
2311  struct sockaddr_un *addr;
2312  char *s, *e;
2313 
2314  if (family != AF_UNIX)
2315  rb_raise(rb_eSocket, "need AF_UNIX address");
2316 
2317  addr = &rai->addr.un;
2318 
2319  s = addr->sun_path;
2320  e = (char*)addr + rai->sockaddr_len;
2321  if (e < s)
2322  rb_raise(rb_eSocket, "too short AF_UNIX address: %"PRIuSIZE" bytes given for minimum %"PRIuSIZE" bytes.",
2323  (size_t)rai->sockaddr_len, (size_t)(s - (char *)addr));
2324  if (addr->sun_path + sizeof(addr->sun_path) < e)
2326  "too long AF_UNIX path (%"PRIuSIZE" bytes given but %"PRIuSIZE" bytes max)",
2327  (size_t)(e - addr->sun_path), sizeof(addr->sun_path));
2328  while (s < e && *(e-1) == '\0')
2329  e--;
2330  return rb_str_new(s, e-s);
2331 }
2332 #endif
2333 
2334 /*
2335  * call-seq:
2336  * Addrinfo.getaddrinfo(nodename, service, family, socktype, protocol, flags) => [addrinfo, ...]
2337  * Addrinfo.getaddrinfo(nodename, service, family, socktype, protocol) => [addrinfo, ...]
2338  * Addrinfo.getaddrinfo(nodename, service, family, socktype) => [addrinfo, ...]
2339  * Addrinfo.getaddrinfo(nodename, service, family) => [addrinfo, ...]
2340  * Addrinfo.getaddrinfo(nodename, service) => [addrinfo, ...]
2341  *
2342  * returns a list of addrinfo objects as an array.
2343  *
2344  * This method converts nodename (hostname) and service (port) to addrinfo.
2345  * Since the conversion is not unique, the result is a list of addrinfo objects.
2346  *
2347  * nodename or service can be nil if no conversion intended.
2348  *
2349  * family, socktype and protocol are hint for preferred protocol.
2350  * If the result will be used for a socket with SOCK_STREAM,
2351  * SOCK_STREAM should be specified as socktype.
2352  * If so, Addrinfo.getaddrinfo returns addrinfo list appropriate for SOCK_STREAM.
2353  * If they are omitted or nil is given, the result is not restricted.
2354  *
2355  * Similarly, PF_INET6 as family restricts for IPv6.
2356  *
2357  * flags should be bitwise OR of Socket::AI_??? constants such as follows.
2358  * Note that the exact list of the constants depends on OS.
2359  *
2360  * AI_PASSIVE Get address to use with bind()
2361  * AI_CANONNAME Fill in the canonical name
2362  * AI_NUMERICHOST Prevent host name resolution
2363  * AI_NUMERICSERV Prevent service name resolution
2364  * AI_V4MAPPED Accept IPv4-mapped IPv6 addresses
2365  * AI_ALL Allow all addresses
2366  * AI_ADDRCONFIG Accept only if any address is assigned
2367  *
2368  * Note that socktype should be specified whenever application knows the usage of the address.
2369  * Some platform causes an error when socktype is omitted and servname is specified as an integer
2370  * because some port numbers, 512 for example, are ambiguous without socktype.
2371  *
2372  * Addrinfo.getaddrinfo("www.kame.net", 80, nil, :STREAM)
2373  * #=> [#<Addrinfo: 203.178.141.194:80 TCP (www.kame.net)>,
2374  * # #<Addrinfo: [2001:200:dff:fff1:216:3eff:feb1:44d7]:80 TCP (www.kame.net)>]
2375  *
2376  */
2377 static VALUE
2379 {
2380  VALUE node, service, family, socktype, protocol, flags;
2381 
2382  rb_scan_args(argc, argv, "24", &node, &service, &family, &socktype, &protocol, &flags);
2383  return addrinfo_list_new(node, service, family, socktype, protocol, flags);
2384 }
2385 
2386 /*
2387  * call-seq:
2388  * Addrinfo.ip(host) => addrinfo
2389  *
2390  * returns an addrinfo object for IP address.
2391  *
2392  * The port, socktype, protocol of the result is filled by zero.
2393  * So, it is not appropriate to create a socket.
2394  *
2395  * Addrinfo.ip("localhost") #=> #<Addrinfo: 127.0.0.1 (localhost)>
2396  */
2397 static VALUE
2399 {
2400  VALUE ret;
2401  rb_addrinfo_t *rai;
2402  ret = addrinfo_firstonly_new(host, Qnil,
2403  INT2NUM(PF_UNSPEC), INT2FIX(0), INT2FIX(0), INT2FIX(0));
2404  rai = get_addrinfo(ret);
2405  rai->socktype = 0;
2406  rai->protocol = 0;
2407  return ret;
2408 }
2409 
2410 /*
2411  * call-seq:
2412  * Addrinfo.tcp(host, port) => addrinfo
2413  *
2414  * returns an addrinfo object for TCP address.
2415  *
2416  * Addrinfo.tcp("localhost", "smtp") #=> #<Addrinfo: 127.0.0.1:25 TCP (localhost:smtp)>
2417  */
2418 static VALUE
2419 addrinfo_s_tcp(VALUE self, VALUE host, VALUE port)
2420 {
2421  return addrinfo_firstonly_new(host, port,
2422  INT2NUM(PF_UNSPEC), INT2NUM(SOCK_STREAM), INT2NUM(IPPROTO_TCP), INT2FIX(0));
2423 }
2424 
2425 /*
2426  * call-seq:
2427  * Addrinfo.udp(host, port) => addrinfo
2428  *
2429  * returns an addrinfo object for UDP address.
2430  *
2431  * Addrinfo.udp("localhost", "daytime") #=> #<Addrinfo: 127.0.0.1:13 UDP (localhost:daytime)>
2432  */
2433 static VALUE
2434 addrinfo_s_udp(VALUE self, VALUE host, VALUE port)
2435 {
2436  return addrinfo_firstonly_new(host, port,
2437  INT2NUM(PF_UNSPEC), INT2NUM(SOCK_DGRAM), INT2NUM(IPPROTO_UDP), INT2FIX(0));
2438 }
2439 
2440 #ifdef HAVE_SYS_UN_H
2441 
2442 /*
2443  * call-seq:
2444  * Addrinfo.unix(path [, socktype]) => addrinfo
2445  *
2446  * returns an addrinfo object for UNIX socket address.
2447  *
2448  * _socktype_ specifies the socket type.
2449  * If it is omitted, :STREAM is used.
2450  *
2451  * Addrinfo.unix("/tmp/sock") #=> #<Addrinfo: /tmp/sock SOCK_STREAM>
2452  * Addrinfo.unix("/tmp/sock", :DGRAM) #=> #<Addrinfo: /tmp/sock SOCK_DGRAM>
2453  */
2454 static VALUE
2455 addrinfo_s_unix(int argc, VALUE *argv, VALUE self)
2456 {
2457  VALUE path, vsocktype, addr;
2458  int socktype;
2459  rb_addrinfo_t *rai;
2460 
2461  rb_scan_args(argc, argv, "11", &path, &vsocktype);
2462 
2463  if (NIL_P(vsocktype))
2464  socktype = SOCK_STREAM;
2465  else
2466  socktype = rsock_socktype_arg(vsocktype);
2467 
2469  DATA_PTR(addr) = rai = alloc_addrinfo();
2470  init_unix_addrinfo(rai, path, socktype);
2471  OBJ_INFECT(addr, path);
2472  return addr;
2473 }
2474 
2475 #endif
2476 
2477 VALUE
2479 {
2480  VALUE val = *v;
2481  if (IS_ADDRINFO(val)) {
2482  *v = addrinfo_to_sockaddr(val);
2483  }
2484  StringValue(*v);
2485  return *v;
2486 }
2487 
2488 VALUE
2490 {
2491  VALUE val = *v;
2492  *rai_ret = Qnil;
2493  if (IS_ADDRINFO(val)) {
2494  *v = addrinfo_to_sockaddr(val);
2495  *rai_ret = val;
2496  }
2497  StringValue(*v);
2498  return *v;
2499 }
2500 
2501 char *
2503 {
2505  return RSTRING_PTR(*v);
2506 }
2507 
2508 VALUE
2510 {
2511  if (IS_ADDRINFO(val))
2512  return addrinfo_to_sockaddr(val);
2513  return rb_check_string_type(val);
2514 }
2515 
2516 VALUE
2517 rsock_fd_socket_addrinfo(int fd, struct sockaddr *addr, socklen_t len)
2518 {
2519  int family;
2520  int socktype;
2521  int ret;
2522  socklen_t optlen = (socklen_t)sizeof(socktype);
2523 
2524  /* assumes protocol family and address family are identical */
2525  family = get_afamily(addr, len);
2526 
2527  ret = getsockopt(fd, SOL_SOCKET, SO_TYPE, (void*)&socktype, &optlen);
2528  if (ret == -1) {
2529  rb_sys_fail("getsockopt(SO_TYPE)");
2530  }
2531 
2532  return rsock_addrinfo_new(addr, len, family, socktype, 0, Qnil, Qnil);
2533 }
2534 
2535 VALUE
2536 rsock_io_socket_addrinfo(VALUE io, struct sockaddr *addr, socklen_t len)
2537 {
2538  rb_io_t *fptr;
2539 
2540  switch (TYPE(io)) {
2541  case T_FIXNUM:
2542  return rsock_fd_socket_addrinfo(FIX2INT(io), addr, len);
2543 
2544  case T_BIGNUM:
2545  return rsock_fd_socket_addrinfo(NUM2INT(io), addr, len);
2546 
2547  case T_FILE:
2548  GetOpenFile(io, fptr);
2549  return rsock_fd_socket_addrinfo(fptr->fd, addr, len);
2550 
2551  default:
2552  rb_raise(rb_eTypeError, "neither IO nor file descriptor");
2553  }
2554 
2555  UNREACHABLE;
2556 }
2557 
2558 /*
2559  * Addrinfo class
2560  */
2561 void
2563 {
2564  /*
2565  * The Addrinfo class maps <tt>struct addrinfo</tt> to ruby. This
2566  * structure identifies an Internet host and a service.
2567  */
2568  rb_cAddrinfo = rb_define_class("Addrinfo", rb_cData);
2577 #ifdef HAVE_SYS_UN_H
2578  rb_define_singleton_method(rb_cAddrinfo, "unix", addrinfo_s_unix, -1);
2579 #endif
2580 
2586 
2590 
2595 
2599 
2600 #ifdef INET6
2601  rb_define_method(rb_cAddrinfo, "ipv6_unspecified?", addrinfo_ipv6_unspecified_p, 0);
2602  rb_define_method(rb_cAddrinfo, "ipv6_loopback?", addrinfo_ipv6_loopback_p, 0);
2603  rb_define_method(rb_cAddrinfo, "ipv6_multicast?", addrinfo_ipv6_multicast_p, 0);
2604  rb_define_method(rb_cAddrinfo, "ipv6_linklocal?", addrinfo_ipv6_linklocal_p, 0);
2605  rb_define_method(rb_cAddrinfo, "ipv6_sitelocal?", addrinfo_ipv6_sitelocal_p, 0);
2606  rb_define_method(rb_cAddrinfo, "ipv6_unique_local?", addrinfo_ipv6_unique_local_p, 0);
2607  rb_define_method(rb_cAddrinfo, "ipv6_v4mapped?", addrinfo_ipv6_v4mapped_p, 0);
2608  rb_define_method(rb_cAddrinfo, "ipv6_v4compat?", addrinfo_ipv6_v4compat_p, 0);
2609  rb_define_method(rb_cAddrinfo, "ipv6_mc_nodelocal?", addrinfo_ipv6_mc_nodelocal_p, 0);
2610  rb_define_method(rb_cAddrinfo, "ipv6_mc_linklocal?", addrinfo_ipv6_mc_linklocal_p, 0);
2611  rb_define_method(rb_cAddrinfo, "ipv6_mc_sitelocal?", addrinfo_ipv6_mc_sitelocal_p, 0);
2612  rb_define_method(rb_cAddrinfo, "ipv6_mc_orglocal?", addrinfo_ipv6_mc_orglocal_p, 0);
2613  rb_define_method(rb_cAddrinfo, "ipv6_mc_global?", addrinfo_ipv6_mc_global_p, 0);
2614 
2615  rb_define_method(rb_cAddrinfo, "ipv6_to_ipv4", addrinfo_ipv6_to_ipv4, 0);
2616 #endif
2617 
2618 #ifdef HAVE_SYS_UN_H
2619  rb_define_method(rb_cAddrinfo, "unix_path", addrinfo_unix_path, 0);
2620 #endif
2621 
2623  rb_define_method(rb_cAddrinfo, "to_s", addrinfo_to_sockaddr, 0); /* compatibility for ruby before 1.9.2 */
2624 
2625  rb_define_method(rb_cAddrinfo, "getnameinfo", addrinfo_getnameinfo, -1);
2626 
2627  rb_define_method(rb_cAddrinfo, "marshal_dump", addrinfo_mdump, 0);
2628  rb_define_method(rb_cAddrinfo, "marshal_load", addrinfo_mload, 1);
2629 }
VALUE inspectname
Definition: raddrinfo.c:716
#define IPPROTO_UDP
Definition: constdefs.h:627
RUBY_EXTERN VALUE rb_cData
Definition: ruby.h:1881
VALUE rb_ary_entry(VALUE ary, long offset)
Definition: array.c:1196
#define SafeStringValueCStr(v)
Definition: raddrinfo.c:432
VALUE rb_str_equal(VALUE str1, VALUE str2)
Definition: string.c:3105
static int str_is_number(const char *)
Definition: raddrinfo.c:415
size_t strlen(const char *)
#define INT2NUM(x)
Definition: ruby.h:1538
VALUE rsock_inspect_sockaddr(struct sockaddr *sockaddr_arg, socklen_t socklen, VALUE ret)
Definition: raddrinfo.c:1143
#define T_FIXNUM
Definition: ruby.h:503
#define PF_INET
Definition: sockport.h:109
size_t servlen
Definition: raddrinfo.c:348
#define NUM2INT(x)
Definition: ruby.h:684
#define NUM2UINT(x)
Definition: ruby.h:685
void rb_define_singleton_method(VALUE obj, const char *name, VALUE(*func)(ANYARGS), int argc)
Defines a singleton method for obj.
Definition: class.c:1716
static rb_addrinfo_t * check_addrinfo(VALUE self)
Definition: raddrinfo.c:756
VALUE rb_str_cat(VALUE, const char *, long)
Definition: string.c:2664
#define NI_DGRAM
Definition: addrinfo.h:128
#define Qtrue
Definition: ruby.h:437
static VALUE addrinfo_ipv6_p(VALUE self)
Definition: raddrinfo.c:1871
VALUE rsock_make_ipaddr(struct sockaddr *addr, socklen_t addrlen)
Definition: raddrinfo.c:396
Definition: io.h:62
#define TypedData_Wrap_Struct(klass, data_type, sval)
Definition: ruby.h:1169
int rsock_socktype_arg(VALUE type)
Definition: constants.c:50
static VALUE addrinfo_list_new(VALUE node, VALUE service, VALUE family, VALUE socktype, VALUE protocol, VALUE flags)
Definition: raddrinfo.c:937
#define rb_id2str(id)
Definition: vm_backtrace.c:29
const int id
Definition: nkf.c:209
VALUE rb_eTypeError
Definition: error.c:762
#define UNREACHABLE
Definition: ruby.h:46
const struct sockaddr * sa
Definition: raddrinfo.c:342
st_table * names
Definition: encoding.c:58
VALUE rb_ary_push(VALUE ary, VALUE item)
Definition: array.c:905
static void * nogvl_getaddrinfo(void *arg)
Definition: raddrinfo.c:186
if(len<=MAX_WORD_LENGTH &&len >=MIN_WORD_LENGTH)
Definition: zonetab.h:883
static VALUE addrinfo_ipv4_loopback_p(VALUE self)
Definition: raddrinfo.c:2074
void rb_raise(VALUE exc, const char *fmt,...)
Definition: error.c:2207
ID rsock_intern_ipproto(int val)
Definition: constdefs.c:6772
#define IN6_IS_ADDR_UNIQUE_LOCAL(a)
Definition: rubysocket.h:160
#define RSTRING_GETMEM(str, ptrvar, lenvar)
Definition: ruby.h:991
static VALUE addrinfo_protocol(VALUE self)
Definition: raddrinfo.c:1770
VALUE rb_convert_type(VALUE, int, const char *, const char *)
Definition: object.c:2630
static void make_inetaddr(unsigned int host, char *buf, size_t buflen)
Definition: raddrinfo.c:405
void rb_define_alloc_func(VALUE, rb_alloc_func_t)
VALUE rb_obj_is_kind_of(VALUE, VALUE)
Definition: object.c:690
VALUE rb_check_sockaddr_string_type(VALUE val)
Definition: raddrinfo.c:2509
#define INADDR_BROADCAST
Definition: constdefs.h:747
#define DATA_PTR(dta)
Definition: ruby.h:1113
void rb_gc_mark(VALUE ptr)
Definition: gc.c:4394
static VALUE addrinfo_ipv4_p(VALUE self)
Definition: raddrinfo.c:1852
#define T_ARRAY
Definition: ruby.h:498
VALUE rb_tainted_str_new_cstr(const char *)
Definition: string.c:871
const char * service
Definition: raddrinfo.c:152
static size_t addrinfo_memsize(const void *ptr)
Definition: raddrinfo.c:738
VALUE rsock_ipaddr(struct sockaddr *sockaddr, socklen_t sockaddrlen, int norevlookup)
Definition: raddrinfo.c:559
const struct addrinfo * hints
Definition: raddrinfo.c:153
#define FIXNUM_P(f)
Definition: ruby.h:365
#define GetOpenFile(obj, fp)
Definition: io.h:120
void rsock_init_addrinfo(void)
Definition: raddrinfo.c:2562
VALUE rsock_io_socket_addrinfo(VALUE io, struct sockaddr *addr, socklen_t len)
Definition: raddrinfo.c:2536
const char * rb_obj_classname(VALUE)
Definition: variable.c:458
#define rb_ary_new2
Definition: intern.h:90
static VALUE addrinfo_ipv4_multicast_p(VALUE self)
Definition: raddrinfo.c:2088
#define NI_MAXSERV
Definition: addrinfo.h:118
void rb_freeaddrinfo(struct rb_addrinfo *ai)
Definition: raddrinfo.c:322
static VALUE addrinfo_to_sockaddr(VALUE self)
Definition: raddrinfo.c:1788
VALUE rsock_make_hostent(VALUE host, struct rb_addrinfo *addr, VALUE(*ipaddr)(struct sockaddr *, socklen_t))
Definition: raddrinfo.c:704
#define IS_ADDRINFO(obj)
Definition: raddrinfo.c:754
#define RB_TYPE_P(obj, type)
Definition: ruby.h:527
VALUE rsock_addrinfo_new(struct sockaddr *addr, socklen_t len, int family, int socktype, int protocol, VALUE canonname, VALUE inspectname)
Definition: raddrinfo.c:800
#define MEMZERO(p, type, n)
Definition: ruby.h:1660
VALUE rsock_sockaddr_string_value_with_addrinfo(volatile VALUE *v, VALUE *rai_ret)
Definition: raddrinfo.c:2489
struct rb_addrinfo * rsock_addrinfo(VALUE host, VALUE port, int family, int socktype, int flags)
Definition: raddrinfo.c:547
int rsock_ipproto_to_int(const char *str, long len, int *valp)
Definition: constdefs.c:4932
#define RSTRING_SOCKLEN
Definition: rubysocket.h:124
int rsock_family_arg(VALUE domain)
Definition: constants.c:43
#define val
socklen_t salen
Definition: raddrinfo.c:343
VALUE rb_str_cat2(VALUE, const char *)
VALUE rb_ary_new(void)
Definition: array.c:493
static int ai_get_afamily(rb_addrinfo_t *rai)
Definition: raddrinfo.c:1128
VALUE rb_str_buf_cat2(VALUE, const char *)
static VALUE addrinfo_initialize(int argc, VALUE *argv, VALUE self)
Definition: raddrinfo.c:1039
#define STRTOUL(str, endptr, base)
Definition: ruby.h:2141
#define NI_NUMERICSERV
Definition: addrinfo.h:127
#define snprintf
Definition: subst.h:6
#define NIL_P(v)
Definition: ruby.h:451
#define IPPROTO_TCP
Definition: constdefs.h:610
static VALUE addrinfo_ip_unpack(VALUE self)
Definition: raddrinfo.c:1954
VALUE rb_define_class(const char *name, VALUE super)
Defines a top-level class.
Definition: class.c:646
static VALUE addrinfo_getnameinfo(int argc, VALUE *argv, VALUE self)
Definition: raddrinfo.c:1920
static char * host_str(VALUE host, char *hbuf, size_t hbuflen, int *flags_ptr)
Definition: raddrinfo.c:438
int fd
Definition: io.h:64
char * ai_canonname
Definition: addrinfo.h:137
struct rb_addrinfo * rsock_getaddrinfo(VALUE host, VALUE port, struct addrinfo *hints, int socktype_hack)
Definition: raddrinfo.c:506
void rb_ary_store(VALUE ary, long idx, VALUE val)
Definition: array.c:799
#define offsetof(p_type, field)
Definition: addrinfo.h:186
static const rb_data_type_t addrinfo_type
Definition: raddrinfo.c:743
static struct rb_addrinfo * call_getaddrinfo(VALUE node, VALUE service, VALUE family, VALUE socktype, VALUE protocol, VALUE flags, int socktype_hack)
Definition: raddrinfo.c:814
#define TYPE(x)
Definition: ruby.h:521
int argc
Definition: ruby.c:183
int getaddrinfo(const char *hostname, const char *servname, const struct addrinfo *hints, struct addrinfo **res)
Definition: getaddrinfo.c:270
ID rsock_intern_protocol_family(int val)
Definition: constdefs.c:6754
#define Qfalse
Definition: ruby.h:436
#define T_BIGNUM
Definition: ruby.h:501
#define MEMCPY(p1, p2, type, n)
Definition: ruby.h:1661
#define rb_str_new2
Definition: intern.h:857
#define EAI_SYSTEM
Definition: addrinfo.h:88
#define OBJ_FREEZE(x)
Definition: ruby.h:1308
int rsock_family_to_int(const char *str, long len, int *valp)
Definition: constdefs.c:4468
static VALUE addrinfo_s_udp(VALUE self, VALUE host, VALUE port)
Definition: raddrinfo.c:2434
void * rb_thread_call_without_gvl(void *(*func)(void *), void *data1, rb_unblock_function_t *ubf, void *data2)
static int get_afamily(struct sockaddr *addr, socklen_t len)
Definition: raddrinfo.c:1119
#define numberof(array)
Definition: etc.c:616
static VALUE addrinfo_unix_p(VALUE self)
Definition: raddrinfo.c:1894
VALUE rsock_freeaddrinfo(VALUE arg)
Definition: raddrinfo.c:696
static void init_addrinfo_getaddrinfo(rb_addrinfo_t *rai, VALUE node, VALUE service, VALUE family, VALUE socktype, VALUE protocol, VALUE flags, VALUE inspectnode, VALUE inspectservice)
Definition: raddrinfo.c:843
int rsock_socktype_to_int(const char *str, long len, int *valp)
Definition: constdefs.c:4862
#define ZALLOC(type)
Definition: ruby.h:1590
#define IS_IP_FAMILY(af)
Definition: rubysocket.h:156
#define RSTRING_LEN(str)
Definition: ruby.h:978
char * rsock_sockaddr_string_value_ptr(volatile VALUE *v)
Definition: raddrinfo.c:2502
int errno
int socklen_t
Definition: getaddrinfo.c:83
VALUE rb_sprintf(const char *format,...)
Definition: sprintf.c:1440
static void * nogvl_getnameinfo(void *arg)
Definition: raddrinfo.c:352
static VALUE addrinfo_firstonly_new(VALUE node, VALUE service, VALUE family, VALUE socktype, VALUE protocol, VALUE flags)
Definition: raddrinfo.c:911
static VALUE addrinfo_s_getaddrinfo(int argc, VALUE *argv, VALUE self)
Definition: raddrinfo.c:2378
void freeaddrinfo(struct addrinfo *ai)
Definition: getaddrinfo.c:214
#define NI_NUMERICHOST
Definition: addrinfo.h:125
VALUE rsock_fd_socket_addrinfo(int fd, struct sockaddr *addr, socklen_t len)
Definition: raddrinfo.c:2517
int rb_scan_args(int argc, const VALUE *argv, const char *fmt,...)
Definition: class.c:1919
static VALUE addrinfo_mload(VALUE self, VALUE ary)
Definition: raddrinfo.c:1603
unsigned char buf[MIME_BUF_SIZE]
Definition: nkf.c:4309
VALUE rb_assoc_new(VALUE car, VALUE cdr)
Definition: array.c:623
unsigned long ID
Definition: ruby.h:86
static rb_addrinfo_t * alloc_addrinfo(void)
Definition: raddrinfo.c:774
static VALUE addrinfo_pfamily(VALUE self)
Definition: raddrinfo.c:1738
socklen_t sockaddr_len
Definition: raddrinfo.c:721
static VALUE addrinfo_ip_address(VALUE self)
Definition: raddrinfo.c:1981
#define Qnil
Definition: ruby.h:438
ID rsock_intern_family(int val)
Definition: constdefs.c:6736
static VALUE addrinfo_s_ip(VALUE self, VALUE host)
Definition: raddrinfo.c:2398
static char * port_str(VALUE port, char *pbuf, size_t pbuflen, int *flags_ptr)
Definition: raddrinfo.c:477
VALUE host
Definition: raddrinfo.c:646
unsigned long VALUE
Definition: ruby.h:85
#define INIT_SOCKADDR_IN6(addr, len)
Definition: sockport.h:56
#define AI_NUMERICHOST
Definition: addrinfo.h:98
RUBY_EXTERN VALUE rb_cInteger
Definition: ruby.h:1891
#define FIX2INT(x)
Definition: ruby.h:686
#define addrinfo_free
Definition: raddrinfo.c:735
union_sockaddr addr
Definition: raddrinfo.c:722
#define rb_ary_new3
Definition: intern.h:91
const char * rb_id2name(ID)
Definition: symbol.c:759
VALUE rb_ensure(VALUE(*b_proc)(ANYARGS), VALUE data1, VALUE(*e_proc)(ANYARGS), VALUE data2)
Definition: eval.c:923
VALUE rb_str_new_cstr(const char *)
Definition: string.c:770
ID rsock_intern_socktype(int val)
Definition: constdefs.c:6763
static VALUE addrinfo_ip_p(VALUE self)
Definition: raddrinfo.c:1832
void rb_sys_fail(const char *mesg)
Definition: error.c:2326
VALUE rb_str_dup(VALUE)
Definition: string.c:1436
static VALUE addrinfo_s_allocate(VALUE klass)
Definition: raddrinfo.c:749
static void init_addrinfo(rb_addrinfo_t *rai, struct sockaddr *sa, socklen_t len, int pfamily, int socktype, int protocol, VALUE canonname, VALUE inspectname)
Definition: raddrinfo.c:783
static int inet_pton(int af, const char *hostname, void *pton)
Definition: getaddrinfo.c:242
static void make_ipaddr0(struct sockaddr *addr, socklen_t addrlen, char *buf, size_t buflen)
Definition: raddrinfo.c:385
static VALUE make_hostent_internal(struct hostent_arg *arg)
Definition: raddrinfo.c:652
static int numeric_getaddrinfo(const char *node, const char *service, const struct addrinfo *hints, struct addrinfo **res)
Definition: raddrinfo.c:203
unsigned int uint32_t
Definition: sha2.h:101
register unsigned int len
Definition: zonetab.h:51
int ai_protocol
Definition: addrinfo.h:135
static VALUE addrinfo_afamily(VALUE self)
Definition: raddrinfo.c:1722
#define StringValueCStr(v)
Definition: ruby.h:571
int rb_getnameinfo(const struct sockaddr *sa, socklen_t salen, char *host, size_t hostlen, char *serv, size_t servlen, int flags)
Definition: raddrinfo.c:363
#define RSTRING_PTR(str)
Definition: ruby.h:982
VALUE rb_eSocket
Definition: init.c:25
#define AI_NUMERICSERV
Definition: addrinfo.h:99
int rsock_fd_family(int fd)
Definition: raddrinfo.c:534
static VALUE inspect_sockaddr(VALUE addrinfo, VALUE ret)
Definition: raddrinfo.c:1134
struct sockaddr_in in
Definition: rubysocket.h:188
static VALUE addrinfo_canonname(VALUE self)
Definition: raddrinfo.c:1813
#define INIT_SOCKADDR_IN(addr, len)
Definition: sockport.h:47
#define EAI_FAIL
Definition: addrinfo.h:81
#define INADDR_ANY
Definition: constdefs.h:740
#define INT2FIX(i)
Definition: ruby.h:232
static VALUE addrinfo_ipv4_private_p(VALUE self)
Definition: raddrinfo.c:2058
void * rb_check_typeddata(VALUE obj, const rb_data_type_t *data_type)
Definition: error.c:730
#define PF_UNSPEC
Definition: sockport.h:105
#define xmalloc
Definition: defines.h:183
int ai_socktype
Definition: addrinfo.h:134
#define SOCKLEN_MAX
Definition: rubysocket.h:118
VALUE rb_check_array_type(VALUE ary)
Definition: array.c:635
void rsock_raise_socket_error(const char *reason, int error)
Definition: init.c:35
VALUE canonname
Definition: raddrinfo.c:717
size_t hostlen
Definition: raddrinfo.c:346
VALUE rb_str_catf(VALUE str, const char *format,...)
Definition: sprintf.c:1480
VALUE rb_check_string_type(VALUE)
Definition: string.c:2164
struct addrinfo * ai
Definition: rubysocket.h:285
static VALUE addrinfo_s_tcp(VALUE self, VALUE host, VALUE port)
Definition: raddrinfo.c:2419
VALUE rb_cAddrinfo
Definition: init.c:23
#define T_STRING
Definition: ruby.h:496
#define AF_UNSPEC
Definition: sockport.h:101
#define PRIuSIZE
Definition: ruby.h:177
#define OBJ_INFECT(x, s)
Definition: ruby.h:1304
struct rb_encoding_entry * list
Definition: encoding.c:55
int getnameinfo(const struct sockaddr *sa, socklen_t salen, char *host, socklen_t hostlen, char *serv, socklen_t servlen, int flags)
Definition: getnameinfo.c:122
#define T_FILE
Definition: ruby.h:502
int rb_getaddrinfo(const char *node, const char *service, const struct addrinfo *hints, struct rb_addrinfo **res)
Definition: raddrinfo.c:288
#define ISPRINT(c)
Definition: ruby.h:2122
struct addrinfo ** res
Definition: raddrinfo.c:154
struct addrinfo * ai_next
Definition: addrinfo.h:139
static VALUE addrinfo_socktype(VALUE self)
Definition: raddrinfo.c:1754
const char * name
Definition: nkf.c:208
static void addrinfo_mark(void *ptr)
Definition: raddrinfo.c:726
#define IFNAMSIZ
#define EAI_NONAME
Definition: addrinfo.h:85
size_t ai_addrlen
Definition: addrinfo.h:136
#define memcpy(d, s, n)
Definition: ffi_common.h:55
static VALUE make_inspectname(VALUE node, VALUE service, struct addrinfo *res)
Definition: raddrinfo.c:865
void void xfree(void *)
int ai_flags
Definition: addrinfo.h:132
static VALUE addrinfo_ip_port(VALUE self)
Definition: raddrinfo.c:2006
struct rb_addrinfo * addr
Definition: raddrinfo.c:647
#define NULL
Definition: _sdbm.c:102
#define FIX2LONG(x)
Definition: ruby.h:363
#define NI_MAXHOST
Definition: addrinfo.h:117
VALUE rsock_addrinfo_inspect_sockaddr(VALUE self)
Definition: raddrinfo.c:1519
const char * node
Definition: raddrinfo.c:151
static VALUE addrinfo_mdump(VALUE self)
Definition: raddrinfo.c:1526
static rb_addrinfo_t * get_addrinfo(VALUE self)
Definition: raddrinfo.c:762
void rb_define_method(VALUE klass, const char *name, VALUE(*func)(ANYARGS), int argc)
Definition: class.c:1515
static VALUE addrinfo_inspect(VALUE self)
Definition: raddrinfo.c:1439
int allocated_by_malloc
Definition: rubysocket.h:286
struct sockaddr * ai_addr
Definition: addrinfo.h:138
VALUE(* ipaddr)(struct sockaddr *, socklen_t)
Definition: raddrinfo.c:648
VALUE rb_eArgError
Definition: error.c:763
static int extract_in_addr(VALUE self, uint32_t *addrp)
Definition: raddrinfo.c:2044
char ** argv
Definition: ruby.c:184
VALUE rsock_sockaddr_string_value(volatile VALUE *v)
Definition: raddrinfo.c:2478
#define ISSPACE(c)
Definition: ruby.h:2124
#define StringValue(v)
Definition: ruby.h:569
#define RUBY_UBF_IO
Definition: intern.h:900
struct sockaddr addr
Definition: rubysocket.h:187
#define str_equal(ptr, len, name)
Definition: raddrinfo.c:429
VALUE rb_str_new(const char *, long)
Definition: string.c:736
#define xcalloc
Definition: defines.h:185
int ai_family
Definition: addrinfo.h:133