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