Browse Source

modified route setup (#717)

pull/722/head
Logan oos Even 3 years ago
committed by GitHub
parent
commit
b648872c1f
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 8
      src/edge.c
  2. 25
      src/edge_utils.c
  3. 3
      win32/n2n_win32.h
  4. 24
      win32/wintap.c

8
src/edge.c

@ -52,6 +52,7 @@ int fetch_and_eventually_process_data (n2n_edge_t *eee, SOCKET sock,
time_t now);
int resolve_create_thread (n2n_resolve_parameter_t **param, struct peer_info *sn_list);
int resolve_check (n2n_resolve_parameter_t *param, uint8_t resolution_request, time_t now);
int edge_init_routes (n2n_edge_t *eee, n2n_route_t *routes, uint16_t num_routes);
/* ***************************************************** */
@ -1137,7 +1138,7 @@ int main (int argc, char* argv[]) {
}
}
if(runlevel == 4) { /* configure the TUNTAP device */
if(runlevel == 4) { /* configure the TUNTAP device, including routes */
if(tuntap_open(&tuntap, eee->tuntap_priv_conf.tuntap_dev_name, eee->tuntap_priv_conf.ip_mode,
eee->tuntap_priv_conf.ip_addr, eee->tuntap_priv_conf.netmask,
eee->tuntap_priv_conf.device_mac, eee->tuntap_priv_conf.mtu
@ -1151,6 +1152,11 @@ int main (int argc, char* argv[]) {
eee->tuntap_priv_conf.ip_addr,
eee->tuntap_priv_conf.netmask,
macaddr_str(mac_buf, eee->device.mac_addr));
// routes
if(edge_init_routes(eee, eee->conf.routes, eee->conf.num_routes) < 0) {
traceEvent(TRACE_ERROR, "routes setup failed");
exit(1);
}
runlevel = 5;
// no more answers required
seek_answer = 0;

25
src/edge_utils.c

@ -41,7 +41,7 @@ static void check_peer_registration_needed (n2n_edge_t *eee,
const n2n_sock_t *peer);
static int edge_init_sockets (n2n_edge_t *eee);
static int edge_init_routes (n2n_edge_t *eee, n2n_route_t *routes, uint16_t num_routes);
int edge_init_routes (n2n_edge_t *eee, n2n_route_t *routes, uint16_t num_routes);
static void edge_cleanup_routes (n2n_edge_t *eee);
static void check_known_peer_sock_change (n2n_edge_t *eee,
@ -417,11 +417,6 @@ n2n_edge_t* edge_init (const n2n_edge_conf_t *conf, int *rv) {
goto edge_init_error;
}
if(edge_init_routes(eee, eee->conf.routes, eee->conf.num_routes) < 0) {
traceEvent(TRACE_ERROR, "routes setup failed");
goto edge_init_error;
}
eee->network_traffic_filter = create_network_traffic_filter();
network_traffic_filter_add_rule(eee->network_traffic_filter, eee->conf.network_traffic_filter_rules);
@ -3385,12 +3380,14 @@ static int edge_init_routes_linux (n2n_edge_t *eee, n2n_route_t *routes, uint16_
/* ************************************** */
static int edge_init_routes_win (n2n_edge_t *eee, n2n_route_t *routes, uint16_t num_routes) {
static int edge_init_routes_win (n2n_edge_t *eee, n2n_route_t *routes, uint16_t num_routes, uint8_t verb /* 0 = add, 1 = delete */) {
#ifdef WIN32
int i;
struct in_addr net_addr, gateway;
char c_net_addr[32];
char c_gateway[32];
char c_interface[32];
char c_verb[32];
char cmd[256];
for(i = 0; i < num_routes; i++) {
@ -3404,12 +3401,13 @@ static int edge_init_routes_win (n2n_edge_t *eee, n2n_route_t *routes, uint16_t
memcpy(&gateway, &(route->gateway), sizeof(gateway));
_snprintf(c_net_addr, sizeof(c_net_addr), inet_ntoa(net_addr));
_snprintf(c_gateway, sizeof(c_gateway), inet_ntoa(gateway));
_snprintf(cmd, sizeof(cmd), "route add %s/%d %s > nul", c_net_addr, route->net_bitlen, c_gateway);
_snprintf(c_interface, sizeof(c_interface), "if %u", eee->device.if_idx);
_snprintf(c_verb, sizeof(c_verb), verb ? "delete" : "add");
_snprintf(cmd, sizeof(cmd), "route %s %s/%d %s %s > nul", c_verb, c_net_addr, route->net_bitlen, c_gateway, c_interface);
traceEvent(TRACE_NORMAL, "ROUTE CMD = '%s'\n", cmd);
system(cmd);
}
}
#endif // WIN32
return (0);
@ -3420,13 +3418,13 @@ static int edge_init_routes_win (n2n_edge_t *eee, n2n_route_t *routes, uint16_t
/* Add the user-provided routes to the linux routing table. Network routes
* are bound to the n2n TAP device, so they are automatically removed when
* the TAP device is destroyed. */
static int edge_init_routes (n2n_edge_t *eee, n2n_route_t *routes, uint16_t num_routes) {
int edge_init_routes (n2n_edge_t *eee, n2n_route_t *routes, uint16_t num_routes) {
#ifdef __linux__
return edge_init_routes_linux(eee, routes, num_routes);
#endif
#ifdef WIN32
return edge_init_routes_win(eee, routes, num_routes);
return edge_init_routes_win(eee, routes, num_routes, 0 /* add */);
#endif
return 0;
}
@ -3441,6 +3439,11 @@ static void edge_cleanup_routes (n2n_edge_t *eee) {
free(eee->sn_route_to_clean);
}
#endif
#ifdef WIN32
edge_init_routes_win(eee, eee->conf.routes, eee->conf.num_routes, 1 /* del */);
#endif
}
/* ************************************** */

3
win32/n2n_win32.h

@ -24,7 +24,7 @@
#include <winsock2.h>
#include <windows.h>
#include <winioctl.h>
#include <iptypes.h>
#include "wintap.h"
@ -70,6 +70,7 @@ typedef struct tuntap_dev {
HANDLE device_handle;
char *device_name;
char *ifName;
int if_idx;
OVERLAPPED overlap_read, overlap_write;
n2n_mac_t mac_addr;
uint32_t ip_addr;

24
win32/wintap.c

@ -253,6 +253,30 @@ int open_wintap(struct tuntap_dev *device,
/* ************************************** */
/* interface index, required for routing */
ULONG buffer_len = 0;
IP_ADAPTER_INFO *buffer;
// get required buffer size and allocate buffer
GetAdaptersInfo(NULL, &buffer_len);
buffer = malloc(buffer_len);
// find device by name and get its index
if(buffer && !GetAdaptersInfo(buffer, &buffer_len)) {
IP_ADAPTER_INFO *i;
for(i = buffer; i != NULL; i = i->Next) {
if(!strcmp(device->device_name, i->AdapterName)) {
device->if_idx = i->Index;
break;
}
}
}
free(buffer);
/* ************************************** */
if(device_mac[0])
set_interface_mac(device, device_mac);

Loading…
Cancel
Save