]> git.meshlink.io Git - meshlink/blob - src/libmeshlink.c
04e3141561b4aa1d582562872de17278d82d69f9
[meshlink] / src / libmeshlink.c
1 /*
2     libmeshlink.h -- Tincd Library
3     Copyright (C) 2014 Guus Sliepen <guus@tinc-vpn.org> Saverio Proto <zioproto@gmail.com>
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 "libmeshlink.h"
21
22 //tinc_setup() should basically do what cmd_init() from src/tincctl.c does, except it doesn't have to generate a tinc-up script.
23 bool tinc_setup(const char* tinc_conf, const char* name) {
24         if(!access(tinc_conf, F_OK)) {
25                 fprintf(stderr, "Configuration file %s already exists!\n", tinc_conf);
26                 return false;
27         }
28
29         if(!check_id(name)) {
30                 fprintf(stderr, "Invalid Name! Only a-z, A-Z, 0-9 and _ are allowed characters.\n");
31                 return false;
32         }
33
34         if(!confbase_given && mkdir(confdir, 0755) && errno != EEXIST) {
35                 fprintf(stderr, "Could not create directory %s: %s\n", confdir, strerror(errno));
36                 return false;
37         }
38
39         if(mkdir(confbase, 0777) && errno != EEXIST) {
40                 fprintf(stderr, "Could not create directory %s: %s\n", confbase, strerror(errno));
41                 return false;
42         }
43
44         if(mkdir(hosts_dir, 0777) && errno != EEXIST) {
45                 fprintf(stderr, "Could not create directory %s: %s\n", hosts_dir, strerror(errno));
46                 return false;
47         }
48
49         FILE *f = fopen(tinc_conf, "w");
50         if(!f) {
51                 fprintf(stderr, "Could not create file %s: %s\n", tinc_conf, strerror(errno));
52                 return 1;
53         }
54
55         fprintf(f, "Name = %s\n", name);
56         fclose(f);
57
58         if(!rsa_keygen(2048, false) || !ecdsa_keygen(false))
59                 return false;
60
61         check_port(name);
62
63         return true;
64
65 }
66
67
68 bool tinc_start(const char* path);
69
70 bool tinc_stop();
71
72 // can be called from any thread
73 bool tinc_send_packet(node_t *receiver, const char* buf, unsigned int len);
74
75 // handler runs in tinc thread and should return immediately
76 bool tinc_set_packet_receive_handler(void (*handler)(const char* sender, const char* buf, unsigned int len));
77
78
79 //It might also be a good idea to add the option of looking up hosts by public
80 //key (fingerprints) instead of names.
81
82 node_t *tinc_get_host(const char *name);
83
84 bool tinc_get_hosts(node_t** hosts);
85
86 bool tinc_sign(const char* payload, unsigned int len, const char** signature);
87
88 int tinc_verify(const char* sender, const char* payload, unsigned int plen, const char* signature, unsigned int slen);
89
90 /*
91 TODO: It would be good to add a void pointer here that will be passed on to the
92 handler function whenever it is called, or have a void pointer in node_t
93 that can be filled in by the application. That way, you can easily link an
94 application-specific data structure to a node_t.
95 */
96 void channel_set_packet_send_handler(int (*handler)(const char* receiver, const char* buf, unsigned int len));
97 void channel_packet_receive_handler(const char* sender, const char* buf, unsigned int len);
98
99 bool channel_open(const char* partner, void(*read)(int id, const char* buf, unsigned int len), void(*result)(int result, int id));
100 void channel_close(int id);
101 bool channel_write(int id, const char* buf, unsigned int len, void(*result)(int result, int id, unsigned int written));
102
103
104 //We do need some more functions. First of all, we need to be able to add nodes
105 //to a VPN. To do that, either an invitation protocol should be used:
106
107 bool tinc_join_network(const char *invitation);
108 const char *tinc_generate_invitation(const char *name);
109
110 /*
111 Or two nodes should exchange some information (their name, address, public
112 key). If the application provides a way to exchange some data with another
113 node, then:
114 */
115
116 bool tinc_export(char *buf, size_t *len);
117 node_t *tinc_import(const char *buf, size_t len);
118 /*
119 Last but not least, some way to get rid of unwanted nodes. Simplest is a
120 function that just blacklists a node.
121 Which should immediately cause the local tincd to ignore any data from that
122 host from that point on. Of course, a somewhat centrally managed,
123 automatically distributed blacklist or whitelist would be the next step.
124 */
125 bool tinc_blacklist(node_t *host);
126
127
128
129