|
|
@ -11,6 +11,7 @@
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
|
|
|
|
struct Connection {
|
|
|
|
struct Connection {
|
|
|
|
int sockfd;
|
|
|
|
int sockfd;
|
|
|
@ -21,27 +22,52 @@ struct Connection {
|
|
|
|
inline
|
|
|
|
inline
|
|
|
|
static
|
|
|
|
static
|
|
|
|
struct Connection*
|
|
|
|
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));
|
|
|
|
struct Connection* conn = malloc(sizeof(struct Connection));
|
|
|
|
memset(conn, 0, sizeof(struct Connection));
|
|
|
|
memset(conn, 0, sizeof(struct Connection));
|
|
|
|
|
|
|
|
|
|
|
|
// Request a UDP socket
|
|
|
|
// Request a UDP socket
|
|
|
|
conn->sockfd = socket(AF_INET, SOCK_DGRAM, 0);
|
|
|
|
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
|
|
|
|
// Make the socket non-blocking
|
|
|
|
int flags = fcntl(conn->sockfd, F_GETFL);
|
|
|
|
int flags = fcntl(conn->sockfd, F_GETFL);
|
|
|
|
fcntl(conn->sockfd, F_SETFL, flags | O_NONBLOCK);
|
|
|
|
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));
|
|
|
|
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(30030);
|
|
|
|
conn->remote_addr.sin_port = htons(dest_port);
|
|
|
|
conn->remote_addr.sin_addr.s_addr = inet_addr("192.168.2.1");
|
|
|
|
conn->remote_addr.sin_addr.s_addr = inet_addr(dest_ip);
|
|
|
|
|
|
|
|
|
|
|
|
return conn;
|
|
|
|
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
|
|
|
|
static
|
|
|
|
inline
|
|
|
|
inline
|
|
|
|
void
|
|
|
|
void
|
|
|
|