use single struct instead of two arrays, for future easy unmarshalling

This commit is contained in:
mykola2312 2024-12-21 16:28:45 +02:00
parent 438ce59de9
commit 3bb31bb88c

53
main.c
View file

@ -324,18 +324,22 @@ _match_bad1:
return 0; return 0;
} }
// returns number of resolved IP addresses. `resolvedIps` must be capacity of addrNum, typedef struct {
// IPs set in the same order as targetAddrs ordered mac_t addr;
unsigned icmp_resolve(linkinterface_t* link, ip4addr_t ip;
} mac_ip_t;
mac_ip_t* icmp_resolve(linkinterface_t* link,
const mac_t* targetAddrs, unsigned addrNum, const mac_t* targetAddrs, unsigned addrNum,
unsigned timeoutSec, unsigned timeoutSec,
ip4addr_t* resolvedIps) unsigned* resolvedNum)
{ {
mac_ip_t* output = NULL;
unsigned resolved = 0; unsigned resolved = 0;
// sent ICMP packets // sent ICMP packets
for (unsigned i = 0; i < addrNum; i++) { for (unsigned i = 0; i < addrNum; i++) {
if (icmp_direct_broadcast(link, &targetAddrs[i], 0) < 1) { if (icmp_direct_broadcast(link, &targetAddrs[i], 0) < 1) {
return -1; // bad link? return NULL; // bad link?
} }
} }
@ -348,16 +352,15 @@ unsigned icmp_resolve(linkinterface_t* link,
if (icmp_match(link, targetAddrs, addrNum, if (icmp_match(link, targetAddrs, addrNum,
timeoutSec, &matchAddr, &matchIp)) timeoutSec, &matchAddr, &matchIp))
{ {
// find who we matched and place it in appropriate place if (resolved) output = (mac_ip_t*)realloc(output, ++resolved * (sizeof(mac_ip_t)));
for (unsigned i = 0; i < addrNum; i++) { else output = (mac_ip_t*)malloc(++resolved * (sizeof(mac_ip_t)));
if (!memcmp(targetAddrs[i].mac, matchAddr.mac, ETH_ALEN)) { memcpy(&output[resolved - 1].addr, &matchAddr, sizeof(matchAddr));
memcpy(resolvedIps[i].octets, matchIp.octets, 4); memcpy(&output[resolved - 1].ip, &matchIp, sizeof(matchIp));
resolved++;
}
}
} }
} while (resolved < addrNum && time(NULL) < deadline); } while (resolved < addrNum && time(NULL) < deadline);
return resolved;
*resolvedNum = resolved;
return output;
} }
int main(int argc, char** argv) { int main(int argc, char** argv) {
@ -374,23 +377,27 @@ int main(int argc, char** argv) {
unsigned targetNum = argc - 2; unsigned targetNum = argc - 2;
mac_t* targets = (mac_t*)calloc(sizeof(mac_t), targetNum); mac_t* targets = (mac_t*)calloc(sizeof(mac_t), targetNum);
ip4addr_t* ips = (ip4addr_t*)calloc(sizeof(ip4addr_t), targetNum);
for (unsigned i = 0; i < targetNum; i++) { for (unsigned i = 0; i < targetNum; i++) {
parse_mac(argv[2 + i], targets[i].mac); parse_mac(argv[2 + i], targets[i].mac);
} }
unsigned resolved = icmp_resolve(link, targets, targetNum, 5, ips); unsigned resolvedNum;
printf("Resolved: %u\n", resolved); mac_ip_t* resolved = icmp_resolve(link, targets, targetNum, 5, &resolvedNum);
printf("Resolved: %u\n", resolvedNum);
for (unsigned i = 0; i < resolved; i++) { if (resolved) {
output_mac(targets[i].mac); for (unsigned i = 0; i < resolvedNum; i++) {
printf(" -> %d.%d.%d.%d\n", ips[i].octets[0], output_mac(resolved[i].addr.mac);
ips[i].octets[1], ips[i].octets[2], ips[i].octets[3]); printf(" -> %d.%d.%d.%d\n",
resolved[i].ip.octets[0],
resolved[i].ip.octets[1],
resolved[i].ip.octets[2],
resolved[i].ip.octets[3]
);
}
free(resolved);
} }
free(targets);
free(ips);
link_free(link); link_free(link);
return 0; return 0;