forked from docker-java/docker-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDomainSocket.java
More file actions
191 lines (155 loc) · 5.64 KB
/
Copy pathDomainSocket.java
File metadata and controls
191 lines (155 loc) · 5.64 KB
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
/*
* Copyright 2012-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.github.dockerjava.transport;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.nio.ByteBuffer;
import com.github.dockerjava.transport.FileDescriptor.Handle;
import com.sun.jna.LastErrorException;
import com.sun.jna.Native;
import com.sun.jna.Platform;
/**
* A {@link Socket} implementation for Linux of BSD domain sockets.
*
* @author Phillip Webb
*/
public abstract class DomainSocket extends AbstractSocket {
private static final int SHUT_RD = 0;
private static final int SHUT_WR = 1;
protected static final int PF_LOCAL = 1;
protected static final byte AF_LOCAL = 1;
protected static final int SOCK_STREAM = 1;
private final FileDescriptor fileDescriptor;
private final InputStream inputStream;
private final OutputStream outputStream;
static {
Native.register(Platform.C_LIBRARY_NAME);
}
DomainSocket(String path) throws IOException {
try {
this.fileDescriptor = open(path);
this.inputStream = new DomainSocketInputStream();
this.outputStream = new DomainSocketOutputStream();
} catch (LastErrorException ex) {
throw new IOException(ex);
}
}
private FileDescriptor open(String path) {
int handle = socket(PF_LOCAL, SOCK_STREAM, 0);
connect(path, handle);
return new FileDescriptor(handle, this::close);
}
private int read(ByteBuffer buffer) throws IOException {
try (Handle handle = this.fileDescriptor.acquire()) {
if (handle.isClosed()) {
return -1;
}
try {
return read(handle.intValue(), buffer, buffer.remaining());
} catch (LastErrorException ex) {
throw new IOException(ex);
}
}
}
public void write(ByteBuffer buffer) throws IOException {
try (Handle handle = this.fileDescriptor.acquire()) {
if (!handle.isClosed()) {
try {
write(handle.intValue(), buffer, buffer.remaining());
} catch (LastErrorException ex) {
throw new IOException(ex);
}
}
}
}
@Override
public InputStream getInputStream() {
return this.inputStream;
}
@Override
public OutputStream getOutputStream() {
return this.outputStream;
}
@Override
public void close() throws IOException {
super.close();
try {
this.fileDescriptor.close();
} catch (LastErrorException ex) {
throw new IOException(ex);
}
}
protected abstract void connect(String path, int handle);
private native int socket(int domain, int type, int protocol) throws LastErrorException;
private native int read(int fd, ByteBuffer buffer, int count) throws LastErrorException;
private native int write(int fd, ByteBuffer buffer, int count) throws LastErrorException;
private native int close(int fd) throws LastErrorException;
/**
* Return a new {@link DomainSocket} for the given path.
* @param path the path to the domain socket
* @return a {@link DomainSocket} instance
* @throws IOException if the socket cannot be opened
* @deprecated use {@link UnixSocket#get(String)}
*/
@Deprecated
public static DomainSocket get(String path) throws IOException {
if (Platform.isMac() || isBsdPlatform()) {
return new BsdDomainSocket(path);
}
return new LinuxDomainSocket(path);
}
private static boolean isBsdPlatform() {
return Platform.isFreeBSD() || Platform.iskFreeBSD() || Platform.isNetBSD() || Platform.isOpenBSD();
}
/**
* {@link InputStream} returned from the {@link DomainSocket}.
*/
private class DomainSocketInputStream extends InputStream {
@Override
public int read() throws IOException {
ByteBuffer buffer = ByteBuffer.allocate(1);
int amountRead = DomainSocket.this.read(buffer);
return (amountRead != 1) ? -1 : buffer.get() & 0xFF;
}
@Override
public int read(byte[] b, int off, int len) throws IOException {
if (len == 0) {
return 0;
}
int amountRead = DomainSocket.this.read(ByteBuffer.wrap(b, off, len));
return (amountRead > 0) ? amountRead : -1;
}
}
/**
* {@link OutputStream} returned from the {@link DomainSocket}.
*/
private class DomainSocketOutputStream extends OutputStream {
@Override
public void write(int b) throws IOException {
ByteBuffer buffer = ByteBuffer.allocate(1);
buffer.put(0, (byte) (b & 0xFF));
DomainSocket.this.write(buffer);
}
@Override
public void write(byte[] b, int off, int len) throws IOException {
if (len != 0) {
DomainSocket.this.write(ByteBuffer.wrap(b, off, len));
}
}
}
}