Skip to content
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
13 changes: 13 additions & 0 deletions foundation-support/gradle/owned-output-patterns.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ network/crypta/node/FSParseException*
network/crypta/node/FastRunnable*
network/crypta/node/SemiOrderedShutdownHook*
network/crypta/io/WritableToDataOutputStream*
network/crypta/io/AddressIdentifier*
network/crypta/support/api/Bucket*
network/crypta/support/api/BucketFactory*
network/crypta/support/api/LockableRandomAccessBuffer*
Expand All @@ -13,21 +14,29 @@ network/crypta/support/api/RandomAccessBucket*
network/crypta/support/api/RandomAccessBuffer*
network/crypta/support/api/ResumeContext*
network/crypta/support/Base64*
network/crypta/support/BitArray*
network/crypta/support/Buffer*
network/crypta/support/DoublyLinkedList*
network/crypta/support/DoublyLinkedListImpl*
network/crypta/support/ByteArrayWrapper*
network/crypta/support/ByteBufferInputStream*
network/crypta/support/Fields*
network/crypta/support/HTMLEncoder*
network/crypta/support/HTMLEntities*
network/crypta/support/HTMLNode*
network/crypta/support/HexUtil*
network/crypta/support/IllegalBase64Exception*
network/crypta/support/LRUCache*
network/crypta/support/LRUMap*
network/crypta/support/LightweightException*
network/crypta/support/PromiscuousItemException*
network/crypta/support/PriorityAwareExecutor*
network/crypta/support/Loader*
network/crypta/support/SerializationLimits*
network/crypta/support/SentTimeCache*
network/crypta/support/SimpleReadOnlyArrayBucket*
network/crypta/support/ShortBuffer*
network/crypta/support/SparseBitmap*
network/crypta/support/StringValidityChecker*
network/crypta/support/SimpleFieldSet*
network/crypta/support/Ticker*
Expand Down Expand Up @@ -59,6 +68,7 @@ network/crypta/support/io/FilenameSanitizer*
network/crypta/support/io/InsufficientDiskSpaceException*
network/crypta/support/io/LineReader*
network/crypta/support/io/LineReadingInputStream*
network/crypta/support/io/InetAddressIpv6FirstComparator*
network/crypta/support/io/NonClosingInputStream*
network/crypta/support/io/NonClosingOutputStream*
network/crypta/support/io/NotPersistentBucket*
Expand All @@ -79,6 +89,9 @@ network/crypta/support/io/SkipShieldingInputStream*
network/crypta/support/io/StorageFormatException*
network/crypta/support/io/SwitchableProxyRandomAccessBuffer*
network/crypta/support/io/TooLongException*
network/crypta/support/transport/ip/HostnameSyntaxException*
network/crypta/support/transport/ip/HostnameUtil*
network/crypta/support/transport/ip/IPUtil*
network/crypta/support/compress/AbstractCompressor*
network/crypta/support/compress/Bzip2Compressor*
network/crypta/support/compress/CompressionInputSizeException*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* provides convenience methods for reading a buffer from a {@link DataInput} and writing it to a
* {@link DataOutputStream}. When constructed from a {@code DataInput}, the input is expected to be
* framed as a 4-byte length (signed, big-endian, non-negative, and not exceeding {@link
* Serializer#MAX_ARRAY_LENGTH}) followed by exactly that many bytes of payload.
* SerializationLimits#MAX_ARRAY_LENGTH}) followed by exactly that many bytes of payload.
*
* <p>When the buffer spans the entire backing array (i.e., {@code start == 0} and {@code length ==
* data.length}), {@link #getData()} returns the backing array directly. In all other cases it
Expand Down Expand Up @@ -43,16 +43,17 @@ public final class Buffer implements WritableToDataOutputStream {
*
* @param dis source to read the framed data from.
* @throws IllegalArgumentException if the length is negative or greater than {@link
* Serializer#MAX_ARRAY_LENGTH}. The historical behavior is to signal invalid length with this
* exception type rather than {@link IOException}.
* SerializationLimits#MAX_ARRAY_LENGTH}. The historical behavior is to signal invalid length
* with this exception type rather than {@link IOException}.
* @throws IOException if {@code dis} cannot provide the requested bytes.
*/
public Buffer(DataInput dis) throws IOException {
length = dis.readInt();
if (length < 0) throw new IllegalArgumentException("Negative Length: " + length);
if (length > Serializer.MAX_ARRAY_LENGTH) {
if (length > SerializationLimits.MAX_ARRAY_LENGTH) {
// Preserve historical behavior: signal invalid length with IllegalArgumentException.
throw new IllegalArgumentException("Length larger than " + Serializer.MAX_ARRAY_LENGTH);
throw new IllegalArgumentException(
"Length larger than " + SerializationLimits.MAX_ARRAY_LENGTH);
}

data = new byte[length]; // Allocate exactly the announced length.
Expand Down Expand Up @@ -140,7 +141,7 @@ public byte byteAt(int pos) {
* Write this buffer to a {@link DataOutputStream} using the standard framing format.
*
* <p>The method writes a 4-byte signed length followed by the visible bytes. The length is equal
* to {@link #getLength()} and must not exceed {@link Serializer#MAX_ARRAY_LENGTH}.
* to {@link #getLength()} and must not exceed {@link SerializationLimits#MAX_ARRAY_LENGTH}.
*
* @param stream destination stream.
* @throws IOException if an I/O error occurs while writing to {@code stream}.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package network.crypta.support;

/**
* Centralized size limits for generic support-side serialization helpers.
*
* <p>This class gives `:foundation-support` a small, stable home for limits that belong to
* low-level value types and deserialization helpers rather than to a specific runtime subsystem or
* packet-format implementation. The goal is to let classes such as {@code Buffer} and {@code
* BitArray} enforce the same bounds without importing root-owned networking code.
*
* <p>Keep this class intentionally narrow. It is for reusable serialization bounds that apply to
* support-owned helpers across the tree, not for broader protocol constants, transport tuning, or
* runtime configuration. If a limit is specific to one wire format or one daemon subsystem, it
* should stay with that owner instead of being added here.
*/
public final class SerializationLimits {
/**
* Maximum permitted length, in bytes, for generic array-backed payloads accepted by support-side
* deserialization helpers.
*
* <p>Use this bound when reading variable-length byte content whose validation belongs to generic
* support code rather than to a higher-level wire-format implementation.
*/
public static final int MAX_ARRAY_LENGTH = 4092;

/**
* Maximum permitted logical size, in bits, for a {@code BitArray} accepted during
* deserialization.
*
* <p>This limit prevents oversized bit-array allocations in generic support code while keeping
* the shared bound explicit and easy to reuse from compatibility aliases.
*/
public static final int MAX_BITARRAY_SIZE = 2048 * 8;

private SerializationLimits() {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
import network.crypta.support.Fields;
import network.crypta.support.LRUCache;

import static network.crypta.node.NodeStats.DEFAULT_MAX_PING_TIME;

/**
* Comparator for {@link InetAddress} values that prefers IPv6 over IPv4 and applies
* scope/reachability heuristics to produce a stable total ordering.
Expand All @@ -30,9 +28,8 @@
* total order.
* </ol>
*
* <p>Reachability checks use {@link InetAddress#isReachable(int)} with the default node ping time
* (see {@link network.crypta.node.NodeStats#DEFAULT_MAX_PING_TIME}) and are cached to avoid
* repeated probes during sorting.
* <p>Reachability checks use {@link InetAddress#isReachable(int)} with a bounded default timeout
* and are cached to avoid repeated probes during sorting.
*
* <p>Side effects and performance:
*
Expand All @@ -49,6 +46,7 @@
* @author toad
*/
public class InetAddressIpv6FirstComparator implements Comparator<InetAddress> {
private static final long DEFAULT_REACHABILITY_TIMEOUT_MILLIS = 1500L;

// Cache reachability to avoid repeated isReachable() calls during O(N log N) sorts.
// Size ~1000 entries; TTL ~300_000 ms (5 minutes). Keys use InetAddress.hashCode().
Expand Down Expand Up @@ -83,8 +81,8 @@ private boolean isReachableSiteLocalAddress(InetAddress inetAddress) {
* </ul>
*
* <p>May perform a reachability probe for site-local addresses and therefore may block up to
* {@link network.crypta.node.NodeStats#DEFAULT_MAX_PING_TIME} milliseconds on a first encounter;
* results are cached to mitigate repeated calls.
* {@value #DEFAULT_REACHABILITY_TIMEOUT_MILLIS} milliseconds on a first encounter; results are
* cached to mitigate repeated calls.
*
* @param arg0 the first address; may be {@code null}
* @param arg1 the second address; may be {@code null}
Expand All @@ -104,7 +102,7 @@ private boolean isReachable(InetAddress inetAddress) {
Boolean reachable = reachabilityCache.get(hashCode);
if (reachable == null) {
try {
reachable = inetAddress.isReachable((int) DEFAULT_MAX_PING_TIME);
reachable = inetAddress.isReachable((int) DEFAULT_REACHABILITY_TIMEOUT_MILLIS);
} catch (IOException _) {
reachable = false;
}
Expand Down
13 changes: 3 additions & 10 deletions src/main/java/network/crypta/support/Serializer.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import network.crypta.keys.Key;
import network.crypta.keys.NodeCHK;
import network.crypta.keys.NodeSSK;
import network.crypta.node.NewPacketFormat;

/**
* Utility for serializing and deserializing a constrained set of value types to and from {@link
Expand Down Expand Up @@ -42,16 +41,10 @@ private Serializer() {}
* Upper bound, in bits, used when deserializing {@link BitArray} to prevent pathological
* allocations.
*/
public static final int MAX_BITARRAY_SIZE = 2048 * 8;
public static final int MAX_BITARRAY_SIZE = SerializationLimits.MAX_BITARRAY_SIZE;

/**
* Maximum allowed inbound variable-length payload, in bytes.
*
* <p>The limit equals {@link NewPacketFormat#MAX_MESSAGE_SIZE} minus four bytes to account for a
* leading length integer in the wire format.
*/
// Max packet format size – 4 to account for starting size integer.
public static final int MAX_ARRAY_LENGTH = NewPacketFormat.MAX_MESSAGE_SIZE - 4;
/** Maximum allowed inbound variable-length payload, in bytes. */
public static final int MAX_ARRAY_LENGTH = SerializationLimits.MAX_ARRAY_LENGTH;

/**
* Reads a {@link List} whose elements are of {@code elementType} from the given {@link
Expand Down
Loading