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) 18 #elif defined(LOOKUP_ORDER_HACK_INET6) 26 ruby_getaddrinfo(
const char *nodename,
const char *servname,
36 for (i = 0; i < LOOKUP_ORDERS; i++) {
37 af = lookup_order_table[i];
39 tmp_hints.ai_family = af;
40 error =
getaddrinfo(nodename, servname, &tmp_hints, res);
53 #define getaddrinfo(node,serv,hints,res) ruby_getaddrinfo((node),(serv),(hints),(res)) 58 ruby_getaddrinfo__aix(
const char *nodename,
const char *servname,
61 int error =
getaddrinfo(nodename, servname, hints, res);
74 #define getaddrinfo(node,serv,hints,res) ruby_getaddrinfo__aix((node),(serv),(hints),(res)) 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)
80 struct sockaddr_in6 *sa6;
83 if (sa->sa_family == AF_INET6) {
84 sa6 = (
struct sockaddr_in6 *)sa;
85 a6 = sa6->sin6_addr.u6_addr.u6_addr32;
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);
93 return getnameinfo(sa, salen, host, hostlen, serv, servlen, flags);
96 #define getnameinfo(sa, salen, host, hostlen, serv, servlen, flags) \ 97 ruby_getnameinfo__aix((sa), (salen), (host), (hostlen), (serv), (servlen), (flags)) 100 static int str_is_number(
const char *);
102 #if defined(__APPLE__) 104 ruby_getaddrinfo__darwin(
const char *nodename,
const char *servname,
108 const char *tmp_servname;
112 tmp_servname = servname;
114 if (nodename && servname) {
115 if (str_is_number(tmp_servname) && atoi(servname) == 0) {
117 #ifdef AI_NUMERICSERV 123 error =
getaddrinfo(nodename, tmp_servname, &tmp_hints, res);
145 #define getaddrinfo(node,serv,hints,res) ruby_getaddrinfo__darwin((node),(serv),(hints),(res)) 148 #ifndef GETADDRINFO_EMU 157 #ifdef HAVE_INET_PTON 159 parse_numeric_port(
const char *
service,
int *portp)
168 if (strspn(service,
"0123456789") !=
strlen(service))
186 nogvl_getaddrinfo(
void *arg)
198 return (
void *)(
VALUE)ret;
203 numeric_getaddrinfo(
const char *
node,
const char *
service,
207 #ifdef HAVE_INET_PTON 208 # if defined __MINGW64__ 209 # define inet_pton(f,s,d) rb_w32_inet_pton(f,s,d) 214 if (node && parse_numeric_port(service, &port)) {
215 static const struct {
225 int hint_socktype = hints ? hints->
ai_socktype : 0;
226 int hint_protocol = hints ? hints->
ai_protocol : 0;
230 if ((hint_family ==
PF_UNSPEC || hint_family == PF_INET6) &&
231 strspn(node,
"0123456789abcdefABCDEF.:") ==
strlen(node) &&
232 inet_pton(AF_INET6, node, ipv6addr)) {
235 if ((hint_socktype == 0 || hint_socktype ==
list[i].socktype) &&
236 (hint_protocol == 0 ||
list[i].protocol == 0 || hint_protocol ==
list[i].protocol)) {
238 struct sockaddr_in6 *sa =
xmalloc(
sizeof(
struct sockaddr_in6));
240 memcpy(&sa->sin6_addr, ipv6addr,
sizeof(ipv6addr));
241 sa->sin6_port = htons(port);
245 ai0->
ai_addrlen =
sizeof(
struct sockaddr_in6);
246 ai0->
ai_addr = (
struct sockaddr *)sa;
256 strspn(node,
"0123456789.") ==
strlen(node) &&
257 inet_pton(AF_INET, node, ipv4addr)) {
260 if ((hint_socktype == 0 || hint_socktype ==
list[i].socktype) &&
261 (hint_protocol == 0 ||
list[i].protocol == 0 || hint_protocol ==
list[i].protocol)) {
263 struct sockaddr_in *sa =
xmalloc(
sizeof(
struct sockaddr_in));
265 memcpy(&sa->sin_addr, ipv4addr,
sizeof(ipv4addr));
266 sa->sin_port = htons(port);
271 ai0->
ai_addr = (
struct sockaddr *)sa;
294 int allocated_by_malloc = 0;
296 ret = numeric_getaddrinfo(node, service, hints, &ai);
298 allocated_by_malloc = 1;
300 #ifdef GETADDRINFO_EMU 339 #ifndef GETADDRINFO_EMU 342 const struct sockaddr *
sa;
352 nogvl_getnameinfo(
void *arg)
364 char *host,
size_t hostlen,
365 char *serv,
size_t servlen,
int flags)
367 #ifdef GETADDRINFO_EMU 368 return getnameinfo(sa, salen, host, hostlen, serv, servlen, flags);
385 make_ipaddr0(
struct sockaddr *addr,
socklen_t addrlen,
char *
buf,
size_t buflen)
400 make_ipaddr0(addr, addrlen, hbuf,
sizeof(hbuf));
405 make_inetaddr(
unsigned int host,
char *buf,
size_t buflen)
407 struct sockaddr_in sin;
410 sin.sin_addr.s_addr = host;
411 make_ipaddr0((
struct sockaddr*)&sin,
sizeof(sin), buf, buflen);
415 str_is_number(
const char *p)
419 if (!p || *p ==
'\0')
423 if (ep && *ep ==
'\0')
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 {\ 434 rb_check_safe_obj(v);\ 438 host_str(
VALUE host,
char *hbuf,
size_t hbuflen,
int *flags_ptr)
446 make_inetaddr(htonl(i), hbuf, hbuflen);
456 if (!len ||
str_equal(name, len,
"<any>")) {
460 else if (
str_equal(name, len,
"<broadcast>")) {
464 else if (len >= hbuflen) {
469 memcpy(hbuf, name, len);
477 port_str(
VALUE port,
char *pbuf,
size_t pbuflen,
int *flags_ptr)
484 #ifdef AI_NUMERICSERV 495 if (len >= pbuflen) {
499 memcpy(pbuf, serv, len);
512 int additional_flags = 0;
514 hostp = host_str(host, hbuf,
sizeof(hbuf), &additional_flags);
515 portp = port_str(port, pbuf,
sizeof(pbuf), &additional_flags);
517 if (socktype_hack && hints->
ai_socktype == 0 && str_is_number(portp)) {
520 hints->
ai_flags |= additional_flags;
524 if (hostp && hostp[
strlen(hostp)-1] ==
'\n') {
536 struct sockaddr sa = { 0 };
539 if (fd < 0 || getsockname(fd, &sa, &sa_len) != 0 ||
540 (size_t)sa_len <
offsetof(
struct sockaddr, sa_family) +
sizeof(sa.sa_family)) {
561 VALUE family, port, addr1, addr2;
564 char hbuf[1024], pbuf[1024];
572 sprintf(pbuf,
"unknown:%d", sockaddr->sa_family);
601 rsock_unixpath_str(
struct sockaddr_un *sockaddr,
socklen_t len)
604 s = sockaddr->sun_path;
605 e = (
char *)sockaddr + len;
606 while (s < e && *(e-1) ==
'\0')
615 rsock_unixaddr(
struct sockaddr_un *sockaddr,
socklen_t len)
618 rsock_unixpath_str(sockaddr, len));
622 rsock_unix_sockaddr_len(
VALUE path)
638 return (
socklen_t)
sizeof(
struct sockaddr_un);
670 hostp = host_str(host, hbuf,
sizeof(hbuf),
NULL);
677 if (h->h_aliases !=
NULL) {
678 for (pch = h->h_aliases; *pch; pch++) {
688 for (ai = addr; ai; ai = ai->
ai_next) {
689 rb_ary_push(ary, (*ipaddr)(ai->ai_addr, ai->ai_addrlen));
726 addrinfo_mark(
void *ptr)
733 #define addrinfo_free RUBY_TYPED_DEFAULT_FREE 736 addrinfo_memsize(
const void *ptr)
747 addrinfo_s_allocate(
VALUE klass)
752 #define IS_ADDRINFO(obj) rb_typeddata_is_kind_of((obj), &addrinfo_type) 754 check_addrinfo(
VALUE self)
760 get_addrinfo(
VALUE self)
782 int pfamily,
int socktype,
int protocol,
787 memcpy((
void *)&rai->
addr, (
void *)sa, len);
799 int family,
int socktype,
int protocol,
806 DATA_PTR(a) = rai = alloc_addrinfo();
807 init_addrinfo(rai, addr, len, family, socktype, protocol, canonname, inspectname);
822 if (!
NIL_P(socktype)) {
825 if (!
NIL_P(protocol)) {
845 struct rb_addrinfo *res = call_getaddrinfo(node, service, family, socktype, protocol, flags, 1);
847 VALUE inspectname =
rb_str_equal(node, inspectnode) ?
Qnil : make_inspectname(inspectnode, inspectservice, res->
ai);
857 canonname, inspectname);
872 sizeof(hbuf), pbuf,
sizeof(pbuf),
888 if (
NIL_P(inspectname))
895 if (
NIL_P(inspectname))
900 if (!
NIL_P(inspectname)) {
915 struct rb_addrinfo *res = call_getaddrinfo(node, service, family, socktype, protocol, flags, 0);
917 inspectname = make_inspectname(node, service, res->
ai);
928 canonname, inspectname);
941 struct rb_addrinfo *res = call_getaddrinfo(node, service, family, socktype, protocol, flags, 0);
943 inspectname = make_inspectname(node, service, res->
ai);
957 canonname, inspectname);
971 struct sockaddr_un un;
976 if (
sizeof(un.sun_path) < (
size_t)
RSTRING_LEN(path))
978 "too long unix socket path (%"PRIuSIZE" bytes given but %"PRIuSIZE" bytes max)",
981 INIT_SOCKADDR_UN(&un,
sizeof(
struct sockaddr_un));
984 len = rsock_unix_sockaddr_len(path);
985 init_addrinfo(rai, (
struct sockaddr *)&un, len,
1040 VALUE sockaddr_arg, sockaddr_ary, pfamily, socktype, protocol;
1041 int i_pfamily, i_socktype, i_protocol;
1042 struct sockaddr *sockaddr_ptr;
1046 if (check_addrinfo(
self))
1048 DATA_PTR(
self) = rai = alloc_addrinfo();
1050 rb_scan_args(argc, argv,
"13", &sockaddr_arg, &pfamily, &socktype, &protocol);
1057 if (!
NIL_P(sockaddr_ary)) {
1075 if (!
NIL_P(nodename))
1079 #ifdef AI_NUMERICSERV 1083 init_addrinfo_getaddrinfo(rai, numericnode, service,
1090 #ifdef HAVE_SYS_UN_H 1095 init_unix_addrinfo(rai, path, SOCK_STREAM);
1106 sockaddr_ptr = (
struct sockaddr *)
RSTRING_PTR(sockaddr_arg);
1108 init_addrinfo(rai, sockaddr_ptr, sockaddr_len,
1109 i_pfamily, i_socktype, i_protocol,
1110 canonname, inspectname);
1117 get_afamily(
struct sockaddr *addr,
socklen_t len)
1119 if ((
socklen_t)((
char*)&addr->sa_family +
sizeof(addr->sa_family) - (
char*)addr) <= len)
1120 return addr->sa_family;
1147 else if ((
long)socklen < ((
char*)&sockaddr->
addr.sa_family +
sizeof(sockaddr->
addr.sa_family)) - (
char*)sockaddr)
1150 switch (sockaddr->addr.sa_family) {
1159 struct sockaddr_in *addr;
1161 addr = &sockaddr->in;
1162 if ((
socklen_t)(((
char*)&addr->sin_addr)-(
char*)addr+0+1) <= socklen)
1163 rb_str_catf(ret,
"%d", ((
unsigned char*)&addr->sin_addr)[0]);
1166 if ((
socklen_t)(((
char*)&addr->sin_addr)-(
char*)addr+1+1) <= socklen)
1167 rb_str_catf(ret,
".%d", ((
unsigned char*)&addr->sin_addr)[1]);
1170 if ((
socklen_t)(((
char*)&addr->sin_addr)-(
char*)addr+2+1) <= socklen)
1171 rb_str_catf(ret,
".%d", ((
unsigned char*)&addr->sin_addr)[2]);
1174 if ((
socklen_t)(((
char*)&addr->sin_addr)-(
char*)addr+3+1) <= socklen)
1175 rb_str_catf(ret,
".%d", ((
unsigned char*)&addr->sin_addr)[3]);
1179 if ((
socklen_t)(((
char*)&addr->sin_port)-(
char*)addr+(
int)
sizeof(addr->sin_port)) < socklen) {
1180 port = ntohs(addr->sin_port);
1187 if ((
socklen_t)
sizeof(
struct sockaddr_in) != socklen)
1188 rb_str_catf(ret,
" (%d bytes for %d bytes sockaddr_in)",
1190 (
int)
sizeof(
struct sockaddr_in));
1197 struct sockaddr_in6 *addr;
1201 if (socklen < (
socklen_t)
sizeof(
struct sockaddr_in6)) {
1202 rb_str_catf(ret,
"too-short-AF_INET6-sockaddr %d bytes", (
int)socklen);
1205 addr = &sockaddr->in6;
1216 if (addr->sin6_port == 0) {
1220 port = ntohs(addr->sin6_port);
1223 if ((
socklen_t)
sizeof(
struct sockaddr_in6) < socklen)
1224 rb_str_catf(ret,
"(sockaddr %d bytes too long)", (
int)(socklen -
sizeof(
struct sockaddr_in6)));
1230 #ifdef HAVE_SYS_UN_H 1233 struct sockaddr_un *addr = &sockaddr->un;
1236 e = (
char*)addr + socklen;
1237 while (s < e && *(e-1) ==
'\0')
1244 int printable_only = 1;
1250 if (printable_only) {
1265 #if defined(AF_PACKET) && defined(__linux__) 1269 struct sockaddr_ll *addr;
1270 const char *sep =
"[";
1271 #define CATSEP do { rb_str_cat2(ret, sep); sep = " "; } while (0); 1273 addr = (
struct sockaddr_ll *)sockaddr;
1277 if (
offsetof(
struct sockaddr_ll, sll_protocol) +
sizeof(addr->sll_protocol) <= (
size_t)socklen) {
1279 rb_str_catf(ret,
"protocol=%d", ntohs(addr->sll_protocol));
1281 if (
offsetof(
struct sockaddr_ll, sll_ifindex) +
sizeof(addr->sll_ifindex) <= (
size_t)socklen) {
1284 if (if_indextoname(addr->sll_ifindex, buf) ==
NULL)
1285 rb_str_catf(ret,
"ifindex=%d", addr->sll_ifindex);
1289 if (
offsetof(
struct sockaddr_ll, sll_hatype) +
sizeof(addr->sll_hatype) <= (
size_t)socklen) {
1293 if (
offsetof(
struct sockaddr_ll, sll_pkttype) +
sizeof(addr->sll_pkttype) <= (
size_t)socklen) {
1295 if (addr->sll_pkttype == PACKET_HOST)
1297 else if (addr->sll_pkttype == PACKET_BROADCAST)
1299 else if (addr->sll_pkttype == PACKET_MULTICAST)
1301 else if (addr->sll_pkttype == PACKET_OTHERHOST)
1303 else if (addr->sll_pkttype == PACKET_OUTGOING)
1306 rb_str_catf(ret,
"pkttype=%d", addr->sll_pkttype);
1308 if (socklen != (
socklen_t)(
offsetof(
struct sockaddr_ll, sll_addr) + addr->sll_halen)) {
1310 if (
offsetof(
struct sockaddr_ll, sll_halen) +
sizeof(addr->sll_halen) <= (
size_t)socklen) {
1314 if (
offsetof(
struct sockaddr_ll, sll_addr) < (
size_t)socklen) {
1318 len = addr->sll_halen;
1319 if ((
size_t)socklen <
offsetof(
struct sockaddr_ll, sll_addr) + len)
1320 len = socklen -
offsetof(
struct sockaddr_ll, sll_addr);
1321 for (i = 0; i <
len; i++) {
1327 if (socklen < (
socklen_t)(
offsetof(
struct sockaddr_ll, sll_halen) +
sizeof(addr->sll_halen)) ||
1328 (
socklen_t)(
offsetof(
struct sockaddr_ll, sll_addr) + addr->sll_halen) != socklen) {
1330 rb_str_catf(ret,
"(%d bytes for %d bytes sockaddr_ll)",
1331 (
int)socklen, (
int)
sizeof(
struct sockaddr_ll));
1341 #if defined(AF_LINK) && defined(HAVE_TYPE_STRUCT_SOCKADDR_DL) 1356 struct sockaddr_dl *addr = &sockaddr->dl;
1357 char *np =
NULL, *ap =
NULL, *endp;
1358 int nlen = 0, alen = 0;
1360 const char *sep =
"[";
1361 #define CATSEP do { rb_str_cat2(ret, sep); sep = " "; } while (0); 1365 endp = ((
char *)addr) + socklen;
1367 if (
offsetof(
struct sockaddr_dl, sdl_data) < socklen) {
1368 np = addr->sdl_data;
1369 nlen = addr->sdl_nlen;
1370 if (endp - np < nlen)
1371 nlen = (int)(endp - np);
1373 off = addr->sdl_nlen;
1375 if (
offsetof(
struct sockaddr_dl, sdl_data) + off < socklen) {
1376 ap = addr->sdl_data + off;
1377 alen = addr->sdl_alen;
1378 if (endp - ap < alen)
1379 alen = (int)(endp - ap);
1388 if (ap && 0 < alen) {
1390 for (i = 0; i < alen; i++)
1391 rb_str_catf(ret,
"%s%02x", i == 0 ?
"" :
":", (
unsigned char)ap[i]);
1394 if (socklen < (
socklen_t)(
offsetof(
struct sockaddr_dl, sdl_nlen) +
sizeof(addr->sdl_nlen)) ||
1395 socklen < (
socklen_t)(
offsetof(
struct sockaddr_dl, sdl_alen) +
sizeof(addr->sdl_alen)) ||
1396 socklen < (
socklen_t)(
offsetof(
struct sockaddr_dl, sdl_slen) +
sizeof(addr->sdl_slen)) ||
1399 socklen < (
socklen_t)(
offsetof(
struct sockaddr_dl, sdl_data) + addr->sdl_nlen + addr->sdl_alen + addr->sdl_slen)) {
1401 rb_str_catf(ret,
"(%d bytes for %d bytes sockaddr_dl)",
1402 (
int)socklen, (
int)
sizeof(
struct sockaddr_dl));
1415 rb_str_catf(ret,
"unknown address family %d", sockaddr->addr.sa_family);
1437 addrinfo_inspect(
VALUE self)
1445 inspect_sockaddr(
self, ret);
1457 internet_p = internet_p || rai->
pfamily == PF_INET6;
1459 if (internet_p && rai->
socktype == SOCK_STREAM &&
1463 else if (internet_p && rai->
socktype == SOCK_DGRAM &&
1482 goto unknown_protocol;
1519 return inspect_sockaddr(
self,
rb_str_new(
"", 0));
1524 addrinfo_mdump(
VALUE self)
1527 VALUE sockaddr, afamily, pfamily, socktype, protocol, canonname, inspectname;
1528 int afamily_int = ai_get_afamily(rai);
1566 switch(afamily_int) {
1567 #ifdef HAVE_SYS_UN_H 1570 struct sockaddr_un *su = &rai->
addr.un;
1574 while (s < e && *(e-1) ==
'\0')
1596 return rb_ary_new3(7, afamily, sockaddr, pfamily, socktype, protocol, canonname, inspectname);
1604 VALUE canonname, inspectname;
1605 int afamily, pfamily, socktype, protocol;
1610 if (check_addrinfo(
self))
1666 #ifdef HAVE_SYS_UN_H 1669 struct sockaddr_un uaddr;
1670 INIT_SOCKADDR_UN(&uaddr,
sizeof(
struct sockaddr_un));
1673 if (
sizeof(uaddr.sun_path) < (
size_t)
RSTRING_LEN(v))
1675 "too long AF_UNIX path (%"PRIuSIZE" bytes given but %"PRIuSIZE" bytes max)",
1679 memcpy(&ss, &uaddr, len);
1689 #ifdef AI_NUMERICSERV 1703 DATA_PTR(
self) = rai = alloc_addrinfo();
1704 init_addrinfo(rai, &ss.
addr, len,
1705 pfamily, socktype, protocol,
1706 canonname, inspectname);
1720 addrinfo_afamily(
VALUE self)
1723 return INT2NUM(ai_get_afamily(rai));
1736 addrinfo_pfamily(
VALUE self)
1752 addrinfo_socktype(
VALUE self)
1768 addrinfo_protocol(
VALUE self)
1786 addrinfo_to_sockaddr(
VALUE self)
1811 addrinfo_canonname(
VALUE self)
1830 addrinfo_ip_p(
VALUE self)
1833 int family = ai_get_afamily(rai);
1850 addrinfo_ipv4_p(
VALUE self)
1853 return ai_get_afamily(rai) == AF_INET ?
Qtrue :
Qfalse;
1869 addrinfo_ipv6_p(
VALUE self)
1873 return ai_get_afamily(rai) == AF_INET6 ?
Qtrue :
Qfalse;
1892 addrinfo_unix_p(
VALUE self)
1896 return ai_get_afamily(rai) == AF_UNIX ?
Qtrue :
Qfalse;
1918 addrinfo_getnameinfo(
int argc,
VALUE *argv,
VALUE self)
1922 char hbuf[1024], pbuf[1024];
1952 addrinfo_ip_unpack(
VALUE self)
1955 int family = ai_get_afamily(rai);
1963 ret = addrinfo_getnameinfo(1, &vflags,
self);
1979 addrinfo_ip_address(
VALUE self)
1982 int family = ai_get_afamily(rai);
1990 ret = addrinfo_getnameinfo(1, &vflags,
self);
2004 addrinfo_ip_port(
VALUE self)
2007 int family = ai_get_afamily(rai);
2023 port = ntohs(rai->
addr.
in.sin_port);
2030 port = ntohs(rai->
addr.in6.sin6_port);
2045 int family = ai_get_afamily(rai);
2046 if (family != AF_INET)
return 0;
2047 *addrp = ntohl(rai->
addr.
in.sin_addr.s_addr);
2056 addrinfo_ipv4_private_p(
VALUE self)
2059 if (!extract_in_addr(
self, &a))
return Qfalse;
2060 if ((a & 0xff000000) == 0x0a000000 ||
2061 (a & 0xfff00000) == 0xac100000 ||
2062 (a & 0xffff0000) == 0xc0a80000)
2072 addrinfo_ipv4_loopback_p(
VALUE self)
2075 if (!extract_in_addr(
self, &a))
return Qfalse;
2076 if ((a & 0xff000000) == 0x7f000000)
2086 addrinfo_ipv4_multicast_p(
VALUE self)
2089 if (!extract_in_addr(
self, &a))
return Qfalse;
2090 if ((a & 0xf0000000) == 0xe0000000)
2097 static struct in6_addr *
2098 extract_in6_addr(
VALUE self)
2101 int family = ai_get_afamily(rai);
2102 if (family != AF_INET6)
return NULL;
2103 return &rai->
addr.in6.sin6_addr;
2111 addrinfo_ipv6_unspecified_p(
VALUE self)
2113 struct in6_addr *addr = extract_in6_addr(
self);
2114 if (addr && IN6_IS_ADDR_UNSPECIFIED(addr))
return Qtrue;
2123 addrinfo_ipv6_loopback_p(
VALUE self)
2125 struct in6_addr *addr = extract_in6_addr(
self);
2126 if (addr && IN6_IS_ADDR_LOOPBACK(addr))
return Qtrue;
2135 addrinfo_ipv6_multicast_p(
VALUE self)
2137 struct in6_addr *addr = extract_in6_addr(
self);
2138 if (addr && IN6_IS_ADDR_MULTICAST(addr))
return Qtrue;
2147 addrinfo_ipv6_linklocal_p(
VALUE self)
2149 struct in6_addr *addr = extract_in6_addr(
self);
2150 if (addr && IN6_IS_ADDR_LINKLOCAL(addr))
return Qtrue;
2159 addrinfo_ipv6_sitelocal_p(
VALUE self)
2161 struct in6_addr *addr = extract_in6_addr(
self);
2162 if (addr && IN6_IS_ADDR_SITELOCAL(addr))
return Qtrue;
2171 addrinfo_ipv6_unique_local_p(
VALUE self)
2173 struct in6_addr *addr = extract_in6_addr(
self);
2183 addrinfo_ipv6_v4mapped_p(
VALUE self)
2185 struct in6_addr *addr = extract_in6_addr(
self);
2186 if (addr && IN6_IS_ADDR_V4MAPPED(addr))
return Qtrue;
2195 addrinfo_ipv6_v4compat_p(
VALUE self)
2197 struct in6_addr *addr = extract_in6_addr(
self);
2198 if (addr && IN6_IS_ADDR_V4COMPAT(addr))
return Qtrue;
2207 addrinfo_ipv6_mc_nodelocal_p(
VALUE self)
2209 struct in6_addr *addr = extract_in6_addr(
self);
2210 if (addr && IN6_IS_ADDR_MC_NODELOCAL(addr))
return Qtrue;
2219 addrinfo_ipv6_mc_linklocal_p(
VALUE self)
2221 struct in6_addr *addr = extract_in6_addr(
self);
2222 if (addr && IN6_IS_ADDR_MC_LINKLOCAL(addr))
return Qtrue;
2231 addrinfo_ipv6_mc_sitelocal_p(
VALUE self)
2233 struct in6_addr *addr = extract_in6_addr(
self);
2234 if (addr && IN6_IS_ADDR_MC_SITELOCAL(addr))
return Qtrue;
2243 addrinfo_ipv6_mc_orglocal_p(
VALUE self)
2245 struct in6_addr *addr = extract_in6_addr(
self);
2246 if (addr && IN6_IS_ADDR_MC_ORGLOCAL(addr))
return Qtrue;
2255 addrinfo_ipv6_mc_global_p(
VALUE self)
2257 struct in6_addr *addr = extract_in6_addr(
self);
2258 if (addr && IN6_IS_ADDR_MC_GLOBAL(addr))
return Qtrue;
2273 addrinfo_ipv6_to_ipv4(
VALUE self)
2276 struct in6_addr *addr;
2277 int family = ai_get_afamily(rai);
2278 if (family != AF_INET6)
return Qnil;
2279 addr = &rai->
addr.in6.sin6_addr;
2280 if (IN6_IS_ADDR_V4MAPPED(addr) || IN6_IS_ADDR_V4COMPAT(addr)) {
2281 struct sockaddr_in sin4;
2283 memcpy(&sin4.sin_addr, (
char*)addr +
sizeof(*addr) -
sizeof(sin4.sin_addr),
sizeof(sin4.sin_addr));
2295 #ifdef HAVE_SYS_UN_H 2305 addrinfo_unix_path(
VALUE self)
2308 int family = ai_get_afamily(rai);
2309 struct sockaddr_un *addr;
2312 if (family != AF_UNIX)
2315 addr = &rai->
addr.un;
2321 (
size_t)rai->
sockaddr_len, (
size_t)(s - (
char *)addr));
2322 if (addr->sun_path +
sizeof(addr->sun_path) < e)
2324 "too long AF_UNIX path (%"PRIuSIZE
" bytes given but %"PRIuSIZE
" bytes max)",
2325 (
size_t)(e - addr->sun_path),
sizeof(addr->sun_path));
2326 while (s < e && *(e-1) ==
'\0')
2376 addrinfo_s_getaddrinfo(
int argc,
VALUE *argv,
VALUE self)
2380 rb_scan_args(argc, argv,
"24", &node, &service, &family, &socktype, &protocol, &flags);
2381 return addrinfo_list_new(node, service, family, socktype, protocol, flags);
2400 ret = addrinfo_firstonly_new(host,
Qnil,
2402 rai = get_addrinfo(ret);
2419 return addrinfo_firstonly_new(host, port,
2434 return addrinfo_firstonly_new(host, port,
2438 #ifdef HAVE_SYS_UN_H 2453 addrinfo_s_unix(
int argc,
VALUE *argv,
VALUE self)
2455 VALUE path, vsocktype, addr;
2461 if (
NIL_P(vsocktype))
2462 socktype = SOCK_STREAM;
2467 DATA_PTR(addr) = rai = alloc_addrinfo();
2468 init_unix_addrinfo(rai, path, socktype);
2480 *v = addrinfo_to_sockaddr(val);
2492 *v = addrinfo_to_sockaddr(val);
2510 return addrinfo_to_sockaddr(val);
2523 family = get_afamily(addr, len);
2525 ret = getsockopt(fd, SOL_SOCKET, SO_TYPE, (
void*)&socktype, &optlen);
2575 #ifdef HAVE_SYS_UN_H 2616 #ifdef HAVE_SYS_UN_H
RUBY_EXTERN VALUE rb_cData
VALUE rb_ary_entry(VALUE ary, long offset)
#define SafeStringValueCStr(v)
VALUE rb_str_equal(VALUE str1, VALUE str2)
size_t strlen(const char *)
VALUE rsock_inspect_sockaddr(struct sockaddr *sockaddr_arg, socklen_t socklen, VALUE ret)
void rb_define_singleton_method(VALUE obj, const char *name, VALUE(*func)(ANYARGS), int argc)
Defines a singleton method for obj.
void rb_raise(VALUE exc, const char *fmt,...)
VALUE rb_str_cat(VALUE, const char *, long)
VALUE rsock_make_ipaddr(struct sockaddr *addr, socklen_t addrlen)
#define TypedData_Wrap_Struct(klass, data_type, sval)
int rsock_socktype_arg(VALUE type)
const struct sockaddr * sa
VALUE rb_ary_push(VALUE ary, VALUE item)
if(len<=MAX_WORD_LENGTH &&len >=MIN_WORD_LENGTH)
ID rsock_intern_ipproto(int val)
#define IN6_IS_ADDR_UNIQUE_LOCAL(a)
#define RSTRING_GETMEM(str, ptrvar, lenvar)
void rb_define_alloc_func(VALUE, rb_alloc_func_t)
VALUE rb_check_sockaddr_string_type(VALUE val)
void rb_gc_mark(VALUE ptr)
VALUE rb_tainted_str_new_cstr(const char *)
VALUE rsock_ipaddr(struct sockaddr *sockaddr, socklen_t sockaddrlen, int norevlookup)
const struct addrinfo * hints
VALUE rb_ensure(VALUE(*b_proc)(ANYARGS), VALUE data1, VALUE(*e_proc)(ANYARGS), VALUE data2)
An equivalent to ensure clause.
#define GetOpenFile(obj, fp)
void rsock_init_addrinfo(void)
VALUE rsock_io_socket_addrinfo(VALUE io, struct sockaddr *addr, socklen_t len)
const char * rb_obj_classname(VALUE)
void rb_freeaddrinfo(struct rb_addrinfo *ai)
VALUE rsock_make_hostent(VALUE host, struct rb_addrinfo *addr, VALUE(*ipaddr)(struct sockaddr *, socklen_t))
#define RB_TYPE_P(obj, type)
VALUE rsock_addrinfo_new(struct sockaddr *addr, socklen_t len, int family, int socktype, int protocol, VALUE canonname, VALUE inspectname)
VALUE rb_obj_is_kind_of(VALUE, VALUE)
call-seq: obj.is_a?(class) -> true or false obj.kind_of?(class) -> true or false
#define MEMZERO(p, type, n)
VALUE rsock_sockaddr_string_value_with_addrinfo(volatile VALUE *v, VALUE *rai_ret)
struct rb_addrinfo * rsock_addrinfo(VALUE host, VALUE port, int family, int socktype, int flags)
int rsock_ipproto_to_int(const char *str, long len, int *valp)
int rsock_family_arg(VALUE domain)
VALUE rb_str_cat2(VALUE, const char *)
VALUE rb_str_buf_cat2(VALUE, const char *)
#define STRTOUL(str, endptr, base)
VALUE rb_define_class(const char *name, VALUE super)
Defines a top-level class.
struct rb_addrinfo * rsock_getaddrinfo(VALUE host, VALUE port, struct addrinfo *hints, int socktype_hack)
void rb_ary_store(VALUE ary, long idx, VALUE val)
#define offsetof(p_type, field)
int getaddrinfo(const char *hostname, const char *servname, const struct addrinfo *hints, struct addrinfo **res)
ID rsock_intern_protocol_family(int val)
#define MEMCPY(p1, p2, type, n)
int rsock_family_to_int(const char *str, long len, int *valp)
void * rb_thread_call_without_gvl(void *(*func)(void *), void *data1, rb_unblock_function_t *ubf, void *data2)
VALUE rsock_freeaddrinfo(VALUE arg)
void rb_sys_fail(const char *mesg)
int rsock_socktype_to_int(const char *str, long len, int *valp)
char * rsock_sockaddr_string_value_ptr(volatile VALUE *v)
VALUE rb_sprintf(const char *format,...)
void freeaddrinfo(struct addrinfo *ai)
VALUE rsock_fd_socket_addrinfo(int fd, struct sockaddr *addr, socklen_t len)
int rb_scan_args(int argc, const VALUE *argv, const char *fmt,...)
unsigned char buf[MIME_BUF_SIZE]
VALUE rb_assoc_new(VALUE car, VALUE cdr)
ID rsock_intern_family(int val)
#define INIT_SOCKADDR_IN6(addr, len)
RUBY_EXTERN VALUE rb_cInteger
const char * rb_id2name(ID)
VALUE rb_str_new_cstr(const char *)
ID rsock_intern_socktype(int val)
register unsigned int len
#define StringValueCStr(v)
int rb_getnameinfo(const struct sockaddr *sa, socklen_t salen, char *host, size_t hostlen, char *serv, size_t servlen, int flags)
int rsock_fd_family(int fd)
#define INIT_SOCKADDR_IN(addr, len)
VALUE rb_check_array_type(VALUE ary)
VALUE rb_convert_type(VALUE, int, const char *, const char *)
Converts an object into another type.
void rsock_raise_socket_error(const char *reason, int error)
VALUE rb_str_catf(VALUE str, const char *format,...)
VALUE rb_check_string_type(VALUE)
struct rb_encoding_entry * list
int getnameinfo(const struct sockaddr *sa, socklen_t salen, char *host, socklen_t hostlen, char *serv, socklen_t servlen, int flags)
int rb_getaddrinfo(const char *node, const char *service, const struct addrinfo *hints, struct rb_addrinfo **res)
struct addrinfo * ai_next
struct rb_addrinfo * addr
VALUE rsock_addrinfo_inspect_sockaddr(VALUE self)
void rb_define_method(VALUE klass, const char *name, VALUE(*func)(ANYARGS), int argc)
struct sockaddr * ai_addr
VALUE(* ipaddr)(struct sockaddr *, socklen_t)
void * rb_check_typeddata(VALUE obj, const rb_data_type_t *data_type)
VALUE rsock_sockaddr_string_value(volatile VALUE *v)
#define str_equal(ptr, len, name)
VALUE rb_str_new(const char *, long)