packet_utils.pm 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. package packet_utils;
  2. # $Id: packet_utils.pm 1100 2005-01-25 09:12:47Z stanleyhopcroft $
  3. # Revision 1.1 2005/01/25 09:12:47 stanleyhopcroft
  4. # packet creation and dumping hacks used by check_ica* and check_lotus
  5. #
  6. # Revision 1.1 2005-01-25 15:28:58+11 anwsmh
  7. # Initial revision
  8. #
  9. require Exporter;
  10. @ISA = qw(Exporter);
  11. @EXPORT_OK = qw(tethereal pdump);
  12. sub tethereal {
  13. my ($tethereal_dump, $start_byte) = @_ ;
  14. # Return a string or array (depending on context) containing the characters from a tethereal trace.
  15. # Skip all stuff until the first byte given by $start_byte in the first row ..
  16. &outahere("Invalid tethereal dump:", substr($tethereal_dump, 0, 71), 'fails to match m#\d\d\d\d \S\S(?: \S\S){1,15}#')
  17. unless $tethereal_dump =~ m#\d\d\d\d \S\S(?: \S\S){1,15}# ;
  18. my @tethereal_dump = split(/\n/, $tethereal_dump) ;
  19. my $first_line = shift @tethereal_dump ;
  20. $first_line = unpack('x6 a48', $first_line) ;
  21. # Take one extra space (after hex bytes) to use a template of 'a2 x' x 16
  22. # instead of 'a2 x' x 15 . 'a2'
  23. my $last_line = pop @tethereal_dump ;
  24. $last_line = unpack('x6 a48', $last_line) ;
  25. my $idx = index($first_line, $start_byte) ;
  26. &outahere(qq(Invalid tethereal dump: "$start_byte" not found in first line - "$first_line"))
  27. if $idx == -1 ;
  28. $first_line = substr($first_line, $idx) ;
  29. my ($dump, @dump) = ('', ()) ;
  30. my $bytes = 0 ;
  31. $bytes++
  32. while $first_line =~ m#\b\S\S#g ;
  33. push @dump, unpack('a2x' x $bytes, $first_line) ;
  34. push @dump, unpack('x6 ' . 'a2x' x 16, $_)
  35. foreach @tethereal_dump ;
  36. $bytes = 0 ;
  37. $bytes++
  38. while $last_line =~ m#\b\S\S#g ;
  39. # Be more cautious with the last line; the ASCII decode may
  40. # have been omitted.
  41. push @dump, unpack(('a2x' x ($bytes - 1)) . 'a2', $last_line) ;
  42. return wantarray ? map pack('H2', $_), @dump : pack('H2' x scalar @dump, @dump) ;
  43. # return wantarray ? map hex($_), @dump : pack('H2' x scalar @dump, @dump) ;
  44. }
  45. sub pdump {
  46. my ($x) = shift @_ ;
  47. my (@bytes_in_row, $row, $dump) ;
  48. my $number_in_row = 16 ;
  49. my $number_of_bytes = length $x ;
  50. my $full_rows = int( $number_of_bytes / $number_in_row ) ;
  51. my $bytes_in_last_row = $number_of_bytes % $number_in_row ;
  52. my $template = "a$number_in_row " x $full_rows ;
  53. my $nr = 0 ;
  54. # Output format styled on tethereal.
  55. foreach $row ( unpack($template, $x) ) {
  56. @bytes_in_row = unpack('C*', $row) ;
  57. $row =~ tr /\x00-\x1f\x80-\xff/./ ;
  58. $dump .= join(' ', sprintf('%4.4x', $nr * 0x10), join(' ', map { sprintf "%2.2x", $_} @bytes_in_row), $row) ;
  59. $dump .= "\n" ;
  60. $nr++ ;
  61. }
  62. if ( $bytes_in_last_row ) {
  63. my $number_of_spaces = ($number_in_row - $bytes_in_last_row)*3 - 2 ;
  64. # 3 spaces (2 digts + 1 space) for each digit printed
  65. # minus two spaces for those added by the join(' ',) below.
  66. $row = substr($x, -$bytes_in_last_row) ;
  67. @bytes_in_row = unpack('C*', $row) ;
  68. $row =~ tr /\x00-\x1f\x80-\xff/./ ;
  69. # my $bytes = join(' ', map { sprintf "%2.2x", $_} @bytes_in_row) ;
  70. # See comment below.
  71. my $spaces = ' ' x $number_of_spaces ;
  72. $dump .= join(' ', sprintf("%4.4x", $nr * 0x10 ), join(' ', map { sprintf "%2.2x", $_} @bytes_in_row), $spaces, $row) ;
  73. $dump .= "\n" ;
  74. }
  75. print STDERR $dump, "\n" ;
  76. =begin comment
  77. tsitc> perl -MBenchmark -e 'timethese(1_00_000, { printf => q<printf "%2.2x %2.2x %2.2x\n", 61, 62, 63>, sprintf => q<$x = sprintf "%2
  78. .2x %2.2x %2.2x\n", 61, 62, 63; print $x>} )' | perl -ne 'print if /intf/'
  79. Benchmark: timing 100000 iterations of printf, sprintf...
  80. printf: 1 wallclock secs ( 0.55 usr + 0.00 sys = 0.55 CPU)
  81. sprintf: 0 wallclock secs ( 0.11 usr + 0.00 sys = 0.11 CPU)
  82. so having sprintf in a loop seems more rational than a printf for the line ...
  83. =comment
  84. =cut
  85. }