mac.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /* $Id: mac.c 1487 2006-07-09 02:43:55Z aturner $ */
  2. /* Copyright 2004-2005 Aaron Turner
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the names of the copyright owners nor the names of its
  15. * contributors may be used to endorse or promote products derived from
  16. * this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
  19. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  20. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  21. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  23. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  24. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  25. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
  26. * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  27. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  28. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. */
  30. #include "config.h"
  31. #include "defines.h"
  32. #include "common.h"
  33. #include "mac.h"
  34. /*
  35. * converts a string representation of a MAC address, based on
  36. * non-portable ether_aton()
  37. */
  38. void
  39. mac2hex(const char *mac, u_char *dst, int len)
  40. {
  41. int i;
  42. long l;
  43. char *pp;
  44. if (len < 6)
  45. return;
  46. while (isspace(*mac))
  47. mac++;
  48. /* expect 6 hex octets separated by ':' or space/NUL if last octet */
  49. for (i = 0; i < 6; i++) {
  50. l = strtol(mac, &pp, 16);
  51. if (pp == mac || l > 0xFF || l < 0)
  52. return;
  53. if (!(*pp == ':' || (i == 5 && (isspace(*pp) || *pp == '\0'))))
  54. return;
  55. dst[i] = (u_char) l;
  56. mac = pp + 1;
  57. }
  58. }
  59. /*
  60. * converts a string representation of TWO MAC addresses, which
  61. * are comma deliminated into two hex values. Either *first or *second
  62. * can be NULL if there is nothing before or after the comma.
  63. * returns:
  64. * 1 = first mac
  65. * 2 = second mac
  66. * 3 = both mac's
  67. * 0 = none
  68. */
  69. int
  70. dualmac2hex(const char *dualmac, u_char *first, u_char *second, int len)
  71. {
  72. char *tok, *temp, *string;
  73. int ret = 0;
  74. string = safe_strdup(dualmac);
  75. /* if we've only got a comma, then return NULL's */
  76. if (len <= 1) {
  77. second = first = NULL;
  78. return 0;
  79. }
  80. temp = strtok_r(string, ",", &tok);
  81. if (strlen(temp)) {
  82. mac2hex(temp, first, len);
  83. ret = 1;
  84. }
  85. temp = strtok_r(NULL, ",", &tok);
  86. /* temp is null if no comma */
  87. if (temp != NULL) {
  88. if (strlen(temp)) {
  89. mac2hex(temp, second, len);
  90. ret += 2;
  91. }
  92. }
  93. return ret;
  94. }
  95. /*
  96. * Figures out if a MAC is listed in a comma delimited
  97. * string of MAC addresses.
  98. * returns CACHE_PRIMARY if listed
  99. * returns CACHE_SECONDARY if not listed
  100. */
  101. int
  102. macinstring(const char *macstring, const u_char *mac)
  103. {
  104. char *tok, *tempstr, *ourstring;
  105. u_char tempmac[6];
  106. int len = 6, ret = CACHE_SECONDARY;
  107. ourstring = safe_strdup(macstring);
  108. tempstr = strtok_r(ourstring, ",", &tok);
  109. if (strlen(tempstr)) {
  110. mac2hex(tempstr, tempmac, len);
  111. if (memcmp(mac, tempmac, len) == 0) {
  112. dbgx(3, "Packet matches: " MAC_FORMAT " sending out primary.\n", MAC_STR(tempmac));
  113. ret = CACHE_PRIMARY;
  114. goto EXIT_MACINSTRING;
  115. }
  116. } else {
  117. goto EXIT_MACINSTRING;
  118. }
  119. while ((tempstr = strtok_r(NULL, ",", &tok)) != NULL) {
  120. mac2hex(tempstr, tempmac, len);
  121. if (memcmp(mac, tempmac, len) == 0) {
  122. ret = CACHE_PRIMARY;
  123. dbgx(3, "Packet matches: " MAC_FORMAT " sending out primary.\n", MAC_STR(tempmac));
  124. goto EXIT_MACINSTRING;
  125. }
  126. }
  127. EXIT_MACINSTRING:
  128. free(ourstring);
  129. #ifdef DEBUG
  130. if (ret == CACHE_SECONDARY)
  131. dbg(3, "Packet doesn't match any MAC addresses sending out secondary.\n");
  132. #endif
  133. return ret;
  134. }
  135. /*
  136. Local Variables:
  137. mode:c
  138. indent-tabs-mode:nil
  139. c-basic-offset:4
  140. End:
  141. */