2 libmeshlink.h -- Tincd Library
3 Copyright (C) 2014 Guus Sliepen <guus@tinc-vpn.org> Saverio Proto <zioproto@gmail.com>
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.
20 #include "libmeshlink.h"
23 char *hosts_dir = NULL;
24 static char *name = NULL;
28 Generate a public/private ECDSA keypair, and ask for a file to store
31 bool ecdsa_keygen(bool ask) {
34 char *pubname, *privname;
36 fprintf(stderr, "Generating ECDSA keypair:\n");
38 if(!(key = ecdsa_generate())) {
39 fprintf(stderr, "Error during key generation!\n");
42 fprintf(stderr, "Done.\n");
44 xasprintf(&privname, "%s" SLASH "ecdsa_key.priv", confbase);
45 //f = ask_and_open(privname, "private ECDSA key", "a", ask, 0600); //this function is not ported to lib because makes no sense
51 if(!ecdsa_write_pem_private_key(key, f)) {
52 fprintf(stderr, "Error writing private key!\n");
61 xasprintf(&pubname, "%s" SLASH "hosts" SLASH "%s", confbase, name);
63 xasprintf(&pubname, "%s" SLASH "ecdsa_key.pub", confbase);
65 //f = ask_and_open(pubname, "public ECDSA key", "a", ask, 0666);
71 char *pubkey = ecdsa_get_base64_public_key(key);
72 fprintf(f, "ECDSAPublicKey = %s\n", pubkey);
82 Generate a public/private RSA keypair, and ask for a file to store
85 bool rsa_keygen(int bits, bool ask) {
88 char *pubname, *privname;
90 fprintf(stderr, "Generating %d bits keys:\n", bits);
92 if(!(key = rsa_generate(bits, 0x10001))) {
93 fprintf(stderr, "Error during key generation!\n");
96 fprintf(stderr, "Done.\n");
98 xasprintf(&privname, "%s" SLASH "rsa_key.priv", confbase);
99 //f = ask_and_open(privname, "private RSA key", "a", ask, 0600);
105 if(!rsa_write_pem_private_key(key, f)) {
106 fprintf(stderr, "Error writing private key!\n");
115 xasprintf(&pubname, "%s" SLASH "hosts" SLASH "%s", confbase, name);
117 xasprintf(&pubname, "%s" SLASH "rsa_key.pub", confbase);
119 //f = ask_and_open(pubname, "public RSA key", "a", ask, 0666);
125 if(!rsa_write_pem_public_key(key, f)) {
126 fprintf(stderr, "Error writing public key!\n");
138 static bool try_bind(int port) {
139 struct addrinfo *ai = NULL;
140 struct addrinfo hint = {
141 .ai_flags = AI_PASSIVE,
142 .ai_family = AF_UNSPEC,
143 .ai_socktype = SOCK_STREAM,
144 .ai_protocol = IPPROTO_TCP,
148 snprintf(portstr, sizeof portstr, "%d", port);
150 if(getaddrinfo(NULL, portstr, &hint, &ai) || !ai)
154 int fd = socket(ai->ai_family, SOCK_STREAM, IPPROTO_TCP);
157 int result = bind(fd, ai->ai_addr, ai->ai_addrlen);
167 int check_port(char *name) {
171 fprintf(stderr, "Warning: could not bind to port 655. ");
173 for(int i = 0; i < 100; i++) {
174 int port = 0x1000 + (rand() & 0x7fff);
177 xasprintf(&filename, "%s" SLASH "hosts" SLASH "%s", confbase, name);
178 FILE *f = fopen(filename, "a");
181 fprintf(stderr, "Please change tinc's Port manually.\n");
185 fprintf(f, "Port = %d\n", port);
187 fprintf(stderr, "Tinc will instead listen on port %d.\n", port);
192 fprintf(stderr, "Please change tinc's Port manually.\n");
195 //tinc_setup() should basically do what cmd_init() from src/tincctl.c does, except it doesn't have to generate a tinc-up script.
196 bool tinc_setup(const char* tinc_conf, const char* name) {
198 if(!access(tinc_conf, F_OK)) {
199 fprintf(stderr, "Configuration file %s already exists!\n", tinc_conf);
203 if(!check_id(name)) {
204 fprintf(stderr, "Invalid Name! Only a-z, A-Z, 0-9 and _ are allowed characters.\n");
208 if(!confbase_given && mkdir(confdir, 0755) && errno != EEXIST) {
209 fprintf(stderr, "Could not create directory %s: %s\n", confdir, strerror(errno));
213 if(mkdir(confbase, 0777) && errno != EEXIST) {
214 fprintf(stderr, "Could not create directory %s: %s\n", confbase, strerror(errno));
218 if(mkdir(hosts_dir, 0777) && errno != EEXIST) {
219 fprintf(stderr, "Could not create directory %s: %s\n", hosts_dir, strerror(errno));
223 FILE *f = fopen(tinc_conf, "w");
225 fprintf(stderr, "Could not create file %s: %s\n", tinc_conf, strerror(errno));
229 fprintf(f, "Name = %s\n", name);
232 if(!rsa_keygen(2048, false) || !ecdsa_keygen(false))
242 bool tinc_start(const char* path);
246 // can be called from any thread
247 bool tinc_send_packet(node_t *receiver, const char* buf, unsigned int len);
249 // handler runs in tinc thread and should return immediately
250 bool tinc_set_packet_receive_handler(void (*handler)(const char* sender, const char* buf, unsigned int len));
253 //It might also be a good idea to add the option of looking up hosts by public
254 //key (fingerprints) instead of names.
256 node_t *tinc_get_host(const char *name);
258 bool tinc_get_hosts(node_t** hosts);
260 bool tinc_sign(const char* payload, unsigned int len, const char** signature);
262 int tinc_verify(const char* sender, const char* payload, unsigned int plen, const char* signature, unsigned int slen);
265 TODO: It would be good to add a void pointer here that will be passed on to the
266 handler function whenever it is called, or have a void pointer in node_t
267 that can be filled in by the application. That way, you can easily link an
268 application-specific data structure to a node_t.
270 void channel_set_packet_send_handler(int (*handler)(const char* receiver, const char* buf, unsigned int len));
271 void channel_packet_receive_handler(const char* sender, const char* buf, unsigned int len);
273 bool channel_open(const char* partner, void(*read)(int id, const char* buf, unsigned int len), void(*result)(int result, int id));
274 void channel_close(int id);
275 bool channel_write(int id, const char* buf, unsigned int len, void(*result)(int result, int id, unsigned int written));
278 //We do need some more functions. First of all, we need to be able to add nodes
279 //to a VPN. To do that, either an invitation protocol should be used:
281 bool tinc_join_network(const char *invitation);
282 const char *tinc_generate_invitation(const char *name);
285 Or two nodes should exchange some information (their name, address, public
286 key). If the application provides a way to exchange some data with another
290 bool tinc_export(char *buf, size_t *len);
291 node_t *tinc_import(const char *buf, size_t len);
293 Last but not least, some way to get rid of unwanted nodes. Simplest is a
294 function that just blacklists a node.
295 Which should immediately cause the local tincd to ignore any data from that
296 host from that point on. Of course, a somewhat centrally managed,
297 automatically distributed blacklist or whitelist would be the next step.
299 bool tinc_blacklist(node_t *host);