AndroidWrapper.c 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. #include "interface/Iface.h"
  16. #include "interface/tuntap/AndroidWrapper.h"
  17. #include "util/platform/Sockaddr.h"
  18. #include "memory/Allocator.h"
  19. #include "util/Assert.h"
  20. #include "util/Identity.h"
  21. #include "wire/Ethernet.h"
  22. #include "wire/Headers.h"
  23. #include "wire/Message.h"
  24. #include "wire/Error.h"
  25. /**
  26. * Android VpnService is expect you to read/write packet from the tun device
  27. * file description opened by system process rather than in the cjd process,
  28. * this InterfaceWrapper handle this case.
  29. */
  30. struct AndroidWrapper_pvt
  31. {
  32. struct AndroidWrapper pub;
  33. struct Log* logger;
  34. Identity
  35. };
  36. Iface_DEFUN AndroidWrapper_incomingFromWire(struct Message* msg, struct Iface* externalIf)
  37. {
  38. struct AndroidWrapper_pvt* ctx =
  39. Identity_containerOf(externalIf, struct AndroidWrapper_pvt, pub.externalIf);
  40. if (!ctx->pub.internalIf.connectedIf) {
  41. Log_debug(ctx->logger, "DROP message for android tun not inited");
  42. return Error(msg, "UNHANDLED");
  43. }
  44. int version = Headers_getIpVersion(msg->msgbytes);
  45. uint16_t ethertype = 0;
  46. if (version == 4) {
  47. ethertype = Ethernet_TYPE_IP4;
  48. } else if (version == 6) {
  49. ethertype = Ethernet_TYPE_IP6;
  50. } else {
  51. Log_debug(ctx->logger, "Message is not IP/IPv6, dropped.");
  52. return Error(msg, "INVALID not IP");
  53. }
  54. Er_assert(Message_eshift(msg, 4));
  55. ((uint16_t*) msg->msgbytes)[0] = 0;
  56. ((uint16_t*) msg->msgbytes)[1] = ethertype;
  57. return Iface_next(&ctx->pub.internalIf, msg);
  58. }
  59. Iface_DEFUN AndroidWrapper_incomingFromUs(struct Message* msg, struct Iface* internalIf)
  60. {
  61. struct AndroidWrapper_pvt* ctx =
  62. Identity_containerOf(internalIf, struct AndroidWrapper_pvt, pub.internalIf);
  63. if (!ctx->pub.externalIf.connectedIf) {
  64. Log_debug(ctx->logger, "DROP message for android tun not inited");
  65. return Error(msg, "UNHANDLED");
  66. }
  67. Er_assert(Message_eshift(msg, -4));
  68. return Iface_next(&ctx->pub.externalIf, msg);
  69. }
  70. struct AndroidWrapper* AndroidWrapper_new(struct Allocator* alloc, struct Log* log)
  71. {
  72. return NULL;
  73. }