mac.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /* $Id: mac.c 1897 2007-08-25 04:57:38Z 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 <string.h>
  34. #include <stdlib.h>
  35. #include <limits.h>
  36. #include <ctype.h>
  37. #include "mac.h"
  38. /**
  39. * converts a string representation of a MAC address, based on
  40. * non-portable ether_aton()
  41. */
  42. void
  43. mac2hex(const char *mac, u_char *dst, int len)
  44. {
  45. int i;
  46. long l;
  47. char *pp;
  48. if (len < 6)
  49. return;
  50. while (isspace(*mac))
  51. mac++;
  52. /* expect 6 hex octets separated by ':' or space/NUL if last octet */
  53. for (i = 0; i < 6; i++) {
  54. l = strtol(mac, &pp, 16);
  55. if (pp == mac || l > 0xFF || l < 0)
  56. return;
  57. if (!(*pp == ':' || (i == 5 && (isspace(*pp) || *pp == '\0'))))
  58. return;
  59. dst[i] = (u_char) l;
  60. mac = pp + 1;
  61. }
  62. }
  63. /**
  64. * converts a string representation of TWO MAC addresses, which
  65. * are comma deliminated into two hex values. Either *first or *second
  66. * can be NULL if there is nothing before or after the comma.
  67. * returns:
  68. * 1 = first mac
  69. * 2 = second mac
  70. * 3 = both mac's
  71. * 0 = none
  72. */
  73. int
  74. dualmac2hex(const char *dualmac, u_char *first, u_char *second, int len)
  75. {
  76. char *tok, *temp, *string;
  77. int ret = 0;
  78. string = safe_strdup(dualmac);
  79. /* if we've only got a comma, then return NULL's */
  80. if (len <= 1) {
  81. second = first = NULL;
  82. return 0;
  83. }
  84. temp = strtok_r(string, ",", &tok);
  85. if (strlen(temp)) {
  86. mac2hex(temp, first, len);
  87. ret = 1;
  88. }
  89. temp = strtok_r(NULL, ",", &tok);
  90. /* temp is null if no comma */
  91. if (temp != NULL) {
  92. if (strlen(temp)) {
  93. mac2hex(temp, second, len);
  94. ret += 2;
  95. }
  96. }
  97. return ret;
  98. }
  99. /**
  100. * Figures out if a MAC is listed in a comma delimited
  101. * string of MAC addresses.
  102. * returns TCPR_DIR_C2S if listed
  103. * returns TCPR_DIR_S2C if not listed
  104. */
  105. tcpr_dir_t
  106. macinstring(const char *macstring, const u_char *mac)
  107. {
  108. char *tok, *tempstr, *ourstring;
  109. u_char tempmac[6];
  110. int len = 6, ret = TCPR_DIR_S2C;
  111. ourstring = safe_strdup(macstring);
  112. tempstr = strtok_r(ourstring, ",", &tok);
  113. if (strlen(tempstr)) {
  114. mac2hex(tempstr, tempmac, len);
  115. if (memcmp(mac, tempmac, len) == 0) {
  116. dbgx(3, "Packet matches: " MAC_FORMAT " sending out primary.\n", MAC_STR(tempmac));
  117. ret = TCPR_DIR_C2S;
  118. goto EXIT_MACINSTRING;
  119. }
  120. } else {
  121. goto EXIT_MACINSTRING;
  122. }
  123. while ((tempstr = strtok_r(NULL, ",", &tok)) != NULL) {
  124. mac2hex(tempstr, tempmac, len);
  125. if (memcmp(mac, tempmac, len) == 0) {
  126. ret = TCPR_DIR_C2S;
  127. dbgx(3, "Packet matches: " MAC_FORMAT " sending out primary.\n", MAC_STR(tempmac));
  128. goto EXIT_MACINSTRING;
  129. }
  130. }
  131. EXIT_MACINSTRING:
  132. safe_free(ourstring);
  133. #ifdef DEBUG
  134. if (ret == TCPR_DIR_S2C)
  135. dbg(3, "Packet doesn't match any MAC addresses sending out secondary.\n");
  136. #endif
  137. return ret;
  138. }
  139. /*
  140. Local Variables:
  141. mode:c
  142. indent-tabs-mode:nil
  143. c-basic-offset:4
  144. End:
  145. */