proxy.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. /*
  2. 3APA3A simpliest proxy server
  3. (c) 2002-2016 by Vladimir Dubrovin <3proxy@3proxy.ru>
  4. please read License Agreement
  5. */
  6. #define COPYRIGHT "(c)3APA3A, Vladimir Dubrovin & 3proxy.ru\n"\
  7. "Documentation and sources: http://3proxy.ru/\n"\
  8. "Please read license agreement in \'copying\' file.\n"\
  9. "You may not use this program without accepting license agreement"
  10. #ifndef _3PROXY_H_
  11. #define _3PROXY_H_
  12. #include "version.h"
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <string.h>
  16. #include <ctype.h>
  17. #include <sys/types.h>
  18. #include <sys/stat.h>
  19. #include <fcntl.h>
  20. #include <time.h>
  21. #define MAXUSERNAME 128
  22. #define _PASSWORD_LEN 256
  23. #define MAXNSERVERS 5
  24. #define UDPBUFSIZE 16384
  25. #define TCPBUFSIZE 8192
  26. #define SRVBUFSIZE (param->srv->bufsize?param->srv->bufsize:((param->service == S_UDPPM)?UDPBUFSIZE:TCPBUFSIZE))
  27. #ifdef _WIN32
  28. #include <winsock2.h>
  29. #include <sys/timeb.h>
  30. #ifndef _WINCE
  31. #include <io.h>
  32. #else
  33. #include <sys/unistd.h>
  34. #endif
  35. #include <process.h>
  36. #define SASIZETYPE int
  37. #define SHUT_RDWR SD_BOTH
  38. #else
  39. #ifndef FD_SETSIZE
  40. #define FD_SETSIZE 4096
  41. #endif
  42. #include <signal.h>
  43. #include <sys/uio.h>
  44. #include <sys/time.h>
  45. #include <unistd.h>
  46. #include <pthread.h>
  47. #include <syslog.h>
  48. #include <errno.h>
  49. #endif
  50. #ifdef __CYGWIN__
  51. #include <windows.h>
  52. #define daemonize() FreeConsole()
  53. #define SLEEPTIME 1000
  54. #undef _WIN32
  55. #elif _WIN32
  56. #ifdef errno
  57. #undef errno
  58. #endif
  59. #define errno WSAGetLastError()
  60. #ifdef EAGAIN
  61. #undef EAGAIN
  62. #endif
  63. #define EAGAIN WSAEWOULDBLOCK
  64. #ifdef EINTR
  65. #undef EINTR
  66. #endif
  67. #ifndef EINPROGRESS
  68. #define EINPROGRESS WSAEWOULDBLOCK
  69. #endif
  70. #define EINTR WSAEWOULDBLOCK
  71. #define SLEEPTIME 1
  72. #define usleep Sleep
  73. #define pthread_self GetCurrentThreadId
  74. #define getpid GetCurrentProcessId
  75. #define pthread_t unsigned
  76. #ifndef _WINCE
  77. #define daemonize() FreeConsole()
  78. #else
  79. #define daemonize()
  80. #endif
  81. #define socket(x, y, z) WSASocket(x, y, z, NULL, 0, 0)
  82. #define accept(x, y, z) WSAAccept(x, y, z, NULL, 0)
  83. #define ftruncate chsize
  84. #else
  85. #include <pthread.h>
  86. #ifndef PTHREAD_STACK_MIN
  87. #define PTHREAD_STACK_MIN 32768
  88. #define sockerror strerror
  89. #endif
  90. void daemonize(void);
  91. #define SLEEPTIME 1000
  92. #ifndef O_BINARY
  93. #define O_BINARY 0
  94. #endif
  95. #endif
  96. #ifndef NOODBC
  97. #ifndef _WIN32
  98. #include <sqltypes.h>
  99. #endif
  100. #include <sql.h>
  101. #include <sqlext.h>
  102. #endif
  103. #ifdef _WIN32
  104. #define strcasecmp stricmp
  105. #define strncasecmp strnicmp
  106. #endif
  107. #ifndef SOCKET_ERROR
  108. #define SOCKET_ERROR -1
  109. #endif
  110. #ifndef isnumber
  111. #define isnumber(n) (n >= '0' && n <= '9')
  112. #endif
  113. #ifndef ishex
  114. #define ishex(n) ((n >= '0' && n <= '9') || (n >= 'a' && n<='f') || (n >= 'A' && n <= 'F'))
  115. #endif
  116. #define isallowed(n) ((n >= '0' && n <= '9') || (n >= 'a' && n <= 'z') || (n >= 'A' && n <= 'Z') || (n >= '*' && n <= '/') || n == '_')
  117. #include "structures.h"
  118. #define MAXRADIUS 5
  119. #define DEFLOGFORMAT "G%y%m%d%H%M%S.%. %p %E %U %C:%c %R:%r %O %I %h %T"
  120. #define myalloc malloc
  121. #define myfree free
  122. #define myrealloc realloc
  123. #define mystrdup strdup
  124. extern RESOLVFUNC resolvfunc;
  125. extern int wday;
  126. extern time_t basetime;
  127. extern int timetoexit;
  128. extern struct extparam conf;
  129. int sockmap(struct clientparam * param, int timeo, int usesplice);
  130. int socksend(SOCKET sock, unsigned char * buf, int bufsize, int to);
  131. int socksendto(SOCKET sock, struct sockaddr * sin, unsigned char * buf, int bufsize, int to);
  132. int sockrecvfrom(SOCKET sock, struct sockaddr * sin, unsigned char * buf, int bufsize, int to);
  133. int sockgetcharcli(struct clientparam * param, int timeosec, int timeousec);
  134. int sockgetcharsrv(struct clientparam * param, int timeosec, int timeousec);
  135. int sockfillbuffcli(struct clientparam * param, unsigned long size, int timeosec);
  136. int sockfillbuffsrv(struct clientparam * param, unsigned long size, int timeosec);
  137. int sockgetlinebuf(struct clientparam * param, DIRECTION which, unsigned char * buf, int bufsize, int delim, int to);
  138. void initlog(void);
  139. void dolog(struct clientparam * param, const unsigned char *s);
  140. int dobuf(struct clientparam * param, unsigned char * buf, const unsigned char *s, const unsigned char * doublec);
  141. int dobuf2(struct clientparam * param, unsigned char * buf, const unsigned char *s, const unsigned char * doublec, struct tm* tm, char * format);
  142. int doconnect(struct clientparam * param);
  143. int alwaysauth(struct clientparam * param);
  144. int ipauth(struct clientparam * param);
  145. int doauth(struct clientparam * param);
  146. int strongauth(struct clientparam * param);
  147. void trafcountfunc(struct clientparam *param);
  148. unsigned bandlimitfunc(struct clientparam *param, unsigned nbytesin, unsigned nbytesout);
  149. int scanaddr(const unsigned char *s, unsigned long * ip, unsigned long * mask);
  150. int myinet_ntop(int af, void *src, char *dst, socklen_t size);
  151. extern struct nserver nservers[MAXNSERVERS];
  152. extern struct nserver authnserver;
  153. unsigned long getip(unsigned char *name);
  154. unsigned long getip46(int family, unsigned char *name, struct sockaddr *sa);
  155. int afdetect(unsigned char *name);
  156. unsigned long myresolver(int, unsigned char *, unsigned char *);
  157. unsigned long fakeresolver (int, unsigned char *, unsigned char*);
  158. int inithashtable(struct hashtable *hashtable, unsigned nhashsize);
  159. void freeparam(struct clientparam * param);
  160. void clearstat(struct clientparam * param);
  161. void dumpcounters(struct trafcount *tl, int counterd);
  162. int startconnlims (struct clientparam *param);
  163. void stopconnlims (struct clientparam *param);
  164. extern struct auth authfuncs[];
  165. int reload (void);
  166. extern int paused;
  167. extern int demon;
  168. unsigned char * mycrypt(const unsigned char *key, const unsigned char *salt, unsigned char *buf);
  169. unsigned char * ntpwdhash (unsigned char *szHash, const unsigned char *szPassword, int tohex);
  170. int de64 (const unsigned char *in, unsigned char *out, int maxlen);
  171. unsigned char* en64 (const unsigned char *in, unsigned char *out, int inlen);
  172. void tohex(unsigned char *in, unsigned char *out, int len);
  173. void fromhex(unsigned char *in, unsigned char *out, int len);
  174. int ftplogin(struct clientparam *param, char *buf, int *inbuf);
  175. int ftpcd(struct clientparam *param, unsigned char* path, char *buf, int *inbuf);
  176. int ftpsyst(struct clientparam *param, unsigned char *buf, unsigned len);
  177. int ftppwd(struct clientparam *param, unsigned char *buf, unsigned len);
  178. int ftptype(struct clientparam *param, unsigned char* f_type);
  179. int ftpres(struct clientparam *param, unsigned char * buf, int len);
  180. SOCKET ftpcommand(struct clientparam *param, unsigned char * command, unsigned char *arg);
  181. int text2unicode(const char * text, char * buf, int buflen);
  182. void unicode2text(const char *unicode, char * buf, int len);
  183. void genchallenge(struct clientparam *param, char * challenge, char *buf);
  184. void mschap(const unsigned char *win_password,
  185. const unsigned char *challenge, unsigned char *response);
  186. struct hashtable;
  187. void hashadd(struct hashtable *ht, const unsigned char* name, unsigned char* value, time_t expires);
  188. int parsehost(int family, unsigned char *host, struct sockaddr *sa);
  189. int parsehostname(char *hostname, struct clientparam *param, unsigned short port);
  190. int parseusername(char *username, struct clientparam *param, int extpasswd);
  191. int parseconnusername(char *username, struct clientparam *param, int extpasswd, unsigned short port);
  192. int ACLmatches(struct ace* acentry, struct clientparam * param);
  193. int checkACL(struct clientparam * param);
  194. extern int havelog;
  195. unsigned long udpresolve(int af, unsigned char * name, unsigned char * value, unsigned *retttl, struct clientparam* param, int makeauth);
  196. struct ace * copyacl (struct ace *ac);
  197. struct auth * copyauth (struct auth *);
  198. void * itfree(void *data, void * retval);
  199. void freeacl(struct ace *ac);
  200. void freeauth(struct auth *);
  201. void freefilter(struct filter *filter);
  202. void freeconf(struct extparam *confp);
  203. struct passwords * copypwl (struct passwords *pwl);
  204. void freepwl(struct passwords *pw);
  205. void copyfilter(struct filter *, struct srvparam *srv);
  206. FILTER_ACTION makefilters (struct srvparam *srv, struct clientparam *param);
  207. FILTER_ACTION handlereqfilters(struct clientparam *param, unsigned char ** buf_p, int * bufsize_p, int offset, int * length_p);
  208. FILTER_ACTION handlehdrfilterscli(struct clientparam *param, unsigned char ** buf_p, int * bufsize_p, int offset, int * length_p);
  209. FILTER_ACTION handlehdrfilterssrv(struct clientparam *param, unsigned char ** buf_p, int * bufsize_p, int offset, int * length_p);
  210. FILTER_ACTION handlepredatflt(struct clientparam *param);
  211. FILTER_ACTION handledatfltcli(struct clientparam *param, unsigned char ** buf_p, int * bufsize_p, int offset, int * length_p);
  212. FILTER_ACTION handledatfltsrv(struct clientparam *param, unsigned char ** buf_p, int * bufsize_p, int offset, int * length_p);
  213. void srvinit(struct srvparam * srv, struct clientparam *param);
  214. void srvinit2(struct srvparam * srv, struct clientparam *param);
  215. void srvfree(struct srvparam * srv);
  216. unsigned char * dologname (unsigned char *buf, unsigned char *name, const unsigned char *ext, ROTATION lt, time_t t);
  217. int readconfig(FILE * fp);
  218. int connectwithpoll(SOCKET sock, struct sockaddr *sa, SASIZETYPE size, int to);
  219. int myrand(void * entropy, int len);
  220. extern char *copyright;
  221. #define SERVICES 5
  222. void * dnsprchild(struct clientparam * param);
  223. void * pop3pchild(struct clientparam * param);
  224. void * smtppchild(struct clientparam * param);
  225. void * proxychild(struct clientparam * param);
  226. void * sockschild(struct clientparam * param);
  227. void * tcppmchild(struct clientparam * param);
  228. void * udppmchild(struct clientparam * param);
  229. void * adminchild(struct clientparam * param);
  230. void * ftpprchild(struct clientparam * param);
  231. struct datatype;
  232. struct dictionary;
  233. struct node;
  234. struct property;
  235. extern pthread_mutex_t config_mutex;
  236. extern pthread_mutex_t bandlim_mutex;
  237. extern pthread_mutex_t connlim_mutex;
  238. extern pthread_mutex_t hash_mutex;
  239. extern pthread_mutex_t tc_mutex;
  240. extern pthread_mutex_t pwl_mutex;
  241. extern pthread_mutex_t log_mutex;
  242. extern pthread_mutex_t rad_mutex;
  243. extern struct datatype datatypes[64];
  244. extern struct commands commandhandlers[];
  245. #ifdef WITHSPLICE
  246. #define mapsocket(a,b) ((a->srv->usesplice && !a->ndatfilterssrv && !a->ndatfilterscli)?sockmap(a,b,1):sockmap(a,b,0))
  247. #else
  248. #define mapsocket(a,b) sockmap(a,b, 0)
  249. #endif
  250. extern struct radserver {
  251. #ifdef NOIPV6
  252. struct sockaddr_in authaddr, logaddr;
  253. #else
  254. struct sockaddr_in6 authaddr, logaddr;
  255. #endif
  256. /*
  257. SOCKET logsock;
  258. */
  259. } radiuslist[MAXRADIUS];
  260. extern char radiussecret[64];
  261. extern int nradservers;
  262. extern struct socketoptions {
  263. int opt;
  264. char * optname;
  265. } sockopts[];
  266. void setopts(SOCKET s, int opts);
  267. char * printopts(char *sep);
  268. #ifdef _WINCE
  269. char * CEToUnicode (const char *str);
  270. int cesystem(const char *str);
  271. int ceparseargs(const char *str);
  272. extern char * ceargv[32];
  273. #define system(S) cesystem(S)
  274. #endif
  275. #define WEBBANNERS 35
  276. #endif