TUNMessageType.h 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. /* vim: set expandtab ts=4 sw=4: */
  2. /*
  3. * You may redistribute this program and/or modify it under the terms of
  4. * the GNU General Public License as published by the Free Software Foundation,
  5. * either version 3 of the License, or (at your option) any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  14. */
  15. #ifndef TUNMessageType_H
  16. #define TUNMessageType_H
  17. #include "util/Defined.h"
  18. #include "wire/Message.h"
  19. static inline Er_DEFUN(void TUNMessageType_push(struct Message* message,
  20. uint16_t ethertype))
  21. {
  22. Er(Message_eshift(message, 4));
  23. ((uint16_t*) message->msgbytes)[0] = 0;
  24. ((uint16_t*) message->msgbytes)[1] = ethertype;
  25. Er_ret();
  26. }
  27. static inline Er_DEFUN(uint16_t TUNMessageType_pop(struct Message* message))
  28. {
  29. Er(Message_eshift(message, -4));
  30. Er_ret( ((uint16_t*) message->msgbytes)[-1] );
  31. }
  32. enum TUNMessageType {
  33. // Ethertype header, used by linux
  34. TUNMessageType_ETHERTYPE = 0,
  35. // No header, used by android and sunos, ipv4 and ipv6 only
  36. TUNMessageType_NONE = 1,
  37. // address family header, used by BSD
  38. TUNMessageType_AF = 2,
  39. };
  40. static inline enum TUNMessageType TUNMessageType_guess()
  41. {
  42. if (Defined(android) || Defined(sunos)) {
  43. return TUNMessageType_NONE;
  44. } else if (Defined(linux) || Defined(win32)) {
  45. return TUNMessageType_ETHERTYPE;
  46. } else {
  47. return TUNMessageType_AF;
  48. }
  49. }
  50. #endif