static int n = 10;
static meshlink_handle_t **mesh;
-static char* namesprefix="machine1";
+static char *namesprefix = "machine1";
static int nodeindex = 0;
static meshlink_node_t **nodes;
[MESHLINK_ERROR] = "\x1b[31mERROR",
[MESHLINK_CRITICAL] = "\x1b[31mCRITICAL",
};
- fprintf(stderr, "%s\t%s:\x1b[0m %s\n", mesh ? mesh->name : "global",levelstr[level], text);
+ fprintf(stderr, "%s\t%s:\x1b[0m %s\n", mesh ? mesh->name : "global", levelstr[level], text);
}
//Test mesh sending data
}
}
-static bool exportmeshgraph(const char* path) {
+static bool exportmeshgraph(const char *path) {
struct stat ps;
int psr = stat(path, &ps);
return false;
}
- FILE* stream = fopen(path, "w");
+ FILE *stream = fopen(path, "w");
if(!stream) {
perror("stream");
gettimeofday(&ts, NULL);
char name[1024];
- snprintf(name, sizeof(name), "%sgraph_%ld_%03ld.json", namesprefix,ts.tv_sec, ts.tv_usec/1000);
+ snprintf(name, sizeof(name), "%sgraph_%ld_%03ld.json", namesprefix, ts.tv_sec, ts.tv_usec / 1000);
exportmeshgraph(name);
}
#ifndef _WIN32
static bool exportmeshgraph_started = false;
-static bool exportmeshgraph_end(const char* none) {
+static bool exportmeshgraph_end(const char *none) {
if(!exportmeshgraph_started)
return false;
return true;
}
-static bool exportmeshgraph_begin(const char* timeout_str) {
+static bool exportmeshgraph_begin(const char *timeout_str) {
if(!timeout_str)
return false;
return true;
}
#else
-static bool exportmeshgraph_end(const char* none) {
+static bool exportmeshgraph_end(const char *none) {
return false;
}
-static bool exportmeshgraph_begin(const char* timeout_str) {
+static bool exportmeshgraph_begin(const char *timeout_str) {
return false;
}
#endif
return;
}
nodeindex = atoi(arg);
- printf("Index is now %d\n",nodeindex);
+ printf("Index is now %d\n", nodeindex);
} else if(!strcasecmp(buf, "stop"))
meshlink_stop(mesh[nodeindex]);
else if(!strcasecmp(buf, "quit")) {
char filename[PATH_MAX];
char nodename[100];
for(int i = 0; i < n; i++) {
- snprintf(nodename, sizeof(nodename), "%snode%d", namesprefix,i);
+ snprintf(nodename, sizeof(nodename), "%snode%d", namesprefix, i);
snprintf(filename, sizeof(filename), "%s/%s", basebase, nodename);
bool itsnew = access(filename, R_OK);
- if(n/(i+1) > n/4)
+ if(n / (i + 1) > n / 4)
mesh[i] = meshlink_open(filename, nodename, "manynodes", DEV_CLASS_BACKBONE);
else
mesh[i] = meshlink_open(filename, nodename, "manynodes", DEV_CLASS_PORTABLE);
#include "../src/meshlink.h"
void handle_recv_data(meshlink_handle_t *mesh, meshlink_node_t *source, void *data, size_t len) {
- printf("Received %zu bytes from %s: %s\n", len, source->name, (char*)data);
+ printf("Received %zu bytes from %s: %s\n", len, source->name, (char *)data);
}
int main(int argc, char **argv) {
char *remotename = argc > 3 ? argv[3] : "bar";
- meshlink_handle_t* myhandle;
+ meshlink_handle_t *myhandle;
myhandle = meshlink_open(confbase, name, "meshlinkapp", DEV_CLASS_STATIONARY);
//sample data to send out
char mydata[200];
- memset(mydata,0,200);
- strcpy(mydata,"Hello World!");
+ memset(mydata, 0, 200);
+ strcpy(mydata, "Hello World!");
//send out data
- meshlink_send(myhandle,remotenode,mydata,sizeof(mydata));
+ meshlink_send(myhandle, remotenode, mydata, sizeof(mydata));
}
meshlink_stop(myhandle);
return true;
}
-bool set_config_string(config_t *cfg, const char* val) {
+bool set_config_string(config_t *cfg, const char *val) {
if(!cfg)
return false;
/*
Read exactly one line and strip the trailing newline if any.
*/
-static char *readline(FILE * fp, char *buf, size_t buflen) {
+static char *readline(FILE *fp, char *buf, size_t buflen) {
char *newline = NULL;
char *p;
char filename[PATH_MAX];
bool x;
- snprintf(filename, PATH_MAX,"%s" SLASH "meshlink.conf", mesh->confbase);
+ snprintf(filename, PATH_MAX, "%s" SLASH "meshlink.conf", mesh->confbase);
errno = 0;
x = read_config_file(mesh->config, filename);
char filename[PATH_MAX];
bool x;
- snprintf(filename,PATH_MAX, "%s" SLASH "hosts" SLASH "%s", mesh->confbase, name);
+ snprintf(filename, PATH_MAX, "%s" SLASH "hosts" SLASH "%s", mesh->confbase, name);
x = read_config_file(config_tree, filename);
return x;
bool write_host_config(struct meshlink_handle *mesh, const struct splay_tree_t *config_tree, const char *name) {
char filename[PATH_MAX];
- snprintf(filename,PATH_MAX, "%s" SLASH "hosts" SLASH "%s", mesh->confbase, name);
+ snprintf(filename, PATH_MAX, "%s" SLASH "hosts" SLASH "%s", mesh->confbase, name);
return write_config_file(config_tree, filename);
}
#define OPTION_VERSION(x) ((x) >> 24) /* Top 8 bits are for protocol minor version */
typedef struct connection_status_t {
- unsigned int pinged:1; /* sent ping */
- unsigned int active:1; /* 1 if active.. */
- unsigned int connecting:1; /* 1 if we are waiting for a non-blocking connect() to finish */
- unsigned int unused_termreq:1; /* the termination of this connection was requested */
- unsigned int remove_unused:1; /* Set to 1 if you want this connection removed */
- unsigned int timeout_unused:1; /* 1 if gotten timeout */
- unsigned int unused_encryptout:1; /* 1 if we can encrypt outgoing traffic */
- unsigned int unused_decryptin:1; /* 1 if we have to decrypt incoming traffic */
- unsigned int mst:1; /* 1 if this connection is part of a minimum spanning tree */
- unsigned int control:1; /* 1 if this is a control connection */
- unsigned int pcap:1; /* 1 if this is a control connection requesting packet capture */
- unsigned int log:1; /* 1 if this is a control connection requesting log dump */
- unsigned int invitation:1; /* 1 if this is an invitation */
- unsigned int invitation_used:1; /* 1 if the invitation has been consumed */
- unsigned int unused:19;
+ unsigned int pinged: 1; /* sent ping */
+ unsigned int active: 1; /* 1 if active.. */
+ unsigned int connecting: 1; /* 1 if we are waiting for a non-blocking connect() to finish */
+ unsigned int unused_termreq: 1; /* the termination of this connection was requested */
+ unsigned int remove_unused: 1; /* Set to 1 if you want this connection removed */
+ unsigned int timeout_unused: 1; /* 1 if gotten timeout */
+ unsigned int unused_encryptout: 1; /* 1 if we can encrypt outgoing traffic */
+ unsigned int unused_decryptin: 1; /* 1 if we have to decrypt incoming traffic */
+ unsigned int mst: 1; /* 1 if this connection is part of a minimum spanning tree */
+ unsigned int control: 1; /* 1 if this is a control connection */
+ unsigned int pcap: 1; /* 1 if this is a control connection requesting packet capture */
+ unsigned int log: 1; /* 1 if this is a control connection requesting log dump */
+ unsigned int invitation: 1; /* 1 if this is an invitation */
+ unsigned int invitation_used: 1; /* 1 if the invitation has been consumed */
+ unsigned int unused: 19;
} connection_status_t;
#include "ecdsa.h"
return 0;
}
-static bool fstrwrite(const char* str, FILE* stream) {
+static bool fstrwrite(const char *str, FILE *stream) {
size_t len = strlen(str);
- if(fwrite((void*)str, 1, len, stream) != len)
+ if(fwrite((void *)str, 1, len, stream) != len)
return false;
return true;
}
-static const char* __itoa(int value) {
+static const char *__itoa(int value) {
static char buffer[sizeof(int) * 8 + 1]; // not thread safe
if(snprintf(buffer, sizeof(buffer), "%d", value) == -1)
return buffer;
}
-bool devtool_export_json_all_edges_state(meshlink_handle_t *mesh, FILE* stream) {
+bool devtool_export_json_all_edges_state(meshlink_handle_t *mesh, FILE *stream) {
bool result = true;
pthread_mutex_lock(&(mesh->mesh_mutex));
goto fail;
for(size_t i = 0; i < node_count; ++i) {
- if(!fstrwrite("\t\t\"", stream) || !fstrwrite(((node_t*)nodes[i])->name, stream) || !fstrwrite("\": {\n", stream))
+ if(!fstrwrite("\t\t\"", stream) || !fstrwrite(((node_t *)nodes[i])->name, stream) || !fstrwrite("\": {\n", stream))
goto fail;
- if(!fstrwrite("\t\t\t\"name\": \"", stream) || !fstrwrite(((node_t*)nodes[i])->name, stream) || !fstrwrite("\",\n", stream))
+ if(!fstrwrite("\t\t\t\"name\": \"", stream) || !fstrwrite(((node_t *)nodes[i])->name, stream) || !fstrwrite("\",\n", stream))
goto fail;
- if(!fstrwrite("\t\t\t\"options\": ", stream) || !fstrwrite(__itoa(((node_t*)nodes[i])->options), stream) || !fstrwrite(",\n", stream))
+ if(!fstrwrite("\t\t\t\"options\": ", stream) || !fstrwrite(__itoa(((node_t *)nodes[i])->options), stream) || !fstrwrite(",\n", stream))
goto fail;
- if(!fstrwrite("\t\t\t\"devclass\": ", stream) || !fstrwrite(__itoa(((node_t*)nodes[i])->devclass), stream) || !fstrwrite("\n", stream))
+ if(!fstrwrite("\t\t\t\"devclass\": ", stream) || !fstrwrite(__itoa(((node_t *)nodes[i])->devclass), stream) || !fstrwrite("\n", stream))
goto fail;
- if(!fstrwrite((i+1) != node_count ? "\t\t},\n" : "\t\t}\n", stream))
+ if(!fstrwrite((i + 1) != node_count ? "\t\t},\n" : "\t\t}\n", stream))
goto fail;
}
goto fail;
char *host = NULL, *port = NULL, *address = NULL;
- sockaddr2str((const sockaddr_t *)&(edges[i]->address), &host, &port);
+ sockaddr2str((const sockaddr_t *) & (edges[i]->address), &host, &port);
if(host && port)
xasprintf(&address, "{ \"host\": \"%s\", \"port\": %s }", host, port);
if(!fstrwrite("\t\t\t\"weight\": ", stream) || !fstrwrite(__itoa(edges[i]->weight), stream) || !fstrwrite("\n", stream))
goto fail;
- if(!fstrwrite((i+1) != edge_count ? "\t\t},\n" : "\t\t}\n", stream))
+ if(!fstrwrite((i + 1) != edge_count ? "\t\t},\n" : "\t\t}\n", stream))
goto fail;
}
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
-extern bool devtool_export_json_all_edges_state(meshlink_handle_t *mesh, FILE* stream);
+extern bool devtool_export_json_all_edges_state(meshlink_handle_t *mesh, FILE *stream);
#endif
#define MESHLINK_MDNS_NAME_KEY "name"
#define MESHLINK_MDNS_FINGERPRINT_KEY "fingerprint"
-static void generate_rand_string(char* buffer, size_t size) {
+static void generate_rand_string(char *buffer, size_t size) {
for(size_t i = 0; i < (size - 1); ++i)
buffer[i] = 'a' + (rand() % ('z' - 'a' + 1));
- buffer[size-1] = '\0';
+ buffer[size - 1] = '\0';
}
static void discovery_entry_group_callback(CattaServer *server, CattaSEntryGroup *group, CattaEntryGroupState state, void *userdata) {
pthread_mutex_unlock(&(mesh->mesh_mutex));
}
-static void discovery_server_callback(CattaServer *server, CattaServerState state, void * userdata) {
+static void discovery_server_callback(CattaServer *server, CattaServerState state, void *userdata) {
meshlink_handle_t *mesh = userdata;
// asserts
CattaStringList *node_fp_li = catta_string_list_find(txt, MESHLINK_MDNS_FINGERPRINT_KEY);
if(node_name_li != NULL && node_fp_li != NULL) {
- char *node_name = (char*)catta_string_list_get_text(node_name_li) + strlen(MESHLINK_MDNS_NAME_KEY);
- char *node_fp = (char*)catta_string_list_get_text(node_fp_li) + strlen(MESHLINK_MDNS_FINGERPRINT_KEY);
+ char *node_name = (char *)catta_string_list_get_text(node_name_li) + strlen(MESHLINK_MDNS_NAME_KEY);
+ char *node_fp = (char *)catta_string_list_get_text(node_fp_li) + strlen(MESHLINK_MDNS_FINGERPRINT_KEY);
if(node_name[0] == '=' && node_fp[0] == '=') {
node_name += 1;
}
if(naddress.unknown.family != AF_UNKNOWN)
- meshlink_hint_address(mesh, (meshlink_node_t *)node, (struct sockaddr*)&naddress);
+ meshlink_hint_address(mesh, (meshlink_node_t *)node, (struct sockaddr *)&naddress);
else
logger(mesh, MESHLINK_WARNING, "Could not resolve node %s to a known address family type.\n", node->name);
} else
pthread_mutex_unlock(&(mesh->mesh_mutex));
}
-static void discovery_browse_callback(CattaSServiceBrowser *browser, CattaIfIndex interface_, CattaProtocol protocol, CattaBrowserEvent event, const char *name, const char *type, const char *domain, CattaLookupResultFlags flags, void* userdata) {
+static void discovery_browse_callback(CattaSServiceBrowser *browser, CattaIfIndex interface_, CattaProtocol protocol, CattaBrowserEvent event, const char *name, const char *type, const char *domain, CattaLookupResultFlags flags, void *userdata) {
meshlink_handle_t *mesh = userdata;
// asserts
}
bool isOpen() const {
- return (handle!=0);
+ return (handle != 0);
}
// TODO: please enable C++11 in autoconf to enable "move constructors":
*
* @return This function will return a pointer to a meshlink::mesh if MeshLink has succesfully set up its configuration files, NULL otherwise.
*/
- bool open(const char *confbase, const char *name, const char* appname, dev_class_t devclass) {
+ bool open(const char *confbase, const char *name, const char *appname, dev_class_t devclass) {
handle = meshlink_open(confbase, name, appname, devclass);
if(handle)
handle->priv = this;
return isOpen();
}
- mesh(const char *confbase, const char *name, const char* appname, dev_class_t devclass) {
+ mesh(const char *confbase, const char *name, const char *appname, dev_class_t devclass) {
open(confbase, name, appname, devclass);
}
handle->priv = 0;
meshlink_close(handle);
}
- handle=0;
+ handle = 0;
}
/** instead of registerin callbacks you derive your own class and overwrite the following abstract member functions.
*/
/// This function is called whenever another node sends data to the local node.
- virtual void receive(node* source, const void* data, size_t length) { /* do nothing */ }
+ virtual void receive(node *source, const void *data, size_t length) { /* do nothing */ }
/// This functions is called whenever another node's status changed.
- virtual void node_status(node* peer, bool reachable) { /* do nothing */ }
+ virtual void node_status(node *peer, bool reachable) { /* do nothing */ }
/// This functions is called whenever MeshLink has some information to log.
- virtual void log(log_level_t level, const char* message) { /* do nothing */ }
+ virtual void log(log_level_t level, const char *message) { /* do nothing */ }
/// This functions is called whenever another node attemps to open a channel to the local node.
/**
private:
// non-copyable:
- mesh(const mesh&) /* TODO: C++11: = delete */;
- void operator=(const mesh&) /* TODO: C++11: = delete */ ;
+ mesh(const mesh &) /* TODO: C++11: = delete */;
+ void operator=(const mesh &) /* TODO: C++11: = delete */ ;
/// static callback trampolines:
- static void receive_trampoline(meshlink_handle_t* handle, meshlink_node_t* source, const void* data, size_t length) {
+ static void receive_trampoline(meshlink_handle_t *handle, meshlink_node_t *source, const void *data, size_t length) {
if(!(handle->priv))
return;
- meshlink::mesh* that = static_cast<mesh*>(handle->priv);
- that->receive(static_cast<node*>(source), data, length);
+ meshlink::mesh *that = static_cast<mesh *>(handle->priv);
+ that->receive(static_cast<node *>(source), data, length);
}
- static void node_status_trampoline(meshlink_handle_t* handle, meshlink_node_t* peer, bool reachable) {
+ static void node_status_trampoline(meshlink_handle_t *handle, meshlink_node_t *peer, bool reachable) {
if(!(handle->priv))
return;
- meshlink::mesh* that = static_cast<mesh*>(handle->priv);
- that->node_status(static_cast<node*>(peer), reachable);
+ meshlink::mesh *that = static_cast<mesh *>(handle->priv);
+ that->node_status(static_cast<node *>(peer), reachable);
}
- static void log_trampoline(meshlink_handle_t* handle, log_level_t level, const char* message) {
+ static void log_trampoline(meshlink_handle_t *handle, log_level_t level, const char *message) {
if(!(handle->priv))
return;
- meshlink::mesh* that = static_cast<mesh*>(handle->priv);
+ meshlink::mesh *that = static_cast<mesh *>(handle->priv);
that->log(level, message);
}
static bool channel_accept_trampoline(meshlink_handle_t *handle, meshlink_channel *channel, uint16_t port, const void *data, size_t len) {
if(!(handle->priv))
return false;
- meshlink::mesh* that = static_cast<mesh*>(handle->priv);
- bool accepted = that->channel_accept(static_cast<meshlink::channel*>(channel), port, data, len);
+ meshlink::mesh *that = static_cast<mesh *>(handle->priv);
+ bool accepted = that->channel_accept(static_cast<meshlink::channel *>(channel), port, data, len);
if(accepted) {
meshlink_set_channel_receive_cb(handle, channel, &channel_receive_trampoline);
meshlink_set_channel_poll_cb(handle, channel, &channel_poll_trampoline);
return accepted;
}
- static void channel_receive_trampoline(meshlink_handle_t *handle, meshlink_channel *channel, const void* data, size_t len) {
+ static void channel_receive_trampoline(meshlink_handle_t *handle, meshlink_channel *channel, const void *data, size_t len) {
if(!(handle->priv))
return;
- meshlink::mesh* that = static_cast<mesh*>(handle->priv);
- that->channel_receive(static_cast<meshlink::channel*>(channel), data, len);
+ meshlink::mesh *that = static_cast<mesh *>(handle->priv);
+ that->channel_receive(static_cast<meshlink::channel *>(channel), data, len);
}
static void channel_poll_trampoline(meshlink_handle_t *handle, meshlink_channel *channel, size_t len) {
if(!(handle->priv))
return;
- meshlink::mesh* that = static_cast<mesh*>(handle->priv);
- that->channel_poll(static_cast<meshlink::channel*>(channel), len);
+ meshlink::mesh *that = static_cast<mesh *>(handle->priv);
+ that->channel_poll(static_cast<meshlink::channel *>(channel), len);
}
- meshlink_handle_t* handle;
+ meshlink_handle_t *handle;
};
static const char *strerror(errno_t err = meshlink_errno) {
return hostname;
}
-static char *get_my_hostname(meshlink_handle_t* mesh) {
+static char *get_my_hostname(meshlink_handle_t *mesh) {
char *hostname = NULL;
char *port = NULL;
char *hostport = NULL;
if(!data || !*data)
return NULL;
- if(!**data) {
+ if(! **data) {
*data = NULL;
return NULL;
}
}
static bool invitation_send(void *handle, uint8_t type, const void *data, size_t len) {
- meshlink_handle_t* mesh = handle;
+ meshlink_handle_t *mesh = handle;
while(len) {
int result = send(mesh->sock, data, len, 0);
if(result == -1 && errno == EINTR)
}
static bool invitation_receive(void *handle, uint8_t type, const void *msg, uint16_t len) {
- meshlink_handle_t* mesh = handle;
+ meshlink_handle_t *mesh = handle;
switch(type) {
case SPTPS_HANDSHAKE:
return sptps_send_record(&(mesh->sptps), 0, mesh->cookie, sizeof(mesh)->cookie);
return true;
}
-static bool recvline(meshlink_handle_t* mesh, size_t len) {
+static bool recvline(meshlink_handle_t *mesh, size_t len) {
char *newline = NULL;
if(!mesh->sock)
return true;
}
-meshlink_handle_t *meshlink_open(const char *confbase, const char *name, const char* appname, dev_class_t devclass) {
+meshlink_handle_t *meshlink_open(const char *confbase, const char *name, const char *appname, dev_class_t devclass) {
// Validate arguments provided by the application
bool usingname = false;
return false;
}
- mesh->threadstarted=true;
+ mesh->threadstarted = true;
if(mesh->discovery)
discovery_start(mesh);
// leave the last byte as 0 to make sure strings are always
// null-terminated if they are longer than the buffer
strncpy(hdr->destination, destination->name, (sizeof(hdr)->destination) - 1);
- strncpy(hdr->source, mesh->self->name, (sizeof(hdr)->source) -1);
+ strncpy(hdr->source, mesh->self->name, (sizeof(hdr)->source) - 1);
memcpy(packet->data + sizeof(*hdr), data, len);
}
// Notify event loop
- signal_trigger(&(mesh->loop),&(mesh->datafromapp));
+ signal_trigger(&(mesh->loop), &(mesh->datafromapp));
return true;
}
pthread_mutex_lock(&(mesh->mesh_mutex));
node_t *n;
- n = (node_t*)node;
- n->status.blacklisted=true;
- logger(mesh, MESHLINK_DEBUG, "Blacklisted %s.\n",node->name);
+ n = (node_t *)node;
+ n->status.blacklisted = true;
+ logger(mesh, MESHLINK_DEBUG, "Blacklisted %s.\n", node->name);
//Make blacklisting persistent in the config file
append_config_file(mesh, n->name, "blacklisted", "yes");
if(host && port) {
xasprintf(&str, "%s %s", host, port);
- if((strncmp("fe80",host,4) != 0) && (strncmp("127.",host,4) != 0) && (strcmp("localhost",host) !=0))
+ if((strncmp("fe80", host, 4) != 0) && (strncmp("127.", host, 4) != 0) && (strcmp("localhost", host) != 0))
append_config_file(mesh, node->name, "Address", str);
else
logger(mesh, MESHLINK_DEBUG, "Not adding Link Local IPv6 Address to config\n");
// if result is smaller than edges, we have to dealloc all the excess meshlink_edge_t
if(result_size > *nmemb)
- result = realloc(edges, result_size * sizeof(meshlink_edge_t*));
+ result = realloc(edges, result_size * sizeof(meshlink_edge_t *));
else
result = edges;
copy = xzalloc(sizeof(*copy));
else
copy = *p;
- copy->from = (meshlink_node_t*)e->from;
- copy->to = (meshlink_node_t*)e->to;
+ copy->from = (meshlink_node_t *)e->from;
+ copy->to = (meshlink_node_t *)e->to;
copy->address = e->address.storage;
copy->options = e->options;
copy->weight = e->weight;
// shrink result to the actual amount of memory used
for(int i = *nmemb; i > result_size; i--)
free(result[i - 1]);
- result = realloc(result, result_size * sizeof(meshlink_edge_t*));
+ result = realloc(result, result_size * sizeof(meshlink_edge_t *));
*nmemb = result_size;
} else {
*nmemb = 0;
}
/// Device class traits
-dev_class_traits_t dev_class_traits[_DEV_CLASS_MAX +1] = {
+dev_class_traits_t dev_class_traits[_DEV_CLASS_MAX + 1] = {
{ .min_connects = 3, .max_connects = 10000, .edge_weight = 1 }, // DEV_CLASS_BACKBONE
{ .min_connects = 3, .max_connects = 100, .edge_weight = 3 }, // DEV_CLASS_STATIONARY
{ .min_connects = 3, .max_connects = 3, .edge_weight = 6 }, // DEV_CLASS_PORTABLE
* @return A pointer to a meshlink_handle_t which represents this instance of MeshLink, or NULL in case of an error.
* The pointer is valid until meshlink_close() is called.
*/
-extern meshlink_handle_t *meshlink_open(const char *confbase, const char *name, const char* appname, dev_class_t devclass);
+extern meshlink_handle_t *meshlink_open(const char *confbase, const char *name, const char *appname, dev_class_t devclass);
/// Start MeshLink.
/** This function causes MeshLink to open network sockets, make outgoing connections, and
struct CattaSServiceBrowser *catta_browser;
struct CattaSimplePoll *catta_poll;
struct CattaSEntryGroup *catta_group;
- char* catta_servicetype;
+ char *catta_servicetype;
};
/// A handle for a MeshLink node.
uint8_t source[16];
} __attribute__((__packed__)) meshlink_packethdr_t;
-extern void meshlink_send_from_queue(event_loop_t* el,meshlink_handle_t *mesh);
+extern void meshlink_send_from_queue(event_loop_t *el, meshlink_handle_t *mesh);
extern void update_node_status(meshlink_handle_t *mesh, struct node_t *n);
extern meshlink_log_level_t global_log_level;
extern meshlink_log_cb_t global_log_cb;
// connect disconnect nodes
- node_t* connect_to = NULL;
- node_t* disconnect_from = NULL;
+ node_t *connect_to = NULL;
+ node_t *disconnect_from = NULL;
// get cur_connects
logger(mesh, MESHLINK_DEBUG, "* found best one for initial connect");
//timeout = 0;
- connect_to = (node_t*)nodes->head->data;
+ connect_to = (node_t *)nodes->head->data;
} else
logger(mesh, MESHLINK_DEBUG, "* could not find node for initial connect");
if(nodes->head) {
logger(mesh, MESHLINK_DEBUG, "* found better node");
- connect_to = (node_t*)nodes->head->data;
+ connect_to = (node_t *)nodes->head->data;
splay_free_tree(nodes);
break;
if(nodes->head) {
logger(mesh, MESHLINK_DEBUG, "* try to heal partition");
- connect_to = (node_t*)nodes->head->data;
+ connect_to = (node_t *)nodes->head->data;
} else
logger(mesh, MESHLINK_DEBUG, "* could not find nodes for partition healing");
if(nodes->head) {
logger(mesh, MESHLINK_DEBUG, "* disconnect suboptimal outgoing connection");
- disconnect_from = (node_t*)nodes->head->data;
+ disconnect_from = (node_t *)nodes->head->data;
}
splay_free_tree(nodes);
logger(mesh, MESHLINK_DEBUG, "* disconnect connection (too many connections)");
//timeout = 0;
- disconnect_from = (node_t*)nodes->head->data;
+ disconnect_from = (node_t *)nodes->head->data;
} else
logger(mesh, MESHLINK_DEBUG, "* no node we want to disconnect, even though we have too many connections");
//Add signal handler
mesh->datafromapp.signum = 0;
- signal_add(&(mesh->loop),&(mesh->datafromapp), (signal_cb_t)meshlink_send_from_queue,mesh, mesh->datafromapp.signum);
+ signal_add(&(mesh->loop), &(mesh->datafromapp), (signal_cb_t)meshlink_send_from_queue, mesh, mesh->datafromapp.signum);
if(!event_loop_run(&(mesh->loop), &(mesh->mesh_mutex))) {
logger(mesh, MESHLINK_ERROR, "Error while waiting for input: %s", strerror(errno));
typedef struct vpn_packet_t {
struct {
- unsigned int probe:1;
- unsigned int tcp:1;
+ unsigned int probe: 1;
+ unsigned int tcp: 1;
};
uint16_t len; /* the actual number of bytes in the `data' field */
uint8_t data[MAXSIZE];
FILE *fp;
char filename[PATH_MAX];
- snprintf(filename,PATH_MAX, "%s" SLASH "ecdsa_key.priv", mesh->confbase);
+ snprintf(filename, PATH_MAX, "%s" SLASH "ecdsa_key.priv", mesh->confbase);
fp = fopen(filename, "rb");
if(!fp) {
mesh->invitation_key = NULL;
}
- snprintf(filename,PATH_MAX, "%s" SLASH "invitations" SLASH "ecdsa_key.priv", mesh->confbase);
+ snprintf(filename, PATH_MAX, "%s" SLASH "invitations" SLASH "ecdsa_key.priv", mesh->confbase);
fp = fopen(filename, "rb");
// ignore read errors; in case the file does not exist we will create it
read_host_config(mesh, config_tree, n->name);
- config_t* cnf = lookup_config(config_tree, "DeviceClass");
+ config_t *cnf = lookup_config(config_tree, "DeviceClass");
if(!cnf) {
cnf = new_config();
struct dirent *ent;
char dname[PATH_MAX];
- snprintf(dname,PATH_MAX, "%s" SLASH "hosts", mesh->confbase);
+ snprintf(dname, PATH_MAX, "%s" SLASH "hosts", mesh->confbase);
dir = opendir(dname);
if(!dir) {
logger(mesh, MESHLINK_ERROR, "Could not open %s: %s", dname, strerror(errno));
connection_add(mesh, c);
- io_add(&mesh->loop, &c->io, handle_meta_io, c, c->socket, IO_READ|IO_WRITE);
+ io_add(&mesh->loop, &c->io, handle_meta_io, c, c->socket, IO_READ | IO_WRITE);
return true;
}
#include "utcp/utcp.h"
typedef struct node_status_t {
- unsigned int unused_active:1; /* 1 if active (not used for nodes) */
- unsigned int validkey:1; /* 1 if we currently have a valid key for him */
- unsigned int waitingforkey:1; /* 1 if we already sent out a request */
- unsigned int visited:1; /* 1 if this node has been visited by one of the graph algorithms */
- unsigned int reachable:1; /* 1 if this node is reachable in the graph */
- unsigned int indirect:1; /* 1 if this node is not directly reachable by us */
- unsigned int unused_sptps:1; /* 1 if this node supports SPTPS */
- unsigned int udp_confirmed:1; /* 1 if the address is one that we received UDP traffic on */
- unsigned int broadcast:1; /* 1 if the next UDP packet should be broadcast to the local network */
- unsigned int blacklisted:1; /* 1 if the node is blacklist so we never want to speak with him anymore*/
- unsigned int unused:22;
+ unsigned int unused_active: 1; /* 1 if active (not used for nodes) */
+ unsigned int validkey: 1; /* 1 if we currently have a valid key for him */
+ unsigned int waitingforkey: 1; /* 1 if we already sent out a request */
+ unsigned int visited: 1; /* 1 if this node has been visited by one of the graph algorithms */
+ unsigned int reachable: 1; /* 1 if this node is reachable in the graph */
+ unsigned int indirect: 1; /* 1 if this node is not directly reachable by us */
+ unsigned int unused_sptps: 1; /* 1 if this node supports SPTPS */
+ unsigned int udp_confirmed: 1; /* 1 if the address is one that we received UDP traffic on */
+ unsigned int broadcast: 1; /* 1 if the next UDP packet should be broadcast to the local network */
+ unsigned int blacklisted: 1; /* 1 if the node is blacklist so we never want to speak with him anymore*/
+ unsigned int unused: 22;
} node_status_t;
typedef struct node_t {
/* If there already is a lot of data in the outbuf buffer, discard this packet.
We use a very simple Random Early Drop algorithm. */
- if(2.0 * c->outbuf.len / (float)maxoutbufsize - 1 > (float)rand()/(float)RAND_MAX)
+ if(2.0 * c->outbuf.len / (float)maxoutbufsize - 1 > (float)rand() / (float)RAND_MAX)
return true;
if(!send_request(mesh, c, "%d %hd", PACKET, packet->len))
const void *payload = packet->data + sizeof(*hdr);
size_t len = packet->len - sizeof(*hdr);
- char hex[len*2 + 1];
+ char hex[len * 2 + 1];
if(mesh->log_level >= MESHLINK_DEBUG)
bin2hex(payload, hex, len); // don't do this unless it's going to be logged
logger(mesh, MESHLINK_DEBUG, "I received a packet for me with payload: %s\n", hex);
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);
+ fprintf(stderr, "%u.%.03u ", (unsigned int)(tv.tv_sec - tv0.tv_sec), (unsigned int)tv.tv_usec / 1000);
if(mesh)
fprintf(stderr, "(%s) ", mesh->name);
}
void status_cb(meshlink_handle_t *mesh, meshlink_node_t *node, bool reachable) {
- printf("status_cb: %s %sreachable\n", node->name, reachable?"":"un");
+ printf("status_cb: %s %sreachable\n", node->name, reachable ? "" : "un");
if(!strcmp(node->name, "bar"))
bar_reachable = reachable;
}
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)
void poll_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, size_t len) {
meshlink_set_channel_poll_cb(mesh, channel, NULL);
- bar_responded=true;
+ bar_responded = true;
}
int main1(void) {
meshlink_set_channel_poll_cb(mesh1, channel, poll_cb);
// read and buffer stdin
- int BUF_SIZE = 1024*1024;
+ int BUF_SIZE = 1024 * 1024;
char buffer[BUF_SIZE];
for(int i = 0; i < 5; i++) {