From b2f74a8a9fb24520ad79ad03ae265950fc737efa Mon Sep 17 00:00:00 2001 Message-Id: From: Mark Wooding Date: Mon, 4 Jun 2018 02:18:44 +0100 Subject: [PATCH] linux.c: Factor out trundling through the `tcp' file. Organization: Straylight/Edgeware From: Mark Wooding No functional change, but this lays important groundwork for the next bugfix. --- linux.c | 120 ++++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 78 insertions(+), 42 deletions(-) diff --git a/linux.c b/linux.c index ec4fe4e..7508343 100644 --- a/linux.c +++ b/linux.c @@ -190,58 +190,48 @@ done: return (rc); } -/* Find out who is responsible for the connection described in the query Q. - * Write the answer to Q. Errors are logged and reported via the query - * structure. +/* Initially, PP points into a string containing whitespace-separated fields. + * Point P to the next field, null-terminate it, and advance PP so that we + * can read the next field in the next call. */ -void identify(struct query *q) +#define NEXTFIELD do { \ + for (p = pp; isspace((unsigned char)*p); p++); \ + for (pp = p; *pp && !isspace((unsigned char)*pp); pp++); \ + if (*pp) *pp++ = 0; \ +} while (0) + +/* Search the `tcp' connection table for the address family AO, looking for a + * connection between the addresses in QS. GWP is nonzero if the query's + * remote address is our gateway and we shouldn't expect the remote address + * in the system table to actually match it because of NAT. Return nonzero + * if we have filled in Q conclusively; return zero if the caller should try + * a different approach. + */ +static int search_tcp_file(struct query *q, int gwp, + const struct addrops *ao, + struct socket qs[NDIR]) { FILE *fp = 0; dstr d = DSTR_INIT; char *p, *pp; - struct socket s[4]; + struct socket s[NDIR]; int i; - int gwp = 0; - unsigned fl; -#define F_SADDR 1u -#define F_SPORT 2u -#define F_DADDR 4u -#define F_DPORT 8u -#define F_ALL (F_SADDR | F_SPORT | F_DADDR | F_DPORT) -#define F_ESTAB 16u uid_t uid; enum { LOC, REM, ST, UID, NFIELD }; int f, ff[NFIELD]; - - /* If we have a default gateway, and it matches the remote address then - * 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) && - q->ao->addreq(&s[0].addr, &q->s[R].addr)) - gwp = 1; + int rc = 1; /* Open the relevant TCP connection table. */ - if ((fp = fopen(q->ao->sys->procfile, "r")) == 0) { + if ((fp = fopen(ao->sys->procfile, "r")) == 0) { logmsg(q, LOG_ERR, "failed to open `%s' for reading: %s", - q->ao->sys->procfile, strerror(errno)); + ao->sys->procfile, strerror(errno)); goto err_unk; } - /* Initially, PP points into a string containing whitespace-separated - * fields. Point P to the next field, null-terminate it, and advance PP - * so that we can read the next field in the next call. - */ -#define NEXTFIELD do { \ - for (p = pp; isspace((unsigned char)*p); p++); \ - for (pp = p; *pp && !isspace((unsigned char)*pp); pp++); \ - if (*pp) *pp++ = 0; \ -} while (0) - /* Read the header line from the file. */ if (dstr_putline(&d, fp) == EOF) { logmsg(q, LOG_ERR, "failed to read header line from `%s': %s", - q->ao->sys->procfile, + ao->sys->procfile, ferror(fp) ? strerror(errno) : "unexpected EOF"); goto err_unk; } @@ -275,7 +265,7 @@ void identify(struct query *q) for (i = 0; i < NFIELD; i++) { if (ff[i] < 0) { logmsg(q, LOG_ERR, "failed to find required fields in `%s'", - q->ao->sys->procfile); + ao->sys->procfile); goto err_unk; } } @@ -312,13 +302,13 @@ void identify(struct query *q) * query is our gateway then don't check the remote address in the * field (but do check the port number). */ - if (q->ao->sys->parseaddr(&p, &s[i].addr)) goto next_row; + if (ao->sys->parseaddr(&p, &s[i].addr)) goto next_row; if (*p != ':') break; p++; s[i].port = strtoul(p, 0, 16); if ((i == R && gwp) ? - q->s[R].port != s[i].port : - !sockeq(q->ao, &q->s[i], &s[i])) + qs[R].port != s[i].port : + !sockeq(ao, &qs[i], &s[i])) goto next_row; } @@ -329,7 +319,7 @@ void identify(struct query *q) if (uid != -1) { q->resp = R_UID; q->u.uid = uid; - if (gwp) q->s[R].addr = s[i].addr; + if (gwp) qs[R].addr = s[i].addr; goto done; } next_row:; @@ -338,9 +328,55 @@ void identify(struct query *q) /* We got to the end of the file and didn't find anything. */ if (ferror(fp)) { logmsg(q, LOG_ERR, "failed to read connection table `%s': %s", - q->ao->sys->procfile, strerror(errno)); + ao->sys->procfile, strerror(errno)); goto err_unk; } + rc = 0; + +err_unk: + /* Something went wrong and the protocol can't express what. We should + * have logged what the problem actually was. + */ + q->resp = R_ERROR; + q->u.error = E_UNKNOWN; + +done: + /* All done. */ + dstr_destroy(&d); + if (fp) fclose(fp); + return (rc); +} + +/* Find out who is responsible for the connection described in the query Q. + * Write the answer to Q. Errors are logged and reported via the query + * structure. + */ +void identify(struct query *q) +{ + FILE *fp = 0; + dstr d = DSTR_INIT; + char *p, *pp; + struct socket s[4]; + int i; + int gwp = 0; + unsigned fl; +#define F_SADDR 1u +#define F_SPORT 2u +#define F_DADDR 4u +#define F_DPORT 8u +#define F_ALL (F_SADDR | F_SPORT | F_DADDR | F_DPORT) +#define F_ESTAB 16u + + /* If we have a default gateway, and it matches the remote address then + * 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) && + q->ao->addreq(&s[0].addr, &q->s[R].addr)) + gwp = 1; + + /* Search the main `tcp' table. */ + if (search_tcp_file(q, gwp, q->ao, q->s)) goto done; /* If we opened the NAT table file, and we're using IPv4, then check to see * whether we should proxy the connection. At least the addresses in this @@ -457,8 +493,6 @@ void identify(struct query *q) } } -#undef NEXTFIELD - /* We didn't find a match anywhere. How unfortunate. */ logmsg(q, LOG_NOTICE, "connection not found"); q->resp = R_ERROR; @@ -478,6 +512,8 @@ done: if (fp) fclose(fp); } +#undef NEXTFIELD + /* Initialize the system-specific code. */ void init_sys(void) { -- [mdw]