|
|
@ -7,7 +7,6 @@
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/select.h>
|
|
|
|
#include <sys/select.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdio.h>
|
|
|
@ -82,44 +81,49 @@ int max_fd = -1;
|
|
|
|
|
|
|
|
|
|
|
|
// TODO: Close the socket and return NULL on error
|
|
|
|
// TODO: Close the socket and return NULL on error
|
|
|
|
struct Connection*
|
|
|
|
struct Connection*
|
|
|
|
connection_new(unsigned int source_port, const char* source_ip, unsigned int dest_port, const char* dest_ip)
|
|
|
|
connection_new(uint16_t source_port, uint32_t source_ip, uint16_t dest_port, uint32_t dest_ip)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
struct Connection* conn = malloc(sizeof(struct Connection));
|
|
|
|
struct Connection* conn = malloc(sizeof(struct Connection));
|
|
|
|
memset(conn, 0, sizeof(struct Connection));
|
|
|
|
memset(conn, 0, sizeof(struct Connection));
|
|
|
|
|
|
|
|
|
|
|
|
// Request a UDP socket
|
|
|
|
|
|
|
|
conn->sockfd = socket(AF_INET, SOCK_DGRAM, 0);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Set the source address and port if they are provided
|
|
|
|
// Set the source address and port if they are provided
|
|
|
|
if(source_port && source_ip) {
|
|
|
|
if(source_port && source_ip) {
|
|
|
|
struct sockaddr_in loc_addr;
|
|
|
|
conn->local_addr.sin_family = AF_INET;
|
|
|
|
loc_addr.sin_family = AF_INET;
|
|
|
|
conn->local_addr.sin_port = htons(source_port);
|
|
|
|
loc_addr.sin_port = htons(source_port);
|
|
|
|
conn->local_addr.sin_addr.s_addr = source_ip;
|
|
|
|
loc_addr.sin_addr.s_addr = inet_addr(source_ip);
|
|
|
|
|
|
|
|
if(bind(conn->sockfd, (struct sockaddr*)&loc_addr, sizeof(loc_addr)) < 0)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
perror("unable to bind local port");
|
|
|
|
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Make the socket non-blocking
|
|
|
|
|
|
|
|
int flags = fcntl(conn->sockfd, F_GETFL);
|
|
|
|
|
|
|
|
fcntl(conn->sockfd, F_SETFL, flags | O_NONBLOCK);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Set the address of the drone
|
|
|
|
// Set the address of the drone
|
|
|
|
memset(&conn->remote_addr, 0, sizeof(conn->remote_addr));
|
|
|
|
memset(&conn->remote_addr, 0, sizeof(conn->remote_addr));
|
|
|
|
conn->addrlen = sizeof(conn->remote_addr);
|
|
|
|
conn->addrlen = sizeof(conn->remote_addr);
|
|
|
|
conn->remote_addr.sin_family = AF_INET;
|
|
|
|
conn->remote_addr.sin_family = AF_INET;
|
|
|
|
conn->remote_addr.sin_port = htons(dest_port);
|
|
|
|
conn->remote_addr.sin_port = htons(dest_port);
|
|
|
|
conn->remote_addr.sin_addr.s_addr = inet_addr(dest_ip);
|
|
|
|
conn->remote_addr.sin_addr.s_addr = dest_ip;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return conn;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
enum connection_error
|
|
|
|
|
|
|
|
connection_connect(struct Connection* conn) {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Request a UDP socket
|
|
|
|
|
|
|
|
conn->sockfd = socket(AF_INET, SOCK_DGRAM, 0);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if(conn->local_addr.sin_addr.s_addr
|
|
|
|
|
|
|
|
&& bind(conn->sockfd, (struct sockaddr*)&conn->local_addr, sizeof(conn->local_addr)) < 0)
|
|
|
|
|
|
|
|
return CONNECTION_LOCAL_BIND;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Make the socket non-blocking
|
|
|
|
|
|
|
|
int flags = fcntl(conn->sockfd, F_GETFL);
|
|
|
|
|
|
|
|
fcntl(conn->sockfd, F_SETFL, flags | O_NONBLOCK);
|
|
|
|
|
|
|
|
|
|
|
|
// File descriptors are numbers that count up sequentially,
|
|
|
|
// File descriptors are numbers that count up sequentially,
|
|
|
|
// so save the last one as the greatest file descriptor.
|
|
|
|
// so save the last one as the greatest file descriptor.
|
|
|
|
// This is needed for polling the sockets later.
|
|
|
|
// This is needed for polling the sockets later.
|
|
|
|
max_fd = conn->sockfd;
|
|
|
|
max_fd = conn->sockfd;
|
|
|
|
|
|
|
|
|
|
|
|
return conn;
|
|
|
|
return CONNECTION_NO_ERROR;
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
struct Connection*
|
|
|
|
struct Connection*
|
|
|
|