diff --git a/ReadMe.md b/ReadMe.md index aae256c..45e787c 100644 --- a/ReadMe.md +++ b/ReadMe.md @@ -567,8 +567,8 @@ entware|ipkg update
ipkg install smartdns|软件源路径:https://bin.entwa | 键名 | 功能说明 | 默认值 | 可用值/要求 | 举例 | | :--- | :--- | :--- | :--- | :--- | | server-name | DNS 服务器名称 | 操作系统主机名 / smartdns | 符合主机名规格的字符串 | server-name smartdns | -| bind | DNS 监听端口号 | [::]:53 | 可绑定多个端口。
IP:PORT: 服务器 IP:端口号
[-group]: 请求时使用的 DNS 服务器组
[-no-rule-addr]:跳过 address 规则
[-no-rule-nameserver]:跳过 Nameserver 规则
[-no-rule-ipset]:跳过 ipset 和 nftset 规则
[-no-rule-soa]:跳过 SOA(#) 规则
[-no-dualstack-selection]:停用双栈测速
[-no-speed-check]:停用测速
[-no-cache]:停止缓存 | bind :53 | -| bind-tcp | DNS TCP 监听端口号 | [::]:53 | 可绑定多个端口。
IP:PORT: 服务器 IP:端口号
[-group]: 请求时使用的 DNS 服务器组
[-no-rule-addr]:跳过 address 规则
[-no-rule-nameserver]:跳过 nameserver 规则
[-no-rule-ipset]:跳过 ipset 和 nftset 规则。
[-no-rule-soa]:跳过 SOA(#) 规则
[-no-dualstack-selection]:停用双栈测速
[-no-speed-check]:停用测速
[-no-cache]:停止缓存 | bind-tcp :53 | +| bind | DNS 监听端口号 | [::]:53 | 可绑定多个端口。
IP:PORT@DEVICE: 服务器 IP:端口号@设备名
[-group]: 请求时使用的 DNS 服务器组
[-no-rule-addr]:跳过 address 规则
[-no-rule-nameserver]:跳过 Nameserver 规则
[-no-rule-ipset]:跳过 ipset 和 nftset 规则
[-no-rule-soa]:跳过 SOA(#) 规则
[-no-dualstack-selection]:停用双栈测速
[-no-speed-check]:停用测速
[-no-cache]:停止缓存 | bind :53@eth0 | +| bind-tcp | DNS TCP 监听端口号 | [::]:53 | 可绑定多个端口。
IP:PORT@DEVICE: 服务器 IP:端口号@设备名
[-group]: 请求时使用的 DNS 服务器组
[-no-rule-addr]:跳过 address 规则
[-no-rule-nameserver]:跳过 nameserver 规则
[-no-rule-ipset]:跳过 ipset 和 nftset 规则。
[-no-rule-soa]:跳过 SOA(#) 规则
[-no-dualstack-selection]:停用双栈测速
[-no-speed-check]:停用测速
[-no-cache]:停止缓存 | bind-tcp :53 | | cache-size | 域名结果缓存个数 | 512 | 大于等于 0 的数字 | cache-size 512 | | cache-persist | 是否持久化缓存 | 自动。
当 cache-file 所在的位置有超过 128 MB 的可用空间时启用,否则禁用。 | [yes\|no] | cache-persist yes | | cache-file | 缓存持久化文件路径 | /tmp/smartdns.cache | 合法路径字符串 | cache-file /tmp/smartdns.cache | diff --git a/ReadMe_en.md b/ReadMe_en.md index 9ef8540..7daffbd 100644 --- a/ReadMe_en.md +++ b/ReadMe_en.md @@ -529,8 +529,8 @@ Note: Merlin firmware is derived from ASUS firmware and can theoretically be use |parameter|Parameter function|Default value|Value type|Example| |--|--|--|--|--| |server-name|DNS name|host name/smartdns|any string like hosname|server-name smartdns -|bind|DNS listening port number|[::]:53|Support binding multiple ports
`IP:PORT`: server IP, port number.
`[-group]`: The DNS server group used when requesting.
`[-no-rule-addr]`: Skip the address rule.
`[-no-rule-nameserver]`: Skip the Nameserver rule.
`[-no-rule-ipset]`: Skip the Ipset or nftset rules.
`[-no-rule-soa]`: Skip address SOA(#) rules.
`[-no-dualstack-selection]`: Disable dualstack ip selection.
`[-no-speed-check]`: Disable speed measurement.
`[-no-cache]`: stop caching |bind :53 -|bind-tcp|TCP mode DNS listening port number|[::]:53|Support binding multiple ports
`IP:PORT`: server IP, port number.
`[-group]`: The DNS server group used when requesting.
`[-no-rule-addr]`: Skip the address rule.
`[-no-rule-nameserver]`: Skip the Nameserver rule.
`[-no-rule-ipset]`: Skip the Ipset or nftset rules.
`[-no-rule-soa]`: Skip address SOA(#) rules.
`[-no-dualstack-selection]`: Disable dualstack ip selection.
`[-no-speed-check]`: Disable speed measurement.
`[-no-cache]`: stop caching |bind-tcp :53 +|bind|DNS listening port number|[::]:53|Support binding multiple ports
`IP:PORT@DEVICE`: server IP, port number, and device.
`[-group]`: The DNS server group used when requesting.
`[-no-rule-addr]`: Skip the address rule.
`[-no-rule-nameserver]`: Skip the Nameserver rule.
`[-no-rule-ipset]`: Skip the Ipset or nftset rules.
`[-no-rule-soa]`: Skip address SOA(#) rules.
`[-no-dualstack-selection]`: Disable dualstack ip selection.
`[-no-speed-check]`: Disable speed measurement.
`[-no-cache]`: stop caching |bind :53@eth0 +|bind-tcp|TCP mode DNS listening port number|[::]:53|Support binding multiple ports
`IP:PORT@DEVICE`: server IP, port number and device.
`[-group]`: The DNS server group used when requesting.
`[-no-rule-addr]`: Skip the address rule.
`[-no-rule-nameserver]`: Skip the Nameserver rule.
`[-no-rule-ipset]`: Skip the Ipset or nftset rules.
`[-no-rule-soa]`: Skip address SOA(#) rules.
`[-no-dualstack-selection]`: Disable dualstack ip selection.
`[-no-speed-check]`: Disable speed measurement.
`[-no-cache]`: stop caching |bind-tcp :53 |cache-size|Domain name result cache number|512|integer|cache-size 512 |cache-persist|enable persist cache|Auto: Enabled if the location of `cache-file` has more than 128MB of free space.|[yes\|no]|cache-persist yes |cache-file|cache persist file|/tmp/smartdns.cache|路径|cache-file /tmp/smartdns.cache diff --git a/etc/smartdns/smartdns.conf b/etc/smartdns/smartdns.conf index 0abdadb..9ffc2dc 100644 --- a/etc/smartdns/smartdns.conf +++ b/etc/smartdns/smartdns.conf @@ -19,9 +19,9 @@ # dns server bind ip and port, default dns server port is 53, support binding multi ip and port # bind udp server -# bind [IP]:[port] [-group [group]] [-no-rule-addr] [-no-rule-nameserver] [-no-rule-ipset] [-no-speed-check] [-no-cache] [-no-rule-soa] [-no-dualstack-selection] +# bind [IP]:[port][@device] [-group [group]] [-no-rule-addr] [-no-rule-nameserver] [-no-rule-ipset] [-no-speed-check] [-no-cache] [-no-rule-soa] [-no-dualstack-selection] # bind tcp server -# bind-tcp [IP]:[port] [-group [group]] [-no-rule-addr] [-no-rule-nameserver] [-no-rule-ipset] [-no-speed-check] [-no-cache] [-no-rule-soa] [-no-dualstack-selection] +# bind-tcp [IP]:[port][@device] [-group [group]] [-no-rule-addr] [-no-rule-nameserver] [-no-rule-ipset] [-no-speed-check] [-no-cache] [-no-rule-soa] [-no-dualstack-selection] # option: # -group: set domain request to use the appropriate server group. # -no-rule-addr: skip address rule. @@ -36,9 +36,11 @@ # example: # IPV4: # bind :53 +# bind :53@eth0 # bind :6053 -group office -no-speed-check # IPV6: # bind [::]:53 +# bind [::]:53@eth0 # bind-tcp [::]:53 bind [::]:53 diff --git a/src/dns_server.c b/src/dns_server.c index 6833b41..35e857f 100644 --- a/src/dns_server.c +++ b/src/dns_server.c @@ -34,6 +34,7 @@ #include #include #include +#include #include #include #include @@ -42,6 +43,7 @@ #include #include #include +#include #include #include @@ -5333,14 +5335,19 @@ static int _dns_create_socket(const char *host_ip, int type) struct addrinfo *gai = NULL; char port_str[8]; char ip[MAX_IP_LEN]; + char host_ip_device[MAX_IP_LEN * 2]; int port = 0; char *host = NULL; int optval = 1; int yes = 1; const int priority = SOCKET_PRIORITY; const int ip_tos = SOCKET_IP_TOS; + const char *ifname = NULL; - if (parse_ip(host_ip, ip, &port) == 0) { + safe_strncpy(host_ip_device, host_ip, sizeof(host_ip_device)); + ifname = strstr(host_ip_device, "@"); + + if (parse_ip(host_ip_device, ip, &port) == 0) { host = ip; } @@ -5375,6 +5382,17 @@ static int _dns_create_socket(const char *host_ip, int type) setsockopt(fd, SOL_SOCKET, SO_PRIORITY, &priority, sizeof(priority)); setsockopt(fd, IPPROTO_IP, IP_TOS, &ip_tos, sizeof(ip_tos)); + if (ifname != NULL) { + struct ifreq ifr; + memset(&ifr, 0, sizeof(struct ifreq)); + safe_strncpy(ifr.ifr_name, ifname + 1, sizeof(ifr.ifr_name)); + ioctl(fd, SIOCGIFINDEX, &ifr); + if (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, (void *)&ifr, sizeof(struct ifreq)) < 0) { + tlog(TLOG_ERROR, "bind socket to device %s faild, %s\n", ifr.ifr_name, strerror(errno)); + goto errout; + } + } + if (bind(fd, gai->ai_addr, gai->ai_addrlen) != 0) { tlog(TLOG_ERROR, "bind service %s failed, %s\n", host_ip, strerror(errno)); goto errout;