Skip to content

Commit

Permalink
Move fd-checking code to fd.c
Browse files Browse the repository at this point in the history
Signed-off-by: Martin Sustrik <[email protected]>
  • Loading branch information
sustrik committed Jul 8, 2018
1 parent acd8c42 commit e7c322c
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 18 deletions.
21 changes: 21 additions & 0 deletions fd.c
Original file line number Diff line number Diff line change
Expand Up @@ -368,3 +368,24 @@ int dill_fd_own(int s) {
return n;
}

int dill_fd_check(int s, int type, int family1, int family2, int listening) {
/* Check type. E.g. SOCK_STREAM vs. SOCK_DGRAM. */
int val;
socklen_t valsz = sizeof(val);
int rc = getsockopt(s, SOL_SOCKET, SO_TYPE, &val, &valsz);
if(dill_slow(rc < 0)) return -1;
if(dill_slow(val != type)) {errno = EINVAL; return -1;}
/* Check whether the socket is in listening mode. */
rc = getsockopt(s, SOL_SOCKET, SO_ACCEPTCONN, &val, &valsz);
if(dill_slow(rc < 0)) return -1;
if(dill_slow(val != listening)) {errno = EINVAL; return -1;}
/* Check family. E.g. AF_INET vs. AF_UNIX. */
struct sockaddr_storage ss;
socklen_t sssz = sizeof(ss);
rc = getsockname(s, (struct sockaddr*)&ss, &sssz);
if(dill_slow(rc < 0)) return -1;
if(dill_slow(ss.ss_family != family1 && ss.ss_family != family2)) {
errno = EINVAL; return -1;}
return 0;
}

6 changes: 6 additions & 0 deletions fd.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,12 @@ void dill_fd_close(
int s);
int dill_fd_own(
int s);
int dill_fd_check(
int s,
int type,
int family1,
int family2,
int listening);

#endif

20 changes: 2 additions & 18 deletions tcp.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/socket.h>

#define DILL_DISABLE_RAW_NAMES
#include "libdillimpl.h"
Expand Down Expand Up @@ -75,24 +74,9 @@ int dill_tcp_fromfd_mem(int fd, struct dill_tcp_storage *mem) {
int err;
if(dill_slow(!mem)) {err = EINVAL; goto error1;}
if(dill_slow(fd < 0)) {err = errno; goto error1;}
/* Make sure that this is a TCP socket. */
int val;
socklen_t valsz = sizeof(val);
int rc = getsockopt(fd, SOL_SOCKET, SO_TYPE, &val, &valsz);
/* Make sure that the supplied file descriptor is of correct type. */
int rc = dill_fd_check(fd, SOCK_STREAM, AF_INET, AF_INET6, 0);
if(dill_slow(rc < 0)) {err = errno; goto error1;}
if(dill_slow(val != SOCK_STREAM)) {err = EINVAL; goto error1;}
/* Make sure it's not a listening socket. */
rc = getsockopt(fd, SOL_SOCKET, SO_ACCEPTCONN, &val, &valsz);
if(dill_slow(rc < 0)) {err = errno; goto error1;}
if(dill_slow(val != 0)) {err = EINVAL; goto error1;}
/* Make sure the socket is bound to IPv4 or IPv6 address.
This discards, for example, UNIX domain sockets. */
struct sockaddr_storage ss;
socklen_t sssz = sizeof(ss);
rc = getsockname(fd, (struct sockaddr*)&ss, &sssz);
if(dill_slow(rc < 0)) {err = errno; goto error1;}
if(dill_slow(ss.ss_family != AF_INET && ss.ss_family != AF_INET6)) {
err = EINVAL; goto error1;}
/* Take ownership of the file descriptor. */
fd = dill_fd_own(fd);
if(dill_slow(fd < 0)) {err = errno; goto error1;}
Expand Down

0 comments on commit e7c322c

Please sign in to comment.