Skip to content

Commit

Permalink
add tcp sockets (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
soypat authored Sep 16, 2023
1 parent 407ce19 commit 639421a
Show file tree
Hide file tree
Showing 9 changed files with 627 additions and 373 deletions.
4 changes: 1 addition & 3 deletions cmd/cyrw/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,7 @@ func main() {
MAC: nil,
MaxUDPConns: 2,
})
// stack.GlobalHandler = func(b []byte) {
// println("NEW payload:\n", hex.Dump(b))
// }

dev.RecvEthHandle(stack.RecvEth)
for {
println("Trying DoDHCP")
Expand Down
2 changes: 1 addition & 1 deletion internal/tcpctl/dhcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func (d *DHCPClient) HandleUDP(resp []byte, packet *UDPPacket) (_ int, err error

// print("DHCP Option received ", option.String())
optionData := incpayload[ptr+2 : ptr+2+int(optlen)]
if d.State == dhcpStateWaitAck && option == eth.DHCPMessageType && len(optionData) > 0 && optionData[0] == 5 {
if d.State == dhcpStateWaitAck && option == eth.DHCP_MessageType && len(optionData) > 0 && optionData[0] == 5 {
d.State = dhcpStateDone
return 0, nil
}
Expand Down
126 changes: 63 additions & 63 deletions internal/tcpctl/eth/definitions.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,68 +54,68 @@ type DHCPOption uint8

// DHCP options. Taken from https://help.sonicwall.com/help/sw/eng/6800/26/2/3/content/Network_DHCP_Server.042.12.htm.
//
//go:generate stringer -type=DHCPOption -trimprefix=DHCP
//go:generate stringer -type=DHCPOption -trimprefix=DHCP_
const (
DHCPWordAligned DHCPOption = 0
DHCPSubnetMask DHCPOption = 1
DHCPTimeOffset DHCPOption = 2
DHCPRouter DHCPOption = 3
DHCPTimeServers DHCPOption = 4
DHCPNameServers DHCPOption = 5
DHCPDNSServers DHCPOption = 6
DHCPLogServers DHCPOption = 7
DHCPCookieServers DHCPOption = 8
DHCPLPRServers DHCPOption = 9
DHCPImpressServers DHCPOption = 10
DHCPRLPServers DHCPOption = 11
DHCPHostName DHCPOption = 12
DHCPBootFileSize DHCPOption = 13
DHCPMeritDumpFile DHCPOption = 14
DHCPDomainName DHCPOption = 15
DHCPSwapServer DHCPOption = 16
DHCPRootPath DHCPOption = 17
DHCPExtensionFile DHCPOption = 18
DHCPIPLayerForwarding DHCPOption = 19
DHCPSrcrouteenabler DHCPOption = 20
DHCPPolicyFilter DHCPOption = 21
DHCPMaximumDGReassemblySize DHCPOption = 22
DHCPDefaultIPTTL DHCPOption = 23
DHCPPathMTUAgingTimeout DHCPOption = 24
DHCPMTUPlateau DHCPOption = 25
DHCPInterfaceMTUSize DHCPOption = 26
DHCPAllSubnetsAreLocal DHCPOption = 27
DHCPBroadcastAddress DHCPOption = 28
DHCPPerformMaskDiscovery DHCPOption = 29
DHCPProvideMasktoOthers DHCPOption = 30
DHCPPerformRouterDiscovery DHCPOption = 31
DHCPRouterSolicitationAddress DHCPOption = 32
DHCPStaticRoutingTable DHCPOption = 33
DHCPTrailerEncapsulation DHCPOption = 34
DHCPARPCacheTimeout DHCPOption = 35
DHCPEthernetEncapsulation DHCPOption = 36
DHCPDefaultTCPTimetoLive DHCPOption = 37
DHCPTCPKeepaliveInterval DHCPOption = 38
DHCPTCPKeepaliveGarbage DHCPOption = 39
DHCPNISDomainName DHCPOption = 40
DHCPNISServerAddresses DHCPOption = 41
DHCPNTPServersAddresses DHCPOption = 42
DHCPVendorSpecificInformation DHCPOption = 43
DHCPNetBIOSNameServer DHCPOption = 44
DHCPNetBIOSDatagramDistribution DHCPOption = 45
DHCPNetBIOSNodeType DHCPOption = 46
DHCPNetBIOSScope DHCPOption = 47
DHCPXWindowFontServer DHCPOption = 48
DHCPXWindowDisplayManager DHCPOption = 49
DHCPRequestedIPaddress DHCPOption = 50
DHCPIPAddressLeaseTime DHCPOption = 51
DHCPOptionOverload DHCPOption = 52
DHCPMessageType DHCPOption = 53
DHCPServerIdentification DHCPOption = 54
DHCPParameterRequestList DHCPOption = 55
DHCPMessage DHCPOption = 56
DHCPMaximumMessageSize DHCPOption = 57
DHCPRenewTimeValue DHCPOption = 58
DHCPRebindingTimeValue DHCPOption = 59
DHCPClientIdentifier DHCPOption = 60
DHCPClientIdentifier1 DHCPOption = 61
DHCP_WordAligned DHCPOption = 0
DHCP_SubnetMask DHCPOption = 1
DHCP_TimeOffset DHCPOption = 2 // Time offset in seconds from UTC
DHCP_Router DHCPOption = 3 // N/4 router addresses
DHCP_TimeServers DHCPOption = 4 // N/4 time server addresses
DHCP_NameServers DHCPOption = 5 // N/4 IEN-116 server addresses
DHCP_DNSServers DHCPOption = 6 // N/4 DNS server addresses
DHCP_LogServers DHCPOption = 7 // N/4 logging server addresses
DHCP_CookieServers DHCPOption = 8 // N/4 quote server addresses
DHCP_LPRServers DHCPOption = 9 // N/4 printer server addresses
DHCP_ImpressServers DHCPOption = 10 // N/4 impress server addresses
DHCP_RLPServers DHCPOption = 11 // N/4 RLP server addresses
DHCP_HostName DHCPOption = 12 // Hostname string
DHCP_BootFileSize DHCPOption = 13 // Size of boot file in 512 byte chunks
DHCP_MeritDumpFile DHCPOption = 14 // Client to dump and name of file to dump to
DHCP_DomainName DHCPOption = 15 // The DNS domain name of the client
DHCP_SwapServer DHCPOption = 16 // Swap server addresses
DHCP_RootPath DHCPOption = 17 // Path name for root disk
DHCP_ExtensionFile DHCPOption = 18 // Patch name for more BOOTP info
DHCP_IPLayerForwarding DHCPOption = 19 // Enable or disable IP forwarding
DHCP_Srcrouteenabler DHCPOption = 20 // Enable or disable source routing
DHCP_PolicyFilter DHCPOption = 21 // Routing policy filters
DHCP_MaximumDGReassemblySize DHCPOption = 22 // Maximum datagram reassembly size
DHCP_DefaultIPTTL DHCPOption = 23 // Default IP time-to-live
DHCP_PathMTUAgingTimeout DHCPOption = 24 // Path MTU aging timeout
DHCP_MTUPlateau DHCPOption = 25 // Path MTU plateau table
DHCP_InterfaceMTUSize DHCPOption = 26 // Interface MTU size
DHCP_AllSubnetsAreLocal DHCPOption = 27 // All subnets are local
DHCP_BroadcastAddress DHCPOption = 28 // Broadcast address
DHCP_PerformMaskDiscovery DHCPOption = 29 // Perform mask discovery
DHCP_ProvideMasktoOthers DHCPOption = 30 // Provide mask to others
DHCP_PerformRouterDiscovery DHCPOption = 31 // Perform router discovery
DHCP_RouterSolicitationAddress DHCPOption = 32 // Router solicitation address
DHCP_StaticRoutingTable DHCPOption = 33 // Static routing table
DHCP_TrailerEncapsulation DHCPOption = 34 // Trailer encapsulation
DHCP_ARPCacheTimeout DHCPOption = 35 // ARP cache timeout
DHCP_EthernetEncapsulation DHCPOption = 36 // Ethernet encapsulation
DHCP_DefaultTCPTimetoLive DHCPOption = 37 // Default TCP time to live
DHCP_TCPKeepaliveInterval DHCPOption = 38 // TCP keepalive interval
DHCP_TCPKeepaliveGarbage DHCPOption = 39 // TCP keepalive garbage
DHCP_NISDomainName DHCPOption = 40 // NIS domain name
DHCP_NISServerAddresses DHCPOption = 41 // NIS server addresses
DHCP_NTPServersAddresses DHCPOption = 42 // NTP servers addresses
DHCP_VendorSpecificInformation DHCPOption = 43 // Vendor specific information
DHCP_NetBIOSNameServer DHCPOption = 44 // NetBIOS name server
DHCP_NetBIOSDatagramDistribution DHCPOption = 45 // NetBIOS datagram distribution
DHCP_NetBIOSNodeType DHCPOption = 46 // NetBIOS node type
DHCP_NetBIOSScope DHCPOption = 47 // NetBIOS scope
DHCP_XWindowFontServer DHCPOption = 48 // X window font server
DHCP_XWindowDisplayManager DHCPOption = 49 // X window display manager
DHCP_RequestedIPaddress DHCPOption = 50 // Requested IP address
DHCP_IPAddressLeaseTime DHCPOption = 51 // IP address lease time
DHCP_OptionOverload DHCPOption = 52 // Overload “sname” or “file”
DHCP_MessageType DHCPOption = 53 // DHCP message type
DHCP_ServerIdentification DHCPOption = 54 // DHCP server identification
DHCP_ParameterRequestList DHCPOption = 55 // Parameter request list
DHCP_Message DHCPOption = 56 // DHCP error message
DHCP_MaximumMessageSize DHCPOption = 57 // DHCP maximum message size
DHCP_RenewTimeValue DHCPOption = 58 // DHCP renewal (T1) time
DHCP_RebindingTimeValue DHCPOption = 59 // DHCP rebinding (T2) time
DHCP_ClientIdentifier DHCPOption = 60 // Client identifier
DHCP_ClientIdentifier1 DHCPOption = 61 // Client identifier
)
126 changes: 63 additions & 63 deletions internal/tcpctl/eth/dhcpoption_string.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 9 additions & 13 deletions internal/tcpctl/eth/headers.go
Original file line number Diff line number Diff line change
Expand Up @@ -470,12 +470,19 @@ func (tcphdr *TCPHeader) Put(buf []byte) {
binary.BigEndian.PutUint16(buf[18:], tcphdr.UrgentPtr)
}

// Offset specifies the size of the TCP header in 32-bit words. The minimum size
// header is 5 words and the maximum is 15 words thus giving the minimum size of
// 20 bytes and maximum of 60 bytes, allowing for up to 40 bytes of options in
// the header. This field gets its name from the fact that it is also the offset
// from the start of the TCP segment to the actual data.
func (tcphdr *TCPHeader) Offset() (tcpWords uint8) {
return uint8(tcphdr.OffsetAndFlags[0] >> (8 + 4))
}

func (tcphdr *TCPHeader) OffsetInBytes() (offsetInBytes uint16) {
return uint16(tcphdr.Offset()) * tcpWordlen
// OffsetInBytes returns the size of the TCP header in bytes, including options.
// See [TCPHeader.Offset] for more information.
func (tcphdr *TCPHeader) OffsetInBytes() uint8 {
return tcphdr.Offset() * tcpWordlen
}

func (tcphdr *TCPHeader) Flags() TCPFlags {
Expand All @@ -495,17 +502,6 @@ func (tcphdr *TCPHeader) SetOffset(tcpWords uint8) {
tcphdr.OffsetAndFlags[0] |= onlyFlags | (uint16(tcpWords) << 12)
}

// FrameLength returns the size of the TCP frame as described by tcphdr and
// payloadLength, which is the size of the TCP payload not including the TCP options.
func (tcphdr *TCPHeader) FrameLength(payloadLength uint16) uint16 {
return tcphdr.OffsetInBytes() + payloadLength
}

// OptionsLength returns the length of the options section
func (tcphdr *TCPHeader) OptionsLength() uint16 {
return tcphdr.OffsetInBytes()*tcpWordlen - 20
}

// CalculateChecksumIPv4 calculates the checksum of the TCP header, options and payload.
func (tcphdr *TCPHeader) CalculateChecksumIPv4(pseudoHeader *IPv4Header, tcpOptions, payload []byte) uint16 {
const sizePseudo = 12
Expand Down
Loading

0 comments on commit 639421a

Please sign in to comment.