tcpcapinfo.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. /* $Id$ */
  2. /*
  3. * Copyright (c) 2001-2012 Aaron Turner <aturner at synfin dot net>
  4. * Copyright (c) 2013-2018 Fred Klassen <tcpreplay at appneta dot com> - AppNeta
  5. *
  6. * The Tcpreplay Suite of tools is free software: you can redistribute it
  7. * and/or modify it under the terms of the GNU General Public License as
  8. * published by the Free Software Foundation, either version 3 of the
  9. * License, or with the authors permission any later version.
  10. *
  11. * The Tcpreplay Suite is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with the Tcpreplay Suite. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include "config.h"
  20. #include "defines.h"
  21. #include "common.h"
  22. #include <fcntl.h>
  23. #include <sys/types.h>
  24. #include <sys/uio.h>
  25. #include <unistd.h>
  26. #include <pcap.h>
  27. #include <sys/stat.h>
  28. #include <stdio.h>
  29. #include <string.h>
  30. #include <errno.h>
  31. #include <inttypes.h>
  32. #define __STDC_FORMAT_MACROS 1
  33. #include <inttypes.h>
  34. #include "tcpcapinfo_opts.h"
  35. static int do_checksum_math(u_int16_t *data, int len);
  36. #ifdef DEBUG
  37. int debug = 0;
  38. #endif
  39. #ifdef WORDS_BIGENDIAN
  40. char is_swapped[] = "little-endian";
  41. char is_not_swapped[] = "big-endian";
  42. #else
  43. char is_not_swapped[] = "little-endian";
  44. char is_swapped[] = "big-endian";
  45. #endif
  46. int read_packet(int fd, uint32_t len, char *fname);
  47. /*
  48. * Standard libpcap format.
  49. */
  50. #define TCPDUMP_MAGIC 0xa1b2c3d4
  51. /*
  52. * Alexey Kuznetzov's modified libpcap format.
  53. */
  54. #define KUZNETZOV_TCPDUMP_MAGIC 0xa1b2cd34
  55. struct pcap_timeval {
  56. bpf_int32 tv_sec; /* seconds */
  57. bpf_int32 tv_usec; /* microseconds */
  58. };
  59. struct pcap_sf_patched_pkthdr {
  60. struct pcap_timeval ts; /* time stamp */
  61. bpf_u_int32 caplen; /* length of portion present */
  62. bpf_u_int32 len; /* length this packet (off wire) */
  63. int index;
  64. unsigned short protocol;
  65. unsigned char pkt_type;
  66. };
  67. /*
  68. * Reserved for Francisco Mesquita <francisco.mesquita@radiomovel.pt>
  69. * for another modified format.
  70. */
  71. #define FMESQUITA_TCPDUMP_MAGIC 0xa1b234cd
  72. /*
  73. * Navtel Communcations' format, with nanosecond timestamps,
  74. * as per a request from Dumas Hwang <dumas.hwang@navtelcom.com>.
  75. */
  76. #define NAVTEL_TCPDUMP_MAGIC 0xa12b3c4d
  77. /*
  78. * Normal libpcap format, except for seconds/nanoseconds timestamps,
  79. * as per a request by Ulf Lamping <ulf.lamping@web.de>
  80. */
  81. #define NSEC_TCPDUMP_MAGIC 0xa1b23c4d
  82. int
  83. main(int argc, char *argv[])
  84. {
  85. int i, fd, swapped, pkthdrlen, ret, optct, backwards, caplentoobig;
  86. struct pcap_file_header pcap_fh;
  87. struct pcap_pkthdr pcap_ph;
  88. struct pcap_sf_patched_pkthdr pcap_patched_ph; /* Kuznetzov */
  89. char buf[UINT16_MAX];
  90. struct stat statinfo;
  91. uint64_t pktcnt;
  92. uint32_t readword;
  93. int32_t last_sec, last_usec, caplen, maxread;
  94. optct = optionProcess(&tcpcapinfoOptions, argc, argv);
  95. argc -= optct;
  96. argv += optct;
  97. #ifdef DEBUG
  98. if (HAVE_OPT(DBUG))
  99. debug = OPT_VALUE_DBUG;
  100. #endif
  101. for (i = 0; i < argc; i++) {
  102. dbgx(1, "processing: %s\n", argv[i]);
  103. if ((fd = open(argv[i], O_RDONLY)) < 0)
  104. errx(-1, "Error opening file %s: %s", argv[i], strerror(errno));
  105. if (fstat(fd, &statinfo) < 0)
  106. errx(-1, "Error getting file stat info %s: %s", argv[i], strerror(errno));
  107. printf("file size = %"PRIu64" bytes\n", (uint64_t)statinfo.st_size);
  108. if ((ret = read(fd, &buf, sizeof(pcap_fh))) != sizeof(pcap_fh))
  109. errx(-1, "File too small. Unable to read pcap_file_header from %s", argv[i]);
  110. dbgx(3, "Read %d bytes for file header", ret);
  111. swapped = 0;
  112. memcpy(&pcap_fh, &buf, sizeof(pcap_fh));
  113. pkthdrlen = 16; /* pcap_pkthdr isn't the actual on-disk format for 64bit systems! */
  114. switch (pcap_fh.magic) {
  115. case TCPDUMP_MAGIC:
  116. printf("magic = 0x%08"PRIx32" (tcpdump) (%s)\n", pcap_fh.magic, is_not_swapped);
  117. break;
  118. case SWAPLONG(TCPDUMP_MAGIC):
  119. printf("magic = 0x%08"PRIx32" (tcpdump/swapped) (%s)\n", pcap_fh.magic, is_swapped);
  120. swapped = 1;
  121. break;
  122. case KUZNETZOV_TCPDUMP_MAGIC:
  123. pkthdrlen = sizeof(pcap_patched_ph);
  124. printf("magic = 0x%08"PRIx32" (Kuznetzov) (%s)\n", pcap_fh.magic, is_not_swapped);
  125. break;
  126. case SWAPLONG(KUZNETZOV_TCPDUMP_MAGIC):
  127. pkthdrlen = sizeof(pcap_patched_ph);
  128. printf("magic = 0x%08"PRIx32" (Kuznetzov/swapped) (%s)\n", pcap_fh.magic, is_swapped);
  129. swapped = 1;
  130. break;
  131. case FMESQUITA_TCPDUMP_MAGIC:
  132. printf("magic = 0x%08"PRIx32" (Fmesquita) (%s)\n", pcap_fh.magic, is_not_swapped);
  133. break;
  134. case SWAPLONG(FMESQUITA_TCPDUMP_MAGIC):
  135. printf("magic = 0x%08"PRIx32" (Fmesquita) (%s)\n", pcap_fh.magic, is_swapped);
  136. swapped = 1;
  137. break;
  138. case NAVTEL_TCPDUMP_MAGIC:
  139. printf("magic = 0x%08"PRIx32" (Navtel) (%s)\n", pcap_fh.magic, is_not_swapped);
  140. break;
  141. case SWAPLONG(NAVTEL_TCPDUMP_MAGIC):
  142. printf("magic = 0x%08"PRIx32" (Navtel/swapped) (%s)\n", pcap_fh.magic, is_swapped);
  143. swapped = 1;
  144. break;
  145. case NSEC_TCPDUMP_MAGIC:
  146. printf("magic = 0x%08"PRIx32" (Nsec) (%s)\n", pcap_fh.magic, is_not_swapped);
  147. break;
  148. case SWAPLONG(NSEC_TCPDUMP_MAGIC):
  149. printf("magic = 0x%08"PRIx32" (Nsec/swapped) (%s)\n", pcap_fh.magic, is_swapped);
  150. swapped = 1;
  151. break;
  152. default:
  153. printf("magic = 0x%08"PRIx32" (unknown)\n", pcap_fh.magic);
  154. }
  155. if (swapped == 1) {
  156. pcap_fh.version_major = SWAPSHORT(pcap_fh.version_major);
  157. pcap_fh.version_minor = SWAPSHORT(pcap_fh.version_minor);
  158. pcap_fh.thiszone = SWAPLONG(pcap_fh.thiszone);
  159. pcap_fh.sigfigs = SWAPLONG(pcap_fh.sigfigs);
  160. pcap_fh.snaplen = SWAPLONG(pcap_fh.snaplen);
  161. pcap_fh.linktype = SWAPLONG(pcap_fh.linktype);
  162. }
  163. printf("version = %hu.%hu\n", pcap_fh.version_major, pcap_fh.version_minor);
  164. printf("thiszone = 0x%08"PRIx32"\n", pcap_fh.thiszone);
  165. printf("sigfigs = 0x%08"PRIx32"\n", pcap_fh.sigfigs);
  166. printf("snaplen = %"PRIu32"\n", pcap_fh.snaplen);
  167. printf("linktype = 0x%08"PRIx32"\n", pcap_fh.linktype);
  168. if (pcap_fh.version_major != 2 && pcap_fh.version_minor != 4) {
  169. printf("Sorry, we only support file format version 2.4\n");
  170. close(fd);
  171. continue;
  172. }
  173. dbgx(5, "Packet header len: %d", pkthdrlen);
  174. if (pkthdrlen == 24) {
  175. printf("Packet\tOrigLen\t\tCaplen\t\tTimestamp\t\tIndex\tProto\tPktType\tPktCsum\tNote\n");
  176. } else {
  177. printf("Packet\tOrigLen\t\tCaplen\t\tTimestamp\tCsum\tNote\n");
  178. }
  179. pktcnt = 0;
  180. last_sec = 0;
  181. last_usec = 0;
  182. while ((ret = read(fd, &buf, pkthdrlen)) == pkthdrlen) {
  183. pktcnt ++;
  184. backwards = 0;
  185. caplentoobig = 0;
  186. dbgx(3, "Read %d bytes for packet %"PRIu64" header", ret, pktcnt);
  187. memset(&pcap_ph, 0, sizeof(pcap_ph));
  188. /* see what packet header we're using */
  189. if (pkthdrlen == sizeof(pcap_patched_ph)) {
  190. memcpy(&pcap_patched_ph, &buf, sizeof(pcap_patched_ph));
  191. if (swapped == 1) {
  192. dbg(3, "Swapping packet header bytes...");
  193. pcap_patched_ph.caplen = SWAPLONG(pcap_patched_ph.caplen);
  194. pcap_patched_ph.len = SWAPLONG(pcap_patched_ph.len);
  195. pcap_patched_ph.ts.tv_sec = SWAPLONG(pcap_patched_ph.ts.tv_sec);
  196. pcap_patched_ph.ts.tv_usec = SWAPLONG(pcap_patched_ph.ts.tv_usec);
  197. pcap_patched_ph.index = SWAPLONG(pcap_patched_ph.index);
  198. pcap_patched_ph.protocol = SWAPSHORT(pcap_patched_ph.protocol);
  199. }
  200. printf("%"PRIu64"\t%4"PRIu32"\t\t%4"PRIu32"\t\t%"
  201. PRIx32".%"PRIx32"\t\t%4"PRIu32"\t%4hu\t%4hhu",
  202. pktcnt, pcap_patched_ph.len, pcap_patched_ph.caplen,
  203. pcap_patched_ph.ts.tv_sec, pcap_patched_ph.ts.tv_usec,
  204. pcap_patched_ph.index, pcap_patched_ph.protocol, pcap_patched_ph.pkt_type);
  205. if (pcap_fh.snaplen < pcap_patched_ph.caplen) {
  206. caplentoobig = 1;
  207. }
  208. caplen = pcap_patched_ph.caplen;
  209. } else {
  210. /* manually map on-disk bytes to our memory structure */
  211. memcpy(&readword, buf, 4);
  212. pcap_ph.ts.tv_sec = readword;
  213. memcpy(&readword, &buf[4], 4);
  214. pcap_ph.ts.tv_usec = readword;
  215. memcpy(&pcap_ph.caplen, &buf[8], 4);
  216. memcpy(&pcap_ph.len, &buf[12], 4);
  217. if (swapped == 1) {
  218. dbg(3, "Swapping packet header bytes...");
  219. pcap_ph.caplen = SWAPLONG(pcap_ph.caplen);
  220. pcap_ph.len = SWAPLONG(pcap_ph.len);
  221. pcap_ph.ts.tv_sec = SWAPLONG(pcap_ph.ts.tv_sec);
  222. pcap_ph.ts.tv_usec = SWAPLONG(pcap_ph.ts.tv_usec);
  223. }
  224. printf("%"PRIu64"\t%4"PRIu32"\t\t%4"PRIu32"\t\t%"
  225. PRIx32".%"PRIx32,
  226. pktcnt, pcap_ph.len, pcap_ph.caplen,
  227. (unsigned int)pcap_ph.ts.tv_sec, (unsigned int)pcap_ph.ts.tv_usec);
  228. if (pcap_fh.snaplen < pcap_ph.caplen) {
  229. caplentoobig = 1;
  230. }
  231. caplen = pcap_ph.caplen;
  232. }
  233. if (caplentoobig) {
  234. printf("\n\nCapture file appears to be damaged or corrupt.\n"
  235. "Contains packet of size %d, bigger than snap length %u\n",
  236. caplen, pcap_fh.snaplen);
  237. close(fd);
  238. break;
  239. }
  240. /* check to make sure timestamps don't go backwards */
  241. if (last_sec > 0 && last_usec > 0) {
  242. if ((pcap_ph.ts.tv_sec == last_sec) ?
  243. (pcap_ph.ts.tv_usec < last_usec) :
  244. (pcap_ph.ts.tv_sec < last_sec)) {
  245. backwards = 1;
  246. }
  247. }
  248. if (pkthdrlen == sizeof(pcap_patched_ph)) {
  249. last_sec = pcap_patched_ph.ts.tv_sec;
  250. last_usec = pcap_patched_ph.ts.tv_usec;
  251. } else {
  252. last_sec = pcap_ph.ts.tv_sec;
  253. last_usec = pcap_ph.ts.tv_usec;
  254. }
  255. /* read the frame */
  256. maxread = min((size_t)caplen, sizeof(buf));
  257. if ((ret = read(fd, &buf, maxread)) != maxread) {
  258. if (ret < 0) {
  259. printf("Error reading file: %s: %s\n", argv[i], strerror(errno));
  260. } else {
  261. printf("File truncated! Unable to jump to next packet.\n");
  262. }
  263. close(fd);
  264. break;
  265. }
  266. /* print the frame checksum */
  267. printf("\t%x\t", do_checksum_math((u_int16_t *)buf, maxread));
  268. /* print the Note */
  269. if (! backwards && ! caplentoobig) {
  270. printf("OK\n");
  271. } else if (backwards && ! caplentoobig) {
  272. printf("BAD_TS\n");
  273. } else if (caplentoobig && ! backwards) {
  274. printf("TOOBIG\n");
  275. } else if (backwards && caplentoobig) {
  276. printf("BAD_TS|TOOBIG");
  277. }
  278. }
  279. }
  280. restore_stdin();
  281. return 0;
  282. }
  283. /**
  284. * code to do a ones-compliment checksum
  285. */
  286. static int
  287. do_checksum_math(u_int16_t *data, int len)
  288. {
  289. int sum = 0;
  290. union {
  291. u_int16_t s;
  292. u_int8_t b[2];
  293. } pad;
  294. while (len > 1) {
  295. sum += *data++;
  296. len -= 2;
  297. }
  298. if (len == 1) {
  299. pad.b[0] = *(u_int8_t *)data;
  300. pad.b[1] = 0;
  301. sum += pad.s;
  302. }
  303. return (sum);
  304. }