icmp and raw sockets in python

Sagy Drucker sagysrael at gmail.com
Tue Dec 13 11:50:11 EST 2011


hello
i am relatively new to python, so please be considerate...

i'm implementing a server and a client via raw_sockets.
i have the necessary privileges.

now, the server i defined so:
host = socket.gethostbyname(socket.gethostname())
address = (host, 22224)
sockSer = socket.socket(socket.AF_INET, socket.SOCK_RAW,
socket.IPPROTO_ICMP)
sockSer.bind(address)
sockSer.ioctl(socket.SIO_RCVALL, socket.RCVALL_ON)
packet, addr = sockSer .recvfrom(4096)   # wait for packet from client

Q1) why can't i simply type: hosts = 'localhost'.
if i do so, it doesn't allow me to write the line:
sockSer.ioctl(socket.SIO_RCVALL, socket.RCVALL_ON).
only when doing gethostbyname(socket.gethostname()) i get
192.168.1.101
and then it works.

in a different class:
the client socket:
host = socket.gethostbyname(socket.gethostname())
address = (host, 22224)
sockCli = socket.socket(socket.AF_INET, socket.SOCK_RAW,
socket.IPPROTO_ICMP)

Q2) do i also need to type: sockCli.ioctl(socket.SIO_RCVALL,
socket.RCVALL_ON)
or maybe sockCli.connect(address)
for the client socket?

now, the problems arise when i do the following:
1) send a packet from client to socket:
header=...
payload='a'
sockCli.sendto(header + payload, address)

2) receive packet in server and send file to client:
while(true):
    data, addr = sockSer.recvfrom(4096)
    header2=...
    payload2='b'
    sockSer.sendto(header2 + payload2, addr)

now, my important question is:
Q3) the server sent only 1 packet to client, with payload 'b'.
what happens is, my client actually receives 2 packets in the while
loop:
first packet is what the client itself sent to server, and the other
packet is from the client got from the server.
hence my output is 'ab' instead of simply 'b'
why is this happening???

NOTE: i didn't type the entire code, but i think my
syntax,parsing,header composition etc.. are correct.
is there an obvious problem in my code?
if necessary i'll upload the entire code.

thanks



More information about the Python-list mailing list