print-syslog.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*
  2. * Copyright (c) 1998-2004 Hannes Gredler <hannes@gredler.at>
  3. * The TCPDUMP project
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that: (1) source code
  7. * distributions retain the above copyright notice and this paragraph
  8. * in its entirety, and (2) distributions including binary code include
  9. * the above copyright notice and this paragraph in its entirety in
  10. * the documentation or other materials provided with the distribution.
  11. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND
  12. * WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, WITHOUT
  13. * LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  14. * FOR A PARTICULAR PURPOSE.
  15. */
  16. /* \summary: Syslog protocol printer */
  17. #ifdef HAVE_CONFIG_H
  18. #include "config.h"
  19. #endif
  20. #include <netdissect-stdinc.h>
  21. #include "netdissect.h"
  22. #include "extract.h"
  23. static const char tstr[] = "[|syslog]";
  24. /*
  25. * tokenlists and #defines taken from Ethereal - Network traffic analyzer
  26. * by Gerald Combs <gerald@ethereal.com>
  27. */
  28. #define SYSLOG_SEVERITY_MASK 0x0007 /* 0000 0000 0000 0111 */
  29. #define SYSLOG_FACILITY_MASK 0x03f8 /* 0000 0011 1111 1000 */
  30. #define SYSLOG_MAX_DIGITS 3 /* The maximum number if priority digits to read in. */
  31. static const struct tok syslog_severity_values[] = {
  32. { 0, "emergency" },
  33. { 1, "alert" },
  34. { 2, "critical" },
  35. { 3, "error" },
  36. { 4, "warning" },
  37. { 5, "notice" },
  38. { 6, "info" },
  39. { 7, "debug" },
  40. { 0, NULL },
  41. };
  42. static const struct tok syslog_facility_values[] = {
  43. { 0, "kernel" },
  44. { 1, "user" },
  45. { 2, "mail" },
  46. { 3, "daemon" },
  47. { 4, "auth" },
  48. { 5, "syslog" },
  49. { 6, "lpr" },
  50. { 7, "news" },
  51. { 8, "uucp" },
  52. { 9, "cron" },
  53. { 10, "authpriv" },
  54. { 11, "ftp" },
  55. { 12, "ntp" },
  56. { 13, "security" },
  57. { 14, "console" },
  58. { 15, "cron" },
  59. { 16, "local0" },
  60. { 17, "local1" },
  61. { 18, "local2" },
  62. { 19, "local3" },
  63. { 20, "local4" },
  64. { 21, "local5" },
  65. { 22, "local6" },
  66. { 23, "local7" },
  67. { 0, NULL },
  68. };
  69. void
  70. syslog_print(netdissect_options *ndo,
  71. register const u_char *pptr, register u_int len)
  72. {
  73. uint16_t msg_off = 0;
  74. uint16_t pri = 0;
  75. uint16_t facility,severity;
  76. /* extract decimal figures that are
  77. * encapsulated within < > tags
  78. * based on this decimal figure extract the
  79. * severity and facility values
  80. */
  81. ND_TCHECK2(*pptr, 1);
  82. if (*(pptr+msg_off) == '<') {
  83. msg_off++;
  84. ND_TCHECK2(*(pptr + msg_off), 1);
  85. while ( *(pptr+msg_off) >= '0' &&
  86. *(pptr+msg_off) <= '9' &&
  87. msg_off <= SYSLOG_MAX_DIGITS) {
  88. pri = pri * 10 + (*(pptr+msg_off) - '0');
  89. msg_off++;
  90. ND_TCHECK2(*(pptr + msg_off), 1);
  91. }
  92. if (*(pptr+msg_off) != '>') {
  93. ND_PRINT((ndo, "%s", tstr));
  94. return;
  95. }
  96. msg_off++;
  97. } else {
  98. ND_PRINT((ndo, "%s", tstr));
  99. return;
  100. }
  101. facility = (pri & SYSLOG_FACILITY_MASK) >> 3;
  102. severity = pri & SYSLOG_SEVERITY_MASK;
  103. if (ndo->ndo_vflag < 1 )
  104. {
  105. ND_PRINT((ndo, "SYSLOG %s.%s, length: %u",
  106. tok2str(syslog_facility_values, "unknown (%u)", facility),
  107. tok2str(syslog_severity_values, "unknown (%u)", severity),
  108. len));
  109. return;
  110. }
  111. ND_PRINT((ndo, "SYSLOG, length: %u\n\tFacility %s (%u), Severity %s (%u)\n\tMsg: ",
  112. len,
  113. tok2str(syslog_facility_values, "unknown (%u)", facility),
  114. facility,
  115. tok2str(syslog_severity_values, "unknown (%u)", severity),
  116. severity));
  117. /* print the syslog text in verbose mode */
  118. for (; msg_off < len; msg_off++) {
  119. ND_TCHECK2(*(pptr + msg_off), 1);
  120. safeputchar(ndo, *(pptr + msg_off));
  121. }
  122. if (ndo->ndo_vflag > 1)
  123. print_unknown_data(ndo, pptr, "\n\t", len);
  124. return;
  125. trunc:
  126. ND_PRINT((ndo, "%s", tstr));
  127. }