1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
|
/* Pcsx - Pc Psx Emulator
* Copyright (C) 1999-2003 Pcsx Team
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <http://www.gnu.org/licenses>.
*/
#ifdef _WIN32
#include <winsock2.h>
#endif
#include "psxcommon.h"
#include "socket.h"
#include "config.h"
#ifndef _WIN32
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <unistd.h>
#include <fcntl.h>
#endif
int StartServer(unsigned short port) {
struct in_addr localhostaddr;
struct sockaddr_in localsocketaddr;
int ret;
#ifdef _WIN32
WSADATA wsaData;
if (WSAStartup(MAKEWORD(2, 0), &wsaData) != 0)
return -1;
#endif
ret = socket(AF_INET, SOCK_STREAM, 0);
#ifdef _WIN32
if (ret == INVALID_SOCKET)
return -1;
#else
if (ret == -1)
return ret;
#endif
SetsNonblock(ret);
memset((void *)&localhostaddr, 0, sizeof(localhostaddr));
memset(&localsocketaddr, 0, sizeof(struct sockaddr_in));
#ifdef _WIN32
localhostaddr.S_un.S_addr = htonl(INADDR_ANY);
#else
localhostaddr.s_addr = htonl(INADDR_ANY);
#endif
localsocketaddr.sin_family = AF_INET;
localsocketaddr.sin_addr = localhostaddr;
localsocketaddr.sin_port = htons(port);
if (bind(ret, (struct sockaddr *) &localsocketaddr, sizeof(localsocketaddr)) < 0)
return -1;
if (listen(ret, 1) != 0)
return -1;
return ret;
}
void StopServer(int s_socket) {
#ifdef _WIN32
shutdown(s_socket, SD_BOTH);
closesocket(s_socket);
WSACleanup();
#else
shutdown(s_socket, SHUT_RDWR);
close(s_socket);
#endif
}
int GetClient(int s_socket) {
int new_socket = accept(s_socket, NULL, NULL);
#ifdef _WIN32
if (new_socket == INVALID_SOCKET)
return -1;
#else
if (new_socket == -1)
return -1;
#endif
#ifndef _WIN32
{
int flags;
flags = fcntl(new_socket, F_GETFL, 0);
fcntl(new_socket, F_SETFL, flags | O_NONBLOCK);
return new_socket;
}
#endif
}
void CloseClient(int client_socket) {
if (client_socket) {
#ifdef _WIN32
shutdown(client_socket, SD_BOTH);
closesocket(client_socket);
#else
shutdown(client_socket, SHUT_RDWR);
close(client_socket);
#endif
}
}
int HasClient(int client_socket) {
return client_socket > 0;
}
#ifdef _WIN32
static enum read_socket_err ReadSocketOS(SOCKET client_socket, char *buf, size_t *const len)
{
const int res = recv(client_socket, buf, len, 0);
switch (res)
{
case SOCKET_ERROR:
return READ_SOCKET_ERR_RECV;
case 0:
return READ_SOCKET_SHUTDOWN;
default:
*len = res;
break;
}
return READ_SOCKET_OK;
}
#elif defined(_POSIX_VERSION)
static enum read_socket_err ReadSocketOS(int client_socket, char *buf, size_t *const len)
{
const ssize_t res = recv(client_socket, buf, *len, 0);
switch (res)
{
case -1:
return READ_SOCKET_ERR_RECV;
case 0:
return READ_SOCKET_SHUTDOWN;
default:
*len = res;
break;
}
return READ_SOCKET_OK;
}
#endif /* _WIN32 */
enum read_socket_err ReadSocket(int client_socket, char *buf, size_t *const len) {
char * endl;
if (!client_socket || !buf || !len || !*len)
return READ_SOCKET_ERR_INVALID_ARG;
return ReadSocketOS(client_socket, buf, len);
#if 0
#ifdef _WIN32
if (r == SOCKET_ERROR)
#else
if (r == -1)
#endif
{
if (ptr == 0)
return -1;
r = 0;
}
ptr += r;
tbuf[ptr] = 0;
endl = strstr(tbuf, "\r\n");
if (endl) {
r = endl - tbuf;
strncpy(buffer, tbuf, r);
r += 2;
memmove(tbuf, tbuf + r, 512 - r);
ptr -= r;
memset(tbuf + r, 0, 512 - r);
r -= 2;
} else {
r = 0;
}
buffer[r] = 0;
#endif
}
int RawReadSocket(int client_socket, char *buffer, size_t len) {
#if 0
int r = 0;
int mlen = len < ptr ? len : ptr;
if (!client_socket)
return -1;
if (ptr) {
memcpy(buffer, tbuf, mlen);
ptr -= mlen;
memmove(tbuf, tbuf + mlen, 512 - mlen);
}
if (len - mlen)
r = recv(client_socket, buffer + mlen, len - mlen, 0);
if (r == 0) {
if (!ptr)
return 0;
}
#ifdef _WIN32
if (r == SOCKET_ERROR)
#else
if (r == -1)
#endif
{
if (ptr == 0)
return -1;
r = 0;
}
r += mlen;
return r;
#endif
}
void WriteSocket(int client_socket, const char *buffer, size_t len) {
if (!client_socket)
return;
if (send(client_socket, buffer, len, 0) == -1) {
perror("send():");
}
}
void SetsBlock(int s_socket) {
#ifdef _WIN32
u_long b = 0;
ioctlsocket(s_socket, FIONBIO, &b);
#else
int flags = fcntl(s_socket, F_GETFL, 0);
fcntl(s_socket, F_SETFL, flags & ~O_NONBLOCK);
#endif
}
void SetsNonblock(int s_socket) {
#ifdef _WIN32
u_long b = 1;
ioctlsocket(s_socket, FIONBIO, &b);
#else
int flags = fcntl(s_socket, F_GETFL, 0);
fcntl(s_socket, F_SETFL, flags | O_NONBLOCK);
#endif
}
|