From 0bdce44687a22c8825695cba185b845f5ea5e060 Mon Sep 17 00:00:00 2001 From: Guus Sliepen Date: Sun, 13 Aug 2017 15:14:27 +0200 Subject: [PATCH] Enforce spaces around operators. --- .astylerc | 3 +++ examples/manynodes.c | 24 ++++++++++---------- examples/meshlinkapp.c | 10 ++++----- src/conf.c | 10 ++++----- src/connection.h | 30 ++++++++++++------------- src/devtools.c | 22 +++++++++--------- src/devtools.h | 2 +- src/discovery.c | 14 ++++++------ src/meshlink++.h | 50 ++++++++++++++++++++--------------------- src/meshlink.c | 36 ++++++++++++++--------------- src/meshlink.h | 2 +- src/meshlink_internal.h | 4 ++-- src/net.c | 16 ++++++------- src/net.h | 4 ++-- src/net_setup.c | 8 +++---- src/net_socket.c | 2 +- src/node.h | 22 +++++++++--------- src/protocol_misc.c | 2 +- src/route.c | 2 +- test/channels.c | 7 +++--- test/echo-fork.c | 4 ++-- 21 files changed, 138 insertions(+), 136 deletions(-) diff --git a/.astylerc b/.astylerc index 957d426c..dd196454 100644 --- a/.astylerc +++ b/.astylerc @@ -3,3 +3,6 @@ -xj -A2 -U +-p +-xg +-k3 diff --git a/examples/manynodes.c b/examples/manynodes.c index 77d895a3..bdc99bcf 100644 --- a/examples/manynodes.c +++ b/examples/manynodes.c @@ -23,7 +23,7 @@ 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; @@ -37,7 +37,7 @@ static void log_message(meshlink_handle_t *mesh, meshlink_log_level_t level, con [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 @@ -76,7 +76,7 @@ static void linkmesh() { } } -static bool exportmeshgraph(const char* path) { +static bool exportmeshgraph(const char *path) { struct stat ps; int psr = stat(path, &ps); @@ -89,7 +89,7 @@ static bool exportmeshgraph(const char* path) { return false; } - FILE* stream = fopen(path, "w"); + FILE *stream = fopen(path, "w"); if(!stream) { perror("stream"); @@ -112,7 +112,7 @@ void exportmeshgraph_timer(int signum) { 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); } @@ -120,7 +120,7 @@ void exportmeshgraph_timer(int signum) { #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; @@ -132,7 +132,7 @@ static bool exportmeshgraph_end(const char* none) { return true; } -static bool exportmeshgraph_begin(const char* timeout_str) { +static bool exportmeshgraph_begin(const char *timeout_str) { if(!timeout_str) return false; @@ -170,11 +170,11 @@ static bool exportmeshgraph_begin(const char* timeout_str) { 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 @@ -263,7 +263,7 @@ static void parse_command(char *buf) { 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")) { @@ -380,10 +380,10 @@ int main(int argc, char *argv[]) { 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); diff --git a/examples/meshlinkapp.c b/examples/meshlinkapp.c index cb15cae8..972544e1 100644 --- a/examples/meshlinkapp.c +++ b/examples/meshlinkapp.c @@ -3,7 +3,7 @@ #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) { @@ -12,7 +12,7 @@ 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); @@ -32,11 +32,11 @@ int main(int argc, char **argv) { //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); diff --git a/src/conf.c b/src/conf.c index 19cfa364..c5c3264e 100644 --- a/src/conf.c +++ b/src/conf.c @@ -169,7 +169,7 @@ bool get_config_string(const config_t *cfg, char **result) { 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; @@ -203,7 +203,7 @@ bool get_config_address(const config_t *cfg, struct addrinfo **result) { /* 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; @@ -356,7 +356,7 @@ bool read_server_config(meshlink_handle_t *mesh) { 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); @@ -370,7 +370,7 @@ bool read_host_config(meshlink_handle_t *mesh, splay_tree_t *config_tree, const 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; @@ -379,7 +379,7 @@ bool read_host_config(meshlink_handle_t *mesh, splay_tree_t *config_tree, const 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); } diff --git a/src/connection.h b/src/connection.h index 35dd1982..acab4484 100644 --- a/src/connection.h +++ b/src/connection.h @@ -31,21 +31,21 @@ #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" diff --git a/src/devtools.c b/src/devtools.c index 1cb2d5e8..7068c726 100644 --- a/src/devtools.c +++ b/src/devtools.c @@ -20,16 +20,16 @@ static int node_compare(const void *a, const void *b) { 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) @@ -38,7 +38,7 @@ static const char* __itoa(int value) { 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)); @@ -62,19 +62,19 @@ bool devtool_export_json_all_edges_state(meshlink_handle_t *mesh, FILE* stream) 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; } @@ -97,7 +97,7 @@ bool devtool_export_json_all_edges_state(meshlink_handle_t *mesh, FILE* stream) 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); @@ -118,7 +118,7 @@ bool devtool_export_json_all_edges_state(meshlink_handle_t *mesh, FILE* stream) 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; } diff --git a/src/devtools.h b/src/devtools.h index 53755187..d75ada5f 100644 --- a/src/devtools.h +++ b/src/devtools.h @@ -20,6 +20,6 @@ 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 diff --git a/src/discovery.c b/src/discovery.c index d82f48b7..8248e9e7 100644 --- a/src/discovery.c +++ b/src/discovery.c @@ -20,11 +20,11 @@ #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) { @@ -127,7 +127,7 @@ done: 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 @@ -251,8 +251,8 @@ static void discovery_resolve_callback(CattaSServiceResolver *resolver, CattaIfI 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; @@ -287,7 +287,7 @@ static void discovery_resolve_callback(CattaSServiceResolver *resolver, CattaIfI } 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 @@ -305,7 +305,7 @@ static void discovery_resolve_callback(CattaSServiceResolver *resolver, CattaIfI 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 diff --git a/src/meshlink++.h b/src/meshlink++.h index b012bb4a..a949f761 100644 --- a/src/meshlink++.h +++ b/src/meshlink++.h @@ -108,7 +108,7 @@ public: } bool isOpen() const { - return (handle!=0); + return (handle != 0); } // TODO: please enable C++11 in autoconf to enable "move constructors": @@ -137,7 +137,7 @@ public: * * @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; @@ -145,7 +145,7 @@ public: 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); } @@ -159,7 +159,7 @@ public: 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. @@ -171,13 +171,13 @@ public: */ /// 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. /** @@ -570,36 +570,36 @@ public: 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(handle->priv); - that->receive(static_cast(source), data, length); + meshlink::mesh *that = static_cast(handle->priv); + that->receive(static_cast(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(handle->priv); - that->node_status(static_cast(peer), reachable); + meshlink::mesh *that = static_cast(handle->priv); + that->node_status(static_cast(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(handle->priv); + meshlink::mesh *that = static_cast(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(handle->priv); - bool accepted = that->channel_accept(static_cast(channel), port, data, len); + meshlink::mesh *that = static_cast(handle->priv); + bool accepted = that->channel_accept(static_cast(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); @@ -607,21 +607,21 @@ private: 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(handle->priv); - that->channel_receive(static_cast(channel), data, len); + meshlink::mesh *that = static_cast(handle->priv); + that->channel_receive(static_cast(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(handle->priv); - that->channel_poll(static_cast(channel), len); + meshlink::mesh *that = static_cast(handle->priv); + that->channel_poll(static_cast(channel), len); } - meshlink_handle_t* handle; + meshlink_handle_t *handle; }; static const char *strerror(errno_t err = meshlink_errno) { diff --git a/src/meshlink.c b/src/meshlink.c index 7da7a9e1..fce1f736 100644 --- a/src/meshlink.c +++ b/src/meshlink.c @@ -255,7 +255,7 @@ char *meshlink_get_external_address(meshlink_handle_t *mesh) { 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; @@ -303,7 +303,7 @@ static char *get_line(const char **data) { if(!data || !*data) return NULL; - if(!**data) { + if(! **data) { *data = NULL; return NULL; } @@ -553,7 +553,7 @@ static bool finalize_join(meshlink_handle_t *mesh) { } 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) @@ -567,7 +567,7 @@ static bool invitation_send(void *handle, uint8_t type, const void *data, size_t } 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); @@ -596,7 +596,7 @@ static bool invitation_receive(void *handle, uint8_t type, const void *msg, uint 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) @@ -841,7 +841,7 @@ static bool meshlink_setup(meshlink_handle_t *mesh) { 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; @@ -1007,7 +1007,7 @@ bool meshlink_start(meshlink_handle_t *mesh) { return false; } - mesh->threadstarted=true; + mesh->threadstarted = true; if(mesh->discovery) discovery_start(mesh); @@ -1195,7 +1195,7 @@ bool meshlink_send(meshlink_handle_t *mesh, meshlink_node_t *destination, const // 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); @@ -1207,7 +1207,7 @@ bool meshlink_send(meshlink_handle_t *mesh, meshlink_node_t *destination, const } // Notify event loop - signal_trigger(&(mesh->loop),&(mesh->datafromapp)); + signal_trigger(&(mesh->loop), &(mesh->datafromapp)); return true; } @@ -2020,9 +2020,9 @@ void meshlink_blacklist(meshlink_handle_t *mesh, meshlink_node_t *node) { 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"); @@ -2070,7 +2070,7 @@ void meshlink_hint_address(meshlink_handle_t *mesh, meshlink_node_t *node, const 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"); @@ -2104,7 +2104,7 @@ meshlink_edge_t **meshlink_get_all_edges_state(meshlink_handle_t *mesh, meshlink // 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; @@ -2123,8 +2123,8 @@ meshlink_edge_t **meshlink_get_all_edges_state(meshlink_handle_t *mesh, meshlink 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; @@ -2133,7 +2133,7 @@ meshlink_edge_t **meshlink_get_all_edges_state(meshlink_handle_t *mesh, meshlink // 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; @@ -2360,7 +2360,7 @@ static void __attribute__((destructor)) meshlink_exit(void) { } /// 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 diff --git a/src/meshlink.h b/src/meshlink.h index a768d435..be929c29 100644 --- a/src/meshlink.h +++ b/src/meshlink.h @@ -166,7 +166,7 @@ extern const char *meshlink_strerror(meshlink_errno_t err); * @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 diff --git a/src/meshlink_internal.h b/src/meshlink_internal.h index 492e2b2b..46cbbb1e 100644 --- a/src/meshlink_internal.h +++ b/src/meshlink_internal.h @@ -143,7 +143,7 @@ struct meshlink_handle { struct CattaSServiceBrowser *catta_browser; struct CattaSimplePoll *catta_poll; struct CattaSEntryGroup *catta_group; - char* catta_servicetype; + char *catta_servicetype; }; /// A handle for a MeshLink node. @@ -168,7 +168,7 @@ typedef struct meshlink_packethdr { 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; diff --git a/src/net.c b/src/net.c index 03bac617..45a9abbf 100644 --- a/src/net.c +++ b/src/net.c @@ -338,8 +338,8 @@ static void periodic_handler(event_loop_t *loop, void *data) { // 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 @@ -380,7 +380,7 @@ static void periodic_handler(event_loop_t *loop, void *data) { 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"); @@ -409,7 +409,7 @@ static void periodic_handler(event_loop_t *loop, void *data) { 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; @@ -437,7 +437,7 @@ static void periodic_handler(event_loop_t *loop, void *data) { 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"); @@ -492,7 +492,7 @@ static void periodic_handler(event_loop_t *loop, void *data) { 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); @@ -519,7 +519,7 @@ static void periodic_handler(event_loop_t *loop, void *data) { 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"); @@ -589,7 +589,7 @@ int main_loop(meshlink_handle_t *mesh) { //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)); diff --git a/src/net.h b/src/net.h index 1eed88d1..fa34dcf7 100644 --- a/src/net.h +++ b/src/net.h @@ -37,8 +37,8 @@ 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]; diff --git a/src/net_setup.c b/src/net_setup.c index b02dbd14..acdea368 100644 --- a/src/net_setup.c +++ b/src/net_setup.c @@ -82,7 +82,7 @@ bool read_ecdsa_private_key(meshlink_handle_t *mesh) { 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) { @@ -108,7 +108,7 @@ static bool read_invitation_key(meshlink_handle_t *mesh) { 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"); @@ -157,7 +157,7 @@ bool node_write_devclass(meshlink_handle_t *mesh, node_t *n) { // 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(); @@ -182,7 +182,7 @@ void load_all_nodes(meshlink_handle_t *mesh) { 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)); diff --git a/src/net_socket.c b/src/net_socket.c index ef34d0a8..dddd866a 100644 --- a/src/net_socket.c +++ b/src/net_socket.c @@ -517,7 +517,7 @@ begin: 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; } diff --git a/src/node.h b/src/node.h index 33bac256..afcd43ea 100644 --- a/src/node.h +++ b/src/node.h @@ -26,17 +26,17 @@ #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 { diff --git a/src/protocol_misc.c b/src/protocol_misc.c index b4470808..6b3c4ab8 100644 --- a/src/protocol_misc.c +++ b/src/protocol_misc.c @@ -125,7 +125,7 @@ bool send_tcppacket(meshlink_handle_t *mesh, connection_t *c, const vpn_packet_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)) diff --git a/src/route.c b/src/route.c index 9ab0688d..1c0048e2 100644 --- a/src/route.c +++ b/src/route.c @@ -59,7 +59,7 @@ void route(meshlink_handle_t *mesh, node_t *source, vpn_packet_t *packet) { 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); diff --git a/test/channels.c b/test/channels.c index 10369ca6..cf1ff891 100644 --- a/test/channels.c +++ b/test/channels.c @@ -17,7 +17,7 @@ void log_cb(meshlink_handle_t *mesh, meshlink_log_level_t level, const char *tex 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); @@ -25,7 +25,7 @@ void log_cb(meshlink_handle_t *mesh, meshlink_log_level_t level, const char *tex } 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; } @@ -52,9 +52,8 @@ bool reject_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, uint16_t po 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) diff --git a/test/echo-fork.c b/test/echo-fork.c index 289e24a7..bd8da445 100644 --- a/test/echo-fork.c +++ b/test/echo-fork.c @@ -58,7 +58,7 @@ bool accept_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, uint16_t po 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) { @@ -104,7 +104,7 @@ 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++) { -- 2.39.5