myipaddr.c 942 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. #include <u.h>
  2. #include <libc.h>
  3. #include <ip.h>
  4. static uchar loopbacknet[IPaddrlen] = {
  5. 0, 0, 0, 0,
  6. 0, 0, 0, 0,
  7. 0, 0, 0xff, 0xff,
  8. 127, 0, 0, 0
  9. };
  10. static uchar loopbackmask[IPaddrlen] = {
  11. 0xff, 0xff, 0xff, 0xff,
  12. 0xff, 0xff, 0xff, 0xff,
  13. 0xff, 0xff, 0xff, 0xff,
  14. 0xff, 0, 0, 0
  15. };
  16. // find first ip addr that isn't the friggin loopback address
  17. // unless there are no others
  18. int
  19. myipaddr(uchar *ip, char *net)
  20. {
  21. Ipifc *nifc;
  22. Iplifc *lifc;
  23. static Ipifc *ifc;
  24. uchar mynet[IPaddrlen];
  25. int haveloopback = 0;
  26. ifc = readipifc(net, ifc, -1);
  27. for(nifc = ifc; nifc; nifc = nifc->next)
  28. for(lifc = nifc->lifc; lifc; lifc = lifc->next){
  29. maskip(lifc->ip, loopbackmask, mynet);
  30. if(ipcmp(mynet, loopbacknet) == 0){
  31. ipmove(ip, lifc->ip);
  32. haveloopback = 1;
  33. continue;
  34. }
  35. if(ipcmp(lifc->ip, IPnoaddr) != 0){
  36. ipmove(ip, lifc->ip);
  37. return 0;
  38. }
  39. }
  40. if(haveloopback)
  41. return 0;
  42. ipmove(ip, IPnoaddr);
  43. return -1;
  44. }