linux.c (parseaddr_ipv6): Fix operator spacing.
[yaid] / linux.c
diff --git a/linux.c b/linux.c
index d7d8475..b37f7f4 100644 (file)
--- a/linux.c
+++ b/linux.c
@@ -64,8 +64,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,7 +97,7 @@ 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. */
@@ -163,7 +163,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 +201,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;
 
@@ -296,21 +296,23 @@ 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[0].addr)) goto next_row;
+      if (q->ao->sys->parseaddr(&p, &s[i].addr)) goto next_row;
       if (*p != ':') break; p++;
-      s[0].port = strtoul(p, 0, 16);
+      s[i].port = strtoul(p, 0, 16);
       if ((i == R && gwp) ?
-           q->s[R].port != s[0].port :
-           !sockeq(q->ao, &q->s[i], &s[0]))
+           q->s[R].port != s[i].port :
+           !sockeq(q->ao, &q->s[i], &s[i]))
        goto next_row;
     }
 
     /* We got to the end, and everything matched.  If we found a UID then
-     * we're done.
+     * we're done.  If the apparent remote address is our gateway then copy
+     * the true one into the query structure.
      */
     if (uid != -1) {
       q->resp = R_UID;
       q->u.uid = uid;
+      if (gwp) q->s[R].addr = s[i].addr;
       goto done;
     }
   next_row:;