]> git.meshlink.io Git - meshlink/blob - examples/channels.c
Add the /whitelist command to the chat examples.
[meshlink] / examples / channels.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <strings.h>
5 #include "../src/meshlink.h"
6
7 #define CHAT_PORT 531
8
9 static void log_message(meshlink_handle_t *mesh, meshlink_log_level_t level, const char *text) {
10         (void) mesh;
11
12         static const char *levelstr[] = {
13                 [MESHLINK_DEBUG] = "\x1b[34mDEBUG",
14                 [MESHLINK_INFO] = "\x1b[32mINFO",
15                 [MESHLINK_WARNING] = "\x1b[33mWARNING",
16                 [MESHLINK_ERROR] = "\x1b[31mERROR",
17                 [MESHLINK_CRITICAL] = "\x1b[31mCRITICAL",
18         };
19         fprintf(stderr, "%s:\x1b[0m %s\n", levelstr[level], text);
20 }
21
22 static void channel_receive(meshlink_handle_t *mesh, meshlink_channel_t *channel, const void *data, size_t len) {
23         if(!len) {
24                 if(meshlink_errno) {
25                         fprintf(stderr, "Error while reading data from %s: %s\n", channel->node->name, meshlink_strerror(meshlink_errno));
26                 } else {
27                         fprintf(stderr, "Chat connection closed by %s\n", channel->node->name);
28                 }
29
30                 channel->node->priv = NULL;
31                 meshlink_channel_close(mesh, channel);
32                 return;
33         }
34
35         // TODO: we now have TCP semantics, don't expect exactly one message per receive call.
36
37         printf("%s says: ", channel->node->name);
38         fwrite(data, len, 1, stdout);
39         fputc('\n', stdout);
40 }
41
42 static bool channel_accept(meshlink_handle_t *mesh, meshlink_channel_t *channel, uint16_t port, const void *data, size_t len) {
43         (void)data;
44         (void)len;
45
46         // Only accept connections to the chat port
47         if(port != CHAT_PORT) {
48                 fprintf(stderr, "Rejected incoming channel from '%s' to port %u\n", channel->node->name, port);
49                 return false;
50         }
51
52         fprintf(stderr, "Accepted incoming channel from '%s'\n", channel->node->name);
53
54         // Remember the channel
55         channel->node->priv = channel;
56
57         // Set the receive callback
58         meshlink_set_channel_receive_cb(mesh, channel, channel_receive);
59
60         // Accept this channel
61         return true;
62 }
63
64 static void channel_poll(meshlink_handle_t *mesh, meshlink_channel_t *channel, size_t len) {
65         (void)len;
66
67         fprintf(stderr, "Channel to '%s' connected\n", channel->node->name);
68         meshlink_set_channel_poll_cb(mesh, channel, NULL);
69 }
70
71 static void node_status(meshlink_handle_t *mesh, meshlink_node_t *node, bool reachable) {
72         (void)mesh;
73
74         if(reachable) {
75                 printf("%s joined.\n", node->name);
76         } else {
77                 printf("%s left.\n", node->name);
78         }
79 }
80
81 static meshlink_node_t **nodes;
82 static size_t nnodes;
83
84 static void parse_command(meshlink_handle_t *mesh, char *buf) {
85         char *arg = strchr(buf, ' ');
86
87         if(arg) {
88                 *arg++ = 0;
89         }
90
91         if(!strcasecmp(buf, "invite")) {
92                 char *invitation;
93
94                 if(!arg) {
95                         fprintf(stderr, "/invite requires an argument!\n");
96                         return;
97                 }
98
99                 invitation = meshlink_invite(mesh, NULL, arg);
100
101                 if(!invitation) {
102                         fprintf(stderr, "Could not invite '%s': %s\n", arg, meshlink_strerror(meshlink_errno));
103                         return;
104                 }
105
106                 printf("Invitation for %s: %s\n", arg, invitation);
107                 free(invitation);
108         } else if(!strcasecmp(buf, "join")) {
109                 if(!arg) {
110                         fprintf(stderr, "/join requires an argument!\n");
111                         return;
112                 }
113
114                 meshlink_stop(mesh);
115
116                 if(!meshlink_join(mesh, arg)) {
117                         fprintf(stderr, "Could not join using invitation: %s\n", meshlink_strerror(meshlink_errno));
118                 } else {
119                         fprintf(stderr, "Invitation accepted!\n");
120                 }
121
122                 if(!meshlink_start(mesh)) {
123                         fprintf(stderr, "Could not restart MeshLink: %s\n", meshlink_strerror(meshlink_errno));
124                         exit(1);
125                 }
126         } else if(!strcasecmp(buf, "kick")) {
127                 if(!arg) {
128                         fprintf(stderr, "/kick requires an argument!\n");
129                         return;
130                 }
131
132                 meshlink_node_t *node = meshlink_get_node(mesh, arg);
133
134                 if(!node) {
135                         fprintf(stderr, "Error looking up '%s': %s\n", arg, meshlink_strerror(meshlink_errno));
136                         return;
137                 }
138
139                 meshlink_blacklist(mesh, node);
140
141                 printf("Node '%s' blacklisted.\n", arg);
142         } else if(!strcasecmp(buf, "whitelist")) {
143                 if(!arg) {
144                         fprintf(stderr, "/whitelist requires an argument!\n");
145                         return;
146                 }
147
148                 meshlink_node_t *node = meshlink_get_node(mesh, arg);
149
150                 if(!node) {
151                         fprintf(stderr, "Error looking up '%s': %s\n", arg, meshlink_strerror(meshlink_errno));
152                         return;
153                 }
154
155                 meshlink_whitelist(mesh, node);
156
157                 printf("Node '%s' whitelisted.\n", arg);
158         } else if(!strcasecmp(buf, "who")) {
159                 if(!arg) {
160                         nodes = meshlink_get_all_nodes(mesh, nodes, &nnodes);
161
162                         if(!nnodes) {
163                                 fprintf(stderr, "Could not get list of nodes: %s\n", meshlink_strerror(meshlink_errno));
164                         } else {
165                                 printf("%zu known nodes:", nnodes);
166
167                                 for(size_t i = 0; i < nnodes; i++) {
168                                         printf(" %s", nodes[i]->name);
169                                 }
170
171                                 printf("\n");
172                         }
173                 } else {
174                         meshlink_node_t *node = meshlink_get_node(mesh, arg);
175
176                         if(!node) {
177                                 fprintf(stderr, "Error looking up '%s': %s\n", arg, meshlink_strerror(meshlink_errno));
178                         } else {
179                                 printf("Node %s found\n", arg);
180                         }
181                 }
182         } else if(!strcasecmp(buf, "quit")) {
183                 printf("Bye!\n");
184                 fclose(stdin);
185         } else if(!strcasecmp(buf, "help")) {
186                 printf(
187                         "<name>: <message>     Send a message to the given node.\n"
188                         "                      Subsequent messages don't need the <name>: prefix.\n"
189                         "/invite <name>        Create an invitation for a new node.\n"
190                         "/join <invitation>    Join an existing mesh using an invitation.\n"
191                         "/kick <name>          Blacklist the given node.\n"
192                         "/who [<name>]         List all nodes or show information about the given node.\n"
193                         "/quit                 Exit this program.\n"
194                 );
195         } else {
196                 fprintf(stderr, "Unknown command '/%s'\n", buf);
197         }
198 }
199
200 static void parse_input(meshlink_handle_t *mesh, char *buf) {
201         static meshlink_node_t *destination;
202         size_t len;
203
204         if(!buf) {
205                 return;
206         }
207
208         // Remove newline.
209
210         len = strlen(buf);
211
212         if(len && buf[len - 1] == '\n') {
213                 buf[--len] = 0;
214         }
215
216         if(len && buf[len - 1] == '\r') {
217                 buf[--len] = 0;
218         }
219
220         // Ignore empty lines.
221
222         if(!len) {
223                 return;
224         }
225
226         // Commands start with '/'
227
228         if(*buf == '/') {
229                 parse_command(mesh, buf + 1);
230                 return;
231         }
232
233         // Lines in the form "name: message..." set the destination node.
234
235         char *msg = buf;
236         char *colon = strchr(buf, ':');
237
238         if(colon) {
239                 *colon = 0;
240                 msg = colon + 1;
241
242                 if(*msg == ' ') {
243                         msg++;
244                 }
245
246                 destination = meshlink_get_node(mesh, buf);
247
248                 if(!destination) {
249                         fprintf(stderr, "Error looking up '%s': %s\n", buf, meshlink_strerror(meshlink_errno));
250                         return;
251                 }
252         }
253
254         if(!destination) {
255                 fprintf(stderr, "Who are you talking to? Write 'name: message...'\n");
256                 return;
257         }
258
259         // We want to have one channel per node.
260         // We keep the pointer to the meshlink_channel_t in the priv field of that node.
261         meshlink_channel_t *channel = destination->priv;
262
263         if(!channel) {
264                 fprintf(stderr, "Opening chat channel to '%s'\n", destination->name);
265                 channel = meshlink_channel_open(mesh, destination, CHAT_PORT, channel_receive, NULL, 0);
266
267                 if(!channel) {
268                         fprintf(stderr, "Could not create channel to '%s': %s\n", destination->name, meshlink_strerror(meshlink_errno));
269                         return;
270                 }
271
272                 destination->priv = channel;
273                 meshlink_set_channel_poll_cb(mesh, channel, channel_poll);
274         }
275
276         if(!meshlink_channel_send(mesh, channel, msg, strlen(msg))) {
277                 fprintf(stderr, "Could not send message to '%s': %s\n", destination->name, meshlink_strerror(meshlink_errno));
278                 return;
279         }
280
281         printf("Message sent to '%s'.\n", destination->name);
282 }
283
284 int main(int argc, char *argv[]) {
285         const char *confbase = ".chat";
286         const char *nick = NULL;
287         char buf[1024];
288
289         if(argc > 1) {
290                 confbase = argv[1];
291         }
292
293         if(argc > 2) {
294                 nick = argv[2];
295         }
296
297         meshlink_set_log_cb(NULL, MESHLINK_DEBUG, log_message);
298
299         meshlink_handle_t *mesh = meshlink_open(confbase, nick, "chat", DEV_CLASS_STATIONARY);
300
301         if(!mesh) {
302                 fprintf(stderr, "Could not open MeshLink: %s\n", meshlink_strerror(meshlink_errno));
303                 return 1;
304         }
305
306         meshlink_set_node_status_cb(mesh, node_status);
307         meshlink_set_log_cb(mesh, MESHLINK_INFO, log_message);
308
309         // Set the channel accept callback. This implicitly turns on channels for all nodes.
310         // This replaces the call to meshlink_set_receive_cb().
311         meshlink_set_channel_accept_cb(mesh, channel_accept);
312
313         if(!meshlink_start(mesh)) {
314                 fprintf(stderr, "Could not start MeshLink: %s\n", meshlink_strerror(meshlink_errno));
315                 return 1;
316         }
317
318         printf("Chat started.\nType /help for a list of commands.\n");
319
320         while(fgets(buf, sizeof(buf), stdin)) {
321                 parse_input(mesh, buf);
322         }
323
324         printf("Chat stopping.\n");
325
326         meshlink_stop(mesh);
327         meshlink_close(mesh);
328
329         return 0;
330 }