Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Receiving ICMP Host Unreachable message on UDP socket in Python

I'm writing a simple UDP chat server in Python 2.7 on Linux 2.6.38.

How can ICMP error messages be read that a host(client) receives when it sends UDP segments to say, an unreachable server?

I tried

sockFd.setsockopt( socket.IPPROTO_IP, socket.IP_RECVERR, 1 ) 

But socket.IP_RECVERR isn't defined in socket module.

I tried using a timeout on sockFd.recvfrom and doing sendto a second time but that didn't help. Is there an API to read the ICMP errors received by the host?

like image 532
crk Avatar asked Dec 05 '25 14:12

crk


1 Answers

The question is a bit old but I'll answer it since I've faced the same problem.

The constant IP_RECVERR is defined in the "IN" module, so the next statement should do the job:

import socket
import IN
sockFd.setsockopt( socket.IPPROTO_IP, IN.IP_RECVERR, 1 )
like image 76
Oleg Andriyanov Avatar answered Dec 08 '25 05:12

Oleg Andriyanov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!