簡體   English   中英

將newlib與gcc一起使用以路由套接字(unix網絡編程)

[英]using newlib with gcc for routing sockets (unix network programming)

我從一本名為《 Unix網絡編程第三版》的書中學習,我編寫了以下代碼,我發現它需要一個名為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 }

該代碼在Linux上不起作用。 在第486頁上暗示Stevens在他的Solaris,AIX和FreeBSD測試計算機上(我們希望它可以在其中運行)對其進行測試。 Linux具有AF_NETLINK,應該被認為是等效的,但與BSD AF_ROUTE確實有一些細微的差異。

<linux/netlink.h>使用定義,而不是在<linux/netlink.h>使用<net/if_dl.h> 看到重復的問題,使用路由套接字需要安裝什么軟件包?

如您所見,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);
}

為什么使用newlib(供嵌入式使用的C庫實現)而不是glibc?

嘗試使用glibc進行編譯(如果未安裝libc6-dev,則應隨ubuntu發行版一起安裝)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM