dongzhan1383 2016-03-07 10:25
浏览 142

使用Go语言发送带有以太网标头的原始数据包

I am trying to send raw TCP packet with custom Ethernet header through raw socket in Go language. I am trying to reproduce similar code in c language.

package main

import (
    "fmt"
    "syscall"
)

func main() {

    fmt.Println("=============================================================================")
    fmt.Println("= Start sending                                                             =")
    fmt.Println("=============================================================================")

    pkt := []byte{
        0x6c, 0x62, 0x6d, 0x50, 0xe6, 0xe4, 0x94, 0xde, 0x80, 0xa5, 0xec, 0x79, 0x08, 0x00, 0x45, 0x00,
        0x00, 0x3c, 0x47, 0xd9, 0x40, 0x00, 0x40, 0x06, 0xb5, 0x94, 0xc0, 0xa8, 0x34, 0x7b, 0x36, 0xe7,
        0x11, 0x44, 0xc3, 0x66, 0x00, 0x50, 0x09, 0x58, 0x6b, 0xeb, 0x00, 0x00, 0x00, 0x00, 0xa0, 0x02,
        0x72, 0x10, 0x26, 0x38, 0x00, 0x00, 0x02, 0x04, 0x05, 0xb4, 0x04, 0x02, 0x08, 0x0a, 0x0d, 0x3d,
        0x2c, 0x32, 0x00, 0x00, 0x00, 0x00, 0x01, 0x03, 0x03, 0x07,
    }

    fd, err := syscall.Socket(syscall.AF_PACKET, syscall.SOCK_RAW, syscall.ETH_P_ALL)
    if err != nil {
        fmt.Println("=============================================================================")
        fmt.Println("= Error 1                                                                   =")
        fmt.Println("=============================================================================")
        fmt.Println(err)
    }

    addr := syscall.SockaddrInet4{
        Port: 0,
        Addr: [4]byte{127, 0, 0, 1},
    }

    err = syscall.Bind(fd, &addr)
    if err != nil {
        fmt.Println("=============================================================================")
        fmt.Println("= Error 2                                                                   =")
        fmt.Println("=============================================================================")
        fmt.Println(err)
    }

    err = syscall.SetLsfPromisc("eth0", true)
    if err != nil {
        fmt.Println("=============================================================================")
        fmt.Println("= Error 3                                                                   =")
        fmt.Println("=============================================================================")
        fmt.Println(err)
    }

    n, err := syscall.Write(fd, pkt)
    if err != nil {
        fmt.Println("=============================================================================")
        fmt.Println("= Error 4                                                                   =")
        fmt.Println("=============================================================================")
        fmt.Println(err)
        fmt.Println(n)
    } else {
        fmt.Println("=============================================================================")
        fmt.Println("= Packet is sent                                                            =")
        fmt.Println("=============================================================================")
        fmt.Println(n)
    }
}

However I am getting following error:

=============================================================================
= Start sending                                                             =
=============================================================================
=============================================================================
= Error 2                                                                   =
=============================================================================
invalid argument
=============================================================================
= Error 4                                                                   =
=============================================================================
no such device or address
-1

Please guide me if you have any knowledge.

  • 写回答

2条回答 默认 最新

  • dooo61733 2016-03-08 09:03
    关注

    What you want to accomplish is more complicated.

    1. You cannot use the kernels bind syscall to bind to a raw socket: err = syscall.Bind(fd, &addr)
    2. You want to write ethernet frames to the raw socket. In order to do so you need to assemble the ethernet frame and TCP header yourself. The whole point of using a raw socket on that level is not having the kernel do this for you.

    Some working examples in C can be found here. They are probably easy to port to Go

    评论

报告相同问题?

悬赏问题

  • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集