|
|

楼主 |
发表于 2006-10-31 15:29:28
|
显示全部楼层
- #include <features.h> /* 需要里面的 glibc 版本号 */
- #if __GLIBC__ >= 2 && __GLIBC_MINOR__ >= 1
- #include <netpacket/packet.h>
- #include <net/ethernet.h> /* 链路层(L2)协议 */
- #else
- #include <asm/types.h>
- #include <linux/if_packet.h>
- #include <linux/if_ether.h> /* 链路层协议 */
- #endif
- #include<stdio.h>
- #include<unistd.h>
- #include<stdlib.h>
- #include<string.h>
- #include<sys/socket.h>
- #include<netinet/in.h>
- #include<sys/types.h>
- #include<sys/ioctl.h>
- #include<net/if.h>
- #include<netinet/ip.h>
- #include<err.h>
- #include<netdb.h>
- #include<fcntl.h>
- #include<linux/netlink.h>
- #include<linux/rtnetlink.h>
- #include<net/route.h>
- int print_route(struct rtentry *rt);
- int main(void)
- {
-
- /** char buf[BUFSIZ];
- int fd;
- int n;
- fd = open ("/proc/net/route", O_RDONLY);
- if (fd < 0) {
- perror("open error");
- exit (1);
- }
- n = read(fd, buf , BUFSIZ-1);
-
- if (n < 0) {
- perror("read error");
- exit (1);
- }
- printf("%s",buf);
- **/
- char *buf[BUFSIZ];
- int fd;
- int n;
- struct rtmsg rtreq;
- struct sockaddr_nl nl;
- struct nlmsghdr msghdr, *msghdrp;
-
- struct sockaddr addr;
- char *c;
- struct rtentry *entryp;
- struct msghdr msg, recev;
- struct iovec iov;
- int len;
-
- fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
- if (fd < 0) {
- perror("open error");
- exit (1);
- }
- nl.nl_family = AF_NETLINK;
- nl.nl_pad = 0;
- nl.nl_pid = getpid();
- nl.nl_groups = 0;
-
- n = bind(fd, (struct sockaddr*)&nl, sizeof(nl));
- if (n < 0) {
- perror(" bind error");
- exit (1);
- }
- memset(&nl, 0, sizeof(struct rtmsg));
- rtreq.rtm_family = AF_NETLINK;
- rtreq.rtm_table = RT_SCOPE_UNIVERSE;
-
- memset(&msghdr, 0, sizeof(msghdr));
- msghdr.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
- msghdr.nlmsg_type = RTM_GETROUTE;
- msghdr.nlmsg_flags = NLM_F_MATCH|NLM_F_REQUEST;
- msghdr.nlmsg_pid = nl.nl_pid;
-
- iov.iov_base = (void*)&msghdr;
- iov.iov_len = msghdr.nlmsg_len;
- msg.msg_iov = &iov;
- msg.msg_iovlen = 1;
-
- len = sendmsg(fd, &msg, 0);
- if (len < 0)
- {
- perror("send error!");
- exit (-1);
- }
- else
- printf("%d bytes send!\n",len);
- msghdrp = (struct nlmsghdr *)buf;
-
- iov.iov_base = (void *)buf;
- iov.iov_len = BUFSIZ;
- recev.msg_name = (void*)&msghdrp;
- recev.msg_namelen = sizeof (struct nlmsghdr);
- recev.msg_iov = &iov;
- recev.msg_iovlen = 1;
- len = recvmsg(fd, &recev, 0);
- // len = recv(fd, buf, sizeof(buf)-sizeof(struct nlmsghdr),0);
- if (len < 0)
- perror("received failed!");
- printf("%d bytes received!\n", len);
-
- for (msghdrp = (struct nlmsghdr*)buf; \
- (msghdrp->nlmsg_type != NLMSG_DONE)&& NLMSG_OK(msghdrp, len); \
- msghdrp = NLMSG_NEXT(msghdrp, len)) {
- entryp = NLMSG_DATA(msghdrp);
- print_route(entryp);
-
- }
- }
- int print_route(struct rtentry *rt)
- {
- struct sockaddr addr;
- char *c;
- c = inet_ntop (AF_INET, &(rt->rt_dst), &addr, INET_ADDRSTRLEN);
- if (!c) {
- perror("inet_ntop failed !");
- exit (1);
- }
- printf("rt_dst :%s\n",c);
- c = inet_ntop (AF_INET, &(rt->rt_gateway), &addr, INET_ADDRSTRLEN);
- if (!c) {
- perror("inet_ntop failed !");
- exit (1);
- }
- printf("rt_gateway :%s\n",c);
- c = inet_ntop (AF_INET, &(rt->rt_genmask), &addr, INET_ADDRSTRLEN);
- if (!c) {
- perror("inet_ntop failed !");
- exit (1);
- }
- printf("rt_genmask :%s\n",c);
- }
复制代码 我上面的代码有问题,这个是我刚修改的,读是出来了,可数据和命令输出不一样。
楼上的没看清我以前的恢复,那个IOCTL在我机器上不行,总返回无效参数。 |
|