⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 javanet.c

📁 gcc的组建
💻 C
📖 第 1 页 / 共 3 页
字号:
/* javanet.c - Common internal functions for the java.net package   Copyright (C) 1998, 2002, 2004, 2005  Free Software Foundation, Inc.This file is part of GNU Classpath.GNU Classpath is free software; you can redistribute it and/or modifyit under the terms of the GNU General Public License as published bythe 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, butWITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNUGeneral Public License for more details.You should have received a copy of the GNU General Public Licensealong with GNU Classpath; see the file COPYING.  If not, write to theFree Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA02110-1301 USA.Linking this library statically or dynamically with other modules ismaking a combined work based on this library.  Thus, the terms andconditions of the GNU General Public License cover the wholecombination.As a special exception, the copyright holders of this library give youpermission to link this library with independent modules to produce anexecutable, regardless of the license terms of these independentmodules, and to copy and distribute the resulting executable underterms of your choice, provided that you also meet, for each linkedindependent module, the terms and conditions of the license of thatmodule.  An independent module is a module which is not derived fromor based on this library.  If you modify this library, you may extendthis exception to your version of the library, but you are notobligated to do so.  If you do not wish to do so, delete thisexception statement from your version. *//* do not move; needed here because of some macro definitions */#include <config.h>#include <stdlib.h>#include <stdio.h>#include <string.h>#include <assert.h>#include <jni.h>#include <jcl.h>#include "javanet.h"#include "target_native.h"#ifndef WITHOUT_NETWORK#include "target_native_network.h"#endif /* WITHOUT_NETWORK */#ifndef WITHOUT_NETWORK/* Need to have some value for SO_TIMEOUT */#ifndef SO_TIMEOUT#ifndef SO_RCVTIMEO#warning Neither SO_TIMEOUT or SO_RCVTIMEO are defined!#warning This will cause all get/setOption calls with that value to throw an exception#else#define SO_TIMEOUT SO_RCVTIMEO#endif /* not SO_RCVTIMEO */#endif /* not SO_TIMEOUT */#endif /* WITHOUT_NETWORK *//*************************************************************************//* * Sets an integer field in the specified object. */static void_javanet_set_int_field (JNIEnv * env, jobject obj,			const char *class, const char *field, int val){  jclass cls;  jfieldID fid;  assert (env != NULL);  assert ((*env) != NULL);  cls = (*env)->FindClass (env, class);  if (cls == NULL)    return;  fid = (*env)->GetFieldID (env, cls, field, "I");  if (fid == NULL)    return;  (*env)->SetIntField (env, obj, fid, val);  return;}/*************************************************************************//* * Returns the value of the specified integer instance variable field or * -1 if an error occurs. */int_javanet_get_int_field (JNIEnv * env, jobject obj, const char *field){  jclass cls = 0;  jfieldID fid;  int fd;  assert (env != NULL);  assert ((*env) != NULL);  DBG ("_javanet_get_int_field(): Entered _javanet_get_int_field\n");  cls = (*env)->GetObjectClass (env, obj);  if (cls == NULL)    return -1;  fid = (*env)->GetFieldID (env, cls, field, "I");  if (fid == NULL)    return -1;  DBG ("_javanet_get_int_field(): Found field id\n");  fd = (*env)->GetIntField (env, obj, fid);  return fd;}/*************************************************************************//* * Creates a FileDescriptor object in the parent class.  It is not used * by this implementation, but the docs list it as a variable, so we * need to include it. */static void_javanet_create_localfd (JNIEnv * env, jobject this){  jclass this_cls, fd_cls;  jfieldID fid;  jmethodID mid;  jobject fd_obj;  assert (env != NULL);  assert ((*env) != NULL);  DBG ("_javanet_create_localfd(): Entered _javanet_create_localfd\n");  /* Look up the fd field */  this_cls = (*env)->FindClass (env, "java/net/SocketImpl");  if (this_cls == NULL)    return;  fid = (*env)->GetFieldID (env, this_cls, "fd", "Ljava/io/FileDescriptor;");  if (fid == NULL)    return;  DBG ("_javanet_create_localfd(): Found fd variable\n");  /* Create a FileDescriptor */  fd_cls = (*env)->FindClass (env, "java/io/FileDescriptor");  if (fd_cls == NULL)    return;  DBG ("_javanet_create_localfd(): Found FileDescriptor class\n");  mid = (*env)->GetMethodID (env, fd_cls, "<init>", "()V");  if (mid == NULL)    return;  DBG ("_javanet_create_localfd(): Found FileDescriptor constructor\n");  fd_obj = (*env)->NewObject (env, fd_cls, mid);  if (fd_obj == NULL)    return;  DBG ("_javanet_create_localfd(): Created FileDescriptor\n");  /* Now set the pointer to the new FileDescriptor */  (*env)->SetObjectField (env, this, fid, fd_obj);  DBG ("_javanet_create_localfd(): Set fd field\n");  return;}/*************************************************************************//* * Returns a Boolean object with the specfied value */static jobject_javanet_create_boolean (JNIEnv * env, jboolean val){  jclass cls;  jmethodID mid;  jobject obj;  assert (env != NULL);  assert ((*env) != NULL);  cls = (*env)->FindClass (env, "java/lang/Boolean");  if (cls == NULL)    return NULL;  mid = (*env)->GetMethodID (env, cls, "<init>", "(Z)V");  if (mid == NULL)    return NULL;  obj = (*env)->NewObject (env, cls, mid, val);  if (obj == NULL)    return NULL;  return obj;}/*************************************************************************//* * Returns an Integer object with the specfied value */static jobject_javanet_create_integer (JNIEnv * env, jint val){  jclass cls;  jmethodID mid;  jobject obj;  assert (env != NULL);  assert ((*env) != NULL);  cls = (*env)->FindClass (env, "java/lang/Integer");  if (cls == NULL)    return NULL;  mid = (*env)->GetMethodID (env, cls, "<init>", "(I)V");  if (mid == NULL)    return NULL;  obj = (*env)->NewObject (env, cls, mid, val);  if (obj == NULL)    return NULL;  return obj;}/*************************************************************************//* * Builds an InetAddress object from a 32 bit address in host byte order */static jobject_javanet_create_inetaddress (JNIEnv * env, int netaddr){#ifndef WITHOUT_NETWORK  unsigned char octets[4];  char buf[16];  jclass ia_cls;  jmethodID mid;  jstring ip_str;  jobject ia;  assert (env != NULL);  assert ((*env) != NULL);  /* Build a string IP address */  TARGET_NATIVE_NETWORK_INT_TO_IPADDRESS_BYTES (netaddr,						octets[0],						octets[1],						octets[2], octets[3]);  sprintf (buf, "%d.%d.%d.%d", octets[0], octets[1], octets[2], octets[3]);  DBG ("_javanet_create_inetaddress(): Created ip addr string\n");  /* Get an InetAddress object for this IP */  ia_cls = (*env)->FindClass (env, "java/net/InetAddress");  if (ia_cls == NULL)    {      return NULL;    }  DBG ("_javanet_create_inetaddress(): Found InetAddress class\n");  mid = (*env)->GetStaticMethodID (env, ia_cls, "getByName",				   "(Ljava/lang/String;)Ljava/net/InetAddress;");  if (mid == NULL)    {      return NULL;    }  DBG ("_javanet_create_inetaddress(): Found getByName method\n");  ip_str = (*env)->NewStringUTF (env, buf);  if (ip_str == NULL)    {      return NULL;    }  ia = (*env)->CallStaticObjectMethod (env, ia_cls, mid, ip_str);  if (ia == NULL)    {      return NULL;    }  DBG ("_javanet_create_inetaddress(): Called getByName method\n");  return ia;#else /* not WITHOUT_NETWORK */  return NULL;#endif /* not WITHOUT_NETWORK */}/*************************************************************************/static void_javanet_set_remhost_addr (JNIEnv * env, jobject this, jobject ia){  jclass this_cls;  jfieldID fid;  assert (env != NULL);  assert ((*env) != NULL);  /* Set the variable in the object */  this_cls = (*env)->FindClass (env, "java/net/SocketImpl");  if (this_cls == NULL)    return;  fid =    (*env)->GetFieldID (env, this_cls, "address", "Ljava/net/InetAddress;");  if (fid == NULL)    return;  DBG ("_javanet_set_remhost_addr(): Found address field\n");  (*env)->SetObjectField (env, this, fid, ia);  DBG ("_javanet_set_remhost_addr(): Set field\n");}/* * Set's the value of the "addr" field in PlainSocketImpl with a new * InetAddress for the specified addr */static void_javanet_set_remhost (JNIEnv * env, jobject this, int netaddr){  jobject ia;  assert (env != NULL);  assert ((*env) != NULL);  DBG ("_javanet_set_remhost(): Entered _javanet_set_remhost\n");  /* Get an InetAddress object */  ia = _javanet_create_inetaddress (env, netaddr);  if (ia == NULL)    return;  _javanet_set_remhost_addr (env, this, ia);}/*************************************************************************//* * Returns a 32 bit Internet address for the passed in InetAddress object */int_javanet_get_netaddr (JNIEnv * env, jobject addr){#ifndef WITHOUT_NETWORK  jclass cls = 0;  jmethodID mid;  jarray arr = 0;  jbyte *octets;  int netaddr, len;  assert (env != NULL);  assert ((*env) != NULL);  DBG ("_javanet_get_netaddr(): Entered _javanet_get_netaddr\n");  if (addr == NULL)    {      JCL_ThrowException (env, "java/lang/NullPointerException",			  "Null address");      return 0;    }  /* Call the getAddress method on the object to retrieve the IP address */  cls = (*env)->GetObjectClass (env, addr);  if (cls == NULL)    return 0;  mid = (*env)->GetMethodID (env, cls, "getAddress", "()[B");  if (mid == NULL)    return 0;  DBG ("_javanet_get_netaddr(): Got getAddress method\n");  arr = (*env)->CallObjectMethod (env, addr, mid);  if (arr == NULL)    return 0;  DBG ("_javanet_get_netaddr(): Got the address\n");  /* Turn the IP address into a 32 bit Internet address in network byte order */  len = (*env)->GetArrayLength (env, arr);  if (len != 4)    {      JCL_ThrowException (env, IO_EXCEPTION, "Internal Error");      return 0;    }  DBG ("_javanet_get_netaddr(): Length ok\n");  octets = (*env)->GetByteArrayElements (env, arr, 0);  if (octets == NULL)    return 0;  DBG ("_javanet_get_netaddr(): Grabbed bytes\n");  TARGET_NATIVE_NETWORK_IPADDRESS_BYTES_TO_INT (octets[0],						octets[1],						octets[2],						octets[3], netaddr);  (*env)->ReleaseByteArrayElements (env, arr, octets, 0);  DBG ("_javanet_get_netaddr(): Done getting addr\n");  return netaddr;#else /* not WITHOUT_NETWORK */#endif /* not WITHOUT_NETWORK */}/*************************************************************************//* * Creates a new stream or datagram socket */void_javanet_create (JNIEnv * env, jobject this, jboolean stream){#ifndef WITHOUT_NETWORK  int fd;  int result;  assert (env != NULL);  assert ((*env) != NULL);  if (stream)    {      /* create a stream socket */      TARGET_NATIVE_NETWORK_SOCKET_OPEN_STREAM (fd, result);      if (result != TARGET_NATIVE_OK)	{	  JCL_ThrowException (env, IO_EXCEPTION,			      TARGET_NATIVE_LAST_ERROR_STRING ());	  return;	}    }  else    {      /* create a datagram socket, set broadcast option */      TARGET_NATIVE_NETWORK_SOCKET_OPEN_DATAGRAM (fd, result);      if (result != TARGET_NATIVE_OK)	{	  JCL_ThrowException (env, IO_EXCEPTION,			      TARGET_NATIVE_LAST_ERROR_STRING ());	  return;	}      TARGET_NATIVE_NETWORK_SOCKET_SET_OPTION_BROADCAST (fd, 1, result);      if (result != TARGET_NATIVE_OK)	{	  JCL_ThrowException (env, IO_EXCEPTION,			      TARGET_NATIVE_LAST_ERROR_STRING ());	  return;	}    }  if (stream)    _javanet_set_int_field (env, this, "gnu/java/net/PlainSocketImpl",			    "native_fd", fd);  else    _javanet_set_int_field (env, this, "gnu/java/net/PlainDatagramSocketImpl",			    "native_fd", fd);  if ((*env)->ExceptionOccurred (env))    {      /* Try to make sure we close the socket since close() won't work. */      do	{	  TARGET_NATIVE_NETWORK_SOCKET_CLOSE (fd, result);	  if (result != TARGET_NATIVE_OK	      && (TARGET_NATIVE_LAST_ERROR ()		  != TARGET_NATIVE_ERROR_INTERRUPT_FUNCTION_CALL))	    return;	}      while (result != TARGET_NATIVE_OK);      return;    }#else /* not WITHOUT_NETWORK */#endif /* not WITHOUT_NETWORK */}/*************************************************************************//* * Close the socket.  Any underlying streams will be closed by this * action as well. */void_javanet_close (JNIEnv * env, jobject this, int stream){#ifndef WITHOUT_NETWORK  int fd;  int result;  int error = 0;  assert (env != NULL);  assert ((*env) != NULL);  fd = _javanet_get_int_field (env, this, "native_fd");  if (fd == -1)    return;  if (stream)    _javanet_set_int_field (env, this, "gnu/java/net/PlainSocketImpl",			    "native_fd", -1);  else

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -