diff --git a/src/.output/xdp_filter.o b/src/.output/xdp_filter.o index 56f4a00..c449e6e 100644 Binary files a/src/.output/xdp_filter.o and b/src/.output/xdp_filter.o differ diff --git a/src/Makefile b/src/Makefile index e82d949..63970b0 100644 --- a/src/Makefile +++ b/src/Makefile @@ -5,6 +5,10 @@ LLVM_STRIP ?= llvm-strip BPFTOOL ?= $(abspath ./tools/bpftool) LIBBPF_SRC := $(abspath ./libbpf/src) LIBBPF_OBJ := $(abspath $(OUTPUT)/libbpf.a) +USER_INCLUDES_DIR := $(abspath ./user/include/utils) +USER_INCLUDES_HDR := $(wildcard $(USER_INCLUDES_DIR)/**/*.h) +USER_INCLUDES_SRC := $(wildcard $(USER_INCLUDES_DIR)/**/*.c) +USER_INCLUDES_OBJ := $(USER_INCLUDES_SRC:.c=.o) VMLINUX := ./vmlinux/newvmlinux.h USER := user EBPF := ebpf @@ -76,14 +80,20 @@ $(OUTPUT)/%.skel.h: $(OUTPUT)/%.bpf.o | $(OUTPUT) $(Q)$(BPFTOOL) gen skeleton $< > $@ # Build user-space code -$(patsubst %,$(OUTPUT)/%.o,$(APPS)): %.o: %.skel.h +$(patsubst %,$(OUTPUT)/%.o, $(APPS)): %.o: %.skel.h -$(OUTPUT)/%.o: $(USER)/%.c $(wildcard $(USER)/%.h) | $(OUTPUT) +#User includes +$(USER_INCLUDES_OBJ): $(wildcard $(USER_INCLUDES_SRC)/**/*.h)| $(OUTPUT) + $(call msg,CC,$@) + $(Q)$(CC) $(CFLAGS) -I$(wildcard $(USER_INCLUDES_SRC)/*.h) -c $(wildcard $(USER_INCLUDES_SRC)/*.c) -o $@ + +#User code +$(OUTPUT)/%.o: $(USER)/%.c $(wildcard $(USER)/%.h)| $(OUTPUT) $(call msg,CC,$@) $(Q)$(CC) $(CFLAGS) $(INCLUDES) $(COMMON_INCLUDES) -c $(filter $(USER)/%.c,$^) -o $@ # Build application binary -$(APPS): %: $(OUTPUT)/%.o $(LIBBPF_OBJ) | $(OUTPUT) +$(APPS): %: $(OUTPUT)/%.o $(LIBBPF_OBJ) $(USER_INCLUDES_SRC)| $(OUTPUT) $(call msg,BINARY,$@) $(Q)$(CC) $(CFLAGS) $^ -lelf -lz -o bin/$@ diff --git a/src/bin/xdp_filter b/src/bin/xdp_filter index 2992b67..a411fac 100755 Binary files a/src/bin/xdp_filter and b/src/bin/xdp_filter differ diff --git a/src/user/include/utils/files/path.c b/src/user/include/utils/files/path.c new file mode 100644 index 0000000..096c9d9 --- /dev/null +++ b/src/user/include/utils/files/path.c @@ -0,0 +1,100 @@ +#define _XOPEN_SOURCE 700 +#define _LARGEFILE64_SOURCE +#define _FILE_OFFSET_BITS 64 +#include +#include +#include +#include +#include +#include +#include +#include "path.h" +#define USE_FDS 15 + +int print_entry(const char *filepath, const struct stat *info, const int typeflag, struct FTW *pathinfo){ + /* const char *const filename = filepath + pathinfo->base; */ + const double bytes = (double)info->st_size; /* Not exact if large! */ + struct tm mtime; + + localtime_r(&(info->st_mtime), &mtime); + + printf("%04d-%02d-%02d %02d:%02d:%02d", + mtime.tm_year+1900, mtime.tm_mon+1, mtime.tm_mday, + mtime.tm_hour, mtime.tm_min, mtime.tm_sec); + + if (bytes >= 1099511627776.0) + printf(" %9.3f TiB", bytes / 1099511627776.0); + else + if (bytes >= 1073741824.0) + printf(" %9.3f GiB", bytes / 1073741824.0); + else + if (bytes >= 1048576.0) + printf(" %9.3f MiB", bytes / 1048576.0); + else + if (bytes >= 1024.0) + printf(" %9.3f KiB", bytes / 1024.0); + else + printf(" %9.0f B ", bytes); + + if (typeflag == FTW_SL) { + char *target; + size_t maxlen = 1023; + ssize_t len; + + while (1) { + + target = malloc(maxlen + 1); + if (target == NULL) + return ENOMEM; + + len = readlink(filepath, target, maxlen); + if (len == (ssize_t)-1) { + const int saved_errno = errno; + free(target); + return saved_errno; + } + if (len >= (ssize_t)maxlen) { + free(target); + maxlen += 1024; + continue; + } + + target[len] = '\0'; + break; + } + + printf(" %s -> %s\n", filepath, target); + free(target); + + } else + if (typeflag == FTW_SLN) + printf(" %s (dangling symlink)\n", filepath); + else + if (typeflag == FTW_F) + printf(" %s\n", filepath); + else + if (typeflag == FTW_D || typeflag == FTW_DP) + printf(" %s/\n", filepath); + else + if (typeflag == FTW_DNR) + printf(" %s/ (unreadable)\n", filepath); + else + printf(" %s (unknown)\n", filepath); + + return 0; +} + + +int print_directory_tree(const char *const dirpath){ + int result; + + /* Invalid directory path? */ + if (dirpath == NULL || *dirpath == '\0') + return errno = EINVAL; + + result = nftw(dirpath, print_entry, USE_FDS, FTW_PHYS); + if (result >= 0) + errno = result; + + return errno; +} \ No newline at end of file diff --git a/src/user/include/utils/files/path.h b/src/user/include/utils/files/path.h new file mode 100644 index 0000000..966fad2 --- /dev/null +++ b/src/user/include/utils/files/path.h @@ -0,0 +1,7 @@ +#ifndef __PATH_H +#define __PATH_H + +int print_directory_tree(const char *const dirpath); + + +#endif \ No newline at end of file diff --git a/src/user/include/utils/files/path.o b/src/user/include/utils/files/path.o new file mode 100644 index 0000000..f214758 Binary files /dev/null and b/src/user/include/utils/files/path.o differ diff --git a/src/user/utils/structures/fdlist.c b/src/user/include/utils/structures/fdlist.c similarity index 92% rename from src/user/utils/structures/fdlist.c rename to src/user/include/utils/structures/fdlist.c index e37afbb..b7e474a 100644 --- a/src/user/utils/structures/fdlist.c +++ b/src/user/include/utils/structures/fdlist.c @@ -10,11 +10,12 @@ * @param size * @return FdList */ -FdList FdList_create(int size){ +FdList* FdList_create(int size){ FdList *fd_list = (FdList*)calloc(1, sizeof(FdList)); fd_list->max_size = size; fd_list->size = 0; fd_list->list = (int*)calloc(size, sizeof(int)); + return fd_list; } /** @@ -28,6 +29,7 @@ int FdList_add(FdList *fd_list, int fd_new){ if(fd_list->size+1 >= fd_list->max_size){ return -1; } + return 0; } /** @@ -51,4 +53,5 @@ int FdList_extend(FdList *fd_list, int new_size){ int FdList_destroy(FdList *fd_list){ free(fd_list->list); free(fd_list); + return 0; } \ No newline at end of file diff --git a/src/user/utils/structures/fdlist.h b/src/user/include/utils/structures/fdlist.h similarity index 88% rename from src/user/utils/structures/fdlist.h rename to src/user/include/utils/structures/fdlist.h index 6db1145..bdecb80 100644 --- a/src/user/utils/structures/fdlist.h +++ b/src/user/include/utils/structures/fdlist.h @@ -8,7 +8,7 @@ typedef struct FdList{ } FdList; -FdList FdList_create(int size); +FdList* FdList_create(int size); int FdList_add(FdList *fd_list, int fd_new); diff --git a/src/user/include/utils/structures/fdlist.o b/src/user/include/utils/structures/fdlist.o new file mode 100644 index 0000000..f214758 Binary files /dev/null and b/src/user/include/utils/structures/fdlist.o differ diff --git a/src/user/xdp_filter.c b/src/user/xdp_filter.c index 3b03ad3..b105e9b 100644 --- a/src/user/xdp_filter.c +++ b/src/user/xdp_filter.c @@ -11,6 +11,7 @@ #include "xdp_filter.skel.h" #include "include/xdp_filter.h" #include "../constants/constants.h" +#include "include/utils/files/path.h" static struct env { bool verbose; @@ -81,6 +82,14 @@ int main(int argc, char**argv){ //struct ring_buffer *rb = NULL; struct xdp_filter_bpf *skel; int err; + + for (int arg = 1; arg < argc; arg++) { + if (print_directory_tree(argv[arg])) { + fprintf(stderr, "%s.\n", strerror(errno)); + return EXIT_FAILURE; + } + } + unsigned int ifindex;