ipaux.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. /*
  13. * well known IP addresses
  14. */
  15. uint8_t IPv4bcast[IPaddrlen] = {
  16. 0, 0, 0, 0,
  17. 0, 0, 0, 0,
  18. 0, 0, 0xff, 0xff,
  19. 0xff, 0xff, 0xff, 0xff
  20. };
  21. uint8_t IPv4allsys[IPaddrlen] = {
  22. 0, 0, 0, 0,
  23. 0, 0, 0, 0,
  24. 0, 0, 0xff, 0xff,
  25. 0xe0, 0, 0, 0x01
  26. };
  27. uint8_t IPv4allrouter[IPaddrlen] = {
  28. 0, 0, 0, 0,
  29. 0, 0, 0, 0,
  30. 0, 0, 0xff, 0xff,
  31. 0xe0, 0, 0, 0x02
  32. };
  33. uint8_t IPallbits[IPaddrlen] = {
  34. 0xff, 0xff, 0xff, 0xff,
  35. 0xff, 0xff, 0xff, 0xff,
  36. 0xff, 0xff, 0xff, 0xff,
  37. 0xff, 0xff, 0xff, 0xff
  38. };
  39. uint8_t IPnoaddr[IPaddrlen];
  40. /*
  41. * prefix of all v4 addresses
  42. */
  43. uint8_t v4prefix[IPaddrlen] = {
  44. 0, 0, 0, 0,
  45. 0, 0, 0, 0,
  46. 0, 0, 0xff, 0xff,
  47. 0, 0, 0, 0
  48. };
  49. int
  50. isv4(uint8_t *ip)
  51. {
  52. return memcmp(ip, v4prefix, IPv4off) == 0;
  53. }
  54. /*
  55. * the following routines are unrolled with no memset's to speed
  56. * up the usual case
  57. */
  58. void
  59. v4tov6(uint8_t *v6, uint8_t *v4)
  60. {
  61. v6[0] = 0;
  62. v6[1] = 0;
  63. v6[2] = 0;
  64. v6[3] = 0;
  65. v6[4] = 0;
  66. v6[5] = 0;
  67. v6[6] = 0;
  68. v6[7] = 0;
  69. v6[8] = 0;
  70. v6[9] = 0;
  71. v6[10] = 0xff;
  72. v6[11] = 0xff;
  73. v6[12] = v4[0];
  74. v6[13] = v4[1];
  75. v6[14] = v4[2];
  76. v6[15] = v4[3];
  77. }
  78. int
  79. v6tov4(uint8_t *v4, uint8_t *v6)
  80. {
  81. if(v6[0] == 0
  82. && v6[1] == 0
  83. && v6[2] == 0
  84. && v6[3] == 0
  85. && v6[4] == 0
  86. && v6[5] == 0
  87. && v6[6] == 0
  88. && v6[7] == 0
  89. && v6[8] == 0
  90. && v6[9] == 0
  91. && v6[10] == 0xff
  92. && v6[11] == 0xff)
  93. {
  94. v4[0] = v6[12];
  95. v4[1] = v6[13];
  96. v4[2] = v6[14];
  97. v4[3] = v6[15];
  98. return 0;
  99. } else {
  100. memset(v4, 0, 4);
  101. if(memcmp(v6, IPnoaddr, IPaddrlen) == 0)
  102. return 0;
  103. return -1;
  104. }
  105. }