-
Notifications
You must be signed in to change notification settings - Fork 0
Socket Buffer
Buffer for network packet data with read/write pointers, enabling efficient storage across network layers.
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).
| 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 |
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
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
Buffers can be chained via the next field for handling scattered data:
-
append(SocketBuffer)- chain another buffer -
getNext()/setNext()- manage chain -
resetNext()- clear chain
When a packet arrives:
- Link layer driver creates SocketBuffer with packet data
- Link layer parses Ethernet header → stored in
linkLayerHeader - Network layer parses IP header → stored in
networkLayerHeader - Transport layer parses TCP/UDP header → stored in
transportLayerHeader - Application receives buffer with all headers accessible
Each layer uses pushHeader() to prepend its header before sending, and pullHeader() to remove after processing.
- 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 viatoByteArray(). - 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.
- Network-Stack - Network stack overview
- Network-Layers - Protocol layer architecture
- TCP-Protocol - TCP buffer usage
- UDP-Protocol - UDP buffer usage
- Ethernet-Driver - Ethernet driver buffer handling