-
Notifications
You must be signed in to change notification settings - Fork 0
/
cpn-discover.c
202 lines (170 loc) · 5.91 KB
/
cpn-discover.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/*
* Copyright (C) 2016 Patrick Steinhardt
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <poll.h>
#include "capone/cfg.h"
#include "capone/client.h"
#include "capone/common.h"
#include "capone/global.h"
#include "capone/list.h"
#include "capone/log.h"
#include "capone/opts.h"
#include "capone/socket.h"
#include "capone/proto/discovery.pb-c.h"
#define LISTEN_PORT 6668
static struct cpn_sign_keys local_keys;
static void print_announcement(struct cpn_discovery_results *announce)
{
struct cpn_sign_pk_hex hex;
uint32_t i;
cpn_sign_pk_hex_from_key(&hex, &announce->identity);
printf("%s - %s (v%"PRIu32")\n", announce->name, hex.data, announce->version);
for (i = 0; i < announce->nservices; i++) {
printf("\t%"PRIu32" -> %s (%s)\n", announce->services[i].port,
announce->services[i].name, announce->services[i].category);
}
}
static void undirected_discovery()
{
struct cpn_list known_keys = CPN_LIST_INIT;
struct cpn_channel channel;
channel.fd = -1;
if (cpn_channel_init_from_host(&channel, "224.0.0.1", 6667, CPN_CHANNEL_TYPE_UDP) < 0) {
puts("Unable to initialize channel");
goto out;
}
while (true) {
if (cpn_client_discovery_probe(&channel, &known_keys) < 0) {
puts("Unable to write protobuf");
goto out;
} else {
cpn_log(LOG_LEVEL_DEBUG, "Sent probe message");
}
while (true) {
struct cpn_discovery_results results;
struct cpn_sign_pk *key;
struct cpn_list_entry *it;
struct pollfd pfd[1];
int err;
pfd[0].fd = channel.fd;
pfd[0].events = POLLIN;
err = poll(pfd, 1, 5000);
if (err < 0) {
printf("Unable to await announcement: %s", strerror(errno));
goto out;
} else if (err == 0) {
break;
} else if (cpn_client_discovery_handle_announce(&results, &channel) < 0) {
puts("Unable to handle announce");
continue;
}
cpn_list_foreach(&known_keys, it, key) {
if (!memcmp(key->data, results.identity.data,
sizeof(struct cpn_sign_pk)))
{
struct cpn_sign_pk_hex hex;
cpn_sign_pk_hex_from_key(&hex, &results.identity);
cpn_log(LOG_LEVEL_DEBUG, "Ignoring known key %s", hex.data);
continue;
}
}
key = malloc(sizeof(struct cpn_sign_pk));
memcpy(key, results.identity.data, sizeof(struct cpn_sign_pk));
cpn_list_append(&known_keys, key);
print_announcement(&results);
cpn_discovery_results_clear(&results);
}
}
out:
cpn_channel_close(&channel);
}
static void directed_discovery(const struct cpn_sign_pk *remote_key,
const char *host, uint32_t port)
{
struct cpn_discovery_results results;
struct cpn_channel channel;
if (cpn_client_connect(&channel, host, port, &local_keys, remote_key) < 0) {
puts("Unable to connect");
}
if (cpn_client_discovery_probe(&channel, NULL) < 0) {
puts("Unable to send directed discover");
goto out;
}
if (cpn_client_discovery_handle_announce(&results, &channel) < 0) {
puts("Unable to handle announce");
goto out;
}
print_announcement(&results);
cpn_discovery_results_clear(&results);
out:
cpn_channel_close(&channel);
}
int main(int argc, const char *argv[])
{
static struct cpn_opt directed_opts[] = {
CPN_OPTS_OPT_SIGKEY(0, "--remote-key",
"Public signature key of the host to query", "KEY", false),
CPN_OPTS_OPT_STRING(0, "--remote-host",
"Network address of the host to query", "ADDRESS", false),
CPN_OPTS_OPT_UINT32(0, "--remote-port",
"Port of the host to query", "PORT", false),
CPN_OPTS_OPT_END
};
struct cpn_opt opts[] = {
CPN_OPTS_OPT_STRING('c', "--config", "Configuration file", "FILE", false),
CPN_OPTS_OPT_ACTION("broadcast", NULL, NULL),
CPN_OPTS_OPT_ACTION("direct", NULL, directed_opts),
CPN_OPTS_OPT_COUNTER('v', "--verbose", "Verbosity"),
CPN_OPTS_OPT_END
};
if (cpn_global_init() < 0)
return -1;
if (cpn_opts_parse_cmd(opts, argc, argv) < 0)
return -1;
if (opts[3].set) {
switch (opts[3].value.counter) {
case 0:
cpn_log_set_level(LOG_LEVEL_ERROR);
break;
case 1:
cpn_log_set_level(LOG_LEVEL_WARNING);
break;
case 2:
cpn_log_set_level(LOG_LEVEL_VERBOSE);
break;
case 3:
cpn_log_set_level(LOG_LEVEL_TRACE);
break;
default:
break;
}
}
if (opts[1].set) {
undirected_discovery();
} else if (opts[2].set) {
directed_discovery(&directed_opts[0].value.sigkey,
directed_opts[1].value.string,
directed_opts[2].value.uint32);
} else {
puts("No action specified");
}
return 0;
}