dlt2name.pl 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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 from the tcpreplay source base directory as:
  5. # cat /usr/include/pcap-bpf.h | ./scripts/dlt2name.pl
  6. use strict;
  7. my $out_c = 'src/common/dlt_names.c';
  8. my $out_h = 'src/common/dlt_names.h';
  9. # open outfile
  10. open(OUT_C, ">$out_c") or die("Unable to open $out_c for writing: $!");
  11. open(OUT_H, ">$out_h") or die("Unable to open $out_h for writing: $!");
  12. # read STDIN
  13. # some DLT types aren't in a format we can parse easily or just doesn't
  14. # exist in my /usr/include/net/bpf.h file so we list them here
  15. my %known = (107 => 'BSD/OS Frame Relay',
  16. 108 => 'OpenBSD Loopback',
  17. 113 => 'Linux Cooked Sockets',
  18. 114 => 'Apple LocalTalk',
  19. 115 => 'Acorn Econet',
  20. 116 => 'OpenBSD IPFilter',
  21. 117 => 'OpenBSD PF Log/SuSE 6.3 LANE 802.3',
  22. 118 => 'Cisco IOS',
  23. 119 => '802.11 Prism Header',
  24. 120 => '802.11 Aironet Header',
  25. 121 => 'Siemens HiPath HDLC',
  26. 122 => 'IP over Fibre Channel'
  27. );
  28. my @names;
  29. # put our known DLT types in names since the format of bpf.h is
  30. # inconsistent
  31. foreach my $dlt (keys %known) {
  32. $names[$dlt]{name} = $known{$dlt};
  33. }
  34. while (my $line = <STDIN>) {
  35. if ($line =~ /^\#define\s+(DLT_[a-zA-Z0-9_]+)\s+(\d+)/) {
  36. my $key = $1;
  37. my $dlt = $2;
  38. my $name = $names[$dlt]{name} ? $names[$dlt]{name} : "";
  39. if ($line =~ /\/\*\s+(.*)\s+\*\//) {
  40. $name = $1;
  41. }
  42. $names[$dlt]{key} = $key;
  43. $names[$dlt]{name} = $name;
  44. }
  45. }
  46. # print the license info
  47. while (my $line = <DATA>) {
  48. print OUT_C $line;
  49. print OUT_H $line;
  50. }
  51. # prep the header
  52. print OUT_C <<HEADER;
  53. #include <stdlib.h>
  54. /* DLT to descriptions */
  55. char *dlt2desc[] = {
  56. HEADER
  57. for (my $i = 0; $i < $#names; $i ++) {
  58. if (! defined $names[$i]) {
  59. print OUT_C "\t\t\"Unknown\",\n";
  60. } else {
  61. print OUT_C "\t\t\"$names[$i]->{name}\",\n";
  62. }
  63. }
  64. print OUT_C <<FOOTER;
  65. \t\tNULL
  66. };
  67. FOOTER
  68. print OUT_H <<HEADER;
  69. /* include all the DLT types form pcap-bpf.h */
  70. extern char *dlt2desc[];
  71. #define DLT2DESC_LEN $#names
  72. HEADER
  73. for (my $i = 0; $i < 255; $i++) {
  74. next if ! defined $names[$i];
  75. print OUT_H "#ifndef $names[$i]{key}\n#define $names[$i]{key} $i\n#endif\n\n";
  76. }
  77. close OUT_C;
  78. close OUT_H;
  79. exit 0;
  80. __DATA__
  81. /*
  82. * Copyright (c) 2006 Aaron Turner
  83. * All rights reserved.
  84. *
  85. * This file is generated by scripts/dlt2name.pl which converts your pcap-bpf.h
  86. * header file which comes with libpcap into a header file
  87. * which translates DLT values to their string names as well as a list of all
  88. * of the available DLT types.
  89. *
  90. * Hence DO NOT EDIT THIS FILE!
  91. * If your DLT type is not listed here, edit the %known hash in
  92. * scripts/dlt2name.pl
  93. *
  94. * This file contains data which was taken from libpcap's pcap-bpf.h.
  95. * The copyright/license is included below:
  96. */
  97. /*-
  98. * Copyright (c) 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997
  99. * The Regents of the University of California. All rights reserved.
  100. *
  101. * This code is derived from the Stanford/CMU enet packet filter,
  102. * (net/enet.c) distributed as part of 4.3BSD, and code contributed
  103. * to Berkeley by Steven McCanne and Van Jacobson both of Lawrence
  104. * Berkeley Laboratory.
  105. *
  106. * Redistribution and use in source and binary forms, with or without
  107. * modification, are permitted provided that the following conditions
  108. * are met:
  109. * 1. Redistributions of source code must retain the above copyright
  110. * notice, this list of conditions and the following disclaimer.
  111. * 2. Redistributions in binary form must reproduce the above copyright
  112. * notice, this list of conditions and the following disclaimer in the
  113. * documentation and/or other materials provided with the distribution.
  114. * 3. All advertising materials mentioning features or use of this software
  115. * must display the following acknowledgement:
  116. * This product includes software developed by the University of
  117. * California, Berkeley and its contributors.
  118. * 4. Neither the name of the University nor the names of its contributors
  119. * may be used to endorse or promote products derived from this software
  120. * without specific prior written permission.
  121. *
  122. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  123. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  124. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  125. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  126. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  127. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  128. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  129. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  130. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  131. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  132. * SUCH DAMAGE.
  133. *
  134. * @(#)bpf.h 7.1 (Berkeley) 5/7/91
  135. *
  136. * @(#) $Header: /tcpdump/master/libpcap/pcap-bpf.h,v 1.34.2.6 2005/08/13 22:29:47 hannes Exp $ (LBL)
  137. */