Android网络设置(route & DNS)

来源:互联网 发布:linux绑核运行 编辑:程序博客网 时间:2024/04/30 02:19

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_ */

1 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 疣迪去除疣体怎么办 尖锐湿庞出血了怎么办 尿道口周围烂了怎么办 尖锐湿庞复发了怎么办 宝宝脸上长湿疹怎么办如何治疗 孕妇得尖锐湿庞怎么办 痘痘留下的小坑怎么办 花洒固定座坏了怎么办 脚上起水泡烂了怎么办 月经期吃了芒果怎么办 月经量少又黑怎么办 来月经黑色的血怎么办 月经来的是黑色怎么办 来月经有血块是怎么办 月经又少又黑怎么办 来月经发黑又少怎么办 月经血发黑量少怎么办 做人流后肚子胀怎么办 怀孕见红了肚子不痛怎么办 月经来是黑色的怎么办 怀孕了长了痔疮怎么办 怀孕了有外痔疮怎么办 孕妇长痔疮很痛怎么办 孕9个月尿路感染怎么办 旁边有人尿不出来怎么办 外阴破皮了应该怎么办 脸上长脂肪粒怎么办怎么能消除 挤黑头留下的坑怎么办 长痘留下的坑怎么办 鼻子上留下黑印怎么办 狗狗眼里长息肉怎么办 狗狗眼角长息肉怎么办 脸上长了好多脂肪粒怎么办 脸上毛孔粗大有黑头怎么办 脸颊毛孔粗有黑头怎么办 鼻子上有黑头怎么办小窍门 脸上很多粉刺和油脂粒怎么办 毛孔里都是角栓怎么办 脸上长了很多脂肪粒怎么办 脸上全是油脂粒怎么办 外阴口长了疙瘩怎么办