2 * Copyright (C) 2012 Tobias Brunner
3 * Hochschule fuer Technik Rapperswil
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 package org
.strongswan
.android
.logic
;
18 import java
.net
.Inet4Address
;
19 import java
.net
.Inet6Address
;
20 import java
.net
.InetAddress
;
21 import java
.net
.NetworkInterface
;
22 import java
.net
.SocketException
;
23 import java
.util
.Enumeration
;
25 public class NetworkManager
27 public NetworkManager()
32 * Function that retrieves a local address of the given family.
34 * @param ipv4 true to return an IPv4 address, false for IPv6
35 * @return string representation of an IPv4 address, or null if none found
37 public String
getLocalAddress(boolean ipv4
)
41 Enumeration
<NetworkInterface
> en
= NetworkInterface
.getNetworkInterfaces();
43 { /* no interfaces at all */
46 while (en
.hasMoreElements())
48 NetworkInterface intf
= en
.nextElement();
49 if (intf
.isLoopback() || !intf
.isUp() ||
50 intf
.getName().startsWith("tun"))
54 Enumeration
<InetAddress
> enumIpAddr
= intf
.getInetAddresses();
55 while (enumIpAddr
.hasMoreElements())
57 InetAddress inetAddress
= enumIpAddr
.nextElement();
58 if (inetAddress
.isLoopbackAddress())
62 if ((ipv4
&& inetAddress
instanceof Inet4Address
) ||
63 (!ipv4
&& inetAddress
instanceof Inet6Address
))
65 return inetAddress
.getHostAddress();
70 catch (SocketException ex
)