dlt2name.pl 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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 const char *dlt2desc[];
  71. extern const char *dlt2name[];
  72. #define DLT2DESC_LEN $#names
  73. #define DLT2NAME_LEN $#names
  74. HEADER
  75. for (my $i = 0; $i < 255; $i++) {
  76. next if ! defined $names[$i];
  77. print OUT_H "#ifndef $names[$i]{key}\n#define $names[$i]{key} $i\n#endif\n\n";
  78. }
  79. print OUT_C <<NAMES;
  80. /* DLT to names */
  81. char *dlt2name[] = {
  82. NAMES
  83. for (my $i = 0; $i < 255; $i++) {
  84. if (! defined $names[$i]) {
  85. print OUT_C "\t\t\"Unknown\",\n";
  86. } else {
  87. print OUT_C "\t\t\"$names[$i]{key}\",\n";
  88. }
  89. }
  90. print OUT_C <<FOOTER;
  91. \t\tNULL
  92. };
  93. FOOTER
  94. close OUT_C;
  95. close OUT_H;
  96. exit 0;
  97. __DATA__
  98. /*
  99. * Copyright (c) 2006 Aaron Turner
  100. * All rights reserved.
  101. *
  102. * This file is generated by scripts/dlt2name.pl which converts your pcap-bpf.h
  103. * header file which comes with libpcap into a header file
  104. * which translates DLT values to their string names as well as a list of all
  105. * of the available DLT types.
  106. *
  107. * Hence DO NOT EDIT THIS FILE!
  108. * If your DLT type is not listed here, edit the %known hash in
  109. * scripts/dlt2name.pl
  110. *
  111. * This file contains data which was taken from libpcap's pcap-bpf.h.
  112. * The copyright/license is included below:
  113. */
  114. /*-
  115. * Copyright (c) 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997
  116. * The Regents of the University of California. All rights reserved.
  117. *
  118. * This code is derived from the Stanford/CMU enet packet filter,
  119. * (net/enet.c) distributed as part of 4.3BSD, and code contributed
  120. * to Berkeley by Steven McCanne and Van Jacobson both of Lawrence
  121. * Berkeley Laboratory.
  122. *
  123. * Redistribution and use in source and binary forms, with or without
  124. * modification, are permitted provided that the following conditions
  125. * are met:
  126. * 1. Redistributions of source code must retain the above copyright
  127. * notice, this list of conditions and the following disclaimer.
  128. * 2. Redistributions in binary form must reproduce the above copyright
  129. * notice, this list of conditions and the following disclaimer in the
  130. * documentation and/or other materials provided with the distribution.
  131. * 3. All advertising materials mentioning features or use of this software
  132. * must display the following acknowledgement:
  133. * This product includes software developed by the University of
  134. * California, Berkeley and its contributors.
  135. * 4. Neither the name of the University nor the names of its contributors
  136. * may be used to endorse or promote products derived from this software
  137. * without specific prior written permission.
  138. *
  139. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  140. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  141. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  142. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  143. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  144. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  145. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  146. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  147. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  148. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  149. * SUCH DAMAGE.
  150. *
  151. * @(#)bpf.h 7.1 (Berkeley) 5/7/91
  152. *
  153. * @(#) $Header: /tcpdump/master/libpcap/pcap-bpf.h,v 1.34.2.6 2005/08/13 22:29:47 hannes Exp $ (LBL)
  154. */