OpenVPNAdapter/OpenVPN Adapter/OpenVPNClient.mm
Sergey Abramchuk 6860411e53 Add TODOs
2017-03-16 20:00:07 +03:00

153 lines
4.8 KiB
Plaintext

//
// OpenVPNClient.m
// OpenVPN iOS Client
//
// Created by Sergey Abramchuk on 11.02.17.
//
//
#import <sstream>
#import <Foundation/Foundation.h>
#import "OpenVPNAdapter+Internal.h"
#import "OpenVPNClient.h"
OpenVPNClient::OpenVPNClient(void *adapter) : ClientAPI::OpenVPNClient() {
this->adapter = adapter;
}
bool OpenVPNClient::tun_builder_new() {
return [(__bridge OpenVPNAdapter *)adapter configureSockets];
}
bool OpenVPNClient::tun_builder_set_remote_address(const std::string &address, bool ipv6) {
// TODO: Adapter should be able to set IPv6 remote address
NSString *remoteAddress = [NSString stringWithUTF8String:address.c_str()];
return [(__bridge OpenVPNAdapter *)adapter setRemoteAddress:remoteAddress];
}
bool OpenVPNClient::tun_builder_add_address(const std::string &address, int prefix_length, const std::string &gateway, bool ipv6, bool net30) {
// TODO: Adapter should be able to add IPv6 addresses
NSString *localAddress = [NSString stringWithUTF8String:address.c_str()];
NSString *subnet = [NSString stringWithUTF8String:get_subnet(prefix_length).c_str()];
NSString *gatewayAddress = [NSString stringWithUTF8String:gateway.c_str()];
return [(__bridge OpenVPNAdapter *)adapter addLocalAddress:localAddress subnet:subnet gateway:gatewayAddress];
}
bool OpenVPNClient::tun_builder_reroute_gw(bool ipv4, bool ipv6, unsigned int flags) {
return true;
}
bool OpenVPNClient::tun_builder_add_route(const std::string& address, int prefix_length, int metric, bool ipv6) {
// TODO: Adapter should be able to add IPv6 routes
NSString *route = [NSString stringWithUTF8String:address.c_str()];
NSString *subnet = [NSString stringWithUTF8String:get_subnet(prefix_length).c_str()];
return [(__bridge OpenVPNAdapter *)adapter addRoute:route subnet:subnet];
}
bool OpenVPNClient::tun_builder_exclude_route(const std::string& address, int prefix_length, int metric, bool ipv6) {
// TODO: Adapter should be able to exclude IPv6 routes
NSString *route = [NSString stringWithUTF8String:address.c_str()];
NSString *subnet = [NSString stringWithUTF8String:get_subnet(prefix_length).c_str()];
return [(__bridge OpenVPNAdapter *)adapter excludeRoute:route subnet:subnet];
}
bool OpenVPNClient::tun_builder_add_dns_server(const std::string& address, bool ipv6) {
// TODO: Adapter should be able to add IPv6 DNS
NSString *dnsAddress = [NSString stringWithUTF8String:address.c_str()];
return [(__bridge OpenVPNAdapter *)adapter addDNSAddress:dnsAddress];
}
bool OpenVPNClient::tun_builder_add_search_domain(const std::string& domain) {
NSString *searchDomain = [NSString stringWithUTF8String:domain.c_str()];
return [(__bridge OpenVPNAdapter *)adapter addSearchDomain:searchDomain];
}
bool OpenVPNClient::tun_builder_set_mtu(int mtu) {
return [(__bridge OpenVPNAdapter *)adapter setMTU:mtu];
}
bool OpenVPNClient::tun_builder_set_session_name(const std::string& name) {
return true;
}
bool OpenVPNClient::tun_builder_add_proxy_bypass(const std::string& bypass_host) {
return true;
}
bool OpenVPNClient::tun_builder_set_proxy_auto_config_url(const std::string& url) {
return true;
}
bool OpenVPNClient::tun_builder_set_proxy_http(const std::string& host, int port) {
return true;
}
bool OpenVPNClient::tun_builder_set_proxy_https(const std::string& host, int port) {
return true;
}
bool OpenVPNClient::tun_builder_add_wins_server(const std::string& address) {
return true;
}
int OpenVPNClient::tun_builder_establish() {
return (int)[(__bridge OpenVPNAdapter *)adapter establishTunnel];
}
bool OpenVPNClient::tun_builder_persist() {
return true;
}
void OpenVPNClient::tun_builder_establish_lite() {
}
void OpenVPNClient::tun_builder_teardown(bool disconnect) {
}
bool OpenVPNClient::socket_protect(int socket) {
return true;
}
void OpenVPNClient::external_pki_cert_request(ClientAPI::ExternalPKICertRequest& certreq) { }
void OpenVPNClient::external_pki_sign_request(ClientAPI::ExternalPKISignRequest& signreq) { }
bool OpenVPNClient::pause_on_connection_timeout() {
return false;
}
void OpenVPNClient::event(const ClientAPI::Event& ev) {
[(__bridge OpenVPNAdapter *)adapter handleEvent:&ev];
}
void OpenVPNClient::log(const ClientAPI::LogInfo& log) {
[(__bridge OpenVPNAdapter *)adapter handleLog:&log];
}
std::string OpenVPNClient::get_subnet(int prefix_length) {
uint32_t bitmask = UINT_MAX << (sizeof(uint32_t) * 8 - prefix_length);
uint8_t first = (bitmask >> 24) & 0xFF;
uint8_t second = (bitmask >> 16) & 0xFF;
uint8_t third = (bitmask >> 8) & 0xFF;
uint8_t fourth = bitmask & 0xFF;
std::stringstream stream;
stream << std::to_string(first) << "." << std::to_string(second) << "." << std::to_string(third) << "." << std::to_string(fourth);
return stream.str();
}