3434import java .io .FileInputStream ;
3535import java .io .IOException ;
3636import java .io .InputStreamReader ;
37+ import java .lang .reflect .Method ;
3738import java .net .InetAddress ;
3839import java .net .InterfaceAddress ;
3940import java .net .NetworkInterface ;
@@ -101,6 +102,7 @@ public String toString() {
101102 private IP4Address mNetmask = null ;
102103 private IP4Address mLocal = null ;
103104 private IP4Address mBase = null ;
105+ private Method mTetheredIfacesMethod = null ;
104106
105107 /**
106108 * see http://en.wikipedia.org/wiki/Reserved_IP_addresses
@@ -121,6 +123,7 @@ public Network(Context context, String iface) throws SocketException, UnknownHos
121123 mGateway = new IP4Address (mInfo .gateway );
122124 mNetmask = getNetmask ();
123125 mBase = new IP4Address (mInfo .netmask & mInfo .gateway );
126+ mTetheredIfacesMethod = getTetheredIfacesMethod (mConnectivityManager );
124127
125128 if (iface != null ) {
126129 if (initNetworkInterface (iface ))
@@ -136,6 +139,15 @@ public Network(Context context, String iface) throws SocketException, UnknownHos
136139 throw new NoRouteToHostException ("Not connected to any network." );
137140 }
138141
142+ private static Method getTetheredIfacesMethod (ConnectivityManager connectivityManager ) {
143+ try {
144+ return connectivityManager .getClass ().getDeclaredMethod ("getTetheredIfaces" );
145+ } catch (NoSuchMethodException e ) {
146+ Logger .warning ("unable to get 'ConnectivityManager#getTetheredIfaces()': " + e .getMessage ());
147+ return null ;
148+ }
149+ }
150+
139151 public boolean initNetworkInterface (String iface ) {
140152
141153 try {
@@ -158,10 +170,17 @@ public boolean initNetworkInterface(String iface) {
158170 Short .toString (ifaceAddress .getNetworkPrefixLength ()));
159171
160172 mLocal = new IP4Address (su .getInfo ().getAddress ());
161- mGateway = new IP4Address (getSystemGateway (mInterface .getDisplayName ()));
162173 mNetmask = new IP4Address (su .getInfo ().getNetmask ());
163174 mBase = new IP4Address (su .getInfo ().getNetworkAddress ());
164175
176+ String gateway = getSystemGateway (mInterface .getDisplayName ());
177+
178+ if (gateway == null ) {
179+ mGateway = null ;
180+ } else {
181+ mGateway = new IP4Address (gateway );
182+ }
183+
165184 return true ;
166185 } catch (Exception e ) {
167186 Logger .error ("Error: " + e .getLocalizedMessage ());
@@ -194,11 +213,11 @@ public boolean equals(Network network) {
194213 }
195214
196215 public boolean isInternal (byte [] address ) {
197- byte [] gateway = mGateway .toByteArray ();
216+ byte [] local = mLocal .toByteArray ();
198217 byte [] mask = mNetmask .toByteArray ();
199218
200- for (int i = 0 ; i < gateway .length ; i ++)
201- if ((gateway [i ] & mask [i ]) != (address [i ] & mask [i ]))
219+ for (int i = 0 ; i < local .length ; i ++)
220+ if ((local [i ] & mask [i ]) != (address [i ] & mask [i ]))
202221 return false ;
203222
204223 return true ;
@@ -265,8 +284,25 @@ public InetAddress getNetmaskAddress() {
265284 return mNetmask .toInetAddress ();
266285 }
267286
287+ public boolean haveGateway () {
288+ return mGateway != null ;
289+ }
290+
291+ public boolean isTetheringEnabled () {
292+ if (mTetheredIfacesMethod == null ) {
293+ return false ;
294+ }
295+ try {
296+ String [] ifaces = (String []) mTetheredIfacesMethod .invoke (mConnectivityManager );
297+ return ifaces .length > 0 ;
298+ } catch (Exception e ) {
299+ Logger .error ("unable to retrieve tethered ifaces: " + e .getMessage ());
300+ return false ;
301+ }
302+ }
303+
268304 public InetAddress getGatewayAddress () {
269- return mGateway .toInetAddress ();
305+ return mGateway == null ? null : mGateway .toInetAddress ();
270306 }
271307
272308 public byte [] getGatewayHardware () {
0 commit comments