LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 2476|回复: 13

谁有关于ipv6的小程序给一个?

[复制链接]
发表于 2005-6-3 08:54:37 | 显示全部楼层 |阅读模式
或者相关的程序开发资料
发表于 2005-6-3 10:37:30 | 显示全部楼层
去看看《UNIX网络编程》
回复 支持 反对

使用道具 举报

发表于 2005-6-3 13:55:38 | 显示全部楼层
要涉及协议的还是只要socket程序设计?如果是socket程序设计一般都写成协议无关的,不会专门成IPv6
回复 支持 反对

使用道具 举报

发表于 2005-6-3 13:56:24 | 显示全部楼层
等一下,找我一个经典程序给你。
回复 支持 反对

使用道具 举报

 楼主| 发表于 2005-6-4 10:59:41 | 显示全部楼层
谢谢!涉及协议的最好,贴出来或者发到我的邮箱jinchangjiu@163.com
回复 支持 反对

使用道具 举报

发表于 2005-6-4 14:15:01 | 显示全部楼层
也给我一个如何?呵呵,谢谢了mr_aying@163.com
回复 支持 反对

使用道具 举报

发表于 2005-6-4 19:24:36 | 显示全部楼层
ipdump.c

  1. /* I like BSD style better. */
  2. #define _BSD_SOURCE
  3. #include <errno.h>
  4. #include <sys/types.h>
  5. #include <sys/socket.h>
  6. #include <sys/ioctl.h>
  7. #include <net/if.h>
  8. #include <net/ethernet.h>
  9. #include <netinet/in.h>
  10. #include <netinet/ip.h>
  11. #ifdef IPV6
  12. # include <netinet/ip6.h>
  13. #endif
  14. #include <netinet/tcp.h>
  15. #include <netinet/udp.h>
  16. #include <arpa/inet.h>
  17. #include <unistd.h>
  18. #include <string.h>
  19. #include "ipdump.h"
  20.                                                                                 
  21. #ifdef IPV6
  22. # define IP_PACKET_MAX \
  23.     (sizeof (struct ether_header) + sizeof (struct ip6_hdr) + 65535)
  24. #else
  25. # define IP_PACKET_MAX \
  26.     (sizeof (struct ether_header) + 65535)
  27. #endif
  28.                                                                                 
  29. static char __recvbuf[IP_PACKET_MAX];
  30. const void *__packet_data_ptr;
  31. size_t __packet_data_len;
  32.                                                                                 
  33. static int __proc_tcp_packet(const struct tcphdr *tcp, size_t len,
  34.                              struct ippacket *packet)
  35. {
  36.     if (len >= sizeof (struct tcphdr) && len >= tcp->th_off << 2)
  37.     {
  38.         packet->tcp_seq = ntohl(tcp->th_seq);
  39.         packet->tcp_flags = tcp->th_flags;
  40.         packet->sport = ntohs(tcp->th_sport);
  41.         packet->dport = ntohs(tcp->th_dport);
  42.                                                                                 
  43.         __packet_data_ptr = (const char *)tcp + (tcp->th_off << 2);
  44.         __packet_data_len = len - (tcp->th_off << 2);
  45.         return 0;
  46.     }
  47.     else
  48.     {
  49.         errno = EBADMSG;
  50.         return -1;
  51.     }
  52. }
  53.                                                                                 
  54. static int __proc_udp_packet(const struct udphdr *udp, size_t len,
  55.                              struct ippacket *packet)
  56. {
  57.     if (len >= sizeof (struct udphdr))
  58.     {
  59.         packet->tcp_flags = 0;
  60.         packet->tcp_seq = 0;
  61.         packet->sport = ntohs(udp->uh_sport);
  62.         packet->dport = ntohs(udp->uh_dport);
  63.                                                                                 
  64.         __packet_data_ptr = udp + 1;
  65.         __packet_data_len = len - sizeof (struct udphdr);
  66.         return 0;
  67.     }
  68.     else
  69.     {
  70.         errno = EBADMSG;
  71.         return -1;
  72.     }
  73. }
  74.                                                                                 
  75. static int __proc_other_packet(const void *other, size_t len,
  76.                                struct ippacket *packet)
  77. {
  78.     packet->tcp_flags = 0;
  79.     packet->tcp_seq = 0;
  80.     packet->dport = packet->sport = 0;
  81.                                                                                 
  82.     __packet_data_ptr = other;
  83.     __packet_data_len = len;
  84.     return 0;
  85. }
  86.                                                                                 
  87. inline static int __proc_ip4_packet(const struct ip *ip4, size_t len,
  88.                                     struct ippacket *packet)
  89. {
  90.     if (len >= sizeof (struct ip) && len >= ip4->ip_hl << 2)
  91.     {
  92.         packet->size = ntohs(ip4->ip_len);
  93.         packet->family = AF_INET;
  94.         packet->saddr.in_addr = ip4->ip_src;
  95.         packet->daddr.in_addr = ip4->ip_dst;
  96.         packet->protocol = ip4->ip_p;
  97.                                                                                 
  98.         len -= ip4->ip_hl << 2;
  99.         (const char *)ip4 += ip4->ip_hl << 2;
  100.         if (packet->protocol == IPPROTO_TCP)
  101.             return __proc_tcp_packet((const struct tcphdr *)ip4, len, packet);
  102.         else if (packet->protocol == IPPROTO_UDP)
  103.             return __proc_udp_packet((const struct udphdr *)ip4, len, packet);
  104.         else
  105.             return __proc_other_packet(ip4, len, packet);
  106.     }
  107.     else
  108.     {
  109.         errno = EBADMSG;
  110.         return -1;
  111.     }
  112. }
  113.                                                                                 
  114. #ifdef IPV6
  115. inline static int __proc_ip6_packet(const struct ip6_hdr *ip6, size_t len,
  116.                                     struct ippacket *packet)
  117. {
  118.     if (len >= sizeof (struct ip6_hdr))
  119.     {
  120.         packet->size = ntohs(ip6->ip6_plen);
  121.         packet->family = AF_INET6;
  122.         packet->saddr.in6_addr = ip6->ip6_src;
  123.         packet->daddr.in6_addr = ip6->ip6_dst;
  124.         packet->protocol = ip6->ip6_nxt;
  125.                                                                                 
  126.         len -= sizeof (struct ip6_hdr);
  127.         ip6++;
  128.         if (packet->protocol == IPPROTO_TCP)
  129.             return __proc_tcp_packet((const struct tcphdr *)ip6, len, packet);
  130.         else if (packet->protocol == IPPROTO_UDP)
  131.             return __proc_udp_packet((const struct udphdr *)ip6, len, packet);
  132.         else
  133.             return __proc_other_packet(ip6, len, packet);
  134.     }
  135.     else
  136.     {
  137.         errno = EBADMSG;
  138.         return -1;
  139.     }
  140. }
  141. #endif
  142.                                                                                 
  143. inline static int __proc_ip_packet(const void *ip, size_t len,
  144.                                    struct ippacket *packet)
  145. {
  146.     unsigned char version;
  147.                                                                                 
  148.     if (len > 0)
  149.     {
  150.         version = *(unsigned char *)ip >> 4;
  151.         if (version == 4)
  152.             return __proc_ip4_packet((const struct ip *)ip, len, packet);
  153. #ifdef IPV6
  154.         else if (version == 6)
  155.             return __proc_ip6_packet((const struct ip6_hdr *)ip, len, packet);
  156. #endif
  157.         else
  158.             errno = EAFNOSUPPORT;
  159.     }
  160.     else
  161.         errno = EBADMSG;
  162.                                                                                 
  163.     return -1;
  164. }
  165.                                                                                 
  166. inline static int __mark_packet_dir(const struct ether_header *ethhdr,
  167.                                     int sockfd, const char *ifname,
  168.                                     struct ippacket *packet)
  169. {
  170.     struct ifreq ifreq;
  171.                                                                                 
  172.     strncpy(ifreq.ifr_name, ifname, IFNAMSIZ);
  173.     if (ioctl(sockfd, SIOCGIFHWADDR, &ifreq) >= 0)
  174.     {
  175.         if (memcmp(ifreq.ifr_hwaddr.sa_data, ethhdr->ether_shost,
  176.                    ETH_ALEN) == 0)
  177.             packet->direction = IPPACKET_OUT;
  178.         else
  179.             packet->direction = IPPACKET_IN;
  180.                                                                                 
  181.         return 0;
  182.     }
  183.     else
  184.         return -1;
  185. }
  186.                                                                                 
  187. inline static int __proc_ether_packet(const struct ether_header *ether,
  188.                                       size_t len, int sockfd,
  189.                                       const char *ifname,
  190.                                       struct ippacket *packet)
  191. {
  192.     if (len >= sizeof (struct ether_header))
  193.     {
  194.         if (ntohs(ether->ether_type) == ETHERTYPE_IP)
  195.         {
  196.             if (__proc_ip_packet(ether + 1, len - sizeof (struct ether_header),
  197.                                  packet) >= 0)
  198.             {
  199.                 if (__mark_packet_dir(ether, sockfd, ifname, packet) >= 0)
  200.                     return 0;
  201.             }
  202.         }
  203.         else
  204.             errno = ENOMSG;
  205.     }
  206.     else
  207.         errno = EBADMSG;
  208.                                                                                 
  209.     return -1;
  210. }
  211.                                                                                 
  212. /**
  213. * Read an IP pakcet from "sockfd". Make sure that "sockfd" is a
  214. * Linux packet socket.
  215. */
  216. int recv_ip_packet(int sockfd, struct ippacket *packet)
  217. {
  218.     struct sockaddr sockaddr;
  219.     socklen_t addrlen = sizeof (struct sockaddr);
  220.     ssize_t n = recvfrom(sockfd, __recvbuf, IP_PACKET_MAX, 0,
  221.                          &sockaddr, &addrlen);
  222.                                                                                 
  223.     if (n >= 0)
  224.     {
  225.         n = __proc_ether_packet((struct ether_header *)__recvbuf, n,
  226.                                 sockfd, sockaddr.sa_data, packet);
  227.     }
  228.                                                                                 
  229.     return n;
  230. }
复制代码


ipdump.h

  1. #ifndef _IPDUMP_H_
  2. #define _IPDUMP_H_
  3.                                                                                 
  4. #include <sys/types.h>
  5. #include <sys/socket.h>
  6. #include <netinet/in.h>
  7.                                                                                 
  8. #define IPPACKET_IN         1
  9. #define IPPACKET_OUT        2
  10.                                                                                 
  11. struct ippacket
  12. {
  13.     int direction;
  14.     unsigned short size;
  15.     unsigned short family;
  16.     union
  17.     {
  18.         struct in_addr in_addr;
  19. #ifdef IPV6
  20.         struct in6_addr in6_addr;
  21. #endif
  22.     } saddr, daddr;
  23.     unsigned short sport;
  24.     unsigned short dport;
  25.     unsigned int tcp_seq;
  26.     unsigned char tcp_flags;
  27.     unsigned char protocol;
  28. };
  29.                                                                                 
  30. #ifdef __cplusplus
  31. extern "C"
  32. {
  33. #endif
  34.                                                                                 
  35. int recv_ip_packet(int sockfd, struct ippacket *packet);
  36.                                                                                 
  37. #ifdef __cplusplus
  38. }
  39. #endif
  40.                                                                                 
  41. #endif

复制代码
回复 支持 反对

使用道具 举报

发表于 2005-6-4 19:38:51 | 显示全部楼层
这是去年趋势竞赛题目里的一个模块,其要求是,监视从本机流入和流出的IP包,对每个IP包提取出对方IP地址,上层协议(TCP,UDP,ICMP,OTHERS),对方端口和本地端口(如果上层是TCP或UDP的话),tcp_flags和tcp_seq(如是要上层是TCP的话),包的方向(流入或流出)。我在实现上加入了对IPv6的支持,以及一个隐藏接口用于提取网络包数据,因为另一个模块要监控本机发送与接收的email。
回复 支持 反对

使用道具 举报

发表于 2005-6-4 19:51:19 | 显示全部楼层
这个模块我觉得是一个不错的网络包分析的例子,流程很清晰。
回复 支持 反对

使用道具 举报

发表于 2005-6-4 21:17:55 | 显示全部楼层
Tetris兄这么强,传授点学习方法啊
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表