Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added helper functions for creating various Tcp packets #32

Merged
merged 1 commit into from
Oct 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ interface IpHeader {
}
return when (sourceAddress) {
is Inet4Address -> {
destinationAddress as Inet4Address
val totalLength = (IP4_MIN_HEADER_LENGTH + payloadSize.toUShort()).toUShort()
Ipv4Header(
sourceAddress = sourceAddress,
Expand All @@ -76,6 +77,7 @@ interface IpHeader {
)
}
is Inet6Address -> {
destinationAddress as Inet6Address
Ipv6Header(
sourceAddress = sourceAddress,
destinationAddress = destinationAddress,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import com.jasonernst.knet.network.ip.v4.options.Ipv4Option
import com.jasonernst.knet.network.ip.v4.options.Ipv4Option.Companion.parseOptions
import org.slf4j.LoggerFactory
import java.net.Inet4Address
import java.net.InetAddress
import java.nio.ByteBuffer
import java.nio.ByteOrder
import java.util.concurrent.atomic.AtomicInteger
import kotlin.experimental.or
import kotlin.math.ceil
import kotlin.math.min
Expand Down Expand Up @@ -51,9 +51,9 @@ data class Ipv4Header(
// https://en.wikipedia.org/wiki/IPv4#Header_checksum
var headerChecksum: UShort = 0u,
// 32-bits, source address
override val sourceAddress: InetAddress = Inet4Address.getLocalHost(),
override val sourceAddress: Inet4Address = Inet4Address.getLocalHost() as Inet4Address,
// 32-bits, destination address
override val destinationAddress: InetAddress = Inet4Address.getLocalHost(),
override val destinationAddress: Inet4Address = Inet4Address.getLocalHost() as Inet4Address,
val options: List<Ipv4Option> = emptyList(),
) : IpHeader {
// 3-bits: set from mayFragment and lastFragment
Expand Down Expand Up @@ -81,7 +81,6 @@ data class Ipv4Header(

// calculate the checksum for packet creation based on the set fields
if (headerChecksum == 0u.toUShort()) {
logger.debug("Calculating checksum for IPv4 header")
val buffer = toByteArray()
// ^ this will compute the checksum and put it in the buffer
// note: it's tempting to call the checksum function here but if we do we'll get a zero
Expand All @@ -92,6 +91,7 @@ data class Ipv4Header(

companion object {
private val logger = LoggerFactory.getLogger(Ipv4Header::class.java)
val packetCounter: AtomicInteger = AtomicInteger(0) // used to generate monotonic ids for Ipv4 packets
private const val CHECKSUM_OFFSET = 10
const val IP4_WORD_LENGTH: UByte = 4u
val IP4_MIN_HEADER_LENGTH: UByte = (IP4_WORD_LENGTH * 5u).toUByte()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import com.jasonernst.knet.network.ip.v6.extenions.Ipv6Fragment
import com.jasonernst.knet.network.nextheader.NextHeader
import org.slf4j.LoggerFactory
import java.net.Inet6Address
import java.net.InetAddress
import java.nio.ByteBuffer
import java.nio.ByteOrder

Expand All @@ -31,9 +30,9 @@ data class Ipv6Header(
// 8-bits: hop limit, decremented by 1 at each hop, if 0, packet is discarded, similar to TTL
val hopLimit: UByte = 0u,
// 128-bits: source address
override val sourceAddress: InetAddress = Inet6Address.getByName("::1"),
override val sourceAddress: Inet6Address = Inet6Address.getByName("::1") as Inet6Address,
// 128-bits: destination address
override val destinationAddress: InetAddress = Inet6Address.getByName("::1"),
override val destinationAddress: Inet6Address = Inet6Address.getByName("::1") as Inet6Address,
val extensionHeaders: List<Ipv6ExtensionHeader> = emptyList(),
) : IpHeader {
init {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import com.jasonernst.knet.network.ip.v6.Ipv6Header.Companion.IP6_HEADER_SIZE
import com.jasonernst.knet.network.nextheader.NextHeader
import com.jasonernst.knet.transport.tcp.TcpHeader
import com.jasonernst.knet.transport.udp.UdpHeader
import com.jasonernst.packetdumper.stringdumper.StringPacketDumper
import org.slf4j.LoggerFactory
import java.nio.ByteBuffer

Expand Down Expand Up @@ -79,7 +78,6 @@ interface TransportHeader : NextHeader {
}
}
pseudoHeader.rewind()
logger.debug("Pseudo header: {}", StringPacketDumper().dumpBufferToString(pseudoHeader))
val computedChecksum = Checksum.calculateChecksum(pseudoHeader)

if (verify) {
Expand Down
Loading