ipaux.c 1.5 KB

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