-
Notifications
You must be signed in to change notification settings - Fork 23
/
socks.c
1697 lines (1528 loc) · 39.1 KB
/
socks.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
/*
socks.c:
$Id$
Copyright (C) 2001-2010 Tomo.M (author).
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. Neither the name of the author nor the names of its contributors
may be used to endorse or promote products derived from this software
without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "srelay.h"
#define TIMEOUTSEC 30
#define GEN_ERR_REP(s, v) \
switch ((v)) { \
case 0x04:\
socks_rep((s), (v), S4EGENERAL, 0);\
close((s));\
break;\
case 0x05:\
socks_rep((s), (v), S5EGENERAL, 0);\
close((s));\
break;\
case -1:\
break;\
default:\
break;\
}\
#define POSITIVE_REP(s, v, a) \
switch ((v)) { \
case 0x04:\
error = socks_rep((s), (v), S4AGRANTED, (a));\
break;\
case 0x05:\
error = socks_rep((s), (v), S5AGRANTED, (a));\
break;\
case -1:\
error = 0;\
break;\
default:\
error = -1;\
break;\
}\
/* ss: pointer of struct sockaddr_storage */
#define SET_SOCK_PORT(ss, port) \
switch (((struct sockaddr *)(ss))->sa_family) {\
case AF_INET:\
((struct sockaddr_in*)(ss))->sin_port = (port);\
break;\
case AF_INET6:\
((struct sockaddr_in6*)(ss))->sin6_port = (port);\
break;\
}\
struct host_info {
char host[NI_MAXHOST];
char port[NI_MAXSERV];
};
struct req_host_info {
struct host_info dest;
struct host_info proxy;
};
/* prototypes */
int proto_socks4 __P((SOCKS_STATE *));
int proto_socks5 __P((SOCKS_STATE *));
int socks_direct_conn __P((SOCKS_STATE *));
int proxy_connect __P((SOCKS_STATE *));
int build_socks_request __P((SOCKS_STATE *, u_char *, int));
int connect_to_socks __P((SOCKS_STATE *));
int socks_proxy_reply __P((int, SOCKS_STATE *));
int socks_rep __P((int , int , int , SockAddr *));
int build_socks_reply __P((int, int, SockAddr *, u_char *));
int s5auth_s __P((int));
int s5auth_s_rep __P((int, int));
int s5auth_c __P((int, bin_addr *, u_int16_t));
int connect_to_http __P((SOCKS_STATE *));
int forward_connect __P((SOCKS_STATE *));
int bind_sock __P((int, SOCKS_STATE *, struct addrinfo *));
int do_bind __P((int, struct addrinfo *, u_int16_t));
#ifdef SO_BINDTODEVICE
static int do_bindtodevice __P((int, char *));
#endif
int read_until_delim __P((int, char *, size_t, int));
int get_line __P((int, char *, size_t));
int get_str __P((int, char *, size_t));
int lookup_tbl __P((SOCKS_STATE *));
int resolv_host __P((bin_addr *, u_int16_t, struct host_info *));
int log_request __P((SOCKS_STATE *));
/*
proto_socks:
handle socks protocol.
*/
int proto_socks(SOCKS_STATE *state)
{
u_char buf[128];
int r;
int on = 1;
size_t len;
state->r = -1; /* forward socket not connected. */
r = timerd_read(state->s, buf, 1, TIMEOUTSEC, 0);
if ( r < 1 ) { /* EOF or error */
close(state->s);
return(-1);
}
state->sr.ver = buf[0];
switch (state->sr.ver) {
case 4:
if (method_num > 0) {
/* this implies this server is working in V5 mode */
/* dummy read for flush socket buf */
r = timerd_read(state->s, buf, sizeof(buf), TIMEOUTSEC, 0);
GEN_ERR_REP(state->s, 4);
msg_out(warn, "V4 request is not accepted.");
r = -1;
} else {
r = proto_socks4(state);
}
break;
case 5:
if ((r = s5auth_s(state->s)) == 0) {
r = proto_socks5(state);
}
break;
default:
r = -1;
}
if (r >= 0) {
lookup_tbl(state);
log_request(state);
if ( (state->hops == DIRECT && state->sr.req == S5REQ_CONN)
|| state->hops > 0 ) {
r = forward_connect(state);
if (r < 0) {
GEN_ERR_REP(state->s, state->sr.ver);
return(-1);
}
}
if (state->hops > 0) {
for (state->cur = state->hops - 1; state->cur >= 0; state->cur--) {
r = proxy_connect(state);
}
} else {
r = socks_direct_conn(state);
}
}
if (r >= 0) {
if (state->r >= 0) {
/* state->sr.req != UDPA or proxy_connect */
setsockopt(state->r, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof on);
#if defined(FREEBSD) || defined(MACOSX)
setsockopt(state->r, SOL_SOCKET, SO_REUSEPORT, (char *)&on, sizeof on);
#endif
setsockopt(state->r, IPPROTO_TCP, TCP_NODELAY, (char *)&on, sizeof on);
/* get upstream-side socket/peer name */
len = SS_LEN;
getsockname(state->r, &state->si->mys.addr.sa, (socklen_t *)&len);
state->si->mys.len = len;
len = SS_LEN;
getpeername(state->r, &state->si->prs.addr.sa, (socklen_t *)&len);
state->si->prs.len = len;
}
return(0); /* 0: OK */
}
/* error */
if (state->r >= 0) {
close(state->r);
}
state->r = -1;
return(-1); /* error */
}
/* socks4 protocol functions */
/*
proto_socks4:
handle socks v4/v4a protocol.
get socks v4/v4a request from client.
*/
int proto_socks4(SOCKS_STATE *state)
{
u_char buf[512];
int r;
r = timerd_read(state->s, buf+1, 1+2+4, TIMEOUTSEC, 0);
if (r < 1+2+4) { /* cannot read request */
GEN_ERR_REP(state->s, 4);
return(-1);
}
state->sr.req = buf[1];
/* check if request has socks4-a domain name format */
if ( buf[4] == 0 && buf[5] == 0 &&
buf[6] == 0 && buf[7] != 0 ) {
state->sr.dest.atype = S4ATFQDN;
} else {
state->sr.dest.atype = S4ATIPV4;
}
/* port */
state->sr.port = buf[2] * 0x100 + buf[3];
/* IP */
memcpy(state->sr.dest.v4_addr, &buf[4], 4);
/* rest of the req could be
username '\0'
or,
username '\0' hostname '\0'
*/
/* read client user name in request */
r = get_str(state->s, (char *)buf, sizeof(buf));
if (r < 0 || r > 255) {
/* invalid username length */
GEN_ERR_REP(state->s, 4);
return(-1);
}
state->sr.u_len = r;
memcpy(state->sr.user, buf, r);
if ( state->sr.dest.atype == S4ATFQDN ) {
/* request is socks4-A specific */
r = get_str(state->s, (char *)buf, sizeof(buf));
if ( r > 0 && r <= 256 ) { /* r should be 1 <= r <= 256 */
state->sr.dest.len_fqdn = r;
memcpy(state->sr.dest.fqdn, buf, r);
} else {
/* read error or something */
GEN_ERR_REP(state->s, 4);
return(-1);
}
}
return(0);
}
/* socks5 protocol functions */
/*
proto_socks5:
handle socks v5 protocol.
get socks v5 request from client.
*/
int proto_socks5(SOCKS_STATE *state)
{
u_char buf[512];
int r, len;
/* read first 4 bytes of request. */
r = timerd_read(state->s, buf, 4, TIMEOUTSEC, 0);
if ( r < 4 ) {
/* cannot read client request */
close(state->s);
return(-1);
}
if ( buf[0] != 0x05 ) {
/* wrong version request */
GEN_ERR_REP(state->s, 5);
return(-1);
}
state->sr.req = buf[1];
state->sr.dest.atype = buf[3]; /* address type field */
switch(state->sr.dest.atype) {
case S5ATIPV4: /* IPv4 address */
r = timerd_read(state->s, buf+4, 4+2, TIMEOUTSEC, 0);
if (r < 4+2) { /* cannot read request (why?) */
GEN_ERR_REP(state->s, 5);
return(-1);
}
memcpy(state->sr.dest.v4_addr, &buf[4], sizeof(struct in_addr));
state->sr.port = buf[8] * 0x100 + buf[9];
break;
case S5ATIPV6:
r = timerd_read(state->s, buf+4, 16+2, TIMEOUTSEC, 0);
if (r < 16+2) { /* cannot read request (why?) */
GEN_ERR_REP(state->s, 5);
return(-1);
}
memcpy(state->sr.dest.v6_addr, &buf[4], sizeof(struct in6_addr));
state->sr.port = buf[20] * 0x100 + buf[21];
break;
case S5ATFQDN: /* string or FQDN */
r = timerd_read(state->s, buf+4, 1, TIMEOUTSEC, 0);
if (r < 1) { /* cannot read request (why?) */
GEN_ERR_REP(state->s, 5);
return(-1);
}
if ((len = buf[4]) < 0 || len > 255) {
/* invalid length */
socks_rep(state->s, 5, S5EINVADDR, 0);
close(state->s);
return(-1);
}
r = timerd_read(state->s, buf+5, len+2, TIMEOUTSEC, 0);
if ( r < len+2 ) { /* cannot read request (why?) */
GEN_ERR_REP(state->s, 5);
return(-1);
}
memcpy(state->sr.dest.fqdn, &buf[5], len);
state->sr.dest.len_fqdn = len;
state->sr.port = buf[5+len] * 0x100 + buf[5+len+1];
break;
default:
/* unsupported address */
socks_rep(state->s, 5, S5EUSATYPE, 0);
close(state->s);
return(-1);
}
return(0);
}
/*
socks_direct_conn:
behave as socks server to connect/bind.
*/
int socks_direct_conn(SOCKS_STATE *state)
{
int cs, acs;
int len;
struct addrinfo ba;
SockAddr ss;
int error = 0;
int save_errno = 0;
/* process direct connect/bind to destination */
cs = acs = -1;
/* process by_command request */
switch (state->sr.req) { /* request */
case S5REQ_CONN:
len = SS_LEN;
if (getsockname(state->r, &ss.sa, (socklen_t *)&len) < 0) {
save_errno = errno;
close(state->r);
state->r = -1;
}
if (save_errno != 0) {
/* any socket error */
switch (state->sr.ver) {
case 0x04:
GEN_ERR_REP(state->s, 4);
break;
case 0x05:
switch(save_errno) {
case ENETUNREACH: socks_rep(state->s, 5, S5ENETURCH, 0); break;
case ECONNREFUSED: socks_rep(state->s, 5, S5ECREFUSE, 0); break;
#ifndef _POSIX_SOURCE
case EHOSTUNREACH: socks_rep(state->s, 5, S5EHOSURCH, 0); break;
#endif
case ETIMEDOUT: socks_rep(state->s, 5, S5ETTLEXPR, 0); break; /* ??? */
default: socks_rep(state->s, 5, S5EGENERAL, 0); break;
}
break;
default:
break;
}
close(state->s);
return(-1);
}
break;
case S5REQ_BIND:
memset(&ba, 0, sizeof(ba));
memset(&ss.ss, 0, SS_LEN);
ba.ai_addr = &ss.sa;
ba.ai_addrlen = SS_LEN;
/* just one address can be stored */
error = get_bind_addr(&state->sr.dest, &ba);
if (error) {
GEN_ERR_REP(state->s, state->sr.ver);
return(-1);
}
acs = socket(ba.ai_family, ba.ai_socktype, ba.ai_protocol);
if (acs < 0) {
/* socket error */
GEN_ERR_REP(state->s, state->sr.ver);
return(-1);
}
#ifdef SO_BINDTODEVICE
if (bindtodevice && do_bindtodevice(acs, bindtodevice) < 0) {
GEN_ERR_REP(state->s, state->sr.ver);
close(acs);
return(-1);
}
#endif
if (bind_sock(acs, state, &ba) != 0) {
GEN_ERR_REP(state->s, state->sr.ver);
return(-1);
}
listen(acs, INT_MAX);
/* get my socket name again to acquire an
actual listen port number */
len = SS_LEN;
if (getsockname(acs, &ss.sa, (socklen_t *)&len) == -1) {
/* getsockname failed */
GEN_ERR_REP(state->s, state->sr.ver);
close(acs);
return(-1);
}
memcpy(&state->si->mys.addr.ss, &ss, len);
state->si->mys.len = len;
/* first reply for bind request */
POSITIVE_REP(state->s, state->sr.ver, &ss);
if ( error < 0 ) {
/* could not reply */
close(state->s);
close(acs);
return(-1);
}
if (wait_for_read(acs, TIMEOUTSEC) <= 0) {
GEN_ERR_REP(state->s, state->sr.ver);
close(acs);
return(-1);
}
len = SS_LEN;
if ((cs = accept(acs, &ss.sa, (socklen_t *)&len)) < 0) {
GEN_ERR_REP(state->s, state->sr.ver);
close(acs);
return(-1);
}
close(acs); /* accept socket is not needed
any more, for current socks spec. */
state->r = cs; /* set forwarding socket */
/* sock name is in ss */
/* TODO:
* we must check ss against state->sr.dest here for security reason
*/
/* XXXXX */
break;
case S5REQ_UDPA:
/* on UDP assoc of DIRECT method, state->r will not used */
/* Allocate UDP_ATTR structure */
state->sr.udp = (UDP_ATTR *)malloc(sizeof(UDP_ATTR));
if (state->sr.udp == NULL) {
GEN_ERR_REP(state->s, state->sr.ver);
close(state->s);
return(-1);
}
memset(state->sr.udp, 0, sizeof(UDP_ATTR));
/* save the socket name of requester's TCP socket into UDP_ATTR
* for checking lator.
*/
memcpy(&state->sr.udp->si.prc.addr, &state->si->prc.addr, SS_LEN);
SET_SOCK_PORT(&state->sr.udp->si.prc.addr, 0);
if (state->sr.port != 0)
SET_SOCK_PORT(&state->sr.udp->si.prc.addr, htons(state->sr.port));
/* initialize UDP transport sockets */
state->sr.udp->d = state->sr.udp->u = -1;
/* create UDP socket on the same I/F as the request was reached */
memcpy(&ss.ss, &state->si->myc.addr.ss, SS_LEN);
if ((cs = socket(ss.sa.sa_family, SOCK_DGRAM, IPPROTO_IP)) >= 0) {
SET_SOCK_PORT(&ss.ss, 0);
if (bind(cs, &ss.sa, state->si->myc.len) < 0) {
/* bind error */
close(cs);
GEN_ERR_REP(state->s, state->sr.ver);
close(state->s);
return(-1);
}
/* XXXXXXXX */
/* get my socket name to acquire an actual bound port */
len = SS_LEN;
if (getsockname(cs, &ss.sa, (socklen_t *)&len) == -1) {
/* getsockname failed */
GEN_ERR_REP(state->s, state->sr.ver);
close(cs);
return(-1);
}
/* ss is actual socket name here for usig positive reply */
/* set downward soket */
state->sr.udp->d = cs;
} else {
GEN_ERR_REP(state->s, state->sr.ver);
return(-1);
}
break;
default:
/* unsupported request */
GEN_ERR_REP(state->s, state->sr.ver);
close(state->s);
return(-1);
}
POSITIVE_REP(state->s, state->sr.ver, &ss);
if ( error < 0 ) {
/* could not reply */
close(state->s);
close(cs);
return(-1);
}
return(0);
}
/* proxy socks functions */
/*
proxy_connect:
connect to next hop socks/ server.
indirect connection to destination.
*/
int proxy_connect(SOCKS_STATE *state)
{
int r = 0;
switch (state->prx[state->cur].pproto) {
case HTTP:
r = connect_to_http(state);
break;
case SOCKS:
case SOCKSv4:
case SOCKSv5:
default:
r = connect_to_socks(state);
break;
}
if (r < 0 && state->cur == 0) {
GEN_ERR_REP(state->s, state->sr.ver);
return(-1);
}
return(0);
}
int connect_to_socks(SOCKS_STATE *state)
{
int r, len = 0;
u_char buf[640];
int cur = state->cur;
int pproto = state->prx[cur].pproto;
if (state->r < 0 && state->cur == 0) {
GEN_ERR_REP(state->s, state->sr.ver);
return(-1);
}
/* process proxy request to next hop socks */
/* first try socks5 server */
if (pproto == SOCKS || pproto == SOCKSv5) {
if ((len = build_socks_request(state, buf, 5)) > 0) {
if (s5auth_c(state->r, &state->prx[cur].proxy, state->prx[cur].pport) == 0) {
/* socks5 auth nego to next hop success */
r = timerd_write(state->r, buf, len, TIMEOUTSEC);
if ( r == len ) {
/* send request success */
r = socks_proxy_reply(5, state);
if (r == 0) {
return(r);
}
}
}
}
}
/* if an error, second try socks4 server */
if (pproto == SOCKS || pproto == SOCKSv4) {
if ((len = build_socks_request(state, buf, 4)) > 0) {
r = timerd_write(state->r, buf, len, TIMEOUTSEC);
if ( r == len ) {
/* send request success */
r = socks_proxy_reply(4, state);
if (r == 0) {
return(r);
}
}
}
}
/* still be an error, give it up. */
if (state->cur == 0) {
GEN_ERR_REP(state->s, state->sr.ver);
}
return(-1);
}
/*
build_socks_request:
build socks request on buf
return buf length
*/
int build_socks_request(SOCKS_STATE *state, u_char *buf, int ver)
{
/* buf must be at least 640 bytes long */
int r, len = 0;
char *user;
bin_addr dest;
u_int16_t port;
int req;
if (state->cur > 0) {
port = state->prx[state->cur-1].pport;
req = S5REQ_CONN; /* only CONNECT among relays */
memcpy(&dest, &state->prx[state->cur-1].proxy, sizeof(bin_addr));
} else {
port = state->sr.port;
req = state->sr.req;
memcpy(&dest, &state->sr.dest, sizeof(bin_addr));
}
switch (ver) { /* next hop socks server version */
case 0x04:
/* build v4 request */
buf[0] = 0x04;
buf[1] = req;
if ( state->sr.u_len == 0 ) {
user = S4DEFUSR;
r = strlen(user);
} else {
user = state->sr.user;
r = state->sr.u_len;
}
if (r < 0 || r > 255) {
return(-1);
}
buf[2] = (port / 256);
buf[3] = (port % 256);
memcpy(&buf[8], user, r);
len = 8+r;
buf[len++] = 0x00;
switch (dest.atype) {
case S4ATIPV4:
memcpy(&buf[4], dest.v4_addr, sizeof(struct in_addr));
break;
case S4ATFQDN:
buf[4] = buf[5] = buf[6] = 0; buf[7] = 1;
r = dest.len_fqdn;
if (r <= 0 || r > 255) {
return(-1);
}
memcpy(&buf[len], dest.fqdn, r);
len += r;
buf[len++] = 0x00;
break;
default:
return(-1);
}
break;
case 0x05:
/* build v5 request */
buf[0] = 0x05;
buf[1] = req;
buf[2] = 0;
buf[3] = dest.atype;
switch (dest.atype) {
case S5ATIPV4:
memcpy(&buf[4], dest.v4_addr, 4);
buf[8] = (port / 256);
buf[9] = (port % 256);
len = 10;
break;
case S5ATIPV6:
memcpy(&buf[4], dest.v6_addr, 16);
buf[20] = (port / 256);
buf[21] = (port % 256);
len = 22;
break;
case S5ATFQDN:
len = dest.len_fqdn;
buf[4] = len;
memcpy(&buf[5], dest.fqdn, len);
buf[5+len] = (port / 256);
buf[5+len+1] = (port % 256);
len = 5+len+2;
break;
default:
return(-1);
}
break;
default:
return(-1); /* unknown version */
}
return(len); /* OK */
}
/*
socks_proxy_reply:
v: server socks version.
read server response and
write converted reply to client if needed.
note: state->sr.ver == -1 means DEEP indirect proxy.
*/
int socks_proxy_reply(int v, SOCKS_STATE *state)
{
int r, c, len;
u_char buf[512];
struct addrinfo hints, *res, *res0;
int error;
int found = 0;
SockAddr ss;
switch (state->sr.req) {
case S5REQ_CONN:
c = 1;
break;
case S5REQ_BIND:
c = 2;
break;
default: /* i don't know what to do */
c = 1;
break;
}
while (c-- > 0) {
memset(&ss, 0, SS_LEN);
/* read server reply */
r = timerd_read(state->r, buf, sizeof buf, TIMEOUTSEC, 0);
if (state->cur > 0) { /* special case */
if ((v == 5 && buf[1] == S5AGRANTED)
|| (v == 4 && buf[1] == S4AGRANTED)) {
return(0);
}
return(-1);
}
switch (v) { /* server socks version */
case 4: /* server v:4 */
if ( r < 8 ) { /* from v4 spec, r should be 8 */
/* cannot read server reply */
r = -1;
break;
}
switch (state->sr.ver) { /* client ver */
case 4: /* same version */
r = timerd_write(state->s, buf, r, TIMEOUTSEC);
break;
case 5:
if ( buf[1] == S4AGRANTED ) {
/* translate reply v4->v5 */
ss.v4.sin_family = AF_INET;
memcpy(&(ss.v4.sin_addr), &buf[4], 4);
memcpy(&(ss.v4.sin_port), &buf[2], 2);
r = socks_rep(state->s, 5, S5AGRANTED, &ss);
} else {
r = -1;
}
break;
default:
r = -1;
break;
}
break;
case 5: /* server v:5 */
if ( r < 7 ) { /* should be 10 or more */
/* cannot read server reply */
r = -1;
break;
}
switch (state->sr.ver) { /* client ver */
case 4:
/* translate reply v5->v4 */
if ( buf[1] == S5AGRANTED ) {
switch (buf[3]) { /* address type */
case S5ATIPV4:
ss.v4.sin_family = AF_INET;
memcpy(&(ss.v4.sin_addr), &buf[4], 4);
memcpy(&(ss.v4.sin_port), &buf[8], 2);
break;
case S5ATIPV6:
/* basically v4 cannot handle IPv6 address */
/* make fake IPv4 to forcing reply */
ss.v4.sin_family = AF_INET;
memcpy(&(ss.v4.sin_addr), &buf[16], 4);
memcpy(&(ss.v4.sin_port), &buf[20], 2);
break;
case S5ATFQDN:
default:
ss.v4.sin_family = AF_INET;
len = buf[4] & 0xff;
memcpy(&(ss.v4.sin_port), &buf[5+len], 2);
buf[5+len] = '\0';
memset(&hints, 0, sizeof(hints));
hints.ai_socktype = SOCK_STREAM;
hints.ai_family = AF_INET;
error = getaddrinfo((char *)&buf[5], NULL, &hints, &res0);
if (!error) {
for (res = res0; res; res = res->ai_next) {
if (res->ai_socktype != AF_INET)
continue;
memcpy(&(ss.v4.sin_addr),
&(((struct sockaddr_in *)res->ai_addr)->sin_addr),
sizeof(struct in_addr));
found++; break;
}
freeaddrinfo(res0);
}
if (!found) {
/* set fake ip */
memset(&(ss.v4.sin_addr), 0, 4);
}
break;
}
r = socks_rep(state->s, 4, S4AGRANTED, &ss);
} else {
r = -1;
}
break;
case 5: /* same version */
r = timerd_write(state->s, buf, r, TIMEOUTSEC);
break;
default:
r = -1;
break;
}
break;
default:
/* parameter error */
r = -1;
break;
}
if (r < 0) {
return(r);
}
}
return(0);
}
int socks_rep(int s, int ver, int code, SockAddr *addr)
{
u_char buf[512];
int len = 0, r;
memset(buf, 0, sizeof(buf));
len = build_socks_reply(ver, code, addr, buf);
if (len > 0)
r = timerd_write(s, buf, len, TIMEOUTSEC);
else
r = -1;
if (r < len)
return -1;
return 0;
}
int build_socks_reply(int ver, int code, SockAddr *addr, u_char *buf)
{
int len = 0;
switch (ver) {
case 0x04:
switch (code) {
case S4AGRANTED:
buf[0] = 0;
buf[1] = code; /* succeeded */
if (addr) {
memcpy(&buf[2], &addr->v4.sin_port, 2);
memcpy(&buf[4], &addr->v4.sin_addr, 4);
}
len = 8;
break;
default: /* error cases */
buf[0] = ver;
buf[1] = code; /* error code */
len = 8;
break;
}
break;
case 0x05:
switch (code) {
case S5AGRANTED:
buf[0] = ver;
buf[1] = code; /* succeeded */
buf[2] = 0;
if (addr) {
switch (addr->sa.sa_family) {
case AF_INET:
buf[3] = S5ATIPV4;
memcpy(&buf[4], &addr->v4.sin_addr, 4);
memcpy(&buf[8], &addr->v4.sin_port, 2);
len = 4+4+2;
break;
case AF_INET6:
buf[3] = S5ATIPV6;
memcpy(&buf[4], &addr->v6.sin6_addr, 16);
memcpy(&buf[20], &addr->v6.sin6_port, 2);
len = 4+16+2;
break;
default:
buf[1] = S5EUSATYPE;
buf[3] = S5ATIPV4;
len = 4+4+2;
break;
}
}
break;
default: /* error cases */
buf[0] = ver;
buf[1] = code & 0xff; /* error code */
buf[2] = 0;
buf[3] = S5ATIPV4; /* addr type fixed to IPv4 */
len = 10;
break;
}
break;
default:
/* unsupported socks version */
len = 0;
break;
}
return(len);
}
/*
socks5 auth negotiation as server.
*/
int s5auth_s(int s)
{
u_char buf[512];
int r, i, j, len;
int method=0, done=0;
/* auth method negotiation */
r = timerd_read(s, buf+1, 1, TIMEOUTSEC, 0);
if ( r < 1 ) {
/* cannot read */
s5auth_s_rep(s, S5ANOTACC);
return(-1);
}
len = buf[1];
if ( len < 0 || len > 255 ) {
/* invalid number of methods */