proxy.h 11 KB

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