#include <mLib/alloc.h>
#include <mLib/bits.h>
+#include <mLib/darray.h>
#include <mLib/dstr.h>
#include <mLib/fdflags.h>
#include <mLib/mdwopt.h>
/*----- Data structures ---------------------------------------------------*/
+typedef union addr {
+ struct sockaddr sa;
+ struct sockaddr_in sin;
+} addr;
+
+DA_DECL(addr_v, addr);
+DA_DECL(str_v, const char *);
+
typedef struct pk {
struct pk *next; /* Next packet in the chain */
octet *p, *o; /* Buffer start and current posn */
} pkstream;
typedef struct connwait {
- sel_file a; /* Selector */
- struct sockaddr_in me; /* Who I'm meant to be */
- struct in_addr peer; /* Who my peer is */
+ unsigned f; /* Various flags */
+#define cwf_port 1u /* Port is defined => listen */
+ sel_file *sfv; /* Selectors */
+ addr_v me, peer; /* Who I'm meant to be; who peer is */
} connwait;
/*----- Static variables --------------------------------------------------*/
static int cloexec(int fd)
{ return (fdflags(fd, 0, 0, FD_CLOEXEC, FD_CLOEXEC)); }
+static socklen_t addrsz(const addr *a)
+{
+ switch (a->sa.sa_family) {
+ case AF_INET: return sizeof(a->sin);
+ default: abort();
+ }
+}
+
+static int knownafp(int af)
+{
+ switch (af) {
+ case AF_INET: return (1);
+ default: return (0);
+ }
+}
+
+static int initsock(int fd, int af)
+{
+ switch (af) {
+ case AF_INET: break;
+ default: abort();
+ }
+ return (0);
+}
+
+static const char *addrstr(const addr *a)
+{
+ static char buf[128];
+ socklen_t n = sizeof(buf);
+
+ if (getnameinfo(&a->sa, addrsz(a), buf, n, 0, 0, NI_NUMERICHOST))
+ return ("<addrstr failed>");
+ return (buf);
+}
+
+static int addreq(const addr *a, const addr *b)
+{
+ if (a->sa.sa_family != b->sa.sa_family) return (0);
+ switch (a->sa.sa_family) {
+ case AF_INET:
+ return (a->sin.sin_addr.s_addr == b->sin.sin_addr.s_addr);
+ default:
+ abort();
+ }
+}
+
+static void initaddr(addr *a, int af)
+{
+ a->sa.sa_family = af;
+ switch (af) {
+ case AF_INET:
+ a->sin.sin_addr.s_addr = INADDR_ANY;
+ a->sin.sin_port = 0;
+ break;
+ default:
+ abort();
+ }
+}
+
+#define caf_addr 1u
+#define caf_port 2u
+static void copyaddr(addr *a, const struct sockaddr *sa, unsigned f)
+{
+ const struct sockaddr_in *sin;
+
+ a->sa.sa_family = sa->sa_family;
+ switch (sa->sa_family) {
+ case AF_INET:
+ sin = (const struct sockaddr_in *)sa;
+ if (f&caf_addr) a->sin.sin_addr = sin->sin_addr;
+ if (f&caf_port) a->sin.sin_port = sin->sin_port;
+ break;
+ default:
+ abort();
+ }
+}
+
static void dolisten(void);
static void doclose(pkstream *p)
xfree(pk);
}
xfree(p);
- if (cw.me.sin_port) dolisten();
+ if (cw.f&cwf_port) dolisten();
else exit(0);
}
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);
+ size_t i, n;
- 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.s_addr != INADDR_ANY &&
- cw.peer.s_addr != sin.sin_addr.s_addr) {
- moan("rejecting connection from %s", inet_ntoa(sin.sin_addr));
- close(fd); return;
- }
+ n = DA_LEN(&cw.peer);
+ if (!n) goto match;
+ for (i = 0; i < n; i++) if (addreq(&a, &DA(&cw.peer)[i])) goto match;
+ moan("rejecting connection from %s", addrstr(&a));
+ close(fd); return;
+match:
if (nonblockify(fd) || cloexec(fd)) {
moan("couldn't accept incoming connection: %s", strerror(errno));
close(fd); return;
}
dofwd(fd, fd);
- close(fd_s);
- sel_rmfile(&cw.a);
+ n = DA_LEN(&cw.me);
+ for (i = 0; i < n; i++) { close(cw.sfv[i].fd); sel_rmfile(&cw.sfv[i]); }
}
-static void dolisten(void)
+static void dolisten1(const addr *a, sel_file *sf)
{
int fd;
int opt = 1;
- if ((fd = socket(PF_INET, SOCK_STREAM, 0)) < 0 ||
+ if ((fd = socket(a->sa.sa_family, SOCK_STREAM, IPPROTO_TCP)) < 0 ||
setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) ||
- bind(fd, (struct sockaddr *)&cw.me, sizeof(cw.me)) ||
+ initsock(fd, a->sa.sa_family) ||
+ bind(fd, &a->sa, addrsz(a)) ||
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);
- sel_addfile(&cw.a);
+ sel_initfile(&sel, sf, fd, SEL_READ, doaccept, 0);
+ sel_addfile(sf);
+}
+
+static void dolisten(void)
+{
+ size_t i, n;
+
+ n = DA_LEN(&cw.me);
+ for (i = 0; i < n; i++)
+ dolisten1(&DA(&cw.me)[i], &cw.sfv[i]);
}
-static void parseaddr(const char *pp, struct in_addr *a, unsigned short *pt)
+static void pushaddrs(addr_v *av, const struct addrinfo *ailist)
{
- char *p = xstrdup(pp);
- char *q = 0;
- struct hostent *h;
- struct servent *s;
- char *qq;
- unsigned long n;
-
- if (!pt);
- else if (a) q = p;
- else {
- strtok(p, ":");
- q = strtok(0, "");
- if (!q) die(1, "missing port number in address `%s'", p);
+ const struct addrinfo *ai;
+ size_t i, n;
+
+ for (ai = ailist, n = 0; ai; ai = ai->ai_next)
+ if (knownafp(ai->ai_family)) n++;
+ DA_ENSURE(av, n);
+ for (i = DA_LEN(av), ai = ailist; ai; ai = ai->ai_next) {
+ if (!knownafp(ai->ai_family)) continue;
+ initaddr(&DA(av)[i], ai->ai_family);
+ copyaddr(&DA(av)[i++], ai->ai_addr, caf_addr | caf_port);
}
+ DA_EXTEND(av, n);
+}
- if (a) {
- if ((h = gethostbyname(p)) == 0) die(1, "unknown host `%s'", p);
- memcpy(a, h->h_addr, sizeof(*a));
+#define paf_parse 1u
+static void parseaddr(const struct addrinfo *aihint,
+ const char *host, const char *svc, unsigned f,
+ struct addrinfo **ai_out)
+{
+ char *alloc = 0, *sep;
+ int err;
+
+ if (f&paf_parse) {
+ alloc = xstrdup(host);
+ if (alloc[0] != '[') {
+ if ((sep = strchr(alloc, ':')) == 0)
+ die(1, "missing port number in address `%s'", host);
+ host = alloc; *sep = 0; svc = sep + 1;
+ } else {
+ if ((sep = strchr(alloc, ']')) == 0 || sep[1] != ':')
+ die(1, "bad syntax in address `%s:'", host);
+ host = alloc + 1; *sep = 0; svc = sep + 2;
+ }
}
- if (pt) {
- if ((n = strtoul(q, &qq, 0)) > 0 && !*qq && n <= 0xffff) *pt = htons(n);
- else if ((s = getservbyname(q, "tcp")) != 0) *pt = s->s_port;
- else die(1, "bad port number `%s'", q);
+ err = getaddrinfo(host, svc, aihint, ai_out);
+ if (err) {
+ if (host && svc) {
+ die(1, "failed to resolve hostname `%s', service `%s': %s",
+ host, svc, gai_strerror(err));
+ } else if (host)
+ die(1, "failed to resolve hostname `%s': %s", host, gai_strerror(err));
+ else
+ die(1, "failed to resolve service `%s': %s", svc, gai_strerror(err));
}
+
+ xfree(alloc);
}
static void usage(FILE *fp)
int main(int argc, char *argv[])
{
unsigned f = 0;
- unsigned short pt;
- struct sockaddr_in connaddr, bindaddr;
- struct sockaddr_in udp_me, udp_peer;
- int fd;
+ str_v bindhosts = DA_INIT, peerhosts = DA_INIT;
+ const char *bindsvc = 0;
+ addr bindaddr;
+ const char *connhost = 0;
+ struct addrinfo aihint = { 0 }, *ai, *ailist;
+ int fd = -1;
int len = 65536;
+ size_t i, n;
#define f_bogus 1u
+ cw.f = 0;
+
ego(argv[0]);
- bindaddr.sin_family = AF_INET;
- bindaddr.sin_addr.s_addr = INADDR_ANY;
- bindaddr.sin_port = 0;
- connaddr.sin_family = AF_INET;
- connaddr.sin_addr.s_addr = INADDR_ANY;
- cw.me.sin_family = AF_INET;
- cw.me.sin_addr.s_addr = INADDR_ANY;
- cw.me.sin_port = 0;
- cw.peer.s_addr = INADDR_ANY;
sel_init(&sel);
for (;;) {
static struct option opt[] = {
case 'h': help(stdout); exit(0);
case 'v': version(stdout); exit(0);
case 'u': usage(stdout); exit(0);
- case 'l': parseaddr(optarg, 0, &pt); cw.me.sin_port = pt; break;
- case 'p': parseaddr(optarg, &cw.peer, 0); break;
- case 'b':
- parseaddr(optarg, &bindaddr.sin_addr, 0);
- cw.me.sin_addr = bindaddr.sin_addr;
- break;
- case 'c':
- parseaddr(optarg, &connaddr.sin_addr, 0);
- connaddr.sin_port = pt;
- break;
+ case 'l': bindsvc = optarg; break;
+ case 'p': DA_PUSH(&peerhosts, optarg); break;
+ case 'b': DA_PUSH(&bindhosts, optarg); break;
+ case 'c': connhost = optarg; break;
default: f |= f_bogus; break;
}
}
if (optind + 2 != argc || (f&f_bogus)) { usage(stderr); exit(1); }
- udp_me.sin_family = udp_peer.sin_family = AF_INET;
- parseaddr(argv[optind], &udp_me.sin_addr, &pt);
- udp_me.sin_port = pt;
- parseaddr(argv[optind + 1], &udp_peer.sin_addr, &pt);
- udp_peer.sin_port = pt;
+ if (DA_LEN(&bindhosts) && !bindsvc && !connhost)
+ die(1, "bind addr only makes sense when listening or connecting");
+ if (DA_LEN(&peerhosts) && !bindsvc)
+ die(1, "peer addr only makes sense when listening");
+ if (bindsvc && connhost)
+ die(1, "can't listen and connect");
+
+ aihint.ai_family = AF_INET;
+ DA_CREATE(&cw.me); DA_CREATE(&cw.peer);
+
+ n = DA_LEN(&bindhosts);
+ if (n || bindsvc) {
+ aihint.ai_socktype = SOCK_STREAM;
+ aihint.ai_protocol = IPPROTO_TCP;
+ aihint.ai_flags = AI_ADDRCONFIG | AI_PASSIVE;
+ if (!n) {
+ parseaddr(&aihint, 0, bindsvc, 0, &ailist);
+ pushaddrs(&cw.me, ailist);
+ freeaddrinfo(ailist);
+ } else if (!bindsvc) {
+ if (n != 1) die(1, "can only bind to one address as client");
+ parseaddr(&aihint, DA(&bindhosts)[0], 0, 0, &ailist);
+ for (ai = ailist; ai && !knownafp(ai->ai_family); ai = ai->ai_next);
+ if (!ai)
+ die(1, "no usable addresses returned for `%s'", DA(&bindhosts)[0]);
+ initaddr(&bindaddr, ai->ai_family);
+ copyaddr(&bindaddr, ai->ai_addr, caf_addr);
+ aihint.ai_family = ai->ai_family;
+ freeaddrinfo(ailist);
+ } else for (i = 0; i < n; i++) {
+ parseaddr(&aihint, DA(&bindhosts)[i], bindsvc, 0, &ailist);
+ pushaddrs(&cw.me, ailist);
+ freeaddrinfo(ailist);
+ }
+ if (bindsvc) {
+ cw.f |= cwf_port;
+ n = DA_LEN(&cw.me);
+ cw.sfv = xmalloc(n*sizeof(*cw.sfv));
+ }
+ }
- if ((fd_udp = socket(PF_INET, SOCK_DGRAM, 0)) < 0 ||
- bind(fd_udp, (struct sockaddr *)&udp_me, sizeof(udp_me)) ||
- connect(fd_udp, (struct sockaddr *)&udp_peer, sizeof(udp_peer)) ||
- setsockopt(fd_udp, SOL_SOCKET, SO_RCVBUF, &len, sizeof(len)) ||
- setsockopt(fd_udp, SOL_SOCKET, SO_SNDBUF, &len, sizeof(len)) ||
- nonblockify(fd_udp) || cloexec(fd_udp))
- die(1, "couldn't set up UDP socket: %s", strerror(errno));
+ n = DA_LEN(&peerhosts);
+ if (n) {
+ aihint.ai_socktype = SOCK_STREAM;
+ aihint.ai_protocol = IPPROTO_TCP;
+ aihint.ai_flags = AI_ADDRCONFIG;
+ for (i = 0; i < n; i++) {
+ parseaddr(&aihint, DA(&peerhosts)[i], 0, 0, &ailist);
+ pushaddrs(&cw.peer, ailist);
+ freeaddrinfo(ailist);
+ }
+ if (!DA_LEN(&cw.peer)) die(1, "no usable peer addresses");
+ }
- if (cw.me.sin_port != 0)
- dolisten();
- else if (connaddr.sin_addr.s_addr == INADDR_ANY)
- dofwd(STDIN_FILENO, STDOUT_FILENO);
- else {
- if ((fd = socket(PF_INET, SOCK_STREAM, 0)) < 0 ||
- bind(fd, (struct sockaddr *)&bindaddr, sizeof(bindaddr)) ||
- connect(fd, (struct sockaddr *)&connaddr, sizeof(connaddr)) ||
- nonblockify(fd) || cloexec(fd))
+ if (connhost) {
+ aihint.ai_socktype = SOCK_STREAM;
+ aihint.ai_protocol = IPPROTO_TCP;
+ aihint.ai_flags = AI_ADDRCONFIG;
+ parseaddr(&aihint, connhost, 0, paf_parse, &ailist);
+
+ for (ai = ailist; ai; ai = ai->ai_next) {
+ if ((fd = socket(ai->ai_family, SOCK_STREAM, IPPROTO_TCP)) >= 0 &&
+ !initsock(fd, ai->ai_family) &&
+ (!DA_LEN(&bindhosts) ||
+ !bind(fd, &bindaddr.sa, addrsz(&bindaddr))) &&
+ !connect(fd, ai->ai_addr, ai->ai_addrlen))
+ goto conn_tcp;
+ if (fd >= 0) close(fd);
+ }
+ die(1, "couldn't connect to TCP server: %s", strerror(errno));
+ conn_tcp:
+ if (nonblockify(fd) || cloexec(fd))
die(1, "couldn't connect to TCP server: %s", strerror(errno));
- dofwd(fd, fd);
}
+ aihint.ai_family = AF_INET;
+ aihint.ai_socktype = SOCK_DGRAM;
+ aihint.ai_protocol = IPPROTO_UDP;
+ aihint.ai_flags = AI_ADDRCONFIG | AI_PASSIVE;
+ parseaddr(&aihint, argv[optind], 0, paf_parse, &ailist);
+ for (ai = ailist; ai && !knownafp(ai->ai_family); ai = ai->ai_next);
+ if (!ai) die(1, "no usable addresses returned for `%s'", argv[optind]);
+ if ((fd_udp = socket(ai->ai_family, SOCK_DGRAM, IPPROTO_UDP)) < 0 ||
+ initsock(fd_udp, ai->ai_family) ||
+ 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, ai->ai_addr, ai->ai_addrlen))
+ die(1, "couldn't set up UDP socket: %s", strerror(errno));
+ freeaddrinfo(ailist);
+ aihint.ai_family = ai->ai_family;
+ aihint.ai_flags = AI_ADDRCONFIG;
+ parseaddr(&aihint, argv[optind + 1], 0, paf_parse, &ailist);
+ for (ai = ailist; ai; ai = ai->ai_next)
+ if (!connect(fd_udp, ai->ai_addr, ai->ai_addrlen)) goto conn_udp;
+ die(1, "couldn't set up UDP socket: %s", strerror(errno));
+conn_udp:
+
+ if (bindsvc) dolisten();
+ else if (connhost) dofwd(fd, fd);
+ else dofwd(STDIN_FILENO, STDOUT_FILENO);
+
for (;;) {
if (sel_select(&sel) && errno != EINTR)
die(1, "select failed: %s", strerror(errno));