print-timed.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /*
  2. * Copyright (c) 2000 Ben Smithurst <ben@scientia.demon.co.uk>
  3. * 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 time daemon protocol printer */
  22. #ifdef HAVE_CONFIG_H
  23. #include "config.h"
  24. #endif
  25. #include <netdissect-stdinc.h>
  26. #include "netdissect.h"
  27. #include "extract.h"
  28. /*
  29. * Time Synchronization Protocol
  30. *
  31. * http://docs.freebsd.org/44doc/smm/12.timed/paper.pdf
  32. */
  33. struct tsp_timeval {
  34. uint32_t tv_sec;
  35. uint32_t tv_usec;
  36. };
  37. struct tsp {
  38. uint8_t tsp_type;
  39. uint8_t tsp_vers;
  40. uint16_t tsp_seq;
  41. union {
  42. struct tsp_timeval tspu_time;
  43. int8_t tspu_hopcnt;
  44. } tsp_u;
  45. int8_t tsp_name[256];
  46. };
  47. #define tsp_time tsp_u.tspu_time
  48. #define tsp_hopcnt tsp_u.tspu_hopcnt
  49. /*
  50. * Command types.
  51. */
  52. #define TSP_ANY 0 /* match any types */
  53. #define TSP_ADJTIME 1 /* send adjtime */
  54. #define TSP_ACK 2 /* generic acknowledgement */
  55. #define TSP_MASTERREQ 3 /* ask for master's name */
  56. #define TSP_MASTERACK 4 /* acknowledge master request */
  57. #define TSP_SETTIME 5 /* send network time */
  58. #define TSP_MASTERUP 6 /* inform slaves that master is up */
  59. #define TSP_SLAVEUP 7 /* slave is up but not polled */
  60. #define TSP_ELECTION 8 /* advance candidature for master */
  61. #define TSP_ACCEPT 9 /* support candidature of master */
  62. #define TSP_REFUSE 10 /* reject candidature of master */
  63. #define TSP_CONFLICT 11 /* two or more masters present */
  64. #define TSP_RESOLVE 12 /* masters' conflict resolution */
  65. #define TSP_QUIT 13 /* reject candidature if master is up */
  66. #define TSP_DATE 14 /* reset the time (date command) */
  67. #define TSP_DATEREQ 15 /* remote request to reset the time */
  68. #define TSP_DATEACK 16 /* acknowledge time setting */
  69. #define TSP_TRACEON 17 /* turn tracing on */
  70. #define TSP_TRACEOFF 18 /* turn tracing off */
  71. #define TSP_MSITE 19 /* find out master's site */
  72. #define TSP_MSITEREQ 20 /* remote master's site request */
  73. #define TSP_TEST 21 /* for testing election algo */
  74. #define TSP_SETDATE 22 /* New from date command */
  75. #define TSP_SETDATEREQ 23 /* New remote for above */
  76. #define TSP_LOOP 24 /* loop detection packet */
  77. #define TSPTYPENUMBER 25
  78. static const char tstr[] = "[|timed]";
  79. static const char *tsptype[TSPTYPENUMBER] =
  80. { "ANY", "ADJTIME", "ACK", "MASTERREQ", "MASTERACK", "SETTIME", "MASTERUP",
  81. "SLAVEUP", "ELECTION", "ACCEPT", "REFUSE", "CONFLICT", "RESOLVE", "QUIT",
  82. "DATE", "DATEREQ", "DATEACK", "TRACEON", "TRACEOFF", "MSITE", "MSITEREQ",
  83. "TEST", "SETDATE", "SETDATEREQ", "LOOP" };
  84. void
  85. timed_print(netdissect_options *ndo,
  86. register const u_char *bp)
  87. {
  88. const struct tsp *tsp = (const struct tsp *)bp;
  89. long sec, usec;
  90. ND_TCHECK(tsp->tsp_type);
  91. if (tsp->tsp_type < TSPTYPENUMBER)
  92. ND_PRINT((ndo, "TSP_%s", tsptype[tsp->tsp_type]));
  93. else
  94. ND_PRINT((ndo, "(tsp_type %#x)", tsp->tsp_type));
  95. ND_TCHECK(tsp->tsp_vers);
  96. ND_PRINT((ndo, " vers %u", tsp->tsp_vers));
  97. ND_TCHECK(tsp->tsp_seq);
  98. ND_PRINT((ndo, " seq %u", tsp->tsp_seq));
  99. switch (tsp->tsp_type) {
  100. case TSP_LOOP:
  101. ND_TCHECK(tsp->tsp_hopcnt);
  102. ND_PRINT((ndo, " hopcnt %u", tsp->tsp_hopcnt));
  103. break;
  104. case TSP_SETTIME:
  105. case TSP_ADJTIME:
  106. case TSP_SETDATE:
  107. case TSP_SETDATEREQ:
  108. ND_TCHECK(tsp->tsp_time);
  109. sec = EXTRACT_32BITS(&tsp->tsp_time.tv_sec);
  110. usec = EXTRACT_32BITS(&tsp->tsp_time.tv_usec);
  111. /* XXX The comparison below is always false? */
  112. if (usec < 0)
  113. /* invalid, skip the rest of the packet */
  114. return;
  115. ND_PRINT((ndo, " time "));
  116. if (sec < 0 && usec != 0) {
  117. sec++;
  118. if (sec == 0)
  119. ND_PRINT((ndo, "-"));
  120. usec = 1000000 - usec;
  121. }
  122. ND_PRINT((ndo, "%ld.%06ld", sec, usec));
  123. break;
  124. }
  125. ND_TCHECK(tsp->tsp_name);
  126. ND_PRINT((ndo, " name "));
  127. if (fn_print(ndo, (const u_char *)tsp->tsp_name, (const u_char *)tsp->tsp_name + sizeof(tsp->tsp_name)))
  128. goto trunc;
  129. return;
  130. trunc:
  131. ND_PRINT((ndo, " %s", tstr));
  132. }