"dial ip4:tcp: lookup ip:port: no such host" error in Golang

187 views Asked by At

I want to learn raw sockets in Golang and want to create a tool to send raw TCP packages. I created IPv4 and TCP headers. I researched about this thing on the internet and i used ChatGPT. I got an error about "net.Dial" function. The error i got:

dial ip4:tcp: lookup 192.168.0.44:9095: no such host

The host i tried to send raw TCP packages is reachable, i can ping it.

The codes of the tool which will send raw TCP packages:

package main

import (
    "fmt"
    "net"

    "github.com/google/gopacket"
    "github.com/google/gopacket/layers"
)

func main() {
    /*
        source_addr := net.IP("192.168.0.254")
        destination_addr := net.IP("192.168.0.41")
        source_port := layers.TCPPort(12345)
        destination_port := layers.TCPPort(9095)
    */

    ipv4_header := &layers.IPv4{
        Version:    4,
        Length:     20,
        TOS:        0,
        Id:         1234,
        FragOffset: 0,
        TTL:        64,
        Protocol:   6,
        Checksum:   0,
        SrcIP:      net.IP{192, 168, 0, 254},
        DstIP:      net.IP{192, 168, 0, 44},
        Options:    nil,
        Flags:      layers.IPv4DontFragment,
    }

    tcp_header := &layers.TCP{
        SrcPort: layers.TCPPort(12345),
        DstPort: layers.TCPPort(9095),
        Seq:     123,
        Ack:     0,
        SYN:     true,
        Window:  8192,
    }

    buffer := gopacket.NewSerializeBuffer()
    options := gopacket.SerializeOptions{}
    serializing_error := gopacket.SerializeLayers(buffer, options, ipv4_header, tcp_header)
    if serializing_error != nil {
        fmt.Println(serializing_error.Error())
    }

    //connection, connection_error := net.Dial("ip4:tcp", "192.168.0.41")
    //fmt.Println(fmt.Sprintf("%s:%d", ipv4_header.DstIP.String(), layers.TCPPort(9095)))
    connection, connection_error := net.Dial("ip4:tcp", "192.168.0.44:9095")
    if connection_error != nil {
        fmt.Println(connection_error.Error())
    }
    defer connection.Close()

    packet := buffer.Bytes()
    _, sending_error := connection.Write(packet)
    if sending_error != nil {
        fmt.Println(sending_error.Error())
    }
}

And i wanted ChatGPT to create a python script which would listen raw TCP packages, i use it to listen the port for raw TCP packages on a Linux WM.

I tried to run the tool on another Linux VM but i got the same error.

0

There are 0 answers