2 meshlink.c -- Implementation of the MeshLink API.
3 Copyright (C) 2014 Guus Sliepen <guus@meshlink.io>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 #include "meshlink_internal.h"
29 meshlink_handle_t *mesh;
31 static const char *errstr[] = {
32 [MESHLINK_OK] = "No error",
33 [MESHLINK_ENOMEM] = "Out of memory",
34 [MESHLINK_ENOENT] = "No such node",
37 const char *meshlink_strerror(meshlink_errno_t errno) {
41 // TODO: hack, remove once all global variables are gone.
42 static void set_mesh(meshlink_handle_t *localmesh) {
46 static meshlink_handle_t *meshlink_setup(meshlink_handle_t *mesh) {
51 meshlink_handle_t *meshlink_open(const char *confbase, const char *name) {
52 // Validate arguments provided by the application
54 if(!confbase || !*confbase) {
55 fprintf(stderr, "No confbase given!\n");
60 fprintf(stderr, "No name given!\n");
65 fprintf(stderr, "Invalid name given!\n");
69 meshlink_handle_t *mesh = xzalloc(sizeof *mesh);
70 mesh->confbase = xstrdup(confbase);
71 mesh->name = xstrdup(name);
73 // Check whether meshlink.conf already exists
75 char filename[PATH_MAX];
76 snprintf(filename, sizeof filename, "%s" SLASH "meshlink.conf", confbase);
78 if(access(filename, R_OK)) {
83 fprintf(stderr, "Cannot not read from %s: %s\n", filename, strerror(errno));
84 return meshlink_close(mesh), NULL;
88 // Read the configuration
90 init_configuration(&mesh->config);
92 if(!read_server_config())
93 return meshlink_close(mesh), NULL;
95 // Setup up everything
96 // TODO: we should not open listening sockets yet
99 return meshlink_close(mesh), NULL;
105 void *meshlink_main_loop(void *arg) {
106 meshlink_handle_t *mesh = arg;
108 try_outgoing_connections();
115 bool meshlink_start(meshlink_handle_t *mesh) {
116 // TODO: open listening sockets first
118 // Start the main thread
120 if(pthread_create(&mesh->thread, NULL, meshlink_main_loop, mesh) != 0) {
121 fprintf(stderr, "Could not start thread: %s\n", strerror(errno));
122 memset(&mesh->thread, 0, sizeof mesh->thread);
129 void meshlink_stop(meshlink_handle_t *mesh) {
130 // TODO: close the listening sockets to signal the main thread to shut down
132 // Wait for the main thread to finish
134 pthread_join(mesh->thread, NULL);
137 void meshlink_close(meshlink_handle_t *mesh) {
138 // Close and free all resources used.
140 close_network_connections();
142 logger(DEBUG_ALWAYS, LOG_NOTICE, "Terminating");
144 exit_configuration(&mesh->config);
148 void meshlink_set_receive_cb(meshlink_handle_t *mesh, meshlink_receive_cb_t cb) {
149 mesh->receive_cb = cb;
152 void meshlink_set_node_status_cb(meshlink_handle_t *mesh, meshlink_node_status_cb_t cb) {
153 mesh->node_status_cb = cb;
156 void meshlink_set_log_cb(meshlink_handle_t *mesh, meshlink_log_level_t level, meshlink_log_cb_t cb) {
158 mesh->log_level = level;
161 bool meshlink_send(meshlink_handle_t *mesh, meshlink_node_t *destination, const void *data, unsigned int len) {
163 meshlink_packethdr_t *hdr = (meshlink_packethdr_t *)packet.data;
164 if (sizeof(meshlink_packethdr_t) + len > MAXSIZE) {
169 packet.probe = false;
170 memset(hdr, 0, sizeof *hdr);
171 memcpy(hdr->destination, destination->name, sizeof hdr->destination);
172 memcpy(hdr->source, mesh->self->name, sizeof hdr->source);
174 packet.len = sizeof *hdr + len;
175 memcpy(packet.data + sizeof *hdr, data, len);
177 mesh->self->in_packets++;
178 mesh->self->in_bytes += packet.len;
179 route(mesh->self, &packet);
183 meshlink_node_t *meshlink_get_node(meshlink_handle_t *mesh, const char *name) {
184 return (meshlink_node_t *)lookup_node(name);
188 size_t meshlink_get_all_nodes(meshlink_handle_t *mesh, meshlink_node_t **nodes, size_t nmemb) {
192 char *meshlink_sign(meshlink_handle_t *mesh, const char *data, size_t len) {
196 bool meshlink_verify(meshlink_handle_t *mesh, meshlink_node_t *source, const char *data, size_t len, const char *signature) {
200 char *meshlink_invite(meshlink_handle_t *mesh, const char *name) {
204 bool meshlink_join(meshlink_handle_t *mesh, const char *invitation) {
208 char *meshlink_export(meshlink_handle_t *mesh) {
212 bool meshlink_import(meshlink_handle_t *mesh, const char *data) {
216 void meshlink_blacklist(meshlink_handle_t *mesh, meshlink_node_t *node) {