fix timeouts
This commit is contained in:
parent
e1a5e8e29f
commit
1b5981d335
1 changed files with 10 additions and 11 deletions
21
main.c
21
main.c
|
|
@ -177,18 +177,17 @@ ssize_t link_send(linkinterface_t* link, const mac_t* dstAddr,
|
||||||
|
|
||||||
size_t link_recv_any_from(linkinterface_t* link,
|
size_t link_recv_any_from(linkinterface_t* link,
|
||||||
const mac_t* srcAddrs, unsigned addrNum,
|
const mac_t* srcAddrs, unsigned addrNum,
|
||||||
uint16_t type, frame_t* frame, unsigned timeoutMilis,
|
uint16_t type, frame_t* frame, unsigned timeoutSec,
|
||||||
mac_t* matchAddr)
|
mac_t* matchAddr)
|
||||||
{
|
{
|
||||||
clock_t beginTime = clock();
|
uint64_t deadline = time(NULL) + timeoutSec;
|
||||||
clock_t deadline = beginTime + timeoutMilis;
|
|
||||||
|
|
||||||
uint16_t want_type = htons(type);
|
uint16_t want_type = htons(type);
|
||||||
do {
|
do {
|
||||||
ssize_t rd = recv(link->fd, frame->data, frame->datalen, 0);
|
ssize_t rd = recv(link->fd, frame->data, frame->datalen, 0);
|
||||||
if (rd < 0) return -1;
|
if (rd < 0) return -1;
|
||||||
|
|
||||||
if (clock() > deadline) return 0; // TIMEOUT
|
if (time(NULL) > deadline) return 0; // TIMEOUT
|
||||||
|
|
||||||
struct ether_header* ether = (struct ether_header*)frame->data;
|
struct ether_header* ether = (struct ether_header*)frame->data;
|
||||||
|
|
||||||
|
|
@ -286,12 +285,12 @@ ssize_t icmp_direct_broadcast(linkinterface_t* link, const mac_t* dstAddr, uint1
|
||||||
|
|
||||||
// 0 - no match, 1 - matched
|
// 0 - no match, 1 - matched
|
||||||
unsigned icmp_match(linkinterface_t* link, const mac_t* srcAddrs, unsigned addrNum,
|
unsigned icmp_match(linkinterface_t* link, const mac_t* srcAddrs, unsigned addrNum,
|
||||||
unsigned timeoutMillis,
|
unsigned timeoutSec,
|
||||||
mac_t* matchAddr, ip4addr_t* matchIp)
|
mac_t* matchAddr, ip4addr_t* matchIp)
|
||||||
{
|
{
|
||||||
frame_t* frame = frame_full(link);
|
frame_t* frame = frame_full(link);
|
||||||
size_t recv = link_recv_any_from(link, srcAddrs, addrNum,
|
size_t recv = link_recv_any_from(link, srcAddrs, addrNum,
|
||||||
ETHERTYPE_IP, frame, timeoutMillis, matchAddr);
|
ETHERTYPE_IP, frame, timeoutSec, matchAddr);
|
||||||
if (recv < 1) goto _match_bad1;
|
if (recv < 1) goto _match_bad1;
|
||||||
|
|
||||||
// we got matching Ethernet frame, let's check IP
|
// we got matching Ethernet frame, let's check IP
|
||||||
|
|
@ -329,7 +328,7 @@ _match_bad1:
|
||||||
// IPs set in the same order as targetAddrs ordered
|
// IPs set in the same order as targetAddrs ordered
|
||||||
unsigned icmp_resolve(linkinterface_t* link,
|
unsigned icmp_resolve(linkinterface_t* link,
|
||||||
const mac_t* targetAddrs, unsigned addrNum,
|
const mac_t* targetAddrs, unsigned addrNum,
|
||||||
unsigned timeoutMillis,
|
unsigned timeoutSec,
|
||||||
ip4addr_t* resolvedIps)
|
ip4addr_t* resolvedIps)
|
||||||
{
|
{
|
||||||
unsigned resolved = 0;
|
unsigned resolved = 0;
|
||||||
|
|
@ -341,13 +340,13 @@ unsigned icmp_resolve(linkinterface_t* link,
|
||||||
}
|
}
|
||||||
|
|
||||||
// receive ICMP packets as many as we match
|
// receive ICMP packets as many as we match
|
||||||
clock_t deadline = clock() + timeoutMillis;
|
uint64_t deadline = time(NULL) + timeoutSec;
|
||||||
do {
|
do {
|
||||||
mac_t matchAddr;
|
mac_t matchAddr;
|
||||||
ip4addr_t matchIp;
|
ip4addr_t matchIp;
|
||||||
|
|
||||||
if (icmp_match(link, targetAddrs, addrNum,
|
if (icmp_match(link, targetAddrs, addrNum,
|
||||||
timeoutMillis, &matchAddr, &matchIp))
|
timeoutSec, &matchAddr, &matchIp))
|
||||||
{
|
{
|
||||||
// find who we matched and place it in appropriate place
|
// find who we matched and place it in appropriate place
|
||||||
for (unsigned i = 0; i < addrNum; i++) {
|
for (unsigned i = 0; i < addrNum; i++) {
|
||||||
|
|
@ -357,7 +356,7 @@ unsigned icmp_resolve(linkinterface_t* link,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} while (resolved < addrNum && clock() < deadline);
|
} while (resolved < addrNum && time(NULL) < deadline);
|
||||||
return resolved;
|
return resolved;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -381,7 +380,7 @@ int main(int argc, char** argv) {
|
||||||
parse_mac(argv[2 + i], targets[i].mac);
|
parse_mac(argv[2 + i], targets[i].mac);
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned resolved = icmp_resolve(link, targets, targetNum, 5000, ips);
|
unsigned resolved = icmp_resolve(link, targets, targetNum, 5, ips);
|
||||||
printf("Resolved: %u\n", resolved);
|
printf("Resolved: %u\n", resolved);
|
||||||
|
|
||||||
for (unsigned i = 0; i < resolved; i++) {
|
for (unsigned i = 0; i < resolved; i++) {
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue