1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package io.netty.channel.uring;
17
18 import io.netty.channel.unix.DomainSocketAddress;
19 import io.netty.util.internal.PlatformDependent;
20
21 import java.net.Inet4Address;
22 import java.net.Inet6Address;
23 import java.net.InetAddress;
24 import java.net.InetSocketAddress;
25 import java.net.UnknownHostException;
26 import java.nio.ByteBuffer;
27 import java.nio.ByteOrder;
28 import java.nio.charset.StandardCharsets;
29
30 final class SockaddrIn {
31 static final byte[] IPV4_MAPPED_IPV6_PREFIX = {
32 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, (byte) 0xff, (byte) 0xff };
33 static final int IPV4_ADDRESS_LENGTH = 4;
34 static final int IPV6_ADDRESS_LENGTH = 16;
35 static final byte[] SOCKADDR_IN6_EMPTY_ARRAY = new byte[Native.SIZEOF_SOCKADDR_IN6];
36 static final byte[] SOCKADDR_IN_EMPTY_ARRAY = new byte[Native.SIZEOF_SOCKADDR_IN];
37
38 private SockaddrIn() { }
39
40 static int set(boolean ipv6, ByteBuffer memory, InetSocketAddress address) {
41 if (ipv6) {
42 return setIPv6(memory, address.getAddress(), address.getPort());
43 }
44 return setIPv4(memory, address.getAddress(), address.getPort());
45 }
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61 static int setIPv4(ByteBuffer memory, InetAddress address, int port) {
62 int position = memory.position();
63 memory.mark();
64 try {
65
66 memory.put(SOCKADDR_IN_EMPTY_ARRAY);
67
68 memory.putShort(position + Native.SOCKADDR_IN_OFFSETOF_SIN_FAMILY, Native.AF_INET);
69 memory.putShort(position + Native.SOCKADDR_IN_OFFSETOF_SIN_PORT, handleNetworkOrder(memory.order(),
70 (short) port));
71
72 byte[] bytes = address.getAddress();
73 int offset = 0;
74 if (bytes.length == IPV6_ADDRESS_LENGTH) {
75
76 offset = IPV4_MAPPED_IPV6_PREFIX.length;
77 }
78 assert bytes.length == offset + IPV4_ADDRESS_LENGTH;
79 memory.position(position + Native.SOCKADDR_IN_OFFSETOF_SIN_ADDR + Native.IN_ADDRESS_OFFSETOF_S_ADDR);
80 memory.put(bytes, offset, IPV4_ADDRESS_LENGTH);
81 return Native.SIZEOF_SOCKADDR_IN;
82 } finally {
83
84 memory.reset();
85 }
86 }
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103 static int setIPv6(ByteBuffer memory, InetAddress address, int port) {
104 int position = memory.position();
105 memory.mark();
106 try {
107
108 memory.put(SOCKADDR_IN6_EMPTY_ARRAY);
109 memory.putShort(position + Native.SOCKADDR_IN6_OFFSETOF_SIN6_FAMILY, Native.AF_INET6);
110 memory.putShort(position + Native.SOCKADDR_IN6_OFFSETOF_SIN6_PORT,
111 handleNetworkOrder(memory.order(), (short) port));
112
113 byte[] bytes = address.getAddress();
114 int offset = Native.SOCKADDR_IN6_OFFSETOF_SIN6_ADDR + Native.IN6_ADDRESS_OFFSETOF_S6_ADDR;
115 if (bytes.length == IPV4_ADDRESS_LENGTH) {
116 memory.position(position + offset);
117 memory.put(IPV4_MAPPED_IPV6_PREFIX);
118 memory.put(bytes, 0, IPV4_ADDRESS_LENGTH);
119
120
121 } else {
122 memory.position(position + offset);
123 memory.put(bytes, 0, IPV6_ADDRESS_LENGTH);
124
125 memory.putInt(position + Native.SOCKADDR_IN6_OFFSETOF_SIN6_SCOPE_ID,
126 ((Inet6Address) address).getScopeId());
127 }
128 return Native.SIZEOF_SOCKADDR_IN6;
129 } finally {
130 memory.reset();
131 }
132 }
133
134 static int setUds(ByteBuffer memory, DomainSocketAddress address) {
135 byte[] path = address.path().getBytes(StandardCharsets.UTF_8);
136 if (path.length + 1 > Native.MAX_SUN_PATH_LEN) {
137 throw new IllegalArgumentException("path too long: " + address.path());
138 }
139 int position = memory.position();
140 memory.mark();
141 try {
142 memory.putShort(position + Native.SOCKADDR_UN_OFFSETOF_SUN_FAMILY, Native.AF_UNIX);
143 memory.position(position + Native.SOCKADDR_UN_OFFSETOF_SUN_PATH);
144 memory.put(path);
145 memory.put((byte) 0);
146 return Native.SIZEOF_SOCKADDR_UN;
147 } finally {
148 memory.reset();
149 }
150 }
151
152 static InetSocketAddress getIPv4(ByteBuffer memory, byte[] tmpArray) {
153 assert tmpArray.length == IPV4_ADDRESS_LENGTH;
154 int position = memory.position();
155 memory.mark();
156 try {
157 int port = handleNetworkOrder(memory.order(),
158 memory.getShort(position + Native.SOCKADDR_IN_OFFSETOF_SIN_PORT)) & 0xFFFF;
159 memory.position(position + Native.SOCKADDR_IN_OFFSETOF_SIN_ADDR + Native.IN_ADDRESS_OFFSETOF_S_ADDR);
160 memory.get(tmpArray);
161 try {
162 return new InetSocketAddress(InetAddress.getByAddress(tmpArray), port);
163 } catch (UnknownHostException ignore) {
164 return null;
165 }
166 } finally {
167 memory.reset();
168 }
169 }
170
171 static InetSocketAddress getIPv6(ByteBuffer memory, byte[] ipv6Array, byte[] ipv4Array) {
172 assert ipv6Array.length == IPV6_ADDRESS_LENGTH;
173 assert ipv4Array.length == IPV4_ADDRESS_LENGTH;
174 int position = memory.position();
175 memory.mark();
176 try {
177 int port = handleNetworkOrder(memory.order(), memory.getShort(
178 position + Native.SOCKADDR_IN6_OFFSETOF_SIN6_PORT)) & 0xFFFF;
179 memory.position(position + Native.SOCKADDR_IN6_OFFSETOF_SIN6_ADDR + Native.IN6_ADDRESS_OFFSETOF_S6_ADDR);
180 memory.get(ipv6Array);
181 if (PlatformDependent.equals(
182 ipv6Array, 0, IPV4_MAPPED_IPV6_PREFIX, 0, IPV4_MAPPED_IPV6_PREFIX.length)) {
183 System.arraycopy(ipv6Array, IPV4_MAPPED_IPV6_PREFIX.length, ipv4Array, 0, IPV4_ADDRESS_LENGTH);
184 try {
185 return new InetSocketAddress(Inet4Address.getByAddress(ipv4Array), port);
186 } catch (UnknownHostException ignore) {
187 return null;
188 }
189 } else {
190 int scopeId = memory.getInt(position + Native.SOCKADDR_IN6_OFFSETOF_SIN6_SCOPE_ID);
191 try {
192 return new InetSocketAddress(Inet6Address.getByAddress(null, ipv6Array, scopeId), port);
193 } catch (UnknownHostException ignore) {
194 return null;
195 }
196 }
197 } finally {
198 memory.reset();
199 }
200 }
201
202 static boolean hasPortIpv4(ByteBuffer memory) {
203 int port = memory.getShort(memory.position() + Native.SOCKADDR_IN_OFFSETOF_SIN_PORT) & 0xFFFF;
204 return port > 0;
205 }
206
207 static boolean hasPortIpv6(ByteBuffer memory) {
208 int port = memory.getShort(memory.position() + Native.SOCKADDR_IN6_OFFSETOF_SIN6_PORT) & 0xFFFF;
209 return port > 0;
210 }
211
212 private static short handleNetworkOrder(ByteOrder order, short v) {
213 return order != ByteOrder.nativeOrder() ? v : Short.reverseBytes(v);
214 }
215 }