inet_legacy.c 810 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. #include <sys/socket.h>
  2. #include <netinet/in.h>
  3. #include <arpa/inet.h>
  4. #include "__dns.h"
  5. in_addr_t inet_network(const char *p)
  6. {
  7. return ntohl(inet_addr(p));
  8. }
  9. int inet_aton(const char *cp, struct in_addr *inp)
  10. {
  11. struct sockaddr_in sin;
  12. if (__ipparse(&sin, AF_INET, cp) < 0) return 0;
  13. *inp = sin.sin_addr;
  14. return 1;
  15. }
  16. struct in_addr inet_makeaddr(in_addr_t n, in_addr_t h)
  17. {
  18. if (n < 256) h |= n<<24;
  19. else if (n < 65536) h |= n<<16;
  20. else h |= n<<8;
  21. return (struct in_addr){ h };
  22. }
  23. in_addr_t inet_lnaof(struct in_addr in)
  24. {
  25. uint32_t h = in.s_addr;
  26. if (h>>24 < 128) return h & 0xffffff;
  27. if (h>>24 < 192) return h & 0xffff;
  28. return h & 0xff;
  29. }
  30. in_addr_t inet_netof(struct in_addr in)
  31. {
  32. uint32_t h = in.s_addr;
  33. if (h>>24 < 128) return h >> 24;
  34. if (h>>24 < 192) return h >> 16;
  35. return h >> 8;
  36. }