use of org.graalvm.nativeimage.c.type.CCharPointer in project graal by oracle.
the class Util_jni method lookupAllHostAddr.
/* @formatter:on */
// Do not re-wrap long lines and comments: @formatter:off
@Substitute
@SuppressWarnings({ "static-method" })
public InetAddress[] lookupAllHostAddr(String host) throws UnknownHostException, SocketException, InterruptedException {
CCharPointer hostname;
InetAddress[] ret = null;
int retLen = 0;
int error = 0;
Netdb.addrinfo hints = StackValue.get(SizeOf.get(Netdb.addrinfo.class));
Netdb.addrinfo res = WordFactory.nullPointer();
Netdb.addrinfoPointer resPtr = StackValue.get(SizeOf.get(Netdb.addrinfoPointer.class));
Netdb.addrinfo resNew = WordFactory.nullPointer();
if (host == null) {
throw new NullPointerException("host is null");
}
try (CCharPointerHolder hostPin = CTypeConversion.toCString(host)) {
// "hostname" is pinned through the end of the method.
hostname = hostPin.get();
// #ifdef MACOSX
if (IsDefined.MACOSX()) {
/*
* If we're looking up the local machine, attempt to get the address from
* getifaddrs. This ensures we get an IPv6 address for the local machine.
*/
ret = Util_java_net_Inet6AddressImpl.lookupIfLocalhost(hostname, true);
if (ret != null) {
return ret;
}
}
// #endif MACOSX
/* Try once, with our static buffer. */
LibC.memset(hints, WordFactory.signed(0), SizeOf.unsigned(Netdb.addrinfo.class));
hints.set_ai_flags(Netdb.AI_CANONNAME());
hints.set_ai_family(Socket.AF_UNSPEC());
// #endif
try {
// res needs cleanup at "cleanupAndReturn".
error = Netdb.getaddrinfo(hostname, WordFactory.nullPointer(), hints, resPtr);
if (error != 0) {
throw new UnknownHostException(host);
} else {
res = resPtr.read();
int i = 0;
int inetCount = 0;
int inet6Count = 0;
int inetIndex;
int inet6Index;
Netdb.addrinfo itr;
Netdb.addrinfo last = WordFactory.nullPointer();
Netdb.addrinfo iterator = res;
while (iterator.isNonNull()) {
boolean skip = false;
itr = resNew;
while (itr.isNonNull()) {
if ((iterator.ai_family() == itr.ai_family()) && (iterator.ai_addrlen() == itr.ai_addrlen())) {
if (itr.ai_family() == Socket.AF_INET()) {
NetinetIn.sockaddr_in addr1;
NetinetIn.sockaddr_in addr2;
addr1 = (NetinetIn.sockaddr_in) iterator.ai_addr();
addr2 = (NetinetIn.sockaddr_in) itr.ai_addr();
if (addr1.sin_addr().s_addr() == addr2.sin_addr().s_addr()) {
skip = true;
break;
}
} else {
NetinetIn.sockaddr_in6 addr1;
NetinetIn.sockaddr_in6 addr2;
addr1 = (NetinetIn.sockaddr_in6) iterator.ai_addr();
addr2 = (NetinetIn.sockaddr_in6) itr.ai_addr();
int t;
for (t = 0; t < 16; t++) {
if (addr1.sin6_addr().s6_addr().read(t) != addr2.sin6_addr().s6_addr().read(t)) {
break;
}
}
if (t < 16) {
itr = itr.ai_next();
continue;
} else {
skip = true;
break;
}
}
} else if ((iterator.ai_family() != Socket.AF_INET()) && (iterator.ai_family() != Socket.AF_INET6())) {
/* we can't handle other family types */
skip = true;
break;
}
itr = itr.ai_next();
}
if (!skip) {
Netdb.addrinfo next = LibC.malloc(SizeOf.unsigned(Netdb.addrinfo.class));
if (next.isNull()) {
throw new OutOfMemoryError("malloc failed");
}
LibC.memcpy(next, iterator, SizeOf.unsigned(Netdb.addrinfo.class));
next.set_ai_next(WordFactory.nullPointer());
if (resNew.isNull()) {
resNew = next;
} else {
last.set_ai_next(next);
}
last = next;
i++;
if (iterator.ai_family() == Socket.AF_INET()) {
inetCount++;
} else if (iterator.ai_family() == Socket.AF_INET6()) {
inet6Count++;
}
}
iterator = iterator.ai_next();
}
retLen = i;
iterator = resNew;
ret = new InetAddress[retLen];
if (Target_java_net_InetAddress.preferIPv6Address) {
/* AF_INET addresses will be offset by inet6Count */
inetIndex = inet6Count;
inet6Index = 0;
} else {
/* AF_INET6 addresses will be offset by inetCount */
inetIndex = 0;
inet6Index = inetCount;
}
while (iterator.isNonNull()) {
int ret1;
if (iterator.ai_family() == Socket.AF_INET()) {
Inet4Address iaObj = Util_java_net_Inet4Address.new_Inet4Address();
JavaNetNetUtil.setInetAddress_addr(iaObj, NetinetIn.ntohl(((NetinetIn.sockaddr_in) iterator.ai_addr()).sin_addr().s_addr()));
JavaNetNetUtil.setInetAddress_hostName(iaObj, host);
ret[inetIndex] = iaObj;
inetIndex++;
} else if (iterator.ai_family() == Socket.AF_INET6()) {
// 455 jint scope = 0;
int scope = 0;
// 457 jobject iaObj = (*env)->NewObject(env, ni_ia6cls, ni_ia6ctrID);
Inet6Address iaObj = Util_java_net_Inet6Address.new_Inet6Address();
// 458 if (IS_NULL(iaObj)) {
if (iaObj == null) {
// 459 ret = NULL;
ret = null;
// 460 goto cleanupAndReturn;
return ret;
}
// 462 ret1 = setInet6Address_ipaddress(env, iaObj, (char *)&(((struct sockaddr_in6*)iterator->ai_addr)->sin6_addr));
ret1 = JavaNetNetUtil.setInet6Address_ipaddress(iaObj, ((NetinetIn.sockaddr_in6) iterator.ai_addr()).sin6_addr().s6_addr());
// 463 if (!ret1) {
if (!CTypeConversion.toBoolean(ret1)) {
// 464 ret = NULL;
ret = null;
// 465 goto cleanupAndReturn;
return ret;
}
// 468 scope = ((struct sockaddr_in6*)iterator->ai_addr)->sin6_scope_id;
scope = ((NetinetIn.sockaddr_in6) iterator.ai_addr()).sin6_scope_id();
// 469 if (scope != 0) { /* zero is default value, no need to set */
if (scope != 0) {
/* zero is default value, no need to set */
// 470 setInet6Address_scopeid(env, iaObj, scope);
JavaNetNetUtil.setInet6Address_scopeid(iaObj, scope);
}
// 472 setInetAddress_hostName(env, iaObj, host);
JavaNetNetUtil.setInetAddress_hostName(iaObj, host);
// 473 (*env)->SetObjectArrayElement(env, ret, inet6Index, iaObj);
ret[inet6Index] = iaObj;
// 474 inet6Index++;
inet6Index++;
}
iterator = iterator.ai_next();
}
}
} finally {
/* cleanupAndReturn: */
Netdb.addrinfo iterator;
Netdb.addrinfo tmp;
iterator = resNew;
while (iterator.isNonNull()) {
tmp = iterator;
iterator = iterator.ai_next();
LibC.free(tmp);
}
Netdb.freeaddrinfo(res);
}
// JNU_ReleaseStringPlatformChars(env, host, hostname)
// happens when I exit the CCharPointerHolder region.
}
return ret;
}
use of org.graalvm.nativeimage.c.type.CCharPointer in project graal by oracle.
the class PosixUtils method fileOpen.
static void fileOpen(String path, FileDescriptor fd, int flags) throws FileNotFoundException {
try (CCharPointerHolder pathPin = CTypeConversion.toCString(removeTrailingSlashes(path))) {
CCharPointer pathPtr = pathPin.get();
int handle = open(pathPtr, flags, 0666);
if (handle >= 0) {
Util_java_io_FileDescriptor.setFD(fd, handle);
} else {
throw new FileNotFoundException(path);
}
}
}
use of org.graalvm.nativeimage.c.type.CCharPointer in project graal by oracle.
the class PosixUtils method setLocale.
public static String setLocale(int category, String locale) {
if (locale == null) {
CCharPointer cstrResult = Locale.setlocale(category, WordFactory.nullPointer());
return CTypeConversion.toJavaString(cstrResult);
}
try (CCharPointerHolder localePin = CTypeConversion.toCString(locale)) {
CCharPointer cstrLocale = localePin.get();
CCharPointer cstrResult = Locale.setlocale(category, cstrLocale);
return CTypeConversion.toJavaString(cstrResult);
}
}
use of org.graalvm.nativeimage.c.type.CCharPointer in project graal by oracle.
the class PosixUtils method readBytes.
static int readBytes(byte[] b, int off, int len, FileDescriptor fd) throws IOException {
if (b == null) {
throw new NullPointerException();
} else if (PosixUtils.outOfBounds(off, len, b)) {
throw new IndexOutOfBoundsException();
}
if (len == 0) {
return 0;
}
SignedWord nread;
CCharPointer buf = LibC.malloc(WordFactory.unsigned(len));
try {
if (buf.equal(WordFactory.zero())) {
throw new OutOfMemoryError();
}
int handle = getFDHandle(fd);
nread = read(handle, buf, WordFactory.unsigned(len));
if (nread.greaterThan(0)) {
/*
* We do not read directly into the (pinned) result array because read can block,
* and that could lead to object pinned for an unexpectedly long time.
*/
try (PinnedObject pin = PinnedObject.create(b)) {
LibC.memcpy(pin.addressOfArrayElement(off), buf, (UnsignedWord) nread);
}
} else if (nread.equal(-1)) {
throw new IOException(lastErrorString("Read error"));
} else {
// EOF
nread = WordFactory.signed(-1);
}
} finally {
LibC.free(buf);
}
return (int) nread.rawValue();
}
use of org.graalvm.nativeimage.c.type.CCharPointer in project graal by oracle.
the class PosixUtils method fileClose.
static void fileClose(FileDescriptor fd) throws IOException {
int handle = Util_java_io_FileDescriptor.getFD(fd);
if (handle == -1) {
return;
}
Util_java_io_FileDescriptor.setFD(fd, -1);
// Do not close file descriptors 0, 1, 2. Instead, redirect to /dev/null.
if (handle >= 0 && handle <= 2) {
int devnull;
try (CCharPointerHolder pathPin = CTypeConversion.toCString("/dev/null")) {
CCharPointer pathPtr = pathPin.get();
devnull = open(pathPtr, O_WRONLY(), 0);
}
if (devnull < 0) {
Util_java_io_FileDescriptor.setFD(fd, handle);
throw new IOException(lastErrorString("open /dev/null failed"));
} else {
dup2(devnull, handle);
close(devnull);
}
} else if (close(handle) == -1) {
throw new IOException(lastErrorString("close failed"));
}
}
Aggregations