summaryrefslogtreecommitdiffstats
path: root/luni
diff options
context:
space:
mode:
authorLorenzo Colitti <lorenzo@google.com>2015-03-16 11:12:52 +0000
committerAndroid Git Automerger <android-git-automerger@android.com>2015-03-16 11:12:52 +0000
commit357c5b8fdf4ab505bb41ffaa2492da837231dfb8 (patch)
tree1350f5d55d1ddbc6b9b12e9e2b20fef423f740f2 /luni
parent1461a444bc7ef309a2499485199303e7e93bfdd0 (diff)
parent51e455d0366267008c3efe1ca9ca7563f5a2bcfc (diff)
downloadlibcore-357c5b8fdf4ab505bb41ffaa2492da837231dfb8.zip
libcore-357c5b8fdf4ab505bb41ffaa2492da837231dfb8.tar.gz
libcore-357c5b8fdf4ab505bb41ffaa2492da837231dfb8.tar.bz2
am 51e455d0: am 8bce9ae7: Merge "Add a version of sendto that takes a SocketAddress."
* commit '51e455d0366267008c3efe1ca9ca7563f5a2bcfc': Add a version of sendto that takes a SocketAddress.
Diffstat (limited to 'luni')
-rw-r--r--luni/src/main/java/libcore/io/ForwardingOs.java1
-rw-r--r--luni/src/main/java/libcore/io/Os.java1
-rw-r--r--luni/src/main/java/libcore/io/Posix.java4
-rw-r--r--luni/src/main/native/libcore_io_Posix.cpp19
-rw-r--r--luni/src/test/java/libcore/io/OsTest.java19
5 files changed, 44 insertions, 0 deletions
diff --git a/luni/src/main/java/libcore/io/ForwardingOs.java b/luni/src/main/java/libcore/io/ForwardingOs.java
index d07b99a..5c0c8fd 100644
--- a/luni/src/main/java/libcore/io/ForwardingOs.java
+++ b/luni/src/main/java/libcore/io/ForwardingOs.java
@@ -135,6 +135,7 @@ public class ForwardingOs implements Os {
public long sendfile(FileDescriptor outFd, FileDescriptor inFd, MutableLong inOffset, long byteCount) throws ErrnoException { return os.sendfile(outFd, inFd, inOffset, byteCount); }
public int sendto(FileDescriptor fd, ByteBuffer buffer, int flags, InetAddress inetAddress, int port) throws ErrnoException, SocketException { return os.sendto(fd, buffer, flags, inetAddress, port); }
public int sendto(FileDescriptor fd, byte[] bytes, int byteOffset, int byteCount, int flags, InetAddress inetAddress, int port) throws ErrnoException, SocketException { return os.sendto(fd, bytes, byteOffset, byteCount, flags, inetAddress, port); }
+ public int sendto(FileDescriptor fd, byte[] bytes, int byteOffset, int byteCount, int flags, SocketAddress address) throws ErrnoException, SocketException { return os.sendto(fd, bytes, byteOffset, byteCount, flags, address); }
public void setegid(int egid) throws ErrnoException { os.setegid(egid); }
public void setenv(String name, String value, boolean overwrite) throws ErrnoException { os.setenv(name, value, overwrite); }
public void seteuid(int euid) throws ErrnoException { os.seteuid(euid); }
diff --git a/luni/src/main/java/libcore/io/Os.java b/luni/src/main/java/libcore/io/Os.java
index 3fc031c..987d331 100644
--- a/luni/src/main/java/libcore/io/Os.java
+++ b/luni/src/main/java/libcore/io/Os.java
@@ -127,6 +127,7 @@ public interface Os {
public void rename(String oldPath, String newPath) throws ErrnoException;
public int sendto(FileDescriptor fd, ByteBuffer buffer, int flags, InetAddress inetAddress, int port) throws ErrnoException, SocketException;
public int sendto(FileDescriptor fd, byte[] bytes, int byteOffset, int byteCount, int flags, InetAddress inetAddress, int port) throws ErrnoException, SocketException;
+ public int sendto(FileDescriptor fd, byte[] bytes, int byteOffset, int byteCount, int flags, SocketAddress address) throws ErrnoException, SocketException;
public long sendfile(FileDescriptor outFd, FileDescriptor inFd, MutableLong inOffset, long byteCount) throws ErrnoException;
public void setegid(int egid) throws ErrnoException;
public void setenv(String name, String value, boolean overwrite) throws ErrnoException;
diff --git a/luni/src/main/java/libcore/io/Posix.java b/luni/src/main/java/libcore/io/Posix.java
index 251d06f..d680200 100644
--- a/luni/src/main/java/libcore/io/Posix.java
+++ b/luni/src/main/java/libcore/io/Posix.java
@@ -208,7 +208,11 @@ public final class Posix implements Os {
// This indirection isn't strictly necessary, but ensures that our public interface is type safe.
return sendtoBytes(fd, bytes, byteOffset, byteCount, flags, inetAddress, port);
}
+ public int sendto(FileDescriptor fd, byte[] bytes, int byteOffset, int byteCount, int flags, SocketAddress address) throws ErrnoException, SocketException {
+ return sendtoBytes(fd, bytes, byteOffset, byteCount, flags, address);
+ }
private native int sendtoBytes(FileDescriptor fd, Object buffer, int byteOffset, int byteCount, int flags, InetAddress inetAddress, int port) throws ErrnoException, SocketException;
+ private native int sendtoBytes(FileDescriptor fd, Object buffer, int byteOffset, int byteCount, int flags, SocketAddress address) throws ErrnoException, SocketException;
public native void setegid(int egid) throws ErrnoException;
public native void setenv(String name, String value, boolean overwrite) throws ErrnoException;
public native void seteuid(int euid) throws ErrnoException;
diff --git a/luni/src/main/native/libcore_io_Posix.cpp b/luni/src/main/native/libcore_io_Posix.cpp
index 0f12e06..f41ffe6 100644
--- a/luni/src/main/native/libcore_io_Posix.cpp
+++ b/luni/src/main/native/libcore_io_Posix.cpp
@@ -38,6 +38,7 @@
#include <net/if.h>
#include <netdb.h>
#include <netinet/in.h>
+#include <netpacket/packet.h>
#include <poll.h>
#include <pwd.h>
#include <signal.h>
@@ -1401,6 +1402,23 @@ static jint Posix_sendtoBytes(JNIEnv* env, jobject, jobject javaFd, jobject java
NULL_ADDR_OK, bytes.get() + byteOffset, byteCount, flags);
}
+static jint Posix_sendtoBytesSocketAddress(JNIEnv* env, jobject, jobject javaFd, jobject javaBytes, jint byteOffset, jint byteCount, jint flags, jobject javaSocketAddress) {
+ ScopedBytesRO bytes(env, javaBytes);
+ if (bytes.get() == NULL) {
+ return -1;
+ }
+
+ sockaddr_storage ss;
+ socklen_t sa_len;
+ if (!javaSocketAddressToSockaddr(env, javaSocketAddress, ss, sa_len)) {
+ return -1;
+ }
+
+ const sockaddr* sa = reinterpret_cast<const sockaddr*>(&ss);
+ // We don't need the return value because we'll already have thrown.
+ return NET_FAILURE_RETRY(env, ssize_t, sendto, javaFd, bytes.get() + byteOffset, byteCount, flags, sa, sa_len);
+}
+
static void Posix_setegid(JNIEnv* env, jobject, jint egid) {
throwIfMinusOne(env, "setegid", TEMP_FAILURE_RETRY(setegid(egid)));
}
@@ -1784,6 +1802,7 @@ static JNINativeMethod gMethods[] = {
NATIVE_METHOD(Posix, rename, "(Ljava/lang/String;Ljava/lang/String;)V"),
NATIVE_METHOD(Posix, sendfile, "(Ljava/io/FileDescriptor;Ljava/io/FileDescriptor;Landroid/util/MutableLong;J)J"),
NATIVE_METHOD(Posix, sendtoBytes, "(Ljava/io/FileDescriptor;Ljava/lang/Object;IIILjava/net/InetAddress;I)I"),
+ NATIVE_METHOD_OVERLOAD(Posix, sendtoBytes, "(Ljava/io/FileDescriptor;Ljava/lang/Object;IIILjava/net/SocketAddress;)I", SocketAddress),
NATIVE_METHOD(Posix, setegid, "(I)V"),
NATIVE_METHOD(Posix, setenv, "(Ljava/lang/String;Ljava/lang/String;Z)V"),
NATIVE_METHOD(Posix, seteuid, "(I)V"),
diff --git a/luni/src/test/java/libcore/io/OsTest.java b/luni/src/test/java/libcore/io/OsTest.java
index e6c946c..5a9d436 100644
--- a/luni/src/test/java/libcore/io/OsTest.java
+++ b/luni/src/test/java/libcore/io/OsTest.java
@@ -18,6 +18,7 @@ package libcore.io;
import android.system.ErrnoException;
import android.system.NetlinkSocketAddress;
+import android.system.StructTimeval;
import android.system.StructUcred;
import java.io.File;
import java.io.FileDescriptor;
@@ -298,6 +299,24 @@ public class OsTest extends TestCase {
checkByteBufferPositions_sendto_recvfrom(AF_INET6, InetAddress.getByName("::1"));
}
+ public void test_sendtoSocketAddress() throws Exception {
+ FileDescriptor recvFd = Libcore.os.socket(AF_INET6, SOCK_DGRAM, 0);
+ Libcore.os.bind(recvFd, InetAddress.getLoopbackAddress(), 0);
+ StructTimeval tv = StructTimeval.fromMillis(20);
+ Libcore.os.setsockoptTimeval(recvFd, SOL_SOCKET, SO_RCVTIMEO, tv);
+
+ InetSocketAddress to = ((InetSocketAddress) Libcore.os.getsockname(recvFd));
+ FileDescriptor sendFd = Libcore.os.socket(AF_INET6, SOCK_DGRAM, 0);
+ byte[] msg = ("Hello, I'm going to a socket address: " + to.toString()).getBytes("UTF-8");
+ int len = msg.length;
+
+ assertEquals(len, Libcore.os.sendto(sendFd, msg, 0, len, 0, to));
+ byte[] received = new byte[msg.length + 42];
+ InetSocketAddress from = new InetSocketAddress();
+ assertEquals(len, Libcore.os.recvfrom(recvFd, received, 0, received.length, 0, from));
+ assertEquals(InetAddress.getLoopbackAddress(), from.getAddress());
+ }
+
public void test_socketFamilies() throws Exception {
FileDescriptor fd = Libcore.os.socket(AF_INET6, SOCK_STREAM, 0);
Libcore.os.bind(fd, InetAddress.getByName("::"), 0);