proxy.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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 <semaphore.h>
  48. #include <syslog.h>
  49. #include <errno.h>
  50. #endif
  51. #ifdef __CYGWIN__
  52. #include <windows.h>
  53. #define daemonize() FreeConsole()
  54. #define SLEEPTIME 1000
  55. #undef _WIN32
  56. #elif _WIN32
  57. #ifdef errno
  58. #undef errno
  59. #endif
  60. #define errno WSAGetLastError()
  61. #ifdef EAGAIN
  62. #undef EAGAIN
  63. #endif
  64. #define EAGAIN WSAEWOULDBLOCK
  65. #ifdef EINTR
  66. #undef EINTR
  67. #endif
  68. #ifndef EINPROGRESS
  69. #define EINPROGRESS WSAEWOULDBLOCK
  70. #endif
  71. #define EINTR WSAEWOULDBLOCK
  72. #define SLEEPTIME 1
  73. #define usleep Sleep
  74. #define pthread_self GetCurrentThreadId
  75. #define getpid GetCurrentProcessId
  76. #define pthread_t unsigned
  77. #ifndef _WINCE
  78. #define daemonize() FreeConsole()
  79. #else
  80. #define daemonize()
  81. #endif
  82. #define socket(x, y, z) WSASocket(x, y, z, NULL, 0, 0)
  83. #define accept(x, y, z) WSAAccept(x, y, z, NULL, 0)
  84. #define ftruncate chsize
  85. #else
  86. #include <pthread.h>
  87. #ifndef PTHREAD_STACK_MIN
  88. #define PTHREAD_STACK_MIN 32768
  89. #define sockerror strerror
  90. #endif
  91. void daemonize(void);
  92. #define SLEEPTIME 1000
  93. #ifndef O_BINARY
  94. #define O_BINARY 0
  95. #endif
  96. #endif
  97. #ifndef NOODBC
  98. #ifndef _WIN32
  99. #include <sqltypes.h>
  100. #endif
  101. #include <sql.h>
  102. #include <sqlext.h>
  103. #endif
  104. #ifdef _WIN32
  105. #define strcasecmp stricmp
  106. #define strncasecmp strnicmp
  107. #endif
  108. #ifndef SOCKET_ERROR
  109. #define SOCKET_ERROR -1
  110. #endif
  111. #ifndef isnumber
  112. #define isnumber(n) (n >= '0' && n <= '9')
  113. #endif
  114. #ifndef ishex
  115. #define ishex(n) ((n >= '0' && n <= '9') || (n >= 'a' && n<='f') || (n >= 'A' && n <= 'F'))
  116. #endif
  117. #define isallowed(n) ((n >= '0' && n <= '9') || (n >= 'a' && n <= 'z') || (n >= 'A' && n <= 'Z') || (n >= '*' && n <= '/') || n == '_')
  118. #include "structures.h"
  119. #define MAXRADIUS 5
  120. #define DEFLOGFORMAT "G%y%m%d%H%M%S.%. %p %E %U %C:%c %R:%r %O %I %h %T"
  121. #define myalloc malloc
  122. #define myfree free
  123. #define myrealloc realloc
  124. #define mystrdup strdup
  125. extern RESOLVFUNC resolvfunc;
  126. extern int wday;
  127. extern time_t basetime;
  128. extern int timetoexit;
  129. extern struct extparam conf;
  130. int sockmap(struct clientparam * param, int timeo, int usesplice);
  131. int socksend(SOCKET sock, char * buf, int bufsize, int to);
  132. int socksendto(SOCKET sock, struct sockaddr * sin, char * buf, int bufsize, int to);
  133. int sockrecvfrom(SOCKET sock, struct sockaddr * sin, char * buf, int bufsize, int to);
  134. int sockgetcharcli(struct clientparam * param, int timeosec, int timeousec);
  135. int sockgetcharsrv(struct clientparam * param, int timeosec, int timeousec);
  136. int sockfillbuffcli(struct clientparam * param, unsigned long size, int timeosec);
  137. int sockfillbuffsrv(struct clientparam * param, unsigned long size, int timeosec);
  138. int sockgetlinebuf(struct clientparam * param, DIRECTION which, char * buf, int bufsize, int delim, int to);
  139. void initlog(void);
  140. void dolog(struct clientparam * param, const char *s);
  141. int dobuf(struct clientparam * param, char * buf, int bufsize, const char *s, const char * doublec);
  142. int dobuf2(struct clientparam * param, char * buf, int bufsize, const char *s, const char * doublec, struct tm* tm, char * format);
  143. int doconnect(struct clientparam * param);
  144. int alwaysauth(struct clientparam * param);
  145. int ipauth(struct clientparam * param);
  146. int dopreauth(struct clientparam * param);
  147. int doauth(struct clientparam * param);
  148. int strongauth(struct clientparam * param);
  149. void trafcountfunc(struct clientparam *param);
  150. unsigned bandlimitfunc(struct clientparam *param, unsigned nbytesin, unsigned nbytesout);
  151. int scanaddr(const char *s, unsigned long * ip, unsigned long * mask);
  152. int myinet_ntop(int af, void *src, char *dst, socklen_t size);
  153. extern struct nserver nservers[MAXNSERVERS];
  154. extern struct nserver authnserver;
  155. unsigned long getip(char *name);
  156. unsigned long getip46(int family, char *name, struct sockaddr *sa);
  157. int afdetect(char *name);
  158. unsigned long myresolver(int, char *, char *);
  159. unsigned long fakeresolver (int, char *, char*);
  160. int inithashtable(struct hashtable *hashtable, unsigned nhashsize);
  161. void freeparam(struct clientparam * param);
  162. void srvpostfree(struct srvparam * srv);
  163. void clearstat(struct clientparam * param);
  164. void dumpcounters(struct trafcount *tl, int counterd);
  165. int startconnlims (struct clientparam *param);
  166. void stopconnlims (struct clientparam *param);
  167. int timechanged (time_t oldtime, time_t newtime, ROTATION lt);
  168. extern struct auth authfuncs[];
  169. int reload (void);
  170. extern int paused;
  171. extern int demon;
  172. char * mycrypt(const char *key, const char *salt, char *buf);
  173. char * ntpwdhash (char *szHash, const char *szPassword, int tohex);
  174. int de64 (const char *in, char *out, int maxlen);
  175. char* en64 (const char *in, char *out, int inlen);
  176. void tohex(char *in, char *out, int len);
  177. void fromhex(char *in, char *out, int len);
  178. int ftplogin(struct clientparam *param, char *buf, int *inbuf);
  179. int ftpcd(struct clientparam *param, char* path, char *buf, int *inbuf);
  180. int ftpsyst(struct clientparam *param, char *buf, unsigned len);
  181. int ftppwd(struct clientparam *param, char *buf, unsigned len);
  182. int ftptype(struct clientparam *param, char* f_type);
  183. int ftpres(struct clientparam *param, char * buf, int len);
  184. SOCKET ftpcommand(struct clientparam *param, char * command, 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 char *win_password,
  189. const char *challenge, char *response);
  190. struct hashtable;
  191. void hashadd(struct hashtable *ht, const char* name, char* value, time_t expires);
  192. int parsehost(int family, 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. int checkpreACL(struct clientparam * param);
  199. extern int havelog;
  200. unsigned long udpresolve(int af, char * name, char * value, unsigned *retttl, struct clientparam* param, int makeauth);
  201. struct auth * copyauth (struct auth *);
  202. void * itfree(void *data, void * retval);
  203. void freeacl(struct ace *ac);
  204. void freeauth(struct auth *);
  205. void freefilter(struct filter *filter);
  206. void freeconf(struct extparam *confp);
  207. struct passwords * copypwl (struct passwords *pwl);
  208. void freepwl(struct passwords *pw);
  209. void copyfilter(struct filter *, struct srvparam *srv);
  210. FILTER_ACTION makefilters (struct srvparam *srv, struct clientparam *param);
  211. FILTER_ACTION handlereqfilters(struct clientparam *param, char ** buf_p, int * bufsize_p, int offset, int * length_p);
  212. FILTER_ACTION handlehdrfilterscli(struct clientparam *param, char ** buf_p, int * bufsize_p, int offset, int * length_p);
  213. FILTER_ACTION handlehdrfilterssrv(struct clientparam *param, char ** buf_p, int * bufsize_p, int offset, int * length_p);
  214. FILTER_ACTION handlepredatflt(struct clientparam *param);
  215. FILTER_ACTION handledatfltcli(struct clientparam *param, char ** buf_p, int * bufsize_p, int offset, int * length_p);
  216. FILTER_ACTION handledatfltsrv(struct clientparam *param, char ** buf_p, int * bufsize_p, int offset, int * length_p);
  217. void srvinit(struct srvparam * srv, struct clientparam *param);
  218. void srvinit2(struct srvparam * srv, struct clientparam *param);
  219. void srvfree(struct srvparam * srv);
  220. char * dologname (char *buf, int bufsize, char *name, const char *ext, ROTATION lt, time_t t);
  221. int readconfig(FILE * fp);
  222. int connectwithpoll(SOCKET sock, struct sockaddr *sa, SASIZETYPE size, int to);
  223. int myrand(void * entropy, int len);
  224. extern char *copyright;
  225. #define SERVICES 5
  226. void * dnsprchild(struct clientparam * param);
  227. void * pop3pchild(struct clientparam * param);
  228. void * smtppchild(struct clientparam * param);
  229. void * proxychild(struct clientparam * param);
  230. void * sockschild(struct clientparam * param);
  231. void * tcppmchild(struct clientparam * param);
  232. void * udppmchild(struct clientparam * param);
  233. void * adminchild(struct clientparam * param);
  234. void * ftpprchild(struct clientparam * param);
  235. struct datatype;
  236. struct dictionary;
  237. struct node;
  238. struct property;
  239. extern pthread_mutex_t config_mutex;
  240. extern pthread_mutex_t bandlim_mutex;
  241. extern pthread_mutex_t connlim_mutex;
  242. extern pthread_mutex_t hash_mutex;
  243. extern pthread_mutex_t tc_mutex;
  244. extern pthread_mutex_t pwl_mutex;
  245. extern pthread_mutex_t log_mutex;
  246. extern pthread_mutex_t rad_mutex;
  247. extern struct datatype datatypes[64];
  248. extern struct commands commandhandlers[];
  249. #ifdef WITHSPLICE
  250. #define mapsocket(a,b) ((a->srv->usesplice && !a->ndatfilterssrv && !a->ndatfilterscli)?sockmap(a,b,1):sockmap(a,b,0))
  251. #else
  252. #define mapsocket(a,b) sockmap(a,b, 0)
  253. #endif
  254. extern struct radserver {
  255. #ifdef NOIPV6
  256. struct sockaddr_in authaddr, logaddr, localaddr;
  257. #else
  258. struct sockaddr_in6 authaddr, logaddr, localaddr;
  259. #endif
  260. /*
  261. SOCKET logsock;
  262. */
  263. } radiuslist[MAXRADIUS];
  264. extern char radiussecret[64];
  265. extern int nradservers;
  266. extern struct socketoptions {
  267. int opt;
  268. char * optname;
  269. } sockopts[];
  270. void setopts(SOCKET s, int opts);
  271. char * printopts(char *sep);
  272. #ifdef _WINCE
  273. char * CEToUnicode (const char *str);
  274. int cesystem(const char *str);
  275. int ceparseargs(const char *str);
  276. extern char * ceargv[32];
  277. #define system(S) cesystem(S)
  278. #endif
  279. #define WEBBANNERS 35
  280. #endif