File: socks.h

package info (click to toggle)
dante 1.4.2%2Bdfsg-7
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye
  • size: 11,640 kB
  • sloc: ansic: 64,514; sh: 11,180; yacc: 3,127; lex: 1,683; makefile: 364; awk: 220
file content (986 lines) | stat: -rwxr-xr-x 31,458 bytes parent folder | download | duplicates (3)
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
/*
 * Copyright (c) 1997, 1998, 1999, 2000, 2001, 2002, 2004, 2005, 2008, 2009,
 *               2010, 2011, 2012, 2013, 2016, 2017
 *      Inferno Nettverk A/S, Norway.  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. The above copyright notice, this list of conditions and the following
 *    disclaimer must appear in all copies of the software, derivative works
 *    or modified versions, and any portions thereof, aswell as in all
 *    supporting documentation.
 * 2. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *      This product includes software developed by
 *      Inferno Nettverk A/S, Norway.
 * 3. The name of the author may not be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 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.
 *
 * Inferno Nettverk A/S requests users of this software to return to
 *
 *  Software Distribution Coordinator  or  sdc@inet.no
 *  Inferno Nettverk A/S
 *  Oslo Research Park
 *  Gaustadallen 21
 *  NO-0349 Oslo
 *  Norway
 *
 * any improvements or extensions that they make and grant Inferno Nettverk A/S
 * the rights to redistribute these changes.
 *
 */

/* $Id: socks.h,v 1.287.6.6 2017/01/31 14:35:55 karls Exp $ */

#ifndef _SOCKS_H_
#define _SOCKS_H_

#define HAVE_SOCKS_RULES                  (0)

#ifndef HAVE_OSF_OLDSTYLE
#define HAVE_OSF_OLDSTYLE 0
#endif /* !HAVE_OSF_OLDSTYLE */

#if SOCKSLIBRARY_DYNAMIC

#ifdef __COVERITY__
/*
 * Coverity naturally has no idea what the function sys_foo calls does,
 * so let it pretend sys_foo is the same as foo.
 * Means Coverity can't catch errors in the code around the call to
 * sys_foo(), but avoids dozens of false positives because Coverity has no
 * idea what the dlopen(3)-ed functions do.
 */
#define sys_accept accept
#define sys_bind bind
#define sys_bindresvport bindresvport
#define sys_connect connect
#define sys_gethostbyname gethostbyname
#define sys_gethostbyname2 gethostbyname2
#define sys_getaddrinfo getaddrinfo
#define sys_getipnodebyname getipnodebyname
#define sys_getpeername getpeername
#define sys_getsockname getsockname
#define sys_getsockopt getsockopt
#define sys_listen listen
#define sys_read read
#define sys_readv readv
#define sys_recv recv
#define sys_recvfrom recvfrom
#define sys_recvfrom recvfrom
#define sys_recvmsg recvmsg
#define sys_rresvport rresvport
#define sys_send send
#define sys_sendmsg sendmsg
#define sys_sendto sendto
#define sys_write write
#define sys_writev writev
#endif /* __COVERITY__ */


#if 0 /* XXX disable until testing on AIX/other can be done */

/* XXX needed on AIX apparently */
#ifdef recvmsg
#define recvmsg_system recvmsg
#undef recvmsg
#endif /* recvmsg */

#if HAVE_SYSTEM_XMSG_MAGIC
#undef recvmsg_system
#define recvmsg_system nrecvmsg
#endif /* HAVE_SYSTEM_XMSG_MAGIC */

#ifdef sendmsg
#define sendmsg_system sendmsg
#undef sendmsg
#endif /* sendmsg */

#if HAVE_SYSTEM_XMSG_MAGIC
#undef sendmsg_system
#define sendmsg_system nsendmsg
#endif /* HAVE_SYSTEM_XMSG_MAGIC */
#endif

#ifdef accept
#undef accept
#endif /* accept */
#if HAVE_EXTRA_OSF_SYMBOLS
#define accept(s, addr, addrlen)         sys_Eaccept(s, addr, addrlen)
#else
#define accept(s, addr, addrlen)         sys_accept(s, addr, addrlen)
#endif /* HAVE_EXTRA_OSF_SYMBOLS */

#ifdef bind
#undef bind
#endif /* bind */
#if (defined __sun) && (defined _XPG4_2)
#define bind(s, name, namelen)         sys_xnet_bind(s, name, namelen)
#else
#define bind(s, name, namelen)         sys_bind(s, name, namelen)
#endif

#ifdef bindresvport
#undef bindresvport
#endif /* bindresvport */
#define bindresvport(sd, sin)            sys_bindresvport(sd, sin)

#ifdef connect
#undef connect
#endif /* connect */
#if (defined __sun) && (defined _XPG4_2)
#define connect(s, name, namelen)      sys_xnet_connect(s, name, namelen)
#else
#define connect(s, name, namelen)      sys_connect(s, name, namelen)
#endif


#ifdef gethostbyname
#undef gethostbyname
#endif /* gethostbyname */

#if HAVE_GETHOSTBYNAME2

/*
 * a little tricky ... we need it to be at the bottom of the stack,
 * like a syscall.
 */
#define gethostbyname(name)            sys_gethostbyname2(name, AF_INET)
#else
#define gethostbyname(name)            sys_gethostbyname(name)

#endif /* HAVE_GETHOSTBYNAME2 */

#ifdef gethostbyname2
#undef gethostbyname2
#endif /* gethostbyname2 */
#define gethostbyname2(name, af)       sys_gethostbyname2(name, af)

#ifdef getaddrinfo
#undef getaddrinfo
#endif /* getaddrinfo */
#define getaddrinfo(nodename, servname, hints, res)   \
         sys_getaddrinfo(nodename, servname, hints, res)

#ifdef getipnodebyname
#undef getipnodebyname
#endif /* getipnodebyname */
#define getipnodebyname(name, af, flags, error_num)   \
         sys_getipnodebyname(name, af, flags, error_num)

#ifdef freehostent
#undef freehostent
#endif /* freehostent */
#define freehostent(ptr)            sys_freehostent(ptr)

#if HAVE_GETNAMEINFO

#define getnameinfo(sa, salen, host, hostlen, serv, servlen, flags) \
         sys_getnameinfo(sa, salen, host, hostlen, serv, servlen, flags)

#endif /* HAVE_GETNAMEINFO  */


#ifdef getpeername
#undef getpeername
#endif /* getpeername */

#if HAVE_EXTRA_OSF_SYMBOLS
#define getpeername(s, name, namelen)   sys_Egetpeername(s, name, namelen)
#else
#define getpeername(s, name, namelen)   sys_getpeername(s, name, namelen)
#endif /* HAVE_EXTRA_OSF_SYMBOLS */

#ifdef getsockname
#undef getsockname
#endif /* getsockname */
#if HAVE_EXTRA_OSF_SYMBOLS
#define getsockname(s, name, namelen)   sys_Egetsockname(s, name, namelen)
#else
#define getsockname(s, name, namelen)   sys_getsockname(s, name, namelen)
#endif /* HAVE_EXTRA_OSF_SYMBOLS */

#ifdef getsockopt
#undef getsockopt
#endif /* getsockopt */
#if HAVE_EXTRA_OSF_SYMBOLS
#define getsockopt(a, b, c, d, e) sys_Egetsockopt(a, b, c, d, e)
#else
#define getsockopt(a, b, c, d, e) sys_getsockopt(a, b, c, d, e)
#endif /* HAVE_EXTRA_OSF_SYMBOLS */

#ifdef listen
#undef listen
#endif /* listen */
#if (defined __sun) && (defined _XPG4_2)
#define listen(s, backlog)   sys_xnet_listen(s, backlog)
#else
#define listen(s, backlog)   sys_listen(s, backlog)
#endif

#ifdef read
#undef read
#endif /* read */
#define read(d, buf, nbytes)            sys_read(d, buf, nbytes)

#ifdef readv
#undef readv
#endif /* readv */
#if HAVE_EXTRA_OSF_SYMBOLS
#define readv(d, iov, iovcnt)            sys_Ereadv(d, iov, iovcnt)
#else
#define readv(d, iov, iovcnt)            sys_readv(d, iov, iovcnt)
#endif /* HAVE_EXTRA_OSF_SYMBOLS */

#ifdef recv
#undef recv
#endif /* recv */
#define recv(s, msg, len, flags)         sys_recv(s, msg, len, flags)

#ifdef recvfrom
#undef recvfrom
#endif /* recvfrom */
#if HAVE_EXTRA_OSF_SYMBOLS
#define recvfrom(s, buf, len, flags, from, fromlen)   \
        sys_Erecvfrom(s, buf, len, flags, from, fromlen)
#else
#define recvfrom(s, buf, len, flags, from, fromlen)   \
        sys_recvfrom(s, buf, len, flags, from, fromlen)

#endif /* HAVE_EXTRA_OSF_SYMBOLS */

#ifdef recvmsg
#undef recvmsg
#endif /* recvmsg */
#if (defined __sun) && (defined _XPG4_2)
#define recvmsg(s, msg, flags)          sys_xnet_recvmsg(s, msg, flags)
#else
#define recvmsg(s, msg, flags)          sys_recvmsg(s, msg, flags)
#endif

#if HAVE_RRESVPORT
#ifdef rresvport
#undef rresvport
#endif /* rresvport */
#define rresvport(port)                  sys_rresvport(port)
#endif /* HAVE_RRESVPORT */

#ifdef sendto
#undef sendto
#endif /* sendto */
#if (defined __sun) && (defined _XPG4_2)
#define sendto(s, msg, len, flags, to, tolen)   \
        sys_xnet_sendto(s, msg, len, flags, to, tolen)
#else
#define sendto(s, msg, len, flags, to, tolen)   \
        sys_sendto(s, msg, len, flags, to, tolen)
#endif

#ifdef write
#undef write
#endif /* write */
#define write(d, buf, nbytes)            sys_write(d, buf, nbytes)

#ifdef writev
#undef writev
#endif /* writev */
#if HAVE_EXTRA_OSF_SYMBOLS
#define writev(d, iov, iovcnt)         sys_Ewritev(d, iov, iovcnt)
#else
#define writev(d, iov, iovcnt)         sys_writev(d, iov, iovcnt)
#endif /* HAVE_EXTRA_OSF_SYMBOLS */

#ifdef send
#undef send
#endif /* send */
#define send(s, msg, len, flags)         sys_send(s, msg, len, flags)

#undef sendmsg
#if HAVE_EXTRA_OSF_SYMBOLS
#define sendmsg(s, msg, flags)         sys_Esendmsg(s, msg, flags)
#else
#if (defined __sun) && (defined _XPG4_2)
#define sendmsg(s, msg, flags)         sys_xnet_sendmsg(s, msg, flags)
#else
#define sendmsg(s, msg, flags)         sys_sendmsg(s, msg, flags)
#endif
#endif /* HAVE_EXTRA_OSF_SYMBOLS */

#undef recvmsg
#if HAVE_EXTRA_OSF_SYMBOLS
#define recvmsg(s, msg, flags)         sys_Erecvmsg(s, msg, flags)
#else
#if (defined __sun) && (defined _XPG4_2)
#define recvmsg(s, msg, flags)         sys_xnet_recvmsg(s, msg, flags)
#else
#define recvmsg(s, msg, flags)         sys_recvmsg(s, msg, flags)
#endif
#endif /* HAVE_EXTRA_OSF_SYMBOLS */

#if HAVE_GSSAPI && HAVE_LINUX_GLIBC_WORKAROUND
#ifdef getc
#undef getc
#endif /* getc */
#define getc(s)                           sys_getc(s)

#ifdef fgetc
#undef fgetc
#endif /* fgetc */
#define fgetc(s)                          sys_fgetc(s)

#ifdef gets
#undef gets
#endif /* gets */
#define gets(s)                           sys_gets(s)

#ifdef fgets
#undef fgets
#endif /* fgets */
#define fgets(c, i, s)                    sys_fgets(c, i, s)

#ifdef putc
#undef putc
#endif /* putc */
#define putc(c, s)                        sys_putc(c, s)

#ifdef fputc
#undef fputc
#endif /* fputc */
#define fputc(c, s)                       sys_fputc(c, s)

#ifdef puts
#undef pus
#endif /* puts */
#define puts(b)                           sys_puts(b)

#ifdef fputs
#undef fputs
#endif /* fputc */
#define fputs(b, s)                       sys_fputs(b, s)

#ifdef fflush
#undef fflush
#endif /* fflush */
#define fflush(s)                         sys_fflush(s)

#ifdef fclose
#undef fclose
#endif /* fclose */
#define fclose(s)                         sys_fclose(s)

#ifdef printf
#undef printf
#endif /* printf */
#define printf(f, ...)                    sys_printf(f, __VA_ARGS__ )

#ifdef vprintf
#undef vprintf
#endif /* vprintf */
#define vprintf(f, v)                     sys_vfprintf(f, v)

#ifdef fprintf
#undef fprintf
#endif /* fprintf */
#define fprintf(s, ...)                   sys_fprintf(s, __VA_ARGS__ )

#ifdef vfprintf
#undef vfprintf
#endif /* vfprintf */
#define vfprintf(s, f, v)                 sys_vfprintf(s, f, v)

#ifdef fwrite
#undef fwrite
#endif /* fwrite */
#define fwrite(p, i, n, s)                sys_fwrite(p, i, n, s)

#ifdef fread
#undef fread
#endif /* fread */
#define fread(p, i, n, s)                 sys_fread(p, i, n, s)

#endif /* HAVE_GSSAPI && HAVE_LINUX_GLIBC_WORKAROUND */

#endif /* SOCKSLIBRARY_DYNAMIC */

/* not used in client. */
#define loglevel_errno(e,  side) (LOG_DEBUG)
#define loglevel_gaierr(e, side) (LOG_DEBUG)

#define FDPASS_MAX         2   /* max number of descriptors we send/receive.  */

typedef struct {
   unsigned char      inited;

   unsigned char      havegssapisockets;/* have any gssapi-sockets?           */
   unsigned char      threadlockenabled;/* is threadlocking enabled?          */

   ssize_t            executingdnscode; /* exec. gethost*()/getname*()/etc.   */
   unsigned char      internalerrordetected;
   sig_atomic_t       insignal;         /* executing in signalhandler?        */
   sig_atomic_t       handledsignal;   /*
                                        * between now and the time this
                                        * variable was last cleared, did we
                                        * handle a signal?
                                        */

   sockshost_t        lastconnect;      /* address we last connected to.      */
   pid_t              pid;              /* our pid.                           */
   rlim_t             maxopenfiles;
} configstate_t;

typedef struct {
   int               debug;
   int               directfallback; /* fallback to direct connections        */
   char              *configfile;    /* name of current configfile.           */
} option_t;

struct config {
   pid_t                    connectchild;            /* connect process.      */
   int                      child_data;              /* data socket to child. */
   int                      child_ack;               /* ack to child.         */

   char                     domain[MAXHOSTNAMELEN];  /* localdomain.          */

   logtype_t                errlog;                  /* for errors only.      */
   logtype_t                log;                     /* where to log.         */
   int                      loglock;                 /* lockfile for logging. */

   option_t                 option;                  /* misc. options.        */
   int                      resolveprotocol;         /* resolveprotocol.      */

   routeoptions_t           routeoptions;            /* global route flags.   */
   route_t                  *route;                  /* linked list of routes */

   /* XXX not supported in client yet. */
   socketoption_t           *socketoptionv;          /* global socket options.*/
   size_t                   socketoptionc;

   configstate_t            state;
   timeout_t                timeout;
};

typedef struct {
   int           s;         /* socket used for control-connection.     */
   socks_t       packet;    /* socks packet exchanged with server.     */
} childpacket_t;

typedef sigset_t addrlockopaque_t;

/*
 * libsocksd function declarations
 */

void __ATTRIBUTE__((ATTRIBUTE_CONSTRUCTOR))
clientinit(void);
/*
 * initializes client state, reads config file, etc.
 */

#if !HAVE_OSF_OLDSTYLE
int Raccept(int, struct sockaddr *, socklen_t *);
int Rconnect(int, const struct sockaddr *, socklen_t);
int Rgetsockname(int, struct sockaddr *, socklen_t *);
int Rgetsockopt(int, int, int, void *, socklen_t *);
int Rgetpeername(int, struct sockaddr *, socklen_t *);
ssize_t Rsendto(int s, const void *msg, size_t len, int flags,
                const struct sockaddr *to, socklen_t tolen)
      __ATTRIBUTE__((__BOUNDED__(__buffer__, 2, 3)));
ssize_t Rrecvfrom(int s, void *buf, size_t len, int flags,
                  struct sockaddr * from, socklen_t *fromlen)
      __ATTRIBUTE__((__BOUNDED__(__buffer__, 2, 3)));
ssize_t Rsendmsg(int s, const struct msghdr *msg, int flags);
ssize_t Rrecvmsg(int s, struct msghdr *msg, int flags);
int Rbind(int, const struct sockaddr *, socklen_t);
#endif /* !HAVE_OSF_OLDSTYLE */

int Rbindresvport(int, struct sockaddr_in *);
int Rrresvport(int *);
struct hostent *Rgethostbyname(const char *);
struct hostent *Rgethostbyname2(const char *, int af);

#if HAVE_GETNAMEINFO

int Rgetnameinfo(const struct sockaddr *sa, socklen_t salen, char *host,
                 socklen_t hostlen, char *serv, socklen_t servlen,
                 int flags);

#endif /* HAVE_GETNAMEINFO  */

#if HAVE_GETADDRINFO
int Rgetaddrinfo(const char *nodename, const char *servname,
                 const struct addrinfo *hints, struct addrinfo **res);
#endif /* HAVE_GETADDRINFO */

#if HAVE_GETIPNODEBYNAME
struct hostent *Rgetipnodebyname(const char *, int, int, int *);
void Rfreehostent(struct hostent *);
#endif /* HAVE_GETIPNODEBYNAME */

ssize_t Rwrite(int d, const void *buf, size_t nbytes)
      __ATTRIBUTE__((__BOUNDED__(__buffer__, 2, 3)));
ssize_t Rwritev(int d, const struct iovec *iov, int iovcnt);
ssize_t Rsend(int s, const void *msg, size_t len, int flags)
      __ATTRIBUTE__((__BOUNDED__(__buffer__, 2, 3)));
ssize_t Rread(int d, void *buf, size_t nbytes)
      __ATTRIBUTE__((__BOUNDED__(__buffer__, 2, 3)));
ssize_t Rreadv(int d, const struct iovec *iov, int iovcnt);
ssize_t Rrecv(int s, void *msg, size_t len, int flags)
      __ATTRIBUTE__((__BOUNDED__(__buffer__, 2, 3)));

#if HAVE_GSSAPI && HAVE_LINUX_GLIBC_WORKAROUND
int Rfgetc(FILE *fp);
char *Rgets(char *s);
char *Rfgets(char *s, int size, FILE *fp);
int Rfputc(int c, FILE *fp);
int Rfputs(const char *s, FILE *fp);
int Rfflush(FILE *fp);
int Rfclose(FILE *fp);
int Rfprintf(FILE *stream, const char *format, ...);
int Rvfprintf(FILE *stream, const char *format, va_list ap);
size_t Rfread(void *ptr, size_t size, size_t nmemb, FILE *s);
size_t Rfwrite(const void *ptr, size_t size, size_t nmemb, FILE *s);
#endif /* HAVE_GSSAPI && HAVE_LINUX_GLIBC_WORKAROUND */


int SOCKSinit(char *);
int Rlisten(int, int);
int Rselect(int, fd_set *, fd_set *, fd_set *, struct timeval *);
/*
 * unused functions needed to compile programs with support for other
 * socks implementations.
 */




int
cgetaddrinfo(const char *name, const char *service,
             const struct addrinfo *hints, struct addrinfo **res,
             dnsinfo_t *resmem);
/*
 * Like getaddrinfo(3), but "resmem" is used to hold the contents of "res",
 * rather than allocating the memory for "res" dynamically and then
 * having to call freeaddrinfo(3).
 */

route_t *
udpsetup(int s, const struct sockaddr_storage *to, int type, int shouldconnect,
         char *emsg, const size_t emsglen);
/*
 * sets up udp relaying between address of "s" and "to" by connecting
 * to a proxy server.
 * If relaying is already set up the function returns with success.
 * "type" is the type of connection to set up, SOCKS_SEND or SOCKS_RECV.
 * "shouldconnect" indicates whether the socket should be connected or not.
 *
 * Returns the route that was used (possibly a direct route), or NULL if no
 * route could be set up.  In the latter case, errno and emsg will be set.
 */

int
fd_is_network_socket(const int fd);
/*
 * Returns true if "fd" is a network socket of a kind we support.
 * Returns False otherwise.
 */




   /*
    *  Misc. functions to help keep track of our connection(s) to the server.
    */

void socks_addrinit(void);
/*
 * inits thing, including memory and locks, for socks_addaddr()-functions.
 */

void socks_addrlock(const int locktype, addrlockopaque_t *opaque);
void socks_addrunlock(const addrlockopaque_t *opaque);
/*
 * Lock/unlock global address object.
 * "type" is one of F_WRLCK or F_RDLCK, for write or read-lock.
 * "opaque" is a pointer filled in by "socks_addrlock()", and
 * the same pointer needs to be passed to socks_addrunlock();
 */

socksfd_t *
socks_addrdup(const socksfd_t *old, socksfd_t *new);
/*
 * Duplicates "old", in "new".
 * Returns:
 *    On success: "new".
 *    On failure: NULL (resource shortage).
 */

socksfd_t *
socks_addaddr(const int clientfd, const socksfd_t *socksaddress,
              const int takelock);
/*
 * "clientfd" is associated with the structure "socksfd".
 * If "takelock" is true, it means the function should take the
 * socksfdv/addrlock.
 *
 * The function duplicates all arguments in its own form and does
 * not access the memory referenced by them afterwards.
 *
 * The function checks the state of all file descriptors on each call and
 * removes those that are no longer open.
 *
 * Returns:
 *      On success: pointer to the added socksfd_t structure.
 *      On failure: exits.  (memory exhausted and process grew descriptor size.)
 *
 */

socksfd_t *
socks_getaddr(const int fd, socksfd_t *socksfd, const int takelock);
/*
 * Returns a copy of the socksfd corresponding to "fd".
 * If "socksfd" is not NULL, the contents of the socksfd is also stored in
 * "socksfd".
 *
 * If "takelock" is true, it means the function should take the
 * socksfdv/addrlock.
 *
 * Returns:
 *      On success:  the socket address associated with file descriptor "fd".
 *      On failure:    NULL.  (no socket address associated with "fd").
 */

void
socks_rmaddr(const int s, const int takelock);
/*
 * If "takelock" is true, it means the function should take the
 * socksfdv/addrlock.
 *
 * removes the association for the socket "s", also closes the server
 * connection.  If "s" is not registered the request is ignored.
 */

int
socks_addrcontrol(const int controlsent, const int controlreceived,
                  const int takelock);
/*
 * Goes through all addresses registered and tries to find one where
 * the control socket has a local address of "local" and peer address
 * of "remote".
 *
 * "controlsent" gives the expected fd index for control, if not -1.
 * That is the fd index control had when we sent the request to
 * our connect-child, and may belong to another fd now.
 *
 * "controlreceived" is the actual fd we sent to the connectchild, and
 * which we now receive back from it.
 *
 *   Returns:
 *      On success: the descriptor the socksfd struct was registered with.
 *      On failure: -1
 */

int
socks_addrmatch(const struct sockaddr_storage *local,
                const struct sockaddr_storage *remote,
                const socksstate_t *state, const int takelock);
/*
 * If "takelock" is true, it means the function should take the
 * socksfdv/addrlock.
 *
 * Goes through all addresses registered and tries to find one where
 * all arguments match.
 * Arguments that are NULL or have "illegal" values are ignored.
 * Returns:
 *      On success: the descriptor the socksfd with matching arguments was
 *                registered with (>= 0).
 *      On failure: -1.
 */

int
socks_addrisours(const int s, socksfd_t *socksfd, const int takelock);
/*
 * Compares the current address of "s" to the registered address.
 * If there is a mismatch, the function will try to correct it if possible.
 *
 * If "takelock" is true, it means the function should take the
 * socksfdv/addrlock.
 *
 * If the current address matches the registered address and "socksfd"
 * is not NULL, "socksfd" is filled in with the data of the matching socket.
 *
 * Returns:
 *      If current address found to match registered: true.
 *      Else: false.
 */

void
update_after_negotiate(const socks_t *packet, socksfd_t *socksfd);
/*
 * Updates "socksfd" after a successful socks_negotiate() using
 * that used "packet".
 */

int
fdisopen(int fd);
/*
 * returns 1 if the file descriptor "fd" currently references a open object.
 * returns 0 otherwise.
 */


#if DIAGNOSTIC
void
cc_socksfdv(int sig);
/*
 * consistency check on socksfdv.
 */
#endif /* DIAGNOSTIC */


#if SOCKSLIBRARY_DYNAMIC

int sys_rresvport(int *);
int sys_bindresvport(int, struct sockaddr_in *);
void sys_freehostent(struct hostent *);

HAVE_PROT_READ_0
sys_read(HAVE_PROT_READ_1, HAVE_PROT_READ_2, HAVE_PROT_READ_3);
HAVE_PROT_LISTEN_0
sys_listen(HAVE_PROT_LISTEN_1, HAVE_PROT_LISTEN_2);
HAVE_PROT_READV_0
sys_readv(HAVE_PROT_READV_1, HAVE_PROT_READV_2, HAVE_PROT_READV_3);
HAVE_PROT_RECV_0
sys_recv(HAVE_PROT_RECV_1, HAVE_PROT_RECV_2, HAVE_PROT_RECV_3,
      HAVE_PROT_RECV_4);
HAVE_PROT_RECVMSG_0
sys_recvmsg(HAVE_PROT_RECVMSG_1, HAVE_PROT_RECVMSG_2, HAVE_PROT_RECVMSG_3);
HAVE_PROT_SEND_0
sys_send(HAVE_PROT_SEND_1, HAVE_PROT_SEND_2, HAVE_PROT_SEND_3,
      HAVE_PROT_SEND_4);
HAVE_PROT_WRITE_0
sys_write(HAVE_PROT_WRITE_1, HAVE_PROT_WRITE_2, HAVE_PROT_WRITE_3);

#if HAVE_OSF_OLDSTYLE
int sys_accept(int, struct sockaddr *, int *);
#else
HAVE_PROT_ACCEPT_0
sys_accept(HAVE_PROT_ACCEPT_1, HAVE_PROT_ACCEPT_2, HAVE_PROT_ACCEPT_3);
#endif /* HAVE_EXTRA_OSF_SYMBOLS */

#if HAVE_OSF_OLDSTYLE
int sys_bind(int, const struct sockaddr *, int);
#else
HAVE_PROT_BIND_0
sys_bind(HAVE_PROT_BIND_1, HAVE_PROT_BIND_2, HAVE_PROT_BIND_3);
#endif /* !HAVE_OSF_OLDSTYLE */

#if HAVE_OSF_OLDSTYLE
int sys_connect(int, const struct sockaddr *, int);
#else
HAVE_PROT_CONNECT_0
sys_connect(HAVE_PROT_CONNECT_1, HAVE_PROT_CONNECT_2, HAVE_PROT_CONNECT_3);
#endif /* HAVE_OSF_OLDSTYLE */

#if HAVE_OSF_OLDSTYLE
int sys_getpeername(int, struct sockaddr *, int *);
#else
HAVE_PROT_GETPEERNAME_0
sys_getpeername(HAVE_PROT_GETPEERNAME_1, HAVE_PROT_GETPEERNAME_2,
      HAVE_PROT_GETPEERNAME_3);
#endif /* HAVE_EXTRA_OSF_SYMBOLS */

#if HAVE_OSF_OLDSTYLE
int sys_getsockname(int, struct sockaddr *, int *);
#else
HAVE_PROT_GETSOCKNAME_0
sys_getsockname(HAVE_PROT_GETSOCKNAME_1, HAVE_PROT_GETSOCKNAME_2,
      HAVE_PROT_GETSOCKNAME_3);
HAVE_PROT_GETSOCKNAME_0
sys_getsockname_notracking(HAVE_PROT_GETSOCKNAME_1, HAVE_PROT_GETSOCKNAME_2,
      HAVE_PROT_GETSOCKNAME_3);
#endif /* HAVE_EXTRA_OSF_SYMBOLS */

HAVE_PROT_GETSOCKOPT_0
sys_getsockopt(HAVE_PROT_GETSOCKOPT_1, HAVE_PROT_GETSOCKOPT_2,
      HAVE_PROT_GETSOCKOPT_3, HAVE_PROT_GETSOCKOPT_4, HAVE_PROT_GETSOCKOPT_5);

#if HAVE_OSF_OLDSTYLE
int sys_recvfrom(int, void*, int, int, struct sockaddr *, int *);
#else
HAVE_PROT_RECVFROM_0
sys_recvfrom(HAVE_PROT_RECVFROM_1, HAVE_PROT_RECVFROM_2, HAVE_PROT_RECVFROM_3,
      HAVE_PROT_RECVFROM_4, HAVE_PROT_RECVFROM_5, HAVE_PROT_RECVFROM_6);
#endif /* HAVE_OSF_OLDSTYLE */

#if HAVE_OSF_OLDSTYLE
ssize_t sys_writev(int, const struct iovec *, int);
#else
HAVE_PROT_WRITEV_0
sys_writev(HAVE_PROT_WRITEV_1, HAVE_PROT_WRITEV_2, HAVE_PROT_WRITEV_3);
#endif /* HAVE_OSF_OLDSTYLE */

#if HAVE_OSF_OLDSTYLE
ssize_t sys_sendmsg(int, struct msghdr *, int);
#else
HAVE_PROT_SENDMSG_0
sys_sendmsg(HAVE_PROT_SENDMSG_1, HAVE_PROT_SENDMSG_2, HAVE_PROT_SENDMSG_3);
#endif /* HAVE_OSF_OLDSTYLE */

#if HAVE_OSF_OLDSTYLE
int sys_sendto(int, const void *, int, int, const struct sockaddr *,
      socklen_t);
#else
HAVE_PROT_SENDTO_0
sys_sendto(HAVE_PROT_SENDTO_1, HAVE_PROT_SENDTO_2, HAVE_PROT_SENDTO_3,
      HAVE_PROT_SENDTO_4, HAVE_PROT_SENDTO_5, HAVE_PROT_SENDTO_6);
#endif /* HAVE_OSF_OLDSTYLE */

#if HAVE_EXTRA_OSF_SYMBOLS
int sys_Eaccept(int, struct sockaddr *, socklen_t *);
int sys_Egetpeername(int, struct sockaddr *, socklen_t *);
int sys_Egetsockname(int, struct sockaddr *, socklen_t *);
ssize_t sys_Ereadv(int, const struct iovec *, int);
int sys_Erecvfrom(int, void *, size_t, int, struct sockaddr *, size_t *)
      __ATTRIBUTE__((__BOUNDED__(__buffer__, 2, 3)));
ssize_t sys_Erecvmsg(int, struct msghdr *, int);
ssize_t sys_Esendmsg(int, const struct msghdr *, int);
ssize_t sys_Ewritev(int, const struct iovec *, int);

int sys_naccept(int, struct sockaddr *, socklen_t *);
int sys_ngetpeername(int, struct sockaddr *, socklen_t *);
int sys_ngetsockname(int, struct sockaddr *, socklen_t *);
int sys_nrecvfrom(int, void *, size_t, int, struct sockaddr *, size_t *)
      __ATTRIBUTE__((__BOUNDED__(__buffer__, 2, 3)));
ssize_t sys_nrecvmsg(int, struct msghdr *, int);
ssize_t sys_nsendmsg(int, const struct msghdr *, int);
#endif /* HAVE_EXTRA_OSF_SYMBOLS */

#ifdef __sun
HAVE_PROT_BIND_0
sys_xnet_bind(HAVE_PROT_BIND_1, HAVE_PROT_BIND_2, HAVE_PROT_BIND_3);
HAVE_PROT_LISTEN_0
sys_xnet_listen(HAVE_PROT_LISTEN_1, HAVE_PROT_LISTEN_2);
HAVE_PROT_RECVMSG_0
sys_xnet_recvmsg(HAVE_PROT_RECVMSG_1, HAVE_PROT_RECVMSG_2, HAVE_PROT_RECVMSG_3);
HAVE_PROT_SENDMSG_0
sys_xnet_sendmsg(HAVE_PROT_SENDMSG_1, HAVE_PROT_SENDMSG_2, HAVE_PROT_SENDMSG_3);
HAVE_PROT_SENDTO_0
sys_xnet_sendto(HAVE_PROT_SENDTO_1, HAVE_PROT_SENDTO_2, HAVE_PROT_SENDTO_3,
   HAVE_PROT_SENDTO_4, HAVE_PROT_SENDTO_5, HAVE_PROT_SENDTO_6);
HAVE_PROT_CONNECT_0
sys_xnet_connect(HAVE_PROT_CONNECT_1, HAVE_PROT_CONNECT_2, HAVE_PROT_CONNECT_3);
#endif /* __sun */

#ifdef __FreeBSD__
HAVE_PROT_ACCEPT_0
_accept(HAVE_PROT_ACCEPT_1, HAVE_PROT_ACCEPT_2, HAVE_PROT_ACCEPT_3);
HAVE_PROT_BIND_0
_bind(HAVE_PROT_BIND_1, HAVE_PROT_BIND_2, HAVE_PROT_BIND_3);
HAVE_PROT_CONNECT_0
_connect(HAVE_PROT_CONNECT_1, HAVE_PROT_CONNECT_2, HAVE_PROT_CONNECT_3);
HAVE_PROT_GETPEERNAME_0
_getpeername(HAVE_PROT_GETPEERNAME_1, HAVE_PROT_GETPEERNAME_2,
    HAVE_PROT_GETPEERNAME_3);
HAVE_PROT_GETSOCKNAME_0
_getsockname(HAVE_PROT_GETSOCKNAME_1, HAVE_PROT_GETSOCKNAME_2,
    HAVE_PROT_GETSOCKNAME_3);
HAVE_PROT_GETSOCKOPT_0
_getsockopt(HAVE_PROT_GETSOCKOPT_1, HAVE_PROT_GETSOCKOPT_2,
    HAVE_PROT_GETSOCKOPT_3, HAVE_PROT_GETSOCKOPT_4, HAVE_PROT_GETSOCKOPT_5);
HAVE_PROT_LISTEN_0
_listen(HAVE_PROT_LISTEN_1, HAVE_PROT_LISTEN_2);
HAVE_PROT_READ_0
_read(HAVE_PROT_READ_1, HAVE_PROT_READ_2, HAVE_PROT_READ_3);
HAVE_PROT_READV_0
_readv(HAVE_PROT_READV_1, HAVE_PROT_READV_2, HAVE_PROT_READV_3);
HAVE_PROT_RECV_0
_recv(HAVE_PROT_RECV_1, HAVE_PROT_RECV_2, HAVE_PROT_RECV_3, HAVE_PROT_RECV_4);
HAVE_PROT_RECVFROM_0
_recvfrom(HAVE_PROT_RECVFROM_1, HAVE_PROT_RECVFROM_2, HAVE_PROT_RECVFROM_3,
    HAVE_PROT_RECVFROM_4, HAVE_PROT_RECVFROM_5, HAVE_PROT_RECVFROM_6);
HAVE_PROT_RECVMSG_0
_recvmsg(HAVE_PROT_RECVMSG_1, HAVE_PROT_RECVMSG_2, HAVE_PROT_RECVMSG_3);
HAVE_PROT_WRITE_0
_write(HAVE_PROT_WRITE_1, HAVE_PROT_WRITE_2, HAVE_PROT_WRITE_3);
HAVE_PROT_WRITEV_0
_writev(HAVE_PROT_WRITEV_1, HAVE_PROT_WRITEV_2, HAVE_PROT_WRITEV_3);
HAVE_PROT_SEND_0
_send(HAVE_PROT_SEND_1, HAVE_PROT_SEND_2, HAVE_PROT_SEND_3, HAVE_PROT_SEND_4);
HAVE_PROT_SENDMSG_0
_sendmsg(HAVE_PROT_SENDMSG_1, HAVE_PROT_SENDMSG_2, HAVE_PROT_SENDMSG_3);
HAVE_PROT_SENDTO_0
_sendto(HAVE_PROT_SENDTO_1, HAVE_PROT_SENDTO_2, HAVE_PROT_SENDTO_3,
    HAVE_PROT_SENDTO_4, HAVE_PROT_SENDTO_5, HAVE_PROT_SENDTO_6);
#endif /* __FreeBSD__ */

#if HAVE_GSSAPI && HAVE_LINUX_GLIBC_WORKAROUND
HAVE_PROT_GETC_0
sys_getc(HAVE_PROT_GETC_1);
HAVE_PROT_FGETC_0
sys_fgetc(HAVE_PROT_FGETC_1);
HAVE_PROT_GETS_0
sys_gets(HAVE_PROT_GETS_1);
HAVE_PROT_FGETS_0
sys_fgets(HAVE_PROT_FGETS_1, HAVE_PROT_FGETS_2, HAVE_PROT_FGETS_3);
HAVE_PROT_PUTC_0
sys_putc(HAVE_PROT_PUTC_1, HAVE_PROT_PUTC_2);
HAVE_PROT_FPUTC_0
sys_fputc(HAVE_PROT_FPUTC_1, HAVE_PROT_FPUTC_2);
HAVE_PROT_PUTS_0
sys_puts(HAVE_PROT_PUTS_1);
HAVE_PROT_FPUTS_0
sys_fputs(HAVE_PROT_FPUTS_1, HAVE_PROT_FPUTS_2);
HAVE_PROT_FFLUSH_0
sys_fflush(HAVE_PROT_FFLUSH_1);
HAVE_PROT_FCLOSE_0
sys_fclose(HAVE_PROT_FCLOSE_1);
HAVE_PROT_PRINTF_0
sys_printf(HAVE_PROT_PRINTF_1, ...);
HAVE_PROT_VPRINTF_0
sys_vprintf(HAVE_PROT_VPRINTF_1, HAVE_PROT_VPRINTF_2);
HAVE_PROT_FPRINTF_0
sys_fprintf(HAVE_PROT_FPRINTF_1, HAVE_PROT_FPRINTF_2, ...);
HAVE_PROT_VFPRINTF_0
sys_vfprintf(HAVE_PROT_VFPRINTF_1, HAVE_PROT_VFPRINTF_2, HAVE_PROT_VFPRINTF_3);
HAVE_PROT_FWRITE_0
sys_fwrite(HAVE_PROT_FWRITE_1, HAVE_PROT_FWRITE_2, HAVE_PROT_FWRITE_3,
      HAVE_PROT_FWRITE_4);
HAVE_PROT_FREAD_0
sys_fread(HAVE_PROT_FREAD_1, HAVE_PROT_FREAD_2, HAVE_PROT_FREAD_3,
      HAVE_PROT_FREAD_4);
#endif /* HAVE_GSSAPI && HAVE_LINUX_GLIBC_WORKAROUND */

#if HAVE_DARWIN

HAVE_PROT_READ_0
sys_read_nocancel(HAVE_PROT_READ_1, HAVE_PROT_READ_2, HAVE_PROT_READ_3);
HAVE_PROT_CONNECT_0
sys_connect_nocancel(HAVE_PROT_CONNECT_1, HAVE_PROT_CONNECT_2,
   HAVE_PROT_CONNECT_3 namelen);
HAVE_PROT_RECVFROM_0
sys_recvfrom_nocancel(HAVE_PROT_RECVFROM_1, HAVE_PROT_RECVFROM_2,
   HAVE_PROT_RECVFROM_3, HAVE_PROT_RECVFROM_4, HAVE_PROT_RECVFROM_5,
   HAVE_PROT_RECVFROM_6);
HAVE_PROT_SENDTO_0
sys_sendto_nocancel(HAVE_PROT_SENDTO_1, HAVE_PROT_SENDTO_2, HAVE_PROT_SENDTO_3,
   HAVE_PROT_SENDTO_4, HAVE_PROT_SENDTO_5, HAVE_PROT_SENDTO_6);
HAVE_PROT_WRITE_0
sys_write_nocancel(HAVE_PROT_WRITE_1, HAVE_PROT_WRITE_2, HAVE_PROT_WRITE_3);

#endif /* HAVE_DARWIN */

#endif /* SOCKSLIBRARY_DYNAMIC */

#endif /* !_SOCKS_H_ */