13 static VALUE sym_wait_writable;
108 setup_domain_and_type(
VALUE domain,
int *dv,
VALUE type,
int *tv)
135 VALUE domain, type, protocol;
143 setup_domain_and_type(domain, &d, type, &t);
150 #if defined HAVE_SOCKETPAIR 152 io_call_close(
VALUE io)
160 return rb_rescue(io_call_close, io, 0, 0);
164 pair_yield(
VALUE pair)
170 #if defined HAVE_SOCKETPAIR 174 rsock_socketpair0(
int domain,
int type,
int protocol,
int sv[2])
177 static int cloexec_state = -1;
179 if (cloexec_state > 0) {
180 ret =
socketpair(domain, type|SOCK_CLOEXEC, protocol, sv);
181 if (ret == 0 && (sv[0] <= 2 || sv[1] <= 2)) {
186 else if (cloexec_state < 0) {
187 ret =
socketpair(domain, type|SOCK_CLOEXEC, protocol, sv);
190 if ((cloexec_state == 0) || (sv[0] <= 2 || sv[1] <= 2))
194 else if (ret == -1 &&
errno == EINVAL) {
225 rsock_socketpair0(
int domain,
int type,
int protocol,
int sv[2])
227 int ret =
socketpair(domain, type, protocol, sv);
239 rsock_socketpair(
int domain,
int type,
int protocol,
int sv[2])
243 ret = rsock_socketpair0(domain, type, protocol, sv);
245 ret = rsock_socketpair0(domain, type, protocol, sv);
283 VALUE domain, type, protocol;
292 setup_domain_and_type(domain, &d, type, &t);
294 ret = rsock_socketpair(d, t, p, sp);
303 return rb_ensure(pair_yield, r, io_close, s1);
308 #define rsock_sock_s_socketpair rb_f_notimplement 458 return sym_wait_writable;
651 if (listen(fptr->
fd, backlog) < 0)
790 sock_accept(
VALUE sock)
810 struct sockaddr *addr = &buf.
addr;
860 sock_sysaccept(
VALUE sock)
873 #ifdef HAVE_GETHOSTNAME 888 #if defined(NI_MAXHOST) 889 # define RUBY_MAX_HOST_NAME_LEN NI_MAXHOST 890 #elif defined(HOST_NAME_MAX) 891 # define RUBY_MAX_HOST_NAME_LEN HOST_NAME_MAX 893 # define RUBY_MAX_HOST_NAME_LEN 1024 896 long len = RUBY_MAX_HOST_NAME_LEN;
921 #include <sys/utsname.h> 932 #define sock_gethostname rb_f_notimplement 937 make_addrinfo(
struct rb_addrinfo *res0,
int norevlookup)
946 for (res = res0->
ai; res; res = res->
ai_next) {
964 switch (addr->sa_family) {
966 ptr = (
char*)&((
struct sockaddr_in*)addr)->sin_addr.s_addr;
967 len = (
socklen_t)
sizeof(((
struct sockaddr_in*)addr)->sin_addr.s_addr);
971 ptr = (
char*)&((
struct sockaddr_in6*)addr)->sin6_addr.s6_addr;
972 len = (
socklen_t)
sizeof(((
struct sockaddr_in6*)addr)->sin6_addr.s6_addr);
1046 if (!
NIL_P(family)) {
1056 #ifdef HAVE_HSTRERROR 1067 if (h->h_aliases !=
NULL) {
1068 for (pch = h->h_aliases; *pch; pch++) {
1074 for (pch = h->h_addr_list; *pch; pch++) {
1103 const char *servicename, *protoname =
"tcp";
1110 sp = getservbyname(servicename, protoname);
1112 port = ntohs(sp->s_port);
1117 port =
STRTOUL(servicename, &end, 0);
1144 const char *protoname =
"tcp";
1148 if (portnum != (uint16_t)portnum) {
1149 const char *s = portnum > 0 ?
"big" :
"small";
1154 sp = getservbyport((
int)htons((uint16_t)portnum), protoname);
1199 VALUE host, port, family, socktype, protocol, flags, ret, revlookup;
1204 rb_scan_args(
argc, argv,
"25", &host, &port, &family, &socktype, &protocol, &flags, &revlookup);
1209 if (!
NIL_P(socktype)) {
1212 if (!
NIL_P(protocol)) {
1215 if (!
NIL_P(flags)) {
1223 ret = make_addrinfo(res, norevlookup);
1255 char hbuf[1024], pbuf[1024];
1259 int error, saved_errno;
1261 struct sockaddr *sap;
1268 if (!
NIL_P(flags)) {
1306 #ifdef AI_NUMERICHOST 1321 hbuf[
sizeof(hbuf) - 1] =
'\0';
1335 pbuf[
sizeof(pbuf) - 1] =
'\0';
1342 if (error)
goto error_exit_addr;
1352 pbuf,
sizeof(pbuf), fl);
1353 if (error)
goto error_exit_name;
1356 char hbuf2[1024], pbuf2[1024];
1361 pbuf2,
sizeof(pbuf2), fl);
1362 if (error)
goto error_exit_name;
1363 if (strcmp(hbuf, hbuf2) != 0|| strcmp(pbuf, pbuf2) != 0) {
1373 saved_errno =
errno;
1375 errno = saved_errno;
1379 saved_errno =
errno;
1381 errno = saved_errno;
1428 sock_s_unpack_sockaddr_in(
VALUE self,
VALUE addr)
1430 struct sockaddr_in * sockaddr;
1435 (
char*)&((
struct sockaddr *)sockaddr)->sa_family +
1436 sizeof(((
struct sockaddr *)sockaddr)->sa_family) -
1439 if (((
struct sockaddr *)sockaddr)->sa_family != AF_INET
1441 && ((
struct sockaddr *)sockaddr)->sa_family != AF_INET6
1455 #ifdef HAVE_SYS_UN_H 1468 sock_s_pack_sockaddr_un(
VALUE self,
VALUE path)
1470 struct sockaddr_un sockaddr;
1474 INIT_SOCKADDR_UN(&sockaddr,
sizeof(
struct sockaddr_un));
1475 if (
sizeof(sockaddr.sun_path) < (
size_t)
RSTRING_LEN(path)) {
1477 (
size_t)
RSTRING_LEN(path),
sizeof(sockaddr.sun_path));
1480 addr =
rb_str_new((
char*)&sockaddr, rsock_unix_sockaddr_len(path));
1499 sock_s_unpack_sockaddr_un(
VALUE self,
VALUE addr)
1501 struct sockaddr_un * sockaddr;
1506 (
char*)&((
struct sockaddr *)sockaddr)->sa_family +
1507 sizeof(((
struct sockaddr *)sockaddr)->sa_family) -
1510 if (((
struct sockaddr *)sockaddr)->sa_family != AF_UNIX) {
1513 if (
sizeof(
struct sockaddr_un) < (
size_t)
RSTRING_LEN(addr)) {
1515 RSTRING_LEN(addr), (
int)
sizeof(
struct sockaddr_un));
1523 #if defined(HAVE_GETIFADDRS) || defined(SIOCGLIFCONF) || defined(SIOCGIFCONF) || defined(_WIN32) 1526 sockaddr_len(
struct sockaddr *addr)
1531 #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN 1532 if (addr->sa_len != 0)
1533 return addr->sa_len;
1536 switch (addr->sa_family) {
1538 return (
socklen_t)
sizeof(
struct sockaddr_in);
1542 return (
socklen_t)
sizeof(
struct sockaddr_in6);
1545 #ifdef HAVE_SYS_UN_H 1547 return (
socklen_t)
sizeof(
struct sockaddr_un);
1552 return (
socklen_t)(
offsetof(
struct sockaddr_ll, sll_addr) + ((
struct sockaddr_ll *)addr)->sll_halen);
1556 return (
socklen_t)(
offsetof(
struct sockaddr, sa_family) +
sizeof(addr->sa_family));
1563 return sockaddr_len(addr);
1569 #if defined(AF_INET6) && defined(__KAME__) 1570 struct sockaddr_in6 addr6;
1576 len = sockaddr_len(addr);
1578 #if defined(__KAME__) && defined(AF_INET6) 1579 if (addr->sa_family == AF_INET6) {
1583 len = (
socklen_t)
sizeof(
struct sockaddr_in6);
1584 memcpy(&addr6, addr, len);
1585 addr = (
struct sockaddr *)&addr6;
1586 if (IN6_IS_ADDR_LINKLOCAL(&addr6.sin6_addr) &&
1587 addr6.sin6_scope_id == 0 &&
1588 (addr6.sin6_addr.s6_addr[2] || addr6.sin6_addr.s6_addr[3])) {
1589 addr6.sin6_scope_id = (addr6.sin6_addr.s6_addr[2] << 8) | addr6.sin6_addr.s6_addr[3];
1590 addr6.sin6_addr.s6_addr[2] = 0;
1591 addr6.sin6_addr.s6_addr[3] = 0;
1602 return sockaddr_obj(addr, len);
1607 #if defined(HAVE_GETIFADDRS) || (defined(SIOCGLIFCONF) && defined(SIOCGLIFNUM) && !defined(__hpux)) || defined(SIOCGIFCONF) || defined(_WIN32) 1626 #if defined(HAVE_GETIFADDRS) 1638 for (p = ifp; p; p = p->
ifa_next) {
1640 struct sockaddr *addr = p->
ifa_addr;
1641 #if defined(AF_INET6) && defined(__sun) 1647 struct sockaddr_in6 addr6;
1648 if (addr->sa_family == AF_INET6) {
1650 memcpy(&addr6, addr, len);
1651 addr = (
struct sockaddr *)&addr6;
1652 if (IN6_IS_ADDR_LINKLOCAL(&addr6.sin6_addr) &&
1653 addr6.sin6_scope_id == 0) {
1654 unsigned int ifindex = if_nametoindex(p->
ifa_name);
1656 addr6.sin6_scope_id = ifindex;
1661 rb_ary_push(list, sockaddr_obj(addr, sockaddr_len(addr)));
1668 #elif defined(SIOCGLIFCONF) && defined(SIOCGLIFNUM) && !defined(__hpux) 1675 const char *reason =
NULL;
1682 fd = socket(AF_INET, SOCK_DGRAM, 0);
1686 memset(&ln, 0,
sizeof(ln));
1689 ret =
ioctl(fd, SIOCGLIFNUM, &ln);
1691 reason =
"SIOCGLIFNUM";
1695 memset(&lc, 0,
sizeof(lc));
1698 lc.lifc_len =
sizeof(
struct lifreq) * ln.lifn_count;
1699 lc.lifc_req =
xmalloc(lc.lifc_len);
1701 ret =
ioctl(fd, SIOCGLIFCONF, &lc);
1703 reason =
"SIOCGLIFCONF";
1708 for (i = 0; i < ln.lifn_count; i++) {
1709 struct lifreq *req = &lc.lifc_req[i];
1711 if (req->lifr_addr.ss_family == AF_INET6 &&
1712 IN6_IS_ADDR_LINKLOCAL(&((
struct sockaddr_in6 *)(&req->lifr_addr))->sin6_addr) &&
1713 ((
struct sockaddr_in6 *)(&req->lifr_addr))->sin6_scope_id == 0) {
1715 memcpy(req2.lifr_name, req->lifr_name, LIFNAMSIZ);
1716 ret =
ioctl(fd, SIOCGLIFINDEX, &req2);
1718 reason =
"SIOCGLIFINDEX";
1721 ((
struct sockaddr_in6 *)(&req->lifr_addr))->sin6_scope_id = req2.lifr_index;
1723 rb_ary_push(list, sockaddr_obj((
struct sockaddr *)&req->lifr_addr, req->lifr_addrlen));
1729 if (lc.lifc_buf !=
NULL)
1739 #elif defined(SIOCGIFCONF) 1742 #define EXTRA_SPACE ((int)(sizeof(struct ifconf) + sizeof(union_sockaddr))) 1743 char initbuf[4096+EXTRA_SPACE];
1744 char *
buf = initbuf;
1749 const char *reason =
NULL;
1752 fd = socket(AF_INET, SOCK_DGRAM, 0);
1756 bufsize =
sizeof(initbuf);
1760 conf.ifc_len = bufsize;
1761 conf.ifc_req = (
struct ifreq *)buf;
1765 ret =
ioctl(fd, SIOCGIFCONF, &conf);
1767 reason =
"SIOCGIFCONF";
1773 if (bufsize - EXTRA_SPACE < conf.ifc_len) {
1774 if (bufsize < conf.ifc_len) {
1776 bufsize = conf.ifc_len + EXTRA_SPACE;
1779 bufsize = bufsize << 1;
1792 while ((
char*)req < (
char*)conf.ifc_req + conf.ifc_len) {
1793 struct sockaddr *addr = &req->ifr_addr;
1795 rb_ary_push(list, sockaddr_obj(addr, sockaddr_len(addr)));
1797 #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN 1798 # ifndef _SIZEOF_ADDR_IFREQ 1799 # define _SIZEOF_ADDR_IFREQ(r) \ 1800 (sizeof(struct ifreq) + \ 1801 (sizeof(struct sockaddr) < (r).ifr_addr.sa_len ? \ 1802 (r).ifr_addr.sa_len - sizeof(struct sockaddr) : \ 1805 req = (
struct ifreq *)((
char*)req + _SIZEOF_ADDR_IFREQ(*req));
1807 req = (
struct ifreq *)((
char*)req +
sizeof(
struct ifreq));
1825 #elif defined(_WIN32) 1826 typedef struct ip_adapter_unicast_address_st {
1827 unsigned LONG_LONG dummy0;
1828 struct ip_adapter_unicast_address_st *
Next;
1830 struct sockaddr *lpSockaddr;
1831 int iSockaddrLength;
1839 } ip_adapter_unicast_address_t;
1840 typedef struct ip_adapter_anycast_address_st {
1841 unsigned LONG_LONG dummy0;
1842 struct ip_adapter_anycast_address_st *
Next;
1844 struct sockaddr *lpSockaddr;
1845 int iSockaddrLength;
1847 } ip_adapter_anycast_address_t;
1848 typedef struct ip_adapter_addresses_st {
1849 unsigned LONG_LONG dummy0;
1850 struct ip_adapter_addresses_st *
Next;
1852 ip_adapter_unicast_address_t *FirstUnicastAddress;
1853 ip_adapter_anycast_address_t *FirstAnycastAddress;
1868 } ip_adapter_addresses_t;
1869 typedef ULONG (WINAPI *GetAdaptersAddresses_t)(ULONG, ULONG, PVOID, ip_adapter_addresses_t *, PULONG);
1871 GetAdaptersAddresses_t pGetAdaptersAddresses;
1874 ip_adapter_addresses_t *adapters;
1877 h = LoadLibrary(
"iphlpapi.dll");
1880 pGetAdaptersAddresses = (GetAdaptersAddresses_t)GetProcAddress(h,
"GetAdaptersAddresses");
1881 if (!pGetAdaptersAddresses) {
1887 if (ret != ERROR_SUCCESS && ret != ERROR_BUFFER_OVERFLOW) {
1892 adapters = (ip_adapter_addresses_t *)
ALLOCA_N(BYTE, len);
1893 ret = pGetAdaptersAddresses(
AF_UNSPEC, 0,
NULL, adapters, &len);
1894 if (ret != ERROR_SUCCESS) {
1901 for (; adapters; adapters = adapters->Next) {
1902 ip_adapter_unicast_address_t *uni;
1903 ip_adapter_anycast_address_t *any;
1904 if (adapters->OperStatus != 1)
1906 for (uni = adapters->FirstUnicastAddress; uni; uni = uni->Next) {
1908 if (uni->Address.lpSockaddr->sa_family == AF_INET)
1912 rb_ary_push(list, sockaddr_obj(uni->Address.lpSockaddr, uni->Address.iSockaddrLength));
1914 for (any = adapters->FirstAnycastAddress; any; any = any->Next) {
1916 if (any->Address.lpSockaddr->sa_family == AF_INET)
1920 rb_ary_push(list, sockaddr_obj(any->Address.lpSockaddr, any->Address.iSockaddrLength));
1929 #define socket_s_ip_address_list rb_f_notimplement 2063 "__connect_nonblock", sock_connect_nonblock, 2);
2071 "__accept_nonblock", sock_accept_nonblock, 1);
2079 "__recvfrom_nonblock", sock_recvfrom_nonblock, 4);
2093 #ifdef HAVE_SYS_UN_H void rsock_syserr_fail_raddrinfo_or_sockaddr(int err, const char *mesg, VALUE addr, VALUE rai)
VALUE rb_ary_entry(VALUE ary, long offset)
void rb_syserr_fail(int e, const char *mesg)
#define VALIDATE_SOCKLEN(addr, len)
size_t strlen(const char *)
void rb_update_max_fd(int fd)
void rb_io_set_nonblock(rb_io_t *fptr)
void rb_define_singleton_method(VALUE obj, const char *name, VALUE(*func)(ANYARGS), int argc)
Defines a singleton method for obj.
VALUE rsock_sockaddr_obj(struct sockaddr *addr, socklen_t len)
void rb_syserr_fail_str(int e, VALUE mesg)
int rb_block_given_p(void)
Determines if the current method is given a block.
void rb_raise(VALUE exc, const char *fmt,...)
VALUE rsock_make_ipaddr(struct sockaddr *addr, socklen_t addrlen)
int rsock_socktype_arg(VALUE type)
void rb_define_private_method(VALUE klass, const char *name, VALUE(*func)(ANYARGS), int argc)
VALUE rb_ary_push(VALUE ary, VALUE item)
VALUE rsock_init_sock(VALUE sock, int fd)
int rb_gc_for_fd(int err)
VALUE rsock_s_accept(VALUE klass, int fd, struct sockaddr *sockaddr, socklen_t *len)
VALUE rb_rescue(VALUE(*b_proc)(ANYARGS), VALUE data1, VALUE(*r_proc)(ANYARGS), VALUE data2)
An equivalent of rescue clause.
VALUE rb_obj_alloc(VALUE)
Allocates an instance of klass.
VALUE rb_check_sockaddr_string_type(VALUE val)
struct sockaddr * ifa_addr
void rsock_syserr_fail_raddrinfo(int err, const char *mesg, VALUE rai)
VALUE rsock_ipaddr(struct sockaddr *sockaddr, socklen_t sockaddrlen, int norevlookup)
VALUE rb_ensure(VALUE(*b_proc)(ANYARGS), VALUE data1, VALUE(*e_proc)(ANYARGS), VALUE data2)
An equivalent to ensure clause.
int rb_w32_map_errno(DWORD)
VALUE rsock_s_recvfrom_nonblock(VALUE sock, VALUE len, VALUE flg, VALUE str, VALUE ex, enum sock_recv_type from)
#define GetOpenFile(obj, fp)
void freeifaddrs(struct ifaddrs *)
VALUE rsock_io_socket_addrinfo(VALUE io, struct sockaddr *addr, socklen_t len)
socklen_t rsock_sockaddr_len(struct sockaddr *addr)
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 rsock_s_accept_nonblock(VALUE klass, VALUE ex, rb_io_t *fptr, struct sockaddr *sockaddr, socklen_t *len)
void rsock_syserr_fail_host_port(int err, const char *mesg, VALUE host, VALUE port)
#define MEMZERO(p, type, n)
struct rb_addrinfo * rsock_addrinfo(VALUE host, VALUE port, int family, int socktype, int flags)
int getifaddrs(struct ifaddrs **)
struct ifaddrs * ifa_next
int rsock_family_arg(VALUE domain)
void rsock_syserr_fail_path(int err, const char *mesg, VALUE path)
#define STRTOUL(str, endptr, base)
int rsock_socket(int domain, int type, int proto)
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)
#define offsetof(p_type, field)
void rb_notimplement(void)
#define ALLOCA_N(type, n)
#define SockAddrStringValuePtr(v)
VALUE rb_str_resize(VALUE, long)
void rb_sys_fail(const char *mesg)
VALUE rsock_sock_listen(VALUE sock, VALUE log)
void rsock_syserr_fail_sockaddr(int err, const char *mesg, struct sockaddr *addr, socklen_t len)
VALUE rsock_s_recvfrom(VALUE sock, int argc, VALUE *argv, enum sock_recv_type from)
VALUE rb_sprintf(const char *format,...)
int rb_scan_args(int argc, const VALUE *argv, const char *fmt,...)
void rb_str_modify_expand(VALUE, long)
unsigned char buf[MIME_BUF_SIZE]
VALUE rb_assoc_new(VALUE car, VALUE cdr)
void rsock_init_basicsocket(void)
int rsock_do_not_reverse_lookup
#define SockAddrStringValueWithAddrinfo(v, rai_ret)
void rsock_sys_fail_sockaddr(const char *mesg, struct sockaddr *addr, socklen_t len)
void rb_readwrite_syserr_fail(enum rb_io_wait_readwrite writable, int n, const char *mesg)
void rsock_sys_fail_host_port(const char *mesg, VALUE host, VALUE port)
int rsock_detect_cloexec(int fd)
#define rb_tainted_str_new2
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)
void rsock_sys_fail_raddrinfo(const char *mesg, VALUE rai)
#define RARRAY_ASET(a, i, v)
#define RARRAY_AREF(a, i)
int rsock_revlookup_flag(VALUE revlookup, int *norevlookup)
void rb_fd_fix_cloexec(int fd)
VALUE rb_check_array_type(VALUE ary)
void rsock_sys_fail_raddrinfo_or_sockaddr(const char *mesg, VALUE addr, VALUE rai)
void rsock_raise_socket_error(const char *reason, int error)
int rsock_connect(int fd, const struct sockaddr *sockaddr, int len, int socks)
struct rb_encoding_entry * list
int rb_getaddrinfo(const char *node, const char *service, const struct addrinfo *hints, struct rb_addrinfo **res)
struct addrinfo * ai_next
void rsock_sys_fail_path(const char *mesg, VALUE path)
#define rsock_sock_s_socketpair
int socketpair(int, int, int, int *)
void rsock_init_socket_init(void)
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
void rb_maygvl_fd_fix_cloexec(int fd)
#define socket_s_ip_address_list
VALUE rb_str_new(const char *, long)