diff --git a/.gitignore b/.gitignore index 3e7013a..950da59 100644 --- a/.gitignore +++ b/.gitignore @@ -14,6 +14,8 @@ supernode tools/n2n-benchmark tools/n2n-decode build +.idea +cmake-build-default packages/debian/debian/changelog packages/debian/debian/control packages/debian/debian/files diff --git a/CMakeLists.txt b/CMakeLists.txt index f6e72eb..8e4ab89 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -73,6 +73,7 @@ set(CMAKE_C_FLAGS_RELEASE "-O2 -DNDEBUG") set(CMAKE_CXX_FLAGS_RELEASE "-O2 -DNDEBUG") endif(DEFINED UNIX) +# Static target. #SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -static") @@ -80,7 +81,9 @@ INCLUDE_DIRECTORIES(.) INCLUDE_DIRECTORIES(include) if(DEFINED WIN32) INCLUDE_DIRECTORIES(win32) +# Customize include. # INCLUDE_DIRECTORIES("D:/Program Files/MinGW/opt/include/" "D:/Program Files/MinGW/x86_64-w64-mingw32/include/") +# Customize library. # LINK_DIRECTORIES("D:/Program Files/MinGW/opt/lib/" "D:/Program Files/MinGW/x86_64-w64-mingw32/lib/") endif(DEFINED WIN32) @@ -155,7 +158,7 @@ install(TARGETS edge supernode # Tools add_executable(n2n-benchmark tools/benchmark.c) -target_link_libraries(n2n-benchmark n2n ${OPENSSL_LIBRARIES}) +target_link_libraries(n2n-benchmark n2n) find_library(PCAP_LIB pcap) if(PCAP_LIB) diff --git a/include/edge_utils_win32.h b/include/edge_utils_win32.h index 57d7e09..569dc29 100644 --- a/include/edge_utils_win32.h +++ b/include/edge_utils_win32.h @@ -16,6 +16,9 @@ * */ +#ifndef _EDGE_UTILS_WIN32_H_ +#define _EDGE_UTILS_WIN32_H_ + #ifdef WIN32 #include @@ -33,3 +36,5 @@ extern HANDLE startTunReadThread(struct tunread_arg *arg); #endif +#endif /* _EDGE_UTILS_WIN32_H_ */ + diff --git a/include/n2n.h b/include/n2n.h index 9b4b5c2..88af2a5 100644 --- a/include/n2n.h +++ b/include/n2n.h @@ -65,7 +65,7 @@ #include #include #include -#include +#include #ifndef WIN32 #include @@ -215,7 +215,7 @@ typedef struct n2n_route { in_addr_t gateway; } n2n_route_t; -typedef struct n2n_edge n2n_edge_t; /* Opaque, see edge_utils.c */ +typedef struct n2n_edge n2n_edge_t; /* *************************************************** */ @@ -244,6 +244,23 @@ typedef struct n2n_edge_callbacks { void (*ip_address_changed)(n2n_edge_t *eee, uint32_t old_ip, uint32_t new_ip); } n2n_edge_callbacks_t; +/* ***************************************************** */ + +typedef struct n2n_priv_config { + char tuntap_dev_name[N2N_IFNAMSIZ]; + char ip_mode[N2N_IF_MODE_SIZE]; + char ip_addr[N2N_NETMASK_STR_SIZE]; + char netmask[N2N_NETMASK_STR_SIZE]; + char device_mac[N2N_MACNAMSIZ]; + int mtu; + uint8_t got_s; + uint8_t daemon; +#ifndef WIN32 + uid_t userid; + gid_t groupid; +#endif +} n2n_priv_config_t; + /* *************************************************** */ typedef struct n2n_edge_conf { @@ -270,6 +287,55 @@ typedef struct n2n_edge_conf { int mgmt_port; } n2n_edge_conf_t; +struct n2n_edge_stats { + uint32_t tx_p2p; + uint32_t rx_p2p; + uint32_t tx_sup; + uint32_t rx_sup; + uint32_t tx_sup_broadcast; + uint32_t rx_sup_broadcast; +}; + +struct n2n_edge { + n2n_priv_config_t priv_conf; + n2n_edge_conf_t conf; + + /* Status */ + uint8_t sn_idx; /**< Currently active supernode. */ + uint8_t sn_wait; /**< Whether we are waiting for a supernode response. */ + size_t sup_attempts; /**< Number of remaining attempts to this supernode. */ + tuntap_dev device; /**< All about the TUNTAP device */ + n2n_trans_op_t transop; /**< The transop to use when encoding */ + n2n_cookie_t last_cookie; /**< Cookie sent in last REGISTER_SUPER. */ + n2n_route_t *sn_route_to_clean; /**< Supernode route to clean */ + n2n_edge_callbacks_t cb; /**< API callbacks */ + void *user_data; /**< Can hold user data */ + + /* Sockets */ + n2n_sock_t supernode; + int udp_sock; + int udp_mgmt_sock; /**< socket for status info. */ + +#ifndef SKIP_MULTICAST_PEERS_DISCOVERY + n2n_sock_t multicast_peer; /**< Multicast peer group (for local edges) */ + int udp_multicast_sock; /**< socket for local multicast registrations. */ + int multicast_joined; /**< 1 if the group has been joined.*/ +#endif + + /* Peers */ + struct peer_info * known_peers; /**< Edges we are connected to. */ + struct peer_info * pending_peers; /**< Edges we have tried to register with. */ + + /* Timers */ + time_t last_register_req; /**< Check if time to re-register with super*/ + time_t last_p2p; /**< Last time p2p traffic was received. */ + time_t last_sup; /**< Last time a packet arrived from supernode. */ + time_t start_time; /**< For calculating uptime */ + + /* Statistics */ + struct n2n_edge_stats stats; +}; + typedef struct sn_stats { size_t errors; /* Number of errors encountered. */ diff --git a/include/n2n_define.h b/include/n2n_define.h index 55c67bb..97922f2 100644 --- a/include/n2n_define.h +++ b/include/n2n_define.h @@ -75,6 +75,11 @@ #define N2N_PATHNAME_MAXLEN 256 #define N2N_EDGE_MGMT_PORT 5644 +#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_IF_MODE_SIZE 16 /* static | dhcp */ + + /* ************************************** */ #define SUPERNODE_IP "127.0.0.1" diff --git a/src/edge.c b/src/edge.c index 63c8314..d74dc53 100644 --- a/src/edge.c +++ b/src/edge.c @@ -18,10 +18,6 @@ #include "n2n.h" -#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_IF_MODE_SIZE 16 /* static | dhcp */ - /* *************************************************** */ /** maximum length of command line arguments */ @@ -47,23 +43,6 @@ int num_cap = sizeof(cap_values)/sizeof(cap_value_t); /* ***************************************************** */ -typedef struct n2n_priv_config { - char tuntap_dev_name[N2N_IFNAMSIZ]; - char ip_mode[N2N_IF_MODE_SIZE]; - char ip_addr[N2N_NETMASK_STR_SIZE]; - char netmask[N2N_NETMASK_STR_SIZE]; - char device_mac[N2N_MACNAMSIZ]; - int mtu; - uint8_t got_s; - uint8_t daemon; -#ifndef WIN32 - uid_t userid; - gid_t groupid; -#endif -} n2n_priv_config_t; - -/* ***************************************************** */ - /** Find the address and IP mode for the tuntap device. * * s is one of these forms: @@ -904,16 +883,17 @@ int main(int argc, char* argv[]) { /* setgid(0); */ #endif - if(tuntap_open(&tuntap, ec.tuntap_dev_name, ec.ip_mode, ec.ip_addr, ec.netmask, ec.device_mac, ec.mtu) < 0) - return(-1); - if(conf.encrypt_key && !strcmp((char*)conf.community_name, conf.encrypt_key)) traceEvent(TRACE_WARNING, "Community and encryption key must differ, otherwise security will be compromised"); - if((eee = edge_init(&tuntap, &conf, &rc)) == NULL) { - traceEvent(TRACE_ERROR, "Failed in edge_init"); - exit(1); - } + if(tuntap_open(&tuntap, ec.tuntap_dev_name, ec.ip_mode, ec.ip_addr, ec.netmask, ec.device_mac, ec.mtu) < 0) + return(-1); + + if((eee = edge_init(&tuntap, &conf, &rc)) == NULL) { + traceEvent(TRACE_ERROR, "Failed in edge_init"); + exit(1); + } + memcpy(&(eee->priv_conf), &ec, sizeof(ec)); #ifndef WIN32 if(ec.daemon) { diff --git a/src/edge_utils.c b/src/edge_utils.c index a5cf145..03cfacc 100644 --- a/src/edge_utils.c +++ b/src/edge_utils.c @@ -18,6 +18,7 @@ #include "n2n.h" #include "header_encryption.h" +#include "edge_utils_win32.h" /* heap allocation for compression as per lzo example doc */ #define HEAP_ALLOC(var,size) lzo_align_t __LZO_MMODEL var [ ((size) + (sizeof(lzo_align_t) - 1)) / sizeof(lzo_align_t) ] @@ -60,57 +61,6 @@ int edge_verify_conf(const n2n_edge_conf_t *conf) { return(0); } -/* ************************************** */ - -struct n2n_edge_stats { - uint32_t tx_p2p; - uint32_t rx_p2p; - uint32_t tx_sup; - uint32_t rx_sup; - uint32_t tx_sup_broadcast; - uint32_t rx_sup_broadcast; -}; - -/* ************************************** */ - -struct n2n_edge { - n2n_edge_conf_t conf; - - /* Status */ - uint8_t sn_idx; /**< Currently active supernode. */ - uint8_t sn_wait; /**< Whether we are waiting for a supernode response. */ - size_t sup_attempts; /**< Number of remaining attempts to this supernode. */ - tuntap_dev device; /**< All about the TUNTAP device */ - n2n_trans_op_t transop; /**< The transop to use when encoding */ - n2n_cookie_t last_cookie; /**< Cookie sent in last REGISTER_SUPER. */ - n2n_route_t *sn_route_to_clean; /**< Supernode route to clean */ - n2n_edge_callbacks_t cb; /**< API callbacks */ - void *user_data; /**< Can hold user data */ - - /* Sockets */ - n2n_sock_t supernode; - int udp_sock; - int udp_mgmt_sock; /**< socket for status info. */ - -#ifndef SKIP_MULTICAST_PEERS_DISCOVERY - n2n_sock_t multicast_peer; /**< Multicast peer group (for local edges) */ - int udp_multicast_sock; /**< socket for local multicast registrations. */ - int multicast_joined; /**< 1 if the group has been joined.*/ -#endif - - /* Peers */ - struct peer_info * known_peers; /**< Edges we are connected to. */ - struct peer_info * pending_peers; /**< Edges we have tried to register with. */ - - /* Timers */ - time_t last_register_req; /**< Check if time to re-register with super*/ - time_t last_p2p; /**< Last time p2p traffic was received. */ - time_t last_sup; /**< Last time a packet arrived from supernode. */ - time_t start_time; /**< For calculating uptime */ - - /* Statistics */ - struct n2n_edge_stats stats; -}; /* ************************************** */ @@ -1564,6 +1514,11 @@ void edge_read_from_tap(n2n_edge_t * eee) { { traceEvent(TRACE_WARNING, "read()=%d [%d/%s]", (signed int)len, errno, strerror(errno)); + traceEvent(TRACE_WARNING, "TAP I/O operation aborted, restart after 10 seconds."); + sleep(10); + tuntap_close(&(eee->device)); + tuntap_open(&(eee->device), eee->priv_conf.tuntap_dev_name, eee->priv_conf.ip_mode, eee->priv_conf.ip_addr, + eee->priv_conf.netmask, eee->priv_conf.device_mac, eee->priv_conf.mtu); } else { @@ -1886,7 +1841,6 @@ int run_edge_loop(n2n_edge_t * eee, int *keep_running) { #endif #ifdef WIN32 - #include "edge_utils_win32.h" struct tunread_arg arg; arg.eee = eee; arg.keep_running = keep_running;