summaryrefslogtreecommitdiff
path: root/ext
diff options
context:
space:
mode:
Diffstat (limited to 'ext')
-rw-r--r--ext/socket/ancdata.c4
-rw-r--r--ext/socket/basicsocket.c4
-rw-r--r--ext/socket/init.c23
-rw-r--r--ext/socket/rubysocket.h8
4 files changed, 32 insertions, 7 deletions
diff --git a/ext/socket/ancdata.c b/ext/socket/ancdata.c
index d0290ba07a..da8b8160d2 100644
--- a/ext/socket/ancdata.c
+++ b/ext/socket/ancdata.c
@@ -1146,7 +1146,7 @@ bsock_sendmsg_internal(int argc, VALUE *argv, VALUE sock, int nonblock)
GetOpenFile(sock, fptr);
#if defined(HAVE_STRUCT_MSGHDR_MSG_CONTROL)
- family = rsock_getfamily(fptr->fd);
+ family = rsock_getfamily(fptr);
#endif
data = vflags = dest_sockaddr = Qnil;
@@ -1692,7 +1692,7 @@ bsock_recvmsg_internal(int argc, VALUE *argv, VALUE sock, int nonblock)
);
#if defined(HAVE_STRUCT_MSGHDR_MSG_CONTROL)
- family = rsock_getfamily(fptr->fd);
+ family = rsock_getfamily(fptr);
if (mh.msg_controllen) {
char *msg_end = (char *)mh.msg_control + mh.msg_controllen;
for (cmh = CMSG_FIRSTHDR(&mh); cmh != NULL; cmh = CMSG_NXTHDR(&mh, cmh)) {
diff --git a/ext/socket/basicsocket.c b/ext/socket/basicsocket.c
index 6bc1828d61..16fa12dd38 100644
--- a/ext/socket/basicsocket.c
+++ b/ext/socket/basicsocket.c
@@ -214,7 +214,7 @@ bsock_setsockopt(int argc, VALUE *argv, VALUE sock)
}
GetOpenFile(sock, fptr);
- family = rsock_getfamily(fptr->fd);
+ family = rsock_getfamily(fptr);
level = rsock_level_arg(family, lev);
option = rsock_optname_arg(family, level, optname);
@@ -311,7 +311,7 @@ bsock_getsockopt(VALUE sock, VALUE lev, VALUE optname)
int family;
GetOpenFile(sock, fptr);
- family = rsock_getfamily(fptr->fd);
+ family = rsock_getfamily(fptr);
level = rsock_level_arg(family, lev);
option = rsock_optname_arg(family, level, optname);
len = 256;
diff --git a/ext/socket/init.c b/ext/socket/init.c
index 34f6a116f0..00e20fa308 100644
--- a/ext/socket/init.c
+++ b/ext/socket/init.c
@@ -624,15 +624,34 @@ rsock_s_accept(VALUE klass, int fd, struct sockaddr *sockaddr, socklen_t *len)
}
int
-rsock_getfamily(int sockfd)
+rsock_getfamily(rb_io_t *fptr)
{
union_sockaddr ss;
socklen_t sslen = (socklen_t)sizeof(ss);
+ int cached = fptr->mode & FMODE_SOCK;
+
+ if (cached) {
+ switch (cached) {
+#ifdef AF_UNIX
+ case FMODE_UNIX: return AF_UNIX;
+#endif
+ case FMODE_INET: return AF_INET;
+ case FMODE_INET6: return AF_INET6;
+ }
+ }
ss.addr.sa_family = AF_UNSPEC;
- if (getsockname(sockfd, &ss.addr, &sslen) < 0)
+ if (getsockname(fptr->fd, &ss.addr, &sslen) < 0)
return AF_UNSPEC;
+ switch (ss.addr.sa_family) {
+#ifdef AF_UNIX
+ case AF_UNIX: fptr->mode |= FMODE_UNIX; break;
+#endif
+ case AF_INET: fptr->mode |= FMODE_INET; break;
+ case AF_INET6: fptr->mode |= FMODE_INET6; break;
+ }
+
return ss.addr.sa_family;
}
diff --git a/ext/socket/rubysocket.h b/ext/socket/rubysocket.h
index d03b1c5e0b..79dd78324f 100644
--- a/ext/socket/rubysocket.h
+++ b/ext/socket/rubysocket.h
@@ -232,6 +232,12 @@ extern int rsock_do_not_reverse_lookup;
extern int rsock_cmsg_cloexec_state;
#define FMODE_NOREVLOOKUP 0x100
+/* common socket families only */
+#define FMODE_UNIX 0x00200000
+#define FMODE_INET 0x00400000
+#define FMODE_INET6 0x00800000
+#define FMODE_SOCK (FMODE_UNIX|FMODE_INET|FMODE_INET6)
+
extern VALUE rb_cBasicSocket;
extern VALUE rb_cIPSocket;
extern VALUE rb_cTCPSocket;
@@ -279,7 +285,7 @@ int rsock_optname_arg(int family, int level, VALUE optname);
int rsock_cmsg_type_arg(int family, int level, VALUE type);
int rsock_shutdown_how_arg(VALUE how);
-int rsock_getfamily(int sockfd);
+int rsock_getfamily(rb_io_t *fptr);
struct rb_addrinfo {
struct addrinfo *ai;