mac.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /* $Id:$ */
  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. Local Variables:
  97. mode:c
  98. indent-tabs-mode:nil
  99. c-basic-offset:4
  100. End:
  101. */