-
Notifications
You must be signed in to change notification settings - Fork 0
/
comm.cpp
4635 lines (3891 loc) · 146 KB
/
comm.cpp
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
/* ************************************************************************
* File: comm.c Part of CircleMUD *
* Usage: Communication, socket handling, main(), central game loop *
* *
* All rights reserved. See license.doc for complete information. *
* *
* Copyright (C) 1993, 94 by the Trustees of the Johns Hopkins University *
* CircleMUD is based on DikuMUD, Copyright (C) 1990, 1991. *
************************************************************************ */
/*
* Compression support. Currently could be used with:
*
* MUD Client for Linux, by Erwin S. Andreasen
* http://www.andreasen.org/mcl/
*
* mcclient, by Oliver 'Nemon@AR' Jowett
* http://homepages.ihug.co.nz/~icecube/compress/
*
* Contact them for help with the clients. Contact greerga@circlemud.org
* for problems with the server end of the connection. If you think you
* have found a bug, please test another MUD for the same problem to see
* if it is a client or server problem.
*
* Version 2 support added.
*/
#define __COMM_C__
#include "config.h"
#include "sysdep.h"
#include "execinfo.h"
#if CIRCLE_GNU_LIBC_MEMORY_TRACK
# include <mcheck.h>
#endif
#ifdef CIRCLE_MACINTOSH /* Includes for the Macintosh */
# define SIGPIPE 13
# define SIGALRM 14
/* GUSI headers */
# include <sys/ioctl.h>
/* Codewarrior dependant */
# include <SIOUX.h>
# include <console.h>
#endif
#ifdef CIRCLE_WINDOWS /* Includes for Win32 */
# ifdef __BORLANDC__
# include <dir.h>
# else /* MSVC */
# include <direct.h>
# endif
# include <mmsystem.h>
#endif /* CIRCLE_WINDOWS */
#ifdef CIRCLE_AMIGA /* Includes for the Amiga */
# include <sys/ioctl.h>
# include <clib/socket_protos.h>
#endif /* CIRCLE_AMIGA */
#ifdef CIRCLE_ACORN /* Includes for the Acorn (RiscOS) */
# include <socklib.h>
# include <inetlib.h>
# include <sys/ioctl.h>
#endif
#include <sys/resource.h>
#define RUNNING_IDENT 0
#ifdef HAVE_ARPA_TELNET_H
#include <arpa/telnet.h>
#else
#include "telnet.h"
#endif
/*
* Note, most includes for all platforms are in sysdep.h. The list of
* files that is included is controlled by config.h for that platform.
*/
#include "structs.h"
#include "utils.h"
#include "comm.h"
#include "interpreter.h"
#include "handler.h"
#include "db.h"
#include "house.h"
#include "dg_scripts.h"
#include "screen.h"
#include "arena.h"
#include "mail.h"
#include "dg_event.h"
#include "clan.h"
#include "oasis.h"
#include "genolc.h"
#include "constants.h"
#include "ident.h"
#include "auction.h"
#include "mxp.h"
#include "descriptor.h"
#include "trees.h"
#include "compressor.h"
#if ECL
#include <ecl/ecl.h>
#include "lisp.h"
#endif
#ifndef INVALID_SOCKET
#define INVALID_SOCKET -1
#endif
#define WATCHDOG 3
void delete_descriptor_list();
/* externs */
extern struct ban_list_element *ban_list;
extern int num_invalid;
extern char *GREETINGS;
extern const char *circlemud_version;
extern const char *oasisolc_version;
extern int circle_restrict;
extern int mini_mud;
extern int no_rent_check;
extern FILE *qic_fl;
extern ush_int DFLT_PORT;
extern int id_serv_socket;
extern int *cmd_sort_info;
extern struct time_info_data time_info; /* In db.c */
extern char help[];
extern const char *save_info_msg[]; /* In olc.c */
extern char *race_abbrevs[];
const char *ACTNULL = "<NULL>";
/* local globals */
struct comm_data * comlist = NULL;
int syslogfd = -1;
std::map<string, meta_host_data *> host_list;
int message_type = NOTHING;
void string_format(BYTE * cmd, LWORD space);
Descriptor *descriptor_list = NULL; /* master desc list */
struct txt_block *bufpool = NULL; /* pool of large output buffers */
int buf_largecount = 0; /* # of large buffers which exist */
int buf_overflows = 0; /* # of overflows of output */
int buf_switches = 0; /* # of switches from small to large buf */
int circle_shutdown = 0; /* clean shutdown */
int circle_reboot = 0; /* reboot the game after a shutdown */
int no_specials = 0; /* Suppress ass. of special routines */
int max_players = 0; /* max descriptors available */
int tics_passed = 0; /* for extern checkpointing */
int scheck = 0; /* for syntax checking mode */
int dg_act_check = 0; /* toggle for act_trigger */
unsigned long pulse = 0; /* number of pulses since game start */
extern const int xap_objs; /* ascii objects. */
struct timeval null_time; /* zero-valued time structure */
FILE *logfile = NULL; /* Where to send the log messages. */
FILE *comfile = NULL; /* where to send communications */
struct comm_data *commlist = NULL;
int intermud_desc;
int connected_to_intermud;
sbyte reread_wizlist; /* signal: SIGUSR1 */
sbyte emergency_unban; /* signal: SIGUSR2 */
void free_host_list();
void add_ip_to_host_list(string &host_ip, string &host, time_t date);
int no_ext_processes = 0; /* shall we use external processes? */
extern int use_external_lookup_process; /* see config.c */
static bool fCopyOver; /* Are we booting in copyover mode? */
socket_t mother_desc; /* Now a global */
ush_int port = 0;
char last_command[MAX_STRING_LENGTH] = "Empty";
/* functions in this file */
void send_out_signals(Descriptor *d);
char * parse_prompt(Character *ch, char *str, size_t lenn);
void clear_char_q(Descriptor *t);
RETSIGTYPE reread_wizlists(int sig);
RETSIGTYPE unrestrict_game(int sig);
RETSIGTYPE reap(int sig);
RETSIGTYPE checkpointing(int sig);
RETSIGTYPE hupsig(int sig);
ssize_t perform_socket_read(socket_t desc, char *read_point, size_t space_left);
ssize_t perform_socket_write(socket_t desc, const char *txt, size_t length, struct compr *comp);
void echo_off(Descriptor *d);
void echo_on(Descriptor *d);
void circle_sleep(struct timeval *timeout);
void signal_setup(void);
void game_loop(socket_t s_mother_desc);
void timediff(struct timeval *diff, struct timeval *a, struct timeval *b);
void timeadd(struct timeval *sum, struct timeval *a, struct timeval *b);
void nonblock(socket_t s);
int perform_subst(Descriptor *t, char *orig, char *subst);
void record_usage(void);
void check_idle_passwords(void);
void heartbeat(int heart_pulse);
void make_who2html(void);
void init_descriptor(Descriptor *newd, int desc);
void tick_grenade(void);
void setup_log(const char *filename, int fd);
void setup_com(const char *filename, int fd);
socket_t init_socket(ush_int port);
void init_game(ush_int port);
int get_from_q(struct txt_q *queue, char *dest, int *aliased);
Descriptor *new_descriptor(socket_t s, int copyover);
int get_max_players(void);
int process_output(Descriptor *t);
int perform_alias(Descriptor *d, char *orig, size_t maxlen);
int parse_ip(const char *addr, struct in_addr *inaddr);
int set_sendbuf(socket_t s);
void free_bufpool(void);
int open_logfile(const char *filename, FILE * stderr_fp);
int open_comlogfile(const char *filename, FILE * stderr_fp);
char *make_prompt(Descriptor *point);
struct in_addr *get_bind_addr(void);
#if defined(POSIX)
sigfunc *my_signal(int signo, sigfunc * func);
#endif
#if defined(HAVE_ZLIB)
void *zlib_alloc(void *opaque, unsigned int items, unsigned int size);
void zlib_free(void *opaque, void *address);
#endif
void free_all_notes(void);
void clear_free_list(void);
void free_messages(void);
void Board_clear_all(void);
void free_social_messages(void);
void free_mail_index(void);
void Free_Invalid_List(void);
void free_pending_objects();
void free_command_list(void);
//void free_help(struct help_index_element *help);
/* extern fcnts */
void crashproof_objects_save_all();
void check_for_dead(void);
void extract_pending_chars(void);
#if 0
void extract_pending_objects(void);
#endif
void free_mine_shafts(void);
void clear_free_list(void);
void reboot_wizlists(void);
void boot_world(void);
void regen_update(void);
void msdp_update(void); //@TODO:PROTOCOL
void affect_update(void); /* In spells.c */
void mobile_activity(void);
void perform_violence(void);
void sector_update(void);
void mobile_mating(void);
void load_config(void);
void free_mail_index(void);
void show_string(Descriptor *d, char *input);
void weather_and_time(int mode);
int id_init(void);
void id_kill(void);
void init_intermud_socket(void);
size_t proc_colour(char *inbuf, int colour_lvl, size_t len);
void redit_save_to_disk(int zone_num);
void oedit_save_to_disk(int zone_num);
void medit_save_to_disk(int zone_num);
void sedit_save_to_disk(int zone_num);
void zedit_save_to_disk(int zone_num);
void start_arena(void);
void run_events2(void);
void extract_delayed_mobs(void);
void extract_delayed_objs(void);
void check_auction(void);
int real_zone(int number);
int isbanned(char *hostname, bool is_player_name);
void free_messages(void);
void update_spell_wait(void);
int restrict_check(const Character *ch);
void Board_clear_all(void);
int has_note(Character *ch, int type);
void thefree_social_messages(void);
void free_ban_list(void);
void free_vehicles(void);
int enter_player_game(Descriptor *d);
void process_vehicle(void);
void process_room_affect_queue(void);
#if RUNNING_IDENT
static void get_lookup_reply(void);
#endif
void send_compress_offer(Descriptor *d);
#ifdef HAVE_ZLIB_H
/* zlib helper functions */
void *z_alloc(void *opaque, uInt items, uInt size) {
return calloc(items, size);
}
void z_free(void *opaque, void *address) {
free(address);
address = NULL;
}
#endif /* HAVE_ZLIB_H */
#ifdef __CXREF__
#undef FD_ZERO
#undef FD_SET
#undef FD_ISSET
#undef FD_CLR
#define FD_ZERO(x)
#define FD_SET(x, y) 0
#define FD_ISSET(x, y) 0
#define FD_CLR(x, y)
#endif
#if defined(HAVE_ZLIB)
/*
* MUD Client for Linux and mcclient compression support.
* "The COMPRESS option (unofficial and completely arbitary) is
* option 85." -- mcclient documentation as of Dec '98.
*
* [ Compression protocol documentation below, from Compress.c ]
*
* Server sends IAC WILL COMPRESS
* We reply with IAC DO COMPRESS
*
* Later the server sends IAC SB COMPRESS WILL SE, and immediately following
* that, begins compressing
*
* Compression ends on a Z_STREAM_END, no other marker is used
*
* 2001/02/08 - Version 2 support. - mike
* Version one uses an improper subnegotiation sequence to indicate start of compression
* This represents the changes in v1 and v2:
* It uses the equally arbitrary COMPRESS2 option 86,
* It properly terminates the subnegotiation sequence.
*/
#define TELOPT_COMPRESS static_cast<const char>(85)
#define TELOPT_COMPRESS2 static_cast<const char>(86)
#define COMPRESS static_cast<const char>(85)
#define COMPRESS2 static_cast<const char>(86)
/* first compression neg. string */
static const char compress_offer[] = {
(char) IAC,
(char) WILL,
(char) TELOPT_COMPRESS2,
(char) 0,
};
static const char will_sig[] = {
static_cast<const char>(IAC), static_cast<const char>(WILL), TELOPT_COMPRESS, 0
};
static const char do_sig[] = {
static_cast<const char>(IAC), static_cast<const char>(DO), TELOPT_COMPRESS, 0
};
static const char dont_sig[] = {
static_cast<const char>(IAC), static_cast<const char>(DONT), TELOPT_COMPRESS, 0
};
static const char on_sig[] = {
static_cast<const char>(IAC), static_cast<const char>(SB), TELOPT_COMPRESS, static_cast<const char>(WILL), static_cast<const char>(SE), 0
};
static const char will_sig2[] = {
static_cast<const char>(IAC), static_cast<const char>(WILL), TELOPT_COMPRESS2, 0
};
static const char do_sig2[] = {
static_cast<const char>(IAC), static_cast<const char>(DO), TELOPT_COMPRESS2, 0
};
static const char dont_sig2[] = {
static_cast<const char>(IAC), static_cast<const char>(DONT), TELOPT_COMPRESS2, 0
};
static const char on_sig2[] = {
static_cast<const char>(IAC), static_cast<const char>(SB), TELOPT_COMPRESS2, static_cast<const char>(IAC), static_cast<const char>(SE), static_cast<const char>(0)
};
#endif
/***********************************************************************
* main game loop and related stuff *
***********************************************************************/
#if defined(CIRCLE_WINDOWS) || defined(CIRCLE_MACINTOSH)
/*
* Windows doesn't have gettimeofday, so we'll simulate it.
* The Mac doesn't have gettimeofday either.
* Borland C++ warns: "Undefined structure 'timezone'"
*/
void gettimeofday(struct timeval *t, struct timezone *dummy) {
#if defined(CIRCLE_WINDOWS)
DWORD millisec = GetTickCount();
#elif defined(CIRCLE_MACINTOSH)
unsigned long int millisec;
millisec = (int) ((float) TickCount() * 1000.0 / 60.0);
#endif
t->tv_sec = (int) (millisec / 1000);
t->tv_usec = (millisec % 1000) * 1000;
}
#endif /* CIRCLE_WINDOWS || CIRCLE_MACINTOSH */
#define plant_magic(x) do { (x)[sizeof(x) - 1] = MAGIC_NUMBER; } while (0)
#define test_magic(x) ((x)[sizeof(x) - 1])
#if ECL
int start_argc;
char **start_argv;
void boot_lisp (int argc, char **argv) {
ecl_set_option(ECL_OPT_TRAP_SIGSEGV, FALSE);
ecl_set_option(ECL_OPT_TRAP_SIGINT, FALSE);
cl_boot(argc, argv);
ecl_init_module(OBJNULL, init_4d_lisp);
}
#endif
int main(int argc, char **argv) {
//ush_int port;
int pos = 1;
const char *dir;
Room *rm = NULL;
#ifdef CIRCLE_UNIX
if (!getuid()) {
puts("Please, *DO*NOT* run CircleMUD as root! Someone may get hurt!\r\n");
exit(1);
}
#endif
#if CIRCLE_GNU_LIBC_MEMORY_TRACK
mtrace(); /* This must come before any use of malloc(). */
#endif
#ifdef CIRCLE_MACINTOSH
/*
* ccommand() calls the command line/io redirection dialog box from
* Codewarriors's SIOUX library
*/
argc = ccommand(&argv);
/* Initialize the GUSI library calls. */
GUSIDefaultSetup();
#endif
#if ECL
const char* shell_flag = "--shell";
const char *const_string;
if (argc >= 2 && !strncmp(shell_flag,argv[1],strlen(shell_flag))) {
boot_lisp(argc,argv);
ecl_init_module(OBJNULL, init_4d_lisp);
if (argc == 3) {
const_string = "(handler-case (load \"%s\") (error (e) (format t \"error: ~a~%\" e)))";
int es_size = strlen(const_string) + strlen(argv[2]);
char* eval_string = (char *)malloc(es_size);
snprintf(eval_string, es_size, const_string, argv[2]);
cl_safe_eval(c_string_to_object(eval_string), Cnil, Cnil);
free(eval_string);
eval_string = NULL;
}
else {
printf("No file argument detected. starting interactive repl.\n");
const_string = "(handler-case (loop (write-string \"> \") (princ (eval (read))) (terpri)) (error (e) (format t \"error: ~a~%\" e)))";
cl_safe_eval(c_string_to_object(const_string), Cnil, Cnil);
}
cl_shutdown();
exit(0);
}
#endif
/****************************************************************************/
/** Load the game configuration. **/
/** We must load BEFORE we use any of the constants stored in constants.c. **/
/** Otherwise, there will be no variables set to set the rest of the vars **/
/** to, which will mean trouble --> Mythran **/
/****************************************************************************/
CONFIG_CONFFILE = NULL;
while ((pos < argc) && (*(argv[pos]) == '-')) {
if (*(argv[pos] + 1) == 'f') {
if (*(argv[pos] + 2))
CONFIG_CONFFILE = argv[pos] + 2;
else if (++pos < argc)
CONFIG_CONFFILE = argv[pos];
else {
puts("SYSERR: File name to read from expected after option -f.");
exit(1);
}
}
pos++;
}
pos = 1;
/** this is a dum place to have this, however the damn config
setup would need to be changed to handle not having the world array created yet
one day Mord!
**/
world_vnum.assign(HIGHEST_VNUM, rm);
if (!CONFIG_CONFFILE)
CONFIG_CONFFILE = strdup(CONFIG_FILE);
load_config();
port = CONFIG_DFLT_PORT;
dir = CONFIG_DFLT_DIR;
while ((pos < argc) && (*(argv[pos]) == '-')) {
switch (*(argv[pos] + 1)) {
case 'f':
if (! *(argv[pos] + 2))
++pos;
break;
case 'x':
no_ext_processes = 1;
log("Running without external processes.");
break;
case 'o':
if (*(argv[pos] + 2))
CONFIG_LOGNAME = argv[pos] + 2;
else if (++pos < argc)
CONFIG_LOGNAME = argv[pos];
else {
puts("SYSERR: File name to log to expected after option -o.");
exit(1);
}
break;
case 'C': /* -C<socket number> - recover from copyover, this is the control socket */
fCopyOver = TRUE;
mother_desc = atoi(argv[pos] + 2);
break;
case 'd':
if (*(argv[pos] + 2))
dir = argv[pos] + 2;
else if (++pos < argc)
dir = argv[pos];
else {
puts("SYSERR: Directory arg expected after option -d.");
exit(1);
}
break;
case 'm':
mini_mud = 1;
no_rent_check = 1;
puts("Running in minimized mode & with no rent check.");
break;
case 'c':
scheck = 1;
puts("Syntax check mode enabled.");
break;
case 'q':
no_rent_check = 1;
puts("Quick boot mode -- rent check supressed.");
break;
case 'r':
circle_restrict = 1;
puts("Restricting game -- no new players allowed.");
break;
case 's':
no_specials = 1;
puts("Suppressing assignment of special routines.");
break;
case 'h':
/* From: Anil Mahajan <amahajan@proxicom.com> */
printf
("Usage: %s [-c] [-m] [-q] [-r] [-s] [-x] [-d pathname] [port #]\n"
" -c Enable syntax check mode.\n"
" -d <directory> Specify library directory (defaults to 'lib').\n"
" -h Print this command line argument help.\n"
" -m Start in mini-MUD mode.\n"
" -f<file> Use <file> for configuration.\n"
" -o <file> Write log to <file> instead of stderr.\n"
" -q Quick boot (doesn't scan rent for object limits)\n"
" -r Restrict MUD -- no new players allowed.\n"
" -s Suppress special procedure assignments.\n"
" Note: These arguments are 'CaSe SeNsItIvE!!!'\n",
argv[0]
);
exit(1);
default:
printf("SYSERR: Unknown option -%c in argument string.",
*(argv[pos] + 1));
break;
}
pos++;
}
if (pos < argc) {
if (!isdigit(*argv[pos])) {
printf
("Usage: %s [-c] [-m] [-q] [-r] [-s] [-x] [-d pathname] [port #]\n",
argv[0]);
exit(1);
} else if ((port = atoi(argv[pos])) <= 1024) {
printf("SYSERR: Illegal port number %d.\n", port);
exit(1);
}
}
/* All arguments have been parsed, try to open log file. */
setup_log(CONFIG_LOGNAME, STDERR_FILENO);
setup_com("log/comlog", -1);
/*
* Moved here to distinguish command line options and to show up
* in the log if stderr is redirected to a file.
*/
log("Using %s for configuration.", CONFIG_CONFFILE);
log("%s", circlemud_version);
log("%s", oasisolc_version);
log("%s", fourdimensions_version);
// if (id_init() < 0)
// exit(2);
if (chdir(dir) < 0) {
perror("SYSERR: Fatal error changing to data directory");
exit(1);
}
log("Using %s as data directory.", dir);
if (scheck) {
boot_world();
log("Done.");
} else {
log("Running game on port %d.", port);
#if ECL
start_argc = argc;
start_argv = argv;
#endif
init_game(port);
}
log("Clearing game world.");
destroy_db();
if (!scheck) {
log("Clearing other memory.");
free_bufpool(); /* comm.c */
log("Buf pool freed.");
free_all_notes(); /*note.c*/
log("Notes freed.");
free_social_messages(); /* act.social.c */
log("Socials freed.");
pi.FreeSelf(); /* db.c */
log("Playerindex freed.");
free_messages(); /* fight.c */
log("Fight messages freed.");
clear_free_list(); /* mail.c */
log("clear free list - mail.");
free_mail_index(); /* mail.c */
log("clear mail index.");
free_text_files(); /* db.c */
log("text files freed.");
Board_clear_all(); /* boards.c */
log("boards freed.");
free(cmd_sort_info); /* act.informative.c */
cmd_sort_info = NULL;
log("commands sort info freed.");
free_command_list(); /* act.informative.c */
log("command list freed");
the_free_help(); /* db.c */
log("help freed.");
Free_Invalid_List(); /* ban.c */
log("invalid names list freed.");
free_ban_list(); /* ban.c*/
log("ban list freed.");
free_host_list();
log("host cache freed.");
free_strings(&config_info, OASIS_CFG); /* oasis_delete.c */
log("config freed.");
free_vehicles();
log("vehicles freed.");
free_commlist(comlist);
log("communication chatter freed.");
free_mine_shafts();
log("mines freed.");
if (comfile)
fclose(comfile);
}
/* probably should free the entire config here.. */
free(CONFIG_CONFFILE);
CONFIG_CONFFILE = NULL;
log("Done.");
return (0);
}
void circle_exit(int retval) {
// id_kill();
#undef exit
exit(retval);
#define exit(n) circle_exit(n)
}
/* Reload players after a copyover */
void copyover_recover(void) {
Descriptor *d;
FILE *fp;
char host[1024];
int desc, player_i;
bool fOld;
char name[MAX_INPUT_LENGTH];
char client_name[MAX_INPUT_LENGTH];
char client_version[MAX_INPUT_LENGTH];
char protocol_data[MAX_STRING_LENGTH];
int saved_loadroom = NOWHERE, varcnt = 0;
log("Copyover recovery initiated");
fp = fopen(COPYOVER_FILE, "r");
if (!fp) { /* there are some descriptors open which will hang forever then ? */
perror("copyover_recover:fopen");
log("Copyover file not found. Exitting.\r\n");
exit(1);
}
unlink(COPYOVER_FILE); /* In case something crashes - doesn't prevent reading */
for (;;) {
fOld = TRUE;
varcnt = fscanf(fp, "%d %s %s %d %s %s %s\n", &desc, name, host, &saved_loadroom, client_name, client_version, protocol_data);
if (desc == -1)
break;
if (varcnt != 7)
continue;
/* Write something, and check if it goes error-free */
if (write_to_descriptor(desc,"\n\r...the flames begin to subside and the world brightens...\r\n", NULL) == 0) {
close(desc); /* nope */
continue;
}
#if 0
if ((d = new_descriptor(desc, TRUE)) == NULL)
continue;
#else
/* create a new descriptor */
d = new Descriptor();
d->pProtocol->bNegotiated = true; //@TODO:PROTOCOL: Don't renegotiate.
d->init_descriptor(desc);
d->next = descriptor_list;
descriptor_list = d;
#endif
MSDPSetString( d, eMSDP_CLIENT_ID, client_name );
MSDPSetString( d, eMSDP_CLIENT_VERSION, client_version );
CopyoverSet( d, protocol_data );
d->host = host;
d->connected = CON_CLOSE;
/* Now, find the pfile */
d->character = new Character(FALSE);
d->character->desc = d;
if ((player_i = pi.LoadChar(name, d->character)) >= 0) {
if (!PLR_FLAGGED(d->character, PLR_DELETED)) {
REMOVE_BIT_AR(PLR_FLAGS(d->character), PLR_WRITING);
REMOVE_BIT_AR(PLR_FLAGS(d->character), PLR_MAILING);
REMOVE_BIT_AR(PLR_FLAGS(d->character), PLR_CRYO);
} else
fOld = FALSE;
} else
fOld = FALSE;
if (!fOld) { /* Player file not found?! */
write_to_descriptor(desc,"\n\rSomehow, your character was lost in the copyover. Sorry.\r\n", NULL);
delete d;
} else { /* ok! */
write_to_descriptor (desc, "\n\rThe world has been reborn.\r\n", NULL);
#ifdef HAVE_ZLIB_H
//@TODO:PROTOCOL
// if (!PRF_FLAGGED(d->character, PRF_NOCOMPRESS) && !d->mxp) {
// d->comp->state = 1; /* indicates waiting for comp negotiation */
// send_compress_offer(d);
// }
#endif /* HAVE_ZLIB_H */
send_out_signals(d);
d->connected = CON_PLAYING;
GET_LOADROOM(d->character) = saved_loadroom;
enter_player_game(d);
}
}
fclose(fp);
}
/* Init sockets, run game, and cleanup sockets */
void init_game(ush_int s_port) {
/* We don't want to restart if we crash before we get up. */
touch(KILLSCRIPT_FILE);
circle_srandom(time(0));
log("Finding player limit.");
max_players = get_max_players();
if (!fCopyOver) { /* If copyover mother_desc is already set up */
log("Opening mother connection.");
mother_desc = init_socket(s_port);
}
log("Initiating events.");
event_init();
#if defined(CIRCLE_UNIX) || defined(CIRCLE_MACINTOSH)
log("Signal trapping.");
signal_setup();
#endif
#if ECL
// start lisp
boot_lisp(start_argc, start_argv);
ecl_init_module(OBJNULL, init_4d_lisp);
//read_VV(OBJNULL, init_lib_4D_LISP);
cl_safe_eval(c_string_to_object("(handler-case (4d::init) (error (e) (format t \"error: ~a~%\" e)))"), Cnil, Cnil);
//start_swank(s_port+100);
#endif
// log("Setting hash table");
/* set up hash table for find_char() */
// init_lookup_table();
log("Booting database.");
boot_db();
/* If we made it this far, we will be able to restart without problem. */
remove
(KILLSCRIPT_FILE);
if (fCopyOver) /* reload players */
copyover_recover();
log("Entering game loop.");
game_loop(mother_desc);
#if ECL
cl_shutdown();
#endif
log("Crash saving all.");
Crash_save_all();
log("Closing all sockets.");
delete_descriptor_list();
CLOSE_SOCKET(mother_desc);
CLOSE_SOCKET(intermud_desc);
fclose(qic_fl); /* close qic database */
if (circle_reboot != 2)
save_all();
log("Saving current MUD time.");
save_mud_time(&time_info);
if (circle_reboot) {
log("Rebooting.");
exit(52); /* what's so great about HHGTTG, anyhow? */
}
log("Normal termination of game.");
}
/*
* init_socket sets up the mother descriptor - creates the socket, sets
* its options up, binds it, and listens.
*/
socket_t init_socket(ush_int s_port) {
socket_t s;
struct sockaddr_in sa;
int opt;
#ifdef CIRCLE_WINDOWS
{
WORD wVersionRequested;
WSADATA wsaData;
wVersionRequested = MAKEWORD(1, 1);
if (WSAStartup(wVersionRequested, &wsaData) != 0) {
log("SYSERR: WinSock not available!");
exit(1);
}
if ((wsaData.iMaxSockets - 4) < max_players) {
max_players = wsaData.iMaxSockets - 4;
}
log("Max players set to %d", max_players);
if ((s = socket(PF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET) {
log("SYSERR: Error opening network connection: Winsock error #%d", WSAGetLastError());
exit(1);
}
}
#else
/*
* Should the first argument to socket() be AF_INET or PF_INET? I don't
* know, take your pick. PF_INET seems to be more widely adopted, and
* Comer (_Internetworking with TCP/IP_) even makes a point to say that
* people erroneously use AF_INET with socket() when they should be using
* PF_INET. However, the man pages of some systems indicate that AF_INET
* is correct; some such as ConvexOS even say that you can use either one.
* All implementations I've seen define AF_INET and PF_INET to be the same
* number anyway, so the point is (hopefully) moot.
*/
if ((s = socket(PF_INET, SOCK_STREAM, 0)) < 0) {
perror("SYSERR: Error creating socket");
exit(1);
}
#endif /* CIRCLE_WINDOWS */
#if defined(SO_REUSEADDR) && !defined(CIRCLE_MACINTOSH)
opt = 1;
if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (char *) &opt, sizeof(opt))
< 0) {
perror("SYSERR: setsockopt REUSEADDR");
exit(1);
}
#endif
#ifdef SO_SNDBUF
set_sendbuf(s);
#endif
/*
* The GUSI sockets library is derived from BSD, so it defines
* SO_LINGER, even though setsockopt() is unimplimented.
* (from Dean Takemori <dean@UHHEPH.PHYS.HAWAII.EDU>)
*/
#if defined(SO_LINGER) && !defined(CIRCLE_MACINTOSH)
{
struct linger ld;
ld.l_onoff = 0;
ld.l_linger = 0;
if (setsockopt(s, SOL_SOCKET, SO_LINGER, (char *) &ld, sizeof(ld))
< 0)
perror("SYSERR: setsockopt SO_LINGER"); /* Not fatal I suppose. */
}
#endif
/* Clear the structure */
memset((char *) &sa, 0, sizeof(sa));
sa.sin_family = AF_INET;
sa.sin_port = htons(s_port);
sa.sin_addr = *(get_bind_addr());
if (bind(s, (struct sockaddr *) &sa, sizeof(sa)) < 0) {
perror("SYSERR: bind");
CLOSE_SOCKET(s);
exit(1);
}
nonblock(s);
listen(s, 5);
return (s);
}
int get_max_players(void) {
#ifndef CIRCLE_UNIX
return (CONFIG_MAX_PLAYING);
#else
int max_descs = 0;
const char *method;
/*
* First, we'll try using getrlimit/setrlimit. This will probably work