]> git.meshlink.io Git - meshlink/blob - src/node.h
Avoid allocating packet buffers unnecessarily.
[meshlink] / src / node.h
1 #ifndef MESHLINK_NODE_H
2 #define MESHLINK_NODE_H
3
4 /*
5     node.h -- header for node.c
6     Copyright (C) 2014, 2017 Guus Sliepen <guus@meshlink.io>
7
8     This program is free software; you can redistribute it and/or modify
9     it under the terms of the GNU General Public License as published by
10     the Free Software Foundation; either version 2 of the License, or
11     (at your option) any later version.
12
13     This program is distributed in the hope that it will be useful,
14     but WITHOUT ANY WARRANTY; without even the implied warranty of
15     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16     GNU General Public License for more details.
17
18     You should have received a copy of the GNU General Public License along
19     with this program; if not, write to the Free Software Foundation, Inc.,
20     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22
23 #include "event.h"
24 #include "sockaddr.h"
25 #include "sptps.h"
26 #include "utcp/utcp.h"
27 #include "submesh.h"
28
29 typedef struct node_status_t {
30         uint16_t validkey: 1;               /* 1 if we currently have a valid key for him */
31         uint16_t waitingforkey: 1;          /* 1 if we already sent out a request */
32         uint16_t visited: 1;                /* 1 if this node has been visited by one of the graph algorithms */
33         uint16_t reachable: 1;              /* 1 if this node is reachable in the graph */
34         uint16_t udp_confirmed: 1;          /* 1 if the address is one that we received UDP traffic on */
35         uint16_t broadcast: 1;              /* 1 if the next UDP packet should be broadcast to the local network */
36         uint16_t blacklisted: 1;            /* 1 if the node is blacklist so we never want to speak with him anymore */
37         uint16_t destroyed: 1;              /* 1 if the node is being destroyed, deallocate channels when any callback is triggered */
38         uint16_t duplicate: 1;              /* 1 if the node is duplicate, ie. multiple nodes using the same Name are online */
39         uint16_t dirty: 1;                  /* 1 if the configuration of the node is dirty and needs to be written out */
40         uint16_t want_udp: 1;               /* 1 if we want working UDP because we have data to send */
41 } node_status_t;
42
43 #define MAX_RECENT 5
44
45 typedef struct node_t {
46         // Public member variables
47         char *name;                             /* name of this node */
48         void *priv;
49
50         // Private member variables
51         node_status_t status;
52         uint16_t minmtu;                        /* Probed minimum MTU */
53         dev_class_t devclass;
54
55         // Used for packet I/O
56         int sock;                               /* Socket to use for outgoing UDP packets */
57         uint32_t session_id;                    /* Unique ID for this node's currently running process */
58         sptps_t sptps;
59         sockaddr_t address;                     /* his real (internet) ip to send UDP packets to */
60
61         struct utcp *utcp;
62
63         // Traffic counters
64         uint64_t in_packets;
65         uint64_t in_bytes;
66         uint64_t out_packets;
67         uint64_t out_bytes;
68
69         // MTU probes
70         timeout_t mtutimeout;                   /* Probe event */
71         int mtuprobes;                          /* Number of probes */
72         uint16_t mtu;                           /* Maximum size of packets to send to this node */
73         uint16_t maxmtu;                        /* Probed maximum MTU */
74
75         // Used for meta-connection I/O, timeouts
76         struct meshlink_handle *mesh;           /* The mesh this node belongs to */
77         struct submesh_t *submesh;              /* Nodes Sub-Mesh Handle*/
78
79         time_t last_req_key;
80
81         struct ecdsa *ecdsa;                    /* His public ECDSA key */
82
83         struct connection_t *connection;        /* Connection associated with this node (if a direct connection exists) */
84         time_t last_connect_try;
85         time_t last_successfull_connection;
86
87         char *canonical_address;                /* The canonical address of this node, if known */
88         sockaddr_t recent[MAX_RECENT];                   /* Recently seen addresses */
89
90         // Graph-related member variables
91         time_t last_reachable;
92         time_t last_unreachable;
93
94         int distance;
95         struct node_t *nexthop;                 /* nearest node from us to him */
96         struct edge_t *prevedge;                /* nearest node from him to us */
97
98         struct splay_tree_t *edge_tree;         /* Edges with this node as one of the endpoints */
99 } node_t;
100
101 extern void init_nodes(struct meshlink_handle *mesh);
102 extern void exit_nodes(struct meshlink_handle *mesh);
103 extern node_t *new_node(void) __attribute__((__malloc__));
104 extern void free_node(node_t *n);
105 extern void node_add(struct meshlink_handle *mesh, node_t *n);
106 extern void node_del(struct meshlink_handle *mesh, node_t *n);
107 extern node_t *lookup_node(struct meshlink_handle *mesh, const char *name) __attribute__((__warn_unused_result__));
108 extern node_t *lookup_node_udp(struct meshlink_handle *mesh, const sockaddr_t *sa) __attribute__((__warn_unused_result__));
109 extern void update_node_udp(struct meshlink_handle *mesh, node_t *n, const sockaddr_t *sa);
110 extern bool node_add_recent_address(struct meshlink_handle *mesh, node_t *n, const sockaddr_t *addr);
111
112 #endif