Filter.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. * ZeroTier One - Global Peer to Peer Ethernet
  3. * Copyright (C) 2012-2013 ZeroTier Networks LLC
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. * --
  19. *
  20. * ZeroTier may be used and distributed under the terms of the GPLv3, which
  21. * are available at: http://www.gnu.org/licenses/gpl-3.0.html
  22. *
  23. * If you would like to embed ZeroTier into a commercial application or
  24. * redistribute it in a modified binary form, please contact ZeroTier Networks
  25. * LLC. Start here: http://www.zerotier.com/
  26. */
  27. #include <stdint.h>
  28. #include "RuntimeEnvironment.hpp"
  29. #include "Logger.hpp"
  30. #include "Filter.hpp"
  31. namespace ZeroTier {
  32. bool Filter::Rule::operator()(unsigned int etype,const void *data,unsigned int len) const
  33. {
  34. if ((_etherType >= 0)&&(etype != (unsigned int)_etherType))
  35. return false; // ethertype mismatch
  36. switch(etype) {
  37. case ZT_ETHERTYPE_IPV4:
  38. if (len < 20)
  39. return false; // invalid packets don't match
  40. if ((_protocol >= 0)&&(((const uint8_t *)data)[9] != (uint8_t)(_protocol & 0xff)))
  41. return false; // IP protocol # mismatch
  42. switch(((const uint8_t *)data)[9]) {
  43. }
  44. break;
  45. case ZT_ETHERTYPE_IPV6
  46. break;
  47. }
  48. return false;
  49. }
  50. Filter::Filter(const RuntimeEnvironment *renv) :
  51. _r(renv)
  52. {
  53. }
  54. Filter::~Filter()
  55. {
  56. }
  57. void Filter::add(const Rule &r,const Action &a)
  58. {
  59. Mutex::Lock _l(_chain_m);
  60. for(std::vector<Entry>::iterator i(_chain.begin());i!=_chain.end();++i) {
  61. if (i->rule == r) {
  62. _chain.erase(i);
  63. break;
  64. }
  65. }
  66. _chain.push_back(Entry(r,a));
  67. }
  68. const char *Filter::etherTypeName(const unsigned int etherType)
  69. throw()
  70. {
  71. static char tmp[6];
  72. switch(etherType) {
  73. case ZT_ETHERTYPE_IPV4:
  74. return "IPV4";
  75. case ZT_ETHERTYPE_ARP:
  76. return "ARP";
  77. case ZT_ETHERTYPE_RARP:
  78. return "RARP";
  79. case ZT_ETHERTYPE_ATALK:
  80. return "ATALK";
  81. case ZT_ETHERTYPE_AARP:
  82. return "AARP";
  83. case ZT_ETHERTYPE_IPX_A:
  84. return "IPX_A";
  85. case ZT_ETHERTYPE_IPX_B:
  86. return "IPX_B";
  87. case ZT_ETHERTYPE_IPV6:
  88. return "IPV6";
  89. }
  90. sprintf(tmp,"%.4x",etherType);
  91. return tmp; // technically not thread safe, but we're only going to see this in debugging if ever
  92. }
  93. } // namespace ZeroTier