dlt2name.pl 2.2 KB

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