-
Notifications
You must be signed in to change notification settings - Fork 4
/
net_wipx.pas
548 lines (455 loc) · 13.8 KB
/
net_wipx.pas
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
// ------------------------------------------------------------------------------
// DelphiQuake, Copyright (C) 2005-2011 by Jim Valavanis
// E-Mail: [email protected]
//
// Copyright (C) 1996-1997 Id Software, Inc.
//
// 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, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
//
// ------------------------------------------------------------------------------
{$I dquake.inc}
{$Z4}
unit net_wipx;
interface
uses
q_delphi,
net;
function WIPX_Init: integer;
procedure WIPX_Shutdown;
procedure WIPX_Listen(state: qboolean);
function WIPX_OpenSocket(port: integer): integer;
function WIPX_CloseSocket(handle: integer): integer;
function WIPX_Connect(handle: integer; addr: Pqsockaddr_t): integer;
function WIPX_CheckNewConnections: integer;
function WIPX_Read(handle: integer; buf: PByteArray; len: integer; addr: Pqsockaddr_t): integer;
function WIPX_Broadcast(handle: integer; buf: PByteArray; len: integer): integer;
function WIPX_Write(handle: integer; buf: PByteArray; len: integer; addr: Pqsockaddr_t): integer;
function WIPX_AddrToString(addr: Pqsockaddr_t): PChar;
function WIPX_StringToAddr(str: PChar; addr: Pqsockaddr_t): integer;
function WIPX_GetSocketAddr(handle: integer; addr: Pqsockaddr_t): integer;
function WIPX_GetNameFromAddr(addr: Pqsockaddr_t; name: PChar): integer;
function WIPX_GetAddrFromName(name: PChar; addr: Pqsockaddr_t): integer;
function WIPX_AddrCompare(addr1: Pqsockaddr_t; addr2: Pqsockaddr_t): integer;
function WIPX_GetSocketPort(addr: Pqsockaddr_t): integer;
function WIPX_SetSocketPort(addr: Pqsockaddr_t; port: integer): integer;
implementation
uses
Windows,
winsock,
wsipx_h,
net_wins,
common,
console,
net_main,
cvar,
sys_win;
const
MAXHOSTNAMELEN = 256;
var
net_acceptsocket: integer = -1; // socket for fielding new connections
net_controlsocket: integer;
broadcastaddr: qsockaddr_t;
const
IPXSOCKETS = 18;
var
ipxsocket: array[0..IPXSOCKETS - 1] of integer;
sequence: array[0..IPXSOCKETS - 1] of integer;
//=============================================================================
function WIPX_Init: integer;
var
i: integer;
buff: array[0..MAXHOSTNAMELEN - 1] of char;
addr: qsockaddr_t;
p: PChar;
r: integer;
wVersionRequested: word;
begin
if COM_CheckParm('-noipx') <> 0 then
begin
result := -1;
exit;
end;
if winsock_initialized = 0 then
begin
wVersionRequested := MAKEWORD(1, 1);
r := WSAStartup(wVersionRequested, winsockdata);
if r <> 0 then
begin
Con_Printf('Winsock initialization failed.'#10);
result := -1;
exit;
end;
end;
inc(winsock_initialized);
for i := 0 to IPXSOCKETS - 1 do
ipxsocket[i] := 0;
// determine my name & address
if gethostname(buff, MAXHOSTNAMELEN) = 0 then
begin
// if the quake hostname isn't set, set it to the machine name
if Q_strcmp(hostname.text, 'UNNAMED') = 0 then
begin
// see if it's a text IP address (well, close enough)
p := @buff[0];
while p^ <> #0 do
begin
if ((p^ < '0') or (p^ > '9')) and (p^ <> '.') then
break;
inc(p);
end;
// if it is a real name, strip off the domain; we only want the host
if p^ <> #0 then
begin
i := 0;
while i < 15 do
begin
if buff[i] = '.' then
break;
inc(i);
end;
buff[i] := #0;
end;
Cvar_Set('hostname', buff);
end;
end;
net_controlsocket := WIPX_OpenSocket(0);
if net_controlsocket = -1 then
begin
Con_Printf('WIPX_Init: Unable to open control socket'#10);
dec(winsock_initialized);
if winsock_initialized = 0 then
WSACleanup;
result := -1;
exit;
end;
PSockAddrIpx(@broadcastaddr).sa_family := AF_IPX;
memset(@PSockAddrIpx(@broadcastaddr).sa_netnum, 0, 4);
memset(@PSockAddrIpx(@broadcastaddr).sa_nodenum, $FF, 6);
PSockAddrIpx(@broadcastaddr).sa_socket := htons(u_short(net_hostport));
WIPX_GetSocketAddr(net_controlsocket, @addr);
Q_strcpy(my_ipx_address, WIPX_AddrToString(@addr));
p := Q_strrchr(my_ipx_address, ':');
if p <> nil then
p^ := #0;
Con_Printf('Winsock IPX Initialized'#10);
ipxAvailable := true;
result := net_controlsocket;
end;
//=============================================================================
procedure WIPX_Shutdown;
begin
WIPX_Listen(false);
WIPX_CloseSocket(net_controlsocket);
dec(winsock_initialized);
if winsock_initialized = 0 then
WSACleanup;
end;
//=============================================================================
procedure WIPX_Listen(state: qboolean);
begin
// enable listening
if state then
begin
if net_acceptsocket <> -1 then
exit;
net_acceptsocket := WIPX_OpenSocket(net_hostport);
if net_acceptsocket = -1 then
Sys_Error('WIPX_Listen: Unable to open accept socket'#10);
exit;
end;
// disable listening
if net_acceptsocket = -1 then
exit;
WIPX_CloseSocket(net_acceptsocket);
net_acceptsocket := -1;
end;
//=============================================================================
function WIPX_OpenSocket(port: integer): integer;
label
ErrorReturn;
var
handle: integer;
newsocket: integer;
address: TSockAddrIpx;
_true: u_long;
begin
_true := 1;
handle := 0;
while handle < IPXSOCKETS do
begin
if ipxsocket[handle] = 0 then
break;
inc(handle);
end;
if handle = IPXSOCKETS then
begin
result := -1;
exit;
end;
newsocket := socket(AF_IPX, SOCK_DGRAM, NSPROTO_IPX);
if newsocket = INVALID_SOCKET then
begin
result := -1;
exit;
end;
if ioctlsocket(newsocket, FIONBIO, _true) = -1 then
goto ErrorReturn;
if setsockopt(newsocket, SOL_SOCKET, SO_BROADCAST, PChar(@_true), SizeOf(_true)) < 0 then
goto ErrorReturn;
address.sa_family := AF_IPX;
ZeroMemory(@address.sa_netnum, 4);
ZeroMemory(@address.sa_nodenum, 6); ;
address.sa_socket := htons(u_short(port));
if bind(newsocket, PSockAddr(@address)^, SizeOf(address)) = 0 then
begin
ipxsocket[handle] := newsocket;
sequence[handle] := 0;
result := handle;
exit;
end;
Sys_Error('Winsock IPX bind failed'#10);
ErrorReturn:
closesocket(newsocket);
result := -1;
end;
//=============================================================================
function WIPX_CloseSocket(handle: integer): integer;
var
socket: integer;
begin
socket := ipxsocket[handle];
result := closesocket(socket);
ipxsocket[handle] := 0;
end;
//=============================================================================
function WIPX_Connect(handle: integer; addr: Pqsockaddr_t): integer;
begin
result := 0;
end;
//=============================================================================
function WIPX_CheckNewConnections: integer;
var
available: u_long;
begin
if net_acceptsocket = -1 then
begin
result := -1;
exit;
end;
if ioctlsocket(ipxsocket[net_acceptsocket], FIONREAD, available) = -1 then
Sys_Error('WIPX: ioctlsocket (FIONREAD) failed'#10);
if available <> 0 then
result := net_acceptsocket
else
result := -1;
end;
//=============================================================================
var
packetBuffer: array[0..NET_DATAGRAMSIZE + 3] of byte;
function WIPX_Read(handle: integer; buf: PByteArray; len: integer; addr: Pqsockaddr_t): integer;
var
addrlen: integer;
socket: integer;
errno: integer;
begin
addrlen := SizeOf(qsockaddr_t);
socket := ipxsocket[handle];
result := recvfrom_a(socket, packetBuffer, len + 4, 0, PSockAddr(addr), @addrlen);
if result = -1 then
begin
errno := WSAGetLastError;
if (errno = WSAEWOULDBLOCK) or (errno = WSAECONNREFUSED) then
begin
result := 0;
exit;
end;
end;
if result < 4 then
begin
result := 0;
exit;
end;
// remove sequence number, it's only needed for DOS IPX
dec(result, 4);
memcpy(buf, @packetBuffer[4], result);
end;
//=============================================================================
function WIPX_Broadcast(handle: integer; buf: PByteArray; len: integer): integer;
begin
result := WIPX_Write(handle, buf, len, @broadcastaddr);
end;
//=============================================================================
function WIPX_Write(handle: integer; buf: PByteArray; len: integer; addr: Pqsockaddr_t): integer;
var
socket: integer;
begin
socket := ipxsocket[handle];
// build packet with sequence number
PInteger(@packetBuffer[0])^ := sequence[handle];
inc(sequence[handle]);
memcpy(@packetBuffer[4], buf, len);
inc(len, 4);
result := sendto(socket, packetBuffer, len, 0, PSockAddr(addr)^, SizeOf(qsockaddr_t));
if result = -1 then
if WSAGetLastError = WSAEWOULDBLOCK then
result := 0;
end;
//=============================================================================
var
buf_WIPX_AddrToString: array[0..27] of char;
function WIPX_AddrToString(addr: Pqsockaddr_t): PChar;
begin
sprintf(buf_WIPX_AddrToString, '%02x%02x%02x%02x:%02x%02x%02x%02x%02x%02x:%u',
[
Ord(PSockAddrIpx(addr).sa_netnum[0]),
Ord(PSockAddrIpx(addr).sa_netnum[1]),
Ord(PSockAddrIpx(addr).sa_netnum[2]),
Ord(PSockAddrIpx(addr).sa_netnum[3]),
Ord(PSockAddrIpx(addr).sa_nodenum[0]),
Ord(PSockAddrIpx(addr).sa_nodenum[1]),
Ord(PSockAddrIpx(addr).sa_nodenum[2]),
Ord(PSockAddrIpx(addr).sa_nodenum[3]),
Ord(PSockAddrIpx(addr).sa_nodenum[4]),
Ord(PSockAddrIpx(addr).sa_nodenum[5]),
ntohs(PSockAddrIpx(addr).sa_socket)
]);
result := @buf_WIPX_AddrToString[0];
end;
//=============================================================================
function WIPX_StringToAddr(str: PChar; addr: Pqsockaddr_t): integer;
var
v: integer;
buf: array[0..2] of char;
code: integer;
p: PChar;
function DOIT(const offs: integer; p: PChar): boolean;
begin
buf[0] := str[offs];
buf[1] := str[offs + 1];
val(buf, v, code);
result := code = 0;
if result then
p^ := Chr(v);
end;
begin
buf[2] := #0;
ZeroMemory(addr, SizeOf(qsockaddr_t));
addr.sa_family := AF_IPX;
result := -1;
if not DOIT(0, @PSockAddrIpx(addr).sa_netnum[0]) then exit;
if not DOIT(2, @PSockAddrIpx(addr).sa_netnum[1]) then exit;
if not DOIT(4, @PSockAddrIpx(addr).sa_netnum[2]) then exit;
if not DOIT(6, @PSockAddrIpx(addr).sa_netnum[3]) then exit;
if not DOIT(9, @PSockAddrIpx(addr).sa_nodenum[0]) then exit;
if not DOIT(11, @PSockAddrIpx(addr).sa_nodenum[1]) then exit;
if not DOIT(13, @PSockAddrIpx(addr).sa_nodenum[2]) then exit;
if not DOIT(15, @PSockAddrIpx(addr).sa_nodenum[3]) then exit;
if not DOIT(17, @PSockAddrIpx(addr).sa_nodenum[4]) then exit;
if not DOIT(19, @PSockAddrIpx(addr).sa_nodenum[5]) then exit;
p := @str[22];
val(p, v, code);
PSockAddrIpx(addr).sa_socket := htons(u_short(v));
result := 0;
end;
//=============================================================================
function WIPX_GetSocketAddr(handle: integer; addr: Pqsockaddr_t): integer;
var
socket: integer;
addrlen: integer;
begin
socket := ipxsocket[handle];
addrlen := SizeOf(qsockaddr_t);
ZeroMemory(addr, SizeOf(qsockaddr_t));
if getsockname(socket, PSockAddr(addr)^, addrlen) <> 0 then // JVAL mayby always return 0 ???
result := -1
else
result := 0;
(*
{
int errno = pWSAGetLastError();
}
return 0;
*)
end;
//=============================================================================
function WIPX_GetNameFromAddr(addr: Pqsockaddr_t; name: PChar): integer;
begin
Q_strcpy(name, WIPX_AddrToString(addr));
result := 0;
end;
//=============================================================================
function WIPX_GetAddrFromName(name: PChar; addr: Pqsockaddr_t): integer;
var
n: integer;
buf: array[0..31] of char;
begin
n := Q_strlen(name);
if n = 12 then
begin
sprintf(buf, '00000000:%s:%u', [name, net_hostport]);
result := WIPX_StringToAddr(buf, addr);
exit;
end;
if n = 21 then
begin
sprintf(buf, '%s:%u', [name, net_hostport]);
result := WIPX_StringToAddr(buf, addr);
exit;
end;
if (n > 21) and (n <= 27) then
begin
result := WIPX_StringToAddr(name, addr);
exit;
end;
result := -1;
end;
//=============================================================================
function WIPX_AddrCompare(addr1: Pqsockaddr_t; addr2: Pqsockaddr_t): integer;
begin
if addr1.sa_family <> addr2.sa_family then
begin
result := -1;
exit;
end;
if (PSockAddrIpx(addr1).sa_netnum[0] <> #0) and (PSockAddrIpx(addr2).sa_netnum[0] <> #0) then
if Q_memcmp(@PSockAddrIpx(addr1).sa_netnum, @PSockAddrIpx(addr2).sa_netnum, 4) <> 0 then
begin
result := -1;
exit;
end;
if Q_memcmp(@PSockAddrIpx(addr1).sa_nodenum, @PSockAddrIpx(addr2).sa_nodenum, 6) <> 0 then
begin
result := -1;
exit;
end;
if PSockAddrIpx(addr1).sa_socket <> PSockAddrIpx(addr2).sa_socket then
begin
result := 1;
exit;
end;
result := 0;
end;
//=============================================================================
function WIPX_GetSocketPort(addr: Pqsockaddr_t): integer;
begin
result := ntohs(PSockAddrIpx(addr).sa_socket);
end;
function WIPX_SetSocketPort(addr: Pqsockaddr_t; port: integer): integer;
begin
PSockAddrIpx(addr).sa_socket := htons(u_short(port));
result := 0;
end;
end.