This repository has been archived by the owner on Jan 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
xpa.c
4720 lines (4450 loc) · 115 KB
/
xpa.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
/*
* Copyright (c) 1999-2003 Smithsonian Astrophysical Observatory
*/
#include <xpap.h>
/*
*----------------------------------------------------------------------------
*
*
* Private Routines and Data
*
*
*----------------------------------------------------------------------------
*/
/* this is the head of the global list -- too lazy to do anything more */
static XPA xpahead=NULL;
/* globals for the xpa environment, controlled by environment variables */
static int stimeout=XPA_SHORT_TIMEOUT; /* select, gets timeout in secs */
static int ltimeout=XPA_LONG_TIMEOUT; /* fillbuf timeout in secs */
static int ctimeout=XPA_CONNECT_TIMEOUT;/* local xpans connect */
static int verbosity=XPA_VERBOSITY; /* 0=quiet, 1=normal, 2=full */
static int nsdelay=XPA_NSDELAY; /* delay increment waiting for ns to start */
static int retries=XPA_RETRIES; /* retries when retrying */
static int guseacl=1; /* 0=don't use acls, 1=enable acls */
static int etimestamp=0; /* 0=don't timestamp errors, 1=do timestamp */
static int nsregister=1; /* 0=don't register with xpans, 1=register */
static int sigusr1=1; /* 0=don't use sigusr1, 1=enable sigusr1 */
static int vercheck=1; /* 0=don't check version, 1=check version */
static char *tmpdir=NULL; /* temporary dir for logs. etc. */
#if HAVE_ATEXIT
static int atexitinit=0; /* whether atexit has been registered */
#endif
/* variables used by all XPAs in this process */
static char activefds[FD_SETSIZE];
static char nsmethod[SZ_LINE];
static char nsusers[SZ_LINE];
/* global method type: unix or inet */
static int mtype=0;
/* width of select channel flag */
static int swidth=FD_SETSIZE;
/* defined in tcp.c */
extern int use_localhost;
/* erro code strings -- must match the error codes in xpap.h */
char *xpaMessbuf[] = {
"oh, what a mess we've made!",
"authentication failed",
"xpa connection refused",
"can't resolve host name for xpa",
"can't read initialization info for xpa",
"invalid xpa command in initialization string",
"no receive function defined for this xpa",
"no send function defined for this xpa",
"no info function defined for this xpa",
"undefined command for this xpa",
"missing command for this xpa",
"name does not match target template",
"can't create data channel socket",
"could not read data buf (possible timeout)",
"illegal command or command switch"
};
/* temp static time buffer */
static char ctimebuf[SZ_LINE];
/*
*----------------------------------------------------------------------------
*
* Routine: XPAProxyConnect
*
* Purpose: connect to a client (in proxy mode)
*
* Returns: 0 if successfull, otherwise -1
*
*----------------------------------------------------------------------------
*/
#ifdef ANSI_FUNC
static int
XPAProxyConnect(XPA xpa, char *method,
unsigned int *rip, unsigned short *rport, char *rname)
#else
static int XPAProxyConnect(xpa, method, rip, rport, rname)
XPA xpa;
char *method;
unsigned int *rip;
unsigned short *rport;
char *rname;
#endif
{
int ofd;
int tries=0;
int keep_alive=1;
unsigned int ip;
unsigned short port;
struct sockaddr_in sock_in;
#if HAVE_SYS_UN_H
struct sockaddr_un sock_un;
#endif
FPRINTF((stderr, "%sXPAProxyConnect to %s\n", _sp, method));
/* initialize results */
if( rip ) *rip = 0;
if( rport ) *rport = 0;
if( rname ) *rname = '\0';
switch(XPAMethod(method)){
case XPA_INET:
again1:
if( !XPAParseIpPort(method, &ip, &port) ){
goto error;
}
/* use $localhost over $host (we don't trust host to be correct) */
if( (ip == gethostip("$host")) && (tries == 0) ){
ip = gethostip("$localhost");
}
/* connect to the server before we go further */
if( (ofd = xsocket(AF_INET, SOCK_STREAM, 0)) < 0 ){
PERROR(("XPAProxyConnect: socket()"));
goto error;
}
setsockopt(ofd, SOL_SOCKET, SO_KEEPALIVE,
(char *)&keep_alive, sizeof(keep_alive));
memset((char *)&sock_in, 0, sizeof(sock_in));
sock_in.sin_family = AF_INET;
sock_in.sin_addr.s_addr = htonl(ip);
sock_in.sin_port = htons(port);
/* make the connection with the server */
if(connect(ofd, (struct sockaddr *)&sock_in, sizeof(sock_in))<0){
xclose(ofd);
/* if localhost doesn't work, make one try with the host ip */
/* we also try again just in case there was an odd error such
as "permission denied", which we have seen once or twice */
if( tries < 2 ){
tries++;
goto again1;
}
/* give up */
else{
PERROR(("XPAProxyConnect: connect()"));
goto error;
}
}
break;
#if HAVE_SYS_UN_H
case XPA_UNIX:
again2:
ip = 0;
port = 0;
/* open a socket and fill in socket information */
if( (ofd = xsocket(AF_UNIX, SOCK_STREAM, 0)) < 0 ){
goto error;
}
setsockopt(ofd, SOL_SOCKET, SO_KEEPALIVE,
(char *)&keep_alive, sizeof(keep_alive));
memset((char *)&sock_un, 0, sizeof(sock_un));
sock_un.sun_family = AF_UNIX;
strcpy(sock_un.sun_path, method);
/* make the connection with the server */
if(connect(ofd, (struct sockaddr *)&sock_un, sizeof(sock_un))<0){
xclose(ofd);
/* Unix sockets get ECONNREFUSED when the listen queue is full,
so we try a few times to give the server a chance to recover */
if( (xerrno == ECONNREFUSED) && (tries < retries) ){
tries++;
XPASleep(10);
goto again2;
}
/* give up */
else{
goto error;
}
}
break;
#endif
default:
goto error;
}
/* fill in blansk */
if( rip ) *rip = ip;
if( rport ) *rport = port;
if( rname ){
strncpy(rname, method, SZ_LINE-1);
rname[SZ_LINE-1] = '\0';
}
return(ofd);
error:
return -1;
}
/*
*----------------------------------------------------------------------------
*
* Routine: CommNew
*
* Purpose: allocate a new comm record and add to end of list
*
* Returns: allocated comm record
*
*----------------------------------------------------------------------------
*/
#ifdef ANSI_FUNC
static XPAComm
CommNew (XPA xpa, int fd, unsigned int ip, int port, char *name, NS ns)
#else
static XPAComm CommNew(xpa, fd, ip, port, name, ns)
XPA xpa;
int fd;
unsigned int ip;
int port;
char *name;
NS ns;
#endif
{
XPAComm comm, cur;
int i;
socklen_t slen;
struct sockaddr_in sock_in;
#if HAVE_SYS_UN_H
struct sockaddr_un sock_un;
#endif
/* create the comm struct */
if( (comm = (XPAComm)xcalloc(1, sizeof(XPACommRec))) == NULL )
return(NULL);
/* if fd < 0, we accept a connection to get the fd */
if( fd < 0 ){
/* accept the connection */
switch(mtype){
case XPA_INET:
while( 1 ){
slen = sizeof(struct sockaddr_in);
if((comm->cmdfd=xaccept(xpa->fd,(struct sockaddr *)&sock_in,&slen))>=0){
comm->cmdip = ntohl(sock_in.sin_addr.s_addr);
comm->cmdport = ntohs(sock_in.sin_port);
break;
}
else{
if( xerrno == EINTR )
continue;
else{
xfree(comm);
return(NULL);
}
}
}
break;
#if HAVE_SYS_UN_H
case XPA_UNIX:
while( 1 ){
slen = sizeof(struct sockaddr_un);
if((comm->cmdfd=xaccept(xpa->fd,(struct sockaddr *)&sock_un,&slen))>=0){
comm->cmdname = xstrdup(sock_un.sun_path);
break;
}
else{
if( xerrno == EINTR )
continue;
else{
xfree(comm);
return(NULL);
}
}
}
break;
#endif
default:
xfree(comm);
return(NULL);
}
}
/* all info is supplied */
else{
switch(mtype){
case XPA_INET:
comm->cmdip = ip;
comm->cmdport = port;
break;
case XPA_UNIX:
comm->cmdname = xstrdup(name);
break;
}
comm->cmdfd = fd;
/* store name server record */
comm->ns = ns;
}
/* set back pointer */
/* make sure we close on exec */
xfcntl(comm->cmdfd, F_SETFD, FD_CLOEXEC);
/* mark data socket with impossible value */
comm->datafd = -1;
/* default is to ack */
comm->ack = 1;
/* set default cendian flag */
comm->cendian = "?";
/* clear the acl flags */
for(i=0; i<XPA_CMDS+1; i++){
comm->acl[i] = -1;
}
/* add this comm to end of list of comms */
if( xpa->commhead == NULL ){
xpa->commhead = comm;
}
else{
for(cur=xpa->commhead; cur->next!=NULL; cur=cur->next){
;
}
cur->next = comm;
}
/* we might have to add this fd specially to a non-select event loop */
if( xpa->seladd )
comm->selcptr = (xpa->seladd)(xpa, comm->cmdfd);
/* make this fd active */
XPAActive(xpa, comm, 1);
FPRINTF((stderr, "%sCommNew: ip=%x port=%d fd=%d\n", _sp,
comm->cmdip, comm->cmdport, comm->cmdfd));
/* return the good news */
return(comm);
}
/*
*----------------------------------------------------------------------------
*
* Routine: CommFree
*
* Purpose: free a comm record and remove from list
*
* Returns: none
*
*----------------------------------------------------------------------------
*/
#ifdef ANSI_FUNC
static void
CommFree (XPA xpa, XPAComm comm, int flag)
#else
static void CommFree(xpa, comm, flag)
XPA xpa;
XPAComm comm;
int flag;
#endif
{
XPAComm cur;
if( !comm )
return;
FPRINTF((stderr, "%sCommFree: ip=%x port=%d fd=%d dfd=%d\n", _sp,
comm->cmdip, comm->cmdport, comm->cmdfd, comm->datafd));
/* remove from list of this xpa's comms */
if( xpa ){
if( xpa->commhead ){
if( xpa->commhead == comm ){
xpa->commhead = comm->next;
}
else{
for(cur=xpa->commhead; cur!=NULL; cur=cur->next){
if( cur->next == comm ){
cur->next = comm->next;
break;
}
}
}
}
}
/* must check all xpas */
else{
for(xpa=xpahead; xpa!=NULL; xpa=xpa->next){
if( xpa->commhead ){
if( xpa->commhead == comm ){
xpa->commhead = comm->next;
}
else{
for(cur=xpa->commhead; cur!=NULL; cur=cur->next){
if( cur->next == comm ){
cur->next = comm->next;
break;
}
}
}
}
}
}
/* close socket connections */
if( flag && (comm->cmdfd >= 0) ){
FPRINTF((stderr, "%sCommFree closing cmd fd: %d\n", _sp, comm->cmdfd));
/* remove from active */
if( comm->cmdfd < FD_SETSIZE )
activefds[comm->cmdfd] = 0;
/* delete the comm cmd fd specially from a non-select event loop */
if( xpa && xpa->seldel && comm->selcptr ){
(xpa->seldel)(comm->selcptr);
comm->selcptr = NULL;
}
/* close file */
xclose(comm->cmdfd);
}
/* close data channel */
XPACloseData(xpa, comm);
/* if we have a file name (unix sockets), free it */
if( comm->cmdname != NULL ){
unlink(comm->cmdname);
xfree(comm->cmdname);
}
if( comm->dataname != NULL ){
unlink(comm->dataname);
xfree(comm->dataname);
}
/* free up the space */
if( comm->id != NULL ) xfree(comm->id);
if( comm->info != NULL ) xfree(comm->info);
if( comm->target != NULL ) xfree(comm->target);
if( comm->paramlist != NULL ) xfree(comm->paramlist);
/* this comm is no longer associated with an ns */
if( comm->ns ){
comm->ns->nproxy -= 1;
}
/* disassociate from parent xpa */
if( xpa ){
xpa->comm = NULL;
}
/* free up structure */
xfree(comm);
}
/*
*----------------------------------------------------------------------------
*
* Routine: XPANSOpen
*
* Purpose: open a connection to the name service
*
* Returns: connection fd on success, -1 on failure
*
*----------------------------------------------------------------------------
*/
#ifdef ANSI_FUNC
static NS
XPANSOpen (XPA xpa, char *host, int force)
#else
static NS XPANSOpen(xpa, host, force)
XPA xpa;
char *host;
int force;
#endif
{
int i;
int status;
int xnsfd=0;
int keep_alive=1;
int tries=0;
int ip=0;
int dowarn=0;
int contmode=XPA_CONNECT_TIMEOUT_MODE;
unsigned short xnsport=0;
unsigned int xnsip=0;
static int errinit=0;
char *s;
char *path;
char *method;
char nscmd[SZ_LINE];
char tbuf[SZ_LINE];
char tbuf2[SZ_LINE];
struct sockaddr_in sock_in;
struct passwd *pw;
#if HAVE_SYS_UN_H
struct sockaddr_un sock_un;
#endif
char *findname=NULL;
NS ns, cur;
/* get name server method */
method = XPANSMethod(host, 0);
/* if the name service already is open, just return fd */
if( xpa ){
for(ns=xpa->nshead; ns!=NULL; ns=ns->next){
if( !strcmp(ns->method, method) ){
/* if forcing, see if the connection is valid */
if( force >= 0 ){
if( (XPAPuts(xpa, ns->fd, "status\n", stimeout) >0) &&
(XPAGets(xpa, ns->fd, tbuf, SZ_LINE, stimeout) >0) &&
!strncmp(tbuf, "XPA$OK", 6) ){
FPRINTF((stderr, "%sXPANSOpen: found existing ns: %s\n",
_sp, ns->method));
return ns;
}
/* found the ns, but its not open */
else{
FPRINTF((stderr, "%sXPANSOpen: existing ns is dead: %s\n",
_sp, ns->method));
XPANSClose(xpa, ns);
break;
}
}
/* just return whatever we have if we are not forcing */
else{
return ns;
}
}
}
}
/* if no existing ns and flag is negative, we are done */
if( force == -1 )
return(NULL);
/* we always make up the command afresh */
*nscmd = '\0';
/* get users for this user */
if( (s=(char *)getenv("XPA_NSUSERS")) != NULL )
strncpy(nsusers, s, SZ_LINE-1);
/* default is just this use's userrname, from the environment */
else if( (s=(char *)getenv("LOGNAME")) != NULL )
strncpy(nsusers, s, SZ_LINE-1);
#if HAVE_MINGW32==0
/* this is a last resort */
else if( (pw=getpwuid(geteuid())) )
strncpy(nsusers, pw->pw_name, SZ_LINE-1);
#endif
/* if nothing good has happened, make it "all" */
if( *nsusers == '\0' )
strcpy(nsusers, "*");
/* null-terminate string */
nsusers[SZ_LINE-1] = '\0';
/* set up communications socket for this method */
switch(mtype){
case XPA_INET:
again1:
XPAParseIpPort(method, &xnsip, &xnsport);
/* use $localhost over $host (we do not trust host to be correct) */
if( (xnsip == gethostip("$host")) && (tries == 0) ){
xnsip = gethostip("$localhost");
}
if( xnsip == 0 ){
fprintf(stderr,
"XPA$ERROR: invalid host name specified: %s.\n", method);
return(NULL);
}
if( (xnsfd = xsocket(AF_INET, SOCK_STREAM, 0)) < 0 )
goto nons;
memset((char *)&sock_in, 0, sizeof(sock_in));
sock_in.sin_family = AF_INET;
sock_in.sin_addr.s_addr = htonl(xnsip);
sock_in.sin_port = htons(xnsport);
/* try connecting to the name server */
if( ctimeout <= 0 ) contmode = 0;
switch(contmode){
case 1:
#if HAVE_MINGW32==0
status=alrmconnect(xnsfd, (void *)&sock_in, sizeof(sock_in), ctimeout);
#else
status=noblkconnect(xnsfd, (void *)&sock_in, sizeof(sock_in), ctimeout);
#endif
break;
case 2:
status=noblkconnect(xnsfd, (void *)&sock_in, sizeof(sock_in), ctimeout);
break;
default:
status=connect(xnsfd, (struct sockaddr *)&sock_in, sizeof(sock_in));
break;
}
if( status == 0 ){
FPRINTF((stderr, "%sXPANSOpen: connect to xpans\n", _sp));
goto okns;
}
else{
xclose(xnsfd);
/* if localhost doesn't work, make one try with the host ip */
if( (xerrno == ECONNREFUSED) && (tries < 1) ){
tries++;
goto again1;
}
}
break;
#if HAVE_SYS_UN_H
case XPA_UNIX:
again2:
/* open a socket and fill in socket information */
if( (xnsfd = xsocket(AF_UNIX, SOCK_STREAM, 0)) < 0 )
goto nons;
memset((char *)&sock_un, 0, sizeof(sock_un));
sock_un.sun_family = AF_UNIX;
strcpy(sock_un.sun_path, method);
xnsip = 0;
/* try connecting to the name server */
status=connect(xnsfd, (struct sockaddr *)&sock_un, sizeof(sock_un));
if( status == 0 ){
FPRINTF((stderr, "%sXPANSOpen: connect to xpans\n", _sp));
goto okns;
}
else{
xclose(xnsfd);
/* Unix sockets get ECONNREFUSED when the listen queue is full,
so we try a few times to give the server a chance to recover */
if( (xerrno == ECONNREFUSED) && (tries < retries) ){
tries++;
XPASleep(10);
goto again2;
}
}
break;
#endif
}
/* if force is set, we try to start up the name server */
if( force == 0 )
goto noforce;
/* make up the xpans command we will use */
if( *nscmd == '\0' ){
if( (mtype == XPA_UNIX) || LOCALIP(xnsip) ){
path = (char *)getenv("PATH");
findname = (char *)Find(XPANSNAME, "x", NULL, path);
#if HAVE_CYGWIN
/* this will help start up xpans under Windows */
if( !findname ) findname = (char *)Find(XPANSNAME, "x", NULL, ".");
#endif
}
if( findname != NULL ){
switch(mtype){
case XPA_INET:
#if USE_LAUNCH
/* change spaces to special launch space */
for(i=0; i<strlen(findname); i++){
if( findname[i] == ' '){
findname[i] = LAUNCH_SPACE;
}
}
snprintf(nscmd, SZ_LINE, "%s -e -p %d -l %s/xpans_%d.log",
findname, xnsport, tmpdir, xnsport);
#else
snprintf(nscmd, SZ_LINE, "\"%s\" -e -p %d -l %s/xpans_%d.log &\n",
findname, xnsport, tmpdir, xnsport);
#endif
break;
#if HAVE_SYS_UN_H
case XPA_UNIX:
#if USE_LAUNCH
/* change spaces to special launch space */
for(i=0; i<strlen(findname); i++){
if( findname[i] == ' '){
findname[i] = LAUNCH_SPACE;
}
}
snprintf(nscmd, SZ_LINE, "%s -e -f %s -l %s.log",
findname, method, method);
#else
snprintf(nscmd, SZ_LINE, "\"%s\" -e -f %s -l %s.log &\n",
findname, method, method);
#endif
break;
#endif
}
}
else{
*nscmd = '\0';
}
}
/* did not find the name server -- start it up if we can, i.e.,
if its on the same machine as we are on and we found it in the path */
if((*nscmd != '\0') && ((mtype == XPA_UNIX) || LOCALIP(xnsip)) ){
FPRINTF((stderr, "%sLaunching: %s\n", _sp, nscmd));
#if USE_LAUNCH
if( Launch(nscmd, 0, NULL, NULL) != 0 )
goto nons;
#else
if( system(nscmd) != 0 )
goto nons;
#endif
/* enter loop looking to connect */
for(tries=0; tries<retries; tries++){
switch(mtype){
case XPA_INET:
/* use $localhost alternately with $host */
if( (xnsip == gethostip("$host")) && (tries % 2 == 0) ){
xnsip = gethostip("$localhost");
}
if( xnsip == 0 ){
fprintf(stderr,
"XPA$ERROR: invalid host name specified: %s.\n", method);
return(NULL);
}
if( (xnsfd = xsocket(AF_INET, SOCK_STREAM, 0)) < 0 )
goto nons;
memset((char *)&sock_in, 0, sizeof(sock_in));
sock_in.sin_family = AF_INET;
sock_in.sin_addr.s_addr = htonl(xnsip);
sock_in.sin_port = htons(xnsport);
FPRINTF((stderr, "%sXPANSOPEN: attempting connect to %x\n",
_sp, xnsip));
if(connect(xnsfd, (struct sockaddr *)&sock_in, sizeof(sock_in)) ==0)
goto okns;
break;
#if HAVE_SYS_UN_H
case XPA_UNIX:
if( (xnsfd = xsocket(AF_UNIX, SOCK_STREAM, 0)) < 0 )
goto nons;
if(connect(xnsfd, (struct sockaddr *)&sock_un, sizeof(sock_un)) ==0)
goto okns;
break;
#endif
}
xclose(xnsfd);
XPASleep(nsdelay);
}
/* if we got here, we did not connect */
goto nons;
}
/* name server is on a remote machine, so there is no hope */
else{
goto nons;
}
okns:
/* make sure we close on exec */
xfcntl(xnsfd, F_SETFD, FD_CLOEXEC);
setsockopt(xnsfd, SOL_SOCKET, SO_KEEPALIVE,
(char *)&keep_alive, sizeof(keep_alive));
/* fill in new record */
if( (ns = (NS)xcalloc(1, sizeof(NSRec))) == NULL ){
xclose(xnsfd);
return NULL;
}
ns->method = xstrdup(method);
ns->host = xstrdup(host);
ns->fd = xnsfd;
FPRINTF((stderr, "%sXPANSOpen: host %s opened on fd %d\n", _sp,
host?host:"<unknown>", xnsfd));
switch(mtype){
case XPA_INET:
ns->ip = xnsip;
ns->port = xnsport;
break;
#if HAVE_SYS_UN_H
case XPA_UNIX:
ns->name = xstrdup(method);
break;
#endif
}
if( xpa ){
/* add to list of name servers */
if( xpa->nshead == NULL ){
xpa->nshead = ns;
}
else{
for(cur=xpa->nshead; cur->next!=NULL; cur=cur->next)
;
cur->next = ns;
}
}
/* check version with xpans */
snprintf(tbuf, SZ_LINE, "version %s\n", XPA_VERSION);
if( (XPAPuts(xpa, ns->fd, tbuf, stimeout) >0) &&
(XPAGets(xpa, ns->fd, tbuf, SZ_LINE, stimeout) >0) ){
if( word(tbuf, tbuf2, &ip) ){
if( !strcmp(tbuf2, "XPA$VERSION") ){
if( word(tbuf, tbuf2, &ip) ){
/* version check: our server version should be <= xpans version */
dowarn = (XPAVersionCheck(XPA_VERSION, tbuf2)>0);
}
else{
strcpy(tbuf2, "unknown/pre-2.1 (noversion)");
dowarn = 1;
}
}
else{
strcpy(tbuf2, "unknown/pre-2.1 (badformat)");
dowarn = 1;
}
}
FPRINTF((stderr, "%sXPANSOpen: version info: %s\n", _sp, tbuf));
if( dowarn ){
XPAVersionWarn(XPA_VERSION, tbuf2);
}
}
/* clean up */
if( findname != NULL ) xfree(findname);
return(ns);
nons:
/* if we specified an explicit port, we don't need the name server,
so don't bother with any warning or error messages */
if( XPAPort(xpa) >0 )
return NULL;
switch(mtype){
case XPA_INET:
if( !errinit && verbosity ){
if( LOCALIP(xnsip) ){
fprintf(stderr,
"XPA$WARNING: xpans needs to be running on this machine.\n");
}
else{
fprintf(stderr,
"XPA$WARNING: xpans needs to be running on machine: ");
fprintf(stderr, "%s\n", getiphost(xnsip));
}
}
break;
#if HAVE_SYS_UN_H
case XPA_UNIX:
if( !errinit && verbosity ){
fprintf(stderr,
"XPA$WARNING: xpans needs to be running on this machine.\n");
}
break;
#endif
default:
break;
}
if( xpa && verbosity ){
if( !errinit ){
/* make up the command users will need to start xpans */
if( *nscmd == '\0' ){
switch(mtype){
case XPA_INET:
snprintf(nscmd, SZ_LINE, "xpans -e -p %d -l %s/xpans_%d.log",
xnsport, tmpdir, xnsport);
break;
#if HAVE_SYS_UN_H
case XPA_UNIX:
snprintf(nscmd, SZ_LINE, "xpans -e -f %s -l %s.log", method, method);
break;
#endif
}
}
fprintf(stderr, "Please start xpans using the command:\n\t%s\n", nscmd);
fprintf(stderr, "Once xpans is running, register all xpas in this process using:\n");
fprintf(stderr, "\txpaset -p %s -nsconnect\n", xpa->method);
}
fprintf(stderr, "For now, contact %s using:", xpa->name);
fprintf(stderr, " xpaset %s .. or xpaget %s ..\n",
xpa->method, xpa->method);
}
errinit++;
noforce:
if( findname != NULL ) xfree(findname);
return(NULL);
}
/*
*---------------------------------------------------------------------------
*
* Routine: _XPAFree
*
* Purpose: free up memory in the XPA record structure
* (internal version)
*
* Results: 0 on success, -1 for failure
*
*---------------------------------------------------------------------------
*/
#ifdef ANSI_FUNC
static int
_XPAFree (XPA xpa)
#else
static int _XPAFree(xpa)
XPA xpa;
#endif
{
char tbuf[SZ_LINE];
XPACmd cmd, tcmd;
XPAComm comm, tcomm;
XPAClip clip, tclip;
NS ns, tns;
/* make sure we have something to do */
if( xpa == NULL )
return(-1);
FPRINTF((stderr, "%s_XPAFree: freeing xpa struct\n", _sp));
/* remove this xpa from public availability */
if( xpa->type != NULL )
XPANSDel(xpa, NULL, NULL);
/* free all sub-commands */
for(cmd=xpa->commands; cmd!=NULL; ){
tcmd = cmd->next;
XPACmdDel(xpa, cmd);
cmd = tcmd;
}
/* remove from list of xpas */
XPAListDel(&xpahead, xpa);
/* close down listening socket */
if( xpa->fd >= 0 )
xclose(xpa->fd);
/* perform method-specific cleanup */
switch(mtype){
#if HAVE_SYS_UN_H
case XPA_UNIX:
/* delete the unix socket files */
unlink(xpa->method);
snprintf(tbuf, SZ_LINE, "%s_data", xpa->method);
unlink(tbuf);
break;
#endif
default:
break;
}
/* free up space */
if( xpa->version ) xfree(xpa->version);
if( xpa->type ) xfree(xpa->type);
if( xpa->method ) xfree(xpa->method);
if( xpa->xclass ) xfree(xpa->xclass);
if( xpa->name ) xfree(xpa->name);
if( xpa->help ) xfree(xpa->help);
if( xpa->sendian ) xfree(xpa->sendian);
/* call the select free routine for the listening socket and loop type.
we use an indirect routine to avoid having to link Xt, Tcl, etc. */
if( xpa->seldel && xpa->selptr ){
(xpa->seldel)(xpa->selptr);
xpa->selptr = NULL;
}
/* close communication channels */
for(comm=xpa->commhead; comm!=NULL; ){
tcomm = comm->next;
CommFree(xpa, comm, 1);
comm = tcomm;
}
/* free up clipboards */
for(clip=xpa->cliphead; clip!=NULL; ){
tclip = clip->next;
ClipBoardFree(xpa, clip);
clip = tclip;
}
/* close down the name server and all of the remotes for this xpa */
for(ns=xpa->nshead; ns!=NULL; ){
tns = ns->next;
XPANSClose(xpa, ns);
ns = tns;
}
/* free up record struct */
xfree((char *)xpa);
return(0);
}
#if HAVE_ATEXIT
/*
*---------------------------------------------------------------------------
*
* Routine: _XPAAtExit
*
* Purpose: XPA cleanup on exit
* The main purpose of this routine is to try to delete the
* unix socket files when an XPA server exits.
*
* Results: none
*
*---------------------------------------------------------------------------
*/
#ifdef ANSI_FUNC
static void
_XPAAtExit (void)
#else
static void _XPAAtExit()
#endif
{
XPA xpa, txpa;
static int done=0;
if( !done ){
/* return if we were not initialized */
if( !atexitinit ) return;
/* return if I am not the process who initialized (I'm a child) */