EDIT: the solution is on the bottom of the page maybe. I answered my question with the solution. I hope this helps the others.
I'm having here in linux a little problem. I'm programming a simple port scan but I'm having a problem with the function that takes the arguments.
I will explain on the code:
#include <stdio.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <string.h>
//this function handle the arguments.
char* ret[2]= {"NULL","NULL"}; //declaring this in global because of segmention fault?
char** arguments_handle(int argc,char **arg)
{
if(argc!=5)
{
printf("Usage:./file -p PORT-RAGE -h HOST.IP\n");
exit(1);
}
//make sure the user type the correct arguments. in this case just -h and -p
if(strcmp(arg[1],"-p")==0 || strcmp(arg[1],"-h")==0 && strcmp(arg[3],"-p")==0 || strcmp(arg[3],"-h")==0)
{
//if in the arguments we got -h or -p run this
//if is -p
if(strcmp(arg[1],"-p")==0)
{
//take the next argument in this case is the port range and put in our array
strcpy(ret[0],arg[2]);
}
else
{
strcpy(ret[1],arg[2]);
}
if(strcmp(arg[3],"-h")==0)
{
//now for the -h
strcpy(ret[1],arg[4]);
}
else
{
strcpy(ret[0],arg[4]);
}
}
return ret;
}
int main(int argc, char **argv)
{
char** ipnport;
ipnport = arguments_handle(argc,argv);
printf("IP is :%s port range is %s\n",ipnport[0],ipnport[1]);
//the rest of the code about port scan goes here. I'm just cutting
return 0x0;
}
The problem here is that I can compile right but I got Segmentation fault. I can't see where I'm wrong. I guess it is about something deal with buffer or stack overflow.
So what I'm doing here in this function is taking the argv and sending it to arguments_handle function. What that does is see where is the arguments "-p" and "-h" and "store" that in the right order in an array of char. Like this char: "char pointer to this array that contains an array of chars"
pointer pointer pointer
pointer to this-> ["firstarg","secondarg","etc"]
In that case, the "pointer pointer pointer" will be the first char of the string.
Summarizing: I want to make a string array and return that from arguments_handle to the main's function.
Any ideas? :)
Sincerely,
int3
retbeyond the bounds of the array resulting in undefined behavior.