Skip to content

Commit

Permalink
socktap: refactor packet assembly for TCP link
Browse files Browse the repository at this point in the history
  • Loading branch information
riebl committed Feb 8, 2025
1 parent e7b7a1c commit 0b241f3
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 23 deletions.
40 changes: 18 additions & 22 deletions tools/socktap/tcp_link.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,33 +27,30 @@ void TcpLink::indicate(IndicationCallback cb)
void TcpLink::request(const access::DataRequest& request, std::unique_ptr<ChunkPacket> packet)
{
// create ethernet header
vanetza::ByteBuffer buffer = create_ethernet_header(request.destination_addr, request.source_addr, request.ether_type);
vanetza::ByteBuffer eth = create_ethernet_header(request.destination_addr, request.source_addr, request.ether_type);
packet->layer(OsiLayer::Link) = std::move(eth);

// insert packet size before ethernet header
uint16_t packet_size = packet->size(OsiLayer::Network, OsiLayer::Application) + EthernetHeader::length_bytes;
buffer.insert(buffer.begin(), packet_size & 0x00FF);
buffer.insert(buffer.begin(), (packet_size & 0xFF00) >> 8);

packet->layer(OsiLayer::Link) = std::move(buffer);
// insert packet size as frame delimiter
uint16_t packet_size = packet->size(OsiLayer::Link, OsiLayer::Application);
vanetza::ByteBuffer frame_delimiter { uint8_t(packet_size >> 8), uint8_t(packet_size) };
packet->layer(OsiLayer::Physical) = std::move(frame_delimiter);

std::array<boost::asio::const_buffer, layers_> const_buffers;
for (auto& layer : osi_layer_range<OsiLayer::Link, OsiLayer::Application>()) {
const auto index = distance(OsiLayer::Link, layer);
for (auto& layer : osi_layer_range<OsiLayer::Physical, OsiLayer::Application>()) {
const auto index = distance(OsiLayer::Physical, layer);
packet->layer(layer).convert(tx_buffers_[index]);
const_buffers[index] = boost::asio::buffer(tx_buffers_[index]);
}

std::list<TcpSocket>::iterator i = sockets_.begin();

while (i != sockets_.end()) {
if ((*i).status() == TcpSocket::CONNECTED) {
(*i).request(const_buffers);
i++;
} else if ((*i).status() == TcpSocket::ERROR) {
sockets_.erase(i++);
for (auto it = sockets_.begin(); it != sockets_.end();) {
if (it->status() == TcpSocket::CONNECTED) {
it->request(const_buffers);
++it;
} else if (it->status() == TcpSocket::ERROR) {
it = sockets_.erase(it);
std::cerr << "Socket removed" << std::endl;
} else {
i++;
++it;
}
}
}
Expand Down Expand Up @@ -149,16 +146,15 @@ void TcpSocket::do_receive()
void TcpSocket::receive_handler(boost::system::error_code ec, std::size_t length) {
if (!ec) {
status_ = CONNECTED;

rx_store_.insert(rx_store_.end(), rx_buffer_.begin(), rx_buffer_.begin() + length);

// While we have enough bytes stored to construct a packet, pass it up
while (rx_store_.size() >= get_next_packet_size() + 2)
{
uint16_t packet_length = get_next_packet_size();
ByteBuffer packet_buffer(rx_store_.begin() + 2, rx_store_.begin() + 2 + packet_length);
uint16_t packet_length = get_next_packet_size() + 2;
ByteBuffer packet_buffer(rx_store_.begin() + 2, rx_store_.begin() + packet_length);
pass_up(std::move(packet_buffer));
rx_store_.erase(rx_store_.begin(), rx_store_.begin() + 2 + packet_length);
rx_store_.erase(rx_store_.begin(), rx_store_.begin() + packet_length);
}

do_receive();
Expand Down
2 changes: 1 addition & 1 deletion tools/socktap/tcp_link.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#include <array>
#include <list>

static constexpr std::size_t layers_ = num_osi_layers(vanetza::OsiLayer::Link, vanetza::OsiLayer::Application);
static constexpr std::size_t layers_ = num_osi_layers(vanetza::OsiLayer::Physical, vanetza::OsiLayer::Application);


class TcpSocket
Expand Down

0 comments on commit 0b241f3

Please sign in to comment.