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