mirror of
https://github.com/ntop/n2n.git
synced 2024-09-20 00:51:10 +02:00
change space indention
This commit is contained in:
parent
102ff18aaa
commit
067f675faa
1277
src/edge.c
1277
src/edge.c
File diff suppressed because it is too large
Load Diff
4178
src/edge_utils.c
4178
src/edge_utils.c
File diff suppressed because it is too large
Load Diff
|
@ -8,7 +8,7 @@
|
||||||
*
|
*
|
||||||
* This program is distributed in the hope that it will be useful,
|
* This program is distributed in the hope that it will be useful,
|
||||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
* GNU General Public License for more details.
|
* GNU General Public License for more details.
|
||||||
*
|
*
|
||||||
* You should have received a copy of the GNU General Public License
|
* You should have received a copy of the GNU General Public License
|
||||||
|
@ -24,28 +24,29 @@
|
||||||
|
|
||||||
static DWORD* tunReadThread (LPVOID lpArg) {
|
static DWORD* tunReadThread (LPVOID lpArg) {
|
||||||
|
|
||||||
struct tunread_arg *arg = (struct tunread_arg*)lpArg;
|
struct tunread_arg *arg = (struct tunread_arg*)lpArg;
|
||||||
|
|
||||||
while(*arg->keep_running)
|
while(*arg->keep_running) {
|
||||||
edge_read_from_tap(arg->eee);
|
edge_read_from_tap(arg->eee);
|
||||||
|
}
|
||||||
|
|
||||||
return((DWORD*)NULL);
|
return((DWORD*)NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* ************************************** */
|
/* ************************************** */
|
||||||
|
|
||||||
/** Start a second thread in Windows because TUNTAP interfaces do not expose
|
/** Start a second thread in Windows because TUNTAP interfaces do not expose
|
||||||
* file descriptors. */
|
* file descriptors. */
|
||||||
HANDLE startTunReadThread (struct tunread_arg *arg) {
|
HANDLE startTunReadThread (struct tunread_arg *arg) {
|
||||||
|
|
||||||
DWORD dwThreadId;
|
DWORD dwThreadId;
|
||||||
|
|
||||||
return(CreateThread(NULL, /* security attributes */
|
return(CreateThread(NULL, /* security attributes */
|
||||||
0, /* use default stack size */
|
0, /* use default stack size */
|
||||||
(LPTHREAD_START_ROUTINE)tunReadThread, /* thread function */
|
(LPTHREAD_START_ROUTINE)tunReadThread, /* thread function */
|
||||||
(void*)arg, /* argument to thread function */
|
(void*)arg, /* argument to thread function */
|
||||||
0, /* thread creation flags */
|
0, /* thread creation flags */
|
||||||
&dwThreadId)); /* thread id out */
|
&dwThreadId)); /* thread id out */
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
|
@ -8,7 +8,7 @@
|
||||||
*
|
*
|
||||||
* This program is distributed in the hope that it will be useful,
|
* This program is distributed in the hope that it will be useful,
|
||||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
* GNU General Public License for more details.
|
* GNU General Public License for more details.
|
||||||
*
|
*
|
||||||
* You should have received a copy of the GNU General Public License
|
* You should have received a copy of the GNU General Public License
|
||||||
|
@ -22,52 +22,52 @@ static int keep_running;
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
|
|
||||||
n2n_edge_conf_t conf;
|
n2n_edge_conf_t conf;
|
||||||
tuntap_dev tuntap;
|
tuntap_dev tuntap;
|
||||||
n2n_edge_t *eee;
|
n2n_edge_t *eee;
|
||||||
int rc;
|
int rc;
|
||||||
|
|
||||||
edge_init_conf_defaults(&conf);
|
edge_init_conf_defaults(&conf);
|
||||||
conf.allow_p2p = 1; // Whether to allow peer-to-peer communication
|
conf.allow_p2p = 1; // Whether to allow peer-to-peer communication
|
||||||
conf.allow_routing = 1; // Whether to allow the edge to route packets to other edges
|
conf.allow_routing = 1; // Whether to allow the edge to route packets to other edges
|
||||||
snprintf((char *)conf.community_name, sizeof(conf.community_name), "%s", "mycommunity"); // Community to connect to
|
snprintf((char *)conf.community_name, sizeof(conf.community_name), "%s", "mycommunity"); // Community to connect to
|
||||||
conf.disable_pmtu_discovery = 1; // Whether to disable the path MTU discovery
|
conf.disable_pmtu_discovery = 1; // Whether to disable the path MTU discovery
|
||||||
conf.drop_multicast = 0; // Whether to disable multicast
|
conf.drop_multicast = 0; // Whether to disable multicast
|
||||||
conf.tuntap_ip_mode = TUNTAP_IP_MODE_SN_ASSIGN; // How to set the IP address
|
conf.tuntap_ip_mode = TUNTAP_IP_MODE_SN_ASSIGN; // How to set the IP address
|
||||||
conf.encrypt_key = "mysecret"; // Secret to decrypt & encrypt with
|
conf.encrypt_key = "mysecret"; // Secret to decrypt & encrypt with
|
||||||
conf.local_port = 0; // What port to use (0 = any port)
|
conf.local_port = 0; // What port to use (0 = any port)
|
||||||
conf.mgmt_port = N2N_EDGE_MGMT_PORT; // Edge management port (5644 by default)
|
conf.mgmt_port = N2N_EDGE_MGMT_PORT; // Edge management port (5644 by default)
|
||||||
conf.register_interval = 1; // Interval for both UDP NAT hole punching and supernode registration
|
conf.register_interval = 1; // Interval for both UDP NAT hole punching and supernode registration
|
||||||
conf.register_ttl = 1; // Interval for UDP NAT hole punching through supernode
|
conf.register_ttl = 1; // Interval for UDP NAT hole punching through supernode
|
||||||
edge_conf_add_supernode(&conf, "localhost:1234"); // Supernode to connect to
|
edge_conf_add_supernode(&conf, "localhost:1234"); // Supernode to connect to
|
||||||
conf.tos = 16; // Type of service for sent packets
|
conf.tos = 16; // Type of service for sent packets
|
||||||
conf.transop_id = N2N_TRANSFORM_ID_TWOFISH; // Use the twofish encryption
|
conf.transop_id = N2N_TRANSFORM_ID_TWOFISH; // Use the twofish encryption
|
||||||
|
|
||||||
if(edge_verify_conf(&conf) != 0) {
|
if(edge_verify_conf(&conf) != 0) {
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
if(tuntap_open(&tuntap,
|
|
||||||
"edge0", // Name of the device to create
|
|
||||||
"static", // IP mode; static|dhcp
|
|
||||||
"10.0.0.1", // Set ip address
|
|
||||||
"255.255.255.0", // Netmask to use
|
|
||||||
"DE:AD:BE:EF:01:10", // Set mac address
|
|
||||||
DEFAULT_MTU) < 0) // MTU to use
|
|
||||||
{
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
eee = edge_init(&conf, &rc);
|
if(tuntap_open(&tuntap,
|
||||||
if(eee == NULL) {
|
"edge0", // Name of the device to create
|
||||||
exit(1);
|
"static", // IP mode; static|dhcp
|
||||||
}
|
"10.0.0.1", // Set ip address
|
||||||
|
"255.255.255.0", // Netmask to use
|
||||||
|
"DE:AD:BE:EF:01:10", // Set mac address
|
||||||
|
DEFAULT_MTU) < 0) // MTU to use
|
||||||
|
{
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
keep_running = 1;
|
eee = edge_init(&conf, &rc);
|
||||||
rc = run_edge_loop(eee, &keep_running);
|
if(eee == NULL) {
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
edge_term(eee);
|
keep_running = 1;
|
||||||
tuntap_close(&tuntap);
|
rc = run_edge_loop(eee, &keep_running);
|
||||||
|
|
||||||
return rc;
|
edge_term(eee);
|
||||||
|
tuntap_close(&tuntap);
|
||||||
|
|
||||||
|
return rc;
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,7 +8,7 @@
|
||||||
*
|
*
|
||||||
* This program is distributed in the hope that it will be useful,
|
* This program is distributed in the hope that it will be useful,
|
||||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
* GNU General Public License for more details.
|
* GNU General Public License for more details.
|
||||||
*
|
*
|
||||||
* You should have received a copy of the GNU General Public License
|
* You should have received a copy of the GNU General Public License
|
||||||
|
@ -19,37 +19,37 @@
|
||||||
#include "n2n.h"
|
#include "n2n.h"
|
||||||
|
|
||||||
/*
|
/*
|
||||||
This tool demonstrates how to easily embed
|
This tool demonstrates how to easily embed
|
||||||
n2n on an existing application
|
n2n on an existing application
|
||||||
*/
|
*/
|
||||||
|
|
||||||
int main (int argc, char* argv[]) {
|
int main (int argc, char* argv[]) {
|
||||||
|
|
||||||
char *device_name = (char*)"n2n0";
|
char *device_name = (char*)"n2n0";
|
||||||
char *network_name = (char*)"mynetwork";
|
char *network_name = (char*)"mynetwork";
|
||||||
char *secret_key = (char*)"mysecret";
|
char *secret_key = (char*)"mysecret";
|
||||||
char *my_mac_address = (char*)"DE:AD:BE:EF:01:10";
|
char *my_mac_address = (char*)"DE:AD:BE:EF:01:10";
|
||||||
char *my_ipv4_addr = (char*)"1.2.3.4";
|
char *my_ipv4_addr = (char*)"1.2.3.4";
|
||||||
char *supernode = (char*)"7.8.9.10:1234";
|
char *supernode = (char*)"7.8.9.10:1234";
|
||||||
int keep_on_running = 1;
|
int keep_on_running = 1;
|
||||||
|
|
||||||
/* Increase tracelevel to see what's happening */
|
/* Increase tracelevel to see what's happening */
|
||||||
setTraceLevel(10);
|
setTraceLevel(10);
|
||||||
|
|
||||||
/* Random seed */
|
/* Random seed */
|
||||||
n2n_srand (n2n_seed());
|
n2n_srand (n2n_seed());
|
||||||
|
|
||||||
/*
|
/*
|
||||||
NOTE
|
NOTE
|
||||||
|
|
||||||
As the function below won't end, you should
|
As the function below won't end, you should
|
||||||
call it inside a separate thread
|
call it inside a separate thread
|
||||||
*/
|
*/
|
||||||
return(quick_edge_init(device_name,
|
return(quick_edge_init(device_name,
|
||||||
network_name,
|
network_name,
|
||||||
secret_key,
|
secret_key,
|
||||||
my_mac_address,
|
my_mac_address,
|
||||||
my_ipv4_addr,
|
my_ipv4_addr,
|
||||||
supernode,
|
supernode,
|
||||||
&keep_on_running));
|
&keep_on_running));
|
||||||
}
|
}
|
||||||
|
|
|
@ -20,31 +20,29 @@
|
||||||
|
|
||||||
static int keep_running;
|
static int keep_running;
|
||||||
|
|
||||||
int main()
|
int main () {
|
||||||
{
|
|
||||||
n2n_sn_t sss_node;
|
|
||||||
int rc;
|
|
||||||
|
|
||||||
sn_init(&sss_node);
|
n2n_sn_t sss_node;
|
||||||
sss_node.daemon = 0; // Whether to daemonize
|
int rc;
|
||||||
sss_node.lport = 1234; // Main UDP listen port
|
|
||||||
|
|
||||||
sss_node.sock = open_socket(sss_node.lport, 1);
|
sn_init(&sss_node);
|
||||||
if (-1 == sss_node.sock)
|
sss_node.daemon = 0; // Whether to daemonize
|
||||||
{
|
sss_node.lport = 1234; // Main UDP listen port
|
||||||
exit(-2);
|
|
||||||
}
|
|
||||||
|
|
||||||
sss_node.mgmt_sock = open_socket(5645, 0); // Main UDP management port
|
sss_node.sock = open_socket(sss_node.lport, 1);
|
||||||
if (-1 == sss_node.mgmt_sock)
|
if(-1 == sss_node.sock) {
|
||||||
{
|
exit(-2);
|
||||||
exit(-2);
|
}
|
||||||
}
|
|
||||||
|
|
||||||
keep_running = 1;
|
sss_node.mgmt_sock = open_socket(5645, 0); // Main UDP management port
|
||||||
rc = run_sn_loop(&sss_node, &keep_running);
|
if(-1 == sss_node.mgmt_sock) {
|
||||||
|
exit(-2);
|
||||||
|
}
|
||||||
|
|
||||||
sn_term(&sss_node);
|
keep_running = 1;
|
||||||
|
rc = run_sn_loop(&sss_node, &keep_running);
|
||||||
|
|
||||||
return rc;
|
sn_term(&sss_node);
|
||||||
|
|
||||||
|
return rc;
|
||||||
}
|
}
|
||||||
|
|
586
src/n2n_regex.c
586
src/n2n_regex.c
|
@ -8,7 +8,7 @@
|
||||||
*
|
*
|
||||||
* This program is distributed in the hope that it will be useful,
|
* This program is distributed in the hope that it will be useful,
|
||||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
* GNU General Public License for more details.
|
* GNU General Public License for more details.
|
||||||
*
|
*
|
||||||
* You should have received a copy of the GNU General Public License
|
* You should have received a copy of the GNU General Public License
|
||||||
|
@ -29,21 +29,21 @@
|
||||||
*
|
*
|
||||||
* Supports:
|
* Supports:
|
||||||
* ---------
|
* ---------
|
||||||
* '.' Dot, matches any character
|
* '.' Dot, matches any character
|
||||||
* '^' Start anchor, matches beginning of string -- NOTE: currently disabled (checking for full matches anyway)
|
* '^' Start anchor, matches beginning of string -- NOTE: currently disabled (checking for full matches anyway)
|
||||||
* '$' End anchor, matches end of string -- NOTE: currently disabled (checking for full matches anyway)
|
* '$' End anchor, matches end of string -- NOTE: currently disabled (checking for full matches anyway)
|
||||||
* '*' Asterisk, match zero or more (greedy)
|
* '*' Asterisk, match zero or more (greedy)
|
||||||
* '+' Plus, match one or more (greedy)
|
* '+' Plus, match one or more (greedy)
|
||||||
* '?' Question, match zero or one (non-greedy)
|
* '?' Question, match zero or one (non-greedy)
|
||||||
* '[abc]' Character class, match if one of {'a', 'b', 'c'}
|
* '[abc]' Character class, match if one of {'a', 'b', 'c'}
|
||||||
* '[^abc]' Inverted class, match if NOT one of {'a', 'b', 'c'} -- NOTE: feature is currently broken!
|
* '[^abc]' Inverted class, match if NOT one of {'a', 'b', 'c'} -- NOTE: feature is currently broken!
|
||||||
* '[a-zA-Z]' Character ranges, the character set of the ranges { a-z | A-Z }
|
* '[a-zA-Z]' Character ranges, the character set of the ranges { a-z | A-Z }
|
||||||
* '\s' Whitespace, \t \f \r \n \v and spaces
|
* '\s' Whitespace, \t \f \r \n \v and spaces
|
||||||
* '\S' Non-whitespace
|
* '\S' Non-whitespace
|
||||||
* '\w' Alphanumeric, [a-zA-Z0-9_]
|
* '\w' Alphanumeric, [a-zA-Z0-9_]
|
||||||
* '\W' Non-alphanumeric
|
* '\W' Non-alphanumeric
|
||||||
* '\d' Digits, [0-9]
|
* '\d' Digits, [0-9]
|
||||||
* '\D' Non-digits
|
* '\D' Non-digits
|
||||||
*
|
*
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
@ -54,18 +54,18 @@
|
||||||
|
|
||||||
/* Definitions: */
|
/* Definitions: */
|
||||||
|
|
||||||
#define MAX_REGEXP_OBJECTS 30 /* Max number of regex symbols in expression. */
|
#define MAX_REGEXP_OBJECTS 30 /* Max number of regex symbols in expression. */
|
||||||
#define MAX_CHAR_CLASS_LEN 40 /* Max length of character-class buffer in. */
|
#define MAX_CHAR_CLASS_LEN 40 /* Max length of character-class buffer in. */
|
||||||
|
|
||||||
|
|
||||||
enum { UNUSED, DOT, BEGIN, END, QUESTIONMARK, STAR, PLUS, CHAR_TYPE, CHAR_CLASS, INV_CHAR_CLASS, DIGIT, NOT_DIGIT, ALPHA, NOT_ALPHA, WHITESPACE, NOT_WHITESPACE, /* BRANCH */ };
|
enum { UNUSED, DOT, BEGIN, END, QUESTIONMARK, STAR, PLUS, CHAR_TYPE, CHAR_CLASS, INV_CHAR_CLASS, DIGIT, NOT_DIGIT, ALPHA, NOT_ALPHA, WHITESPACE, NOT_WHITESPACE, /* BRANCH */ };
|
||||||
|
|
||||||
typedef struct regex_t {
|
typedef struct regex_t {
|
||||||
unsigned char type; /* CHAR_TYPE, STAR, etc. */
|
unsigned char type; /* CHAR_TYPE, STAR, etc. */
|
||||||
union {
|
union {
|
||||||
unsigned char ch; /* the character itself */
|
unsigned char ch; /* the character itself */
|
||||||
unsigned char* ccl; /* OR a pointer to characters in class */
|
unsigned char* ccl; /* OR a pointer to characters in class */
|
||||||
};
|
};
|
||||||
} regex_t;
|
} regex_t;
|
||||||
|
|
||||||
|
|
||||||
|
@ -89,187 +89,187 @@ static int ismetachar (char c);
|
||||||
/* Public functions: */
|
/* Public functions: */
|
||||||
int re_match (const char* pattern, const char* text, int* matchlength) {
|
int re_match (const char* pattern, const char* text, int* matchlength) {
|
||||||
|
|
||||||
re_t re_p; /* pointer to (to be created) copy of compiled regex */
|
re_t re_p; /* pointer to (to be created) copy of compiled regex */
|
||||||
int ret = -1;
|
int ret = -1;
|
||||||
|
|
||||||
re_p = re_compile (pattern);
|
re_p = re_compile (pattern);
|
||||||
ret = re_matchp(re_p, text, matchlength);
|
ret = re_matchp(re_p, text, matchlength);
|
||||||
free(re_p);
|
free(re_p);
|
||||||
|
|
||||||
return(ret);
|
return(ret);
|
||||||
}
|
}
|
||||||
|
|
||||||
int re_matchp (re_t pattern, const char* text, int* matchlength) {
|
int re_matchp (re_t pattern, const char* text, int* matchlength) {
|
||||||
|
|
||||||
*matchlength = 0;
|
*matchlength = 0;
|
||||||
|
|
||||||
if(pattern != 0) {
|
if(pattern != 0) {
|
||||||
if(pattern[0].type == BEGIN) {
|
if(pattern[0].type == BEGIN) {
|
||||||
return ((matchpattern(&pattern[1], text, matchlength)) ? 0 : -1);
|
return ((matchpattern(&pattern[1], text, matchlength)) ? 0 : -1);
|
||||||
} else {
|
} else {
|
||||||
int idx = -1;
|
int idx = -1;
|
||||||
|
|
||||||
do {
|
do {
|
||||||
idx += 1;
|
idx += 1;
|
||||||
|
|
||||||
if(matchpattern(pattern, text, matchlength)) {
|
if(matchpattern(pattern, text, matchlength)) {
|
||||||
if(text[0] == '\0') {
|
if(text[0] == '\0') {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
return idx;
|
return idx;
|
||||||
|
}
|
||||||
|
} while(*text++ != '\0');
|
||||||
}
|
}
|
||||||
} while(*text++ != '\0');
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
re_t re_compile (const char* pattern) {
|
re_t re_compile (const char* pattern) {
|
||||||
|
|
||||||
/* The sizes of the two static arrays below substantiates the static RAM usage of this module.
|
/* The sizes of the two static arrays below substantiates the static RAM usage of this module.
|
||||||
MAX_REGEXP_OBJECTS is the max number of symbols in the expression.
|
MAX_REGEXP_OBJECTS is the max number of symbols in the expression.
|
||||||
MAX_CHAR_CLASS_LEN determines the size of buffer for chars in all char-classes in the expression. */
|
MAX_CHAR_CLASS_LEN determines the size of buffer for chars in all char-classes in the expression. */
|
||||||
static regex_t re_compiled[MAX_REGEXP_OBJECTS];
|
static regex_t re_compiled[MAX_REGEXP_OBJECTS];
|
||||||
re_t re_p; /* pointer to (to be created) copy of compiled regex in re_compiled */
|
re_t re_p; /* pointer to (to be created) copy of compiled regex in re_compiled */
|
||||||
|
|
||||||
static unsigned char ccl_buf[MAX_CHAR_CLASS_LEN];
|
static unsigned char ccl_buf[MAX_CHAR_CLASS_LEN];
|
||||||
int ccl_bufidx = 1;
|
int ccl_bufidx = 1;
|
||||||
|
|
||||||
char c; /* current char in pattern */
|
char c; /* current char in pattern */
|
||||||
int i = 0; /* index into pattern */
|
int i = 0; /* index into pattern */
|
||||||
int j = 0; /* index into re_compiled */
|
int j = 0; /* index into re_compiled */
|
||||||
|
|
||||||
while(pattern[i] != '\0' && (j+1 < MAX_REGEXP_OBJECTS)) {
|
while(pattern[i] != '\0' && (j + 1 < MAX_REGEXP_OBJECTS)) {
|
||||||
c = pattern[i];
|
c = pattern[i];
|
||||||
|
|
||||||
switch(c) {
|
switch(c) {
|
||||||
/* Meta-characters: */
|
/* Meta-characters: */
|
||||||
// case '^': { re_compiled[j].type = BEGIN; } break; <-- disabled (always full matches)
|
// case '^': { re_compiled[j].type = BEGIN; } break; <-- disabled (always full matches)
|
||||||
// case '$': { re_compiled[j].type = END; } break; <-- disabled (always full matches)
|
// case '$': { re_compiled[j].type = END; } break; <-- disabled (always full matches)
|
||||||
case '.': { re_compiled[j].type = DOT; } break;
|
case '.': { re_compiled[j].type = DOT; } break;
|
||||||
case '*': { re_compiled[j].type = STAR; } break;
|
case '*': { re_compiled[j].type = STAR; } break;
|
||||||
case '+': { re_compiled[j].type = PLUS; } break;
|
case '+': { re_compiled[j].type = PLUS; } break;
|
||||||
case '?': { re_compiled[j].type = QUESTIONMARK; } break;
|
case '?': { re_compiled[j].type = QUESTIONMARK; } break;
|
||||||
/* case '|': { re_compiled[j].type = BRANCH; } break; <-- not working properly */
|
/* case '|': { re_compiled[j].type = BRANCH; } break; <-- not working properly */
|
||||||
|
|
||||||
/* Escaped character-classes (\s \w ...): */
|
/* Escaped character-classes (\s \w ...): */
|
||||||
case '\\': {
|
case '\\': {
|
||||||
if(pattern[i+1] != '\0') {
|
if(pattern[i + 1] != '\0') {
|
||||||
/* Skip the escape-char '\\' */
|
/* Skip the escape-char '\\' */
|
||||||
i += 1;
|
i += 1;
|
||||||
/* ... and check the next */
|
/* ... and check the next */
|
||||||
switch(pattern[i]) {
|
switch(pattern[i]) {
|
||||||
/* Meta-character: */
|
/* Meta-character: */
|
||||||
case 'd': { re_compiled[j].type = DIGIT; } break;
|
case 'd': { re_compiled[j].type = DIGIT; } break;
|
||||||
case 'D': { re_compiled[j].type = NOT_DIGIT; } break;
|
case 'D': { re_compiled[j].type = NOT_DIGIT; } break;
|
||||||
case 'w': { re_compiled[j].type = ALPHA; } break;
|
case 'w': { re_compiled[j].type = ALPHA; } break;
|
||||||
case 'W': { re_compiled[j].type = NOT_ALPHA; } break;
|
case 'W': { re_compiled[j].type = NOT_ALPHA; } break;
|
||||||
case 's': { re_compiled[j].type = WHITESPACE; } break;
|
case 's': { re_compiled[j].type = WHITESPACE; } break;
|
||||||
case 'S': { re_compiled[j].type = NOT_WHITESPACE; } break;
|
case 'S': { re_compiled[j].type = NOT_WHITESPACE; } break;
|
||||||
|
|
||||||
/* Escaped character, e.g. '.' */
|
/* Escaped character, e.g. '.' */
|
||||||
default: {
|
default: {
|
||||||
re_compiled[j].type = CHAR_TYPE;
|
re_compiled[j].type = CHAR_TYPE;
|
||||||
re_compiled[j].ch = pattern[i];
|
re_compiled[j].ch = pattern[i];
|
||||||
|
} break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/* '\\' as last char in pattern -> invalid regular expression. */
|
||||||
|
/*
|
||||||
|
else
|
||||||
|
{
|
||||||
|
re_compiled[j].type = CHAR_TYPE;
|
||||||
|
re_compiled[j].ch = pattern[i];
|
||||||
|
}
|
||||||
|
*/
|
||||||
} break;
|
} break;
|
||||||
}
|
|
||||||
}
|
|
||||||
/* '\\' as last char in pattern -> invalid regular expression. */
|
|
||||||
/*
|
|
||||||
else
|
|
||||||
{
|
|
||||||
re_compiled[j].type = CHAR_TYPE;
|
|
||||||
re_compiled[j].ch = pattern[i];
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
} break;
|
|
||||||
|
|
||||||
/* Character class: */
|
/* Character class: */
|
||||||
case '[': {
|
case '[': {
|
||||||
/* Remember where the char-buffer starts. */
|
/* Remember where the char-buffer starts. */
|
||||||
int buf_begin = ccl_bufidx;
|
int buf_begin = ccl_bufidx;
|
||||||
|
|
||||||
/* Look-ahead to determine if negated */
|
/* Look-ahead to determine if negated */
|
||||||
if(pattern[i+1] == '^') {
|
if(pattern[i+1] == '^') {
|
||||||
re_compiled[j].type = INV_CHAR_CLASS;
|
re_compiled[j].type = INV_CHAR_CLASS;
|
||||||
i += 1; /* Increment i to avoid including '^' in the char-buffer */
|
i += 1; /* Increment i to avoid including '^' in the char-buffer */
|
||||||
} else {
|
} else {
|
||||||
re_compiled[j].type = CHAR_CLASS;
|
re_compiled[j].type = CHAR_CLASS;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Copy characters inside [..] to buffer */
|
/* Copy characters inside [..] to buffer */
|
||||||
while( (pattern[++i] != ']')
|
while((pattern[++i] != ']')
|
||||||
&& (pattern[i] != '\0')) /* Missing ] */
|
&& (pattern[i] != '\0')) /* Missing ] */
|
||||||
{
|
{
|
||||||
if(pattern[i] == '\\') {
|
if(pattern[i] == '\\') {
|
||||||
if(ccl_bufidx >= MAX_CHAR_CLASS_LEN - 1) {
|
if(ccl_bufidx >= MAX_CHAR_CLASS_LEN - 1) {
|
||||||
//fputs("exceeded internal buffer!\n", stderr);
|
//fputs("exceeded internal buffer!\n", stderr);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
ccl_buf[ccl_bufidx++] = pattern[i++];
|
ccl_buf[ccl_bufidx++] = pattern[i++];
|
||||||
} else if(ccl_bufidx >= MAX_CHAR_CLASS_LEN) {
|
} else if(ccl_bufidx >= MAX_CHAR_CLASS_LEN) {
|
||||||
//fputs("exceeded internal buffer!\n", stderr);
|
//fputs("exceeded internal buffer!\n", stderr);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
ccl_buf[ccl_bufidx++] = pattern[i];
|
ccl_buf[ccl_bufidx++] = pattern[i];
|
||||||
}
|
}
|
||||||
if(ccl_bufidx >= MAX_CHAR_CLASS_LEN) {
|
if(ccl_bufidx >= MAX_CHAR_CLASS_LEN) {
|
||||||
/* Catches cases such as [00000000000000000000000000000000000000][ */
|
/* Catches cases such as [00000000000000000000000000000000000000][ */
|
||||||
//fputs("exceeded internal buffer!\n", stderr);
|
//fputs("exceeded internal buffer!\n", stderr);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
/* Null-terminate string end */
|
/* Null-terminate string end */
|
||||||
ccl_buf[ccl_bufidx++] = 0;
|
ccl_buf[ccl_bufidx++] = 0;
|
||||||
re_compiled[j].ccl = &ccl_buf[buf_begin];
|
re_compiled[j].ccl = &ccl_buf[buf_begin];
|
||||||
} break;
|
} break;
|
||||||
|
|
||||||
/* Other characters: */
|
/* Other characters: */
|
||||||
default: {
|
default: {
|
||||||
re_compiled[j].type = CHAR_TYPE;
|
re_compiled[j].type = CHAR_TYPE;
|
||||||
re_compiled[j].ch = c;
|
re_compiled[j].ch = c;
|
||||||
} break;
|
} break;
|
||||||
|
}
|
||||||
|
i += 1;
|
||||||
|
j += 1;
|
||||||
}
|
}
|
||||||
i += 1;
|
/* 'UNUSED' is a sentinel used to indicate end-of-pattern */
|
||||||
j += 1;
|
re_compiled[j].type = UNUSED;
|
||||||
}
|
|
||||||
/* 'UNUSED' is a sentinel used to indicate end-of-pattern */
|
|
||||||
re_compiled[j].type = UNUSED;
|
|
||||||
|
|
||||||
re_p = (re_t)calloc(1, sizeof(re_compiled));
|
re_p = (re_t)calloc(1, sizeof(re_compiled));
|
||||||
memcpy (re_p, re_compiled, sizeof(re_compiled));
|
memcpy (re_p, re_compiled, sizeof(re_compiled));
|
||||||
|
|
||||||
return (re_t) re_p;
|
return (re_t) re_p;
|
||||||
}
|
}
|
||||||
|
|
||||||
void re_print (regex_t* pattern) {
|
void re_print (regex_t* pattern) {
|
||||||
|
|
||||||
const char* types[] = { "UNUSED", "DOT", "BEGIN", "END", "QUESTIONMARK", "STAR", "PLUS", "CHAR_TYPE", "CHAR_CLASS", "INV_CHAR_CLASS", "DIGIT", "NOT_DIGIT", "ALPHA", "NOT_ALPHA", "WHITESPACE" , "NOT_WHITESPACE", /* "BRANCH" */ };
|
const char* types[] = { "UNUSED", "DOT", "BEGIN", "END", "QUESTIONMARK", "STAR", "PLUS", "CHAR_TYPE", "CHAR_CLASS", "INV_CHAR_CLASS", "DIGIT", "NOT_DIGIT", "ALPHA", "NOT_ALPHA", "WHITESPACE" , "NOT_WHITESPACE", /* "BRANCH" */ };
|
||||||
int i;
|
int i;
|
||||||
int j;
|
int j;
|
||||||
char c;
|
char c;
|
||||||
|
|
||||||
for(i = 0; i < MAX_REGEXP_OBJECTS; ++i) {
|
for(i = 0; i < MAX_REGEXP_OBJECTS; ++i) {
|
||||||
if(pattern[i].type == UNUSED) {
|
if(pattern[i].type == UNUSED) {
|
||||||
break;
|
break;
|
||||||
}
|
|
||||||
|
|
||||||
printf("type: %s", types[pattern[i].type]);
|
|
||||||
if((pattern[i].type == CHAR_CLASS) || (pattern[i].type == INV_CHAR_CLASS)) {
|
|
||||||
printf(" [");
|
|
||||||
for(j = 0; j < MAX_CHAR_CLASS_LEN; ++j) {
|
|
||||||
c = pattern[i].ccl[j];
|
|
||||||
if((c == '\0') || (c == ']')) {
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
printf("%c", c);
|
|
||||||
}
|
printf("type: %s", types[pattern[i].type]);
|
||||||
printf("]");
|
if((pattern[i].type == CHAR_CLASS) || (pattern[i].type == INV_CHAR_CLASS)) {
|
||||||
} else if(pattern[i].type == CHAR_TYPE) {
|
printf(" [");
|
||||||
printf(" '%c'", pattern[i].ch);
|
for(j = 0; j < MAX_CHAR_CLASS_LEN; ++j) {
|
||||||
|
c = pattern[i].ccl[j];
|
||||||
|
if((c == '\0') || (c == ']')) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
printf("%c", c);
|
||||||
|
}
|
||||||
|
printf("]");
|
||||||
|
} else if(pattern[i].type == CHAR_TYPE) {
|
||||||
|
printf(" '%c'", pattern[i].ch);
|
||||||
|
}
|
||||||
|
printf("\n");
|
||||||
}
|
}
|
||||||
printf("\n");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -277,154 +277,154 @@ void re_print (regex_t* pattern) {
|
||||||
/* Private functions: */
|
/* Private functions: */
|
||||||
static int matchdigit (char c) {
|
static int matchdigit (char c) {
|
||||||
|
|
||||||
return ((c >= '0') && (c <= '9'));
|
return ((c >= '0') && (c <= '9'));
|
||||||
}
|
}
|
||||||
|
|
||||||
static int matchalpha (char c) {
|
static int matchalpha (char c) {
|
||||||
|
|
||||||
return ((c >= 'a') && (c <= 'z')) || ((c >= 'A') && (c <= 'Z'));
|
return ((c >= 'a') && (c <= 'z')) || ((c >= 'A') && (c <= 'Z'));
|
||||||
}
|
}
|
||||||
|
|
||||||
static int matchwhitespace (char c) {
|
static int matchwhitespace (char c) {
|
||||||
|
|
||||||
return ((c == ' ') || (c == '\t') || (c == '\n') || (c == '\r') || (c == '\f') || (c == '\v'));
|
return ((c == ' ') || (c == '\t') || (c == '\n') || (c == '\r') || (c == '\f') || (c == '\v'));
|
||||||
}
|
}
|
||||||
|
|
||||||
static int matchalphanum (char c) {
|
static int matchalphanum (char c) {
|
||||||
|
|
||||||
return ((c == '_') || matchalpha(c) || matchdigit(c));
|
return ((c == '_') || matchalpha(c) || matchdigit(c));
|
||||||
}
|
}
|
||||||
|
|
||||||
static int matchrange (char c, const char* str) {
|
static int matchrange (char c, const char* str) {
|
||||||
|
|
||||||
return ((c != '-') && (str[0] != '\0') && (str[0] != '-') &&
|
return ((c != '-') && (str[0] != '\0') && (str[0] != '-') &&
|
||||||
(str[1] == '-') && (str[1] != '\0') &&
|
(str[1] == '-') && (str[1] != '\0') &&
|
||||||
(str[2] != '\0') && ((c >= str[0]) && (c <= str[2])));
|
(str[2] != '\0') && ((c >= str[0]) && (c <= str[2])));
|
||||||
}
|
}
|
||||||
|
|
||||||
static int matchdot (char c) {
|
static int matchdot (char c) {
|
||||||
|
|
||||||
return ((c != '\n') && (c != '\r'));
|
return ((c != '\n') && (c != '\r'));
|
||||||
}
|
}
|
||||||
|
|
||||||
static int ismetachar (char c) {
|
static int ismetachar (char c) {
|
||||||
|
|
||||||
return ((c == 's') || (c == 'S') || (c == 'w') || (c == 'W') || (c == 'd') || (c == 'D'));
|
return ((c == 's') || (c == 'S') || (c == 'w') || (c == 'W') || (c == 'd') || (c == 'D'));
|
||||||
}
|
}
|
||||||
|
|
||||||
static int matchmetachar (char c, const char* str) {
|
static int matchmetachar (char c, const char* str) {
|
||||||
|
|
||||||
switch(str[0]) {
|
switch(str[0]) {
|
||||||
case 'd': return matchdigit(c);
|
case 'd': return matchdigit(c);
|
||||||
case 'D': return !matchdigit(c);
|
case 'D': return !matchdigit(c);
|
||||||
case 'w': return matchalphanum(c);
|
case 'w': return matchalphanum(c);
|
||||||
case 'W': return !matchalphanum(c);
|
case 'W': return !matchalphanum(c);
|
||||||
case 's': return matchwhitespace(c);
|
case 's': return matchwhitespace(c);
|
||||||
case 'S': return !matchwhitespace(c);
|
case 'S': return !matchwhitespace(c);
|
||||||
default: return (c == str[0]);
|
default: return (c == str[0]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static int matchcharclass (char c, const char* str) {
|
static int matchcharclass (char c, const char* str) {
|
||||||
|
|
||||||
do {
|
do {
|
||||||
if(matchrange(c, str)) {
|
if(matchrange(c, str)) {
|
||||||
return 1;
|
return 1;
|
||||||
} else if(str[0] == '\\') {
|
} else if(str[0] == '\\') {
|
||||||
/* Escape-char: increment str-ptr and match on next char */
|
/* Escape-char: increment str-ptr and match on next char */
|
||||||
str += 1;
|
str += 1;
|
||||||
if(matchmetachar(c, str)) {
|
if(matchmetachar(c, str)) {
|
||||||
return 1;
|
return 1;
|
||||||
} else if((c == str[0]) && !ismetachar(c)) {
|
} else if((c == str[0]) && !ismetachar(c)) {
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
} else if(c == str[0]) {
|
} else if(c == str[0]) {
|
||||||
if(c == '-') {
|
if(c == '-') {
|
||||||
return ((str[-1] == '\0') || (str[1] == '\0'));
|
return ((str[-1] == '\0') || (str[1] == '\0'));
|
||||||
} else {
|
} else {
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} while(*str++ != '\0');
|
} while(*str++ != '\0');
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int matchone (regex_t p, char c) {
|
static int matchone (regex_t p, char c) {
|
||||||
|
|
||||||
switch(p.type) {
|
switch(p.type) {
|
||||||
case DOT: return matchdot(c);
|
case DOT: return matchdot(c);
|
||||||
case CHAR_CLASS: return matchcharclass(c, (const char*)p.ccl);
|
case CHAR_CLASS: return matchcharclass(c, (const char*)p.ccl);
|
||||||
case INV_CHAR_CLASS: return !matchcharclass(c, (const char*)p.ccl);
|
case INV_CHAR_CLASS: return !matchcharclass(c, (const char*)p.ccl);
|
||||||
case DIGIT: return matchdigit(c);
|
case DIGIT: return matchdigit(c);
|
||||||
case NOT_DIGIT: return !matchdigit(c);
|
case NOT_DIGIT: return !matchdigit(c);
|
||||||
case ALPHA: return matchalphanum(c);
|
case ALPHA: return matchalphanum(c);
|
||||||
case NOT_ALPHA: return !matchalphanum(c);
|
case NOT_ALPHA: return !matchalphanum(c);
|
||||||
case WHITESPACE: return matchwhitespace(c);
|
case WHITESPACE: return matchwhitespace(c);
|
||||||
case NOT_WHITESPACE: return !matchwhitespace(c);
|
case NOT_WHITESPACE: return !matchwhitespace(c);
|
||||||
default: return (p.ch == c);
|
default: return (p.ch == c);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static int matchstar (regex_t p, regex_t* pattern, const char* text, int* matchlength) {
|
static int matchstar (regex_t p, regex_t* pattern, const char* text, int* matchlength) {
|
||||||
|
|
||||||
int prelen = *matchlength;
|
int prelen = *matchlength;
|
||||||
const char* prepoint = text;
|
const char* prepoint = text;
|
||||||
|
|
||||||
while((text[0] != '\0') && matchone(p, *text)) {
|
while((text[0] != '\0') && matchone(p, *text)) {
|
||||||
text++;
|
text++;
|
||||||
(*matchlength)++;
|
(*matchlength)++;
|
||||||
}
|
|
||||||
|
|
||||||
while(text >= prepoint) {
|
|
||||||
if(matchpattern(pattern, text--, matchlength)) {
|
|
||||||
return 1;
|
|
||||||
}
|
}
|
||||||
(*matchlength)--;
|
|
||||||
}
|
|
||||||
|
|
||||||
*matchlength = prelen;
|
while(text >= prepoint) {
|
||||||
|
if(matchpattern(pattern, text--, matchlength)) {
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
(*matchlength)--;
|
||||||
|
}
|
||||||
|
|
||||||
return 0;
|
*matchlength = prelen;
|
||||||
|
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int matchplus (regex_t p, regex_t* pattern, const char* text, int* matchlength) {
|
static int matchplus (regex_t p, regex_t* pattern, const char* text, int* matchlength) {
|
||||||
|
|
||||||
const char* prepoint = text;
|
const char* prepoint = text;
|
||||||
|
|
||||||
while((text[0] != '\0') && matchone(p, *text)) {
|
while((text[0] != '\0') && matchone(p, *text)) {
|
||||||
text++;
|
text++;
|
||||||
(*matchlength)++;
|
(*matchlength)++;
|
||||||
}
|
|
||||||
|
|
||||||
while(text > prepoint) {
|
|
||||||
if(matchpattern(pattern, text--, matchlength)) {
|
|
||||||
return 1;
|
|
||||||
}
|
}
|
||||||
(*matchlength)--;
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0;
|
while(text > prepoint) {
|
||||||
|
if(matchpattern(pattern, text--, matchlength)) {
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
(*matchlength)--;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int matchquestion (regex_t p, regex_t* pattern, const char* text, int* matchlength) {
|
static int matchquestion (regex_t p, regex_t* pattern, const char* text, int* matchlength) {
|
||||||
|
|
||||||
if(p.type == UNUSED) {
|
if(p.type == UNUSED) {
|
||||||
return 1;
|
return 1;
|
||||||
}
|
|
||||||
|
|
||||||
if(matchpattern(pattern, text, matchlength)) {
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
if(*text && matchone(p, *text++)) {
|
|
||||||
if(matchpattern(pattern, text, matchlength)) {
|
|
||||||
(*matchlength)++;
|
|
||||||
return 1;
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
return 0;
|
if(matchpattern(pattern, text, matchlength)) {
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(*text && matchone(p, *text++)) {
|
||||||
|
if(matchpattern(pattern, text, matchlength)) {
|
||||||
|
(*matchlength)++;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -433,23 +433,23 @@ static int matchquestion (regex_t p, regex_t* pattern, const char* text, int* ma
|
||||||
/* Recursive matching */
|
/* Recursive matching */
|
||||||
static int matchpattern (regex_t* pattern, const char* text, int *matchlength) {
|
static int matchpattern (regex_t* pattern, const char* text, int *matchlength) {
|
||||||
|
|
||||||
int pre = *matchlength;
|
int pre = *matchlength;
|
||||||
|
|
||||||
if((pattern[0].type == UNUSED) || (pattern[1].type == QUESTIONMARK)) {
|
if((pattern[0].type == UNUSED) || (pattern[1].type == QUESTIONMARK)) {
|
||||||
return matchquestion(pattern[1], &pattern[2], text, matchlength);
|
return matchquestion(pattern[1], &pattern[2], text, matchlength);
|
||||||
} else if(pattern[1].type == STAR) {
|
} else if(pattern[1].type == STAR) {
|
||||||
return matchstar(pattern[0], &pattern[2], text, matchlength);
|
return matchstar(pattern[0], &pattern[2], text, matchlength);
|
||||||
} else if(pattern[1].type == PLUS) {
|
} else if(pattern[1].type == PLUS) {
|
||||||
return matchplus(pattern[0], &pattern[2], text, matchlength);
|
return matchplus(pattern[0], &pattern[2], text, matchlength);
|
||||||
} else if((pattern[0].type == END) && pattern[1].type == UNUSED) {
|
} else if((pattern[0].type == END) && pattern[1].type == UNUSED) {
|
||||||
return text[0] == '\0';
|
return text[0] == '\0';
|
||||||
} else if((text[0] != '\0') && matchone(pattern[0], text[0])) {
|
} else if((text[0] != '\0') && matchone(pattern[0], text[0])) {
|
||||||
(*matchlength)++;
|
(*matchlength)++;
|
||||||
return matchpattern(&pattern[1], text+1);
|
return matchpattern(&pattern[1], text+1);
|
||||||
} else {
|
} else {
|
||||||
*matchlength = pre;
|
*matchlength = pre;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#else
|
#else
|
||||||
|
@ -457,30 +457,30 @@ static int matchpattern (regex_t* pattern, const char* text, int *matchlength) {
|
||||||
/* Iterative matching */
|
/* Iterative matching */
|
||||||
static int matchpattern (regex_t* pattern, const char* text, int* matchlength) {
|
static int matchpattern (regex_t* pattern, const char* text, int* matchlength) {
|
||||||
|
|
||||||
int pre = *matchlength;
|
int pre = *matchlength;
|
||||||
|
|
||||||
do {
|
do {
|
||||||
if((pattern[0].type == UNUSED) || (pattern[1].type == QUESTIONMARK)) {
|
if((pattern[0].type == UNUSED) || (pattern[1].type == QUESTIONMARK)) {
|
||||||
return matchquestion(pattern[0], &pattern[2], text, matchlength);
|
return matchquestion(pattern[0], &pattern[2], text, matchlength);
|
||||||
} else if(pattern[1].type == STAR) {
|
} else if(pattern[1].type == STAR) {
|
||||||
return matchstar(pattern[0], &pattern[2], text, matchlength);
|
return matchstar(pattern[0], &pattern[2], text, matchlength);
|
||||||
} else if(pattern[1].type == PLUS) {
|
} else if(pattern[1].type == PLUS) {
|
||||||
return matchplus(pattern[0], &pattern[2], text, matchlength);
|
return matchplus(pattern[0], &pattern[2], text, matchlength);
|
||||||
} else if((pattern[0].type == END) && pattern[1].type == UNUSED) {
|
} else if((pattern[0].type == END) && pattern[1].type == UNUSED) {
|
||||||
return (text[0] == '\0');
|
return (text[0] == '\0');
|
||||||
}
|
}
|
||||||
/* Branching is not working properly
|
/* Branching is not working properly
|
||||||
else if (pattern[1].type == BRANCH)
|
else if (pattern[1].type == BRANCH)
|
||||||
{
|
{
|
||||||
return (matchpattern(pattern, text) || matchpattern(&pattern[2], text));
|
return (matchpattern(pattern, text) || matchpattern(&pattern[2], text));
|
||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
(*matchlength)++;
|
(*matchlength)++;
|
||||||
} while((text[0] != '\0') && matchone(*pattern++, *text++));
|
} while((text[0] != '\0') && matchone(*pattern++, *text++));
|
||||||
|
|
||||||
*matchlength = pre;
|
*matchlength = pre;
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
File diff suppressed because it is too large
Load Diff
|
@ -8,7 +8,7 @@
|
||||||
*
|
*
|
||||||
* This program is distributed in the hope that it will be useful,
|
* This program is distributed in the hope that it will be useful,
|
||||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
* GNU General Public License for more details.
|
* GNU General Public License for more details.
|
||||||
*
|
*
|
||||||
* You should have received a copy of the GNU General Public License
|
* You should have received a copy of the GNU General Public License
|
||||||
|
@ -27,19 +27,19 @@ static int sn_selection_criterion_sort (peer_info_t *a, peer_info_t *b);
|
||||||
/* Initialize selection_criterion field in peer_info structure*/
|
/* Initialize selection_criterion field in peer_info structure*/
|
||||||
int sn_selection_criterion_init (peer_info_t *peer) {
|
int sn_selection_criterion_init (peer_info_t *peer) {
|
||||||
|
|
||||||
if(peer != NULL) {
|
if(peer != NULL) {
|
||||||
sn_selection_criterion_default(&(peer->selection_criterion));
|
sn_selection_criterion_default(&(peer->selection_criterion));
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0; /* OK */
|
return 0; /* OK */
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Set selection_criterion field to default value according to selected strategy. */
|
/* Set selection_criterion field to default value according to selected strategy. */
|
||||||
int sn_selection_criterion_default (SN_SELECTION_CRITERION_DATA_TYPE *selection_criterion) {
|
int sn_selection_criterion_default (SN_SELECTION_CRITERION_DATA_TYPE *selection_criterion) {
|
||||||
|
|
||||||
*selection_criterion = (SN_SELECTION_CRITERION_DATA_TYPE) UINT32_MAX >> 1;
|
*selection_criterion = (SN_SELECTION_CRITERION_DATA_TYPE) UINT32_MAX >> 1;
|
||||||
|
|
||||||
return 0; /* OK */
|
return 0; /* OK */
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Take data from PEER_INFO payload and transform them into a selection_criterion.
|
/* Take data from PEER_INFO payload and transform them into a selection_criterion.
|
||||||
|
@ -47,84 +47,84 @@ int sn_selection_criterion_default (SN_SELECTION_CRITERION_DATA_TYPE *selection_
|
||||||
*/
|
*/
|
||||||
int sn_selection_criterion_calculate (n2n_edge_t *eee, peer_info_t *peer, SN_SELECTION_CRITERION_DATA_TYPE *data) {
|
int sn_selection_criterion_calculate (n2n_edge_t *eee, peer_info_t *peer, SN_SELECTION_CRITERION_DATA_TYPE *data) {
|
||||||
|
|
||||||
SN_SELECTION_CRITERION_DATA_TYPE common_data;
|
SN_SELECTION_CRITERION_DATA_TYPE common_data;
|
||||||
int sum = 0;
|
int sum = 0;
|
||||||
|
|
||||||
common_data = sn_selection_criterion_common_read(eee);
|
common_data = sn_selection_criterion_common_read(eee);
|
||||||
peer->selection_criterion = (SN_SELECTION_CRITERION_DATA_TYPE)(be32toh(*data) + common_data);
|
peer->selection_criterion = (SN_SELECTION_CRITERION_DATA_TYPE)(be32toh(*data) + common_data);
|
||||||
|
|
||||||
/* Mitigation of the real supernode load in order to see less oscillations.
|
/* Mitigation of the real supernode load in order to see less oscillations.
|
||||||
* Edges jump from a supernode to another back and forth due to purging.
|
* Edges jump from a supernode to another back and forth due to purging.
|
||||||
* Because this behavior has a cost of switching, the real load is mitigated with a stickyness factor.
|
* Because this behavior has a cost of switching, the real load is mitigated with a stickyness factor.
|
||||||
* This factor is dynamically calculated basing on network size and prevent that unnecessary switching */
|
* This factor is dynamically calculated basing on network size and prevent that unnecessary switching */
|
||||||
if(peer == eee->curr_sn) {
|
if(peer == eee->curr_sn) {
|
||||||
sum = HASH_COUNT(eee->known_peers) + HASH_COUNT(eee->pending_peers);
|
sum = HASH_COUNT(eee->known_peers) + HASH_COUNT(eee->pending_peers);
|
||||||
peer->selection_criterion = peer->selection_criterion * sum / (sum + 1);
|
peer->selection_criterion = peer->selection_criterion * sum / (sum + 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0; /* OK */
|
return 0; /* OK */
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Set sn_selection_criterion_common_data field to default value. */
|
/* Set sn_selection_criterion_common_data field to default value. */
|
||||||
int sn_selection_criterion_common_data_default (n2n_edge_t *eee) {
|
int sn_selection_criterion_common_data_default (n2n_edge_t *eee) {
|
||||||
|
|
||||||
SN_SELECTION_CRITERION_DATA_TYPE tmp = 0;
|
SN_SELECTION_CRITERION_DATA_TYPE tmp = 0;
|
||||||
|
|
||||||
tmp = HASH_COUNT(eee->pending_peers);
|
tmp = HASH_COUNT(eee->pending_peers);
|
||||||
if(eee->conf.header_encryption == HEADER_ENCRYPTION_ENABLED) {
|
if(eee->conf.header_encryption == HEADER_ENCRYPTION_ENABLED) {
|
||||||
tmp *= 2;
|
tmp *= 2;
|
||||||
}
|
}
|
||||||
eee->sn_selection_criterion_common_data = tmp / HASH_COUNT(eee->conf.supernodes);
|
eee->sn_selection_criterion_common_data = tmp / HASH_COUNT(eee->conf.supernodes);
|
||||||
|
|
||||||
return 0; /* OK */
|
return 0; /* OK */
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Return the value of sn_selection_criterion_common_data field. */
|
/* Return the value of sn_selection_criterion_common_data field. */
|
||||||
static SN_SELECTION_CRITERION_DATA_TYPE sn_selection_criterion_common_read (n2n_edge_t *eee) {
|
static SN_SELECTION_CRITERION_DATA_TYPE sn_selection_criterion_common_read (n2n_edge_t *eee) {
|
||||||
|
|
||||||
return eee->sn_selection_criterion_common_data;
|
return eee->sn_selection_criterion_common_data;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Function that compare two selection_criterion fields and sorts them in ascending order. */
|
/* Function that compare two selection_criterion fields and sorts them in ascending order. */
|
||||||
static int sn_selection_criterion_sort (peer_info_t *a, peer_info_t *b) {
|
static int sn_selection_criterion_sort (peer_info_t *a, peer_info_t *b) {
|
||||||
|
|
||||||
// comparison function for sorting supernodes in ascending order of their selection_criterion.
|
// comparison function for sorting supernodes in ascending order of their selection_criterion.
|
||||||
return (a->selection_criterion - b->selection_criterion);
|
return (a->selection_criterion - b->selection_criterion);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Function that sorts peer_list using sn_selection_criterion_sort. */
|
/* Function that sorts peer_list using sn_selection_criterion_sort. */
|
||||||
int sn_selection_sort (peer_info_t **peer_list) {
|
int sn_selection_sort (peer_info_t **peer_list) {
|
||||||
|
|
||||||
HASH_SORT(*peer_list, sn_selection_criterion_sort);
|
HASH_SORT(*peer_list, sn_selection_criterion_sort);
|
||||||
|
|
||||||
return 0; /* OK */
|
return 0; /* OK */
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Function that gathers requested data on a supernode. */
|
/* Function that gathers requested data on a supernode. */
|
||||||
SN_SELECTION_CRITERION_DATA_TYPE sn_selection_criterion_gather_data (n2n_sn_t *sss) {
|
SN_SELECTION_CRITERION_DATA_TYPE sn_selection_criterion_gather_data (n2n_sn_t *sss) {
|
||||||
|
|
||||||
SN_SELECTION_CRITERION_DATA_TYPE data = 0, tmp = 0;
|
SN_SELECTION_CRITERION_DATA_TYPE data = 0, tmp = 0;
|
||||||
struct sn_community *comm, *tmp_comm;
|
struct sn_community *comm, *tmp_comm;
|
||||||
|
|
||||||
HASH_ITER(hh, sss->communities, comm, tmp_comm) {
|
HASH_ITER(hh, sss->communities, comm, tmp_comm) {
|
||||||
tmp = HASH_COUNT(comm->edges) + 1; /* number of nodes in the community + the community itself. */
|
tmp = HASH_COUNT(comm->edges) + 1; /* number of nodes in the community + the community itself. */
|
||||||
if(comm->header_encryption == HEADER_ENCRYPTION_ENABLED) { /*double-count encrypted communities (and their nodes): they exert more load on supernode. */
|
if(comm->header_encryption == HEADER_ENCRYPTION_ENABLED) { /*double-count encrypted communities (and their nodes): they exert more load on supernode. */
|
||||||
tmp *= 2;
|
tmp *= 2;
|
||||||
|
}
|
||||||
|
data += tmp;
|
||||||
}
|
}
|
||||||
data += tmp;
|
|
||||||
}
|
|
||||||
|
|
||||||
return htobe32(data);
|
return htobe32(data);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Convert selection_criterion field in a string for management port output. */
|
/* Convert selection_criterion field in a string for management port output. */
|
||||||
extern char * sn_selection_criterion_str (selection_criterion_str_t out, peer_info_t *peer) {
|
extern char * sn_selection_criterion_str (selection_criterion_str_t out, peer_info_t *peer) {
|
||||||
|
|
||||||
if(NULL == out) {
|
if(NULL == out) {
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
memset(out, 0, SN_SELECTION_CRITERION_BUF_SIZE);
|
memset(out, 0, SN_SELECTION_CRITERION_BUF_SIZE);
|
||||||
snprintf(out, SN_SELECTION_CRITERION_BUF_SIZE - 1, "ld = %d", (short int)(peer->selection_criterion));
|
snprintf(out, SN_SELECTION_CRITERION_BUF_SIZE - 1, "ld = %d", (short int)(peer->selection_criterion));
|
||||||
|
|
||||||
return out;
|
return out;
|
||||||
}
|
}
|
||||||
|
|
2764
src/sn_utils.c
2764
src/sn_utils.c
File diff suppressed because it is too large
Load Diff
598
src/wire.c
598
src/wire.c
|
@ -8,7 +8,7 @@
|
||||||
*
|
*
|
||||||
* This program is distributed in the hope that it will be useful,
|
* This program is distributed in the hope that it will be useful,
|
||||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
* GNU General Public License for more details.
|
* GNU General Public License for more details.
|
||||||
*
|
*
|
||||||
* You should have received a copy of the GNU General Public License
|
* You should have received a copy of the GNU General Public License
|
||||||
|
@ -33,10 +33,10 @@ int encode_uint8 (uint8_t * base,
|
||||||
size_t * idx,
|
size_t * idx,
|
||||||
const uint8_t v) {
|
const uint8_t v) {
|
||||||
|
|
||||||
*(base + (*idx)) = (v & 0xff);
|
*(base + (*idx)) = (v & 0xff);
|
||||||
++(*idx);
|
++(*idx);
|
||||||
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
int decode_uint8 (uint8_t * out,
|
int decode_uint8 (uint8_t * out,
|
||||||
|
@ -44,26 +44,26 @@ int decode_uint8 (uint8_t * out,
|
||||||
size_t * rem,
|
size_t * rem,
|
||||||
size_t * idx) {
|
size_t * idx) {
|
||||||
|
|
||||||
if(*rem < 1) {
|
if(*rem < 1) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
*out = ( base[*idx] & 0xff );
|
*out = ( base[*idx] & 0xff );
|
||||||
++(*idx);
|
++(*idx);
|
||||||
--(*rem);
|
--(*rem);
|
||||||
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
int encode_uint16 (uint8_t * base,
|
int encode_uint16 (uint8_t * base,
|
||||||
size_t * idx,
|
size_t * idx,
|
||||||
const uint16_t v) {
|
const uint16_t v) {
|
||||||
|
|
||||||
*(base + (*idx)) = ( v >> 8) & 0xff;
|
*(base + (*idx)) = ( v >> 8) & 0xff;
|
||||||
*(base + (1 + *idx)) = ( v & 0xff );
|
*(base + (1 + *idx)) = ( v & 0xff );
|
||||||
*idx += 2;
|
*idx += 2;
|
||||||
|
|
||||||
return 2;
|
return 2;
|
||||||
}
|
}
|
||||||
|
|
||||||
int decode_uint16 (uint16_t * out,
|
int decode_uint16 (uint16_t * out,
|
||||||
|
@ -71,29 +71,29 @@ int decode_uint16 (uint16_t * out,
|
||||||
size_t * rem,
|
size_t * rem,
|
||||||
size_t * idx) {
|
size_t * idx) {
|
||||||
|
|
||||||
if (*rem < 2) {
|
if (*rem < 2) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
*out = ( base[*idx] & 0xff ) << 8;
|
*out = ( base[*idx] & 0xff ) << 8;
|
||||||
*out |= ( base[1 + *idx] & 0xff );
|
*out |= ( base[1 + *idx] & 0xff );
|
||||||
*idx += 2;
|
*idx += 2;
|
||||||
*rem -= 2;
|
*rem -= 2;
|
||||||
|
|
||||||
return 2;
|
return 2;
|
||||||
}
|
}
|
||||||
|
|
||||||
int encode_uint32 (uint8_t * base,
|
int encode_uint32 (uint8_t * base,
|
||||||
size_t * idx,
|
size_t * idx,
|
||||||
const uint32_t v) {
|
const uint32_t v) {
|
||||||
|
|
||||||
*(base + (0 + *idx)) = ( v >> 24) & 0xff;
|
*(base + (0 + *idx)) = ( v >> 24) & 0xff;
|
||||||
*(base + (1 + *idx)) = ( v >> 16) & 0xff;
|
*(base + (1 + *idx)) = ( v >> 16) & 0xff;
|
||||||
*(base + (2 + *idx)) = ( v >> 8) & 0xff;
|
*(base + (2 + *idx)) = ( v >> 8) & 0xff;
|
||||||
*(base + (3 + *idx)) = ( v & 0xff );
|
*(base + (3 + *idx)) = ( v & 0xff );
|
||||||
*idx += 4;
|
*idx += 4;
|
||||||
|
|
||||||
return 4;
|
return 4;
|
||||||
}
|
}
|
||||||
|
|
||||||
int decode_uint32 (uint32_t * out,
|
int decode_uint32 (uint32_t * out,
|
||||||
|
@ -101,28 +101,28 @@ int decode_uint32 (uint32_t * out,
|
||||||
size_t * rem,
|
size_t * rem,
|
||||||
size_t * idx) {
|
size_t * idx) {
|
||||||
|
|
||||||
if (*rem < 4) {
|
if (*rem < 4) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
*out = ( base[0 + *idx] & 0xff ) << 24;
|
*out = ( base[0 + *idx] & 0xff ) << 24;
|
||||||
*out |= ( base[1 + *idx] & 0xff ) << 16;
|
*out |= ( base[1 + *idx] & 0xff ) << 16;
|
||||||
*out |= ( base[2 + *idx] & 0xff ) << 8;
|
*out |= ( base[2 + *idx] & 0xff ) << 8;
|
||||||
*out |= ( base[3 + *idx] & 0xff );
|
*out |= ( base[3 + *idx] & 0xff );
|
||||||
*idx += 4;
|
*idx += 4;
|
||||||
*rem -= 4;
|
*rem -= 4;
|
||||||
|
|
||||||
return 4;
|
return 4;
|
||||||
}
|
}
|
||||||
|
|
||||||
int encode_uint64 (uint8_t * base,
|
int encode_uint64 (uint8_t * base,
|
||||||
size_t * idx,
|
size_t * idx,
|
||||||
const uint64_t v) {
|
const uint64_t v) {
|
||||||
|
|
||||||
*(uint64_t*)(base + *idx) = htobe64(v);
|
*(uint64_t*)(base + *idx) = htobe64(v);
|
||||||
*idx += 8;
|
*idx += 8;
|
||||||
|
|
||||||
return 8;
|
return 8;
|
||||||
}
|
}
|
||||||
|
|
||||||
int decode_uint64 (uint64_t * out,
|
int decode_uint64 (uint64_t * out,
|
||||||
|
@ -130,15 +130,15 @@ int decode_uint64 (uint64_t * out,
|
||||||
size_t * rem,
|
size_t * rem,
|
||||||
size_t * idx) {
|
size_t * idx) {
|
||||||
|
|
||||||
if (*rem < 8) {
|
if (*rem < 8) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
*out = be64toh(*(uint64_t*)base + *idx);
|
*out = be64toh(*(uint64_t*)base + *idx);
|
||||||
*idx += 8;
|
*idx += 8;
|
||||||
*rem -= 8;
|
*rem -= 8;
|
||||||
|
|
||||||
return 8;
|
return 8;
|
||||||
}
|
}
|
||||||
|
|
||||||
int encode_buf (uint8_t * base,
|
int encode_buf (uint8_t * base,
|
||||||
|
@ -146,10 +146,10 @@ int encode_buf (uint8_t * base,
|
||||||
const void * p,
|
const void * p,
|
||||||
size_t s) {
|
size_t s) {
|
||||||
|
|
||||||
memcpy((base + (*idx)), p, s);
|
memcpy((base + (*idx)), p, s);
|
||||||
*idx += s;
|
*idx += s;
|
||||||
|
|
||||||
return s;
|
return s;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Copy from base to out of size bufsize */
|
/* Copy from base to out of size bufsize */
|
||||||
|
@ -159,15 +159,15 @@ int decode_buf (uint8_t * out,
|
||||||
size_t * rem,
|
size_t * rem,
|
||||||
size_t * idx) {
|
size_t * idx) {
|
||||||
|
|
||||||
if (*rem < bufsize) {
|
if (*rem < bufsize) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
memcpy(out, (base + *idx), bufsize);
|
memcpy(out, (base + *idx), bufsize);
|
||||||
*idx += bufsize;
|
*idx += bufsize;
|
||||||
*rem -= bufsize;
|
*rem -= bufsize;
|
||||||
|
|
||||||
return bufsize;
|
return bufsize;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -176,7 +176,7 @@ int encode_mac (uint8_t * base,
|
||||||
size_t * idx,
|
size_t * idx,
|
||||||
const n2n_mac_t m) {
|
const n2n_mac_t m) {
|
||||||
|
|
||||||
return encode_buf(base, idx, m, N2N_MAC_SIZE);
|
return encode_buf(base, idx, m, N2N_MAC_SIZE);
|
||||||
}
|
}
|
||||||
|
|
||||||
int decode_mac (uint8_t * out, /* of size N2N_MAC_SIZE. This clearer than passing a n2n_mac_t */
|
int decode_mac (uint8_t * out, /* of size N2N_MAC_SIZE. This clearer than passing a n2n_mac_t */
|
||||||
|
@ -184,7 +184,7 @@ int decode_mac (uint8_t * out, /* of size N2N_MAC_SIZE. This clearer than passin
|
||||||
size_t * rem,
|
size_t * rem,
|
||||||
size_t * idx) {
|
size_t * idx) {
|
||||||
|
|
||||||
return decode_buf(out, N2N_MAC_SIZE, base, rem, idx);
|
return decode_buf(out, N2N_MAC_SIZE, base, rem, idx);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -193,18 +193,18 @@ int encode_common (uint8_t * base,
|
||||||
size_t * idx,
|
size_t * idx,
|
||||||
const n2n_common_t * common) {
|
const n2n_common_t * common) {
|
||||||
|
|
||||||
uint16_t flags = 0;
|
uint16_t flags = 0;
|
||||||
|
|
||||||
encode_uint8(base, idx, N2N_PKT_VERSION);
|
encode_uint8(base, idx, N2N_PKT_VERSION);
|
||||||
encode_uint8(base, idx, common->ttl);
|
encode_uint8(base, idx, common->ttl);
|
||||||
|
|
||||||
flags = common->pc & N2N_FLAGS_TYPE_MASK;
|
flags = common->pc & N2N_FLAGS_TYPE_MASK;
|
||||||
flags |= common->flags & N2N_FLAGS_BITS_MASK;
|
flags |= common->flags & N2N_FLAGS_BITS_MASK;
|
||||||
|
|
||||||
encode_uint16(base, idx, flags);
|
encode_uint16(base, idx, flags);
|
||||||
encode_buf(base, idx, common->community, N2N_COMMUNITY_SIZE);
|
encode_buf(base, idx, common->community, N2N_COMMUNITY_SIZE);
|
||||||
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
int decode_common (n2n_common_t * out,
|
int decode_common (n2n_common_t * out,
|
||||||
|
@ -212,23 +212,23 @@ int decode_common (n2n_common_t * out,
|
||||||
size_t * rem,
|
size_t * rem,
|
||||||
size_t * idx) {
|
size_t * idx) {
|
||||||
|
|
||||||
size_t idx0 = *idx;
|
size_t idx0 = *idx;
|
||||||
uint8_t dummy = 0;
|
uint8_t dummy = 0;
|
||||||
|
|
||||||
decode_uint8(&dummy, base, rem, idx);
|
decode_uint8(&dummy, base, rem, idx);
|
||||||
|
|
||||||
if (N2N_PKT_VERSION != dummy) {
|
if(N2N_PKT_VERSION != dummy) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
decode_uint8(&(out->ttl), base, rem, idx);
|
decode_uint8(&(out->ttl), base, rem, idx);
|
||||||
decode_uint16(&(out->flags), base, rem, idx);
|
decode_uint16(&(out->flags), base, rem, idx);
|
||||||
out->pc = ( out->flags & N2N_FLAGS_TYPE_MASK );
|
out->pc = (out->flags & N2N_FLAGS_TYPE_MASK);
|
||||||
out->flags &= N2N_FLAGS_BITS_MASK;
|
out->flags &= N2N_FLAGS_BITS_MASK;
|
||||||
|
|
||||||
decode_buf(out->community, N2N_COMMUNITY_SIZE, base, rem, idx);
|
decode_buf(out->community, N2N_COMMUNITY_SIZE, base, rem, idx);
|
||||||
|
|
||||||
return (*idx - idx0);
|
return (*idx - idx0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -236,31 +236,31 @@ int encode_sock (uint8_t * base,
|
||||||
size_t * idx,
|
size_t * idx,
|
||||||
const n2n_sock_t * sock) {
|
const n2n_sock_t * sock) {
|
||||||
|
|
||||||
int retval = 0;
|
int retval = 0;
|
||||||
uint16_t f;
|
uint16_t f;
|
||||||
|
|
||||||
switch(sock->family) {
|
switch(sock->family) {
|
||||||
case AF_INET: {
|
case AF_INET: {
|
||||||
f = 0;
|
f = 0;
|
||||||
retval += encode_uint16(base, idx, f);
|
retval += encode_uint16(base, idx, f);
|
||||||
retval += encode_uint16(base, idx, sock->port);
|
retval += encode_uint16(base, idx, sock->port);
|
||||||
retval += encode_buf(base, idx, sock->addr.v4, IPV4_SIZE);
|
retval += encode_buf(base, idx, sock->addr.v4, IPV4_SIZE);
|
||||||
break;
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
case AF_INET6: {
|
||||||
|
f = 0x8000;
|
||||||
|
retval += encode_uint16(base, idx, f);
|
||||||
|
retval += encode_uint16(base, idx, sock->port);
|
||||||
|
retval += encode_buf(base, idx, sock->addr.v6, IPV6_SIZE);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
default:
|
||||||
|
retval = -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
case AF_INET6: {
|
return retval;
|
||||||
f = 0x8000;
|
|
||||||
retval += encode_uint16(base, idx, f);
|
|
||||||
retval += encode_uint16(base, idx, sock->port);
|
|
||||||
retval += encode_buf(base, idx, sock->addr.v6, IPV6_SIZE);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
default:
|
|
||||||
retval = -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
return retval;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -269,26 +269,26 @@ int decode_sock (n2n_sock_t * sock,
|
||||||
size_t * rem,
|
size_t * rem,
|
||||||
size_t * idx) {
|
size_t * idx) {
|
||||||
|
|
||||||
size_t * idx0 = idx;
|
size_t * idx0 = idx;
|
||||||
uint16_t f = 0;
|
uint16_t f = 0;
|
||||||
|
|
||||||
decode_uint16(&f, base, rem, idx);
|
decode_uint16(&f, base, rem, idx);
|
||||||
|
|
||||||
if(f & 0x8000) {
|
if(f & 0x8000) {
|
||||||
|
|
||||||
/* IPv6 */
|
/* IPv6 */
|
||||||
sock->family = AF_INET6;
|
sock->family = AF_INET6;
|
||||||
decode_uint16(&(sock->port), base, rem, idx);
|
decode_uint16(&(sock->port), base, rem, idx);
|
||||||
decode_buf(sock->addr.v6, IPV6_SIZE, base, rem, idx);
|
decode_buf(sock->addr.v6, IPV6_SIZE, base, rem, idx);
|
||||||
} else {
|
} else {
|
||||||
/* IPv4 */
|
/* IPv4 */
|
||||||
sock->family = AF_INET;
|
sock->family = AF_INET;
|
||||||
decode_uint16(&(sock->port), base, rem, idx);
|
decode_uint16(&(sock->port), base, rem, idx);
|
||||||
memset(sock->addr.v6, 0, IPV6_SIZE); /* so memcmp() works for equality. */
|
memset(sock->addr.v6, 0, IPV6_SIZE); /* so memcmp() works for equality. */
|
||||||
decode_buf(sock->addr.v4, IPV4_SIZE, base, rem, idx);
|
decode_buf(sock->addr.v4, IPV4_SIZE, base, rem, idx);
|
||||||
}
|
}
|
||||||
|
|
||||||
return (idx-idx0);
|
return (idx - idx0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -297,20 +297,20 @@ int encode_REGISTER (uint8_t *base,
|
||||||
const n2n_common_t *common,
|
const n2n_common_t *common,
|
||||||
const n2n_REGISTER_t *reg) {
|
const n2n_REGISTER_t *reg) {
|
||||||
|
|
||||||
int retval = 0;
|
int retval = 0;
|
||||||
|
|
||||||
retval += encode_common(base, idx, common);
|
retval += encode_common(base, idx, common);
|
||||||
retval += encode_buf(base, idx, reg->cookie, N2N_COOKIE_SIZE);
|
retval += encode_buf(base, idx, reg->cookie, N2N_COOKIE_SIZE);
|
||||||
retval += encode_mac(base, idx, reg->srcMac);
|
retval += encode_mac(base, idx, reg->srcMac);
|
||||||
retval += encode_mac(base, idx, reg->dstMac);
|
retval += encode_mac(base, idx, reg->dstMac);
|
||||||
if(0 != reg->sock.family) {
|
if(0 != reg->sock.family) {
|
||||||
retval += encode_sock(base, idx, &(reg->sock));
|
retval += encode_sock(base, idx, &(reg->sock));
|
||||||
}
|
}
|
||||||
retval += encode_uint32(base, idx, reg->dev_addr.net_addr);
|
retval += encode_uint32(base, idx, reg->dev_addr.net_addr);
|
||||||
retval += encode_uint8(base, idx, reg->dev_addr.net_bitlen);
|
retval += encode_uint8(base, idx, reg->dev_addr.net_bitlen);
|
||||||
retval += encode_buf(base, idx, reg->dev_desc, N2N_DESC_SIZE);
|
retval += encode_buf(base, idx, reg->dev_desc, N2N_DESC_SIZE);
|
||||||
|
|
||||||
return retval;
|
return retval;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -320,20 +320,20 @@ int decode_REGISTER (n2n_REGISTER_t *reg,
|
||||||
size_t *rem,
|
size_t *rem,
|
||||||
size_t *idx) {
|
size_t *idx) {
|
||||||
|
|
||||||
size_t retval = 0;
|
size_t retval = 0;
|
||||||
memset(reg, 0, sizeof(n2n_REGISTER_t));
|
memset(reg, 0, sizeof(n2n_REGISTER_t));
|
||||||
|
|
||||||
retval += decode_buf(reg->cookie, N2N_COOKIE_SIZE, base, rem, idx);
|
retval += decode_buf(reg->cookie, N2N_COOKIE_SIZE, base, rem, idx);
|
||||||
retval += decode_mac(reg->srcMac, base, rem, idx);
|
retval += decode_mac(reg->srcMac, base, rem, idx);
|
||||||
retval += decode_mac(reg->dstMac, base, rem, idx);
|
retval += decode_mac(reg->dstMac, base, rem, idx);
|
||||||
if(cmn->flags & N2N_FLAGS_SOCKET) {
|
if(cmn->flags & N2N_FLAGS_SOCKET) {
|
||||||
retval += decode_sock(&(reg->sock), base, rem, idx);
|
retval += decode_sock(&(reg->sock), base, rem, idx);
|
||||||
}
|
}
|
||||||
retval += decode_uint32(&(reg->dev_addr.net_addr), base, rem, idx);
|
retval += decode_uint32(&(reg->dev_addr.net_addr), base, rem, idx);
|
||||||
retval += decode_uint8(&(reg->dev_addr.net_bitlen), base, rem, idx);
|
retval += decode_uint8(&(reg->dev_addr.net_bitlen), base, rem, idx);
|
||||||
retval += decode_buf(reg->dev_desc, N2N_DESC_SIZE, base, rem, idx);
|
retval += decode_buf(reg->dev_desc, N2N_DESC_SIZE, base, rem, idx);
|
||||||
|
|
||||||
return retval;
|
return retval;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -342,22 +342,22 @@ int encode_REGISTER_SUPER (uint8_t *base,
|
||||||
const n2n_common_t *common,
|
const n2n_common_t *common,
|
||||||
const n2n_REGISTER_SUPER_t *reg) {
|
const n2n_REGISTER_SUPER_t *reg) {
|
||||||
|
|
||||||
int retval = 0;
|
int retval = 0;
|
||||||
|
|
||||||
retval += encode_common(base, idx, common);
|
retval += encode_common(base, idx, common);
|
||||||
retval += encode_buf(base, idx, reg->cookie, N2N_COOKIE_SIZE);
|
retval += encode_buf(base, idx, reg->cookie, N2N_COOKIE_SIZE);
|
||||||
retval += encode_mac(base, idx, reg->edgeMac);
|
retval += encode_mac(base, idx, reg->edgeMac);
|
||||||
if(0 != reg->sock.family) {
|
if(0 != reg->sock.family) {
|
||||||
retval += encode_sock(base, idx, &(reg->sock));
|
retval += encode_sock(base, idx, &(reg->sock));
|
||||||
}
|
}
|
||||||
retval += encode_uint32(base, idx, reg->dev_addr.net_addr);
|
retval += encode_uint32(base, idx, reg->dev_addr.net_addr);
|
||||||
retval += encode_uint8(base, idx, reg->dev_addr.net_bitlen);
|
retval += encode_uint8(base, idx, reg->dev_addr.net_bitlen);
|
||||||
retval += encode_buf(base, idx, reg->dev_desc, N2N_DESC_SIZE);
|
retval += encode_buf(base, idx, reg->dev_desc, N2N_DESC_SIZE);
|
||||||
retval += encode_uint16(base, idx, reg->auth.scheme);
|
retval += encode_uint16(base, idx, reg->auth.scheme);
|
||||||
retval += encode_uint16(base, idx, reg->auth.toksize);
|
retval += encode_uint16(base, idx, reg->auth.toksize);
|
||||||
retval += encode_buf(base, idx, reg->auth.token, reg->auth.toksize);
|
retval += encode_buf(base, idx, reg->auth.token, reg->auth.toksize);
|
||||||
|
|
||||||
return retval;
|
return retval;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -367,22 +367,22 @@ int decode_REGISTER_SUPER (n2n_REGISTER_SUPER_t *reg,
|
||||||
size_t *rem,
|
size_t *rem,
|
||||||
size_t *idx) {
|
size_t *idx) {
|
||||||
|
|
||||||
size_t retval = 0;
|
size_t retval = 0;
|
||||||
memset(reg, 0, sizeof(n2n_REGISTER_SUPER_t));
|
memset(reg, 0, sizeof(n2n_REGISTER_SUPER_t));
|
||||||
|
|
||||||
retval += decode_buf(reg->cookie, N2N_COOKIE_SIZE, base, rem, idx);
|
retval += decode_buf(reg->cookie, N2N_COOKIE_SIZE, base, rem, idx);
|
||||||
retval += decode_mac(reg->edgeMac, base, rem, idx);
|
retval += decode_mac(reg->edgeMac, base, rem, idx);
|
||||||
if(cmn->flags & N2N_FLAGS_SOCKET) {
|
if(cmn->flags & N2N_FLAGS_SOCKET) {
|
||||||
retval += decode_sock(&(reg->sock), base, rem, idx);
|
retval += decode_sock(&(reg->sock), base, rem, idx);
|
||||||
}
|
}
|
||||||
retval += decode_uint32(&(reg->dev_addr.net_addr), base, rem, idx);
|
retval += decode_uint32(&(reg->dev_addr.net_addr), base, rem, idx);
|
||||||
retval += decode_uint8(&(reg->dev_addr.net_bitlen), base, rem, idx);
|
retval += decode_uint8(&(reg->dev_addr.net_bitlen), base, rem, idx);
|
||||||
retval += decode_buf(reg->dev_desc, N2N_DESC_SIZE, base, rem, idx);
|
retval += decode_buf(reg->dev_desc, N2N_DESC_SIZE, base, rem, idx);
|
||||||
retval += decode_uint16(&(reg->auth.scheme), base, rem, idx);
|
retval += decode_uint16(&(reg->auth.scheme), base, rem, idx);
|
||||||
retval += decode_uint16(&(reg->auth.toksize), base, rem, idx);
|
retval += decode_uint16(&(reg->auth.toksize), base, rem, idx);
|
||||||
retval += decode_buf(reg->auth.token, reg->auth.toksize, base, rem, idx);
|
retval += decode_buf(reg->auth.token, reg->auth.toksize, base, rem, idx);
|
||||||
|
|
||||||
return retval;
|
return retval;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -391,15 +391,15 @@ int encode_UNREGISTER_SUPER (uint8_t *base,
|
||||||
const n2n_common_t *common,
|
const n2n_common_t *common,
|
||||||
const n2n_UNREGISTER_SUPER_t *unreg) {
|
const n2n_UNREGISTER_SUPER_t *unreg) {
|
||||||
|
|
||||||
int retval = 0;
|
int retval = 0;
|
||||||
|
|
||||||
retval += encode_common(base, idx, common);
|
retval += encode_common(base, idx, common);
|
||||||
retval += encode_uint16(base, idx, unreg->auth.scheme);
|
retval += encode_uint16(base, idx, unreg->auth.scheme);
|
||||||
retval += encode_uint16(base, idx, unreg->auth.toksize);
|
retval += encode_uint16(base, idx, unreg->auth.toksize);
|
||||||
retval += encode_buf(base, idx, unreg->auth.token, unreg->auth.toksize);
|
retval += encode_buf(base, idx, unreg->auth.token, unreg->auth.toksize);
|
||||||
retval += encode_mac(base, idx, unreg->srcMac);
|
retval += encode_mac(base, idx, unreg->srcMac);
|
||||||
|
|
||||||
return retval;
|
return retval;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -409,15 +409,15 @@ int decode_UNREGISTER_SUPER (n2n_UNREGISTER_SUPER_t *unreg,
|
||||||
size_t *rem,
|
size_t *rem,
|
||||||
size_t *idx) {
|
size_t *idx) {
|
||||||
|
|
||||||
size_t retval = 0;
|
size_t retval = 0;
|
||||||
memset(unreg, 0, sizeof(n2n_UNREGISTER_SUPER_t));
|
memset(unreg, 0, sizeof(n2n_UNREGISTER_SUPER_t));
|
||||||
|
|
||||||
retval += decode_uint16(&(unreg->auth.scheme), base, rem, idx);
|
retval += decode_uint16(&(unreg->auth.scheme), base, rem, idx);
|
||||||
retval += decode_uint16(&(unreg->auth.toksize), base, rem, idx);
|
retval += decode_uint16(&(unreg->auth.toksize), base, rem, idx);
|
||||||
retval += decode_buf(unreg->auth.token, unreg->auth.toksize, base, rem, idx);
|
retval += decode_buf(unreg->auth.token, unreg->auth.toksize, base, rem, idx);
|
||||||
retval += decode_mac(unreg->srcMac, base, rem, idx);
|
retval += decode_mac(unreg->srcMac, base, rem, idx);
|
||||||
|
|
||||||
return retval;
|
return retval;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -426,21 +426,21 @@ int encode_REGISTER_ACK (uint8_t *base,
|
||||||
const n2n_common_t *common,
|
const n2n_common_t *common,
|
||||||
const n2n_REGISTER_ACK_t *reg) {
|
const n2n_REGISTER_ACK_t *reg) {
|
||||||
|
|
||||||
int retval = 0;
|
int retval = 0;
|
||||||
|
|
||||||
retval += encode_common(base, idx, common);
|
retval += encode_common(base, idx, common);
|
||||||
retval += encode_buf(base, idx, reg->cookie, N2N_COOKIE_SIZE);
|
retval += encode_buf(base, idx, reg->cookie, N2N_COOKIE_SIZE);
|
||||||
retval += encode_mac(base, idx, reg->dstMac);
|
retval += encode_mac(base, idx, reg->dstMac);
|
||||||
retval += encode_mac(base, idx, reg->srcMac);
|
retval += encode_mac(base, idx, reg->srcMac);
|
||||||
|
|
||||||
/* The socket in REGISTER_ACK is the socket from which the REGISTER
|
/* The socket in REGISTER_ACK is the socket from which the REGISTER
|
||||||
* arrived. This is sent back to the sender so it knows what its public
|
* arrived. This is sent back to the sender so it knows what its public
|
||||||
* socket is. */
|
* socket is. */
|
||||||
if(0 != reg->sock.family) {
|
if(0 != reg->sock.family) {
|
||||||
retval += encode_sock(base, idx, &(reg->sock));
|
retval += encode_sock(base, idx, &(reg->sock));
|
||||||
}
|
}
|
||||||
|
|
||||||
return retval;
|
return retval;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -450,21 +450,21 @@ int decode_REGISTER_ACK (n2n_REGISTER_ACK_t *reg,
|
||||||
size_t *rem,
|
size_t *rem,
|
||||||
size_t *idx) {
|
size_t *idx) {
|
||||||
|
|
||||||
size_t retval = 0;
|
size_t retval = 0;
|
||||||
memset(reg, 0, sizeof(n2n_REGISTER_ACK_t));
|
memset(reg, 0, sizeof(n2n_REGISTER_ACK_t));
|
||||||
|
|
||||||
retval += decode_buf(reg->cookie, N2N_COOKIE_SIZE, base, rem, idx);
|
retval += decode_buf(reg->cookie, N2N_COOKIE_SIZE, base, rem, idx);
|
||||||
retval += decode_mac(reg->dstMac, base, rem, idx);
|
retval += decode_mac(reg->dstMac, base, rem, idx);
|
||||||
retval += decode_mac(reg->srcMac, base, rem, idx);
|
retval += decode_mac(reg->srcMac, base, rem, idx);
|
||||||
|
|
||||||
/* The socket in REGISTER_ACK is the socket from which the REGISTER
|
/* The socket in REGISTER_ACK is the socket from which the REGISTER
|
||||||
* arrived. This is sent back to the sender so it knows what its public
|
* arrived. This is sent back to the sender so it knows what its public
|
||||||
* socket is. */
|
* socket is. */
|
||||||
if(cmn->flags & N2N_FLAGS_SOCKET) {
|
if(cmn->flags & N2N_FLAGS_SOCKET) {
|
||||||
retval += decode_sock(&(reg->sock), base, rem, idx);
|
retval += decode_sock(&(reg->sock), base, rem, idx);
|
||||||
}
|
}
|
||||||
|
|
||||||
return retval;
|
return retval;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -474,19 +474,19 @@ int encode_REGISTER_SUPER_ACK (uint8_t *base,
|
||||||
const n2n_REGISTER_SUPER_ACK_t *reg,
|
const n2n_REGISTER_SUPER_ACK_t *reg,
|
||||||
uint8_t *tmpbuf) {
|
uint8_t *tmpbuf) {
|
||||||
|
|
||||||
int retval = 0;
|
int retval = 0;
|
||||||
|
|
||||||
retval += encode_common(base, idx, common);
|
retval += encode_common(base, idx, common);
|
||||||
retval += encode_buf(base, idx, reg->cookie, N2N_COOKIE_SIZE);
|
retval += encode_buf(base, idx, reg->cookie, N2N_COOKIE_SIZE);
|
||||||
retval += encode_mac(base, idx, reg->edgeMac);
|
retval += encode_mac(base, idx, reg->edgeMac);
|
||||||
retval += encode_uint32(base, idx, reg->dev_addr.net_addr);
|
retval += encode_uint32(base, idx, reg->dev_addr.net_addr);
|
||||||
retval += encode_uint8(base, idx, reg->dev_addr.net_bitlen);
|
retval += encode_uint8(base, idx, reg->dev_addr.net_bitlen);
|
||||||
retval += encode_uint16(base, idx, reg->lifetime);
|
retval += encode_uint16(base, idx, reg->lifetime);
|
||||||
retval += encode_sock(base, idx, &(reg->sock));
|
retval += encode_sock(base, idx, &(reg->sock));
|
||||||
retval += encode_uint8(base, idx, reg->num_sn);
|
retval += encode_uint8(base, idx, reg->num_sn);
|
||||||
retval += encode_buf(base, idx, tmpbuf, (reg->num_sn*REG_SUPER_ACK_PAYLOAD_ENTRY_SIZE));
|
retval += encode_buf(base, idx, tmpbuf, (reg->num_sn*REG_SUPER_ACK_PAYLOAD_ENTRY_SIZE));
|
||||||
|
|
||||||
return retval;
|
return retval;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -497,23 +497,23 @@ int decode_REGISTER_SUPER_ACK (n2n_REGISTER_SUPER_ACK_t *reg,
|
||||||
size_t *idx,
|
size_t *idx,
|
||||||
uint8_t *tmpbuf) {
|
uint8_t *tmpbuf) {
|
||||||
|
|
||||||
size_t retval = 0;
|
size_t retval = 0;
|
||||||
memset(reg, 0, sizeof(n2n_REGISTER_SUPER_ACK_t));
|
memset(reg, 0, sizeof(n2n_REGISTER_SUPER_ACK_t));
|
||||||
|
|
||||||
retval += decode_buf(reg->cookie, N2N_COOKIE_SIZE, base, rem, idx);
|
retval += decode_buf(reg->cookie, N2N_COOKIE_SIZE, base, rem, idx);
|
||||||
retval += decode_mac(reg->edgeMac, base, rem, idx);
|
retval += decode_mac(reg->edgeMac, base, rem, idx);
|
||||||
retval += decode_uint32(&(reg->dev_addr.net_addr), base, rem, idx);
|
retval += decode_uint32(&(reg->dev_addr.net_addr), base, rem, idx);
|
||||||
retval += decode_uint8(&(reg->dev_addr.net_bitlen), base, rem, idx);
|
retval += decode_uint8(&(reg->dev_addr.net_bitlen), base, rem, idx);
|
||||||
retval += decode_uint16(&(reg->lifetime), base, rem, idx);
|
retval += decode_uint16(&(reg->lifetime), base, rem, idx);
|
||||||
|
|
||||||
/* Socket is mandatory in this message type */
|
/* Socket is mandatory in this message type */
|
||||||
retval += decode_sock(&(reg->sock), base, rem, idx);
|
retval += decode_sock(&(reg->sock), base, rem, idx);
|
||||||
|
|
||||||
/* Following the edge socket are an array of backup supernodes. */
|
/* Following the edge socket are an array of backup supernodes. */
|
||||||
retval += decode_uint8(&(reg->num_sn), base, rem, idx);
|
retval += decode_uint8(&(reg->num_sn), base, rem, idx);
|
||||||
retval += decode_buf(tmpbuf, (reg->num_sn*REG_SUPER_ACK_PAYLOAD_ENTRY_SIZE), base, rem, idx);
|
retval += decode_buf(tmpbuf, (reg->num_sn * REG_SUPER_ACK_PAYLOAD_ENTRY_SIZE), base, rem, idx);
|
||||||
|
|
||||||
return retval;
|
return retval;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -522,13 +522,13 @@ int encode_REGISTER_SUPER_NAK (uint8_t *base,
|
||||||
const n2n_common_t *common,
|
const n2n_common_t *common,
|
||||||
const n2n_REGISTER_SUPER_NAK_t *nak) {
|
const n2n_REGISTER_SUPER_NAK_t *nak) {
|
||||||
|
|
||||||
int retval = 0;
|
int retval = 0;
|
||||||
|
|
||||||
retval += encode_common(base, idx, common);
|
retval += encode_common(base, idx, common);
|
||||||
retval += encode_buf(base, idx, nak->cookie, N2N_COOKIE_SIZE);
|
retval += encode_buf(base, idx, nak->cookie, N2N_COOKIE_SIZE);
|
||||||
retval += encode_mac(base, idx, nak->srcMac);
|
retval += encode_mac(base, idx, nak->srcMac);
|
||||||
|
|
||||||
return retval;
|
return retval;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -538,13 +538,13 @@ int decode_REGISTER_SUPER_NAK (n2n_REGISTER_SUPER_NAK_t *nak,
|
||||||
size_t *rem,
|
size_t *rem,
|
||||||
size_t *idx) {
|
size_t *idx) {
|
||||||
|
|
||||||
size_t retval = 0;
|
size_t retval = 0;
|
||||||
memset(nak, 0, sizeof(n2n_REGISTER_SUPER_NAK_t));
|
memset(nak, 0, sizeof(n2n_REGISTER_SUPER_NAK_t));
|
||||||
|
|
||||||
retval += decode_buf(nak->cookie, N2N_COOKIE_SIZE, base, rem, idx);
|
retval += decode_buf(nak->cookie, N2N_COOKIE_SIZE, base, rem, idx);
|
||||||
retval += decode_mac(nak->srcMac, base, rem, idx);
|
retval += decode_mac(nak->srcMac, base, rem, idx);
|
||||||
|
|
||||||
return retval;
|
return retval;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -552,19 +552,19 @@ int fill_sockaddr (struct sockaddr * addr,
|
||||||
size_t addrlen,
|
size_t addrlen,
|
||||||
const n2n_sock_t * sock) {
|
const n2n_sock_t * sock) {
|
||||||
|
|
||||||
int retval = -1;
|
int retval = -1;
|
||||||
|
|
||||||
if(AF_INET == sock->family) {
|
if(AF_INET == sock->family) {
|
||||||
if(addrlen >= sizeof(struct sockaddr_in)) {
|
if(addrlen >= sizeof(struct sockaddr_in)) {
|
||||||
struct sockaddr_in * si = (struct sockaddr_in *)addr;
|
struct sockaddr_in * si = (struct sockaddr_in *)addr;
|
||||||
si->sin_family = sock->family;
|
si->sin_family = sock->family;
|
||||||
si->sin_port = htons(sock->port);
|
si->sin_port = htons(sock->port);
|
||||||
memcpy(&(si->sin_addr.s_addr), sock->addr.v4, IPV4_SIZE);
|
memcpy(&(si->sin_addr.s_addr), sock->addr.v4, IPV4_SIZE);
|
||||||
retval = 0;
|
retval = 0;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
return retval;
|
return retval;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -573,18 +573,18 @@ int encode_PACKET (uint8_t * base,
|
||||||
const n2n_common_t * common,
|
const n2n_common_t * common,
|
||||||
const n2n_PACKET_t * pkt) {
|
const n2n_PACKET_t * pkt) {
|
||||||
|
|
||||||
int retval = 0;
|
int retval = 0;
|
||||||
|
|
||||||
retval += encode_common(base, idx, common);
|
retval += encode_common(base, idx, common);
|
||||||
retval += encode_mac(base, idx, pkt->srcMac);
|
retval += encode_mac(base, idx, pkt->srcMac);
|
||||||
retval += encode_mac(base, idx, pkt->dstMac);
|
retval += encode_mac(base, idx, pkt->dstMac);
|
||||||
if(0 != pkt->sock.family) {
|
if(0 != pkt->sock.family) {
|
||||||
retval += encode_sock(base, idx, &(pkt->sock));
|
retval += encode_sock(base, idx, &(pkt->sock));
|
||||||
}
|
}
|
||||||
retval += encode_uint8(base, idx, pkt->compression);
|
retval += encode_uint8(base, idx, pkt->compression);
|
||||||
retval += encode_uint8(base, idx, pkt->transform);
|
retval += encode_uint8(base, idx, pkt->transform);
|
||||||
|
|
||||||
return retval;
|
return retval;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -594,20 +594,20 @@ int decode_PACKET (n2n_PACKET_t * pkt,
|
||||||
size_t * rem,
|
size_t * rem,
|
||||||
size_t * idx) {
|
size_t * idx) {
|
||||||
|
|
||||||
size_t retval = 0;
|
size_t retval = 0;
|
||||||
memset(pkt, 0, sizeof(n2n_PACKET_t));
|
memset(pkt, 0, sizeof(n2n_PACKET_t));
|
||||||
|
|
||||||
retval += decode_mac(pkt->srcMac, base, rem, idx);
|
retval += decode_mac(pkt->srcMac, base, rem, idx);
|
||||||
retval += decode_mac(pkt->dstMac, base, rem, idx);
|
retval += decode_mac(pkt->dstMac, base, rem, idx);
|
||||||
|
|
||||||
if(cmn->flags & N2N_FLAGS_SOCKET) {
|
if(cmn->flags & N2N_FLAGS_SOCKET) {
|
||||||
retval += decode_sock(&(pkt->sock), base, rem, idx);
|
retval += decode_sock(&(pkt->sock), base, rem, idx);
|
||||||
}
|
}
|
||||||
|
|
||||||
retval += decode_uint8(&(pkt->compression), base, rem, idx);
|
retval += decode_uint8(&(pkt->compression), base, rem, idx);
|
||||||
retval += decode_uint8(&(pkt->transform), base, rem, idx);
|
retval += decode_uint8(&(pkt->transform), base, rem, idx);
|
||||||
|
|
||||||
return retval;
|
return retval;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -616,16 +616,16 @@ int encode_PEER_INFO (uint8_t *base,
|
||||||
const n2n_common_t *common,
|
const n2n_common_t *common,
|
||||||
const n2n_PEER_INFO_t *pkt) {
|
const n2n_PEER_INFO_t *pkt) {
|
||||||
|
|
||||||
int retval = 0;
|
int retval = 0;
|
||||||
|
|
||||||
retval += encode_common(base, idx, common);
|
retval += encode_common(base, idx, common);
|
||||||
retval += encode_uint16(base, idx, pkt->aflags);
|
retval += encode_uint16(base, idx, pkt->aflags);
|
||||||
retval += encode_mac(base, idx, pkt->srcMac);
|
retval += encode_mac(base, idx, pkt->srcMac);
|
||||||
retval += encode_mac(base, idx, pkt->mac);
|
retval += encode_mac(base, idx, pkt->mac);
|
||||||
retval += encode_sock(base, idx, &pkt->sock);
|
retval += encode_sock(base, idx, &pkt->sock);
|
||||||
retval += encode_buf(base, idx, &pkt->data, sizeof(SN_SELECTION_CRITERION_DATA_TYPE));
|
retval += encode_buf(base, idx, &pkt->data, sizeof(SN_SELECTION_CRITERION_DATA_TYPE));
|
||||||
|
|
||||||
return retval;
|
return retval;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -635,16 +635,16 @@ int decode_PEER_INFO (n2n_PEER_INFO_t *pkt,
|
||||||
size_t *rem,
|
size_t *rem,
|
||||||
size_t *idx) {
|
size_t *idx) {
|
||||||
|
|
||||||
size_t retval = 0;
|
size_t retval = 0;
|
||||||
memset(pkt, 0, sizeof(n2n_PEER_INFO_t));
|
memset(pkt, 0, sizeof(n2n_PEER_INFO_t));
|
||||||
|
|
||||||
retval += decode_uint16(&(pkt->aflags), base, rem, idx);
|
retval += decode_uint16(&(pkt->aflags), base, rem, idx);
|
||||||
retval += decode_mac(pkt->srcMac, base, rem, idx);
|
retval += decode_mac(pkt->srcMac, base, rem, idx);
|
||||||
retval += decode_mac(pkt->mac, base, rem, idx);
|
retval += decode_mac(pkt->mac, base, rem, idx);
|
||||||
retval += decode_sock(&pkt->sock, base, rem, idx);
|
retval += decode_sock(&pkt->sock, base, rem, idx);
|
||||||
retval += decode_buf((uint8_t*)&pkt->data, sizeof(SN_SELECTION_CRITERION_DATA_TYPE), base, rem, idx);
|
retval += decode_buf((uint8_t*)&pkt->data, sizeof(SN_SELECTION_CRITERION_DATA_TYPE), base, rem, idx);
|
||||||
|
|
||||||
return retval;
|
return retval;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -653,13 +653,13 @@ int encode_QUERY_PEER (uint8_t * base,
|
||||||
const n2n_common_t * common,
|
const n2n_common_t * common,
|
||||||
const n2n_QUERY_PEER_t * pkt) {
|
const n2n_QUERY_PEER_t * pkt) {
|
||||||
|
|
||||||
int retval = 0;
|
int retval = 0;
|
||||||
|
|
||||||
retval += encode_common(base, idx, common);
|
retval += encode_common(base, idx, common);
|
||||||
retval += encode_mac(base, idx, pkt->srcMac);
|
retval += encode_mac(base, idx, pkt->srcMac);
|
||||||
retval += encode_mac(base, idx, pkt->targetMac);
|
retval += encode_mac(base, idx, pkt->targetMac);
|
||||||
|
|
||||||
return retval;
|
return retval;
|
||||||
}
|
}
|
||||||
|
|
||||||
int decode_QUERY_PEER (n2n_QUERY_PEER_t * pkt,
|
int decode_QUERY_PEER (n2n_QUERY_PEER_t * pkt,
|
||||||
|
@ -668,11 +668,11 @@ int decode_QUERY_PEER (n2n_QUERY_PEER_t * pkt,
|
||||||
size_t * rem,
|
size_t * rem,
|
||||||
size_t * idx) {
|
size_t * idx) {
|
||||||
|
|
||||||
size_t retval = 0;
|
size_t retval = 0;
|
||||||
memset(pkt, 0, sizeof(n2n_QUERY_PEER_t));
|
memset(pkt, 0, sizeof(n2n_QUERY_PEER_t));
|
||||||
|
|
||||||
retval += decode_mac(pkt->srcMac, base, rem, idx);
|
retval += decode_mac(pkt->srcMac, base, rem, idx);
|
||||||
retval += decode_mac(pkt->targetMac, base, rem, idx);
|
retval += decode_mac(pkt->targetMac, base, rem, idx);
|
||||||
|
|
||||||
return retval;
|
return retval;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user