LONGBOW_TEST_CASE(Global, cpiAddress_Equals_ReallyEqual) { struct sockaddr_in addr_in; memset(&addr_in, 0, sizeof(struct sockaddr_in)); addr_in.sin_addr.s_addr = 0x01020304; addr_in.sin_family = AF_INET; addr_in.sin_port = 0x0A0B; CPIAddress *a = cpiAddress_CreateFromInet(&addr_in); CPIAddress *b = cpiAddress_CreateFromInet(&addr_in); assertTrue(cpiAddress_Equals(a, b), "Equals did not compare two equal addresses: %s and %s", cpiAddress_ToString(a), cpiAddress_ToString(b)); cpiAddress_Destroy(&a); cpiAddress_Destroy(&b); }
LONGBOW_TEST_CASE(Global, cpiAddress_ToString_INET) { struct sockaddr_in *addr_in = parcNetwork_SockInet4Address("1.2.3.4", 12345); char expected[] = "inet4://1.2.3.4:12345"; CPIAddress *cpiaddr = cpiAddress_CreateFromInet(addr_in); char *actual = cpiAddress_ToString(cpiaddr); assertTrue(strcmp(actual, expected) == 0, "Bad string, expected '%s' got '%s'", expected, actual); parcMemory_Deallocate((void **) &actual); cpiAddress_Destroy(&cpiaddr); parcMemory_Deallocate((void **) &addr_in); }
LONGBOW_TEST_CASE(Global, cpiAddress_Equals_NotEqual) { struct sockaddr_in addr_in; memset(&addr_in, 0, sizeof(struct sockaddr_in)); addr_in.sin_addr.s_addr = 0x01020304; addr_in.sin_family = AF_INET; addr_in.sin_port = 0x0A0B; CPIAddress *a = cpiAddress_CreateFromInet(&addr_in); CPIAddress *b = cpiAddress_CreateFromInterface(1); assertFalse(cpiAddress_Equals(a, b), "Equals failed on different addresses: %s and %s", cpiAddress_ToString(a), cpiAddress_ToString(b)); cpiAddress_Destroy(&a); cpiAddress_Destroy(&b); }
LONGBOW_TEST_CASE(Global, cpiAddress_CreateFromInet) { struct sockaddr_in addr_in; struct sockaddr_in addr_test; memset(&addr_in, 0, sizeof(struct sockaddr_in)); addr_in.sin_addr.s_addr = 0x01020304; addr_in.sin_family = AF_INET; addr_in.sin_port = 0x0A0B; CPIAddress *address = cpiAddress_CreateFromInet(&addr_in); bool success = cpiAddress_GetInet(address, &addr_test); assertTrue(success, "Got false converting back address"); assertTrue(memcmp(&addr_in, &addr_test, sizeof(struct sockaddr_in)) == 0, "Got mismatch addressed"); assertTrue(cpiAddress_GetType(address) == cpiAddressType_INET, "Got wrong address type, expected %d, got %d", cpiAddressType_INET, cpiAddress_GetType(address)); PARCJSON *json = cpiAddress_ToJson(address); CPIAddress *fromjson = cpiAddress_CreateFromJson(json); assertTrue(cpiAddress_GetType(address) == cpiAddress_GetType(fromjson), "fromjson type does not equal known"); assertTrue(parcBuffer_Equals(address->blob, fromjson->blob), "fromjson blob does not equal known address"); assertTrue(cpiAddress_Equals(address, fromjson), "cpiAddress_Equals broken for INET type"); // This test does too much. Case 1032 CPIAddress *copy = cpiAddress_Copy(address); assertTrue(cpiAddress_Equals(copy, address), "Copy and address not equal for INET"); cpiAddress_Destroy(©); cpiAddress_Destroy(&fromjson); parcJSON_Release(&json); cpiAddress_Destroy(&address); return; }
LONGBOW_TEST_CASE(Local, _Inet_BuildString) { struct sockaddr_in addr_in; addr_in.sin_addr.s_addr = 0x04030201; addr_in.sin_port = htons(12345); char *expected = "inet4://1.2.3.4:12345"; CPIAddress *cpiaddr = cpiAddress_CreateFromInet(&addr_in); PARCBufferComposer *composer = parcBufferComposer_Create(); _Inet_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); }
/* 3) create a tunnel from A->B. */ // connect from any address struct sockaddr_in metisA_AnyIpAddress; memset(&metisA_AnyIpAddress, 0, sizeof(metisA_AnyIpAddress)); metisA_AnyIpAddress.sin_family = PF_INET; metisA_AnyIpAddress.sin_addr.s_addr = INADDR_ANY; // connect to 127.0.0.1:10002 struct sockaddr_in metisB_LoopbackAddress; memset(&metisB_LoopbackAddress, 0, sizeof(metisB_LoopbackAddress)); metisB_LoopbackAddress.sin_family = PF_INET; metisB_LoopbackAddress.sin_port = htons(metisB_port); inet_pton(AF_INET, "127.0.0.1", &(metisB_LoopbackAddress.sin_addr)); CPIAddress *metisA_localCpiAddress = cpiAddress_CreateFromInet(&metisA_AnyIpAddress); CPIAddress *metisA_remoteCpiAddress = cpiAddress_CreateFromInet(&metisB_LoopbackAddress); MetisIoOperations *ops = metisTcpTunnel_Create(metis_a, metisA_localCpiAddress, metisA_remoteCpiAddress); MetisConnection *conn = metisConnection_Create(ops); metisConnectionTable_Add(metisForwarder_GetConnectionTable(metis_a), conn); cpiAddress_Destroy(&metisA_localCpiAddress); cpiAddress_Destroy(&metisA_remoteCpiAddress); // ---- run metisDispatcher_RunDuration(dispatcher_a, &((struct timeval) { 0, 1000 })); metisDispatcher_RunDuration(dispatcher_b, &((struct timeval) { 0, 1000 })); // ---- // ===============================================
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; }