inet_legacy.c 628 B

1234567891011121314151617181920212223242526272829303132
  1. #include <sys/socket.h>
  2. #include <netinet/in.h>
  3. #include <arpa/inet.h>
  4. in_addr_t inet_network(const char *p)
  5. {
  6. return ntohl(inet_addr(p));
  7. }
  8. struct in_addr inet_makeaddr(in_addr_t n, in_addr_t h)
  9. {
  10. if (n < 256) h |= n<<24;
  11. else if (n < 65536) h |= n<<16;
  12. else h |= n<<8;
  13. return (struct in_addr){ h };
  14. }
  15. in_addr_t inet_lnaof(struct in_addr in)
  16. {
  17. uint32_t h = in.s_addr;
  18. if (h>>24 < 128) return h & 0xffffff;
  19. if (h>>24 < 192) return h & 0xffff;
  20. return h & 0xff;
  21. }
  22. in_addr_t inet_netof(struct in_addr in)
  23. {
  24. uint32_t h = in.s_addr;
  25. if (h>>24 < 128) return h >> 24;
  26. if (h>>24 < 192) return h >> 16;
  27. return h >> 8;
  28. }