mirror of
https://github.com/corda/corda.git
synced 2025-01-07 13:38:47 +00:00
Merge branch 'dns'
This commit is contained in:
commit
a3daeee1f6
76
classpath/java-net.cpp
Normal file
76
classpath/java-net.cpp
Normal file
@ -0,0 +1,76 @@
|
|||||||
|
/* Copyright (c) 2010, Avian Contributors
|
||||||
|
|
||||||
|
Permission to use, copy, modify, and/or distribute this software
|
||||||
|
for any purpose with or without fee is hereby granted, provided
|
||||||
|
that the above copyright notice and this permission notice appear
|
||||||
|
in all copies.
|
||||||
|
|
||||||
|
There is NO WARRANTY for this software. See license.txt for
|
||||||
|
details. */
|
||||||
|
|
||||||
|
#include "jni.h"
|
||||||
|
#include "jni-util.h"
|
||||||
|
|
||||||
|
#ifdef PLATFORM_WINDOWS
|
||||||
|
# include <winsock2.h>
|
||||||
|
# define ONLY_ON_WINDOWS(x) x
|
||||||
|
#else
|
||||||
|
# include <netdb.h>
|
||||||
|
# define ONLY_ON_WINDOWS(x)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
extern "C" JNIEXPORT void JNICALL
|
||||||
|
Java_java_net_Socket_init(JNIEnv* ONLY_ON_WINDOWS(e), jclass)
|
||||||
|
{
|
||||||
|
#ifdef PLATFORM_WINDOWS
|
||||||
|
static bool wsaInitialized = false;
|
||||||
|
if (not wsaInitialized) {
|
||||||
|
WSADATA data;
|
||||||
|
int r = WSAStartup(MAKEWORD(2, 2), &data);
|
||||||
|
if (r or LOBYTE(data.wVersion) != 2 or HIBYTE(data.wVersion) != 2) {
|
||||||
|
throwNew(e, "java/io/IOException", "WSAStartup failed");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
extern "C" JNIEXPORT jint JNICALL
|
||||||
|
Java_java_net_InetAddress_ipv4AddressForName(JNIEnv* e,
|
||||||
|
jclass,
|
||||||
|
jstring name)
|
||||||
|
{
|
||||||
|
const char* chars = e->GetStringUTFChars(name, 0);
|
||||||
|
if (chars) {
|
||||||
|
#ifdef PLATFORM_WINDOWS
|
||||||
|
hostent* host = gethostbyname(chars);
|
||||||
|
e->ReleaseStringUTFChars(name, chars);
|
||||||
|
if (host) {
|
||||||
|
return htonl(reinterpret_cast<in_addr*>(host->h_addr_list[0])->s_addr);
|
||||||
|
} else {
|
||||||
|
fprintf(stderr, "trouble %d\n", WSAGetLastError());
|
||||||
|
}
|
||||||
|
#else
|
||||||
|
addrinfo hints;
|
||||||
|
memset(&hints, 0, sizeof(addrinfo));
|
||||||
|
hints.ai_family = AF_INET;
|
||||||
|
hints.ai_socktype = SOCK_STREAM;
|
||||||
|
|
||||||
|
addrinfo* result;
|
||||||
|
int r = getaddrinfo(chars, 0, &hints, &result);
|
||||||
|
e->ReleaseStringUTFChars(name, chars);
|
||||||
|
|
||||||
|
jint address;
|
||||||
|
if (r != 0) {
|
||||||
|
address = 0;
|
||||||
|
} else {
|
||||||
|
address = htonl
|
||||||
|
(reinterpret_cast<sockaddr_in*>(result->ai_addr)->sin_addr.s_addr);
|
||||||
|
|
||||||
|
freeaddrinfo(result);
|
||||||
|
}
|
||||||
|
|
||||||
|
return address;
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
@ -18,6 +18,7 @@
|
|||||||
|
|
||||||
#ifdef PLATFORM_WINDOWS
|
#ifdef PLATFORM_WINDOWS
|
||||||
# include <winsock2.h>
|
# include <winsock2.h>
|
||||||
|
# include <ws2tcpip.h>
|
||||||
# include <errno.h>
|
# include <errno.h>
|
||||||
# ifdef _MSC_VER
|
# ifdef _MSC_VER
|
||||||
# define snprintf sprintf_s
|
# define snprintf sprintf_s
|
||||||
@ -150,20 +151,40 @@ init(JNIEnv* e, sockaddr_in* address, jstring hostString, jint port)
|
|||||||
{
|
{
|
||||||
const char* chars = e->GetStringUTFChars(hostString, 0);
|
const char* chars = e->GetStringUTFChars(hostString, 0);
|
||||||
if (chars) {
|
if (chars) {
|
||||||
|
#ifdef PLATFORM_WINDOWS
|
||||||
hostent* host = gethostbyname(chars);
|
hostent* host = gethostbyname(chars);
|
||||||
e->ReleaseStringUTFChars(hostString, chars);
|
e->ReleaseStringUTFChars(hostString, chars);
|
||||||
if (host == 0) {
|
if (host == 0) {
|
||||||
#ifdef PLATFORM_WINDOWS
|
|
||||||
throwIOException(e);
|
throwIOException(e);
|
||||||
#else
|
|
||||||
throwIOException(e, hstrerror(h_errno));
|
|
||||||
#endif
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
memset(address, 0, sizeof(sockaddr_in));
|
memset(address, 0, sizeof(sockaddr_in));
|
||||||
address->sin_family = AF_INET;
|
address->sin_family = AF_INET;
|
||||||
address->sin_port = htons(port);
|
address->sin_port = htons(port);
|
||||||
address->sin_addr = *reinterpret_cast<in_addr*>(host->h_addr_list[0]);
|
address->sin_addr = *reinterpret_cast<in_addr*>(host->h_addr_list[0]);
|
||||||
|
#else
|
||||||
|
addrinfo hints;
|
||||||
|
memset(&hints, 0, sizeof(addrinfo));
|
||||||
|
hints.ai_family = AF_INET;
|
||||||
|
hints.ai_socktype = SOCK_STREAM;
|
||||||
|
|
||||||
|
addrinfo* result;
|
||||||
|
int r = getaddrinfo(chars, 0, &hints, &result);
|
||||||
|
e->ReleaseStringUTFChars(hostString, chars);
|
||||||
|
if (r != 0) {
|
||||||
|
throwIOException(e, gai_strerror(r));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
memset(address, 0, sizeof(sockaddr_in));
|
||||||
|
address->sin_family = AF_INET;
|
||||||
|
address->sin_port = htons(port);
|
||||||
|
address->sin_addr = reinterpret_cast<sockaddr_in*>
|
||||||
|
(result->ai_addr)->sin_addr;
|
||||||
|
|
||||||
|
freeaddrinfo(result);
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -328,17 +349,6 @@ doWrite(int fd, const void* buffer, size_t count)
|
|||||||
int
|
int
|
||||||
makeSocket(JNIEnv* e)
|
makeSocket(JNIEnv* e)
|
||||||
{
|
{
|
||||||
#ifdef PLATFORM_WINDOWS
|
|
||||||
static bool wsaInitialized = false;
|
|
||||||
if (not wsaInitialized) {
|
|
||||||
WSADATA data;
|
|
||||||
int r = WSAStartup(MAKEWORD(2, 2), &data);
|
|
||||||
if (r or LOBYTE(data.wVersion) != 2 or HIBYTE(data.wVersion) != 2) {
|
|
||||||
throwIOException(e, "WSAStartup failed");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
int s = ::socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
|
int s = ::socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
|
||||||
if (s < 0) {
|
if (s < 0) {
|
||||||
throwIOException(e);
|
throwIOException(e);
|
||||||
|
53
classpath/java/net/InetAddress.java
Normal file
53
classpath/java/net/InetAddress.java
Normal file
@ -0,0 +1,53 @@
|
|||||||
|
/* Copyright (c) 2010, Avian Contributors
|
||||||
|
|
||||||
|
Permission to use, copy, modify, and/or distribute this software
|
||||||
|
for any purpose with or without fee is hereby granted, provided
|
||||||
|
that the above copyright notice and this permission notice appear
|
||||||
|
in all copies.
|
||||||
|
|
||||||
|
There is NO WARRANTY for this software. See license.txt for
|
||||||
|
details. */
|
||||||
|
|
||||||
|
package java.net;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
public class InetAddress {
|
||||||
|
private final String address;
|
||||||
|
|
||||||
|
private InetAddress(String address) {
|
||||||
|
this.address = address;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getHostAddress() {
|
||||||
|
return address;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static InetAddress getByName(String name)
|
||||||
|
throws UnknownHostException
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
Socket.init();
|
||||||
|
} catch (IOException e) {
|
||||||
|
UnknownHostException uhe = new UnknownHostException(name);
|
||||||
|
uhe.initCause(e);
|
||||||
|
throw uhe;
|
||||||
|
}
|
||||||
|
|
||||||
|
int address = ipv4AddressForName(name);
|
||||||
|
if (address == 0) {
|
||||||
|
throw new UnknownHostException(name);
|
||||||
|
} else {
|
||||||
|
return new InetAddress(ipv4AddressToString(address));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static String ipv4AddressToString(int address) {
|
||||||
|
return (((address >>> 24) ) + "." +
|
||||||
|
((address >>> 16) & 0xFF) + "." +
|
||||||
|
((address >>> 8 ) & 0xFF) + "." +
|
||||||
|
((address ) & 0xFF));
|
||||||
|
}
|
||||||
|
|
||||||
|
private static native int ipv4AddressForName(String name);
|
||||||
|
}
|
@ -10,6 +10,10 @@
|
|||||||
|
|
||||||
package java.net;
|
package java.net;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
public abstract class Socket {
|
public abstract class Socket {
|
||||||
|
public static native void init() throws IOException;
|
||||||
|
|
||||||
public abstract void setTcpNoDelay(boolean on) throws SocketException;
|
public abstract void setTcpNoDelay(boolean on) throws SocketException;
|
||||||
}
|
}
|
||||||
|
21
classpath/java/net/UnknownHostException.java
Normal file
21
classpath/java/net/UnknownHostException.java
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
/* Copyright (c) 2010, Avian Contributors
|
||||||
|
|
||||||
|
Permission to use, copy, modify, and/or distribute this software
|
||||||
|
for any purpose with or without fee is hereby granted, provided
|
||||||
|
that the above copyright notice and this permission notice appear
|
||||||
|
in all copies.
|
||||||
|
|
||||||
|
There is NO WARRANTY for this software. See license.txt for
|
||||||
|
details. */
|
||||||
|
|
||||||
|
package java.net;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
public class UnknownHostException extends IOException {
|
||||||
|
public UnknownHostException(String host) {
|
||||||
|
super(host);
|
||||||
|
}
|
||||||
|
|
||||||
|
public UnknownHostException() { }
|
||||||
|
}
|
@ -18,7 +18,7 @@ public abstract class Selector {
|
|||||||
protected final Set<SelectionKey> keys = new HashSet();
|
protected final Set<SelectionKey> keys = new HashSet();
|
||||||
protected final Set<SelectionKey> selectedKeys = new HashSet();
|
protected final Set<SelectionKey> selectedKeys = new HashSet();
|
||||||
|
|
||||||
public static Selector open() {
|
public static Selector open() throws IOException {
|
||||||
return new SocketSelector();
|
return new SocketSelector();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -15,11 +15,16 @@ import java.io.IOException;
|
|||||||
import java.net.InetSocketAddress;
|
import java.net.InetSocketAddress;
|
||||||
import java.net.SocketAddress;
|
import java.net.SocketAddress;
|
||||||
import java.net.ServerSocket;
|
import java.net.ServerSocket;
|
||||||
|
import java.net.Socket;
|
||||||
|
|
||||||
public class ServerSocketChannel extends SelectableChannel {
|
public class ServerSocketChannel extends SelectableChannel {
|
||||||
private final SocketChannel channel = new SocketChannel();
|
private final SocketChannel channel;
|
||||||
|
|
||||||
public static ServerSocketChannel open() {
|
private ServerSocketChannel() throws IOException {
|
||||||
|
channel = new SocketChannel();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static ServerSocketChannel open() throws IOException {
|
||||||
return new ServerSocketChannel();
|
return new ServerSocketChannel();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -58,6 +63,8 @@ public class ServerSocketChannel extends SelectableChannel {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private int doListen(String host, int port) throws IOException {
|
private int doListen(String host, int port) throws IOException {
|
||||||
|
Socket.init();
|
||||||
|
|
||||||
return natDoListen(host, port);
|
return natDoListen(host, port);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -26,7 +26,9 @@ public class SocketChannel extends SelectableChannel
|
|||||||
boolean connected = false;
|
boolean connected = false;
|
||||||
boolean blocking = true;
|
boolean blocking = true;
|
||||||
|
|
||||||
public static SocketChannel open() {
|
public static SocketChannel open() throws IOException {
|
||||||
|
Socket.init();
|
||||||
|
|
||||||
return new SocketChannel();
|
return new SocketChannel();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -12,13 +12,16 @@ package java.nio.channels;
|
|||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
|
import java.net.Socket;
|
||||||
|
|
||||||
class SocketSelector extends Selector {
|
class SocketSelector extends Selector {
|
||||||
protected long state;
|
protected long state;
|
||||||
protected final Object lock = new Object();
|
protected final Object lock = new Object();
|
||||||
protected boolean woken = false;
|
protected boolean woken = false;
|
||||||
|
|
||||||
public SocketSelector() {
|
public SocketSelector() throws IOException {
|
||||||
|
Socket.init();
|
||||||
|
|
||||||
state = natInit();
|
state = natInit();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user