Python problem with sending/receiving packets to local host address using socket and dpkt module

79 Views Asked by At

I'm trying to send an ICMP echo packet to my local host address and then return a response acknowledging that the packet was received. So far I have

def send_echo(socket, data, id, seq, dest):
    echo = dpkt.icmp.ICMP.Echo()
    echo.id = id
    echo.seq = seq
    echo.payload = bytes(data, 'utf-8')

   icmp = dpkt.icmp.ICMP()
   icmp.type = dpkt.icmp.ICMP_ECHO
   icmp.data = echo

   icmp_packed = icmp.pack()
   socket.sendto(icmp_packed, destination) #sending packet here 

And then I wrote a function to receive this packet as follows:

def recv_response():
    mySocket = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_ICMP) 
    while True:
    try:
        received, addr = mySocket.recvfrom(1024)
        break
    except socket.error:
        test = 0
    return received

The loop gets stuck, so my guess is its not sending correctly but I'm not certain.

Main function:

def main():
    host_port = tuple(["127.0.0.1", 65432])
    send_echo(make_socket(50,50), "data", 0, 0, host_port)    
    recv_response()

So I get stuck in the loop in response but I'm not really sure if this is a sending problem or a receiving problem. Any help would be really appreciated.

Edit: Thanks Barmar, I used wireshark and successfully sent to 8.8.8.8. So the issue is with the receiving function.

0

There are 0 best solutions below