print-null.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  2. * Copyright (c) 1991, 1993, 1994, 1995, 1996, 1997
  3. * The Regents of the University of California. All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that: (1) source code distributions
  7. * retain the above copyright notice and this paragraph in its entirety, (2)
  8. * distributions including binary code include the above copyright notice and
  9. * this paragraph in its entirety in the documentation or other materials
  10. * provided with the distribution, and (3) all advertising materials mentioning
  11. * features or use of this software display the following acknowledgement:
  12. * ``This product includes software developed by the University of California,
  13. * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
  14. * the University nor the names of its contributors may be used to endorse
  15. * or promote products derived from this software without specific prior
  16. * written permission.
  17. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
  18. * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
  19. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  20. */
  21. /* \summary: BSD loopback device printer */
  22. #ifdef HAVE_CONFIG_H
  23. #include "config.h"
  24. #endif
  25. #include <netdissect-stdinc.h>
  26. #include <string.h>
  27. #include "netdissect.h"
  28. #include "af.h"
  29. /*
  30. * The DLT_NULL packet header is 4 bytes long. It contains a host-byte-order
  31. * 32-bit integer that specifies the family, e.g. AF_INET.
  32. *
  33. * Note here that "host" refers to the host on which the packets were
  34. * captured; that isn't necessarily *this* host.
  35. *
  36. * The OpenBSD DLT_LOOP packet header is the same, except that the integer
  37. * is in network byte order.
  38. */
  39. #define NULL_HDRLEN 4
  40. /*
  41. * Byte-swap a 32-bit number.
  42. * ("htonl()" or "ntohl()" won't work - we want to byte-swap even on
  43. * big-endian platforms.)
  44. */
  45. #define SWAPLONG(y) \
  46. ((((y)&0xff)<<24) | (((y)&0xff00)<<8) | (((y)&0xff0000)>>8) | (((y)>>24)&0xff))
  47. static inline void
  48. null_hdr_print(netdissect_options *ndo, u_int family, u_int length)
  49. {
  50. if (!ndo->ndo_qflag) {
  51. ND_PRINT((ndo, "AF %s (%u)",
  52. tok2str(bsd_af_values,"Unknown",family),family));
  53. } else {
  54. ND_PRINT((ndo, "%s",
  55. tok2str(bsd_af_values,"Unknown AF %u",family)));
  56. }
  57. ND_PRINT((ndo, ", length %u: ", length));
  58. }
  59. /*
  60. * This is the top level routine of the printer. 'p' points
  61. * to the ether header of the packet, 'h->ts' is the timestamp,
  62. * 'h->len' is the length of the packet off the wire, and 'h->caplen'
  63. * is the number of bytes actually captured.
  64. */
  65. u_int
  66. null_if_print(netdissect_options *ndo, const struct pcap_pkthdr *h, const u_char *p)
  67. {
  68. u_int length = h->len;
  69. u_int caplen = h->caplen;
  70. u_int family;
  71. if (caplen < NULL_HDRLEN) {
  72. ND_PRINT((ndo, "[|null]"));
  73. return (NULL_HDRLEN);
  74. }
  75. memcpy((char *)&family, (const char *)p, sizeof(family));
  76. /*
  77. * This isn't necessarily in our host byte order; if this is
  78. * a DLT_LOOP capture, it's in network byte order, and if
  79. * this is a DLT_NULL capture from a machine with the opposite
  80. * byte-order, it's in the opposite byte order from ours.
  81. *
  82. * If the upper 16 bits aren't all zero, assume it's byte-swapped.
  83. */
  84. if ((family & 0xFFFF0000) != 0)
  85. family = SWAPLONG(family);
  86. if (ndo->ndo_eflag)
  87. null_hdr_print(ndo, family, length);
  88. length -= NULL_HDRLEN;
  89. caplen -= NULL_HDRLEN;
  90. p += NULL_HDRLEN;
  91. switch (family) {
  92. case BSD_AFNUM_INET:
  93. ip_print(ndo, p, length);
  94. break;
  95. case BSD_AFNUM_INET6_BSD:
  96. case BSD_AFNUM_INET6_FREEBSD:
  97. case BSD_AFNUM_INET6_DARWIN:
  98. ip6_print(ndo, p, length);
  99. break;
  100. case BSD_AFNUM_ISO:
  101. isoclns_print(ndo, p, length);
  102. break;
  103. case BSD_AFNUM_APPLETALK:
  104. atalk_print(ndo, p, length);
  105. break;
  106. case BSD_AFNUM_IPX:
  107. ipx_print(ndo, p, length);
  108. break;
  109. default:
  110. /* unknown AF_ value */
  111. if (!ndo->ndo_eflag)
  112. null_hdr_print(ndo, family, length + NULL_HDRLEN);
  113. if (!ndo->ndo_suppress_default_print)
  114. ND_DEFAULTPRINT(p, caplen);
  115. }
  116. return (NULL_HDRLEN);
  117. }
  118. /*
  119. * Local Variables:
  120. * c-style: whitesmith
  121. * c-basic-offset: 8
  122. * End:
  123. */