pim_util.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*
  2. PIM for Quagga
  3. Copyright (C) 2008 Everton da Silva Marques
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; see the file COPYING; if not, write to the
  14. Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
  15. MA 02110-1301 USA
  16. $QuaggaId: $Format:%an, %ai, %h$ $
  17. */
  18. #include <zebra.h>
  19. #include "log.h"
  20. #include "pim_util.h"
  21. /*
  22. RFC 3376: 4.1.7. QQIC (Querier's Query Interval Code)
  23. If QQIC < 128, QQI = QQIC
  24. If QQIC >= 128, QQI = (mant | 0x10) << (exp + 3)
  25. 0 1 2 3 4 5 6 7
  26. +-+-+-+-+-+-+-+-+
  27. |1| exp | mant |
  28. +-+-+-+-+-+-+-+-+
  29. Since exp=0..7 then (exp+3)=3..10, then QQI has
  30. one of the following bit patterns:
  31. exp=0: QQI = 0000.0000.1MMM.M000
  32. exp=1: QQI = 0000.0001.MMMM.0000
  33. ...
  34. exp=6: QQI = 001M.MMM0.0000.0000
  35. exp=7: QQI = 01MM.MM00.0000.0000
  36. --------- ---------
  37. 0x4 0x0 0x0 0x0
  38. */
  39. uint8_t igmp_msg_encode16to8(uint16_t value)
  40. {
  41. uint8_t code;
  42. if (value < 128) {
  43. code = value;
  44. }
  45. else {
  46. uint16_t mask = 0x4000;
  47. uint8_t exp;
  48. uint16_t mant;
  49. for (exp = 7; exp > 0; --exp) {
  50. if (mask & value)
  51. break;
  52. mask >>= 1;
  53. }
  54. mant = 0x000F & (value >> (exp + 3));
  55. code = ((uint8_t) 1 << 7) | ((uint8_t) exp << 4) | (uint8_t) mant;
  56. }
  57. return code;
  58. }
  59. /*
  60. RFC 3376: 4.1.7. QQIC (Querier's Query Interval Code)
  61. If QQIC < 128, QQI = QQIC
  62. If QQIC >= 128, QQI = (mant | 0x10) << (exp + 3)
  63. 0 1 2 3 4 5 6 7
  64. +-+-+-+-+-+-+-+-+
  65. |1| exp | mant |
  66. +-+-+-+-+-+-+-+-+
  67. */
  68. uint16_t igmp_msg_decode8to16(uint8_t code)
  69. {
  70. uint16_t value;
  71. if (code < 128) {
  72. value = code;
  73. }
  74. else {
  75. uint16_t mant = (code & 0x0F);
  76. uint8_t exp = (code & 0x70) >> 4;
  77. value = (mant | 0x10) << (exp + 3);
  78. }
  79. return value;
  80. }
  81. void pim_pkt_dump(const char *label, const uint8_t *buf, int size)
  82. {
  83. char dump_buf[1000];
  84. int i = 0;
  85. int j = 0;
  86. for (; i < size; ++i, j += 2) {
  87. int left = sizeof(dump_buf) - j;
  88. if (left < 4) {
  89. if (left > 1) {
  90. strcat(dump_buf + j, "!"); /* mark as truncated */
  91. }
  92. break;
  93. }
  94. snprintf(dump_buf + j, left, "%02x", buf[i]);
  95. }
  96. zlog_debug("%s: pkt dump size=%d: %s",
  97. label,
  98. size,
  99. dump_buf);
  100. }