ether_addr.c 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. #include <ctype.h>
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <netinet/ether.h>
  19. #include <netinet/if_ether.h>
  20. #define __FORCE_GLIBC
  21. struct ether_addr *ether_aton(const char *asc)
  22. {
  23. static struct ether_addr result;
  24. return ether_aton_r(asc, &result);
  25. }
  26. struct ether_addr *ether_aton_r(const char *asc, struct ether_addr *addr)
  27. {
  28. size_t cnt;
  29. for (cnt = 0; cnt < 6; ++cnt) {
  30. unsigned int number;
  31. char ch;
  32. ch = _tolower(*asc++);
  33. if ((ch < '0' || ch > '9') && (ch < 'a' || ch > 'f'))
  34. return NULL;
  35. number = isdigit(ch) ? (ch - '0') : (ch - 'a' + 10);
  36. ch = _tolower(*asc);
  37. if ((cnt < 5 && ch != ':')
  38. || (cnt == 5 && ch != '\0' && !isspace(ch))) {
  39. ++asc;
  40. if ((ch < '0' || ch > '9') && (ch < 'a' || ch > 'f'))
  41. return NULL;
  42. number <<= 4;
  43. number += isdigit(ch) ? (ch - '0') : (ch - 'a' + 10);
  44. ch = *asc;
  45. if (cnt < 5 && ch != ':')
  46. return NULL;
  47. }
  48. /* Store result. */
  49. addr->ether_addr_octet[cnt] = (unsigned char) number;
  50. /* Skip ':'. */
  51. ++asc;
  52. }
  53. return addr;
  54. }
  55. char *ether_ntoa(const struct ether_addr *addr)
  56. {
  57. static char asc[18];
  58. return ether_ntoa_r(addr, asc);
  59. }
  60. char *ether_ntoa_r(const struct ether_addr *addr, char *buf)
  61. {
  62. sprintf(buf, "%x:%x:%x:%x:%x:%x",
  63. addr->ether_addr_octet[0], addr->ether_addr_octet[1],
  64. addr->ether_addr_octet[2], addr->ether_addr_octet[3],
  65. addr->ether_addr_octet[4], addr->ether_addr_octet[5]);
  66. return buf;
  67. }