Use Raw Sockets in Go

I am trying to write a program that receives DHCP openings (UDP) and forwards them to a given IP address using a different source IP address depending on the contents of a specific field (GIADDR) in the DHCP packet. I could get a send and receive bit, but I had a problem using everything that is not a configured IP address on the local machine as the source IP address. I believe that this can only be done using Raw sockets; it's true? Are there any examples on how to do this in Go? I spent a couple of days looking around, but could not find much.

Cheers sal

+4
source share
1 answer

There are a number of barriers to transition with what you offer:

Security

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

Raw sockets in Go

The standard network library does not provide socket capabilities because it is very specialized, and the API can be changed as people begin to use it in anger.

The go.net subcategory provides ipv4 and the ipv6 package, the first of which should meet your needs:

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

Header Subversion

You will need to use ipv4.RawConn ReadFrom to read the source package. Then you should use most of these fields along with the GIADDR logic to configure the headers for the WriteTo call. It probably looks something like this:

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 { ... } } 
+6
source

Source: https://habr.com/ru/post/1498811/


All Articles