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