-
Notifications
You must be signed in to change notification settings - Fork 3
/
psxnet.c
280 lines (171 loc) · 4.33 KB
/
psxnet.c
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
#include <sys/types.h>
#include "psxnet.h"
#include "psxnet_cmd.h"
#include "serial.h"
static void _net_SendCommand(u_char code) {
u_char cmd[] = { 0x8a, code };
ser_SendBytes(cmd, 2);
}
static int _net_GetResult() {
u_char result[4];
int r,c=0;
while(c<4) {
r = ser_ReadBytes(&result[c], 4);
if (r <= 0)
continue;
c += r;
}
return(*((int*)result));
}
static void _net_crc32_init(unsigned int* table) {
int i,j;
unsigned int crcVal;
for(i=0; i<256; i++) {
crcVal = i;
for(j=0; j<8; j++) {
if (crcVal&0x00000001L)
crcVal = (crcVal>>1)^0xEDB88320L;
else
crcVal = crcVal>>1;
}
table[i] = crcVal;
}
}
unsigned int net_crc32(void* buff, int bytes, unsigned int crc) {
int i;
unsigned char* byteBuff = (unsigned char*)buff;
unsigned int byte;
unsigned int crcTable[256];
_net_crc32_init(crcTable);
for(i=0; i<bytes; i++) {
byte = 0x000000ffL&(unsigned int)byteBuff[i];
crc = (crc>>8)^crcTable[(crc^byte)&0xff];
}
return(crc^0xFFFFFFFF);
}
int net_Init(int baud) {
int result=0;
// Initialize serial
ser_Init(baud);
// Send a test command to the client
_net_SendCommand(NET_CMD_TEST);
// Get result
result = _net_GetResult();
// Check if result is correct
if (result == 0xdeadbeef)
return(NET_ERR_INIT);
// Return
return(NET_ERR_OK);
}
int net_GetClientInfo(char* infobuff, int infolen) {
int len=0;
int result=0xff;
_net_SendCommand(NET_CMD_GETINFO);
result = _net_GetResult();
if (result != 0)
return(NET_ERR_CMD);
ser_ReadBytes(&len, 2);
if (len > infolen) {
short i;
char dummy;
ser_ReadBytes(infobuff, infolen);
for(i=0; i<(len-infolen); i++)
ser_ReadBytes(&dummy, 1);
} else {
ser_ReadBytes(infobuff, len);
}
return(NET_ERR_OK);
}
int net_Connect(char* ipaddr, int port, int type) {
int result=-1;
short len;
_net_SendCommand(NET_CMD_CONNECT);
result = _net_GetResult();
if (result != 0)
return(NET_ERR_CMD);
ser_SendBytes(&port, sizeof(short));
ser_SendBytes(&type, sizeof(short));
len = strlen(ipaddr);
ser_SendBytes(&len, 2);
ser_SendBytes(ipaddr, len);
while(ser_ReadBytes(&result, sizeof(int)) == 0);
if (result < 0)
return(NET_ERR_CONNECT);
return(result);
}
int net_ConnectHost(char* hostName, int port, int connectType, char* ip) {
int hostNameLen = strlen(hostName)+1;
int socket,result;
_net_SendCommand(NET_CMD_CONNECTHOST);
result = _net_GetResult();
ser_SendBytes(&hostNameLen, sizeof(short));
ser_SendBytes(&connectType, sizeof(short));
ser_SendBytes(&port, sizeof(short));
ser_SendBytes(hostName, hostNameLen);
// Wait until result is relayed back
while(ser_ReadBytes(&result, sizeof(int)) == 0);
if (result < 0)
return(NET_ERR_CONNECT);
socket = result;
ser_SendBytes(&result, sizeof(int));
ser_ReadBytes(ip, 18);
return(socket);
}
int net_Disconnect(int socket) {
int result=0xff;
_net_SendCommand(NET_CMD_DISCONNECT);
result = _net_GetResult();
ser_SendBytes(&socket, 2);
return(NET_ERR_OK);
}
int net_Send(int socket, void* buff, int bytes) {
int result,crc32;
_net_SendCommand(NET_CMD_SEND);
result = _net_GetResult();
crc32 = net_crc32(buff, bytes, NET_CRC32_REMAINDER);
// Send socket, data length and CRC32
ser_SendBytes(&socket, sizeof(short));
ser_SendBytes(&bytes, sizeof(u_short));
ser_SendBytes(&crc32, sizeof(int));
// Wait for acknowledgment
result = _net_GetResult();
do {
// Send data
ser_SendBytes(buff, bytes);
// Get result
result = _net_GetResult();
// Retry if CRC check failed
} while (result != 0);
// Get result from sending packet
result = _net_GetResult();
return(result);
}
int net_Receive(int socket, void* buff, int bytes) {
int result;
int rec=0,dlen;
unsigned int crc32;
_net_SendCommand(NET_CMD_RECEIVE);
result = _net_GetResult();
// Send socket and data length
ser_SendBytes(&socket, sizeof(short));
ser_SendBytes(&bytes, sizeof(u_short));
// Get downloaded length
dlen = _net_GetResult();
// If failed
if (dlen <= 0)
return(-1);
// Get CRC
while(ser_ReadBytes(&crc32, sizeof(int)) <= 0);
do {
// Begin receiving stream
while(rec < dlen) {
rec += ser_ReadBytes(&buff[rec], dlen);
}
if (net_crc32(buff, dlen, NET_CRC32_REMAINDER) != crc32)
result = 1;
else
result = 0;
ser_SendBytes(&result, sizeof(int));
} while(result != 0);
return(dlen);
}