1
votes

I want to implement UDP ping for my client/server application where the client sends UDP packets to any of the server's ephemeral port in trying to get ICMP port unreachable reply.

I have the following code. ReadFromUDP() return error = nil and 0 bytes read from socket.

Question is, how can I read in specific port unreachable ICMP reply from the server?

conn, _ := net.ListenUDP("udp4", src)
defer conn.Close()

t := time.Now()
conn.SetDeadline(t.Add(100 * time.Millisecond))
conn.SetReadDeadline(t.Add(250 * time.Millisecond))

w, e := conn.WriteTo([]byte("PING"), dst)
if e != nil {
    return nil, errors.New("Failed to send UDP4 ping request")
}

r, _, e := conn.ReadFromUDP(b4)
if e != nil {
    return nil, errors.New("Failed to read UDP4 response packets")
}
1
Did you try using IPConn and ReadFromIP instead of ReadFromUDP ?Brad Peabody

1 Answers

0
votes

Check the first 2 bytes of the reply message for type 3, code 3 (port unreachable):

To quote RFC792:

Destination Unreachable Message

    0                   1                   2                   3
    0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   |     Type      |     Code      |          Checksum             |
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   |                             unused                            |
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   |      Internet Header + 64 bits of Original Data Datagram      |
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

   IP Fields:

   Destination Address

      The source network and address from the original datagram's data.

   ICMP Fields:

   Type

      3

   Code

      0 = net unreachable;

      1 = host unreachable;

      2 = protocol unreachable;

      3 = port unreachable;

      4 = fragmentation needed and DF set;

      5 = source route failed.