I am trying to write a port scanner for a security course. I decided to write it in C on Linux as I’ve never done anything networking related outside of Java. I’m using GCC 4.4.5 on Ubuntu 10.10. I’ve got a main function that parses arguments and then calls a scan function with the resultant variables. Here is my complete program: http://pastebin.com/DHU7SEQR
The problem I’m having is that it doesn’t work properly (it reports that all ports are open) unless I print out the variables received from the user before calling the function (or rearrange the order of the parameters passed to the executable), which makes absolutely no sense to me. Note the line that is commented out (150), leaving this line commented out and compiling with the command
gcc scanner.c -o scanner
and then running the program with
./scanner -a 127.0.0.1 -b 0 -e 1000 -t 1000
results in it reporting all ports to be open. However, uncommenting that line (ie, printing out all the variables before calling the function), results in the ports’ status being reported correctly. Rearranging the order of the parameters to
./scanner -b 0 -e 1000 -t 1000 -a 127.0.0.1
seems to work as well, as does adding a printf statement to each case block (even when not printing the variables themselves).
Check the manpage for
getsockopt(2).So you need to initialize
lenon line 82.Note: There might be other problems with the code.