proxy.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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. #define EINTR WSAEWOULDBLOCK
  76. #define SLEEPTIME 1
  77. #define usleep Sleep
  78. #define pthread_self GetCurrentThreadId
  79. #define getpid GetCurrentProcessId
  80. #define pthread_t unsigned
  81. #ifndef _WINCE
  82. #define daemonize() FreeConsole()
  83. #else
  84. #define daemonize()
  85. #endif
  86. #define socket(x, y, z) WSASocket(x, y, z, NULL, 0, 0)
  87. #define accept(x, y, z) WSAAccept(x, y, z, NULL, 0)
  88. #define ftruncate chsize
  89. #else
  90. #include <pthread.h>
  91. #ifndef PTHREAD_STACK_MIN
  92. #define PTHREAD_STACK_MIN 32768
  93. #define sockerror strerror
  94. #endif
  95. void daemonize(void);
  96. #define SLEEPTIME 1000
  97. #ifndef O_BINARY
  98. #define O_BINARY 0
  99. #endif
  100. #endif
  101. #ifndef NOODBC
  102. #ifndef _WIN32
  103. #include <sqltypes.h>
  104. #endif
  105. #include <sql.h>
  106. #include <sqlext.h>
  107. #endif
  108. #ifdef _WIN32
  109. #define strcasecmp stricmp
  110. #define strncasecmp strnicmp
  111. #endif
  112. #ifndef SOCKET_ERROR
  113. #define SOCKET_ERROR -1
  114. #endif
  115. #ifndef isnumber
  116. #define isnumber(n) (n >= '0' && n <= '9')
  117. #endif
  118. #ifndef ishex
  119. #define ishex(n) ((n >= '0' && n <= '9') || (n >= 'a' && n<='f') || (n >= 'A' && n <= 'F'))
  120. #endif
  121. #define isallowed(n) ((n >= '0' && n <= '9') || (n >= 'a' && n <= 'z') || (n >= 'A' && n <= 'Z') || (n >= '*' && n <= '/') || n == '_')
  122. #include "structures.h"
  123. extern RESOLVFUNC resolvfunc;
  124. extern int wday;
  125. extern time_t basetime;
  126. extern int timetoexit;
  127. extern struct extparam conf;
  128. int sockmap(struct clientparam * param, int timeo);
  129. int splicemap(struct clientparam * param, int timeo);
  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. int dobuf(struct clientparam * param, unsigned char * buf, const unsigned char *s, const unsigned char * doublec);
  139. int dobuf2(struct clientparam * param, unsigned char * buf, const unsigned char *s, const unsigned char * doublec, struct tm* tm, char * format);
  140. extern FILE * stdlog;
  141. void logstdout(struct clientparam * param, const unsigned char *s);
  142. void logsyslog(struct clientparam * param, const unsigned char *s);
  143. void lognone(struct clientparam * param, const unsigned char *s);
  144. #ifndef NOSQL
  145. void logsql(struct clientparam * param, const unsigned char *s);
  146. int init_sql(char * s);
  147. void close_sql();
  148. #endif
  149. int doconnect(struct clientparam * param);
  150. int alwaysauth(struct clientparam * param);
  151. int ipauth(struct clientparam * param);
  152. int doauth(struct clientparam * param);
  153. int strongauth(struct clientparam * param);
  154. void trafcountfunc(struct clientparam *param);
  155. unsigned bandlimitfunc(struct clientparam *param, unsigned nbytesin, unsigned nbytesout);
  156. int scanaddr(const unsigned char *s, unsigned long * ip, unsigned long * mask);
  157. int myinet_ntop(int af, void *src, char *dst, socklen_t size);
  158. extern struct nserver nservers[MAXNSERVERS];
  159. extern struct nserver authnserver;
  160. unsigned long getip(unsigned char *name);
  161. unsigned long getip46(int family, unsigned char *name, struct sockaddr *sa);
  162. unsigned long myresolver(int, unsigned char *, unsigned char *);
  163. unsigned long fakeresolver (int, unsigned char *, unsigned char*);
  164. int inithashtable(struct hashtable *hashtable, unsigned nhashsize);
  165. void freeparam(struct clientparam * param);
  166. void clearstat(struct clientparam * param);
  167. void dumpcounters(struct trafcount *tl, int counterd);
  168. extern struct auth authfuncs[];
  169. int reload (void);
  170. extern int paused;
  171. extern int demon;
  172. unsigned char * mycrypt(const unsigned char *key, const unsigned char *salt, unsigned char *buf);
  173. unsigned char * ntpwdhash (unsigned char *szHash, const unsigned char *szPassword, int tohex);
  174. int de64 (const unsigned char *in, unsigned char *out, int maxlen);
  175. unsigned char* en64 (const unsigned char *in, unsigned char *out, int inlen);
  176. void tohex(unsigned char *in, unsigned char *out, int len);
  177. void fromhex(unsigned char *in, unsigned char *out, int len);
  178. int ftplogin(struct clientparam *param, char *buf, int *inbuf);
  179. int ftpcd(struct clientparam *param, unsigned char* path, char *buf, int *inbuf);
  180. int ftpsyst(struct clientparam *param, unsigned char *buf, unsigned len);
  181. int ftppwd(struct clientparam *param, unsigned char *buf, unsigned len);
  182. int ftptype(struct clientparam *param, unsigned char* f_type);
  183. int ftpres(struct clientparam *param, unsigned char * buf, int len);
  184. SOCKET ftpcommand(struct clientparam *param, unsigned char * command, unsigned char *arg);
  185. int text2unicode(const char * text, char * buf, int buflen);
  186. void unicode2text(const char *unicode, char * buf, int len);
  187. void genchallenge(struct clientparam *param, char * challenge, char *buf);
  188. void mschap(const unsigned char *win_password,
  189. const unsigned char *challenge, unsigned char *response);
  190. struct hashtable;
  191. void hashadd(struct hashtable *ht, const unsigned char* name, unsigned char* value, time_t expires);
  192. int parsehost(int family, unsigned char *host, struct sockaddr *sa);
  193. int parsehostname(char *hostname, struct clientparam *param, unsigned short port);
  194. int parseusername(char *username, struct clientparam *param, int extpasswd);
  195. int parseconnusername(char *username, struct clientparam *param, int extpasswd, unsigned short port);
  196. int ACLmatches(struct ace* acentry, struct clientparam * param);
  197. int checkACL(struct clientparam * param);
  198. unsigned long udpresolve(int af, unsigned char * name, unsigned char * value, unsigned *retttl, struct clientparam* param, int makeauth);
  199. struct ace * copyacl (struct ace *ac);
  200. struct auth * copyauth (struct auth *);
  201. void * itfree(void *data, void * retval);
  202. void freeacl(struct ace *ac);
  203. void freeauth(struct auth *);
  204. void freefilter(struct filter *filter);
  205. void freeconf(struct extparam *confp);
  206. struct passwords * copypwl (struct passwords *pwl);
  207. void freepwl(struct passwords *pw);
  208. void copyfilter(struct filter *, struct srvparam *srv);
  209. FILTER_ACTION makefilters (struct srvparam *srv, struct clientparam *param);
  210. FILTER_ACTION handlereqfilters(struct clientparam *param, unsigned char ** buf_p, int * bufsize_p, int offset, int * length_p);
  211. FILTER_ACTION handlehdrfilterscli(struct clientparam *param, unsigned char ** buf_p, int * bufsize_p, int offset, int * length_p);
  212. FILTER_ACTION handlehdrfilterssrv(struct clientparam *param, unsigned char ** buf_p, int * bufsize_p, int offset, int * length_p);
  213. FILTER_ACTION handlepredatflt(struct clientparam *param);
  214. FILTER_ACTION handledatfltcli(struct clientparam *param, unsigned char ** buf_p, int * bufsize_p, int offset, int * length_p);
  215. FILTER_ACTION handledatfltsrv(struct clientparam *param, unsigned char ** buf_p, int * bufsize_p, int offset, int * length_p);
  216. void srvinit(struct srvparam * srv, struct clientparam *param);
  217. void srvinit2(struct srvparam * srv, struct clientparam *param);
  218. void srvfree(struct srvparam * srv);
  219. unsigned char * dologname (unsigned char *buf, unsigned char *name, const unsigned char *ext, ROTATION lt, time_t t);
  220. int readconfig(FILE * fp);
  221. int myrand(void * entropy, int len);
  222. #ifdef WITH_STD_MALLOC
  223. #define myalloc malloc
  224. #define myfree free
  225. #define myrealloc realloc
  226. #define mystrdup strdup
  227. #else
  228. void *myalloc(size_t size);
  229. void myfree(void *ptr);
  230. void *myrealloc(void *ptr, size_t size);
  231. char * mystrdup(const char *str);
  232. #endif
  233. extern char *copyright;
  234. #define SERVICES 5
  235. void * dnsprchild(struct clientparam * param);
  236. void * pop3pchild(struct clientparam * param);
  237. void * smtppchild(struct clientparam * param);
  238. void * proxychild(struct clientparam * param);
  239. void * sockschild(struct clientparam * param);
  240. void * tcppmchild(struct clientparam * param);
  241. void * icqprchild(struct clientparam * param);
  242. void * msnprchild(struct clientparam * param);
  243. void * udppmchild(struct clientparam * param);
  244. void * adminchild(struct clientparam * param);
  245. void * ftpprchild(struct clientparam * param);
  246. struct datatype;
  247. struct dictionary;
  248. struct node;
  249. struct property;
  250. extern unsigned char tmpbuf[8192];
  251. extern pthread_mutex_t config_mutex;
  252. extern pthread_mutex_t bandlim_mutex;
  253. extern pthread_mutex_t hash_mutex;
  254. extern pthread_mutex_t tc_mutex;
  255. extern pthread_mutex_t pwl_mutex;
  256. extern pthread_mutex_t log_mutex;
  257. extern pthread_mutex_t rad_mutex;
  258. extern struct datatype datatypes[64];
  259. extern struct commands commandhandlers[];
  260. #ifdef WITHSPLICE
  261. #define mapsocket(a,b) (a->srv->usesplice?splicemap(a,b):sockmap(a,b))
  262. #else
  263. #define mapsocket(a,b) sockmap(a,b)
  264. #endif
  265. #ifdef _WINCE
  266. char * CEToUnicode (const char *str);
  267. int cesystem(const char *str);
  268. int ceparseargs(const char *str);
  269. extern char * ceargv[32];
  270. #define system(S) cesystem(S)
  271. #endif
  272. #define WEBBANNERS 35
  273. #endif