myipaddr.c 841 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  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. ifc = readipifc(net, ifc, -1);
  26. for(nifc = ifc; nifc; nifc = nifc->next)
  27. for(lifc = nifc->lifc; lifc; lifc = lifc->next){
  28. maskip(lifc->ip, loopbackmask, mynet);
  29. if(ipcmp(mynet, loopbacknet) == 0){
  30. continue;
  31. }
  32. if(ipcmp(lifc->ip, IPnoaddr) != 0){
  33. ipmove(ip, lifc->ip);
  34. return 0;
  35. }
  36. }
  37. ipmove(ip, IPnoaddr);
  38. return -1;
  39. }