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

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/media/webrtc/signaling/src/sipcc/plat/darwin/netif.c	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,253 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +#include <sys/types.h>
     1.9 +#include <sys/socket.h>
    1.10 +#include <net/if.h>
    1.11 +#include <net/if_dl.h>
    1.12 +#include <arpa/inet.h>
    1.13 +#include <unistd.h>
    1.14 +#include <ifaddrs.h>
    1.15 +
    1.16 +#include <string.h>
    1.17 +#include <strings.h>
    1.18 +#include <stdio.h>
    1.19 +
    1.20 +#define IN_ADDR_PLEN 16 /* including trailing '\0' */
    1.21 +#define IN6_ADDR_PLEN 128 /* including trailing '\0' */
    1.22 +#define MAC_ADDR_PLEN 18
    1.23 +
    1.24 +
    1.25 +//static char netif_ip_addr[IN6_ADDR_PLEN];
    1.26 +//static char netif_mac_addr[MAC_ADDR_PLEN];
    1.27 +
    1.28 +//static int
    1.29 +//eth_macaddr_ntop(const struct sockaddr_dl *sdl, char *dst, size_t dstsize)
    1.30 +//{
    1.31 +//    const unsigned char *lladdr = LLADDR(sdl);
    1.32 +//    int r;
    1.33 +//
    1.34 +//    r = snprintf(dst, dstsize, "%02x:%02x:%02x:%02x:%02x:%02x",
    1.35 +//                (int) lladdr[0], (int) lladdr[1], (int) lladdr[2],
    1.36 +//                (int) lladdr[3], (int) lladdr[4], (int) lladdr[5]);
    1.37 +//
    1.38 +//    if (r >= dstsize)
    1.39 +//        /* The destination buffer is too small */
    1.40 +//        return -1;
    1.41 +//    else
    1.42 +//        return 0;
    1.43 +//}
    1.44 +
    1.45 +
    1.46 +/*
    1.47 + * Find the local IP (v4 or v6) address used by the system to reach a given IP
    1.48 + * address.
    1.49 + */
    1.50 +//static int
    1.51 +//findlocaladdr(int af, struct sockaddr *dest, socklen_t destsize,
    1.52 +//                struct sockaddr *local, socklen_t *localsize)
    1.53 +//{
    1.54 +//    int fd;
    1.55 +//
    1.56 +//    if ((fd = socket(af, SOCK_DGRAM, 0)) == -1) {
    1.57 +//        perror("socket");
    1.58 +//        return -1;
    1.59 +//    }
    1.60 +//
    1.61 +//    if (connect(fd, dest, destsize) != 0) {
    1.62 +//        perror("connect");
    1.63 +//        close(fd);
    1.64 +//        return -1;
    1.65 +//    }
    1.66 +//
    1.67 +//    /*
    1.68 +//     * Retrieve the local address associated with the socket.
    1.69 +//     */
    1.70 +//    if (getsockname(fd, (struct sockaddr *) local, localsize) != 0) {
    1.71 +//        perror("getsockname");
    1.72 +//        close(fd);
    1.73 +//        return -1;
    1.74 +//    }
    1.75 +//
    1.76 +//    close(fd);
    1.77 +//
    1.78 +//    /* Check that the retrieved address is of the same family */
    1.79 +//    if (local->sa_family == af)
    1.80 +//        return 0;
    1.81 +//    else
    1.82 +//        return -1;
    1.83 +//}
    1.84 +//
    1.85 +//static int
    1.86 +//inaddrcmp(struct sockaddr *s1, struct sockaddr *s2)
    1.87 +//{
    1.88 +//    if (s1->sa_family == s2->sa_family && s1->sa_family == AF_INET) {
    1.89 +//        return bcmp(&((struct sockaddr_in *) s1)->sin_addr,
    1.90 +//                    &((struct sockaddr_in *) s2)->sin_addr,
    1.91 +//                    sizeof(struct in_addr));
    1.92 +//    } else if (s1->sa_family == s2->sa_family && s1->sa_family == AF_INET6) {
    1.93 +//        return bcmp(&((struct sockaddr_in6 *) s1)->sin6_addr,
    1.94 +//                    &((struct sockaddr_in6 *) s2)->sin6_addr,
    1.95 +//                    sizeof(struct in6_addr));
    1.96 +//    } else {
    1.97 +//        return -1;
    1.98 +//    }
    1.99 +//}
   1.100 +
   1.101 +/*
   1.102 + * Retrieve the IP address (and associated link-layer address) that will be
   1.103 + * used by the operating system as the source IP address when sending packets
   1.104 + * to the given destination address.
   1.105 + */
   1.106 +//static int
   1.107 +//getifaddr(int dstaf, struct sockaddr *dst, socklen_t dstsize,
   1.108 +//          struct sockaddr *ipaddr, size_t ipaddrsize, struct sockaddr_dl *lladdr)
   1.109 +//{
   1.110 +//    struct sockaddr_storage ss;
   1.111 +//    socklen_t sslen;
   1.112 +//    struct ifaddrs *ifap, *ifp;
   1.113 +//    char *ifname = NULL;
   1.114 +//    int found_lladdr = 0;
   1.115 +//
   1.116 +//    /*
   1.117 +//     * First, determine the local IP address that will be used to reach the
   1.118 +//     * given destination IP address.  From that we will retrieve the interface
   1.119 +//     * and then the MAC address.
   1.120 +//     * `dstaf' can only be AF_INET or AF_INET6.
   1.121 +//     */
   1.122 +//    bzero(&ss, sizeof(struct sockaddr_storage));
   1.123 +//    sslen = sizeof(struct sockaddr_storage);
   1.124 +//    if (findlocaladdr(dstaf, dst, dstsize, (struct sockaddr *) &ss, &sslen)
   1.125 +//            != 0) {
   1.126 +//        return -1;
   1.127 +//    }
   1.128 +//
   1.129 +//    /*
   1.130 +//     * Find the name of the network interface matching the address discovered
   1.131 +//     * using findlocaladdr().  Note that this is not garanteed to yield the
   1.132 +//     * correct result (or a result at all) because the network configuration
   1.133 +//     * may change between the call to findlocaladdr() and getifaddrs().
   1.134 +//     * But this should work in most cases.
   1.135 +//     */
   1.136 +//    if (getifaddrs(&ifap) == -1)
   1.137 +//        return -1;
   1.138 +//
   1.139 +//    for (ifp = ifap; ifp->ifa_next != NULL; ifp = ifp->ifa_next) {
   1.140 +//        if (inaddrcmp(ifp->ifa_addr, (struct sockaddr *) &ss) == 0) {
   1.141 +//            ifname = ifp->ifa_name;
   1.142 +//            break;
   1.143 +//        }
   1.144 +//    }
   1.145 +//
   1.146 +//    /*
   1.147 +//     * Get the link-layer address matching the interface name.
   1.148 +//     */
   1.149 +//    if (ifname != NULL) {
   1.150 +//        for (ifp = ifap; ifp->ifa_next != NULL; ifp = ifp->ifa_next) {
   1.151 +//            if (ifp->ifa_addr->sa_family == AF_LINK
   1.152 +//                && strcmp(ifname, ifp->ifa_name) == 0) {
   1.153 +//                bcopy(ifp->ifa_addr, lladdr, sizeof(struct sockaddr_dl));
   1.154 +//                found_lladdr = 1;
   1.155 +//                break;
   1.156 +//            }
   1.157 +//        }
   1.158 +//    }
   1.159 +//
   1.160 +//    freeifaddrs(ifap);
   1.161 +//
   1.162 +//    if (!found_lladdr) {
   1.163 +//        /* The link-layer address was not found! */
   1.164 +//        return -1;
   1.165 +//    } else {
   1.166 +//        /* Copy the IP address to the buffer provided by the caller */
   1.167 +//        bcopy(&ss, ipaddr, ipaddrsize);
   1.168 +//        return 0;
   1.169 +//    }
   1.170 +//}
   1.171 +//
   1.172 +//static int
   1.173 +//getifaddr2(int af, struct sockaddr *ipaddr, size_t ipaddrsize,
   1.174 +//           struct sockaddr_dl *lladdr)
   1.175 +//{
   1.176 +//    struct ifaddrs *ifap, *ifp;
   1.177 +//    char *ifname;
   1.178 +//    int found_lladdr = 0;
   1.179 +//
   1.180 +//    if (getifaddrs(&ifap) == -1)
   1.181 +//        return -1;
   1.182 +//
   1.183 +//    /* Walk the list to find an active interface with an IPv4 or IPv6 address */
   1.184 +//    for (ifp = ifap; ifp->ifa_next != NULL; ifp = ifp->ifa_next) {
   1.185 +//        if (ifp->ifa_flags & (IFF_UP|IFF_RUNNING)
   1.186 +//            && !(ifp->ifa_flags & IFF_LOOPBACK)
   1.187 +//            && ifp->ifa_addr->sa_family == af) {
   1.188 +//            ifname = ifp->ifa_name;
   1.189 +//            bcopy(ifp->ifa_addr, ipaddr, ipaddrsize);
   1.190 +//            break;
   1.191 +//        }
   1.192 +//    }
   1.193 +//
   1.194 +//    /* Get the matching link-layer address */
   1.195 +//    for (ifp = ifap; ifp->ifa_next != NULL; ifp = ifp->ifa_next) {
   1.196 +//        if (ifp->ifa_addr->sa_family == AF_LINK
   1.197 +//            && strcmp(ifname, ifp->ifa_name) == 0) {
   1.198 +//            bcopy(ifp->ifa_addr, lladdr, sizeof(struct sockaddr_dl));
   1.199 +//            found_lladdr = 1;
   1.200 +//        }
   1.201 +//    }
   1.202 +//
   1.203 +//    freeifaddrs(ifap);
   1.204 +//
   1.205 +//    if (found_lladdr)
   1.206 +//        return 0;
   1.207 +//    else
   1.208 +//        return -1;
   1.209 +//}
   1.210 +
   1.211 +//char *
   1.212 +//platGetIPAddr(void)
   1.213 +//{
   1.214 +//    struct sockaddr_in inaddr;
   1.215 +//    struct sockaddr_dl lladdr; /* Unused */
   1.216 +//
   1.217 +//    /*
   1.218 +//     * XXX We should use getifaddr() but need to figure out how this can be done
   1.219 +//     * (we would need the IP address of the CUCM at this stage).  Using
   1.220 +//     * getifaddr2() ATM.
   1.221 +//     */
   1.222 +//
   1.223 +//    /*
   1.224 +//     * XXX This will return an IPv4 address.  getifaddr() and getifaddr2() can
   1.225 +//     * handle IPv6 addresses properly though.
   1.226 +//     */
   1.227 +//
   1.228 +//    if (getifaddr2(AF_INET, (struct sockaddr *) &inaddr, sizeof(inaddr),
   1.229 +//            &lladdr) != 0)
   1.230 +//        return NULL;
   1.231 +//
   1.232 +//    inet_ntop(AF_INET, &inaddr.sin_addr, netif_ip_addr, IN_ADDR_PLEN);
   1.233 +//    return netif_ip_addr;
   1.234 +//}
   1.235 +
   1.236 +//void
   1.237 +//platGetMacAddr(char *maddr)
   1.238 +//{
   1.239 +//    struct sockaddr_in inaddr; /* Unused */
   1.240 +//    struct sockaddr_dl lladdr;
   1.241 +//
   1.242 +    /*
   1.243 +     * XXX Same comment applies (see platGetIPAddr).  Additionally, it is just
   1.244 +     * not possible to properly implement platGetIpAddr() and platGetMacAddr()
   1.245 +     * so that the caller has a guarantee that both address come from the same
   1.246 +     * network address.
   1.247 +     */
   1.248 +
   1.249 +//    if (getifaddr2(AF_INET, (struct sockaddr *) &inaddr, sizeof(inaddr),
   1.250 +//            &lladdr) != 0)
   1.251 +//        /* XXX */
   1.252 +//        bzero(maddr, MAC_ADDR_PLEN);
   1.253 +//
   1.254 +//    eth_macaddr_ntop(&lladdr, maddr, MAC_ADDR_PLEN);
   1.255 +//}
   1.256 +

mercurial