project_files/frontlib/socket.c
author Medo <smaxein@googlemail.com>
Sun, 03 Jun 2012 01:24:18 +0200
changeset 7171 906e72caea7b
child 7175 038e3415100a
permissions -rw-r--r--
frontlib refactoring socket.h now completely wraps all the lowlevel neworking, so it would be easy to switch away from SDL_net if needed. Also reduced global state by making an IPC connection an object-like thing.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
7171
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
     1
#include "socket.h"
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
     2
#include "logging.h"
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
     3
#include <stdlib.h>
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
     4
#include <SDL_net.h>
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
     5
#include <time.h>
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
     6
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
     7
typedef struct _flib_tcpsocket {
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
     8
	TCPsocket sock;
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
     9
	SDLNet_SocketSet sockset;
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
    10
} _flib_tcpsocket;
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
    11
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
    12
typedef struct _flib_acceptor {
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
    13
	TCPsocket sock;
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
    14
	uint16_t port;
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
    15
} _flib_acceptor;
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
    16
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
    17
static uint32_t get_peer_ip(TCPsocket sock) {
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
    18
	IPaddress *addr = SDLNet_TCP_GetPeerAddress(sock);
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
    19
	return SDLNet_Read32(&addr->host);
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
    20
}
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
    21
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
    22
static bool connection_is_local(TCPsocket sock) {
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
    23
	return get_peer_ip(sock) == (uint32_t)((127UL<<24)+1); // 127.0.0.1
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
    24
}
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
    25
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
    26
flib_acceptor flib_acceptor_create(uint16_t port) {
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
    27
	flib_acceptor result = malloc(sizeof(_flib_acceptor));
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
    28
	if(!result) {
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
    29
		flib_log_e("Out of memory!");
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
    30
		return NULL;
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
    31
	}
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
    32
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
    33
	IPaddress addr;
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
    34
	addr.host = INADDR_ANY;
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
    35
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
    36
	if(port > 0) {
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
    37
		result->port = port;
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
    38
		SDLNet_Write16(port, &addr.port);
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
    39
		result->sock = SDLNet_TCP_Open(&addr);
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
    40
		if(result->sock) {
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
    41
			return result;
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
    42
		} else {
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
    43
			flib_log_e("Unable to listen on port %u: %s", port, SDLNet_GetError());
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
    44
			free(result);
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
    45
			return NULL;
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
    46
		}
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
    47
	} else {
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
    48
		/* SDL_net does not seem to have a way to listen on a random unused port
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
    49
		   and find out which port that is, so let's try to find one ourselves. */
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
    50
		// TODO: Is socket binding fail-fast on all platforms?
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
    51
		srand(time(NULL));
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
    52
		rand();
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
    53
		for(int i=0; i<1000; i++) {
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
    54
			// IANA suggests using ports in the range 49152-65535 for things like this
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
    55
			result->port = 49152+(rand()%(65535-49152));
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
    56
			SDLNet_Write16(result->port, &addr.port);
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
    57
			result->sock = SDLNet_TCP_Open(&addr);
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
    58
			if(result->sock) {
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
    59
				return result;
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
    60
			} else {
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
    61
				flib_log_i("Unable to listen on port %u: %s", result->port, SDLNet_GetError());
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
    62
			}
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
    63
		}
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
    64
		flib_log_e("Unable to listen on a random unused port.");
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
    65
		free(result);
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
    66
		return NULL;
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
    67
	}
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
    68
}
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
    69
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
    70
uint16_t flib_acceptor_listenport(flib_acceptor acceptor) {
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
    71
	return acceptor->port;
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
    72
}
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
    73
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
    74
void flib_acceptor_close(flib_acceptor *acceptorptr) {
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
    75
	if(acceptorptr == NULL || *acceptorptr == NULL) {
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
    76
		return;
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
    77
	}
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
    78
	SDLNet_TCP_Close((*acceptorptr)->sock);
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
    79
	free(*acceptorptr);
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
    80
	*acceptorptr = NULL;
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
    81
}
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
    82
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
    83
flib_tcpsocket flib_socket_accept(flib_acceptor acceptor, bool localOnly) {
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
    84
	flib_tcpsocket result = NULL;
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
    85
	if(!acceptor) {
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
    86
		return NULL;
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
    87
	}
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
    88
	while(result==NULL) {
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
    89
		TCPsocket sock = SDLNet_TCP_Accept(acceptor->sock);
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
    90
		if(!sock) {
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
    91
			// No incoming connections
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
    92
			return NULL;
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
    93
		}
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
    94
		if(localOnly && !connection_is_local(sock)) {
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
    95
			flib_log_i("Rejected nonlocal connection attempt from %s", flib_format_ip(get_peer_ip(sock)));
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
    96
			SDLNet_TCP_Close(sock);
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
    97
		} else {
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
    98
			result = malloc(sizeof(_flib_tcpsocket));
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
    99
			if(result==NULL) {
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
   100
				flib_log_e("Out of memory!");
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
   101
				SDLNet_TCP_Close(sock);
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
   102
				return NULL;
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
   103
			}
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
   104
			result->sock = sock;
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
   105
			result->sockset = SDLNet_AllocSocketSet(1);
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
   106
			if(result->sockset==NULL) {
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
   107
				flib_log_e("Out of memory!");
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
   108
				SDLNet_TCP_Close(sock);
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
   109
				free(result);
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
   110
				return NULL;
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
   111
			}
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
   112
			SDLNet_AddSocket(result->sockset, (SDLNet_GenericSocket)result->sock);
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
   113
		}
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
   114
	}
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
   115
	return result;
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
   116
}
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
   117
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
   118
void flib_socket_close(flib_tcpsocket *sockptr) {
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
   119
	if(sockptr==NULL || *sockptr == NULL) {
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
   120
		return;
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
   121
	}
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
   122
	flib_tcpsocket sock = *sockptr;
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
   123
	SDLNet_DelSocket(sock->sockset, (SDLNet_GenericSocket)sock->sock);
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
   124
	SDLNet_TCP_Close(sock->sock);
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
   125
	SDLNet_FreeSocketSet(sock->sockset);
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
   126
	free(sock);
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
   127
	*sockptr = NULL;
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
   128
}
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
   129
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
   130
int flib_socket_nbrecv(flib_tcpsocket sock, void *data, int maxlen) {
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
   131
	if(!sock) {
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
   132
		flib_log_e("Attempt to receive on a NULL socket.");
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
   133
		return -1;
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
   134
	}
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
   135
	int readySockets = SDLNet_CheckSockets(sock->sockset, 0);
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
   136
	if(readySockets>0) {
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
   137
		int size = SDLNet_TCP_Recv(sock->sock, data, maxlen);
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
   138
		return size>0 ? size : -1;
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
   139
	} else if(readySockets==0) {
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
   140
		return 0;
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
   141
	} else {
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
   142
		flib_log_e("Error in select system call: %s", SDLNet_GetError());
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
   143
		return -1;
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
   144
	}
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
   145
}
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
   146
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
   147
int flib_socket_send(flib_tcpsocket sock, void *data, int len) {
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
   148
	return SDLNet_TCP_Send(sock->sock, data, len);
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
   149
}