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
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
|
/*
* NET.java
* Copyright (C) 2003
*
* $Id: NET.java,v 1.1 2004-07-07 19:59:51 hzi Exp $
*/
/*
Copyright (C) 1997-2001 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.
*/
package jake2.sys;
import jake2.Defines;
import jake2.Globals;
import jake2.game.cvar_t;
import jake2.qcommon.*;
import jake2.util.Lib;
import java.net.*;
import java.util.Arrays;
public final class NET extends Defines {
public static netadr_t net_local_adr = new netadr_t();
// 127.0.0.1
public final static int LOOPBACK = 0x7f000001;
public final static int MAX_LOOPBACK = 4;
public static class loopmsg_t {
byte data[] = new byte[Defines.MAX_MSGLEN];
int datalen;
};
public static class loopback_t {
public loopback_t()
{
msgs = new loopmsg_t[MAX_LOOPBACK];
for (int n=0; n < MAX_LOOPBACK;n++)
{
msgs[n] = new loopmsg_t();
}
}
loopmsg_t msgs[];
int get, send;
};
public static loopback_t loopbacks[] = new loopback_t[2];
static {
loopbacks[0] = new loopback_t();
loopbacks[1] = new loopback_t();
}
public static DatagramSocket ip_sockets[] = {null,null};
//public static DatagramSocket ipx_sockets[] = new int[2];
// we dont need beschissene sockaddr_in structs in java !
//=============================================================================
// void NetadrToSockadr (netadr_t *a, struct sockaddr_in *s)
// {
// memset (s, 0, sizeof(*s));
//
// if (a.type == NA_BROADCAST)
// {
// s.sin_family = AF_INET;
//
// s.sin_port = a.port;
// *(int *)&s.sin_addr = -1;
// }
// else if (a.type == NA_IP)
// {
// s.sin_family = AF_INET;
//
// *(int *)&s.sin_addr = *(int *)&a.ip;
// s.sin_port = a.port;
// }
// }
//
// void SockadrToNetadr (struct sockaddr_in *s, netadr_t *a)
// {
// *(int *)&a.ip = *(int *)&s.sin_addr;
// a.port = s.sin_port;
// a.type = NA_IP;
// }
//
public static boolean CompareAdr(netadr_t a, netadr_t b) {
if (a.ip[0] == b.ip[0] && a.ip[1] == b.ip[1] && a.ip[2] == b.ip[2] && a.ip[3] == b.ip[3] && a.port == b.port)
return true;
return false;
}
/*
===================
NET_CompareBaseAdr
Compares without the port
===================
*/
public static boolean NET_CompareBaseAdr(netadr_t a, netadr_t b) {
if (a.type != b.type)
return false;
if (a.type == Defines.NA_LOOPBACK)
return true;
if (a.type == Defines.NA_IP) {
if (a.ip[0] == b.ip[0] && a.ip[1] == b.ip[1] && a.ip[2] == b.ip[2] && a.ip[3] == b.ip[3])
return true;
return false;
}
/*
if (a.type == Defines.NA_IPX) {
for (int n = 0; n < 10; n++)
if (a.ipx[n] != b.ipx[n])
return false;
//was:
//if ((memcmp(a.ipx, b.ipx, 10) == 0)) return true;
return true;
}
*/
return false;
}
public static String AdrToString(netadr_t a) {
//was:
//static char s[64];
//Com_sprintf (s, sizeof(s), "%i.%i.%i.%i:%i", a.ip[0], a.ip[1], a.ip[2], a.ip[3], ntohs(a.port));
return "" + (a.ip[0] & 0xff) + "." + (a.ip[1] & 0xff) + "." + (a.ip[2] & 0xff) + "." + (a.ip[3] & 0xff) + ":" + a.port;
}
public static String NET_BaseAdrToString(netadr_t a) {
//was:
//static char s[64];
//Com_sprintf (s, sizeof(s), "%i.%i.%i.%i", a.ip[0], a.ip[1], a.ip[2], a.ip[3]);
return "" + (a.ip[0] & 0xff) + "." + (a.ip[1] & 0xff) + "." + (a.ip[2] & 0xff) + "." + (a.ip[3] & 0xff);
}
/*
=============
NET_StringToAdr
localhost
idnewt
idnewt:28000
192.246.40.70
192.246.40.70:28000
=============
*/
// boolean NET_StringToSockaddr (char *s, struct sockaddr *sadr)
// {
// struct hostent *h;
// char *colon;
// char copy[128];
//
// memset (sadr, 0, sizeof(*sadr));
// ((struct sockaddr_in *)sadr).sin_family = AF_INET;
//
// ((struct sockaddr_in *)sadr).sin_port = 0;
//
// strcpy (copy, s);
// // strip off a trailing :port if present
// for (colon = copy ; *colon ; colon++)
// if (*colon == ':')
// {
// *colon = 0;
// ((struct sockaddr_in *)sadr).sin_port = htons((short)atoi(colon+1));
// }
//
// if (copy[0] >= '0' && copy[0] <= '9')
// {
// *(int *)&((struct sockaddr_in *)sadr).sin_addr = inet_addr(copy);
// }
// else
// {
// if (! (h = gethostbyname(copy)) )
// return 0;
// *(int *)&((struct sockaddr_in *)sadr).sin_addr = *(int *)h.h_addr_list[0];
// }
//
// return true;
// }
//
// /*
// =============
// NET_StringToAdr
//
// localhost
// idnewt
// idnewt:28000
// 192.246.40.70
// 192.246.40.70:28000
// =============
// */
// boolean NET_StringToAdr (char *s, netadr_t *a)
// {
// struct sockaddr_in sadr;
//
// if (!strcmp (s, "localhost"))
// {
// memset (a, 0, sizeof(*a));
// a.type = NA_LOOPBACK;
// return true;
// }
//
// if (!NET_StringToSockaddr (s, (struct sockaddr *)&sadr))
// return false;
//
// SockadrToNetadr (&sadr, a);
//
// return true;
// }
//
public static boolean StringToAdr( String s, netadr_t a)
{
// bugfix bzw. hack cwei
if (s.equalsIgnoreCase("localhost")) {
a.type = Defines.NA_LOOPBACK;
Arrays.fill(a.ip, (byte)0);
a.port = 0;
return true;
}
try
{
String[] address = s.split(":");
InetAddress ia = InetAddress.getByName(address[0]);
a.ip = ia.getAddress();
a.type= NA_IP;
if (address.length == 2) a.port = Integer.parseInt(address[1]);
return true;
}
catch
(
Exception e)
{
e.printStackTrace();
return false;
}
}
public static boolean IsLocalAddress (netadr_t adr)
{
return CompareAdr (adr, net_local_adr);
}
/*
=============================================================================
LOOPBACK BUFFERS FOR LOCAL PLAYER
=============================================================================
*/
// trivial! this SHOULD work !
public static boolean NET_GetLoopPacket (int sock, netadr_t net_from, sizebuf_t net_message)
{
int i;
loopback_t loop;
loop = loopbacks[sock];
if (loop.send - loop.get > MAX_LOOPBACK)
loop.get = loop.send - MAX_LOOPBACK;
if (loop.get >= loop.send)
return false;
i = loop.get & (MAX_LOOPBACK-1);
loop.get++;
//memcpy (net_message.data, loop.msgs[i].data, loop.msgs[i].datalen);
System.arraycopy(loop.msgs[i].data, 0, net_message.data, 0, loop.msgs[i].datalen);
net_message.cursize = loop.msgs[i].datalen;
net_from.ip = net_local_adr.ip;
net_from.port = net_local_adr.port;
net_from.type = net_local_adr.type;
return true;
}
// trivial! this SHOULD work !
public static void NET_SendLoopPacket (int sock, int length, byte [] data, netadr_t to)
{
int i;
loopback_t loop;
loop = loopbacks[sock^1];
// modulo 4
i = loop.send & (MAX_LOOPBACK-1);
loop.send++;
//memcpy (loop.msgs[i].data, data, length);
System.arraycopy(data,0,loop.msgs[i].data,0,length);
loop.msgs[i].datalen = length;
}
private static DatagramPacket receivedatagrampacket = new DatagramPacket(new byte[65507], 65507);
//=============================================================================
public static boolean GetPacket (int sock, netadr_t net_from, sizebuf_t net_message)
{
DatagramSocket net_socket;
// int ret;
// struct sockaddr_in from;
// int fromlen;
//
// int protocol;
// int err;
if (NET_GetLoopPacket (sock, net_from, net_message))
{
//Com.DPrintf("received packet on sock=" + sock + " len=" + net_message.cursize+"\n");
return true;
}
net_socket = ip_sockets[sock];
if (net_socket == null)
return false;
try
{
net_socket.receive(receivedatagrampacket);
// no timeout...
net_from.ip = receivedatagrampacket.getAddress().getAddress();
net_from.port = receivedatagrampacket.getPort();
net_from.type = NA_IP;
if (receivedatagrampacket.getLength() > net_message.maxsize)
{
Com.Printf ("Oversize packet from " + AdrToString(net_from) + "\n");
return false;
}
int length = receivedatagrampacket.getLength();
System.arraycopy(receivedatagrampacket.getData(), 0, net_message.data, 0, length);
// bugfix cwei
net_message.cursize = length; // set the size
net_message.data[length] = 0; // sentinel
//Com.DPrintf(Lib.hexDump(net_message.data, Math.max(length, length - (length % 16) + 16), false));
//Com.DPrintf("\n");
return true;
}
catch (SocketTimeoutException e1)
{
return false;
}
catch(Exception e)
{
Com.DPrintf ("NET_GetPacket: " + e + " from " + AdrToString(net_from) + "\n");
return false;
}
//fromlen = sizeof(from);
//ret = recvfrom (net_socket, net_message.data, net_message.maxsize
// , 0, (struct sockaddr *)&from, &fromlen);
//SockadrToNetadr (&from, net_from);
// if (ret == -1)
// {
// err = errno;
//
// if (err == EWOULDBLOCK || err == ECONNREFUSED)
// continue;
// Com_Printf ("NET_GetPacket: %s from %s\n", NET_ErrorString(),
// NET_AdrToString(*net_from));
// continue;
// }
//
// if (ret == net_message.maxsize)
// {
// Com_Printf ("Oversize packet from %s\n", NET_AdrToString (*net_from));
// continue;
// }
//
// net_message.cursize = ret;
// return true;
}
// =============================================================================
public static void SendPacket (int sock, int length, byte [] data, netadr_t to)
{
//Com.Printf("NET_SendPacket: sock=" + sock + " len=" + length + "\n");
//Com.DPrintf(Lib.hexDump(data, Math.max(length, length - (length % 16) + 16), false));
//Com.DPrintf("\n");
int ret;
//struct sockaddr_in addr;
DatagramSocket net_socket;
if ( to.type == NA_LOOPBACK )
{
NET_SendLoopPacket (sock, length, data, to);
return;
}
if (to.type == NA_BROADCAST)
{
net_socket = ip_sockets[sock];
if (net_socket==null)
return;
}
else if (to.type == NA_IP)
{
net_socket = ip_sockets[sock];
if (net_socket==null)
return;
}
/*
else if (to.type == NA_IPX)
{
net_socket = ipx_sockets[sock];
if (net_socket==null)
return;
}
else if (to.type == NA_BROADCAST_IPX)
{
net_socket = ipx_sockets[sock];
if (!net_socket)
return;
}
*/
else
{
Com.Error (ERR_FATAL, "NET_SendPacket: bad address type");
return;
}
//was:
//NetadrToSockadr (&to, &addr);
try
{ //was:
//ret = sendto (net_socket, data, length, 0, (struct sockaddr *)&addr, sizeof(addr) );
DatagramPacket dp;
if (to.type == NA_BROADCAST)
{
dp = new DatagramPacket(data, length, InetAddress.getByAddress(new byte[]{-1,-1,-1,-1}), to.port);
}
else
dp = new DatagramPacket(data, length, to.getInetAddress(), to.port);
net_socket.send(dp);
}
catch(Exception e)
{
Com.Printf ("NET_SendPacket ERROR: " + e + " to " + AdrToString (to) + "\n");
}
}
//=============================================================================
/*
====================
NET_OpenIP
====================
*/
public static void NET_OpenIP ()
{
cvar_t port, ip;
port = Cvar.Get ("port", "" + Defines.PORT_SERVER, CVAR_NOSET);
ip = Cvar.Get ("ip", "localhost", CVAR_NOSET);
if (ip_sockets[NS_SERVER]==null)
ip_sockets[NS_SERVER] = NET_Socket (ip.string, (int)port.value);
if (ip_sockets[NS_CLIENT]==null)
ip_sockets[NS_CLIENT] = NET_Socket (ip.string, Defines.PORT_ANY);
}
/*
====================
NET_OpenIPX
====================
*/
public static void NET_OpenIPX ()
{
}
/*
====================
NET_Config
A single player game will only use the loopback code
====================
*/
public static void Config (boolean multiplayer)
{
int i;
if (!multiplayer)
{ // shut down any existing sockets
for (i=0 ; i<2 ; i++)
{
if (ip_sockets[i]!=null)
{
ip_sockets[i].close();
ip_sockets[i] = null;
}
/*
if (ipx_sockets[i])
{
ipx_sockets[i].close();
ipx_sockets[i] = null;
}
*/
}
}
else
{ // open sockets
NET_OpenIP ();
}
}
//===================================================================
/*
====================
NET_Init
====================
*/
public static void NET_Init ()
{
//empty
}
/*
====================
NET_Socket
====================
*/
public static DatagramSocket NET_Socket (String ip, int port)
{
DatagramSocket newsocket = null;
try
{
if (ip==null || ip.length()==0 || ip.equals("localhost"))
{
if (port == PORT_ANY)
{
newsocket = new DatagramSocket();
}
else
{
newsocket = new DatagramSocket(port);
}
}
else
{
InetAddress ia = InetAddress.getByName(ip);
newsocket = new DatagramSocket(port, ia);
}
newsocket.setBroadcast(true);
// nonblocking (1 ms), 0== neverending infinite timeout
newsocket.setSoTimeout(1);
}
catch (Exception e)
{
newsocket = null;
}
return newsocket;
// int newsocket;
// //struct sockaddr_in address;
// qboolean _true = true;
// int i = 1;
//
// if ((newsocket = socket (PF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1)
// {
// Com.Printf ("ERROR: UDP_OpenSocket: socket: ", NET_ErrorString());
// return 0;
// }
//
// // make it non-blocking
// if (ioctl (newsocket, FIONBIO, &_true) == -1)
// {
// Com_Printf ("ERROR: UDP_OpenSocket: ioctl FIONBIO:%s\n", NET_ErrorString());
// return 0;
// }
//
// // make it broadcast capable
// if (setsockopt(newsocket, SOL_SOCKET, SO_BROADCAST, (char *)&i, sizeof(i)) == -1)
// {
// Com_Printf ("ERROR: UDP_OpenSocket: setsockopt SO_BROADCAST:%s\n", NET_ErrorString());
// return 0;
// }
//
// if (!net_interface || !net_interface[0] || !stricmp(net_interface, "localhost"))
// address.sin_addr.s_addr = INADDR_ANY;
// else
// NET_StringToSockaddr (net_interface, (struct sockaddr *)&address);
//
// if (port == PORT_ANY)
// address.sin_port = 0;
// else
// address.sin_port = htons((short)port);
//
// address.sin_family = AF_INET;
//
// if( bind (newsocket, (void *)&address, sizeof(address)) == -1)
// {
// Com_Printf ("ERROR: UDP_OpenSocket: bind: %s\n", NET_ErrorString());
// close (newsocket);
// return 0;
// }
//
// return newsocket;
}
/*
====================
NET_Shutdown
====================
*/
public static void NET_Shutdown ()
{
Config (false); // close sockets
}
/*
====================
NET_ErrorString
====================
*/
public static String NET_ErrorString() {
int code;
//code = errno;
//return strerror (code);
return "errno can not yet resolved in java";
}
// sleeps msec or until net socket is ready
public static void NET_Sleep(int msec)
{
if (ip_sockets[NS_SERVER]==null || (Globals.dedicated!=null && Globals.dedicated.value == 0))
return; // we're not a server, just run full speed
try {
//TODO: check for
Thread.sleep(msec);
}
catch (InterruptedException e) {
}
//ip_sockets[NS_SERVER].
// this should wait up to 100ms until a packet
/*
struct timeval timeout;
fd_set fdset;
extern cvar_t *dedicated;
extern qboolean stdin_active;
if (!ip_sockets[NS_SERVER] || (dedicated && !dedicated.value))
return; // we're not a server, just run full speed
FD_ZERO(&fdset);
if (stdin_active)
FD_SET(0, &fdset); // stdin is processed too
FD_SET(ip_sockets[NS_SERVER], &fdset); // network socket
timeout.tv_sec = msec/1000;
timeout.tv_usec = (msec%1000)*1000;
select(ip_sockets[NS_SERVER]+1, &fdset, NULL, NULL, &timeout);
*/
}
}
|