All pastes #83344 Raw Edit

Miscellany

public cpp v1 · immutable
·published 2006-07-09 17:27 UTC
rendered paste body
#include "library.h"#include "check_proxy.h"#define MAX_SOCKETS 1#define TIMEOUT 3#define S_NONE       0#define S_CONNECTING 1#define IP_LOG "ip.log"int status;struct conn_t {	int s;	char status;	time_t a;	sockaddr_in addr;};conn_t connlist[MAX_SOCKETS];std::string ip = "129.74.152.66";std::string port = "3127";void init_sockets();void check_sockets();void log_host(char * h);void fatal(char *);int main(int argc, char * argv[]) {	int done, i, cip, bb, ret, k, ns, fd;	time_t scantime;	status = 0;	fprintf(stdout, "\nCopyright © 2004 Rosiello Security - http://www.rosiello.org\n");	fprintf(stdout, "-- Modified by skauert [skauert@gmail.com]\n\n");		if (argc != 2) {		std::cout << "Invalid arguments!" << std::endl;		std::cout << "Format: " << argv[0] << " <IP-address>:<port>" << std::endl;		return -1;	}	fd = open(IP_LOG, O_RDONLY);	if (fd >= 0) {		remove(IP_LOG);		close(fd);	}	init_sockets();	scantime = time(0);	if (connlist[0].status == S_NONE) {		connlist[0].s = socket(AF_INET, SOCK_STREAM, 0);		if (connlist[0].s == -1) {			printf("Unable to allocate socket.\n");		}		else {			ret = fcntl(connlist[0].s, F_SETFL, O_NONBLOCK);			if (ret == -1) {				printf("Unable to set O_NONBLOCK\n");				close(connlist[0].s);			}			else {				sprintf(ip, "%s.%d.%d", argv[1], bb, cip);				connlist[0].addr.sin_addr.s_addr = inet_addr(ip.c_str);				if (connlist[0].addr.sin_addr.s_addr == -1) {					fatal("Invalid IP.");				}				connlist[0].addr.sin_family = AF_INET;				connlist[0].addr.sin_port = htons(atoi(port.c_str()));				connlist[0].a = time(0);				connlist[0].status = S_CONNECTING;			}		}		check_sockets();	}	printf( "\nScan completed in %u seconds.\n\n", (time(0) - scantime) );	if (status == 1) {		printf("Starting the validation phase\n");		printf("Check the file proxy_ok.log when finished\n");		check_proxy(argv[2]);	}	else {		printf("No proxy found!\n");	}	return 0;}void init_sockets(void) {	int i;	for (i=0; i<MAX_SOCKETS; i++) {		connlist[i].status = S_NONE;		memset((struct sockaddr_in*)&connlist[i].addr, 0, sizeof(struct sockaddr_in));	}}void check_sockets() {	int i, ret;	for (i=0; i<MAX_SOCKETS; i++) {		if ((connlist[i].a < (time(0) - TIMEOUT)) && (connlist[i].status == S_CONNECTING)) {			close(connlist[i].s);			connlist[i].status = S_NONE;		}		else if (connlist[i].status == S_CONNECTING) {			ret = connect(connlist[i].s, (struct sockaddr *)&connlist[i].addr, sizeof(struct sockaddr_in));			if (ret == -1) {				if (errno == EISCONN) {					printf("%15s - %2u second(s)\n", (char*)inet_ntoa(connlist[i].addr.sin_addr), (time(0)-connlist[i].a));					log_host((char*)inet_ntoa(connlist[i].addr.sin_addr));					close(connlist[i].s);					connlist[i].status = S_NONE;				}				if ((errno != EALREADY) && (errno != EINPROGRESS)) {					close(connlist[i].s);					connlist[i].status = S_NONE;				}			}			else {				printf("%15s - %2u second(s)\n", (char*)inet_ntoa(connlist[i].addr.sin_addr), (time(0)-connlist[i].a));				log_host((char*)inet_ntoa(connlist[i].addr.sin_addr));				close(connlist[i].s);				connlist[i].status = S_NONE;			}		}	}}void fatal(char * err) {	int i;	printf("Error: %s\n", err);	for (i=0; i<MAX_SOCKETS; i++) {		if (connlist[i].status >= S_CONNECTING) {			close(connlist[i].s);		}	}	exit(-1);}void log_host(char * h) {	char buffer[1024];	int fd;	if(strlen(h) > 1023) {		printf("HostName too long!\n");		exit( -1 );	}	sprintf(buffer, "%s\n", h);	fd = open(IP_LOG, O_CREAT | O_WRONLY | O_APPEND, 0644);	write(fd, buffer, strlen(buffer));	close(fd);        status = 1;}