]> git.meshlink.io Git - meshlink/blob - examples/manynodes.c
Start of the manynodes example.
[meshlink] / examples / manynodes.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <strings.h>
5 #include <sys/types.h>
6 #include <sys/stat.h>
7 #include <linux/limits.h>
8
9 #include "../src/meshlink.h"
10
11 static int n = 100;
12 static meshlink_handle_t **mesh;
13
14 static void log_message(meshlink_handle_t *mesh, meshlink_log_level_t level, const char *text) {
15         const char *levelstr[] = {"DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"};
16         fprintf(stderr, "%s: %s\n", levelstr[level], text);
17 }
18
19 static void receive(meshlink_handle_t *mesh, meshlink_node_t *source, const void *data, size_t len) {
20         const char *msg = data;
21
22         if(!len || msg[len - 1]) {
23                 fprintf(stderr, "Received invalid data from %s\n", source->name);
24                 return;
25         }
26
27         printf("%s says: %s\n", source->name, msg);
28 }
29
30 static void node_status(meshlink_handle_t *mesh, meshlink_node_t *node, bool reachable) {
31         if(reachable)
32                 printf("%s joined.\n", node->name);
33         else
34                 printf("%s left.\n", node->name);
35 }
36
37 // Make all nodes know about each other by importing each others public keys and addresses.
38 static void linkmesh() {
39         for(int i = 0; i < n; i++) {
40                 char *datai = meshlink_export(mesh[i]);
41
42                 for(int j = i + 1; j < n; j++) {
43                         char *dataj = meshlink_export(mesh[j]);
44                         meshlink_import(mesh[i], dataj);
45                         meshlink_import(mesh[j], datai);
46                         free(dataj);
47                 }
48
49                 free(datai);
50         }
51 }
52
53 static void parse_command(char *buf) {
54         char *arg = strchr(buf, ' ');
55         if(arg)
56                 *arg++ = 0;
57
58         if(!strcasecmp(buf, "invite")) {
59                 char *invitation;
60
61                 if(!arg) {
62                         fprintf(stderr, "/invite requires an argument!\n");
63                         return;
64                 }
65
66                 invitation = meshlink_invite(mesh[0], arg);
67                 if(!invitation) {
68                         fprintf(stderr, "Could not invite '%s': %s\n", arg, meshlink_strerror(meshlink_errno));
69                         return;
70                 }
71
72                 printf("Invitation for %s: %s\n", arg, invitation);
73                 free(invitation);
74         } else if(!strcasecmp(buf, "join")) {
75                 if(!arg) {
76                         fprintf(stderr, "/join requires an argument!\n");
77                         return;
78                 }
79
80                 if(!meshlink_join(mesh[0], arg))
81                         fprintf(stderr, "Could not join using invitation: %s\n", meshlink_strerror(meshlink_errno));
82                 else
83                         fprintf(stderr, "Invitation accepted!\n");
84         } else if(!strcasecmp(buf, "kick")) {
85                 if(!arg) {
86                         fprintf(stderr, "/kick requires an argument!\n");
87                         return;
88                 }
89
90                 meshlink_node_t *node = meshlink_get_node(mesh[0], arg);
91                 if(!node) {
92                         fprintf(stderr, "Unknown node '%s'\n", arg);
93                         return;
94                 }
95
96                 meshlink_blacklist(mesh[0], node);
97
98                 printf("Node '%s' blacklisted.\n", arg);
99         } else if(!strcasecmp(buf, "who")) {
100                 if(!arg) {
101                         meshlink_node_t *nodes[100];
102                         size_t n = meshlink_get_all_nodes(mesh[0], nodes, 100);
103                         if(!n) {
104                                 fprintf(stderr, "No nodes known!\n");
105                         } else {
106                                 printf("Known nodes:");
107                                 for(int i = 0; i < n && i < 100; i++)
108                                         printf(" %s", nodes[i]->name);
109                                 if(n > 100)
110                                         printf(" (and %zu more)", n - 100);
111                                 printf("\n");
112                         }
113                 } else {
114                         meshlink_node_t *node = meshlink_get_node(mesh[0], arg);
115                         if(!node) {
116                                 fprintf(stderr, "Unknown node '%s'\n", arg);
117                         } else {
118                                 printf("Node %s found\n", arg);
119                         }
120                 }
121         } else if(!strcasecmp(buf, "link")) {
122                 linkmesh();
123         } else if(!strcasecmp(buf, "quit")) {
124                 printf("Bye!\n");
125                 fclose(stdin);
126         } else if(!strcasecmp(buf, "help")) {
127                 printf(
128                         "<name>: <message>     Send a message to the given node.\n"
129                         "                      Subsequent messages don't need the <name>: prefix.\n"
130                         "/invite <name>        Create an invitation for a new node.\n"
131                         "/join <invitation>    Join an existing mesh using an invitation.\n"
132                         "/kick <name>          Blacklist the given node.\n"
133                         "/who [<name>]         List all nodes or show information about the given node.\n"
134                         "/link                 Link all nodes together.\n"
135                         "/quit                 Exit this program.\n"
136                         );
137         } else {
138                 fprintf(stderr, "Unknown command '/%s'\n", buf);
139         }
140 }
141
142 static void parse_input(char *buf) {
143         static meshlink_node_t *destination;
144         size_t len;
145
146         if(!buf)
147                 return;
148
149         // Remove newline.
150
151         len = strlen(buf);
152
153         if(len && buf[len - 1] == '\n')
154                 buf[--len] = 0;
155
156         if(len && buf[len - 1] == '\r')
157                 buf[--len] = 0;
158
159         // Ignore empty lines.
160
161         if(!len)
162                 return;
163
164         // Commands start with '/'
165
166         if(*buf == '/')
167                 return parse_command(buf + 1);
168
169         // Lines in the form "name: message..." set the destination node.
170
171         char *msg = buf;
172         char *colon = strchr(buf, ':');
173
174         if(colon) {
175                 *colon = 0;
176                 msg = colon + 1;
177                 if(*msg == ' ')
178                         msg++;
179
180                 destination = meshlink_get_node(mesh[0], buf);
181                 if(!destination) {
182                         fprintf(stderr, "Unknown node '%s'\n", buf);
183                         return;
184                 }
185         }
186
187         if(!destination) {
188                 fprintf(stderr, "Who are you talking to? Write 'name: message...'\n");
189                 return;
190         }
191
192         if(!meshlink_send(mesh[0], destination, msg, strlen(msg) + 1)) {
193                 fprintf(stderr, "Could not send message to '%s': %s\n", destination->name, meshlink_strerror(meshlink_errno));
194                 return;
195         }
196
197         printf("Message sent to '%s'.\n", destination->name);
198 }
199
200 int main(int argc, char *argv[]) {
201         const char *basebase = ".manynodes";
202         char buf[1024];
203
204         if(argc > 1)
205                 n = atoi(argv[1]);
206
207         if(n < 1) {
208                 fprintf(stderr, "Usage: %s [number of local nodes] [confbase]\n", argv[0]);
209                 return 1;
210         }
211
212         if(argc > 2)
213                 basebase = argv[2];
214
215         mesh = calloc(n, sizeof *mesh);
216
217         mkdir(basebase, 0750);
218
219         char filename[PATH_MAX];
220         char nodename[100];
221         for(int i = 0; i < n; i++) {
222                 snprintf(nodename, sizeof nodename, "node%d", i);
223                 snprintf(filename, sizeof filename, "%s/%s", basebase, nodename);
224                 bool itsnew = access(filename, R_OK);
225                 mesh[i] = meshlink_open(filename, nodename);
226                 if(itsnew)
227                         meshlink_add_address(mesh[i], "localhost");
228                 if(!mesh[i]) {
229                         fprintf(stderr, "errno is: %d\n", meshlink_errno);
230                         fprintf(stderr, "Could not open %s: %s\n", filename, meshlink_strerror(meshlink_errno));
231                         return 1;
232                 }
233         }
234
235         int started = 0;
236
237         for(int i = 0; i < n; i++) {
238                 if(!meshlink_start(mesh[i]))
239                         fprintf(stderr, "Could not start node %d: %s\n", i, meshlink_strerror(meshlink_errno));
240                 else
241                         started++;
242         }
243
244         if(!started) {
245                 fprintf(stderr, "Could not start any node!\n");
246                 return 1;
247         }
248
249         printf("%d nodes started.\nType /help for a list of commands.\n", started);
250
251         while(fgets(buf, sizeof buf, stdin))
252                 parse_input(buf);
253
254         printf("Nodes stopping.\n");
255
256         for(int i = 0; i < n; i++)
257                 meshlink_close(mesh[i]);
258
259         return 0;
260 }