LONGBOW_TEST_CASE(Global, cpiAddress_CreateFromLink) { uint8_t mac[] = { 0x01, 0x02, 0x03, 0x04, 0xFF, 0x8F }; PARCBuffer *macbuffer = parcBuffer_Flip(parcBuffer_CreateFromArray(mac, sizeof(mac))); CPIAddress *address = cpiAddress_CreateFromLink(mac, sizeof(mac)); // Do not release test, it is the same reference as address->blob PARCBuffer *test = cpiAddress_GetLinkAddress(address); assertNotNull(test, "Got null link address buffer"); assertTrue(parcBuffer_Equals(test, address->blob), "Returned buffer from cpiAddress_GetLinkAddress not equal to address"); assertTrue(cpiAddress_GetType(address) == cpiAddressType_LINK, "Got wrong address type, expected %d, got %d", cpiAddressType_LINK, 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 LINK type"); CPIAddress *copy = cpiAddress_Copy(address); assertTrue(cpiAddress_Equals(copy, address), "Copy and address not equal for LINK"); parcJSON_Release(&json); cpiAddress_Destroy(&address); cpiAddress_Destroy(©); cpiAddress_Destroy(&fromjson); parcBuffer_Release(&macbuffer); }
LONGBOW_TEST_CASE(Global, cpiAddress_CreateFromUnix) { struct sockaddr_un addr_un; struct sockaddr_un addr_test; memset(&addr_un, 0, sizeof(struct sockaddr_un)); char path[] = "/Hello/Cruel/World"; strcpy(addr_un.sun_path, path); addr_un.sun_family = AF_UNIX; CPIAddress *address = cpiAddress_CreateFromUnix(&addr_un); bool success = cpiAddress_GetUnix(address, &addr_test); assertTrue(success, "Got false converting back address"); assertTrue(memcmp(&addr_un, &addr_test, sizeof(struct sockaddr_un)) == 0, "Got mismatch addressed"); assertTrue(cpiAddress_GetType(address) == cpiAddressType_UNIX, "Got wrong address type, expected %d, got %d", cpiAddressType_UNIX, 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 UNIX type"); CPIAddress *copy = cpiAddress_Copy(address); assertTrue(cpiAddress_Equals(copy, address), "Copy and address not equal for UNIX"); parcJSON_Release(&json); cpiAddress_Destroy(&address); cpiAddress_Destroy(©); cpiAddress_Destroy(&fromjson); }
LONGBOW_TEST_CASE(Global, cpiAddress_CreateFromInterface) { uint32_t ifidx = 0x01020304; uint32_t test; CPIAddress *address = cpiAddress_CreateFromInterface(ifidx); bool success = cpiAddress_GetInterfaceIndex(address, &test); assertTrue(success, "Got false converting back address"); assertTrue(ifidx == test, "Got mismatch addressed"); assertTrue(cpiAddress_GetType(address) == cpiAddressType_IFACE, "Got wrong address type, expected %d, got %d", cpiAddressType_IFACE, 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 IFACE type"); CPIAddress *copy = cpiAddress_Copy(address); assertTrue(cpiAddress_Equals(copy, address), "Copy and address not equal for IFACE"); parcJSON_Release(&json); cpiAddress_Destroy(&address); cpiAddress_Destroy(©); cpiAddress_Destroy(&fromjson); }
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(©); cpiAddress_Destroy(&fromjson); }
// returns a strdup() of the interface name, use free(3) static char * _pickInterfaceName(MetisForwarder *metis) { char *ifname = NULL; CPIInterfaceSet *set = metisSystem_Interfaces(metis); size_t length = cpiInterfaceSet_Length(set); assertTrue(length > 0, "metisSystem_Interfaces returned no interfaces"); for (size_t i = 0; i < length; i++) { CPIInterface *iface = cpiInterfaceSet_GetByOrdinalIndex(set, i); const CPIAddressList *addressList = cpiInterface_GetAddresses(iface); size_t length = cpiAddressList_Length(addressList); for (size_t i = 0; i < length && !ifname; i++) { const CPIAddress *a = cpiAddressList_GetItem(addressList, i); if (cpiAddress_GetType(a) == cpiAddressType_LINK) { ifname = strdup(cpiInterface_GetName(iface)); } } } cpiInterfaceSet_Destroy(&set); return ifname; }
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; }
PARCEventQueue * metisDispatcher_StreamBufferConnect(MetisDispatcher *dispatcher, const MetisAddressPair *pair) { const CPIAddress *localAddress = metisAddressPair_GetLocal(pair); const CPIAddress *remoteAddress = metisAddressPair_GetRemote(pair); // they must be of the same address family if (cpiAddress_GetType(localAddress) != cpiAddress_GetType(remoteAddress)) { char message[2048]; char *localAddressString = cpiAddress_ToString(localAddress); char *remoteAddressString = cpiAddress_ToString(remoteAddress); snprintf(message, 2048, "Remote address not same type as local address, expected %d got %d\nlocal %s remote %s", cpiAddress_GetType(localAddress), cpiAddress_GetType(remoteAddress), localAddressString, remoteAddressString); parcMemory_Deallocate((void **) &localAddressString); parcMemory_Deallocate((void **) &remoteAddressString); assertTrue(cpiAddress_GetType(localAddress) == cpiAddress_GetType(remoteAddress), "%s", message); } switch (cpiAddress_GetType(localAddress)) { case cpiAddressType_INET: return metisDispatcher_StreamBufferConnect_INET(dispatcher, localAddress, remoteAddress); break; case cpiAddressType_INET6: return metisDispatcher_StreamBufferConnect_INET6(dispatcher, localAddress, remoteAddress); break; default: trapIllegalValue(pair, "local address unsupported CPI address type: %d", cpiAddress_GetType(localAddress)); } }
CPIAddress * metisSystem_GetMacAddressByName(MetisForwarder *metis, const char *interfaceName) { CPIAddress *linkAddress = NULL; CPIInterfaceSet *interfaceSet = metisSystem_Interfaces(metis); CPIInterface *interface = cpiInterfaceSet_GetByName(interfaceSet, interfaceName); if (interface) { const CPIAddressList *addressList = cpiInterface_GetAddresses(interface); size_t length = cpiAddressList_Length(addressList); for (size_t i = 0; i < length && !linkAddress; i++) { const CPIAddress *a = cpiAddressList_GetItem(addressList, i); if (cpiAddress_GetType(a) == cpiAddressType_LINK) { linkAddress = cpiAddress_Copy(a); } } } cpiInterfaceSet_Destroy(&interfaceSet); return linkAddress; }