dqj96395 2013-08-25 09:39
浏览 122

在Go中使用原始套接字

I'm trying to write a program that receives DHCP discoveries (UDP) and forwards them on to a given IP address using a different source IP address depending on the content of a specific field (GIADDR) in the DHCP packet. I could get working the receiving and sending bit but I'm having an issue with using as IP source address anything that is not a configured IP address on the local machine. I believe that this can only be done using Raw sockets; is that true ? Are there any examples out there on how to do that in Go ? I've spent a couple of days looking around but could not find much.

Cheers, Sal

  • 写回答

1条回答 默认 最新

  • dongzhuo1958 2013-08-28 04:30
    关注

    There are a number of hurdles to jump with what you propose:

    Security

    In general, being able to set the source IP address for a packet could be a very dangerous thing security wise. Under linux, in order to forge your own raw DHCP packets with custom headers, you will need to run your application as root or from an application with the CAP_NET_RAW capability (see setcap).

    Raw Sockets in Go

    The standard net library does not provide raw socket capability because it is very specialized and the API may be subject to change as people begin to use it in anger.

    The go.net subrepository provides an ipv4 and an ipv6 package, the former of which should suit your needs:

    http://godoc.org/code.google.com/p/go.net/ipv4#NewRawConn

    Header Spoofing

    You will need to use ipv4.RawConn's ReadFrom method to read your source packet. You should then be able to use most of those fields, along with your GIADDR logic, to set up the headers for the WriteTo call. It will probably look something like:

    for {
      hdr, payload, _, err := conn.ReadFrom(buf)
      if err != nil { ... }
      hdr.ID = 0
      hdr.Checksum = 0
      hdr.Src = ...
      hdr.Dst = ...
      if err := conn.WriteTo(hdr, payload, nil); err != nil { ... }
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥30 自适应 LMS 算法实现 FIR 最佳维纳滤波器matlab方案
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥15 Python3.5 相关代码写作
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动