OSDN Git Service

2002-09-25 Michael Koch <konqueror@gmx.de>
[pf3gnuchains/gcc-fork.git] / libjava / java / net / NetworkInterface.java
1 /* NetworkInterface.java
2    Copyright (C) 2002 Free Software Foundation, Inc.
3
4 This file is part of GNU Classpath.
5
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
10  
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING.  If not, write to the
18 Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA.
20
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library.  Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
25
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module.  An independent module is a module which is not derived from
33 or based on this library.  If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so.  If you do not wish to do so, delete this
36 exception statement from your version. */
37
38 package java.net;
39
40 import java.util.Enumeration;
41 import java.util.Vector;
42
43 /**
44  * @author Michael Koch <konqueror@gmx.de>
45  * @since 1.4
46  */
47 public final class NetworkInterface
48 {
49   private static Vector networkInterfaces;
50         
51   private String name;
52   
53   private Vector inetAddresses;
54
55   private NetworkInterface (String name, InetAddress address)
56   {
57     this.name = name;
58     this.inetAddresses = new Vector (1, 1);
59     this.inetAddresses.add (address);
60   }
61
62   private native static Vector getRealNetworkInterfaces ()
63     throws SocketException;
64
65   /**
66    *  Returns the name of the network interface
67    */
68   public String getName ()
69   {
70     return name;
71   }
72
73   /**
74    *  Returns all available addresses of the network interface
75    *  
76    *  If a @see SecurityManager is available all addresses are checked
77    *  with @see SecurityManager::checkConnect() if they are available.
78    *  Only InetAddresses are returned where the security manager doesn't
79    *  thrown an exception.
80    *  
81    *  @return An enumeration of all addresses.
82    */
83   public Enumeration getInetAddresses ()
84   {
85     SecurityManager s = System.getSecurityManager ();
86
87     if (s == null)
88       return inetAddresses.elements ();
89
90     Vector tmpInetAddresses = new Vector (1, 1);
91
92     for (Enumeration addresses = inetAddresses.elements ();
93          addresses.hasMoreElements (); )
94       {
95         InetAddress addr = (InetAddress) addresses.nextElement ();
96         try
97           {
98             s.checkConnect (addr.getHostAddress (), 58000);
99             tmpInetAddresses.add (addr);
100           }
101         catch (SecurityException e)
102           {
103           }
104     }
105
106     return tmpInetAddresses.elements ();
107   }
108
109   /**
110    *  Returns the display name of the interface
111    */
112   public String getDisplayName ()
113   {
114     return name;
115   }
116
117   /**
118    *  Returns an network interface by name
119    *
120    *  @param name The name of the interface to return
121    *
122    *  @exception SocketException If an error occurs
123    *  @exception NullPointerException If the specified name is null
124    */
125   public static NetworkInterface getByName (String name)
126     throws SocketException
127   {
128     if (networkInterfaces == null)
129       networkInterfaces = getRealNetworkInterfaces ();
130
131     for (Enumeration e = networkInterfaces.elements ();
132          e.hasMoreElements (); )
133       {
134         NetworkInterface tmp = (NetworkInterface) e.nextElement ();
135       
136         if (name.equals (tmp.getName ()))
137           return tmp;
138       }
139
140     throw new SocketException ("no network interface with this name exists");
141   }
142
143   /**
144    *  Return a network interface by its address
145    *
146    *  @param addr The address of the interface to return
147    *
148    *  @exception SocketException If an error occurs
149    *  @exception NullPointerException If the specified addess is null
150    */
151   public static NetworkInterface getByInetAddress (InetAddress addr)
152     throws SocketException
153   {
154     if (networkInterfaces == null)
155       networkInterfaces = getRealNetworkInterfaces ();
156     
157     for (Enumeration interfaces = networkInterfaces.elements ();
158          interfaces.hasMoreElements (); )
159       {
160         NetworkInterface tmp = (NetworkInterface) interfaces.nextElement ();
161       
162         for (Enumeration addresses = tmp.inetAddresses.elements ();
163              addresses.hasMoreElements (); )
164           {
165             if (addr.equals ((InetAddress) addresses.nextElement ()))
166               return tmp;
167           }
168       }
169
170     throw new SocketException (
171       "no network interface is bound to such an IP address");
172   }
173
174   /**
175    *  Return an Enumeration of all available network interfaces
176    *
177    *  @exception SocketException If an error occurs
178    */
179   public static Enumeration getNetworkInterfaces ()
180     throws SocketException
181   {
182     if (networkInterfaces == null)
183       networkInterfaces = getRealNetworkInterfaces ();
184
185     Enumeration tmp = networkInterfaces.elements ();
186     if (tmp.hasMoreElements ())
187       return tmp;
188
189     return null;
190   }
191
192   /**
193    *  Checks if the current instance is equal to obj
194    *
195    *  @param obj The object to compare with
196    */ 
197   public boolean equals (Object obj)
198   {
199     if (!(obj instanceof NetworkInterface))
200       return false;
201    
202     NetworkInterface tmp = (NetworkInterface) obj;
203     return name.equals (tmp.name) &&
204            inetAddresses.equals (tmp.inetAddresses);
205   }
206
207   /**
208    *  Returns the hashcode of the current instance
209    */
210   public int hashCode ()
211   {
212     // FIXME: hash correctly
213     return name.hashCode () + inetAddresses.hashCode ();
214   }
215
216   /**
217    *  Returns a string representation of the interface
218    */
219   public String toString ()
220   {
221     // FIXME: check if this is correct
222     String result;
223
224     result = "name: " + getDisplayName () + " (" + getName () +
225              ") addresses:\n";
226
227     for (Enumeration e = inetAddresses.elements ();
228          e.hasMoreElements (); )
229       {
230         InetAddress address = (InetAddress) e.nextElement ();
231         result += address.toString () + "\n";
232       }
233
234     return result;
235   }
236 } // class NetworkInterface