rawsocket

package
v0.0.0-...-4ef28c0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 16, 2024 License: GPL-3.0 Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IPChecksum

func IPChecksum(b []byte) uint16

IPChecksum computes the checksum for IPv4 Wiki: https://en.wikipedia.org/wiki/IPv4_header_checksum Explanation: https://gist.github.com/david-hoze/0c7021434796997a4ca42d7731a7073a

func InitRecvSocket

func InitRecvSocket() (int, error)

InitRecvSocket creates a raw socket for receiving packets.

func InitSendSocket

func InitSendSocket() (int, error)

InitSendSocket creates a raw socket for sending packets.

func SetTimeout

func SetTimeout(fd int, duration time.Duration) error

SetTimeout sets a receive timeout on the socket file descriptor.

func Unwrap

func Unwrap(packet []byte) (*IPHeader, *TCPHeader, error)

Unwrap a packet and return (1) IP Header (2) TCP Header (3) error. The Payload is stored inside the TCP Header, if any.

func Wrap

func Wrap(ip *IPHeader, tcp *TCPHeader) []byte

Wrap (1) IP Header (2) TCP Header into a packet. The Payload should be stored inside the TCP Header, if any.

Types

type IPFlags

type IPFlags uint16
const (
	RF IPFlags = 1 << 15
	DF IPFlags = 1 << 14
	MF IPFlags = 1 << 13
)

Bit positions [ RF, DF, MF, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ]

type IPHeader

type IPHeader struct {
	Version    uint8 // Always 4
	Ihl        uint8 // Always 5 since we have no options
	Tos        uint8 // Always 0 when we send out, can be 8 when receiving from server
	TotLen     uint16
	Id         uint16
	Flags      IPFlags // 3 bits, part of uint16
	FragOffset uint16  // 13 bits, part of uint16
	Ttl        uint8   // Always 64 when we send out
	Protocol   uint8   // Always 6 for TCP
	Checksum   uint16
	SrcIp      [4]byte
	DstIp      [4]byte
}

IPHeader struct size is 20 bytes for IPv4 https://en.wikipedia.org/wiki/IPv4#Header

func NewIPHeader

func NewIPHeader(packet []byte) *IPHeader

NewIPHeader converts a byte array into an IPv4 header

func (*IPHeader) ToBytes

func (ip *IPHeader) ToBytes() []byte

ToBytes converts an IPv4 header into a byte array

type TCPFlags

type TCPFlags uint8
const (
	CWR TCPFlags = 1 << 7
	ECE TCPFlags = 1 << 6
	URG TCPFlags = 1 << 5
	ACK TCPFlags = 1 << 4
	PSH TCPFlags = 1 << 3
	RST TCPFlags = 1 << 2
	SYN TCPFlags = 1 << 1
	FIN TCPFlags = 1 << 0
)

Bit positions [ CWR, ECE, URG, ACK, PSH, RST, SYN, FIN ]

func (TCPFlags) String

func (f TCPFlags) String() string

For debugging purposes

type TCPHeader

type TCPHeader struct {
	SrcPort    uint16
	DstPort    uint16
	SeqNum     uint32
	AckNum     uint32
	DataOffset uint8 // 4 bits, part of uint8
	Reserved   uint8 // 4 bits of 0's, part of uint8
	Flags      TCPFlags
	Window     uint16
	Checksum   uint16
	Urgent     uint16
	Options    []byte
	Payload    []byte
}

TCPHeader size is between 20 and 60 bytes https://en.wikipedia.org/wiki/Transmission_Control_Protocol

func NewTCPHeader

func NewTCPHeader(data []byte) *TCPHeader

NewTCPHeader converts a byte array into a TCP header

func (*TCPHeader) ToBytes

func (tcp *TCPHeader) ToBytes(ip *IPHeader) []byte

ToBytes converts a TCP header into a byte array

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL