From 5db82aca938cf27355c17e5e5cdab4448beb60c2 Mon Sep 17 00:00:00 2001 Message-Id: <5db82aca938cf27355c17e5e5cdab4448beb60c2.1714821348.git.mdw@distorted.org.uk> From: Mark Wooding Date: Wed, 27 Sep 2017 23:38:38 +0100 Subject: [PATCH] pkstream/pkstream.c: Wrap addresses up in a union. Organization: Straylight/Edgeware From: Mark Wooding This makes casting to `struct sockaddr' pointers more pleasant, but doesn't do anything else of use yet. No functional change. --- pkstream/pkstream.c | 50 ++++++++++++++++++++++++--------------------- 1 file changed, 27 insertions(+), 23 deletions(-) diff --git a/pkstream/pkstream.c b/pkstream/pkstream.c index b291a1c7..4df37e7e 100644 --- a/pkstream/pkstream.c +++ b/pkstream/pkstream.c @@ -57,6 +57,11 @@ /*----- Data structures ---------------------------------------------------*/ +typedef union addr { + struct sockaddr sa; + struct sockaddr_in sin; +} addr; + typedef struct pk { struct pk *next; /* Next packet in the chain */ octet *p, *o; /* Buffer start and current posn */ @@ -76,7 +81,7 @@ typedef struct connwait { unsigned f; /* Various flags */ #define cwf_port 1u /* Port is defined => listen */ sel_file a; /* Selector */ - struct sockaddr_in me, peer; /* Who I'm meant to be; who peer is */ + addr me, peer; /* Who I'm meant to be; who peer is */ } connwait; /*----- Static variables --------------------------------------------------*/ @@ -94,11 +99,11 @@ static int nonblockify(int fd) static int cloexec(int fd) { return (fdflags(fd, 0, 0, FD_CLOEXEC, FD_CLOEXEC)); } -static void initaddr(struct sockaddr_in *sin) +static void initaddr(addr *a) { - sin->sin_family = AF_INET; - sin->sin_addr.s_addr = INADDR_ANY; - sin->sin_port = 0; + a->sin.sin_family = AF_INET; + a->sin.sin_addr.s_addr = INADDR_ANY; + a->sin.sin_port = 0; } static void dolisten(void); @@ -225,17 +230,17 @@ static void dofwd(int fd_in, int fd_out) static void doaccept(int fd_s, unsigned mode, void *p) { int fd; - struct sockaddr_in sin; - socklen_t sz = sizeof(sin); + addr a; + socklen_t sz = sizeof(a); - if ((fd = accept(fd_s, (struct sockaddr *)&sin, &sz)) < 0) { + if ((fd = accept(fd_s, &a.sa, &sz)) < 0) { if (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR) return; moan("couldn't accept incoming connection: %s", strerror(errno)); return; } - if (cw.peer.sin_addr.s_addr != INADDR_ANY && - cw.peer.sin_addr.s_addr != sin.sin_addr.s_addr) { - moan("rejecting connection from %s", inet_ntoa(sin.sin_addr)); + if (cw.peer.sin.sin_addr.s_addr != INADDR_ANY && + cw.peer.sin.sin_addr.s_addr != a.sin.sin_addr.s_addr) { + moan("rejecting connection from %s", inet_ntoa(a.sin.sin_addr)); close(fd); return; } if (nonblockify(fd) || cloexec(fd)) { @@ -254,7 +259,7 @@ static void dolisten(void) if ((fd = socket(PF_INET, SOCK_STREAM, 0)) < 0 || setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) || - bind(fd, (struct sockaddr *)&cw.me, sizeof(cw.me)) || + bind(fd, &cw.me.sa, sizeof(cw.me.sin)) || listen(fd, 1) || nonblockify(fd) || cloexec(fd)) die(1, "couldn't set up listening socket: %s", strerror(errno)); sel_initfile(&sel, &cw.a, fd, SEL_READ, doaccept, 0); @@ -262,8 +267,7 @@ static void dolisten(void) } #define paf_parse 1u -static void parseaddr(const char *host, const char *svc, unsigned f, - struct sockaddr_in *sin) +static void parseaddr(const char *host, const char *svc, unsigned f, addr *a) { char *alloc = 0, *sep; struct hostent *h; @@ -280,14 +284,14 @@ static void parseaddr(const char *host, const char *svc, unsigned f, if (host) { if ((h = gethostbyname(host)) == 0) die(1, "unknown host `%s'", host); - memcpy(&sin->sin_addr, h->h_addr, sizeof(sin->sin_addr)); + memcpy(&a->sin.sin_addr, h->h_addr, sizeof(a->sin.sin_addr)); } if (svc) { if ((n = strtoul(svc, &qq, 0)) > 0 && !*qq && n <= 0xffff) - sin->sin_port = htons(n); + a->sin.sin_port = htons(n); else if ((s = getservbyname(svc, "tcp")) != 0) - sin->sin_port = s->s_port; + a->sin.sin_port = s->s_port; else die(1, "bad service name/number `%s'", svc); } @@ -331,9 +335,9 @@ int main(int argc, char *argv[]) { unsigned f = 0; const char *bindhost = 0, *bindsvc = 0, *peerhost = 0; - struct sockaddr_in bindaddr; + addr bindaddr; const char *connhost = 0; - struct sockaddr_in tmpaddr; + addr tmpaddr; int fd = -1; int len = 65536; @@ -397,8 +401,8 @@ int main(int argc, char *argv[]) parseaddr(connhost, 0, paf_parse, &tmpaddr); if ((fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0 || (bindhost && - bind(fd, (struct sockaddr *)&bindaddr, sizeof(bindaddr))) || - connect(fd, (struct sockaddr *)&tmpaddr, sizeof(tmpaddr))) + bind(fd, &bindaddr.sa, sizeof(bindaddr.sin))) || + connect(fd, &tmpaddr.sa, sizeof(tmpaddr.sin))) die(1, "couldn't connect to TCP server: %s", strerror(errno)); if (nonblockify(fd) || cloexec(fd)) die(1, "couldn't connect to TCP server: %s", strerror(errno)); @@ -410,11 +414,11 @@ int main(int argc, char *argv[]) nonblockify(fd_udp) || cloexec(fd_udp) || setsockopt(fd_udp, SOL_SOCKET, SO_RCVBUF, &len, sizeof(len)) || setsockopt(fd_udp, SOL_SOCKET, SO_SNDBUF, &len, sizeof(len)) || - bind(fd_udp, (struct sockaddr *)&tmpaddr, sizeof(tmpaddr))) + bind(fd_udp, &tmpaddr.sa, sizeof(tmpaddr.sin))) die(1, "couldn't set up UDP socket: %s", strerror(errno)); initaddr(&tmpaddr); parseaddr(argv[optind + 1], 0, paf_parse, &tmpaddr); - if (connect(fd_udp, (struct sockaddr *)&tmpaddr, sizeof(tmpaddr))) + if (connect(fd_udp, &tmpaddr.sa, sizeof(tmpaddr.sin))) die(1, "couldn't set up UDP socket: %s", strerror(errno)); if (bindsvc) dolisten(); -- [mdw]