推 richliu:用 netfilter 將 eth0 的 output tcp/udp port 53 DROP 02/25 11:27
這樣不行~因為eth0不能完全不送出dns query (因為有的要問nameserver0);
如果只drop 1.1.1.1 port 53的話~
eth0還是會對2.2.2.2送出 ftp.test.com 這個多餘的 dns query ~
對不起~有些地方我弄錯了改一下內文~我剛看了wireshark 抓的封包我弄錯了~
現在的狀況是 gethostbyname("ftp.test.com")時~
eth0 會對 1.1.1.1 跟2.2.2.2同時送出 "ftp.test.com" 的 dns query~
eth1則不會送~
我想問要怎樣才能 只有eth1對1.1.1.1發dns query~
我查了一些文章~
好像有提到把gethostbyname()改用getaddrinfo()可以指定interface送出封包~
可是我怎麼作都失敗~還是一樣從 eth0對1.1.1.1 2.2.2.2送出dns query
有高手可以教我一下getaddrinfo的用法嗎??~
//以下是失敗的code:
test_main(void)
{
struct addrinfo hints, *res;
int errcode;
char addrstr[100];
void *ptr;
memset (&hints, 0, sizeof (hints));
hints.ai_family = PF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_CANONNAME;
hints.ai_canonname="eth1";
inet_aton("192.168.2.1",hints.ai_if);
errcode = getaddrinfo ("www.google.com.tw", NULL, &hints, &res);
if (errcode != 0)
{
printf ("errcode:%d\n",errcode);
return -1;
}
printf ("Host: %s\n", host);
while (res)
{
inet_ntop (res->ai_family, res->ai_addr->sa_data, addrstr, 100);
switch (res->ai_family)
{
case AF_INET:
ptr = &((struct sockaddr_in *) res->ai_addr)->sin_addr;
break;
case AF_INET6:
ptr = &((struct sockaddr_in6 *) res->ai_addr)->sin6_addr;
break;
}
inet_ntop (res->ai_family, ptr, addrstr, 100);
printf ("IPv%d address: %s (%s)\n", res->ai_family == PF_INET6 ? 6 : 4,
addrstr, res->ai_canonname);
res = res->ai_next;
}
return 0;
}
※ 編輯: scarface 來自: 118.169.3.156 (02/25 16:46)
→ richliu:我個人看完的感覺似乎是你的 routing 怪怪的. 02/25 18:29
→ richliu:ping 1.1.1.1 應該要可以從 eth1 出去才對 02/25 18:29
→ richliu:基本上如果你的網路是照你設的, 到 1.1.1.1 和到 2.2.2.2 02/25 18:29
→ richliu:是會走不同的 interface, 不過從你內文描述是沒有 02/25 18:30
→ richliu:是你不是 eth0 10.1.1.1 netmask 255.0.0.0 eth1 是 02/25 18:30
→ richliu:10.2.1.1 netmask 255.0.0.0 也就是 mask 設錯? 02/25 18:30
→ scarface:主要是因為eth0 eth1 nameserver1 nameserver0 ftpserver 02/25 19:35
→ scarface:他們是有可能都在不同網域的~在這種情況下~送出dns query 02/25 19:35
→ scarface:是會送往gateway的 02/25 19:36
→ scarface:對eth0來說~它不知道1.1.1.1在哪~所以送往gateway~ 02/25 19:45
→ richliu:這樣看起來是你的 routing 走 default gateway 02/25 22:50
→ richliu:所以你必需設一個 policy route 指定 nameserver1 是要走 02/25 22:50
→ richliu:eth1 的 gateway . 02/25 22:50
→ richliu:ex: route add -net 192.168.0.0 netmask 255.255.0.0 gw 02/25 22:51
→ richliu:172.20.1.254 --> 172.20.1.254 是 eth1 的 default gw 02/25 22:52