Skip to content

Socket Buffer

Levente Santha edited this page May 11, 2026 · 1 revision

Socket Buffer

Buffer for network packet data with read/write pointers, enabling efficient storage across network layers.

Overview

SocketBuffer is the fundamental data structure for network packet handling in JNode. It provides a flexible buffer abstraction that supports efficient prepend/append operations as packets travel through the network stack. Each network layer (Link, Network, Transport) adds its header to the front of the buffer and removes it from the front when processing, making SocketBuffer essential for protocol handling.

The buffer supports a linked-list model via the next field, allowing chaining of multiple buffers for scatter-gather I/O. All multi-byte values are automatically converted to/from network byte order (big-endian).

Key Components

Class / File Role
net/src/net/org/jnode/net/SocketBuffer.java Main buffer implementation
LinkLayerHeader Link layer (e.g., Ethernet) header data
NetworkLayerHeader Network layer (e.g., IPv4) header data
TransportLayerHeader Transport layer (e.g., TCP/UDP) header data

How It Works

Buffer Structure

SocketBuffer fields:
  data[]       - underlying byte array
  start        - offset to first valid data
  size         - number of valid bytes
  next         - linked buffer for chaining
  device       - source/destination network device
  protocolID   - packet type identifier
  linkLayerHeader   - parsed link header
  networkLayerHeader - parsed network header
  transportLayerHeader - parsed transport header

Buffer Operations

Prepend (for adding headers):

  • insert(count) - inserts space at front, shifts existing data
  • Optimized when start >= count (no data movement needed)

Append (for adding trailers):

  • setSize(size + count) - expands buffer capacity
  • setLong, setInt, setShort - write with network byte order

Read access:

  • getOffset() - current read position
  • getLong, getInt, getShort - read with network byte order conversion
  • getArray() - get underlying data

Buffer Chaining

Buffers can be chained via the next field for handling scattered data:

  • append(SocketBuffer) - chain another buffer
  • getNext() / setNext() - manage chain
  • resetNext() - clear chain

Network Layer Integration

When a packet arrives:

  1. Link layer driver creates SocketBuffer with packet data
  2. Link layer parses Ethernet header → stored in linkLayerHeader
  3. Network layer parses IP header → stored in networkLayerHeader
  4. Transport layer parses TCP/UDP header → stored in transportLayerHeader
  5. Application receives buffer with all headers accessible

Each layer uses pushHeader() to prepend its header before sending, and pullHeader() to remove after processing.

Gotchas & Non-Obvious Behavior

  • No automatic pooling: SocketBuffers are allocated per-packet; high-throughput scenarios may need explicit pooling.
  • Data copy on clone: SocketBuffer(SocketBuffer src) copies the data array via toByteArray().
  • Array dimension preserved: When wrapping a byte array, the full array is copied, not just the valid portion.
  • Network byte order: All multi-byte read/write methods use big-endian (network order).
  • Header access after removal: Once pullHeader() is called, the header is discarded from the buffer.

Related Pages

Clone this wiki locally