-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutility.c
66 lines (50 loc) · 1.48 KB
/
utility.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// Jakub Grobelny 300481
#include "utility.h"
#include <stdlib.h>
#include <netinet/ip.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
char* translate_address(const struct sockaddr_in* addr, char buffer[20]) {
const char* sender_ip = inet_ntop(
AF_INET,
&addr->sin_addr,
buffer,
20
);
if (sender_ip == NULL) {
perror("inet_ntop");
exit(EXIT_FAILURE);
}
return buffer;
}
void print_unique_responders(response_info_t* responders, int n) {
for (int i = 0; i < n; i++) {
const char* ip = responders[i].ip;
bool unique = true;
for (int j = 0; j < i; j++) {
const char* other_ip = responders[j].ip;
if (strcmp(ip, other_ip) == 0) {
unique = false;
break;
}
}
if (unique) {
printf("%s ", ip);
}
}
}
void update_time(response_info_t* responder, struct timeval* time_left) {
responder->time.tv_sec = 1;
responder->time.tv_usec = 0;
timersub(&responder->time, time_left, &responder->time);
}
void print_responders_avg_time(response_info_t* responders, int n) {
uint64_t total_microseconds = 0;
for (int i = 0; i < n; i++) {
total_microseconds += responders[i].time.tv_usec;
}
uint64_t average = total_microseconds / 1000 / 3;
printf("%ldms\n", average);
}