summaryrefslogtreecommitdiff
path: root/native/jni/native-lib
diff options
context:
space:
mode:
Diffstat (limited to 'native/jni/native-lib')
-rw-r--r--native/jni/native-lib/.cvsignore8
-rw-r--r--native/jni/native-lib/Makefile.am12
-rw-r--r--native/jni/native-lib/cpio.c461
-rw-r--r--native/jni/native-lib/cpio.h84
-rw-r--r--native/jni/native-lib/cpnative.h49
-rw-r--r--native/jni/native-lib/cpnet.c718
-rw-r--r--native/jni/native-lib/cpnet.h208
-rw-r--r--native/jni/native-lib/cpproc.c136
-rw-r--r--native/jni/native-lib/cpproc.h52
9 files changed, 1728 insertions, 0 deletions
diff --git a/native/jni/native-lib/.cvsignore b/native/jni/native-lib/.cvsignore
new file mode 100644
index 000000000..e9f2658a6
--- /dev/null
+++ b/native/jni/native-lib/.cvsignore
@@ -0,0 +1,8 @@
+*.o
+*.a
+*.lo
+*.la
+.libs
+.deps
+Makefile
+Makefile.in
diff --git a/native/jni/native-lib/Makefile.am b/native/jni/native-lib/Makefile.am
new file mode 100644
index 000000000..beab77e57
--- /dev/null
+++ b/native/jni/native-lib/Makefile.am
@@ -0,0 +1,12 @@
+noinst_LTLIBRARIES = libclasspathnative.la
+libclasspathnative_la_SOURCES = cpnet.c \
+ cpnet.h \
+ cpio.c \
+ cpio.h \
+ cpnative.h \
+ cpproc.h \
+ cpproc.c
+
+AM_LDFLAGS = @CLASSPATH_MODULE@
+AM_CPPFLAGS = @CLASSPATH_INCLUDES@
+AM_CFLAGS = @WARNING_CFLAGS@ @STRICT_WARNING_CFLAGS@ @ERROR_CFLAGS@
diff --git a/native/jni/native-lib/cpio.c b/native/jni/native-lib/cpio.c
new file mode 100644
index 000000000..eb544dc83
--- /dev/null
+++ b/native/jni/native-lib/cpio.c
@@ -0,0 +1,461 @@
+/* cpio.c - Common java file IO native functions
+ Copyright (C) 2005 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+/* do not move; needed here because of some macro definitions */
+#include <config.h>
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <errno.h>
+#include <string.h>
+#include <sys/types.h>
+#include <dirent.h>
+
+#include <jni.h>
+
+#if defined(HAVE_SYS_IOCTL_H)
+#define BSD_COMP /* Get FIONREAD on Solaris2 */
+#include <sys/ioctl.h>
+#endif
+#if defined(HAVE_SYS_FILIO_H) /* Get FIONREAD on Solaris 2.5 */
+#include <sys/filio.h>
+#endif
+
+#if defined(HAVE_SYS_STAT_H)
+#include <sys/stat.h>
+#endif
+
+#if defined(HAVE_FCNTL_H)
+#include <fcntl.h>
+#endif
+
+#if defined(HAVE_UNISTD_H)
+#include <unistd.h>
+#endif
+
+#if defined(HAVE_SYS_SELECT_H)
+#include <sys/select.h>
+#endif
+
+#include <utime.h>
+
+#include "cpnative.h"
+#include "cpio.h"
+
+/* Some POSIX systems don't have O_SYNC and O_DYSNC so we define them here. */
+#if !defined (O_SYNC) && defined (O_FSYNC)
+#define O_SYNC O_FSYNC
+#endif
+#if !defined (O_DSYNC) && defined (O_FSYNC)
+#define O_DSYNC O_FSYNC
+#endif
+/* If O_DSYNC is still not defined, use O_SYNC (needed for newlib). */
+#if !defined (O_DSYNC)
+#define O_DSYNC O_SYNC
+#endif
+
+JNIEXPORT int cpio_openFile (const char *filename, int *fd, int flags, int permissions)
+{
+ int sflags = 0;
+ int rwflags = flags & CPFILE_FLAG_READWRITE;
+ int perms;
+
+ if (flags & CPFILE_FLAG_CREATE)
+ sflags |= O_CREAT;
+ if (flags & CPFILE_FLAG_APPEND)
+ sflags |= O_APPEND;
+ if (flags & CPFILE_FLAG_TRUNCATE)
+ sflags |= O_TRUNC;
+ if (flags & CPFILE_FLAG_SYNC)
+ sflags |= O_SYNC;
+ if (flags & CPFILE_FLAG_DSYNC)
+ sflags |= O_DSYNC;
+#if defined(O_BINARY)
+ if (flags & CPFILE_FLAG_BINARY)
+ sflags |= O_BINARY;
+#endif
+
+ switch (rwflags)
+ {
+ case CPFILE_FLAG_READ:
+ sflags |= O_RDONLY;
+ break;
+ case CPFILE_FLAG_WRITE:
+ sflags |= O_WRONLY;
+ break;
+ case CPFILE_FLAG_READWRITE:
+ sflags |= O_RDWR;
+ break;
+ }
+
+ if (permissions == CPFILE_PERMISSION_NORMAL)
+ perms = (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
+ else
+ perms = 0;
+
+ *fd = open (filename, sflags, perms);
+
+ if (*fd < 0)
+ return errno;
+
+ return CPNATIVE_OK;
+}
+
+JNIEXPORT int cpio_closeFile (int fd)
+{
+ if (close (fd) < 0)
+ return errno;
+
+ return CPNATIVE_OK;
+}
+
+JNIEXPORT int cpio_availableBytes (int fd, jlong *bytes_available)
+{
+#if defined (FIONREAD)
+ ssize_t n;
+
+ if (ioctl (fd, FIONREAD, (char *)&n) != 0)
+ return errno;
+
+ *bytes_available = n;
+ return CPNATIVE_OK;
+#elif defined(HAVE_FSTAT)
+ struct stat statBuffer;
+ off_t n;
+ int result;
+
+ *bytes_available = 0
+ if ((fstat (fd, &statBuffer) == 0) && S_ISREG (statBuffer.st_mode))
+ {
+ n = lseek (fd, 0, SEEK_CUR);
+ if (n != -1)
+ {
+ *bytes_available = statBuffer.st_size - n;
+ result = 0;
+ }
+ else
+ {
+ result = errno;
+ }
+ }
+ else
+ {
+ result = errno;
+ }
+
+ return result;
+#elif defined(HAVE_SELECT)
+ fd_set filedescriptset;
+ struct timeval tv;
+ int result;
+
+ *bytes_available = 0;
+
+ FD_ZERO (&filedescriptset);
+ FD_SET (fd,&filedescriptset);
+ memset (&tv, 0, sizeof(tv));
+
+ switch (select (fd+1, &filedescriptset, NULL, NULL, &timeval)) \
+ {
+ case -1:
+ result=errno;
+ break;
+ case 0:
+ *bytes_available = 0;
+ result = CPNATIVE_OK;
+ break;
+ default:
+ *bytes_available = 1;
+ result = CPNATIVE_OK;
+ break;
+ }
+ return result;
+
+#else
+ *bytes_available = 0;
+ return ENOTSUP;
+#endif
+}
+
+JNIEXPORT int cpio_getFileSize (int fd, jlong *filesize)
+{
+ struct stat statBuffer;
+
+ if (fstat(fd, &statBuffer) < 0)
+ return errno;
+
+ *filesize = statBuffer.st_size;
+ return CPNATIVE_OK;
+}
+
+JNIEXPORT int cpio_getFilePosition (int fd, jlong *offset)
+{
+ *offset = lseek (fd, 0, SEEK_CUR);
+ if (*offset < 0)
+ return errno;
+
+ return CPNATIVE_OK;
+}
+
+JNIEXPORT int cpio_setFilePosition (int fd, jlong position)
+{
+ if (lseek (fd, position, SEEK_SET) < 0)
+ return errno;
+
+ return CPNATIVE_OK;
+}
+
+JNIEXPORT int cpio_read (int fd, void *buffer, jint length, jint *bytes_read)
+{
+ *bytes_read = read (fd, buffer, length);
+
+ if (*bytes_read < 0)
+ {
+ return errno;
+ }
+
+ return CPNATIVE_OK;
+}
+
+JNIEXPORT int cpio_write (int fd, const void *buffer, jint length, jint *bytes_written)
+{
+ *bytes_written = write (fd, buffer, length);
+
+ if (*bytes_written < 0)
+ return errno;
+
+ return CPNATIVE_OK;
+}
+
+JNIEXPORT int cpio_fsync (int fd)
+{
+ if (fsync (fd) < 0)
+ return errno;
+
+ return CPNATIVE_OK;
+}
+
+JNIEXPORT int cpio_truncate (int fd, jlong size)
+{
+ if (ftruncate (fd, size) < 0)
+ return errno;
+
+ return CPNATIVE_OK;
+}
+
+JNIEXPORT int cpio_setFileSize (int native_fd, jlong new_size)
+{
+ jlong file_size;
+ jlong save_offset;
+ int result;
+ char data;
+ jint bytes_written;
+
+ result = cpio_getFileSize (native_fd, &file_size);
+ if (result != CPNATIVE_OK)
+ return result;
+
+ /* Save off current position */
+ result = cpio_getFilePosition (native_fd, &save_offset);
+ if (result != CPNATIVE_OK)
+ return result;
+
+ if (file_size < new_size)
+ {
+ /* File is too short -- seek to one byte short of where we want,
+ * then write a byte */
+
+ /* move to position n-1 */
+ result = cpio_setFilePosition (native_fd, new_size-1);
+ if (result != CPNATIVE_OK)
+ return result;
+
+ /* write a byte
+ Note: This will fail if we somehow get here in read only mode
+ * That shouldn't happen */
+ data = '\0';
+ result = cpio_write (native_fd, &data, 1, &bytes_written);
+ if (result != CPNATIVE_OK)
+ return result;
+
+ /* Reposition file pointer to where we started if not beyond new len. */
+ if (save_offset < new_size)
+ {
+ result = cpio_setFilePosition (native_fd, save_offset);
+ if (result != CPNATIVE_OK)
+ return result;
+ }
+ }
+ else if (new_size < file_size)
+ {
+ /* File is too long - use ftruncate if available */
+ result = cpio_truncate (native_fd, new_size);
+ if (result != CPNATIVE_OK)
+ return result;
+
+ /* Reposition file pointer when it now is beyond the end of file. */
+ if (new_size < save_offset)
+ {
+ result = cpio_setFilePosition (native_fd, new_size);
+ if (result != CPNATIVE_OK)
+ return result;
+ }
+ }
+
+ return CPNATIVE_OK;
+}
+
+int cpio_setFileReadonly (const char *filename)
+{
+ struct stat statbuf;
+
+ if (stat(filename, &statbuf) < 0)
+ return errno;
+
+ if (chmod(filename, statbuf.st_mode & ~(S_IWRITE | S_IWGRP | S_IWOTH)) < 0)
+ return errno;
+
+ return 0;
+}
+
+int cpio_isFileExists (const char *filename)
+{
+ struct stat statbuf;
+
+ if (stat(filename, &statbuf) < 0)
+ {
+ return errno;
+ }
+
+ return 0;
+}
+
+int cpio_checkType (const char *filename, jint *entryType)
+{
+ struct stat statbuf;
+
+ if (stat(filename, &statbuf) < 0)
+ return errno;
+
+ if (S_ISDIR(statbuf.st_mode))
+ *entryType = CPFILE_DIRECTORY;
+ else
+ *entryType = CPFILE_FILE;
+
+ return 0;
+}
+
+int cpio_getModificationTime (const char *filename, jlong *mtime)
+{
+ struct stat statbuf;
+
+ if (stat(filename, &statbuf) < 0)
+ return errno;
+
+ *mtime = (jlong)statbuf.st_mtime * (jlong)1000;
+
+ return 0;
+}
+
+int cpio_setModificationTime (const char *filename, jlong mtime)
+{
+ struct stat statbuf;
+ struct utimbuf buf;
+
+ if (stat(filename, &statbuf) < 0)
+ return errno;
+
+ buf.actime = statbuf.st_atime;
+ buf.modtime = mtime / 1000;
+
+ if (utime(filename, &buf) < 0)
+ return errno;
+
+ return 0;
+}
+
+int cpio_removeFile (const char *filename)
+{
+ if (unlink(filename) < 0 && rmdir(filename) < 0)
+ return errno;
+
+ return 0;
+}
+
+int cpio_mkdir (const char *path)
+{
+ if (mkdir(path, S_IRWXU | S_IRWXG | S_IRWXO) < 0)
+ return errno;
+
+ return 0;
+}
+
+int cpio_rename (const char *old_name, const char *new_name)
+{
+ if (rename(old_name, new_name) < 0)
+ return errno;
+
+ return 0;
+}
+
+int cpio_openDir (const char *dirname, void **handle)
+{
+ *handle = (void *)opendir(dirname);
+ if (*handle == NULL)
+ return errno;
+
+ return 0;
+}
+
+int cpio_closeDir (void *handle)
+{
+ closedir((DIR *)handle);
+ return 0;
+}
+
+
+int cpio_readDir (void *handle, const char **filename)
+{
+ struct dirent *dBuf;
+
+ dBuf = readdir((DIR *)handle);
+ if (dBuf == NULL)
+ return errno;
+
+ *filename = dBuf->d_name;
+ return 0;
+}
diff --git a/native/jni/native-lib/cpio.h b/native/jni/native-lib/cpio.h
new file mode 100644
index 000000000..1776b199d
--- /dev/null
+++ b/native/jni/native-lib/cpio.h
@@ -0,0 +1,84 @@
+/* cpio.h -
+ Copyright (C) 2003, 2004, 2005 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+#ifndef _CLASSPATH_IO_H_INCLUDED
+#define _CLASSPATH_IO_H_INCLUDED
+
+#include <jni.h>
+
+#define CPFILE_FLAG_CREATE 0x0001
+#define CPFILE_FLAG_APPEND 0x0002
+#define CPFILE_FLAG_TRUNCATE 0x0004
+#define CPFILE_FLAG_SYNC 0x0008
+#define CPFILE_FLAG_DSYNC 0x0010
+#define CPFILE_FLAG_BINARY 0x0020
+#define CPFILE_FLAG_READ 0x0040
+#define CPFILE_FLAG_WRITE 0x0080
+
+#define CPFILE_PERMISSION_NORMAL 1
+
+#define CPFILE_FLAG_READWRITE (CPFILE_FLAG_READ|CPFILE_FLAG_WRITE)
+
+JNIEXPORT int cpio_openFile (const char *filename, int *fd, int flags, int permissions);
+JNIEXPORT int cpio_closeFile (int fd);
+JNIEXPORT int cpio_availableBytes (int fd, jlong *avail);
+JNIEXPORT int cpio_getFileSize (int fd, jlong *filesize);
+JNIEXPORT int cpio_setFileSize (int fd, jlong filesize);
+JNIEXPORT int cpio_getFilePosition (int fd, jlong *position);
+JNIEXPORT int cpio_setFilePosition (int fd, jlong position);
+JNIEXPORT int cpio_read (int fd, void *data, jint len, jint *bytes_read);
+JNIEXPORT int cpio_write (int fd, const void *data, jint len, jint *bytes_written);
+JNIEXPORT int cpio_fsync (int fd);
+JNIEXPORT int cpio_truncate (int fd, jlong size);
+
+#define CPFILE_FILE 0
+#define CPFILE_DIRECTORY 1
+
+JNIEXPORT int cpio_setFileReadonly (const char *filename);
+JNIEXPORT int cpio_isFileExists (const char *filename);
+JNIEXPORT int cpio_checkType (const char *filename, jint *entryType);
+JNIEXPORT int cpio_getModificationTime (const char *filename, jlong *mtime);
+JNIEXPORT int cpio_setModificationTime (const char *filename, jlong mtime);
+JNIEXPORT int cpio_removeFile (const char *filename);
+JNIEXPORT int cpio_mkdir (const char *filename);
+JNIEXPORT int cpio_rename (const char *old_name, const char *new_name);
+
+JNIEXPORT int cpio_openDir (const char *dirname, void **handle);
+JNIEXPORT int cpio_closeDir (void *handle);
+JNIEXPORT int cpio_readDir (void *handle, const char **filename);
+
+#endif
diff --git a/native/jni/native-lib/cpnative.h b/native/jni/native-lib/cpnative.h
new file mode 100644
index 000000000..4ba772603
--- /dev/null
+++ b/native/jni/native-lib/cpnative.h
@@ -0,0 +1,49 @@
+/* cpnative.h -
+ Copyright (C) 2003, 2004, 2005 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+#ifndef _CLASSPATH_NATIVE_H_INCLUDED
+#define _CLASSPATH_NATIVE_H_INCLUDED
+
+#include <errno.h>
+#include <string.h>
+
+#define CPNATIVE_OK 0
+#define CPNATIVE_EINTR EINTR
+
+#define cpnative_getErrorString strerror
+
+#endif
diff --git a/native/jni/native-lib/cpnet.c b/native/jni/native-lib/cpnet.c
new file mode 100644
index 000000000..c112ac2a7
--- /dev/null
+++ b/native/jni/native-lib/cpnet.c
@@ -0,0 +1,718 @@
+/* cpnet.c -
+ Copyright (C) 2003, 2004, 2005, 2006 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+#include "config.h"
+#include <jni.h>
+#include <assert.h>
+#include <sys/socket.h>
+#include <sys/types.h>
+#include <unistd.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <netinet/in.h>
+#include <netinet/tcp.h>
+#include <netdb.h>
+#include <sys/ioctl.h>
+#include <sys/time.h>
+#include <unistd.h>
+
+#include "cpnet.h"
+
+#define SOCKET_DEFAULT_TIMEOUT -1 /* milliseconds */
+
+#if defined (HAVE_MSG_NOSIGNAL)
+#define SOCKET_NOSIGNAL MSG_NOSIGNAL
+#elif defined (HAVE_SO_NOSIGPIPE)
+#define SOCKET_NOSIGNAL SO_NOSIGPIPE
+#else
+#error "No suitable flag found to ommit a SIGPIPE on signal errors with send()."
+#endif
+
+static int socketTimeouts[FD_SETSIZE];
+
+static jint waitForWritable(jint fd)
+{
+ struct timeval tv;
+ fd_set writeset;
+ int ret;
+
+
+ FD_ZERO(&writeset);
+ FD_SET(fd, &writeset);
+ if (socketTimeouts[fd] > 0)
+ {
+ tv.tv_sec = socketTimeouts[fd] / 1000;
+ tv.tv_usec = (socketTimeouts[fd] % 1000) * 1000;
+ ret = select(fd+1, NULL, &writeset, NULL, &tv);
+ }
+ else
+ ret = select(fd+1, NULL, &writeset, NULL, NULL);
+
+ return (ret <= 0) ? -1 : 0;
+}
+
+static jint waitForReadable(jint fd)
+{
+ struct timeval tv;
+ fd_set readset;
+ int ret;
+
+
+ FD_ZERO(&readset);
+ FD_SET(fd, &readset);
+ if (socketTimeouts[fd] > 0)
+ {
+ tv.tv_sec = socketTimeouts[fd] / 1000;
+ tv.tv_usec = (socketTimeouts[fd] % 1000) * 1000;
+ ret = select(fd+1, &readset, NULL, NULL, &tv);
+ }
+ else
+ ret = select(fd+1, &readset, NULL, NULL, NULL);
+
+ return (ret <= 0) ? -1 : 0;
+}
+
+jint cpnet_openSocketStream(JNIEnv *env UNUSED, jint *fd, jint family)
+{
+ *fd = socket(family, SOCK_STREAM, 0);
+ if (*fd == -1)
+ return errno;
+
+ fcntl(*fd, F_SETFD, FD_CLOEXEC);
+ assert(*fd < FD_SETSIZE);
+ socketTimeouts[*fd] = SOCKET_DEFAULT_TIMEOUT;
+ return 0;
+}
+
+jint cpnet_openSocketDatagram(JNIEnv *env UNUSED, jint *fd, jint family)
+{
+ *fd = socket(family, SOCK_DGRAM, 0);
+ if (*fd == -1)
+ return errno;
+
+ fcntl(*fd, F_SETFD, FD_CLOEXEC);
+ assert(*fd < FD_SETSIZE);
+ socketTimeouts[*fd] = SOCKET_DEFAULT_TIMEOUT;
+ return 0;
+}
+
+jint cpnet_shutdown (JNIEnv *env UNUSED, jint fd, jbyte flag)
+{
+ int ret;
+ int shut_flag = 0;
+
+ if (flag == CPNET_SHUTDOWN_READ)
+ shut_flag = SHUT_RD;
+ else if (flag == CPNET_SHUTDOWN_WRITE)
+ shut_flag = SHUT_WR;
+
+ ret = shutdown (fd, shut_flag);
+ if (ret != 0)
+ return errno;
+ return 0;
+}
+
+jint cpnet_close(JNIEnv *env UNUSED, jint fd)
+{
+ if (close (fd) != 0)
+ return errno;
+ return 0;
+}
+
+jint cpnet_listen(JNIEnv *env UNUSED, jint fd, jint queuelen)
+{
+ if (listen (fd, queuelen) != 0)
+ return errno;
+ return 0;
+}
+
+jint cpnet_accept(JNIEnv *env UNUSED, jint fd, jint *newfd)
+{
+ if (waitForReadable (fd) < 0)
+ return ETIMEDOUT;
+
+ *newfd = accept(fd, NULL, 0);
+ if (*newfd != 0)
+ return errno;
+
+ return 0;
+}
+
+jint cpnet_bind(JNIEnv *env UNUSED, jint fd, cpnet_address *addr)
+{
+ int ret;
+
+ ret = bind(fd, (struct sockaddr *)addr->data, addr->len);
+ if (ret != 0)
+ return errno;
+
+ return 0;
+}
+
+jint cpnet_connect(JNIEnv *env UNUSED, jint fd, cpnet_address *addr)
+{
+ int ret;
+
+ /* TODO: implement socket time out */
+ ret = connect(fd, (struct sockaddr *)addr->data, addr->len);
+ if (ret != 0)
+ return errno;
+
+ return 0;
+}
+
+jint cpnet_getLocalAddr(JNIEnv *env, jint fd, cpnet_address **addr)
+{
+ socklen_t slen = 1024;
+ int ret;
+
+ *addr = JCL_malloc(env, slen);
+
+ slen -= sizeof(jint);
+ ret = getsockname(fd, (struct sockaddr *)(*addr)->data, &slen );
+ if (ret != 0)
+ {
+ int err = errno;
+ JCL_free(env, *addr);
+ return err;
+ }
+
+ (*addr)->len = slen;
+
+ return 0;
+}
+
+jint cpnet_getRemoteAddr(JNIEnv *env, jint fd, cpnet_address **addr)
+{
+ socklen_t slen = 1024;
+ int ret;
+
+ *addr = JCL_malloc(env, slen);
+
+ slen -= sizeof(jint);
+ ret = getpeername(fd, (struct sockaddr *)(*addr)->data, &slen );
+ if (ret != 0)
+ {
+ int err = errno;
+ JCL_free(env, *addr);
+ return err;
+ }
+
+ (*addr)->len = slen;
+
+ return 0;
+}
+
+jint cpnet_setBroadcast(JNIEnv *env UNUSED, jint fd, jint flag)
+{
+ int ret;
+
+ ret = setsockopt(fd, SOL_SOCKET, SO_BROADCAST, &flag, sizeof(flag));
+ if (ret != 0)
+ return errno;
+
+ return 0;
+}
+
+jint cpnet_send (JNIEnv *env UNUSED, jint fd, jbyte *data, jint len, jint *bytes_sent)
+{
+ ssize_t ret;
+
+ if (waitForWritable(fd) < 0)
+ return ETIMEDOUT;
+
+ ret = send(fd, data, len, SOCKET_NOSIGNAL);
+ if (ret < 0)
+ return errno;
+
+ *bytes_sent = ret;
+
+ return 0;
+}
+
+jint cpnet_sendTo (JNIEnv *env UNUSED, jint fd, jbyte *data, jint len, cpnet_address *addr, jint *bytes_sent)
+{
+ ssize_t ret;
+
+ if (waitForWritable(fd) < 0)
+ return ETIMEDOUT;
+
+ ret = sendto(fd, data, len, SOCKET_NOSIGNAL, (struct sockaddr *)addr->data,
+ addr->len);
+ if (ret < 0)
+ return errno;
+
+ *bytes_sent = ret;
+ return 0;
+}
+
+jint cpnet_recv (JNIEnv *env UNUSED, jint fd, jbyte *data, jint len, jint *bytes_recv)
+{
+ ssize_t ret;
+
+ if (waitForReadable(fd) < 0)
+ return ETIMEDOUT;
+
+ ret = recv(fd, data, len, 0);
+ if (ret < 0)
+ return errno;
+
+ *bytes_recv = ret;
+
+ return 0;
+}
+
+jint cpnet_recvFrom (JNIEnv *env, jint fd, jbyte *data, jint len, cpnet_address **addr, jint *bytes_recv)
+{
+ socklen_t slen = 1024;
+ ssize_t ret;
+
+ if (waitForReadable(fd) < 0)
+ return ETIMEDOUT;
+
+ *addr = JCL_malloc(env, slen);
+
+ slen -= sizeof(jint);
+ ret = recvfrom(fd, data, len, 0, (struct sockaddr *) (*addr)->data, &slen);
+ if (ret < 0)
+ {
+ int err = errno;
+ JCL_free(env, *addr);
+ return err;
+ }
+
+ (*addr)->len = slen;
+ *bytes_recv = ret;
+
+ return 0;
+}
+
+jint cpnet_setSocketTCPNoDelay (JNIEnv *env UNUSED, jint fd, jint nodelay)
+{
+ socklen_t len = sizeof(jint);
+ int ret;
+
+ ret = setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &nodelay, len);
+ if (ret < 0)
+ return errno;
+
+ return 0;
+}
+
+jint cpnet_getSocketTCPNoDelay (JNIEnv *env UNUSED, jint fd, jint *nodelay)
+{
+ socklen_t len = sizeof(jint);
+ int ret;
+
+ ret = getsockopt(fd, IPPROTO_TCP, TCP_NODELAY, nodelay, &len);
+ if (ret < 0)
+ return errno;
+
+ return 0;
+}
+
+jint cpnet_setLinger (JNIEnv *env UNUSED, jint fd, jint flag, jint value)
+{
+ socklen_t len = sizeof(struct linger);
+ int ret;
+ struct linger __linger;
+
+ if (flag)
+ {
+ __linger.l_onoff = 0;
+ }
+ else
+ {
+ __linger.l_linger = value;
+ __linger.l_onoff = 1;
+ }
+
+ ret = setsockopt(fd, SOL_SOCKET, SO_LINGER, &__linger, len);
+ if (ret < 0)
+ return errno;
+
+ return 0;
+}
+
+jint cpnet_getLinger (JNIEnv *env UNUSED, jint fd, jint *flag, jint *value)
+{
+ socklen_t slen = sizeof(struct linger);
+ struct linger __linger;
+ int ret;
+
+ ret = getsockopt(fd, SOL_SOCKET, SO_LINGER, &__linger, &slen);
+ if (ret != 0)
+ return errno;
+
+ *flag = __linger.l_onoff;
+ *value = __linger.l_linger;
+
+ return ret;
+}
+
+jint cpnet_setSocketTimeout (JNIEnv *env UNUSED, jint fd, jint value)
+{
+ socketTimeouts[fd] = value;
+ return 0;
+}
+
+jint cpnet_getSocketTimeout (JNIEnv *env UNUSED, jint fd, jint *value)
+{
+ *value = socketTimeouts[fd];
+ return 0;
+}
+
+jint cpnet_setSendBuf (JNIEnv *env UNUSED, jint fd, jint value)
+{
+ int ret;
+
+ ret = setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &value, sizeof(value));
+ if (ret != 0)
+ return errno;
+
+ return 0;
+}
+
+jint cpnet_getSendBuf (JNIEnv *env UNUSED, jint fd, jint *value)
+{
+ int ret;
+ socklen_t slen = sizeof(*value);
+
+ ret = getsockopt(fd, SOL_SOCKET, SO_SNDBUF, value, &slen);
+ if (ret != 0)
+ return errno;
+
+ return 0;
+}
+
+jint cpnet_setRecvBuf (JNIEnv *env UNUSED, jint fd, jint value)
+{
+ int ret;
+
+ ret = setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &value, sizeof(value));
+ if (ret != 0)
+ return errno;
+
+ return 0;
+}
+
+jint cpnet_getRecvBuf (JNIEnv *env UNUSED, jint fd, jint *value)
+{
+ int ret;
+ socklen_t slen = sizeof(*value);
+
+ ret = getsockopt(fd, SOL_SOCKET, SO_RCVBUF, value, &slen);
+ if (ret != 0)
+ return errno;
+
+ return 0;
+}
+
+jint cpnet_setTTL (JNIEnv *env UNUSED, jint fd, jint value)
+{
+ int ret;
+
+ ret = setsockopt(fd, IPPROTO_IP, IP_TTL, &value, sizeof(value));
+ if (ret != 0)
+ return errno;
+
+ return 0;
+}
+
+jint cpnet_getTTL (JNIEnv *env UNUSED, jint fd, jint *value)
+{
+ int ret;
+ socklen_t slen = sizeof(*value);
+
+ ret = getsockopt(fd, IPPROTO_IP, IP_TTL, value, &slen);
+ if (ret != 0)
+ return errno;
+
+ return 0;
+}
+
+jint cpnet_setMulticastIF (JNIEnv *env UNUSED, jint fd, cpnet_address *addr)
+{
+ int ret;
+
+ ret = setsockopt(fd, IPPROTO_IP, IP_MULTICAST_IF, (struct sockaddr *)addr->data, addr->len);
+ if (ret != 0)
+ return errno;
+
+ return 0;
+}
+
+jint cpnet_getMulticastIF (JNIEnv *env, jint fd, cpnet_address **addr)
+{
+ socklen_t slen = 1024;
+ int ret;
+
+ *addr = JCL_malloc(env, slen);
+
+ slen -= sizeof(jint);
+ ret = getsockopt(fd, IPPROTO_IP, IP_MULTICAST_IF, (struct sockaddr *)(*addr)->data, &slen);
+ (*addr)->len = slen;
+
+ if (ret != 0)
+ return errno;
+
+ return 0;
+}
+
+jint cpnet_setReuseAddress (JNIEnv *env UNUSED, jint fd, jint reuse)
+{
+ int ret;
+
+ ret = setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse));
+ if (ret != 0)
+ return errno;
+
+ return 0;
+}
+
+jint cpnet_getReuseAddress (JNIEnv *env UNUSED, jint fd, jint *reuse)
+{
+ int ret;
+ socklen_t slen = sizeof(*reuse);
+
+ ret = getsockopt(fd, SOL_SOCKET, SO_REUSEADDR, reuse, &slen);
+ if (ret != 0)
+ return errno;
+
+ return 0;
+}
+
+jint cpnet_setKeepAlive (JNIEnv *env UNUSED, jint fd, jint keep)
+{
+ int ret;
+
+ ret = setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &keep, sizeof(keep));
+ if (ret != 0)
+ return errno;
+
+ return 0;
+}
+
+jint cpnet_getKeepAlive (JNIEnv *env UNUSED, jint fd, jint *keep)
+{
+ int ret;
+ socklen_t slen = sizeof(*keep);
+
+ ret = getsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, keep, &slen);
+ if (ret != 0)
+ return errno;
+
+ return 0;
+}
+
+jint cpnet_addMembership (JNIEnv *env UNUSED, jint fd, cpnet_address *addr)
+{
+ struct ip_mreq req;
+ int ret;
+
+ memset(&req, 0, sizeof(req));
+ req.imr_multiaddr = ((struct sockaddr_in *)addr->data)->sin_addr;
+ req.imr_interface.s_addr = INADDR_ANY;
+ ret = setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &req, sizeof(req));
+ if (ret != 0)
+ return errno;
+
+ return 0;
+}
+
+jint cpnet_dropMembership (JNIEnv *env UNUSED, jint fd, cpnet_address *addr)
+{
+ struct ip_mreq req;
+ int ret;
+
+ memset(&req, 0, sizeof(req));
+ req.imr_multiaddr = ((struct sockaddr_in *)addr->data)->sin_addr;
+ req.imr_interface.s_addr = INADDR_ANY;
+ ret = setsockopt(fd, IPPROTO_IP, IP_DROP_MEMBERSHIP, &req, sizeof(req));
+ if (ret != 0)
+ return errno;
+
+ return 0;
+}
+
+jint cpnet_getAvailableBytes (JNIEnv *env UNUSED, jint fd, jint *availableBytes)
+{
+ int ret;
+
+ ret = ioctl(fd, FIONREAD, availableBytes);
+ if (ret != 0)
+ return errno;
+
+ return 0;
+}
+
+jint cpnet_getHostname (JNIEnv *env UNUSED, char *hostname, jint hostname_len)
+{
+ int ret;
+
+ ret = gethostname(hostname, hostname_len);
+ if (ret != 0)
+ return errno;
+
+ hostname[hostname_len-1] = 0;
+ return 0;
+}
+
+jint cpnet_getHostByName (JNIEnv *env, const char *hostname, cpnet_address ***addresses, jint *addresses_count)
+{
+ struct hostent hret;
+ struct hostent *result;
+ jint buflen = 1024;
+ int herr;
+ int ret;
+ int counter = 0;
+ cpnet_address **addr_arr;
+ int i;
+ char *buf;
+
+ do
+ {
+ buf = (char *)JCL_malloc(env, buflen);
+#ifdef HAVE_GETHOSTBYNAME_R
+ ret = gethostbyname_r (hostname, &hret, buf, buflen, &result, &herr);
+#else
+ ret = gethostbyname (hostname);
+#endif
+ if (ret != 0 || result == NULL)
+ {
+ if (herr == ERANGE)
+ {
+ buflen *= 2;
+ JCL_free(env, buf);
+ continue;
+ }
+ JCL_free(env, buf);
+
+ return -herr;
+ }
+
+ break;
+ }
+ while (1);
+
+ while (hret.h_addr_list[counter] != NULL)
+ counter++;
+
+ *addresses_count = counter;
+ addr_arr = *addresses = JCL_malloc(env, sizeof(cpnet_address *) * counter);
+ switch (hret.h_addrtype)
+ {
+ case AF_INET:
+ for (i = 0; i < counter; i++)
+ {
+ addr_arr[i] = cpnet_newIPV4Address(env);
+ cpnet_bytesToIPV4Address(addr_arr[i], (jbyte *)hret.h_addr_list[i]);
+ }
+ break;
+ case AF_INET6:
+ for (i = 0; i < counter; i++)
+ {
+ addr_arr[i] = cpnet_newIPV6Address(env);
+ cpnet_bytesToIPV6Address(addr_arr[i], (jbyte *)hret.h_addr_list[i]);
+ }
+ break;
+ default:
+ *addresses_count = 0;
+ JCL_free(env, addr_arr);
+ break;
+ }
+
+ JCL_free(env, buf);
+
+ return 0;
+}
+
+jint cpnet_getHostByAddr (JNIEnv *env UNUSED, cpnet_address *addr, char *hostname, jint hostname_len)
+{
+ union
+ {
+ struct sockaddr_in *addr_v4;
+ struct sockaddr_in6 *addr_v6;
+ char *data;
+ } haddr;
+ void *raw_addr;
+ int addr_type;
+ struct hostent *ret;
+ int addr_len;
+
+ haddr.data = addr->data;
+
+ if (haddr.addr_v4->sin_family == AF_INET)
+ {
+ raw_addr = &haddr.addr_v4->sin_addr;
+ addr_len = sizeof(haddr.addr_v4->sin_addr);
+ addr_type = AF_INET;
+ }
+ else if (haddr.addr_v6->sin6_family == AF_INET6)
+ {
+ raw_addr = &haddr.addr_v6->sin6_addr;
+ addr_type = AF_INET6;
+ addr_len = sizeof(haddr.addr_v6->sin6_addr);
+ }
+ else
+ return EINVAL;
+
+ /* Here we do not have any thread safe call. VM implementors will have to
+ * do a big lock. Or it should be put on the Classpath VM interface.
+ */
+ ret = gethostbyaddr(raw_addr, addr_len, addr_type);
+ if (ret == NULL)
+ {
+ /* The trouble here is how to distinguish the two cases ? */
+ if (h_errno != 0)
+ return h_errno;
+ else
+ return errno;
+
+ }
+ strncpy(hostname, ret->h_name, hostname_len);
+
+ return 0;
+}
+
+void cpnet_freeAddresses(JNIEnv * env, cpnet_address **addr, jint addresses_count)
+{
+ jint i;
+
+ for (i = 0; i < addresses_count; i++)
+ cpnet_freeAddress(env, addr[i]);
+}
diff --git a/native/jni/native-lib/cpnet.h b/native/jni/native-lib/cpnet.h
new file mode 100644
index 000000000..0c7c215f8
--- /dev/null
+++ b/native/jni/native-lib/cpnet.h
@@ -0,0 +1,208 @@
+/* cpnet.h -
+ Copyright (C) 2003, 2004, 2005, 2006 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+#ifndef _CLASSPATH_NET_H_INCLUDED
+#define _CLASSPATH_NET_H_INCLUDED
+
+#include <jni.h>
+#include <jcl.h>
+#include <string.h>
+
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <netinet/ip.h>
+
+typedef struct {
+ jint len;
+ char data[1];
+} cpnet_address;
+
+#define CPNET_SHUTDOWN_READ 1
+#define CPNET_SHUTDOWN_WRITE 2
+
+JNIEXPORT jint cpnet_openSocketStream(JNIEnv *env, jint *fd, jint family);
+JNIEXPORT jint cpnet_openSocketDatagram(JNIEnv *env, jint *fd, jint family);
+JNIEXPORT jint cpnet_shutdown (JNIEnv *env, jint fd, jbyte flag);
+JNIEXPORT jint cpnet_close(JNIEnv *env, jint fd);
+JNIEXPORT jint cpnet_listen(JNIEnv *env, jint fd, jint queuelen);
+JNIEXPORT jint cpnet_accept(JNIEnv *env, jint fd, jint *newfd);
+JNIEXPORT jint cpnet_bind(JNIEnv *env, jint fd, cpnet_address *addr);
+JNIEXPORT jint cpnet_connect(JNIEnv *env, jint fd, cpnet_address *addr);
+JNIEXPORT jint cpnet_getLocalAddr(JNIEnv *env, jint fd, cpnet_address **addr);
+JNIEXPORT jint cpnet_getRemoteAddr(JNIEnv *env, jint fd, cpnet_address **addr);
+JNIEXPORT jint cpnet_setBroadcast(JNIEnv *env, jint fd, jint flag);
+JNIEXPORT jint cpnet_send (JNIEnv *env, jint fd, jbyte *data, jint len, jint *bytes_sent);
+JNIEXPORT jint cpnet_sendTo (JNIEnv *env, jint fd, jbyte *data, jint len, cpnet_address *addr, jint *bytes_sent);
+JNIEXPORT jint cpnet_recv (JNIEnv *env, jint fd, jbyte *data, jint len, jint *bytes_recv);
+JNIEXPORT jint cpnet_recvFrom (JNIEnv *env, jint fd, jbyte *data, jint len, cpnet_address **addr, jint *bytes_recv);
+JNIEXPORT jint cpnet_setSocketTCPNoDelay (JNIEnv *env, jint fd, jint nodelay);
+JNIEXPORT jint cpnet_getSocketTCPNoDelay (JNIEnv *env, jint fd, jint *nodelay);
+JNIEXPORT jint cpnet_setLinger (JNIEnv *env, jint fd, jint flag, jint value);
+JNIEXPORT jint cpnet_getLinger (JNIEnv *env, jint fd, jint *flag, jint *value);
+JNIEXPORT jint cpnet_setSocketTimeout (JNIEnv *env, jint fd, jint value);
+JNIEXPORT jint cpnet_getSocketTimeout (JNIEnv *env, jint fd, jint *value);
+JNIEXPORT jint cpnet_setSendBuf (JNIEnv *env, jint fd, jint value);
+JNIEXPORT jint cpnet_getSendBuf (JNIEnv *env, jint fd, jint *value);
+JNIEXPORT jint cpnet_setRecvBuf (JNIEnv *env, jint fd, jint value);
+JNIEXPORT jint cpnet_getRecvBuf (JNIEnv *env, jint fd, jint *value);
+JNIEXPORT jint cpnet_setTTL (JNIEnv *env, jint fd, jint value);
+JNIEXPORT jint cpnet_getTTL (JNIEnv *env, jint fd, jint *value);
+JNIEXPORT jint cpnet_setMulticastIF (JNIEnv *env, jint fd, cpnet_address *addr);
+JNIEXPORT jint cpnet_getMulticastIF (JNIEnv *env, jint fd, cpnet_address **addr);
+JNIEXPORT jint cpnet_setReuseAddress (JNIEnv *env, jint fd, jint reuse);
+JNIEXPORT jint cpnet_getReuseAddress (JNIEnv *env, jint fd, jint *reuse);
+JNIEXPORT jint cpnet_setKeepAlive (JNIEnv *env, jint fd, jint keep);
+JNIEXPORT jint cpnet_getKeepAlive (JNIEnv *env, jint fd, jint *keep);
+JNIEXPORT jint cpnet_getBindAddress (JNIEnv *env, jint fd, cpnet_address **addr);
+JNIEXPORT jint cpnet_addMembership (JNIEnv *env, jint fd, cpnet_address *addr);
+JNIEXPORT jint cpnet_dropMembership (JNIEnv *env, jint fd, cpnet_address *addr);
+JNIEXPORT jint cpnet_getAvailableBytes (JNIEnv *env, jint fd, jint *availableBytes);
+JNIEXPORT jint cpnet_getHostname (JNIEnv *env, char *hostname, jint hostname_len);
+JNIEXPORT jint cpnet_getHostByName (JNIEnv *env, const char *hostname, cpnet_address ***adresses, jint *addresses_count);
+JNIEXPORT jint cpnet_getHostByAddr (JNIEnv *env, cpnet_address *addr, char *hostname, jint hostname_len);
+JNIEXPORT void cpnet_freeAddresses(JNIEnv * env, cpnet_address **addr, jint addresses_count);
+
+static inline cpnet_address *cpnet_newIPV4Address(JNIEnv * env)
+{
+ cpnet_address *addr = (cpnet_address *)JCL_malloc(env, sizeof(cpnet_address) + sizeof(struct sockaddr_in));
+ struct sockaddr_in *netaddr = (struct sockaddr_in *)&(addr->data[0]);
+
+ addr->len = sizeof(struct sockaddr_in);
+ memset(netaddr, 0, addr->len);
+ netaddr->sin_family = AF_INET;
+ return addr;
+}
+
+static inline void cpnet_setIPV4Any(cpnet_address *addr)
+{
+ struct sockaddr_in *netaddr = (struct sockaddr_in *)&(addr->data[0]);
+
+ netaddr->sin_addr.s_addr = INADDR_ANY;
+}
+
+static inline cpnet_address *cpnet_newIPV6Address(JNIEnv * env)
+{
+ cpnet_address * addr = (cpnet_address *)JCL_malloc(env, sizeof(cpnet_address) + sizeof(struct sockaddr_in6));
+ struct sockaddr_in6 *netaddr = (struct sockaddr_in6 *)&(addr->data[0]);
+
+ addr->len = sizeof(struct sockaddr_in6);
+ memset(netaddr, 0, addr->len);
+ netaddr->sin6_family = AF_INET6;
+
+ return addr;
+}
+
+static inline void cpnet_freeAddress(JNIEnv * env, cpnet_address *addr)
+{
+ JCL_free(env, addr);
+}
+
+static inline void cpnet_addressSetPort(cpnet_address *addr, jint port)
+{
+ struct sockaddr_in *ipaddr = (struct sockaddr_in *)&(addr->data[0]);
+
+ ipaddr->sin_port = htons(port);
+}
+
+static inline jint cpnet_addressGetPort(cpnet_address *addr)
+{
+ struct sockaddr_in *ipaddr = (struct sockaddr_in *)&(addr->data[0]);
+
+ return ntohs(ipaddr->sin_port);
+}
+
+static inline jboolean cpnet_isAddressEqual(cpnet_address *addr1, cpnet_address *addr2)
+{
+ if (addr1->len != addr2->len)
+ return JNI_FALSE;
+
+ return memcmp(addr1->data, addr2->data, addr1->len) == 0;
+}
+
+static inline jboolean cpnet_isIPV6Address(cpnet_address *addr)
+{
+ struct sockaddr_in *ipaddr = (struct sockaddr_in *)&(addr->data[0]);
+
+ return ipaddr->sin_family == AF_INET6;
+}
+
+static inline jboolean cpnet_isIPV4Address(cpnet_address *addr)
+{
+ struct sockaddr_in *ipaddr = (struct sockaddr_in *)&(addr->data[0]);
+
+ return ipaddr->sin_family == AF_INET;
+}
+
+static inline void cpnet_IPV4AddressToBytes(cpnet_address *netaddr, jbyte *octets)
+{
+ struct sockaddr_in *ipaddr = (struct sockaddr_in *)&(netaddr->data[0]);
+ unsigned long sysaddr = ntohl(ipaddr->sin_addr.s_addr);
+
+ octets[0] = ((sysaddr >> 24) & 0xff);
+ octets[1] = ((sysaddr >> 16) & 0xff);
+ octets[2] = ((sysaddr >> 8) & 0xff);
+ octets[3] = (sysaddr & 0xff);
+}
+
+static inline void cpnet_bytesToIPV4Address(cpnet_address *netaddr, jbyte *octets)
+{
+ jint sysaddr;
+ struct sockaddr_in *ipaddr = (struct sockaddr_in *)&(netaddr->data[0]);
+
+ sysaddr = ((jint)(unsigned char)octets[0]) << 24;
+ sysaddr |= ((jint)(unsigned char)octets[1]) << 16;
+ sysaddr |= ((jint)(unsigned char)octets[2]) << 8;
+ sysaddr |= ((jint)(unsigned char)octets[3]);
+
+ ipaddr->sin_addr.s_addr = htonl(sysaddr);
+}
+
+static inline void cpnet_IPV6AddressToBytes(cpnet_address *netaddr, jbyte *octets)
+{
+ struct sockaddr_in6 *ipaddr = (struct sockaddr_in6 *)&(netaddr->data[0]);
+
+ memcpy(octets, &ipaddr->sin6_addr, 16);
+}
+
+static inline void cpnet_bytesToIPV6Address(cpnet_address *netaddr, jbyte *octets)
+{
+ struct sockaddr_in6 *ipaddr = (struct sockaddr_in6 *)&(netaddr->data[0]);
+
+ memcpy(&ipaddr->sin6_addr, octets, 16);
+}
+
+#endif
diff --git a/native/jni/native-lib/cpproc.c b/native/jni/native-lib/cpproc.c
new file mode 100644
index 000000000..b6e9030b5
--- /dev/null
+++ b/native/jni/native-lib/cpproc.c
@@ -0,0 +1,136 @@
+/* cpproc.c -
+ Copyright (C) 2003, 2004, 2005, 2006 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+#include "config.h"
+#include <jni.h>
+#include "cpproc.h"
+#include <signal.h>
+#include <sys/types.h>
+#include <sys/wait.h>
+#include <unistd.h>
+#include <errno.h>
+#include <stdlib.h>
+
+static void close_all_fds(int *fds, int numFds)
+{
+ int i;
+
+ for (i = 0; i < numFds; i++)
+ close(fds[i]);
+}
+
+int cpproc_forkAndExec (char * const *commandLine, char * const * newEnviron,
+ int *fds, int pipe_count, pid_t *out_pid, const char *wd)
+{
+ int local_fds[6];
+ int i;
+ pid_t pid;
+
+ for (i = 0; i < (pipe_count * 2); i += 2)
+ {
+ if (pipe(&local_fds[i]) < 0)
+ {
+ int err = errno;
+
+ close_all_fds(local_fds, i);
+
+ return err;
+ }
+ }
+
+ pid = fork();
+
+ switch (pid)
+ {
+ case 0:
+ dup2(local_fds[0], 0);
+ dup2(local_fds[3], 1);
+ if (pipe_count == 3)
+ dup2(local_fds[5], 2);
+ else
+ dup2(1, 2);
+
+ close_all_fds(local_fds, pipe_count * 2);
+
+ chdir(wd);
+ if (newEnviron == NULL)
+ execvp(commandLine[0], commandLine);
+ else
+ execve(commandLine[0], commandLine, newEnviron);
+
+ abort();
+
+ break;
+ case -1:
+ {
+ int err = errno;
+
+ close_all_fds(local_fds, pipe_count * 2);
+ return err;
+ }
+ default:
+ close(local_fds[0]);
+ close(local_fds[3]);
+ if (pipe_count == 3)
+ close(local_fds[5]);
+
+ fds[0] = local_fds[1];
+ fds[1] = local_fds[2];
+ fds[2] = local_fds[4];
+ *out_pid = pid;
+ return 0;
+ }
+}
+
+int cpproc_waitpid (pid_t pid, int *status, pid_t *outpid, int options)
+{
+ pid_t wp = waitpid(pid, status, options);
+
+ if (wp < 0)
+ return errno;
+
+ *outpid = wp;
+ return 0;
+}
+
+int cpproc_kill (pid_t pid, int signal)
+{
+ if (kill(pid, signal) < 0)
+ return errno;
+
+ return 0;
+}
diff --git a/native/jni/native-lib/cpproc.h b/native/jni/native-lib/cpproc.h
new file mode 100644
index 000000000..5e8db5800
--- /dev/null
+++ b/native/jni/native-lib/cpproc.h
@@ -0,0 +1,52 @@
+/* cpproc.h -
+ Copyright (C) 2006 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+#ifndef _CLASSPATH_PROC_H_INCLUDED
+#define _CLASSPATH_PROC_H_INCLUDED
+
+#include <sys/types.h>
+
+#define CPIO_EXEC_STDIN 0
+#define CPIO_EXEC_STDOUT 1
+#define CPIO_EXEC_STDERR 2
+#define CPIO_EXEC_NUM_PIPES 3
+
+JNIEXPORT int cpproc_forkAndExec (char * const *commandLine, char * const * newEnviron,
+ int *fds, int pipe_count, pid_t *pid, const char *wd);
+JNIEXPORT int cpproc_waitpid (pid_t pid, int *status, pid_t *outpid, int options);
+JNIEXPORT int cpproc_kill (pid_t pid, int signal);
+
+#endif