myetheraddr.c 837 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. #include <u.h>
  2. #include <libc.h>
  3. #include <ip.h>
  4. int
  5. myetheraddr(uchar *to, char *dev)
  6. {
  7. int n, fd;
  8. char buf[256], *ptr;
  9. if(*dev == '/')
  10. sprint(buf, "%s/stats", dev);
  11. else
  12. sprint(buf, "/net/%s/stats", dev);
  13. fd = open(buf, OREAD);
  14. if(fd < 0) {
  15. /* try the old place - this code will disappear on Nov 18th */
  16. /* Make one exist */
  17. if(*dev == '/')
  18. sprint(buf, "%s/clone", dev);
  19. else
  20. sprint(buf, "/net/%s/clone", dev);
  21. fd = open(buf, ORDWR);
  22. if(fd >= 0)
  23. close(fd);
  24. if(*dev == '/')
  25. sprint(buf, "%s/0/stats", dev);
  26. else
  27. sprint(buf, "/net/%s/0/stats", dev);
  28. fd = open(buf, OREAD);
  29. if(fd < 0)
  30. return -1;
  31. }
  32. n = read(fd, buf, sizeof(buf)-1);
  33. close(fd);
  34. if(n <= 0)
  35. return -1;
  36. buf[n] = 0;
  37. ptr = strstr(buf, "addr: ");
  38. if(!ptr)
  39. return -1;
  40. ptr += 6;
  41. parseether(to, ptr);
  42. return 0;
  43. }