akaros/user/iplib/myipaddr.c
<<
>>
Prefs
   1/*
   2 * This file is part of the UCB release of Plan 9. It is subject to the license
   3 * terms in the LICENSE file found in the top-level directory of this
   4 * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
   5 * part of the UCB release of Plan 9, including this file, may be copied,
   6 * modified, propagated, or distributed except according to the terms contained
   7 * in the LICENSE file.
   8 */
   9#define _GNU_SOURCE
  10#include <stdlib.h>
  11
  12#include <iplib/iplib.h>
  13#include <parlib/parlib.h>
  14#include <signal.h>
  15#include <stdio.h>
  16#include <unistd.h>
  17
  18// find first ip addr that isn't the friggin loopback address
  19// unless there are no others
  20int myipaddr(uint8_t *ip, char *net)
  21{
  22        struct iplifc *lifc;
  23        struct ipifc *ifc;
  24
  25        lifc = get_first_noloop_iplifc(net, &ifc);
  26        if (!lifc) {
  27                ipmove(ip, IPnoaddr);
  28                return -1;
  29        }
  30        ipmove(ip, lifc->ip);
  31        free_ipifc(ifc);
  32        return 0;
  33}
  34
  35/* Finds the default router from net/iproute, stores the full IP address
  36 * (IPaddrlen) in addr.  Returns 0 on success. */
  37int my_router_addr(uint8_t *addr, char *net)
  38{
  39        char *line = NULL;
  40        size_t len = 0;
  41        ssize_t read;
  42        FILE *fp;
  43        char *p, *str;
  44        uint8_t ipaddr[IPaddrlen];
  45        uint8_t v4addr[IPaddrlen];
  46        char netpath[MAX_PATH_LEN];
  47        int ret = -1;
  48
  49        if (!net)
  50                net = "/net";
  51        snprintf(netpath, sizeof(netpath), "%s/iproute", net);
  52        fp = fopen(netpath, "r");
  53        if (!fp)
  54                return -1;
  55        while ((read = getline(&line, &len, fp)) != -1) {
  56                p = strchr(line, ' ');
  57                if (!p) {
  58                        werrstr("Malformed line, no initial space");
  59                        goto out;
  60                }
  61                *p++ = 0;
  62                parseip(ipaddr, line);
  63                if (isv4(ipaddr)) {
  64                        v6tov4(v4addr, ipaddr);
  65                        if (!equivip4(v4addr, IPnoaddr))
  66                                continue;
  67                } else {
  68                        if (!equivip6(ipaddr, IPnoaddr))
  69                                continue;
  70                }
  71                p = strchr(p, ' ');
  72                if (!p) {
  73                        werrstr("Malformed line, no second space %s", line);
  74                        goto out;
  75                }
  76                p++;
  77                str = p;
  78                p = strchr(p, ' ');
  79                if (!p) {
  80                        werrstr("Malformed line, no third space %s", line);
  81                        goto out;
  82                }
  83                *p++ = 0;
  84                parseip(addr, str);
  85                ret = 0;
  86        }
  87out:
  88        free(line);
  89        fclose(fp);
  90        return ret;
  91}
  92