-
Notifications
You must be signed in to change notification settings - Fork 24
Tip: Sending and Receiving a Packet
In KENS, the TCP layer calls sendPacket to send a packet down to the IP layer. It receives a packet object to send and transfers it to the next module. In KENS, "IPv4"
is the only next module, so you can safely set the toModule parameter to "IPv4"
.
We strongly recommend you to use R-value for sending packets. This prevents reuse of sent packets and their UUIDs.
sendPacket("IPv4", packet); // OK, but not recommended
sendPacket("IPv4", std::move(packet)); // Recommended
You can create a new packet from scratch or by cloning an existing packet object. Cloning creates a new packet that has the same data but for the UUID. Note that copying (using = operator) just creates a new packet object that has the same data and UUID.
// Creating a new packet from scratch
size_t packet_size = 100;
Packet pkt (packet_size);
pkt.writeData(0, data, 20);
...
// Cloning an existing packet
Packet pkt2 = pkt.clone(); // cloning pkt. pkt2 has different UUID
Packet pkt3 = pkt; // copying pkt. pkt3 has same UUID
When a packet arrives at the IP layer, the IP layer calls packetArrived methods in TCPAssignment.cpp
.
void TCPAssignment::packetArrived(std::string fromModule, Packet &&packet) {
// Remove below
(void)fromModule;
(void)packet;
}
You can safely assume that the KENS IP layer computes the IP checksum and only transfers correctly formed packets.
You can extract data from the received packet via class Packet
interfaces. For example, you can read its raw data from 0 to 16 by calling the readData
method.
Depending on the received packet, you may have to return a blocked system call.