Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

net: IPConn can't handle ARP packets #7574

Closed
gopherbot opened this issue Mar 18, 2014 · 5 comments
Closed

net: IPConn can't handle ARP packets #7574

gopherbot opened this issue Mar 18, 2014 · 5 comments

Comments

@gopherbot
Copy link

by jikai507:

package main

import (
    "bytes"
    "encoding/binary"
    . "fmt"
    "net"
    "strconv"
    "strings"
)

type ARP struct {
    NetType       uint16
    ProtoType     uint16
    MacAddrLen    uint8
    IPAddrLen     uint8
    OperationCode uint16
    SrcMacAddr    [6]byte
    SrcIPAddr     [4]byte
    DstMacAddr    [6]byte
    DstIPAddr     [4]byte
}

func ParseMacAddr(macaddr string) (mac [6]byte) {
    segments := strings.Split(macaddr, "-")
    for i := 0; i < 6; i++ {
        value, err := strconv.ParseUint(segments[i], 16, 64)
        if err != nil {
            panic("parse mac addr error")
        }
        mac[i] = byte(value)
    }
    return mac
}

func ParseIP(ipaddr string) (ip [4]byte) {
    segments := strings.Split(ipaddr, ".")
    for i := 0; i < 4; i++ {
        value, err := strconv.Atoi(segments[i])
        if err != nil {
            panic("parse ip addr error")
        }
        ip[i] = byte(value)
    }
    return ip
}

func main() {
    var (
        laddr net.IPAddr = net.IPAddr{IP: net.ParseIP("192.168.137.111")}
        raddr net.IPAddr = net.IPAddr{IP: net.ParseIP("192.168.137.1")}
    )

        // problems happens in this row...
    ipconn, err := net.DialIP("ip:arp", &laddr, &raddr)
    if err != nil {
        Println("DialIP() error:", err.Error())
        return
    }
    defer ipconn.Close()

    var (
        arp ARP
    )
    arp.NetType = 0x1      
    arp.ProtoType = 0x0800 
    arp.MacAddrLen = 6
    arp.IPAddrLen = 4
    arp.OperationCode = 0x1
    arp.SrcMacAddr = ParseMacAddr("00-23-5A-BD-DF-82")
    arp.SrcIPAddr = ParseIP("192.168.137.111")
    arp.DstMacAddr = ParseMacAddr("00-00-00-00-00-00")
    arp.DstIPAddr = ParseIP("192.168.137.1")

    var (
        buffer bytes.Buffer
    )
    binary.Write(&buffer, binary.BigEndian, arp)
    if _, err := ipconn.Write(buffer.Bytes()); err == nil {
        Println("send success")
    }

}


What does 'go version' print?
go version go1.2.1 windows/386

What happened?
The error message like this :
DialIP() error: dial ip:arp 192.168.137.1: GetProtoByName: The requested name is valid,
but no data of the requested type was found.

What should have happened instead?
If it can run successfully, it should output "send success".

Please provide any additional information below.
@mikioh
Copy link
Contributor

mikioh commented Mar 18, 2014

Comment 1:

ARP is not part of IPv4 protocol family.

Status changed to Invalid.

@mikioh
Copy link
Contributor

mikioh commented Mar 19, 2014

Comment 2:

PS: FWIW, you can use net.ParseMAC which can parse several burn-in/hardware MAC/EUI-48
to EUI-64 address representations instead.

@gopherbot
Copy link
Author

Comment 3 by jikai507:

how can i send a arp package.
I try the:
net.Dial("arp" , "127.0.0.1")
but it also can not compiled success.
2014��3��19�� ����11:10�� <go@googlecode.com>���

@mikioh
Copy link
Contributor

mikioh commented Mar 22, 2014

Comment 4:

Use platform-specific services that Go stdlib and subrepo stuff never provide:
On Windows, some APIs such as SendArp exist,
On Unix-like systems, you can use BPF/LSF and/or NetMap/PF_RING,
On some systems that equip with kinda big interfaces such as Mellanox, Chelsio, etc, use
APIs provided by such vendors
Anyway more questions about you are asking for would be not related to the Go language
stuff.

@wheelcomplex
Copy link
Contributor

@golang golang locked and limited conversation to collaborators Jun 25, 2016
This issue was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants