proxy.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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. extern RESOLVFUNC resolvfunc;
  121. extern int wday;
  122. extern time_t basetime;
  123. extern int timetoexit;
  124. extern struct extparam conf;
  125. int sockmap(struct clientparam * param, int timeo);
  126. int splicemap(struct clientparam * param, int timeo);
  127. int socksend(SOCKET sock, unsigned char * buf, int bufsize, int to);
  128. int socksendto(SOCKET sock, struct sockaddr * sin, unsigned char * buf, int bufsize, int to);
  129. int sockrecvfrom(SOCKET sock, struct sockaddr * sin, unsigned char * buf, int bufsize, int to);
  130. int sockgetcharcli(struct clientparam * param, int timeosec, int timeousec);
  131. int sockgetcharsrv(struct clientparam * param, int timeosec, int timeousec);
  132. int sockfillbuffcli(struct clientparam * param, unsigned long size, int timeosec);
  133. int sockfillbuffsrv(struct clientparam * param, unsigned long size, int timeosec);
  134. int sockgetlinebuf(struct clientparam * param, DIRECTION which, unsigned char * buf, int bufsize, int delim, int to);
  135. int dobuf(struct clientparam * param, unsigned char * buf, const unsigned char *s, const unsigned char * doublec);
  136. int dobuf2(struct clientparam * param, unsigned char * buf, const unsigned char *s, const unsigned char * doublec, struct tm* tm, char * format);
  137. extern FILE * stdlog;
  138. void logstdout(struct clientparam * param, const unsigned char *s);
  139. void logsyslog(struct clientparam * param, const unsigned char *s);
  140. void lognone(struct clientparam * param, const unsigned char *s);
  141. void logradius(struct clientparam * param, const unsigned char *s);
  142. #ifndef NOSQL
  143. void logsql(struct clientparam * param, const unsigned char *s);
  144. int init_sql(char * s);
  145. void close_sql();
  146. #endif
  147. int doconnect(struct clientparam * param);
  148. int alwaysauth(struct clientparam * param);
  149. int ipauth(struct clientparam * param);
  150. int doauth(struct clientparam * param);
  151. int strongauth(struct clientparam * param);
  152. void trafcountfunc(struct clientparam *param);
  153. unsigned bandlimitfunc(struct clientparam *param, unsigned nbytesin, unsigned nbytesout);
  154. int scanaddr(const unsigned char *s, unsigned long * ip, unsigned long * mask);
  155. int myinet_ntop(int af, void *src, char *dst, socklen_t size);
  156. extern struct nserver nservers[MAXNSERVERS];
  157. extern struct nserver authnserver;
  158. unsigned long getip(unsigned char *name);
  159. unsigned long getip46(int family, unsigned char *name, struct sockaddr *sa);
  160. unsigned long myresolver(int, unsigned char *, unsigned char *);
  161. unsigned long fakeresolver (int, unsigned char *, unsigned char*);
  162. int inithashtable(struct hashtable *hashtable, unsigned nhashsize);
  163. void freeparam(struct clientparam * param);
  164. void clearstat(struct clientparam * param);
  165. void dumpcounters(struct trafcount *tl, int counterd);
  166. int startconnlims (struct clientparam *param);
  167. void stopconnlims (struct clientparam *param);
  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. extern int havelog;
  199. unsigned long udpresolve(int af, unsigned char * name, unsigned char * value, unsigned *retttl, struct clientparam* param, int makeauth);
  200. struct ace * copyacl (struct ace *ac);
  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, unsigned char ** buf_p, int * bufsize_p, int offset, int * length_p);
  212. FILTER_ACTION handlehdrfilterscli(struct clientparam *param, unsigned char ** buf_p, int * bufsize_p, int offset, int * length_p);
  213. FILTER_ACTION handlehdrfilterssrv(struct clientparam *param, unsigned 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, unsigned char ** buf_p, int * bufsize_p, int offset, int * length_p);
  216. FILTER_ACTION handledatfltsrv(struct clientparam *param, unsigned 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. unsigned char * dologname (unsigned char *buf, unsigned char *name, const unsigned char *ext, ROTATION lt, time_t t);
  221. int readconfig(FILE * fp);
  222. int connectwithpoll(SOCKET sock, struct sockaddr *sa, SASIZETYPE size);
  223. int myrand(void * entropy, int len);
  224. #ifdef WITH_STD_MALLOC
  225. #define myalloc malloc
  226. #define myfree free
  227. #define myrealloc realloc
  228. #define mystrdup strdup
  229. #else
  230. void *myalloc(size_t size);
  231. void myfree(void *ptr);
  232. void *myrealloc(void *ptr, size_t size);
  233. char * mystrdup(const char *str);
  234. #endif
  235. extern char *copyright;
  236. #define SERVICES 5
  237. void * dnsprchild(struct clientparam * param);
  238. void * pop3pchild(struct clientparam * param);
  239. void * smtppchild(struct clientparam * param);
  240. void * proxychild(struct clientparam * param);
  241. void * sockschild(struct clientparam * param);
  242. void * tcppmchild(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 pthread_mutex_t config_mutex;
  251. extern pthread_mutex_t bandlim_mutex;
  252. extern pthread_mutex_t connlim_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 && !a->ndatfilterssrv && !a->ndatfilterscli && !a->npredatfilters?splicemap(a,b):sockmap(a,b))
  262. #else
  263. #define mapsocket(a,b) sockmap(a,b)
  264. #endif
  265. extern struct radserver {
  266. #ifdef NOIPV6
  267. struct sockaddr_in authaddr, logaddr;
  268. #else
  269. struct sockaddr_in6 authaddr, logaddr;
  270. #endif
  271. /*
  272. SOCKET logsock;
  273. */
  274. } radiuslist[MAXRADIUS];
  275. extern char radiussecret[64];
  276. extern int nradservers;
  277. extern struct socketoptions {
  278. int opt;
  279. char * optname;
  280. } sockopts[];
  281. void setopts(SOCKET s, int opts);
  282. char * printopts(char *sep);
  283. #ifdef _WINCE
  284. char * CEToUnicode (const char *str);
  285. int cesystem(const char *str);
  286. int ceparseargs(const char *str);
  287. extern char * ceargv[32];
  288. #define system(S) cesystem(S)
  289. #endif
  290. #define WEBBANNERS 35
  291. #endif