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")
}