Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions jni/org_scalasbt_ipcsocket_JNIUnixDomainSocketLibraryProvider.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,13 @@
return result < 0 ? -ERRNO_OF(result) : result; \
} while (0);

static void native_sockaddr(JNIEnv *env, struct sockaddr_un *native,
jobject array, jint len) {
/* Returns 0, or -ENAMETOOLONG if the path and its terminating NUL don't fit. */
static int native_sockaddr(JNIEnv *env, struct sockaddr_un *native,
jobject array, jint len) {
memset(native, 0, sizeof(struct sockaddr_un));
if (len < 0 || (size_t)len >= sizeof(native->sun_path)) {
return -ENAMETOOLONG;
}
if (array) {
jbyte *bytes = (*env)->GetByteArrayElements(env, (jbyteArray)array, 0);
memcpy(native->sun_path, bytes, len);
Expand All @@ -38,6 +42,7 @@ static void native_sockaddr(JNIEnv *env, struct sockaddr_un *native,
#ifdef __APPLE__
native->sun_len = len + 2;
#endif
return 0;
}

jint JNICALL
Expand All @@ -56,7 +61,9 @@ Java_org_scalasbt_ipcsocket_JNIUnixDomainSocketLibraryProvider_bindNative(
errno = 0;
struct sockaddr_un addr;
const struct sockaddr *sa = (struct sockaddr *)&addr;
native_sockaddr(env, &addr, path, len);
int err = native_sockaddr(env, &addr, path, len);
if (err)
return err;
int res = bind(fd, sa, sizeof(struct sockaddr_un));
THROW_ON_ERROR(res)
}
Expand Down Expand Up @@ -85,7 +92,9 @@ Java_org_scalasbt_ipcsocket_JNIUnixDomainSocketLibraryProvider_connectNative(
UNUSED JNIEnv *env, UNUSED jclass clazz, jint fd, jbyteArray path,
jint len) {
struct sockaddr_un addr;
native_sockaddr(env, &addr, path, len);
int err = native_sockaddr(env, &addr, path, len);
if (err)
return err;
errno = 0;
int res = connect(fd, (struct sockaddr *)&addr, sizeof(struct sockaddr_un));
THROW_ON_ERROR(res);
Expand Down
18 changes: 17 additions & 1 deletion src/main/java/org/scalasbt/ipcsocket/UnixDomainSocket.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,13 @@ public UnixDomainSocket(String path, boolean useJNI) throws IOException {
provider.socket(
UnixDomainSocketLibrary.PF_LOCAL, UnixDomainSocketLibrary.SOCK_STREAM, 0));
int socketFd = fd.get();
provider.connect(socketFd, path.getBytes(), path.length());
byte[] address = path.getBytes();
try {
provider.connect(socketFd, address, address.length);
} catch (NativeErrorException e) {
provider.close(socketFd);
throw e;
}
this.fd = new ReferenceCountedFileDescriptor(socketFd, provider);
this.is = new UnixDomainSocketInputStream();
this.os = new UnixDomainSocketOutputStream();
Expand Down Expand Up @@ -123,6 +129,14 @@ public void close() throws IOException {
fd.close();
}

// The JNI provider passes off and len to native code unchecked
private static void checkBounds(byte[] b, int off, int len) {
if (off < 0 || len < 0 || len > b.length - off) {
throw new IndexOutOfBoundsException(
"off " + off + ", len " + len + ", array length " + b.length);
}
}

private class UnixDomainSocketInputStream extends InputStream {
public int available() throws IOException {
int socketFd = fd.acquire();
Expand All @@ -148,6 +162,7 @@ public int read() throws IOException {
}

public int read(byte[] b, int off, int len) throws IOException {
checkBounds(b, off, len);
if (len == 0) {
return 0;
}
Expand Down Expand Up @@ -189,6 +204,7 @@ public void write(int b) throws IOException {
}

public void write(byte[] b, int off, int len) throws IOException {
checkBounds(b, off, len);
if (len == 0) {
return;
}
Expand Down
69 changes: 69 additions & 0 deletions src/test/java/org/scalasbt/ipcsocket/UnixSocketPathTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package org.scalasbt.ipcsocket;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.file.Files;
import java.nio.file.Path;
import org.junit.Test;
import static org.junit.Assert.*;

public class UnixSocketPathTest extends BaseSocketSetup {
private static String repeat(char c, int n) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < n; i++) sb.append(c);
return sb.toString();
}

@Test
public void tooLongPathIsAnError() throws IOException {
if (isWin) return;
String path = "/tmp/" + repeat('x', 300) + ".sock";
try {
newServerSocket(path).close();
fail("bind accepted a path longer than sun_path");
} catch (IOException e) {
}
try {
newClientSocket(path).close();
fail("connect accepted a path longer than sun_path");
} catch (IOException e) {
}
}

@Test
public void nonAsciiPath() throws IOException {
if (isWin) return;
Path dir = Files.createTempDirectory("ipcsocket-j\u00fcrgen");
String path = dir.resolve("s\u00f6ck").toString();
try {
ServerSocket server = newServerSocket(path);
try (Socket client = newClientSocket(path);
Socket peer = server.accept()) {
client.getOutputStream().write(42);
assertEquals(42, peer.getInputStream().read());
} finally {
server.close();
}
} finally {
Files.deleteIfExists(dir.resolve("s\u00f6ck"));
Files.deleteIfExists(dir);
}
}

@Test
public void outOfBoundsWriteIsAnError() throws IOException, InterruptedException {
if (isWin) return;
withSocket(
path -> {
ServerSocket server = newServerSocket(path);
try (Socket client = newClientSocket(path)) {
client.getOutputStream().write(new byte[4], 2, 100);
fail("write sent bytes past the end of the array");
} catch (IndexOutOfBoundsException e) {
} finally {
server.close();
}
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package org.scalasbt.ipcsocket;

public class UnixSocketPathTestJNI extends UnixSocketPathTest {
@Override
boolean useJNI() {
return true;
}
}
Loading