|
|
|
@ -4,9 +4,26 @@
|
|
|
|
|
#include "connection.h"
|
|
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
|
bool StartupWSA() {
|
|
|
|
|
static bool wsastarted = false;
|
|
|
|
|
if (!wsastarted) {
|
|
|
|
|
WORD wVersionRequested;
|
|
|
|
|
WSADATA wsaData;
|
|
|
|
|
|
|
|
|
|
wVersionRequested = MAKEWORD(2, 2);
|
|
|
|
|
if (WSAStartup(wVersionRequested, &wsaData) == 0) {
|
|
|
|
|
wsastarted = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return wsastarted;
|
|
|
|
|
}
|
|
|
|
|
#else
|
|
|
|
|
#include <sys/socket.h>
|
|
|
|
|
#include <sys/select.h>
|
|
|
|
|
#include <netinet/in.h>
|
|
|
|
|
#endif
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include <fcntl.h>
|
|
|
|
|
#include <stdio.h>
|
|
|
|
@ -106,6 +123,10 @@ connection_new(uint16_t source_port, uint32_t source_ip, uint16_t dest_port, uin
|
|
|
|
|
enum connection_error
|
|
|
|
|
connection_connect(struct Connection* conn) {
|
|
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
|
StartupWSA();
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
// Request a UDP socket
|
|
|
|
|
conn->sockfd = socket(AF_INET, SOCK_DGRAM, 0);
|
|
|
|
|
|
|
|
|
@ -114,8 +135,13 @@ connection_connect(struct Connection* conn) {
|
|
|
|
|
return CONNECTION_LOCAL_BIND;
|
|
|
|
|
|
|
|
|
|
// Make the socket non-blocking
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
|
static unsigned long sock_opts = 1;
|
|
|
|
|
ioctlsocket(conn->sockfd, FIONBIO, &sock_opts); //Non-Blocking
|
|
|
|
|
#else
|
|
|
|
|
int flags = fcntl(conn->sockfd, F_GETFL);
|
|
|
|
|
fcntl(conn->sockfd, F_SETFL, flags | O_NONBLOCK);
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
// File descriptors are numbers that count up sequentially,
|
|
|
|
|
// so save the last one as the greatest file descriptor.
|
|
|
|
|