mirror of
https://github.com/ntop/n2n.git
synced 2024-09-20 00:51:10 +02:00
Merge pull request #259 from emanuele-f/hin2n_android
Compilation fixes for hin2n
This commit is contained in:
commit
590800c996
|
@ -16,68 +16,17 @@
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include "../n2n.h"
|
#include "n2n.h"
|
||||||
|
|
||||||
#ifdef __ANDROID_NDK__
|
#ifdef __ANDROID_NDK__
|
||||||
#include "edge_android.h"
|
#include <edge_jni/edge_jni.h>
|
||||||
#include <tun2tap/tun2tap.h>
|
#include <tun2tap/tun2tap.h>
|
||||||
|
|
||||||
#define N2N_NETMASK_STR_SIZE 16 /* dotted decimal 12 numbers + 3 dots */
|
#define N2N_NETMASK_STR_SIZE 16 /* dotted decimal 12 numbers + 3 dots */
|
||||||
#define N2N_MACNAMSIZ 18 /* AA:BB:CC:DD:EE:FF + NULL*/
|
#define N2N_MACNAMSIZ 18 /* AA:BB:CC:DD:EE:FF + NULL*/
|
||||||
#define N2N_IF_MODE_SIZE 16 /* static | dhcp */
|
#define N2N_IF_MODE_SIZE 16 /* static | dhcp */
|
||||||
|
|
||||||
/* *************************************************** */
|
n2n_edge_status_t* g_status;
|
||||||
|
|
||||||
#if defined(DUMMY_ID_00001) /* Disabled waiting for config option to enable it */
|
|
||||||
|
|
||||||
static char gratuitous_arp[] = {
|
|
||||||
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, /* Dest mac */
|
|
||||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* Src mac */
|
|
||||||
0x08, 0x06, /* ARP */
|
|
||||||
0x00, 0x01, /* Ethernet */
|
|
||||||
0x08, 0x00, /* IP */
|
|
||||||
0x06, /* Hw Size */
|
|
||||||
0x04, /* Protocol Size */
|
|
||||||
0x00, 0x01, /* ARP Request */
|
|
||||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* Src mac */
|
|
||||||
0x00, 0x00, 0x00, 0x00, /* Src IP */
|
|
||||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* Target mac */
|
|
||||||
0x00, 0x00, 0x00, 0x00 /* Target IP */
|
|
||||||
};
|
|
||||||
|
|
||||||
/* ************************************** */
|
|
||||||
|
|
||||||
/** Build a gratuitous ARP packet for a /24 layer 3 (IP) network. */
|
|
||||||
static int build_gratuitous_arp(char *buffer, uint16_t buffer_len) {
|
|
||||||
if(buffer_len < sizeof(gratuitous_arp)) return(-1);
|
|
||||||
|
|
||||||
memcpy(buffer, gratuitous_arp, sizeof(gratuitous_arp));
|
|
||||||
memcpy(&buffer[6], device.mac_addr, 6);
|
|
||||||
memcpy(&buffer[22], device.mac_addr, 6);
|
|
||||||
memcpy(&buffer[28], &device.ip_addr, 4);
|
|
||||||
|
|
||||||
/* REVISIT: BbMaj7 - use a real netmask here. This is valid only by accident
|
|
||||||
* for /24 IPv4 networks. */
|
|
||||||
buffer[31] = 0xFF; /* Use a faked broadcast address */
|
|
||||||
memcpy(&buffer[38], &device.ip_addr, 4);
|
|
||||||
return(sizeof(gratuitous_arp));
|
|
||||||
}
|
|
||||||
|
|
||||||
/* ************************************** */
|
|
||||||
|
|
||||||
/** Called from update_supernode_reg to periodically send gratuitous ARP
|
|
||||||
* broadcasts. */
|
|
||||||
static void send_grat_arps(n2n_edge_t * eee,) {
|
|
||||||
char buffer[48];
|
|
||||||
size_t len;
|
|
||||||
|
|
||||||
traceEvent(TRACE_NORMAL, "Sending gratuitous ARP...");
|
|
||||||
len = build_gratuitous_arp(buffer, sizeof(buffer));
|
|
||||||
send_packet2net(eee, buffer, len);
|
|
||||||
send_packet2net(eee, buffer, len); /* Two is better than one :-) */
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif /* #if defined(DUMMY_ID_00001) */
|
|
||||||
|
|
||||||
/* ***************************************************** */
|
/* ***************************************************** */
|
||||||
|
|
||||||
|
@ -98,201 +47,169 @@ static void send_grat_arps(n2n_edge_t * eee,) {
|
||||||
* return 0 on success and -1 on error
|
* return 0 on success and -1 on error
|
||||||
*/
|
*/
|
||||||
static int scan_address(char * ip_addr, size_t addr_size,
|
static int scan_address(char * ip_addr, size_t addr_size,
|
||||||
char * ip_mode, size_t mode_size,
|
char * ip_mode, size_t mode_size,
|
||||||
const char * s) {
|
const char * s) {
|
||||||
int retval = -1;
|
int retval = -1;
|
||||||
char * p;
|
char * p;
|
||||||
|
|
||||||
if((NULL == s) || (NULL == ip_addr))
|
if((NULL == s) || (NULL == ip_addr))
|
||||||
{
|
{
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
memset(ip_addr, 0, addr_size);
|
memset(ip_addr, 0, addr_size);
|
||||||
|
|
||||||
p = strpbrk(s, ":");
|
p = strpbrk(s, ":");
|
||||||
|
|
||||||
if(p)
|
if(p)
|
||||||
{
|
{
|
||||||
/* colon is present */
|
/* colon is present */
|
||||||
if(ip_mode)
|
if(ip_mode)
|
||||||
{
|
{
|
||||||
size_t end=0;
|
size_t end=0;
|
||||||
|
|
||||||
memset(ip_mode, 0, mode_size);
|
memset(ip_mode, 0, mode_size);
|
||||||
end = MIN(p-s, (ssize_t)(mode_size-1)); /* ensure NULL term */
|
end = MIN(p-s, (ssize_t)(mode_size-1)); /* ensure NULL term */
|
||||||
strncpy(ip_mode, s, end);
|
strncpy(ip_mode, s, end);
|
||||||
strncpy(ip_addr, p+1, addr_size-1); /* ensure NULL term */
|
strncpy(ip_addr, p+1, addr_size-1); /* ensure NULL term */
|
||||||
retval = 0;
|
retval = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
/* colon is not present */
|
/* colon is not present */
|
||||||
strncpy(ip_addr, s, addr_size);
|
strncpy(ip_addr, s, addr_size);
|
||||||
}
|
}
|
||||||
|
|
||||||
return retval;
|
return retval;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* *************************************************** */
|
/* *************************************************** */
|
||||||
|
|
||||||
//TODO use new API
|
static const char *random_device_mac(void)
|
||||||
int start_edge(const n2n_edge_cmd_t* cmd)
|
{
|
||||||
|
const char key[] = "0123456789abcdef";
|
||||||
|
static char mac[18];
|
||||||
|
int i;
|
||||||
|
|
||||||
|
srand(getpid());
|
||||||
|
for (i = 0; i < sizeof(mac) - 1; ++i) {
|
||||||
|
if ((i + 1) % 3 == 0) {
|
||||||
|
mac[i] = ':';
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
mac[i] = key[random() % sizeof(key)];
|
||||||
|
}
|
||||||
|
mac[sizeof(mac) - 1] = '\0';
|
||||||
|
return mac;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* *************************************************** */
|
||||||
|
|
||||||
|
int start_edge_v2(n2n_edge_status_t* status)
|
||||||
{
|
{
|
||||||
int keep_on_running = 0;
|
int keep_on_running = 0;
|
||||||
int local_port = 0 /* any port */;
|
|
||||||
char tuntap_dev_name[N2N_IFNAMSIZ] = "tun0";
|
char tuntap_dev_name[N2N_IFNAMSIZ] = "tun0";
|
||||||
char ip_mode[N2N_IF_MODE_SIZE]="static";
|
char ip_mode[N2N_IF_MODE_SIZE]="static";
|
||||||
char ip_addr[N2N_NETMASK_STR_SIZE] = "";
|
char ip_addr[N2N_NETMASK_STR_SIZE] = "";
|
||||||
char netmask[N2N_NETMASK_STR_SIZE]="255.255.255.0";
|
char netmask[N2N_NETMASK_STR_SIZE]="255.255.255.0";
|
||||||
char device_mac[N2N_MACNAMSIZ]="";
|
char device_mac[N2N_MACNAMSIZ]="";
|
||||||
char * encrypt_key=NULL;
|
char * encrypt_key=NULL;
|
||||||
n2n_edge_t eee;
|
struct in_addr gateway_ip = {0};
|
||||||
|
n2n_edge_conf_t conf;
|
||||||
|
n2n_edge_t *eee = NULL;
|
||||||
int i;
|
int i;
|
||||||
|
tuntap_dev dev;
|
||||||
|
|
||||||
keep_on_running = 0;
|
if (!status) {
|
||||||
pthread_mutex_lock(&status.mutex);
|
|
||||||
status.is_running = keep_on_running;
|
|
||||||
pthread_mutex_unlock(&status.mutex);
|
|
||||||
report_edge_status();
|
|
||||||
if (!cmd) {
|
|
||||||
traceEvent( TRACE_ERROR, "Empty cmd struct" );
|
traceEvent( TRACE_ERROR, "Empty cmd struct" );
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
g_status = status;
|
||||||
traceLevel = cmd->trace_vlevel;
|
n2n_edge_cmd_t* cmd = &status->cmd;
|
||||||
traceLevel = traceLevel < 0 ? 0 : traceLevel; /* TRACE_ERROR */
|
|
||||||
traceLevel = traceLevel > 4 ? 4 : traceLevel; /* TRACE_DEBUG */
|
|
||||||
|
|
||||||
/* Random seed */
|
|
||||||
srand(time(NULL));
|
|
||||||
|
|
||||||
if (-1 == edge_init(&eee) )
|
|
||||||
{
|
|
||||||
traceEvent( TRACE_ERROR, "Failed in edge_init" );
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
memset(&(eee.supernode), 0, sizeof(eee.supernode));
|
|
||||||
eee.supernode.family = AF_INET;
|
|
||||||
|
|
||||||
if (cmd->vpn_fd < 0) {
|
if (cmd->vpn_fd < 0) {
|
||||||
traceEvent(TRACE_ERROR, "VPN socket is invalid.");
|
traceEvent(TRACE_ERROR, "VPN socket is invalid.");
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
eee.device.fd = cmd->vpn_fd;
|
|
||||||
if (cmd->enc_key)
|
pthread_mutex_lock(&g_status->mutex);
|
||||||
{
|
g_status->running_status = EDGE_STAT_CONNECTING;
|
||||||
encrypt_key = strdup(cmd->enc_key);
|
pthread_mutex_unlock(&g_status->mutex);
|
||||||
|
g_status->report_edge_status();
|
||||||
|
|
||||||
|
edge_init_conf_defaults(&conf);
|
||||||
|
|
||||||
|
/* Load the configuration */
|
||||||
|
strncpy((char *)conf.community_name, cmd->community, N2N_COMMUNITY_SIZE-1);
|
||||||
|
|
||||||
|
if(cmd->enc_key && cmd->enc_key[0]) {
|
||||||
|
conf.transop_id = N2N_TRANSFORM_ID_TWOFISH;
|
||||||
|
conf.encrypt_key = strdup(cmd->enc_key);
|
||||||
traceEvent(TRACE_DEBUG, "encrypt_key = '%s'\n", encrypt_key);
|
traceEvent(TRACE_DEBUG, "encrypt_key = '%s'\n", encrypt_key);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (cmd->ip_addr[0] != '\0')
|
scan_address(ip_addr, N2N_NETMASK_STR_SIZE,
|
||||||
{
|
ip_mode, N2N_IF_MODE_SIZE,
|
||||||
scan_address(ip_addr, N2N_NETMASK_STR_SIZE,
|
cmd->ip_addr);
|
||||||
ip_mode, N2N_IF_MODE_SIZE,
|
|
||||||
cmd->ip_addr);
|
dev.fd = cmd->vpn_fd;
|
||||||
}
|
|
||||||
else
|
conf.drop_multicast = cmd->drop_multicast == 0 ? 0 : 1;
|
||||||
{
|
conf.allow_routing = cmd->allow_routing == 0 ? 0 : 1;
|
||||||
traceEvent(TRACE_ERROR, "Ip address is not set.");
|
conf.dyn_ip_mode = (strcmp("dhcp", ip_mode) == 0) ? 1 : 0;
|
||||||
free(encrypt_key);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
if (cmd->community[0] != '\0')
|
|
||||||
{
|
|
||||||
strncpy((char *)eee.community_name, cmd->community, N2N_COMMUNITY_SIZE);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
traceEvent(TRACE_ERROR, "Community is not set.");
|
|
||||||
free(encrypt_key);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
eee.drop_multicast = cmd->drop_multicast == 0 ? 0 : 1;
|
|
||||||
if (cmd->mac_addr[0] != '\0')
|
|
||||||
{
|
|
||||||
strncpy(device_mac, cmd->mac_addr, N2N_MACNAMSIZ);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
strncpy(device_mac, random_device_mac(), N2N_MACNAMSIZ);
|
|
||||||
traceEvent(TRACE_DEBUG, "random device mac: %s\n", device_mac);
|
|
||||||
}
|
|
||||||
eee.allow_routing = cmd->allow_routing == 0 ? 0 : 1;
|
|
||||||
for (i = 0; i < N2N_EDGE_NUM_SUPERNODES && i < EDGE_CMD_SUPERNODES_NUM; ++i)
|
for (i = 0; i < N2N_EDGE_NUM_SUPERNODES && i < EDGE_CMD_SUPERNODES_NUM; ++i)
|
||||||
{
|
{
|
||||||
if (cmd->supernodes[i][0] != '\0')
|
if (cmd->supernodes[i][0] != '\0')
|
||||||
{
|
{
|
||||||
strncpy(eee.sn_ip_array[eee.sn_num], cmd->supernodes[i], N2N_EDGE_SN_HOST_SIZE);
|
strncpy(conf.sn_ip_array[conf.sn_num], cmd->supernodes[i], N2N_EDGE_SN_HOST_SIZE);
|
||||||
traceEvent(TRACE_DEBUG, "Adding supernode[%u] = %s\n", (unsigned int)eee.sn_num, (eee.sn_ip_array[eee.sn_num]));
|
traceEvent(TRACE_DEBUG, "Adding supernode[%u] = %s\n", (unsigned int)conf.sn_num, (conf.sn_ip_array[conf.sn_num]));
|
||||||
++eee.sn_num;
|
++conf.sn_num;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
eee.re_resolve_supernode_ip = cmd->re_resolve_supernode_ip == 0 ? 0 : 1;
|
|
||||||
if (cmd->ip_netmask[0] != '\0')
|
if (cmd->ip_netmask[0] != '\0')
|
||||||
{
|
|
||||||
strncpy(netmask, cmd->ip_netmask, N2N_NETMASK_STR_SIZE);
|
strncpy(netmask, cmd->ip_netmask, N2N_NETMASK_STR_SIZE);
|
||||||
|
|
||||||
|
if (cmd->gateway_ip[0] != '\0')
|
||||||
|
inet_aton(cmd->gateway_ip, &gateway_ip);
|
||||||
|
|
||||||
|
if (cmd->mac_addr[0] != '\0')
|
||||||
|
strncpy(device_mac, cmd->mac_addr, N2N_MACNAMSIZ);
|
||||||
|
else {
|
||||||
|
strncpy(device_mac, random_device_mac(), N2N_MACNAMSIZ);
|
||||||
|
traceEvent(TRACE_DEBUG, "random device mac: %s\n", device_mac);
|
||||||
}
|
}
|
||||||
|
|
||||||
for (i=0; i< N2N_EDGE_NUM_SUPERNODES; ++i )
|
if(edge_verify_conf(&conf) != 0) {
|
||||||
{
|
if(conf.encrypt_key) free(conf.encrypt_key);
|
||||||
traceEvent(TRACE_NORMAL, "supernode %u => %s\n", i, (eee.sn_ip_array[i]));
|
traceEvent(TRACE_ERROR, "Bad configuration");
|
||||||
|
return 1;
|
||||||
}
|
}
|
||||||
supernode2addr(&(eee.supernode), eee.sn_ip_array[eee.sn_idx]);
|
|
||||||
if (encrypt_key == NULL)
|
/* Open the TAP device */
|
||||||
{
|
if(tuntap_open(&dev, tuntap_dev_name, ip_mode, ip_addr, netmask, device_mac, cmd->mtu) < 0) {
|
||||||
traceEvent(TRACE_WARNING, "Encryption is disabled in edge.");
|
|
||||||
eee.null_transop = 1;
|
|
||||||
}
|
|
||||||
if (0 == strcmp("dhcp", ip_mode))
|
|
||||||
{
|
|
||||||
traceEvent(TRACE_NORMAL, "Dynamic IP address assignment enabled.");
|
|
||||||
eee.dyn_ip_mode = 1;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
traceEvent(TRACE_NORMAL, "ip_mode='%s'", ip_mode);
|
|
||||||
}
|
|
||||||
if(tuntap_open(&(eee.device), tuntap_dev_name, ip_mode, ip_addr, netmask, device_mac, cmd->mtu) < 0)
|
|
||||||
{
|
|
||||||
traceEvent(TRACE_ERROR, "Failed in tuntap_open");
|
traceEvent(TRACE_ERROR, "Failed in tuntap_open");
|
||||||
free(encrypt_key);
|
free(encrypt_key);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
if(local_port > 0)
|
|
||||||
{
|
/* Start n2n */
|
||||||
traceEvent(TRACE_NORMAL, "Binding to local port %d", (signed int)local_port);
|
eee = edge_init(&dev, &conf, &i);
|
||||||
}
|
|
||||||
if (encrypt_key)
|
if(eee == NULL) {
|
||||||
{
|
traceEvent( TRACE_ERROR, "Failed in edge_init" );
|
||||||
if(edge_init_twofish(&eee, (uint8_t *)(encrypt_key), strlen(encrypt_key)) < 0)
|
|
||||||
{
|
|
||||||
traceEvent(TRACE_ERROR, "twofish setup failed.\n");
|
|
||||||
free(encrypt_key);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
free(encrypt_key);
|
|
||||||
encrypt_key = NULL;
|
|
||||||
}
|
|
||||||
/* else run in NULL mode */
|
|
||||||
eee.udp_sock = open_socket(local_port, 1 /*bind ANY*/ );
|
|
||||||
if(eee.udp_sock < 0)
|
|
||||||
{
|
|
||||||
traceEvent(TRACE_ERROR, "Failed to bind main UDP port %u", (signed int)local_port);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
eee.udp_mgmt_sock = open_socket(N2N_EDGE_MGMT_PORT, 0 /* bind LOOPBACK*/ );
|
|
||||||
if(eee.udp_mgmt_sock < 0)
|
|
||||||
{
|
|
||||||
traceEvent( TRACE_ERROR, "Failed to bind management UDP port %u", (unsigned int)N2N_EDGE_MGMT_PORT );
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Set runtime information */
|
||||||
|
eee->gateway_ip = gateway_ip.s_addr;
|
||||||
|
|
||||||
/* set host addr, netmask, mac addr for UIP and init arp*/
|
/* set host addr, netmask, mac addr for UIP and init arp*/
|
||||||
{
|
{
|
||||||
int match, i;
|
int match, i;
|
||||||
u8_t ip[4];
|
int ip[4];
|
||||||
uip_ipaddr_t ipaddr;
|
uip_ipaddr_t ipaddr;
|
||||||
struct uip_eth_addr eaddr;
|
struct uip_eth_addr eaddr;
|
||||||
|
|
||||||
|
@ -311,7 +228,7 @@ int start_edge(const n2n_edge_cmd_t* cmd)
|
||||||
uip_ipaddr(ipaddr, ip[0], ip[1], ip[2], ip[3]);
|
uip_ipaddr(ipaddr, ip[0], ip[1], ip[2], ip[3]);
|
||||||
uip_setnetmask(ipaddr);
|
uip_setnetmask(ipaddr);
|
||||||
for (i = 0; i < 6; ++i) {
|
for (i = 0; i < 6; ++i) {
|
||||||
eaddr.addr[i] = eee.device.mac_addr[i];
|
eaddr.addr[i] = eee->device.mac_addr[i];
|
||||||
}
|
}
|
||||||
uip_setethaddr(eaddr);
|
uip_setethaddr(eaddr);
|
||||||
|
|
||||||
|
@ -319,16 +236,27 @@ int start_edge(const n2n_edge_cmd_t* cmd)
|
||||||
}
|
}
|
||||||
|
|
||||||
keep_on_running = 1;
|
keep_on_running = 1;
|
||||||
pthread_mutex_lock(&status.mutex);
|
pthread_mutex_lock(&g_status->mutex);
|
||||||
status.is_running = keep_on_running;
|
g_status->running_status = EDGE_STAT_CONNECTED;
|
||||||
pthread_mutex_unlock(&status.mutex);
|
pthread_mutex_unlock(&g_status->mutex);
|
||||||
report_edge_status();
|
g_status->report_edge_status();
|
||||||
traceEvent(TRACE_NORMAL, "edge started");
|
traceEvent(TRACE_NORMAL, "edge started");
|
||||||
|
|
||||||
return run_edge_loop(&eee, &keep_on_running);
|
update_supernode_reg(eee, time(NULL));
|
||||||
|
|
||||||
|
run_edge_loop(eee, &keep_on_running);
|
||||||
|
|
||||||
|
/* Cleanup */
|
||||||
|
edge_term(eee);
|
||||||
|
tuntap_close(&dev);
|
||||||
|
edge_term_conf(&conf);
|
||||||
|
|
||||||
|
traceEvent(TRACE_NORMAL, "Edge stopped");
|
||||||
|
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int stop_edge(void)
|
int stop_edge_v2(void)
|
||||||
{
|
{
|
||||||
// quick stop
|
// quick stop
|
||||||
int fd = open_socket(0, 0 /* bind LOOPBACK*/ );
|
int fd = open_socket(0, 0 /* bind LOOPBACK*/ );
|
||||||
|
@ -341,12 +269,12 @@ int stop_edge(void)
|
||||||
peer_addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
|
peer_addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
|
||||||
peer_addr.sin_port = htons(N2N_EDGE_MGMT_PORT);
|
peer_addr.sin_port = htons(N2N_EDGE_MGMT_PORT);
|
||||||
sendto(fd, "stop", 4, 0, (struct sockaddr *)&peer_addr, sizeof(struct sockaddr_in));
|
sendto(fd, "stop", 4, 0, (struct sockaddr *)&peer_addr, sizeof(struct sockaddr_in));
|
||||||
close(fd);
|
close(fd);
|
||||||
|
|
||||||
pthread_mutex_lock(&status.mutex);
|
pthread_mutex_lock(&g_status->mutex);
|
||||||
status.is_running = 0;
|
g_status->running_status = EDGE_STAT_DISCONNECT;
|
||||||
pthread_mutex_unlock(&status.mutex);
|
pthread_mutex_unlock(&g_status->mutex);
|
||||||
report_edge_status();
|
g_status->report_edge_status();
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,64 +0,0 @@
|
||||||
//
|
|
||||||
// Created by switchwang(https://github.com/switch-st) on 2018-04-13.
|
|
||||||
//
|
|
||||||
|
|
||||||
#ifndef _EDGE_ANDROID_H_
|
|
||||||
#define _EDGE_ANDROID_H_
|
|
||||||
|
|
||||||
#ifdef __ANDROID_NDK__
|
|
||||||
|
|
||||||
#include "../n2n.h"
|
|
||||||
#include <pthread.h>
|
|
||||||
|
|
||||||
#define EDGE_CMD_IPSTR_SIZE 16
|
|
||||||
#define EDGE_CMD_SUPERNODES_NUM 2
|
|
||||||
#define EDGE_CMD_SN_HOST_SIZE 48
|
|
||||||
#define EDGE_CMD_MACNAMSIZ 18
|
|
||||||
|
|
||||||
|
|
||||||
typedef struct n2n_edge_cmd_st
|
|
||||||
{
|
|
||||||
char ip_addr[EDGE_CMD_IPSTR_SIZE];
|
|
||||||
char ip_netmask[EDGE_CMD_IPSTR_SIZE];
|
|
||||||
char supernodes[EDGE_CMD_SUPERNODES_NUM][EDGE_CMD_SN_HOST_SIZE];
|
|
||||||
char community[N2N_COMMUNITY_SIZE];
|
|
||||||
char* enc_key;
|
|
||||||
char* enc_key_file;
|
|
||||||
char mac_addr[EDGE_CMD_MACNAMSIZ];
|
|
||||||
unsigned int mtu;
|
|
||||||
int re_resolve_supernode_ip;
|
|
||||||
unsigned int local_port;
|
|
||||||
int allow_routing;
|
|
||||||
int drop_multicast;
|
|
||||||
int trace_vlevel;
|
|
||||||
int vpn_fd;
|
|
||||||
} n2n_edge_cmd_t;
|
|
||||||
|
|
||||||
typedef struct n2n_edge_status_st
|
|
||||||
{
|
|
||||||
pthread_mutex_t mutex;
|
|
||||||
uint8_t is_running;
|
|
||||||
} n2n_edge_status;
|
|
||||||
|
|
||||||
#define INIT_EDGE_CMD(cmd) do {\
|
|
||||||
memset(&(cmd), 0, sizeof((cmd))); \
|
|
||||||
(cmd).enc_key = NULL; \
|
|
||||||
(cmd).enc_key_file = NULL; \
|
|
||||||
(cmd).mtu = 1400; \
|
|
||||||
(cmd).re_resolve_supernode_ip = 0;\
|
|
||||||
(cmd).local_port = 0; \
|
|
||||||
(cmd).allow_routing = 0; \
|
|
||||||
(cmd).drop_multicast = 1; \
|
|
||||||
(cmd).trace_vlevel = 2; \
|
|
||||||
(cmd).vpn_fd = -1; \
|
|
||||||
} while (0);
|
|
||||||
|
|
||||||
n2n_edge_status status;
|
|
||||||
|
|
||||||
int start_edge(const n2n_edge_cmd_t* cmd);
|
|
||||||
int stop_edge(void);
|
|
||||||
void report_edge_status(void);
|
|
||||||
|
|
||||||
#endif /* __ANDROID_NDK__ */
|
|
||||||
|
|
||||||
#endif //_EDGE_ANDROID_H_
|
|
|
@ -15,7 +15,7 @@
|
||||||
* along with this program; if not, see <http://www.gnu.org/licenses/>
|
* along with this program; if not, see <http://www.gnu.org/licenses/>
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include "../n2n.h"
|
#include "n2n.h"
|
||||||
|
|
||||||
#ifdef __ANDROID_NDK__
|
#ifdef __ANDROID_NDK__
|
||||||
#include <tun2tap/tun2tap.h>
|
#include <tun2tap/tun2tap.h>
|
||||||
|
@ -57,7 +57,7 @@ int tuntap_open(tuntap_dev *device,
|
||||||
device->ip_addr = inet_addr(device_ip);
|
device->ip_addr = inet_addr(device_ip);
|
||||||
device->device_mask = inet_addr(device_mask);
|
device->device_mask = inet_addr(device_mask);
|
||||||
device->mtu = mtu;
|
device->mtu = mtu;
|
||||||
strncpy(device->dev_name, dev, MIN(IFNAMSIZ, N2N_IFNAMSIZ));
|
strncpy(device->dev_name, dev, N2N_IFNAMSIZ);
|
||||||
return device->fd;
|
return device->fd;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -118,8 +118,8 @@ typedef struct ether_hdr ether_hdr_t;
|
||||||
#undef N2N_HAVE_DAEMON
|
#undef N2N_HAVE_DAEMON
|
||||||
#undef N2N_HAVE_SETUID
|
#undef N2N_HAVE_SETUID
|
||||||
#undef N2N_CAN_NAME_IFACE
|
#undef N2N_CAN_NAME_IFACE
|
||||||
#include "android/edge_android.h"
|
|
||||||
#include <tun2tap/tun2tap.h>
|
#include <tun2tap/tun2tap.h>
|
||||||
|
#include <edge_jni/edge_jni.h>
|
||||||
#define ARP_PERIOD_INTERVAL (10) /* sec */
|
#define ARP_PERIOD_INTERVAL (10) /* sec */
|
||||||
#endif /* #ifdef __ANDROID_NDK__ */
|
#endif /* #ifdef __ANDROID_NDK__ */
|
||||||
|
|
||||||
|
@ -242,13 +242,13 @@ typedef struct n2n_edge n2n_edge_t; /* Opaque, see edge_utils.c */
|
||||||
|
|
||||||
typedef struct sn_stats
|
typedef struct sn_stats
|
||||||
{
|
{
|
||||||
size_t errors; /* Number of errors encountered. */
|
size_t errors; /* Number of errors encountered. */
|
||||||
size_t reg_super; /* Number of REGISTER_SUPER requests received. */
|
size_t reg_super; /* Number of REGISTER_SUPER requests received. */
|
||||||
size_t reg_super_nak; /* Number of REGISTER_SUPER requests declined. */
|
size_t reg_super_nak; /* Number of REGISTER_SUPER requests declined. */
|
||||||
size_t fwd; /* Number of messages forwarded. */
|
size_t fwd; /* Number of messages forwarded. */
|
||||||
size_t broadcast; /* Number of messages broadcast to a community. */
|
size_t broadcast; /* Number of messages broadcast to a community. */
|
||||||
time_t last_fwd; /* Time when last message was forwarded. */
|
time_t last_fwd; /* Time when last message was forwarded. */
|
||||||
time_t last_reg_super; /* Time when last REGISTER_SUPER was received. */
|
time_t last_reg_super; /* Time when last REGISTER_SUPER was received. */
|
||||||
} sn_stats_t;
|
} sn_stats_t;
|
||||||
|
|
||||||
struct sn_community
|
struct sn_community
|
||||||
|
@ -263,14 +263,14 @@ struct sn_community
|
||||||
|
|
||||||
typedef struct n2n_sn
|
typedef struct n2n_sn
|
||||||
{
|
{
|
||||||
time_t start_time; /* Used to measure uptime. */
|
time_t start_time; /* Used to measure uptime. */
|
||||||
sn_stats_t stats;
|
sn_stats_t stats;
|
||||||
int daemon; /* If non-zero then daemonise. */
|
int daemon; /* If non-zero then daemonise. */
|
||||||
uint16_t lport; /* Local UDP port to bind to. */
|
uint16_t lport; /* Local UDP port to bind to. */
|
||||||
int sock; /* Main socket for UDP traffic with edges. */
|
int sock; /* Main socket for UDP traffic with edges. */
|
||||||
int mgmt_sock; /* management socket. */
|
int mgmt_sock; /* management socket. */
|
||||||
int lock_communities; /* If true, only loaded communities can be used. */
|
int lock_communities; /* If true, only loaded communities can be used. */
|
||||||
struct sn_community *communities;
|
struct sn_community *communities;
|
||||||
} n2n_sn_t;
|
} n2n_sn_t;
|
||||||
|
|
||||||
/* ************************************** */
|
/* ************************************** */
|
||||||
|
@ -311,7 +311,7 @@ void traceEvent(int eventTraceLevel, char* file, int line, char * format, ...);
|
||||||
|
|
||||||
/* Tuntap API */
|
/* Tuntap API */
|
||||||
int tuntap_open(tuntap_dev *device, char *dev, const char *address_mode, char *device_ip,
|
int tuntap_open(tuntap_dev *device, char *dev, const char *address_mode, char *device_ip,
|
||||||
char *device_mask, const char * device_mac, int mtu);
|
char *device_mask, const char * device_mac, int mtu);
|
||||||
int tuntap_read(struct tuntap_dev *tuntap, unsigned char *buf, int len);
|
int tuntap_read(struct tuntap_dev *tuntap, unsigned char *buf, int len);
|
||||||
int tuntap_write(struct tuntap_dev *tuntap, unsigned char *buf, int len);
|
int tuntap_write(struct tuntap_dev *tuntap, unsigned char *buf, int len);
|
||||||
void tuntap_close(struct tuntap_dev *tuntap);
|
void tuntap_close(struct tuntap_dev *tuntap);
|
||||||
|
@ -330,10 +330,10 @@ void print_edge_stats(const n2n_edge_t *eee);
|
||||||
|
|
||||||
/* Sockets */
|
/* Sockets */
|
||||||
char* sock_to_cstr( n2n_sock_str_t out,
|
char* sock_to_cstr( n2n_sock_str_t out,
|
||||||
const n2n_sock_t * sock );
|
const n2n_sock_t * sock );
|
||||||
SOCKET open_socket(int local_port, int bind_any);
|
SOCKET open_socket(int local_port, int bind_any);
|
||||||
int sock_equal( const n2n_sock_t * a,
|
int sock_equal( const n2n_sock_t * a,
|
||||||
const n2n_sock_t * b );
|
const n2n_sock_t * b );
|
||||||
|
|
||||||
/* Operations on peer_info lists. */
|
/* Operations on peer_info lists. */
|
||||||
size_t purge_peer_list( struct peer_info ** peer_list,
|
size_t purge_peer_list( struct peer_info ** peer_list,
|
||||||
|
|
|
@ -31,7 +31,7 @@
|
||||||
# include <endian.h>
|
# include <endian.h>
|
||||||
# include <features.h>
|
# include <features.h>
|
||||||
/* See http://linux.die.net/man/3/endian */
|
/* See http://linux.die.net/man/3/endian */
|
||||||
# if defined(htobe16) && defined(htole16) && defined(be16toh) && defined(le16toh) && defined(htobe32) && defined(htole32) && defined(be32toh) && defined(htole32) && defined(htobe64) && defined(htole64) && defined(be64) && defined(le64)
|
# if defined(htobe16) && defined(htole16) && defined(be16toh) && defined(le16toh) && defined(htobe32) && defined(htole32) && defined(be32toh) && defined(htole32) && defined(htobe64) && defined(htole64) && defined(htobe64) && defined(be64toh) && defined(htole64) && defined(le64toh)
|
||||||
/* Do nothing. The macros we need already exist. */
|
/* Do nothing. The macros we need already exist. */
|
||||||
# elif !defined(__GLIBC__) || !defined(__GLIBC_MINOR__) || ((__GLIBC__ < 2) || ((__GLIBC__ == 2) && (__GLIBC_MINOR__ < 9)))
|
# elif !defined(__GLIBC__) || !defined(__GLIBC_MINOR__) || ((__GLIBC__ < 2) || ((__GLIBC__ == 2) && (__GLIBC_MINOR__ < 9)))
|
||||||
# include <arpa/inet.h>
|
# include <arpa/inet.h>
|
||||||
|
|
903
src/edge_utils.c
903
src/edge_utils.c
File diff suppressed because it is too large
Load Diff
50
src/n2n.c
50
src/n2n.c
|
@ -22,6 +22,10 @@
|
||||||
|
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
|
|
||||||
|
#ifdef __ANDROID_NDK__
|
||||||
|
#include <edge_jni/edge_jni.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
#define PURGE_REGISTRATION_FREQUENCY 30
|
#define PURGE_REGISTRATION_FREQUENCY 30
|
||||||
#define REGISTRATION_TIMEOUT 60
|
#define REGISTRATION_TIMEOUT 60
|
||||||
|
|
||||||
|
@ -31,6 +35,47 @@ static const uint8_t ipv6_multicast_addr[6] = { 0x33, 0x33, 0x00, 0x00, 0x00, 0x
|
||||||
|
|
||||||
/* ************************************** */
|
/* ************************************** */
|
||||||
|
|
||||||
|
#ifdef __ANDROID_NDK__
|
||||||
|
|
||||||
|
static int protect_socket(int sock) {
|
||||||
|
JNIEnv *env = NULL;
|
||||||
|
|
||||||
|
if(!g_status)
|
||||||
|
return(-1);
|
||||||
|
|
||||||
|
if ((*g_status->jvm)->GetEnv(g_status->jvm, &env, JNI_VERSION_1_1) != JNI_OK || !env) {
|
||||||
|
traceEvent(TRACE_ERROR, "GetEnv failed");
|
||||||
|
return(-1);
|
||||||
|
}
|
||||||
|
|
||||||
|
jclass vpn_service_cls = (*env)->GetObjectClass(env, g_status->jobj_service);
|
||||||
|
|
||||||
|
if(!vpn_service_cls) {
|
||||||
|
traceEvent(TRACE_ERROR, "GetObjectClass(VpnService) failed");
|
||||||
|
return(-1);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Call VpnService protect */
|
||||||
|
jmethodID midProtect = (*env)->GetMethodID(env, vpn_service_cls, "protect", "(I)Z");
|
||||||
|
if(!midProtect) {
|
||||||
|
traceEvent(TRACE_ERROR, "Could not resolve VpnService::protect");
|
||||||
|
return(-1);
|
||||||
|
}
|
||||||
|
|
||||||
|
jboolean isProtected = (*env)->CallBooleanMethod(env, g_status->jobj_service, midProtect, sock);
|
||||||
|
|
||||||
|
if(!isProtected) {
|
||||||
|
traceEvent(TRACE_ERROR, "VpnService::protect failed");
|
||||||
|
return(-1);
|
||||||
|
}
|
||||||
|
|
||||||
|
return(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/* ************************************** */
|
||||||
|
|
||||||
SOCKET open_socket(int local_port, int bind_any) {
|
SOCKET open_socket(int local_port, int bind_any) {
|
||||||
SOCKET sock_fd;
|
SOCKET sock_fd;
|
||||||
struct sockaddr_in local_address;
|
struct sockaddr_in local_address;
|
||||||
|
@ -59,6 +104,11 @@ SOCKET open_socket(int local_port, int bind_any) {
|
||||||
return(-1);
|
return(-1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#ifdef __ANDROID_NDK__
|
||||||
|
/* Protect the socket so that the supernode traffic won't go inside the n2n VPN */
|
||||||
|
protect_socket(sock_fd);
|
||||||
|
#endif
|
||||||
|
|
||||||
return(sock_fd);
|
return(sock_fd);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user