-
Notifications
You must be signed in to change notification settings - Fork 2
/
socket.c
4488 lines (4057 loc) · 117 KB
/
socket.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
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
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Socket.c
*
* Kevin P. Smith 1/29/89 UDP stuff v1.0 by Andy McFadden Feb-Apr 1992
*
* UDP protocol v1.0
*
* Routines to allow connection to the xtrek server.
*/
#include "copyright2.h"
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <math.h>
#include <errno.h>
#include "netrek.h"
#ifdef SHORT_PACKETS
#include "sp.h"
#endif
#ifdef nodef
#define INCLUDE_SCAN /* include Amdahl scanning beams */
#define INCLUDE_VISTRACT /* include visible tractor beams */
#endif
#ifdef GATEWAY
/*
* (these values are now defined in "main.c":) char *gw_mach =
* "charon"; |client gateway; strcmp(serverName) int gw_serv_port =
* 5000; |what to tell the server to use int gw_port = 5001;
* |where we will contact gw int gw_local_port = 5100; |where we
* expect gw to contact us
*
* The client binds to "5100" and sends "5000" to the server (TCP). The server
* sees that and sends a UDP packet to gw on port udp5000, which passes it
* through to port udp5100 on the client. The client-gw gets the server's
* host and port from recvfrom. (The client can't use the same method since
* these sockets are one-way only, so it connect()s to gw_port (udp5001) on
* the gateway machine regardless of what the server sends.)
*
* So all we need in .gwrc is: udp 5000 5001 tde.uts 5100
*
* assuming the client is on tde.uts. Note that a UDP declaration will work for
* ANY server, but you need one per player, and the client has to have the
* port numbers in advance.
*
* If we're using a standard server, we're set. If we're running through a
* gatewayed server, we have to do some unpleasant work on the server side...
*/
#endif
#ifdef NEED_EXIT
#ifdef sgi
void exit(int status);
#endif /* sgi */
#endif /* NEED_EXIT */
#ifdef RECORD
#include "recorder.h"
#endif RECORD
#ifdef SHORT_PACKETS
static char numofbits[256] =
{0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4, 1,
2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, 1,
2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, 2,
3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 1,
2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, 2,
3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 2,
3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 3,
4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, 1,
2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, 2,
3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 2,
3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 3,
4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, 2,
3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 3,
4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, 3,
4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, 4,
5, 5, 6, 5, 6, 6, 7, 5, 6, 6, 7, 6, 7, 7, 8};
static int vtsize[9] =
{4, 8, 8, 12, 12, 16, 20, 20, 24}; /* How big is the torppacket */
#ifdef unused
static int vtdata[9] =
{0, 3, 5, 7, 9, 12, 14, 16, 18};/* How big is Torpdata */
#endif
static int vtisize[9] =
{4, 7, 9, 11, 13, 16, 18, 20, 22}; /* 4 byte Header + torpdata */
int spwinside = 500;/* WINSIDE from Server */
#define SPWINSIDE 500 /* To make it safe */
long spgwidth = GWIDTH; /* GWITHT from Server */
static
int my_x, my_y; /* for rotation we need to keep track of our
* real coordinates */
#endif /* SHORT_PACKETS */
#ifdef PACKET_LOG
int ALL_BYTES = 0; /* To log all bytes */
#endif
#if __STDC__ || defined(__cplusplus)
#define P_(s) s
#else
#define P_(s) ()
#endif
/* socket.c */
static void handleTorp P_((struct torp_spacket * packet));
static void handleVTorp P_((unsigned char *sbuf));
static void handleTorpInfo P_((struct torp_info_spacket * packet));
static void handleStatus P_((struct status_spacket * packet));
static void handleSelf P_((struct you_spacket * packet));
static void handleSelfShort P_((struct youshort_spacket * packet));
static void handleSelfShip P_((struct youss_spacket * packet));
static void handlePlayer P_((struct player_spacket * packet));
static void handleVPlayer P_((unsigned char *sbuf));
static void handleWarning P_((struct warning_spacket * packet));
static void handlePlanet P_((struct planet_spacket * packet));
static void handlePhaser P_((struct phaser_spacket * packet));
static void handleSMessage P_((struct mesg_s_spacket * packet));
static void handleQueue P_((struct queue_spacket * packet));
static void handlePickok P_((struct pickok_spacket * packet));
static void handleLogin P_((struct login_spacket * packet));
static void handlePlasmaInfo P_((struct plasma_info_spacket * packet));
static void handlePlasma P_((struct plasma_spacket * packet));
static void handleFlags P_((struct flags_spacket * packet));
static void handleKills P_((struct kills_spacket * packet));
static void handlePStatus P_((struct pstatus_spacket * packet));
static void handleMotd P_((struct motd_spacket * packet));
static void handleMotdPic P_((struct motd_pic_spacket * packet));
static void handleBitmap P_((struct bitmap_spacket * packet));
static void handleMask P_((struct mask_spacket * packet));
static void handleBadVersion P_((struct badversion_spacket * packet));
static void handleHostile P_((struct hostile_spacket * packet));
static void handlePlyrLogin P_((struct plyr_login_spacket * packet));
static void handleStats P_((struct stats_spacket * packet));
static void handlePlyrInfo P_((struct plyr_info_spacket * packet));
static void handlePlanetLoc P_((struct planet_loc_spacket * packet));
static void handleReserved P_((struct reserved_spacket * packet));
static void handleFeature P_((struct feature_spacket * packet));
static void handleUdpReply P_((struct udp_reply_spacket * packet));
static void handleSequence P_((struct sequence_spacket * packet));
static void handleShortReply P_((struct shortreply_spacket * packet));
static void handleVTorpInfo P_((unsigned char *sbuf));
static void handleVPlanet P_((unsigned char *sbuf));
/* S_P2 */
static void handleVKills P_((unsigned char *sbuf));
static void handleVPhaser P_((unsigned char *sbuf));
static void handle_s_Stats P_((struct stats_s_spacket * packet));
static void handleShipCap P_((struct ship_cap_spacket *packet));
static void new_flags P_((unsigned int data, int which));
static void processBitmap P_((struct bitmap_spacket * packet, int size, char *bits));
static int sock_read P_((int sock, char *buff, int size));
#undef P_
struct packet_handler handlers[] =
{
{0, NULL}, /* record 0 */
{sizeof(struct mesg_spacket), handleMessage}, /* SP_MESSAGE */
{sizeof(struct plyr_info_spacket), handlePlyrInfo}, /* SP_PLAYER_INFO */
{sizeof(struct kills_spacket), handleKills}, /* SP_KILLS */
{sizeof(struct player_spacket), handlePlayer}, /* SP_PLAYER */
{sizeof(struct torp_info_spacket), handleTorpInfo}, /* SP_TORP_INFO */
{sizeof(struct torp_spacket), handleTorp}, /* SP_TORP */
{sizeof(struct phaser_spacket), handlePhaser}, /* SP_PHASER */
{sizeof(struct plasma_info_spacket), handlePlasmaInfo}, /* SP_PLASMA_INFO */
{sizeof(struct plasma_spacket), handlePlasma}, /* SP_PLASMA */
{sizeof(struct warning_spacket), handleWarning}, /* SP_WARNING */
{sizeof(struct motd_spacket), handleMotd}, /* SP_MOTD */
{sizeof(struct you_spacket), handleSelf}, /* SP_YOU */
{sizeof(struct queue_spacket), handleQueue}, /* SP_QUEUE */
{sizeof(struct status_spacket), handleStatus}, /* SP_STATUS */
{sizeof(struct planet_spacket), handlePlanet}, /* SP_PLANET */
{sizeof(struct pickok_spacket), handlePickok}, /* SP_PICKOK */
{sizeof(struct login_spacket), handleLogin}, /* SP_LOGIN */
{sizeof(struct flags_spacket), handleFlags}, /* SP_FLAGS */
{sizeof(struct mask_spacket), handleMask}, /* SP_MASK */
{sizeof(struct pstatus_spacket), handlePStatus}, /* SP_PSTATUS */
{sizeof(struct badversion_spacket), handleBadVersion}, /* SP_BADVERSION */
{sizeof(struct hostile_spacket), handleHostile}, /* SP_HOSTILE */
{sizeof(struct stats_spacket), handleStats}, /* SP_STATS */
{sizeof(struct plyr_login_spacket), handlePlyrLogin}, /* SP_PL_LOGIN */
{sizeof(struct reserved_spacket), handleReserved}, /* SP_RESERVED */
{sizeof(struct planet_loc_spacket), handlePlanetLoc}, /* SP_PLANET_LOC */
#ifdef HANDLE_SCAN
{sizeof(struct scan_spacket), handleScan}, /* SP_SCAN (ATM) */
#else
{0, exit}, /* won't be called */
#endif
{sizeof(struct udp_reply_spacket), handleUdpReply}, /* SP_UDP_STAT */
{sizeof(struct sequence_spacket), handleSequence}, /* SP_SEQUENCE */
{sizeof(struct sc_sequence_spacket), handleSequence}, /* SP_SC_SEQUENCE */
{0, exit}, /* #31, and exit won't really be called */
{sizeof(struct motd_pic_spacket), handleMotdPic},
{0, exit}, /* 33 */
{0, exit}, /* 34 */
{0, exit}, /* 35 */
{0, exit}, /* 36 */
{0, exit}, /* 37 */
{0, exit}, /* 38 */
{sizeof(struct ship_cap_spacket), handleShipCap}, /* SP_SHIP_CAP */
#ifdef SHORT_PACKETS
{sizeof(struct shortreply_spacket), handleShortReply}, /* SP_S_REPLY */
{-1, handleSMessage}, /* SP_S_MESSAGE */
{-1 /* sizeof(struct warning_s_spacket) */ , handleSWarning}, /* SP_S_WARNING */
{sizeof(struct youshort_spacket), handleSelfShort}, /* SP_S_YOU */
{sizeof(struct youss_spacket), handleSelfShip}, /* SP_S_YOU_SS */
{-1, /* variable */ handleVPlayer}, /* SP_S_PLAYER */
#else
{0, exit}, /* 40 */
{0, exit}, /* 41 */
{0, exit}, /* 42 */
{0, exit}, /* 43 */
{0, exit}, /* 44 */
{0, exit}, /* 45 */
#endif
#ifdef PING
{sizeof(struct ping_spacket), handlePing}, /* SP_PING */
#else
{0, exit},
#endif
#ifdef SHORT_PACKETS
{-1, /* variable */ handleVTorp}, /* SP_S_TORP */
{-1, handleVTorpInfo}, /* SP_S_TORP_INFO */
{20, handleVTorp}, /* SP_S_8_TORP */
{-1, handleVPlanet}, /* SP_S_PLANET */
#endif
{0, exit}, /* 51 */
{0, exit}, /* 52 */
{0, exit}, /* 53 */
{0, exit}, /* 54 */
{0, exit}, /* 55 */
#ifdef SHORT_PACKETS /* S_P2 */
{0, exit}, /* SP_S_SEQUENCE not yet implemented */
{-1, handleVPhaser}, /* SP_S_PHASER */
{-1, handleVKills}, /* SP_S_KILLS */
{sizeof(struct stats_s_spacket), handle_s_Stats}, /* SP_S_STATS */
#else
{0, exit}, /* 56 */
{0, exit}, /* 57 */
{0, exit}, /* 58 */
{0, exit}, /* 59 */
#endif
#ifdef FEATURE
{sizeof(struct feature_spacket), handleFeature}, /* SP_FEATURE */
#endif
{sizeof(struct bitmap_spacket), handleBitmap}, /* SP_BITMAP */
};
int sizes[] =
{
0, /* record 0 */
sizeof(struct mesg_cpacket), /* CP_MESSAGE */
sizeof(struct speed_cpacket),/* CP_SPEED */
sizeof(struct dir_cpacket), /* CP_DIRECTION */
sizeof(struct phaser_cpacket), /* CP_PHASER */
sizeof(struct plasma_cpacket), /* CP_PLASMA */
sizeof(struct torp_cpacket), /* CP_TORP */
sizeof(struct quit_cpacket), /* CP_QUIT */
sizeof(struct login_cpacket),/* CP_LOGIN */
sizeof(struct outfit_cpacket), /* CP_OUTFIT */
sizeof(struct war_cpacket), /* CP_WAR */
sizeof(struct practr_cpacket), /* CP_PRACTR */
sizeof(struct shield_cpacket), /* CP_SHIELD */
sizeof(struct repair_cpacket), /* CP_REPAIR */
sizeof(struct orbit_cpacket),/* CP_ORBIT */
sizeof(struct planlock_cpacket), /* CP_PLANLOCK */
sizeof(struct playlock_cpacket), /* CP_PLAYLOCK */
sizeof(struct bomb_cpacket), /* CP_BOMB */
sizeof(struct beam_cpacket), /* CP_BEAM */
sizeof(struct cloak_cpacket),/* CP_CLOAK */
sizeof(struct det_torps_cpacket), /* CP_DET_TORPS */
sizeof(struct det_mytorp_cpacket), /* CP_DET_MYTORP */
sizeof(struct copilot_cpacket), /* CP_COPILOT */
sizeof(struct refit_cpacket),/* CP_REFIT */
sizeof(struct tractor_cpacket), /* CP_TRACTOR */
sizeof(struct repress_cpacket), /* CP_REPRESS */
sizeof(struct coup_cpacket), /* CP_COUP */
sizeof(struct socket_cpacket), /* CP_SOCKET */
sizeof(struct options_cpacket), /* CP_OPTIONS */
sizeof(struct bye_cpacket), /* CP_BYE */
sizeof(struct dockperm_cpacket), /* CP_DOCKPERM */
sizeof(struct updates_cpacket), /* CP_UPDATES */
sizeof(struct resetstats_cpacket), /* CP_RESETSTATS */
sizeof(struct reserved_cpacket), /* CP_RESERVED */
#ifdef INCLUDE_SCAN
sizeof(struct scan_cpacket), /* CP_SCAN (ATM) */
#else
0,
#endif
sizeof(struct udp_req_cpacket), /* CP_UDP_REQ */
sizeof(struct sequence_cpacket), /* CP_SEQUENCE */
0, /* 37 */
0, /* 38 */
0, /* 39 */
0, /* 40 */
0, /* 41 */
#ifdef PING
sizeof(struct ping_cpacket), /* CP_PING_RESPONSE */
#else
0,
#endif
#ifdef SHORT_PACKETS
sizeof(struct shortreq_cpacket), /* CP_S_REQ */
sizeof(struct threshold_cpacket), /* CP_S_THRS */
-1, /* CP_S_MESSAGE */
#endif
0, /* 46 */
0, /* 47 */
0, /* 48 */
0, /* 49 */
0, /* 50 */
0, /* 51 */
0, /* 52 */
0, /* 53 */
0, /* 54 */
0, /* 55 */
0, /* 56 */
0, /* 57 */
0, /* 58 */
0, /* 59 */
#ifdef FEATURE
sizeof(struct feature_cpacket),
#endif
};
#define NUM_PACKETS (sizeof(handlers) / sizeof(handlers[0]) - 1)
#define NUM_SIZES (sizeof(sizes) / sizeof(sizes[0]) - 1)
#ifdef PACKET_LOG
/*
* stuff useful for logging server packets to see how much bandwidth * netrek
* is really using
*/
int log_packets = 0;/* whether or not to be logging packets */
int packet_log[NUM_PACKETS]; /* number of packets logged */
int outpacket_log[NUM_SIZES];
#endif /* PACKET_LOG */
int serverDead = 0;
#define BUFSIZE 2048
char buf[BUFSIZE];
static int udpLocalPort = 0;
static int udpServerPort = 0;
static long serveraddr = 0;
static long sequence = 0;
static int drop_flag = 0;
static int chan = -1; /* tells sequence checker where packet is
* from */
static short fSpeed, fDirection, fShield, fOrbit, fRepair, fBeamup, fBeamdown,
fCloak, fBomb, fDockperm, fPhaser, fPlasma, fPlayLock,
fPlanLock, fTractor, fRepress;
/* reset all the "force command" variables */
void
resetForce()
{
fSpeed = fDirection = fShield = fOrbit = fRepair = fBeamup = fBeamdown =
fCloak = fBomb = fDockperm = fPhaser = fPlasma = fPlayLock = fPlanLock =
fTractor = fRepress = -1;
}
/*
* If something we want to happen hasn't yet, send it again.
*
* The low byte is the request, the high byte is a max count. When the max
* count reaches zero, the client stops trying. Checking is done with a
* macro for speed & clarity.
*/
#define FCHECK_FLAGS(flag, force, const) { \
if (force > 0) { \
if (((me->p_flags & flag) && 1) ^ ((force & 0xff) && 1)) { \
speedReq.type = const; \
speedReq.speed = (force & 0xff); \
sendServerPacket(&speedReq); \
V_UDPDIAG(("Forced %d:%d\n", const, force & 0xff)); \
force -= 0x100; \
if (force < 0x100) force = -1; /* give up */ \
} else \
force = -1; \
} \
}
#define FCHECK_VAL(value, force, const) { \
if (force > 0) { \
if ((value) != (force & 0xff)) { \
speedReq.type = const; \
speedReq.speed = (force & 0xff); \
sendServerPacket(&speedReq); \
V_UDPDIAG(("Forced %d:%d\n", const, force & 0xff)); \
force -= 0x100; \
if (force < 0x100) force = -1; /* give up */ \
} else \
force = -1; \
} \
}
#define FCHECK_TRACT(flag, force, const) { \
if (force > 0) { \
if (((me->p_flags & flag) && 1) ^ ((force & 0xff) && 1)) { \
tractorReq.type = const; \
tractorReq.state = ((force & 0xff) >= 0x40); \
tractorReq.pnum = (force & 0xff) & (~0x40); \
sendServerPacket(&tractorReq); \
V_UDPDIAG(("Forced %d:%d/%d\n", const, \
tractorReq.state, tractorReq.pnum)); \
force -= 0x100; \
if (force < 0x100) force = -1; /* give up */ \
} else \
force = -1; \
} \
}
void
checkForce()
{
struct speed_cpacket speedReq;
struct tractor_cpacket tractorReq;
FCHECK_VAL(me->p_speed, fSpeed, CP_SPEED); /* almost always repeats */
#ifdef nodef
FCHECK_VAL(me->p_dir, fDirection, CP_DIRECTION); /* (ditto) */
#endif
FCHECK_FLAGS(PFSHIELD, fShield, CP_SHIELD);
FCHECK_FLAGS(PFORBIT, fOrbit, CP_ORBIT);
FCHECK_FLAGS(PFREPAIR, fRepair, CP_REPAIR);
FCHECK_FLAGS(PFBEAMUP, fBeamup, CP_BEAM);
if (!(me->p_flags & PFBEAMUP))
FCHECK_FLAGS(PFBEAMDOWN, fBeamdown, CP_BEAM);
FCHECK_FLAGS(PFCLOAK, fCloak, CP_CLOAK);
if (!(me->p_flags & PFBEAMDOWN))
FCHECK_FLAGS(PFBOMB, fBomb, CP_BOMB);
FCHECK_FLAGS(PFDOCKOK, fDockperm, CP_DOCKPERM);
FCHECK_VAL(phasers[me->p_no].ph_status, fPhaser, CP_PHASER); /* bug: dir 0 */
FCHECK_VAL(plasmatorps[me->p_no].pt_status, fPlasma, CP_PLASMA); /* (ditto) */
FCHECK_FLAGS(PFPLOCK, fPlayLock, CP_PLAYLOCK);
FCHECK_FLAGS(PFPLLOCK, fPlanLock, CP_PLANLOCK);
FCHECK_TRACT(PFPRESS, fRepress, CP_REPRESS);
FCHECK_TRACT(PFTRACT, fTractor, CP_TRACTOR);
}
void
connectToServer(port)
int port;
{
int s;
struct sockaddr_in addr;
struct sockaddr_in naddr;
int len;
fd_set readfds;
struct timeval timeout;
struct hostent *hp;
int optval;
serverDead = 0;
if (sock != -1) {
shutdown(sock, 2);
close(sock);
sock = -1;
}
#ifdef nodef
sleep(3); /* I think this is necessary for some unknown
* reason */
#endif
printf("Waiting for connection (port %d). \n", port);
if ((s = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
printf("I can't create a socket\n");
exit(2);
}
/* allow local address resuse */
optval = 1;
if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (char *) &optval, sizeof(int))
< 0) {
perror("setsockopt");
}
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = INADDR_ANY;
addr.sin_port = htons(port);
if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
#ifdef nodef
sleep(10);
if (bind(s, &addr, sizeof(addr)) < 0) {
sleep(10);
if (bind(s, &addr, sizeof(addr)) < 0) {
printf("I can't bind to port!\n");
exit(3);
}
}
#endif
perror("bind"); /* NEW */
exit(1);
}
if (listen(s, 1) < 0)
perror("listen");
len = sizeof(naddr);
tryagain:
timeout.tv_sec = 240; /* four minutes */
timeout.tv_usec = 0;
FD_ZERO(&readfds);
FD_SET(s, &readfds);
if (s >= max_fd)
max_fd = s + 1;
if (select(max_fd, &readfds, NULL, NULL, &timeout) == 0) {
printf("Well, I think the server died!\n");
exit(0);
}
sock = accept(s, (struct sockaddr *)&naddr, &len);
if (sock == -1) {
goto tryagain;
}
if (sock >= max_fd)
max_fd = sock + 1;
printf("Got connection.\n");
close(s);
pickSocket(port); /* new socket != port */
/*
* This is necessary; it tries to determine who the caller is, and set
* "serverName" and "serveraddr" appropriately.
*/
len = sizeof(struct sockaddr_in);
if (getpeername(sock, (struct sockaddr *) & addr, &len) < 0) {
perror("unable to get peername");
serverName = "nowhere";
} else {
serveraddr = addr.sin_addr.s_addr;
hp = gethostbyaddr((char *) &addr.sin_addr.s_addr, sizeof(long), AF_INET);
if (hp != NULL) {
serverName = (char *) malloc(strlen(hp->h_name) + 1);
strcpy(serverName, hp->h_name);
} else {
serverName = (char *) malloc(strlen(inet_ntoa(addr.sin_addr)) + 1);
strcpy(serverName, inet_ntoa(addr.sin_addr));
}
}
printf("Connection from server %s (0x%x)\n", serverName,
(unsigned int) serveraddr);
}
#ifdef nodef
void
set_tcp_opts(s)
int s;
{
int optval = 1;
struct protoent *ent;
ent = getprotobyname("TCP");
if (!ent) {
fprintf(stderr, "TCP protocol not found.\n");
return;
}
if (setsockopt(s, ent->p_proto, TCP_NODELAY, &optval, sizeof(int)) < 0)
perror("setsockopt");
}
void
set_udp_opts(s)
int s;
{
int optval = BUFSIZ;
struct protoent *ent;
ent = getprotobyname("UDP");
if (!ent) {
fprintf(stderr, "UDP protocol not found.\n");
return;
}
if (setsockopt(s, ent->p_proto, SO_RCVBUF, &optval, sizeof(int)) < 0)
perror("setsockopt");
}
#endif
void
callServer(port, server)
int port;
char *server;
{
int s;
struct sockaddr_in addr;
struct hostent *hp;
serverDead = 0;
printf("Calling %s on port %d.\n", server, port);
if ((s = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
printf("I can't create a socket\n");
exit(0);
}
addr.sin_family = AF_INET;
addr.sin_port = htons(port);
if ((addr.sin_addr.s_addr = inet_addr(server)) == -1) {
if ((hp = gethostbyname(server)) == NULL) {
printf("Who is %s?\n", server);
exit(0);
} else {
addr.sin_addr.s_addr = *(long *) hp->h_addr;
}
}
serveraddr = addr.sin_addr.s_addr;
if (connect(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
fprintf(stderr, "%s not listening!\n", server);
exit(0);
}
printf("Got connection.\n");
sock = s;
if (sock >= max_fd)
max_fd = sock + 1;
#ifdef TREKHOPD
/*
* We use a different scheme from gw: we tell the server who we want to
* connect to and what our local UDP port is; it picks its own UDP ports
* and tells us what they are. This is MUCH more flexible and avoids a
* number of problems, and may come in handy if the blessed scheme
* changes. It's also slightly more work.
*/
{
extern int port_req, use_trekhopd, serv_port;
extern char *host_req;
struct mesg_cpacket msg;
struct mesg_spacket reply;
int n, count, *ip;
char *buf;
if (use_trekhopd) {
msg.type = SP_MESSAGE;
msg.group = msg.indiv = msg.pad1 = 0;
ip = (int *) msg.mesg;
*(ip++) = htons(port_req);
*(ip++) = htons(gw_local_port);
strncpy(msg.mesg + 8, login, 8);
strcpy(msg.mesg + 16, host_req);
if (gwrite(s, (char *) &msg, sizeof(struct mesg_cpacket)) < 0) {
fprintf(stderr, "trekhopd init failure\n");
exit(1);
}
printf("--- trekhopd request sent, awaiting reply\n");
/* now block waiting for reply */
count = sizeof(struct mesg_spacket);
for (buf = (char *) &reply; count; buf += n, count -= n) {
if ((n = sock_read(s, buf, count)) <= 0) {
perror("trekhopd read");
exit(1);
}
}
if (reply.type != SP_MESSAGE) {
fprintf(stderr, "Got bogus reply from trekhopd (%d)\n",
reply.type);
exit(1);
}
ip = (int *) reply.mesg;
gw_serv_port = ntohl(*ip++);
gw_port = ntohl(*ip++);
serv_port = ntohl(*ip++);
printf("--- trekhopd reply received\n");
printf("ports = %d/%d, %d\n", gw_serv_port, gw_port, serv_port);
}
}
#endif /* TREKHOPD */
#ifdef RECORD
if(recordGame)
startRecorder();
#endif
pickSocket(port); /* new socket != port */
}
int
isServerDead()
{
return (serverDead);
}
void
socketPause()
{
struct timeval timeout;
fd_set readfds;
#ifdef RECORD
if(playback)
return;
#endif
timeout.tv_sec = 1;
timeout.tv_usec = 0;
FD_ZERO(&readfds);
FD_SET(sock, &readfds);
if (udpSock >= 0) /* new */
FD_SET(udpSock, &readfds);
select(max_fd, &readfds, 0, 0, &timeout);
}
int
readFromServer(readfds)
fd_set *readfds;
{
int retval = 0;
#ifdef RECORD
if(playback)
{
if(!me) {
while(!me) /* read up to the handleSelf[short] to satisfy */
doRead(sock); /* findslot() */
}
else if(loginAccept < 1) {
while(loginAccept < 1)
doRead(sock);
/* there always seem to be 2 handleLogin packets, grab another */
/* doRead(sock); loginAccept is >=1 on the 2nd one, no need */
}
else if(pickOk < 1) { /* read up to the handlePickok to simulate */
while(pickOk < 1) /* entrywin () */
doRead(sock);
}
else {
while(!playback_update)
doRead(sock);
}
return 1;
}
#endif
if (serverDead)
return (0);
if (!readfds) {
struct timeval timeout;
fd_set mask;
readfds = &mask;
/*
* tryagain:
*/
FD_ZERO(readfds);
FD_SET(sock, readfds);
if (udpSock >= 0)
FD_SET(udpSock, readfds);
timeout.tv_sec = 0;
timeout.tv_usec = 0;
if ((select(max_fd, readfds, 0, 0, &timeout)) == 0) {
dotimers();
return 0;
}
}
if (udpSock >= 0 && FD_ISSET(udpSock, readfds)) {
/* WAS V_ */
UDPDIAG(("Activity on UDP socket\n"));
chan = udpSock;
if (commStatus == STAT_VERIFY_UDP) {
warning("UDP connection established");
sequence = 0; /* reset sequence #s */
resetForce();
if (udpDebug)
printUdpInfo();
UDPDIAG(("UDP connection established.\n"));
commMode = COMM_UDP;
commStatus = STAT_CONNECTED;
commSwitchTimeout = 0;
if (udpClientRecv != MODE_SIMPLE)
sendUdpReq(COMM_MODE + udpClientRecv);
if (udpWin) {
udprefresh(UDP_CURRENT);
udprefresh(UDP_STATUS);
}
}
retval += doRead(udpSock);
}
/* Read info from the xtrek server */
if (FD_ISSET(sock, readfds)) {
chan = sock;
if (commMode == COMM_TCP)
drop_flag = 0; /* just in case */
retval += doRead(sock);
}
dotimers();
return (retval != 0); /* convert to 1/0 */
}
void
dotimers()
{
/* if switching comm mode, decrement timeout counter */
if (commSwitchTimeout > 0) {
if (!(--commSwitchTimeout)) {
/*
* timed out; could be initial request to non-UDP server (which
* won't be answered), or the verify packet got lost en route to the
* server. Could also be a request for TCP which timed out (weird),
* in which case we just reset anyway.
*/
commModeReq = commMode = COMM_TCP;
commStatus = STAT_CONNECTED;
if (udpSock >= 0)
closeUdpConn();
if (udpWin) {
udprefresh(UDP_CURRENT);
udprefresh(UDP_STATUS);
}
warning("Timed out waiting for UDP response from server");
UDPDIAG(("Timed out waiting for UDP response from server\n"));
}
}
/* if we're in a UDP "force" mode, check to see if we need to do something */
if (udpClientSend > 1 && commMode == COMM_UDP)
checkForce();
}
int
doRead(asock)
int asock;
{
struct timeval timeout;
fd_set readfds;
char *bufptr;
int size;
int count;
int temp;
timeout.tv_sec = 0;
timeout.tv_usec = 0;
count = sock_read(asock, buf, BUFSIZE - /* space for packet frag */ BUFSIZE / 4);
#ifdef NETSTAT
if (netstat &&
(asock == udpSock ||
commMode != COMM_UDP ||
udpClientRecv == MODE_TCP)) {
ns_record_update(count);
}
#endif
if (debug)
printf("R- %8d: %d b %s\n",
mstime(), count, asock == udpSock ? "UDP" : "TCP");
if (count <= 0) {
if (asock == udpSock) {
if (errno == ECONNREFUSED) {
struct sockaddr_in addr;
UDPDIAG(("asock=%d, sock=%d, udpSock=%d, errno=%d\n",
asock, sock, udpSock, errno));
UDPDIAG(("count=%d\n", count));
UDPDIAG(("Hiccup(%d)! Reconnecting\n", errno));
addr.sin_addr.s_addr = serveraddr;
addr.sin_port = htons(udpServerPort);
addr.sin_family = AF_INET;
if (connect(udpSock, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
perror("connect");
UDPDIAG(("Unable to reconnect\n"));
/* and fall through to disconnect */
} else {
UDPDIAG(("Reconnect successful\n"));
return (0);
}
}
UDPDIAG(("*** UDP disconnected (res=%d, err=%d)\n",
count, errno));
warning("UDP link severed");
printUdpInfo();
closeUdpConn();
commMode = commModeReq = COMM_TCP;
commStatus = STAT_CONNECTED;
if (udpWin) {
udprefresh(UDP_STATUS);
udprefresh(UDP_CURRENT);
}
return (0);
}
printf("1) Got read() of %d. Server dead\n", count);
perror("1) read()");
serverDead = 1;
return (0);
}
bufptr = buf;
while (bufptr < buf + count) {
#ifdef RECORD
if(playback && *bufptr == RECORD_UPDATE)
size = 4;
else
#endif
{
if (*bufptr < 1 || *bufptr > NUM_PACKETS || handlers[(int) *bufptr].size == 0) {
fprintf(stderr, "Unknown packet type: %d\n", *bufptr);
#ifndef CORRUPTED_PACKETS
printf("count: %d, bufptr at %d, Content:\n", count,
bufptr - buf);
for (i = 0; i < count; i++) {
printf("0x%x, ", (unsigned int) buf[i]);
}
#endif
return (0);
}
size = handlers[(int) *bufptr].size;
} /* playback */
if (size == -1) { /* variable packet */
switch (*bufptr) {
#ifdef SHORT_PACKETS
case SP_S_MESSAGE:
#ifdef RECORD
if(playback)
/* reading 4 bytes at a time from playback file, need to read the
fifth to get the correct size for this packet*/
count += sock_read(asock, buf+count, 1);
#endif
size = ((unsigned char) bufptr[4]); /* IMPORTANT Changed */
break;
case SP_S_WARNING:
if ((unsigned char) bufptr[1] == STEXTE_STRING ||
(unsigned char) bufptr[1] == SHORT_WARNING) {
size = ((unsigned char) bufptr[3]);
} else
size = 4; /* Normal Packet */
break;
case SP_S_PLAYER:
if ((unsigned char) bufptr[1] & (unsigned char) 128) { /* Small +extended
* Header */
size = ((unsigned char) (bufptr[1] & 0x3f) * 4) + 4;
} else if ((unsigned char) bufptr[1] & 64) { /* Small Header */
if (shortversion == SHORTVERSION) /* S_P2 */
size = ((unsigned char) (bufptr[1] & 0x3f) * 4) + 4 + (bufptr[2] * 4);
else
size = ((unsigned char) (bufptr[1] & 0x3f) * 4) + 4;
} else { /* Big Header */
size = ((unsigned char) bufptr[1] * 4 + 12);
}
break;
case SP_S_TORP:
size = vtsize[(int) numofbits[(int) (unsigned char) bufptr[1]]];
break;
case SP_S_TORP_INFO:
size = (vtisize[(int) numofbits[(int) (unsigned char) bufptr[1]]] + numofbits[(unsigned char) bufptr[3]]);
break;
case SP_S_PLANET:
size = ((unsigned char) bufptr[1] * VPLANET_SIZE) + 2;
break;
case SP_S_PHASER: /* S_P2 */
switch ((unsigned char) bufptr[1] & 0x0f) {
case PHFREE:
case PHHIT:
case PHMISS:
size = 4;
break;
case PHHIT2:
size = 8;
break;