This repository has been archived by the owner on Apr 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 95
/
uxnet.c
1650 lines (1476 loc) · 40.4 KB
/
uxnet.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
/*
* Unix networking abstraction.
*/
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <netdb.h>
#include <sys/un.h>
#include <pwd.h>
#include <grp.h>
#define DEFINE_PLUG_METHOD_MACROS
#include "putty.h"
#include "network.h"
#include "tree234.h"
/* Solaris needs <sys/sockio.h> for SIOCATMARK. */
#ifndef SIOCATMARK
#include <sys/sockio.h>
#endif
#ifndef X11_UNIX_PATH
# define X11_UNIX_PATH "/tmp/.X11-unix/X"
#endif
/*
* Access to sockaddr types without breaking C strict aliasing rules.
*/
union sockaddr_union {
struct sockaddr_storage storage;
struct sockaddr sa;
struct sockaddr_in sin;
#ifndef NO_IPV6
struct sockaddr_in6 sin6;
#endif
struct sockaddr_un su;
};
/*
* We used to typedef struct Socket_tag *Socket.
*
* Since we have made the networking abstraction slightly more
* abstract, Socket no longer means a tcp socket (it could mean
* an ssl socket). So now we must use Actual_Socket when we know
* we are talking about a tcp socket.
*/
typedef struct Socket_tag *Actual_Socket;
/*
* Mutable state that goes with a SockAddr: stores information
* about where in the list of candidate IP(v*) addresses we've
* currently got to.
*/
typedef struct SockAddrStep_tag SockAddrStep;
struct SockAddrStep_tag {
#ifndef NO_IPV6
struct addrinfo *ai; /* steps along addr->ais */
#endif
int curraddr;
};
struct Socket_tag {
struct socket_function_table *fn;
/* the above variable absolutely *must* be the first in this structure */
const char *error;
int s;
Plug plug;
bufchain output_data;
int connected; /* irrelevant for listening sockets */
int writable;
int frozen; /* this causes readability notifications to be ignored */
int localhost_only; /* for listening sockets */
char oobdata[1];
int sending_oob;
int oobpending; /* is there OOB data available to read? */
int oobinline;
enum { EOF_NO, EOF_PENDING, EOF_SENT } outgoingeof;
int incomingeof;
int pending_error; /* in case send() returns error */
int listener;
int nodelay, keepalive; /* for connect()-type sockets */
int privport, port; /* and again */
SockAddr addr;
SockAddrStep step;
/*
* We sometimes need pairs of Socket structures to be linked:
* if we are listening on the same IPv6 and v4 port, for
* example. So here we define `parent' and `child' pointers to
* track this link.
*/
Actual_Socket parent, child;
};
struct SockAddr_tag {
int refcount;
const char *error;
enum { UNRESOLVED, UNIX, IP } superfamily;
#ifndef NO_IPV6
struct addrinfo *ais; /* Addresses IPv6 style. */
#else
unsigned long *addresses; /* Addresses IPv4 style. */
int naddresses;
#endif
char hostname[512]; /* Store an unresolved host name. */
};
/*
* Which address family this address belongs to. AF_INET for IPv4;
* AF_INET6 for IPv6; AF_UNSPEC indicates that name resolution has
* not been done and a simple host name is held in this SockAddr
* structure.
*/
#ifndef NO_IPV6
#define SOCKADDR_FAMILY(addr, step) \
((addr)->superfamily == UNRESOLVED ? AF_UNSPEC : \
(addr)->superfamily == UNIX ? AF_UNIX : \
(step).ai ? (step).ai->ai_family : AF_INET)
#else
/* Here we gratuitously reference 'step' to avoid gcc warnings about
* 'set but not used' when compiling -DNO_IPV6 */
#define SOCKADDR_FAMILY(addr, step) \
((addr)->superfamily == UNRESOLVED ? AF_UNSPEC : \
(addr)->superfamily == UNIX ? AF_UNIX : \
(step).curraddr ? AF_INET : AF_INET)
#endif
/*
* Start a SockAddrStep structure to step through multiple
* addresses.
*/
#ifndef NO_IPV6
#define START_STEP(addr, step) \
((step).ai = (addr)->ais, (step).curraddr = 0)
#else
#define START_STEP(addr, step) \
((step).curraddr = 0)
#endif
static tree234 *sktree;
static void uxsel_tell(Actual_Socket s);
static int cmpfortree(void *av, void *bv)
{
Actual_Socket a = (Actual_Socket) av, b = (Actual_Socket) bv;
int as = a->s, bs = b->s;
if (as < bs)
return -1;
if (as > bs)
return +1;
if (a < b)
return -1;
if (a > b)
return +1;
return 0;
}
static int cmpforsearch(void *av, void *bv)
{
Actual_Socket b = (Actual_Socket) bv;
int as = *(int *)av, bs = b->s;
if (as < bs)
return -1;
if (as > bs)
return +1;
return 0;
}
void sk_init(void)
{
sktree = newtree234(cmpfortree);
}
void sk_cleanup(void)
{
Actual_Socket s;
int i;
if (sktree) {
for (i = 0; (s = index234(sktree, i)) != NULL; i++) {
close(s->s);
}
}
}
SockAddr sk_namelookup(const char *host, char **canonicalname, int address_family)
{
SockAddr ret = snew(struct SockAddr_tag);
#ifndef NO_IPV6
struct addrinfo hints;
int err;
#else
unsigned long a;
struct hostent *h = NULL;
int n;
#endif
char realhost[8192];
/* Clear the structure and default to IPv4. */
memset(ret, 0, sizeof(struct SockAddr_tag));
ret->superfamily = UNRESOLVED;
*realhost = '\0';
ret->error = NULL;
ret->refcount = 1;
#ifndef NO_IPV6
hints.ai_flags = AI_CANONNAME;
hints.ai_family = (address_family == ADDRTYPE_IPV4 ? AF_INET :
address_family == ADDRTYPE_IPV6 ? AF_INET6 :
AF_UNSPEC);
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = 0;
hints.ai_addrlen = 0;
hints.ai_addr = NULL;
hints.ai_canonname = NULL;
hints.ai_next = NULL;
{
char *trimmed_host = host_strduptrim(host); /* strip [] on literals */
err = getaddrinfo(trimmed_host, NULL, &hints, &ret->ais);
sfree(trimmed_host);
}
if (err != 0) {
ret->error = gai_strerror(err);
return ret;
}
ret->superfamily = IP;
*realhost = '\0';
if (ret->ais->ai_canonname != NULL)
strncat(realhost, ret->ais->ai_canonname, sizeof(realhost) - 1);
else
strncat(realhost, host, sizeof(realhost) - 1);
#else
if ((a = inet_addr(host)) == (unsigned long)(in_addr_t)(-1)) {
/*
* Otherwise use the IPv4-only gethostbyname... (NOTE:
* we don't use gethostbyname as a fallback!)
*/
if (ret->superfamily == UNRESOLVED) {
/*debug(("Resolving \"%s\" with gethostbyname() (IPv4 only)...\n", host)); */
if ( (h = gethostbyname(host)) )
ret->superfamily = IP;
}
if (ret->superfamily == UNRESOLVED) {
ret->error = (h_errno == HOST_NOT_FOUND ||
h_errno == NO_DATA ||
h_errno == NO_ADDRESS ? "Host does not exist" :
h_errno == TRY_AGAIN ?
"Temporary name service failure" :
"gethostbyname: unknown error");
return ret;
}
/* This way we are always sure the h->h_name is valid :) */
strncpy(realhost, h->h_name, sizeof(realhost));
for (n = 0; h->h_addr_list[n]; n++);
ret->addresses = snewn(n, unsigned long);
ret->naddresses = n;
for (n = 0; n < ret->naddresses; n++) {
memcpy(&a, h->h_addr_list[n], sizeof(a));
ret->addresses[n] = ntohl(a);
}
} else {
/*
* This must be a numeric IPv4 address because it caused a
* success return from inet_addr.
*/
ret->superfamily = IP;
strncpy(realhost, host, sizeof(realhost));
ret->addresses = snew(unsigned long);
ret->naddresses = 1;
ret->addresses[0] = ntohl(a);
}
#endif
realhost[lenof(realhost)-1] = '\0';
*canonicalname = snewn(1+strlen(realhost), char);
strcpy(*canonicalname, realhost);
return ret;
}
SockAddr sk_nonamelookup(const char *host)
{
SockAddr ret = snew(struct SockAddr_tag);
ret->error = NULL;
ret->superfamily = UNRESOLVED;
strncpy(ret->hostname, host, lenof(ret->hostname));
ret->hostname[lenof(ret->hostname)-1] = '\0';
#ifndef NO_IPV6
ret->ais = NULL;
#else
ret->addresses = NULL;
#endif
ret->refcount = 1;
return ret;
}
static int sk_nextaddr(SockAddr addr, SockAddrStep *step)
{
#ifndef NO_IPV6
if (step->ai && step->ai->ai_next) {
step->ai = step->ai->ai_next;
return TRUE;
} else
return FALSE;
#else
if (step->curraddr+1 < addr->naddresses) {
step->curraddr++;
return TRUE;
} else {
return FALSE;
}
#endif
}
void sk_getaddr(SockAddr addr, char *buf, int buflen)
{
if (addr->superfamily == UNRESOLVED || addr->superfamily == UNIX) {
strncpy(buf, addr->hostname, buflen);
buf[buflen-1] = '\0';
} else {
#ifndef NO_IPV6
if (getnameinfo(addr->ais->ai_addr, addr->ais->ai_addrlen, buf, buflen,
NULL, 0, NI_NUMERICHOST) != 0) {
buf[0] = '\0';
strncat(buf, "<unknown>", buflen - 1);
}
#else
struct in_addr a;
SockAddrStep step;
START_STEP(addr, step);
assert(SOCKADDR_FAMILY(addr, step) == AF_INET);
a.s_addr = htonl(addr->addresses[0]);
strncpy(buf, inet_ntoa(a), buflen);
buf[buflen-1] = '\0';
#endif
}
}
int sk_addr_needs_port(SockAddr addr)
{
if (addr->superfamily == UNRESOLVED || addr->superfamily == UNIX) {
return FALSE;
} else {
return TRUE;
}
}
int sk_hostname_is_local(const char *name)
{
return !strcmp(name, "localhost") ||
!strcmp(name, "::1") ||
!strncmp(name, "127.", 4);
}
#define ipv4_is_loopback(addr) \
(((addr).s_addr & htonl(0xff000000)) == htonl(0x7f000000))
static int sockaddr_is_loopback(struct sockaddr *sa)
{
union sockaddr_union *u = (union sockaddr_union *)sa;
switch (u->sa.sa_family) {
case AF_INET:
return ipv4_is_loopback(u->sin.sin_addr);
#ifndef NO_IPV6
case AF_INET6:
return IN6_IS_ADDR_LOOPBACK(&u->sin6.sin6_addr);
#endif
case AF_UNIX:
return TRUE;
default:
return FALSE;
}
}
int sk_address_is_local(SockAddr addr)
{
if (addr->superfamily == UNRESOLVED)
return 0; /* we don't know; assume not */
else if (addr->superfamily == UNIX)
return 1;
else {
#ifndef NO_IPV6
return sockaddr_is_loopback(addr->ais->ai_addr);
#else
struct in_addr a;
SockAddrStep step;
START_STEP(addr, step);
assert(SOCKADDR_FAMILY(addr, step) == AF_INET);
a.s_addr = htonl(addr->addresses[0]);
return ipv4_is_loopback(a);
#endif
}
}
int sk_address_is_special_local(SockAddr addr)
{
return addr->superfamily == UNIX;
}
int sk_addrtype(SockAddr addr)
{
SockAddrStep step;
int family;
START_STEP(addr, step);
family = SOCKADDR_FAMILY(addr, step);
return (family == AF_INET ? ADDRTYPE_IPV4 :
#ifndef NO_IPV6
family == AF_INET6 ? ADDRTYPE_IPV6 :
#endif
ADDRTYPE_NAME);
}
void sk_addrcopy(SockAddr addr, char *buf)
{
SockAddrStep step;
int family;
START_STEP(addr, step);
family = SOCKADDR_FAMILY(addr, step);
#ifndef NO_IPV6
if (family == AF_INET)
memcpy(buf, &((struct sockaddr_in *)step.ai->ai_addr)->sin_addr,
sizeof(struct in_addr));
else if (family == AF_INET6)
memcpy(buf, &((struct sockaddr_in6 *)step.ai->ai_addr)->sin6_addr,
sizeof(struct in6_addr));
else
assert(FALSE);
#else
struct in_addr a;
assert(family == AF_INET);
a.s_addr = htonl(addr->addresses[step.curraddr]);
memcpy(buf, (char*) &a.s_addr, 4);
#endif
}
void sk_addr_free(SockAddr addr)
{
if (--addr->refcount > 0)
return;
#ifndef NO_IPV6
if (addr->ais != NULL)
freeaddrinfo(addr->ais);
#else
sfree(addr->addresses);
#endif
sfree(addr);
}
SockAddr sk_addr_dup(SockAddr addr)
{
addr->refcount++;
return addr;
}
static Plug sk_tcp_plug(Socket sock, Plug p)
{
Actual_Socket s = (Actual_Socket) sock;
Plug ret = s->plug;
if (p)
s->plug = p;
return ret;
}
static void sk_tcp_flush(Socket s)
{
/*
* We send data to the socket as soon as we can anyway,
* so we don't need to do anything here. :-)
*/
}
static void sk_tcp_close(Socket s);
static int sk_tcp_write(Socket s, const char *data, int len);
static int sk_tcp_write_oob(Socket s, const char *data, int len);
static void sk_tcp_write_eof(Socket s);
static void sk_tcp_set_frozen(Socket s, int is_frozen);
static char *sk_tcp_peer_info(Socket s);
static const char *sk_tcp_socket_error(Socket s);
static struct socket_function_table tcp_fn_table = {
sk_tcp_plug,
sk_tcp_close,
sk_tcp_write,
sk_tcp_write_oob,
sk_tcp_write_eof,
sk_tcp_flush,
sk_tcp_set_frozen,
sk_tcp_socket_error,
sk_tcp_peer_info,
};
static Socket sk_tcp_accept(accept_ctx_t ctx, Plug plug)
{
int sockfd = ctx.i;
Actual_Socket ret;
/*
* Create Socket structure.
*/
ret = snew(struct Socket_tag);
ret->fn = &tcp_fn_table;
ret->error = NULL;
ret->plug = plug;
bufchain_init(&ret->output_data);
ret->writable = 1; /* to start with */
ret->sending_oob = 0;
ret->frozen = 1;
ret->localhost_only = 0; /* unused, but best init anyway */
ret->pending_error = 0;
ret->oobpending = FALSE;
ret->outgoingeof = EOF_NO;
ret->incomingeof = FALSE;
ret->listener = 0;
ret->parent = ret->child = NULL;
ret->addr = NULL;
ret->connected = 1;
ret->s = sockfd;
if (ret->s < 0) {
ret->error = strerror(errno);
return (Socket) ret;
}
ret->oobinline = 0;
uxsel_tell(ret);
add234(sktree, ret);
return (Socket) ret;
}
static int try_connect(Actual_Socket sock)
{
int s;
union sockaddr_union u;
const union sockaddr_union *sa;
int err = 0;
short localport;
int salen, family;
/*
* Remove the socket from the tree before we overwrite its
* internal socket id, because that forms part of the tree's
* sorting criterion. We'll add it back before exiting this
* function, whether we changed anything or not.
*/
del234(sktree, sock);
if (sock->s >= 0)
close(sock->s);
plug_log(sock->plug, 0, sock->addr, sock->port, NULL, 0);
/*
* Open socket.
*/
family = SOCKADDR_FAMILY(sock->addr, sock->step);
assert(family != AF_UNSPEC);
s = socket(family, SOCK_STREAM, 0);
sock->s = s;
if (s < 0) {
err = errno;
goto ret;
}
cloexec(s);
if (sock->oobinline) {
int b = TRUE;
if (setsockopt(s, SOL_SOCKET, SO_OOBINLINE,
(void *) &b, sizeof(b)) < 0) {
err = errno;
close(s);
goto ret;
}
}
if (sock->nodelay) {
int b = TRUE;
if (setsockopt(s, IPPROTO_TCP, TCP_NODELAY,
(void *) &b, sizeof(b)) < 0) {
err = errno;
close(s);
goto ret;
}
}
if (sock->keepalive) {
int b = TRUE;
if (setsockopt(s, SOL_SOCKET, SO_KEEPALIVE,
(void *) &b, sizeof(b)) < 0) {
err = errno;
close(s);
goto ret;
}
}
/*
* Bind to local address.
*/
if (sock->privport)
localport = 1023; /* count from 1023 downwards */
else
localport = 0; /* just use port 0 (ie kernel picks) */
/* BSD IP stacks need sockaddr_in zeroed before filling in */
memset(&u,'\0',sizeof(u));
/* We don't try to bind to a local address for UNIX domain sockets. (Why
* do we bother doing the bind when localport == 0 anyway?) */
if (family != AF_UNIX) {
/* Loop round trying to bind */
while (1) {
int retcode;
#ifndef NO_IPV6
if (family == AF_INET6) {
/* XXX use getaddrinfo to get a local address? */
u.sin6.sin6_family = AF_INET6;
u.sin6.sin6_addr = in6addr_any;
u.sin6.sin6_port = htons(localport);
retcode = bind(s, &u.sa, sizeof(u.sin6));
} else
#endif
{
assert(family == AF_INET);
u.sin.sin_family = AF_INET;
u.sin.sin_addr.s_addr = htonl(INADDR_ANY);
u.sin.sin_port = htons(localport);
retcode = bind(s, &u.sa, sizeof(u.sin));
}
if (retcode >= 0) {
err = 0;
break; /* done */
} else {
err = errno;
if (err != EADDRINUSE) /* failed, for a bad reason */
break;
}
if (localport == 0)
break; /* we're only looping once */
localport--;
if (localport == 0)
break; /* we might have got to the end */
}
if (err)
goto ret;
}
/*
* Connect to remote address.
*/
switch(family) {
#ifndef NO_IPV6
case AF_INET:
/* XXX would be better to have got getaddrinfo() to fill in the port. */
((struct sockaddr_in *)sock->step.ai->ai_addr)->sin_port =
htons(sock->port);
sa = (const union sockaddr_union *)sock->step.ai->ai_addr;
salen = sock->step.ai->ai_addrlen;
break;
case AF_INET6:
((struct sockaddr_in *)sock->step.ai->ai_addr)->sin_port =
htons(sock->port);
sa = (const union sockaddr_union *)sock->step.ai->ai_addr;
salen = sock->step.ai->ai_addrlen;
break;
#else
case AF_INET:
u.sin.sin_family = AF_INET;
u.sin.sin_addr.s_addr = htonl(sock->addr->addresses[sock->step.curraddr]);
u.sin.sin_port = htons((short) sock->port);
sa = &u;
salen = sizeof u.sin;
break;
#endif
case AF_UNIX:
assert(sock->port == 0); /* to catch confused people */
assert(strlen(sock->addr->hostname) < sizeof u.su.sun_path);
u.su.sun_family = AF_UNIX;
strcpy(u.su.sun_path, sock->addr->hostname);
sa = &u;
salen = sizeof u.su;
break;
default:
assert(0 && "unknown address family");
exit(1); /* XXX: GCC doesn't understand assert() on some systems. */
}
nonblock(s);
if ((connect(s, &(sa->sa), salen)) < 0) {
if ( errno != EINPROGRESS ) {
err = errno;
goto ret;
}
} else {
/*
* If we _don't_ get EWOULDBLOCK, the connect has completed
* and we should set the socket as connected and writable.
*/
sock->connected = 1;
sock->writable = 1;
}
uxsel_tell(sock);
ret:
/*
* No matter what happened, put the socket back in the tree.
*/
add234(sktree, sock);
if (err)
plug_log(sock->plug, 1, sock->addr, sock->port, strerror(err), err);
return err;
}
Socket sk_new(SockAddr addr, int port, int privport, int oobinline,
int nodelay, int keepalive, Plug plug)
{
Actual_Socket ret;
int err;
/*
* Create Socket structure.
*/
ret = snew(struct Socket_tag);
ret->fn = &tcp_fn_table;
ret->error = NULL;
ret->plug = plug;
bufchain_init(&ret->output_data);
ret->connected = 0; /* to start with */
ret->writable = 0; /* to start with */
ret->sending_oob = 0;
ret->frozen = 0;
ret->localhost_only = 0; /* unused, but best init anyway */
ret->pending_error = 0;
ret->parent = ret->child = NULL;
ret->oobpending = FALSE;
ret->outgoingeof = EOF_NO;
ret->incomingeof = FALSE;
ret->listener = 0;
ret->addr = addr;
START_STEP(ret->addr, ret->step);
ret->s = -1;
ret->oobinline = oobinline;
ret->nodelay = nodelay;
ret->keepalive = keepalive;
ret->privport = privport;
ret->port = port;
err = 0;
do {
err = try_connect(ret);
} while (err && sk_nextaddr(ret->addr, &ret->step));
if (err)
ret->error = strerror(err);
return (Socket) ret;
}
Socket sk_newlistener(const char *srcaddr, int port, Plug plug,
int local_host_only, int orig_address_family)
{
int s;
#ifndef NO_IPV6
struct addrinfo hints, *ai = NULL;
char portstr[6];
#endif
union sockaddr_union u;
union sockaddr_union *addr;
int addrlen;
Actual_Socket ret;
int retcode;
int address_family;
int on = 1;
/*
* Create Socket structure.
*/
ret = snew(struct Socket_tag);
ret->fn = &tcp_fn_table;
ret->error = NULL;
ret->plug = plug;
bufchain_init(&ret->output_data);
ret->writable = 0; /* to start with */
ret->sending_oob = 0;
ret->frozen = 0;
ret->localhost_only = local_host_only;
ret->pending_error = 0;
ret->parent = ret->child = NULL;
ret->oobpending = FALSE;
ret->outgoingeof = EOF_NO;
ret->incomingeof = FALSE;
ret->listener = 1;
ret->addr = NULL;
ret->s = -1;
/*
* Translate address_family from platform-independent constants
* into local reality.
*/
address_family = (orig_address_family == ADDRTYPE_IPV4 ? AF_INET :
#ifndef NO_IPV6
orig_address_family == ADDRTYPE_IPV6 ? AF_INET6 :
#endif
AF_UNSPEC);
#ifndef NO_IPV6
/* Let's default to IPv6.
* If the stack doesn't support IPv6, we will fall back to IPv4. */
if (address_family == AF_UNSPEC) address_family = AF_INET6;
#else
/* No other choice, default to IPv4 */
if (address_family == AF_UNSPEC) address_family = AF_INET;
#endif
/*
* Open socket.
*/
s = socket(address_family, SOCK_STREAM, 0);
#ifndef NO_IPV6
/* If the host doesn't support IPv6 try fallback to IPv4. */
if (s < 0 && address_family == AF_INET6) {
address_family = AF_INET;
s = socket(address_family, SOCK_STREAM, 0);
}
#endif
if (s < 0) {
ret->error = strerror(errno);
return (Socket) ret;
}
cloexec(s);
ret->oobinline = 0;
if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR,
(const char *)&on, sizeof(on)) < 0) {
ret->error = strerror(errno);
close(s);
return (Socket) ret;
}
retcode = -1;
addr = NULL; addrlen = -1; /* placate optimiser */
if (srcaddr != NULL) {
#ifndef NO_IPV6
hints.ai_flags = AI_NUMERICHOST;
hints.ai_family = address_family;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = 0;
hints.ai_addrlen = 0;
hints.ai_addr = NULL;
hints.ai_canonname = NULL;
hints.ai_next = NULL;
assert(port >= 0 && port <= 99999);
sprintf(portstr, "%d", port);
{
char *trimmed_addr = host_strduptrim(srcaddr);
retcode = getaddrinfo(trimmed_addr, portstr, &hints, &ai);
sfree(trimmed_addr);
}
if (retcode == 0) {
addr = (union sockaddr_union *)ai->ai_addr;
addrlen = ai->ai_addrlen;
}
#else
memset(&u,'\0',sizeof u);
u.sin.sin_family = AF_INET;
u.sin.sin_port = htons(port);
u.sin.sin_addr.s_addr = inet_addr(srcaddr);
if (u.sin.sin_addr.s_addr != (in_addr_t)(-1)) {
/* Override localhost_only with specified listen addr. */
ret->localhost_only = ipv4_is_loopback(u.sin.sin_addr);
}
addr = &u;
addrlen = sizeof(u.sin);
retcode = 0;
#endif
}
if (retcode != 0) {
memset(&u,'\0',sizeof u);
#ifndef NO_IPV6
if (address_family == AF_INET6) {
u.sin6.sin6_family = AF_INET6;
u.sin6.sin6_port = htons(port);
if (local_host_only)
u.sin6.sin6_addr = in6addr_loopback;
else
u.sin6.sin6_addr = in6addr_any;
addr = &u;
addrlen = sizeof(u.sin6);
} else
#endif
{
u.sin.sin_family = AF_INET;
u.sin.sin_port = htons(port);
if (local_host_only)
u.sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
else
u.sin.sin_addr.s_addr = htonl(INADDR_ANY);
addr = &u;
addrlen = sizeof(u.sin);
}
}
retcode = bind(s, &addr->sa, addrlen);
#ifndef NO_IPV6
if (ai)
freeaddrinfo(ai);
#endif
if (retcode < 0) {
close(s);
ret->error = strerror(errno);
return (Socket) ret;
}
if (listen(s, SOMAXCONN) < 0) {
close(s);
ret->error = strerror(errno);
return (Socket) ret;
}
#ifndef NO_IPV6
/*
* If we were given ADDRTYPE_UNSPEC, we must also create an
* IPv4 listening socket and link it to this one.
*/
if (address_family == AF_INET6 && orig_address_family == ADDRTYPE_UNSPEC) {
Actual_Socket other;
other = (Actual_Socket) sk_newlistener(srcaddr, port, plug,
local_host_only, ADDRTYPE_IPV4);
if (other) {
if (!other->error) {
other->parent = ret;
ret->child = other;
} else {
/* If we couldn't create a listening socket on IPv4 as well
* as IPv6, we must return an error overall. */
close(s);
sfree(ret);
return (Socket) other;
}
}
}
#endif
ret->s = s;
uxsel_tell(ret);
add234(sktree, ret);
return (Socket) ret;
}
static void sk_tcp_close(Socket sock)
{
Actual_Socket s = (Actual_Socket) sock;
if (s->child)
sk_tcp_close((Socket)s->child);
uxsel_del(s->s);
del234(sktree, s);
close(s->s);
if (s->addr)
sk_addr_free(s->addr);
sfree(s);
}
void *sk_getxdmdata(void *sock, int *lenp)
{