periodically rediscover network
[corutils.git] / src / routed / routed.h
blob06dcfd529c20522cd6ee863f82dca4dfe913a382
1 /**
2 * Connection oriented routing user space utils
3 * Copyright (C) 2009-2020
4 * Authors:
5 * Michael Blizek
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20 * 02110-1301, USA.
23 #include <stdio.h>
24 #include <string.h>
25 #include <sys/socket.h>
26 #include <unistd.h>
27 #include <fcntl.h>
28 #include <arpa/inet.h>
29 #include <errno.h>
30 #include <sys/epoll.h>
31 #include <sys/stat.h>
32 #include <time.h>
34 #include "../list.h"
35 #include "../utils.h"
36 #include "../cor.h"
37 #include "libcor.h"
40 #define U32_MAX ((__u32) ((1LL << 32) - 1))
43 #define MAX_ADDRLEN 128 \
46 struct route{
47 struct node *dst;
50 struct route_list{
51 struct route *routes;
52 __u16 rows_alloc;
53 __u16 numroutes;
56 struct service_list{
57 __be16 *ports;
58 __u16 rows_alloc;
59 __u16 numports;
62 #define QUERY_STATUS_NOTQUERIED 0
63 #define QUERY_STATUS_QUERIED 1
64 #define QUERY_STATUS_ERROR 2
66 struct node{
67 char *addr;
69 struct route_list routes;
71 struct service_list services;
73 __u16 addrlen;
74 __u8 query_status:2,
75 query_in_progress:1,
76 is_neighbor:1;
78 __u64 last_query_time;
80 struct list_head node_list;
82 struct node *prevhop;
83 __u32 hopcount;
86 #define SEARCHQUERY_NEIGHSNOTQUERIED 1
87 #define SEARCHQUERY_NODEBYADDRESS 2
88 #define SEARCHQUERY_NODEBYSERVICEPORT 3
89 struct search_query{
90 int type;
91 union {
92 struct {
93 __u16 addrlen;
94 char *addr;
95 }nodebyaddress;
97 __be16 serviceport;
98 }query;
101 struct e2eroute{
102 __u16 numhops;
103 /* __u16 addrlen[numhops] */
104 /* char addr[][] */
108 #define EPOLLDATA_TIMER 1
109 #define EPOLLDATA_DISCOVERNETWORK 2
110 #define EPOLLDATA_RDSCONNECT 3
111 #define EPOLLDATA_RDSOCKCMD 4
112 #define EPOLLDATA_FWD_TO_R 5
113 #define EPOLLDATA_FWD_TO_LSERVICE 6
114 #define EPOLLDATA_FORWARD 7
116 struct nonblock_resumeinfo_connect_to_host_send{
117 int fd;
118 struct e2eroute *e2er;
120 __u8 state;
122 __u32 u;
125 struct nonblock_resumeinfo_connect_to_host_recv{
126 int fd;
127 __u32 numhops;
129 __u8 state;
131 __u32 u;
134 #define FORWARD_BUF_SIZE 4096
136 struct nonblock_resumeinfo{
137 int fd;
139 __u8 type;
141 union{
142 struct{
143 __u8 state;
145 struct node *node;
147 struct e2eroute *e2er;
149 struct nonblock_resumeinfo_connect_to_host_send connect_send;
150 struct nonblock_resumeinfo_connect_to_host_recv connect_recv;
152 struct libcor_nonblock_resumeinfo lnr;
153 }discover_network;
155 struct{
156 __u8 state;
158 __u64 cookie;
159 struct e2eroute *e2er;
160 __be16 port;
162 struct nonblock_resumeinfo_connect_to_host_send connect_send;
163 struct nonblock_resumeinfo_connect_to_host_recv connect_resp;
165 struct libcor_nonblock_resumeinfo lnr;
166 }rds_connect;
168 struct{
169 struct libcor_nonblock_resumeinfo lnr;
170 }rdsock_cmd;
172 struct{
173 __u8 fwd_to_host;
174 char *addr;
175 __u16 addrlen;
176 __be16 targetport;
177 }fwd_to_r;
179 struct{
180 int af;
181 struct sockaddr *saddr;
182 socklen_t saddrlen;
183 }fwd_to_lservice;
185 struct{
187 * buf gets filled by reversedir and written into
188 * this fd
190 char *buf;
191 __u32 buffill;
192 __u32 bufwritten;
194 __u8 is_connected;
196 struct nonblock_resumeinfo *reversedir;
197 }forward;
198 }data;
201 struct fwd_to_r_item{
202 struct list_head allfwds;
204 int bindfd;
206 __u8 fwd_to_host;
207 char *addr;
208 __u16 addrlen;
209 __be16 targetport;
212 struct fwd_to_lservice_item{
213 struct list_head allfwds;
215 int bindfd;
217 int af;
218 struct sockaddr *saddr;
219 socklen_t saddrlen;
223 #define offsetof(type, member) __builtin_offsetof (type, member)
225 #define container_of(ptr, type, member) ({ \
226 const typeof( ((type *)0)->member ) *__mptr = (ptr); \
227 (type *)( (char *)__mptr - offsetof(type,member) );})
230 static inline int check_rc(int rc, char *errormsg)
232 if (rc != RC_OK && rc != RC_WOULDBLOCK)
233 printf("%s\n", errormsg);
234 return (rc != RC_OK);
238 /* main.c */
239 extern struct list_head node_list;
241 extern int epoll_fd;
243 extern int rdsock_fd;
246 /* util.c */
247 extern void set_nonblock(int fd, int value);
249 extern int addr_eq(char *addr1, __u32 addrlen1, char *addr2, __u32 addrlen2);
251 extern void print_hex(char *buf, __u32 len);
253 extern void neigh_printaddr(__u16 addrlen, char *addr);
256 /* conf.c */
257 extern char *localaddr;
258 extern __u16 localaddrlen;
260 extern __u32 sleep_before_discover;
261 extern __u8 export_servicelist_enabled;
263 extern __u32 verbosity;
266 extern int parse_args(int argc, char *argv[], struct list_head *r_fwds,
267 struct list_head *lservice_fwds);
270 /* nodedb.c */
271 extern struct route_list localneighs;
272 extern struct service_list localservices;
274 extern struct list_head node_list;
276 extern int service_list_contains(struct service_list *services, __be16 port);
278 extern struct node * try_find_neigh_byaddr(__u16 addrlen, char *addr);
280 extern struct node * try_find_neigh_byservice(__be16 serviceport);
282 extern struct e2eroute * get_e2eroute_localhost(void);
284 extern struct e2eroute * get_e2eroute(struct node *node);
286 extern int connect_to_host_recv(int fd, struct libcor_nonblock_resumeinfo *lnr,
287 struct nonblock_resumeinfo_connect_to_host_recv *rcr,
288 struct e2eroute *e2er);
290 extern int connect_to_host_send(int fd,
291 struct libcor_nonblock_resumeinfo *lnr,
292 struct nonblock_resumeinfo_connect_to_host_send *cth,
293 struct e2eroute *e2er);
295 extern void _discover_network(int fd, struct nonblock_resumeinfo *nr);
297 extern void discover_network(void);
299 extern int initial_discovery_inprogress(void);
301 extern void init_nodedb(void);
303 /* forward.c */
304 extern void fwd_to_r_accept(int listenerfd, __u8 fwd_to_host, char *hostaddr,
305 __u32 hostaddrlen, __be16 targetport);
307 extern void fwd_to_lservice_accept(int listenerfd, int af,
308 struct sockaddr *saddr, socklen_t saddrlen);
310 extern void forward(struct nonblock_resumeinfo *nr, __u32 eventflags);
312 extern void epoll_add_fwd_to_r(struct list_head *r_fwds);
314 extern void epoll_add_fwd_to_lservice(struct list_head *lservice_fwds);
317 static inline __u64 get_time_nsec(void)
319 struct timespec ts;
320 clock_gettime(CLOCK_BOOTTIME, &ts);
321 return 1000000000L * ((__u64) ts.tv_sec) + ((__u64) ts.tv_nsec);