// 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, cpiAddressList_Copy) { CPIAddressList *list = cpiAddressList_Create(); unsigned loops = 10; for (unsigned i = 0; i < loops; i++) { cpiAddressList_Append(list, cpiAddress_CreateFromInterface(i)); } CPIAddressList *copy = cpiAddressList_Copy(list); assertTrue(cpiAddressList_Length(copy) == cpiAddressList_Length(list), "Copy wrong size, got %zu expected %zu", cpiAddressList_Length(copy), cpiAddressList_Length(list)); for (unsigned i = 0; i < cpiAddressList_Length(copy); i++) { const CPIAddress *truth = cpiAddressList_GetItem(list, i); const CPIAddress *test = cpiAddressList_GetItem(copy, i); assertTrue(cpiAddress_Equals(truth, test), "Lists do not match at element %u", i); } cpiAddressList_Destroy(&list); cpiAddressList_Destroy(©); }
LONGBOW_TEST_CASE(Global, cpiAddressList_GetItem) { CPIAddressList *list = cpiAddressList_Create(); unsigned loops = 10; for (unsigned i = 0; i < loops; i++) { cpiAddressList_Append(list, cpiAddress_CreateFromInterface(i)); } assertTrue(cpiAddressList_Length(list) == loops, "Got wrong length, expected %u got %zu", loops, cpiAddressList_Length(list)); CPIAddress *truth = cpiAddress_CreateFromInterface(5); const CPIAddress *test = cpiAddressList_GetItem(list, 5); assertTrue(cpiAddress_Equals(truth, test), "Item 5 did not match!"); cpiAddressList_Destroy(&list); cpiAddress_Destroy(&truth); }
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; }