ping program

This is a discussion on ping program within the Networking/Device Communication forums, part of the General Programming Boards category; hello.
doing some raw sockets programming and having some fun...this stuff has some real potential!
but,
im having a strange ...

say 192.168.0.5 is a machine on my network and 192.168.0.7 is my machine:

if i do:
./pinger 192.168.0.5 -s 192.168.0.7
it works! its great i even confirmed the ICMP REPLY packet with a sniffer (ethereal) and the ttl and the ID match....very sexy
but if i do
./pinger 192.168.0.5
(note that getip() does return 192.168.0.7) no reply is received....looking closer i found that the one with out source specified on the command line ends up sending only an ip packet with the fragment flags turned on! what the hell is that all about?

First off I should warn you that I know nothing about sockets so this may not be the answer to your problem, but I did notice that you have some memory related problems that can often lead to strange behaviour.

1) you do not free() any of the memory that you malloc().

2) you malloc() memory pointed to by ip and ipcmp, then change the value of these pointers to point to some other memory locations. This leaves the previous memory hanging around with no way of using it or freeing it up. I suspect, given the rest of your code, that you do not need to malloc these two areas in the first place. You also do the same thing with ip_reply later in the code.

3) sizeof(packet) will not give you the size of the packet you sent but the size of the pointer to char that packet is. I know that this has nothing to do with the code not working, but it's just a point to notice ...

All these things may have no effect whatsoever on the curent problem you are seeing, but it is worth fixing them to avoid future problems, but one of them may have some strange side effect that causes your problems...

Originally posted by DavT First off I should warn you that I know nothing about sockets so this may not be the answer to your problem, but I did notice that you have some memory related problems that can often lead to strange behaviour.

1) you do not free() any of the memory that you malloc().

2) you malloc() memory pointed to by ip and ipcmp, then change the value of these pointers to point to some other memory locations. This leaves the previous memory hanging around with no way of using it or freeing it up. I suspect, given the rest of your code, that you do not need to malloc these two areas in the first place. You also do the same thing with ip_reply later in the code.

3) sizeof(packet) will not give you the size of the packet you sent but the size of the pointer to char that packet is. I know that this has nothing to do with the code not working, but it's just a point to notice ...

All these things may have no effect whatsoever on the curent problem you are seeing, but it is worth fixing them to avoid future problems, but one of them may have some strange side effect that causes your problems...

Good luck

2. you are right...i dont need to malloc ip and icmp because of these lines: