Android网络设置(route & DNS)

route设置

android4.4只使用了一份路由表,使用busybox route就可以完成路由表的设置,从android5.0之后,考虑要对多网络的支持,采用了多路由表,下面的设置方法只适用于android4.4之前的版本,android5.0之后的版本路由表如何设置留到以后再说明。

1、查看路由表 busybox route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 192.168.7.1 0.0.0.0 UG 0 0 0 eth0
0.0.0.0 192.168.7.1 0.0.0.0 UG 202 0 0 eth0
192.168.0.0 0.0.0.0 255.255.248.0 U 0 0 0 eth0
192.168.0.0 0.0.0.0 255.255.248.0 U 202 0 0 eth0
192.168.7.1 0.0.0.0 255.255.255.255 UH 0 0 0 eth0

2、删除默认路由 busybox route del default
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 192.168.7.1 0.0.0.0 UG 202 0 0 eth0
192.168.0.0 0.0.0.0 255.255.248.0 U 0 0 0 eth0
192.168.0.0 0.0.0.0 255.255.248.0 U 202 0 0 eth0
192.168.7.1 0.0.0.0 255.255.255.255 UH 0 0 0 eth0

3、添加一条默认路由 busybox route add -net 0.0.0.0 gw 192.168.7.1 netmask 0.0.0.0 dev eth0
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 192.168.7.1 0.0.0.0 UG 0 0 0 eth0
192.168.0.0 0.0.0.0 255.255.248.0 U 0 0 0 eth0
192.168.0.0 0.0.0.0 255.255.248.0 U 202 0 0 eth0
192.168.7.1 0.0.0.0 255.255.255.255 UH 0 0 0 eth0

4、删除一条主机路由 busybox route del -host 192.168.7.1 dev eth0
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 192.168.7.1 0.0.0.0 UG 0 0 0 eth0
192.168.0.0 0.0.0.0 255.255.248.0 U 0 0 0 eth0
192.168.0.0 0.0.0.0 255.255.248.0 U 202 0 0 eth0

5、添加一条主机路由 busybox route add -host 192.168.7.1 dev eth0
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 192.168.7.1 0.0.0.0 UG 0 0 0 eth0
192.168.0.0 0.0.0.0 255.255.248.0 U 0 0 0 eth0
192.168.0.0 0.0.0.0 255.255.248.0 U 202 0 0 eth0
192.168.7.1 0.0.0.0 255.255.255.255 UH 0 0 0 eth0

6、删除一条网络路由 busybox route del -net 192.168.0.0 netmask 255.255.248.0 dev eth
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 192.168.7.1 0.0.0.0 UG 0 0 0 eth0
192.168.0.0 0.0.0.0 255.255.248.0 U 202 0 0 eth0

7、添加一条网络路由 busybox route add -net 192.168.0.0 netmask 255.255.248.0 dev eth
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 192.168.7.1 0.0.0.0 UG 0 0 0 eth0
192.168.0.0 0.0.0.0 255.255.248.0 U 0 0 0 eth0
192.168.0.0 0.0.0.0 255.255.248.0 U 202 0 0 eth0

DNS Server设置

同样以android4.4为例来进行说明,android系统中,不管是dns server的设置,还是域名解析,都是由netd进程来执行的,netd与dns的相关操作是调用了一个netbsd的库,netd中dns server设置对应于文件ResolverController.cpp,域名解析对应于文件DnsProxyListener.cpp。

原生的android系统只提供了设置系统dns server的接口,而没有提供查看系统dns server的接口,需要自己添加代码来实现。

通过ndc配合netd可以对android dns进行设置和查看。
1、设置dns interface & server
ndc resolver setdefaultif eth0
ndc resolver setifdns eth0 “” 192.168.7.2

2、查看dns interface
ndc resolver getdefaultif
226 0 DNS default interface is eth0

3、查看dns server
ndc resolver getdefaultServer 1
226 0 DNS default server[1] is 192.168.7.2

其中2、3是通过添加如下代码后才能支持;
1) system\netd\CommandListener.cpp

 if (!strcmp(argv[1], "getdefaultif")) { // "resolver getdefaultif"
        char ifname[IF_NAMESIZE] = {0};
        sResolverCtrl->getDefaultInterface(ifname, sizeof(ifname));
        char *msg = NULL;
        asprintf(&msg, "DNS default interface is %s", ifname);
        cli->sendMsg(ResponseCode::DnsInfoResult, msg, false);
        free(msg);
    } else if (!strcmp(argv[1], "getdefaultServer")) { // "resolver getdefaultServer num"
        if (argc == 3) {
            char addr[128] = {0};
            sResolverCtrl->getDefaultDnsServers(addr, sizeof(addr), atoi(argv[2]));
            char *msg = NULL;
            asprintf(&msg, "DNS default server[%d] is %s", atoi(argv[2]), addr);
            cli->sendMsg(ResponseCode::DnsInfoResult, msg, false);
            free(msg);
        } else {
            cli->sendMsg(ResponseCode::CommandSyntaxError,
                    "Wrong number of arguments to resolver getdefaultServer", false);
            return 0;
        }
    }

2) system\netd\ResolverController.cpp

void ResolverController::getDefaultInterface(char* iface, int len) {
    _resolv_get_default_iface(iface, len);
}
void ResolverController::getDefaultDnsServers(char* addr, int addrLen, int num) {
    _resolv_cache_get_nameserver(num, addr, addrLen);
}

3) _resolv_get_default_iface 和 _resolv_cache_get_nameserver是netbsd的函数,这两个并不对外开放,netd要调用这两个函数,需要修改加上如下的patch;
diff
--- a/libc/private/resolv_cache.h
+++ b/libc/private/resolv_cache.h
@@ -34,6 +34,8 @@
struct __res_state;
struct resolv_cache; /* forward */
+__BEGIN_DECLS
+
/* gets the cache for an interface. Set ifname argument to NULL or
* empty buffer ('\0') to get cache for default interface.
* returned cache might be NULL*/
@@ -48,7 +50,6 @@ extern void _resolv_cache_reset( unsigned generation );
/* Gets the address of the n:th name server for the default interface
* Return length of address on success else 0.
* Note: The first name server is at n = 1 */
-__LIBC_HIDDEN__
extern int _resolv_cache_get_nameserver(int n, char* addr, int addrLen);
/* Gets the address of the n:th name server for a certain interface
@@ -81,7 +82,6 @@ extern struct in_addr* _resolv_get_addr_of_iface(const char* ifname);
/* Copy the name of the default interface to the provided buffer.
* Returns the string length of the default interface,
* be that less or more than the buffLen, or 0 if nothing had been written */
-__LIBC_HIDDEN__
extern size_t _resolv_get_default_iface(char* buff, size_t buffLen);
/* sets the name server addresses to the provided res_state structure. The
@@ -124,4 +124,6 @@ _resolv_cache_query_failed( struct resolv_cache* cache,
const void* query,
int querylen);
+__END_DECLS
+
#endif /* _RESOLV_CACHE_H_ */

你可能感兴趣的:(Android网络设置(route & DNS))