dighosts.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. * Copyright (c) 2000-2008 3APA3A
  3. *
  4. * please read License Agreement
  5. *
  6. * $Id: dighosts.c,v 1.10 2009/10/06 08:38:00 v.dubrovin Exp $
  7. */
  8. #include "proxy.h"
  9. int sockgetchar(SOCKET sock, int timeosec, int timeousec){
  10. unsigned char buf;
  11. fd_set fds;
  12. struct timeval tv;
  13. tv.tv_sec = timeosec;
  14. tv.tv_usec = timeousec;
  15. FD_ZERO(&fds);
  16. FD_SET(sock, &fds);
  17. if (select (((int)sock)+1, &fds, NULL, NULL, &tv)!=1) return EOF;
  18. if (recv(sock, &buf, 1, 0)!=1) return EOF;
  19. return((int)buf);
  20. }
  21. int sockgetline(SOCKET sock, unsigned char * buf, int bufsize, int delim, int to){
  22. int c;
  23. int i=0, tos, tou;
  24. if(bufsize<2) return 0;
  25. c = sockgetchar(sock, to, 0);
  26. if (c == EOF) {
  27. return 0;
  28. }
  29. tos = to/16;
  30. tou = ((to * 1000) / bufsize)%1000;
  31. do {
  32. buf[i++] = c;
  33. if(delim != EOF && c == delim) break;
  34. }while(i < bufsize && (c = sockgetchar(sock, tos, tou)) != EOF);
  35. return i;
  36. }
  37. unsigned char request[] = "GET %.1024s HTTP/1.0\r\nHost: %.256s\r\n\r\n";
  38. int main(int argc, char *argv[]){
  39. unsigned char *host, *hostend;
  40. SOCKET sock;
  41. struct sockaddr_in sa;
  42. FILE *fp;
  43. unsigned char buf[16000];
  44. int i;
  45. unsigned x,y,z,w,cidr, x1,y1,z1,w1, mask;
  46. int first = 1;
  47. #ifdef _WIN32
  48. WSADATA wd;
  49. WSAStartup(MAKEWORD( 1, 1 ), &wd);
  50. #endif
  51. if(argc < 3 || argc > 4 || (argc == 4 && (argv[1][0] != '-' || argv[1][1] != 'm'))) {
  52. fprintf(stderr, "Usage: %s [-m] <URL> <FILE>\n"
  53. " program retrieves requested <URL> and builds comma delimited list of networks\n"
  54. " list than stored in <FILE>\n"
  55. " networks are searched in xxx.yyy.zzz.www/cidr format\n"
  56. " switches:\n"
  57. " -m networks are searched in xxx.yyy.zzz.www mmm.mmm.mmm.mmm format\n"
  58. "\n(c)2002 by 3APA3A\n",
  59. argv[0]);
  60. return 1;
  61. }
  62. if(strncasecmp(argv[argc-2], "http://", 7)) {
  63. fprintf(stderr, "URL must be HTTP://\n");
  64. return 2;
  65. }
  66. hostend = (unsigned char *)strchr((char *)argv[argc-2] + 7, '/');
  67. if(!hostend) {
  68. fprintf(stderr, "Wrong URL syntaxis\n");
  69. return 3;
  70. }
  71. *hostend = 0;
  72. if(!(host = (unsigned char *)strdup((char *)argv[argc-2] + 7))) {
  73. return 4;
  74. }
  75. *hostend = '/';
  76. if(!(sa.sin_addr.s_addr = getip(host))) {
  77. fprintf(stderr, "Unable to resolve %s\n", host);
  78. return 5;
  79. }
  80. sa.sin_port = htons(80);
  81. sa.sin_family = AF_INET;
  82. if((sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == INVALID_SOCKET) return 6;
  83. sprintf((char *)buf, (char *)request, hostend, host);
  84. if(connect(sock,(struct sockaddr *)&sa,sizeof(sa))) {
  85. fprintf(stderr, "Unable to connect: %s\n", host);
  86. return 8;
  87. }
  88. if(send(sock, buf, (int)strlen((char *)buf), 0) != (int)strlen((char *)buf)) return 9;
  89. while( (i = sockgetline(sock, buf, sizeof(buf) - 1, '\n', 30)) > 2);
  90. if(i<1) return 9;
  91. if(!(fp = fopen(argv[argc-1], "w"))) {
  92. fprintf(stderr, "Unable to open: %s\n", argv[2]);
  93. return 7;
  94. }
  95. while( (i = sockgetline(sock, buf, sizeof(buf) - 1, '\n', 30)) > 0){
  96. buf[i] = 0;
  97. for(i = 0; buf[i]; i++){
  98. if((buf[i]<'0' || buf[i] > '9') && buf[i] != '.' && buf[i] != '/')buf[i] = ' ';
  99. }
  100. if(argc == 3){
  101. if((i=sscanf((char *)buf, "%u.%u.%u.%u/%u", &x, &y, &z, &w, &cidr)) == 5 &&
  102. x<256 && y<256 && z<256 && w<256 &&
  103. cidr <= 32){
  104. if(!first)fprintf(fp, ",");
  105. fprintf(fp, "%u.%u.%u.%u/%u", x, y, z, w, cidr);
  106. first = 0;
  107. }
  108. }
  109. else{
  110. if((i = sscanf((char *)buf, "%u.%u.%u.%u %u.%u.%u.%u", &x, &y, &z, &w, &x1, &y1, &z1, &w1)) == 8 &&
  111. x<256 && y<256 && z<256 && w<256 &&
  112. x1<256 && y1<256 && z1<256 && w1<256
  113. ){
  114. mask = (x1<<24)|(y1<<16)|(z1<<8)|w1;
  115. for(cidr = 0; cidr <= 32; cidr++)if((((unsigned long)(0xFFFFFFFF))<<(32-cidr)) == mask) break;
  116. if(cidr > 32) continue;
  117. if(!first)fprintf(fp, ",");
  118. fprintf(fp, "%u.%u.%u.%u/%u", x, y, z, w, cidr);
  119. first = 0;
  120. }
  121. }
  122. }
  123. shutdown(sock, SHUT_RDWR);
  124. #ifdef _WIN32
  125. closesocket(sock);
  126. #else
  127. close(sock);
  128. #endif
  129. fclose(fp);
  130. return 0;
  131. }