|
|
|
@ -1,4 +1,10 @@
|
|
|
|
|
#include "includes/easysock.hpp"
|
|
|
|
|
#include <cstring>
|
|
|
|
|
#include <cstdlib>
|
|
|
|
|
#include <cstdio>
|
|
|
|
|
#include <cerrno>
|
|
|
|
|
#include <fcntl.h>
|
|
|
|
|
|
|
|
|
|
#ifndef _WIN_32
|
|
|
|
|
const int INVALID_SOCKET = -1;
|
|
|
|
|
#endif
|
|
|
|
@ -12,7 +18,6 @@ int sock_init(void) {
|
|
|
|
|
return 0;
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int sock_quit(void) {
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
|
return WSACleanup();
|
|
|
|
@ -21,8 +26,11 @@ int sock_quit(void) {
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int create_socket(int network, char transport) {
|
|
|
|
|
/* Function to create a socket - Accepts IP version(4 or 6), protocol
|
|
|
|
|
type (PROTO_TCP or PROTO_UDP) and a flag to indicate whether the socket
|
|
|
|
|
should be set in blocking mode or not. This flag is ONLY FOR TCP. It does
|
|
|
|
|
nothing if the protocol is UDP.*/
|
|
|
|
|
int create_socket(int network, char transport, bool is_blocking) {
|
|
|
|
|
sock_init();
|
|
|
|
|
int domain;
|
|
|
|
|
int type;
|
|
|
|
@ -35,15 +43,25 @@ int create_socket(int network, char transport) {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (transport == 'T') {
|
|
|
|
|
if (transport == ES_TCP) {
|
|
|
|
|
type = SOCK_STREAM;
|
|
|
|
|
} else if (transport == 'U') {
|
|
|
|
|
} else if (transport == ES_UDP) {
|
|
|
|
|
type = SOCK_DGRAM;
|
|
|
|
|
} else {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int newSock = socket(domain,type,0);
|
|
|
|
|
|
|
|
|
|
/* Set REUSEADDR flag, allowing program to be run twice */
|
|
|
|
|
int set_opt = 1;
|
|
|
|
|
setsockopt(newSock, SOL_SOCKET, SO_REUSEADDR, (char *)&set_opt, sizeof(set_opt));
|
|
|
|
|
|
|
|
|
|
if (is_blocking && transport == ES_TCP) {
|
|
|
|
|
int flags = fcntl(newSock, F_GETFL);
|
|
|
|
|
flags |= O_NONBLOCK;
|
|
|
|
|
fcntl(newSock,F_SETFL,flags);
|
|
|
|
|
}
|
|
|
|
|
return newSock;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -72,8 +90,8 @@ int create_addr(int network, char* address, int port,struct sockaddr* dest) {
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int create_local (int network, char transport, char* address, int port,struct sockaddr* addr_struct) {
|
|
|
|
|
int socket = create_socket(network,transport);
|
|
|
|
|
int create_local (int network, char transport, char* address, int port,struct sockaddr* addr_struct, bool is_blocking) {
|
|
|
|
|
int socket = create_socket(network,transport, is_blocking);
|
|
|
|
|
if (socket < 0) {
|
|
|
|
|
return (-1 * errno);
|
|
|
|
|
}
|
|
|
|
@ -98,7 +116,7 @@ int create_local (int network, char transport, char* address, int port,struct so
|
|
|
|
|
return socket;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int create_remote (int network,char transport,char* address,int port,struct sockaddr* remote_addr_struct) {
|
|
|
|
|
int create_remote (int network,char transport,char* address,int port,struct sockaddr* remote_addr_struct, bool is_blocking) {
|
|
|
|
|
|
|
|
|
|
struct addrinfo hints; /* Used to tell getaddrinfo what kind of address we want */
|
|
|
|
|
struct addrinfo* results; /* Used by getaddrinfo to store the addresses */
|
|
|
|
@ -124,7 +142,7 @@ int create_remote (int network,char transport,char* address,int port,struct sock
|
|
|
|
|
create_addr(network,address,port,remote_addr_struct);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int socket = create_socket(network,transport);
|
|
|
|
|
int socket = create_socket(network,transport, is_blocking);
|
|
|
|
|
if (socket < 0) {
|
|
|
|
|
return (-1 * errno);
|
|
|
|
|
}
|
|
|
|
@ -197,9 +215,9 @@ int inet_to_int(int af_type) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int char_to_socktype(char transport) {
|
|
|
|
|
if (transport == 'T') {
|
|
|
|
|
if (transport == ES_TCP) {
|
|
|
|
|
return SOCK_STREAM;
|
|
|
|
|
} else if (transport == 'U') {
|
|
|
|
|
} else if (transport == ES_UDP) {
|
|
|
|
|
return SOCK_DGRAM;
|
|
|
|
|
} else {
|
|
|
|
|
return -250;
|
|
|
|
|