-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathclient_sample.c
96 lines (83 loc) · 4.25 KB
/
client_sample.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include <stdio.h>
#include <unistd.h>
#include <arpa/inet.h>
#include "../src/hexdump.h"
#include "../src/unyte_udp_collector.h"
#define USED_VLEN 1
#define MAX_TO_RECEIVE 10
int main(int argc, char *argv[])
{
if (argc != 3)
{
printf("Error: arguments not valid\n");
printf("Usage: ./client_sample <ip> <port>\n");
exit(1);
}
// Create a udp socket with default socket buffer
int socketfd = unyte_udp_create_socket(argv[1], argv[2], DEFAULT_SK_BUFF_SIZE);
printf("Listening on %s:%s\n", argv[1], argv[2]);
// Initialize collector options
unyte_udp_options_t options = {0};
options.recvmmsg_vlen = USED_VLEN;
options.socket_fd = socketfd; // passing socket file descriptor to listen to
options.msg_dst_ip = false; // destination IP not parsed from IP packet to improve performance
/* Initialize collector */
unyte_udp_collector_t *collector = unyte_udp_start_collector(&options);
int recv_count = 0;
int max = MAX_TO_RECEIVE;
while (recv_count < max)
{
/* Read queue */
void *seg_pointer = unyte_udp_queue_read(collector->queue);
if (seg_pointer == NULL)
{
printf("seg_pointer null\n");
fflush(stdout);
}
unyte_seg_met_t *seg = (unyte_seg_met_t *)seg_pointer;
// printf("unyte_udp_get_version: %u\n", unyte_udp_get_version(seg));
// printf("unyte_udp_get_space: %u\n", unyte_udp_get_space(seg));
// printf("unyte_udp_get_media_type: %u\n", unyte_udp_get_media_type(seg));
// printf("unyte_udp_get_header_length: %u\n", unyte_udp_get_header_length(seg));
// printf("unyte_udp_get_message_length: %u\n", unyte_udp_get_message_length(seg));
// printf("unyte_udp_get_observation_domain_id: %u\n", unyte_udp_get_observation_domain_id(seg));
// printf("unyte_udp_get_message_id: %u\n", unyte_udp_get_message_id(seg));
// printf("unyte_udp_get_src[family]: %u\n", unyte_udp_get_src(seg)->ss_family);
printf("unyte_udp_get_dest_addr[family]: %u\n", unyte_udp_get_dest_addr(seg) == NULL ? 0 : unyte_udp_get_dest_addr(seg)->ss_family); // NULL if options.msg_dst_ip is set to false (default)
// char ip_canonical[100];
// if (unyte_udp_get_src(seg)->ss_family == AF_INET) {
// printf("src IPv4: %s\n", inet_ntop(unyte_udp_get_src(seg)->ss_family, &((struct sockaddr_in*)unyte_udp_get_src(seg))->sin_addr.s_addr, ip_canonical, sizeof ip_canonical));
// printf("src port: %u\n", ntohs(((struct sockaddr_in*)unyte_udp_get_src(seg))->sin_port));
// } else {
// printf("src IPv6: %s\n", inet_ntop(unyte_udp_get_src(seg)->ss_family, &((struct sockaddr_in6*)unyte_udp_get_src(seg))->sin6_addr.s6_addr, ip_canonical, sizeof ip_canonical));
// printf("src port: %u\n", ntohs(((struct sockaddr_in6*)unyte_udp_get_src(seg))->sin6_port));
// }
// Only if options.msg_dst_ip is set to true
if (unyte_udp_get_dest_addr(seg) != NULL) {
char ip_dest_canonical[100];
if (unyte_udp_get_dest_addr(seg)->ss_family == AF_INET) {
printf("dest IPv4: %s\n", inet_ntop(unyte_udp_get_dest_addr(seg)->ss_family, &((struct sockaddr_in*)unyte_udp_get_dest_addr(seg))->sin_addr.s_addr, ip_dest_canonical, sizeof ip_dest_canonical));
printf("dest port: %u\n", ntohs(((struct sockaddr_in*)unyte_udp_get_dest_addr(seg))->sin_port));
} else {
printf("dest IPv6: %s\n", inet_ntop(unyte_udp_get_dest_addr(seg)->ss_family, &((struct sockaddr_in6*)unyte_udp_get_dest_addr(seg))->sin6_addr.s6_addr, ip_dest_canonical, sizeof ip_dest_canonical));
printf("dest port: %u\n", ntohs(((struct sockaddr_in6*)unyte_udp_get_dest_addr(seg))->sin6_port));
}
}
// printf("unyte_udp_get_payload: %.*s\n", unyte_udp_get_payload_length(seg), unyte_udp_get_payload(seg)); // payload may not be NULL-terminated
// printf("unyte_udp_get_payload_length: %u\n", unyte_udp_get_payload_length(seg));
/* Processing sample */
recv_count++;
print_udp_notif_header(seg->header, stdout);
// hexdump(seg->payload, seg->header->message_length - seg->header->header_length);
fflush(stdout);
/* Struct frees */
unyte_udp_free_all(seg);
}
printf("Shutdown the socket\n");
close(*collector->sockfd);
pthread_join(*collector->main_thread, NULL);
// freeing collector mallocs
unyte_udp_free_collector(collector);
fflush(stdout);
return 0;
}