Guest User

Untitled

a guest
Feb 17th, 2019
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.98 KB | None | 0 0
  1. 1111 1111 1111 1111 1111 1111 1000 0000
  2.  
  3. octet 1 . octet 2 . octet 3 . octet 4
  4. 0000 0000 0000 0000 0000 0000 0000 0000
  5. 1111 1111 1111 1111 1111 1111 1000 0000
  6.  
  7. 255 . 255 . 255 . 128
  8.  
  9. In bytes: 128.2.19.0
  10. In binary 10000000 00000010 00010011 00000000
  11. The bitmask: 11111111 11111111 11111111 10000000
  12. Ergo: ------- network ------------ host
  13.  
  14. #include <stdio.h>
  15. #include <arpa/inet.h>
  16.  
  17. uint32_t cidr_to_netmask(uint8_t cidr)
  18. {
  19. uint8_t unset_bits = 32 - cidr;
  20. return ntohl(0xffffffff << unset_bits);
  21. }
  22.  
  23. int main(void)
  24. {
  25. uint8_t cidr = 25;
  26.  
  27. uint32_t _netmask = cidr_to_netmask(cidr);
  28. struct in_addr _netmask_addr = { _netmask };
  29.  
  30. char netmask[16];
  31. if (inet_ntop(AF_INET, (struct in_addr *)&_netmask_addr, (char *)&netmask, sizeof(netmask)) == NULL) {
  32. fprintf(stderr, "error.n");
  33. return 1;
  34. }
  35.  
  36. printf("%d = %sn", cidr, netmask);
  37.  
  38. return 0;
  39. }
  40.  
  41. 25 = 255.255.255.128
Add Comment
Please, Sign In to add comment