Hello,I was Designing a Program that has two function the random_ip() and main()
the random_ip() returns an array of random generated ip addressit seems the code has a problem with the memcpy() function.Please Help figure this one out!Code:#include<stdio.h> #include<netinet/in.h> #include<stdlib.h> #include<string.h> #include<sys/socket.h> #include<arpa/inet.h> #include<errno.h> char* random_ip(char figure,int size,int check)//a thousand,mil,bil etc { printf("Figure %c Size %d\n",figure,size); int i=65536; int j=0; int BUFSIZE; struct sockaddr_in temp; switch(figure) { case 't': BUFSIZE=(size*1024); break; case 'm': BUFSIZE=(size*1024*1024); break; case 'b': BUFSIZE=(size*1024*1024*1024); break; default:printf("Choose btn choices available!\n"); } printf("BUFSIZE %d\n",BUFSIZE); char *ip_buffer[BUFSIZE]; srand(i);//seed the randomizer while(j<BUFSIZE) { ip_buffer[j]=(char*)malloc(16); temp.sin_addr.s_addr=(unsigned long)rand()%4294967295; ip_buffer[j]=(char*)malloc(16); strcpy(ip_buffer[j],inet_ntoa(temp.sin_addr)); j++; } if(check){ int a; int repeated=0; char*ip; for(j=0;j<BUFSIZE;j++) { ip=(char*)malloc(16); strcpy(ip,ip_buffer[j]); for(a=++j;a<BUFSIZE;a++) { if((strcmp(ip,ip_buffer[a]))==0){//repeated ip repeated++; printf("\t\t\t\tFound %s repeated addresses.\n",ip); } } ip=NULL; } if(repeated>0) printf("Found %d repetead Addresses in %d Addresses.\n",repeated,BUFSIZE); } return ip_buffer; } main(int argc,char**argv) { int i=9; int j=0; int BUFSIZE; srand(i);//seed the randomizer switch((char)*argv[1]) { case 't': BUFSIZE=(atoi(argv[2])*1024); break; case 'm': BUFSIZE=(atoi(argv[2])*1024*1024); break; case 'b': BUFSIZE=(atoi(argv[2])*1024*1024*1024); break; default:printf("Choose btn choices available!\n"); } printf("BUFSIZE MAIN:%d\n",BUFSIZE); char *ip_buffer[BUFSIZE]; memcpy(ip_buffer,random_ip((char)*argv[1],atoi(argv[2]),0),BUFSIZE); while(j<BUFSIZE) { printf("%s\t\t\t %d\n",ip_buffer[j],j); //printf("%s\n",inet_addr((char*)ip_buffer[j])); j++; }printf("%d Addresses\n",j); }
Thanks.