forked from cotillion/cd-gamedriver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
comm1.c
1301 lines (1121 loc) · 33.2 KB
/
comm1.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
#include <sys/types.h>
#include <sys/time.h>
#include <sys/ioctl.h>
#include <sys/file.h>
#include <fcntl.h>
#include <stdarg.h>
#include <arpa/telnet.h>
#include <netdb.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <ctype.h>
#include <signal.h>
#include <memory.h>
#include <fcntl.h>
#include <stdlib.h>
#include "config.h"
#include "lint.h"
#include "mstring.h"
#include "interpret.h"
#include "comm.h"
#include "object.h"
#include "sent.h"
#include "simulate.h"
#include "super_snoop.h"
#include "ndesc.h"
#include "hname.h"
#include "nqueue.h"
#include "telnet.h"
#include "tcpsvc.h"
#include "udpsvc.h"
#include "main.h"
#include "backend.h"
#include "mapping.h"
#include "json.h"
#include "inline_svalue.h"
#include "comm1.h"
void set_prompt (char *);
void prepare_ipc (void);
void ipc_remove (void);
char *query_ip_number (struct object *);
char *query_port_number (struct object *);
char *query_host_name (void);
extern char *string_copy(char *);
extern int d_flag;
extern int s_flag;
extern int no_ip_demon;
void remove_interactive(struct interactive *, int), add_ref(struct object *, char *);
struct interactive *all_players[MAX_PLAYERS];
extern struct object *current_interactive;
extern struct object *previous_ob;
#ifdef CATCH_UDP_PORT
extern int udp_port;
udpsvc_t *udpsvc;
#endif /* CATCH_UDP_PORT */
void *new_player(void *, struct sockaddr_storage *, socklen_t, u_short local_port);
int num_player;
/*
* Interprocess communication interface to the backend.
*/
extern int port_number;
#ifndef NO_IP_DEMON
static void *hname;
static void
shutdown_hname(void *hp)
{
hname = NULL;
}
static void
receive_hname(const char *addr, int lport, int rport,
const char *ip_name, const char *rname)
{
int i;
struct interactive *ip;
if (addr == NULL || rname == NULL)
return;
for (i = 0; i < MAX_PLAYERS; i++)
{
ip = all_players[i];
if (ip == NULL)
continue;
const char *ip_number = query_ip_number(ip->ob);
if (ip_number != NULL && !strcmp(addr, query_ip_number(ip->ob)))
{
if (strlen(ip_name))
{
if (ip->host_name)
free(ip->host_name);
ip->host_name = xalloc(strlen(ip_name) + 1);
strcpy(ip->host_name, ip_name);
}
if (ip->lport == lport && atoi(query_port_number(ip->ob)) == rport && strlen(rname))
{
if (ip->rname != NULL)
free(ip->rname);
ip->rname = xalloc(strlen(rname) + 1);
strcpy(ip->rname, rname);
}
}
}
}
#endif
void
prepare_ipc()
{
nd_init();
#ifndef NO_IP_DEMON
if (!no_ip_demon)
hname = hname_init(receive_hname, shutdown_hname);
#endif
telnet_init((u_short)port_number);
#ifdef SERVICE_PORT
tcpsvc_init((u_short)service_port);
#endif /* SERVICE_PORT */
#ifdef CATCH_UDP_PORT
udpsvc = udpsvc_init(udp_port);
#endif /* CATCH_UDP_PORT */
(void)signal(SIGPIPE, SIG_IGN);
}
/*
* This one is called when shutting down the MUD.
*/
void
ipc_remove()
{
(void)printf("Shutting down ipc...\n");
nd_shutdown();
}
void write_socket(char *, struct object *);
/*
* Send a message to a player. If that player is shadowed, special
* care has to be taken.
*/
/*VARARGS1*/
void
add_message2(struct object *ob, char *fmt, ...)
{
char buff[10000];
va_list argp;
va_start(argp, fmt);
(void)vsnprintf(buff, sizeof(buff), fmt, argp);
/* LINTED: expression has null effect */
va_end(argp);
if (ob && !(ob->flags & O_DESTRUCTED))
{
push_string(buff, STRING_MSTRING);
(void)apply("catch_tell", ob, 1, 1);
}
else
write_socket(buff, (struct object *)0);
}
void
add_message(char *fmt, ...)
{
char buff[10000];
va_list argp;
va_start(argp, fmt);
(void)vsnprintf(buff, sizeof(buff), fmt, argp);
/* LINTED: expression has null effect */
va_end(argp);
if (command_giver && !(command_giver->flags & O_DESTRUCTED))
{
push_string(buff, STRING_MSTRING);
(void)apply("catch_tell", command_giver, 1, 1);
}
else
write_socket(buff, (struct object *)0);
}
/*
* Outputs GMCP data to the client
*/
void
write_gmcp(struct object *ob, char *data)
{
struct interactive *ip;
if (ob == NULL || ob->flags & O_DESTRUCTED || (ip = ob->interactive) == NULL || ip->do_close)
return;
telnet_output_gmcp(ip->tp, (u_char *)data);
}
/*
* Will output to telnet on every newline to avoid overflow.
*/
void
write_socket(char *cp, struct object *ob)
{
struct interactive *ip;
int telnet_overflow = 0, overflow = 0;
#ifdef WORD_WRAP
int len, current_column;
char *bp, buf[MAX_WRITE_SOCKET_SIZE + 2];
#endif
if (ob == NULL || ob->flags & O_DESTRUCTED || (ip = ob->interactive) == NULL || ip->do_close)
{
(void)fputs(cp, stderr);
(void)fflush(stderr);
return;
}
#ifdef SUPER_SNOOP
if (ip->snoop_fd != -1)
(void)write(ip->snoop_fd, cp, strlen(cp));
#endif
if (ip->snoop_by != NULL)
add_message2(ip->snoop_by->ob, "%%%s", cp);
#ifdef WORD_WRAP
if (ip->screen_width != 0)
{
current_column = ip->current_column;
bp = buf;
for (;;)
{
if (*cp == '\0')
break;
if (bp >= &buf[MAX_WRITE_SOCKET_SIZE])
{
bp = &buf[MAX_WRITE_SOCKET_SIZE];
overflow = 1;
break;
}
if (*cp == '\n')
{
current_column = 0;
*bp++ = *cp++;
*bp = '\0';
telnet_overflow = telnet_output(ip->tp, (u_char *)buf);
bp = buf;
if (telnet_overflow)
break;
continue;
}
if (*cp == '\t')
{
current_column &= ~7;
current_column += 8;
if (current_column >= ip->screen_width)
{
current_column = 0;
*bp++ = '\n';
*bp = '\0';
telnet_overflow = telnet_output(ip->tp, (u_char *)buf);
bp = buf;
if (telnet_overflow)
break;
while (*cp == ' ' || *cp == '\t')
cp++;
}
else
{
*bp++ = *cp++;
}
continue;
}
len = 1;
while (cp[len] > ' ' && cp[len - 1] != '-')
len++;
if (current_column + len >= ip->screen_width)
{
if (current_column > ip->screen_width * 4 / 5)
{
current_column = 0;
*bp++ = '\n';
*bp = '\0';
telnet_overflow = telnet_output(ip->tp, (u_char *)buf);
bp = buf;
if (telnet_overflow)
break;
while (*cp == ' ' || *cp == '\t')
cp++;
if (*cp == '\n')
cp++;
continue;
}
}
if (bp + len > &buf[MAX_WRITE_SOCKET_SIZE])
{
len = (&buf[MAX_WRITE_SOCKET_SIZE] - bp);
overflow = 1;
}
current_column += len;
for (; len > 0; len--)
*bp++ = *cp++;
}
ip->current_column = current_column;
*bp = '\0';
telnet_overflow |= telnet_output(ip->tp, (u_char *)buf);
if (overflow && !telnet_overflow)
telnet_output(ip->tp, (u_char *)"*** Truncated! ***\n");
}
else
#endif
{
telnet_output(ip->tp, (u_char *)cp);
/* No test on overflow. Let the telnet buffer handle it. */
}
}
#define MSR_SIZE 20
int msecs_response[MSR_SIZE];
int msr_point = -1;
int
get_msecs_response(int ix)
{
return (ix >= 0 && ix < MSR_SIZE) ? msecs_response[ix] : -1;
}
/*
* Remove an interactive player immediately.
*/
void
remove_interactive(struct interactive *ip, int link_dead)
{
struct object *save = command_giver;
struct object *ob = ip->ob;
int i;
if (!ob || !(ob->interactive))
return;
for (i = 0; i < MAX_PLAYERS; i++)
{
if (all_players[i] != ob->interactive)
continue;
if (ob->interactive->closing)
fatal("Double call to remove_interactive()\n");
command_giver = ob;
if (ob->interactive->ed_buffer)
{
extern void save_ed_buffer(void);
/* This will call 'get_ed_buffer_save_file_name' in master_ob
* If that fails(error in LPC) the closing IP will hang and
* on the next read a 'fatal read on closing socket will occur'
* unless we do this here before ip->closing = 1
*/
(void)add_message("Saving editor buffer.\n");
save_ed_buffer();
}
if (ob->interactive == NULL)
return;
ob->interactive->closing = 1;
if (ob->interactive->snoop_by)
{
ob->interactive->snoop_by->snoop_on = 0;
ob->interactive->snoop_by = 0;
}
if (ob->interactive->snoop_on)
{
ob->interactive->snoop_on->snoop_by = 0;
ob->interactive->snoop_on = 0;
}
write_socket("Closing down.\n", ob);
telnet_detach(ob->interactive->tp);
#ifdef SUPER_SNOOP
if (ob->interactive->snoop_fd >= 0) {
(void)close(ob->interactive->snoop_fd);
ob->interactive->snoop_fd = -1;
}
#endif
num_player--;
if (ob->interactive->input_to)
{
free_sentence(ob->interactive->input_to);
ob->interactive->input_to = 0;
}
if (ob->interactive->host_name)
free(ob->interactive->host_name);
if (ob->interactive->rname)
free(ob->interactive->rname);
if (current_interactive == ob)
current_interactive = 0;
free((char *)ob->interactive);
ob->interactive = 0;
all_players[i] = 0;
free_object(ob, "remove_interactive");
push_object(ob);
push_number(link_dead);
(void)apply_master_ob(M_REMOVE_INTERACTIVE,2);
command_giver = save;
return;
}
fatal("Could not find and remove player %s\n", ob->name);
}
void
set_interactive_address(struct interactive *ip, struct sockaddr_storage *addr, socklen_t len)
{
memcpy(&ip->addr, (char *)addr, len);
ip->addrlen = len;
if (ip->host_name) {
free(ip->host_name);
ip->host_name = NULL;
}
#ifndef NO_IP_DEMON
if (!no_ip_demon)
hname_sendreq(hname, query_ip_number(ip->ob), (u_short)ip->lport, atoi(query_port_number(ip->ob)));
#endif
}
/*
* get the I'th player object from the interactive list, i starts at 0
* and can go to num_player - 1. For users(), etc.
*/
struct object *
get_interactive_object(int i)
{
int n;
if (i >= num_player) /* love them ASSERTS() :-) */
fatal("Get interactive (%d) with only %d players!\n", i, num_player);
for (n = 0; n < MAX_PLAYERS; n++)
if (all_players[n])
if (!(i--))
return(all_players[n]->ob);
fatal("Get interactive: player %d not found! (num_players = %d)\n",
i, num_player);
return 0; /* Just to satisfy some compiler warnings */
}
void *
new_player(void *tp, struct sockaddr_storage *addr, socklen_t len, u_short local_port)
{
int i;
for (i = 0; i < MAX_PLAYERS; i++)
{
struct interactive *inter;
struct object *ob;
struct svalue *ret;
extern struct object *master_ob;
if (all_players[i] != 0)
continue;
command_giver = master_ob;
current_interactive = master_ob;
inter = (struct interactive *)xalloc(sizeof (struct interactive));
memset(inter, 0, sizeof(struct interactive));
master_ob->interactive = inter;
master_ob->interactive->default_err_message = 0;
master_ob->flags |= O_ONCE_INTERACTIVE;
/* This initialization is not pretty. */
master_ob->interactive->rname = 0;
master_ob->interactive->ob = master_ob;
master_ob->interactive->input_to = 0;
master_ob->interactive->closing = 0;
master_ob->interactive->snoop_on = 0;
master_ob->interactive->snoop_by = 0;
master_ob->interactive->do_close = 0;
master_ob->interactive->noecho = 0;
master_ob->interactive->trace_level = 0;
master_ob->interactive->trace_prefix = 0;
master_ob->interactive->last_time = current_time;
master_ob->interactive->ed_buffer = 0;
#ifdef SUPER_SNOOP
master_ob->interactive->snoop_fd = -1;
#endif
#ifdef WORD_WRAP
master_ob->interactive->current_column = 0;
master_ob->interactive->screen_width = 0;
#endif
all_players[i] = master_ob->interactive;
all_players[i]->tp = tp;
set_prompt(NULL);
inter->lport = local_port;
set_interactive_address(inter, addr, len);
num_player++;
/*
* The player object has one extra reference.
* It is asserted that the master_ob is loaded.
*/
add_ref(master_ob, "new_player");
ret = apply_master_ob(M_CONNECT, 0);
if (ret == 0 || ret->type != T_OBJECT ||
current_interactive != master_ob)
{
if (master_ob->interactive)
remove_interactive(master_ob->interactive, 0);
current_interactive = 0;
return NULL;
}
/*
* There was an object returned from connect(). Use this as the
* player object.
*/
ob = ret->u.ob;
ob->interactive = master_ob->interactive;
ob->interactive->ob = ob;
ob->flags |= O_ONCE_INTERACTIVE;
master_ob->flags &= ~O_ONCE_INTERACTIVE;
master_ob->interactive = 0;
free_object(master_ob, "reconnect");
add_ref(ob, "new_player");
command_giver = ob;
current_interactive = ob;
#ifdef SUPER_SNOOP
check_supersnoop(ob);
#endif
logon(ob);
current_interactive = 0;
return all_players[i];
}
telnet_output(tp, (u_char *)"Lpmud is full. Come back later.\n");
return NULL;
}
int
call_function_interactive(struct interactive *i, char *str)
{
struct closure *func;
if (!i->input_to)
return 0;
func = i->input_to->funct;
/*
* Special feature: input_to() has been called to setup
* a call to a function.
*/
if (!legal_closure(func))
{
/* Sorry, the object has selfdestructed ! */
free_sentence(i->input_to);
i->input_to = 0;
return 0;
}
INCREF(func->ref);
free_sentence(i->input_to);
/*
* We must clear this reference before the call, because
* someone might want to set up a new input_to().
*/
i->input_to = 0;
push_string(str, STRING_MSTRING);
current_object = func->funobj; /* It seems that some code relies on previous_object having a value, this will ensure that it does. */
(void)call_var(1, func);
free_closure(func);
return 1;
}
int
set_call(struct object *ob, struct sentence *sent, int noecho)
{
struct object *save = command_giver;
if (ob == 0 || sent == 0)
return 0;
if (ob->interactive == 0 || ob->interactive->input_to)
return 0;
ob->interactive->input_to = sent;
ob->interactive->noecho = noecho;
command_giver = ob;
if (noecho)
telnet_enable_echo(ob->interactive->tp);
command_giver = save;
return 1;
}
void
remove_all_players()
{
struct svalue *ret;
struct gdexception exception_frame;
exception_frame.e_exception = NULL;
exception_frame.e_catch = 0;
if (setjmp(exception_frame.e_context))
{
clear_state();
(void)add_message("Anomaly in the fabric of world space.\n");
ret = NULL;
}
else
{
exception = &exception_frame;
eval_cost = 0;
ret = apply_master_ob(M_START_SHUTDOWN, 0);
}
if (ret && ret->type == T_POINTER)
{
volatile int i;
struct vector *unload_vec = ret->u.vec;
INCREF(unload_vec->ref);
for (i = 0; i < unload_vec->size; i++)
if (setjmp(exception_frame.e_context))
{
clear_state();
(void)add_message("Anomaly in the fabric of world space.\n");
}
else
{
exception = &exception_frame;
push_svalue(&(unload_vec->item[i]));
eval_cost = 0;
(void)apply_master_ob(M_CLEANUP_SHUTDOWN, 1);
}
free_vector(unload_vec);
}
if (setjmp(exception_frame.e_context))
{
clear_state();
(void)add_message("Anomaly in the fabric of world space.\n");
}
else
{
exception = &exception_frame;
eval_cost = 0;
ret = apply_master_ob(M_FINAL_SHUTDOWN, 0);
}
exception = NULL;
}
void
set_prompt(char *str)
{
if (str)
command_giver->interactive->prompt = str;
else
command_giver->interactive->prompt = "> ";
}
/*
* Let object 'me' snoop object 'you'. If 'you' is 0, then turn off
* snooping.
*
* This routine is almost identical to the old set_snoop. The main
* difference is that the routine writes nothing to player directly,
* all such communication is taken care of by the mudlib. It communicates
* with master.c in order to find out if the operation is permissble or
* not. The old routine let everyone snoop anyone. This routine also returns
* 0 or 1 depending on success.
*/
int
set_snoop(struct object *me, struct object *you)
{
struct interactive *on = 0, *by = 0, *tmp;
int i;
struct svalue *ret;
extern struct object *master_ob;
/* Stop if people managed to quit before we got this far */
if (me->flags & O_DESTRUCTED)
return 0;
if (you && (you->flags & O_DESTRUCTED))
return 0;
/* Find the snooper & snopee */
for(i = 0 ; i < MAX_PLAYERS && (on == 0 || by == 0); i++)
{
if (all_players[i] == 0)
continue;
if (all_players[i]->ob == me)
by = all_players[i];
else if (all_players[i]->ob == you)
on = all_players[i];
}
/* Check for permissions with valid_snoop in master */
if (current_object != master_ob)
{
push_object(current_object);
push_object(me);
if (you == 0)
push_number(0);
else
push_object(you);
ret = apply_master_ob(M_VALID_SNOOP, 3);
if (ret && (ret->type != T_NUMBER || ret->u.number == 0))
return 0;
}
/* Stop snoop */
if (you == 0)
{
if (by == 0)
error("Could not find snooper to stop snoop on.\n");
if (by->snoop_on == 0)
return 1;
by->snoop_on->snoop_by = 0;
by->snoop_on = 0;
return 1;
}
/* Strange event, but possible, so test for it */
if (on == 0 || by == 0)
return 0;
/* Protect against snooping loops */
for (tmp = on; tmp; tmp = tmp->snoop_on)
{
if (tmp == by)
return 0;
}
/* Terminate previous snoop, if any */
if (by->snoop_on)
{
by->snoop_on->snoop_by = 0;
by->snoop_on = 0;
}
if (on->snoop_by)
{
on->snoop_by->snoop_on = 0;
on->snoop_by = 0;
}
on->snoop_by = by;
by->snoop_on = on;
return 1;
}
char *
query_ip_name(struct object *ob)
{
if (ob == 0)
ob = command_giver;
if (!ob || ob->interactive == 0)
return 0;
if (ob->interactive->host_name)
return ob->interactive->host_name;
return query_ip_number(ob);
}
char *
query_ip_number(struct object *ob)
{
static char host[NI_MAXHOST];
if (ob == 0) {
ob = command_giver;
}
if (!ob || ob->interactive == 0)
return NULL;
if (getnameinfo((struct sockaddr * )&ob->interactive->addr, ob->interactive->addrlen, host, sizeof(host), NULL, 0, NI_NUMERICHOST) != 0)
return NULL;
return host;
}
/*
* Set the ip number for an interactive.
*/
void
set_ip_number(struct object *ob, char *ip)
{
struct addrinfo hints;
struct addrinfo *result, *rp;
if (!ob || ob->interactive == 0) {
error("Attempt to update ip number of non interactive\n");
return;
}
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_NUMERICHOST | AI_NUMERICSERV;
query_port_number(ob);
int err;
if ((err = getaddrinfo(ip, query_port_number(ob), &hints, &result)) != 0) {
error("Invalid address: %s", gai_strerror(err));
}
for (rp = result; rp != NULL; rp = rp->ai_next) {
set_interactive_address(ob->interactive, (struct sockaddr_storage *)rp->ai_addr, rp->ai_addrlen);
memcpy(&ob->interactive->addr, (char *)rp->ai_addr, rp->ai_addrlen);
ob->interactive->addrlen = rp->ai_addrlen;
if (ob->interactive->host_name) {
free(ob->interactive->host_name);
ob->interactive->host_name = NULL;
}
break;
}
freeaddrinfo(rp);
}
char *
query_port_number(struct object *ob)
{
static char port[NI_MAXSERV];
if (ob == 0)
ob = command_giver;
if (!ob || ob->interactive == 0)
return NULL;
if (getnameinfo((struct sockaddr*)&ob->interactive->addr, ob->interactive->addrlen, NULL, 0, port, sizeof(port), NI_NUMERICSERV) != 0)
return NULL;
return port;
}
char *
query_host_name()
{
static char name[64];
(void)gethostname(name, sizeof(name));
name[sizeof(name) - 1] = '\0'; /* Just to make sure */
return name;
}
struct object *
query_snoop(struct object *ob)
{
if (ob->interactive->snoop_by == 0)
return 0;
return ob->interactive->snoop_by->ob;
}
int
query_idle(struct object *ob)
{
if (!ob->interactive)
error("query_idle() of non-interactive object.\n");
return current_time - ob->interactive->last_time;
}
void
notify_no_command()
{
char *p,*m;
extern struct object *vbfc_object;
if (!command_giver || !command_giver->interactive)
return;
p = command_giver->interactive->default_err_message;
if (p)
{
/* We want 'value by function call' */
m = process_string(p, vbfc_object != 0);
if (m) {
(void)add_message("%s", m);
free_mstring(m);
} else
(void)add_message("%s", p);
free_sstring(p);
command_giver->interactive->default_err_message = 0;
}
else
(void)add_message("What?\n");
}
void
clear_notify()
{
if (!command_giver || !command_giver->interactive)
return;
if (command_giver->interactive->default_err_message)
{
free_sstring(command_giver->interactive->default_err_message);
command_giver->interactive->default_err_message = 0;
}
}
void
set_notify_fail_message(char *str, int pri)
{
if (!command_giver || !command_giver->interactive)
return;
if (command_giver->interactive->default_err_message && pri == 0)
return;
clear_notify();
if (command_giver->interactive->default_err_message)
free_sstring(command_giver->interactive->default_err_message);
command_giver->interactive->default_err_message = make_sstring(str);
}
int
replace_interactive(struct object *ob, struct object *obfrom,
/*IGN*/char *name)
{
struct svalue *v;
/*
* Check with master that exec allowed
*/
push_string(name, STRING_MSTRING);
if (ob)
push_object(ob);
else
push_number(0);
push_object(obfrom);
v = apply_master_ob(M_VALID_EXEC, 3);
if (v && (v->type != T_NUMBER || v->u.number == 0))
return 0;
/* (void)fprintf(stderr,"DEBUG: %s,%s\n",ob->name,obfrom->name); */
if (ob && ob->interactive)
error("Bad argument1 to exec(), was an interactive\n");
if (!obfrom->interactive)
error("Bad argument2 to exec(), was not an interactive\n");
if (ob)
{
#ifdef SUPER_SNOOP
if (obfrom->interactive->snoop_fd >= 0) {
(void)close(obfrom->interactive->snoop_fd);
obfrom->interactive->snoop_fd = -1;
}
#endif
ob->interactive = obfrom->interactive;
ob->interactive->ob = ob;
ob->flags |= O_ONCE_INTERACTIVE;
add_ref(ob, "exec");
free_object(obfrom, "exec");
if (current_interactive == obfrom)
current_interactive = ob;
obfrom->interactive = 0;
obfrom->flags &= ~O_ONCE_INTERACTIVE;
#ifdef SUPER_SNOOP
check_supersnoop(ob);
#endif
}
else
remove_interactive(obfrom->interactive, 0);
if (obfrom == command_giver)
command_giver = ob;