ether_addr.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /*
  2. * Copyright (C) 1996, 1997, 1999 Free Software Foundation, Inc.
  3. * This file was assembled from parts of the GNU C Library.
  4. * Contributed by Ulrich Drepper <drepper@cygnus.com>, 1996.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. */
  11. /*
  12. * 2002-12-24 Nick Fedchik <nick@fedchik.org.ua>
  13. * - initial uClibc port
  14. */
  15. #define __FORCE_GLIBC
  16. #include <features.h>
  17. #include <ctype.h>
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <netinet/ether.h>
  21. #include <netinet/if_ether.h>
  22. struct ether_addr *ether_aton(const char *asc)
  23. {
  24. static struct ether_addr result;
  25. return ether_aton_r(asc, &result);
  26. }
  27. struct ether_addr *ether_aton_r(const char *asc, struct ether_addr *addr)
  28. {
  29. size_t cnt;
  30. for (cnt = 0; cnt < 6; ++cnt) {
  31. unsigned int number;
  32. char ch;
  33. ch = _tolower(*asc++);
  34. if ((ch < '0' || ch > '9') && (ch < 'a' || ch > 'f'))
  35. return NULL;
  36. number = isdigit(ch) ? (ch - '0') : (ch - 'a' + 10);
  37. ch = _tolower(*asc);
  38. if ((cnt < 5 && ch != ':')
  39. || (cnt == 5 && ch != '\0' && !isspace(ch))) {
  40. ++asc;
  41. if ((ch < '0' || ch > '9') && (ch < 'a' || ch > 'f'))
  42. return NULL;
  43. number <<= 4;
  44. number += isdigit(ch) ? (ch - '0') : (ch - 'a' + 10);
  45. ch = *asc;
  46. if (cnt < 5 && ch != ':')
  47. return NULL;
  48. }
  49. /* Store result. */
  50. addr->ether_addr_octet[cnt] = (unsigned char) number;
  51. /* Skip ':'. */
  52. ++asc;
  53. }
  54. return addr;
  55. }
  56. char *ether_ntoa(const struct ether_addr *addr)
  57. {
  58. static char asc[18];
  59. return ether_ntoa_r(addr, asc);
  60. }
  61. char *ether_ntoa_r(const struct ether_addr *addr, char *buf)
  62. {
  63. sprintf(buf, "%x:%x:%x:%x:%x:%x",
  64. addr->ether_addr_octet[0], addr->ether_addr_octet[1],
  65. addr->ether_addr_octet[2], addr->ether_addr_octet[3],
  66. addr->ether_addr_octet[4], addr->ether_addr_octet[5]);
  67. return buf;
  68. }