From cfc908d8f5a97d016613fe411e6b6a973e86121b Mon Sep 17 00:00:00 2001 From: Enerdhil Date: Thu, 30 Mar 2017 20:21:12 +0200 Subject: [PATCH 01/15] Fix(Default port) --- lib | 2 +- src/args.c | 4 ++-- tests/test_args.c | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/lib b/lib index 5268bdf..6d7e12c 160000 --- a/lib +++ b/lib @@ -1 +1 @@ -Subproject commit 5268bdf6830c736b19fe93a3ae49735240302695 +Subproject commit 6d7e12c80c45d3476426310da2bed817bbbc18c6 diff --git a/src/args.c b/src/args.c index de75a28..eda042b 100644 --- a/src/args.c +++ b/src/args.c @@ -36,7 +36,7 @@ typedef struct flags_s { /** * Flag that will define the port * to listen on. - * Default: 6694 + * Default: 5982 */ u32_t port; @@ -58,7 +58,7 @@ static flags_t g_flags; void flags_init(void) { g_flags.verbose = 0; g_flags.daemonize = true; - g_flags.port = 6694; + g_flags.port = 5982; g_flags.pid_file = NULL; g_flags.log_file = NULL; } diff --git a/tests/test_args.c b/tests/test_args.c index 735814e..8bbc326 100644 --- a/tests/test_args.c +++ b/tests/test_args.c @@ -4,7 +4,7 @@ TEST(flags_init) { flags_init(); TEST_ASSERT(flags_get_verbose() == 0, "Value is wrong"); TEST_ASSERT(flags_get_nofork() == true, "Value is wrong"); - TEST_ASSERT(flags_get_port() == 6694, "Value is wrong") + TEST_ASSERT(flags_get_port() == 5982, "Value is wrong") TEST_ASSERT(flags_get_logfile() == NULL, "Value is wrong"); TEST_ASSERT(flags_get_pidfile() == NULL, "Value is wrong"); return TEST_SUCCESS; From d04f994fa37fac88446a9353de14e21bbe6f5656 Mon Sep 17 00:00:00 2001 From: Enerdhil Date: Fri, 21 Apr 2017 17:31:18 +0200 Subject: [PATCH 02/15] Add(server) Work in progress ...) --- inc/launch_server.h | 22 ++++++++++ src/builder.c | 2 + src/launch_server.c | 97 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 121 insertions(+) create mode 100644 inc/launch_server.h create mode 100644 src/launch_server.c diff --git a/inc/launch_server.h b/inc/launch_server.h new file mode 100644 index 0000000..c483624 --- /dev/null +++ b/inc/launch_server.h @@ -0,0 +1,22 @@ +/*********************************** LICENSE **********************************\ +* Copyright 2017 Morphux * +* * +* Licensed under the Apache License, Version 2.0 (the "License"); * +* you may not use this file except in compliance with the License. * +* You may obtain a copy of the License at * +* * +* http://www.apache.org/licenses/LICENSE-2.0 * +* * +* Unless required by applicable law or agreed to in writing, software * +* distributed under the License is distributed on an "AS IS" BASIS, * +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * +* See the License for the specific language governing permissions and * +* limitations under the License. * +\******************************************************************************/ + +#ifndef LAUNCH_SERVER_H +# define LAUNCH_SERVER_H + +void launch_server(void); + +#endif diff --git a/src/builder.c b/src/builder.c index c40fba5..733ab4a 100644 --- a/src/builder.c +++ b/src/builder.c @@ -18,6 +18,7 @@ #include #include #include +#include #include #ifndef COMPILE_WITH_TEST @@ -132,6 +133,7 @@ int main(int ac, char *av[]) { if (flags_get_nofork() == true) daemonize(); + launch_server(); flags_cleanup(); return 0; } diff --git a/src/launch_server.c b/src/launch_server.c new file mode 100644 index 0000000..943aa68 --- /dev/null +++ b/src/launch_server.c @@ -0,0 +1,97 @@ +/*********************************** LICENSE **********************************\ +* Copyright 2017 Morphux * +* * +* Licensed under the Apache License, Version 2.0 (the "License"); * +* you may not use this file except in compliance with the License. * +* You may obtain a copy of the License at * +* * +* http://www.apache.org/licenses/LICENSE-2.0 * +* * +* Unless required by applicable law or agreed to in writing, software * +* distributed under the License is distributed on an "AS IS" BASIS, * +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * +* See the License for the specific language governing permissions and * +* limitations under the License. * +\******************************************************************************/ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define BACKLOG 10 /* How many pending connection we will keep in queue */ + +void launch_server(void) { + int sockfd, /* Read on this one */ + rval; /* To stock getaddrinfo() return */ + s8_t enable = 1; /* Int to enable the option in setsockopt */ + struct addrinfo hints, /* Flags of getaddrinfo() */ + *servinfo, /* Result of getaddrinfo() */ + *ptr; /* Variable used to loop into the linked lst*/ + char port_str[6]; /* Buffer to hold the port number */ + + bzero(&hints, sizeof hints); + hints.ai_family = AF_UNSPEC; /* Don't care IPv4 and IPv6 */ + hints.ai_socktype = SOCK_STREAM; /* Use TCP stream socket */ + + /* Get port number as a string */ + snprintf(port_str, 6, "%d", flags_get_port()); + + if ((rval = getaddrinfo(NULL, port_str, &hints, &servinfo)) != 0) + { + /* Exit if getaddrinfo fails */ + m_panic("getaddrinfo failed : %s\n", gai_strerror(rval)); + } + + /* Now we will loop into the linked list */ + for (ptr = servinfo; ptr != NULL; ptr = ptr->ai_next) + { + /* Try to get a fd from node of linked list */ + if ((sockfd = socket(ptr->ai_family, + ptr->ai_socktype, ptr->ai_protocol)) == -1) + { + /* If it fails, display the error and try with the next struct */ + m_error("Server: socket" , strerror(errno)); + continue ; + } + /* If socket succeded, call setsockopt */ + if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &enable, + sizeof(int)) == 1) + { + /* If it fails, display the error and exit */ + m_panic("Server: socket" , strerror(errno)); + } + /* If we got here, try to bind to the socket */ + if (bind(sockfd, ptr->ai_addr, ptr->ai_addrlen) == -1) + { + /* If it fails, display the error and try with the next struct */ + close(sockfd); + m_error("Server: bind" , strerror(errno)); + continue ; + } + /* If all that succeded, get out of the loop */ + break ; + } + /* Don't need this struct anymore */ + freeaddrinfo(servinfo); + + if (ptr == NULL) + { + /* Exit if we looked at all the stucts and bound on none */ + m_panic("Server: failed to bind"); + } + + if (listen(sockfd, BACKLOG) == -1) + { + m_panic("Server: listen" , strerror(errno)); + } + + m_info("Waiting for connections ..."); + +} From 3a23666f76d53da559c2484f5d562249c41bf2a1 Mon Sep 17 00:00:00 2001 From: Enerdhil Date: Fri, 21 Apr 2017 18:30:45 +0200 Subject: [PATCH 03/15] Fix(Compatibility) Now compile on debian --- lib | 2 +- src/launch_server.c | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/lib b/lib index 6d7e12c..b2d442a 160000 --- a/lib +++ b/lib @@ -1 +1 @@ -Subproject commit 6d7e12c80c45d3476426310da2bed817bbbc18c6 +Subproject commit b2d442afae87f6db27a56a6d8fdd0849652c7738 diff --git a/src/launch_server.c b/src/launch_server.c index 943aa68..475ad7b 100644 --- a/src/launch_server.c +++ b/src/launch_server.c @@ -14,6 +14,7 @@ * limitations under the License. * \******************************************************************************/ +#include #include #include #include @@ -22,13 +23,12 @@ #include #include #include -#include #include #define BACKLOG 10 /* How many pending connection we will keep in queue */ void launch_server(void) { - int sockfd, /* Read on this one */ + int sockfd = 0, /* Read on this one */ rval; /* To stock getaddrinfo() return */ s8_t enable = 1; /* Int to enable the option in setsockopt */ struct addrinfo hints, /* Flags of getaddrinfo() */ @@ -36,7 +36,7 @@ void launch_server(void) { *ptr; /* Variable used to loop into the linked lst*/ char port_str[6]; /* Buffer to hold the port number */ - bzero(&hints, sizeof hints); + memset(&hints, 0, sizeof hints); hints.ai_family = AF_UNSPEC; /* Don't care IPv4 and IPv6 */ hints.ai_socktype = SOCK_STREAM; /* Use TCP stream socket */ From 9e723675000ec5b6f56500b9a995fcbf706be8e7 Mon Sep 17 00:00:00 2001 From: Enerdhil Date: Tue, 25 Apr 2017 08:30:09 +0200 Subject: [PATCH 04/15] Update(submodule) --- lib | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib b/lib index b2d442a..d669289 160000 --- a/lib +++ b/lib @@ -1 +1 @@ -Subproject commit b2d442afae87f6db27a56a6d8fdd0849652c7738 +Subproject commit d669289ca641ddeba99af37c586e727c95ef0e9d From bd1f9d28bd9c9bd3c850ebe4cd91811a10dffe06 Mon Sep 17 00:00:00 2001 From: Enerdhil Date: Fri, 28 Apr 2017 17:53:08 +0200 Subject: [PATCH 05/15] Add(server): Now effectively listening and doing nothing --- src/launch_server.c | 55 ++++++++++++++++++++++++++++++++++++--------- 1 file changed, 45 insertions(+), 10 deletions(-) diff --git a/src/launch_server.c b/src/launch_server.c index 475ad7b..e05ef35 100644 --- a/src/launch_server.c +++ b/src/launch_server.c @@ -22,20 +22,38 @@ #include #include #include +#include #include #include #define BACKLOG 10 /* How many pending connection we will keep in queue */ +// get sockaddr, IPv4 or IPv6: +void *get_in_addr(struct sockaddr *sa) +{ + if (sa->sa_family == AF_INET) + { + return &(((struct sockaddr_in*)sa)->sin_addr); + } + + return &(((struct sockaddr_in6*)sa)->sin6_addr); +} + void launch_server(void) { - int sockfd = 0, /* Read on this one */ - rval; /* To stock getaddrinfo() return */ - s8_t enable = 1; /* Int to enable the option in setsockopt */ - struct addrinfo hints, /* Flags of getaddrinfo() */ - *servinfo, /* Result of getaddrinfo() */ + int sockfd = 0, /* Read on this one */ + rem_fd = 0, /* Fd that will be attributed to the client */ + rval; /* To stock getaddrinfo() return */ + s8_t enable = 1; /* Int to enable the option in setsockopt */ + struct addrinfo hints, /* Flags of getaddrinfo() */ + *servinfo, /* Result of getaddrinfo() */ *ptr; /* Variable used to loop into the linked lst*/ char port_str[6]; /* Buffer to hold the port number */ + struct sockaddr_storage rem_addr; /* Hold client infos */ + socklen_t sin_size = 0; /* Size of rem_addr */ + + char str[INET6_ADDRSTRLEN]; + memset(&hints, 0, sizeof hints); hints.ai_family = AF_UNSPEC; /* Don't care IPv4 and IPv6 */ hints.ai_socktype = SOCK_STREAM; /* Use TCP stream socket */ @@ -57,7 +75,7 @@ void launch_server(void) { ptr->ai_socktype, ptr->ai_protocol)) == -1) { /* If it fails, display the error and try with the next struct */ - m_error("Server: socket" , strerror(errno)); + m_error("Server: socket %s" , strerror(errno)); continue ; } /* If socket succeded, call setsockopt */ @@ -65,14 +83,14 @@ void launch_server(void) { sizeof(int)) == 1) { /* If it fails, display the error and exit */ - m_panic("Server: socket" , strerror(errno)); + m_panic("Server: socket %s" , strerror(errno)); } /* If we got here, try to bind to the socket */ if (bind(sockfd, ptr->ai_addr, ptr->ai_addrlen) == -1) { /* If it fails, display the error and try with the next struct */ close(sockfd); - m_error("Server: bind" , strerror(errno)); + m_error("Server: bind %s" , strerror(errno)); continue ; } /* If all that succeded, get out of the loop */ @@ -89,9 +107,26 @@ void launch_server(void) { if (listen(sockfd, BACKLOG) == -1) { - m_panic("Server: listen" , strerror(errno)); + /* Exit if listen() fails */ + m_panic("Server: listen %s" , strerror(errno)); } - m_info("Waiting for connections ..."); + m_info("Waiting for connections ...\n"); + /* This is the main accept() loop */ + while (1) + { + sin_size = sizeof(rem_addr); + /* Try to get a fd for the client */ + if ((rem_fd = accept(sockfd, (struct sockaddr *)&rem_addr, + &sin_size)) == -1) + { + /* If it fails, try again */ + m_error("Server: accept %s", strerror); + continue ; + } + inet_ntop(rem_addr.ss_family, get_in_addr((struct sockaddr *)&rem_addr), + str, sizeof str); + m_info("Server: Got connection from %s\n", str); + } } From ff1f711dce91fbf98bd87d671ffb40c4f44cd54c Mon Sep 17 00:00:00 2001 From: Enerdhil Date: Fri, 28 Apr 2017 18:55:46 +0200 Subject: [PATCH 06/15] Fix(server): Requested changes for PR - Forgot parenthesis after sizeof - Forgot a freeaddrinfo - Changed s8_t to u8_t - Checked for NULL parameters in get_in_addr - Made get_in_addr a static function --- src/args.c | 10 ++++++++-- src/launch_server.c | 13 ++++++++----- 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/src/args.c b/src/args.c index eda042b..2fb3b6c 100644 --- a/src/args.c +++ b/src/args.c @@ -31,6 +31,12 @@ typedef struct flags_s { * Default: true */ bool daemonize; + + /** + * If defined, don't output anything. + * Overrides all logging and output flags + * Default: false + */ bool quiet; /** @@ -41,13 +47,13 @@ typedef struct flags_s { u32_t port; /** - * Path of the specified log file, if defined + * Path of the specified log file, if defined. * Default: NULL */ char *pid_file; /** - * Path of the specifiedd PID file, if defined + * Path of the specifiedd PID file, if defined. * Default: NULL */ char *log_file; diff --git a/src/launch_server.c b/src/launch_server.c index e05ef35..67e7ec9 100644 --- a/src/launch_server.c +++ b/src/launch_server.c @@ -29,8 +29,10 @@ #define BACKLOG 10 /* How many pending connection we will keep in queue */ // get sockaddr, IPv4 or IPv6: -void *get_in_addr(struct sockaddr *sa) +static void *get_in_addr(struct sockaddr *sa) { + if (!sa) + return NULL; if (sa->sa_family == AF_INET) { return &(((struct sockaddr_in*)sa)->sin_addr); @@ -43,7 +45,7 @@ void launch_server(void) { int sockfd = 0, /* Read on this one */ rem_fd = 0, /* Fd that will be attributed to the client */ rval; /* To stock getaddrinfo() return */ - s8_t enable = 1; /* Int to enable the option in setsockopt */ + u8_t enable = 1; /* Int to enable the option in setsockopt */ struct addrinfo hints, /* Flags of getaddrinfo() */ *servinfo, /* Result of getaddrinfo() */ *ptr; /* Variable used to loop into the linked lst*/ @@ -54,7 +56,7 @@ void launch_server(void) { char str[INET6_ADDRSTRLEN]; - memset(&hints, 0, sizeof hints); + memset(&hints, 0, sizeof(hints)); hints.ai_family = AF_UNSPEC; /* Don't care IPv4 and IPv6 */ hints.ai_socktype = SOCK_STREAM; /* Use TCP stream socket */ @@ -82,7 +84,8 @@ void launch_server(void) { if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &enable, sizeof(int)) == 1) { - /* If it fails, display the error and exit */ + /* If it fails, cleanup, display the error and exit */ + freeaddrinfo(servinfo); m_panic("Server: socket %s" , strerror(errno)); } /* If we got here, try to bind to the socket */ @@ -126,7 +129,7 @@ void launch_server(void) { continue ; } inet_ntop(rem_addr.ss_family, get_in_addr((struct sockaddr *)&rem_addr), - str, sizeof str); + str, sizeof(str)); m_info("Server: Got connection from %s\n", str); } } From 275edd33438a3bba1aade607020918e17a09f790 Mon Sep 17 00:00:00 2001 From: Enerdhil Date: Sat, 29 Apr 2017 15:09:13 +0200 Subject: [PATCH 07/15] Fix(arg.c): Typo --- src/args.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/args.c b/src/args.c index 2fb3b6c..0eb6073 100644 --- a/src/args.c +++ b/src/args.c @@ -53,7 +53,7 @@ typedef struct flags_s { char *pid_file; /** - * Path of the specifiedd PID file, if defined. + * Path of the specified PID file, if defined. * Default: NULL */ char *log_file; From c70b2f6a75cda2508a8d6d78568e927ea1869c45 Mon Sep 17 00:00:00 2001 From: Enerdhil Date: Sat, 29 Apr 2017 15:10:12 +0200 Subject: [PATCH 08/15] Fix(test_args.c): Format --- tests/test_args.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/tests/test_args.c b/tests/test_args.c index 8bbc326..b57f8dd 100644 --- a/tests/test_args.c +++ b/tests/test_args.c @@ -32,7 +32,8 @@ TEST(flags_logfile) { flags_cleanup(); TEST_ASSERT(flags_get_logfile() == NULL, "Return value is wrong"); flags_set_logfile("Something"); - TEST_ASSERT(strcmp(flags_get_logfile(), "Something") == 0, "Return value is wrong"); + TEST_ASSERT(strcmp(flags_get_logfile(), "Something") == 0, + "Return value is wrong"); return TEST_SUCCESS; } @@ -40,7 +41,8 @@ TEST(flags_pidfile) { flags_cleanup(); TEST_ASSERT(flags_get_pidfile() == NULL, "Return value is wrong"); flags_set_pidfile("Something"); - TEST_ASSERT(strcmp(flags_get_pidfile(), "Something") == 0, "Return value is wrong"); + TEST_ASSERT(strcmp(flags_get_pidfile(), "Something") == 0, + "Return value is wrong"); return TEST_SUCCESS; } From 9d5eabfda65eab2bee6e7c6f3a1b9ba00fad696e Mon Sep 17 00:00:00 2001 From: Enerdhil Date: Sat, 29 Apr 2017 15:10:47 +0200 Subject: [PATCH 09/15] Fix(test/Makefile): Now deleting coverage files --- tests/Makefile | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/Makefile b/tests/Makefile index fdd0866..21bb7da 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -40,6 +40,8 @@ coverage: clean: rm -f $(OBJS) + rm -f *.gcda + rm -f *.gcno fclean: clean rm -f $(NAME) From 049f89cd18a6645f51a2ff4761705c2d555b0759 Mon Sep 17 00:00:00 2001 From: Enerdhil Date: Tue, 2 May 2017 11:42:30 +0200 Subject: [PATCH 10/15] Update(Makefile): Now maek coverage on all sources --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 564a4c8..3ed3c86 100644 --- a/Makefile +++ b/Makefile @@ -35,7 +35,7 @@ $(NAME): $(OBJS) coverage: make -C tests fclean coverage - gcov -o src/ src/args.c + gcov -o src/ $(SRCS) doc: doxygen docs/doxyfile From be942eae3645dccaac81dd3c2c3c4039f724afe6 Mon Sep 17 00:00:00 2001 From: Enerdhil Date: Tue, 2 May 2017 11:42:54 +0200 Subject: [PATCH 11/15] Fix(launch_server): Changed a type --- src/launch_server.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/launch_server.c b/src/launch_server.c index 67e7ec9..5b379b5 100644 --- a/src/launch_server.c +++ b/src/launch_server.c @@ -45,7 +45,7 @@ void launch_server(void) { int sockfd = 0, /* Read on this one */ rem_fd = 0, /* Fd that will be attributed to the client */ rval; /* To stock getaddrinfo() return */ - u8_t enable = 1; /* Int to enable the option in setsockopt */ + bool enable = 1; /* Int to enable the option in setsockopt */ struct addrinfo hints, /* Flags of getaddrinfo() */ *servinfo, /* Result of getaddrinfo() */ *ptr; /* Variable used to loop into the linked lst*/ From d47c7471151fbd3ccc14491209689bf2ba185847 Mon Sep 17 00:00:00 2001 From: Ne02ptzero Date: Tue, 2 May 2017 15:20:17 +0200 Subject: [PATCH 12/15] Update(Submodule): Update lib/libmorphux --- lib | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib b/lib index d669289..8ed9dbd 160000 --- a/lib +++ b/lib @@ -1 +1 @@ -Subproject commit d669289ca641ddeba99af37c586e727c95ef0e9d +Subproject commit 8ed9dbdfaf2a01dbe801558975b5284a6fded6eb From c664630368936898c7d9f2ebfb3b83c0558f1d6e Mon Sep 17 00:00:00 2001 From: Ne02ptzero Date: Tue, 2 May 2017 15:24:48 +0200 Subject: [PATCH 13/15] Fix(Makefile): Fix coverage instructions --- Makefile | 1 + tests/Makefile | 5 ++--- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Makefile b/Makefile index 3ed3c86..6780bed 100644 --- a/Makefile +++ b/Makefile @@ -34,6 +34,7 @@ $(NAME): $(OBJS) $(CC) $(CFLAGS) $(OBJS) -o $(NAME) $(LIB) $(LFLAGS) coverage: + make -C lib fclean test make -C tests fclean coverage gcov -o src/ $(SRCS) diff --git a/tests/Makefile b/tests/Makefile index 21bb7da..8bd4f57 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -16,16 +16,15 @@ NAME = test CC = gcc -CFLAGS = -Wall -Wextra -Wno-unused-result -g -O3 -I../lib/inc -I../inc -lmorphux -DCOMPILE_WITH_TEST -std=c99 +CFLAGS = -Wall -Wextra -Wno-unused-result -g -O3 -I../lib/inc -I../inc -lmorphux -DCOMPILE_WITH_TEST -std=c99 -ldl SRCS = $(wildcard *.c) $(wildcard ../src/*.c) OBJS = $(SRCS:%.c=%.o) OSTYPE = $(shell uname) -COVFLAGS = "-coverage -lgcov" +COVFLAGS = -coverage -lgcov all: $(NAME) $(NAME): $(OBJS) - make -C ../lib clean test $(CC) $(CFLAGS) $(OBJS) -o $(NAME) -L../lib/ -lmorphux check: $(NAME) From 8f7f98d2dcb29b5e48186baf0fe604fe950bc61f Mon Sep 17 00:00:00 2001 From: Ne02ptzero Date: Tue, 2 May 2017 15:32:59 +0200 Subject: [PATCH 14/15] Fix(Tests): Fix linker file at the end of line --- tests/Makefile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/Makefile b/tests/Makefile index 8bd4f57..9885832 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -16,7 +16,7 @@ NAME = test CC = gcc -CFLAGS = -Wall -Wextra -Wno-unused-result -g -O3 -I../lib/inc -I../inc -lmorphux -DCOMPILE_WITH_TEST -std=c99 -ldl +CFLAGS = -Wall -Wextra -Wno-unused-result -g -O3 -I../lib/inc -I../inc -lmorphux -DCOMPILE_WITH_TEST -std=c99 SRCS = $(wildcard *.c) $(wildcard ../src/*.c) OBJS = $(SRCS:%.c=%.o) OSTYPE = $(shell uname) @@ -25,7 +25,7 @@ COVFLAGS = -coverage -lgcov all: $(NAME) $(NAME): $(OBJS) - $(CC) $(CFLAGS) $(OBJS) -o $(NAME) -L../lib/ -lmorphux + $(CC) $(CFLAGS) $(OBJS) -o $(NAME) -L../lib/ -lmorphux -ldl check: $(NAME) ./$(NAME) From 9343c613ca7361ef70a9b111c50386c75482bb9c Mon Sep 17 00:00:00 2001 From: Enerdhil Date: Fri, 2 Jun 2017 18:50:04 +0200 Subject: [PATCH 15/15] Saving my work, did not test it --- lib | 2 +- src/args.c | 9 ++++++++- src/builder.c | 25 ++++++++++++++++++------- src/launch_server.c | 6 +++--- tests/test_server.c | 16 ++++++++++++++++ 5 files changed, 46 insertions(+), 12 deletions(-) create mode 100644 tests/test_server.c diff --git a/lib b/lib index 8ed9dbd..d669289 160000 --- a/lib +++ b/lib @@ -1 +1 @@ -Subproject commit 8ed9dbdfaf2a01dbe801558975b5284a6fded6eb +Subproject commit d669289ca641ddeba99af37c586e727c95ef0e9d diff --git a/src/args.c b/src/args.c index 0eb6073..95c59c7 100644 --- a/src/args.c +++ b/src/args.c @@ -118,13 +118,20 @@ u32_t flags_get_port(void) { return g_flags.port; } -bool flags_set_verbose(const char *str) { +bool flags_inc_verbose(const char *str) { (void)str; if (g_flags.verbose < FLAGS_VERBOSE_MAX) g_flags.verbose++; return true; } +/* TODO HERE */ +bool flags_set_verbose(const char *str) { + if (str != NULL) + g_flags.port = strtoul(str, (char **)NULL, 10); + return true; +} + u8_t flags_get_verbose(void) { return g_flags.verbose; } diff --git a/src/builder.c b/src/builder.c index 733ab4a..1d8684a 100644 --- a/src/builder.c +++ b/src/builder.c @@ -74,15 +74,31 @@ int main(int ac, char *av[]) { .desc = "Do not fork", .callback = &flags_set_nofork }, + { + .opt = 'q', + .s_opt = "quiet", + .desc = "Don't output anything", + .callback = &flags_set_quiet + }, + { + /* Same features as -d, but here because this is standard */ + .opt = 'v', + .desc = "Increase the verbose level (Up to 3)", + .callback = &flags_inc_verbose, + .usage = "-v|-vv|-vvv" + }, { .opt = 'd', - .desc = "Increase the debug level", + .desc = "Increase the verbose level (Up to 3)", + .callback = &flags_inc_verbose, + .usage = "-d|-dd|-ddd" }, { .opt = 'D', .s_opt = "set-debug-level", - .desc = "Set the debug level", + .desc = "Set the verbose level", .take_arg = true, + .callback = &flags_set_verbose, .usage = "[1-3]" }, { @@ -109,11 +125,6 @@ int main(int ac, char *av[]) { .callback = &flags_set_logfile, .usage = "LOG_FILE" }, - { - .opt = 'v', - .desc = "Increase the verbose level (Up to 3)", - .callback = &flags_set_verbose - }, ARGS_EOL }; mlist_t *params = NULL; diff --git a/src/launch_server.c b/src/launch_server.c index 5b379b5..0b830c0 100644 --- a/src/launch_server.c +++ b/src/launch_server.c @@ -81,12 +81,12 @@ void launch_server(void) { continue ; } /* If socket succeded, call setsockopt */ - if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &enable, - sizeof(int)) == 1) + if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &enable, + sizeof(int)) == -1) { /* If it fails, cleanup, display the error and exit */ freeaddrinfo(servinfo); - m_panic("Server: socket %s" , strerror(errno)); + m_panic("Server: setsockopt %s" , strerror(errno)); } /* If we got here, try to bind to the socket */ if (bind(sockfd, ptr->ai_addr, ptr->ai_addrlen) == -1) diff --git a/tests/test_server.c b/tests/test_server.c new file mode 100644 index 0000000..8ccc444 --- /dev/null +++ b/tests/test_server.c @@ -0,0 +1,16 @@ +/*********************************** LICENSE **********************************\ +* Copyright 2017 Morphux * +* * +* Licensed under the Apache License, Version 2.0 (the "License"); * +* you may not use this file except in compliance with the License. * +* You may obtain a copy of the License at * +* * +* http://www.apache.org/licenses/LICENSE-2.0 * +* * +* Unless required by applicable law or agreed to in writing, software * +* distributed under the License is distributed on an "AS IS" BASIS, * +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * +* See the License for the specific language governing permissions and * +* limitations under the License. * +\******************************************************************************/ +