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