dlt2name.pl 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. #!/usr/bin/perl -w
  2. # Parses the bpf.h header file to generate the dlt_names.h header
  3. # which maps the DLT types to the DLT string name
  4. # run as:
  5. # cat /usr/include/net/bpf.h | ./scripts/dlt2name.pl
  6. use strict;
  7. my $outfile = "dlt_names.h";
  8. defined $ARGV[0] or die("Please specify an output file");
  9. # open outfile
  10. open(OUT, ">$ARGV[0]") or die("Unable to open $ARGV[0] for writing: $!");
  11. # read STDIN
  12. # some DLT types aren't in a format we can parse easily or just doesn't
  13. # exist in my /usr/include/net/bpf.h file so we list them here
  14. my %known = (107 => 'BSD/OS Frame Relay',
  15. 108 => 'OpenBSD Loopback',
  16. 113 => 'Linux Cooked Sockets',
  17. 114 => 'Apple LocalTalk',
  18. 115 => 'Acorn Econet',
  19. 116 => 'OpenBSD IPFilter',
  20. 117 => 'OpenBSD PF Log/SuSE 6.3 LANE 802.3',
  21. 118 => 'Cisco IOS',
  22. 119 => '802.11 Prism Header',
  23. 120 => '802.11 Aironet Header',
  24. 121 => 'Siemens HiPath HDLC',
  25. 122 => 'IP over Fibre Channel'
  26. );
  27. my @names = ( );
  28. # put our known DLT types in names since the format of bpf.h is
  29. # inconsistent
  30. foreach my $dlt (keys %known) {
  31. $names[$dlt] = { name => $known{$dlt} };
  32. }
  33. while (my $line = <STDIN>) {
  34. if ($line =~ /^\#define\s+(DLT_[a-zA-Z0-9_]+)\s+(\d+)/) {
  35. my $key = $1;
  36. my $dlt = $2;
  37. my $name = $names[$dlt]->{name} ? $names[$dlt]->{name} : "";
  38. if ($line =~ /\/\*\s+(.*)\s+\*\//) {
  39. $name = $1;
  40. }
  41. $names[$dlt] = { key => $key,
  42. name => $name
  43. };
  44. }
  45. }
  46. # prep the header
  47. print OUT (<<HEADER);
  48. /*
  49. * This file is generated by scripts/dlt2name.pl which converts your bpf.h
  50. * header file which comes with libpcap into a header file
  51. * which translates DLT values to their string names
  52. *
  53. * Hence DO NOT EDIT THIS FILE!
  54. * If your DLT type is not listed here, edit the %known hash in
  55. * scripts/dlt2name.pl
  56. */
  57. /* DLT to descriptions */
  58. char *dlt2desc[] = {
  59. HEADER
  60. for (my $i = 0; $i < $#names; $i ++) {
  61. if (! defined $names[$i]) {
  62. print OUT "\t\t\"Unknown\",\n";
  63. } else {
  64. print OUT "\t\t\"$names[$i]->{name}\",\n";
  65. }
  66. }
  67. print OUT "\t\tNULL\n};\n";
  68. print OUT "\#define DLT2DESC_LEN $#names\n";
  69. close OUT;
  70. exit 0;