1
0

capinfo.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /* $Id: capinfo.c 767 2004-10-06 12:48:49Z aturner $ */
  2. /*
  3. * Copyright (c) 2001-2004 Aaron Turner, Matt Bing.
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. *
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. * 3. Neither the names of the copyright owners nor the names of its
  16. * contributors may be used to endorse or promote products derived from
  17. * this software without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
  20. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  21. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  22. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  23. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  24. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  25. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  26. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
  27. * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  28. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  29. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. */
  31. #include <fcntl.h>
  32. #include <stdio.h>
  33. #include <stdlib.h>
  34. #include <sys/types.h>
  35. #include <unistd.h>
  36. #include "config.h"
  37. #include "err.h"
  38. #include "capinfo.h"
  39. #include "libpcap.h"
  40. #include "snoop.h"
  41. void print_pcap(struct pcap_info *);
  42. void print_snoop(struct snoop_info *);
  43. void usage();
  44. #ifdef DEBUG
  45. int debug = 0;
  46. #endif
  47. int
  48. main(int argc, char *argv[])
  49. {
  50. struct pcap_info p;
  51. struct snoop_info s;
  52. int i, fd, flag;
  53. if (argc == 0)
  54. usage();
  55. for (i = 1; i < argc; i++) {
  56. flag = 0;
  57. if ((fd = open(argv[i], O_RDONLY, 0)) < 0) {
  58. warn("could not open");
  59. continue;
  60. }
  61. if (is_pcap(fd)) {
  62. stat_pcap(fd, &p);
  63. flag = 1;
  64. printf("%s pcap file\n", argv[1]);
  65. print_pcap(&p);
  66. return 0;
  67. }
  68. /* rewind */
  69. if (lseek(fd, 0, SEEK_SET) != 0)
  70. err(1, "lseek");
  71. if (is_snoop(fd)) {
  72. stat_snoop(fd, &s);
  73. printf("%s snoop file\n", argv[1]);
  74. print_snoop(&s);
  75. return 0;
  76. }
  77. warnx("unknown format");
  78. (void)printf("\n");
  79. }
  80. return 0;
  81. }
  82. void
  83. print_pcap(struct pcap_info *p)
  84. {
  85. char *start, *finish;
  86. printf("\tpcap (%s%s)\n", (p->modified ? "modified, " : ""), p->swapped);
  87. (void)printf("\tversion: %d.%d\n", p->phdr.version_major,
  88. p->phdr.version_minor);
  89. (void)printf("\tzone: %d\n", p->phdr.thiszone);
  90. (void)printf("\tsig figs: %d\n", p->phdr.sigfigs);
  91. (void)printf("\tsnaplen: %d\n", p->phdr.snaplen);
  92. (void)printf("\tlinktype: %s\n", p->linktype);
  93. (void)printf("\t%d packets, %d bytes\n", p->cnt, p->bytes);
  94. if (p->trunc > 0)
  95. (void)printf("\t%d packets truncated (larger than snaplen)\n",
  96. p->trunc);
  97. if (p->cnt > 0) {
  98. start = ctime(&p->start_tm.tv_sec);
  99. (void)printf("\tfirst packet: %s", start);
  100. finish = ctime(&p->finish_tm.tv_sec);
  101. (void)printf("\tlast packet: %s", finish);
  102. }
  103. }
  104. void
  105. print_snoop(struct snoop_info *s)
  106. {
  107. char *start, *finish;
  108. (void)printf("\tversion: %d\n", s->version);
  109. (void)printf("\tlinktype: %s\n", s->linktype);
  110. (void)printf("\t%d packets, %d bytes\n", s->cnt, s->bytes);
  111. if (s->trunc > 0)
  112. (void)printf("\t%d packets truncated (larger than snaplen)\n",
  113. s->trunc);
  114. if (s->cnt > 0) {
  115. start = ctime(&s->start_tm.tv_sec);
  116. (void)printf("\tfirst packet: %s", start);
  117. finish = ctime(&s->finish_tm.tv_sec);
  118. (void)printf("\tlast packet: %s", finish);
  119. }
  120. }
  121. void
  122. usage()
  123. {
  124. (void)fprintf(stderr, "capinfo <files>\n");
  125. exit(1);
  126. }