|
|
|
@ -5,7 +5,7 @@
|
|
|
|
|
#include <cerrno>
|
|
|
|
|
#include <fcntl.h>
|
|
|
|
|
|
|
|
|
|
#ifndef _WIN_32
|
|
|
|
|
#ifndef _WIN32
|
|
|
|
|
const int INVALID_SOCKET = -1;
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
@ -27,10 +27,8 @@ int sock_quit(void) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 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) {
|
|
|
|
|
type (PROTO_TCP or PROTO_UDP). */
|
|
|
|
|
SOCKET create_socket(int network, char transport) {
|
|
|
|
|
sock_init();
|
|
|
|
|
int domain;
|
|
|
|
|
int type;
|
|
|
|
@ -57,11 +55,6 @@ int create_socket(int network, char transport, bool is_blocking) {
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -90,8 +83,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, bool is_blocking) {
|
|
|
|
|
int socket = create_socket(network,transport, is_blocking);
|
|
|
|
|
SOCKET create_local (int network, char transport, char* address, int port,struct sockaddr* addr_struct) {
|
|
|
|
|
int socket = create_socket(network,transport);
|
|
|
|
|
if (socket < 0) {
|
|
|
|
|
return (-1 * errno);
|
|
|
|
|
}
|
|
|
|
@ -116,7 +109,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, bool is_blocking) {
|
|
|
|
|
SOCKET create_remote (int network,char transport,char* address,int port,struct sockaddr* remote_addr_struct) {
|
|
|
|
|
|
|
|
|
|
struct addrinfo hints; /* Used to tell getaddrinfo what kind of address we want */
|
|
|
|
|
struct addrinfo* results; /* Used by getaddrinfo to store the addresses */
|
|
|
|
@ -142,7 +135,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, is_blocking);
|
|
|
|
|
int socket = create_socket(network,transport);
|
|
|
|
|
if (socket < 0) {
|
|
|
|
|
return (-1 * errno);
|
|
|
|
|
}
|
|
|
|
|