Skip to content

Commit

Permalink
Rename SockaddrIn to Sockaddr since sockaddr_un is not an Internet ad…
Browse files Browse the repository at this point in the history
…dress type
  • Loading branch information
astei committed Jan 22, 2021
1 parent 236e8a0 commit 7c3bb09
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
import io.netty.channel.socket.ChannelInputShutdownReadComplete;
import io.netty.channel.socket.SocketChannelConfig;
import io.netty.channel.unix.Buffer;
import io.netty.channel.unix.DomainSocketAddress;
import io.netty.channel.unix.Errors;
import io.netty.channel.unix.FileDescriptor;
import io.netty.channel.unix.UnixChannel;
Expand Down Expand Up @@ -677,7 +676,7 @@ public void connect(
remoteAddressMemory = Buffer.allocateDirectWithNativeOrder(Native.SIZEOF_SOCKADDR_STORAGE);
long remoteAddressMemoryAddress = Buffer.memoryAddress(remoteAddressMemory);

int addrLen = SockaddrIn.write(remoteAddressMemoryAddress, remoteAddress);
int addrLen = Sockaddr.write(remoteAddressMemoryAddress, remoteAddress);

final IOUringSubmissionQueue ioUringSubmissionQueue = submissionQueue();
ioUringSubmissionQueue.addConnect(socket.intValue(), remoteAddressMemoryAddress, addrLen, (short) 0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ final class IOUringEventLoop extends SingleThreadEventLoop implements IOUringCom
private final FileDescriptor eventfd;

// The maximum number of bytes for an InetAddress / Inet6Address
private final byte[] inet4AddressArray = new byte[SockaddrIn.IPV4_ADDRESS_LENGTH];
private final byte[] inet6AddressArray = new byte[SockaddrIn.IPV6_ADDRESS_LENGTH];
private final byte[] inet4AddressArray = new byte[Sockaddr.IPV4_ADDRESS_LENGTH];
private final byte[] inet6AddressArray = new byte[Sockaddr.IPV6_ADDRESS_LENGTH];

private long prevDeadlineNanos = NONE;
private boolean pendingWakeup;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import io.netty.channel.Channel;
import io.netty.channel.socket.ServerSocketChannel;

import java.net.Inet4Address;
import java.net.InetSocketAddress;
import java.net.SocketAddress;

Expand All @@ -41,10 +40,10 @@ Channel newChildChannel(int fd, long acceptedAddressMemoryAddress, long accepted
if (socket.isIpv6()) {
byte[] ipv6Array = ((IOUringEventLoop) eventLoop()).inet6AddressArray();
byte[] ipv4Array = ((IOUringEventLoop) eventLoop()).inet4AddressArray();
address = SockaddrIn.readIPv6(acceptedAddressMemoryAddress, ipv6Array, ipv4Array);
address = Sockaddr.readIPv6(acceptedAddressMemoryAddress, ipv6Array, ipv4Array);
} else {
byte[] addressArray = ((IOUringEventLoop) eventLoop()).inet4AddressArray();
address = SockaddrIn.readIPv4(acceptedAddressMemoryAddress, addressArray);
address = Sockaddr.readIPv4(acceptedAddressMemoryAddress, addressArray);
}
return new IOUringSocketChannel(this, new LinuxSocket(fd), address);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ void write(LinuxSocket socket, InetSocketAddress address, long bufferAddress , i
addressLength = socket.isIpv6() ? Native.SIZEOF_SOCKADDR_IN6 : Native.SIZEOF_SOCKADDR_IN;
PlatformDependent.setMemory(sockAddress, Native.SIZEOF_SOCKADDR_STORAGE, (byte) 0);
} else {
addressLength = SockaddrIn.write(sockAddress, address);
addressLength = Sockaddr.write(sockAddress, address);
}
Iov.write(iovAddress, bufferAddress, length);
MsgHdr.write(memory, sockAddress, addressLength, iovAddress, 1);
Expand All @@ -54,10 +54,10 @@ DatagramPacket read(IOUringDatagramChannel channel, ByteBuf buffer, int bytesRea
byte[] ipv6Bytes = eventLoop.inet6AddressArray();
byte[] ipv4bytes = eventLoop.inet4AddressArray();

sender = SockaddrIn.readIPv6(sockAddress, ipv6Bytes, ipv4bytes);
sender = Sockaddr.readIPv6(sockAddress, ipv6Bytes, ipv4bytes);
} else {
byte[] bytes = eventLoop.inet4AddressArray();
sender = SockaddrIn.readIPv4(sockAddress, bytes);
sender = Sockaddr.readIPv4(sockAddress, bytes);
}
long iovAddress = memory + Native.SIZEOF_MSGHDR + Native.SIZEOF_SOCKADDR_STORAGE;
long bufferAddress = Iov.readBufferAddress(iovAddress);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,25 +28,25 @@

import static io.netty.util.internal.PlatformDependent.BIG_ENDIAN_NATIVE_ORDER;

final class SockaddrIn {
final class Sockaddr {
static final byte[] IPV4_MAPPED_IPV6_PREFIX = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, (byte) 0xff, (byte) 0xff };
static final int IPV4_ADDRESS_LENGTH = 4;
static final int IPV6_ADDRESS_LENGTH = 16;
static final int DOMAIN_ADDRESS_LENGTH = 108;

private SockaddrIn() { }
private Sockaddr() { }

static int write(long memory, SocketAddress address) {
if (address instanceof DomainSocketAddress) {
return SockaddrIn.writeDomain(memory, ((DomainSocketAddress) address).path());
return Sockaddr.writeDomain(memory, ((DomainSocketAddress) address).path());
} else if (address instanceof InetSocketAddress) {
InetSocketAddress inetSocketAddress = (InetSocketAddress) address;
boolean ipv6 = ((InetSocketAddress) address).getAddress() instanceof Inet6Address;
if (ipv6) {
return SockaddrIn.writeIPv6(memory, inetSocketAddress.getAddress(), inetSocketAddress.getPort());
return Sockaddr.writeIPv6(memory, inetSocketAddress.getAddress(), inetSocketAddress.getPort());
} else {
return SockaddrIn.writeIPv4(memory, inetSocketAddress.getAddress(), inetSocketAddress.getPort());
return Sockaddr.writeIPv4(memory, inetSocketAddress.getAddress(), inetSocketAddress.getPort());
}
} else {
throw new IllegalArgumentException("Unknown address family");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

import static org.junit.Assume.assumeTrue;

public class SockaddrInTest {
public class SockaddrTest {

@BeforeClass
public static void loadJNI() {
Expand All @@ -42,9 +42,9 @@ public void testIp4() throws Exception {
long memoryAddress = Buffer.memoryAddress(buffer);
InetAddress address = InetAddress.getByAddress(new byte[] { 10, 10, 10, 10 });
int port = 45678;
Assert.assertEquals(Native.SIZEOF_SOCKADDR_IN, SockaddrIn.writeIPv4(memoryAddress, address, port));
Assert.assertEquals(Native.SIZEOF_SOCKADDR_IN, Sockaddr.writeIPv4(memoryAddress, address, port));
byte[] bytes = new byte[4];
InetSocketAddress sockAddr = SockaddrIn.readIPv4(memoryAddress, bytes);
InetSocketAddress sockAddr = Sockaddr.readIPv4(memoryAddress, bytes);
Assert.assertArrayEquals(address.getAddress(), sockAddr.getAddress().getAddress());
Assert.assertEquals(port, sockAddr.getPort());
} finally {
Expand All @@ -60,11 +60,11 @@ public void testIp6() throws Exception {
Inet6Address address = Inet6Address.getByAddress(
null, new byte[] { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }, 12345);
int port = 45678;
Assert.assertEquals(Native.SIZEOF_SOCKADDR_IN6, SockaddrIn.writeIPv6(memoryAddress, address, port));
Assert.assertEquals(Native.SIZEOF_SOCKADDR_IN6, Sockaddr.writeIPv6(memoryAddress, address, port));
byte[] ipv6Bytes = new byte[16];
byte[] ipv4Bytes = new byte[4];

InetSocketAddress sockAddr = SockaddrIn.readIPv6(memoryAddress, ipv6Bytes, ipv4Bytes);
InetSocketAddress sockAddr = Sockaddr.readIPv6(memoryAddress, ipv6Bytes, ipv4Bytes);
Inet6Address inet6Address = (Inet6Address) sockAddr.getAddress();
Assert.assertArrayEquals(address.getAddress(), inet6Address.getAddress());
Assert.assertEquals(address.getScopeId(), inet6Address.getScopeId());
Expand All @@ -81,15 +81,15 @@ public void testWriteIp4ReadIpv6Mapped() throws Exception {
long memoryAddress = Buffer.memoryAddress(buffer);
InetAddress address = InetAddress.getByAddress(new byte[] { 10, 10, 10, 10 });
int port = 45678;
Assert.assertEquals(Native.SIZEOF_SOCKADDR_IN6, SockaddrIn.writeIPv6(memoryAddress, address, port));
Assert.assertEquals(Native.SIZEOF_SOCKADDR_IN6, Sockaddr.writeIPv6(memoryAddress, address, port));
byte[] ipv6Bytes = new byte[16];
byte[] ipv4Bytes = new byte[4];

InetSocketAddress sockAddr = SockaddrIn.readIPv6(memoryAddress, ipv6Bytes, ipv4Bytes);
InetSocketAddress sockAddr = Sockaddr.readIPv6(memoryAddress, ipv6Bytes, ipv4Bytes);
Inet4Address ipv4Address = (Inet4Address) sockAddr.getAddress();

System.arraycopy(SockaddrIn.IPV4_MAPPED_IPV6_PREFIX, 0, ipv6Bytes, 0,
SockaddrIn.IPV4_MAPPED_IPV6_PREFIX.length);
System.arraycopy(Sockaddr.IPV4_MAPPED_IPV6_PREFIX, 0, ipv6Bytes, 0,
Sockaddr.IPV4_MAPPED_IPV6_PREFIX.length);
Assert.assertArrayEquals(ipv4Bytes, ipv4Address.getAddress());
Assert.assertEquals(port, sockAddr.getPort());
} finally {
Expand Down

0 comments on commit 7c3bb09

Please sign in to comment.