project_files/frontlib/socket.c
changeset 7224 5143861c83bd
parent 7179 f84805e6df03
child 7234 613998625a3c
equal deleted inserted replaced
7221:8d04e85ca204 7224:5143861c83bd
     1 #include "socket.h"
     1 #include "socket.h"
     2 #include "util/logging.h"
     2 #include "util/logging.h"
       
     3 #include "util/util.h"
     3 #include <stdlib.h>
     4 #include <stdlib.h>
     4 #include <SDL_net.h>
     5 #include <SDL_net.h>
     5 #include <time.h>
     6 #include <time.h>
     6 
     7 
     7 typedef struct _flib_tcpsocket {
     8 typedef struct _flib_tcpsocket {
    21 
    22 
    22 static bool connection_is_local(TCPsocket sock) {
    23 static bool connection_is_local(TCPsocket sock) {
    23 	return get_peer_ip(sock) == (uint32_t)((127UL<<24)+1); // 127.0.0.1
    24 	return get_peer_ip(sock) == (uint32_t)((127UL<<24)+1); // 127.0.0.1
    24 }
    25 }
    25 
    26 
    26 static flib_tcpsocket flib_socket_create(TCPsocket sdlsock) {
    27 static flib_tcpsocket *flib_socket_create(TCPsocket sdlsock) {
    27 	flib_tcpsocket result = malloc(sizeof(_flib_tcpsocket));
    28 	flib_tcpsocket *result = flib_calloc(1, sizeof(_flib_tcpsocket));
    28 	if(!result) {
    29 	if(!result) {
    29 		flib_log_e("Can't allocate socket: Out of memory!");
       
    30 		return NULL;
    30 		return NULL;
    31 	}
    31 	}
    32 	result->sock = sdlsock;
    32 	result->sock = sdlsock;
    33 	result->sockset = SDLNet_AllocSocketSet(1);
    33 	result->sockset = SDLNet_AllocSocketSet(1);
    34 
    34 
    41 
    41 
    42 	SDLNet_AddSocket(result->sockset, (SDLNet_GenericSocket)result->sock);
    42 	SDLNet_AddSocket(result->sockset, (SDLNet_GenericSocket)result->sock);
    43 	return result;
    43 	return result;
    44 }
    44 }
    45 
    45 
    46 flib_acceptor flib_acceptor_create(uint16_t port) {
    46 flib_acceptor *flib_acceptor_create(uint16_t port) {
    47 	flib_acceptor result = malloc(sizeof(_flib_acceptor));
    47 	flib_acceptor *result = flib_calloc(1, sizeof(_flib_acceptor));
    48 	if(!result) {
    48 	if(!result) {
    49 		flib_log_e("Can't allocate acceptor: Out of memory!");
       
    50 		return NULL;
    49 		return NULL;
    51 	}
    50 	}
    52 
    51 
    53 	IPaddress addr;
    52 	IPaddress addr;
    54 	addr.host = INADDR_ANY;
    53 	addr.host = INADDR_ANY;
    84 		free(result);
    83 		free(result);
    85 		return NULL;
    84 		return NULL;
    86 	}
    85 	}
    87 }
    86 }
    88 
    87 
    89 uint16_t flib_acceptor_listenport(flib_acceptor acceptor) {
    88 uint16_t flib_acceptor_listenport(flib_acceptor *acceptor) {
    90 	if(!acceptor) {
    89 	if(!acceptor) {
    91 		flib_log_e("Call to flib_acceptor_listenport with acceptor==null");
    90 		flib_log_e("Call to flib_acceptor_listenport with acceptor==null");
    92 		return 0;
    91 		return 0;
    93 	}
    92 	}
    94 	return acceptor->port;
    93 	return acceptor->port;
    95 }
    94 }
    96 
    95 
    97 void flib_acceptor_close(flib_acceptor *acceptorptr) {
    96 void flib_acceptor_close(flib_acceptor *acceptor) {
    98 	if(!acceptorptr) {
    97 	if(acceptor) {
    99 		flib_log_e("Call to flib_acceptor_close with acceptorptr==null");
    98 		SDLNet_TCP_Close(acceptor->sock);
   100 	} else if(*acceptorptr) {
    99 		free(acceptor);
   101 		SDLNet_TCP_Close((*acceptorptr)->sock);
       
   102 		free(*acceptorptr);
       
   103 		*acceptorptr = NULL;
       
   104 	}
   100 	}
   105 }
   101 }
   106 
   102 
   107 flib_tcpsocket flib_socket_accept(flib_acceptor acceptor, bool localOnly) {
   103 flib_tcpsocket *flib_socket_accept(flib_acceptor *acceptor, bool localOnly) {
   108 	if(!acceptor) {
   104 	if(!acceptor) {
   109 		flib_log_e("Call to flib_socket_accept with acceptor==null");
   105 		flib_log_e("Call to flib_socket_accept with acceptor==null");
   110 		return NULL;
   106 		return NULL;
   111 	}
   107 	}
   112 	flib_tcpsocket result = NULL;
   108 	flib_tcpsocket *result = NULL;
   113 	TCPsocket sock = NULL;
   109 	TCPsocket sock = NULL;
   114 	while(!result && (sock = SDLNet_TCP_Accept(acceptor->sock))) {
   110 	while(!result && (sock = SDLNet_TCP_Accept(acceptor->sock))) {
   115 		if(localOnly && !connection_is_local(sock)) {
   111 		if(localOnly && !connection_is_local(sock)) {
   116 			flib_log_i("Rejected nonlocal connection attempt from %s", flib_format_ip(get_peer_ip(sock)));
   112 			flib_log_i("Rejected nonlocal connection attempt from %s", flib_format_ip(get_peer_ip(sock)));
   117 			SDLNet_TCP_Close(sock);
   113 			SDLNet_TCP_Close(sock);
   123 		}
   119 		}
   124 	}
   120 	}
   125 	return result;
   121 	return result;
   126 }
   122 }
   127 
   123 
   128 void flib_socket_close(flib_tcpsocket *sockptr) {
   124 void flib_socket_close(flib_tcpsocket *sock) {
   129 	if(!sockptr) {
   125 	if(sock) {
   130 		flib_log_e("Call to flib_socket_close with sockptr==null");
       
   131 	} else if(*sockptr) {
       
   132 		flib_tcpsocket sock = *sockptr;
       
   133 		SDLNet_DelSocket(sock->sockset, (SDLNet_GenericSocket)sock->sock);
   126 		SDLNet_DelSocket(sock->sockset, (SDLNet_GenericSocket)sock->sock);
   134 		SDLNet_TCP_Close(sock->sock);
   127 		SDLNet_TCP_Close(sock->sock);
   135 		SDLNet_FreeSocketSet(sock->sockset);
   128 		SDLNet_FreeSocketSet(sock->sockset);
   136 		free(sock);
   129 		free(sock);
   137 		*sockptr = NULL;
       
   138 	}
   130 	}
   139 }
   131 }
   140 
   132 
   141 int flib_socket_nbrecv(flib_tcpsocket sock, void *data, int maxlen) {
   133 int flib_socket_nbrecv(flib_tcpsocket *sock, void *data, int maxlen) {
   142 	if(!sock || (maxlen>0 && !data)) {
   134 	if(!sock || (maxlen>0 && !data)) {
   143 		flib_log_e("Call to flib_socket_nbrecv with sock==null or data==null");
   135 		flib_log_e("Call to flib_socket_nbrecv with sock==null or data==null");
   144 		return -1;
   136 		return -1;
   145 	}
   137 	}
   146 	int readySockets = SDLNet_CheckSockets(sock->sockset, 0);
   138 	int readySockets = SDLNet_CheckSockets(sock->sockset, 0);
   153 		flib_log_e("Error in select system call: %s", SDLNet_GetError());
   145 		flib_log_e("Error in select system call: %s", SDLNet_GetError());
   154 		return -1;
   146 		return -1;
   155 	}
   147 	}
   156 }
   148 }
   157 
   149 
   158 int flib_socket_send(flib_tcpsocket sock, const void *data, int len) {
   150 int flib_socket_send(flib_tcpsocket *sock, const void *data, int len) {
   159 	if(!sock || (len>0 && !data)) {
   151 	if(!sock || (len>0 && !data)) {
   160 		flib_log_e("Call to flib_socket_send with sock==null or data==null");
   152 		flib_log_e("Call to flib_socket_send with sock==null or data==null");
   161 		return -1;
   153 		return -1;
   162 	}
   154 	}
   163 	return SDLNet_TCP_Send(sock->sock, data, len);
   155 	return SDLNet_TCP_Send(sock->sock, data, len);