Thread: Random IP Generation Program

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    9

    Random IP Generation Program

    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 address
    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);
    	
    }
    it seems the code has a problem with the memcpy() function.Please Help figure this one out!
    Thanks.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Care to share what the compiler error was exactly?

    Why not save everyone a lot of time?

  3. #3
    Registered User
    Join Date
    Apr 2011
    Posts
    9
    I have received compiler warnings that looks like
    Code:
    random_ip.c: In function ‘random_ip’:
    random_ip.c:51: warning: this decimal constant is unsigned only in ISO C90
    random_ip.c:83: warning: return from incompatible pointer type
    random_ip.c:83: warning: function returns address of local variable
    It has a 'Segmentation fault' Error At Runtime...

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Did you do the math to figure out A) how big an IP address is and B) how big your buffers are?

    Code:
    char *ip_buffer[BUFSIZE];
    Do you really expect to create a multi gigabyte (argv[2]*1024*1024*1024) array on the program's stack?

    argv[2] is a pointer, you're treating it like a number, probably at least 5 digits... so lets assume conservativley it comes up with 10001...
    How big is that array going to be?

    10001 *1024 *1024 *1024 = 10,738,491,981,824

    Got ram?
    Last edited by CommonTater; 04-11-2011 at 01:13 AM.

  5. #5
    Registered User
    Join Date
    Apr 2011
    Posts
    9
    Thanks Tater!I tried once with 1024*1024 array size it work but too slow.


    What size a program's stack is?It might be are reason why memcpy() fails

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by lvandaz View Post
    Thanks Tater!I tried once with 1024*1024 array size it work but too slow.


    What size a program's stack is?It might be are reason why memcpy() fails
    Probably about a tenth of a percent of what you're asking for... typically a megabyte.

    Look, I don't know exactly what you're trying to do (although it looks suspiciously like a DOS Bomb) but you're going to have to figure out a way to do it without asking your system for (potentially) 10 terrabytes of ram.
    Last edited by CommonTater; 04-11-2011 at 06:21 AM.

  7. #7
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    STOP ... Explain your intentions immediately!

    Anyone giving any advice here should think, would they be happy if this program, or whatever it evolved into or is really part of, happened to randomly generate your IP address? I mean what do any of you think is going to happen next?
    The only possible thing I can think of for generating random IP Addresses is that this person is atempting to write a virus/worm etc.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You've got some major problems anyway:
    Code:
    char* random_ip(...) /* return a pointer to char */
    {
            ....
    	char *ip_buffer[BUFSIZE]; /* make an array of pointers to characters ... local to this function */
            ....
    return ip_buffer; /* oops */
    }
    Or you could just you know:
    Code:
    printf( "%d.%d.%d.%d", rand()&0xFF,rand()&0xFF,rand()&0xFF,rand()&0xFF );
    Quzah.
    Hope is the first step on the road to disappointment.

  9. #9
    Registered User
    Join Date
    Apr 2011
    Posts
    9
    Thanks!I think I figure out some other solns for my "problem" :-)

  10. #10
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by lvandaz View Post
    Thanks!I think I figure out some other solns for my "problem" :-)
    Ummm.... what is a "soln" ?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Random Generation
    By davewang in forum C++ Programming
    Replies: 11
    Last Post: 04-11-2010, 02:02 AM
  2. Random number generation
    By parisha in forum C Programming
    Replies: 9
    Last Post: 12-17-2008, 11:00 AM
  3. random number generation
    By megastar in forum C Programming
    Replies: 4
    Last Post: 06-26-2007, 04:51 AM
  4. Random number generation
    By Lisa Mowbray in forum C++ Programming
    Replies: 4
    Last Post: 04-30-2002, 12:22 PM
  5. random number generation
    By freakme in forum C Programming
    Replies: 1
    Last Post: 03-27-2002, 12:40 AM