2021-03-02 18:18:22 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2022-07-28 16:07:38 +00:00
|
|
|
#include "platform.hpp"
|
2021-03-09 22:24:35 +00:00
|
|
|
#include "common.hpp"
|
2021-03-02 18:18:22 +00:00
|
|
|
#include <llarp.hpp>
|
|
|
|
|
|
|
|
namespace llarp::vpn
|
|
|
|
{
|
|
|
|
class AndroidInterface : public NetworkInterface
|
|
|
|
{
|
|
|
|
const int m_fd;
|
|
|
|
|
|
|
|
public:
|
2022-07-28 16:07:38 +00:00
|
|
|
AndroidInterface(InterfaceInfo info, int fd) : NetworkInterface{std::move(info)}, m_fd{fd}
|
2021-03-02 18:18:22 +00:00
|
|
|
{
|
|
|
|
if (m_fd == -1)
|
|
|
|
throw std::runtime_error(
|
|
|
|
"Error opening AndroidVPN layer FD: " + std::string{strerror(errno)});
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual ~AndroidInterface()
|
|
|
|
{
|
|
|
|
if (m_fd != -1)
|
|
|
|
::close(m_fd);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
PollFD() const override
|
|
|
|
{
|
|
|
|
return m_fd;
|
|
|
|
}
|
|
|
|
|
|
|
|
net::IPPacket
|
|
|
|
ReadNextPacket() override
|
|
|
|
{
|
2022-07-28 16:07:38 +00:00
|
|
|
std::vector<byte_t> pkt;
|
|
|
|
pkt.reserve(net::IPPacket::MaxSize);
|
|
|
|
const auto n = read(m_fd, pkt.data(), pkt.capacity());
|
|
|
|
pkt.resize(std::min(std::max(ssize_t{}, n), static_cast<ssize_t>(pkt.capacity())));
|
|
|
|
return net::IPPacket{std::move(pkt)};
|
2021-03-02 18:18:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
WritePacket(net::IPPacket pkt) override
|
|
|
|
{
|
2022-07-28 16:07:38 +00:00
|
|
|
const auto sz = write(m_fd, pkt.data(), pkt.size());
|
2021-03-02 18:18:22 +00:00
|
|
|
if (sz <= 0)
|
|
|
|
return false;
|
2022-07-28 16:07:38 +00:00
|
|
|
return sz == static_cast<ssize_t>(pkt.size());
|
2021-03-02 18:18:22 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-08-27 23:49:01 +00:00
|
|
|
class AndroidRouteManager : public IRouteManager
|
|
|
|
{
|
2022-07-28 16:07:38 +00:00
|
|
|
void AddRoute(net::ipaddr_t, net::ipaddr_t) override{};
|
2021-08-27 23:49:01 +00:00
|
|
|
|
2022-07-28 16:07:38 +00:00
|
|
|
void DelRoute(net::ipaddr_t, net::ipaddr_t) override{};
|
2021-08-27 23:49:01 +00:00
|
|
|
|
2022-07-28 16:07:38 +00:00
|
|
|
void
|
|
|
|
AddDefaultRouteViaInterface(NetworkInterface&) override{};
|
2021-08-27 23:49:01 +00:00
|
|
|
|
2022-07-28 16:07:38 +00:00
|
|
|
void
|
|
|
|
DelDefaultRouteViaInterface(NetworkInterface&) override{};
|
2021-08-27 23:49:01 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
AddRouteViaInterface(NetworkInterface&, IPRange) override{};
|
|
|
|
|
|
|
|
void
|
|
|
|
DelRouteViaInterface(NetworkInterface&, IPRange) override{};
|
|
|
|
|
2022-07-28 16:07:38 +00:00
|
|
|
std::vector<net::ipaddr_t>
|
|
|
|
GetGatewaysNotOnInterface(NetworkInterface&) override
|
2021-08-27 23:49:01 +00:00
|
|
|
{
|
2022-07-28 16:07:38 +00:00
|
|
|
return std::vector<net::ipaddr_t>{};
|
2021-08-27 23:49:01 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2021-03-02 18:18:22 +00:00
|
|
|
class AndroidPlatform : public Platform
|
|
|
|
{
|
|
|
|
const int fd;
|
2021-08-27 23:49:01 +00:00
|
|
|
AndroidRouteManager _routeManager{};
|
2021-03-02 18:18:22 +00:00
|
|
|
|
|
|
|
public:
|
2022-07-28 16:07:38 +00:00
|
|
|
AndroidPlatform(llarp::Context* ctx) : fd{ctx->androidFD}
|
2021-03-02 18:18:22 +00:00
|
|
|
{}
|
|
|
|
|
|
|
|
std::shared_ptr<NetworkInterface>
|
2021-12-07 16:17:20 +00:00
|
|
|
ObtainInterface(InterfaceInfo info, AbstractRouter*) override
|
2021-03-02 18:18:22 +00:00
|
|
|
{
|
|
|
|
return std::make_shared<AndroidInterface>(std::move(info), fd);
|
|
|
|
}
|
2021-08-27 23:49:01 +00:00
|
|
|
IRouteManager&
|
|
|
|
RouteManager() override
|
|
|
|
{
|
|
|
|
return _routeManager;
|
|
|
|
}
|
2021-03-02 18:18:22 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace llarp::vpn
|