使用带有gcc的newlib来路由套接字(unix网络编程)

时间:2011-03-21 15:55:42

标签: c unix routing network-programming

我从一本名为Unix网络编程的第3版开始学习 我写了下面的代码,我发现它需要一个名为if_dl.h的库 我没有,因为我在ubuntu工作所以我安装了lib-newlib并使用它..

但是在编译时我得到很多错误,这些错误引用了newlib喜欢的头文件 /usr/lib/newlib/i486-linux-gnu/include/net/route.h:54:错误:字段ro_dst的类型不完整 和rt_dst和rt_gateway相同 但还有另一个问题...... 代码使用sa_len,假定它是sockaddr结构中的成员。 但是sockaddr只有sa_family,sa_data成员.. 那么我需要在freeBSD上工作的问题还是什么?

我正在谈论的代码

   #include <stdio.h>
   #include <stdlib.h>
   #include <net/route.h>
   #include <net/if.h>
   #include <net/if_dl.h>
   #include <netinet/in.h>
   #include <sys/socket.h>
   #include <unistd.h>

 2 #define BUFLEN   (sizeof(struct rt_msghdr) + 512)
 3                      /* sizeof(struct sockaddr_in6) * 8 = 192 */
 4 #define SEQ      9999

 5 int
 6 main(int argc, char **argv)
 7 {
 8     int     sockfd;
 9     char   *buf;
10     pid_t   pid;
11     ssize_t n;
12     struct rt_msghdr *rtm;
13     struct sockaddr *sa, *rti_info[RTAX_MAX];
14     struct sockaddr_in *sin;

15     if (argc != 2)
16         err_quit("usage: getrt <IPaddress>");

17     sockfd = Socket(AF_ROUTE, SOCK_RAW, 0); /* need superuser privileges */

18     buf = Calloc(1, BUFLEN);     /* and initialized to 0 */

19     rtm = (struct rt_msghdr *) buf;
20     rtm->rtm_msglen = sizeof(struct rt_msghdr) + sizeof(struct sockaddr_in);
21     rtm->rtm_version = RTM_VERSION;
22     rtm->rtm_type = RTM_GET;
23     rtm->rtm_addrs = RTA_DST;
24     rtm->rtm_pid = pid = getpid();
25     rtm->rtm_seq = SEQ;

26     sin = (struct sockaddr_in *) (rtm + 1);
27     sin->sin_len = sizeof(struct sockaddr_in);
28     sin->sin_family = AF_INET;
29     Inet_pton(AF_INET, argv[1], &sin->sin_addr);

30     Write(sockfd, rtm, rtm->rtm_msglen);

31     do {
32         n = Read(sockfd, rtm, BUFLEN);
33     } while (rtm->rtm_type != RTM_GET || rtm->rtm_seq != SEQ ||
34              rtm->rtm_pid != pid);
35     rtm = (struct rt_msghdr *) buf;
36     sa = (struct sockaddr *) (rtm + 1);
37     get_rtaddrs(rtm->rtm_addrs, sa, rti_info);
38     if ( (sa = rti_info[RTAX_DST]) != NULL)
39         printf("dest: %s\n", Sock_ntop_host(sa, sa->sa_len));

40     if ( (sa = rti_info[RTAX_GATEWAY]) != NULL)
41         printf("gateway: %s\n", Sock_ntop_host(sa, sa->sa_len));

42     if ( (sa = rti_info[RTAX_NETMASK]) != NULL)
43         printf("netmask: %s\n", Sock_masktop(sa, sa->sa_len));

44     if ( (sa = rti_info[RTAX_GENMASK]) != NULL)
45         printf("genmask: %s\n", Sock_masktop(sa, sa->sa_len));

46     exit(0);
47 }

2 /*
 3  * Round up 'a' to next multiple of 'size', which must be a power of 2
 4  */
 5 #define ROUNDUP(a, size) (((a) & ((size)-1)) ? (1 + ((a) | ((size)-1))) : (a))
 6 /*
 7  * Step to next socket address structure;
 8  * if sa_len is 0, assume it is sizeof(u_long).
 9  */
10 #define NEXT_SA(ap) ap = (SA *) \
11     ((caddr_t) ap + (ap->sa_len ? ROUNDUP(ap->sa_len, sizeof (u_long)) : \
12                                        sizeof(u_long)))
13 void
14 get_rtaddrs(int addrs, SA *sa, SA **rti_info)
15 {
16     int     i;
17     for (i = 0; i < RTAX_MAX; i++) {
18         if (addrs & (1 << i)) {
19             rti_info[i] = sa;
20             NEXT_SA(sa);
21         } else
22             rti_info[i] = NULL;
23     }
24 }

 2 const char *
 3 sock_masktop(SA *sa, socklen_t salen)
 4 {
 5     static char str[INET6_ADDRSTRLEN];
 6     unsigned char *ptr = &sa->sa_data[2];
 7     if (sa->sa_len == 0)
 8         return ("0.0.0.0");
 9     else if (sa->sa_len == 5)
10         snprintf(str, sizeof(str), "%d.0.0.0", *ptr);
11     else if (sa->sa_len == 6)
12         snprintf(str, sizeof(str), "%d.%d.0.0", *ptr, *(ptr + 1));
13     else if (sa->sa_len == 7)
14         snprintf(str, sizeof(str), "%d.%d.%d.0", *ptr, *(ptr + 1),
15                  *(ptr + 2));
16     else if (sa->sa_len == 8)
17         snprintf(str, sizeof(str), "%d.%d.%d.%d",
18                  *ptr, *(ptr + 1), *(ptr + 2), *(ptr + 3));
19     else
20         snprintf(str, sizeof(str), "(unknown mask, len = %d, family = %d)",
21                  sa->sa_len, sa->sa_family);
22     return (str);
23 }

2 个答案:

答案 0 :(得分:1)

该代码不适用于Linux。第486页暗示史蒂文斯在他的Solaris,AIX和FreeBSD测试机器上测试它(我们希望它可以工作)。 Linux有AF_NETLINK,它应该是等价的,但与BSD AF_ROUTE有一些细微的差别。

在linux上使用<linux/netlink.h>中的定义而不是<net/if_dl.h>。请参阅重复的问题What package do i need to install for using routing sockets?

正如您所发现的那样,sa_len不存在于linux上(事实上它也不在Solaris上),因此返回的地址的所有字节都是有效的。这适用于sock_masktop

const char * sock_masktop(SA *sa, socklen_t salen) {
  static char str[INET6_ADDRSTRLEN];
  unsigned char *ptr = &sa->sa_data[2];
  snprintf(str, sizeof(str), "%d.%d.%d.%d",
           *ptr, *(ptr + 1), *(ptr + 2), *(ptr + 3));
  return (str);
}

答案 1 :(得分:-1)

为什么使用newlib(用于嵌入式使用的C库实现)而不是glibc?

尝试使用glibc编译它(如果没有安装libc6-dev,应该与你的ubuntu发行版一起安装)