AndroidWrapper.c 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. static Iface_DEFUN 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 NULL;
  43. }
  44. int version = Headers_getIpVersion(msg->bytes);
  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 NULL;
  53. }
  54. Message_shift(msg, 4, NULL);
  55. ((uint16_t*) msg->bytes)[0] = 0;
  56. ((uint16_t*) msg->bytes)[1] = ethertype;
  57. return Iface_next(&ctx->pub.internalIf, msg);
  58. }
  59. static Iface_DEFUN 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 NULL;
  66. }
  67. Message_shift(msg, -4, NULL);
  68. return Iface_next(&ctx->pub.externalIf, msg);
  69. }
  70. struct AndroidWrapper* AndroidWrapper_new(struct Allocator* alloc, struct Log* log)
  71. {
  72. struct AndroidWrapper_pvt* context =
  73. Allocator_calloc(alloc, sizeof(struct AndroidWrapper_pvt), 1);
  74. Identity_set(context);
  75. context->pub.externalIf.send = incomingFromWire;
  76. context->pub.internalIf.send = incomingFromUs;
  77. context->logger = log;
  78. return &context->pub;
  79. }