n2n/include/n2n.h

222 lines
8.1 KiB
C
Raw Normal View History

/**
2022-03-12 10:22:42 +01:00
* (C) 2007-22 - ntop.org and contributors
2016-10-23 10:46:15 +02:00
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2016-10-23 10:46:15 +02:00
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not see see <http://www.gnu.org/licenses/>
2016-10-23 10:46:15 +02:00
*
*/
#ifndef _N2N_H_
#define _N2N_H_
/*
tunctl -t tun0
tunctl -t tun1
ifconfig tun0 1.2.3.4 up
ifconfig tun1 1.2.3.5 up
./edge -d tun0 -l 2000 -r 127.0.0.1:3000 -c hello
./edge -d tun1 -l 3000 -r 127.0.0.1:2000 -c hello
2016-10-23 10:46:15 +02:00
tunctl -u UID -t tunX
2016-10-23 10:46:15 +02:00
*/
#define SN_MANUAL_MAC /* allows supernode MAC address to be set manually */
2021-01-27 19:22:11 +01:00
#define N2N_HAVE_DAEMON /* needs to be defined before it gets undefined */
#define N2N_HAVE_TCP /* needs to be defined before it gets undefined */
#define HAVE_BRIDGING_SUPPORT
2016-10-23 10:46:15 +02:00
/* #define N2N_CAN_NAME_IFACE */
#include "config.h" /* Visual C++ */
2016-10-23 10:46:15 +02:00
/* Moved here to define _CRT_SECURE_NO_WARNINGS before all the including takes place */
#ifdef _WIN32
#define N2N_CAN_NAME_IFACE 1
2016-10-23 10:46:15 +02:00
#undef N2N_HAVE_DAEMON
#undef N2N_HAVE_TCP /* as explained on https://github.com/ntop/n2n/pull/627#issuecomment-782093706 */
2016-10-23 10:46:15 +02:00
#undef N2N_HAVE_SETUID
#endif /* _WIN32 */
2020-08-06 19:28:39 +02:00
2016-10-23 10:46:15 +02:00
#include <stdbool.h>
#include <stdio.h> // for size_t, FILE
#include "n2n_define.h"
#include "n2n_typedefs.h"
2016-10-23 10:46:15 +02:00
#ifdef _WIN32
#include <winsock2.h> /* for tcp */
#include <lmaccess.h> /* for privilege check in tools/n2n-route */
#include <lmapibuf.h> /* for privilege check in tools/n2n-route */
#include <sys/stat.h>
#include <windows.h> /* for privilege check in tools/n2n-route */
#define SHUT_RDWR SD_BOTH /* for tcp */
#endif /* #ifdef _WIN32 */
2016-10-23 10:46:15 +02:00
#ifndef _WIN32
#include <netinet/in.h> // for in_addr (ptr only), in_addr_t
#include <pwd.h>
#include <stdint.h> // for uint8_t, uint64_t, uint32_t, uint16_t
#include <sys/types.h> // for time_t
#include <unistd.h> // for close
#define closesocket(a) close(a)
2016-10-23 10:46:15 +02:00
#ifdef __linux__
#define N2N_CAN_NAME_IFACE 1
#endif /* #ifdef __linux__ */
#ifdef __FreeBSD__
#include <netinet/in_systm.h>
#endif /* #ifdef __FreeBSD__ */
#ifdef HAVE_ZSTD
2020-06-21 22:26:27 +02:00
#include <zstd.h>
#endif
#ifdef HAVE_LIBCRYPTO
2020-04-24 08:45:33 +02:00
#include <openssl/opensslv.h>
#include <openssl/crypto.h>
2020-06-10 00:30:11 +02:00
#endif
#endif /* #ifndef _WIN32 */
2016-10-23 10:46:15 +02:00
2021-03-29 09:28:13 +02:00
/* ************************************** */
2018-06-08 14:01:33 +02:00
#ifndef TRACE_ERROR
#define TRACE_ERROR 0
#define TRACE_WARNING 1
#define TRACE_NORMAL 2
#define TRACE_INFO 3
#define TRACE_DEBUG 4
2018-06-08 14:01:33 +02:00
#endif
2016-10-23 10:46:15 +02:00
/* ************************************** */
/* Transop Init Functions */
2020-12-19 12:28:45 +01:00
int n2n_transop_null_init (const n2n_edge_conf_t *conf, n2n_trans_op_t *ttt);
int n2n_transop_tf_init (const n2n_edge_conf_t *conf, n2n_trans_op_t *ttt);
int n2n_transop_aes_init (const n2n_edge_conf_t *conf, n2n_trans_op_t *ttt);
int n2n_transop_cc20_init (const n2n_edge_conf_t *conf, n2n_trans_op_t *ttt);
int n2n_transop_speck_init (const n2n_edge_conf_t *conf, n2n_trans_op_t *ttt);
int n2n_transop_lzo_init (const n2n_edge_conf_t *conf, n2n_trans_op_t *ttt);
#ifdef HAVE_ZSTD
int n2n_transop_zstd_init (const n2n_edge_conf_t *conf, n2n_trans_op_t *ttt);
#endif
2019-04-14 18:08:51 +02:00
/* Log */
2020-12-19 12:28:45 +01:00
void setTraceLevel (int level);
void setUseSyslog (int use_syslog);
void setTraceFile (FILE *f);
int getTraceLevel ();
void closeTraceFile ();
void _traceEvent (int eventTraceLevel, char* file, int line, char * format, ...);
#define traceEvent(level, format, ...) _traceEvent(level, __FILE__, __LINE__, format, ##__VA_ARGS__)
2016-10-23 10:46:15 +02:00
2019-04-14 18:08:51 +02:00
/* Tuntap API */
2021-02-10 10:31:16 +01:00
int tuntap_open (struct tuntap_dev *device, char *dev, const char *address_mode, char *device_ip,
char *device_mask, const char * device_mac, int mtu, int metric);
2020-12-19 12:28:45 +01:00
int tuntap_read (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_get_address (struct tuntap_dev *tuntap);
2019-04-14 18:08:51 +02:00
/* Utils */
char* inaddrtoa (ipstr_t out, struct in_addr addr);
2020-12-19 12:28:45 +01:00
char* intoa (uint32_t addr, char* buf, uint16_t buf_len);
uint32_t bitlen2mask (uint8_t bitlen);
uint8_t mask2bitlen (uint32_t mask);
char* macaddr_str (macstr_t buf, const n2n_mac_t mac);
int str2mac (uint8_t * outmac /* 6 bytes */, const char * s);
int supernode2sock (n2n_sock_t * sn, const n2n_sn_name_t addrIn);
2021-01-13 10:05:26 +01:00
uint8_t is_multi_broadcast (const n2n_mac_t dest_mac);
uint8_t is_broadcast (const n2n_mac_t dest_mac);
uint8_t is_null_mac (const n2n_mac_t dest_mac);
2020-12-19 12:28:45 +01:00
char* msg_type2str (uint16_t msg_type);
void hexdump (const uint8_t * buf, size_t len);
void print_n2n_version ();
int is_empty_ip_address (const n2n_sock_t * sock);
void print_edge_stats (const n2n_edge_t *eee);
2021-04-18 21:02:37 +02:00
int memrnd (uint8_t *address, size_t len);
int memxor (uint8_t *destination, const uint8_t *source, size_t len);
2016-10-23 10:46:15 +02:00
2019-04-14 18:08:51 +02:00
/* Sockets */
2020-12-19 12:28:45 +01:00
char* sock_to_cstr (n2n_sock_str_t out,
const n2n_sock_t * sock);
char * ip_subnet_to_str (dec_ip_bit_str_t buf, const n2n_ip_subnet_t *ipaddr);
SOCKET open_socket (int local_port, in_addr_t address, int type);
2020-12-19 12:28:45 +01:00
int sock_equal (const n2n_sock_t * a,
const n2n_sock_t * b);
2016-10-23 10:46:15 +02:00
2020-07-23 12:21:54 +02:00
/* Header encryption */
2020-12-19 12:28:45 +01:00
uint64_t time_stamp (void);
2020-07-25 14:28:20 +02:00
uint64_t initial_time_stamp (void);
int time_stamp_verify_and_update (uint64_t stamp, uint64_t * previous_stamp, int allow_jitter);
2020-07-23 12:21:54 +02:00
2016-10-23 10:46:15 +02:00
/* Operations on peer_info lists. */
2020-12-19 12:28:45 +01:00
size_t purge_peer_list (struct peer_info ** peer_list,
SOCKET socket_not_to_close,
2021-03-26 16:00:31 +01:00
n2n_tcp_connection_t **tcp_connections,
2020-12-19 12:28:45 +01:00
time_t purge_before);
2020-12-19 12:28:45 +01:00
size_t clear_peer_list (struct peer_info ** peer_list);
size_t purge_expired_nodes (struct peer_info **peer_list,
SOCKET socket_not_to_close,
2021-03-26 16:00:31 +01:00
n2n_tcp_connection_t **tcp_connections,
time_t *p_last_purge,
int frequency, int timeout);
2019-04-14 18:08:51 +02:00
/* Edge conf */
2020-12-19 12:28:45 +01:00
void edge_init_conf_defaults (n2n_edge_conf_t *conf);
int edge_verify_conf (const n2n_edge_conf_t *conf);
int edge_conf_add_supernode (n2n_edge_conf_t *conf, const char *ip_and_port);
const n2n_edge_conf_t* edge_get_conf (const n2n_edge_t *eee);
void edge_term_conf (n2n_edge_conf_t *conf);
2019-04-14 18:08:51 +02:00
/* Public functions */
2020-12-19 12:28:45 +01:00
n2n_edge_t* edge_init (const n2n_edge_conf_t *conf, int *rv);
void update_supernode_reg (n2n_edge_t * eee, time_t nowTime);
void readFromIPSocket (n2n_edge_t * eee, int in_sock);
void edge_term (n2n_edge_t *eee);
void edge_set_callbacks (n2n_edge_t *eee, const n2n_edge_callbacks_t *callbacks);
void edge_set_userdata (n2n_edge_t *eee, void *user_data);
void* edge_get_userdata (n2n_edge_t *eee);
void edge_send_packet2net (n2n_edge_t *eee, uint8_t *tap_pkt, size_t len);
void edge_read_from_tap (n2n_edge_t *eee);
int edge_get_n2n_socket (n2n_edge_t *eee);
int edge_get_management_socket (n2n_edge_t *eee);
JSON Reply Management API - feature parity with old management interfaces (#861) * Ensure that recent code additions pass the linter * Include some of the more obviously correct lint fixes to edge_utils.c * Refactor edge JSON api into its own source file * Use shorter names for static management functions * Implement a JSON RPC way of managing the verbosity * Tidy up help display in n2nctl script * Make note of issue with implementing the stop command * Implement a JSON RPC call to fetch current community * Make n2nhttpd time value be more self-contained * Make n2nhttpd order more closely match the existing management stats output * Wire up status page to the verbosity setting * Add JSON versions of the remainder of the edge management stats * Add new file to cmake * Properly define management handler * Only update the last updated timestamp after a successful data fetch * Function and types definition cleanup * Force correct type for python scripts mgmt port * Implement initial JSON API for supernode * Fix whitespace error * Use helper function for rendering peers ip4 address * Proxy the auth requirement back out to the http client, allowing normal http auth to be used * Ensure that we do not leak the federation community * Use the same rpc method name and output for both edge and supernode for peers/edges * Allow n2nctl to show raw data returned without resorting to tricks * Make n2nctl pretty printer understandable with an empty table * Use the full name for supernodes RPC call * Use same RPC method name (but some missing fields) for getting communities from both edge and supernode * Add *_sup_broadcast stats to edge packet stats output * Refacter the stats into a packetstats method for supernode RPC * Even if I am not going to prettyprint the timestamps, at least make all the timestamps on the page the same unit * Simplify the RPC handlers by flagging some as writable and checking that in the multiplexer * Remove invalid edges data * Avoid crash on bad data to verbose RPC * Avoid showing bad or inconsistant protocol data in communities RPC * Minor clarification on when --write is handled * Make linter happy * Fix changed method name in n2nhttpd * Move mainloop stop flag into the n2n_edge_t structure, allowing access from management commands * Implement edge RPC stop command * Move mainloop stop flag into the n2n_sn_t structure, allowing access from management commands * Implement supernode RPC stop command * Allow multiple pages to be served from mini httpd * Extract common script functions into a separate URL * Handle an edge case in the python rpc class With a proper tag-based demultiplexer, this case should be a nop, but we are single-threaded and rely on the packet ordering in this library. * Add n2nhttpd support to query supernode using urls prefixed with /supernode/ * Handle missing values in javascript table print * Add another less filtering javascript key/value renderer * Add a supernode.html page to the n2nhttpd * Address lint issue * Mention the second html page on the Scripts doc * Remove purgable column from supernode edges list - it looks like it is rarely going to be set * Add a simple one-line example command at the top of the API documentation * Acknowledge that this is not the most efficient protocol, but point out that it was not supposed to be * Make it clear that the n2nctl script works for both edge and supernode * Fight with inconsistant github runner results * Turn off the /right/ coverage generator
2021-10-23 07:20:05 +02:00
int run_edge_loop (n2n_edge_t *eee);
2020-12-19 12:28:45 +01:00
int quick_edge_init (char *device_name, char *community_name,
char *encrypt_key, char *device_mac,
char *local_ip_address,
char *supernode_ip_address_port,
bool *keep_on_running);
2020-12-19 12:28:45 +01:00
int comm_init (struct sn_community *comm, char *cmn);
2021-08-30 10:38:31 +02:00
int sn_init_defaults (n2n_sn_t *sss);
2021-09-15 18:23:45 +02:00
void sn_init (n2n_sn_t *sss);
2020-12-19 12:28:45 +01:00
void sn_term (n2n_sn_t *sss);
int supernode2sock (n2n_sock_t * sn, const n2n_sn_name_t addrIn);
2021-01-13 10:05:26 +01:00
struct peer_info* add_sn_to_list_by_mac_or_sock (struct peer_info **sn_list, n2n_sock_t *sock, const n2n_mac_t mac, int *skip_add);
JSON Reply Management API - feature parity with old management interfaces (#861) * Ensure that recent code additions pass the linter * Include some of the more obviously correct lint fixes to edge_utils.c * Refactor edge JSON api into its own source file * Use shorter names for static management functions * Implement a JSON RPC way of managing the verbosity * Tidy up help display in n2nctl script * Make note of issue with implementing the stop command * Implement a JSON RPC call to fetch current community * Make n2nhttpd time value be more self-contained * Make n2nhttpd order more closely match the existing management stats output * Wire up status page to the verbosity setting * Add JSON versions of the remainder of the edge management stats * Add new file to cmake * Properly define management handler * Only update the last updated timestamp after a successful data fetch * Function and types definition cleanup * Force correct type for python scripts mgmt port * Implement initial JSON API for supernode * Fix whitespace error * Use helper function for rendering peers ip4 address * Proxy the auth requirement back out to the http client, allowing normal http auth to be used * Ensure that we do not leak the federation community * Use the same rpc method name and output for both edge and supernode for peers/edges * Allow n2nctl to show raw data returned without resorting to tricks * Make n2nctl pretty printer understandable with an empty table * Use the full name for supernodes RPC call * Use same RPC method name (but some missing fields) for getting communities from both edge and supernode * Add *_sup_broadcast stats to edge packet stats output * Refacter the stats into a packetstats method for supernode RPC * Even if I am not going to prettyprint the timestamps, at least make all the timestamps on the page the same unit * Simplify the RPC handlers by flagging some as writable and checking that in the multiplexer * Remove invalid edges data * Avoid crash on bad data to verbose RPC * Avoid showing bad or inconsistant protocol data in communities RPC * Minor clarification on when --write is handled * Make linter happy * Fix changed method name in n2nhttpd * Move mainloop stop flag into the n2n_edge_t structure, allowing access from management commands * Implement edge RPC stop command * Move mainloop stop flag into the n2n_sn_t structure, allowing access from management commands * Implement supernode RPC stop command * Allow multiple pages to be served from mini httpd * Extract common script functions into a separate URL * Handle an edge case in the python rpc class With a proper tag-based demultiplexer, this case should be a nop, but we are single-threaded and rely on the packet ordering in this library. * Add n2nhttpd support to query supernode using urls prefixed with /supernode/ * Handle missing values in javascript table print * Add another less filtering javascript key/value renderer * Add a supernode.html page to the n2nhttpd * Address lint issue * Mention the second html page on the Scripts doc * Remove purgable column from supernode edges list - it looks like it is rarely going to be set * Add a simple one-line example command at the top of the API documentation * Acknowledge that this is not the most efficient protocol, but point out that it was not supposed to be * Make it clear that the n2nctl script works for both edge and supernode * Fight with inconsistant github runner results * Turn off the /right/ coverage generator
2021-10-23 07:20:05 +02:00
int run_sn_loop (n2n_sn_t *sss);
2020-12-19 12:28:45 +01:00
int assign_one_ip_subnet (n2n_sn_t *sss, struct sn_community *comm);
const char* compression_str (uint8_t cmpr);
const char* transop_str (enum n2n_transform tr);
void readFromMgmtSocket (n2n_edge_t *eee);
void mgmt_event_post (enum n2n_event_topic topic, int data0, void *data1);
2016-10-23 10:46:15 +02:00
#endif /* _N2N_H_ */