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