diff --git a/Android.mk b/Android.mk index 402d4fb1..2c1286c4 100644 --- a/Android.mk +++ b/Android.mk @@ -6,6 +6,7 @@ include $(LOCAL_PATH)/sourcefiles.mk SERVALD_SRC_FILES = \ $(SQLITE3_SOURCES) \ $(SERVAL_CLIENT_SOURCES) \ + $(MDP_CLIENT_SOURCES) \ $(SERVAL_DAEMON_SOURCES) \ $(ANDROIDONLY_SOURCES) NACL_BASE = nacl/src diff --git a/Makefile.in b/Makefile.in index 9162b3df..5638c1d5 100644 --- a/Makefile.in +++ b/Makefile.in @@ -10,26 +10,25 @@ include $(NACL_BASE)/nacl.mk include sourcefiles.mk include headerfiles.mk -SERVALD_SRCS = \ - $(SQLITE3_SOURCES) \ - $(NACL_SOURCES) \ - $(SERVAL_CLIENT_SOURCES) \ - $(SERVAL_DAEMON_SOURCES) +SERVAL_DAEMON_OBJS = \ + $(addprefix objs_servald/, $(SERVAL_CLIENT_SOURCES:.c=.o)) \ + $(addprefix objs_servald/, $(MDP_CLIENT_SOURCES:.c=.o)) \ + $(addprefix objs_servald/, $(SERVAL_DAEMON_SOURCES:.c=.o)) +SERVALD_OBJS = \ + $(addprefix objs_servald/, $(notdir $(SQLITE3_SOURCES:.c=.o))) \ + $(addprefix objs_servald/, $(NACL_SOURCES:.c=.o)) \ + $(SERVAL_DAEMON_OBJS) +LIB_SERVAL_OBJS = \ + $(addprefix objs_lib/, $(SERVAL_CLIENT_SOURCES:.c=.o)) \ + $(addprefix objs_lib/, $(SERVAL_LIB_SOURCES:.c=.o)) \ + $(addprefix objs_lib/, $(MDP_CLIENT_SOURCES:.c=.o)) -MONITOR_CLIENT_SRCS = \ - $(SERVAL_CLIENT_SOURCES) \ - monitor-client.c +MONITOR_CLIENT_OBJS = \ + $(addprefix objs_lib/, $(SERVAL_CLIENT_SOURCES:.c=.o)) \ + $(addprefix objs_lib/, $(MONITOR_CLIENT_SRCS:.c=.o)) -MDP_CLIENT_SRCS = \ - $(SERVAL_CLIENT_SOURCES) \ - mdp_client.c \ - mdp_net.c - -SERVALD_OBJS= $(SERVALD_SRCS:.c=.o) -SERVAL_DAEMON_OBJS= $(SERVAL_DAEMON_SOURCES:.c=.o) -MONITOR_CLIENT_OBJS= $(MONITOR_CLIENT_SRCS:.c=.o) -MDP_CLIENT_OBJS= $(MDP_CLIENT_SRCS:.c=.o) -SIMULATOR_OBJS= $(SIMULATOR_SOURCES:.c=.o) +SIMULATOR_OBJS = \ + $(addprefix objs/, $(SIMULATOR_SOURCES:.c=.o)) LDFLAGS=@LDFLAGS@ @LIBS@ @PTHREAD_LIBS@ @@ -61,54 +60,75 @@ all: servald libmonitorclient.so libmonitorclient.a test test: tfw_createfile directory_service fakeradio config_test simulator -sqlite-amalgamation-3070900/sqlite3.o: sqlite-amalgamation-3070900/sqlite3.c - @echo CC $< - @$(CC) $(CFLAGS) $(DEFS) -c $< -o sqlite-amalgamation-3070900/sqlite3.o - -version.o: $(SERVALD_OBJS) version_string.sh $(wildcard VERSION.txt) COPYRIGHT.txt Makefile - @echo CC version_servald.c - @V=`./version_string.sh --ignore-untracked` \ - && C="`sed -e :a -e N -e '$$!ba' -e 's/[\\\\"]/\\\\&/g' -e 's/\\n/\\\\n/g' COPYRIGHT.txt`" \ - && $(CC) -c version_servald.c -o $@ -DSERVALD_VERSION="\"$$V\"" -DSERVALD_COPYRIGHT="\"$$C\"" - Makefile: $(wildcard Makefile.in) $(wildcard configure) $(warning Makefile may be out of date, please run ./configure) configure: $(wildcard configure.in) $(warning configure may be out of date, please run autoreconf -f -i) -%.o: %.c +objs/version.o: $(SERVALD_OBJS) version_string.sh $(wildcard VERSION.txt) COPYRIGHT.txt Makefile + @echo CC version_servald.c + @V=`./version_string.sh --ignore-untracked` \ + && C="`sed -e :a -e N -e '$$!ba' -e 's/[\\\\"]/\\\\&/g' -e 's/\\n/\\\\n/g' COPYRIGHT.txt`" \ + && $(CC) -c version_servald.c -o $@ -DSERVALD_VERSION="\"$$V\"" -DSERVALD_COPYRIGHT="\"$$C\"" + +#' <-- fixes vim syntax highlighting + +objs_servald/sqlite3.o: sqlite-amalgamation-3070900/sqlite3.c + @echo SERVALD CC $< + @mkdir -p $(dir $@) + @$(CC) $(CFLAGS) $(DEFS) -c $< -o $@ + +# No object files in source directory! +%.o: %.c + +objs/%.o: %.c @echo CC $< + @mkdir -p $(dir $@) + @$(CC) $(CFLAGS) $(DEFS) -c $< -o $@ + +objs_servald/%.o: %.c + @echo SERVALD CC $< + @mkdir -p $(dir $@) + @$(CC) $(CFLAGS) $(DEFS) -c $< -o $@ + +objs_lib/%.o: %.c + @echo LIB CC $< + @mkdir -p $(dir $@) @$(CC) $(CFLAGS) $(DEFS) -c $< -o $@ $(SERVAL_DAEMON_OBJS): $(HDRS) +$(LIB_SERVAL_OBJS): $(HDRS) $(MONITOR_CLIENT_OBJS): $(HDRS) -$(MDP_CLIENT_OBJS): $(HDRS) $(SIMULATOR_OBJS): $(HDRS) -servald: $(SERVALD_OBJS) version.o +servald: $(SERVALD_OBJS) objs/version.o @echo LINK $@ - @$(CC) $(CFLAGS) -Wall -o $@ $(SERVALD_OBJS) version.o $(LDFLAGS) + @$(CC) $(CFLAGS) -Wall -o $@ $(SERVALD_OBJS) objs/version.o $(LDFLAGS) -directory_service: $(MDP_CLIENT_OBJS) version.o directory_service.o - @echo LINK $@ - @$(CC) $(CFLAGS) -Wall -o $@ $(MDP_CLIENT_OBJS) version.o directory_service.o $(LDFLAGS) +libserval.a: $(LIB_SERVAL_OBJS) objs/version.o + @echo AR $@ + @$(AR) -cr $@ $(LIB_SERVAL_OBJS) objs/version.o -tfw_createfile: tfw_createfile.o str.o strbuf.o strbuf_helpers.o +directory_service: objs/directory_service.o libserval.a @echo LINK $@ - @$(CC) $(CFLAGS) -Wall -o $@ tfw_createfile.o str.o strbuf.o strbuf_helpers.o + @$(CC) $(CFLAGS) -Wall -o $@ objs/directory_service.o libserval.a $(LDFLAGS) -fakeradio: fakeradio.o +tfw_createfile: objs/tfw_createfile.o libserval.a @echo LINK $@ - @$(CC) $(CFLAGS) -Wall -o $@ fakeradio.o + @$(CC) $(CFLAGS) -Wall -o $@ objs/tfw_createfile.o libserval.a $(LDFLAGS) -simulator: $(SIMULATOR_OBJS) +fakeradio: objs/fakeradio.o libserval.a @echo LINK $@ - @$(CC) $(CFLAGS) -Wall -o $@ $(SIMULATOR_OBJS) $(LDFLAGS) + @$(CC) $(CFLAGS) -Wall -o $@ objs/fakeradio.o libserval.a $(LDFLAGS) -config_test: config_test.o conf_om.o conf_schema.o conf_parse.o str.o strbuf.o strbuf_helpers.o mem.o dataformats.o net.o log_util.o +simulator: $(SIMULATOR_OBJS) libserval.a @echo LINK $@ - @$(CC) $(CFLAGS) -Wall -o $@ config_test.o conf_om.o conf_schema.o conf_parse.o str.o strbuf.o strbuf_helpers.o mem.o dataformats.o net.o log_util.o $(LDFLAGS) + @$(CC) $(CFLAGS) -Wall -o $@ $(SIMULATOR_OBJS) libserval.a $(LDFLAGS) + +config_test: objs/config_test.o objs/conf_om.o objs/conf_schema.o objs/conf_parse.o libserval.a + @echo LINK $@ + @$(CC) $(CFLAGS) -Wall -o $@ objs/config_test.o objs/conf_om.o objs/conf_schema.o objs/conf_parse.o libserval.a $(LDFLAGS) copyright: @if [ -x "$(COPYRIGHT_TOOL)" ]; then \ @@ -123,17 +143,17 @@ COPYRIGHT_TOOL := $(call findPATH,sp-copyright-tool) # This does not build on 64 bit elf platforms as NaCL isn't built with -fPIC # DOC 20120615 -libserval.so: $(SERVALD_OBJS) version.o +libserval.so: $(SERVALD_OBJS) objs/version.o @echo LINK $@ - @$(CC) $(CFLAGS) -Wall -shared -o $@ $(SERVALD_OBJS) version.o $(LDFLAGS) + @$(CC) $(CFLAGS) -Wall -shared -o $@ $(SERVALD_OBJS) objs/version.o $(LDFLAGS) -libmonitorclient.so: $(MONITOR_CLIENT_OBJS) version.o +libmonitorclient.so: $(MONITOR_CLIENT_OBJS) objs/version.o @echo LINK $@ - @$(CC) $(CFLAGS) -Wall -shared -o $@ $(MONITOR_CLIENT_OBJS) version.o $(LDFLAGS) + @$(CC) $(CFLAGS) -Wall -shared -o $@ $(MONITOR_CLIENT_OBJS) objs/version.o $(LDFLAGS) -libmonitorclient.a: $(MONITOR_CLIENT_OBJS) version.o +libmonitorclient.a: $(MONITOR_CLIENT_OBJS) objs/version.o @echo AR $@ - @$(AR) -cr $@ $(MONITOR_CLIENT_OBJS) version.o + @$(AR) -cr $@ $(MONITOR_CLIENT_OBJS) objs/version.o install: servald $(INSTALL_PROGRAM) -D servald $(DESTDIR)$(sbindir)/servald @@ -142,8 +162,8 @@ uninstall: $(RM) $(DESTDIR)$(sbindir)/servald clean: - @$(RM) $(SERVALD_OBJS) $(MONITOR_CLIENT_OBJS) $(MDP_CLIENT_OBJS) \ - tfw_createfile.o version.o \ - fakeradio.o fakeradio \ - tfw_createfile servald \ - libservald.so libmonitorclient.so libmonitorclient.a + @$(RM) -r objs/* objs_servald/* objs_lib/* \ + servald libservald.so \ + libserval.a libmonitorclient.so libmonitorclient.a \ + fakeradio \ + tfw_createfile diff --git a/cli.c b/cli.c index b2914ad0..a4400708 100644 --- a/cli.c +++ b/cli.c @@ -18,14 +18,17 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include -#include #include -#include "cli.h" +#include "constants.h" +#include "serval_types.h" +#include "rhizome_types.h" +#include "fdqueue.h" +#include "os.h" #include "log.h" -#include "serval.h" -#include "rhizome.h" +#include "str.h" #include "strbuf_helpers.h" #include "dataformats.h" +#include "cli.h" int cli_usage(const struct cli_schema *commands, XPRINTF xpf) { diff --git a/commandline.c b/commandline.c index 9f9c7288..f76a10a7 100644 --- a/commandline.c +++ b/commandline.c @@ -51,9 +51,6 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. #include #include #include -#ifdef HAVE_STRINGS_H -#include -#endif #include #include #include @@ -70,6 +67,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. #include "strbuf.h" #include "strbuf_helpers.h" #include "str.h" +#include "os.h" #include "mdp_client.h" #include "cli.h" #include "overlay_address.h" diff --git a/conf.c b/conf.c index 60c7894e..3b68875f 100644 --- a/conf.c +++ b/conf.c @@ -21,7 +21,9 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. #include #include #include +#include // for PRIu64 on Android #include "conf.h" +#include "instance.h" #include "log.h" #include "str.h" #include "mem.h" diff --git a/conf.h b/conf.h index 69738168..1cc24b12 100644 --- a/conf.h +++ b/conf.h @@ -229,9 +229,9 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. #include #include "constants.h" +#include "serval_types.h" +#include "rhizome_types.h" #include "strbuf.h" -#include "serval.h" -#include "rhizome.h" #include "httpd.h" #define CONFIG_FILE_MAX_SIZE (32 * 1024) diff --git a/conf_parse.c b/conf_parse.c index c8065d55..721061ee 100644 --- a/conf_parse.c +++ b/conf_parse.c @@ -19,6 +19,8 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. #include #include "constants.h" +#include "net.h" +#include "mem.h" #include "log.h" #include "conf.h" diff --git a/conf_schema.c b/conf_schema.c index ad22e9c7..0246c90a 100644 --- a/conf_schema.c +++ b/conf_schema.c @@ -28,6 +28,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. #include #include "log.h" +#include "mem.h" #include "str.h" #include "strbuf.h" #include "strbuf_helpers.h" diff --git a/config_test.c b/config_test.c index 2dd07cf8..b26f1042 100644 --- a/config_test.c +++ b/config_test.c @@ -108,46 +108,6 @@ int main(int argc, char **argv) exit(0); } -const struct __sourceloc __whence = __NOWHERE__; - -static const char *_trimbuildpath(const char *path) +void cf_on_config_change() { - /* Remove common path prefix */ - int lastsep = 0; - int i; - for (i = 0; __FILE__[i] && path[i]; ++i) { - if (i && path[i - 1] == '/') - lastsep = i; - if (__FILE__[i] != path[i]) - break; - } - return &path[lastsep]; -} - -void logMessage(int level, struct __sourceloc whence, const char *fmt, ...) -{ - const char *levelstr = "UNKWN:"; - switch (level) { - case LOG_LEVEL_FATAL: levelstr = "FATAL:"; break; - case LOG_LEVEL_ERROR: levelstr = "ERROR:"; break; - case LOG_LEVEL_INFO: levelstr = "INFO:"; break; - case LOG_LEVEL_WARN: levelstr = "WARN:"; break; - case LOG_LEVEL_DEBUG: levelstr = "DEBUG:"; break; - } - fprintf(stderr, "%s ", levelstr); - if (whence.file) { - fprintf(stderr, "%s", _trimbuildpath(whence.file)); - if (whence.line) - fprintf(stderr, ":%u", whence.line); - if (whence.function) - fprintf(stderr, ":%s()", whence.function); - fputc(' ', stderr); - } else if (whence.function) { - fprintf(stderr, "%s() ", whence.function); - } - va_list ap; - va_start(ap, fmt); - vfprintf(stderr, fmt, ap); - va_end(ap); - fputc('\n', stderr); } diff --git a/console.h b/console.h index 24ebb4c2..34a3badf 100644 --- a/console.h +++ b/console.h @@ -1,4 +1,5 @@ /* + Interactive console primitives Copyright (C) 2014 Serval Project Inc. This program is free software; you can redistribute it and/or @@ -16,10 +17,15 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ +#ifndef __SERVAL_DNA__CONSOLE_H +#define __SERVAL_DNA__CONSOLE_H + +#include "cli.h" + struct command_state; struct command_state *command_register(struct cli_schema *commands, int fd); uint8_t is_command_closed(struct command_state *state); void command_close(struct command_state *state); void command_free(struct command_state *state); - +#endif // __SERVAL_DNA__CONSOLE_H diff --git a/constants.h b/constants.h index 2174dfd7..4e884098 100644 --- a/constants.h +++ b/constants.h @@ -32,12 +32,8 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. # define UNUSED(x) x #endif -#define SID_SIZE 32 // == crypto_sign_edwards25519sha512batch_PUBLICKEYBYTES -#define SAS_SIZE 32 // == crypto_sign_edwards25519sha512batch_PUBLICKEYBYTES -#define DID_MINSIZE 5 -#define DID_MAXSIZE 32 - -#define SID_STRLEN (SID_SIZE*2) +// UDP Port numbers for various Serval services. +#define PORT_DNA 4110 #define OVERLAY_MAX_INTERFACES 16 @@ -191,7 +187,4 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. #define UNLOCK_CHALLENGE (0xF1) #define UNLOCK_RESPONSE (0xF2) -// should there be a types.h to hold this? -typedef char bool_t; - #endif // __SERVAL_DNA__CONSTANTS_H diff --git a/dataformats.c b/dataformats.c index 4e8bfd15..f55cee18 100644 --- a/dataformats.c +++ b/dataformats.c @@ -17,9 +17,11 @@ along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ +#define __RHIZOME_TYPES_INLINE #include -#include "serval.h" -#include "rhizome.h" +#include "serval_types.h" +#include "rhizome_types.h" +#include "os.h" #include "str.h" #include "dataformats.h" diff --git a/directory_client.c b/directory_client.c index 86f056dd..a154e89b 100644 --- a/directory_client.c +++ b/directory_client.c @@ -30,13 +30,15 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ -#include "serval.h" +#include "serval_types.h" +#include "cli.h" #include "str.h" #include "overlay_address.h" #include "overlay_packet.h" #include "overlay_buffer.h" #include "conf.h" #include "keyring.h" +#include "serval.h" // for overlay_send_frame() struct subscriber *directory_service; diff --git a/dna_helper.c b/dna_helper.c index 209cd7d1..2a35566b 100644 --- a/dna_helper.c +++ b/dna_helper.c @@ -51,13 +51,14 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. #include #include #include -#include "serval.h" +#include "sighandlers.h" #include "conf.h" #include "str.h" #include "strbuf.h" #include "strbuf_helpers.h" -#include "overlay_address.h" #include "dataformats.h" +#include "overlay_address.h" +#include "serval.h" /* The challenge with making an interface for calling an external program to diff --git a/fakeradio.c b/fakeradio.c index 026e5f05..d3dcb1a5 100644 --- a/fakeradio.c +++ b/fakeradio.c @@ -29,9 +29,8 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. #include #include #include -#include -#include #include +#include "os.h" #define PACKET_SIZE 255 int chars_per_ms=1; @@ -130,25 +129,6 @@ int processCommand(struct radio_state *s) return 1; } -int dump(char *name, unsigned char *addr, size_t len) -{ - unsigned i,j; - if (name) - fprintf(stderr,"Dump of %s\n",name); - for(i=0;i=' '&&addr[i+j]<0x7f?addr[i+j]:'.'); - fprintf(stderr,"\n"); - } - return 0; -} - static void store_char(struct radio_state *s, unsigned char c) { if(s->txb_lentxbuffer)){ diff --git a/fdqueue.c b/fdqueue.c index d7481a6a..79314cc4 100644 --- a/fdqueue.c +++ b/fdqueue.c @@ -46,8 +46,10 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. POSSIBILITY OF SUCH DAMAGE. */ +#include // for PRIu64 on Android #include "fdqueue.h" #include "conf.h" +#include "net.h" #include "str.h" #include "strbuf.h" #include "strbuf_helpers.h" diff --git a/headerfiles.mk b/headerfiles.mk index 4914b22c..3d5a986b 100644 --- a/headerfiles.mk +++ b/headerfiles.mk @@ -9,6 +9,7 @@ HDRS= fifo.h \ httpd.h \ instance.h \ meshms.h \ + serval_types.h \ serval.h \ keyring.h \ socket.h \ @@ -18,6 +19,7 @@ HDRS= fifo.h \ mem.h \ os.h \ uuid.h \ + sighandlers.h \ strbuf.h \ strbuf_helpers.h \ sha2.h \ diff --git a/http_server.c b/http_server.c index def7a9da..dbcd6266 100644 --- a/http_server.c +++ b/http_server.c @@ -20,9 +20,10 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. #include #include #include -#include "serval.h" -#include "conf.h" +#include "serval_types.h" #include "http_server.h" +#include "sighandlers.h" +#include "conf.h" #include "log.h" #include "str.h" #include "strbuf.h" diff --git a/http_server.h b/http_server.h index 5456258b..6c4ed923 100644 --- a/http_server.h +++ b/http_server.h @@ -21,7 +21,8 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. #define __SERVAL_DNA__HTTP_SERVER_H #include -#include "constants.h" +#include "serval_types.h" +#include "net.h" #include "strbuf.h" #include "strbuf_helpers.h" #include "fdqueue.h" diff --git a/httpd.c b/httpd.c index 271bb50e..9f15cfb4 100644 --- a/httpd.c +++ b/httpd.c @@ -17,11 +17,14 @@ along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ +#include #include "serval.h" -#include "conf.h" #include "httpd.h" +#include "conf.h" #include "overlay_address.h" #include "overlay_interface.h" +#include "mem.h" +#include "net.h" #define RHIZOME_SERVER_MAX_LIVE_REQUESTS 32 diff --git a/instance.c b/instance.c index 4cb4c944..071f0ec0 100644 --- a/instance.c +++ b/instance.c @@ -18,7 +18,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include -#include "serval.h" +#include "instance.h" #include "str.h" #include "os.h" #include "strbuf.h" diff --git a/log.c b/log.c index a956b3fc..88c41827 100644 --- a/log.c +++ b/log.c @@ -36,7 +36,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. #include #include -#define __SERVAL_LOG_INLINE +#include "serval.h" #include "log.h" #include "net.h" #include "os.h" @@ -47,7 +47,6 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. #include "xprintf.h" int serverMode = 0; -const struct __sourceloc __whence = __NOWHERE__; #define NO_FILE ((FILE *)1) diff --git a/log.h b/log.h index 033d02ac..aef63b17 100644 --- a/log.h +++ b/log.h @@ -19,70 +19,12 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. #ifndef __SERVAL_DNA__LOG_H #define __SERVAL_DNA__LOG_H -#include +#include // for size_t +#include // for NULL #include #include -#include #include - -/* - * Every log message identifies the location in the source code at which the - * message was produced. This location is represented by a struct __sourceloc, - * which is passed by value to the logMessage() function and its ilk. - * - * A struct __sourceloc value is generated by the __HERE__ macro, which uses - * the cpp(1) built-in macros __FILE__, __LINE__ and __FUNCTION__ to generate - * its elements. The __NOWHERE__ macro creates a struct __sourceloc with NULL - * and zero fields. If you pass __NOWHERE__ to logMessage(), it will omit - * location information from the log line. The __NOWHENCE__ macro creates a - * special source __sourceloc that logging primitives should interpret to - * suppress the output of the usual source-code location information. - * - * Sometimes, a function wants to log a message as though its caller were the - * origin of the message. This is typical of "primitive" type functions that - * are used in many places throughout the code, and whose internal workings are - * generally well-debugged and of little interest for ongoing development. In - * this case, the code pattern is to declare the underscore-prefixed function - * as taking a struct __sourceloc argument, and a macro that invokes the - * function, passing the __HERE__ macro for that argument: - * - * int _primitive(struct __sourceloc __whence, int arg1, const char *arg2); - * - * #define primitive(arg1, arg2) _primitive(__HERE__, (arg1), (arg2)) - * - * Within the _primitive() function, the standard logging macros defined below - * (WHYF(), WARNF(), INFOF(), DEBUGF() etc.) will use the __whence argument - * instead of __HERE__ when logging their message. This is achieved using a - * dirty trick: in the function *definition*, the __sourceloc argument MUST be - * named '__whence'. The trick is that there is a global variable called - * '__whence' which always contains the value of __NOWHERE__. If that variable - * is lexically obscured by a local variable or parameter called '__whence', - * then the DEBUG macros will use __whence, otherwise they will use __HERE__. - * This logic is encapsulated in the __WHENCE__ macro, to make it available to - * for other purposes. For example, a better definition of the primitive() - * macro above would be: - * - * #define primitive(arg1, arg2) _primitive(__WHENCE__, (arg1), (arg2)) - * - * Then, if it were invoked from within another primitive-type function, it - * would log messages with the __sourceloc of that primitive's caller, which is - * probably the most useful for diagnosis. - * - * @author Andrew Bettison - */ - -struct __sourceloc { - const char *file; - unsigned int line; - const char *function; -}; - -extern const struct __sourceloc __whence; // see above - -#define __HERE__ ((struct __sourceloc){ .file = __FILE__, .line = __LINE__, .function = __FUNCTION__ }) -#define __NOWHERE__ ((struct __sourceloc){ .file = NULL, .line = 0, .function = NULL }) -#define __NOWHENCE__ ((struct __sourceloc){ .file = "", .line = 0, .function = NULL }) -#define __WHENCE__ (__whence.file ? __whence : __HERE__) +#include "whence.h" #ifndef __SERVAL_LOG_INLINE # if __GNUC__ && !__GNUC_STDC_INLINE__ diff --git a/logMessage.c b/logMessage.c new file mode 100644 index 00000000..60bceb58 --- /dev/null +++ b/logMessage.c @@ -0,0 +1,24 @@ +/* +Serval DNA logging +Copyright 2013-2014 Serval Project Inc. + +This program is free software; you can redistribute it and/or +modify it under the terms of the GNU General Public License +as published by the Free Software Foundation; either version 2 +of the License, or (at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +*/ + +// This compilation unit simply provides a statically linkable logMessage() function for those other +// units that did not inline it. + +#define __SERVAL_LOG_INLINE +#include "log.h" diff --git a/log_stderr.c b/log_stderr.c new file mode 100644 index 00000000..7756e4dc --- /dev/null +++ b/log_stderr.c @@ -0,0 +1,96 @@ +/* +Serval logging to standard error +Copyright 2014 Serval Project Inc. + +This program is free software; you can redistribute it and/or +modify it under the terms of the GNU General Public License +as published by the Free Software Foundation; either version 2 +of the License, or (at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +*/ + +#include +#include +#include +#include "log.h" + +/* An implementation of the Serval logging API that writes directly to standard error + * using stdio buffered output. + */ + +int serverMode=0; + +static const char *_trimbuildpath(const char *path) +{ + /* Remove common path prefix */ + int lastsep = 0; + int i; + for (i = 0; __FILE__[i] && path[i]; ++i) { + if (i && path[i - 1] == '/') + lastsep = i; + if (__FILE__[i] != path[i]) + break; + } + return &path[lastsep]; +} + +void vlogMessage(int level, struct __sourceloc whence, const char *fmt, va_list ap) +{ + const char *levelstr = "UNKWN:"; + switch (level) { + case LOG_LEVEL_FATAL: + levelstr = "FATAL:"; + break; + case LOG_LEVEL_ERROR: + levelstr = "ERROR:"; + break; + case LOG_LEVEL_INFO: + levelstr = "INFO:"; + break; + case LOG_LEVEL_WARN: + levelstr = "WARN:"; + break; + case LOG_LEVEL_DEBUG: + levelstr = "DEBUG:"; + break; + } + + struct timeval tv; + struct tm tm; + gettimeofday(&tv, NULL); + localtime_r(&tv.tv_sec, &tm); + char buf[50]; + strftime(buf, sizeof buf, "%T", &tm); + fprintf(stderr, "%s.%03u ", buf, (unsigned int)tv.tv_usec / 1000); + + fprintf(stderr, "%s ", levelstr); + if (whence.file) { + fprintf(stderr, "%s", _trimbuildpath(whence.file)); + if (whence.line) + fprintf(stderr, ":%u", whence.line); + if (whence.function) + fprintf(stderr, ":%s()", whence.function); + fputc(' ', stderr); + } else if (whence.function) { + fprintf(stderr, "%s() ", whence.function); + } + vfprintf(stderr, fmt, ap); + fputc('\n', stderr); +} + +void logFlush() +{ + fflush(stderr); +} + +void logConfigChanged() +{ +} diff --git a/main.c b/main.c index ea4c93d3..2ea76aa1 100644 --- a/main.c +++ b/main.c @@ -17,6 +17,7 @@ along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ +#include #include "serval.h" #include "conf.h" diff --git a/mdp_client.c b/mdp_client.c index 394fdf1d..22b13318 100644 --- a/mdp_client.c +++ b/mdp_client.c @@ -17,8 +17,8 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ +#include // for PRIx64 on Android #include -#include "serval.h" #include "conf.h" #include "log.h" #include "str.h" diff --git a/mdp_client.h b/mdp_client.h index c590a420..f0a21992 100644 --- a/mdp_client.h +++ b/mdp_client.h @@ -19,6 +19,10 @@ #ifndef __SERVAL_DNA__MDP_CLIENT_H #define __SERVAL_DNA__MDP_CLIENT_H +#include "constants.h" +#include "serval_types.h" +#include "os.h" +#include "net.h" #include "log.h" // define 3rd party mdp API without any structure padding diff --git a/mdp_filter.c b/mdp_filter.c index ac0c978b..8a9f2c66 100644 --- a/mdp_filter.c +++ b/mdp_filter.c @@ -17,7 +17,9 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ -#include "serval.h" +#include // for PRIx64 on Android +#include "serval_types.h" +#include "instance.h" #include "overlay_address.h" #include "overlay_packet.h" #include "constants.h" diff --git a/meshms.c b/meshms.c index 1e997409..5891bbdb 100644 --- a/meshms.c +++ b/meshms.c @@ -20,7 +20,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. #define __MESHMS_INLINE #include #include "serval.h" -#include "rhizome.h" +#include "rhizome_types.h" #include "meshms.h" #include "log.h" #include "conf.h" diff --git a/meshms.h b/meshms.h index c4d78f09..8e3d9b8c 100644 --- a/meshms.h +++ b/meshms.h @@ -28,7 +28,6 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. # endif #endif -#include "serval.h" #include "rhizome.h" #define MESHMS_MESSAGE_MAX_LEN 4095 diff --git a/monitor-client.c b/monitor-client.c index 76c89478..fd0e4bc3 100644 --- a/monitor-client.c +++ b/monitor-client.c @@ -21,10 +21,6 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. #include #include #include -#ifdef HAVE_STRINGS_H -#include -#endif -#include #include #include diff --git a/msp_client.c b/msp_client.c index 6d06ac66..1f49244c 100644 --- a/msp_client.c +++ b/msp_client.c @@ -19,10 +19,11 @@ #include #include -#include "serval.h" +#include "serval_types.h" #include "conf.h" #include "mdp_client.h" #include "msp_client.h" +#include "mem.h" #include "str.h" #include "dataformats.h" #include "socket.h" diff --git a/msp_client.h b/msp_client.h index 8e83eba2..f160c046 100644 --- a/msp_client.h +++ b/msp_client.h @@ -20,7 +20,7 @@ #ifndef __SERVAL_DNA__MSP_CLIENT_H #define __SERVAL_DNA__MSP_CLIENT_H -#include "constants.h" +#include "constants.h" // for MDP_MTU #ifndef __MSP_CLIENT_INLINE # if __GNUC__ && !__GNUC_STDC_INLINE__ diff --git a/msp_proxy.c b/msp_proxy.c index 3f33ff87..adfb131c 100644 --- a/msp_proxy.c +++ b/msp_proxy.c @@ -17,13 +17,18 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ -#include "serval.h" +#include +#include "serval_types.h" +#include "mdp_client.h" +#include "msp_client.h" +#include "fdqueue.h" +#include "cli.h" +#include "log.h" +#include "mem.h" #include "str.h" #include "strbuf.h" #include "strbuf_helpers.h" #include "dataformats.h" -#include "mdp_client.h" -#include "msp_client.h" #include "socket.h" struct buffer{ diff --git a/net.c b/net.c index c1c6f5a6..67a3e003 100644 --- a/net.c +++ b/net.c @@ -24,7 +24,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. #include #include -#include "serval.h" +#include "serval_types.h" #include "conf.h" #include "net.h" #include "socket.h" diff --git a/net.h b/net.h index baeadf31..6c049c7d 100644 --- a/net.h +++ b/net.h @@ -24,7 +24,18 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. #ifdef HAVE_NETINET_IN_H #include // for struct in_addr #endif + +#if !defined(FORASTERISK) && !defined(s_addr) +#ifdef HAVE_ARPA_INET_H #include // for in_addr_t +#else +typedef uint32_t in_addr_t; +struct in_addr { + in_addr_t s_addr; +}; +#endif +#endif + #include "log.h" // for __WHENCE__ and struct __sourceloc /* Build a struct in_addr from a host-byte-order integer. diff --git a/nonce.c b/nonce.c index 855cfcdb..13130cf8 100644 --- a/nonce.c +++ b/nonce.c @@ -17,7 +17,9 @@ along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ -#include "serval.h" +#include "constants.h" +#include "os.h" +#include "cli.h" int nonce_initialised=0; unsigned char nonce_buffer[128]; diff --git a/os.h b/os.h index c6f8c933..d20dfb35 100644 --- a/os.h +++ b/os.h @@ -20,11 +20,15 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. #ifndef __SERVAL_DNA__OS_H #define __SERVAL_DNA__OS_H -#include +#include // for off64_t +#include // for NULL #include -#include -#include -#include +#include // for int64_t +#include // for lseek() +#ifdef HAVE_STRINGS_H +#include // for bcopy() +#endif +#include // for memcmp() #include "log.h" #ifndef __SERVAL_DNA__OS_INLINE diff --git a/overlay_address.h b/overlay_address.h index bb9c73c5..38a5586d 100644 --- a/overlay_address.h +++ b/overlay_address.h @@ -21,7 +21,9 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. #define __SERVAL_DNA__OVERLAY_ADDRESS_H #include "constants.h" +#include "os.h" // for time_ms_t #include "socket.h" +#include "overlay_buffer.h" // for struct overlay_buffer // not reachable #define REACHABLE_NONE 0 diff --git a/overlay_packet.h b/overlay_packet.h index e15c983c..419be054 100644 --- a/overlay_packet.h +++ b/overlay_packet.h @@ -21,8 +21,7 @@ #define __SERVAL_DNA__OVERLAY_PACKET_H #include "overlay_address.h" -#include "serval.h" -#include "conf.h" +#include "serval_types.h" #define FRAME_NOT_SENT -1 #define FRAME_DONT_SEND -2 diff --git a/performance_timing.c b/performance_timing.c index 4695eb4e..e82e4638 100644 --- a/performance_timing.c +++ b/performance_timing.c @@ -46,6 +46,7 @@ POSSIBILITY OF SUCH DAMAGE. */ +#include // for PRIu64 on Android #include "fdqueue.h" #include "conf.h" diff --git a/rhizome.h b/rhizome.h index 86d76ba3..fdcf89e4 100644 --- a/rhizome.h +++ b/rhizome.h @@ -22,12 +22,15 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. #define __SERVAL_DNA__RHIZOME_H #include -#include -#include "sha2.h" +#include "serval_types.h" +#include "rhizome_types.h" +#include "overlay_address.h" +#include "overlay_packet.h" +#include "fdqueue.h" +#include "os.h" #include "uuid.h" #include "str.h" #include "strbuf.h" -#include "nacl.h" #ifndef __RHIZOME_INLINE # if __GNUC__ && !__GNUC_STDC_INLINE__ @@ -37,80 +40,9 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. # endif #endif -#define RHIZOME_BUNDLE_ID_BYTES crypto_sign_edwards25519sha512batch_PUBLICKEYBYTES -#define RHIZOME_BUNDLE_ID_STRLEN (RHIZOME_BUNDLE_ID_BYTES * 2) -#define RHIZOME_BUNDLE_KEY_BYTES (crypto_sign_edwards25519sha512batch_SECRETKEYBYTES - crypto_sign_edwards25519sha512batch_PUBLICKEYBYTES) -#define RHIZOME_BUNDLE_KEY_STRLEN (RHIZOME_BUNDLE_KEY_BYTES * 2) -#define RHIZOME_FILEHASH_BYTES SHA512_DIGEST_LENGTH -#define RHIZOME_FILEHASH_STRLEN (RHIZOME_FILEHASH_BYTES * 2) - -#define RHIZOME_CRYPT_KEY_BYTES crypto_stream_xsalsa20_ref_KEYBYTES -#define RHIZOME_CRYPT_KEY_STRLEN (RHIZOME_CRYPT_KEY_BYTES * 2) - -// TODO Rename MANIFEST_ID to BUNDLE_ID -// The following constants are deprecated, use the BUNDLE_ID forms instead -#define RHIZOME_MANIFEST_ID_BYTES RHIZOME_BUNDLE_ID_BYTES -#define RHIZOME_MANIFEST_ID_STRLEN RHIZOME_BUNDLE_ID_STRLEN - // assumed to always be 2^n #define RHIZOME_CRYPT_PAGE_SIZE 4096 -/* Fundamental data type: Rhizome Bundle ID - * - * @author Andrew Bettison - */ - -typedef struct rhizome_bid_binary { - unsigned char binary[RHIZOME_MANIFEST_ID_BYTES]; -} rhizome_bid_t; - -#define RHIZOME_BID_ZERO ((rhizome_bid_t){{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}}) -#define RHIZOME_BID_MAX ((rhizome_bid_t){{0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff}}) -#define rhizome_bid_t_is_zero(bid) is_all_matching((bid).binary, sizeof (*(rhizome_bid_t*)0).binary, 0) -#define rhizome_bid_t_is_max(bid) is_all_matching((bid).binary, sizeof (*(rhizome_bid_t*)0).binary, 0xff) -#define alloca_tohex_rhizome_bid_t(bid) alloca_tohex((bid).binary, sizeof (*(rhizome_bid_t*)0).binary) -int cmp_rhizome_bid_t(const rhizome_bid_t *a, const rhizome_bid_t *b); -int str_to_rhizome_bid_t(rhizome_bid_t *bid, const char *hex); -int strn_to_rhizome_bid_t(rhizome_bid_t *bid, const char *hex, const char **endp); - -/* Fundamental data type: Rhizome File Hash - * - * @author Andrew Bettison - */ - -typedef struct rhizome_filehash_binary { - unsigned char binary[RHIZOME_FILEHASH_BYTES]; -} rhizome_filehash_t; - -#define RHIZOME_FILEHASH_NONE ((rhizome_filehash_t){{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}}) -#define rhizome_filehash_t_is_zero(fh) is_all_matching((fh).binary, sizeof (*(rhizome_filehash_t*)0).binary, 0) -#define rhizome_filehash_t_is_max(fh) is_all_matching((fh).binary, sizeof (*(rhizome_filehash_t*)0).binary, 0xff) -#define alloca_tohex_rhizome_filehash_t(fh) alloca_tohex((fh).binary, sizeof (*(rhizome_filehash_t*)0).binary) -int cmp_rhizome_filehash_t(const rhizome_filehash_t *a, const rhizome_filehash_t *b); -int str_to_rhizome_filehash_t(rhizome_filehash_t *fh, const char *hex); -int strn_to_rhizome_filehash_t(rhizome_filehash_t *fh, const char *hex, const char **endp); - -/* Fundamental data type: Rhizome Bundle Key - * - * @author Andrew Bettison - */ - -typedef struct rhizome_bk_binary { - unsigned char binary[RHIZOME_BUNDLE_KEY_BYTES]; -} rhizome_bk_t; - -#define RHIZOME_BK_NONE ((rhizome_bk_t){{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}}) - -__RHIZOME_INLINE int rhizome_is_bk_none(const rhizome_bk_t *bk) { - return is_all_matching(bk->binary, sizeof bk->binary, 0); -} - -#define alloca_tohex_rhizome_bk_t(bk) alloca_tohex((bk).binary, sizeof (*(rhizome_bk_t*)0).binary) -int cmp_rhizome_bk_t(const rhizome_bk_t *a, const rhizome_bk_t *b); -int str_to_rhizome_bk_t(rhizome_bk_t *bk, const char *hex); -int strn_to_rhizome_bk_t(rhizome_bk_t *bk, const char *hex, const char **endp); - - extern time_ms_t rhizome_voice_timeout; #define RHIZOME_PRIORITY_HIGHEST RHIZOME_PRIORITY_SERVAL_CORE @@ -141,8 +73,6 @@ typedef struct rhizome_signature { #define MAX_MANIFEST_VARS 256 #define MAX_MANIFEST_BYTES 8192 -#define RHIZOME_SIZE_UNSET UINT64_MAX - typedef struct rhizome_manifest { int manifest_record_number; diff --git a/rhizome_types.h b/rhizome_types.h new file mode 100644 index 00000000..8a8c77c2 --- /dev/null +++ b/rhizome_types.h @@ -0,0 +1,114 @@ +/* +Serval Rhizome foundation types +Copyright (C) 2012-2014 Serval Project Inc. + +This program is free software; you can redistribute it and/or +modify it under the terms of the GNU General Public License +as published by the Free Software Foundation; either version 2 +of the License, or (at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +*/ + +#ifndef __SERVAL_DNA__RHIZOME_TYPES_H +#define __SERVAL_DNA__RHIZOME_TYPES_H + +#include +#include +#include +#include "nacl.h" +#include "sha2.h" +#include "str.h" + +#ifndef __RHIZOME_TYPES_INLINE +# if __GNUC__ && !__GNUC_STDC_INLINE__ +# define __RHIZOME_TYPES_INLINE extern inline +# else +# define __RHIZOME_TYPES_INLINE inline +# endif +#endif + +#define RHIZOME_BUNDLE_ID_BYTES crypto_sign_edwards25519sha512batch_PUBLICKEYBYTES +#define RHIZOME_BUNDLE_ID_STRLEN (RHIZOME_BUNDLE_ID_BYTES * 2) +#define RHIZOME_BUNDLE_KEY_BYTES (crypto_sign_edwards25519sha512batch_SECRETKEYBYTES - crypto_sign_edwards25519sha512batch_PUBLICKEYBYTES) +#define RHIZOME_BUNDLE_KEY_STRLEN (RHIZOME_BUNDLE_KEY_BYTES * 2) +#define RHIZOME_FILEHASH_BYTES SHA512_DIGEST_LENGTH +#define RHIZOME_FILEHASH_STRLEN (RHIZOME_FILEHASH_BYTES * 2) +#define RHIZOME_CRYPT_KEY_BYTES crypto_stream_xsalsa20_ref_KEYBYTES +#define RHIZOME_CRYPT_KEY_STRLEN (RHIZOME_CRYPT_KEY_BYTES * 2) + +// TODO Rename MANIFEST_ID to BUNDLE_ID +// The following constants are deprecated, use the BUNDLE_ID forms instead +#define RHIZOME_MANIFEST_ID_BYTES RHIZOME_BUNDLE_ID_BYTES +#define RHIZOME_MANIFEST_ID_STRLEN RHIZOME_BUNDLE_ID_STRLEN + +/* Fundamental data type: Rhizome Bundle ID + * + * @author Andrew Bettison + */ + +typedef struct rhizome_bid_binary { + unsigned char binary[RHIZOME_MANIFEST_ID_BYTES]; +} rhizome_bid_t; + +#define RHIZOME_BID_ZERO ((rhizome_bid_t){{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}}) +#define RHIZOME_BID_MAX ((rhizome_bid_t){{0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff}}) +#define rhizome_bid_t_is_zero(bid) is_all_matching((bid).binary, sizeof (*(rhizome_bid_t*)0).binary, 0) +#define rhizome_bid_t_is_max(bid) is_all_matching((bid).binary, sizeof (*(rhizome_bid_t*)0).binary, 0xff) +#define alloca_tohex_rhizome_bid_t(bid) alloca_tohex((bid).binary, sizeof (*(rhizome_bid_t*)0).binary) +int cmp_rhizome_bid_t(const rhizome_bid_t *a, const rhizome_bid_t *b); +int str_to_rhizome_bid_t(rhizome_bid_t *bid, const char *hex); +int strn_to_rhizome_bid_t(rhizome_bid_t *bid, const char *hex, const char **endp); + +/* Fundamental data type: Rhizome File Hash + * + * @author Andrew Bettison + */ + +typedef struct rhizome_filehash_binary { + unsigned char binary[RHIZOME_FILEHASH_BYTES]; +} rhizome_filehash_t; + +#define RHIZOME_FILEHASH_NONE ((rhizome_filehash_t){{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}}) +#define rhizome_filehash_t_is_zero(fh) is_all_matching((fh).binary, sizeof (*(rhizome_filehash_t*)0).binary, 0) +#define rhizome_filehash_t_is_max(fh) is_all_matching((fh).binary, sizeof (*(rhizome_filehash_t*)0).binary, 0xff) +#define alloca_tohex_rhizome_filehash_t(fh) alloca_tohex((fh).binary, sizeof (*(rhizome_filehash_t*)0).binary) +int cmp_rhizome_filehash_t(const rhizome_filehash_t *a, const rhizome_filehash_t *b); +int str_to_rhizome_filehash_t(rhizome_filehash_t *fh, const char *hex); +int strn_to_rhizome_filehash_t(rhizome_filehash_t *fh, const char *hex, const char **endp); + +/* Fundamental data type: Rhizome Bundle Key + * + * @author Andrew Bettison + */ + +typedef struct rhizome_bk_binary { + unsigned char binary[RHIZOME_BUNDLE_KEY_BYTES]; +} rhizome_bk_t; + +#define RHIZOME_BK_NONE ((rhizome_bk_t){{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}}) + +__RHIZOME_TYPES_INLINE int rhizome_is_bk_none(const rhizome_bk_t *bk) { + return is_all_matching(bk->binary, sizeof bk->binary, 0); +} + +#define alloca_tohex_rhizome_bk_t(bk) alloca_tohex((bk).binary, sizeof (*(rhizome_bk_t*)0).binary) +int cmp_rhizome_bk_t(const rhizome_bk_t *a, const rhizome_bk_t *b); +int str_to_rhizome_bk_t(rhizome_bk_t *bk, const char *hex); +int strn_to_rhizome_bk_t(rhizome_bk_t *bk, const char *hex, const char **endp); + +/* Fundamental data type: Rhizome payload size + * + * @author Andrew Bettison + */ + +#define RHIZOME_SIZE_UNSET UINT64_MAX + +#endif // __SERVAL_DNA__RHIZOME_TYPES_H diff --git a/serval.h b/serval.h index 3c6353c0..175567c8 100644 --- a/serval.h +++ b/serval.h @@ -54,11 +54,6 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. #include #include #include -#ifdef HAVE_STRINGS_H -#include -#endif -#include -#include #include #ifdef WIN32 @@ -98,17 +93,6 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. # endif #endif //!WIN32 -#if !defined(FORASTERISK) && !defined(s_addr) -#ifdef HAVE_ARPA_INET_H -#include -#else -typedef uint32_t in_addr_t; -struct in_addr { - in_addr_t s_addr; -}; -#endif -#endif - #ifdef HAVE_SYS_SOCKET_H #include #endif @@ -133,6 +117,8 @@ struct in_addr { #include #include +#include "serval_types.h" +#include "sighandlers.h" #include "instance.h" #include "fdqueue.h" #include "cli.h" @@ -143,10 +129,6 @@ struct in_addr { #include "net.h" #include "os.h" -/* UDP Port numbers for various Serval services. - The overlay mesh works over DNA */ -#define PORT_DNA 4110 - #define BATCH 1 #define NONBATCH 0 @@ -171,31 +153,6 @@ struct in_addr { extern const char version_servald[]; extern const char copyright_servald[]; -/* Fundamental types. - */ - -typedef struct sid_binary { - unsigned char binary[SID_SIZE]; -} sid_t; - -#define SID_ANY ((sid_t){{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}}) -#define SID_BROADCAST ((sid_t){{0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff}}) - -// is the SID entirely 0xFF? -#define is_sid_t_broadcast(SID) is_all_matching((SID).binary, sizeof (*(sid_t*)0).binary, 0xFF) - -// is the SID entirely 0x00? -#define is_sid_t_any(SID) is_all_matching((SID).binary, sizeof (*(sid_t*)0).binary, 0) - -#define alloca_tohex_sid_t(sid) alloca_tohex((sid).binary, sizeof (*(sid_t*)0).binary) -#define alloca_tohex_sid_t_trunc(sid,strlen) tohex((char *)alloca((strlen)+1), (strlen), (sid).binary) - -int cmp_sid_t(const sid_t *a, const sid_t *b); -int str_to_sid_t(sid_t *sid, const char *hex); -int strn_to_sid_t(sid_t *sid, const char *hex, size_t hexlen, const char **endp); - -#define alloca_tohex_sas(sas) alloca_tohex((sas), SAS_SIZE) - struct cli_parsed; extern int servalShutdown; @@ -277,10 +234,6 @@ int rhizome_opendb(); int parseCommandLine(struct cli_context *context, const char *argv0, int argc, const char *const *argv); -typedef uint32_t mdp_port_t; -#define PRImdp_port_t "#010" PRIx32 - - /* Server-side MDP functions */ void mdp_init_response(const struct internal_mdp_header *in, struct internal_mdp_header *out); void overlay_mdp_encode_ports(struct overlay_buffer *plaintext, mdp_port_t dst_port, mdp_port_t src_port); @@ -344,12 +297,6 @@ int dna_helper_start(); int dna_helper_shutdown(); int dna_helper_enqueue(struct subscriber *source, mdp_port_t source_port, const char *did); int parseDnaReply(const char *buf, size_t len, char *token, char *did, char *name, char *uri, const char **bufp); -extern int sigPipeFlag; -extern int sigIoFlag; -extern int sigIntFlag; -void sigPipeHandler(int signal); -void sigIoHandler(int signal); -void sigIntHandler(int signal); int overlay_mdp_setup_sockets(); diff --git a/serval_packetvisualise.c b/serval_packetvisualise.c index b732675d..f4d83296 100644 --- a/serval_packetvisualise.c +++ b/serval_packetvisualise.c @@ -49,11 +49,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include -#include -#include #include -#include -#include #include "xprintf.h" #define MAX_SPACES 120 diff --git a/serval_types.h b/serval_types.h new file mode 100644 index 00000000..7eb7ee93 --- /dev/null +++ b/serval_types.h @@ -0,0 +1,73 @@ +/* +Serval DNA foundation types +Copyright (C) 2012-2014 Serval Project Inc. + +This program is free software; you can redistribute it and/or +modify it under the terms of the GNU General Public License +as published by the Free Software Foundation; either version 2 +of the License, or (at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +*/ + +#ifndef __SERVAL_DNA__SERVAL_TYPES_H +#define __SERVAL_DNA__SERVAL_TYPES_H + +#include +#include + +/* Conveniences to assist readability + */ + +typedef char bool_t; + +/* Serval ID (aka Subscriber ID) + */ + +#define SID_SIZE 32 // == crypto_sign_edwards25519sha512batch_PUBLICKEYBYTES +#define SAS_SIZE 32 // == crypto_sign_edwards25519sha512batch_PUBLICKEYBYTES + +#define SID_STRLEN (SID_SIZE*2) + +typedef struct sid_binary { + unsigned char binary[SID_SIZE]; +} sid_t; + +#define SID_ANY ((sid_t){{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}}) +#define SID_BROADCAST ((sid_t){{0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff}}) + +// is the SID entirely 0xFF? +#define is_sid_t_broadcast(SID) is_all_matching((SID).binary, sizeof (*(sid_t*)0).binary, 0xFF) + +// is the SID entirely 0x00? +#define is_sid_t_any(SID) is_all_matching((SID).binary, sizeof (*(sid_t*)0).binary, 0) + +#define alloca_tohex_sid_t(sid) alloca_tohex((sid).binary, sizeof (*(sid_t*)0).binary) +#define alloca_tohex_sid_t_trunc(sid,strlen) tohex((char *)alloca((strlen)+1), (strlen), (sid).binary) + +int cmp_sid_t(const sid_t *a, const sid_t *b); +int str_to_sid_t(sid_t *sid, const char *hex); +int strn_to_sid_t(sid_t *sid, const char *hex, size_t hexlen, const char **endp); + +#define alloca_tohex_sas(sas) alloca_tohex((sas), SAS_SIZE) + +/* MDP port number + */ + +typedef uint32_t mdp_port_t; +#define PRImdp_port_t "#010" PRIx32 + +/* DID (phone number) + */ + +#define DID_MINSIZE 5 +#define DID_MAXSIZE 32 + +#endif // __SERVAL_DNA__SERVAL_TYPES_H diff --git a/sighandlers.c b/sighandlers.c index c852631a..cc5d3665 100644 --- a/sighandlers.c +++ b/sighandlers.c @@ -1,5 +1,6 @@ /* -Serval Distributed Numbering Architecture (DNA) +Serval DNA signal handlers +Copyright (C) 2014 Serval Project Inc. Copyright (C) 2012 Paul Gardner-Stephen This program is free software; you can redistribute it and/or @@ -18,6 +19,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include "constants.h" +#include "sighandlers.h" int sigPipeFlag=0; int sigIoFlag=0; @@ -38,4 +40,4 @@ void sigIoHandler(int UNUSED(signal)) void sigIntHandler(int UNUSED(signal)) { sigIntFlag++; -} \ No newline at end of file +} diff --git a/sighandlers.h b/sighandlers.h new file mode 100644 index 00000000..854c4071 --- /dev/null +++ b/sighandlers.h @@ -0,0 +1,31 @@ +/* +Serval DNA signal handlers +Copyright (C) 2014 Serval Project Inc. + +This program is free software; you can redistribute it and/or +modify it under the terms of the GNU General Public License +as published by the Free Software Foundation; either version 2 +of the License, or (at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +*/ + +#ifndef __SERVAL_DNA__SIGHANDLERS_H +#define __SERVAL_DNA__SIGHANDLERS_H + +extern int sigPipeFlag; +extern int sigIoFlag; +extern int sigIntFlag; + +void sigPipeHandler(int signal); +void sigIoHandler(int signal); +void sigIntHandler(int signal); + +#endif // __SERVAL_DNA__SIGHANDLERS_H diff --git a/simulator.c b/simulator.c index 9bf60718..ca239700 100644 --- a/simulator.c +++ b/simulator.c @@ -22,6 +22,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. #include #include +#include #include "mem.h" #include "socket.h" @@ -108,82 +109,10 @@ struct command_state *stdin_state; struct network *networks=NULL; static void unicast_alarm(struct sched_ent *alarm); -const struct __sourceloc __whence = __NOWHERE__; - -static const char *_trimbuildpath(const char *path) -{ - /* Remove common path prefix */ - int lastsep = 0; - int i; - for (i = 0; __FILE__[i] && path[i]; ++i) { - if (i && path[i - 1] == '/') - lastsep = i; - if (__FILE__[i] != path[i]) - break; - } - return &path[lastsep]; -} - void cf_on_config_change() { } -void logMessage(int level, struct __sourceloc whence, const char *fmt, ...) -{ - const char *levelstr = "UNKWN:"; - switch (level) { - case LOG_LEVEL_FATAL: - levelstr = "FATAL:"; - break; - case LOG_LEVEL_ERROR: - levelstr = "ERROR:"; - break; - case LOG_LEVEL_INFO: - levelstr = "INFO:"; - break; - case LOG_LEVEL_WARN: - levelstr = "WARN:"; - break; - case LOG_LEVEL_DEBUG: - levelstr = "DEBUG:"; - break; - } - - struct timeval tv; - struct tm tm; - gettimeofday(&tv, NULL); - localtime_r(&tv.tv_sec, &tm); - char buf[50]; - strftime(buf, sizeof buf, "%T", &tm); - fprintf(stderr, "%s.%03u ", buf, (unsigned int)tv.tv_usec / 1000); - - fprintf(stderr, "%s ", levelstr); - if (whence.file) { - fprintf(stderr, "%s", _trimbuildpath(whence.file)); - if (whence.line) - fprintf(stderr, ":%u", whence.line); - if (whence.function) - fprintf(stderr, ":%s()", whence.function); - fputc(' ', stderr); - } else if (whence.function) { - fprintf(stderr, "%s() ", whence.function); - } - va_list ap; - va_start(ap, fmt); - vfprintf(stderr, fmt, ap); - va_end(ap); - fputc('\n', stderr); -} - -void logFlush() -{ -} -void logConfigChanged() -{ -} - -int serverMode=0; - static void recv_packet(int fd, struct network *network, struct peer *destination) { struct socket_address addr; diff --git a/socket.c b/socket.c index a7f5d6d8..ba2044c2 100644 --- a/socket.c +++ b/socket.c @@ -22,7 +22,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. #include #include -#include "serval.h" +#include "instance.h" #include "str.h" #include "conf.h" #include "log.h" diff --git a/sourcefiles.mk b/sourcefiles.mk index d104ac02..f50cd757 100644 --- a/sourcefiles.mk +++ b/sourcefiles.mk @@ -1,20 +1,26 @@ # The "client" source files do not depend on "serval.h" or "rhizome.h", ie, # they can be linked into executables other than servald. SERVAL_CLIENT_SOURCES = \ + cli.c \ conf.c \ conf_om.c \ conf_parse.c \ conf_schema.c \ + console.c \ dataformats.c \ + fdqueue.c \ fifo.c \ instance.c \ - log.c \ + limit.c \ + logMessage.c \ log_util.c \ mem.c \ net.c \ os.c \ + performance_timing.c \ randombytes.c \ rotbuf.c \ + sighandlers.c \ socket.c \ srandomdev.c \ strbuf.c \ @@ -22,8 +28,14 @@ SERVAL_CLIENT_SOURCES = \ str.c \ strlcpy.c \ uuid.c \ + whence.c \ xprintf.c +# These objects do not belong in the Serval DNA daemon but are available for +# client applications. +SERVAL_LIB_SOURCES = \ + log_stderr.c + # These source files are imported and do not depend on any local header files. # They also take a long time to compile, so their dependencies should be as # narrow as possible to avoid unnecessary recompilations when developers modify @@ -33,26 +45,21 @@ SQLITE3_SOURCES = \ # The source files for building the Serval DNA daemon. SERVAL_DAEMON_SOURCES = \ - cli.c \ commandline.c \ - console.c \ crypto.c \ directory_client.c \ dna_helper.c \ encode.c \ - fdqueue.c \ golay.c \ httpd.c \ http_server.c \ keyring.c \ + log.c \ lsif.c \ - limit.c \ main.c \ radio_link.c \ meshms.c \ meshms_restful.c \ - mdp_client.c \ - mdp_net.c \ msp_client.c \ msp_proxy.c \ monitor.c \ @@ -72,7 +79,6 @@ SERVAL_DAEMON_SOURCES = \ overlay_olsr.c \ overlay_packetformats.c \ overlay_payload.c \ - performance_timing.c \ route_link.c \ rhizome.c \ rhizome_bundle.c \ @@ -89,17 +95,20 @@ SERVAL_DAEMON_SOURCES = \ serval_packetvisualise.c \ server.c \ sha2.c \ - sighandlers.c \ vomp.c \ vomp_console.c \ fec-3.0.1/ccsds_tables.c \ fec-3.0.1/decode_rs_8.c \ fec-3.0.1/encode_rs_8.c \ fec-3.0.1/init_rs_char.c \ - context1.c + context1.c -SIMULATOR_SOURCES = cli.c conf.c conf_om.c conf_parse.c conf_schema.c \ - console.c simulator.c socket.c fdqueue.c performance_timing.c \ - str.c os.c mem.c net.c log_util.c strbuf.c strbuf_helpers.c \ - dataformats.c xprintf.c instance.c limit.c version.c +MDP_CLIENT_SOURCES = \ + mdp_client.c \ + mdp_net.c +SIMULATOR_SOURCES = \ + simulator.c + +MONITOR_CLIENT_SRCS = \ + monitor-client.c diff --git a/whence.c b/whence.c new file mode 100644 index 00000000..bec7b442 --- /dev/null +++ b/whence.c @@ -0,0 +1,21 @@ +/* +Copyright (C) 2014 Serval Project Inc. + +This program is free software; you can redistribute it and/or +modify it under the terms of the GNU General Public License +as published by the Free Software Foundation; either version 2 +of the License, or (at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +*/ + +#include "whence.h" + +const struct __sourceloc __whence = __NOWHERE__; diff --git a/whence.h b/whence.h new file mode 100644 index 00000000..07b271f0 --- /dev/null +++ b/whence.h @@ -0,0 +1,83 @@ +/* +Copyright (C) 2012 Serval Project Inc. + +This program is free software; you can redistribute it and/or +modify it under the terms of the GNU General Public License +as published by the Free Software Foundation; either version 2 +of the License, or (at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +*/ + +#ifndef __SERVAL_DNA__WHENCE_H +#define __SERVAL_DNA__WHENCE_H + +#include // for NULL + +/* + * Every log message identifies the location in the source code at which the + * message was produced. This location is represented by a struct __sourceloc, + * which is passed by value to the logMessage() function and its ilk. + * + * A struct __sourceloc value is generated by the __HERE__ macro, which uses + * the cpp(1) built-in macros __FILE__, __LINE__ and __FUNCTION__ to generate + * its elements. The __NOWHERE__ macro creates a struct __sourceloc with NULL + * and zero fields. If you pass __NOWHERE__ to logMessage(), it will omit + * location information from the log line. The __NOWHENCE__ macro creates a + * special source __sourceloc that logging primitives should interpret to + * suppress the output of the usual source-code location information. + * + * Sometimes, a function wants to log a message as though its caller were the + * origin of the message. This is typical of "primitive" type functions that + * are used in many places throughout the code, and whose internal workings are + * generally well-debugged and of little interest for ongoing development. In + * this case, the code pattern is to declare the underscore-prefixed function + * as taking a struct __sourceloc argument, and a macro that invokes the + * function, passing the __HERE__ macro for that argument: + * + * int _primitive(struct __sourceloc __whence, int arg1, const char *arg2); + * + * #define primitive(arg1, arg2) _primitive(__HERE__, (arg1), (arg2)) + * + * Within the _primitive() function, the standard logging macros defined below + * (WHYF(), WARNF(), INFOF(), DEBUGF() etc.) will use the __whence argument + * instead of __HERE__ when logging their message. This is achieved using a + * dirty trick: in the function *definition*, the __sourceloc argument MUST be + * named '__whence'. The trick is that there is a global variable called + * '__whence' which always contains the value of __NOWHERE__. If that variable + * is lexically obscured by a local variable or parameter called '__whence', + * then the DEBUG macros will use __whence, otherwise they will use __HERE__. + * This logic is encapsulated in the __WHENCE__ macro, to make it available to + * for other purposes. For example, a better definition of the primitive() + * macro above would be: + * + * #define primitive(arg1, arg2) _primitive(__WHENCE__, (arg1), (arg2)) + * + * Then, if it were invoked from within another primitive-type function, it + * would log messages with the __sourceloc of that primitive's caller, which is + * probably the most useful for diagnosis. + * + * @author Andrew Bettison + */ + +struct __sourceloc { + const char *file; + unsigned int line; + const char *function; +}; + +extern const struct __sourceloc __whence; // see above + +#define __HERE__ ((struct __sourceloc){ .file = __FILE__, .line = __LINE__, .function = __FUNCTION__ }) +#define __NOWHERE__ ((struct __sourceloc){ .file = NULL, .line = 0, .function = NULL }) +#define __NOWHENCE__ ((struct __sourceloc){ .file = "", .line = 0, .function = NULL }) +#define __WHENCE__ (__whence.file ? __whence : __HERE__) + +#endif // __SERVAL_DNA__WHENCE_H