proxy.h 11 KB

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