gonetwork-programmingudpicmp

Only receive 0 length ICMP packets from icmp.Packetconn


The main goal is to detect open UDP ports by sending data trough the ports and listening for incoming ICMP messages that the port is unreachable. For this the following Go function is used that runs as a goroutine:

func listenIcmp(ipAddr string) {
    conn, err := icmp.ListenPacket("ip4:1", ipAddr)
    if err != nil {
    log.Println("Error while listening for ICMP packets: ")
        log.Println(err)
    }

    for {
        var incoming []byte
        length, sourceIP, err := conn.ReadFrom(incoming)
        if err != nil {
            log.Println(err)
            continue
        }

        log.Printf("message = '%s', length = %d, source-ip = %s", string(incoming), length, sourceIP)

    }
}

this is able to detect ICMP pings and destination unreachable but always returns messages of length 0. Example:

2023/10/11 14:38:32 message = '', length = 0, source-ip = 127.0.0.1

I already tried parsing the message but since it is empty, a runtime error is returned. Moreover, I gave the go executable root permission but this did not work either.

Edit: OS is Ubuntu 20.04


Solution

  • Found my own mistake...

    I tried to read into a buffer that was not initialized. The following change to the incoming buffer fixes the problem:

    // var incoming []byte
    incoming := make([]byte, 80)