//ver [Sat Mar 31 15:56:07 CEST 2001] -- 555 /*================================================================ ************************* ****** ***** *** ** * ++[X3ni0n]++ xenion@libero.it ~ http://www.tba.tsx.org 30/3/2k+1 ************************* ****** ***** *** ** * IP_LIST v1.0 an ip_list generator.. useful with some net-utilities compile: gcc -Wall -o ip_list ./ip_list.c usage: ./ip_list examples: ./ip_list 192.*.12-50.1 list_file ================================================================*/ #include #include #include int getp(const char *c,int *w) { /*return '.' position in w[2] (first pos.==1, not 0!!)*/ unsigned char i=0,k; for(k=0;k<=2;k++) { for(;c[i]!='.';i++); i++; w[k]=i; } return 1; } int strtoi(const char *c) { /* converts strings to int (something like strtod but more simple&bugged) : "12"= 12 "012"= 12 "a1"= 491= ('a'-48)*10+1 */ int el(int x) { /* el= 10^x */ int y,r=1; for(y=1;y!=x;y++) r*=10; return r; } int p,r=0; for(p=0;p!=strlen(c);p++) r+=(c[p]-48)*el(strlen(c)-p); return r; } int get_range(const char *c, int x0, int x1, unsigned *r1, unsigned *r2) { int k=0,i; char r[80+1]; for(i=x0;i<=x1;i++) if(c[i]=='-') for(k=x0;k!=i;k++) r[k-x0]=c[k]; if(k==0) return(0); // if there isn't '-'in *c return 0 r[k-x0]=0; if(strtoi(r)>255||strlen(r)>3) return(0); *r1=strtoi(r); /*now (i= x1) && (k='-'position)*/ for(i=++k;i<=x1;i++) r[i-k]=c[i]; r[i-k]=0; if(strtoi(r)>255||strlen(r)>3) return(0); *r2=strtoi(r); return(1); } int get_campo(const char *c, int x0, int x1, unsigned *r1, unsigned *r2) { int i; char r[80+1]; for(i=x0;i<=x1;i++) if(c[i]=='-'||c[i]=='*') return(0); for(i=x0;i<=x1;i++) r[i-x0]=c[i]; r[i-x0]=0; if(strtoi(r)>255||strlen(r)>3) return(0); *r1=*r2=strtoi(r); return(1); } int main(int argc, char *argv[]) { FILE *out; struct { unsigned l1,h1; /*l1-h1.l2-h2.l3-h3.l4-h4*/ unsigned l2,h2; unsigned l3,h3; unsigned l4,h4; } ip; int pos[3],ipx[5]; char ip_input[80]; if(argc<3) { printf("ip_list generator v1.0 by xenion@libero.it * tba member\n\n"); printf("usage: ./ip_list \n"); return 0; } if((out=fopen(argv[2],"w"))==NULL) { printf("%s: %s: unable to open file\n",argv[0],argv[2]); return 2; } ip.l1=ip.h1=ip.l2=ip.h2=ip.l3=ip.h3=ip.l4=ip.h4=0; strcpy(ip_input,argv[1]); getp(ip_input,pos); // get '.' position : "127.0.0.1" => 4 6 8 // * if(ip_input[0]=='*') {ip.l1=0;ip.h1=255;} if(ip_input[pos[0]]=='*') {ip.l2=0;ip.h2=255;} if(ip_input[pos[1]]=='*') {ip.l3=0;ip.h3=255;} if(ip_input[pos[2]]=='*') {ip.l4=0;ip.h4=255;} // x-y get_range(ip_input, 0, pos[0]-2, &ip.l1,&ip.h1); get_range(ip_input, pos[0], pos[1]-2, &ip.l2, &ip.h2); get_range(ip_input, pos[1], pos[2]-2, &ip.l3, &ip.h3); get_range(ip_input,pos[2],strlen(ip_input)-1, &ip.l4,&ip.h4); // x get_campo(ip_input, 0, pos[0]-2, &ip.l1,&ip.h1); get_campo(ip_input, pos[0], pos[1]-2, &ip.l2, &ip.h2); get_campo(ip_input, pos[1], pos[2]-2, &ip.l3, &ip.h3); get_campo(ip_input,pos[2],strlen(ip_input)-1, &ip.l4,&ip.h4); /* calcolo numero totale ip */ ipx[0]=ip.h1-ip.l1; ipx[1]=ip.h2-ip.l2; ipx[2]=ip.h3-ip.l3; ipx[3]=ip.h4-ip.l4; ipx[4]=1; if(ipx[0]>0) ipx[4]*=(ipx[0]+1); if(ipx[1]>0) ipx[4]*=(ipx[1]+1); if(ipx[2]>0) ipx[4]*=(ipx[2]+1); if(ipx[3]>0) ipx[4]*=(ipx[3]+1); printf("list_file : [%s]\n",argv[2]); printf("ranges : [%d-%d.",ip.l1,ip.h1); printf("%d-%d.",ip.l2,ip.h2); printf("%d-%d.",ip.l3,ip.h3); printf("%d-%d]",ip.l4,ip.h4); printf(" -> [%d]\n",ipx[4]); printf("working...\n"); fprintf(out,"[%d-%d.",ip.l1,ip.h1); fprintf(out,"%d-%d.",ip.l2,ip.h2); fprintf(out,"%d-%d.",ip.l3,ip.h3); fprintf(out,"%d-%d]\n",ip.l4,ip.h4); for(ipx[0]=ip.l1;ipx[0]<=ip.h1;ipx[0]++) for(ipx[1]=ip.l2;ipx[1]<=ip.h2;ipx[1]++) for(ipx[2]=ip.l3;ipx[2]<=ip.h3;ipx[2]++) for(ipx[3]=ip.l4;ipx[3]<=ip.h4;ipx[3]++) fprintf(out,"%d.%d.%d.%d\n",ipx[0],ipx[1],ipx[2],ipx[3]); printf("done.\n"); fclose(out); return(1); }