Use raw sockets in Go

谁都会走 提交于 2020-01-10 15:38:29

问题


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:


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 { ... }
}


来源:https://stackoverflow.com/questions/18427655/use-raw-sockets-in-go

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!