Skip to content

Commit e7f0ed6

Browse files
committed
jnr-unixsocket: Added javadoc; Fixed: SASL mode was never set to SERVER even transport was for a listening socket
1 parent 0b09e9a commit e7f0ed6

4 files changed

Lines changed: 115 additions & 9 deletions

File tree

dbus-java/src/main/java/org/freedesktop/dbus/connections/transports/AbstractTransport.java

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@
1515
import org.slf4j.Logger;
1616
import org.slf4j.LoggerFactory;
1717

18+
/**
19+
* Base class for all transport types.
20+
*
21+
* @author hypfvieh
22+
* @since v3.2.0 - 2019-02-08
23+
*/
1824
public abstract class AbstractTransport implements Closeable {
1925

2026
private final Logger logger;
@@ -30,11 +36,23 @@ public abstract class AbstractTransport implements Closeable {
3036
AbstractTransport(BusAddress _address, int _timeout) {
3137
address = _address;
3238
timeout = _timeout;
33-
saslMode = SASL.SaslMode.CLIENT;
39+
40+
if (_address.isListeningSocket()) {
41+
saslMode = SASL.SaslMode.SERVER;
42+
} else {
43+
saslMode = SASL.SaslMode.CLIENT;
44+
}
45+
3446
saslAuthMode = SASL.AUTH_NONE;
3547
logger = LoggerFactory.getLogger(getClass());
3648
}
3749

50+
/**
51+
* Write a message to the underlying socket.
52+
*
53+
* @param _msg message to write
54+
* @throws IOException on write error or if output was already closed or null
55+
*/
3856
public void writeMessage(Message _msg) throws IOException {
3957
if (outputWriter != null && !outputWriter.isClosed()) {
4058
outputWriter.writeMessage(_msg);
@@ -43,19 +61,35 @@ public void writeMessage(Message _msg) throws IOException {
4361
}
4462
}
4563

64+
/**
65+
* Read a message from the underlying socket.
66+
*
67+
* @return read message, maybe null
68+
* @throws IOException when input already close or null
69+
* @throws DBusException when message could not be converted to a DBus message
70+
*/
4671
public Message readMessage() throws IOException, DBusException {
4772
if (inputReader != null && !inputReader.isClosed()) {
4873
return inputReader.readMessage();
4974
}
5075
throw new IOException("InputReader already closed or null");
5176
}
5277

53-
void start() throws IOException {
54-
connect();
55-
}
56-
78+
/**
79+
* Abstract method implemented by concrete sub classes to establish a connection
80+
* using whatever transport type (e.g. TCP/Unix socket).
81+
* @throws IOException when connection fails
82+
*/
5783
abstract void connect() throws IOException;
58-
84+
85+
/**
86+
* Helper method to authenticate to DBus using SASL.
87+
*
88+
* @param _out output stream
89+
* @param _in input stream
90+
* @param _sock socket
91+
* @throws IOException on any error
92+
*/
5993
protected void authenticate(OutputStream _out, InputStream _in, Socket _sock) throws IOException {
6094
if (!(new SASL()).auth(saslMode, saslAuthMode, address.getGuid(), _out, _in, _sock)) {
6195
_out.close();
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package org.freedesktop.dbus.connections.transports;
2+
3+
import java.io.IOException;
4+
import java.net.InetSocketAddress;
5+
import java.net.ServerSocket;
6+
import java.net.Socket;
7+
8+
import org.freedesktop.dbus.connections.BusAddress;
9+
import org.freedesktop.dbus.connections.SASL;
10+
11+
/**
12+
* Transport type representing a transport connection to TCP.
13+
*
14+
* @author hypfvieh
15+
* @since v3.2.0 - 2019-02-08
16+
*/
17+
public class TcpTransport extends AbstractTransport {
18+
19+
private Socket socket;
20+
21+
TcpTransport(BusAddress _address, int _timeout) {
22+
super(_address, _timeout);
23+
setSaslAuthMode(SASL.AUTH_SHA);
24+
}
25+
26+
/**
27+
* Connect to DBus using TCP.
28+
* @throws IOException on error
29+
*/
30+
void connect() throws IOException {
31+
32+
if (getAddress().isListeningSocket()) {
33+
try (ServerSocket ss = new ServerSocket()) {
34+
ss.bind(new InetSocketAddress(getAddress().getHost(), getAddress().getPort()));
35+
socket = ss.accept();
36+
}
37+
} else {
38+
socket = new Socket();
39+
socket.connect(new InetSocketAddress(getAddress().getHost(), getAddress().getPort()));
40+
}
41+
42+
getLogger().trace("Setting timeout to {} on Socket", getTimeout());
43+
socket.setSoTimeout(getTimeout());
44+
45+
authenticate(socket.getOutputStream(), socket.getInputStream(), socket);
46+
}
47+
48+
@Override
49+
public void close() throws IOException {
50+
if (socket != null && !socket.isClosed()) {
51+
socket.close();
52+
}
53+
super.close();
54+
}
55+
}
56+

dbus-java/src/main/java/org/freedesktop/dbus/connections/transports/TransportFactory.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,23 @@ public static AbstractTransport createTransport(BusAddress _address, int _timeou
3535

3636
if (_address.getBusType() == AddressBusTypes.UNIX) {
3737
transport = new UnixSocketTransport(_address, _timeout);
38-
// } else if (_address.getBusType() == AddressBusTypes.TCP) {
39-
// return new TcpTransport(_address, _timeout);
38+
} else if (_address.getBusType() == AddressBusTypes.TCP) {
39+
transport = new TcpTransport(_address, _timeout);
4040
} else {
4141
throw new IOException("Unknown address type " + _address.getType());
4242
}
4343

44-
transport.start();
44+
transport.connect();
4545
return transport;
4646
}
4747

48+
/**
49+
* Creates a new transport encapsulating connection to a unix socket or TCP socket.
50+
*
51+
* @param _address Address parameter
52+
* @return {@link AbstractTransport}
53+
* @throws IOException when transport could not be created
54+
*/
4855
public static AbstractTransport createTransport(BusAddress _address) throws IOException {
4956
return createTransport(_address, 10000);
5057
}

dbus-java/src/main/java/org/freedesktop/dbus/connections/transports/UnixSocketTransport.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@
1010
import jnr.unixsocket.UnixSocketChannel;
1111
import jnr.unixsocket.UnixSocketOptions;
1212

13+
/**
14+
* Transport type representing a transport connection to a unix socket.
15+
* @author hypfvieh
16+
* @since v3.2.0 - 2019-02-08
17+
*/
1318
public class UnixSocketTransport extends AbstractTransport {
1419
private final UnixSocketAddress unixSocketAddress;
1520
private UnixServerSocketChannel unixServerSocket;
@@ -28,6 +33,10 @@ public class UnixSocketTransport extends AbstractTransport {
2833
setSaslAuthMode(SASL.AUTH_EXTERNAL);
2934
}
3035

36+
/**
37+
* Establish a connection to DBus using unix sockets.
38+
* @throws IOException on error
39+
*/
3140
void connect() throws IOException {
3241
UnixSocketChannel us;
3342
if (getAddress().isListeningSocket()) {

0 commit comments

Comments
 (0)