X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~mdw/git/yaid/blobdiff_plain/223e3e2be7a30ab58804b9b741c9438b123a16fd..d817f2c3f8bbfcacd2a9cb255b66bf7d9d5dedfa:/linux.c diff --git a/linux.c b/linux.c index 2c9a1d7..ec4fe4e 100644 --- a/linux.c +++ b/linux.c @@ -34,6 +34,22 @@ /*----- Static variables --------------------------------------------------*/ static FILE *natfp; /* File handle for NAT table */ +static int randfd; /* File descriptor for random data */ + +/*----- Miscellaneous system services -------------------------------------*/ + +/* Fill the buffer at P with SZ random bytes. The buffer will be moderately + * large: this is intended to be a low-level interface, not a general-purpose + * utility. + */ +void fill_random(void *p, size_t sz) +{ + ssize_t n; + + n = read(randfd, p, sz); + if (n < 0) fatal("error reading `/dev/urandom': %s", strerror(errno)); + else if (n < sz) fatal("unexpected short read from `/dev/urandom'"); +} /*----- Address-type operations -------------------------------------------*/ @@ -64,8 +80,8 @@ static int parseaddr_ipv6(char **pp, union addr *a) y = 0; for (j = 0; j < 8; j++) { if ('0' <= *p && *p <= '9') x = *p - '0'; - else if ('a' <= *p && *p <= 'f') x = *p - 'a'+ 10; - else if ('A' <= *p && *p <= 'F') x = *p - 'A'+ 10; + else if ('a' <= *p && *p <= 'f') x = *p - 'a' + 10; + else if ('A' <= *p && *p <= 'F') x = *p - 'A' + 10; else return (-1); y = (y << 4) | x; p++; @@ -97,12 +113,12 @@ static int get_default_gw(int af, union addr *a) const struct rtattr *rta; const struct rtmsg *rtm; ssize_t n, nn; - int rc = 0; + int rc = -1; static unsigned long seq = 0x48b4aec4; /* Open a netlink socket for interrogating the kernel. */ if ((fd = socket(AF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE)) < 0) - die(1, "failed to create netlink socket: %s", strerror(errno)); + fatal("failed to create netlink socket: %s", strerror(errno)); /* We want to read the routing table. There doesn't seem to be a good way * to do this without just crawling through the whole thing. @@ -119,14 +135,14 @@ static int get_default_gw(int af, union addr *a) rtgen->rtgen_family = af; if (write(fd, nlmsg, nlmsg->nlmsg_len) < 0) - die(1, "failed to send RTM_GETROUTE request: %s", strerror(errno)); + fatal("failed to send RTM_GETROUTE request: %s", strerror(errno)); /* Now we try to parse the answer. */ for (;;) { /* Not finished yet, so read another chunk of answer. */ if ((n = read(fd, buf, sizeof(buf))) < 0) - die(1, "failed to read RTM_GETROUTE response: %s", strerror(errno)); + fatal("failed to read RTM_GETROUTE response: %s", strerror(errno)); /* Start at the beginning of the response. */ nlmsg = (struct nlmsghdr *)buf; @@ -163,7 +179,7 @@ static int get_default_gw(int af, union addr *a) if (rta->rta_type == RTA_GATEWAY) { assert(RTA_PAYLOAD(rta) <= sizeof(*a)); memcpy(a, RTA_DATA(rta), RTA_PAYLOAD(rta)); - rc = 1; + rc = 0; } } } @@ -201,7 +217,7 @@ void identify(struct query *q) * this may be a proxy connection from our NAT, so remember this, and don't * inspect the remote addresses in the TCP tables. */ - if (get_default_gw(q->ao->af, &s[0].addr) && + if (!get_default_gw(q->ao->af, &s[0].addr) && q->ao->addreq(&s[0].addr, &q->s[R].addr)) gwp = 1; @@ -297,7 +313,8 @@ void identify(struct query *q) * field (but do check the port number). */ if (q->ao->sys->parseaddr(&p, &s[i].addr)) goto next_row; - if (*p != ':') break; p++; + if (*p != ':') break; + p++; s[i].port = strtoul(p, 0, 16); if ((i == R && gwp) ? q->s[R].port != s[i].port : @@ -381,7 +398,7 @@ void identify(struct query *q) } } -#ifdef notdef +#ifdef DEBUG { /* Print the record we found. */ dstr dd = DSTR_INIT; @@ -417,6 +434,13 @@ void identify(struct query *q) !sockeq(q->ao, &s[i^1], &q->s[R])) continue; + /* As a trap for the unwary, this file contains unhelpful entries which + * just mirror the source/destination addresses. If this is one of + * those, we'll be stuck in a cycle talking to ourselves. + */ + if (sockeq(q->ao, &s[i], &s[i^3])) + continue; + /* We win. The remaining address must be the client host. We should * proxy this query. */ @@ -457,11 +481,18 @@ done: /* Initialize the system-specific code. */ void init_sys(void) { + /* Open the NAT connection map. */ if ((natfp = fopen("/proc/net/nf_conntrack", "r")) == 0 && errno != ENOENT) { die(1, "failed to open `/proc/net/nf_conntrack' for reading: %s", strerror(errno)); } + + /* Open the random data source. */ + if ((randfd = open("/dev/urandom", O_RDONLY)) < 0) { + die(1, "failed to open `/dev/urandom' for reading: %s", + strerror(errno)); + } } /*----- That's all, folks -------------------------------------------------*/