请优化这段代码,执行ioctl报错无效的参数:#include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <sys/socket.h> #include <sys/ioctl.h> #include <netinet/in.h> #include <net/if.h> #include <arpa/inet.h> #include <linux/route.h> int main() { int fd; struct rtentry route; struct sockaddr_in *addr; memset(&route, 0, sizeof(route)); route.rt_dst.sa_family = AF_INET; addr = (struct sockaddr_in *)&route.rt_gateway; addr->sin_family = AF_INET; fd = socket(AF_INET, SOCK_DGRAM, 0); if (ioctl(fd, SIOCRTMSG, &route) == -1) { perror("ioctl"); exit(1); } close(fd); printf("Gateway address: %s\n", inet_ntoa(addr->sin_addr)); return 0; }
时间: 2024-03-10 19:50:09 浏览: 269
这段代码的问题在于使用了已经过时的 SIOCRTMSG ioctl 命令,并且没有对错误进行恰当的处理。可以使用更现代化的方法,如使用 netlink socket 来获取网关地址。以下是更新后的代码:
```c++
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <linux/netlink.h>
#include <linux/rtnetlink.h>
#include <arpa/inet.h>
#define MAX_PAYLOAD 1024
int main() {
int sockfd;
struct sockaddr_nl sa;
struct nlmsghdr *nlh;
char buffer[MAX_PAYLOAD];
struct rtmsg *rtm;
struct rtattr *rta;
int len;
memset(&sa, 0, sizeof(sa));
sa.nl_family = AF_NETLINK;
nlh = (struct nlmsghdr *)buffer;
rtm = (struct rtmsg *)NLMSG_DATA(nlh);
nlh->nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
nlh->nlmsg_type = RTM_GETROUTE;
nlh->nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
sockfd = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
if (sockfd == -1) {
perror("socket");
exit(1);
}
if (bind(sockfd, (struct sockaddr *)&sa, sizeof(sa)) == -1) {
perror("bind");
exit(1);
}
if (send(sockfd, nlh, nlh->nlmsg_len, 0) == -1) {
perror("send");
exit(1);
}
while ((len = recv(sockfd, buffer, MAX_PAYLOAD, 0)) > 0) {
for (nlh = (struct nlmsghdr *)buffer; NLMSG_OK(nlh, len); nlh = NLMSG_NEXT(nlh, len)) {
if (nlh->nlmsg_type == NLMSG_DONE) {
break;
} else if (nlh->nlmsg_type == NLMSG_ERROR) {
perror("recv");
exit(1);
} else if (nlh->nlmsg_type == RTM_NEWROUTE) {
rtm = (struct rtmsg *)NLMSG_DATA(nlh);
if (rtm->rtm_table != RT_TABLE_MAIN) {
continue;
}
rta = (struct rtattr *)RTM_RTA(rtm);
len = RTM_PAYLOAD(nlh);
for (; RTA_OK(rta, len); rta = RTA_NEXT(rta, len)) {
if (rta->rta_type == RTA_GATEWAY) {
struct in_addr *addr = (struct in_addr *)RTA_DATA(rta);
printf("Gateway address: %s\n", inet_ntoa(*addr));
close(sockfd);
return 0;
}
}
}
}
}
close(sockfd);
return 0;
}
```
这个程序使用 netlink socket 发送 RTM_GETROUTE 请求来获取路由表,并遍历路由表中的每个条目,找到目标路由条目,并从中提取出网关地址。此方法可以避免使用已经过时的 ioctl 命令,并且可以正确地处理错误。
阅读全文