ether_addr.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. * Copyright (C) 2006 by egnite Software GmbH
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. *
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. * 3. Neither the name of the copyright holders nor the names of
  16. * contributors may be used to endorse or promote products derived
  17. * from this software without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  22. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  23. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  24. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  25. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  26. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  27. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  28. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  29. * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  30. * SUCH DAMAGE.
  31. *
  32. * For additional information see http://www.ethernut.de/
  33. */
  34. /*!
  35. * \file net/ether_addr.c
  36. * \brief MAC address conversion functions.
  37. *
  38. * \verbatim
  39. * $Id: ether_addr.c 4473 2012-08-20 15:12:45Z haraldkipp $
  40. * \endverbatim
  41. */
  42. #include <string.h>
  43. #include <netinet/if_ether.h>
  44. static INLINE int hex2bin(char c)
  45. {
  46. if (c >= '0' && c <= '9')
  47. return c - '0';
  48. if (c >= 'a' && c <= 'f')
  49. return c - 'a' + 10;
  50. if (c >= 'A' && c <= 'F')
  51. return c - 'A' + 10;
  52. return 0;
  53. }
  54. static char hexdigit[] = "0123456789ABCDEF";
  55. /*!
  56. * \brief Convert an ASCII string to a binary Ethernet address.
  57. *
  58. * \note This function returns a pointer to internal static storage space
  59. * that will be overwritten by subsequent calls.
  60. *
  61. * \param str String to convert.
  62. *
  63. * \return Pointer to a static string that contains the converted
  64. * binary address.
  65. */
  66. uint8_t *ether_aton(const char *str)
  67. {
  68. static uint8_t mac[6];
  69. const char *cp = str;
  70. int n = 0;
  71. memset(mac, 0, sizeof(mac));
  72. for (cp = str; *cp;) {
  73. if (*cp < '0' || (*cp > '9' && *cp < 'A') || (*cp > 'F' && *cp < 'a') || *cp > 'f') {
  74. return NULL;
  75. }
  76. if (mac[n] > 0x0F) {
  77. return NULL;
  78. }
  79. mac[n] <<= 4;
  80. mac[n] |= hex2bin(*cp);
  81. cp++;
  82. if (*cp == ':') {
  83. n++;
  84. if (n > 5) {
  85. return NULL;
  86. }
  87. cp++;
  88. }
  89. }
  90. return mac;
  91. }
  92. /*!
  93. * \brief Convert a binary Ethernet address to an ASCII string.
  94. *
  95. * \note This function returns a pointer to internal static storage space
  96. * that will be overwritten by subsequent calls.
  97. *
  98. * \param mac Address to convert.
  99. *
  100. * \return Pointer to a static string that contains the converted
  101. * ASCII string.
  102. */
  103. char *ether_ntoa(const uint8_t *mac)
  104. {
  105. static char str[18];
  106. uint_fast8_t i;
  107. char *cp = str;
  108. for (i = 0; i < 6; i++) {
  109. *cp++ = hexdigit[(mac[i] >> 4) & 0x0F];
  110. *cp++ = hexdigit[mac[i] & 0x0F];
  111. *cp++ = ':';
  112. }
  113. *--cp = 0;
  114. return str;
  115. }