diff --git a/Makefile b/Makefile index 4054440..a3354ec 100644 --- a/Makefile +++ b/Makefile @@ -1,11 +1,14 @@ all: $(MAKE) -C src all + $(MAKE) -C bin all $(MAKE) -C examples all install: $(MAKE) -C src install + $(MAKE) -C bin install $(MAKE) -C examples install clean: $(MAKE) -C src clean + $(MAKE) -C bin clean $(MAKE) -C examples clean diff --git a/bin/Makefile b/bin/Makefile new file mode 100644 index 0000000..46e8cf7 --- /dev/null +++ b/bin/Makefile @@ -0,0 +1,28 @@ +include ../mk/build.mk + +CC = $(CROSS)cc + +INCLUDE_PATH = ../include + +CFLAGS += -I$(INCLUDE_PATH) +LDFLAGS = -L../src -lhexagram +STATIC = ../src/libhexagram.a +OBJS = capture.o convert.o replay.o replaypcap.o main.o +NAME = hexagram + +RM = /bin/rm + +all: $(NAME) + +install: $(NAME) + $(INSTALL) -d 0755 $(PREFIX)/bin + $(INSTALL) -c -m 0755 $(NAME) $(PREFIX)/bin + +$(OBJS): %.o: %.c + $(CC) $(CFLAGS) -c $< + +$(NAME): $(OBJS) $(STATIC) + $(CC) $(CFLAGS) -o $(NAME) $(OBJS) $(STATIC) + +clean: + $(RM) -f $(NAME) $(OBJS) diff --git a/bin/app.h b/bin/app.h new file mode 100644 index 0000000..42e80a8 --- /dev/null +++ b/bin/app.h @@ -0,0 +1,10 @@ +#ifndef _APP_H +#define _APP_H + +typedef struct { + const char *name; + const char *arglist; + int (*main)(int argc, char **argv); +} hexagram_app; + +#endif /* _APP_H *? diff --git a/examples/capture.c b/bin/capture.c similarity index 83% rename from examples/capture.c rename to bin/capture.c index d5a74f3..dc4c712 100644 --- a/examples/capture.c +++ b/bin/capture.c @@ -8,7 +8,13 @@ #include #include -static void usage(int argc, char **argv, const char *message, ...) { +#include "capture.h" + +char *hexagram_arglist_capture(void) { + return "canif [file.can]"; +} + +static int usage(int argc, char **argv, const char *message, ...) { if (message) { va_list args; @@ -17,14 +23,12 @@ static void usage(int argc, char **argv, const char *message, ...) { va_end(args); } - fprintf(stderr, "usage: %s canif [file.can]\n", argv[0]); + fprintf(stderr, "usage: hexagram %s %s\n", argv[0], hexagram_arglist_capture()); exit(1); } - - -int main(int argc, char **argv) { +int hexagram_main_capture(int argc, char **argv) { hexagram_can_if *can_if; hexagram_capture *capture; diff --git a/bin/capture.h b/bin/capture.h new file mode 100644 index 0000000..1cd2ca4 --- /dev/null +++ b/bin/capture.h @@ -0,0 +1,8 @@ +#ifndef _CAPTURE_H +#define _CAPTURE_H + +char *hexagram_arglist_capture(void); + +int hexagram_main_capture(int argc, char **argv); + +#endif /* _CAPTURE_H */ diff --git a/examples/convert.c b/bin/convert.c similarity index 97% rename from examples/convert.c rename to bin/convert.c index 0b4335c..9720a63 100644 --- a/examples/convert.c +++ b/bin/convert.c @@ -5,11 +5,15 @@ #include #include #include -#include +#include #include #include +char *hexagram_arglist_convert(void) { + return "[infile] [outfile]"; +} + static ssize_t handle_option(hexagram_pcapng_stream *stream, uint32_t type, uint16_t code, @@ -182,7 +186,7 @@ error_io: return -1; } -int main(int argc, char **argv) { +int hexagram_main_convert(int argc, char **argv) { int fd; hexagram_pcapng_stream *stream; diff --git a/bin/convert.h b/bin/convert.h new file mode 100644 index 0000000..e3f946f --- /dev/null +++ b/bin/convert.h @@ -0,0 +1,8 @@ +#ifndef _CONVERT_H +#define _CONVERT_H + +char *hexagram_arglist_convert(void); + +int hexagram_main_convert(int argc, char **argv); + +#endif /* _CONVERT_H */ diff --git a/bin/main.c b/bin/main.c new file mode 100644 index 0000000..f8915c0 --- /dev/null +++ b/bin/main.c @@ -0,0 +1,59 @@ +#include +#include +#include +#include + +#include "capture.h" +#include "convert.h" +#include "replay.h" +#include "replaypcap.h" + +typedef struct { + const char *name; + char *(*arglist)(void); + int (*main)(int argc, char **argv); +} hexagram_app; + +hexagram_app apps[] = { + { "capture", hexagram_arglist_capture, hexagram_main_capture }, + { "convert", hexagram_arglist_convert, hexagram_main_convert }, + { "replay", hexagram_arglist_replay, hexagram_main_replay }, + { "replaypcap", hexagram_arglist_replaypcap, hexagram_main_replaypcap }, + { NULL, NULL, NULL } +}; + +static int usage(int argc, char **argv, const char *message, ...) { + hexagram_app *app; + + if (message) { + va_list args; + + va_start(args, message); + vfprintf(stderr, message, args); + va_end(args); + } + + fprintf(stderr, "usage: %s [command args ...]\n", argv[0]); + + for (app = apps; app->name != NULL; app++) { + fprintf(stderr, " %s %s %s\n", argv[0], app->name, app->arglist()); + } + + exit(1); +} + +int main(int argc, char **argv) { + hexagram_app *app; + + if (argc < 2) { + return usage(argc, argv, "No command provided\n"); + } + + for (app = apps; app->name != NULL; app++) { + if (strcmp(argv[1], app->name) == 0) { + return app->main(--argc, ++argv); + } + } + + return usage(argc, argv, NULL); +} diff --git a/examples/replay.c b/bin/replay.c similarity index 87% rename from examples/replay.c rename to bin/replay.c index 47d7e65..284fe9b 100644 --- a/examples/replay.c +++ b/bin/replay.c @@ -8,6 +8,12 @@ #include #include +#include "replay.h" + +char *hexagram_arglist_replay(void) { + return "canif [file.can]"; +} + static void usage(int argc, char **argv, const char *message, ...) { if (message) { va_list args; @@ -17,12 +23,12 @@ static void usage(int argc, char **argv, const char *message, ...) { va_end(args); } - fprintf(stderr, "usage: %s canif [file.can]\n", argv[0]); + fprintf(stderr, "usage: hexagram %s %s\n", argv[0], hexagram_arglist_replay()); exit(1); } -int main(int argc, char **argv) { +int hexagram_main_replay(int argc, char **argv) { hexagram_can_if *can_if; hexagram_capture *capture; diff --git a/bin/replay.h b/bin/replay.h new file mode 100644 index 0000000..88537a7 --- /dev/null +++ b/bin/replay.h @@ -0,0 +1,8 @@ +#ifndef _REPLAY_H +#define _REPLAY_H + +char *hexagram_arglist_replay(void); + +int hexagram_main_replay(int argc, char **argv); + +#endif /* _REPLAY_H */ diff --git a/examples/pcapread.c b/bin/replaypcap.c similarity index 96% rename from examples/pcapread.c rename to bin/replaypcap.c index 90a1e8b..f68a420 100644 --- a/examples/pcapread.c +++ b/bin/replaypcap.c @@ -5,8 +5,8 @@ #include #include #include +#include #include -#include #include #include @@ -16,11 +16,17 @@ #include +#include "replaypcap.h" + struct pcapinfo { int sock; useconds_t last_time; }; +char *hexagram_arglist_replaypcap(void) { + return "canif [file.pcapng]"; +} + static void usage(int argc, char **argv, const char *message, ...) { if (message) { va_list args; @@ -30,7 +36,8 @@ static void usage(int argc, char **argv, const char *message, ...) { va_end(args); } - fprintf(stderr, "usage: %s canif [file.pcapng]\n", argv[0]); + fprintf(stderr, "usage: hexagram %s %s\n", + argv[0], hexagram_arglist_replaypcap()); exit(1); } @@ -220,7 +227,7 @@ error_io: return -1; } -int main(int argc, char **argv) { +int hexagram_main_replaypcap(int argc, char **argv) { int fd; struct sockaddr_can addr; diff --git a/bin/replaypcap.h b/bin/replaypcap.h new file mode 100644 index 0000000..8f34b70 --- /dev/null +++ b/bin/replaypcap.h @@ -0,0 +1,8 @@ +#ifndef _REPLAYPCAP_H +#define _REPLAYPCAP_H + +char *hexagram_arglist_replaypcap(void); + +int hexagram_main_replaypcap(int argc, char **argv); + +#endif /* _REPLAYPCAP_H */ diff --git a/examples/Makefile b/examples/Makefile index d9c76ea..e2dd50a 100644 --- a/examples/Makefile +++ b/examples/Makefile @@ -8,7 +8,7 @@ CFLAGS += -I$(INCLUDE_PATH) LDFLAGS = -L../src -lhexagram STATIC = ../src/libhexagram.a -EXAMPLES = capture convert duration pcapread replay view +EXAMPLES = duration view RM = /bin/rm