|
|
|
@ -11,6 +11,7 @@
|
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include <fcntl.h>
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
|
|
struct Connection {
|
|
|
|
|
int sockfd;
|
|
|
|
@ -21,7 +22,7 @@ struct Connection {
|
|
|
|
|
inline
|
|
|
|
|
static
|
|
|
|
|
struct Connection*
|
|
|
|
|
connection_new()
|
|
|
|
|
connection_new(unsigned int source_port, const char* source_ip, unsigned int dest_port, const char* dest_ip)
|
|
|
|
|
{
|
|
|
|
|
struct Connection* conn = malloc(sizeof(struct Connection));
|
|
|
|
|
memset(conn, 0, sizeof(struct Connection));
|
|
|
|
@ -29,19 +30,44 @@ connection_new()
|
|
|
|
|
// Request a UDP socket
|
|
|
|
|
conn->sockfd = socket(AF_INET, SOCK_DGRAM, 0);
|
|
|
|
|
|
|
|
|
|
if(source_port && source_ip) {
|
|
|
|
|
struct sockaddr_in loc_addr;
|
|
|
|
|
loc_addr.sin_family = AF_INET;
|
|
|
|
|
loc_addr.sin_port = htons(source_port);
|
|
|
|
|
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
|
|
|
|
|
memset(&conn->remote_addr, 0, sizeof(conn->remote_addr));
|
|
|
|
|
conn->addrlen = sizeof(conn->remote_addr);
|
|
|
|
|
conn->remote_addr.sin_family = AF_INET;
|
|
|
|
|
conn->remote_addr.sin_port = htons(30030);
|
|
|
|
|
conn->remote_addr.sin_addr.s_addr = inet_addr("192.168.2.1");
|
|
|
|
|
conn->remote_addr.sin_port = htons(dest_port);
|
|
|
|
|
conn->remote_addr.sin_addr.s_addr = inet_addr(dest_ip);
|
|
|
|
|
|
|
|
|
|
return conn;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
inline
|
|
|
|
|
static
|
|
|
|
|
void
|
|
|
|
|
connection_set_remote(struct Connection* conn, unsigned int dest_port, const char* dest_ip)
|
|
|
|
|
{
|
|
|
|
|
// Set the address of the drone
|
|
|
|
|
conn->addrlen = sizeof(conn->remote_addr);
|
|
|
|
|
conn->remote_addr.sin_family = AF_INET;
|
|
|
|
|
conn->remote_addr.sin_port = htons(dest_port);
|
|
|
|
|
conn->remote_addr.sin_addr.s_addr = inet_addr(dest_ip);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static
|
|
|
|
|
inline
|
|
|
|
|
void
|
|
|
|
|