2013-07-03 02:52:38 +00:00
|
|
|
/* Copyright (c) 2008-2013, Avian Contributors
|
2010-06-14 22:09:56 +00:00
|
|
|
|
|
|
|
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. */
|
|
|
|
|
2011-06-01 19:56:03 +00:00
|
|
|
#include "jni.h"
|
2010-06-14 22:09:56 +00:00
|
|
|
#include "jni-util.h"
|
|
|
|
|
|
|
|
#ifdef PLATFORM_WINDOWS
|
|
|
|
# include <winsock2.h>
|
2010-07-02 15:36:55 +00:00
|
|
|
# define ONLY_ON_WINDOWS(x) x
|
2010-06-14 22:09:56 +00:00
|
|
|
#else
|
|
|
|
# include <netdb.h>
|
2012-07-18 00:21:17 +00:00
|
|
|
# include <sys/socket.h>
|
|
|
|
# include <netinet/in.h>
|
2010-07-02 15:36:55 +00:00
|
|
|
# define ONLY_ON_WINDOWS(x)
|
2010-06-14 22:09:56 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
extern "C" JNIEXPORT void JNICALL
|
2010-07-02 15:36:55 +00:00
|
|
|
Java_java_net_Socket_init(JNIEnv* ONLY_ON_WINDOWS(e), jclass)
|
2010-06-14 22:09:56 +00:00
|
|
|
{
|
|
|
|
#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");
|
2010-09-14 01:50:24 +00:00
|
|
|
} else {
|
|
|
|
wsaInitialized = true;
|
2010-06-14 22:09:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#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;
|
|
|
|
}
|