/*
 * Copyright (c) 2000-2003 3APA3A
 *
 * please read License Agreement
 *
 * $Id: dighosts.c,v 1.1.1.1 2003/07/19 11:18:18 vlad Exp $
 */

#include "proxy.h"

unsigned char request[] = "GET %.1024s HTTP/1.0\r\nHost: %.256s\r\n\r\n";

int main(int argc, char *argv[]){
	unsigned char *host, *hostend;
	SOCKET sock;
	struct sockaddr_in sa;
	FILE *fp;
	unsigned char buf[16000];
	int i;
	unsigned x,y,z,w,cidr, x1,y1,z1,w1, mask;
	int first = 1;

#ifdef _WIN32
 WSADATA wd;
 WSAStartup(MAKEWORD( 1, 1 ), &wd);
#endif

	if(argc < 3 || argc > 4 || (argc == 4 && (argv[1][0] != '-' || argv[1][1] != 'm'))) {
		fprintf(stderr, "Usage: %s [-m] <URL> <FILE>\n"
				" program retrieves requested <URL> and builds comma delimited list of networks\n"
				" list than stored in <FILE>\n"
				" networks are searched in xxx.yyy.zzz.www/cidr format\n"
				" switches:\n"
				"  -m networks are searched in xxx.yyy.zzz.www mmm.mmm.mmm.mmm format\n"
				"\n(c)2002 by 3APA3A\n",
				argv[0]);
		return 1;
	}
	if(strncasecmp(argv[argc-2], "http://", 7)) {
		fprintf(stderr, "URL must be HTTP://\n");
		return 2;
	}
	hostend = (unsigned char *)strchr((char *)argv[argc-2] + 7, '/');
	if(!hostend) {
		fprintf(stderr, "Wrong URL syntaxis\n");
		return 3;
	}
	*hostend = 0;
	if(!(host = (unsigned char *)strdup((char *)argv[argc-2] + 7))) {
		return 4;
	}
	*hostend = '/';
	if(!(sa.sin_addr.s_addr = getip(host))) {
		fprintf(stderr, "Unable to resolve %s\n", host);
		return 5;
	}
	sa.sin_port = htons(80);
	sa.sin_family = AF_INET;
	if((sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == INVALID_SOCKET) return 6;
	if(!(fp = fopen(argv[argc-1], "w"))) {
		fprintf(stderr, "Unable to open: %s\n", argv[2]);
		return 7;
	}
	sprintf((char *)buf, (char *)request, hostend, host);
	if(connect(sock,(struct sockaddr *)&sa,sizeof(sa))) {
		fprintf(stderr, "Unable to connect: %s\n", host);
		return 8;
	}
	if(send(sock, buf, strlen((char *)buf), 0) != (int)strlen((char *)buf)) return 9;
	while( (i = sockgetline(sock, buf, sizeof(buf) - 1, '\n', 30)) > 2);
	while( (i = sockgetline(sock, buf, sizeof(buf) - 1, '\n', 30)) > 0){
		buf[i] = 0;
		for(i = 0; buf[i]; i++){
			if((buf[i]<'0' || buf[i] > '9') && buf[i] != '.' && buf[i] != '/')buf[i] = ' ';
		}
		if(argc == 3){
			if((i=sscanf((char *)buf, "%u.%u.%u.%u/%u", &x, &y, &z, &w, &cidr)) == 5 &&
					x<256 && y<256 && z<256 && w<256 &&
					cidr <= 32){
				if(!first)fprintf(fp, ",");
				fprintf(fp, "%u.%u.%u.%u/%u", x, y, z, w, cidr);
				first = 0;
			}
		}
		else{
			if((i = sscanf((char *)buf, "%u.%u.%u.%u %u.%u.%u.%u", &x, &y, &z, &w, &x1, &y1, &z1, &w1)) == 8 &&
					x<256 && y<256 && z<256 && w<256 &&
					x1<256 && y1<256 && z1<256 && w1<256
					){
				mask = (x1<<24)|(y1<<16)|(z1<<8)|w1;
				for(cidr = 0; cidr <= 32; cidr++)if((((unsigned long)(0xFFFFFFFF))<<(32-cidr)) == mask) break;
				if(cidr > 32) continue;
				if(!first)fprintf(fp, ",");
				fprintf(fp, "%u.%u.%u.%u/%u", x, y, z, w, cidr);
				first = 0;
			}
		}
	}
	shutdown(sock, SHUT_RDWR);
	close(sock);
	fclose(fp);
	return 0;
}
