author | Medo <smaxein@googlemail.com> |
Tue, 12 Jun 2012 11:25:05 +0200 | |
changeset 7224 | 5143861c83bd |
parent 7179 | f84805e6df03 |
child 7234 | 613998625a3c |
permissions | -rw-r--r-- |
7171 | 1 |
#include "socket.h" |
7179
f84805e6df03
Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
7177
diff
changeset
|
2 |
#include "util/logging.h" |
7224
5143861c83bd
Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
7179
diff
changeset
|
3 |
#include "util/util.h" |
7171 | 4 |
#include <stdlib.h> |
5 |
#include <SDL_net.h> |
|
6 |
#include <time.h> |
|
7 |
||
8 |
typedef struct _flib_tcpsocket { |
|
9 |
TCPsocket sock; |
|
10 |
SDLNet_SocketSet sockset; |
|
11 |
} _flib_tcpsocket; |
|
12 |
||
13 |
typedef struct _flib_acceptor { |
|
14 |
TCPsocket sock; |
|
15 |
uint16_t port; |
|
16 |
} _flib_acceptor; |
|
17 |
||
18 |
static uint32_t get_peer_ip(TCPsocket sock) { |
|
19 |
IPaddress *addr = SDLNet_TCP_GetPeerAddress(sock); |
|
20 |
return SDLNet_Read32(&addr->host); |
|
21 |
} |
|
22 |
||
23 |
static bool connection_is_local(TCPsocket sock) { |
|
24 |
return get_peer_ip(sock) == (uint32_t)((127UL<<24)+1); // 127.0.0.1 |
|
25 |
} |
|
26 |
||
7224
5143861c83bd
Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
7179
diff
changeset
|
27 |
static flib_tcpsocket *flib_socket_create(TCPsocket sdlsock) { |
5143861c83bd
Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
7179
diff
changeset
|
28 |
flib_tcpsocket *result = flib_calloc(1, sizeof(_flib_tcpsocket)); |
7175
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
7171
diff
changeset
|
29 |
if(!result) { |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
7171
diff
changeset
|
30 |
return NULL; |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
7171
diff
changeset
|
31 |
} |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
7171
diff
changeset
|
32 |
result->sock = sdlsock; |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
7171
diff
changeset
|
33 |
result->sockset = SDLNet_AllocSocketSet(1); |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
7171
diff
changeset
|
34 |
|
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
7171
diff
changeset
|
35 |
if(!result->sockset) { |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
7171
diff
changeset
|
36 |
flib_log_e("Can't allocate socket: Out of memory!"); |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
7171
diff
changeset
|
37 |
SDLNet_FreeSocketSet(result->sockset); |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
7171
diff
changeset
|
38 |
free(result); |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
7171
diff
changeset
|
39 |
return NULL; |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
7171
diff
changeset
|
40 |
} |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
7171
diff
changeset
|
41 |
|
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
7171
diff
changeset
|
42 |
SDLNet_AddSocket(result->sockset, (SDLNet_GenericSocket)result->sock); |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
7171
diff
changeset
|
43 |
return result; |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
7171
diff
changeset
|
44 |
} |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
7171
diff
changeset
|
45 |
|
7224
5143861c83bd
Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
7179
diff
changeset
|
46 |
flib_acceptor *flib_acceptor_create(uint16_t port) { |
5143861c83bd
Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
7179
diff
changeset
|
47 |
flib_acceptor *result = flib_calloc(1, sizeof(_flib_acceptor)); |
7171 | 48 |
if(!result) { |
49 |
return NULL; |
|
50 |
} |
|
51 |
||
52 |
IPaddress addr; |
|
53 |
addr.host = INADDR_ANY; |
|
54 |
||
55 |
if(port > 0) { |
|
56 |
result->port = port; |
|
57 |
SDLNet_Write16(port, &addr.port); |
|
58 |
result->sock = SDLNet_TCP_Open(&addr); |
|
59 |
if(result->sock) { |
|
60 |
return result; |
|
61 |
} else { |
|
7177
bf6cf4dd847a
Implemented public API for letting the engine render maps
Medo <smaxein@googlemail.com>
parents:
7175
diff
changeset
|
62 |
flib_log_e("Unable to listen on port %u: %s", (unsigned)port, SDLNet_GetError()); |
7171 | 63 |
free(result); |
64 |
return NULL; |
|
65 |
} |
|
66 |
} else { |
|
67 |
/* SDL_net does not seem to have a way to listen on a random unused port |
|
68 |
and find out which port that is, so let's try to find one ourselves. */ |
|
69 |
srand(time(NULL)); |
|
70 |
rand(); |
|
71 |
for(int i=0; i<1000; i++) { |
|
72 |
// IANA suggests using ports in the range 49152-65535 for things like this |
|
73 |
result->port = 49152+(rand()%(65535-49152)); |
|
74 |
SDLNet_Write16(result->port, &addr.port); |
|
75 |
result->sock = SDLNet_TCP_Open(&addr); |
|
76 |
if(result->sock) { |
|
77 |
return result; |
|
78 |
} else { |
|
7177
bf6cf4dd847a
Implemented public API for letting the engine render maps
Medo <smaxein@googlemail.com>
parents:
7175
diff
changeset
|
79 |
flib_log_w("Unable to listen on port %u: %s", (unsigned)result->port, SDLNet_GetError()); |
7171 | 80 |
} |
81 |
} |
|
82 |
flib_log_e("Unable to listen on a random unused port."); |
|
83 |
free(result); |
|
84 |
return NULL; |
|
85 |
} |
|
86 |
} |
|
87 |
||
7224
5143861c83bd
Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
7179
diff
changeset
|
88 |
uint16_t flib_acceptor_listenport(flib_acceptor *acceptor) { |
7175
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
7171
diff
changeset
|
89 |
if(!acceptor) { |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
7171
diff
changeset
|
90 |
flib_log_e("Call to flib_acceptor_listenport with acceptor==null"); |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
7171
diff
changeset
|
91 |
return 0; |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
7171
diff
changeset
|
92 |
} |
7171 | 93 |
return acceptor->port; |
94 |
} |
|
95 |
||
7224
5143861c83bd
Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
7179
diff
changeset
|
96 |
void flib_acceptor_close(flib_acceptor *acceptor) { |
5143861c83bd
Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
7179
diff
changeset
|
97 |
if(acceptor) { |
5143861c83bd
Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
7179
diff
changeset
|
98 |
SDLNet_TCP_Close(acceptor->sock); |
5143861c83bd
Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
7179
diff
changeset
|
99 |
free(acceptor); |
7171 | 100 |
} |
101 |
} |
|
102 |
||
7224
5143861c83bd
Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
7179
diff
changeset
|
103 |
flib_tcpsocket *flib_socket_accept(flib_acceptor *acceptor, bool localOnly) { |
7171 | 104 |
if(!acceptor) { |
7175
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
7171
diff
changeset
|
105 |
flib_log_e("Call to flib_socket_accept with acceptor==null"); |
7171 | 106 |
return NULL; |
107 |
} |
|
7224
5143861c83bd
Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
7179
diff
changeset
|
108 |
flib_tcpsocket *result = NULL; |
7175
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
7171
diff
changeset
|
109 |
TCPsocket sock = NULL; |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
7171
diff
changeset
|
110 |
while(!result && (sock = SDLNet_TCP_Accept(acceptor->sock))) { |
7171 | 111 |
if(localOnly && !connection_is_local(sock)) { |
112 |
flib_log_i("Rejected nonlocal connection attempt from %s", flib_format_ip(get_peer_ip(sock))); |
|
113 |
SDLNet_TCP_Close(sock); |
|
114 |
} else { |
|
7175
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
7171
diff
changeset
|
115 |
result = flib_socket_create(sock); |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
7171
diff
changeset
|
116 |
if(!result) { |
7171 | 117 |
SDLNet_TCP_Close(sock); |
118 |
} |
|
119 |
} |
|
120 |
} |
|
121 |
return result; |
|
122 |
} |
|
123 |
||
7224
5143861c83bd
Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
7179
diff
changeset
|
124 |
void flib_socket_close(flib_tcpsocket *sock) { |
5143861c83bd
Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
7179
diff
changeset
|
125 |
if(sock) { |
7175
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
7171
diff
changeset
|
126 |
SDLNet_DelSocket(sock->sockset, (SDLNet_GenericSocket)sock->sock); |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
7171
diff
changeset
|
127 |
SDLNet_TCP_Close(sock->sock); |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
7171
diff
changeset
|
128 |
SDLNet_FreeSocketSet(sock->sockset); |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
7171
diff
changeset
|
129 |
free(sock); |
7171 | 130 |
} |
131 |
} |
|
132 |
||
7224
5143861c83bd
Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
7179
diff
changeset
|
133 |
int flib_socket_nbrecv(flib_tcpsocket *sock, void *data, int maxlen) { |
7175
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
7171
diff
changeset
|
134 |
if(!sock || (maxlen>0 && !data)) { |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
7171
diff
changeset
|
135 |
flib_log_e("Call to flib_socket_nbrecv with sock==null or data==null"); |
7171 | 136 |
return -1; |
137 |
} |
|
138 |
int readySockets = SDLNet_CheckSockets(sock->sockset, 0); |
|
139 |
if(readySockets>0) { |
|
140 |
int size = SDLNet_TCP_Recv(sock->sock, data, maxlen); |
|
141 |
return size>0 ? size : -1; |
|
142 |
} else if(readySockets==0) { |
|
143 |
return 0; |
|
144 |
} else { |
|
145 |
flib_log_e("Error in select system call: %s", SDLNet_GetError()); |
|
146 |
return -1; |
|
147 |
} |
|
148 |
} |
|
149 |
||
7224
5143861c83bd
Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents:
7179
diff
changeset
|
150 |
int flib_socket_send(flib_tcpsocket *sock, const void *data, int len) { |
7175
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
7171
diff
changeset
|
151 |
if(!sock || (len>0 && !data)) { |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
7171
diff
changeset
|
152 |
flib_log_e("Call to flib_socket_send with sock==null or data==null"); |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
7171
diff
changeset
|
153 |
return -1; |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
7171
diff
changeset
|
154 |
} |
7171 | 155 |
return SDLNet_TCP_Send(sock->sock, data, len); |
156 |
} |