dnspr.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /*
  2. 3APA3A simpliest proxy server
  3. (c) 2002-2008 by ZARAZA <3APA3A@security.nnov.ru>
  4. please read License Agreement
  5. $Id: dnspr.c,v 1.22 2009/09/17 12:21:05 v.dubrovin Exp $
  6. */
  7. #include "proxy.h"
  8. #ifndef UDP
  9. #define UDP
  10. #endif
  11. #define RETURN(xxx) { param->res = xxx; goto CLEANRET; }
  12. #define BUFSIZE 16384
  13. void * dnsprchild(struct clientparam* param) {
  14. unsigned long ip = 0;
  15. unsigned char *bbuf;
  16. unsigned char *buf, *s1, *s2;
  17. char * host = NULL;
  18. unsigned char c;
  19. SASIZETYPE size;
  20. int res, i;
  21. int len;
  22. unsigned type=0;
  23. unsigned ttl;
  24. #ifdef _WIN32
  25. unsigned long ul = 1;
  26. #endif
  27. if(!(bbuf = myalloc(BUFSIZE+2))){
  28. param->srv->fds.events = POLLIN;
  29. RETURN (21);
  30. }
  31. buf = bbuf+2;
  32. size = sizeof(param->sincr);
  33. i = so._recvfrom(param->srv->srvsock, buf, BUFSIZE, 0, (struct sockaddr *)&param->sincr, &size);
  34. #ifdef _WIN32
  35. if((param->clisock=so._socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == INVALID_SOCKET) {
  36. RETURN(818);
  37. }
  38. ioctlsocket(param->clisock, FIONBIO, &ul);
  39. size = sizeof(param->sinsl);
  40. if(so._getsockname(param->srv->srvsock, (struct sockaddr *)&param->sincl, &size)) {RETURN(21);};
  41. if(so._setsockopt(param->clisock, SOL_SOCKET, SO_REUSEADDR, (unsigned char *)&ul, sizeof(int))) {RETURN(820);};
  42. if(so._bind(param->clisock,(struct sockaddr *)&param->sincl,sizeof(param->sincl))) {
  43. RETURN(822);
  44. }
  45. #else
  46. param->clisock = param->srv->srvsock;
  47. #endif
  48. param->srv->fds.events = POLLIN;
  49. if(i < 0) {
  50. RETURN(813);
  51. }
  52. buf[BUFSIZE - 1] = 0;
  53. if(i<=13 || i>1000){
  54. RETURN (814);
  55. }
  56. param->operation = DNSRESOLVE;
  57. if((res = (*param->srv->authfunc)(param))) {RETURN(res);}
  58. if(buf[4]!=0 || buf[5]!=1) RETURN(816);
  59. for(len = 12; len<i; len+=(c+1)){
  60. c = buf[len];
  61. if(!c)break;
  62. buf[len] = '.';
  63. }
  64. if(len > (i-4)) {RETURN(817);}
  65. host = mystrdup((char *)buf+13);
  66. if(!host) {RETURN(21);}
  67. for(s2 = buf + 12; (s1 = (unsigned char *)strchr((char *)s2 + 1, '.')); s2 = s1)*s2 = (unsigned char)((s1 - s2) - 1);
  68. *s2 = (len - (int)(s2 - buf)) - 1;
  69. type = ((unsigned)buf[len+1])*256 + (unsigned)buf[len+2];
  70. if(type==1 && !param->srv->singlepacket){
  71. ip = udpresolve((unsigned char *)host, &ttl, param, 0);
  72. }
  73. len+=5;
  74. if(ip){
  75. buf[2] = 0x85;
  76. buf[3] = 0x80;
  77. buf[6] = 0;
  78. buf[7] = 1;
  79. buf[8] = buf[9] = buf[10] = buf[11] = 0;
  80. memset(buf+len, 0, 16);
  81. buf[len] = 0xc0;
  82. buf[len+1] = 0x0c;
  83. buf[len+3] = 1;
  84. buf[len+5] = 1;
  85. ttl = htonl(ttl);
  86. memcpy(buf + len + 6, &ttl, 4);
  87. buf[len+11] = 4;
  88. memcpy(buf+len+12,(void *)&ip,4);
  89. len+=16;
  90. }
  91. else if(type == 0x0c) {
  92. unsigned a, b, c, d;
  93. sscanf(host, "%u.%u.%u.%u", &a, &b, &c, &d);
  94. ip = htonl((d<<24) ^ (c<<16) ^ (b<<8) ^ a);
  95. if(*SAFAMILY(&param->srv->intsa) == AF_INET && ip == *(unsigned long*)SAADDR(&param->srv->intsa)){
  96. buf[2] = 0x85;
  97. buf[3] = 0x80;
  98. buf[6] = 0;
  99. buf[7] = 1;
  100. buf[8] = buf[9] = buf[10] = buf[11] = 0;
  101. memset(buf+len, 0, 20);
  102. buf[len] = 0xc0;
  103. buf[len+1] = 0x0c;
  104. buf[len+3] = 0x0c;
  105. buf[len+5] = 1;
  106. ttl = htonl(3600);
  107. memcpy(buf + len + 6, &ttl, 4);
  108. buf[len+11] = 7;
  109. buf[len+12] = 6;
  110. memcpy(buf+len+13,(void *)"3proxy",6);
  111. len+=20;
  112. }
  113. else ip = 0;
  114. }
  115. if(!ip && nservers[0].ip){
  116. if((param->remsock=so._socket(PF_INET, nservers[0].usetcp? SOCK_STREAM:SOCK_DGRAM, nservers[0].usetcp?IPPROTO_TCP:IPPROTO_UDP)) == INVALID_SOCKET) {
  117. RETURN(818);
  118. }
  119. *SAFAMILY(&param->sinsl) = AF_INET;
  120. *SAPORT(&param->sinsl) = htons(0);
  121. *(unsigned long*)SAADDR(&param->sinsl) = htonl(0);
  122. if(so._bind(param->remsock,(struct sockaddr *)&param->sinsl,sizeof(param->sinsl))) {
  123. RETURN(819);
  124. }
  125. *SAFAMILY(&param->sinsr) = AF_INET;
  126. *(unsigned long*)SAADDR(&param->sinsr) = nservers[0].ip;
  127. *SAPORT(&param->sinsr) = htons(53);
  128. if(nservers[0].usetcp) {
  129. if(so._connect(param->remsock,(struct sockaddr *)&param->sinsr,sizeof(param->sinsr))) RETURN(830);
  130. buf-=2;
  131. *(unsigned short*)buf = htons(i);
  132. i+=2;
  133. }
  134. else {
  135. #ifdef _WIN32
  136. /* ioctlsocket(param->remsock, FIONBIO, &ul); */
  137. #else
  138. /* fcntl(param->remsock,F_SETFL,O_NONBLOCK); */
  139. #endif
  140. }
  141. if(socksendto(param->remsock, (struct sockaddr *)&param->sinsr, buf, i, conf.timeouts[SINGLEBYTE_L]*1000) != i){
  142. RETURN(820);
  143. }
  144. param->statscli64 += i;
  145. param->nwrites++;
  146. len = sockrecvfrom(param->remsock, (struct sockaddr *)&param->sinsr, buf, BUFSIZE, 15000);
  147. if(len <= 13) {
  148. RETURN(821);
  149. }
  150. param->statssrv64 += len;
  151. param->nreads++;
  152. if(nservers[0].usetcp) {
  153. buf += 2;
  154. len -= 2;
  155. }
  156. if(buf[6] || buf[7]){
  157. if(socksendto(param->clisock, (struct sockaddr *)&param->sincr, buf, len, conf.timeouts[SINGLEBYTE_L]*1000) != len){
  158. RETURN(822);
  159. }
  160. RETURN(0);
  161. }
  162. }
  163. if(!ip) {
  164. buf[2] = 0x85;
  165. buf[3] = 0x83;
  166. }
  167. /* usleep(SLEEPTIME); */
  168. res = socksendto(param->clisock, (struct sockaddr *)&param->sincr, buf, len, conf.timeouts[SINGLEBYTE_L]*1000);
  169. if(res != len){RETURN(819);}
  170. if(!ip) {RETURN(888);}
  171. CLEANRET:
  172. if(param->res!=813){
  173. sprintf((char *)buf, "%04x/%s(%u.%u.%u.%u)",
  174. (unsigned)type,
  175. host?host:"",
  176. (unsigned)(ntohl(ip)&0xff000000)>>24,
  177. (unsigned)(ntohl(ip)&0x00ff0000)>>16,
  178. (unsigned)(ntohl(ip)&0x0000ff00)>>8,
  179. (unsigned)(ntohl(ip)&0x000000ff)
  180. );
  181. (*param->srv->logfunc)(param, buf);
  182. }
  183. if(bbuf)myfree(bbuf);
  184. if(host)myfree(host);
  185. #ifndef _WIN32
  186. param->clisock = INVALID_SOCKET;
  187. #endif
  188. freeparam(param);
  189. return (NULL);
  190. }