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