|  |  |  | @ -16,6 +16,7 @@ | 
		
	
		
			
				|  |  |  |  | // It needs to be global for the whole process.
 | 
		
	
		
			
				|  |  |  |  | int max_fd = -1; | 
		
	
		
			
				|  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  | // TODO: Close the socket and return NULL on error
 | 
		
	
		
			
				|  |  |  |  | struct Connection* | 
		
	
		
			
				|  |  |  |  | connection_new(unsigned int source_port, const char* source_ip, unsigned int dest_port, const char* dest_ip) | 
		
	
		
			
				|  |  |  |  | { | 
		
	
	
		
			
				
					|  |  |  | @ -25,6 +26,7 @@ connection_new(unsigned int source_port, const char* source_ip, unsigned int des | 
		
	
		
			
				|  |  |  |  |     // Request a UDP socket
 | 
		
	
		
			
				|  |  |  |  |     conn->sockfd = socket(AF_INET, SOCK_DGRAM, 0); | 
		
	
		
			
				|  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  |     // Set the source address and port if they are provided
 | 
		
	
		
			
				|  |  |  |  |     if(source_port && source_ip) { | 
		
	
		
			
				|  |  |  |  |        struct sockaddr_in loc_addr; | 
		
	
		
			
				|  |  |  |  |        loc_addr.sin_family = AF_INET; | 
		
	
	
		
			
				
					|  |  |  | @ -60,7 +62,7 @@ struct Connection* | 
		
	
		
			
				|  |  |  |  | connection_poll_ready(struct Client* client) { | 
		
	
		
			
				|  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  |     // Return a null connection if no sockets have been opened
 | 
		
	
		
			
				|  |  |  |  |     if(max_fds < 0) | 
		
	
		
			
				|  |  |  |  |     if(max_fd < 0) | 
		
	
		
			
				|  |  |  |  |         return NULL; | 
		
	
		
			
				|  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  |     // Add all the connections' socket file descriptors to a watch list
 | 
		
	
	
		
			
				
					|  |  |  | @ -96,6 +98,10 @@ connection_poll_ready(struct Client* client) { | 
		
	
		
			
				|  |  |  |  | void | 
		
	
		
			
				|  |  |  |  | connection_read(struct Connection* conn, union Message* resp) { | 
		
	
		
			
				|  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  |     memset(resp, 0, sizeof(union Message)); | 
		
	
		
			
				|  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  |     if(!conn) return; | 
		
	
		
			
				|  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  |     int recvb = recvfrom(conn->sockfd, resp, sizeof(union Message), 0, (struct sockaddr*)&conn->remote_addr, &conn->addrlen); | 
		
	
		
			
				|  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  |     // Check for socket read errors
 | 
		
	
	
		
			
				
					|  |  |  | @ -113,6 +119,7 @@ connection_read(struct Connection* conn, union Message* resp) { | 
		
	
		
			
				|  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  | void | 
		
	
		
			
				|  |  |  |  | req_send(struct Connection* conn, union Request* req, size_t length) { | 
		
	
		
			
				|  |  |  |  |     if(!conn || !req) return; | 
		
	
		
			
				|  |  |  |  |     sendto(conn->sockfd, req, length, 0, (struct sockaddr*)&conn->remote_addr, conn->addrlen); | 
		
	
		
			
				|  |  |  |  | } | 
		
	
		
			
				|  |  |  |  | 
 | 
		
	
	
		
			
				
					|  |  |  | 
 |