|
|
@ -4,9 +4,26 @@
|
|
|
|
#include "connection.h"
|
|
|
|
#include "connection.h"
|
|
|
|
|
|
|
|
|
|
|
|
#include <stdlib.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/socket.h>
|
|
|
|
#include <sys/select.h>
|
|
|
|
#include <sys/select.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
|
|
|
|
#endif
|
|
|
|
#include <string.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <stdio.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
|
|
|
|
enum connection_error
|
|
|
|
connection_connect(struct Connection* conn) {
|
|
|
|
connection_connect(struct Connection* conn) {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
|
|
|
|
StartupWSA();
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
// Request a UDP socket
|
|
|
|
// Request a UDP socket
|
|
|
|
conn->sockfd = socket(AF_INET, SOCK_DGRAM, 0);
|
|
|
|
conn->sockfd = socket(AF_INET, SOCK_DGRAM, 0);
|
|
|
|
|
|
|
|
|
|
|
@ -114,8 +135,13 @@ connection_connect(struct Connection* conn) {
|
|
|
|
return CONNECTION_LOCAL_BIND;
|
|
|
|
return CONNECTION_LOCAL_BIND;
|
|
|
|
|
|
|
|
|
|
|
|
// Make the socket non-blocking
|
|
|
|
// 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);
|
|
|
|
int flags = fcntl(conn->sockfd, F_GETFL);
|
|
|
|
fcntl(conn->sockfd, F_SETFL, flags | O_NONBLOCK);
|
|
|
|
fcntl(conn->sockfd, F_SETFL, flags | O_NONBLOCK);
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
// 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.
|
|
|
|