Changeset 4af54d1 for lib/trace.c
- Timestamp:
- 03/26/06 17:15:32 (16 years ago)
- Branches:
- 4.0.1-hotfixes, cachetimestamps, develop, dpdk-ndag, etsilive, getfragoff, help, libtrace4, master, ndag_format, pfring, rc-4.0.1, rc-4.0.2, rc-4.0.3, rc-4.0.4, ringdecrementfix, ringperformance, ringtimestampfixes
- Children:
- c2f71ad
- Parents:
- c219603
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
lib/trace.c
reea0f87 r4af54d1 696 696 */ 697 697 698 /* get a pointer to the IP header (if any) 699 * @param packet a pointer to a libtrace_packet structure 700 * 701 * @returns a pointer to the IP header, or NULL if there is not an IP packet 702 */ 703 struct libtrace_ip *trace_get_ip(const struct libtrace_packet_t *packet) { 704 struct libtrace_ip *ipptr = 0; 705 706 switch(trace_get_link_type(packet)) { 707 case TRACE_TYPE_80211_PRISM: 708 { 709 struct ieee_802_11_header *wifi = 710 (void*)( 711 (char*)trace_get_link(packet)+144); 712 if (!wifi) { 713 ipptr = NULL; 714 break; 715 } 716 717 /* Data packet? */ 718 if (wifi->type != 2) { 719 ipptr = NULL; 720 } 721 else { 722 struct ieee_802_11_payload *eth = 723 (void*)((char*)wifi+sizeof(struct ieee_802_11_header)); 724 ipptr = NULL; 725 726 if (ntohs(eth->type) == 0x0800) { 727 ipptr=(void*) 728 ((char*)eth + sizeof(*eth)); 729 } else if (ntohs(eth->type) == 0x8100) { 730 struct libtrace_8021q *vlanhdr = 731 (struct libtrace_8021q *)eth; 732 if (ntohs(vlanhdr->vlan_ether_type) 733 == 0x0800) { 734 ipptr=(void*)( 735 (char*)eth+sizeof(*vlanhdr)); 736 } 737 } 738 } 739 } 740 break; 741 case TRACE_TYPE_80211: 742 { 743 744 struct ieee_802_11_header *wifi = trace_get_link(packet); 745 if (!wifi) { 746 ipptr = NULL; 747 break; 748 } 749 750 /* Data packet? */ 751 if (wifi->type != 2) { 752 ipptr = NULL; 753 } 754 else { 755 struct ieee_802_11_payload *eth = 756 (void*)((char*)wifi+sizeof(struct ieee_802_11_header)); 757 ipptr = NULL; 758 759 if (ntohs(eth->type) == 0x0800) { 760 ipptr=(void*)((char*)eth + sizeof(*eth)); 761 } else if (ntohs(eth->type) == 0x8100) { 762 struct libtrace_8021q *vlanhdr = 763 (struct libtrace_8021q *)eth; 764 if (ntohs(vlanhdr->vlan_ether_type) 765 == 0x0800) { 766 ipptr=(void*)((char*)eth + 767 sizeof(*vlanhdr)); 768 } 769 } 770 } 771 } 772 break; 773 case TRACE_TYPE_ETH: 774 case TRACE_TYPE_LEGACY_ETH: 775 { 776 struct libtrace_ether *eth = 777 trace_get_link(packet); 778 if (!eth) { 779 ipptr = NULL; 780 break; 781 } 782 ipptr = NULL; 783 784 if (ntohs(eth->ether_type)==0x0800) { 785 ipptr = (void*)((char *)eth + sizeof(*eth)); 786 } else if (ntohs(eth->ether_type) == 0x8100) { 787 struct libtrace_8021q *vlanhdr = 788 (struct libtrace_8021q *)eth; 789 if (ntohs(vlanhdr->vlan_ether_type) 790 == 0x0800) { 791 ipptr = (void*)((char *)eth + 792 sizeof(*vlanhdr)); 793 } 794 } 795 break; 796 } 797 case TRACE_TYPE_NONE: 798 ipptr = trace_get_link(packet); 799 break; 800 case TRACE_TYPE_LINUX_SLL: 801 { 802 struct trace_sll_header_t *sll; 803 804 sll = trace_get_link(packet); 805 if (!sll) { 806 ipptr = NULL; 807 break; 808 } 809 if (ntohs(sll->protocol)!=0x0800) { 810 ipptr = NULL; 811 } 812 else { 813 ipptr = (void*)((char*)sll+ 814 sizeof(*sll)); 815 } 816 } 817 break; 818 case TRACE_TYPE_PFLOG: 819 { 820 struct trace_pflog_header_t *pflog; 821 pflog = trace_get_link(packet); 822 if (!pflog) { 823 ipptr = NULL; 824 break; 825 } 826 if (pflog->af != AF_INET) { 827 ipptr = NULL; 828 } else { 829 ipptr = (void*)((char*)pflog+ 830 sizeof(*pflog)); 831 } 832 } 833 break; 834 case TRACE_TYPE_LEGACY_POS: 835 { 836 /* 64 byte capture. */ 837 struct libtrace_pos *pos = 838 trace_get_link(packet); 839 if (ntohs(pos->ether_type) == 0x0800) { 840 ipptr=(void*)((char *)pos+sizeof(*pos)); 841 } else { 842 ipptr=NULL; 843 } 844 break; 845 846 } 847 case TRACE_TYPE_LEGACY_ATM: 848 case TRACE_TYPE_ATM: 849 { 850 /* 64 byte capture. */ 851 struct libtrace_llcsnap *llc = 852 trace_get_link(packet); 853 854 /* advance the llc ptr +4 into the link layer. 855 * need to check what is in these 4 bytes. 856 * don't have time! 857 */ 858 llc = (void*)((char *)llc + 4); 859 if (ntohs(llc->type) == 0x0800) { 860 ipptr=(void*)((char*)llc+sizeof(*llc)); 861 } else { 862 ipptr = NULL; 863 } 864 break; 865 } 866 default: 867 fprintf(stderr,"Don't understand link layer type %i in trace_get_ip()\n", 868 trace_get_link_type(packet)); 869 ipptr=NULL; 870 break; 871 } 872 873 return ipptr; 874 } 875 876 #define SW_IP_OFFMASK 0xff1f 877 878 /* Gets a pointer to the transport layer header (if any) 879 * @param packet a pointer to a libtrace_packet structure 880 * 881 * @returns a pointer to the transport layer header, or NULL if there is no header 882 */ 883 void *trace_get_transport(const struct libtrace_packet_t *packet) { 884 void *trans_ptr = 0; 885 struct libtrace_ip *ipptr = 0; 886 887 if (!(ipptr = trace_get_ip(packet))) { 888 return 0; 889 } 890 891 if ((ipptr->ip_off & SW_IP_OFFMASK) == 0) { 892 trans_ptr = (void *)((ptrdiff_t)ipptr + (ipptr->ip_hl * 4)); 893 } 894 return trans_ptr; 895 } 896 897 /* Gets a pointer to the transport layer header (if any) given a pointer to the 898 * IP header 899 * @param ip The IP Header 900 * @param[out] skipped An output variable of the number of bytes skipped 901 * 902 * @returns a pointer to the transport layer header, or NULL if there is no header 903 * 904 * Skipped can be NULL, in which case it will be ignored 905 */ 906 void *trace_get_transport_from_ip(const libtrace_ip_t *ip, int *skipped) { 907 void *trans_ptr = 0; 908 909 if ((ip->ip_off & SW_IP_OFFMASK) == 0) { 910 trans_ptr = (void *)((ptrdiff_t)ip + (ip->ip_hl * 4)); 911 } 912 913 if (skipped) 914 *skipped = (ip->ip_hl*4); 915 916 return trans_ptr; 917 } 918 919 /* get a pointer to the TCP header (if any) 920 * @param packet a pointer to a libtrace_packet structure 921 * 922 * @returns a pointer to the TCP header, or NULL if there is not a TCP packet 923 */ 924 libtrace_tcp_t *trace_get_tcp(const libtrace_packet_t *packet) { 925 struct libtrace_tcp *tcpptr = 0; 926 struct libtrace_ip *ipptr = 0; 927 928 if(!(ipptr = trace_get_ip(packet))) { 929 return 0; 930 } 931 if (ipptr->ip_p == 6) { 932 tcpptr = (struct libtrace_tcp *)trace_get_transport_from_ip(ipptr, 0); 933 } 934 return tcpptr; 935 } 936 937 /* get a pointer to the TCP header (if any) given a pointer to the IP header 938 * @param ip The IP header 939 * @param[out] skipped An output variable of the number of bytes skipped 940 * 941 * @returns a pointer to the TCP header, or NULL if this is not a TCP packet 942 * 943 * Skipped can be NULL, in which case it will be ignored by the program. 944 */ 945 libtrace_tcp_t *trace_get_tcp_from_ip(const libtrace_ip_t *ip, int *skipped) 946 { 947 struct libtrace_tcp *tcpptr = 0; 948 949 if (ip->ip_p == 6) { 950 tcpptr = (struct libtrace_tcp *)trace_get_transport_from_ip(ip, skipped); 951 } 952 953 return tcpptr; 954 } 955 956 /* get a pointer to the UDP header (if any) 957 * @param packet a pointer to a libtrace_packet structure 958 * 959 * @returns a pointer to the UDP header, or NULL if this is not a UDP packet 960 */ 961 struct libtrace_udp *trace_get_udp(const struct libtrace_packet_t *packet) { 962 struct libtrace_udp *udpptr = 0; 963 struct libtrace_ip *ipptr = 0; 964 965 if(!(ipptr = trace_get_ip(packet))) { 966 return 0; 967 } 968 if (ipptr->ip_p == 17) { 969 udpptr = (struct libtrace_udp *)trace_get_transport_from_ip(ipptr, 0); 970 } 971 972 return udpptr; 973 } 974 975 /* get a pointer to the UDP header (if any) given a pointer to the IP header 976 * @param ip The IP header 977 * @param[out] skipped An output variable of the number of bytes skipped 978 * 979 * @returns a pointer to the UDP header, or NULL if this is not a UDP packet 980 * 981 * Skipped can be NULL, in which case it will be ignored by the program. 982 */ 983 struct libtrace_udp *trace_get_udp_from_ip(const struct libtrace_ip *ip, int *skipped) 984 { 985 struct libtrace_udp *udpptr = 0; 986 987 if (ip->ip_p == 17) { 988 udpptr = (struct libtrace_udp *)trace_get_transport_from_ip(ip, skipped); 989 } 990 991 return udpptr; 992 } 993 994 995 /* get a pointer to the ICMP header (if any) 996 * @param packet a pointer to a libtrace_packet structure 997 * 998 * @returns a pointer to the ICMP header, or NULL if this is not a ICMP packet 999 */ 1000 struct libtrace_icmp *trace_get_icmp(const struct libtrace_packet_t *packet) { 1001 struct libtrace_icmp *icmpptr = 0; 1002 struct libtrace_ip *ipptr = 0; 1003 1004 if(!(ipptr = trace_get_ip(packet))) { 1005 return 0; 1006 } 1007 if (ipptr->ip_p == 1){ 1008 icmpptr = (struct libtrace_icmp *)trace_get_transport_from_ip(ipptr, 0); 1009 } 1010 return icmpptr; 1011 } 1012 1013 /* get a pointer to the ICMP header (if any) given a pointer to the IP header 1014 * @param ip The IP header 1015 * @param[out] skipped An output variable of the number of bytes skipped 1016 * 1017 * @returns a pointer to the ICMP header, or NULL if this is not a ICMP packet 1018 * 1019 * Skipped can be NULL, in which case it will be ignored by the program. 1020 */ 1021 struct libtrace_icmp *trace_get_icmp_from_ip(const struct libtrace_ip *ip, int *skipped) 1022 { 1023 struct libtrace_icmp *icmpptr = 0; 1024 1025 if (ip->ip_p == 1) { 1026 icmpptr = (struct libtrace_icmp *)trace_get_transport_from_ip(ip, skipped); 1027 } 1028 1029 return icmpptr; 1030 } 698 699 700 1031 701 /* parse an ip or tcp option 1032 702 * @param[in,out] ptr the pointer to the current option … … 1243 913 uint8_t *trace_get_source_mac(const struct libtrace_packet_t *packet) { 1244 914 void *link = trace_get_link(packet); 1245 struct ieee_802_11_header*wifi = link;1246 struct libtrace_ether*ethptr = link;915 libtrace_80211_t *wifi = link; 916 libtrace_ether_t *ethptr = link; 1247 917 if (!link) 1248 918 return NULL; … … 1266 936 uint8_t *trace_get_destination_mac(const struct libtrace_packet_t *packet) { 1267 937 void *link = trace_get_link(packet); 1268 struct ieee_802_11_header*wifi = link;1269 struct libtrace_ether*ethptr = link;938 libtrace_80211_t *wifi = link; 939 libtrace_ether_t *ethptr = link; 1270 940 if (!link) 1271 941 return NULL; … … 1455 1125 uint16_t trace_get_source_port(const struct libtrace_packet_t *packet) 1456 1126 { 1457 struct libtrace_ip *ip = trace_get_ip(packet); 1458 struct ports_t *port; 1459 if (6 != ip->ip_p 1460 && 17 != ip->ip_p) 1461 return 0; 1462 if (0 != (ip->ip_off & SW_IP_OFFMASK)) 1463 return 0; 1464 1465 port = (struct ports_t *)((ptrdiff_t)ip + (ip->ip_hl * 4)); 1127 struct ports_t *port = trace_get_transport(packet); 1466 1128 1467 1129 return ntohs(port->src); … … 1471 1133 uint16_t trace_get_destination_port(const struct libtrace_packet_t *packet) 1472 1134 { 1473 struct libtrace_ip *ip = trace_get_ip(packet); 1474 struct ports_t *port; 1475 1476 if (6 != ip->ip_p 1477 && 17 != ip->ip_p) 1478 return 0; 1479 1480 if (0 != (ip->ip_off & SW_IP_OFFMASK)) 1481 return 0; 1482 1483 port = (struct ports_t *)((ptrdiff_t)ip + (ip->ip_hl * 4)); 1135 struct ports_t *port = trace_get_transport(packet); 1484 1136 1485 1137 return ntohs(port->dst); … … 1818 1470 return buf2; 1819 1471 } 1472
Note: See TracChangeset
for help on using the changeset viewer.