macOS的SIOCGIFINDEX等于什么?

时间:2019-01-21 07:28:22

标签: c linux macos networking interface

尝试编译名为mdns-scan的Linux实用程序以在macOS系统上使用。
这是一个并排比较:

enter image description here

Debian:

编译成功:

root@debian:~# cd /git
root@debian:/git# git clone https://salsa.debian.org/alteholz/mdns-scan.git
Cloning into 'mdns-scan'...
remote: Enumerating objects: 77, done.
remote: Counting objects: 100% (77/77), done.
remote: Compressing objects: 100% (67/67), done.
remote: Total 77 (delta 23), reused 0 (delta 0)
Unpacking objects: 100% (77/77), done.
root@debian:/git# cd mdns-scan/
root@debian:/git/mdns-scan# make
cc -Wall -W -g -O0 -pipe   -c -o mdns-scan.o mdns-scan.c
mdns-scan.c: In function ‘main’:
mdns-scan.c:218:14: warning: unused parameter ‘argc’ [-Wunused-parameter]
 int main(int argc, char*argv[]) {
          ~~~~^~~~
mdns-scan.c:218:25: warning: unused parameter ‘argv’ [-Wunused-parameter]
 int main(int argc, char*argv[]) {
                    ~~~~~^~~~~~
cc -Wall -W -g -O0 -pipe   -c -o dns.o dns.c
cc -Wall -W -g -O0 -pipe   -c -o query.o query.c
cc -Wall -W -g -O0 -pipe   -c -o util.o util.c
cc -o mdns-scan mdns-scan.o dns.o query.o util.o
root@debian:/git/mdns-scan# make install
install mdns-scan /usr/bin/mdns-scan

macOS:

编译失败:

user@macos:~$ cd /git
user@macos:git$ sudo git clone https://salsa.debian.org/alteholz/mdns-scan.git
Cloning into 'mdns-scan'...
remote: Enumerating objects: 77, done.
remote: Counting objects: 100% (77/77), done.
remote: Compressing objects: 100% (67/67), done.
remote: Total 77 (delta 22), reused 0 (delta 0)
Unpacking objects: 100% (77/77), done.
user@macos:/git$ cd mdns-scan/
user@macos:/git/mdns-scan$ sudo make
cc -Wall -W -g -O0 -pipe   -c -o mdns-scan.o mdns-scan.c
mdns-scan.c:218:14: warning: unused parameter 'argc' [-Wunused-parameter]
int main(int argc, char*argv[]) {
             ^
mdns-scan.c:218:25: warning: unused parameter 'argv' [-Wunused-parameter]
int main(int argc, char*argv[]) {
                        ^
2 warnings generated.
cc -Wall -W -g -O0 -pipe   -c -o dns.o dns.c
cc -Wall -W -g -O0 -pipe   -c -o query.o query.c
query.c:192:23: error: use of undeclared identifier 'SIOCGIFINDEX'
        if (ioctl(fd, SIOCGIFINDEX, &ifreq[i]) ipi_ifindex = ifreq[i].ifr_ifindex;
                            ~~~~~~~~ ^
query.c:248:41: error: use of undeclared identifier 'SOL_IP'
                if (cmsg->cmsg_level == SOL_IP && cmsg->cmsg_type == IP_TTL) {
                                        ^
3 errors generated.
make: *** [query.o] Error 1
user@macos:/git/mdns-scan$ sudo make install
install mdns-scan /usr/bin/mdns-scan
install: mdns-scan: No such file or directory
make: *** [install] Error 71

代码:

似乎这是触发第一个错误的代码:

if (ioctl(fd, SIOCGIFCONF, &ifconf) < 0) {
    fprintf(stderr, "SIOCGIFCONF failed: %s\n", strerror(errno));
    return -1;
}

图书馆:

来自GNU标准C库:

#include <sys/ioctl.h>
#include <net/if.h>

详细信息:

netdevice(7) man页上:

SIOCGIFINDEX
       Retrieve the interface index of the interface into ifr_ifindex.

来源: http://man7.org/linux/man-pages/man7/netdevice.7.html


因此,基本上,它提供了一种从索引中选择网络控制器接口的方法。但是它特定于Linux,而不是macOS。我能做什么?我可以用什么代码替代此代码,以将该实用程序(可能还有其他)移植到macOS;或添加兼容性?

0 个答案:

没有答案