📄 common.h
字号:
/* * Copyright (c) 1997, 1998, 1999 * 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 * Gaustadal閑n 21 * N-0349 Oslo * Norway * * any improvements or extensions that they make and grant Inferno Nettverk A/S * the rights to redistribute these changes. * *//* $Id: common.h,v 1.218 1999/12/22 09:29:19 karls Exp $ */#ifndef _COMMON_H_#define _COMMON_H_#endif/* XXX ifdef, not if, defined on command line */#ifdef HAVE_CONFIG_H#include "autoconf.h"#endif /* HAVE_CONFIG_H */#include <sys/types.h>#include <sys/time.h>#if HAVE_SYS_FILE_H#include <sys/file.h>#endif /* HAVE_SYS_FILE_H */#include <sys/resource.h>#include <sys/ioctl.h>#include <sys/ipc.h>#include <sys/sem.h>/* XXX This is a hack. Avoid transparent sockaddr union used in linux to avoid the use of the union in the code. Mainly used in interposition.c*/#ifdef WE_DONT_WANT_NO_SOCKADDR_ARG_UNION#ifdef __GNUC__#define __HAD_GNUC __GNUC__#undef __GNUC__#endif /* __GNUC__ */#endif /* WE_DONT_WANT_NO_SOCKADDR_ARG_UNION */#include <sys/socket.h>#ifdef __HAD_GNUC#define __GNUC__ __HAD_GNUC#endif /* __HAD_GNUC */#if NEED_SYS_SOCKIO_H#include <sys/sockio.h>#endif /* NEED_SYS_SOCKIO_H */#include <sys/mman.h>#include <sys/un.h>#include <sys/stat.h>#include <sys/uio.h>#include <sys/wait.h>#include <netinet/in.h>#if HAVE_NETINET_IP_H#include <netinet/ip.h>#endif /* HAVE_NETINET_IP_H */#if HAVE_NETINET_IP_VAR_H#include <netinet/ip_var.h>#endif /* HAVE_NETINET_IP_VAR_H */#include <arpa/inet.h>#include <arpa/nameser.h>#include <assert.h>#if HAVE_CRYPT_H#include <crypt.h>#endif /* HAVE_CRYPT_H */#include <ctype.h>#ifdef SOCKSLIBRARY_DYNAMIC#include <dlfcn.h>#endif /* SOCKSLIBRARY_DYNAMIC */#include <errno.h>#include <fcntl.h>#if HAVE_LIMITS_H#include <limits.h>#endif /* HAVE_LIMITS_H */#include <netdb.h>#include <pwd.h>#include <setjmp.h>#include <signal.h>#ifdef STDC_HEADERS#include <stdarg.h>#else#include <varargs.h>#endif /* STDC_HEADERS */#include <stdio.h>#include <stdlib.h>#if HAVE_STRINGS_H#include <strings.h>#endif /* HAVE_STRINGS_H */#if STDC_HEADERS#include <string.h>#endif /* STDC_HEADERS */#include <syslog.h>#if HAVE_LIBWRAP && HAVE_TCPD_H#include <tcpd.h>#endif /* HAVE_LIBWRAP && HAVE_TCPD_H */#include <time.h>#if HAVE_UNISTD_H#ifdef HAVE_DEC_PROTO#undef _XOPEN_SOURCE_EXTENDED#endif /* HAVE_DEC_PROTO */#include <unistd.h>#ifdef HAVE_DEC_PROTO#define _XOPEN_SOURCE_EXTENDED#endif /* HAVE_DEC_PROTO */#endif /* HAVE_UNISTD_H */#include <resolv.h>#include "yacconfig.h"#ifdef lintextern const int lintnoloop_common_h;#else#define lintnoloop_common_h 0#endif#include "config.h"#define SOCKS_TRYHARDER 0 /* XXX BUGS */ /* XXX should be configure option. */#ifndef RLIMIT_OFILE#define RLIMIT_OFILE RLIMIT_NOFILE#endif /* !RLIMIT_OFILE */#if NEED_GETSOCKOPT_CAST#define getsockopt(a,b,c,d,e) getsockopt((a),(b),(c),(char *)(d),(e))#define setsockopt(a,b,c,d,e) setsockopt((a),(b),(c),(char *)(d),(e))#endif /* NEED_GETSOCKOPT_CAST */#if !HAVE_BZERO#define bzero(b, len) memset(b, 0, len)#endif /* !HAVE_BZERO */#if !HAVE_SIG_ATOMIC_Ttypedef int sig_atomic_t;#endif /* !HAVE_SIG_ATOMIC_T */#ifdef DIAGNOSTIC#undef DIAGNOSTIC#define DIAGNOSTIC 1#else#define DIAGNOSTIC 0#endif#ifdef DEBUG#undef DEBUG#define DEBUG 1#else#define DEBUG 0#endif#if DEBUG#if !DIAGNOSTIC#undef DIAGNOSTIC#define DIAGNOSTIC 1#endif /* !DIAGNOSTIC *//* * solaris 2.5.1 and it's stream stuff is broken and puts the processes * into never-never land forever on half the sendmsg() calls if they * involve ancillary data. (it seems to deadlock the processes.) */#ifndef HAVE_SENDMSG_DEADLOCK#define HAVE_SENDMSG_DEADLOCK 1#endif#ifndef HAVE_ACCEPTLOCK#define HAVE_ACCEPTLOCK 1#endif#endif /* DEBUG *//* __P and __BEGIN_DECLS definitions taken from libtool manual */#undef __BEGIN_DECLS#undef __END_DECLS#ifdef __cplusplus# define __BEGIN_DECLS extern "C" {# define __END_DECLS }#else# define __BEGIN_DECLS /* empty */# define __END_DECLS /* empty */#endif#undef __P#if defined (__STDC__) || defined (_AIX) \ || (defined (__mips) && defined (_SYSTYPE_SVR4)) \ || defined(WIN32) || defined(__cplusplus)# define __P(protos) protos#else# define __P(protos) ()#endif#if SIZEOF_CHAR == 1 typedef unsigned char ubits_8; typedef char sbits_8;#elseerror "no known 8 bits wide datatype"#endif#if SIZEOF_SHORT == 2 typedef unsigned short ubits_16; typedef short sbits_16;#else# if SIZEOF_INT == 2 typedef unsigned int ubits_16; typedef int sbits_16;# elseerror "no known 16 bits wide datatype"# endif#endif#if SIZEOF_INT == 4 typedef unsigned int ubits_32; typedef int sbits_32;#else# if SIZEOF_SHORT == 4 typedef unsigned short ubits_32; typedef short sbits_32;# else# if SIZEOF_LONG == 4 typedef unsigned long ubits_32; typedef long sbits_32;# elseerror "no known 32 bits wide datatype"# endif /* SIZEOF_LONG == 4 */# endif /* SIZEOF_SHORT == 4 */#endif /* SIZEOF_INT == 4 */#ifndef INADDR_NONE# define INADDR_NONE (ubits_32) 0xffffffff#endif /* !INADDR_NONE */#ifndef MAX# define MAX(a,b) ((a) > (b) ? (a) : (b))#endif /* !MAX */#ifndef MIN# define MIN(a,b) ((a) < (b) ? (a) : (b))#endif /* !MIN */#if NEED_EXIT_FAILURE/* XXX assumes EXIT_SUCCESS is undefined too */#define EXIT_SUCCESS 0#define EXIT_FAILURE 1#endif /* NEED_EXIT_FAILURE */#if NEED_SA_RESTART#define SA_RESTART SV_INTERRUPT#endif /* NEED_SA_RESTART */#if NEED_AF_LOCAL#define AF_LOCAL AF_UNIX#endif /* NEED_AF_LOCAL */#if HAVE_NOMALLOC_REALLOC#define realloc(p,s) (((p) == NULL) ? (malloc(s)) : (realloc((p),(s))))#endif /* HAVE_NOMALLOC_REALLOC */#if HAVE_NONULL_FREE#define free(p) (((p) == NULL) ? ((void)(p)) : (free(p)))#endif/* __CONCAT macro from anoncvs */#ifndef __CONCAT#if defined(__STDC__) || defined(__cplusplus)#define __CONCAT(x,y) x ## y#else#define __CONCAT(x,y) x/**/y#endif#endif#if !HAVE_STRUCT_IPOPTS#define MAX_IPOPTLEN 40struct ipoption { struct in_addr ipopt_dst; sbits_8 ipopt_list[MAX_IPOPTLEN];};#endif /* !HAVE_STRUCT_IPOPTS *//* global variables needed by everyone. */extern struct config_t config;extern char *__progname;#if !HAVE_H_ERRNOextern int h_errno;#endif /* !HAVE_H_ERRNO */ /* * defines *//* * redefine system limits to match that of socks protocol. * No need for these to be bigger than protocol allows, but they * _must_ be atleast as big as protocol allows. */#ifdef MAXHOSTNAMELEN#undef MAXHOSTNAMELEN#endif#define MAXHOSTNAMELEN (255 + 1) /* socks5: 255, +1 for len. */#ifdef MAXNAMELEN#undef MAXNAMELEN#endif#define MAXNAMELEN (255 + 1) /* socks5: 255, +1 for len. */#ifdef MAXPWLEN#undef MAXPWLEN#endif#define MAXPWLEN (255 + 1) /* socks5: 255, +1 for len. *//* "255." "255." "255." "255" "." "65535" + NUL */#define MAXSOCKADDRSTRING (4 + 4 + 4 + 3 + 1 + 5 + 1)/* "." + "65535" + NUL */#define MAXSOCKSHOSTSTRING (MAXHOSTNAMELEN + 1 + 5)#define MAXRULEADDRSTRING (MAXSOCKSHOSTSTRING * 2)#ifndef NUL#define NUL '\0'#endif/* * We don't care whether it's called O_NONBLOCK, FNDELAY or whatever. * We just want to know whether the flags set give blocking or nonblocking * semantics. */#ifndef FNDELAY#define NONBLOCKING (O_NONBLOCK | O_NDELAY)#else#define NONBLOCKING (O_NONBLOCK | FNDELAY | O_NDELAY)#endif#define CONFIGTYPE_SERVER 1#define CONFIGTYPE_CLIENT 2#define PROTOCOL_TCPs "tcp"#define PROTOCOL_UDPs "udp"#define PROTOCOL_UNKNOWNs "unknown"#define RESOLVEPROTOCOL_UDP 0#define RESOLVEPROTOCOL_TCP 1#define RESOLVEPROTOCOL_FAKE 2#define LOGTYPE_SYSLOG 0x1#define LOGTYPE_FILE 0x2#define NOMEM "<memory exhausted>" /* * macros */#define close(n) closen(n)#define PORTISRESERVED(port) \ (ntohs((port)) != 0 && ntohs((port)) < IPPORT_RESERVED)#define ADDRISBOUND(addr) \ ((((struct sockaddr_in *)(&addr))->sin_addr.s_addr != htonl(INADDR_ANY)) \ || (((struct sockaddr_in *)(&addr))->sin_port != htons(0)))#define ELEMENTS(array) (sizeof(array) / sizeof(array[0]))#if UCHAR_MAX > 0xff#define OCTETIFY(a) ((a) = ((a) & 0xff))#else#define OCTETIFY(a) ((a) = (a))#endif/* * Note that it's the argument that will be truncated, not just the * returnvalue. *//* * macros to manipulate ancillary data depending on if were on sysv or bsd. *//* allocate memory for data. "size" is the amount of memory to allocate. */#if HAVE_CMSGHDR#define CMSG_AALLOC(size) \ union { \ char cmsgmem[sizeof(struct cmsghdr) + (size)]; \ struct cmsghdr align; \ } cmsgmem; \ struct cmsghdr *cmsg = &cmsgmem.align#else /* !HAVE_CMSGHDR */#define CMSG_AALLOC(size) \ char cmsgmem[(size)]#endif /* !HAVE_CMSGHDR *//* * add a object to data. "object" is the object to add to data at * offset "offset". */#if HAVE_CMSGHDR#define CMSG_ADDOBJECT(object, offset) \ do \ memcpy(CMSG_DATA(cmsg) + (offset), &(object), sizeof(object)); \ while (lintnoloop_common_h)#else /* !HAVE_CMSGHDR */#define CMSG_ADDOBJECT(object, offset) \ do \ memcpy(cmsgmem + (offset), &(object), sizeof((object))); \ while (lintnoloop_common_h)#endif /* !HAVE_CMSGHDR *//* * get a object from data. "object" is the object to get from data at * offset "offset". */#if HAVE_CMSGHDR#define CMSG_GETOBJECT(object, offset) \ do \ memcpy(&(object), CMSG_DATA(cmsg) + (offset), sizeof((object))); \ while (lintnoloop_common_h)#else /* !HAVE_CMSGHDR */#define CMSG_GETOBJECT(object, offset) \ do \ memcpy(&(object), cmsgmem + (offset), sizeof((object))); \ while (lintnoloop_common_h)#endif /* !HAVE_CMSGHDR *//* set cmsg for sending */#if HAVE_CMSGHDR#define CMSG_SETHDR_SEND(size) \ do { \ cmsg->cmsg_level = SOL_SOCKET; \ cmsg->cmsg_type = SCM_RIGHTS; \ cmsg->cmsg_len = sizeof(struct cmsghdr) + (size); \ \ msg.msg_control = (caddr_t)cmsg; \ msg.msg_controllen = cmsg->cmsg_len; \ } while (lintnoloop_common_h)#else /* !HAVE_CMSGHDR */#define CMSG_SETHDR_SEND(size) \ do { \ msg.msg_accrights = (caddr_t)cmsgmem; \ msg.msg_accrightslen = (size); \ } while (lintnoloop_common_h)#endif /* !HAVE_CMSGHDR *//* set cmsg for receiving */#if HAVE_CMSGHDR#define CMSG_SETHDR_RECV(size) \ do { \ msg.msg_control = (caddr_t)cmsg; \ msg.msg_controllen = (size); \ } while (lintnoloop_common_h)#else /* !HAVE_CMSGHDR */#define CMSG_SETHDR_RECV(size) \ do { \ msg.msg_accrights = (caddr_t)cmsgmem; \ msg.msg_accrightslen = (size); \ } while (lintnoloop_common_h)#endif /* !HAVE_CMSGHDR *//* returns length of controldata actually sent. */#if HAVE_CMSGHDR#define CMSG_GETLEN(msg) (msg.msg_controllen - sizeof(struct cmsghdr))#else#define CMSG_GETLEN(msg) (msg.msg_accrightslen)#endif#define INTERNAL_ERROR \"an internal error was detected at %s:%d\nvalue = %ld, version = %s"
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -