- Timestamp:
- 07/01/18 00:59:20 (3 years ago)
- Branches:
- cachetimestamps, develop, master, rc-4.0.4, ringdecrementfix, ringperformance
- Children:
- 8f5d454
- Parents:
- fc6def2
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
lib/format_pcapng.c
rfc6def2 re9da777 341 341 } 342 342 343 static inline int skip_block(libtrace_t *libtrace, uint32_t toread) {344 int err;345 346 while (toread > 0) {347 char buf[4096];348 int nextread;349 350 if (toread < 4096) {351 nextread = toread;352 } else {353 nextread = 4096;354 }355 356 err = wandio_read(libtrace->io, buf, nextread);357 if (err < 0) {358 trace_set_err(libtrace, TRACE_ERR_WANDIO_FAILED,359 "Reading section header options");360 return -1;361 }362 if (err == 0) {363 return 0;364 }365 toread -= err;366 }367 368 return 1;369 370 }371 372 343 static inline int pcapng_read_body(libtrace_t *libtrace, char *body, 373 344 uint32_t to_read) { … … 378 349 if (err < 0) { 379 350 trace_set_err(libtrace, TRACE_ERR_WANDIO_FAILED, 380 "Failed to read pcapng interface options");351 "Failed reading pcapng block"); 381 352 return err; 382 353 } … … 388 359 if (err < (int)to_read) { 389 360 trace_set_err(libtrace, TRACE_ERR_BAD_PACKET, 390 "Incomplete pcapng interface headerblock");361 "Incomplete pcapng block"); 391 362 return -1; 392 363 } … … 516 487 /* Read all of the options etc. -- we don't need them for now, but 517 488 * we have to skip forward to the next useful header. */ 518 bodyptr = packet->buffer + sizeof(pcapng_sec_t);489 bodyptr = (char *) packet->buffer + sizeof(pcapng_sec_t); 519 490 err = pcapng_read_body(libtrace, bodyptr, to_read); 520 491 if (err <= 0) { … … 532 503 533 504 static int pcapng_read_interface(libtrace_t *libtrace, 534 libtrace_packet_t *packet, uint32_t flags) {505 libtrace_packet_t *packet, uint32_t blocklen, uint32_t flags) { 535 506 536 507 pcapng_int_t *inthdr; 537 int err;538 uint32_t to_read;539 508 pcapng_interface_t *newint; 540 509 uint16_t optcode, optlen; … … 542 511 char *bodyptr = NULL; 543 512 544 err = wandio_read(libtrace->io, packet->buffer, sizeof(pcapng_int_t)); 545 546 if (err < 0) { 547 trace_set_err(libtrace, TRACE_ERR_WANDIO_FAILED, 548 "Reading pcapng interface header block"); 549 return -1; 550 } 551 552 if (err == 0) { 553 return 0; 554 } 555 556 if (err < (int)sizeof(pcapng_int_t)) { 513 if (blocklen < sizeof(pcapng_int_t) + 4) { 557 514 trace_set_err(libtrace, TRACE_ERR_BAD_PACKET, 558 515 "Incomplete pcapng interface header block"); … … 577 534 newint->snaplen = byteswap32(inthdr->snaplen); 578 535 newint->linktype = byteswap16(inthdr->linktype); 579 to_read = byteswap32(inthdr->blocklen) - sizeof(pcapng_int_t);580 536 } else { 581 537 assert(inthdr->blocktype == PCAPNG_INTERFACE_TYPE); 582 538 newint->snaplen = inthdr->snaplen; 583 539 newint->linktype = inthdr->linktype; 584 to_read = inthdr->blocklen - sizeof(pcapng_int_t);585 540 } 586 541 … … 597 552 DATA(libtrace)->nextintid += 1; 598 553 599 bodyptr = packet->buffer + sizeof(pcapng_int_t); 600 err = pcapng_read_body(libtrace, bodyptr, to_read); 601 if (err <= 0) { 602 return err; 603 } 554 bodyptr = (char *) packet->buffer + sizeof(pcapng_int_t); 604 555 605 556 packet->type = TRACE_RT_PCAPNG_META; … … 632 583 } while (optcode != 0); 633 584 634 return 1;585 return (int) blocklen; 635 586 636 587 } 637 588 638 589 static int pcapng_read_nrb(libtrace_t *libtrace, libtrace_packet_t *packet, 639 uint32_t flags) {590 uint32_t blocklen, uint32_t flags) { 640 591 641 592 /* Just read the NR records and pass them off to the caller. If … … 644 595 */ 645 596 pcapng_nrb_t *hdr = NULL; 646 int err; 647 uint32_t to_read; 648 char *bodyptr; 649 650 err = wandio_read(libtrace->io, packet->buffer, sizeof(pcapng_nrb_t)); 651 652 if (err < 0) { 653 trace_set_err(libtrace, TRACE_ERR_WANDIO_FAILED, "reading pcapng name resolution block"); 654 return -1; 655 } 656 657 if (err == 0) { 658 return 0; 659 } 660 661 if (err < (int)sizeof(pcapng_nrb_t)) { 597 598 if (blocklen < sizeof(pcapng_nrb_t) + 4) { 662 599 trace_set_err(libtrace, TRACE_ERR_BAD_PACKET, 663 600 "Incomplete pcapng name resolution block"); … … 670 607 if (DATA(libtrace)->byteswapped) { 671 608 assert(byteswap32(hdr->blocktype) == PCAPNG_NAME_RESOLUTION_TYPE); 672 to_read = byteswap32(hdr->blocklen) - sizeof(pcapng_nrb_t);673 609 } else { 674 610 assert(hdr->blocktype == PCAPNG_NAME_RESOLUTION_TYPE); 675 to_read = hdr->blocklen - sizeof(pcapng_nrb_t);676 }677 678 bodyptr = packet->buffer + sizeof(pcapng_nrb_t);679 err = pcapng_read_body(libtrace, bodyptr, to_read);680 if (err <= 0) {681 return err;682 611 } 683 612 … … 688 617 } 689 618 690 return sizeof(pcapng_nrb_t) + to_read;619 return (int) blocklen; 691 620 692 621 } 693 622 694 623 static int pcapng_read_custom(libtrace_t *libtrace, libtrace_packet_t *packet, 695 uint32_t flags) {624 uint32_t blocklen, uint32_t flags) { 696 625 697 626 /* Just read the custom records and pass them off to the caller. If … … 700 629 */ 701 630 pcapng_custom_t *hdr = NULL; 702 int err; 703 uint32_t to_read; 704 char *bodyptr; 705 706 err = wandio_read(libtrace->io, packet->buffer, sizeof(pcapng_custom_t)); 707 708 if (err < 0) { 709 trace_set_err(libtrace, TRACE_ERR_WANDIO_FAILED, "reading pcapng custom block"); 710 return -1; 711 } 712 713 if (err == 0) { 714 return 0; 715 } 716 717 if (err < (int)sizeof(pcapng_custom_t)) { 631 632 if (blocklen < sizeof(pcapng_custom_t) + 4) { 718 633 trace_set_err(libtrace, TRACE_ERR_BAD_PACKET, 719 634 "Incomplete pcapng custom block"); … … 727 642 assert(byteswap32(hdr->blocktype) == PCAPNG_CUSTOM_TYPE || 728 643 byteswap32(hdr->blocktype) == PCAPNG_CUSTOM_NONCOPY_TYPE); 729 to_read = byteswap32(hdr->blocklen) - sizeof(pcapng_custom_t);730 644 } else { 731 645 assert(hdr->blocktype == PCAPNG_CUSTOM_TYPE || 732 646 hdr->blocktype == PCAPNG_CUSTOM_NONCOPY_TYPE); 733 to_read = hdr->blocklen - sizeof(pcapng_custom_t);734 }735 736 bodyptr = packet->buffer + sizeof(pcapng_custom_t);737 err = pcapng_read_body(libtrace, bodyptr, to_read);738 if (err <= 0) {739 return err;740 647 } 741 648 … … 746 653 } 747 654 748 return sizeof(pcapng_custom_t) + to_read;655 return (int) blocklen; 749 656 750 657 } 751 658 752 659 static int pcapng_read_stats(libtrace_t *libtrace, libtrace_packet_t *packet, 753 uint32_t flags) {660 uint32_t blocklen, uint32_t flags) { 754 661 pcapng_stats_t *hdr = NULL; 755 int err;756 uint32_t to_read;757 662 uint32_t ifaceid; 758 663 uint64_t timestamp; … … 762 667 char *bodyptr; 763 668 764 err = wandio_read(libtrace->io, packet->buffer, sizeof(pcapng_stats_t)); 765 766 if (err < 0) { 767 trace_set_err(libtrace, TRACE_ERR_WANDIO_FAILED, "reading pcapng interface stats"); 768 return -1; 769 } 770 771 if (err == 0) { 772 return 0; 773 } 774 775 if (err < (int)sizeof(pcapng_stats_t)) { 669 if (blocklen < sizeof(pcapng_stats_t) + 4) { 776 670 trace_set_err(libtrace, TRACE_ERR_BAD_PACKET, 777 671 "Incomplete pcapng interface stats header"); … … 784 678 if (DATA(libtrace)->byteswapped) { 785 679 assert(byteswap32(hdr->blocktype) == PCAPNG_INTERFACE_STATS_TYPE); 786 to_read = byteswap32(hdr->blocklen) - sizeof(pcapng_stats_t);787 680 ifaceid = byteswap32(hdr->interfaceid); 788 681 timestamp = ((uint64_t)(byteswap32(hdr->timestamp_high)) << 32) + byteswap32(hdr->timestamp_low); 789 682 } else { 790 683 assert(hdr->blocktype == PCAPNG_INTERFACE_STATS_TYPE); 791 to_read = hdr->blocklen - sizeof(pcapng_stats_t);792 684 ifaceid = hdr->interfaceid; 793 685 timestamp = ((uint64_t)(hdr->timestamp_high) << 32) + 794 686 hdr->timestamp_low; 795 }796 797 bodyptr = packet->buffer + sizeof(pcapng_stats_t);798 err = pcapng_read_body(libtrace, bodyptr, to_read);799 if (err <= 0) {800 return err;801 687 } 802 688 … … 815 701 816 702 if (timestamp < interface->laststats) { 817 return sizeof(pcapng_stats_t) + to_read;703 return (int) blocklen; 818 704 } 819 705 … … 869 755 interface->laststats = timestamp; 870 756 871 return sizeof(pcapng_stats_t) + to_read;757 return (int) blocklen; 872 758 873 759 } 874 760 875 761 static int pcapng_read_simple(libtrace_t *libtrace, libtrace_packet_t *packet, 876 uint32_t flags) { 877 878 int err; 879 uint32_t to_read; 762 uint32_t blocklen, uint32_t flags) { 763 880 764 uint32_t caplen; 881 765 pcapng_spkt_t *hdr = NULL; 882 766 pcapng_interface_t *interface; 883 char *bodyptr; 884 885 err = wandio_read(libtrace->io, packet->buffer, sizeof(pcapng_spkt_t)); 886 887 if (err < 0) { 888 trace_set_err(libtrace, TRACE_ERR_WANDIO_FAILED, "reading pcapng simple packet"); 889 return -1; 890 } 891 892 if (err == 0) { 893 return 0; 894 } 895 896 if (err < (int)sizeof(pcapng_spkt_t)) { 767 768 if (blocklen < sizeof(pcapng_spkt_t) + 4) { 897 769 trace_set_err(libtrace, TRACE_ERR_BAD_PACKET, 898 770 "Incomplete pcapng simple packet header"); … … 905 777 if (DATA(libtrace)->byteswapped) { 906 778 assert(byteswap32(hdr->blocktype) == PCAPNG_SIMPLE_PACKET_TYPE); 907 to_read = byteswap32(hdr->blocklen) - sizeof(pcapng_spkt_t);908 caplen = to_read - 4;/* account for trailing length field */779 caplen = byteswap32(hdr->blocklen) - sizeof(pcapng_spkt_t) - 4; 780 /* account for trailing length field */ 909 781 } else { 910 782 assert(hdr->blocktype == PCAPNG_SIMPLE_PACKET_TYPE); 911 to_read = hdr->blocklen - sizeof(pcapng_spkt_t); 912 caplen = to_read - 4; /* account for trailing length field */ 913 } 914 915 bodyptr = packet->buffer + sizeof(pcapng_spkt_t); 916 err = pcapng_read_body(libtrace, bodyptr, to_read); 917 if (err <= 0) { 918 return err; 783 caplen = hdr->blocklen - sizeof(pcapng_spkt_t) - 4; 784 /* account for trailing length field */ 919 785 } 920 786 … … 936 802 return -1; 937 803 } 938 return sizeof(pcapng_spkt_t) + to_read;804 return (int) blocklen; 939 805 940 806 } 941 807 942 808 static int pcapng_read_enhanced(libtrace_t *libtrace, libtrace_packet_t *packet, 943 uint32_t flags) {809 uint32_t blocklen, uint32_t flags) { 944 810 pcapng_epkt_t *hdr = NULL; 945 int err;946 uint32_t to_read;947 811 uint32_t caplen; 948 812 uint32_t ifaceid; … … 952 816 char *bodyptr; 953 817 954 err = wandio_read(libtrace->io, packet->buffer, sizeof(pcapng_epkt_t)); 955 956 if (err < 0) { 957 trace_set_err(libtrace, TRACE_ERR_WANDIO_FAILED, "reading pcapng enhanced packet"); 958 return -1; 959 } 960 961 if (err == 0) { 962 return 0; 963 } 964 965 if (err < (int)sizeof(pcapng_epkt_t)) { 818 if (blocklen < (int)sizeof(pcapng_epkt_t) + 4) { 966 819 trace_set_err(libtrace, TRACE_ERR_BAD_PACKET, 967 820 "Incomplete pcapng enhanced packet header"); … … 975 828 assert(byteswap32(hdr->blocktype) == PCAPNG_ENHANCED_PACKET_TYPE); 976 829 caplen = byteswap32(hdr->caplen); 977 to_read = byteswap32(hdr->blocklen) - sizeof(pcapng_epkt_t);978 830 ifaceid = byteswap32(hdr->interfaceid); 979 831 } else { 980 832 assert(hdr->blocktype == PCAPNG_ENHANCED_PACKET_TYPE); 981 833 caplen = hdr->caplen; 982 to_read = hdr->blocklen - sizeof(pcapng_epkt_t);983 834 ifaceid = hdr->interfaceid; 984 835 } 985 836 986 bodyptr = packet->buffer + sizeof(pcapng_epkt_t); 987 err = pcapng_read_body(libtrace, bodyptr, to_read); 988 if (err <= 0) { 989 return err; 990 } 837 bodyptr = (char *) packet->buffer + sizeof(pcapng_epkt_t); 991 838 992 839 /* Set packet type based on interface linktype */ … … 1009 856 /* Make sure to parse any useful options */ 1010 857 if ((caplen % 4) == 0) { 1011 bodyptr = packet->payload + caplen; 1012 } else { 1013 bodyptr = packet->payload + caplen + (4 - (caplen % 4)); 858 bodyptr = (char *) packet->payload + caplen; 859 } else { 860 bodyptr = (char *) packet->payload + caplen + (4 - (caplen % 4)); 861 } 862 // Check the packet caplen actually fits within the block we read 863 if ((char *) packet->buffer + blocklen < bodyptr + 4) { 864 trace_set_err(libtrace, TRACE_ERR_BAD_PACKET, 865 "Incomplete pcapng enhanced packet header"); 866 return -1; 1014 867 } 1015 868 … … 1033 886 1034 887 } while (optcode != 0); 1035 return sizeof(pcapng_epkt_t) + to_read;888 return (int) blocklen; 1036 889 1037 890 } … … 1093 946 return -1; 1094 947 } 948 if (btype != PCAPNG_SECTION_TYPE) { 949 // Read the entire block, unless it is a section as our byte ordering has 950 // not been set yet. 951 err = pcapng_read_body(libtrace, packet->buffer, to_read); 952 if (err <= 0) { 953 return err; 954 } 955 if (*((uint32_t *)((char *)packet->buffer+to_read-4)) != peeker.blocklen) { 956 trace_set_err(libtrace, TRACE_ERR_BAD_PACKET, 957 "Mismatched pcapng block sizes found, trace is invalid."); 958 return -1; 959 } 960 } 1095 961 1096 962 switch (btype) { … … 1103 969 /* Interface Header */ 1104 970 case PCAPNG_INTERFACE_TYPE: 1105 err = pcapng_read_interface(libtrace, packet, flags);971 err = pcapng_read_interface(libtrace, packet, to_read, flags); 1106 972 gotpacket = 1; 1107 973 break; … … 1110 976 case PCAPNG_ENHANCED_PACKET_TYPE: 1111 977 err = pcapng_read_enhanced(libtrace, packet, 1112 flags);978 to_read, flags); 1113 979 gotpacket = 1; 1114 980 break; 1115 981 1116 982 case PCAPNG_SIMPLE_PACKET_TYPE: 1117 err = pcapng_read_simple(libtrace, packet, flags);983 err = pcapng_read_simple(libtrace, packet, to_read, flags); 1118 984 gotpacket = 1; 1119 985 break; 1120 986 1121 987 case PCAPNG_INTERFACE_STATS_TYPE: 1122 err = pcapng_read_stats(libtrace, packet, flags);988 err = pcapng_read_stats(libtrace, packet, to_read, flags); 1123 989 gotpacket = 1; 1124 990 break; 1125 991 1126 992 case PCAPNG_NAME_RESOLUTION_TYPE: 1127 err = pcapng_read_nrb(libtrace, packet, flags);993 err = pcapng_read_nrb(libtrace, packet, to_read, flags); 1128 994 gotpacket = 1; 1129 995 break; … … 1131 997 case PCAPNG_CUSTOM_TYPE: 1132 998 case PCAPNG_CUSTOM_NONCOPY_TYPE: 1133 err = pcapng_read_custom(libtrace, packet, flags);999 err = pcapng_read_custom(libtrace, packet, to_read, flags); 1134 1000 gotpacket = 1; 1135 1001 break; … … 1141 1007 /* Everything else -- don't care, skip it */ 1142 1008 default: 1143 err = skip_block(libtrace, to_read);1144 1009 break; 1145 1010 }
Note: See TracChangeset
for help on using the changeset viewer.