Replace throwErrnoException with JNIHelp jniThrowException
Rather than adding throwErrnoException local function, using standard
jniThrowException in JNIHelp.h.
Also improve the readability:
1. Use throwIOException for
jniThrowExceptionFmt(env, "java/io/IOException", ...);
2. Use throwSoecktException for
jniThrowExceptionFmt(env, "java/net/SocketException", ...);
then the code can fit to one line.
Test: atest TetheringPrivilegedTests (ErrnoException is
test in BpfMapTest)
Change-Id: I396771e2c68e319f510d7a4ea5f263d18d7fad9d
diff --git a/Tethering/jni/android_net_util_TetheringUtils.cpp b/Tethering/jni/android_net_util_TetheringUtils.cpp
index 27c84cf..2e76501 100644
--- a/Tethering/jni/android_net_util_TetheringUtils.cpp
+++ b/Tethering/jni/android_net_util_TetheringUtils.cpp
@@ -34,6 +34,10 @@
static const uint32_t kIPv6PayloadStart = sizeof(ip6_hdr);
static const uint32_t kICMPv6TypeOffset = kIPv6PayloadStart + offsetof(icmp6_hdr, icmp6_type);
+static void throwSocketException(JNIEnv *env, const char* msg, int error) {
+ jniThrowExceptionFmt(env, "java/net/SocketException", "%s: %s", msg, strerror(error));
+}
+
static void android_net_util_setupIcmpFilter(JNIEnv *env, jobject javaFd, uint32_t type) {
sock_filter filter_code[] = {
// Check header is ICMPv6.
@@ -56,8 +60,7 @@
int fd = netjniutils::GetNativeFileDescriptor(env, javaFd);
if (setsockopt(fd, SOL_SOCKET, SO_ATTACH_FILTER, &filter, sizeof(filter)) != 0) {
- jniThrowExceptionFmt(env, "java/net/SocketException",
- "setsockopt(SO_ATTACH_FILTER): %s", strerror(errno));
+ throwSocketException(env, "setsockopt(SO_ATTACH_FILTER)", errno);
}
}
@@ -84,8 +87,7 @@
ICMP6_FILTER_SETPASS(ND_ROUTER_SOLICIT, &rs_only);
socklen_t len = sizeof(rs_only);
if (setsockopt(fd, IPPROTO_ICMPV6, ICMP6_FILTER, &rs_only, len) != 0) {
- jniThrowExceptionFmt(env, "java/net/SocketException",
- "setsockopt(ICMP6_FILTER): %s", strerror(errno));
+ throwSocketException(env, "setsockopt(ICMP6_FILTER)", errno);
return;
}
@@ -97,8 +99,7 @@
int hops = kLinkLocalHopLimit;
len = sizeof(hops);
if (setsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &hops, len) != 0) {
- jniThrowExceptionFmt(env, "java/net/SocketException",
- "setsockopt(IPV6_MULTICAST_HOPS): %s", strerror(errno));
+ throwSocketException(env, "setsockopt(IPV6_MULTICAST_HOPS)", errno);
return;
}
@@ -106,8 +107,7 @@
hops = kLinkLocalHopLimit;
len = sizeof(hops);
if (setsockopt(fd, IPPROTO_IPV6, IPV6_UNICAST_HOPS, &hops, len) != 0) {
- jniThrowExceptionFmt(env, "java/net/SocketException",
- "setsockopt(IPV6_UNICAST_HOPS): %s", strerror(errno));
+ throwSocketException(env, "setsockopt(IPV6_UNICAST_HOPS)", errno);
return;
}
@@ -115,16 +115,14 @@
int off = 0;
len = sizeof(off);
if (setsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_LOOP, &off, len) != 0) {
- jniThrowExceptionFmt(env, "java/net/SocketException",
- "setsockopt(IPV6_MULTICAST_LOOP): %s", strerror(errno));
+ throwSocketException(env, "setsockopt(IPV6_MULTICAST_LOOP)", errno);
return;
}
// Specify the IPv6 interface to use for outbound multicast.
len = sizeof(ifIndex);
if (setsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_IF, &ifIndex, len) != 0) {
- jniThrowExceptionFmt(env, "java/net/SocketException",
- "setsockopt(IPV6_MULTICAST_IF): %s", strerror(errno));
+ throwSocketException(env, "setsockopt(IPV6_MULTICAST_IF)", errno);
return;
}
@@ -144,8 +142,7 @@
auto sa = reinterpret_cast<const struct sockaddr *>(&sin6);
len = sizeof(sin6);
if (bind(fd, sa, len) != 0) {
- jniThrowExceptionFmt(env, "java/net/SocketException",
- "bind(IN6ADDR_ANY): %s", strerror(errno));
+ throwSocketException(env, "bind(IN6ADDR_ANY)", errno);
return;
}
@@ -156,8 +153,7 @@
};
len = sizeof(all_rtrs);
if (setsockopt(fd, IPPROTO_IPV6, IPV6_JOIN_GROUP, &all_rtrs, len) != 0) {
- jniThrowExceptionFmt(env, "java/net/SocketException",
- "setsockopt(IPV6_JOIN_GROUP): %s", strerror(errno));
+ throwSocketException(env, "setsockopt(IPV6_JOIN_GROUP)", errno);
return;
}
}