in_ether.c 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Utility routines.
  4. */
  5. //kbuild:lib-$(CONFIG_ARP) += in_ether.o
  6. //kbuild:lib-$(CONFIG_IFCONFIG) += in_ether.o
  7. //kbuild:lib-$(CONFIG_IFENSLAVE) += in_ether.o
  8. #include "libbb.h"
  9. #include <net/if_arp.h>
  10. #include <net/ethernet.h>
  11. /* Convert Ethernet address from "XX[:]XX[:]XX[:]XX[:]XX[:]XX" to sockaddr.
  12. * Return nonzero on error.
  13. */
  14. int FAST_FUNC in_ether(const char *bufp, struct sockaddr *sap)
  15. {
  16. char *ptr;
  17. int i, j;
  18. unsigned char val;
  19. unsigned char c;
  20. sap->sa_family = ARPHRD_ETHER;
  21. ptr = (char *) sap->sa_data;
  22. i = ETH_ALEN;
  23. goto first;
  24. do {
  25. /* We might get a semicolon here */
  26. if (*bufp == ':')
  27. bufp++;
  28. first:
  29. j = val = 0;
  30. do {
  31. c = *bufp;
  32. if (((unsigned char)(c - '0')) <= 9) {
  33. c -= '0';
  34. } else if ((unsigned char)((c|0x20) - 'a') <= 5) {
  35. c = (unsigned char)((c|0x20) - 'a') + 10;
  36. } else {
  37. if (j && (c == ':' || c == '\0'))
  38. /* One-digit byte: __:X:__ */
  39. break;
  40. return -1;
  41. }
  42. ++bufp;
  43. val <<= 4;
  44. val += c;
  45. j ^= 1;
  46. } while (j);
  47. *ptr++ = val;
  48. } while (--i);
  49. /* Error if we aren't at end of string */
  50. return *bufp;
  51. }