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
/
Copy pathclient.c
3067 lines (2930 loc) · 79.3 KB
/
client.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 of client xpas */
static XPA xpaclienthead=NULL;
static char errbuf[SZ_LINE]; /* holds current error message */
static int id=0; /* id of current command */
#define DATA_CONNECT 1
#define DATA_ACCEPT 2
#define DATA_DATA 4
/* use of a double fork() call is used to prevent zombies which happen
if fork is a child of xpans started by an XPA server (i.e., for some
reason, the SIGCHLD signal does not get sent to xpans parent)
See Stevens, Advanced Programming in te Unix Environment, p. 202 */
#define USE_DOUBLE_FORK 1
#ifndef USE_DOUBLE_FORK
#ifdef ANSI_FUNC
void sig_chld(int signo)
#else
#endif
{
int stat;
while(waitpid(-1, &stat, WNOHANG) > 0){
;
}
return;
}
#endif
/*
*----------------------------------------------------------------------------
*
* Routine: rdl
*
* Purpose: read characters up a new-line
*
* Returns: number of characters read
*
*----------------------------------------------------------------------------
*/
#ifdef ANSI_FUNC
static int
rdl (int fd, char *buf, size_t len)
#else
static int rdl(fd, buf, len)
int fd;
char *buf;
int len;
#endif
{
int i=0;
int got;
/* start out clean */
*buf = '\0';
/* make sure we have a valid channel */
if( fd < 0 )
return(-1);
/* grab characters up to a new-line or max len */
while( i < (len-1) ){
got = read(fd, &(buf[i]), 1);
if( got < 1 )
break;
else if( buf[i++] == '\n' )
break;
}
buf[i] = '\0';
return(i);
}
/*
*----------------------------------------------------------------------------
*
* Routine: XPAProxyAccept
*
* Purpose: accept a connection from an XPA proxy server
*
* Return: fd of accepted connection or -1
*
*----------------------------------------------------------------------------
*/
#ifdef ANSI_FUNC
static
int XPAProxyAccept(XPA xpa, char *method, char *xclass, char *name, int ifd,
unsigned int *rip, unsigned short *rport, char *rname)
#else
static
int XPAProxyAccept(xpa, method, xclass, name, ifd, rip, rport, rname)
XPA xpa;
char *method;
char *xclass;
char *name;
int ifd;
unsigned int *rip;
unsigned short *rport;
char *rname;
#endif
{
int sock;
int got;
int oum;
int ofd;
int niter;
int swidth=FD_SETSIZE;
int keep_alive=1;
int reuse_addr=1;
unsigned int ip;
unsigned short port;
char tbuf[SZ_LINE];
char amethod[SZ_LINE];
char *tptr;
socklen_t slen;
struct sockaddr_in sock_in;
#if HAVE_SYS_UN_H
struct sockaddr_un sock_un;
#endif
struct timeval tv;
struct timeval *tvp;
fd_set readfds;
/* initialize results */
if( rip ) *rip = 0;
if( rport ) *rport = 0;
if( rname ) *rname = '\0';
switch(XPAMethod(method)){
case XPA_INET:
if( !XPAParseIpPort(method, &ip, &port) ){
goto error;
}
/* open a socket for data connections */
if( (sock = xsocket(AF_INET, SOCK_STREAM, 0)) < 0 ){
PERROR(("xpaaccept socket"));
goto error;
}
setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE,
(char *)&keep_alive, sizeof(keep_alive));
setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,
(char *)&reuse_addr, sizeof(reuse_addr));
memset((char *)&sock_in, 0, sizeof(sock_in));
sock_in.sin_family = AF_INET;
sock_in.sin_addr.s_addr = htonl(INADDR_ANY);
sock_in.sin_port = htons(port);
/* bind to the ip:port */
if( xbind(sock, (struct sockaddr *)&sock_in, sizeof(sock_in)) < 0 ){
PERROR(("xpaaccept bind"));
xclose(sock);
goto error;
}
snprintf(amethod, SZ_LINE, "%x:%d", ip, port);
break;
#if HAVE_SYS_UN_H
case XPA_UNIX:
ip = 0;
port = 0;
/* get filename part, composed of class and name and unique id */
snprintf(tbuf, SZ_LINE, "%s_%s.%d", xclass, name, (int)time(NULL));
/* change "/" to "_" for filename */
for(tptr = tbuf; *tptr != '\0'; tptr++){
if( *tptr == '/' ) *tptr = '_';
}
/* create full pathname */
snprintf(amethod, SZ_LINE, "%s/%s", XPATmpdir(), tbuf);
/* delete old copy */
unlink (amethod);
/* open a socket and fill in socket information */
if( (sock = xsocket(AF_UNIX, SOCK_STREAM, 0)) < 0 ){
goto error;
}
setsockopt(sock, 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, amethod);
/* unset umask so that everyone can read and write */
oum = umask(0);
/* bind to the file */
got = xbind(sock, (struct sockaddr *)&sock_un, sizeof(sock_un));
/* reset umask to previous */
umask(oum);
/* now check for bind error */
if( got < 0 ){
xclose(sock);
goto error;
}
break;
#endif
default:
goto error;
}
/* send port to client so they can connect */
/* first listen for the connection */
if( listen(sock, XPA_MAXLISTEN) < 0 ){
PERROR(("xpaaccept listen"));
xclose(sock);
goto error;
}
/* and tell the client that we are listening */
snprintf(tbuf, SZ_LINE, "xpaaccept %s (%s:%s %s)\n",
amethod, xclass, name, method);
FPRINTF((stderr, "%sXPAProxyAccept: sending command to %d:\n%s",
_sp, ifd, tbuf));
if( XPAPuts(NULL, ifd, tbuf, XPAShortTimeout()) <= 0 ){
PERROR(("client xpaaccept write"));
xclose(sock);
goto error;
}
/* we will iterate on xselect */
if( XPAShortTimeout() > 0 )
niter = XPAShortTimeout()*100;
else
niter = XPA_SHORT_TIMEOUT*100;
again:
/* this has to be able to time out */
tv.tv_sec = 0;
tv.tv_usec = 10000;
tvp = &tv;
/* wait for this socket and XPA sockets */
FD_ZERO(&readfds);
FD_SET(sock, &readfds);
XPAAddSelect(NULL, &readfds);
/* wait for the connection */
got = xselect(swidth, &readfds, NULL, NULL, tvp);
/* process results of select */
if( got > 0 ){
if( !FD_ISSET(sock, &readfds)){
XPAProcessSelect(&readfds, 0);
goto again;
}
switch(XPAMethod(method)){
case XPA_INET:
while( 1 ){
slen = sizeof(struct sockaddr_in);
if((ofd=xaccept(sock, (struct sockaddr *)&sock_in, &slen)) >= 0){
break;
}
else{
if( xerrno == EINTR )
continue;
else{
PERROR(("xpaaccept acccept"));
xclose(sock);
goto error;
}
}
}
break;
#if HAVE_SYS_UN_H
case XPA_UNIX:
while( 1 ){
slen = sizeof(struct sockaddr_un);
if((ofd=xaccept(sock, (struct sockaddr *)&sock_un, &slen)) >= 0){
break;
}
else{
if( xerrno == EINTR )
continue;
else{
PERROR(("xpaaccept acccept"));
xclose(sock);
goto error;
}
}
}
break;
#endif
default:
xclose(sock);
goto error;
}
}
/* timeout? */
else if( got == 0 ){
if( --niter > 0 ){
goto again;
}
else{
xclose(sock);
FPRINTF((stderr, "%sXPAProxyAccept: select timed out\n", _sp));
goto error;
}
}
/* error */
else{
if( xerrno == EINTR ){
PERROR(("xpaaccept select"));
goto again;
}
else{
xclose(sock);
goto error;
}
}
/* done with listening */
xclose(sock);
/* fill in return information */
if( rip ) *rip = ip;
if( rport ) *rport = port;
if( rname ){
strncpy(rname, amethod, SZ_LINE-1);
rname[SZ_LINE-1] = '\0';
}
return(ofd);
error:
return(-1);
}
/*
*----------------------------------------------------------------------------
*
* Routine: XPAClientNewInput
*
* Purpose: allocate a new input struct for reading data from stdin
*
* Return: input struct, or NULL on error
*
*----------------------------------------------------------------------------
*/
#ifdef ANSI_FUNC
static XPAInput
XPAClientNewInput(XPA xpa)
#else
static XPAInput XPAClientNewInput(xpa)
XPA xpa;
#endif
{
XPAInput xnew, inp;
/* allocate a new record */
if( (xnew=(XPAInput)xcalloc(1, sizeof(XPAInputRec))) == NULL ){
return(NULL);
}
/* allocate the data buffer */
xnew->buf = (char *)xmalloc(XPA_BIOSIZE);
/* this buffer starts (and currently ends) at the current byte count */
xnew->start = xpa->inpbytes;
xnew->end = xpa->inpbytes;
xnew->bytes = 0;
/* add this input to end of list of input's */
if( xpa->inphead == NULL ){
xpa->inphead = xnew;
}
else{
for(inp=xpa->inphead; inp->next!=NULL; inp=inp->next)
;
inp->next = xnew;
}
/* return the record struct */
return(xnew);
}
/*
*----------------------------------------------------------------------------
*
* Routine: XPAClientFreeInput
*
* Purpose: free a input buffer once its been sent to all targets
*
* Return: 0 on success, -1 on failure
*
*----------------------------------------------------------------------------
*/
#ifdef ANSI_FUNC
static void
XPAClientFreeInput (XPA xpa, XPAInput inp)
#else
static void XPAClientFreeInput(xpa, inp)
XPA xpa;
XPAInput inp;
#endif
{
XPAInput cur;
if( !xpa || !inp )
return;
if( inp == xpa->inphead ){
xpa->inphead = inp->next;
}
else{
for(cur=xpa->inphead; cur!=NULL; cur=cur->next){
if( cur->next == inp ){
cur->next = inp->next;
break;
}
}
}
/* free current record */
if( inp != NULL ){
if( inp->buf != NULL )
xfree(inp->buf );
xfree(inp);
}
}
/*
*----------------------------------------------------------------------------
*
* Routine: XPAClientFreeAllInputs
*
* Purpose: free remaining input buffers
*
* Return: 0 on success, -1 on failure
*
*----------------------------------------------------------------------------
*/
#ifdef ANSI_FUNC
static void
XPAClientFreeAllInputs (XPA xpa)
#else
static void XPAClientFreeAllInputs(xpa)
XPA xpa;
#endif
{
XPAInput cur, tmp;
if( !xpa )
return;
for(cur=xpa->inphead; cur!=NULL; ){
tmp = cur->next;
XPAClientFreeInput(xpa, cur);
cur = tmp;
}
xpa->inpbytes = 0;
}
/*
*----------------------------------------------------------------------------
*
* Routine: XPAClientProcessInput
*
* Purpose: read input from stdin and store in an input struct
*
* Return: bytes read
*
*----------------------------------------------------------------------------
*/
#ifdef ANSI_FUNC
static int
XPAClientProcessInput(XPA xpa)
#else
static int XPAClientProcessInput(xpa)
XPA xpa;
#endif
{
static XPAInput cur=NULL;
int get, got;
/* set up next buffer, if necessary */
for(cur=xpa->inphead; cur!=NULL; cur=cur->next){
if( cur->bytes < XPA_BIOSIZE )
break;
}
if( cur == NULL ){
cur = XPAClientNewInput(xpa);
}
/* read data from stdin */
get = MIN(XPA_IOSIZE, XPA_BIOSIZE - cur->bytes);
if( isatty(xpa->ifd) ){
got = rdl(xpa->ifd, &(cur->buf[cur->bytes]), get);
}
else{
got = read(xpa->ifd, &(cur->buf[cur->bytes]), get);
}
switch(got){
case -1:
if( XPAVerbosity() ){
PERROR(("XPA client read"));
}
return(0);
case 0:
xpa->ifd = -1;
FPRINTF((stderr, "%sXPAClientProcessInput: signalling EOF\n", _sp));
break;
default:
break;
}
cur->bytes += got;
cur->end += got;
xpa->inpbytes += got;
#ifdef FIXEDBYCYGWIN
#if HAVE_CYGWIN
/* on non-NT Windows machines, Cygwin select() does not work once a pipe
gets EOF. It should show the fd ready for reading (and read 0 bytes),
but does not, so we have to hack a manual check */
/* GetVersion is a Windows call */
if( GetVersion() >= 0x80000000L ){
if( got < get ){
xpa->ifd = -1;
}
}
#endif
#endif
/* verify to stdout, if necessary */
if( xpa->client_mode & XPA_CLIENT_VERIFY ){
fwrite(&(cur->buf[cur->bytes-got]), sizeof(char), got, stdout);
}
/* return the number of bytes just read */
return(got);
}
/*
*----------------------------------------------------------------------------
*
* Routine: XPAClientFree
*
* Purpose: free a client record and remove from list
*
* Returns: none
*
*----------------------------------------------------------------------------
*/
#ifdef ANSI_FUNC
static void
XPAClientFree (XPA xpa, XPAClient client)
#else
static void XPAClientFree(xpa, client)
XPA xpa;
XPAClient client;
#endif
{
XPAClient cur;
/* remove from list of xpa's */
if( xpa->clienthead ){
if( xpa->clienthead == client ){
xpa->clienthead = client->next;
}
else{
for(cur=xpa->clienthead; cur!=NULL; cur=cur->next){
if( cur->next == client ){
cur->next = client->next;
break;
}
}
}
}
if( client->cmdfd >= 0 ){
#if HAVE_CYGWIN
shutdown(client->cmdfd, SHUT_RDWR);
#endif
xclose(client->cmdfd);
}
if( client->datafd >= 0 ){
#if HAVE_CYGWIN
shutdown(client->datafd, SHUT_RDWR);
#endif
xclose(client->datafd);
}
if( client->dataname ){
unlink(client->dataname);
xfree(client->dataname);
}
if( client->method )
xfree(client->method);
if( client->info )
xfree(client->info);
if( client->xtemplate )
xfree(client->xtemplate);
if( client->xclass )
xfree(client->xclass);
if( client->name )
xfree(client->name);
if( client->id )
xfree(client->id);
/* xpaget's fd mode has an alloc'ed bufptr and lenptr */
if( (client->type == 'g') && (client->mode & XPA_CLIENT_FD) ){
if( client->bufptr && *(client->bufptr) )
xfree(*(client->bufptr));
if( client->bufptr )
xfree(client->bufptr);
if( client->lenptr )
xfree(client->lenptr);
}
xfree(client);
}
/*
*----------------------------------------------------------------------------
*
* Routine: XPAClientDataSent
*
* Purpose: data is sent, so close data channel and change status to
* signal that we are waiting for the server
*
* Returns: none
*
*----------------------------------------------------------------------------
*/
#ifdef ANSI_FUNC
static void
XPAClientDataSent (XPA xpa, XPAClient client)
#else
static void XPAClientDataSent(xpa, client)
XPA xpa;
XPAClient client;
#endif
{
FPRINTF((stderr, "%sXPAClientDataSent: for cmd %d data %d\n", _sp,
client->cmdfd, client->datafd));
/* close the data channel, which should trigger a result from the server */
if( client->datafd >= 0 ){
#if HAVE_CYGWIN
shutdown(client->datafd, SHUT_RDWR);
#endif
xclose(client->datafd);
client->datafd = -1;
}
/* we are now waiting for the server to complete the calllback */
client->status = XPA_CLIENT_WAITING;
}
/*
*----------------------------------------------------------------------------
*
* Routine: XPAClientEnd
*
* Purpose: finish up with this client
*
* Returns: error message or null
*
*----------------------------------------------------------------------------
*/
#ifdef ANSI_FUNC
static char *
XPAClientEnd (XPA xpa, XPAClient client)
#else
static char *XPAClientEnd(xpa, client)
XPA xpa;
XPAClient client;
#endif
{
char *error=NULL;
char *eptr;
FPRINTF((stderr, "%sXPAClientEnd: for cmd %d data %d\n", _sp,
client->cmdfd, client->datafd));
/* always read the status line -- if we are not ack'ing, we'll get an
OK from the server before the calllback and we can exit quickly */
/* don't do this if client is xpainfo and we're not ack'ing */
if( !((client->type == 'i') && !(client->mode & XPA_CLIENT_ACK)) ){
retry:
if( XPAGets(NULL, client->cmdfd, errbuf, SZ_LINE, XPALongTimeout()) >0 ){
FPRINTF((stderr, "%sXPAClientEnd: read %s\n", _sp, errbuf));
eptr = errbuf;
/* this should never happen */
if( *eptr == '?' ){
snprintf(errbuf, SZ_LINE,
"XPA$WARNING: protocol mismatch - missing id\n%s", eptr);
error = NULL;
}
else{
/* make sure we are dealing with a proper message */
if( strncmp(eptr, client->id, strlen(client->id)) ){
if( XPAVerbosity() > 1 ){
fprintf(stderr,
"XPA$WARNING: ignoring out of sync server message:\n");
fprintf(stderr, "%s", errbuf);
}
goto retry;
}
/* go past id */
eptr += strlen(client->id);
while( isspace((int)*eptr) ) eptr++;
if( !strncmp(eptr, "XPA$OK", 6) ){
error = NULL;
}
else{
error = eptr;
}
}
}
else{
if( XPAVerbosity() > 1 ){
fprintf(stderr,
"XPA$WARNING: no reply from server callback (assuming OK)\n");
}
error = NULL;
}
}
else
error = NULL;
/* store the error return */
if( client->errptr )
*(client->errptr) = xstrdup(error);
/* remove this client if we are not meant to persist */
if( !xpa->persist ){
XPAClientFree(xpa, client);
}
/* otherwise mark as inactive */
else{
client->status = XPA_CLIENT_IDLE;
client->bytes = 0;
}
/* return error status */
return(error);
}
/*
*----------------------------------------------------------------------------
*
* Routine: XPAClientNew
*
* Purpose: allocate a new xpa client
*
* Returns: xpa client struct
*
*----------------------------------------------------------------------------
*/
#ifdef ANSI_FUNC
static XPAClient
XPAClientNew (XPA xpa, char *mode, char *xtemplate, int type,
char *xclass, char *name, char *method, char *info)
#else
static XPAClient XPAClientNew(xpa, mode, xtemplate, type,
xclass, name, method, info)
XPA xpa;
char *mode;
char *xtemplate;
int type;
char *xclass;
char *name;
char *method;
char *info;
#endif
{
XPAClient xnew, client;
struct sockaddr_in sock_in;
#if HAVE_SYS_UN_H
struct sockaddr_un sock_un;
#endif
char xmode[SZ_LINE];
char tbuf[SZ_LINE];
char amethod[SZ_LINE];
char *s=NULL;
unsigned short port;
unsigned int ip=0;
int fd;
int pfd;
int tries=0;
int nsproxy=0;
int keep_alive=1;
FPRINTF((stderr, "%sXPAClientNew: entering with %s %s %s %s\n", _sp,
xclass, name, method, info));
/* no errors as yet */
*errbuf = '\0';
/* look for reuse of xpans fd (used in conjunction with the xpans proxy) */
*xmode = '\0';
if( mode ){
strncpy(xmode, mode, SZ_LINE-1);
xmode[SZ_LINE-1] = '\0';
}
if( keyword(xmode, "nsproxy", tbuf, SZ_LINE) ){
nsproxy = 1;
pfd = strtol(tbuf, &s, 0);
fd = XPAProxyAccept(xpa, XPANSMethod(NULL,2),
xclass, name, pfd, &ip, &port, amethod);
/* make sure we got a valid int fd */
if( fd < 0 ){
snprintf(errbuf, SZ_LINE,
"XPA$ERROR: no response from server on proxyaccept (%s:%s%s)\n",
xclass, name, XPATimestamp());
FPRINTF((stderr, "%sXPAClientNew: %s", _sp, errbuf));
PERROR(("XPAClientNew"));
return(NULL);
}
}
/* normal usage: connect to server */
else{
switch(XPAMethod(method)){
case XPA_INET:
again1:
if( !XPAParseIpPort(method, &ip, &port) )
return(NULL);
/* use $localhost over $host (we do not trust host to be correct) */
if( (ip == gethostip("$host")) && (tries == 0) )
ip = gethostip("$localhost");
/* connect to the server before we go further */
if( (fd = xsocket(AF_INET, SOCK_STREAM, 0)) < 0 ){
return(NULL);
}
setsockopt(fd, 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(fd, (struct sockaddr *)&sock_in, sizeof(sock_in)) <0 ){
xclose(fd);
/* 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{
snprintf(errbuf, SZ_LINE,
"XPA$ERROR: no response from server on connect (%s:%s%s)\n",
xclass, name, XPATimestamp());
PERROR(("XPAClientNew"));
return(NULL);
}
}
/* make sure we close on exec */
xfcntl(fd, F_SETFD, FD_CLOEXEC);
FPRINTF((stderr, "%sXPAClientNew: inet connect returns fd %d\n",
_sp, fd));
break;
#if HAVE_SYS_UN_H
case XPA_UNIX:
again2:
/* open a socket and fill in socket information */
if( (fd = xsocket(AF_UNIX, SOCK_STREAM, 0)) < 0 ){
return(NULL);
}
setsockopt(fd, 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(fd, (struct sockaddr *)&sock_un, sizeof(sock_un)) <0 ){
xclose(fd);
/* 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 < XPA_RETRIES) ){
tries++;
XPASleep(10);
goto again2;
}
/* give up */
else{
snprintf(errbuf, SZ_LINE,
"XPA$ERROR: no response from server on connect (%s:%s%s)\n",
xclass, name, XPATimestamp());
PERROR(("XPAClientNew"));
return(NULL);
}
}
/* make sure we close on exec */
xfcntl(fd, F_SETFD, FD_CLOEXEC);
FPRINTF((stderr, "%sXPAClientNew: unix connect returns fd %d\n",
_sp, fd));
break;
#endif
default:
return(NULL);
}
strncpy(amethod, method, SZ_LINE-1);
amethod[SZ_LINE-1] = '\0';
}
/* allocate new send record */
if( (xnew=(XPAClient)xcalloc(1, sizeof(XPAClientRec))) == NULL ){
xclose(fd);
return(NULL);
}
/* fill in the blanks */
xnew->xtemplate = xstrdup(xtemplate);
xnew->type = type;
xnew->cmdfd = fd;
xnew->datafd = -1;
xnew->xclass = xstrdup(xclass);
xnew->name = xstrdup(name);
xnew->method = xstrdup(amethod);
xnew->info = xstrdup(info);
xnew->ip = ip;
xnew->nsproxy = nsproxy;
xnew->status = XPA_CLIENT_ACTIVE;
/* now that we have a valid client, add to list */
if( xpa->clienthead == NULL ){
xpa->clienthead = xnew;
}
else{
for(client=xpa->clienthead; client->next!=NULL; client=client->next)
;
client->next = xnew;
}
FPRINTF((stderr, "%sXPAClientNew: new fd %d\n", _sp, xnew->cmdfd));
/* return the good news */
return(xnew);
}
/*
*----------------------------------------------------------------------------
*
* Routine: XPAClientAddSelect
*
* Purpose: add one or more xpa client sockets to the select flags
*
* Return: number of clients that were added to the select flags
*
*----------------------------------------------------------------------------
*/
#ifdef ANSI_FUNC
int
XPAClientAddSelect (XPA xpa, fd_set *readfdsptr, fd_set *writefdsptr)
#else
int XPAClientAddSelect(xpa, readfdsptr, writefdsptr)
XPA xpa;
fd_set *readfdsptr;
fd_set *writefdsptr;
#endif
{
XPAClient client;
int got=0;
int loop=0;
/* better have some place to set the flags */
if( readfdsptr == NULL )
return(0);
/* if no xpa is specified, do them all */
if( xpa == NULL ){
if( xpaclienthead == NULL ) return(0);
xpa = xpaclienthead;
loop = 1;
}
loop:
/* set select flags for all clients */
for(client=xpa->clienthead; client!=NULL; client=client->next){
/* if this client is processing */
if( (client->status == XPA_CLIENT_PROCESSING) && (client->datafd >= 0) ){
if( client->type == 'g' ){
FPRINTF((stderr, "%sXPAClientAddSelect(get): adding fd %d\n", _sp,
client->datafd));
FD_SET(client->datafd, readfdsptr);
got++;
}
else if( client->type == 's' ){
FPRINTF((stderr, "%sXPAClientAddSelect(set): adding fd %d\n", _sp,
client->datafd));
FD_SET(client->datafd, writefdsptr);
got++;
}
}
/* if this client is waiting */
else if( (client->status == XPA_CLIENT_WAITING) && (client->cmdfd >= 0) ){
FPRINTF((stderr, "%sXPAClientAddSelect(waiting): adding fd %d\n", _sp,
client->cmdfd));
FD_SET(client->cmdfd, readfdsptr);
got++;
}
}
/* loop if necessary */
if( loop && (xpa=xpa->next) ) goto loop;
/* return the news */
return(got);
}
/*
*----------------------------------------------------------------------------
*
* Routine: XPAClientGet
*
* Purpose: process an xpaget request for a given client