I'm trying to implement the UDP protocol in userspace using golang. I've reached upto the point where I'm able to get raw ethernet II packets.
My code looks something like this:
type EthernetFrame struct {
SrcAddress []byte
DestAddress []byte
EtherType []byte
Payload []byte
CRC []byte
}
func ReadEthernetPackets(ethernetSocket int) EthernetFrame {
dataBytes := make([]byte, 1518)
syscall.Read(ethernetSocket, dataBytes)
return EthernetFrame {
SrcAddress: dataBytes[0:6],
DestAddress: dataBytes[6:12],
EtherType: dataBytes[12:14],
Payload: dataBytes[14:1515],
CRC: dataBytes[1515:1518],
}
}
I've seen here(https://commons.wikimedia.org/wiki/File:Ethernet_Type_II_Frame_format.svg) that an ethernet II frame can be anywhere between 64 to 1518 bytes long, but since there is no field in the header which says anything about the payload length, I've just assumed that all the frames would be of length 1518, in which case theoretically my code should work. But since the frames are mostly arriving with different lengths my assumption fails.
How do I parse the bytes received and get only the payload? What should I keep the dataBytes length as?
SrcAddressshould be bytes0to5, not6. Also, under your theory, there will be no byte1518because 1518 bytes is byte numbers0to1517, and the payload is, at most, 1500 bytes (bytes14to `1513, not 1502 bytes the way you have it. The NIC tells the ethernet driver how many bytes were received, so I'm not sure you can get that value the way you would like. - Ron Maupin