transparent_plugin.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*
  2. 3APA3A simpliest proxy server
  3. (c) 2002-2017 by Vladimir Dubrovin <3proxy@3proxy.ru>
  4. please read License Agreement
  5. */
  6. #ifdef WITH_NETFILTER
  7. #include <sys/utsname.h>
  8. #endif
  9. #include "../../structures.h"
  10. #include "../../proxy.h"
  11. #ifdef WITH_NETFILTER
  12. #include <sys/types.h>
  13. #include <sys/socket.h>
  14. #include <limits.h>
  15. #include <linux/netfilter_ipv4.h>
  16. #endif
  17. #ifdef __cplusplus
  18. extern "C" {
  19. #endif
  20. static struct pluginlink * pl;
  21. static int transparent_loaded = 0;
  22. static void* transparent_filter_open(void * idata, struct srvparam * param){
  23. return idata;
  24. }
  25. static FILTER_ACTION transparent_filter_client(void *fo, struct clientparam * param, void** fc){
  26. socklen_t len;
  27. char addrbuf[64];
  28. int i=0;
  29. len = sizeof(param->req);
  30. #ifdef WITH_NETFILTER
  31. #ifdef SO_ORIGINAL_DST
  32. if(getsockopt(param->clisock,
  33. #ifndef NOIPV6
  34. #ifdef SOL_IPV6
  35. *SAFAMILY(&param->sincr) == AF_INET6?SOL_IPV6:
  36. #endif
  37. #endif
  38. SOL_IP, SO_ORIGINAL_DST,(struct sockaddr *) &param->req, &len) || !memcmp((char *)SAADDR(&param->req), "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0", SAADDRLEN(&param->req))){
  39. return PASS;
  40. }
  41. #else
  42. #error No SO_ORIGINAL_DST defined
  43. param->srv->logfunc(param, (unsigned char *)"transparent_plugin: No SO_ORIGINAL_DST defined");
  44. return REJECT;
  45. #endif
  46. #else
  47. param->req = param->sincl;
  48. param->sincl = param->srv->intsa;
  49. #endif
  50. pl->myinet_ntop(*SAFAMILY(&param->req), SAADDR(&param->req), (char *)addrbuf, sizeof(addrbuf));
  51. if(param->hostname) pl->freefunc(param->hostname);
  52. param->hostname = pl->strdupfunc(addrbuf);
  53. param->sinsr = param->req;
  54. return PASS;
  55. }
  56. static void transparent_filter_clear(void *fo){
  57. }
  58. static void transparent_filter_close(void *fo){
  59. }
  60. static struct filter transparent_filter = {
  61. NULL,
  62. "Transparent filter",
  63. "Transparent filter",
  64. transparent_filter_open,
  65. transparent_filter_client,
  66. NULL, NULL, NULL, NULL, NULL, NULL,
  67. transparent_filter_clear,
  68. transparent_filter_close
  69. };
  70. static int h_transparent(int argc, unsigned char **argv){
  71. transparent_filter.filter_open = transparent_filter_open;
  72. return 0;
  73. }
  74. static int h_notransparent(int argc, unsigned char **argv){
  75. transparent_filter.filter_open = NULL;
  76. return 0;
  77. }
  78. static struct commands transparent_commandhandlers[] = {
  79. {transparent_commandhandlers+1, "transparent", h_transparent, 1, 1},
  80. {NULL, "notransparent", h_notransparent, 1, 1}
  81. };
  82. #ifdef WATCOM
  83. #pragma aux transparent_plugin "*" parm caller [ ] value struct float struct routine [eax] modify [eax ecx edx]
  84. #undef PLUGINCALL
  85. #define PLUGINCALL
  86. #endif
  87. PLUGINAPI int PLUGINCALL transparent_plugin (struct pluginlink * pluginlink,
  88. int argc, char** argv){
  89. pl = pluginlink;
  90. if(!transparent_loaded){
  91. transparent_loaded = 1;
  92. transparent_filter.next = pl->conf->filters;
  93. pl->conf->filters = &transparent_filter;
  94. transparent_commandhandlers[1].next = pl->commandhandlers->next;
  95. pl->commandhandlers->next = transparent_commandhandlers;
  96. }
  97. return 0;
  98. }
  99. #ifdef __cplusplus
  100. }
  101. #endif