LONGBOW_TEST_CASE(Local, _Inet6_BuildString)
{
    struct sockaddr_in6 addr_in6;
    memset(&addr_in6, 0, sizeof(struct sockaddr_in6));

    inet_pton(AF_INET6, "2001:720:1500:1::a100", &(addr_in6.sin6_addr));
    addr_in6.sin6_family = AF_INET6;
    addr_in6.sin6_port = htons(43215);

    char *expected = "inet6://[2001:720:1500:1::a100%0]:43215";

    CPIAddress *cpiaddr = cpiAddress_CreateFromInet6(&addr_in6);

    PARCBufferComposer *composer = parcBufferComposer_Create();
    _Inet6_BuildString(cpiaddr, composer);

    PARCBuffer *tempBuffer = parcBufferComposer_ProduceBuffer(composer);
    char *actual = parcBuffer_ToString(tempBuffer);
    parcBuffer_Release(&tempBuffer);
    parcBufferComposer_Release(&composer);

    assertTrue(strcmp(expected, actual) == 0, "Expected '%s' actual '%s'", expected, actual);
    parcMemory_Deallocate((void **) &actual);

    cpiAddress_Destroy(&cpiaddr);
}
LONGBOW_TEST_CASE(Global, cpiAddress_CreateFromInet6)
{
    struct sockaddr_in6 addr_in6;
    memset(&addr_in6, 0, sizeof(struct sockaddr_in6));

    inet_pton(AF_INET6, "2001:720:1500:1::a100", &(addr_in6.sin6_addr));
    addr_in6.sin6_family = AF_INET6;
    addr_in6.sin6_port = 0x0A0B;
    addr_in6.sin6_flowinfo = 0x01020304;

    CPIAddress *address = cpiAddress_CreateFromInet6(&addr_in6);

    struct sockaddr_in6 addr_test;
    bool success = cpiAddress_GetInet6(address, &addr_test);
    assertTrue(success, "Got false converting back address");

    assertTrue(memcmp(&addr_in6, &addr_test, sizeof(struct sockaddr_in6)) == 0, "Got mismatch addressed");

    assertTrue(cpiAddress_GetType(address) == cpiAddressType_INET6,
               "Got wrong address type, expected %d, got %d", cpiAddressType_INET6, cpiAddress_GetType(address));

    PARCJSON *json = cpiAddress_ToJson(address);
    CPIAddress *fromjson = cpiAddress_CreateFromJson(json);

    assertTrue(parcBuffer_Equals(address->blob, fromjson->blob), "fromjson blob does not equal known address");
    assertTrue(cpiAddress_Equals(address, fromjson), "cpiAddress_Equals broken for INET6 type");

    CPIAddress *copy = cpiAddress_Copy(address);
    assertTrue(cpiAddress_Equals(copy, address), "Copy and address not equal for INET6");

    parcJSON_Release(&json);
    cpiAddress_Destroy(&address);
    cpiAddress_Destroy(&copy);
    cpiAddress_Destroy(&fromjson);
}
LONGBOW_TEST_CASE(Global, cpiAddress_ToString_INET6)
{
    struct sockaddr_in6 addr_in6;
    memset(&addr_in6, 0, sizeof(struct sockaddr_in6));

    inet_pton(AF_INET6, "2001:720:1500:1::a100", &(addr_in6.sin6_addr));
    addr_in6.sin6_family = AF_INET6;
    addr_in6.sin6_port = htons(43215);

    char *expected = "inet6://[2001:720:1500:1::a100%0]:43215";

    CPIAddress *cpiaddr = cpiAddress_CreateFromInet6(&addr_in6);
    char *actual = cpiAddress_ToString(cpiaddr);

    assertTrue(strcmp(expected, actual) == 0, "Expected '%s', actual '%s'", expected, actual);

    parcMemory_Deallocate((void **) &actual);
    cpiAddress_Destroy(&cpiaddr);
}
Esempio n. 4
0
CPIInterfaceSet *
metisSystem_Interfaces(MetisForwarder *metis)
{
    CPIInterfaceSet *set = cpiInterfaceSet_Create();

    // this is the dynamically allocated head of the list
    struct ifaddrs *ifaddr;
    int failure = getifaddrs(&ifaddr);
    assertFalse(failure, "Error getifaddrs: (%d) %s", errno, strerror(errno));

    struct ifaddrs *next;
    for (next = ifaddr; next != NULL; next = next->ifa_next) {
        if ((next->ifa_addr == NULL) || ((next->ifa_flags & IFF_UP) == 0)) {
            continue;
        }

        // This assumes the LINK address comes first so we can get the MTU
        // when the interface is created.

        CPIInterface *iface = cpiInterfaceSet_GetByName(set, next->ifa_name);
        if (iface == NULL) {
            unsigned mtu = 0;

            if (next->ifa_data != NULL) {
                struct if_data *ifdata = (struct if_data *) next->ifa_data;
                mtu = ifdata->ifi_mtu;
            }

            iface = cpiInterface_Create(next->ifa_name,
                                        metisForwarder_GetNextConnectionId(metis),
                                        next->ifa_flags & IFF_LOOPBACK,
                                        next->ifa_flags & IFF_MULTICAST,
                                        mtu);

            cpiInterfaceSet_Add(set, iface);
        }

        int family = next->ifa_addr->sa_family;
        switch (family) {
            case AF_INET: {
                CPIAddress *address = cpiAddress_CreateFromInet((struct sockaddr_in *) next->ifa_addr);
                cpiInterface_AddAddress(iface, address);
                break;
            }

            case AF_INET6: {
                CPIAddress *address = cpiAddress_CreateFromInet6((struct sockaddr_in6 *) next->ifa_addr);
                cpiInterface_AddAddress(iface, address);
                break;
            }

            case AF_LINK: {
                struct sockaddr_dl *addr_dl = (struct sockaddr_dl *) next->ifa_addr;

                // skip links with 0-length address
                if (addr_dl->sdl_alen > 0) {
                    // addr_dl->sdl_data[12] contains the interface name followed by the MAC address, so
                    // need to offset in to the array past the interface name.
                    CPIAddress *address = cpiAddress_CreateFromLink((uint8_t *) &addr_dl->sdl_data[ addr_dl->sdl_nlen], addr_dl->sdl_alen);
                    cpiInterface_AddAddress(iface, address);
                }
                break;
            }
        }
    }

    freeifaddrs(ifaddr);

    return set;
}