Skip to content

Commit a45bb34

Browse files
committed
Fix AFSocketAddress serialization
Since AFSocketAddress extends SocketAddress, it must support serialization. Unfortunately, there were two members that aren't serializable. Fix serialization to keep everyone happy.
1 parent 96c9f4f commit a45bb34

2 files changed

Lines changed: 54 additions & 4 deletions

File tree

junixsocket-common/src/main/java/org/newsclub/net/unix/AFSocketAddress.java

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
import java.io.FileDescriptor;
2222
import java.io.FileNotFoundException;
2323
import java.io.IOException;
24+
import java.io.ObjectInputStream;
25+
import java.io.ObjectOutputStream;
2426
import java.net.DatagramSocket;
2527
import java.net.InetAddress;
2628
import java.net.InetSocketAddress;
@@ -87,12 +89,13 @@ protected ByteBuffer initialValue() {
8789
/**
8890
* The system-native representation of this address, or {@code null}.
8991
*/
90-
private final ByteBuffer nativeAddress;
92+
@SuppressWarnings("PMD.ImmutableField")
93+
private transient ByteBuffer nativeAddress;
9194

9295
/**
9396
* The address family.
9497
*/
95-
private final AFAddressFamily<?> addressFamily;
98+
private transient AFAddressFamily<?> addressFamily;
9699

97100
/**
98101
* Creates a new socket address.
@@ -643,12 +646,13 @@ public URI toURI(String scheme, URI template) throws IOException {
643646
* address type is not natively supported by this platform.
644647
*
645648
* This call is mostly suited for debugging purposes. The resulting string is specific to the
646-
* platform the code is executed on, and thus may be different among platforms (or {@code null}).
649+
* platform the code is executed on, and thus may be different among platforms.
647650
*
648651
* @param socketType The socket type, or {@code null} to omit from string.
649652
* @param socketProtocol The socket protocol, or {@code null} to omit from string.
650653
* @return The string (such as 1:0:x2f746d702f796f).
651-
* @throws IOException on error.
654+
* @throws IOException on error (a {@link SocketException} is thrown if the native address cannot
655+
* be accessed).
652656
*/
653657
public @Nullable @SuppressWarnings("PMD.NPathComplexity") String toSocatAddressString(
654658
AFSocketType socketType, AFSocketProtocol socketProtocol) throws IOException {
@@ -703,4 +707,28 @@ public URI toURI(String scheme, URI template) throws IOException {
703707
public boolean covers(AFSocketAddress other) {
704708
return this.equals(other);
705709
}
710+
711+
/**
712+
* Custom serialization: Reference {@link AFAddressFamily} instance by identifier string.
713+
*
714+
* @param in The {@link ObjectInputStream}.
715+
* @throws ClassNotFoundException on error.
716+
* @throws IOException on error.
717+
*/
718+
private void readObject(ObjectInputStream in) throws ClassNotFoundException, IOException {
719+
in.defaultReadObject();
720+
this.addressFamily = Objects.requireNonNull(AFAddressFamily.getAddressFamily(in.readUTF()),
721+
"address family");
722+
}
723+
724+
/**
725+
* Custom serialization: Reference {@link AFAddressFamily} instance by identifier string.
726+
*
727+
* @param out The {@link ObjectOutputStream}.
728+
* @throws IOException on error.
729+
*/
730+
private void writeObject(ObjectOutputStream out) throws IOException {
731+
out.defaultWriteObject();
732+
out.writeUTF(addressFamily.getJuxString());
733+
}
706734
}

junixsocket-common/src/test/java/org/newsclub/net/unix/AFUNIXSocketAddressTest.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,11 @@
2222
import static org.junit.jupiter.api.Assertions.assertNotNull;
2323
import static org.junit.jupiter.api.Assertions.assertTrue;
2424

25+
import java.io.ByteArrayInputStream;
26+
import java.io.ByteArrayOutputStream;
2527
import java.io.File;
28+
import java.io.ObjectInputStream;
29+
import java.io.ObjectOutputStream;
2630
import java.net.URI;
2731
import java.util.Arrays;
2832

@@ -133,4 +137,22 @@ public void testAbstractNamespace() throws Exception {
133137
assertNotNull(AFUNIXSocketAddress.inAbstractNamespace("test"));
134138
assertNotNull(AFUNIXSocketAddress.inAbstractNamespace("test", 1234));
135139
}
140+
141+
@Test
142+
public void testSerialize() throws Exception {
143+
try (ByteArrayOutputStream bos = new ByteArrayOutputStream();
144+
ObjectOutputStream oos = new ObjectOutputStream(bos)) {
145+
146+
AFUNIXSocketAddress addr = AFUNIXSocketAddress.of(URI.create("file:/tmp/yo"));
147+
oos.writeObject(addr);
148+
oos.flush();
149+
150+
try (ObjectInputStream oin = new ObjectInputStream(new ByteArrayInputStream(bos
151+
.toByteArray()))) {
152+
AFUNIXSocketAddress addr2 = (AFUNIXSocketAddress) oin.readObject();
153+
assertEquals(addr, addr2);
154+
assertEquals(addr.getAddressFamily(), addr2.getAddressFamily());
155+
}
156+
}
157+
}
136158
}

0 commit comments

Comments
 (0)