/**
* Notice: The program is not debug on internet and not use thread supervene.
* date : 6-26
* author: jker
**/
#include <sys/socket.h>
#include <stdio.h>
#include <netinet/in.h>
#include <string.h>
#include <netdb.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#define LINE 1024
#define dict "dict.txt"
#define result "manager.txt"
void itos(int num,char *str){
int a=num;
int x,y,z;
x=a/100;
y=a%100/10;
z=a%10;
if(x==0){
if(y==0){
str[0]+=z;
str[1]='\0';
}else{
str[0]+=y;
str[1]+=z;
str[2]='\0';
}
}else{
str[0]+=x;
str[1]+=y;
str[2]+=z;
str[3]='\0';
}
return;
}
char *nip(char *oip){
char *str[4]={""},*rtn;
char *delim = ".";
char * tmp;
int i=0;
char arr[4]={48,48,48,'\0'};
rtn = malloc(512);
memset(rtn,'\0',512);
tmp = strtok(oip,delim);
str[i++]=tmp;
while(tmp=strtok(NULL,delim)){
str[i++]=tmp;
}
for(i=3;i>=0;i--){
if(atoi(str[i])<255){
itos(atoi(str[i])+1,arr);
str[i]=arr;
break;
}
}
for(i=0;i<4;i++){
strcat(rtn,str[i]);
if(i<3)strcat(rtn,delim);
}
return rtn;
}
int main(int argc,char *argv[]){
int sc,fd;
unsigned short port = 80;
struct sockaddr_in add;
char *ip,*sip,*eip;
struct hostent *host;
char rheader[LINE],dictbuf[LINE],resbuf[LINE];
char *dir,path[LINE]="",*arr[LINE],*tmp,state[3],manager[LINE];
int i=0,n=0,bytes;
if(argc != 3){
printf("please input startIp and endIp.\r\n");
return -1;
}
dir = get_current_dir_name();
strcat(path,dir);
strcat(path,"/");
strcat(path,dict);
fd = open(path,O_RDONLY);
read(fd,dictbuf,sizeof(dictbuf));
close(fd);
arr[n++]=strtok(dictbuf,"\r\n");
while(tmp=strtok(NULL,"\r\n"))arr[n++]=tmp;
ip = malloc(512);
memset(ip,'\0',512);
sip = argv[1];
eip = argv[2];
strcpy(ip,sip);
memset(manager,'\0',LINE);
memset(rheader,'\0',LINE);
sc = socket(AF_INET,SOCK_STREAM,0);
if(sc == -1){
printf("create socket fail!\r\n");
return -1;
}
printf("create socket ok.\r\n");
bzero(&add,sizeof(add));
add.sin_family=AF_INET;
add.sin_port=htons(port);
bzero(&path,sizeof(path));
strcat(path,dir);
strcat(path,"/");
strcat(path,result);
fd = open(path,O_WRONLY|O_CREAT);
do{
host = gethostbyaddr(ip,strlen(ip),AF_INET);
if(host == (struct hostent *)NULL) {
printf("can`t get host from %s\r\n",ip);
if(strcmp(ip,eip)<0)ip=nip(ip);
continue;
}else{
printf("host name:%s\n",host->h_name);
}
add.sin_addr.s_addr=inet_addr(ip);
if(connect(sc,(struct sockaddr *)(&add),sizeof(struct sockaddr))==-1){
printf("conn fail!\r\n");
return -1;
}
for(;i<n;i++){
strcat(rheader,"HEAD ");
strcat(rheader,arr[i]);
strcat(rheader," HTTP/1.1");
strcat(rheader,"\r\n");
strcat(rheader,"Host:");
strcat(rheader,host->h_name);
strcat(rheader,"\r\n");
//strcat(rheader,"Accept:*\/*");
//strcat(rheader,"\r\n");
strcat(rheader,"Connection:Keep-Alive");
strcat(rheader,"\r\n");
write(sc, rheader, strlen(rheader));usleep(100);
bytes = read(sc, resbuf, LINE);
printf("Response from %s:\n",ip);
write(STDOUT_FILENO, resbuf, bytes);
for(i=0;i<3;i++){
state[i]=resbuf[i+9];
}
if(!((state>="400" && state<="450")||state=="500")){
strcat(manager,"http://");
strcat(manager,host->h_name);
strcat(manager,arr[i]);
strcat(manager,"\r\n");
write(fd,manager,sizeof(manager));
}
}
close(sc);
ip = nip(ip);
}while(strcmp(ip,eip)<0);
close(fd);
return 0;
}