ether_addr.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /* Copyright (C) 1996, 1997, 1998 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. Contributed by Ulrich Drepper <drepper@cygnus.com>, 1996.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; see the file COPYING.LIB. If
  14. not, see <http://www.gnu.org/licenses/>.
  15. */
  16. /*
  17. * 2002-12-24 Nick Fedchik <nick@fedchik.org.ua>
  18. * - initial uClibc port
  19. */
  20. #include <ctype.h>
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <netinet/ether.h>
  24. #include <netinet/if_ether.h>
  25. struct ether_addr *ether_aton_r(const char *asc, struct ether_addr *addr)
  26. {
  27. /* asc is "X:XX:XX:x:xx:xX" */
  28. int cnt;
  29. for (cnt = 0; cnt < 6; ++cnt) {
  30. unsigned char number;
  31. char ch = *asc++;
  32. if (ch < 0x20)
  33. return NULL;
  34. /* | 0x20 is cheap tolower(), valid for letters/numbers only */
  35. ch |= 0x20;
  36. if ((ch < '0' || ch > '9') && (ch < 'a' || ch > 'f'))
  37. return NULL;
  38. number = !(ch > '9') ? (ch - '0') : (ch - 'a' + 10);
  39. ch = *asc++;
  40. if ((cnt != 5 && ch != ':') /* not last group */
  41. /* What standard says ASCII ether address representation
  42. * may also finish with whitespace, not only NUL?
  43. * We can get rid of isspace() otherwise */
  44. || (cnt == 5 && ch != '\0' /*&& !isspace(ch)*/)
  45. ) {
  46. ch |= 0x20; /* cheap tolower() */
  47. if ((ch < '0' || ch > '9') && (ch < 'a' || ch > 'f'))
  48. return NULL;
  49. number = (number << 4) + (!(ch > '9') ? (ch - '0') : (ch - 'a' + 10));
  50. if (cnt != 5) {
  51. ch = *asc++;
  52. if (ch != ':')
  53. return NULL;
  54. }
  55. }
  56. /* Store result. */
  57. addr->ether_addr_octet[cnt] = number;
  58. }
  59. /* Looks like we allow garbage after last group?
  60. * "1:2:3:4:5:66anything_at_all"? */
  61. return addr;
  62. }
  63. libc_hidden_def(ether_aton_r)
  64. struct ether_addr *ether_aton(const char *asc)
  65. {
  66. static struct ether_addr result;
  67. return ether_aton_r(asc, &result);
  68. }
  69. char *ether_ntoa_r(const struct ether_addr *addr, char *buf)
  70. {
  71. sprintf(buf, "%x:%x:%x:%x:%x:%x",
  72. addr->ether_addr_octet[0], addr->ether_addr_octet[1],
  73. addr->ether_addr_octet[2], addr->ether_addr_octet[3],
  74. addr->ether_addr_octet[4], addr->ether_addr_octet[5]);
  75. return buf;
  76. }
  77. libc_hidden_def(ether_ntoa_r)
  78. char *ether_ntoa(const struct ether_addr *addr)
  79. {
  80. static char asc[18];
  81. return ether_ntoa_r(addr, asc);
  82. }