From ec464a9e4733d84571a4639d1e3efcb41bf6ae57 Mon Sep 17 00:00:00 2001 From: sotech117 Date: Mon, 25 Sep 2023 13:43:14 -0400 Subject: fix udp connection pointers. may abstract into a memo type structure to reduce redunant calls --- server.c | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) (limited to 'server.c') diff --git a/server.c b/server.c index ec0e6a7..d8c6e50 100644 --- a/server.c +++ b/server.c @@ -380,7 +380,7 @@ void *print_info_routine(void *arg) { return (NULL); } -int send_all_udp(int udp_sockfd, char *buf, int *len, struct addrinfo *thread_res) +int send_all_udp(int udp_sockfd, char *buf, int *len, struct sockaddr *addr, socklen_t addrlen) { int MAX_PACKET_SIZE = 512; int total = 0; // how many bytes we've sent @@ -388,8 +388,7 @@ int send_all_udp(int udp_sockfd, char *buf, int *len, struct addrinfo *thread_re int n; while(total < *len) { - n = sendto(udp_sockfd, buf+total, MAX_PACKET_SIZE, 0, thread_res->ai_addr, thread_res->ai_addrlen); - // thread_res->ai_addr, thread_res->ai_addrlen)) == -1; + n = sendto(udp_sockfd, buf+total, MAX_PACKET_SIZE, 0, addr, addrlen); if (n == -1) { break; } total += n; bytesleft -= n; @@ -406,7 +405,7 @@ void udp_port_cleanup_handler(void *arg) close(sockfd); } -int setup_udp_connection(int udp_port, int *udp_sockfd, struct addrinfo* res) { +int setup_udp_connection(int udp_port, int *udp_sockfd, socklen_t *addrlen, struct sockaddr* addr) { // setup hints to get udp_port struct addrinfo thread_hints, *thread_res, *thread_servinfo; memset(&thread_hints, 0, sizeof thread_hints); @@ -447,8 +446,9 @@ int setup_udp_connection(int udp_port, int *udp_sockfd, struct addrinfo* res) { *udp_sockfd = sock; // only these two properties of addrinfo are needed to send udp - *res->ai_addr = *thread_res->ai_addr; - res->ai_addrlen = thread_res->ai_addrlen; + // TODO: FIX, or reroute somewhere else + *addrlen = thread_res->ai_addrlen; + *addr = *thread_res->ai_addr; return 1; } @@ -470,8 +470,9 @@ void *send_udp_packet_routine(void *arg) { // setup udp socket for this thread/user int udp_port = user_data[user_index].udpPort; int udp_sockfd; - struct addrinfo *res; - if (setup_udp_connection(udp_port, &udp_sockfd, &res) == -1){ + struct sockaddr addr; + socklen_t addrlen; + if (setup_udp_connection(udp_port, &udp_sockfd, &addrlen, &addr) == -1){ fprintf(stderr, "failure in setup_udp_connection"); return (NULL); // error occured } @@ -493,7 +494,7 @@ void *send_udp_packet_routine(void *arg) { } // send the data! (no error check since udp) - send_all_udp(udp_sockfd, file_buffer, &buffer_size, &res); + send_all_udp(udp_sockfd, file_buffer, &buffer_size, &addr, addrlen); // cleanup close(udp_sockfd); -- cgit v1.2.3-70-g09d2