media/webrtc/signaling/src/sipcc/plat/darwin/netif.c

Thu, 15 Jan 2015 15:59:08 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 15 Jan 2015 15:59:08 +0100
branch
TOR_BUG_9701
changeset 10
ac0c01689b40
permissions
-rw-r--r--

Implement a real Private Browsing Mode condition by changing the API/ABI;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.

     1 /* This Source Code Form is subject to the terms of the Mozilla Public
     2  * License, v. 2.0. If a copy of the MPL was not distributed with this
     3  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     5 #include <sys/types.h>
     6 #include <sys/socket.h>
     7 #include <net/if.h>
     8 #include <net/if_dl.h>
     9 #include <arpa/inet.h>
    10 #include <unistd.h>
    11 #include <ifaddrs.h>
    13 #include <string.h>
    14 #include <strings.h>
    15 #include <stdio.h>
    17 #define IN_ADDR_PLEN 16 /* including trailing '\0' */
    18 #define IN6_ADDR_PLEN 128 /* including trailing '\0' */
    19 #define MAC_ADDR_PLEN 18
    22 //static char netif_ip_addr[IN6_ADDR_PLEN];
    23 //static char netif_mac_addr[MAC_ADDR_PLEN];
    25 //static int
    26 //eth_macaddr_ntop(const struct sockaddr_dl *sdl, char *dst, size_t dstsize)
    27 //{
    28 //    const unsigned char *lladdr = LLADDR(sdl);
    29 //    int r;
    30 //
    31 //    r = snprintf(dst, dstsize, "%02x:%02x:%02x:%02x:%02x:%02x",
    32 //                (int) lladdr[0], (int) lladdr[1], (int) lladdr[2],
    33 //                (int) lladdr[3], (int) lladdr[4], (int) lladdr[5]);
    34 //
    35 //    if (r >= dstsize)
    36 //        /* The destination buffer is too small */
    37 //        return -1;
    38 //    else
    39 //        return 0;
    40 //}
    43 /*
    44  * Find the local IP (v4 or v6) address used by the system to reach a given IP
    45  * address.
    46  */
    47 //static int
    48 //findlocaladdr(int af, struct sockaddr *dest, socklen_t destsize,
    49 //                struct sockaddr *local, socklen_t *localsize)
    50 //{
    51 //    int fd;
    52 //
    53 //    if ((fd = socket(af, SOCK_DGRAM, 0)) == -1) {
    54 //        perror("socket");
    55 //        return -1;
    56 //    }
    57 //
    58 //    if (connect(fd, dest, destsize) != 0) {
    59 //        perror("connect");
    60 //        close(fd);
    61 //        return -1;
    62 //    }
    63 //
    64 //    /*
    65 //     * Retrieve the local address associated with the socket.
    66 //     */
    67 //    if (getsockname(fd, (struct sockaddr *) local, localsize) != 0) {
    68 //        perror("getsockname");
    69 //        close(fd);
    70 //        return -1;
    71 //    }
    72 //
    73 //    close(fd);
    74 //
    75 //    /* Check that the retrieved address is of the same family */
    76 //    if (local->sa_family == af)
    77 //        return 0;
    78 //    else
    79 //        return -1;
    80 //}
    81 //
    82 //static int
    83 //inaddrcmp(struct sockaddr *s1, struct sockaddr *s2)
    84 //{
    85 //    if (s1->sa_family == s2->sa_family && s1->sa_family == AF_INET) {
    86 //        return bcmp(&((struct sockaddr_in *) s1)->sin_addr,
    87 //                    &((struct sockaddr_in *) s2)->sin_addr,
    88 //                    sizeof(struct in_addr));
    89 //    } else if (s1->sa_family == s2->sa_family && s1->sa_family == AF_INET6) {
    90 //        return bcmp(&((struct sockaddr_in6 *) s1)->sin6_addr,
    91 //                    &((struct sockaddr_in6 *) s2)->sin6_addr,
    92 //                    sizeof(struct in6_addr));
    93 //    } else {
    94 //        return -1;
    95 //    }
    96 //}
    98 /*
    99  * Retrieve the IP address (and associated link-layer address) that will be
   100  * used by the operating system as the source IP address when sending packets
   101  * to the given destination address.
   102  */
   103 //static int
   104 //getifaddr(int dstaf, struct sockaddr *dst, socklen_t dstsize,
   105 //          struct sockaddr *ipaddr, size_t ipaddrsize, struct sockaddr_dl *lladdr)
   106 //{
   107 //    struct sockaddr_storage ss;
   108 //    socklen_t sslen;
   109 //    struct ifaddrs *ifap, *ifp;
   110 //    char *ifname = NULL;
   111 //    int found_lladdr = 0;
   112 //
   113 //    /*
   114 //     * First, determine the local IP address that will be used to reach the
   115 //     * given destination IP address.  From that we will retrieve the interface
   116 //     * and then the MAC address.
   117 //     * `dstaf' can only be AF_INET or AF_INET6.
   118 //     */
   119 //    bzero(&ss, sizeof(struct sockaddr_storage));
   120 //    sslen = sizeof(struct sockaddr_storage);
   121 //    if (findlocaladdr(dstaf, dst, dstsize, (struct sockaddr *) &ss, &sslen)
   122 //            != 0) {
   123 //        return -1;
   124 //    }
   125 //
   126 //    /*
   127 //     * Find the name of the network interface matching the address discovered
   128 //     * using findlocaladdr().  Note that this is not garanteed to yield the
   129 //     * correct result (or a result at all) because the network configuration
   130 //     * may change between the call to findlocaladdr() and getifaddrs().
   131 //     * But this should work in most cases.
   132 //     */
   133 //    if (getifaddrs(&ifap) == -1)
   134 //        return -1;
   135 //
   136 //    for (ifp = ifap; ifp->ifa_next != NULL; ifp = ifp->ifa_next) {
   137 //        if (inaddrcmp(ifp->ifa_addr, (struct sockaddr *) &ss) == 0) {
   138 //            ifname = ifp->ifa_name;
   139 //            break;
   140 //        }
   141 //    }
   142 //
   143 //    /*
   144 //     * Get the link-layer address matching the interface name.
   145 //     */
   146 //    if (ifname != NULL) {
   147 //        for (ifp = ifap; ifp->ifa_next != NULL; ifp = ifp->ifa_next) {
   148 //            if (ifp->ifa_addr->sa_family == AF_LINK
   149 //                && strcmp(ifname, ifp->ifa_name) == 0) {
   150 //                bcopy(ifp->ifa_addr, lladdr, sizeof(struct sockaddr_dl));
   151 //                found_lladdr = 1;
   152 //                break;
   153 //            }
   154 //        }
   155 //    }
   156 //
   157 //    freeifaddrs(ifap);
   158 //
   159 //    if (!found_lladdr) {
   160 //        /* The link-layer address was not found! */
   161 //        return -1;
   162 //    } else {
   163 //        /* Copy the IP address to the buffer provided by the caller */
   164 //        bcopy(&ss, ipaddr, ipaddrsize);
   165 //        return 0;
   166 //    }
   167 //}
   168 //
   169 //static int
   170 //getifaddr2(int af, struct sockaddr *ipaddr, size_t ipaddrsize,
   171 //           struct sockaddr_dl *lladdr)
   172 //{
   173 //    struct ifaddrs *ifap, *ifp;
   174 //    char *ifname;
   175 //    int found_lladdr = 0;
   176 //
   177 //    if (getifaddrs(&ifap) == -1)
   178 //        return -1;
   179 //
   180 //    /* Walk the list to find an active interface with an IPv4 or IPv6 address */
   181 //    for (ifp = ifap; ifp->ifa_next != NULL; ifp = ifp->ifa_next) {
   182 //        if (ifp->ifa_flags & (IFF_UP|IFF_RUNNING)
   183 //            && !(ifp->ifa_flags & IFF_LOOPBACK)
   184 //            && ifp->ifa_addr->sa_family == af) {
   185 //            ifname = ifp->ifa_name;
   186 //            bcopy(ifp->ifa_addr, ipaddr, ipaddrsize);
   187 //            break;
   188 //        }
   189 //    }
   190 //
   191 //    /* Get the matching link-layer address */
   192 //    for (ifp = ifap; ifp->ifa_next != NULL; ifp = ifp->ifa_next) {
   193 //        if (ifp->ifa_addr->sa_family == AF_LINK
   194 //            && strcmp(ifname, ifp->ifa_name) == 0) {
   195 //            bcopy(ifp->ifa_addr, lladdr, sizeof(struct sockaddr_dl));
   196 //            found_lladdr = 1;
   197 //        }
   198 //    }
   199 //
   200 //    freeifaddrs(ifap);
   201 //
   202 //    if (found_lladdr)
   203 //        return 0;
   204 //    else
   205 //        return -1;
   206 //}
   208 //char *
   209 //platGetIPAddr(void)
   210 //{
   211 //    struct sockaddr_in inaddr;
   212 //    struct sockaddr_dl lladdr; /* Unused */
   213 //
   214 //    /*
   215 //     * XXX We should use getifaddr() but need to figure out how this can be done
   216 //     * (we would need the IP address of the CUCM at this stage).  Using
   217 //     * getifaddr2() ATM.
   218 //     */
   219 //
   220 //    /*
   221 //     * XXX This will return an IPv4 address.  getifaddr() and getifaddr2() can
   222 //     * handle IPv6 addresses properly though.
   223 //     */
   224 //
   225 //    if (getifaddr2(AF_INET, (struct sockaddr *) &inaddr, sizeof(inaddr),
   226 //            &lladdr) != 0)
   227 //        return NULL;
   228 //
   229 //    inet_ntop(AF_INET, &inaddr.sin_addr, netif_ip_addr, IN_ADDR_PLEN);
   230 //    return netif_ip_addr;
   231 //}
   233 //void
   234 //platGetMacAddr(char *maddr)
   235 //{
   236 //    struct sockaddr_in inaddr; /* Unused */
   237 //    struct sockaddr_dl lladdr;
   238 //
   239     /*
   240      * XXX Same comment applies (see platGetIPAddr).  Additionally, it is just
   241      * not possible to properly implement platGetIpAddr() and platGetMacAddr()
   242      * so that the caller has a guarantee that both address come from the same
   243      * network address.
   244      */
   246 //    if (getifaddr2(AF_INET, (struct sockaddr *) &inaddr, sizeof(inaddr),
   247 //            &lladdr) != 0)
   248 //        /* XXX */
   249 //        bzero(maddr, MAC_ADDR_PLEN);
   250 //
   251 //    eth_macaddr_ntop(&lladdr, maddr, MAC_ADDR_PLEN);
   252 //}

mercurial