|
| 1 | +#include <stdio.h> |
| 2 | +#include <stdlib.h> |
| 3 | +#include <pthread.h> |
| 4 | +#include <stdint.h> |
| 5 | +#include <string.h> |
| 6 | +#include <signal.h> |
| 7 | +#include <unistd.h> |
| 8 | +#include <arpa/inet.h> |
| 9 | +#include <netdb.h> |
| 10 | + |
| 11 | +#include "../src/hexdump.h" |
| 12 | +#include "../src/unyte_udp_collector.h" |
| 13 | +#include "../src/unyte_udp_utils.h" |
| 14 | +#include "../src/unyte_udp_queue.h" |
| 15 | + |
| 16 | +#define USED_VLEN 1 |
| 17 | +#define MAX_TO_RECEIVE 20 |
| 18 | + |
| 19 | +/** |
| 20 | + * Creates own custom socket |
| 21 | + */ |
| 22 | +int create_socket(char *address, char *port) |
| 23 | +{ |
| 24 | + struct addrinfo *addr_info; |
| 25 | + struct addrinfo hints; |
| 26 | + |
| 27 | + memset(&hints, 0, sizeof(hints)); |
| 28 | + |
| 29 | + hints.ai_socktype = SOCK_DGRAM; |
| 30 | + hints.ai_family = AF_UNSPEC; |
| 31 | + hints.ai_flags = AI_PASSIVE | AI_NUMERICSERV; |
| 32 | + |
| 33 | + // Using getaddrinfo to support both IPv4 and IPv6 |
| 34 | + int rc = getaddrinfo(address, port, &hints, &addr_info); |
| 35 | + |
| 36 | + if (rc != 0) { |
| 37 | + printf("getaddrinfo error: %s\n", gai_strerror(rc)); |
| 38 | + exit(EXIT_FAILURE); |
| 39 | + } |
| 40 | + |
| 41 | + printf("Address type: %s | %d\n", (addr_info->ai_family == AF_INET) ? "IPv4" : "IPv6", ntohs(((struct sockaddr_in *)addr_info->ai_addr)->sin_port)); |
| 42 | + |
| 43 | + // create socket on UDP protocol |
| 44 | + int sockfd = socket(addr_info->ai_family, addr_info->ai_socktype, addr_info->ai_protocol); |
| 45 | + |
| 46 | + // handle error |
| 47 | + if (sockfd < 0) |
| 48 | + { |
| 49 | + perror("Cannot create socket"); |
| 50 | + exit(EXIT_FAILURE); |
| 51 | + } |
| 52 | + |
| 53 | + // Use SO_REUSEPORT to be able to launch multiple collector on the same address |
| 54 | + int optval = 1; |
| 55 | + if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEPORT, &optval, sizeof(int)) < 0) |
| 56 | + { |
| 57 | + perror("Cannot set SO_REUSEPORT option on socket"); |
| 58 | + exit(EXIT_FAILURE); |
| 59 | + } |
| 60 | + |
| 61 | + // Setting socket buffer to default 20 MB |
| 62 | + uint64_t receive_buf_size = DEFAULT_SK_BUFF_SIZE; |
| 63 | + if (setsockopt(sockfd, SOL_SOCKET, SO_RCVBUF, &receive_buf_size, sizeof(receive_buf_size)) < 0) |
| 64 | + { |
| 65 | + perror("Cannot set buffer size"); |
| 66 | + exit(EXIT_FAILURE); |
| 67 | + } |
| 68 | + |
| 69 | + if (bind(sockfd, addr_info->ai_addr, (int)addr_info->ai_addrlen) == -1) |
| 70 | + { |
| 71 | + perror("Bind failed"); |
| 72 | + close(sockfd); |
| 73 | + exit(EXIT_FAILURE); |
| 74 | + } |
| 75 | + |
| 76 | + // free addr_info after usage |
| 77 | + freeaddrinfo(addr_info); |
| 78 | + |
| 79 | + return sockfd; |
| 80 | +} |
| 81 | + |
| 82 | +int main(int argc, char *argv[]) |
| 83 | +{ |
| 84 | + if (argc != 3) |
| 85 | + { |
| 86 | + printf("Error: arguments not valid\n"); |
| 87 | + printf("Usage: ./client_socket <ip> <port>\n"); |
| 88 | + exit(1); |
| 89 | + } |
| 90 | + |
| 91 | + int sockfd = create_socket(argv[1], argv[2]); |
| 92 | + |
| 93 | + // Initialize collector options |
| 94 | + unyte_udp_sk_options_t options = {0}; |
| 95 | + options.recvmmsg_vlen = USED_VLEN; |
| 96 | + options.socket_fd = sockfd; |
| 97 | + printf("Listening on socket %d\n", options.socket_fd); |
| 98 | + |
| 99 | + /* Initialize collector */ |
| 100 | + unyte_udp_collector_t *collector = unyte_udp_start_collector_sk(&options); |
| 101 | + int recv_count = 0; |
| 102 | + int max = MAX_TO_RECEIVE; |
| 103 | + |
| 104 | + while (recv_count < max) |
| 105 | + { |
| 106 | + /* Read queue */ |
| 107 | + void *seg_pointer = unyte_udp_queue_read(collector->queue); |
| 108 | + if (seg_pointer == NULL) |
| 109 | + { |
| 110 | + printf("seg_pointer null\n"); |
| 111 | + fflush(stdout); |
| 112 | + } |
| 113 | + unyte_seg_met_t *seg = (unyte_seg_met_t *)seg_pointer; |
| 114 | + |
| 115 | + // printf("unyte_udp_get_version: %u\n", unyte_udp_get_version(seg)); |
| 116 | + // printf("unyte_udp_get_space: %u\n", unyte_udp_get_space(seg)); |
| 117 | + // printf("unyte_udp_get_encoding_type: %u\n", unyte_udp_get_encoding_type(seg)); |
| 118 | + // printf("unyte_udp_get_header_length: %u\n", unyte_udp_get_header_length(seg)); |
| 119 | + // printf("unyte_udp_get_message_length: %u\n", unyte_udp_get_message_length(seg)); |
| 120 | + // printf("unyte_udp_get_generator_id: %u\n", unyte_udp_get_generator_id(seg)); |
| 121 | + // printf("unyte_udp_get_message_id: %u\n", unyte_udp_get_message_id(seg)); |
| 122 | + // printf("unyte_udp_get_src[family]: %u\n", unyte_udp_get_src(seg)->ss_family); |
| 123 | + // printf("unyte_udp_get_dest_addr[family]: %u\n", unyte_udp_get_dest_addr(seg)->ss_family); |
| 124 | + char ip_canonical[100]; |
| 125 | + if (unyte_udp_get_src(seg)->ss_family == AF_INET) { |
| 126 | + 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)); |
| 127 | + printf("src port: %u\n", ntohs(((struct sockaddr_in*)unyte_udp_get_src(seg))->sin_port)); |
| 128 | + } else { |
| 129 | + 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)); |
| 130 | + printf("src port: %u\n", ntohs(((struct sockaddr_in6*)unyte_udp_get_src(seg))->sin6_port)); |
| 131 | + } |
| 132 | + char ip_dest_canonical[100]; |
| 133 | + if (unyte_udp_get_src(seg)->ss_family == AF_INET) { |
| 134 | + 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)); |
| 135 | + printf("dest port: %u\n", ntohs(((struct sockaddr_in*)unyte_udp_get_dest_addr(seg))->sin_port)); |
| 136 | + } else { |
| 137 | + 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)); |
| 138 | + printf("dest port: %u\n", ntohs(((struct sockaddr_in6*)unyte_udp_get_dest_addr(seg))->sin6_port)); |
| 139 | + } |
| 140 | + // printf("unyte_udp_get_payload: %s\n", unyte_udp_get_payload(seg)); |
| 141 | + // printf("unyte_udp_get_payload_length: %u\n", unyte_udp_get_payload_length(seg)); |
| 142 | + |
| 143 | + /* Processing sample */ |
| 144 | + recv_count++; |
| 145 | + print_udp_notif_header(seg->header, stdout); |
| 146 | + // hexdump(seg->payload, seg->header->message_length - seg->header->header_length); |
| 147 | + |
| 148 | + fflush(stdout); |
| 149 | + |
| 150 | + /* Struct frees */ |
| 151 | + unyte_udp_free_all(seg); |
| 152 | + } |
| 153 | + |
| 154 | + printf("Shutdown the socket\n"); |
| 155 | + close(*collector->sockfd); |
| 156 | + pthread_join(*collector->main_thread, NULL); |
| 157 | + |
| 158 | + // freeing collector mallocs |
| 159 | + unyte_udp_free_collector(collector); |
| 160 | + fflush(stdout); |
| 161 | + return 0; |
| 162 | +} |
0 commit comments