}
void connection_del(connection_t *c) {
- for(list_node_t *node = connection_list->head; node; node = node->next) {
+ for list_each(connection_t, c, connection_list) {
if(node->data == c) {
list_delete_node(connection_list, node);
return;
}
bool dump_connections(connection_t *cdump) {
- for(list_node_t *node = connection_list->head, *next; node; node = next) {
- next = node->next;
- connection_t *c = node->data;
+ for list_each(connection_t, c, connection_list) {
send_request(cdump, "%d %d %s %s %x %d %x",
CONTROL, REQ_DUMP_CONNECTIONS,
c->name, c->hostname, c->options, c->socket,
case REQ_DISCONNECT: {
char name[MAX_STRING_SIZE];
- connection_t *other;
bool found = false;
if(sscanf(request, "%*d %*d " MAX_STRING, name) != 1)
return control_return(c, REQ_DISCONNECT, -1);
- for(list_node_t *node = connection_list->head, *next; node; node = next) {
- next = node->next;
- other = node->data;
+ for list_each(connection_t, other, connection_list) {
if(strcmp(other->name, name))
continue;
terminate_connection(other, other->status.active);
}
bool dump_edges(connection_t *c) {
- splay_node_t *node, *node2;
- node_t *n;
- edge_t *e;
- char *address;
-
- for(node = node_tree->head; node; node = node->next) {
- n = node->data;
- for(node2 = n->edge_tree->head; node2; node2 = node2->next) {
- e = node2->data;
- address = sockaddr2hostname(&e->address);
+ for splay_each(node_t, n, node_tree) {
+ for splay_each(edge_t, e, n->edge_tree) {
+ char *address = sockaddr2hostname(&e->address);
send_request(c, "%d %d %s %s %s %x %d",
CONTROL, REQ_DUMP_EDGES,
e->from->name, e->to->name, address,
static void mst_kruskal(void) {
/* Clear MST status on connections */
- for(list_node_t *node = connection_list->head; node; node = node->next) {
- connection_t *c = node->data;
+ for list_each(connection_t, c, connection_list)
c->status.mst = false;
- }
logger(DEBUG_SCARY_THINGS, LOG_DEBUG, "Running Kruskal's algorithm:");
/* Clear visited status on nodes */
- for(splay_node_t *node = node_tree->head; node; node = node->next) {
- node_t *n = node->data;
+ for splay_each(node_t, n, node_tree)
n->status.visited = false;
- }
/* Add safe edges */
- for(splay_node_t *node = edge_weight_tree->head, *next; node; node = next) {
- next = node->next;
- edge_t *e = node->data;
-
+ for splay_each(edge_t, e, edge_weight_tree) {
if(!e->reverse || (e->from->status.visited && e->to->status.visited))
continue;
*/
static void sssp_bfs(void) {
- splay_node_t *node, *to;
- edge_t *e;
- node_t *n;
- list_t *todo_list;
- list_node_t *from, *todonext;
- bool indirect;
-
- todo_list = list_alloc(NULL);
+ list_t *todo_list = list_alloc(NULL);
/* Clear visited status on nodes */
- for(node = node_tree->head; node; node = node->next) {
- n = node->data;
+ for splay_each(node_t, n, node_tree) {
n->status.visited = false;
n->status.indirect = true;
n->distance = -1;
/* Loop while todo_list is filled */
- for(from = todo_list->head; from; from = todonext) { /* "from" is the node from which we start */
- n = from->data;
+ for list_each(node_t, n, todo_list) { /* "n" is the node from which we start */
+ logger(DEBUG_SCARY_THINGS, LOG_DEBUG, " Examining edges from %s", n->name);
+
if(n->distance < 0)
abort();
- for(to = n->edge_tree->head; to; to = to->next) { /* "to" is the edge connected to "from" */
- e = to->data;
-
+ for splay_each(edge_t, e, n->edge_tree) { /* "e" is the edge connected to "from" */
if(!e->reverse)
continue;
of nodes behind it.
*/
- indirect = n->status.indirect || e->options & OPTION_INDIRECT;
+ bool indirect = n->status.indirect || e->options & OPTION_INDIRECT;
if(e->to->status.visited
&& (!e->to->status.indirect || indirect)
e->to->options = e->options;
e->to->distance = n->distance + 1;
- if(!e->to->status.reachable || (e->to->address.sa.sa_family == AF_UNSPEC && e->address.sa.sa_family != AF_UNKNOWN)
-)
+ if(!e->to->status.reachable || (e->to->address.sa.sa_family == AF_UNSPEC && e->address.sa.sa_family != AF_UNKNOWN))
update_node_udp(e->to, &e->address);
list_insert_tail(todo_list, e->to);
}
- todonext = from->next;
- list_delete_node(todo_list, from);
+ next = node->next; /* Because the list_insert_tail() above could have added something extra for us! */
+ list_delete_node(todo_list, node);
}
list_free(todo_list);
}
static void check_reachability(void) {
- splay_node_t *node, *next;
- node_t *n;
- char *name;
- char *address, *port;
- char *envp[7];
- int i;
-
/* Check reachability status. */
- for(node = node_tree->head; node; node = next) {
- next = node->next;
- n = node->data;
-
+ for splay_each(node_t, n, node_tree) {
if(n->status.visited != n->status.reachable) {
n->status.reachable = !n->status.reachable;
n->last_state_change = time(NULL);
if(timeout_initialized(&n->mtuevent))
event_del(&n->mtuevent);
+ char *name;
+ char *address;
+ char *port;
+ char *envp[7];
+
xasprintf(&envp[0], "NETNAME=%s", netname ? : "");
xasprintf(&envp[1], "DEVICE=%s", device ? : "");
xasprintf(&envp[2], "INTERFACE=%s", iface ? : "");
execute_script(n->status.reachable ? "host-up" : "host-down", envp);
- xasprintf(&name,
- n->status.reachable ? "hosts/%s-up" : "hosts/%s-down",
- n->name);
+ xasprintf(&name, n->status.reachable ? "hosts/%s-up" : "hosts/%s-down", n->name);
execute_script(name, envp);
free(name);
free(address);
free(port);
- for(i = 0; i < 6; i++)
+ for(int i = 0; i < 6; i++)
free(envp[i]);
subnet_update(n, NULL, n->status.reachable);
/* (De)constructors */
list_t *list_alloc(list_action_t delete) {
- list_t *list;
-
- list = xmalloc_and_zero(sizeof(list_t));
+ list_t *list = xmalloc_and_zero(sizeof(list_t));
list->delete = delete;
return list;
/* Insertion and deletion */
list_node_t *list_insert_head(list_t *list, void *data) {
- list_node_t *node;
-
- node = list_alloc_node();
+ list_node_t *node = list_alloc_node();
node->data = data;
node->prev = NULL;
}
list_node_t *list_insert_tail(list_t *list, void *data) {
- list_node_t *node;
-
- node = list_alloc_node();
+ list_node_t *node = list_alloc_node();
node->data = data;
node->next = NULL;
}
list_node_t *list_insert_after(list_t *list, list_node_t *after, void *data) {
- list_node_t *node;
-
- node = list_alloc_node();
+ list_node_t *node = list_alloc_node();
node->data = data;
node->next = after->next;
/* Fast list deletion */
void list_delete_list(list_t *list) {
- list_node_t *node, *next;
-
- for(node = list->head; node; node = next) {
- next = node->next;
+ for(list_node_t *node = list->head, *next; next = node->next, node; node = next)
list_free_node(list, node);
- }
list_free(list);
}
/* Traversing */
void list_foreach_node(list_t *list, list_action_node_t action) {
- list_node_t *node, *next;
-
- for(node = list->head; node; node = next) {
- next = node->next;
+ for(list_node_t *node = list->head, *next; next = node->next, node; node = next)
action(node);
- }
}
void list_foreach(list_t *list, list_action_t action) {
- list_node_t *node, *next;
-
- for(node = list->head; node; node = next) {
- next = node->next;
+ for(list_node_t *node = list->head, *next; next = node->next, node; node = next)
if(node->data)
action(node->data);
- }
}
extern void list_foreach(list_t *, list_action_t);
extern void list_foreach_node(list_t *, list_action_node_t);
+#define list_each(type, item, list) (type *item = (type *)1; item; item = NULL) for(list_node_t *node = (list)->head, *next; item = node ? node->data : NULL, next = node ? node->next : NULL, node; node = next)
+
#endif /* __TINC_LIST_H__ */
if(logcontrol) {
suppress = true;
logcontrol = false;
- for(list_node_t *node = connection_list->head, *next; node; node = next) {
- next = node->next;
- connection_t *c = node->data;
+ for list_each(connection_t, c, connection_list) {
if(!c->status.log)
continue;
logcontrol = true;
}
void broadcast_meta(connection_t *from, const char *buffer, int length) {
- for(list_node_t *node = connection_list->head, *next; node; node = next) {
- next = node->next;
- connection_t *c = node->data;
-
+ for list_each(connection_t, c, connection_list)
if(c != from && c->status.active)
send_meta(c, buffer, length);
- }
}
bool receive_meta_sptps(void *handle, uint8_t type, const char *data, uint16_t length) {
/* Purge edges and subnets of unreachable nodes. Use carefully. */
void purge(void) {
- splay_node_t *nnode, *nnext, *enode, *enext, *snode, *snext;
- node_t *n;
- edge_t *e;
- subnet_t *s;
-
logger(DEBUG_PROTOCOL, LOG_DEBUG, "Purging unreachable nodes");
/* Remove all edges and subnets owned by unreachable nodes. */
- for(nnode = node_tree->head; nnode; nnode = nnext) {
- nnext = nnode->next;
- n = nnode->data;
-
+ for splay_each(node_t, n, node_tree) {
if(!n->status.reachable) {
logger(DEBUG_SCARY_THINGS, LOG_DEBUG, "Purging node %s (%s)", n->name, n->hostname);
- for(snode = n->subnet_tree->head; snode; snode = snext) {
- snext = snode->next;
- s = snode->data;
+ for splay_each(subnet_t, s, n->subnet_tree) {
send_del_subnet(everyone, s);
if(!strictsubnets)
subnet_del(n, s);
}
- for(enode = n->edge_tree->head; enode; enode = enext) {
- enext = enode->next;
- e = enode->data;
+ for splay_each(edge_t, e, n->edge_tree) {
if(!tunnelserver)
send_del_edge(everyone, e);
edge_del(e);
/* Check if anyone else claims to have an edge to an unreachable node. If not, delete node. */
- for(nnode = node_tree->head; nnode; nnode = nnext) {
- nnext = nnode->next;
- n = nnode->data;
-
+ for splay_each(node_t, n, node_tree) {
if(!n->status.reachable) {
- for(enode = edge_weight_tree->head; enode; enode = enext) {
- enext = enode->next;
- e = enode->data;
-
+ for splay_each(edge_t, e, edge_weight_tree)
if(e->to == n)
- break;
- }
+ return;
- if(!enode && (!strictsubnets || !n->subnet_tree->head))
+ if(!strictsubnets || !n->subnet_tree->head)
/* in strictsubnets mode do not delete nodes with subnets */
node_del(n);
}
static void timeout_handler(int fd, short events, void *event) {
time_t now = time(NULL);
- for(list_node_t *node = connection_list->head, *next; node; node = next) {
- next = node->next;
- connection_t *c = node->data;
-
+ for list_each(connection_t, c, connection_list) {
if(c->status.control)
continue;
/* If StrictSubnet is set, expire deleted Subnets and read new ones in */
if(strictsubnets) {
- for(splay_node_t *node = subnet_tree->head; node; node = node->next) {
- subnet_t *subnet = node->data;
+ for splay_each(subnet_t, subnet, subnet_tree)
subnet->expires = 1;
- }
load_all_subnets();
- for(splay_node_t *node = subnet_tree->head, *next; node; node = next) {
- next = node->next;
- subnet_t *subnet = node->data;
+ for splay_each(subnet_t, subnet, subnet_tree) {
if(subnet->expires == 1) {
send_del_subnet(everyone, subnet);
if(subnet->owner->status.reachable)
}
}
} else { /* Only read our own subnets back in */
- for(splay_node_t *node = myself->subnet_tree->head; node; node = node->next) {
- subnet_t *subnet = node->data;
+ for splay_each(subnet_t, subnet, myself->subnet_tree)
if(!subnet->expires)
subnet->expires = 1;
- }
config_t *cfg = lookup_config(config_tree, "Subnet");
cfg = lookup_config_next(config_tree, cfg);
}
- for(splay_node_t *node = myself->subnet_tree->head, *next; node; node = next) {
- next = node->next;
- subnet_t *subnet = node->data;
+ for splay_each(subnet_t, subnet, myself->subnet_tree) {
if(subnet->expires == 1) {
send_del_subnet(everyone, subnet);
subnet_update(myself, subnet, false);
/* Close connections to hosts that have a changed or deleted host config file */
- for(list_node_t *node = connection_list->head, *next; node; node = next) {
- connection_t *c = node->data;
- next = node->next;
-
+ for list_each(connection_t, c, connection_list) {
if(c->status.control)
continue;
}
void retry(void) {
- for(list_node_t *node = connection_list->head, *next; node; node = next) {
- next = node->next;
- connection_t *c = node->data;
-
+ for list_each(connection_t, c, connection_list) {
if(c->outgoing && !c->node) {
if(timeout_initialized(&c->outgoing->ev))
event_del(&c->outgoing->ev);
static void send_mtu_probe_handler(int fd, short events, void *data) {
node_t *n = data;
- vpn_packet_t packet;
- int len, i;
int timeout = 1;
n->mtuprobes++;
timeout = pingtimeout;
}
- for(i = 0; i < 3 + localdiscovery; i++) {
+ for(int i = 0; i < 3 + localdiscovery; i++) {
+ int len;
+
if(n->maxmtu <= n->minmtu)
len = n->maxmtu;
else
if(len < 64)
len = 64;
+ vpn_packet_t packet;
memset(packet.data, 0, 14);
randomize(packet.data + 14, len - 14);
packet.len = len;
// This guarantees all nodes receive the broadcast packet, and
// usually distributes the sending of broadcast packets over all nodes.
case BMODE_MST:
- for(list_node_t *node = connection_list->head, *next; node; node = next) {
- next = node->next;
- connection_t *c = node->data;
-
+ for list_each(connection_t, c, connection_list)
if(c->status.active && c->status.mst && c != from->nexthop->connection)
send_packet(c->node, packet);
- }
break;
// In direct mode, we send copies to each node we know of.
if(from != myself)
break;
- for(splay_node_t *node = node_tree->head; node; node = node->next) {
- node_t *n = node->data;
-
+ for splay_each(node_t, n, node_tree)
if(n->status.reachable && ((n->via == myself && n->nexthop == n) || n->via == n))
send_packet(n, packet);
- }
break;
default:
static time_t last_hard_try = 0;
time_t now = time(NULL);
- for(node = edge_weight_tree->head; node; node = node->next) {
- e = node->data;
-
+ for splay_each(edge_t, e, edge_weight_tree) {
if(!e->to->status.reachable || e->to == myself)
continue;
DIR *dir;
struct dirent *ent;
char *dname;
- char *fname;
- splay_tree_t *config_tree;
- config_t *cfg;
- subnet_t *s, *s2;
- node_t *n;
xasprintf(&dname, "%s" SLASH "hosts", confbase);
dir = opendir(dname);
if(!check_id(ent->d_name))
continue;
- n = lookup_node(ent->d_name);
+ node_t *n = lookup_node(ent->d_name);
#ifdef _DIRENT_HAVE_D_TYPE
//if(ent->d_type != DT_REG)
// continue;
#endif
+ char *fname;
xasprintf(&fname, "%s" SLASH "hosts" SLASH "%s", confbase, ent->d_name);
+
+ splay_tree_t *config_tree;
init_configuration(&config_tree);
read_config_options(config_tree, ent->d_name);
read_config_file(config_tree, fname);
node_add(n);
}
- for(cfg = lookup_config(config_tree, "Subnet"); cfg; cfg = lookup_config_next(config_tree, cfg)) {
+ for(config_t *cfg = lookup_config(config_tree, "Subnet"); cfg; cfg = lookup_config_next(config_tree, cfg)) {
+ subnet_t *s, *s2;
+
if(!get_config_subnet(cfg, &s))
continue;
Configure node_t myself and set up the local sockets (listen only)
*/
static bool setup_myself(void) {
- config_t *cfg;
- subnet_t *subnet;
char *name, *hostname, *cipher, *digest, *type;
char *fname = NULL;
char *address = NULL;
- char *envp[5];
- struct addrinfo *ai, *aip, hint = {0};
- int i, err;
- int replaywin_int;
if(!(name = get_name())) {
logger(DEBUG_ALWAYS, LOG_ERR, "Name for tinc daemon required!");
/* Read in all the subnets specified in the host configuration file */
- cfg = lookup_config(config_tree, "Subnet");
+ for(config_t *cfg = lookup_config(config_tree, "Subnet"); cfg; cfg = lookup_config_next(config_tree, cfg)) {
+ subnet_t *subnet;
- while(cfg) {
if(!get_config_subnet(cfg, &subnet))
return false;
subnet_add(myself, subnet);
-
- cfg = lookup_config_next(config_tree, cfg);
}
/* Check some options */
}
}
+ int replaywin_int;
if(get_config_int(lookup_config(config_tree, "ReplayWindow"), &replaywin_int)) {
if(replaywin_int < 0) {
logger(DEBUG_ALWAYS, LOG_ERR, "ReplayWindow cannot be negative!");
}
/* Run tinc-up script to further initialize the tap interface */
+ char *envp[5];
xasprintf(&envp[0], "NETNAME=%s", netname ? : "");
xasprintf(&envp[1], "DEVICE=%s", device ? : "");
xasprintf(&envp[2], "INTERFACE=%s", iface ? : "");
execute_script("tinc-up", envp);
- for(i = 0; i < 4; i++)
+ for(int i = 0; i < 4; i++)
free(envp[i]);
/* Run subnet-up scripts for our own subnets */
return false;
}
- for(i = 0; i < listen_sockets; i++) {
+ for(int i = 0; i < listen_sockets; i++) {
salen = sizeof sa;
if(getsockname(i + 3, &sa.sa, &salen) < 0) {
logger(DEBUG_ALWAYS, LOG_ERR, "Could not get address of listen fd %d: %s", i + 3, sockstrerror(errno));
}
} else {
listen_sockets = 0;
- cfg = lookup_config(config_tree, "BindToAddress");
+ config_t *cfg = lookup_config(config_tree, "BindToAddress");
do {
get_config_string(cfg, &address);
*address = 0;
}
+ struct addrinfo *ai, hint = {0};
hint.ai_family = addressfamily;
hint.ai_socktype = SOCK_STREAM;
hint.ai_protocol = IPPROTO_TCP;
hint.ai_flags = AI_PASSIVE;
- err = getaddrinfo(address && *address ? address : NULL, port, &hint, &ai);
+ int err = getaddrinfo(address && *address ? address : NULL, port, &hint, &ai);
free(address);
if(err || !ai) {
return false;
}
- for(aip = ai; aip; aip = aip->ai_next) {
+ for(struct addrinfo *aip = ai; aip; aip = aip->ai_next) {
if(listen_sockets >= MAXSOCKETS) {
logger(DEBUG_ALWAYS, LOG_ERR, "Too many listening sockets");
return false;
}
void try_outgoing_connections(void) {
- static config_t *cfg = NULL;
- char *name;
- outgoing_t *outgoing;
-
/* If there is no outgoing list yet, create one. Otherwise, mark all outgoings as deleted. */
if(!outgoing_list) {
outgoing_list = list_alloc((list_action_t)free_outgoing);
} else {
- for(list_node_t *i = outgoing_list->head; i; i = i->next) {
- outgoing = i->data;
+ for list_each(outgoing_t, outgoing, outgoing_list)
outgoing->timeout = -1;
- }
}
/* Make sure there is one outgoing_t in the list for each ConnectTo. */
- for(cfg = lookup_config(config_tree, "ConnectTo"); cfg; cfg = lookup_config_next(config_tree, cfg)) {
+ for(config_t *cfg = lookup_config(config_tree, "ConnectTo"); cfg; cfg = lookup_config_next(config_tree, cfg)) {
+ char *name;
get_config_string(cfg, &name);
if(!check_id(name)) {
bool found = false;
- for(list_node_t *i = outgoing_list->head; i; i = i->next) {
- outgoing = i->data;
+ for list_each(outgoing_t, outgoing, outgoing_list) {
if(!strcmp(outgoing->name, name)) {
found = true;
outgoing->timeout = 0;
}
if(!found) {
- outgoing = xmalloc_and_zero(sizeof *outgoing);
+ outgoing_t *outgoing = xmalloc_and_zero(sizeof *outgoing);
outgoing->name = name;
list_insert_tail(outgoing_list, outgoing);
setup_outgoing_connection(outgoing);
/* Terminate any connections whose outgoing_t is to be deleted. */
- for(list_node_t *node = connection_list->head, *next; node; node = next) {
- next = node->next;
- connection_t *c = node->data;
+ for list_each(connection_t, c, connection_list) {
if(c->outgoing && c->outgoing->timeout == -1) {
c->outgoing = NULL;
logger(DEBUG_CONNECTIONS, LOG_INFO, "No more outgoing connection to %s", c->name);
/* Delete outgoing_ts for which there is no ConnectTo. */
- for(list_node_t *node = outgoing_list->head, *next; node; node = next) {
- next = node->next;
- outgoing = node->data;
+ for list_each(outgoing_t, outgoing, outgoing_list)
if(outgoing->timeout == -1)
list_delete_node(outgoing_list, node);
- }
}
}
void node_del(node_t *n) {
- splay_node_t *node, *next;
- edge_t *e;
- subnet_t *s;
-
- for(node = n->subnet_tree->head; node; node = next) {
- next = node->next;
- s = node->data;
+ for splay_each(subnet_t, s, n->subnet_tree)
subnet_del(n, s);
- }
- for(node = n->edge_tree->head; node; node = next) {
- next = node->next;
- e = node->data;
+ for splay_each(edge_t, e, n->edge_tree)
edge_del(e);
- }
splay_delete(node_tree, n);
}
}
bool dump_nodes(connection_t *c) {
- splay_node_t *node;
- node_t *n;
-
- for(node = node_tree->head; node; node = node->next) {
- n = node->data;
+ for splay_each(node_t, n, node_tree)
send_request(c, "%d %d %s %s %d %d %d %d %x %x %s %s %d %hd %hd %hd %ld", CONTROL, REQ_DUMP_NODES,
n->name, n->hostname ?: "unknown port unknown", cipher_get_nid(&n->outcipher),
digest_get_nid(&n->outdigest), (int)digest_length(&n->outdigest), n->outcompression,
n->options, bitfield_to_int(&n->status, sizeof n->status), n->nexthop ? n->nexthop->name : "-",
n->via ? n->via->name ?: "-" : "-", n->distance, n->mtu, n->minmtu, n->maxmtu, (long)n->last_state_change);
- }
return send_request(c, "%d %d", CONTROL, REQ_DUMP_NODES);
}
bool dump_traffic(connection_t *c) {
- splay_node_t *node;
- node_t *n;
-
- for(node = node_tree->head; node; node = node->next) {
- n = node->data;
+ for splay_each(node_t, n, node_tree)
send_request(c, "%d %d %s %"PRIu64" %"PRIu64" %"PRIu64" %"PRIu64, CONTROL, REQ_DUMP_TRAFFIC,
n->name, n->in_packets, n->in_bytes, n->out_packets, n->out_bytes);
- }
return send_request(c, "%d %d", CONTROL, REQ_DUMP_TRAFFIC);
}
static bool install_service(void) {
char command[4096] = "\"";
- char **argp;
- bool space;
SERVICE_DESCRIPTION description = {"Virtual Private Network daemon"};
manager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
strncat(command, "\"", sizeof command - strlen(command));
- for(argp = g_argv + 1; *argp; argp++) {
- space = strchr(*argp, ' ');
+ for(char **argp = g_argv + 1; *argp; argp++) {
+ char &space = strchr(*argp, ' ');
strncat(command, " ", sizeof command - strlen(command));
if(space)
}
static void age_past_requests(int fd, short events, void *data) {
- splay_node_t *node, *next;
- past_request_t *p;
int left = 0, deleted = 0;
time_t now = time(NULL);
- for(node = past_request_tree->head; node; node = next) {
- next = node->next;
- p = node->data;
-
+ for splay_each(past_request_t, p, past_request_tree) {
if(p->firstseen + pinginterval <= now)
splay_delete_node(past_request_tree, node), deleted++;
else
}
static void send_everything(connection_t *c) {
- splay_node_t *node, *node2;
- node_t *n;
- subnet_t *s;
- edge_t *e;
-
/* Send all known subnets and edges */
if(tunnelserver) {
- for(node = myself->subnet_tree->head; node; node = node->next) {
- s = node->data;
+ for splay_each(subnet_t, s, myself->subnet_tree)
send_add_subnet(c, s);
- }
return;
}
- for(node = node_tree->head; node; node = node->next) {
- n = node->data;
-
- for(node2 = n->subnet_tree->head; node2; node2 = node2->next) {
- s = node2->data;
+ for splay_each(node_t, n, node_tree) {
+ for splay_each(subnet_t, s, n->subnet_tree)
send_add_subnet(c, s);
- }
- for(node2 = n->edge_tree->head; node2; node2 = node2->next) {
- e = node2->data;
+ for splay_each(edge_t, e, n->edge_tree)
send_add_edge(c, e);
- }
}
}
/* Immediately send new keys to directly connected nodes to keep UDP mappings alive */
- for(list_node_t *node = connection_list->head, *next; node; node = next) {
- next = node->next;
- connection_t *c = node->data;
- if(c->status.active && c->node && c->node->status.reachable) {
- if(!c->node->status.sptps)
- send_ans_key(c->node);
- }
- }
+ for list_each(connection_t, c, connection_list)
+ if(c->status.active && c->node && c->node->status.reachable && !c->node->status.sptps)
+ send_ans_key(c->node);
/* Force key exchange for connections using SPTPS */
if(experimental) {
- for(splay_node_t *node = node_tree->head; node; node = node->next) {
- node_t *n = node->data;
+ for splay_each(node_t, n, node_tree)
if(n->status.reachable && n->status.validkey && n->status.sptps)
sptps_force_kex(&n->sptps);
- }
}
}
bool left = false;
time_t now = time(NULL);
- for(splay_node_t *node = myself->subnet_tree->head, *next; node; node = next) {
- next = node->next;
- subnet_t *s = node->data;
+ for splay_each(subnet_t, s, myself->subnet_tree) {
if(s->expires && s->expires < now) {
if(debug_level >= DEBUG_TRAFFIC) {
char netstr[MAXNETSTR];
logger(DEBUG_TRAFFIC, LOG_INFO, "Subnet %s expired", netstr);
}
- for(list_node_t *node = connection_list->head, *next; node; node = next) {
- next = node->next;
- connection_t *c = node->data;
+ for list_each(connection_t, c, connection_list)
if(c->status.active)
send_del_subnet(c, s);
- }
subnet_del(myself, s);
} else {
/* And tell all other tinc daemons it's our MAC */
- for(list_node_t *node = connection_list->head, *next; node; node = next) {
- next = node->next;
- connection_t *c = node->data;
+ for list_each(connection_t, c, connection_list)
if(c->status.active)
send_add_subnet(c, subnet);
- }
if(!timeout_initialized(&age_subnets_event))
timeout_set(&age_subnets_event, age_subnets, NULL);
static void send_pcap(vpn_packet_t *packet) {
pcap = false;
- for(list_node_t *node = connection_list->head, *next; node; node = next) {
- next = node->next;
- connection_t *c = node->data;
+
+ for list_each(connection_t, c, connection_list) {
if(!c->status.pcap)
continue;
/* Fast tree cleanup */
void splay_delete_tree(splay_tree_t *tree) {
- splay_node_t *node, *next;
-
- for(node = tree->head; node; node = next) {
+ for(splay_node_t *node = tree->head, *next; node; node = next) {
next = node->next;
splay_free_node(tree, node);
}
/* Tree walking */
void splay_foreach(const splay_tree_t *tree, splay_action_t action) {
- splay_node_t *node, *next;
-
- for(node = tree->head; node; node = next) {
+ for(splay_node_t *node = tree->head, *next; node; node = next) {
next = node->next;
action(node->data);
}
}
void splay_foreach_node(const splay_tree_t *tree, splay_action_t action) {
- splay_node_t *node, *next;
-
- for(node = tree->head; node; node = next) {
+ for(splay_node_t *node = tree->head, *next; node; node = next) {
next = node->next;
action(node);
}
extern void splay_foreach(const splay_tree_t *, splay_action_t);
extern void splay_foreach_node(const splay_tree_t *, splay_action_t);
+#define splay_each(type, item, tree) (type *item = (type *)1; item; item = NULL) for(splay_node_t *node = (tree)->head, *next; item = node ? node->data : NULL, next = node ? node->next : NULL, node; node = next)
+
#endif
}
subnet_t *lookup_subnet_mac(const node_t *owner, const mac_t *address) {
- subnet_t *p, *r = NULL;
- splay_node_t *n;
+ subnet_t *r = NULL;
// Check if this address is cached
// Search all subnets for a matching one
- for(n = owner ? owner->subnet_tree->head : subnet_tree->head; n; n = n->next) {
- p = n->data;
-
+ for splay_each(subnet_t, p, owner ? owner->subnet_tree : subnet_tree) {
if(!p || p->type != SUBNET_MAC)
continue;
}
subnet_t *lookup_subnet_ipv4(const ipv4_t *address) {
- subnet_t *p, *r = NULL;
- splay_node_t *n;
+ subnet_t *r = NULL;
// Check if this address is cached
// Search all subnets for a matching one
- for(n = subnet_tree->head; n; n = n->next) {
- p = n->data;
-
+ for splay_each(subnet_t, p, subnet_tree) {
if(!p || p->type != SUBNET_IPV4)
continue;
}
subnet_t *lookup_subnet_ipv6(const ipv6_t *address) {
- subnet_t *p, *r = NULL;
- splay_node_t *n;
+ subnet_t *r = NULL;
// Check if this address is cached
// Search all subnets for a matching one
- for(n = subnet_tree->head; n; n = n->next) {
- p = n->data;
-
+ for splay_each(subnet_t, p, subnet_tree) {
if(!p || p->type != SUBNET_IPV6)
continue;
}
void subnet_update(node_t *owner, subnet_t *subnet, bool up) {
- splay_node_t *node;
- int i;
- char *envp[9] = {NULL};
char netstr[MAXNETSTR];
char *name, *address, *port;
char empty[] = "";
// Prepare environment variables to be passed to the script
+ char *envp[9] = {NULL};
xasprintf(&envp[0], "NETNAME=%s", netname ? : "");
xasprintf(&envp[1], "DEVICE=%s", device ? : "");
xasprintf(&envp[2], "INTERFACE=%s", iface ? : "");
name = up ? "subnet-up" : "subnet-down";
if(!subnet) {
- for(node = owner->subnet_tree->head; node; node = node->next) {
- subnet = node->data;
+ for splay_each(subnet_t, subnet, owner->subnet_tree) {
if(!net2str(netstr, sizeof netstr, subnet))
continue;
+
// Strip the weight from the subnet, and put it in its own environment variable
char *weight = strchr(netstr, '#');
if(weight)
}
}
- for(i = 0; envp[i] && i < 8; i++)
+ for(int i = 0; envp[i] && i < 8; i++)
free(envp[i]);
}
bool dump_subnets(connection_t *c) {
- char netstr[MAXNETSTR];
- subnet_t *subnet;
- splay_node_t *node;
+ for splay_each(subnet_t, subnet, subnet_tree) {
+ char netstr[MAXNETSTR];
- for(node = subnet_tree->head; node; node = node->next) {
- subnet = node->data;
if(!net2str(netstr, sizeof netstr, subnet))
continue;
+
send_request(c, "%d %d %s %s",
CONTROL, REQ_DUMP_SUBNETS,
netstr, subnet->owner->name);
subnet->net.ipv4.prefixlength = l;
subnet->weight = weight;
- for(i = 0; i < 4; i++) {
+ for(int i = 0; i < 4; i++) {
if(x[i] > 255)
return false;
subnet->net.ipv4.address.x[i] = x[i];
uint64_t out_packets;
uint64_t out_bytes;
- for(list_node_t *i = node_list.head; i; i = i->next) {
- nodestats_t *node = i->data;
- node->known = false;
- }
+ for list_each(nodestats_t, ns, &node_list)
+ ns->known = false;
while(recvline(fd, line, sizeof line)) {
int n = sscanf(line, "%d %d %s %"PRIu64" %"PRIu64" %"PRIu64" %"PRIu64, &code, &req, name, &in_packets, &in_bytes, &out_packets, &out_bytes);
nodestats_t *found = NULL;
- for(list_node_t *i = node_list.head; i; i = i->next) {
- nodestats_t *node = i->data;
- int result = strcmp(name, node->name);
+ for list_each(nodestats_t, ns, &node_list) {
+ int result = strcmp(name, ns->name);
if(result > 0) {
continue;
} if(result == 0) {
- found = node;
+ found = ns;
break;
} else {
found = xmalloc_and_zero(sizeof *found);
found->name = xstrdup(name);
- list_insert_before(&node_list, i, found);
+ list_insert_before(&node_list, node, found);
changed = true;
break;
}
if(changed) {
n = 0;
sorted = xrealloc(sorted, node_list.count * sizeof *sorted);
- for(list_node_t *i = node_list.head; i; i = i->next)
- sorted[n++] = i->data;
+ for list_each(nodestats_t, ns, &node_list)
+ sorted[n++] = ns;
changed = false;
}
}
int bin2hex(const char *src, char *dst, int length) {
- int i;
- for(i = length - 1; i >= 0; i--) {
+ for(int i = length - 1; i >= 0; i--) {
dst[i * 2 + 1] = hexadecimals[(unsigned char) src[i] & 15];
dst[i * 2] = hexadecimals[(unsigned char) src[i] >> 4];
}