1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
|
/*
*/
package com.wireguard.android.backend;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;
import java.net.SocketException;
import java.net.SocketImplFactory;
import java.net.SocketOptions;
import java.nio.channels.SocketChannel;
import java.util.Vector;
import android.net.LocalSocket;
import android.net.LocalSocketAddress;
import android.util.Log;
/**
* Adaptor allows using a LocalSocket as a Socket.
*/
final class LocalSocketAdapter extends Socket {
private final LocalSocketAddress address;
private final LocalSocket unix;
private final SocketAddress localAddress;
private InetSocketAddress inetSocketAddress;
private InputStream is;
private OutputStream os;
LocalSocketAdapter(LocalSocketAddress address) {
this.address = address;
this.localAddress = new InetSocketAddress(0);
unix = new LocalSocket();
}
LocalSocketAdapter(LocalSocketAddress address, InetSocketAddress inetAddress) {
this(address);
this.inetSocketAddress = inetAddress;
}
private void throwUnsupportedOperationException() {
Log.i("helloworld", "Unsupported: " + Log.getStackTraceString(new Exception()));
throw new UnsupportedOperationException();
}
@Override
public void bind (SocketAddress bindpoint) {
throwUnsupportedOperationException();
}
@Override
public void close() throws IOException {
unix.close();
}
@Override public void connect(SocketAddress endpoint) throws IOException {
this.inetSocketAddress = (InetSocketAddress) endpoint;
try {
unix.connect(address);
} catch (IOException e) {
Log.i("helloworld", "Error: " + e.toString());
throw e;
}
}
@Override
public void connect(SocketAddress endpoint, int timeout) throws IOException {
this.inetSocketAddress = (InetSocketAddress) endpoint;
unix.connect(address, timeout);
}
@Override
public SocketChannel getChannel() {
throwUnsupportedOperationException();
return null;
}
@Override public InetAddress getInetAddress() {
return inetSocketAddress.getAddress();
}
@Override
public InputStream getInputStream() throws IOException {
is = unix.getInputStream();
return is;
}
@Override
public boolean getKeepAlive() {
throwUnsupportedOperationException();
return false;
}
@Override
public InetAddress getLocalAddress() {
throwUnsupportedOperationException();
return null;
}
@Override
public int getLocalPort() {
throwUnsupportedOperationException();
return 0;
}
@Override
public SocketAddress getLocalSocketAddress() {
//throwUnsupportedOperationException();
return localAddress;
}
@Override
public boolean getOOBInline() {
throwUnsupportedOperationException();
return false;
}
@Override
public OutputStream getOutputStream() throws IOException {
if (os != null)
return os;
OutputStream unixOs = unix.getOutputStream();
os = new OutputStream() {
@Override
public void close() throws IOException {
// LocalSocket's default implementation closes the socket,
// which leaves readers of thes InputStream hanging.
// Instead shutdown input (and output) to release readers.
LocalSocketAdapter.this.shutdownInput();
LocalSocketAdapter.this.shutdownOutput();
}
@Override
public void write (byte[] b) throws IOException {
unixOs.write(b);
}
@Override
public void write (byte[] b, int off, int len) throws IOException {
unixOs.write(b, off, len);
}
@Override
public void write (int b) throws IOException {
unixOs.write(b);
}
};
return os;
}
@Override
public int getPort() {
return inetSocketAddress.getPort();
}
@Override
public int getReceiveBufferSize() throws SocketException {
try {
return unix.getReceiveBufferSize();
} catch (IOException e) {
throw new SocketException(e.getMessage());
}
}
@Override
public SocketAddress getRemoteSocketAddress() {
return inetSocketAddress;
}
@Override
public boolean getReuseAddress() {
throwUnsupportedOperationException();
return false;
}
@Override
public int getSendBufferSize() throws SocketException {
try {
return unix.getSendBufferSize();
} catch (IOException e) {
throw new SocketException(e.getMessage());
}
}
@Override
public int getSoLinger() {
throwUnsupportedOperationException();
return 0;
}
@Override
public int getSoTimeout() throws SocketException {
try {
return unix.getSoTimeout();
} catch (IOException e) {
throw new SocketException(e.getMessage());
}
}
@Override
public boolean getTcpNoDelay() {
throwUnsupportedOperationException();
return false;
}
@Override
public int getTrafficClass() {
throwUnsupportedOperationException();
return 0;
}
@Override
public boolean isBound() {
return unix.isBound();
}
@Override
public boolean isClosed() {
return unix.isClosed();
}
@Override
public boolean isConnected() {
return unix.isConnected();
}
@Override
public boolean isInputShutdown() {
return unix.isInputShutdown();
}
@Override
public boolean isOutputShutdown() {
return unix.isOutputShutdown();
}
@Override
public void sendUrgentData (int data) {
throwUnsupportedOperationException();
}
@Override
public void setKeepAlive (boolean on) {
throwUnsupportedOperationException();
}
@Override
public void setOOBInline (boolean on) {
throwUnsupportedOperationException();
}
@Override
public void setPerformancePreferences (int connectionTime,
int latency,
int bandwidth) {
throwUnsupportedOperationException();
}
@Override
public void setReceiveBufferSize(int size) throws SocketException {
try {
unix.setReceiveBufferSize(size);
} catch (IOException e) {
throw new SocketException(e.getMessage());
}
}
@Override
public void setReuseAddress (boolean on) {
throwUnsupportedOperationException();
}
@Override
public void setSendBufferSize(int size) throws SocketException {
try {
unix.setSendBufferSize(size);
} catch (IOException e) {
throw new SocketException(e.getMessage());
}
}
@Override
public void setSoLinger (boolean on,
int linger) {
throwUnsupportedOperationException();
}
@Override
public void setSoTimeout(int timeout) throws SocketException {
try {
unix.setSoTimeout(timeout);
} catch (IOException e) {
throw new SocketException(e.getMessage());
}
}
@Override
public void setTcpNoDelay (boolean on) {
// Not relevant for local sockets.
}
@Override
public void setTrafficClass (int tc) {
throwUnsupportedOperationException();
}
@Override
public void shutdownInput() throws IOException {
unix.shutdownInput();
}
@Override
public void shutdownOutput() throws IOException {
unix.shutdownOutput();
}
@Override
public String toString() {
return unix.toString();
}
}
|