]> git.meshlink.io Git - meshlink/blob - src/meshlink.c
2c9b7e606faeee38b507e0a97cb24167ec6632c7
[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
22 #include "meshlink_internal.h"
23 #include "protocol.h"
24 #include "xalloc.h"
25
26 meshlink_handle_t *mesh;
27
28 static const char *errstr[] = {
29         [MESHLINK_OK] = "No error",
30         [MESHLINK_ENOMEM] = "Out of memory",
31         [MESHLINK_ENOENT] = "No such node",
32 };
33
34 const char *meshlink_strerror(meshlink_errno_t errno) {
35         return errstr[errno];
36 }
37
38 // TODO: hack, remove once all global variables are gone.
39 static void set_mesh(meshlink_handle_t *localmesh) {
40         mesh = localmesh;
41 }
42
43 static meshlink_handle_t *meshlink_setup(meshlink_handle_t *mesh) {
44         set_mesh(mesh);
45         return mesh;
46 }
47
48 meshlink_handle_t *meshlink_open(const char *confbase, const char *name) {
49         if(!confbase || !*confbase) {
50                 fprintf(stderr, "No confbase given!\n");
51                 return NULL;
52         }
53
54         if(!name || !*name) {
55                 fprintf(stderr, "No name given!\n");
56                 return NULL;
57         }
58
59         if(!check_id(name)) {
60                 fprintf(stderr, "Invalid name given!\n");
61                 return NULL;
62         }
63
64         meshlink_handle_t *mesh = xzalloc(sizeof *mesh);
65         mesh->confbase = xstrdup(confbase);
66         mesh->name = xstrdup(name);
67
68         char filename[PATH_MAX];
69         snprintf(filename, sizeof filename, "%s" SLASH "meshlink.conf", confbase);
70
71         FILE *f = fopen(filename, "r");
72
73         if(!f && errno == ENOENT)
74                 return meshlink_setup(mesh);
75
76         if(!f) {
77                 fprintf(stderr, "Could not open %s: %s\n", filename, strerror(errno));
78                 return meshlink_close(mesh), NULL;
79         }
80
81         char buf[1024] = "";
82         if(!fgets(buf, sizeof buf, f)) {
83                 fprintf(stderr, "Could not read line from %s: %s\n", filename, strerror(errno));
84                 fclose(f);
85                 return meshlink_close(mesh), NULL;
86         }
87
88         fclose(f);
89
90         size_t len = strlen(buf);
91         if(len && buf[len - 1] == '\n')
92                 buf[--len] = 0;
93         if(len && buf[len - 1] == '\r')
94                 buf[--len] = 0;
95
96         if(strncmp(buf, "Name = ", 7) || !check_id(buf + 7)) {
97                 fprintf(stderr, "Could not read Name from %s\n", filename);
98                 return meshlink_close(mesh), NULL;
99         }
100
101         if(strcmp(buf + 7, name)) {
102                 fprintf(stderr, "Name in %s is %s, not the same as %s\n", filename, buf + 7, name);
103                 free(mesh->name);
104                 mesh->name = xstrdup(buf + 7);
105         }
106
107         snprintf(filename, sizeof filename, "%s" SLASH "ed25519_key.priv", mesh->confbase);
108         f = fopen(filename, "r");
109         if(!f) {
110                 fprintf(stderr, "Could not open %s: %s\n", filename, strerror(errno));
111                 return meshlink_close(mesh), NULL;
112         }
113
114         mesh->self->ecdsa = ecdsa_read_pem_private_key(f);
115         fclose(f);
116
117         if(!mesh->self->ecdsa) {
118                 fprintf(stderr, "Could not read keypair!\n");
119                 return meshlink_close(mesh), NULL;
120         }
121
122         set_mesh(mesh);
123         return mesh;
124 }
125
126 bool meshlink_start(meshlink_handle_t *mesh) {
127         return false;
128 }
129
130 void meshlink_stop(meshlink_handle_t *mesh) {
131 }
132
133 void meshlink_close(meshlink_handle_t *mesh) {
134 }
135
136 void meshlink_set_receive_cb(meshlink_handle_t *mesh, meshlink_receive_cb_t cb) {
137         mesh->receive_cb = cb;
138 }
139
140 void meshlink_set_node_status_cb(meshlink_handle_t *mesh, meshlink_node_status_cb_t cb) {
141         mesh->node_status_cb = cb;
142 }
143
144 void meshlink_set_log_cb(meshlink_handle_t *mesh, meshlink_log_level_t level, meshlink_log_cb_t cb) {
145         mesh->log_cb = cb;
146         mesh->log_level = level;
147 }
148
149 bool meshlink_send(meshlink_handle_t *mesh, meshlink_node_t *destination, const void *data, unsigned int len) {
150         return false;
151 }
152
153 meshlink_node_t *meshlink_get_node(meshlink_handle_t *mesh, const char *name) {
154         return NULL;
155 }
156
157 size_t meshlink_get_all_nodes(meshlink_handle_t *mesh, meshlink_node_t **nodes, size_t nmemb) {
158         return 0;
159 }
160
161 char *meshlink_sign(meshlink_handle_t *mesh, const char *data, size_t len) {
162         return NULL;
163 }
164
165 bool meshlink_verify(meshlink_handle_t *mesh, meshlink_node_t *source, const char *data, size_t len, const char *signature) {
166         return false;
167 }
168
169 char *meshlink_invite(meshlink_handle_t *mesh, const char *name) {
170         return NULL;
171 }
172
173 bool meshlink_join(meshlink_handle_t *mesh, const char *invitation) {
174         return false;
175 }
176
177 char *meshlink_export(meshlink_handle_t *mesh) {
178         return NULL;
179 }
180
181 bool meshlink_import(meshlink_handle_t *mesh, const char *data) {
182         return false;
183 }
184
185 void meshlink_blacklist(meshlink_handle_t *mesh, meshlink_node_t *node) {
186 }
187