dighosts.c 3.7 KB

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