classmask.c 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. #include <u.h>
  10. #include <libc.h>
  11. #include <ip.h>
  12. static uint8_t classmask[4][16] = {
  13. 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, 0xff,0x00,0x00,0x00,
  14. 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, 0xff,0x00,0x00,0x00,
  15. 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, 0xff,0xff,0x00,0x00,
  16. 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0x00,
  17. };
  18. static uint8_t v6loopback[IPaddrlen] = {
  19. 0, 0, 0, 0,
  20. 0, 0, 0, 0,
  21. 0, 0, 0, 0,
  22. 0, 0, 0, 0x01
  23. };
  24. static uint8_t v6linklocal[IPaddrlen] = {
  25. 0xfe, 0x80, 0, 0,
  26. 0, 0, 0, 0,
  27. 0, 0, 0, 0,
  28. 0, 0, 0, 0
  29. };
  30. static uint8_t v6linklocalmask[IPaddrlen] = {
  31. 0xff, 0xff, 0xff, 0xff,
  32. 0xff, 0xff, 0xff, 0xff,
  33. 0, 0, 0, 0,
  34. 0, 0, 0, 0
  35. };
  36. static int v6llpreflen = 8; /* link-local prefix length in bytes */
  37. static uint8_t v6multicast[IPaddrlen] = {
  38. 0xff, 0, 0, 0,
  39. 0, 0, 0, 0,
  40. 0, 0, 0, 0,
  41. 0, 0, 0, 0
  42. };
  43. static uint8_t v6multicastmask[IPaddrlen] = {
  44. 0xff, 0, 0, 0,
  45. 0, 0, 0, 0,
  46. 0, 0, 0, 0,
  47. 0, 0, 0, 0
  48. };
  49. static int v6mcpreflen = 1; /* multicast prefix length */
  50. static uint8_t v6solicitednode[IPaddrlen] = {
  51. 0xff, 0x02, 0, 0,
  52. 0, 0, 0, 0,
  53. 0, 0, 0, 0x01,
  54. 0xff, 0, 0, 0
  55. };
  56. static uint8_t v6solicitednodemask[IPaddrlen] = {
  57. 0xff, 0xff, 0xff, 0xff,
  58. 0xff, 0xff, 0xff, 0xff,
  59. 0xff, 0xff, 0xff, 0xff,
  60. 0xff, 0x0, 0x0, 0x0
  61. };
  62. static int v6snpreflen = 13;
  63. uint8_t*
  64. defmask(uint8_t *ip)
  65. {
  66. if(isv4(ip))
  67. return classmask[ip[IPv4off]>>6];
  68. else {
  69. if(ipcmp(ip, v6loopback) == 0)
  70. return IPallbits;
  71. else if(memcmp(ip, v6linklocal, v6llpreflen) == 0)
  72. return v6linklocalmask;
  73. else if(memcmp(ip, v6solicitednode, v6snpreflen) == 0)
  74. return v6solicitednodemask;
  75. else if(memcmp(ip, v6multicast, v6mcpreflen) == 0)
  76. return v6multicastmask;
  77. return IPallbits;
  78. }
  79. }
  80. void
  81. maskip(uint8_t *from, uint8_t *mask, uint8_t *to)
  82. {
  83. int i;
  84. for(i = 0; i < IPaddrlen; i++)
  85. to[i] = from[i] & mask[i];
  86. }