OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [libjava/] [java/] [net/] [natVMNetworkInterfacePosix.cc] - Blame information for rev 775

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 758 jeremybenn
/* Copyright (C) 2003, 2005, 2006  Free Software Foundation
2
 
3
   This file is part of libgcj.
4
 
5
This software is copyrighted work licensed under the terms of the
6
Libgcj License.  Please consult the file "LIBGCJ_LICENSE" for
7
details.  */
8
 
9
#include <config.h>
10
#include <platform.h>
11
 
12
#ifdef HAVE_UNISTD_H
13
#include <unistd.h>
14
#endif
15
#include <string.h>
16
#include <errno.h>
17
#include <stdlib.h>
18
 
19
#include <sys/param.h>
20
#include <sys/types.h>
21
#ifdef HAVE_NETINET_IN_H
22
#include <netinet/in.h>
23
#endif
24
#ifdef HAVE_ARPA_INET_H
25
#include <arpa/inet.h>
26
#endif
27
#ifdef HAVE_NETDB_H
28
#include <netdb.h>
29
#endif
30
#ifdef HAVE_SYS_IOCTL_H
31
#define BSD_COMP /* Get FIONREAD on Solaris2. */
32
#include <sys/ioctl.h>
33
#endif
34
#ifdef HAVE_NET_IF_H
35
#include <net/if.h>
36
#endif
37
#ifdef HAVE_IFADDRS_H
38
#include <ifaddrs.h>
39
#endif
40
 
41
#include <gcj/cni.h>
42
#include <jvm.h>
43
#include <java/net/InetAddress.h>
44
#include <java/net/NetworkInterface.h>
45
#include <java/net/SocketException.h>
46
#include <java/net/VMNetworkInterface.h>
47
#include <java/util/Vector.h>
48
 
49
::java::util::Vector*
50
java::net::VMNetworkInterface::getInterfaces ()
51
{
52
  ::java::util::Vector* ht = new ::java::util::Vector ();
53
 
54
#ifdef HAVE_GETIFADDRS
55
 
56
  struct ifaddrs *addrs;
57
  if (::getifaddrs (&addrs) == -1)
58
    throw new ::java::net::SocketException(JvNewStringUTF (strerror (errno)));
59
 
60
  for (struct ifaddrs *work = addrs; work != NULL; work = work->ifa_next)
61
    {
62
      // Sometimes the address can be NULL; I don't know why but
63
      // there's nothing we can do with this.
64
      if (! work->ifa_addr)
65
        continue;
66
      // We only return Inet4 or Inet6 addresses.
67
      jbyteArray laddr;
68
      if (work->ifa_addr->sa_family == AF_INET)
69
        {
70
          sockaddr_in *real = reinterpret_cast<sockaddr_in *> (work->ifa_addr);
71
          laddr = JvNewByteArray(4);
72
          memcpy (elements (laddr), &real->sin_addr, 4);
73
        }
74
#ifdef HAVE_INET6
75
      else if (work->ifa_addr->sa_family == AF_INET6)
76
        {
77
          sockaddr_in6 *real
78
            = reinterpret_cast<sockaddr_in6 *> (work->ifa_addr);
79
          laddr = JvNewByteArray(16);
80
          memcpy (elements (laddr), &real->sin6_addr, 16);
81
        }
82
#endif
83
      else
84
        continue;
85
 
86
      ::java::net::InetAddress *inaddr
87
          =  ::java::net::InetAddress::getByAddress(laddr);
88
 
89
      // It is ok to make a new NetworkInterface for each struct; the
90
      // java code will unify these as necessary; see
91
      // NetworkInterface.condense().
92
      jstring name = JvNewStringUTF (work->ifa_name);
93
 
94
      ht->add (new NetworkInterface (name, inaddr));
95
    }
96
 
97
  freeifaddrs (addrs);
98
 
99
#else /* ! HAVE_GETIFADDRS */
100
 
101
  int fd;
102
  int num_interfaces = 0;
103
  struct ifconf if_data;
104
  struct ifreq* if_record;
105
 
106
  if_data.ifc_len = 0;
107
  if_data.ifc_buf = NULL;
108
 
109
  // Open a (random) socket to have a file descriptor for the ioctl calls.
110
  fd = _Jv_socket (PF_INET, SOCK_DGRAM, htons (IPPROTO_IP));
111
 
112
  if (fd < 0)
113
    throw new ::java::net::SocketException;
114
 
115
  // Get all interfaces. If not enough buffers are available try it
116
  // with a bigger buffer size.
117
  do
118
    {
119
      num_interfaces += 16;
120
 
121
      if_data.ifc_len = sizeof (struct ifreq) * num_interfaces;
122
      if_data.ifc_buf =
123
        (char*) _Jv_Realloc (if_data.ifc_buf, if_data.ifc_len);
124
 
125
      // Try to get all local interfaces.
126
      if (::ioctl (fd, SIOCGIFCONF, &if_data) < 0)
127
        throw new java::net::SocketException;
128
    }
129
  while (if_data.ifc_len >= (int) (sizeof (struct ifreq) * num_interfaces));
130
 
131
  // Get addresses of all interfaces.
132
  if_record = if_data.ifc_req;
133
 
134
  for (int n = 0; n < if_data.ifc_len; n += sizeof (struct ifreq))
135
    {
136
      struct ifreq ifr;
137
 
138
      memset (&ifr, 0, sizeof (ifr));
139
      strcpy (ifr.ifr_name, if_record->ifr_name);
140
 
141
      // Try to get the IPv4-address of the local interface
142
      if (::ioctl (fd, SIOCGIFADDR, &ifr) < 0)
143
        throw new java::net::SocketException;
144
 
145
      int len = 4;
146
      struct sockaddr_in sa = *((sockaddr_in*) &(ifr.ifr_addr));
147
 
148
      jbyteArray baddr = JvNewByteArray (len);
149
      memcpy (elements (baddr), &(sa.sin_addr), len);
150
      jstring if_name = JvNewStringLatin1 (if_record->ifr_name);
151
      InetAddress* address = java::net::InetAddress::getByAddress (baddr);
152
      ht->add (new NetworkInterface (if_name, address));
153
      if_record++;
154
    }
155
 
156
  _Jv_Free (if_data.ifc_buf);
157
 
158
  if (fd >= 0)
159
    _Jv_close (fd);
160
#endif /* HAVE_GETIFADDRS */ 
161
 
162
  return ht;
163
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.