static void
free_netif_list (JNIEnv *env, struct netif_entry *list)
{
  while (list != NULL)
    {
      struct netif_entry *e = list->next;
      JCL_free (env, list);
      list = e;
    }
}
Esempio n. 2
0
JNIEXPORT jobjectArray JNICALL
Java_java_io_VMFile_list (JNIEnv * env, jobject obj
			  __attribute__ ((__unused__)), jstring name)
{
#ifndef WITHOUT_FILESYSTEM
  const int REALLOC_SIZE = 10;

  const char *dirname;
  int result;
  char **filelist;
  void *handle;
  const char *filename;
  unsigned long int filelist_count, max_filelist_count;
  char **tmp_filelist;
  jclass str_clazz;
  jobjectArray filearray;
  unsigned long int i;
  jstring str;

  /* Don't use the JCL convert function because it throws an exception
     on failure */
  dirname = (*env)->GetStringUTFChars (env, name, 0);
  if (dirname == NULL)
    {
      return (0);
    }

  /* open directory for reading */
  TARGET_NATIVE_FILE_OPEN_DIR (dirname, handle, result);

  (*env)->ReleaseStringUTFChars (env, name, dirname);

  if (result != TARGET_NATIVE_OK)
    {
      return (0);
    }

  /* allocate filelist */
  filelist = (char **) JCL_malloc (env, sizeof (char *) * REALLOC_SIZE);
  if (filelist == NULL)
    {
      TARGET_NATIVE_FILE_CLOSE_DIR (handle, result);
      return (0);
    }
  filelist_count = 0;
  max_filelist_count = REALLOC_SIZE;

  /* read the files from the directory */
  TARGET_NATIVE_FILE_READ_DIR (handle, filename, result);
  while (result == TARGET_NATIVE_OK)
    {
      if ((strcmp (filename, ".") != 0) && (strcmp (filename, "..") != 0))
	{
	  /* allocate more memory if necessary */
	  if (filelist_count >= max_filelist_count)
	    {
	      tmp_filelist = (char **) JCL_realloc (env,
						    filelist,
						    (max_filelist_count +
						     REALLOC_SIZE) *
						    sizeof (char *));
	      if (tmp_filelist == NULL)
		{
		  for (i = 0; i < filelist_count; i++)
		    {
		      JCL_free (env, filelist[i]);
		    }
		  JCL_free (env, filelist);
		  TARGET_NATIVE_FILE_CLOSE_DIR (handle, result);
		  return (0);
		}
	      filelist = tmp_filelist;
	      max_filelist_count += REALLOC_SIZE;
	    }

	  /* save entry in list (avoid strdup, because it is not ANSI C, thus difficult to port) */
	  filelist[filelist_count] =
	    (char *) JCL_malloc (env, strlen (filename) + 1);
	  assert (filelist[filelist_count] != NULL);
	  strcpy (filelist[filelist_count], filename);
	  filelist_count++;
	}

      /* read next directory entry */
      TARGET_NATIVE_FILE_READ_DIR (handle, filename, result);
    }

  /* close directory */
  TARGET_NATIVE_FILE_CLOSE_DIR (handle, result);

  /* put the list of files into a Java String array and return it */
  str_clazz = (*env)->FindClass (env, "java/lang/String");
  if (str_clazz == NULL)
    {
      for (i = 0; i < filelist_count; i++)
	{
	  JCL_free (env, filelist[i]);
	}
      JCL_free (env, filelist);
      return (0);
    }
  filearray = (*env)->NewObjectArray (env, filelist_count, str_clazz, 0);
  if (filearray == NULL)
    {
      for (i = 0; i < filelist_count; i++)
	{
	  JCL_free (env, filelist[i]);
	}
      JCL_free (env, filelist);
      return (0);
    }
  for (i = 0; i < filelist_count; i++)
    {
      /* create new string */
      str = (*env)->NewStringUTF (env, filelist[i]);
      if (str == NULL)
	{
	  /* We don't clean up everything here, but if this failed,
	     something serious happened anyway */
	  for (i = 0; i < filelist_count; i++)
	    {
	      JCL_free (env, filelist[i]);
	    }
	  JCL_free (env, filelist);
	  return (0);
	}

      /* save into array */
      (*env)->SetObjectArrayElement (env, filearray, i, str);

      /* delete local reference */
      (*env)->DeleteLocalRef (env, str);
    }

  /* free resources */
  for (i = 0; i < filelist_count; i++)
    {
      JCL_free (env, filelist[i]);
    }
  JCL_free (env, filelist);

  return (filearray);
#else /* not WITHOUT_FILESYSTEM */
  return (0);
#endif /* not WITHOUT_FILESYSTEM */
}
Esempio n. 3
0
JNIEXPORT void JNICALL
Java_gnu_javax_sound_sampled_gstreamer_lines_GstPipeline_init_1instance
  (JNIEnv *env, jobject pipeline)
{
  GstNativePipeline *_pipeline = NULL;
  
  jclass localGstPipelineClass = NULL;
  jclass localPointerClass = NULL;
  jobject _pointer = NULL;
  
  _pipeline =
    (GstNativePipeline *) JCL_malloc (env, sizeof (GstNativePipeline));
  if (_pipeline == NULL)
    return;
  
  _pipeline->priv = (GstNativePipelinePrivate *)
    JCL_malloc (env, sizeof (GstNativePipelinePrivate));
  if (_pipeline->priv == NULL)
    {
      JCL_free (env, _pipeline);
      return;
    }
  
#if SIZEOF_VOID_P == 8
  localPointerClass = JCL_FindClass (env, "gnu/classpath/Pointer64");
#else
# if SIZEOF_VOID_P == 4
  localPointerClass = JCL_FindClass (env, "gnu/classpath/Pointer32");
# else
#   error "Pointer size is not supported."
# endif /* SIZEOF_VOID_P == 4 */
#endif /* SIZEOF_VOID_P == 8 */

  localGstPipelineClass = (*env)->GetObjectClass(env, pipeline);
  if (localGstPipelineClass == NULL || localGstPipelineClass == NULL)
    {
      JCL_free (env, _pipeline->priv);
      JCL_free (env, _pipeline);
      JCL_ThrowException (env, "java/lang/InternalError",
                               "Class Initialization failed.");
      return;
    }

  GST_DETECTED_PIPE_CAPACITY = (long) (*env)->GetLongField(env, pipeline,
                                                           capacityFID);
  
  /* fill the object */
  (*env)->GetJavaVM(env, &_pipeline->priv->vm);
  _pipeline->priv->jni_pipeline = (*env)->NewGlobalRef(env, pipeline);
  _pipeline->priv->GstPipelineClass =
    (*env)->NewGlobalRef(env, localGstPipelineClass);
  _pipeline->priv->PointerClass = (*env)->NewGlobalRef(env, localPointerClass);
  _pipeline->priv->pipeline = NULL;
  
  _pointer = (*env)->GetObjectField(env, pipeline, pipelineFID);
  
  if (_pointer == NULL)
    {
#if SIZEOF_VOID_P == 8
      _pointer = (*env)->NewObject(env, _pipeline->priv->PointerClass,
                                   pointerConstructorMID, (jlong) _pipeline);
#else
      _pointer = (*env)->NewObject(env, _pipeline->priv->PointerClass,
                                   pointerConstructorMID, (jint) _pipeline);
#endif
    }
  else
    {
#if SIZEOF_VOID_P == 8
      (*env)->SetLongField(env, pipeline, pipelineFID, (jlong) _pipeline);
#else
      (*env)->SetIntField(env, pipeline, pipelineFID, (jint) _pipeline);
#endif
    }
      
  /* store back our pointer into the calling class */
  (*env)->SetObjectField(env, pipeline, pipelineFID, _pointer);
}
/*
 * Returns all local network interfaces as an array.
 */
JNIEXPORT jobjectArray JNICALL
Java_java_net_VMNetworkInterface_getVMInterfaces (JNIEnv * env,
                                                  jclass clazz UNUSED)
{
#if defined (HAVE_IFADDRS_H) && defined (HAVE_GETIFADDRS)
  struct ifaddrs *ifaddrs, *i;
  struct netif_entry *iflist = NULL, *e;
  jobjectArray netifs;
  int numifs = 0;
  int k;

  if (getifaddrs (&ifaddrs) == -1)
    {
      JCL_ThrowException (env, "java/net/SocketException", strerror (errno));
      return NULL;
    }

  for (i = ifaddrs; i != NULL; i = i->ifa_next)
    {
      if (iflist == NULL)
        {
          iflist = JCL_malloc (env, sizeof (struct netif_entry));
          if (iflist == NULL)
            {
              freeifaddrs (ifaddrs);
              return NULL;
            }
          iflist->name = i->ifa_name;
          iflist->numaddrs = 0;
          iflist->next = NULL;
          iflist->netif = (*env)->NewObject (env, clazz, java_net_VMNetworkInterface_init,
                                             (*env)->NewStringUTF (env, i->ifa_name));
          if (iflist->netif == NULL)
            {
              freeifaddrs (ifaddrs);
              JCL_free (env, iflist);
              return NULL;
            }
          e = iflist;
        }
      else
        {
          struct netif_entry *p = NULL;
          for (e = iflist; e != NULL; e = e->next)
            {
              if (strcmp (e->name, i->ifa_name) == 0)
                break;
              p = e;
            }

          if (e == NULL)
            {
              p->next = (struct netif_entry *) JCL_malloc (env, sizeof (struct netif_entry));
              if (p->next == NULL)
                {
                  free_netif_list (env, iflist);
                  freeifaddrs (ifaddrs);
                  return NULL;
                }
              e = p->next;
              e->name = i->ifa_name;
              e->numaddrs = 0;
              e->next = NULL;
              e->netif = (*env)->NewObject (env, clazz, java_net_VMNetworkInterface_init,
                                            (*env)->NewStringUTF (env, i->ifa_name));
              if (e->netif == NULL)
                {
                  free_netif_list (env, iflist);
                  freeifaddrs (ifaddrs);
                  return NULL;
                }
            }
        }

      if (i->ifa_addr == NULL)
        continue;

      if (i->ifa_addr->sa_family == AF_INET)
        {
          struct sockaddr_in *sin = (struct sockaddr_in *) i->ifa_addr;
          jobject buffer = (*env)->NewDirectByteBuffer (env, &(sin->sin_addr.s_addr), 4);
          (*env)->CallVoidMethod (env, e->netif, java_net_VMNetworkInterface_addAddress,
                                  buffer);
          if ((*env)->ExceptionCheck (env))
            {
              free_netif_list (env, iflist);
              freeifaddrs (ifaddrs);
              return NULL;
            }
          (*env)->DeleteLocalRef (env, buffer);
          e->numaddrs++;
        }
#ifdef HAVE_INET6
      else if (i->ifa_addr->sa_family == AF_INET6)
        {
          struct sockaddr_in6 *sin = (struct sockaddr_in6 *) i->ifa_addr;
          jobject buffer = (*env)->NewDirectByteBuffer (env, &(sin->sin6_addr.s6_addr), 16);
          (*env)->CallVoidMethod (env, e->netif, java_net_VMNetworkInterface_addAddress,
                                  buffer);
          if ((*env)->ExceptionCheck (env))
            {
              free_netif_list (env, iflist);
              freeifaddrs (ifaddrs);
              return NULL;
            }
          (*env)->DeleteLocalRef (env, buffer);
          e->numaddrs++;
        }
#endif /* HAVE_INET6 */
    }

  /* Count how many interfaces we have that have addresses. */
  for (e = iflist; e != NULL; e = e->next)
    {
      if (e->numaddrs != 0)
        numifs++;
    }

  netifs = (*env)->NewObjectArray (env, numifs, clazz, NULL);
  k = 0;
  for (e = iflist; e != NULL && k < numifs; e = e->next)
    {
      if (e->numaddrs != 0)
        {
          (*env)->SetObjectArrayElement (env, netifs, k, e->netif);
          (*env)->DeleteLocalRef (env, e->netif);
          k++;
        }
    }

  free_netif_list (env, iflist);
  freeifaddrs (ifaddrs);
  return netifs;
#else
  JCL_ThrowException (env, "java/net/SocketException", "getifaddrs not supported");
  return NULL;
#endif /* HAVE_IFADDRS_H && HAVE_GETIFADDRS */
}