-2

basically im building a port scanner of sorts (well technically it piggy-backs another ports-canner but thats not important). The problem lies within the code in which the user inputs the target IP address to be scanned. The program runs smoothly until it returns a 'segmentation fault: 11' error after the user inputs the IP address.

I have done some research into segmentation faults and i believe that this particular error is thanks to the fact that my code has declared the targetIP variable as a char, yet contains numbers with multiple '.'s as you would need for an IP address.

So my question is, what variable type will i need that can handle numbers with multiple '.'s? eg: 225.1.1.24

code:

 char *target_IP;
    scanf("%s",target_IP);

    // segmentation fault occurs here so bellow printf is never displayed vv

    printf("\nWriting target IP [%s] to file\n",target_IP);
user4493605
  • 391
  • 2
  • 18
  • 8
    Show the section of code that reads the IP together with relevant variable declarations. `char` would certainly be wrong, if at all, it should be `char[]`. – PMF Mar 25 '15 at 08:18
  • 2
    There shouldn't be any problem with char[] containing "multiple dots". Revise your code. – Matt Mar 25 '15 at 08:18
  • you could use `char*` to store the characters and convert the string into a `struct` of `int`s not including the `'.'` – James Mar 25 '15 at 08:27
  • 2
    There is no space allocated for `target_IP`. – Anto Jurković Mar 25 '15 at 08:30
  • I don't understand the downvotes. This is not a trivial question, if you want to be portable. – fjardon Mar 25 '15 at 08:34
  • 1
    Check [segmentation fault using scanf](http://stackoverflow.com/questions/13447891/segmentation-fault-using-scanf). – Anto Jurković Mar 25 '15 at 08:35

5 Answers5

1

The current POSIX standard allows that scanf does a memory allocation to the variable. Notice the additional & to change the variable.

char *target_IP;
scanf("%ms", &target_IP);

Otherwise you need to do

char target_IP[200];
scanf("%s", target_IP);

and still risk a malicious buffer overflow.

Joop Eggen
  • 107,315
  • 7
  • 83
  • 138
0

save your input in a string and use strtok() using delimiter as '.' Then store it in char array or 4 different char variables

Chintan Patel
  • 310
  • 1
  • 2
  • 11
0

Have a look at this example:

#include <stdio.h>

int main() {
    int part1, part2, part3, part4;
    printf("Enter IP (e.g. 127.0.0.1): ");
    scanf("%d.%d.%d.%d", &part1, &part2, &part3, &part4);
    printf("IP is %d - %d - %d - %d\r\n", part1, part2, part3, part4);
}
itchee
  • 820
  • 5
  • 20
  • Thank you @itchee, although there are a lot of good answers this is definitely the simplest and easiest to use for a newbe like me. – user4493605 Mar 25 '15 at 10:01
0

If you are working in a POSIX (or even Windows) environment. You can use: inet_addr:

Both these functions returns an integer type suitable for the IPv4 adresses. In practice you should use a variable of type in_addr which contains a member named s_addr (S_addrunder windows) of the correct type.

But, beware, it only works for IPv4. As things go, you'd better to prepare for IPv6 addresses too (and these addresses are NOT separated by .).

fjardon
  • 7,921
  • 22
  • 31
0

I thing the problem is with target_IP itself.
You are declaring target_IP as a character pointer and nowhere allocating memory to char pointer. Then you are trying to write value to unallocated memory location which will definitely cause segmentation fault.

So first allocate memory to target_IP and then use it as you do.

target_IP = malloc(16); 
scanf("%s", target_IP);
niyasc
  • 4,440
  • 1
  • 23
  • 50