]> git.meshlink.io Git - meshlink/blob - src/meshlink.c
Move code from libmeshlink.c into the new functions in meshlink.c.
[meshlink] / src / meshlink.c
1 /*
2     meshlink.c -- Implementation of the MeshLink API.
3     Copyright (C) 2014 Guus Sliepen <guus@meshlink.io>
4
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.
9
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.
14
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.
18 */
19
20 #include "system.h"
21 #include <pthread.h>
22
23 #include "meshlink_internal.h"
24 #include "node.h"
25 #include "protocol.h"
26 #include "route.h"
27 #include "xalloc.h"
28
29 meshlink_handle_t *mesh;
30
31 static const char *errstr[] = {
32         [MESHLINK_OK] = "No error",
33         [MESHLINK_ENOMEM] = "Out of memory",
34         [MESHLINK_ENOENT] = "No such node",
35 };
36
37 const char *meshlink_strerror(meshlink_errno_t errno) {
38         return errstr[errno];
39 }
40
41 // TODO: hack, remove once all global variables are gone.
42 static void set_mesh(meshlink_handle_t *localmesh) {
43         mesh = localmesh;
44 }
45
46 static meshlink_handle_t *meshlink_setup(meshlink_handle_t *mesh) {
47         set_mesh(mesh);
48         return mesh;
49 }
50
51 meshlink_handle_t *meshlink_open(const char *confbase, const char *name) {
52         // Validate arguments provided by the application
53
54         if(!confbase || !*confbase) {
55                 fprintf(stderr, "No confbase given!\n");
56                 return NULL;
57         }
58
59         if(!name || !*name) {
60                 fprintf(stderr, "No name given!\n");
61                 return NULL;
62         }
63
64         if(!check_id(name)) {
65                 fprintf(stderr, "Invalid name given!\n");
66                 return NULL;
67         }
68
69         meshlink_handle_t *mesh = xzalloc(sizeof *mesh);
70         mesh->confbase = xstrdup(confbase);
71         mesh->name = xstrdup(name);
72
73         // Check whether meshlink.conf already exists
74
75         char filename[PATH_MAX];
76         snprintf(filename, sizeof filename, "%s" SLASH "meshlink.conf", confbase);
77
78         if(access(filename, R_OK)) {
79                 if(errno == ENOENT) {
80                         // If not, create it
81                         meshlink_setup(mesh);
82                 } else {
83                         fprintf(stderr, "Cannot not read from %s: %s\n", filename, strerror(errno));
84                         return meshlink_close(mesh), NULL;
85                 }
86         }
87
88         // Read the configuration
89
90         init_configuration(&mesh->config);
91
92         if(!read_server_config())
93                 return meshlink_close(mesh), NULL;
94
95         // Setup up everything
96         // TODO: we should not open listening sockets yet
97
98         if(!setup_network())
99                 return meshlink_close(mesh), NULL;
100
101         set_mesh(mesh);
102         return mesh;
103 }
104
105 void *meshlink_main_loop(void *arg) {
106         meshlink_handle_t *mesh = arg;
107
108         try_outgoing_connections();
109
110         main_loop();
111
112         return NULL;
113 }
114
115 bool meshlink_start(meshlink_handle_t *mesh) {
116         // TODO: open listening sockets first
117
118         // Start the main thread
119
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);
123                 return false;
124         }
125
126         return true;
127 }
128
129 void meshlink_stop(meshlink_handle_t *mesh) {
130         // TODO: close the listening sockets to signal the main thread to shut down
131
132         // Wait for the main thread to finish
133
134         pthread_join(mesh->thread, NULL);
135 }
136
137 void meshlink_close(meshlink_handle_t *mesh) {
138         // Close and free all resources used.
139
140         close_network_connections();
141
142         logger(DEBUG_ALWAYS, LOG_NOTICE, "Terminating");
143
144         exit_configuration(&mesh->config);
145
146 }
147
148 void meshlink_set_receive_cb(meshlink_handle_t *mesh, meshlink_receive_cb_t cb) {
149         mesh->receive_cb = cb;
150 }
151
152 void meshlink_set_node_status_cb(meshlink_handle_t *mesh, meshlink_node_status_cb_t cb) {
153         mesh->node_status_cb = cb;
154 }
155
156 void meshlink_set_log_cb(meshlink_handle_t *mesh, meshlink_log_level_t level, meshlink_log_cb_t cb) {
157         mesh->log_cb = cb;
158         mesh->log_level = level;
159 }
160
161 bool meshlink_send(meshlink_handle_t *mesh, meshlink_node_t *destination, const void *data, unsigned int len) {
162         vpn_packet_t packet;
163         meshlink_packethdr_t *hdr = (meshlink_packethdr_t *)packet.data;
164         if (sizeof(meshlink_packethdr_t) + len > MAXSIZE) {
165                 //log something
166                 return false;
167         }
168
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);
173
174         packet.len = sizeof *hdr + len;
175         memcpy(packet.data + sizeof *hdr, data, len);
176
177         mesh->self->in_packets++;
178         mesh->self->in_bytes += packet.len;
179         route(mesh->self, &packet);
180         return false;
181 }
182
183 meshlink_node_t *meshlink_get_node(meshlink_handle_t *mesh, const char *name) {
184         return (meshlink_node_t *)lookup_node(name);
185         return NULL;
186 }
187
188 size_t meshlink_get_all_nodes(meshlink_handle_t *mesh, meshlink_node_t **nodes, size_t nmemb) {
189         return 0;
190 }
191
192 char *meshlink_sign(meshlink_handle_t *mesh, const char *data, size_t len) {
193         return NULL;
194 }
195
196 bool meshlink_verify(meshlink_handle_t *mesh, meshlink_node_t *source, const char *data, size_t len, const char *signature) {
197         return false;
198 }
199
200 char *meshlink_invite(meshlink_handle_t *mesh, const char *name) {
201         return NULL;
202 }
203
204 bool meshlink_join(meshlink_handle_t *mesh, const char *invitation) {
205         return false;
206 }
207
208 char *meshlink_export(meshlink_handle_t *mesh) {
209         return NULL;
210 }
211
212 bool meshlink_import(meshlink_handle_t *mesh, const char *data) {
213         return false;
214 }
215
216 void meshlink_blacklist(meshlink_handle_t *mesh, meshlink_node_t *node) {
217 }
218