git log > ChangeLog
astyle:
- astyle --options=.astylerc -nQ src/*.[ch] src/ed25519/e*.[ch]
+ astyle --options=.astylerc -nQ src/*.[ch] src/ed25519/e*.[ch] examples/*.[ch] examples/*.cc test/*.[ch]
static void channel_receive(meshlink_handle_t *mesh, meshlink_channel_t *channel, const void *data, size_t len) {
if(!len) {
- if(meshlink_errno)
+ if(meshlink_errno) {
fprintf(stderr, "Error while reading data from %s: %s\n", channel->node->name, meshlink_strerror(meshlink_errno));
- else
+ } else {
fprintf(stderr, "Chat connection closed by %s\n", channel->node->name);
+ }
channel->node->priv = NULL;
meshlink_channel_close(mesh, channel);
}
static void node_status(meshlink_handle_t *mesh, meshlink_node_t *node, bool reachable) {
- if(reachable)
+ if(reachable) {
printf("%s joined.\n", node->name);
- else
+ } else {
printf("%s left.\n", node->name);
+ }
}
static meshlink_node_t **nodes;
static void parse_command(meshlink_handle_t *mesh, char *buf) {
char *arg = strchr(buf, ' ');
- if(arg)
+
+ if(arg) {
*arg++ = 0;
+ }
if(!strcasecmp(buf, "invite")) {
char *invitation;
}
invitation = meshlink_invite(mesh, arg);
+
if(!invitation) {
fprintf(stderr, "Could not invite '%s': %s\n", arg, meshlink_strerror(meshlink_errno));
return;
fprintf(stderr, "/join requires an argument!\n");
return;
}
+
meshlink_stop(mesh);
- if(!meshlink_join(mesh, arg))
+
+ if(!meshlink_join(mesh, arg)) {
fprintf(stderr, "Could not join using invitation: %s\n", meshlink_strerror(meshlink_errno));
- else
+ } else {
fprintf(stderr, "Invitation accepted!\n");
+ }
+
if(!meshlink_start(mesh)) {
fprintf(stderr, "Could not restart MeshLink: %s\n", meshlink_strerror(meshlink_errno));
exit(1);
}
meshlink_node_t *node = meshlink_get_node(mesh, arg);
+
if(!node) {
fprintf(stderr, "Error looking up '%s': %s\n", arg, meshlink_strerror(meshlink_errno));
return;
} else if(!strcasecmp(buf, "who")) {
if(!arg) {
nodes = meshlink_get_all_nodes(mesh, nodes, &nnodes);
- if(!nnodes)
+
+ if(!nnodes) {
fprintf(stderr, "Could not get list of nodes: %s\n", meshlink_strerror(meshlink_errno));
- else {
+ } else {
printf("%zu known nodes:", nnodes);
- for(int i = 0; i < nnodes; i++)
+
+ for(int i = 0; i < nnodes; i++) {
printf(" %s", nodes[i]->name);
+ }
+
printf("\n");
}
} else {
meshlink_node_t *node = meshlink_get_node(mesh, arg);
- if(!node)
+
+ if(!node) {
fprintf(stderr, "Error looking up '%s': %s\n", arg, meshlink_strerror(meshlink_errno));
- else
+ } else {
printf("Node %s found\n", arg);
+ }
}
} else if(!strcasecmp(buf, "quit")) {
printf("Bye!\n");
"/who [<name>] List all nodes or show information about the given node.\n"
"/quit Exit this program.\n"
);
- } else
+ } else {
fprintf(stderr, "Unknown command '/%s'\n", buf);
+ }
}
static void parse_input(meshlink_handle_t *mesh, char *buf) {
static meshlink_node_t *destination;
size_t len;
- if(!buf)
+ if(!buf) {
return;
+ }
// Remove newline.
len = strlen(buf);
- if(len && buf[len - 1] == '\n')
+ if(len && buf[len - 1] == '\n') {
buf[--len] = 0;
+ }
- if(len && buf[len - 1] == '\r')
+ if(len && buf[len - 1] == '\r') {
buf[--len] = 0;
+ }
// Ignore empty lines.
- if(!len)
+ if(!len) {
return;
+ }
// Commands start with '/'
- if(*buf == '/')
+ if(*buf == '/') {
return parse_command(mesh, buf + 1);
+ }
// Lines in the form "name: message..." set the destination node.
if(colon) {
*colon = 0;
msg = colon + 1;
- if(*msg == ' ')
+
+ if(*msg == ' ') {
msg++;
+ }
destination = meshlink_get_node(mesh, buf);
+
if(!destination) {
fprintf(stderr, "Error looking up '%s': %s\n", buf, meshlink_strerror(meshlink_errno));
return;
if(!channel) {
fprintf(stderr, "Opening chat channel to '%s'\n", destination->name);
channel = meshlink_channel_open(mesh, destination, CHAT_PORT, channel_receive, NULL, 0);
+
if(!channel) {
fprintf(stderr, "Could not create channel to '%s': %s\n", destination->name, meshlink_strerror(meshlink_errno));
return;
}
+
destination->priv = channel;
meshlink_set_channel_poll_cb(mesh, channel, channel_poll);
}
const char *nick = NULL;
char buf[1024];
- if(argc > 1)
+ if(argc > 1) {
confbase = argv[1];
+ }
- if(argc > 2)
+ if(argc > 2) {
nick = argv[2];
+ }
meshlink_set_log_cb(NULL, MESHLINK_DEBUG, log_message);
meshlink_handle_t *mesh = meshlink_open(confbase, nick, "chat", DEV_CLASS_STATIONARY);
+
if(!mesh) {
fprintf(stderr, "Could not open MeshLink: %s\n", meshlink_strerror(meshlink_errno));
return 1;
printf("Chat started.\nType /help for a list of commands.\n");
- while(fgets(buf, sizeof(buf), stdin))
+ while(fgets(buf, sizeof(buf), stdin)) {
parse_input(mesh, buf);
+ }
printf("Chat stopping.\n");
}
static void node_status(meshlink_handle_t *mesh, meshlink_node_t *node, bool reachable) {
- if(reachable)
+ if(reachable) {
printf("%s joined.\n", node->name);
- else
+ } else {
printf("%s left.\n", node->name);
+ }
}
static meshlink_node_t **nodes;
static void parse_command(meshlink_handle_t *mesh, char *buf) {
char *arg = strchr(buf, ' ');
- if(arg)
+
+ if(arg) {
*arg++ = 0;
+ }
if(!strcasecmp(buf, "invite")) {
char *invitation;
}
invitation = meshlink_invite(mesh, arg);
+
if(!invitation) {
fprintf(stderr, "Could not invite '%s': %s\n", arg, meshlink_strerror(meshlink_errno));
return;
fprintf(stderr, "/join requires an argument!\n");
return;
}
+
meshlink_stop(mesh);
- if(!meshlink_join(mesh, arg))
+
+ if(!meshlink_join(mesh, arg)) {
fprintf(stderr, "Could not join using invitation: %s\n", meshlink_strerror(meshlink_errno));
- else
+ } else {
fprintf(stderr, "Invitation accepted!\n");
+ }
+
if(!meshlink_start(mesh)) {
fprintf(stderr, "Could not restart MeshLink: %s\n", meshlink_strerror(meshlink_errno));
exit(1);
}
meshlink_node_t *node = meshlink_get_node(mesh, arg);
+
if(!node) {
fprintf(stderr, "Error looking up '%s': %s\n", arg, meshlink_strerror(meshlink_errno));
return;
} else if(!strcasecmp(buf, "who")) {
if(!arg) {
nodes = meshlink_get_all_nodes(mesh, nodes, &nnodes);
- if(!nnodes)
+
+ if(!nnodes) {
fprintf(stderr, "Could not get list of nodes: %s\n", meshlink_strerror(meshlink_errno));
- else {
+ } else {
printf("%zu known nodes:", nnodes);
- for(int i = 0; i < nnodes; i++)
+
+ for(int i = 0; i < nnodes; i++) {
printf(" %s", nodes[i]->name);
+ }
+
printf("\n");
}
} else {
meshlink_node_t *node = meshlink_get_node(mesh, arg);
- if(!node)
+
+ if(!node) {
fprintf(stderr, "Error looking up '%s': %s\n", arg, meshlink_strerror(meshlink_errno));
- else
+ } else {
printf("Node %s found\n", arg);
+ }
}
} else if(!strcasecmp(buf, "quit")) {
printf("Bye!\n");
"/who [<name>] List all nodes or show information about the given node.\n"
"/quit Exit this program.\n"
);
- } else
+ } else {
fprintf(stderr, "Unknown command '/%s'\n", buf);
+ }
}
static void parse_input(meshlink_handle_t *mesh, char *buf) {
static meshlink_node_t *destination;
size_t len;
- if(!buf)
+ if(!buf) {
return;
+ }
// Remove newline.
len = strlen(buf);
- if(len && buf[len - 1] == '\n')
+ if(len && buf[len - 1] == '\n') {
buf[--len] = 0;
+ }
- if(len && buf[len - 1] == '\r')
+ if(len && buf[len - 1] == '\r') {
buf[--len] = 0;
+ }
// Ignore empty lines.
- if(!len)
+ if(!len) {
return;
+ }
// Commands start with '/'
- if(*buf == '/')
+ if(*buf == '/') {
return parse_command(mesh, buf + 1);
+ }
// Lines in the form "name: message..." set the destination node.
if(colon) {
*colon = 0;
msg = colon + 1;
- if(*msg == ' ')
+
+ if(*msg == ' ') {
msg++;
+ }
destination = meshlink_get_node(mesh, buf);
+
if(!destination) {
fprintf(stderr, "Error looking up '%s': %s\n", buf, meshlink_strerror(meshlink_errno));
return;
const char *nick = NULL;
char buf[1024];
- if(argc > 1)
+ if(argc > 1) {
confbase = argv[1];
+ }
- if(argc > 2)
+ if(argc > 2) {
nick = argv[2];
+ }
meshlink_set_log_cb(NULL, MESHLINK_INFO, log_message);
meshlink_handle_t *mesh = meshlink_open(confbase, nick, "chat", DEV_CLASS_STATIONARY);
+
if(!mesh) {
fprintf(stderr, "Could not open MeshLink: %s\n", meshlink_strerror(meshlink_errno));
return 1;
printf("Chat started.\nType /help for a list of commands.\n");
- while(fgets(buf, sizeof(buf), stdin))
+ while(fgets(buf, sizeof(buf), stdin)) {
parse_input(mesh, buf);
+ }
printf("Chat stopping.\n");
}
void node_status(meshlink::node *node, bool reachable) {
- if(reachable)
+ if(reachable) {
printf("%s joined.\n", node->name);
- else
+ } else {
printf("%s left.\n", node->name);
+ }
}
};
static void parse_command(meshlink::mesh *mesh, char *buf) {
char *arg = strchr(buf, ' ');
- if(arg)
+
+ if(arg) {
*arg++ = 0;
+ }
if(!strcasecmp(buf, "invite")) {
char *invitation;
}
invitation = mesh->invite(arg);
+
if(!invitation) {
fprintf(stderr, "Could not invite '%s': %s\n", arg, meshlink::strerror());
return;
mesh->stop();
- if(!mesh->join(arg))
+ if(!mesh->join(arg)) {
fprintf(stderr, "Could not join using invitation: %s\n", meshlink::strerror());
- else
+ } else {
fprintf(stderr, "Invitation accepted!\n");
+ }
if(!mesh->start()) {
fprintf(stderr, "Could not restart MeshLink: %s\n", meshlink::strerror());
}
meshlink::node *node = mesh->get_node(arg);
+
if(!node) {
fprintf(stderr, "Error looking up '%s': %s\n", arg, meshlink::strerror());
return;
} else if(!strcasecmp(buf, "who")) {
if(!arg) {
nodes = mesh->get_all_nodes(nodes, &nnodes);
- if(!nodes)
+
+ if(!nodes) {
fprintf(stderr, "Could not get list of nodes: %s\n", meshlink::strerror());
- else {
+ } else {
printf("%zu known nodes:", nnodes);
- for(size_t i = 0; i < nnodes; i++)
+
+ for(size_t i = 0; i < nnodes; i++) {
printf(" %s", nodes[i]->name);
+ }
+
printf("\n");
}
} else {
meshlink::node *node = mesh->get_node(arg);
- if(!node)
+
+ if(!node) {
fprintf(stderr, "Error looking up '%s': %s\n", arg, meshlink::strerror());
- else
+ } else {
printf("Node %s found\n", arg);
+ }
}
} else if(!strcasecmp(buf, "quit")) {
printf("Bye!\n");
"/who [<name>] List all nodes or show information about the given node.\n"
"/quit Exit this program.\n"
);
- } else
+ } else {
fprintf(stderr, "Unknown command '/%s'\n", buf);
+ }
}
static void parse_input(meshlink::mesh *mesh, char *buf) {
static meshlink::node *destination;
size_t len;
- if(!buf)
+ if(!buf) {
return;
+ }
// Remove newline.
len = strlen(buf);
- if(len && buf[len - 1] == '\n')
+ if(len && buf[len - 1] == '\n') {
buf[--len] = 0;
+ }
- if(len && buf[len - 1] == '\r')
+ if(len && buf[len - 1] == '\r') {
buf[--len] = 0;
+ }
// Ignore empty lines.
- if(!len)
+ if(!len) {
return;
+ }
// Commands start with '/'
- if(*buf == '/')
+ if(*buf == '/') {
return parse_command(mesh, buf + 1);
+ }
// Lines in the form "name: message..." set the destination node.
if(colon) {
*colon = 0;
msg = colon + 1;
- if(*msg == ' ')
+
+ if(*msg == ' ') {
msg++;
+ }
destination = mesh->get_node(buf);
+
if(!destination) {
fprintf(stderr, "Error looking up '%s': %s\n", buf, meshlink::strerror());
return;
const char *nick = NULL;
char buf[1024];
- if(argc > 1)
+ if(argc > 1) {
confbase = argv[1];
+ }
- if(argc > 2)
+ if(argc > 2) {
nick = argv[2];
+ }
ChatMesh mesh;
mesh.open(confbase, nick, "chatpp", DEV_CLASS_STATIONARY);
printf("Chat started.\nType /help for a list of commands.\n");
- while(fgets(buf, sizeof(buf), stdin))
+ while(fgets(buf, sizeof(buf), stdin)) {
parse_input(&mesh, buf);
+ }
printf("Chat stopping.\n");
for(int nindex = 0; nindex < n; nindex++) {
nodes = meshlink_get_all_nodes(mesh[nindex], nodes, &nnodes);
- if(!nodes)
+
+ if(!nodes) {
fprintf(stderr, "Could not get list of nodes: %s\n", meshlink_strerror(meshlink_errno));
- else {
+ } else {
printf("%zu known nodes:\n", nnodes);
+
for(int i = 0; i < nnodes; i++) {
//printf(" %s\n", nodes[i]->name);
- if(!meshlink_send(mesh[nindex], nodes[i], "magic", strlen("magic") + 1))
+ if(!meshlink_send(mesh[nindex], nodes[i], "magic", strlen("magic") + 1)) {
fprintf(stderr, "Could not send message to '%s': %s\n", nodes[i]->name, meshlink_strerror(meshlink_errno));
+ }
}
}
int psr = stat(path, &ps);
if(psr == 0 || errno != ENOENT) {
- if(psr == -1)
+ if(psr == -1) {
perror("stat");
- else
+ } else {
fprintf(stderr, "%s exists already\n", path);
+ }
return false;
}
static bool exportmeshgraph_started = false;
static bool exportmeshgraph_end(const char *none) {
- if(!exportmeshgraph_started)
+ if(!exportmeshgraph_started) {
return false;
+ }
struct itimerval zero_timer = { 0 };
+
setitimer(ITIMER_REAL, &zero_timer, NULL);
exportmeshgraph_started = false;
}
static bool exportmeshgraph_begin(const char *timeout_str) {
- if(!timeout_str)
+ if(!timeout_str) {
return false;
+ }
if(exportmeshgraph_started) {
- if(!exportmeshgraph_end(NULL))
+ if(!exportmeshgraph_end(NULL)) {
return false;
+ }
}
// get timeout
int timeout = atoi(timeout_str);
- if(timeout < 100)
+ if(timeout < 100) {
timeout = 100;
+ }
int timeout_sec = timeout / 1000;
int timeout_msec = timeout % 1000;
static void parse_command(char *buf) {
char *arg = strchr(buf, ' ');
- if(arg)
+
+ if(arg) {
*arg++ = 0;
+ }
if(!strcasecmp(buf, "invite")) {
char *invitation;
}
invitation = meshlink_invite(mesh[nodeindex], arg);
+
if(!invitation) {
fprintf(stderr, "Could not invite '%s': %s\n", arg, meshlink_strerror(meshlink_errno));
return;
fprintf(stderr, "/join requires an argument!\n");
return;
}
+
meshlink_stop(mesh[nodeindex]);
- if(!meshlink_join(mesh[nodeindex], arg))
+
+ if(!meshlink_join(mesh[nodeindex], arg)) {
fprintf(stderr, "Could not join using invitation: %s\n", meshlink_strerror(meshlink_errno));
- else
+ } else {
fprintf(stderr, "Invitation accepted!\n");
+ }
+
if(!meshlink_start(mesh[nodeindex])) {
fprintf(stderr, "Could not restart MeshLink: %s\n", meshlink_strerror(meshlink_errno));
exit(1);
}
meshlink_node_t *node = meshlink_get_node(mesh[nodeindex], arg);
+
if(!node) {
fprintf(stderr, "Unknown node '%s'\n", arg);
return;
} else if(!strcasecmp(buf, "who")) {
if(!arg) {
nodes = meshlink_get_all_nodes(mesh[nodeindex], nodes, &nnodes);
- if(!nodes)
+
+ if(!nodes) {
fprintf(stderr, "Could not get list of nodes: %s\n", meshlink_strerror(meshlink_errno));
- else {
+ } else {
printf("%zu known nodes:", nnodes);
- for(int i = 0; i < nnodes; i++)
+
+ for(int i = 0; i < nnodes; i++) {
printf(" %s", nodes[i]->name);
+ }
+
printf("\n");
}
} else {
meshlink_node_t *node = meshlink_get_node(mesh[nodeindex], arg);
- if(!node)
+
+ if(!node) {
fprintf(stderr, "Unknown node '%s'\n", arg);
- else
+ } else {
printf("Node %s found, pmtu %zd\n", arg, meshlink_get_pmtu(mesh[nodeindex], node));
+ }
}
- } else if(!strcasecmp(buf, "link"))
+ } else if(!strcasecmp(buf, "link")) {
linkmesh();
- else if(!strcasecmp(buf, "eg"))
+ } else if(!strcasecmp(buf, "eg")) {
exportmeshgraph(arg);
- else if(!strcasecmp(buf, "egb"))
+ } else if(!strcasecmp(buf, "egb")) {
exportmeshgraph_begin(arg);
- else if(!strcasecmp(buf, "ege"))
+ } else if(!strcasecmp(buf, "ege")) {
exportmeshgraph_end(NULL);
- else if(!strcasecmp(buf, "test"))
+ } else if(!strcasecmp(buf, "test")) {
testmesh();
- else if(!strcasecmp(buf, "select")) {
+ } else if(!strcasecmp(buf, "select")) {
if(!arg) {
fprintf(stderr, "/select requires an argument!\n");
return;
}
+
nodeindex = atoi(arg);
printf("Index is now %d\n", nodeindex);
- } else if(!strcasecmp(buf, "stop"))
+ } else if(!strcasecmp(buf, "stop")) {
meshlink_stop(mesh[nodeindex]);
- else if(!strcasecmp(buf, "quit")) {
+ } else if(!strcasecmp(buf, "quit")) {
printf("Bye!\n");
fclose(stdin);
} else if(!strcasecmp(buf, "help")) {
"/stop Call meshlink_stop, use /select first to select which node to stop\n"
"/quit Exit this program.\n"
);
- } else
+ } else {
fprintf(stderr, "Unknown command '/%s'\n", buf);
+ }
}
static void parse_input(char *buf) {
static meshlink_node_t *destination;
size_t len;
- if(!buf)
+ if(!buf) {
return;
+ }
// Remove newline.
len = strlen(buf);
- if(len && buf[len - 1] == '\n')
+ if(len && buf[len - 1] == '\n') {
buf[--len] = 0;
+ }
- if(len && buf[len - 1] == '\r')
+ if(len && buf[len - 1] == '\r') {
buf[--len] = 0;
+ }
// Ignore empty lines.
- if(!len)
+ if(!len) {
return;
+ }
// Commands start with '/'
- if(*buf == '/')
+ if(*buf == '/') {
return parse_command(buf + 1);
+ }
// Lines in the form "name: message..." set the destination node.
if(colon) {
*colon = 0;
msg = colon + 1;
- if(*msg == ' ')
+
+ if(*msg == ' ') {
msg++;
+ }
destination = meshlink_get_node(mesh[nodeindex], buf);
+
if(!destination) {
fprintf(stderr, "Unknown node '%s'\n", buf);
return;
const char *graphexporttimeout = NULL;
char buf[1024];
- if(argc > 1)
+ if(argc > 1) {
n = atoi(argv[1]);
+ }
if(n < 1) {
fprintf(stderr, "Usage: %s [number of local nodes] [confbase] [prefixnodenames] [graphexport timeout]\n", argv[0]);
return 1;
}
- if(argc > 2)
+ if(argc > 2) {
basebase = argv[2];
+ }
- if(argc > 3)
+ if(argc > 3) {
namesprefix = argv[3];
+ }
- if(argc > 4)
+ if(argc > 4) {
graphexporttimeout = argv[4];
+ }
mesh = calloc(n, sizeof(*mesh));
char filename[PATH_MAX];
char nodename[100];
+
for(int i = 0; i < n; i++) {
snprintf(nodename, sizeof(nodename), "%snode%d", namesprefix, i);
snprintf(filename, sizeof(filename), "%s/%s", basebase, nodename);
- if(n / (i + 1) > n / 4)
+
+ if(n / (i + 1) > n / 4) {
mesh[i] = meshlink_open(filename, nodename, "manynodes", DEV_CLASS_BACKBONE);
- else
+ } else {
mesh[i] = meshlink_open(filename, nodename, "manynodes", DEV_CLASS_PORTABLE);
+ }
+
meshlink_set_log_cb(mesh[i], MESHLINK_DEBUG, log_message);
+
if(!mesh[i]) {
fprintf(stderr, "errno is: %d\n", meshlink_errno);
fprintf(stderr, "Could not open %s: %s\n", filename, meshlink_strerror(meshlink_errno));
int started = 0;
for(int i = 0; i < n; i++) {
- if(!meshlink_start(mesh[i]))
+ if(!meshlink_start(mesh[i])) {
fprintf(stderr, "Could not start node %d: %s\n", i, meshlink_strerror(meshlink_errno));
- else
+ } else {
started++;
+ }
}
if(!started) {
return 1;
}
- if(graphexporttimeout)
+ if(graphexporttimeout) {
exportmeshgraph_begin(graphexporttimeout);
+ }
printf("%d nodes started.\nType /help for a list of commands.\n", started);
// handle input
- while(fgets(buf, sizeof(buf), stdin))
+ while(fgets(buf, sizeof(buf), stdin)) {
parse_input(buf);
+ }
exportmeshgraph_end(NULL);
printf("Nodes stopping.\n");
- for(int i = 0; i < n; i++)
+ for(int i = 0; i < n; i++) {
meshlink_close(mesh[i]);
+ }
return 0;
}
sleep(10);
meshlink_node_t *remotenode = meshlink_get_node(myhandle, remotename);
+
if(!remotenode) {
fprintf(stderr, "Node %s not known yet.\n", remotename);
continue;
// Open a new meshlink instance.
meshlink_handle_t *mesh = meshlink_open("basic_conf", "foo", "basic", DEV_CLASS_BACKBONE);
+
if(!mesh) {
fprintf(stderr, "Could not initialize configuration for foo\n");
return 1;
// Check that our own node exists.
meshlink_node_t *self = meshlink_get_self(mesh);
+
if(!self) {
fprintf(stderr, "Foo does not know about itself\n");
return 1;
}
+
if(strcmp(self->name, "foo")) {
fprintf(stderr, "Foo thinks its name is %s\n", self->name);
return 1;
fprintf(stderr, "Foo could not start\n");
return 1;
}
+
meshlink_stop(mesh);
// Make sure we can start and stop the mesh again.
fprintf(stderr, "Foo could not start twice\n");
return 1;
}
+
meshlink_stop(mesh);
// Close the mesh and open it again, now with a different name parameter.
// Check that the name is ignored now, and that we still are "foo".
mesh = meshlink_open("basic_conf", "bar", "basic", DEV_CLASS_BACKBONE);
+
if(!mesh) {
fprintf(stderr, "Could not open configuration for foo a second time\n");
return 1;
}
self = meshlink_get_self(mesh);
+
if(!self) {
fprintf(stderr, "Foo doesn't know about itself the second time\n");
return 1;
}
+
if(strcmp(self->name, "foo")) {
fprintf(stderr, "Foo thinks its name is %s the second time\n", self->name);
return 1;
fprintf(stderr, "Foo could not start a third time\n");
return 1;
}
+
meshlink_stop(mesh);
// That's it.
static struct timeval tv0;
struct timeval tv;
- if(tv0.tv_sec == 0)
+ if(tv0.tv_sec == 0) {
gettimeofday(&tv0, NULL);
+ }
+
gettimeofday(&tv, NULL);
fprintf(stderr, "%u.%.03u ", (unsigned int)(tv.tv_sec - tv0.tv_sec), (unsigned int)tv.tv_usec / 1000);
- if(mesh)
+ if(mesh) {
fprintf(stderr, "(%s) ", mesh->name);
+ }
+
fprintf(stderr, "[%d] %s\n", level, text);
}
void a_receive_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, const void *data, size_t len) {
- if(len == 5 && !memcmp(data, "Hello", 5))
+ if(len == 5 && !memcmp(data, "Hello", 5)) {
b_responded = true;
- else if(len == 0)
+ } else if(len == 0) {
b_closed = true;
+ }
}
void b_receive_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, const void *data, size_t len) {
// Send one message back, then close the channel.
- if(len)
+ if(len) {
meshlink_channel_send(mesh, channel, data, len);
+ }
meshlink_channel_close(mesh, channel);
}
bool accept_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, uint16_t port, const void *data, size_t len) {
meshlink_set_channel_accept_cb(mesh, NULL);
meshlink_set_channel_receive_cb(mesh, channel, b_receive_cb);
- if(data)
+
+ if(data) {
b_receive_cb(mesh, channel, data, len);
+ }
+
return true;
}
}
void poll_cb2(meshlink_handle_t *mesh, meshlink_channel_t *channel, size_t len) {
- if(len)
+ if(len) {
a_nonzero_poll_cb = true;
+ }
}
int main(int argc, char *argv[]) {
assert(b_closed);
// Try to create a second channel
-
+
meshlink_channel_t *channel2 = meshlink_channel_open(a, nb, 7, a_receive_cb, NULL, 0);
assert(channel2);
meshlink_set_channel_poll_cb(a, channel2, poll_cb2);
volatile bool bar_responded = false;
void log_cb(meshlink_handle_t *mesh, meshlink_log_level_t level, const char *text) {
- if(mesh)
+ if(mesh) {
fprintf(stderr, "(%s) ", mesh->name);
+ }
+
fprintf(stderr, "[%d] %s\n", level, text);
}
void status_cb(meshlink_handle_t *mesh, meshlink_node_t *node, bool reachable) {
- if(!strcmp(node->name, "bar"))
+ if(!strcmp(node->name, "bar")) {
bar_reachable = reachable;
+ }
}
void foo_receive_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, const void *data, size_t len) {
- if(len == 5 && !memcmp(data, "Hello", 5))
+ if(len == 5 && !memcmp(data, "Hello", 5)) {
bar_responded = true;
+ }
}
void bar_receive_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, const void *data, size_t len) {
}
bool accept_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, uint16_t port, const void *data, size_t len) {
- if(port != 7)
+ if(port != 7) {
return false;
+ }
+
meshlink_set_channel_receive_cb(mesh, channel, bar_receive_cb);
- if(data)
+
+ if(data) {
bar_receive_cb(mesh, channel, data, len);
+ }
+
return true;
}
void poll_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, size_t len) {
meshlink_set_channel_poll_cb(mesh, channel, NULL);
- if(meshlink_channel_send(mesh, channel, "Hello", 5) != 5)
+
+ if(meshlink_channel_send(mesh, channel, "Hello", 5) != 5) {
fprintf(stderr, "Could not send whole message\n");
+ }
}
int main1(int rfd, int wfd) {
meshlink_set_log_cb(NULL, MESHLINK_DEBUG, log_cb);
meshlink_handle_t *mesh1 = meshlink_open("channels_conf.1", "foo", "channels", DEV_CLASS_BACKBONE);
+
if(!mesh1) {
fprintf(stderr, "Could not initialize configuration for foo\n");
return 1;
meshlink_add_address(mesh1, "localhost");
char *data = meshlink_export(mesh1);
+
if(!data) {
fprintf(stderr, "Foo could not export its configuration\n");
return 1;
for(int i = 0; i < 20; i++) {
sleep(1);
- if(bar_reachable)
+
+ if(bar_reachable) {
break;
+ }
}
if(!bar_reachable) {
// Open a channel from foo to bar.
meshlink_node_t *bar = meshlink_get_node(mesh1, "bar");
+
if(!bar) {
fprintf(stderr, "Foo could not find bar\n");
return 1;
for(int i = 0; i < 5; i++) {
sleep(1);
- if(bar_responded)
+
+ if(bar_responded) {
break;
+ }
}
if(!bar_responded) {
meshlink_set_log_cb(NULL, MESHLINK_DEBUG, log_cb);
meshlink_handle_t *mesh2 = meshlink_open("channels_conf.2", "bar", "channels", DEV_CLASS_BACKBONE);
+
if(!mesh2) {
fprintf(stderr, "Could not initialize configuration for bar\n");
return 1;
meshlink_enable_discovery(mesh2, false);
char *data = meshlink_export(mesh2);
+
if(!data) {
fprintf(stderr, "Bar could not export its configuration\n");
return 1;
}
size_t len = strlen(data);
- if(write(wfd, &len, sizeof(len)) <= 0) abort();
- if(write(wfd, data, len) <= 0) abort();
+
+ if(write(wfd, &len, sizeof(len)) <= 0) {
+ abort();
+ }
+
+ if(write(wfd, data, len) <= 0) {
+ abort();
+ }
+
free(data);
read(rfd, &len, sizeof(len));
pipe2(fda, 0);
pipe2(fdb, 0);
- if(fork())
+ if(fork()) {
return main1(fda[0], fdb[1]);
- else
+ } else {
return main2(fdb[0], fda[1]);
+ }
}
static struct timeval tv0;
struct timeval tv;
- if(tv0.tv_sec == 0)
+ if(tv0.tv_sec == 0) {
gettimeofday(&tv0, NULL);
+ }
+
gettimeofday(&tv, NULL);
fprintf(stderr, "%u.%.03u ", (unsigned int)(tv.tv_sec - tv0.tv_sec), (unsigned int)tv.tv_usec / 1000);
- if(mesh)
+ if(mesh) {
fprintf(stderr, "(%s) ", mesh->name);
+ }
+
fprintf(stderr, "[%d] %s\n", level, text);
}
void status_cb(meshlink_handle_t *mesh, meshlink_node_t *node, bool reachable) {
printf("status_cb: %s %sreachable\n", node->name, reachable ? "" : "un");
- if(!strcmp(node->name, "bar"))
+
+ if(!strcmp(node->name, "bar")) {
bar_reachable = reachable;
+ }
}
void foo_receive_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, const void *data, size_t len) {
printf("foo_receive_cb %zu: ", len);
fwrite(data, 1, len, stdout);
printf("\n");
- if(len == 5 && !memcmp(data, "Hello", 5))
+
+ if(len == 5 && !memcmp(data, "Hello", 5)) {
bar_responded = true;
+ }
}
void bar_receive_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, const void *data, size_t len) {
bool accept_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, uint16_t port, const void *data, size_t len) {
printf("accept_cb: (from %s on port %u) ", channel->node->name, (unsigned int)port);
- if(data)
+
+ if(data) {
fwrite(data, 1, len, stdout);
+ }
+
printf("\n");
- if(port != 7)
+ if(port != 7) {
return false;
+ }
+
meshlink_set_channel_receive_cb(mesh, channel, bar_receive_cb);
- if(data)
+
+ if(data) {
bar_receive_cb(mesh, channel, data, len);
+ }
+
return true;
}
void poll_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, size_t len) {
meshlink_set_channel_poll_cb(mesh, channel, NULL);
- if(meshlink_channel_send(mesh, channel, "Hello", 5) != 5)
+
+ if(meshlink_channel_send(mesh, channel, "Hello", 5) != 5) {
fprintf(stderr, "Could not send whole message\n");
+ }
}
int main(int argc, char *argv[]) {
// Open two new meshlink instance.
meshlink_handle_t *mesh1 = meshlink_open("channels_conf.1", "foo", "channels", DEV_CLASS_BACKBONE);
+
if(!mesh1) {
fprintf(stderr, "Could not initialize configuration for foo\n");
return 1;
}
meshlink_handle_t *mesh2 = meshlink_open("channels_conf.2", "bar", "channels", DEV_CLASS_BACKBONE);
+
if(!mesh2) {
fprintf(stderr, "Could not initialize configuration for bar\n");
return 1;
meshlink_add_address(mesh1, "localhost");
char *data = meshlink_export(mesh1);
+
if(!data) {
fprintf(stderr, "Foo could not export its configuration\n");
return 1;
free(data);
data = meshlink_export(mesh2);
+
if(!data) {
fprintf(stderr, "Bar could not export its configuration\n");
return 1;
for(int i = 0; i < 20; i++) {
sleep(1);
- if(bar_reachable)
+
+ if(bar_reachable) {
break;
+ }
}
if(!bar_reachable) {
// Open a channel from foo to bar.
meshlink_node_t *bar = meshlink_get_node(mesh1, "bar");
+
if(!bar) {
fprintf(stderr, "Foo could not find bar\n");
return 1;
for(int i = 0; i < 20; i++) {
sleep(1);
- if(bar_responded)
+
+ if(bar_responded) {
break;
+ }
}
if(!bar_responded) {
int debug_level;
void log_cb(meshlink_handle_t *mesh, meshlink_log_level_t level, const char *text) {
- if(mesh)
+ if(mesh) {
fprintf(stderr, "(%s) ", mesh->name);
+ }
+
fprintf(stderr, "[%d] %s\n", level, text);
}
void status_cb(meshlink_handle_t *mesh, meshlink_node_t *node, bool reachable) {
- if(!strcmp(node->name, "bar"))
+ if(!strcmp(node->name, "bar")) {
bar_reachable = reachable;
- else if(!strcmp(node->name, "foo"))
- if(!reachable)
+ } else if(!strcmp(node->name, "foo"))
+ if(!reachable) {
foo_closed = true;
+ }
}
void foo_receive_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, const void *data, size_t len) {
foo_closed = true;
return;
}
+
// Write data to stdout.
write(1, data, len);
}
}
bool accept_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, uint16_t port, const void *data, size_t len) {
- if(port != 7)
+ if(port != 7) {
return false;
+ }
+
meshlink_set_channel_receive_cb(mesh, channel, bar_receive_cb);
- if(data)
+
+ if(data) {
bar_receive_cb(mesh, channel, data, len);
+ }
+
return true;
}
meshlink_set_log_cb(NULL, debug_level, log_cb);
meshlink_handle_t *mesh1 = meshlink_open("echo-fork_conf.1", "foo", "echo-fork", DEV_CLASS_BACKBONE);
+
if(!mesh1) {
fprintf(stderr, "Could not initialize configuration for foo\n");
return 1;
for(int i = 0; i < 20; i++) {
sleep(1);
- if(bar_reachable)
+
+ if(bar_reachable) {
break;
+ }
}
if(!bar_reachable) {
// Open a channel from foo to bar.
meshlink_node_t *bar = meshlink_get_node(mesh1, "bar");
+
if(!bar) {
fprintf(stderr, "Foo could not find bar\n");
return 1;
for(int i = 0; i < 5; i++) {
sleep(1);
- if(bar_responded)
+
+ if(bar_responded) {
break;
+ }
}
if(!bar_responded) {
do {
//fprintf(stderr, ":");
ssize_t len = read(0, buffer, BUF_SIZE);
- if(len <= 0)
+
+ if(len <= 0) {
break;
+ }
+
char *p = buffer;
+
while(len > 0) {
ssize_t sent = meshlink_channel_send(mesh1, channel, p, len);
+
if(sent < 0) {
fprintf(stderr, "Sending message failed\n");
return 1;
}
- if(!sent)
+
+ if(!sent) {
usleep(100000);
- else {
+ } else {
len -= sent;
p += sent;
}
meshlink_set_log_cb(NULL, debug_level, log_cb);
meshlink_handle_t *mesh2 = meshlink_open("echo-fork_conf.2", "bar", "echo-fork", DEV_CLASS_BACKBONE);
+
if(!mesh2) {
fprintf(stderr, "Could not initialize configuration for bar\n");
return 1;
return 1;
}
- while(!foo_closed)
+ while(!foo_closed) {
sleep(1);
+ }
// Clean up.
meshlink_close(foo);
meshlink_close(bar);
- if(fork())
+ if(fork()) {
return main1();
- else
+ } else {
return main2();
+ }
}
volatile bool bar_reachable = false;
void status_cb(meshlink_handle_t *mesh, meshlink_node_t *node, bool reachable) {
- if(!strcmp(node->name, "bar"))
+ if(!strcmp(node->name, "bar")) {
bar_reachable = reachable;
+ }
}
int main(int argc, char *argv[]) {
// Open two new meshlink instance.
meshlink_handle_t *mesh1 = meshlink_open("import_export_conf.1", "foo", "import-export", DEV_CLASS_BACKBONE);
+
if(!mesh1) {
fprintf(stderr, "Could not initialize configuration for foo\n");
return 1;
}
meshlink_handle_t *mesh2 = meshlink_open("import_export_conf.2", "bar", "import-export", DEV_CLASS_BACKBONE);
+
if(!mesh2) {
fprintf(stderr, "Could not initialize configuration for bar\n");
return 1;
meshlink_add_address(mesh2, "localhost");
char *data = meshlink_export(mesh1);
+
if(!data) {
fprintf(stderr, "Foo could not export its configuration\n");
return 1;
free(data);
data = meshlink_export(mesh2);
+
if(!data) {
fprintf(stderr, "Bar could not export its configuration\n");
return 1;
for(int i = 0; i < 20; i++) {
sleep(1);
- if(bar_reachable)
+
+ if(bar_reachable) {
break;
+ }
}
if(!bar_reachable) {
}
int pmtu = meshlink_get_pmtu(mesh2, meshlink_get_node(mesh2, "bar"));
+
for(int i = 0; i < 10 && !pmtu; i++) {
sleep(1);
pmtu = meshlink_get_pmtu(mesh2, meshlink_get_node(mesh2, "bar"));
volatile bool baz_reachable = false;
void status_cb(meshlink_handle_t *mesh, meshlink_node_t *node, bool reachable) {
- if(!strcmp(node->name, "baz"))
+ if(!strcmp(node->name, "baz")) {
baz_reachable = reachable;
+ }
}
int main(int argc, char *argv[]) {
// Open two new meshlink instance.
meshlink_handle_t *mesh1 = meshlink_open("invite_join_conf.1", "foo", "invite-join", DEV_CLASS_BACKBONE);
+
if(!mesh1) {
fprintf(stderr, "Could not initialize configuration for foo\n");
return 1;
}
meshlink_handle_t *mesh2 = meshlink_open("invite_join_conf.2", "bar", "invite-join", DEV_CLASS_BACKBONE);
+
if(!mesh2) {
fprintf(stderr, "Could not initialize configuration for bar\n");
return 1;
meshlink_add_address(mesh1, "localhost");
char *url = meshlink_invite(mesh1, "baz");
+
if(!url) {
fprintf(stderr, "Foo could not generate an invitation for baz\n");
return 1;
for(int i = 0; i < 60; i++) {
sleep(1);
- if(baz_reachable)
+
+ if(baz_reachable) {
break;
+ }
}
if(!baz_reachable) {
}
int pmtu = meshlink_get_pmtu(mesh1, meshlink_get_node(mesh1, "baz"));
+
for(int i = 0; i < 10 && !pmtu; i++) {
sleep(1);
pmtu = meshlink_get_pmtu(mesh1, meshlink_get_node(mesh1, "baz"));
// Open two new meshlink instance.
meshlink_handle_t *mesh1 = meshlink_open("sign_verify_conf.1", "foo", "sign-verify", DEV_CLASS_BACKBONE);
+
if(!mesh1) {
fprintf(stderr, "Could not initialize configuration for foo\n");
return 1;
}
meshlink_handle_t *mesh2 = meshlink_open("sign_verify_conf.2", "bar", "sign-verify", DEV_CLASS_BACKBONE);
+
if(!mesh2) {
fprintf(stderr, "Could not initialize configuration for bar\n");
return 1;
fprintf(stderr, "Signing failed\n");
return 1;
}
+
if(siglen != MESHLINK_SIGLEN) {
fprintf(stderr, "Signature has unexpected length %zu != %zu\n", siglen, MESHLINK_SIGLEN);
return 1;
}
meshlink_node_t *foo = meshlink_get_node(mesh2, "foo");
+
if(!foo) {
fprintf(stderr, "Bar did not know about node foo\n");
return 1;
}
meshlink_node_t *bar = meshlink_get_node(mesh2, "bar");
+
if(!bar) {
fprintf(stderr, "Bar did not know about node bar\n");
return 1;
fprintf(stderr, "False positive verification with half sized signature\n");
return 1;
}
+
if(meshlink_verify(mesh2, foo, testdata1, sizeof(testdata1), sig, siglen * 2)) {
fprintf(stderr, "False positive verification with double sized signature\n");
return 1;
}
+
if(meshlink_verify(mesh2, foo, testdata2, sizeof(testdata2), sig, siglen)) {
fprintf(stderr, "False positive verification with wrong data\n");
return 1;
}
+
if(meshlink_verify(mesh2, bar, testdata1, sizeof(testdata1), sig, siglen)) {
fprintf(stderr, "False positive verification with wrong signer\n");
return 1;
static struct timeval tv0;
struct timeval tv;
- if(tv0.tv_sec == 0)
+ if(tv0.tv_sec == 0) {
gettimeofday(&tv0, NULL);
+ }
+
gettimeofday(&tv, NULL);
fprintf(stderr, "%u.%.03u ", (unsigned int)(tv.tv_sec - tv0.tv_sec), (unsigned int)tv.tv_usec / 1000);
- if(mesh)
+ if(mesh) {
fprintf(stderr, "(%s) ", mesh->name);
+ }
+
fprintf(stderr, "[%d] %s\n", level, text);
}
static void receive_cb(meshlink_handle_t *mesh, meshlink_node_t *source, const void *data, size_t len) {
fprintf(stderr, "RECEIVED SOMETHING\n");
- if(len == 5 && !memcmp(data, "Hello", 5))
+
+ if(len == 5 && !memcmp(data, "Hello", 5)) {
received = true;
+ }
}
int main(int argc, char *argv[]) {
// start the nodes
- for(int i = 0; i < 3; i++)
+ for(int i = 0; i < 3; i++) {
meshlink_start(mesh[i]);
+ }
// the nodes should now learn about each other
// Stop the other nodes
- for(int i = 1; i < 3; i++)
+ for(int i = 1; i < 3; i++) {
meshlink_stop(mesh[i]);
+ }
sleep(1);
meshlink_set_log_cb(mesh[1], MESHLINK_DEBUG, log_cb);
- for(int i = 1; i < 3; i++)
+ for(int i = 1; i < 3; i++) {
meshlink_start(mesh[i]);
+ }
assert(meshlink_get_node(mesh[1], name[2]));
assert(meshlink_get_node(mesh[2], name[1]));
// Clean up.
- for(int i = 0; i < 3; i++)
+ for(int i = 0; i < 3; i++) {
meshlink_close(mesh[i]);
+ }
}
timeout.tv_sec += seconds;
while(!s->flag)
- if(!pthread_cond_timedwait(&s->cond, &s->mutex, &timeout) || errno != EINTR)
+ if(!pthread_cond_timedwait(&s->cond, &s->mutex, &timeout) || errno != EINTR) {
break;
+ }
return s->flag;
}
/// Stop and cleanup a pair of meshlink instances.
extern void close_meshlink_pair(meshlink_handle_t *a, meshlink_handle_t *b, const char *prefix);
-#define assert_after(cond, timeout) do {\
- for(int i = 0; i++ <= timeout;) {\
- if(cond)\
- break;\
- if(i == timeout)\
- assert(cond);\
- sleep(1);\
- }\
-} while(0);
-
+#define assert_after(cond, timeout)\
+ do {\
+ for(int i = 0; i++ <= timeout;) {\
+ if(cond)\
+ break;\
+ if(i == timeout)\
+ assert(cond);\
+ sleep(1);\
+ }\
+ } while(0)
#endif