From e34c25a0e3584aa0c8093212e5723b28c2633daa Mon Sep 17 00:00:00 2001 From: huangxin Date: Tue, 8 Nov 2022 18:09:54 +0800 Subject: [PATCH] =?UTF-8?q?OCT=20=E9=87=8D=E6=96=B0=E6=A0=BC=E5=BC=8F?= =?UTF-8?q?=E5=8C=96=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .clang-format | 1 + srcs/httpserver/CMakeLists.txt | 6 +- srcs/httpserver/include/haywire.h | 183 +-- .../src/haywire/configuration/ini.c | 18 +- .../src/haywire/connection_consumer.c | 3 +- .../src/haywire/connection_dispatcher.c | 12 +- srcs/httpserver/src/haywire/http_parser.c | 44 +- srcs/httpserver/src/haywire/http_request.c | 6 +- srcs/httpserver/src/haywire/http_response.c | 3 +- srcs/httpserver/src/haywire/http_server.c | 16 +- srcs/httpserver/src/haywire/khash.h | 6 +- srcs/httpserver/src/haywire/picohttpparser.c | 42 +- srcs/libs/init/init.c | 1 - srcs/open_dhcp/opendhcpd.cpp | 834 +++++++++----- srcs/open_dhcp/opendhcpd.h | 1002 ++++++++--------- srcs/open_dhcp/query.cpp | 6 +- 16 files changed, 1208 insertions(+), 975 deletions(-) diff --git a/.clang-format b/.clang-format index 7f5e2b1..0dc2f1e 100644 --- a/.clang-format +++ b/.clang-format @@ -1,6 +1,7 @@ # ClangFormatConfigureSource: 'clang-format-file://D:/development/c/daemon_agent/.clang-format' Language: Cpp AccessModifierOffset: -4 +InsertBraces: true AlignAfterOpenBracket: Align AlignConsecutiveMacros: Consecutive AlignConsecutiveAssignments: Consecutive diff --git a/srcs/httpserver/CMakeLists.txt b/srcs/httpserver/CMakeLists.txt index d20a7c4..79f17cd 100644 --- a/srcs/httpserver/CMakeLists.txt +++ b/srcs/httpserver/CMakeLists.txt @@ -23,8 +23,8 @@ endif (UNIX) INCLUDE_DIRECTORIES(./include ./src ../libs/include) file(GLOB_RECURSE HW_HEADS - ./src/haywire/*.h - ./src/haywire/configuration/*.h) + ./src/haywire/*.h + ./src/haywire/configuration/*.h) AUX_SOURCE_DIRECTORY(src/haywire HW_SRC) AUX_SOURCE_DIRECTORY(src/haywire/configuration HW_SRC) @@ -34,6 +34,6 @@ INCLUDE_DIRECTORIES(. ./include) #find_package(Threads REQUIRED) add_library(haywire STATIC ${HW_SRC} ${HW_HEADS}) -target_link_libraries (haywire -luv -pthread) +target_link_libraries(haywire -luv -pthread) diff --git a/srcs/httpserver/include/haywire.h b/srcs/httpserver/include/haywire.h index 5c8f42a..687f4ae 100644 --- a/srcs/httpserver/include/haywire.h +++ b/srcs/httpserver/include/haywire.h @@ -8,25 +8,25 @@ extern "C" { #include #ifdef _WIN32 - /* Windows - set up dll import/export decorators. */ - #ifdef BUILDING_HAYWIRE_SHARED - /* Building shared library. */ - #define HAYWIRE_EXTERN __declspec(dllexport) - #else - #ifdef USING_HAYWIRE_SHARED - /* Using shared library. */ - #define HAYWIRE_EXTERN __declspec(dllimport) - #else - /* Building static library. */ - #define HAYWIRE_EXTERN /* nothing */ - #endif - #endif - - #define HAYWIRE_CALLING_CONVENTION __cdecl +/* Windows - set up dll import/export decorators. */ +#ifdef BUILDING_HAYWIRE_SHARED +/* Building shared library. */ +#define HAYWIRE_EXTERN __declspec(dllexport) #else - /* Building static library. */ - #define HAYWIRE_EXTERN /* nothing */ - #define HAYWIRE_CALLING_CONVENTION /* nothing */ +#ifdef USING_HAYWIRE_SHARED +/* Using shared library. */ +#define HAYWIRE_EXTERN __declspec(dllimport) +#else +/* Building static library. */ +#define HAYWIRE_EXTERN /* nothing */ +#endif +#endif + +#define HAYWIRE_CALLING_CONVENTION __cdecl +#else +/* Building static library. */ +#define HAYWIRE_EXTERN /* nothing */ +#define HAYWIRE_CALLING_CONVENTION /* nothing */ #endif /* Informational 1xx */ @@ -93,100 +93,107 @@ extern "C" { /* Request Methods */ #define HW_HTTP_METHOD_MAP(XX) \ -XX(0, DELETE, DELETE) \ -XX(1, GET, GET) \ -XX(2, HEAD, HEAD) \ -XX(3, POST, POST) \ -XX(4, PUT, PUT) \ -/* pathological */ \ -XX(5, CONNECT, CONNECT) \ -XX(6, OPTIONS, OPTIONS) \ -XX(7, TRACE, TRACE) \ -/* webdav */ \ -XX(8, COPY, COPY) \ -XX(9, LOCK, LOCK) \ -XX(10, MKCOL, MKCOL) \ -XX(11, MOVE, MOVE) \ -XX(12, PROPFIND, PROPFIND) \ -XX(13, PROPPATCH, PROPPATCH) \ -XX(14, SEARCH, SEARCH) \ -XX(15, UNLOCK, UNLOCK) \ -/* subversion */ \ -XX(16, REPORT, REPORT) \ -XX(17, MKACTIVITY, MKACTIVITY) \ -XX(18, CHECKOUT, CHECKOUT) \ -XX(19, MERGE, MERGE) \ -/* upnp */ \ -XX(20, MSEARCH, M-SEARCH) \ -XX(21, NOTIFY, NOTIFY) \ -XX(22, SUBSCRIBE, SUBSCRIBE) \ -XX(23, UNSUBSCRIBE, UNSUBSCRIBE) \ -/* RFC-5789 */ \ -XX(24, PATCH, PATCH) \ -XX(25, PURGE, PURGE) \ + XX(0, DELETE, DELETE) \ + XX(1, GET, GET) \ + XX(2, HEAD, HEAD) \ + XX(3, POST, POST) \ + XX(4, PUT, PUT) \ + /* pathological */ \ + XX(5, CONNECT, CONNECT) \ + XX(6, OPTIONS, OPTIONS) \ + XX(7, TRACE, TRACE) \ + /* webdav */ \ + XX(8, COPY, COPY) \ + XX(9, LOCK, LOCK) \ + XX(10, MKCOL, MKCOL) \ + XX(11, MOVE, MOVE) \ + XX(12, PROPFIND, PROPFIND) \ + XX(13, PROPPATCH, PROPPATCH) \ + XX(14, SEARCH, SEARCH) \ + XX(15, UNLOCK, UNLOCK) \ + /* subversion */ \ + XX(16, REPORT, REPORT) \ + XX(17, MKACTIVITY, MKACTIVITY) \ + XX(18, CHECKOUT, CHECKOUT) \ + XX(19, MERGE, MERGE) \ + /* upnp */ \ + XX(20, MSEARCH, M - SEARCH) \ + XX(21, NOTIFY, NOTIFY) \ + XX(22, SUBSCRIBE, SUBSCRIBE) \ + XX(23, UNSUBSCRIBE, UNSUBSCRIBE) \ + /* RFC-5789 */ \ + XX(24, PATCH, PATCH) \ + XX(25, PURGE, PURGE) -enum hw_http_method -{ +enum hw_http_method { #define XX(num, name, string) HW_HTTP_##name = num, HW_HTTP_METHOD_MAP(XX) #undef XX }; #define STRLENOF(s) strlen(s) -#define SETSTRING(s,val) s.value=val; s.length=STRLENOF(val) -#define APPENDSTRING(s,val) memcpy((char*)s->value + s->length, val, STRLENOF(val)); s->length+=STRLENOF(val) +#define SETSTRING(s, val) \ + s.value = val; \ + s.length = STRLENOF(val) +#define APPENDSTRING(s, val) \ + memcpy((char *)s->value + s->length, val, STRLENOF(val)); \ + s->length += STRLENOF(val) -typedef void* hw_http_response; +typedef void *hw_http_response; -typedef struct -{ - char* value; +typedef struct { + char *value; size_t length; } hw_string; -typedef struct -{ - char* http_listen_address; +typedef struct { + char *http_listen_address; unsigned int http_listen_port; unsigned int thread_count; - char* balancer; - char* parser; - bool tcp_nodelay; + char *balancer; + char *parser; + bool tcp_nodelay; unsigned int listen_backlog; unsigned int max_request_size; } configuration; - -typedef struct -{ + +typedef struct { unsigned short http_major; unsigned short http_minor; - unsigned char method; - int keep_alive; - hw_string* url; - void* headers; - hw_string* body; - size_t body_length; - enum {OK, SIZE_EXCEEDED, BAD_REQUEST, INTERNAL_ERROR} state; + unsigned char method; + int keep_alive; + hw_string *url; + void *headers; + hw_string *body; + size_t body_length; + enum { + OK, + SIZE_EXCEEDED, + BAD_REQUEST, + INTERNAL_ERROR + } state; } http_request; -typedef void (HAYWIRE_CALLING_CONVENTION *http_request_callback)(http_request* request, hw_http_response* response, void* user_data); -typedef void (HAYWIRE_CALLING_CONVENTION *http_response_complete_callback)(void* user_data); +typedef void(HAYWIRE_CALLING_CONVENTION *http_request_callback)(http_request *request, hw_http_response *response, + void *user_data); +typedef void(HAYWIRE_CALLING_CONVENTION *http_response_complete_callback)(void *user_data); -HAYWIRE_EXTERN int hw_init_from_config(char* configuration_filename); -HAYWIRE_EXTERN int hw_init_with_config(configuration* config); -HAYWIRE_EXTERN int hw_http_open(); -HAYWIRE_EXTERN void free_http_server(); -HAYWIRE_EXTERN void hw_http_add_route(char* route, http_request_callback callback, void* user_data); -HAYWIRE_EXTERN hw_string* hw_get_header(http_request* request, hw_string* key); +HAYWIRE_EXTERN int hw_init_from_config(char *configuration_filename); +HAYWIRE_EXTERN int hw_init_with_config(configuration *config); +HAYWIRE_EXTERN int hw_http_open(); +HAYWIRE_EXTERN void free_http_server(); +HAYWIRE_EXTERN void hw_http_add_route(char *route, http_request_callback callback, void *user_data); +HAYWIRE_EXTERN hw_string *hw_get_header(http_request *request, hw_string *key); -HAYWIRE_EXTERN void hw_free_http_response(hw_http_response* response); -HAYWIRE_EXTERN void hw_set_http_version(hw_http_response* response, unsigned short major, unsigned short minor); -HAYWIRE_EXTERN void hw_set_response_status_code(hw_http_response* response, hw_string* status_code); -HAYWIRE_EXTERN void hw_set_response_header(hw_http_response* response, hw_string* name, hw_string* value); -HAYWIRE_EXTERN void hw_set_body(hw_http_response* response, hw_string* body); -HAYWIRE_EXTERN void hw_http_response_send(hw_http_response* response, void* user_data, http_response_complete_callback callback); +HAYWIRE_EXTERN void hw_free_http_response(hw_http_response *response); +HAYWIRE_EXTERN void hw_set_http_version(hw_http_response *response, unsigned short major, unsigned short minor); +HAYWIRE_EXTERN void hw_set_response_status_code(hw_http_response *response, hw_string *status_code); +HAYWIRE_EXTERN void hw_set_response_header(hw_http_response *response, hw_string *name, hw_string *value); +HAYWIRE_EXTERN void hw_set_body(hw_http_response *response, hw_string *body); +HAYWIRE_EXTERN void hw_http_response_send(hw_http_response *response, void *user_data, + http_response_complete_callback callback); HAYWIRE_EXTERN void hw_http_response_send_error(hw_http_response *response, const char *error, const char *err_msg); -HAYWIRE_EXTERN void hw_print_request_headers(http_request* request); +HAYWIRE_EXTERN void hw_print_request_headers(http_request *request); #ifdef __cplusplus } diff --git a/srcs/httpserver/src/haywire/configuration/ini.c b/srcs/httpserver/src/haywire/configuration/ini.c index 46bf276..98bc6dd 100644 --- a/srcs/httpserver/src/haywire/configuration/ini.c +++ b/srcs/httpserver/src/haywire/configuration/ini.c @@ -21,15 +21,17 @@ /* Strip whitespace chars off end of given string, in place. Return s. */ static char *rstrip(char *s) { char *p = s + strlen(s); - while (p > s && isspace(*--p)) + while (p > s && isspace(*--p)) { *p = '\0'; + } return s; } /* Return pointer to first non-whitespace char in given string. */ static char *lskip(const char *s) { - while (*s && isspace(*s)) + while (*s && isspace(*s)) { s++; + } return (char *)s; } @@ -78,8 +80,9 @@ int ini_parse_file(FILE *file, int (*handler)(void *, const char *, const char * else if (*prev_name && *start && start > line) { /* Non-black line with leading whitespace, treat as continuation of previous name's value (as per Python ConfigParser). */ - if (!handler(user, section, prev_name, start) && !error) + if (!handler(user, section, prev_name, start) && !error) { error = lineno; + } } #endif else if (*start == '[') { @@ -104,14 +107,16 @@ int ini_parse_file(FILE *file, int (*handler)(void *, const char *, const char * name = rstrip(start); value = lskip(end + 1); end = find_char_or_comment(value, '\0'); - if (*end == ';') + if (*end == ';') { *end = '\0'; + } rstrip(value); /* Valid name[=:]value pair found, call handler */ strncpy0(prev_name, name, sizeof(prev_name)); - if (!handler(user, section, name, value) && !error) + if (!handler(user, section, name, value) && !error) { error = lineno; + } } else if (!error) { /* No '=' or ':' found on name[=:]value line */ error = lineno; @@ -128,8 +133,9 @@ int ini_parse(const char *filename, int (*handler)(void *, const char *, const c int error; file = fopen(filename, "r"); - if (!file) + if (!file) { return -1; + } error = ini_parse_file(file, handler, user); fclose(file); return error; diff --git a/srcs/httpserver/src/haywire/connection_consumer.c b/srcs/httpserver/src/haywire/connection_consumer.c index fc2dbe3..2cdcfa1 100644 --- a/srcs/httpserver/src/haywire/connection_consumer.c +++ b/srcs/httpserver/src/haywire/connection_consumer.c @@ -27,8 +27,9 @@ void ipc_read_cb(uv_stream_t *handle, ssize_t nread, const uv_buf_t *buf) { if (tcp_nodelay) { rc = uv_tcp_nodelay((uv_tcp_t *)ctx->server_handle, 1); } - } else if (type == UV_NAMED_PIPE) + } else if (type == UV_NAMED_PIPE) { rc = uv_pipe_init(loop, (uv_pipe_t *)ctx->server_handle, 0); + } rc = uv_accept(handle, ctx->server_handle); uv_close((uv_handle_t *)&ctx->ipc_pipe, NULL); diff --git a/srcs/httpserver/src/haywire/connection_dispatcher.c b/srcs/httpserver/src/haywire/connection_dispatcher.c index 9d4365c..9a2ca73 100644 --- a/srcs/httpserver/src/haywire/connection_dispatcher.c +++ b/srcs/httpserver/src/haywire/connection_dispatcher.c @@ -36,15 +36,17 @@ void ipc_connection_cb(uv_stream_t *ipc_pipe, int status) { if (sc->tcp_nodelay) { rc = uv_tcp_nodelay((uv_tcp_t *)&pc->peer_handle, 1); } - } else if (ipc_pipe->type == UV_NAMED_PIPE) + } else if (ipc_pipe->type == UV_NAMED_PIPE) { rc = uv_pipe_init(loop, (uv_pipe_t *)&pc->peer_handle, 1); + } rc = uv_accept(ipc_pipe, (uv_stream_t *)&pc->peer_handle); rc = uv_write2( &pc->write_req, (uv_stream_t *)&pc->peer_handle, &buf, 1, (uv_stream_t *)&sc->server_handle, ipc_write_cb); - if (--sc->num_connects == 0) + if (--sc->num_connects == 0) { uv_close((uv_handle_t *)ipc_pipe, NULL); + } } extern void print_configuration(); @@ -81,13 +83,15 @@ void start_connection_dispatching(uv_handle_type type, unsigned int num_servers, rc = uv_pipe_bind(&ctx.ipc_pipe, "HAYWIRE_CONNECTION_DISPATCH_PIPE_NAME"); rc = uv_listen((uv_stream_t *)&ctx.ipc_pipe, listen_backlog, ipc_connection_cb); - for (i = 0; i < num_servers; i++) + for (i = 0; i < num_servers; i++) { uv_sem_post(&servers[i].semaphore); + } rc = uv_run(loop, UV_RUN_DEFAULT); uv_close((uv_handle_t *)&ctx.server_handle, NULL); rc = uv_run(loop, UV_RUN_DEFAULT); - for (i = 0; i < num_servers; i++) + for (i = 0; i < num_servers; i++) { uv_sem_wait(&servers[i].semaphore); + } } diff --git a/srcs/httpserver/src/haywire/http_parser.c b/srcs/httpserver/src/haywire/http_parser.c index 9a5a150..91910f5 100644 --- a/srcs/httpserver/src/haywire/http_parser.c +++ b/srcs/httpserver/src/haywire/http_parser.c @@ -758,10 +758,12 @@ size_t http_parser_execute(http_parser *parser, const http_parser_settings *sett } } - if (CURRENT_STATE() == s_header_field) + if (CURRENT_STATE() == s_header_field) { header_field_mark = data; - if (CURRENT_STATE() == s_header_value) + } + if (CURRENT_STATE() == s_header_value) { header_value_mark = data; + } switch (CURRENT_STATE()) { case s_req_path: case s_req_schema: @@ -786,8 +788,9 @@ size_t http_parser_execute(http_parser *parser, const http_parser_settings *sett for (p = data; p != data + len; p++) { ch = *p; - if (PARSING_HEADER(CURRENT_STATE())) + if (PARSING_HEADER(CURRENT_STATE())) { COUNT_HEADER_SIZE(1); + } switch (CURRENT_STATE()) { @@ -795,15 +798,17 @@ size_t http_parser_execute(http_parser *parser, const http_parser_settings *sett /* this state is used after a 'Connection: close' message * the parser will error out if it reads another message */ - if (LIKELY(ch == CR || ch == LF)) + if (LIKELY(ch == CR || ch == LF)) { break; + } SET_ERRNO(HPE_CLOSED_CONNECTION); goto error; case s_start_req_or_res: { - if (ch == CR || ch == LF) + if (ch == CR || ch == LF) { break; + } parser->flags = 0; parser->content_length = ULLONG_MAX; @@ -1028,8 +1033,9 @@ size_t http_parser_execute(http_parser *parser, const http_parser_settings *sett break; case s_start_req: { - if (ch == CR || ch == LF) + if (ch == CR || ch == LF) { break; + } parser->flags = 0; parser->content_length = ULLONG_MAX; @@ -1177,8 +1183,9 @@ size_t http_parser_execute(http_parser *parser, const http_parser_settings *sett } case s_req_spaces_before_url: { - if (ch == ' ') + if (ch == ' ') { break; + } MARK(url); if (parser->method == HTTP_CONNECT) { @@ -1419,8 +1426,9 @@ size_t http_parser_execute(http_parser *parser, const http_parser_settings *sett ch = *p; c = TOKEN(ch); - if (!c) + if (!c) { break; + } switch (parser->header_state) { case h_general: @@ -1511,8 +1519,9 @@ size_t http_parser_execute(http_parser *parser, const http_parser_settings *sett case h_content_length: case h_transfer_encoding: case h_upgrade: - if (ch != ' ') + if (ch != ' ') { parser->header_state = h_general; + } break; default: @@ -1539,8 +1548,9 @@ size_t http_parser_execute(http_parser *parser, const http_parser_settings *sett } case s_header_value_discard_ws: - if (ch == ' ' || ch == '\t') + if (ch == ' ' || ch == '\t') { break; + } if (ch == CR) { UPDATE_STATE(s_header_value_discard_ws_almost_done); @@ -1644,10 +1654,11 @@ size_t http_parser_execute(http_parser *parser, const http_parser_settings *sett p_cr = memchr(p, CR, limit); p_lf = memchr(p, LF, limit); if (p_cr != NULL) { - if (p_lf != NULL && p_cr >= p_lf) + if (p_lf != NULL && p_cr >= p_lf) { p = p_lf; - else + } else { p = p_cr; + } } else if (UNLIKELY(p_lf != NULL)) { p = p_lf; } else { @@ -1666,8 +1677,9 @@ size_t http_parser_execute(http_parser *parser, const http_parser_settings *sett case h_content_length: { uint64_t t; - if (ch == ' ') + if (ch == ' ') { break; + } if (UNLIKELY(!IS_NUM(ch))) { SET_ERRNO(HPE_INVALID_CONTENT_LENGTH); @@ -1754,8 +1766,9 @@ size_t http_parser_execute(http_parser *parser, const http_parser_settings *sett break; case h_transfer_encoding_chunked: - if (ch != ' ') + if (ch != ' ') { h_state = h_general; + } break; case h_connection_keep_alive: @@ -1786,8 +1799,9 @@ size_t http_parser_execute(http_parser *parser, const http_parser_settings *sett COUNT_HEADER_SIZE(p - start); - if (p == data + len) + if (p == data + len) { --p; + } break; } diff --git a/srcs/httpserver/src/haywire/http_request.c b/srcs/httpserver/src/haywire/http_request.c index 971db00..b8d25bf 100644 --- a/srcs/httpserver/src/haywire/http_request.c +++ b/srcs/httpserver/src/haywire/http_request.c @@ -21,9 +21,11 @@ static const char response_404[] = "HTTP/1.1 404 Not Found" CRLF "Server: Haywir static kh_inline khint_t hw_string_hash_func(hw_string *s) { khint_t h = s->length > 0 ? (khint_t)*s->value : 0; - if (h) - for (int i = 0; i < s->length; i++) + if (h) { + for (int i = 0; i < s->length; i++) { h = (h << 5) - h + (khint_t) * (s->value + i); + } + } return h; } diff --git a/srcs/httpserver/src/haywire/http_response.c b/srcs/httpserver/src/haywire/http_response.c index d7d4c1b..e43a0ad 100644 --- a/srcs/httpserver/src/haywire/http_response.c +++ b/srcs/httpserver/src/haywire/http_response.c @@ -54,8 +54,9 @@ void hw_set_body(hw_http_response *response, hw_string *body) { int num_chars(int n) { int r = 1; - if (n < 0) + if (n < 0) { n = (n == INT_MIN) ? INT_MAX : -n; + } while (n > 9) { n /= 10; r++; diff --git a/srcs/httpserver/src/haywire/http_server.c b/srcs/httpserver/src/haywire/http_server.c index 8267285..53dd8fb 100644 --- a/srcs/httpserver/src/haywire/http_server.c +++ b/srcs/httpserver/src/haywire/http_server.c @@ -39,14 +39,14 @@ static uv_tcp_t server; static http_parser_settings parser_settings; static struct sockaddr_in listen_address; -uv_loop_t *uv_loop; -void *routes; -hw_string* http_v1_0; -hw_string *http_v1_1; -hw_string *server_name; -int listener_count; -uv_async_t *listener_async_handles; -uv_loop_t* listener_event_loops; +uv_loop_t *uv_loop; +void *routes; +hw_string *http_v1_0; +hw_string *http_v1_1; +hw_string *server_name; +int listener_count; +uv_async_t *listener_async_handles; +uv_loop_t *listener_event_loops; uv_barrier_t *listeners_created_barrier; int hw_init_with_config(configuration *c) { diff --git a/srcs/httpserver/src/haywire/khash.h b/srcs/httpserver/src/haywire/khash.h index 43f898c..1452b75 100644 --- a/srcs/httpserver/src/haywire/khash.h +++ b/srcs/httpserver/src/haywire/khash.h @@ -413,9 +413,11 @@ static const double __ac_HASH_UPPER = 0.77; */ static kh_inline khint_t __ac_X31_hash_string(const char *s) { khint_t h = (khint_t)*s; - if (h) - for (++s; *s; ++s) + if (h) { + for (++s; *s; ++s) { h = (h << 5) - h + (khint_t)*s; + } + } return h; } /*! @function diff --git a/srcs/httpserver/src/haywire/picohttpparser.c b/srcs/httpserver/src/haywire/picohttpparser.c index 5dd378f..3b54c6e 100644 --- a/srcs/httpserver/src/haywire/picohttpparser.c +++ b/srcs/httpserver/src/haywire/picohttpparser.c @@ -141,8 +141,9 @@ static const char *get_token_to_eol(const char *buf, const char *buf_end, const ; int found; buf = findchar_fast(buf, buf_end, ranges1, sizeof(ranges1) - 1, &found); - if (found) + if (found) { goto FOUND_CTL; + } #else /* find non-printable char within the next 8 bytes, this is the hottest code; manually inlined */ while (likely(buf_end - buf >= 8)) { @@ -483,8 +484,9 @@ ssize_t phr_decode_chunked(struct phr_chunked_decoder *decoder, char *buf, size_ case CHUNKED_IN_CHUNK_SIZE: for (;; ++src) { int v; - if (src == bufsz) + if (src == bufsz) { goto Exit; + } if ((v = decode_hex(buf[src])) == -1) { if (decoder->_hex_count == 0) { ret = -1; @@ -505,10 +507,12 @@ ssize_t phr_decode_chunked(struct phr_chunked_decoder *decoder, char *buf, size_ case CHUNKED_IN_CHUNK_EXT: /* RFC 7230 A.2 "Line folding in chunk extensions is disallowed" */ for (;; ++src) { - if (src == bufsz) + if (src == bufsz) { goto Exit; - if (buf[src] == '\012') + } + if (buf[src] == '\012') { break; + } } ++src; if (decoder->bytes_left_in_chunk == 0) { @@ -524,15 +528,17 @@ ssize_t phr_decode_chunked(struct phr_chunked_decoder *decoder, char *buf, size_ case CHUNKED_IN_CHUNK_DATA: { size_t avail = bufsz - src; if (avail < decoder->bytes_left_in_chunk) { - if (dst != src) + if (dst != src) { memmove(buf + dst, buf + src, avail); + } src += avail; dst += avail; decoder->bytes_left_in_chunk -= avail; goto Exit; } - if (dst != src) + if (dst != src) { memmove(buf + dst, buf + src, decoder->bytes_left_in_chunk); + } src += decoder->bytes_left_in_chunk; dst += decoder->bytes_left_in_chunk; decoder->bytes_left_in_chunk = 0; @@ -541,10 +547,12 @@ ssize_t phr_decode_chunked(struct phr_chunked_decoder *decoder, char *buf, size_ /* fallthru */ case CHUNKED_IN_CHUNK_CRLF: for (;; ++src) { - if (src == bufsz) + if (src == bufsz) { goto Exit; - if (buf[src] != '\015') + } + if (buf[src] != '\015') { break; + } } if (buf[src] != '\012') { ret = -1; @@ -555,21 +563,26 @@ ssize_t phr_decode_chunked(struct phr_chunked_decoder *decoder, char *buf, size_ break; case CHUNKED_IN_TRAILERS_LINE_HEAD: for (;; ++src) { - if (src == bufsz) + if (src == bufsz) { goto Exit; - if (buf[src] != '\015') + } + if (buf[src] != '\015') { break; + } } - if (buf[src++] == '\012') + if (buf[src++] == '\012') { goto Complete; + } decoder->_state = CHUNKED_IN_TRAILERS_LINE_MIDDLE; /* fallthru */ case CHUNKED_IN_TRAILERS_LINE_MIDDLE: for (;; ++src) { - if (src == bufsz) + if (src == bufsz) { goto Exit; - if (buf[src] == '\012') + } + if (buf[src] == '\012') { break; + } } ++src; decoder->_state = CHUNKED_IN_TRAILERS_LINE_HEAD; @@ -582,8 +595,9 @@ ssize_t phr_decode_chunked(struct phr_chunked_decoder *decoder, char *buf, size_ Complete: ret = bufsz - src; Exit: - if (dst != src) + if (dst != src) { memmove(buf + dst, buf + src, bufsz - src); + } *_bufsz = dst; return ret; } diff --git a/srcs/libs/init/init.c b/srcs/libs/init/init.c index 332f0a3..04f23da 100644 --- a/srcs/libs/init/init.c +++ b/srcs/libs/init/init.c @@ -125,7 +125,6 @@ int user_init(const char *pAppCfgFile, const char *pCfgDirectory, const char *pK void user_uninit() { task_manager_exit(); free_http_server(); - uv_sleep(1000); mq_uninit(); zlog_fini(); uv_loop_close(get_task_manager()); diff --git a/srcs/open_dhcp/opendhcpd.cpp b/srcs/open_dhcp/opendhcpd.cpp index 56ffd7e..d4f8ca2 100644 --- a/srcs/open_dhcp/opendhcpd.cpp +++ b/srcs/open_dhcp/opendhcpd.cpp @@ -343,16 +343,19 @@ int dhcpd_main(int daemon, const char *pInifile, const char *pStatusFile, const tv.tv_sec = 20; tv.tv_usec = 0; - if (network.httpConn.ready) + if (network.httpConn.ready) { FD_SET(network.httpConn.sock, &readfds); + } - for (int i = 0; i < MAX_SERVERS && network.dhcpConn[i].ready; i++) + for (int i = 0; i < MAX_SERVERS && network.dhcpConn[i].ready; i++) { FD_SET(network.dhcpConn[i].sock, &readfds); + } FD_SET(network.dhcpListener.sock, &readfds); - if (cfig.replication) + if (cfig.replication) { FD_SET(cfig.dhcpReplConn.sock, &readfds); + } //printf("%i\n",select(network.maxFD, &readfds, nullptr, nullptr, &tv)); if (select(network.maxFD, &readfds, nullptr, nullptr, &tv)) { @@ -371,8 +374,9 @@ int dhcpd_main(int daemon, const char *pInifile, const char *pStatusFile, const sprintf(logBuff, "Accept Failed, Error: %s", strerror(errno)); logDHCPMess(logBuff, 1); free(req); - } else + } else { procHTTP(req); + } } else { sprintf(logBuff, "Memory Error"); logDHCPMess(logBuff, 0); @@ -380,12 +384,14 @@ int dhcpd_main(int daemon, const char *pInifile, const char *pStatusFile, const } for (int i = 0; i < MAX_SERVERS && network.dhcpConn[i].ready; i++) { - if (FD_ISSET(network.dhcpConn[i].sock, &readfds) && gdmess(&dhcpr, i) && sdmess(&dhcpr)) + if (FD_ISSET(network.dhcpConn[i].sock, &readfds) && gdmess(&dhcpr, i) && sdmess(&dhcpr)) { alad(&dhcpr); + } } - if (FD_ISSET(network.dhcpListener.sock, &readfds) && gdmess(&dhcpr, 255) && sdmess(&dhcpr)) + if (FD_ISSET(network.dhcpListener.sock, &readfds) && gdmess(&dhcpr, 255) && sdmess(&dhcpr)) { alad(&dhcpr); + } if (cfig.dhcpReplConn.ready && FD_ISSET(cfig.dhcpReplConn.sock, &readfds)) { errno = 0; @@ -399,11 +405,13 @@ int dhcpd_main(int daemon, const char *pInifile, const char *pStatusFile, const //errno = WSAGetLastError(); - if (errno || dhcpr.bytes <= 0) + if (errno || dhcpr.bytes <= 0) { cfig.dhcpRepl = 0; + } } - } else + } else { t = time(nullptr); + } } } else { if (logBuff[0]) { @@ -483,16 +491,19 @@ int dhcpd_main(int daemon, const char *pInifile, const char *pStatusFile, const tv.tv_sec = 20; tv.tv_usec = 0; - if (network.httpConn.ready) + if (network.httpConn.ready) { FD_SET(network.httpConn.sock, &readfds); + } - for (int i = 0; i < MAX_SERVERS && network.dhcpConn[i].ready; i++) + for (int i = 0; i < MAX_SERVERS && network.dhcpConn[i].ready; i++) { FD_SET(network.dhcpConn[i].sock, &readfds); + } FD_SET(network.dhcpListener.sock, &readfds); - if (cfig.replication) + if (cfig.replication) { FD_SET(cfig.dhcpReplConn.sock, &readfds); + } //printf("%i\n",select(network.maxFD, &readfds, nullptr, nullptr, &tv)); if (select(network.maxFD, &readfds, nullptr, nullptr, &tv)) { @@ -511,8 +522,9 @@ int dhcpd_main(int daemon, const char *pInifile, const char *pStatusFile, const sprintf(logBuff, "Accept Failed, Error: %s", strerror(errno)); logDHCPMess(logBuff, 1); free(req); - } else + } else { procHTTP(req); + } } else { sprintf(logBuff, "Memory Error"); logDHCPMess(logBuff, 0); @@ -520,12 +532,16 @@ int dhcpd_main(int daemon, const char *pInifile, const char *pStatusFile, const } for (int i = 0; i < MAX_SERVERS && network.dhcpConn[i].ready; i++) { - if (FD_ISSET(network.dhcpConn[i].sock, &readfds) && gdmess(&dhcpr_local, i) && sdmess(&dhcpr_local)) + if (FD_ISSET(network.dhcpConn[i].sock, &readfds) && gdmess(&dhcpr_local, i) + && sdmess(&dhcpr_local)) { alad(&dhcpr_local); + } } - if (FD_ISSET(network.dhcpListener.sock, &readfds) && gdmess(&dhcpr_local, 255) && sdmess(&dhcpr_local)) + if (FD_ISSET(network.dhcpListener.sock, &readfds) && gdmess(&dhcpr_local, 255) + && sdmess(&dhcpr_local)) { alad(&dhcpr_local); + } if (cfig.dhcpReplConn.ready && FD_ISSET(cfig.dhcpReplConn.sock, &readfds)) { errno = 0; @@ -539,32 +555,38 @@ int dhcpd_main(int daemon, const char *pInifile, const char *pStatusFile, const //errno = WSAGetLastError(); - if (errno || dhcpr_local.bytes <= 0) + if (errno || dhcpr_local.bytes <= 0) { cfig.dhcpRepl = 0; + } } - } else + } else { t = time(nullptr); + } } } closeConn(); - if (cfig.dhcpReplConn.ready) + if (cfig.dhcpReplConn.ready) { close(cfig.dhcpReplConn.sock); + } return 0; } void closeConn() { - if (network.dhcpListener.ready) + if (network.dhcpListener.ready) { close(network.dhcpListener.sock); + } - if (network.httpConn.ready) + if (network.httpConn.ready) { close(network.httpConn.sock); + } for (int i = 0; i < MAX_SERVERS && network.dhcpConn[i].loaded; i++) { - if (network.dhcpConn[i].ready) + if (network.dhcpConn[i].ready) { close(network.dhcpConn[i].sock); + } } } @@ -581,8 +603,9 @@ void on_system_exit(void *p) { closeConn(); - if (cfig.dhcpReplConn.ready) + if (cfig.dhcpReplConn.ready) { close(cfig.dhcpReplConn.sock); + } close(cfig.fixedSocket); @@ -764,12 +787,13 @@ void prepareUserHtmlRespStatus(data19 *req) { //char *contentStart = fp; fp += sprintf(fp, htmlStart, htmlTitle); - if (cfig.replication == 1) + if (cfig.replication == 1) { fp += sprintf(fp, bodyStart, sVersion, cfig.servername, "(Primary)"); - else if (cfig.replication == 2) + } else if (cfig.replication == 2) { fp += sprintf(fp, bodyStart, sVersion, cfig.servername, "(Secondary)"); - else + } else { fp += sprintf(fp, bodyStart, sVersion, cfig.servername, ""); + } fp += sprintf(fp, "\n\n"); @@ -791,9 +815,9 @@ void prepareUserHtmlRespStatus(data19 *req) { fp += sprintf(fp, td200, dhcpEntry->mapname);// mapname is MAC address fp += sprintf(fp, td200, IP2String(tempbuff, dhcpEntry->ip)); - if (dhcpEntry->expiry >= MY_MAX_TIME) + if (dhcpEntry->expiry >= MY_MAX_TIME) { fp += sprintf(fp, td200, "Infinity"); - else { + } else { tm *ttm = localtime(&dhcpEntry->expiry); strftime(tempbuff, sizeof(tempbuff), "%d-%b-%y %X", ttm); fp += sprintf(fp, tdnowrap200, tempbuff); @@ -803,18 +827,20 @@ void prepareUserHtmlRespStatus(data19 *req) { strcpy(tempbuff, dhcpEntry->hostname); tempbuff[20] = 0; fp += sprintf(fp, td200, tempbuff); - } else + } else { fp += sprintf(fp, td200, " "); + } if (cfig.dhcpRepl > t) { - if (dhcpEntry->local && cfig.replication == 1) + if (dhcpEntry->local && cfig.replication == 1) { fp += sprintf(fp, td200, "Primary"); - else if (dhcpEntry->local && cfig.replication == 2) + } else if (dhcpEntry->local && cfig.replication == 2) { fp += sprintf(fp, td200, "Secondary"); - else if (cfig.replication == 1) + } else if (cfig.replication == 1) { fp += sprintf(fp, td200, "Secondary"); - else + } else { fp += sprintf(fp, td200, "Primary"); + } } fp += sprintf(fp, "\n"); @@ -834,10 +860,11 @@ void prepareUserHtmlRespStatus(data19 *req) { for (MYDWORD ip = cfig.dhcpRanges[rangeInd].rangeStart; ip <= cfig.dhcpRanges[rangeInd].rangeEnd; ip++, ind++) { if (cfig.dhcpRanges[rangeInd].expiry[ind] < MY_MAX_TIME) { - if (cfig.dhcpRanges[rangeInd].expiry[ind] < t) + if (cfig.dhcpRanges[rangeInd].expiry[ind] < t) { ipfree++; - else + } else { ipused++; + } } } @@ -877,8 +904,9 @@ void prepareUserHtmlRespStatus(data19 *req) { } } - if (colNum) + if (colNum) { fp += sprintf(fp, "\n"); + } fp += sprintf(fp, "
\n\n"); //MYBYTE x = sprintf(tempbuff, "%u", (fp - contentStart)); @@ -1113,13 +1141,15 @@ void *sendHTTP(void *lpParam) { //errno = WSAGetLastError(); - if (errno || sent < 0) + if (errno || sent < 0) { break; + } dp += sent; req->bytes -= sent; - } else + } else { break; + } } //Sleep(10*1000); //shutdown(req->sock, 2); @@ -1132,22 +1162,27 @@ void *sendHTTP(void *lpParam) { bool checkRange(data17 *rangeData, char rangeInd) { //debug("checkRange"); - if (!cfig.hasFilter) + if (!cfig.hasFilter) { return true; + } MYBYTE rangeSetInd = cfig.dhcpRanges[rangeInd].rangeSetInd; data14 *rangeSet = &cfig.rangeSet[rangeSetInd]; //printf("checkRange entering, rangeInd=%i rangeSetInd=%i\n", rangeInd, rangeSetInd); //printf("checkRange entered, macFound=%i vendFound=%i userFound=%i\n", macFound, vendFound, userFound); - if ((!rangeData->macFound && !rangeSet->macSize[0]) || (rangeData->macFound && rangeData->macArray[rangeSetInd])) + if ((!rangeData->macFound && !rangeSet->macSize[0]) || (rangeData->macFound && rangeData->macArray[rangeSetInd])) { if ((!rangeData->vendFound && !rangeSet->vendClassSize[0]) - || (rangeData->vendFound && rangeData->vendArray[rangeSetInd])) + || (rangeData->vendFound && rangeData->vendArray[rangeSetInd])) { if ((!rangeData->userFound && !rangeSet->userClassSize[0]) - || (rangeData->userFound && rangeData->userArray[rangeSetInd])) + || (rangeData->userFound && rangeData->userArray[rangeSetInd])) { if ((!rangeData->subnetFound && !rangeSet->subnetIP[0]) - || (rangeData->subnetFound && rangeData->subnetArray[rangeSetInd])) + || (rangeData->subnetFound && rangeData->subnetArray[rangeSetInd])) { return true; + } + } + } + } //printf("checkRange, returning false rangeInd=%i rangeSetInd=%i\n", rangeInd, rangeSetInd); return false; @@ -1159,18 +1194,21 @@ bool checkIP(data9 *req, data17 *rangeData, MYDWORD ip) { char rangeInd = (char)(getRangeInd(ip) & 0xFF); - if (rangeInd < 0) + if (rangeInd < 0) { return false; + } - if (!checkRange(rangeData, rangeInd)) + if (!checkRange(rangeData, rangeInd)) { return false; + } MYWORD ind = getIndex(rangeInd, ip); data13 *range = &cfig.dhcpRanges[rangeInd]; data7 *dhcpEntry = range->dhcpEntry[ind]; - if ((req->dhcpEntry != dhcpEntry && range->expiry[ind] > t) or range->expiry[ind] >= MY_MAX_TIME) + if ((req->dhcpEntry != dhcpEntry && range->expiry[ind] > t) or range->expiry[ind] >= MY_MAX_TIME) { return false; + } if (req->subnetIP) { if (cfig.rangeSet[range->rangeSetInd].subnetIP[0]) { @@ -1179,26 +1217,32 @@ bool checkIP(data9 *req, data17 *rangeData, MYDWORD ip) { } else { calcRangeLimits(req->subnetIP, range->mask, &rangeStart, &rangeEnd); - if (rangeStart < range->rangeStart) + if (rangeStart < range->rangeStart) { rangeStart = range->rangeStart; + } - if (rangeEnd > range->rangeEnd) + if (rangeEnd > range->rangeEnd) { rangeEnd = range->rangeEnd; + } } - if (htonl(ip) >= rangeStart && htonl(ip) <= rangeEnd) + if (htonl(ip) >= rangeStart && htonl(ip) <= rangeEnd) { return true; + } } else { calcRangeLimits(network.dhcpConn[req->sockInd].server, range->mask, &rangeStart, &rangeEnd); - if (rangeStart < range->rangeStart) + if (rangeStart < range->rangeStart) { rangeStart = range->rangeStart; + } - if (rangeEnd > range->rangeEnd) + if (rangeEnd > range->rangeEnd) { rangeEnd = range->rangeEnd; + } - if (htonl(ip) >= rangeStart && htonl(ip) <= rangeEnd) + if (htonl(ip) >= rangeStart && htonl(ip) <= rangeEnd) { return true; + } } return false; } @@ -1289,30 +1333,35 @@ MYDWORD resad(data9 *req) { // printArray("vendArray", (char*)cfig.vendArray); // printArray("userArray", (char*)cfig.userArray); - if (!iipNew && req->reqIP && checkIP(req, &rangeData, req->reqIP)) + if (!iipNew && req->reqIP && checkIP(req, &rangeData, req->reqIP)) { iipNew = ntohl(req->reqIP); + } - if (!iipNew && req->dhcpEntry && req->dhcpEntry->ip && checkIP(req, &rangeData, req->dhcpEntry->ip)) + if (!iipNew && req->dhcpEntry && req->dhcpEntry->ip && checkIP(req, &rangeData, req->dhcpEntry->ip)) { iipNew = ntohl(req->dhcpEntry->ip); + } for (char k = 0; !iipNew && k < cfig.rangeCount; k++) { data13 *range = &cfig.dhcpRanges[k]; if (checkRange(&rangeData, k)) { if (!cfig.rangeSet[range->rangeSetInd].subnetIP[0]) { - if (req->subnetIP) + if (req->subnetIP) { calcRangeLimits(req->subnetIP, range->mask, &rangeStart, &rangeEnd); - else + } else { calcRangeLimits(network.dhcpConn[req->sockInd].server, network.dhcpConn[req->sockInd].mask, &rangeStart, &rangeEnd); + } - if (rangeStart < range->rangeStart) + if (rangeStart < range->rangeStart) { rangeStart = range->rangeStart; + } - if (rangeEnd > range->rangeEnd) + if (rangeEnd > range->rangeEnd) { rangeEnd = range->rangeEnd; + } } else { rangeStart = range->rangeStart; rangeEnd = range->rangeEnd; @@ -1356,8 +1405,9 @@ MYDWORD resad(data9 *req) { //sprintf(logBuff, "New=%u Old=%u", iipNew, iipExp); //logMess(logBuff, 1); - if (!iipNew && iipExp) + if (!iipNew && iipExp) { iipNew = iipExp; + } if (iipNew) { if (!req->dhcpEntry) { @@ -1366,8 +1416,9 @@ MYDWORD resad(data9 *req) { lump.hostname = req->hostname; req->dhcpEntry = createCache(&lump); - if (!req->dhcpEntry) + if (!req->dhcpEntry) { return 0; + } dhcpCache[req->dhcpEntry->mapname] = req->dhcpEntry; } @@ -1381,31 +1432,33 @@ MYDWORD resad(data9 *req) { if (verbatim || cfig.dhcpLogLevel) { if (rangeFound) { - if (req->dhcpp.header.bp_giaddr) + if (req->dhcpp.header.bp_giaddr) { sprintf(logBuff, "No free leases for DHCPDISCOVER for %s (%s) from RelayAgent %s", req->chaddr, req->hostname, IP2String(tempbuff, req->dhcpp.header.bp_giaddr)); - else + } else { sprintf(logBuff, "No free leases for DHCPDISCOVER for %s (%s) from interface %s", req->chaddr, req->hostname, IP2String(tempbuff, network.dhcpConn[req->sockInd].server)); + } } else { - if (req->dhcpp.header.bp_giaddr) + if (req->dhcpp.header.bp_giaddr) { sprintf(logBuff, "No Matching DHCP Range for DHCPDISCOVER for %s (%s) from RelayAgent %s", req->chaddr, req->hostname, IP2String(tempbuff, req->dhcpp.header.bp_giaddr)); - else + } else { sprintf(logBuff, "No Matching DHCP Range for DHCPDISCOVER for %s (%s) from interface %s", req->chaddr, req->hostname, IP2String(tempbuff, network.dhcpConn[req->sockInd].server)); + } } logDHCPMess(logBuff, 1); } @@ -1419,15 +1472,17 @@ MYDWORD chkaddr(data9 *req) { req->dhcpEntry = findDHCPEntry(req->chaddr); - if (!req->dhcpEntry || !req->dhcpEntry->ip) + if (!req->dhcpEntry || !req->dhcpEntry->ip) { return 0; + } //debug(req->dhcpEntry->subnetFlg); req->dhcpEntry->rangeInd = getRangeInd(req->dhcpEntry->ip); - if (req->dhcpEntry->fixed) + if (req->dhcpEntry->fixed) { return req->dhcpEntry->ip; + } MYDWORD rangeStart = 0; MYDWORD rangeEnd = 0; @@ -1492,44 +1547,55 @@ MYDWORD chkaddr(data9 *req) { rangeOK = false; if ((!rangeData.macFound && !rangeSet->macSize[0]) - || (rangeData.macFound && rangeData.macArray[rangeSetInd])) + || (rangeData.macFound && rangeData.macArray[rangeSetInd])) { if ((!rangeData.vendFound && !rangeSet->vendClassSize[0]) - || (rangeData.vendFound && rangeData.vendArray[rangeSetInd])) + || (rangeData.vendFound && rangeData.vendArray[rangeSetInd])) { if ((!rangeData.userFound && !rangeSet->userClassSize[0]) - || (rangeData.userFound && rangeData.userArray[rangeSetInd])) + || (rangeData.userFound && rangeData.userArray[rangeSetInd])) { rangeOK = true; + } + } + } } if (range->dhcpEntry[ind] == req->dhcpEntry && rangeOK) { if (rangeData.subnetFound) { - if (rangeData.subnetArray[range->rangeSetInd]) + if (rangeData.subnetArray[range->rangeSetInd]) { return req->dhcpEntry->ip; - else + } else { return 0; + } } else if (req->subnetIP) { calcRangeLimits(req->subnetIP, range->mask, &rangeStart, &rangeEnd); - if (rangeStart < range->rangeStart) + if (rangeStart < range->rangeStart) { rangeStart = range->rangeStart; + } - if (rangeEnd > range->rangeEnd) + if (rangeEnd > range->rangeEnd) { rangeEnd = range->rangeEnd; + } - if (htonl(req->dhcpEntry->ip) >= rangeStart && htonl(req->dhcpEntry->ip) <= rangeEnd) + if (htonl(req->dhcpEntry->ip) >= rangeStart && htonl(req->dhcpEntry->ip) <= rangeEnd) { return req->dhcpEntry->ip; + } } else if (!req->dhcpEntry->subnetFlg && !cfig.rangeSet[range->rangeSetInd].subnetIP[0]) { calcRangeLimits(network.dhcpConn[req->sockInd].server, range->mask, &rangeStart, &rangeEnd); - if (rangeStart < range->rangeStart) + if (rangeStart < range->rangeStart) { rangeStart = range->rangeStart; + } - if (rangeEnd > range->rangeEnd) + if (rangeEnd > range->rangeEnd) { rangeEnd = range->rangeEnd; + } - if (htonl(req->dhcpEntry->ip) >= rangeStart && htonl(req->dhcpEntry->ip) <= rangeEnd) + if (htonl(req->dhcpEntry->ip) >= rangeStart && htonl(req->dhcpEntry->ip) <= rangeEnd) { return req->dhcpEntry->ip; - } else if (req->dhcpEntry->subnetFlg) + } + } else if (req->dhcpEntry->subnetFlg) { return req->dhcpEntry->ip; + } } } @@ -1545,9 +1611,9 @@ MYDWORD sdmess(data9 *req) { if (req->req_type == DHCP_MESS_NONE) { req->dhcpp.header.bp_yiaddr = chkaddr(req); - if (req->dhcpp.header.bp_yiaddr && req->dhcpEntry && req->dhcpEntry->fixed) + if (req->dhcpp.header.bp_yiaddr && req->dhcpEntry && req->dhcpEntry->fixed) { req->lease = UINT_MAX; - else { + } else { if (verbatim || cfig.dhcpLogLevel) { sprintf(logBuff, "No Static Entry found for BOOTPREQUEST from Host %s", req->chaddr); logDHCPMess(logBuff, 1); @@ -1605,8 +1671,9 @@ MYDWORD sdmess(data9 *req) { //printf("IP=%s bytes=%u replication=%i\n", IP2String(tempbuff, req->remote.sin_addr.s_addr), req->bytes, cfig.replication); if ((cfig.replication == 1 && req->remote.sin_addr.s_addr == cfig.zoneServers[1]) - || (cfig.replication == 2 && req->remote.sin_addr.s_addr == cfig.zoneServers[0])) + || (cfig.replication == 2 && req->remote.sin_addr.s_addr == cfig.zoneServers[0])) { recvRepl(req); + } return 0; } @@ -1614,8 +1681,9 @@ MYDWORD sdmess(data9 *req) { else if (req->req_type == DHCP_MESS_DISCOVER) { req->dhcpp.header.bp_yiaddr = resad(req); - if (!req->dhcpp.header.bp_yiaddr) + if (!req->dhcpp.header.bp_yiaddr) { return 0; + } req->resp_type = DHCP_MESS_OFFER; } else if (req->req_type == DHCP_MESS_REQUEST) { @@ -1643,8 +1711,9 @@ MYDWORD sdmess(data9 *req) { logDHCPMess(logBuff, 1); } } - } else + } else { return 0; + } } else if (req->dhcpp.header.bp_ciaddr && req->dhcpp.header.bp_ciaddr == chkaddr(req) && req->dhcpEntry->expiry > t) { req->resp_type = DHCP_MESS_ACK; @@ -1662,15 +1731,17 @@ MYDWORD sdmess(data9 *req) { logDHCPMess(logBuff, 1); } } - } else + } else { return 0; + } addOptions(req); int packSize = (int)(req->vp - (MYBYTE *)&req->dhcpp); packSize++; - if (req->req_type == DHCP_MESS_NONE) + if (req->req_type == DHCP_MESS_NONE) { packSize = req->messsize; + } if (req->subnetIP && req->dhcpEntry && req->dhcpEntry->rangeInd >= 0) { MYBYTE rangeSetInd = cfig.dhcpRanges[req->dhcpEntry->rangeInd].rangeSetInd; @@ -1707,8 +1778,9 @@ MYDWORD sdmess(data9 *req) { network.dhcpConn[req->sockInd].sock, req->raw, packSize, 0, (sockaddr *)&cliAddr, sizeof(req->remote)); } - if (errno || req->bytes <= 0) + if (errno || req->bytes <= 0) { return 0; + } //printf("goes=%s %i\n",IP2String(tempbuff, req->dhcpp.header.bp_yiaddr),req->sockInd); return req->dhcpp.header.bp_yiaddr; @@ -1723,8 +1795,9 @@ MYDWORD alad(data9 *req) { if (req->dhcpEntry && (req->req_type == DHCP_MESS_NONE || req->resp_type == DHCP_MESS_ACK)) { MYDWORD hangTime = req->lease; - if (req->rebind > req->lease) + if (req->rebind > req->lease) { hangTime = req->rebind; + } req->dhcpEntry->display = true; req->dhcpEntry->local = true; @@ -1762,8 +1835,9 @@ MYDWORD alad(data9 *req) { logDHCPMess(logBuff, 1); } - if (cfig.replication && cfig.dhcpRepl > t) + if (cfig.replication && cfig.dhcpRepl > t) { sendRepl(req); + } return req->dhcpEntry->ip; } else if ((verbatim || cfig.dhcpLogLevel >= 2) && req->resp_type == DHCP_MESS_OFFER) { @@ -1877,10 +1951,11 @@ void addOptions(data9 *req) { op.opt_code = DHCP_OPTION_NETMASK; op.size = 4; - if (req->dhcpEntry->rangeInd >= 0) + if (req->dhcpEntry->rangeInd >= 0) { pIP(op.value, cfig.dhcpRanges[req->dhcpEntry->rangeInd].mask); - else + } else { pIP(op.value, cfig.mask); + } pvdata(req, &op); } @@ -1897,18 +1972,20 @@ void addOptions(data9 *req) { if (req->clientId.opt_code == DHCP_OPTION_CLIENTID) pvdata(req, &req->clientId); */ - if (req->subnet.opt_code == DHCP_OPTION_SUBNETSELECTION) + if (req->subnet.opt_code == DHCP_OPTION_SUBNETSELECTION) { pvdata(req, &req->subnet); + } - if (req->agentOption.opt_code == DHCP_OPTION_RELAYAGENTINFO) + if (req->agentOption.opt_code == DHCP_OPTION_RELAYAGENTINFO) { pvdata(req, &req->agentOption); + } } - if (req->hostname[0]) + if (req->hostname[0]) { strcpy(req->dhcpEntry->hostname, req->hostname); - else if (req->dhcpEntry->hostname[0]) + } else if (req->dhcpEntry->hostname[0]) { strcpy(req->hostname, req->dhcpEntry->hostname); - else { + } else { genHostName(req->hostname, req->dhcpp.header.bp_chaddr, req->dhcpp.header.bp_hlen); strcpy(req->dhcpEntry->hostname, req->hostname); } @@ -1922,29 +1999,33 @@ void pvdata(data9 *req, data3 *op) { MYBYTE opsize = op->size; if (!req->opAdded[op->opt_code] && ((req->vp - (MYBYTE *)&req->dhcpp) + opsize < req->messsize)) { - if (op->opt_code == DHCP_OPTION_NEXTSERVER) + if (op->opt_code == DHCP_OPTION_NEXTSERVER) { req->dhcpp.header.bp_siaddr = fIP(op->value); - else if (op->opt_code == DHCP_OPTION_BP_FILE) { - if (opsize <= 128) + } else if (op->opt_code == DHCP_OPTION_BP_FILE) { + if (opsize <= 128) { memcpy(req->dhcpp.header.bp_file, op->value, opsize); + } } else if (opsize) { if (op->opt_code == DHCP_OPTION_IPADDRLEASE) { - if (!req->lease || req->lease > fUInt(op->value)) + if (!req->lease || req->lease > fUInt(op->value)) { req->lease = fUInt(op->value); + } - if (req->lease >= MY_MAX_TIME) + if (req->lease >= MY_MAX_TIME) { req->lease = UINT_MAX; + } pUInt(op->value, req->lease); - } else if (op->opt_code == DHCP_OPTION_REBINDINGTIME) + } else if (op->opt_code == DHCP_OPTION_REBINDINGTIME) { req->rebind = fUInt(op->value); - else if (op->opt_code == DHCP_OPTION_HOSTNAME) { + } else if (op->opt_code == DHCP_OPTION_HOSTNAME) { memcpy(req->hostname, op->value, opsize); req->hostname[opsize] = 0; req->hostname[64] = 0; - if (char *ptr = strchr(req->hostname, '.')) + if (char *ptr = strchr(req->hostname, '.')) { *ptr = 0; + } opsize = strlen(req->hostname) + 1; memcpy(op->value, req->hostname, opsize); @@ -1970,10 +2051,11 @@ void setTempLease(data7 *dhcpEntry) { void setLeaseExpiry(data7 *dhcpEntry, MYDWORD lease) { //printf("%d=%d\n", t, lease); if (dhcpEntry && dhcpEntry->ip) { - if (lease > (MYDWORD)(MY_MAX_TIME - t)) + if (lease > (MYDWORD)(MY_MAX_TIME - t)) { dhcpEntry->expiry = MY_MAX_TIME; - else + } else { dhcpEntry->expiry = t + lease; + } setLeaseExpiry(dhcpEntry); } @@ -1986,8 +2068,9 @@ void setLeaseExpiry(data7 *dhcpEntry) { if (ind >= 0) { cfig.dhcpRanges[dhcpEntry->rangeInd].dhcpEntry[ind] = dhcpEntry; - if (cfig.dhcpRanges[dhcpEntry->rangeInd].expiry[ind] < MY_MAX_TIME) + if (cfig.dhcpRanges[dhcpEntry->rangeInd].expiry[ind] < MY_MAX_TIME) { cfig.dhcpRanges[dhcpEntry->rangeInd].expiry[ind] = dhcpEntry->expiry; + } } } } @@ -1999,8 +2082,9 @@ void lockIP(MYDWORD ip) { if (iip >= cfig.dhcpRanges[rangeInd].rangeStart && iip <= cfig.dhcpRanges[rangeInd].rangeEnd) { unsigned int ind = iip - cfig.dhcpRanges[rangeInd].rangeStart; - if (cfig.dhcpRanges[rangeInd].expiry[ind] < MY_MAX_TIME) + if (cfig.dhcpRanges[rangeInd].expiry[ind] < MY_MAX_TIME) { cfig.dhcpRanges[rangeInd].expiry[ind] = MY_MAX_TIME; + } break; } @@ -2100,26 +2184,28 @@ MYDWORD sendRepl(data9 *req) { cfig.dhcpRepl = 0; if (verbatim || cfig.dhcpLogLevel >= 1) { - if (cfig.replication == 1) + if (cfig.replication == 1) { sprintf(logBuff, "Error %s Sending DHCP Update to Secondary Server", strerror(errno)); - else + } else { sprintf(logBuff, "Error %s Sending DHCP Update to Primary Server", strerror(errno)); + } logDHCPMess(logBuff, 1); } return 0; } else if (verbatim || cfig.dhcpLogLevel >= 2) { - if (cfig.replication == 1) + if (cfig.replication == 1) { sprintf(logBuff, "DHCP Update for host %s (%s) sent to Secondary Server", req->dhcpEntry->mapname, IP2String(ipBuff, req->dhcpEntry->ip)); - else + } else { sprintf(logBuff, "DHCP Update for host %s (%s) sent to Primary Server", req->dhcpEntry->mapname, IP2String(ipBuff, req->dhcpEntry->ip)); + } logDHCPMess(logBuff, 2); } @@ -2188,22 +2274,24 @@ void recvRepl(data9 *req) { if (req->dhcpEntry && req->dhcpEntry->ip != ip) { if (req->dhcpEntry->fixed || rInd < 0) { - if (cfig.replication == 1) + if (cfig.replication == 1) { sprintf(logBuff, "DHCP Update ignored for %s (%s) from Secondary Server", req->chaddr, IP2String(ipbuff, ip)); - else + } else { sprintf( logBuff, "DHCP Update ignored for %s (%s) from Primary Server", req->chaddr, IP2String(ipbuff, ip)); + } logDHCPMess(logBuff, 1); return; } else if (req->dhcpEntry->rangeInd >= 0) { int ind = getIndex((char)req->dhcpEntry->rangeInd, req->dhcpEntry->ip); - if (ind >= 0) + if (ind >= 0) { cfig.dhcpRanges[req->dhcpEntry->rangeInd].dhcpEntry[ind] = nullptr; + } } } @@ -2213,8 +2301,9 @@ void recvRepl(data9 *req) { lump.hostname = req->hostname; req->dhcpEntry = createCache(&lump); - if (req->dhcpEntry) + if (req->dhcpEntry) { dhcpCache[req->dhcpEntry->mapname] = req->dhcpEntry; + } /* req->dhcpEntry = (data7*)calloc(1, sizeof(data7)); @@ -2246,10 +2335,12 @@ void recvRepl(data9 *req) { MYDWORD hangTime = req->lease; if (hangTime) { - if (req->rebind > hangTime) + if (req->rebind > hangTime) { hangTime = req->rebind; - } else + } + } else { hangTime = UINT_MAX; + } setLeaseExpiry(req->dhcpEntry, hangTime); strcpy(req->dhcpEntry->hostname, req->hostname); @@ -2262,25 +2353,27 @@ void recvRepl(data9 *req) { pthread_attr_destroy(&attr); if (verbatim || cfig.dhcpLogLevel >= 2) { - if (cfig.replication == 1) + if (cfig.replication == 1) { sprintf(logBuff, "DHCP Update received for %s (%s) from Secondary Server", req->chaddr, IP2String(ipbuff, ip)); - else + } else { sprintf(logBuff, "DHCP Update received for %s (%s) from Primary Server", req->chaddr, IP2String(ipbuff, ip)); + } logDHCPMess(logBuff, 2); } } else { - if (cfig.replication == 1) + if (cfig.replication == 1) { sprintf( logBuff, "DHCP Update ignored for %s (%s) from Secondary Server", req->chaddr, IP2String(ipbuff, ip)); - else + } else { sprintf(logBuff, "DHCP Update ignored for %s (%s) from Primary Server", req->chaddr, IP2String(ipbuff, ip)); + } logDHCPMess(logBuff, 1); return; @@ -2291,9 +2384,11 @@ int getRangeInd(MYDWORD ip) { if (ip) { MYDWORD iip = htonl(ip); - for (char k = 0; k < cfig.rangeCount; k++) - if (iip >= cfig.dhcpRanges[k].rangeStart && iip <= cfig.dhcpRanges[k].rangeEnd) + for (char k = 0; k < cfig.rangeCount; k++) { + if (iip >= cfig.dhcpRanges[k].rangeStart && iip <= cfig.dhcpRanges[k].rangeEnd) { return k; + } + } } return -1; } @@ -2301,8 +2396,9 @@ int getRangeInd(MYDWORD ip) { int getIndex(char rangeInd, MYDWORD ip) { if (ip && rangeInd >= 0 && rangeInd < cfig.rangeCount) { MYDWORD iip = htonl(ip); - if (iip >= cfig.dhcpRanges[rangeInd].rangeStart && iip <= cfig.dhcpRanges[rangeInd].rangeEnd) + if (iip >= cfig.dhcpRanges[rangeInd].rangeStart && iip <= cfig.dhcpRanges[rangeInd].rangeEnd) { return (int)(iip - cfig.dhcpRanges[rangeInd].rangeStart); + } } return -1; } @@ -2344,9 +2440,9 @@ void loadOptions(FILE *f, const char *sectionName, data20 *optionData) { } if (!strcasecmp(name, "DHCPRange")) { - if (!strcasecmp(sectionName, RANGESET)) + if (!strcasecmp(sectionName, RANGESET)) { addDHCPRange(value); - else { + } else { sprintf(logBuff, "Warning: section [%s] option %s not allowed in this section, option ignored", sectionName, @@ -2364,14 +2460,15 @@ void loadOptions(FILE *f, const char *sectionName, data20 *optionData) { } else if (!isIP(value) && strcasecmp(value, "0.0.0.0") != 0) { sprintf(logBuff, "Warning: section [%s] option Invalid IP Addr %s option ignored", sectionName, value); logDHCPMess(logBuff, 1); - } else + } else { optionData->ip = inet_addr(value); + } continue; } else if (!strcasecmp(name, "FilterMacRange")) { - if (!strcasecmp(sectionName, RANGESET)) + if (!strcasecmp(sectionName, RANGESET)) { addMacRange(optionData->rangeSetInd, value); - else { + } else { sprintf(logBuff, "Warning: section [%s] option %s not allowed in this section, option ignored", sectionName, @@ -2381,17 +2478,17 @@ void loadOptions(FILE *f, const char *sectionName, data20 *optionData) { continue; } - if (!value[0]) + if (!value[0]) { valType = 9; - else if (value[0] == '"' && value[strlen(value) - 1] == '"') { + } else if (value[0] == '"' && value[strlen(value) - 1] == '"') { valType = 2; value[0] = NBSP; value[strlen(value) - 1] = NBSP; myTrim(value, value); - if (strlen(value) <= UCHAR_MAX) + if (strlen(value) <= UCHAR_MAX) { valSize = strlen(value); - else { + } else { sprintf(logBuff, "Warning: section [%s] option %s value too big, option ignored", sectionName, raw); logDHCPMess(logBuff, 1); continue; @@ -2404,15 +2501,16 @@ void loadOptions(FILE *f, const char *sectionName, data20 *optionData) { if (errorPos) { valType = 1; valSize = strlen(value); - } else + } else { memcpy(value, hoption, valSize); - } else if (isInt(value) && strtol(value, nullptr, 10) > USHRT_MAX) + } + } else if (isInt(value) && strtol(value, nullptr, 10) > USHRT_MAX) { valType = 4; - else if (isInt(value) && STR2INT(value) > UCHAR_MAX) + } else if (isInt(value) && STR2INT(value) > UCHAR_MAX) { valType = 5; - else if (isInt(value)) + } else if (isInt(value)) { valType = 6; - else if (strchr(value, '.') || strchr(value, ',')) { + } else if (strchr(value, '.') || strchr(value, ',')) { valType = 2; char buff[1024]; int numbytes = myTokenize(buff, value, "/,.", true); @@ -2430,13 +2528,14 @@ void loadOptions(FILE *f, const char *sectionName, data20 *optionData) { if (isInt(ptr) && STR2INT(ptr) <= UCHAR_MAX) { hoption[valSize] = STR2INT(ptr); valSize++; - } else + } else { break; + } } - if (!(*ptr)) + if (!(*ptr)) { memcpy(value, hoption, valSize); - else { + } else { valType = 1; valSize = strlen(value); } @@ -2453,9 +2552,9 @@ void loadOptions(FILE *f, const char *sectionName, data20 *optionData) { } if (!strcasecmp(name, "FilterVendorClass")) { - if (!strcasecmp(sectionName, RANGESET)) + if (!strcasecmp(sectionName, RANGESET)) { addVendClass(optionData->rangeSetInd, value, valSize); - else { + } else { sprintf(logBuff, "Warning: section [%s] option %s not allowed in this section, option ignored", sectionName, @@ -2464,9 +2563,9 @@ void loadOptions(FILE *f, const char *sectionName, data20 *optionData) { } continue; } else if (!strcasecmp(name, "FilterUserClass")) { - if (!strcasecmp(sectionName, RANGESET)) + if (!strcasecmp(sectionName, RANGESET)) { addUserClass(optionData->rangeSetInd, value, valSize); - else { + } else { sprintf(logBuff, "Warning: section [%s] option %s not allowed in this section, option ignored", sectionName, @@ -2519,13 +2618,14 @@ void loadOptions(FILE *f, const char *sectionName, data20 *optionData) { opType = 0; } - for (MYBYTE i = 0; i < maxInd; i++) + for (MYBYTE i = 0; i < maxInd; i++) { if (!strcasecmp(name, opData[i].opName) || (opTag && opTag == opData[i].opTag)) { opTag = opData[i].opTag; opType = opData[i].opType; tagFound = true; break; } + } if (!opTag) { sprintf(logBuff, "Warning: section [%s] invalid option %s, ignored", sectionName, raw); @@ -2533,8 +2633,9 @@ void loadOptions(FILE *f, const char *sectionName, data20 *optionData) { continue; } - if (!opType) + if (!opType) { opType = valType; + } //sprintf(logBuff, "Tag %i ValType %i opType %i value=%s size=%u", opTag, valType, opType, value, valSize); //logDHCPMess(logBuff, 1); @@ -2622,8 +2723,9 @@ void loadOptions(FILE *f, const char *sectionName, data20 *optionData) { raw); logDHCPMess(logBuff, 1); continue; - } else + } else { optionData->mask = fIP(value); + } } if (buffsize > valSize + 2) { @@ -2651,11 +2753,11 @@ void loadOptions(FILE *f, const char *sectionName, data20 *optionData) { case 4: { MYDWORD j; - if (valType == 2 && valSize == 4) + if (valType == 2 && valSize == 4) { j = fUInt(value); - else if (valType >= 4 && valType <= 6) + } else if (valType >= 4 && valType <= 6) { j = strtol(value, nullptr, 10); - else { + } else { sprintf(logBuff, "Warning: section [%s] option %s, value should be integer between 0 & %u or 4 bytes, " "option ignored", @@ -2667,8 +2769,9 @@ void loadOptions(FILE *f, const char *sectionName, data20 *optionData) { } if (opTag == DHCP_OPTION_IPADDRLEASE) { - if (j == 0) + if (j == 0) { j = UINT_MAX; + } if (!strcasecmp(serviceName, "DUALServer")) { if (!strcasecmp(sectionName, GLOBALOPTIONS)) { @@ -2689,8 +2792,9 @@ void loadOptions(FILE *f, const char *sectionName, data20 *optionData) { logDHCPMess(logBuff, 1); continue; } - } else if (!strcasecmp(serviceName, "OpenDHCPServer") && !strcasecmp(sectionName, GLOBALOPTIONS)) + } else if (!strcasecmp(serviceName, "OpenDHCPServer") && !strcasecmp(sectionName, GLOBALOPTIONS)) { cfig.lease = j; + } } if (buffsize > 6) { @@ -2710,11 +2814,11 @@ void loadOptions(FILE *f, const char *sectionName, data20 *optionData) { case 5: { MYWORD j; - if (valType == 2 && valSize == 2) + if (valType == 2 && valSize == 2) { j = fUShort(value); - else if (valType == 5 || valType == 6) + } else if (valType == 5 || valType == 6) { j = strtol(value, nullptr, 10); - else { + } else { sprintf( logBuff, "Warning: section [%s] option %s, value should be between 0 & %u or 2 bytes, option ignored", @@ -2741,11 +2845,11 @@ void loadOptions(FILE *f, const char *sectionName, data20 *optionData) { case 6: { MYBYTE j; - if (valType == 2 && valSize == 1) + if (valType == 2 && valSize == 1) { j = *value; - else if (valType == 6) + } else if (valType == 6) { j = strtol(value, nullptr, 10); - else { + } else { sprintf(logBuff, "Warning: section [%s] option %s, value should be between 0 & %u or single byte, option " "ignored", @@ -2773,17 +2877,17 @@ void loadOptions(FILE *f, const char *sectionName, data20 *optionData) { case 7: { MYBYTE j; - if (valType == 2 && valSize == 1 && *value < 2) + if (valType == 2 && valSize == 1 && *value < 2) { j = *value; - else if (valType == 1 - && (!strcasecmp(value, "yes") || !strcasecmp(value, "on") || !strcasecmp(value, "true"))) + } else if (valType == 1 + && (!strcasecmp(value, "yes") || !strcasecmp(value, "on") || !strcasecmp(value, "true"))) { j = 1; - else if (valType == 1 - && (!strcasecmp(value, "no") || !strcasecmp(value, "off") || !strcasecmp(value, "false"))) + } else if (valType == 1 + && (!strcasecmp(value, "no") || !strcasecmp(value, "off") || !strcasecmp(value, "false"))) { j = 0; - else if (valType == 6 && STR2INT(value) < 2) + } else if (valType == 6 && STR2INT(value) < 2) { j = STR2INT(value); - else { + } else { sprintf(logBuff, "Warning: section [%s] option %s, value should be yes/on/true/1 or no/off/false/0, option " "ignored", @@ -2857,19 +2961,23 @@ void lockOptions(FILE *f) { while (readSection(raw, f)) { mySplit(name, value, raw, '='); - if (!name[0] || !value[0]) + if (!name[0] || !value[0]) { continue; + } int op_index; MYBYTE n = sizeof(opData) / sizeof(data4); - for (op_index = 0; op_index < n; op_index++) + for (op_index = 0; op_index < n; op_index++) { if (!strcasecmp(name, opData[op_index].opName) - || (opData[op_index].opTag && STR2INT(name) == opData[op_index].opTag)) + || (opData[op_index].opTag && STR2INT(name) == opData[op_index].opTag)) { break; + } + } - if (op_index >= n) + if (op_index >= n) { continue; + } if (opData[op_index].opType == 3) { if (myTokenize(value, value, "/,.", true)) { @@ -2881,21 +2989,25 @@ void lockOptions(FILE *f) { if (isInt(ptr) && STR2INT(ptr) <= UCHAR_MAX) { hoption[valueSize] = (char)STR2INT(ptr); valueSize++; - } else + } else { break; + } } - if (*ptr) + if (*ptr) { continue; + } - if (valueSize % 4) + if (valueSize % 4) { continue; + } for (MYBYTE i = 0; i < valueSize; i += 4) { MYDWORD ip = *((MYDWORD *)&(hoption[i])); - if (ip != INADDR_ANY && ip != INADDR_NONE) + if (ip != INADDR_ANY && ip != INADDR_NONE) { lockIP(ip); + } } } } @@ -2941,11 +3053,13 @@ void addDHCPRange(char *dp) { range->dhcpEntry = (data7 **)calloc((re - rs + 1), sizeof(struct data7 *)); if (!range->expiry || !range->dhcpEntry) { - if (range->expiry) + if (range->expiry) { free(range->expiry); + } - if (range->dhcpEntry) + if (range->dhcpEntry) { free(range->dhcpEntry); + } sprintf(logBuff, "DHCP Ranges Load, Memory Allocation Error"); logDHCPMess(logBuff, 1); @@ -2969,11 +3083,13 @@ void addVendClass(MYBYTE rangeSetInd, char *vendClass, MYBYTE vendClassSize) { MYBYTE i = 0; - for (; i <= MAX_RANGE_FILTERS && rangeSet->vendClassSize[i]; i++) + for (; i <= MAX_RANGE_FILTERS && rangeSet->vendClassSize[i]; i++) { ; + } - if (i >= MAX_RANGE_FILTERS || !vendClassSize) + if (i >= MAX_RANGE_FILTERS || !vendClassSize) { return; + } rangeSet->vendClass[i] = (MYBYTE *)calloc(vendClassSize, 1); @@ -2996,11 +3112,13 @@ void addUserClass(MYBYTE rangeSetInd, char *userClass, MYBYTE userClassSize) { MYBYTE i = 0; - for (; i <= MAX_RANGE_FILTERS && rangeSet->userClassSize[i]; i++) + for (; i <= MAX_RANGE_FILTERS && rangeSet->userClassSize[i]; i++) { ; + } - if (i >= MAX_RANGE_FILTERS || !userClassSize) + if (i >= MAX_RANGE_FILTERS || !userClassSize) { return; + } rangeSet->userClass[i] = (MYBYTE *)calloc(userClassSize, 1); @@ -3023,11 +3141,13 @@ void addMacRange(MYBYTE rangeSetInd, char *macRange) { MYBYTE i = 0; - for (; i <= MAX_RANGE_FILTERS && rangeSet->macSize[i]; i++) + for (; i <= MAX_RANGE_FILTERS && rangeSet->macSize[i]; i++) { ; + } - if (i >= MAX_RANGE_FILTERS) + if (i >= MAX_RANGE_FILTERS) { return; + } char name[256]; char value[256]; @@ -3097,8 +3217,9 @@ void loadDHCP() { cfig.mask = optionData.mask; } - if (!cfig.mask) + if (!cfig.mask) { cfig.mask = inet_addr("255.255.255.0"); + } for (MYBYTE i = 1; i <= MAX_RANGE_SETS; i++) { if ((f = openSection(RANGESET, i))) { @@ -3120,33 +3241,38 @@ void loadDHCP() { cfig.dhcpRanges[m].mask = optionData.mask; } cfig.rangeCount = (char)m; - } else + } else { break; + } } //printf("%s\n", IP2String(ipbuff, cfig.mask)); for (char rangeInd = 0; rangeInd < cfig.rangeCount; rangeInd++) { - if (!cfig.dhcpRanges[rangeInd].mask) + if (!cfig.dhcpRanges[rangeInd].mask) { cfig.dhcpRanges[rangeInd].mask = cfig.mask; + } for (MYDWORD iip = cfig.dhcpRanges[rangeInd].rangeStart; iip <= cfig.dhcpRanges[rangeInd].rangeEnd; iip++) { MYDWORD ip = htonl(iip); if ((cfig.dhcpRanges[rangeInd].mask | (~ip)) == UINT_MAX - || (cfig.dhcpRanges[rangeInd].mask | ip) == UINT_MAX) + || (cfig.dhcpRanges[rangeInd].mask | ip) == UINT_MAX) { cfig.dhcpRanges[rangeInd].expiry[iip - cfig.dhcpRanges[rangeInd].rangeStart] = MY_MAX_TIME; + } } } - if ((f = openSection(GLOBALOPTIONS, 1))) + if ((f = openSection(GLOBALOPTIONS, 1))) { lockOptions(f); + } for (MYBYTE i = 1; i <= MAX_RANGE_SETS; i++) { - if ((f = openSection(RANGESET, i))) + if ((f = openSection(RANGESET, i))) { lockOptions(f); - else + } else { break; + } } ff = fopen(iniFile, "rt"); @@ -3162,13 +3288,16 @@ void loadDHCP() { *secend = 0; sectionName[0] = NBSP; myTrim(sectionName, sectionName); - } else + } else { continue; - } else + } + } else { continue; + } - if (!strchr(sectionName, ':')) + if (!strchr(sectionName, ':')) { continue; + } //printf("%s\n", sectionName); @@ -3181,16 +3310,19 @@ void loadDHCP() { dhcpEntry = findDHCPEntry(hex2String(mapname, hexValue, hexValueSize)); if (!dhcpEntry) { - if ((f = openSection(sectionName, 1))) + if ((f = openSection(sectionName, 1))) { loadOptions(f, sectionName, &optionData); - if ((f = openSection(sectionName, 1))) + } + if ((f = openSection(sectionName, 1))) { lockOptions(f); + } auto p = dhcpCache.begin(); for (; p != dhcpCache.end(); p++) { - if (p->second && p->second->ip && p->second->ip == optionData.ip) + if (p->second && p->second->ip && p->second->ip == optionData.ip) { break; + } } if (p == dhcpCache.end()) { @@ -3200,8 +3332,9 @@ void loadDHCP() { lump.options = optionData.options; dhcpEntry = createCache(&lump); - if (!dhcpEntry) + if (!dhcpEntry) { return; + } dhcpEntry->ip = optionData.ip; dhcpEntry->rangeInd = getRangeInd(optionData.ip); @@ -3258,13 +3391,15 @@ void loadDHCP() { for (; p != dhcpCache.end(); p++) { dhcpEntry = p->second; - if (!dhcpEntry || (!strcasecmp(mapname, dhcpEntry->mapname) || dhcpEntry->ip == dhcpData.ip)) + if (!dhcpEntry || (!strcasecmp(mapname, dhcpEntry->mapname) || dhcpEntry->ip == dhcpData.ip)) { break; + } } if ((p != dhcpCache.end()) - && (strcasecmp(mapname, dhcpEntry->mapname) != 0 || dhcpEntry->ip != dhcpData.ip)) + && (strcasecmp(mapname, dhcpEntry->mapname) != 0 || dhcpEntry->ip != dhcpData.ip)) { continue; + } dhcpEntry = findDHCPEntry(mapname); rangeInd = getRangeInd(dhcpData.ip); @@ -3316,8 +3451,9 @@ void loadDHCP() { dhcpEntry->dhcpInd = cfig.dhcpInd; dhcpData.dhcpInd = dhcpEntry->dhcpInd; - if (fseek(f, (long)((dhcpData.dhcpInd - 1) * sizeof(data8)), SEEK_SET) >= 0) + if (fseek(f, (long)((dhcpData.dhcpInd - 1) * sizeof(data8)), SEEK_SET) >= 0) { fwrite(&dhcpData, sizeof(data8), 1, f); + } } } @@ -3403,16 +3539,17 @@ FILE *openSection(const char *sectionName, MYBYTE serial) { buff[0] = NBSP; myTrim(buff, buff); - if (strchr(buff, '\\') || strchr(buff, '/')) + if (strchr(buff, '\\') || strchr(buff, '/')) { strcpy(tempbuff, buff); - else + } else { sprintf(tempbuff, "%s%s", filePATH, buff); + } f = fopen(tempbuff, "rt"); - if (f) + if (f) { return f; - else { + } else { sprintf(logBuff, "Error: Section [%s], file %s not found", sectionName, tempbuff); logDHCPMess(logBuff, 1); return nullptr; @@ -3434,12 +3571,14 @@ char *readSection(char *buff, FILE *f) { while (fgets(buff, 511, f)) { myTrim(buff, buff); - if (*buff == '[') + if (*buff == '[') { break; + } if ((*buff) >= '0' && (*buff) <= '9' || (*buff) >= 'A' && (*buff) <= 'Z' || (*buff) >= 'a' && (*buff) <= 'z' - || ((*buff) && strchr("/\\?*", (*buff)))) + || ((*buff) && strchr("/\\?*", (*buff)))) { return buff; + } } fclose(f); @@ -3448,10 +3587,11 @@ char *readSection(char *buff, FILE *f) { char *myGetToken(char *buff, MYBYTE index) { while (*buff) { - if (index) + if (index) { index--; - else + } else { break; + } buff += strlen(buff) + 1; } @@ -3499,19 +3639,22 @@ MYWORD myTokenize(char *target, char *source, const char *sep, bool whiteSep) { } char *myTrim(char *target, char *source) { - while ((*source) && (*source) <= NBSP) + while ((*source) && (*source) <= NBSP) { source++; + } int i = 0; - for (; i < 511 && source[i]; i++) + for (; i < 511 && source[i]; i++) { target[i] = source[i]; + } target[i] = source[i]; i--; - for (; i >= 0 && target[i] <= NBSP; i--) + for (; i >= 0 && target[i] <= NBSP; i--) { target[i] = 0; + } return target; } @@ -3585,9 +3728,9 @@ char *IP2String(char *target, MYDWORD ip) { MYBYTE addServer(MYDWORD *array, MYBYTE maxServers, MYDWORD ip) { if (ip) { for (MYBYTE i = 0; i < maxServers; i++) { - if (array[i] == ip) + if (array[i] == ip) { return i; - else if (!array[i]) { + } else if (!array[i]) { array[i] = ip; return i; } @@ -3599,45 +3742,53 @@ MYBYTE addServer(MYDWORD *array, MYBYTE maxServers, MYDWORD ip) { MYDWORD *findServer(MYDWORD *array, MYBYTE kount, MYDWORD ip) { if (ip) { for (MYBYTE i = 0; i < kount && array[i]; i++) { - if (array[i] == ip) + if (array[i] == ip) { return &(array[i]); + } } } return nullptr; } bool isInt(char *str) { - if (!str || !(*str)) + if (!str || !(*str)) { return false; + } - for (; *str; str++) - if (*str < '0' || *str > '9') + for (; *str; str++) { + if (*str < '0' || *str > '9') { return false; + } + } return true; } bool isIP(char *str) { - if (!str || !(*str)) + if (!str || !(*str)) { return false; + } MYDWORD ip = inet_addr(str); int j = 0; for (; *str; str++) { - if (*str == '.' && *(str + 1) != '.') + if (*str == '.' && *(str + 1) != '.') { j++; - else if (*str < '0' || *str > '9') + } else if (*str < '0' || *str > '9') { return false; + } } if (j == 3) { - if (ip == INADDR_NONE || ip == INADDR_ANY) + if (ip == INADDR_NONE || ip == INADDR_ANY) { return false; - else + } else { return true; - } else + } + } else { return false; + } } /* @@ -3809,13 +3960,15 @@ MYBYTE fromUUE(MYBYTE *target, char *source) char *hex2String(char *target, const MYBYTE *hex, MYBYTE bytes) { char *dp = target; - if (bytes) + if (bytes) { dp += sprintf(target, "%02x", *hex); - else + } else { *target = 0; + } - for (MYBYTE i = 1; i < bytes; i++) + for (MYBYTE i = 1; i < bytes; i++) { dp += sprintf(dp, ":%02x", *(hex + i)); + } return target; } @@ -3823,13 +3976,15 @@ char *hex2String(char *target, const MYBYTE *hex, MYBYTE bytes) { char *genHostName(char *target, const MYBYTE *hex, MYBYTE bytes) { char *dp = target; - if (bytes) + if (bytes) { dp += sprintf(target, "Host%02x", *hex); - else + } else { *target = 0; + } - for (MYBYTE i = 1; i < bytes; i++) + for (MYBYTE i = 1; i < bytes; i++) { dp += sprintf(dp, "%02x", *(hex + i)); + } return target; } @@ -3894,8 +4049,9 @@ char *IP62String(char *target, MYBYTE *source) { #endif char *getHexValue(MYBYTE *target, char *source, MYBYTE *size) { - if (*size) + if (*size) { memset(target, 0, (*size)); + } for ((*size) = 0; (*source) && (*size) < UCHAR_MAX; (*size)++, target++) { if ((*source) >= '0' && (*source) <= '9') { @@ -3932,12 +4088,14 @@ char *getHexValue(MYBYTE *target, char *source, MYBYTE *size) { if ((*source) == ':' || (*source) == '-') { source++; - } else if (*source) + } else if (*source) { return source; + } } - if (*source) + if (*source) { return source; + } return nullptr; } @@ -3945,18 +4103,22 @@ char *getHexValue(MYBYTE *target, char *source, MYBYTE *size) { char *myUpper(char *string) { char diff = 'a' - 'A'; MYWORD len = strlen(string); - for (int i = 0; i < len; i++) - if (string[i] >= 'a' && string[i] <= 'z') + for (int i = 0; i < len; i++) { + if (string[i] >= 'a' && string[i] <= 'z') { string[i] = (char)(string[i] - diff); + } + } return string; } char *myLower(char *string) { char diff = 'a' - 'A'; MYWORD len = strlen(string); - for (int i = 0; i < len; i++) - if (string[i] >= 'A' && string[i] <= 'Z') + for (int i = 0; i < len; i++) { + if (string[i] >= 'A' && string[i] <= 'Z') { string[i] = (char)(string[i] - diff); + } + } return string; } @@ -4061,8 +4223,9 @@ void *updateStateFile(void *lpParam) { FILE *f = fopen(leaFile, "rb+"); if (f) { - if (fseek(f, (long)((dhcpData.dhcpInd - 1) * sizeof(data8)), SEEK_SET) >= 0) + if (fseek(f, (long)((dhcpData.dhcpInd - 1) * sizeof(data8)), SEEK_SET) >= 0) { fwrite(&dhcpData, sizeof(data8), 1, f); + } fclose(f); } @@ -4081,18 +4244,20 @@ data7 *findDHCPEntry(char *key) { myLower(key); auto it = dhcpCache.find(key); - if (it == dhcpCache.end()) + if (it == dhcpCache.end()) { return nullptr; - else + } else { return it->second; + } } bool checkMask(MYDWORD mask) { mask = htonl(mask); while (mask) { - if (mask < (mask << 1)) + if (mask < (mask << 1)) { return false; + } mask <<= 1; } @@ -4136,8 +4301,9 @@ MYDWORD calcMask(MYDWORD rangeStart, MYDWORD rangeEnd) { #endif char *cloneString(char *string) { - if (!string) + if (!string) { return nullptr; + } char *s = (char *)calloc(1, strlen(string) + 1); @@ -4164,26 +4330,31 @@ void *init(void *lparam) { if (name[0] && value[0]) { if (!strcasecmp(name, "LogLevel")) { - if (!strcasecmp(value, "None")) + if (!strcasecmp(value, "None")) { cfig.dhcpLogLevel = 0; - else if (!strcasecmp(value, "Normal")) + } else if (!strcasecmp(value, "Normal")) { cfig.dhcpLogLevel = 1; - else if (!strcasecmp(value, "All")) + } else if (!strcasecmp(value, "All")) { cfig.dhcpLogLevel = 2; - else + } else { sprintf(tempbuff, "Section [LOGGING], Invalid LogLevel: %s", value); - } else + } + } else { sprintf(tempbuff, "Section [LOGGING], Invalid Entry %s ignored", raw); - } else + } + } else { sprintf(tempbuff, "Section [LOGGING], Invalid Entry %s ignored", raw); + } } - if (tempbuff[0]) + if (tempbuff[0]) { logDHCPMess(logBuff, 1); + } sprintf(logBuff, "%s Starting...", sVersion); logDHCPMess(logBuff, 1); - } else + } else { printf("%s Starting...\n", sVersion); + } sleep(1); @@ -4197,12 +4368,13 @@ void *init(void *lparam) { cfig.lease = 36000; loadDHCP(); - if (cfig.dhcpLogLevel > 1) + if (cfig.dhcpLogLevel > 1) { sprintf(logBuff, "Logging: All"); - else if (cfig.dhcpLogLevel >= 1) + } else if (cfig.dhcpLogLevel >= 1) { sprintf(logBuff, "Logging: Normal"); - else + } else { sprintf(logBuff, "Logging: None"); + } logDHCPMess(logBuff, 1); @@ -4263,11 +4435,11 @@ void *init(void *lparam) { if (name[0] && value[0]) { if (!isIP(name) && isIP(value)) { - if (!strcasecmp(name, "Primary")) + if (!strcasecmp(name, "Primary")) { cfig.zoneServers[0] = inet_addr(value); - else if (!strcasecmp(name, "Secondary")) + } else if (!strcasecmp(name, "Secondary")) { cfig.zoneServers[1] = inet_addr(value); - else { + } else { sprintf(logBuff, "Section [REPLICATION_SERVERS] Invalid Entry: %s ignored", raw); logDHCPMess(logBuff, 1); } @@ -4293,11 +4465,11 @@ void *init(void *lparam) { && findServer(newNetwork.allServers, MAX_SERVERS, cfig.zoneServers[1])) { sprintf(logBuff, "Section [REPLICATION_SERVERS] Primary & Secondary should be Different Boxes"); logDHCPMess(logBuff, 1); - } else if (findServer(newNetwork.allServers, MAX_SERVERS, cfig.zoneServers[0])) + } else if (findServer(newNetwork.allServers, MAX_SERVERS, cfig.zoneServers[0])) { cfig.replication = 1; - else if (findServer(newNetwork.allServers, MAX_SERVERS, cfig.zoneServers[1])) + } else if (findServer(newNetwork.allServers, MAX_SERVERS, cfig.zoneServers[1])) { cfig.replication = 2; - else { + } else { sprintf(logBuff, "Section [REPLICATION_SERVERS] No Server IP not found on this Machine"); logDHCPMess(logBuff, 1); } @@ -4318,10 +4490,11 @@ void *init(void *lparam) { } else { //printf("Socket %u\n", cfig.dhcpReplConn.sock); - if (cfig.replication == 1) + if (cfig.replication == 1) { cfig.dhcpReplConn.server = cfig.zoneServers[0]; - else + } else { cfig.dhcpReplConn.server = cfig.zoneServers[1]; + } cfig.dhcpReplConn.addr.sin_family = AF_INET; cfig.dhcpReplConn.addr.sin_addr.s_addr = cfig.dhcpReplConn.server; @@ -4367,10 +4540,11 @@ void *init(void *lparam) { token.remote.sin_port = htons(IPPORT_DHCPS); token.remote.sin_family = AF_INET; - if (cfig.replication == 1) + if (cfig.replication == 1) { token.remote.sin_addr.s_addr = cfig.zoneServers[1]; - else + } else { token.remote.sin_addr.s_addr = cfig.zoneServers[0]; + } if (cfig.replication == 2) { pthread_t threadId; @@ -4386,25 +4560,28 @@ void *init(void *lparam) { sprintf(cfig.servername_fqn, "%s.%s", cfig.servername, cfig.zone); - if (cfig.lease >= MY_MAX_TIME) + if (cfig.lease >= MY_MAX_TIME) { sprintf(logBuff, "Default Lease: Infinity"); - else + } else { sprintf(logBuff, "Default Lease: %u (sec)", cfig.lease); + } logDHCPMess(logBuff, 1); - if (cfig.replication == 1) + if (cfig.replication == 1) { sprintf(logBuff, "Server Name: %s (Primary)", cfig.servername); - else if (cfig.replication == 2) + } else if (cfig.replication == 2) { sprintf(logBuff, "Server Name: %s (Secondary)", cfig.servername); - else + } else { sprintf(logBuff, "Server Name: %s", cfig.servername); + } logDHCPMess(logBuff, 1); do { - if (newNetwork.maxFD < cfig.dhcpReplConn.sock) + if (newNetwork.maxFD < cfig.dhcpReplConn.sock) { newNetwork.maxFD = cfig.dhcpReplConn.sock; + } bool bindfailed = false; @@ -4416,8 +4593,9 @@ void *init(void *lparam) { if (newNetwork.allServers[j] == cfig.specifiedServers[i]) { MYBYTE k = addServer(newNetwork.listenServers, MAX_SERVERS, newNetwork.allServers[j]); - if (k != MAX_SERVERS) + if (k != MAX_SERVERS) { newNetwork.listenMasks[k] = newNetwork.allMasks[j]; + } break; } @@ -4439,8 +4617,9 @@ void *init(void *lparam) { if (findServer(newNetwork.staticServers, MAX_SERVERS, newNetwork.allServers[i])) { MYBYTE k = addServer(newNetwork.listenServers, MAX_SERVERS, newNetwork.allServers[i]); - if (k != MAX_SERVERS) + if (k != MAX_SERVERS) { newNetwork.listenMasks[k] = newNetwork.allMasks[i]; + } } else { sprintf(logBuff, "Warning: Interface %s is not Static, not used", @@ -4457,15 +4636,17 @@ void *init(void *lparam) { int k = 0; for (; k < MAX_SERVERS && network.dhcpConn[k].loaded; k++) { - if (network.dhcpConn[k].ready && network.dhcpConn[k].server == newNetwork.listenServers[j]) + if (network.dhcpConn[k].ready && network.dhcpConn[k].server == newNetwork.listenServers[j]) { break; + } } if (network.dhcpConn[k].ready && network.dhcpConn[k].server == newNetwork.listenServers[j]) { memcpy(&(newNetwork.dhcpConn[i]), &(network.dhcpConn[k]), sizeof(DhcpConnType)); - if (newNetwork.maxFD < newNetwork.dhcpConn[i].sock) + if (newNetwork.maxFD < newNetwork.dhcpConn[i].sock) { newNetwork.maxFD = newNetwork.dhcpConn[i].sock; + } network.dhcpConn[k].ready = false; i++; @@ -4515,8 +4696,9 @@ void *init(void *lparam) { newNetwork.dhcpConn[i].loaded = true; newNetwork.dhcpConn[i].ready = true; - if (newNetwork.maxFD < newNetwork.dhcpConn[i].sock) + if (newNetwork.maxFD < newNetwork.dhcpConn[i].sock) { newNetwork.maxFD = newNetwork.dhcpConn[i].sock; + } newNetwork.dhcpConn[i].server = newNetwork.listenServers[j]; newNetwork.dhcpConn[i].mask = newNetwork.listenMasks[j]; @@ -4529,8 +4711,9 @@ void *init(void *lparam) { if (network.dhcpListener.ready) { memcpy(&(newNetwork.dhcpListener), &(network.dhcpListener), sizeof(DhcpConnType)); - if (newNetwork.maxFD < newNetwork.dhcpListener.sock) + if (newNetwork.maxFD < newNetwork.dhcpListener.sock) { newNetwork.maxFD = newNetwork.dhcpListener.sock; + } network.dhcpListener.ready = false; } else { @@ -4571,8 +4754,9 @@ void *init(void *lparam) { newNetwork.dhcpListener.loaded = true; newNetwork.dhcpListener.ready = true; - if (newNetwork.maxFD < newNetwork.dhcpListener.sock) + if (newNetwork.maxFD < newNetwork.dhcpListener.sock) { newNetwork.maxFD = newNetwork.dhcpListener.sock; + } } } } @@ -4602,9 +4786,9 @@ void *init(void *lparam) { } if (value[0]) { - if (STR2INT(value)) + if (STR2INT(value)) { newNetwork.httpConn.port = STR2INT(value); - else { + } else { newNetwork.httpConn.loaded = false; sprintf(logBuff, "Warning: Section [HTTP_INTERFACE], Invalid port %s, ignored", value); logDHCPMess(logBuff, 1); @@ -4619,9 +4803,9 @@ void *init(void *lparam) { logDHCPMess(logBuff, 1); } } else if (!strcasecmp(name, "HTTPClient")) { - if (isIP(value)) + if (isIP(value)) { addServer(cfig.httpClients, 8, inet_addr(value)); - else { + } else { sprintf(logBuff, "Warning: Section [HTTP_INTERFACE], invalid client IP %s, ignored", raw); logDHCPMess(logBuff, 1); } @@ -4635,14 +4819,16 @@ void *init(void *lparam) { } } - if (htmlTitle[0] == 0) + if (htmlTitle[0] == 0) { sprintf(htmlTitle, "Open DHCP on %s", cfig.servername); + } if (network.httpConn.ready && network.httpConn.server == newNetwork.httpConn.server) { memcpy(&(newNetwork.httpConn), &(network.httpConn), sizeof(ConnType)); - if (newNetwork.httpConn.sock > newNetwork.maxFD) + if (newNetwork.httpConn.sock > newNetwork.maxFD) { newNetwork.maxFD = newNetwork.httpConn.sock; + } network.httpConn.ready = false; } else { @@ -4685,8 +4871,9 @@ void *init(void *lparam) { newNetwork.httpConn.loaded = true; newNetwork.httpConn.ready = true; - if (newNetwork.httpConn.sock > newNetwork.maxFD) + if (newNetwork.httpConn.sock > newNetwork.maxFD) { newNetwork.maxFD = newNetwork.httpConn.sock; + } } } } @@ -4694,13 +4881,15 @@ void *init(void *lparam) { #endif newNetwork.maxFD++; - for (MYBYTE m = 0; m < MAX_SERVERS && newNetwork.allServers[m]; m++) + for (MYBYTE m = 0; m < MAX_SERVERS && newNetwork.allServers[m]; m++) { lockIP(newNetwork.allServers[m]); + } - if (bindfailed) + if (bindfailed) { cfig.failureCount++; - else + } else { cfig.failureCount = 0; + } closeConn(); memcpy(&network, &newNetwork, sizeof(data1)); @@ -4761,14 +4950,16 @@ bool detectChange() { logDHCPMess(logBuff, 1); break; } - } else + } else { cfig.failureCycle = 0; + } } network.ready = false; - while (kRunning && network.busy) + while (kRunning && network.busy) { sleep(1); + } //printf("Returning from detectchange\n"); @@ -4791,30 +4982,35 @@ void getInterfaces(data1 *pNetwork) { for (MYBYTE i = 0; i < numInterfaces; i++) { memcpy(&pIfr, &(IfcBuf[i]), sizeof(ifreq)); - if (!ioctl(cfig.fixedSocket, SIOCGIFADDR, &pIfr)) + if (!ioctl(cfig.fixedSocket, SIOCGIFADDR, &pIfr)) { addr = ((struct sockaddr_in *)&pIfr.ifr_addr)->sin_addr.s_addr; - else + } else { addr = 0; + } - if (!ioctl(cfig.fixedSocket, SIOCGIFNETMASK, &pIfr)) + if (!ioctl(cfig.fixedSocket, SIOCGIFNETMASK, &pIfr)) { mask = ((struct sockaddr_in *)&pIfr.ifr_addr)->sin_addr.s_addr; - else + } else { mask = 0; + } - if (!ioctl(cfig.fixedSocket, SIOCGIFFLAGS, &pIfr)) + if (!ioctl(cfig.fixedSocket, SIOCGIFFLAGS, &pIfr)) { flags = pIfr.ifr_flags; - else + } else { flags = 0; + } if (addr && mask && (flags & IFF_RUNNING) && (flags & IFF_UP) && !(flags & IFF_LOOPBACK) && !(flags & IFF_POINTOPOINT)) { MYBYTE k = addServer(pNetwork->allServers, MAX_SERVERS, addr); - if (k != MAX_SERVERS) + if (k != MAX_SERVERS) { pNetwork->allMasks[k] = mask; + } - if (!(flags & IFF_DYNAMIC)) + if (!(flags & IFF_DYNAMIC)) { addServer(pNetwork->staticServers, MAX_SERVERS, addr); + } } } } @@ -4864,8 +5060,9 @@ MYWORD gdmess(data9 *req, MYBYTE sockInd) { int flags = 0; req->bytes = (int)recvmsg(network.dhcpListener.sock, &req->msg, flags); - if (errno || req->bytes <= 0) + if (errno || req->bytes <= 0) { return 0; + } //printf("%u\n", req->msg.msg_controllen); //msgcontrol = (msg_control*)msg.msg_control; @@ -4884,13 +5081,14 @@ MYWORD gdmess(data9 *req, MYBYTE sockInd) { //printf("%s\n",IP2String(tempbuff, addr)); - if (!addr) + if (!addr) { return 0; + } for (int i = 0;; i++) { - if (i == MAX_SERVERS || !network.dhcpConn[i].server) + if (i == MAX_SERVERS || !network.dhcpConn[i].server) { return 0; - else if (addr == network.dhcpConn[i].server) { + } else if (addr == network.dhcpConn[i].server) { req->sockInd = i; break; } @@ -4902,12 +5100,14 @@ MYWORD gdmess(data9 *req, MYBYTE sockInd) { req->bytes = (int)recvfrom( network.dhcpConn[sockInd].sock, req->raw, sizeof(dhcp_packet), 0, (sockaddr *)&req->remote, &req->sockLen); - if (errno || req->bytes <= 0) + if (errno || req->bytes <= 0) { return 0; + } } - if (req->dhcpp.header.bp_op != BOOTP_REQUEST) + if (req->dhcpp.header.bp_op != BOOTP_REQUEST) { return 0; + } hex2String(req->chaddr, req->dhcpp.header.bp_chaddr, req->dhcpp.header.bp_hlen); @@ -4925,8 +5125,9 @@ MYWORD gdmess(data9 *req, MYBYTE sockInd) { continue; case DHCP_OPTION_PARAMREQLIST: - for (int ix = 0; ix < op->size; ix++) + for (int ix = 0; ix < op->size; ix++) { req->paramreqlist[op->value[ix]] = 1; + } break; case DHCP_OPTION_MESSAGETYPE: @@ -4954,8 +5155,9 @@ MYWORD gdmess(data9 *req, MYBYTE sockInd) { req->hostname[op->size] = 0; req->hostname[64] = 0; - if (char *ptr = strchr(req->hostname, '.')) + if (char *ptr = strchr(req->hostname, '.')) { *ptr = 0; + } break; @@ -4988,14 +5190,16 @@ MYWORD gdmess(data9 *req, MYBYTE sockInd) { raw += op->size; } - if (!req->subnetIP) + if (!req->subnetIP) { req->subnetIP = req->dhcpp.header.bp_giaddr; + } if (!req->messsize) { - if (req->req_type == DHCP_MESS_NONE) + if (req->req_type == DHCP_MESS_NONE) { req->messsize = req->bytes; - else + } else { req->messsize = sizeof(dhcp_packet); + } } // if (!req->hostname[0] && req->dhcpp.header.bp_ciaddr) @@ -5015,49 +5219,52 @@ MYWORD gdmess(data9 *req, MYBYTE sockInd) { if (verbatim || cfig.dhcpLogLevel >= 2) { if (req->req_type == DHCP_MESS_NONE) { - if (req->dhcpp.header.bp_giaddr) + if (req->dhcpp.header.bp_giaddr) { sprintf(logBuff, "BOOTPREQUEST for %s (%s) from RelayAgent %s received", req->chaddr, req->hostname, IP2String(ipbuff, req->dhcpp.header.bp_giaddr)); - else + } else { sprintf(logBuff, "BOOTPREQUEST for %s (%s) from interface %s received", req->chaddr, req->hostname, IP2String(ipbuff, network.dhcpConn[req->sockInd].server)); + } logDHCPMess(logBuff, 2); } else if (req->req_type == DHCP_MESS_DISCOVER) { - if (req->dhcpp.header.bp_giaddr) + if (req->dhcpp.header.bp_giaddr) { sprintf(logBuff, "DHCPDISCOVER for %s (%s) from RelayAgent %s received", req->chaddr, req->hostname, IP2String(ipbuff, req->dhcpp.header.bp_giaddr)); - else + } else { sprintf(logBuff, "DHCPDISCOVER for %s (%s) from interface %s received", req->chaddr, req->hostname, IP2String(ipbuff, network.dhcpConn[req->sockInd].server)); + } logDHCPMess(logBuff, 2); } else if (req->req_type == DHCP_MESS_REQUEST) { if ((!req->server) || req->server == network.dhcpConn[req->sockInd].server) { - if (req->dhcpp.header.bp_giaddr) + if (req->dhcpp.header.bp_giaddr) { sprintf(logBuff, "DHCPREQUEST for %s (%s) from RelayAgent %s received", req->chaddr, req->hostname, IP2String(ipbuff, req->dhcpp.header.bp_giaddr)); - else + } else { sprintf(logBuff, "DHCPREQUEST for %s (%s) from interface %s received", req->chaddr, req->hostname, IP2String(ipbuff, network.dhcpConn[req->sockInd].server)); + } logDHCPMess(logBuff, 2); } @@ -5153,8 +5360,9 @@ data7 *createCache(data71 *pLump) { MYWORD dataSize = 70 + sizeof(data7) + strlen(pLump->mapname) + pLump->optionSize; auto *cache = (data7 *)calloc(1, dataSize); - if (!cache) + if (!cache) { return nullptr; + } MYBYTE *dp = &cache->data; cache->mapname = (char *)dp; @@ -5163,16 +5371,18 @@ data7 *createCache(data71 *pLump) { dp++; cache->hostname = (char *)dp; - if (pLump->hostname) + if (pLump->hostname) { strcpy(cache->hostname, pLump->hostname); + } dp += 65; if (pLump->optionSize >= 3) { cache->options = dp; memcpy(cache->options, pLump->options, pLump->optionSize); - } else + } else { cache->options = nullptr; + } return cache; } diff --git a/srcs/open_dhcp/opendhcpd.h b/srcs/open_dhcp/opendhcpd.h index b48a3ed..8544504 100644 --- a/srcs/open_dhcp/opendhcpd.h +++ b/srcs/open_dhcp/opendhcpd.h @@ -19,13 +19,13 @@ ***************************************************************************/ //This file defines all structures and constants //for both DHCP and DNS Servers -#define MAX_SERVERS 125 -#define MAX_DHCP_RANGES 125 -#define MAX_RANGE_SETS 125 +#define MAX_SERVERS 125 +#define MAX_DHCP_RANGES 125 +#define MAX_RANGE_SETS 125 #define MAX_RANGE_FILTERS 32 #ifndef LOG_MAKEPRI -#define LOG_MAKEPRI(fac, pri) (((fac) << 3) | (pri)) +#define LOG_MAKEPRI(fac, pri) (((fac) << 3) | (pri)) #endif #ifndef SIOCGIFCONF @@ -58,223 +58,215 @@ typedef struct in_pktinfo IN_PKTINFO; #define IFF_DYNAMIC 0x8000 #endif -#define MYBYTE unsigned char -#define MYWORD unsigned short -#define MYDWORD unsigned int -#define SOCKET_ERROR -1 +#define MYBYTE unsigned char +#define MYWORD unsigned short +#define MYDWORD unsigned int +#define SOCKET_ERROR -1 #define INVALID_SOCKET -1 -#define SOCKADDR_IN sockaddr_in -#define SOCKADDR sockaddr -#define SOCKET int -#define BOOL bool -#define LPSOCKADDR sockaddr* -#define closesocket close +#define SOCKADDR_IN sockaddr_in +#define SOCKADDR sockaddr +#define SOCKET int +#define BOOL bool +#define LPSOCKADDR sockaddr * +#define closesocket close -struct data7 //cache +struct data7//cache { - char *mapname; - time_t expiry; - union - { - struct - { - MYBYTE cType; - MYBYTE dnsType; - MYBYTE sockInd; - MYBYTE dnsIndex; - }; - struct - { - unsigned fixed: 1; - unsigned local: 1; - unsigned display: 1; - unsigned subnetFlg: 1; - signed rangeInd: 8; - unsigned dhcpInd: 20; - }; - }; - union - { - char *name; - MYBYTE *options; - }; - union - { - int bytes; - MYDWORD ip; - SOCKADDR_IN *addr; - }; - union - { - MYBYTE *response; - char *hostname; - char *query; - }; - MYBYTE data; + char *mapname; + time_t expiry; + union { + struct { + MYBYTE cType; + MYBYTE dnsType; + MYBYTE sockInd; + MYBYTE dnsIndex; + }; + struct { + unsigned fixed : 1; + unsigned local : 1; + unsigned display : 1; + unsigned subnetFlg : 1; + signed rangeInd : 8; + unsigned dhcpInd : 20; + }; + }; + union { + char *name; + MYBYTE *options; + }; + union { + int bytes; + MYDWORD ip; + SOCKADDR_IN *addr; + }; + union { + MYBYTE *response; + char *hostname; + char *query; + }; + MYBYTE data; }; -struct data71 //Lump +struct data71//Lump { - char *mapname; - MYBYTE *response; - char *hostname; - char *query; - SOCKADDR_IN *addr; - MYBYTE *options; - MYWORD optionSize; - int bytes; - MYBYTE cType; - MYBYTE dnsType; + char *mapname; + MYBYTE *response; + char *hostname; + char *query; + SOCKADDR_IN *addr; + MYBYTE *options; + MYWORD optionSize; + int bytes; + MYBYTE cType; + MYBYTE dnsType; }; -typedef multimap hostMap; -typedef multimap expiryMap; -typedef map dhcpMap; +typedef multimap hostMap; +typedef multimap expiryMap; +typedef map dhcpMap; -struct ConnType -{ - SOCKET sock; - SOCKADDR_IN addr; - MYDWORD server; - MYWORD port; - bool loaded; - bool ready; +struct ConnType { + SOCKET sock; + SOCKADDR_IN addr; + MYDWORD server; + MYWORD port; + bool loaded; + bool ready; }; -#define BOOTP_REQUEST 1 -#define BOOTP_REPLY 2 - -#define DHCP_MESS_NONE 0 -#define DHCP_MESS_DISCOVER 1 -#define DHCP_MESS_OFFER 2 -#define DHCP_MESS_REQUEST 3 -#define DHCP_MESS_DECLINE 4 -#define DHCP_MESS_ACK 5 -#define DHCP_MESS_NAK 6 -#define DHCP_MESS_RELEASE 7 -#define DHCP_MESS_INFORM 8 +#define BOOTP_REQUEST 1 +#define BOOTP_REPLY 2 +#define DHCP_MESS_NONE 0 +#define DHCP_MESS_DISCOVER 1 +#define DHCP_MESS_OFFER 2 +#define DHCP_MESS_REQUEST 3 +#define DHCP_MESS_DECLINE 4 +#define DHCP_MESS_ACK 5 +#define DHCP_MESS_NAK 6 +#define DHCP_MESS_RELEASE 7 +#define DHCP_MESS_INFORM 8 // DHCP OPTIONS -#define DHCP_OPTION_PAD 0 -#define DHCP_OPTION_NETMASK 1 -#define DHCP_OPTION_TIMEOFFSET 2 -#define DHCP_OPTION_ROUTER 3 -#define DHCP_OPTION_TIMESERVER 4 -#define DHCP_OPTION_NAMESERVER 5 -#define DHCP_OPTION_DNS 6 -#define DHCP_OPTION_LOGSERVER 7 -#define DHCP_OPTION_COOKIESERVER 8 -#define DHCP_OPTION_LPRSERVER 9 -#define DHCP_OPTION_IMPRESSSERVER 10 -#define DHCP_OPTION_RESLOCSERVER 11 -#define DHCP_OPTION_HOSTNAME 12 -#define DHCP_OPTION_BOOTFILESIZE 13 -#define DHCP_OPTION_MERITDUMP 14 -#define DHCP_OPTION_DOMAINNAME 15 -#define DHCP_OPTION_SWAPSERVER 16 -#define DHCP_OPTION_ROOTPATH 17 -#define DHCP_OPTION_EXTSPATH 18 -#define DHCP_OPTION_IPFORWARD 19 -#define DHCP_OPTION_NONLOCALSR 20 -#define DHCP_OPTION_POLICYFILTER 21 -#define DHCP_OPTION_MAXREASSEMBLE 22 -#define DHCP_OPTION_IPTTL 23 -#define DHCP_OPTION_PATHMTUAGING 24 -#define DHCP_OPTION_PATHMTUPLATEAU 25 -#define DHCP_OPTION_INTERFACEMTU 26 -#define DHCP_OPTION_SUBNETSLOCAL 27 -#define DHCP_OPTION_BCASTADDRESS 28 -#define DHCP_OPTION_MASKDISCOVERY 29 -#define DHCP_OPTION_MASKSUPPLIER 30 -#define DHCP_OPTION_ROUTERDISCOVERY 31 -#define DHCP_OPTION_ROUTERSOLIC 32 -#define DHCP_OPTION_STATICROUTE 33 -#define DHCP_OPTION_TRAILERENCAPS 34 -#define DHCP_OPTION_ARPTIMEOUT 35 -#define DHCP_OPTION_ETHERNETENCAPS 36 -#define DHCP_OPTION_TCPTTL 37 -#define DHCP_OPTION_TCPKEEPALIVEINT 38 -#define DHCP_OPTION_TCPKEEPALIVEGRBG 39 -#define DHCP_OPTION_NISDOMAIN 40 -#define DHCP_OPTION_NISSERVERS 41 -#define DHCP_OPTION_NTPSERVERS 42 -#define DHCP_OPTION_VENDORSPECIFIC 43 -#define DHCP_OPTION_NETBIOSNAMESERV 44 -#define DHCP_OPTION_NETBIOSDGDIST 45 -#define DHCP_OPTION_NETBIOSNODETYPE 46 -#define DHCP_OPTION_NETBIOSSCOPE 47 -#define DHCP_OPTION_X11FONTS 48 -#define DHCP_OPTION_X11DISPLAYMNGR 49 -#define DHCP_OPTION_REQUESTEDIPADDR 50 -#define DHCP_OPTION_IPADDRLEASE 51 -#define DHCP_OPTION_OVERLOAD 52 -#define DHCP_OPTION_MESSAGETYPE 53 -#define DHCP_OPTION_SERVERID 54 -#define DHCP_OPTION_PARAMREQLIST 55 -#define DHCP_OPTION_MESSAGE 56 -#define DHCP_OPTION_MAXDHCPMSGSIZE 57 -#define DHCP_OPTION_RENEWALTIME 58 -#define DHCP_OPTION_REBINDINGTIME 59 -#define DHCP_OPTION_VENDORCLASSID 60 -#define DHCP_OPTION_CLIENTID 61 -#define DHCP_OPTION_NETWARE_IPDOMAIN 62 -#define DHCP_OPTION_NETWARE_IPOPTION 63 -#define DHCP_OPTION_NISPLUSDOMAIN 64 -#define DHCP_OPTION_NISPLUSSERVERS 65 -#define DHCP_OPTION_TFTPSERVER 66 -#define DHCP_OPTION_BOOTFILE 67 -#define DHCP_OPTION_MOBILEIPHOME 68 -#define DHCP_OPTION_SMTPSERVER 69 -#define DHCP_OPTION_POP3SERVER 70 -#define DHCP_OPTION_NNTPSERVER 71 -#define DHCP_OPTION_WWWSERVER 72 -#define DHCP_OPTION_FINGERSERVER 73 -#define DHCP_OPTION_IRCSERVER 74 -#define DHCP_OPTION_STSERVER 75 -#define DHCP_OPTION_STDASERVER 76 -#define DHCP_OPTION_USERCLASS 77 -#define DHCP_OPTION_SLPDIRAGENT 78 -#define DHCP_OPTION_SLPDIRSCOPE 79 -#define DHCP_OPTION_CLIENTFQDN 81 -#define DHCP_OPTION_RELAYAGENTINFO 82 -#define DHCP_OPTION_I_SNS 83 -#define DHCP_OPTION_NDSSERVERS 85 -#define DHCP_OPTION_NDSTREENAME 86 -#define DHCP_OPTION_NDSCONTEXT 87 -#define DHCP_OPTION_AUTHENTICATION 90 -#define DHCP_OPTION_CLIENTSYSTEM 93 -#define DHCP_OPTION_CLIENTNDI 94 -#define DHCP_OPTION_LDAP 95 -#define DHCP_OPTION_UUID_GUID 97 -#define DHCP_OPTION_USER_AUTH 98 -#define DHCP_OPTION_P_CODE 100 -#define DHCP_OPTION_T_CODE 101 -#define DHCP_OPTION_NETINFOADDRESS 112 -#define DHCP_OPTION_NETINFOTAG 113 -#define DHCP_OPTION_URL 114 -#define DHCP_OPTION_AUTO_CONFIG 116 -#define DHCP_OPTION_NAMESERVICESEARCH 117 -#define DHCP_OPTION_SUBNETSELECTION 118 -#define DHCP_OPTION_DOMAINSEARCH 119 -#define DHCP_OPTION_SIPSERVERSDHCP 120 -#define DHCP_OPTION_CLASSLESSSTATICROUTE 121 -#define DHCP_OPTION_CCC 122 -#define DHCP_OPTION_GEOCONF 123 -#define DHCP_OPTION_V_IVENDORCLASS 124 -#define DHCP_OPTION_V_IVENDOR_SPECIFIC 125 -#define DHCP_OPTION_TFPTSERVERIPADDRESS 128 -#define DHCP_OPTION_CALLSERVERIPADDRESS 129 -#define DHCP_OPTION_DISCRIMINATIONSTRING 130 -#define DHCP_OPTION_REMOTESTATISTICSSERVER 131 -#define DHCP_OPTION_802_1PVLANID 132 -#define DHCP_OPTION_802_1QL2PRIORITY 133 -#define DHCP_OPTION_DIFFSERVCODEPOINT 134 -#define DHCP_OPTION_HTTPPROXYFORPHONE_SPEC 135 -#define DHCP_OPTION_SERIAL 252 -#define DHCP_OPTION_BP_FILE 253 -#define DHCP_OPTION_NEXTSERVER 254 -#define DHCP_OPTION_END 255 +#define DHCP_OPTION_PAD 0 +#define DHCP_OPTION_NETMASK 1 +#define DHCP_OPTION_TIMEOFFSET 2 +#define DHCP_OPTION_ROUTER 3 +#define DHCP_OPTION_TIMESERVER 4 +#define DHCP_OPTION_NAMESERVER 5 +#define DHCP_OPTION_DNS 6 +#define DHCP_OPTION_LOGSERVER 7 +#define DHCP_OPTION_COOKIESERVER 8 +#define DHCP_OPTION_LPRSERVER 9 +#define DHCP_OPTION_IMPRESSSERVER 10 +#define DHCP_OPTION_RESLOCSERVER 11 +#define DHCP_OPTION_HOSTNAME 12 +#define DHCP_OPTION_BOOTFILESIZE 13 +#define DHCP_OPTION_MERITDUMP 14 +#define DHCP_OPTION_DOMAINNAME 15 +#define DHCP_OPTION_SWAPSERVER 16 +#define DHCP_OPTION_ROOTPATH 17 +#define DHCP_OPTION_EXTSPATH 18 +#define DHCP_OPTION_IPFORWARD 19 +#define DHCP_OPTION_NONLOCALSR 20 +#define DHCP_OPTION_POLICYFILTER 21 +#define DHCP_OPTION_MAXREASSEMBLE 22 +#define DHCP_OPTION_IPTTL 23 +#define DHCP_OPTION_PATHMTUAGING 24 +#define DHCP_OPTION_PATHMTUPLATEAU 25 +#define DHCP_OPTION_INTERFACEMTU 26 +#define DHCP_OPTION_SUBNETSLOCAL 27 +#define DHCP_OPTION_BCASTADDRESS 28 +#define DHCP_OPTION_MASKDISCOVERY 29 +#define DHCP_OPTION_MASKSUPPLIER 30 +#define DHCP_OPTION_ROUTERDISCOVERY 31 +#define DHCP_OPTION_ROUTERSOLIC 32 +#define DHCP_OPTION_STATICROUTE 33 +#define DHCP_OPTION_TRAILERENCAPS 34 +#define DHCP_OPTION_ARPTIMEOUT 35 +#define DHCP_OPTION_ETHERNETENCAPS 36 +#define DHCP_OPTION_TCPTTL 37 +#define DHCP_OPTION_TCPKEEPALIVEINT 38 +#define DHCP_OPTION_TCPKEEPALIVEGRBG 39 +#define DHCP_OPTION_NISDOMAIN 40 +#define DHCP_OPTION_NISSERVERS 41 +#define DHCP_OPTION_NTPSERVERS 42 +#define DHCP_OPTION_VENDORSPECIFIC 43 +#define DHCP_OPTION_NETBIOSNAMESERV 44 +#define DHCP_OPTION_NETBIOSDGDIST 45 +#define DHCP_OPTION_NETBIOSNODETYPE 46 +#define DHCP_OPTION_NETBIOSSCOPE 47 +#define DHCP_OPTION_X11FONTS 48 +#define DHCP_OPTION_X11DISPLAYMNGR 49 +#define DHCP_OPTION_REQUESTEDIPADDR 50 +#define DHCP_OPTION_IPADDRLEASE 51 +#define DHCP_OPTION_OVERLOAD 52 +#define DHCP_OPTION_MESSAGETYPE 53 +#define DHCP_OPTION_SERVERID 54 +#define DHCP_OPTION_PARAMREQLIST 55 +#define DHCP_OPTION_MESSAGE 56 +#define DHCP_OPTION_MAXDHCPMSGSIZE 57 +#define DHCP_OPTION_RENEWALTIME 58 +#define DHCP_OPTION_REBINDINGTIME 59 +#define DHCP_OPTION_VENDORCLASSID 60 +#define DHCP_OPTION_CLIENTID 61 +#define DHCP_OPTION_NETWARE_IPDOMAIN 62 +#define DHCP_OPTION_NETWARE_IPOPTION 63 +#define DHCP_OPTION_NISPLUSDOMAIN 64 +#define DHCP_OPTION_NISPLUSSERVERS 65 +#define DHCP_OPTION_TFTPSERVER 66 +#define DHCP_OPTION_BOOTFILE 67 +#define DHCP_OPTION_MOBILEIPHOME 68 +#define DHCP_OPTION_SMTPSERVER 69 +#define DHCP_OPTION_POP3SERVER 70 +#define DHCP_OPTION_NNTPSERVER 71 +#define DHCP_OPTION_WWWSERVER 72 +#define DHCP_OPTION_FINGERSERVER 73 +#define DHCP_OPTION_IRCSERVER 74 +#define DHCP_OPTION_STSERVER 75 +#define DHCP_OPTION_STDASERVER 76 +#define DHCP_OPTION_USERCLASS 77 +#define DHCP_OPTION_SLPDIRAGENT 78 +#define DHCP_OPTION_SLPDIRSCOPE 79 +#define DHCP_OPTION_CLIENTFQDN 81 +#define DHCP_OPTION_RELAYAGENTINFO 82 +#define DHCP_OPTION_I_SNS 83 +#define DHCP_OPTION_NDSSERVERS 85 +#define DHCP_OPTION_NDSTREENAME 86 +#define DHCP_OPTION_NDSCONTEXT 87 +#define DHCP_OPTION_AUTHENTICATION 90 +#define DHCP_OPTION_CLIENTSYSTEM 93 +#define DHCP_OPTION_CLIENTNDI 94 +#define DHCP_OPTION_LDAP 95 +#define DHCP_OPTION_UUID_GUID 97 +#define DHCP_OPTION_USER_AUTH 98 +#define DHCP_OPTION_P_CODE 100 +#define DHCP_OPTION_T_CODE 101 +#define DHCP_OPTION_NETINFOADDRESS 112 +#define DHCP_OPTION_NETINFOTAG 113 +#define DHCP_OPTION_URL 114 +#define DHCP_OPTION_AUTO_CONFIG 116 +#define DHCP_OPTION_NAMESERVICESEARCH 117 +#define DHCP_OPTION_SUBNETSELECTION 118 +#define DHCP_OPTION_DOMAINSEARCH 119 +#define DHCP_OPTION_SIPSERVERSDHCP 120 +#define DHCP_OPTION_CLASSLESSSTATICROUTE 121 +#define DHCP_OPTION_CCC 122 +#define DHCP_OPTION_GEOCONF 123 +#define DHCP_OPTION_V_IVENDORCLASS 124 +#define DHCP_OPTION_V_IVENDOR_SPECIFIC 125 +#define DHCP_OPTION_TFPTSERVERIPADDRESS 128 +#define DHCP_OPTION_CALLSERVERIPADDRESS 129 +#define DHCP_OPTION_DISCRIMINATIONSTRING 130 +#define DHCP_OPTION_REMOTESTATISTICSSERVER 131 +#define DHCP_OPTION_802_1PVLANID 132 +#define DHCP_OPTION_802_1QL2PRIORITY 133 +#define DHCP_OPTION_DIFFSERVCODEPOINT 134 +#define DHCP_OPTION_HTTPPROXYFORPHONE_SPEC 135 +#define DHCP_OPTION_SERIAL 252 +#define DHCP_OPTION_BP_FILE 253 +#define DHCP_OPTION_NEXTSERVER 254 +#define DHCP_OPTION_END 255 //#define DHCP_VENDORDATA_SIZE 272 //#define DHCP_VENDORDATA_SIZE 64 @@ -282,361 +274,341 @@ struct ConnType //#define DHCP_PACKET_SIZE 1024 //#define DHCP_MIN_SIZE 44 //#define DHCP_MAX_CLIENTS 254 -#define IPPORT_DHCPS 67 -#define IPPORT_DHCPC 68 +#define IPPORT_DHCPS 67 +#define IPPORT_DHCPC 68 #define VM_STANFORD 0x5354414EUL #define VM_RFC1048 0x63825363UL -struct data3 -{ - MYBYTE opt_code; - MYBYTE size; - MYBYTE value[256]; +struct data3 { + MYBYTE opt_code; + MYBYTE size; + MYBYTE value[256]; }; -struct data4 -{ - char opName[40]; - MYBYTE opTag; - MYBYTE opType; - bool permitted; +struct data4 { + char opName[40]; + MYBYTE opTag; + MYBYTE opType; + bool permitted; }; -#if defined(__LP64__) || defined(_WIN64) || (defined(__x86_64__) && !defined(__ILP32__) ) || defined(_M_X64) || defined(__ia64) || defined (_M_IA64) || defined(__aarch64__) || defined(__powerpc64__) +#if defined(__LP64__) || defined(_WIN64) || (defined(__x86_64__) && !defined(__ILP32__)) || defined(_M_X64) \ + || defined(__ia64) || defined(_M_IA64) || defined(__aarch64__) || defined(__powerpc64__) #define MY_MAX_TIME UINT_MAX -struct data8 //client +struct data8//client { - union - { - MYDWORD filler; - struct - { - unsigned dhcpInd: 20; - unsigned bp_hlen: 8; - unsigned fixed: 1; - unsigned local: 1; - unsigned display: 1; - unsigned subnetFlg: 1; - }; - }; - MYDWORD ip; - time_t expiry; - MYBYTE bp_chaddr[16]; - char hostname[64]; + union { + MYDWORD filler; + struct { + unsigned dhcpInd : 20; + unsigned bp_hlen : 8; + unsigned fixed : 1; + unsigned local : 1; + unsigned display : 1; + unsigned subnetFlg : 1; + }; + }; + MYDWORD ip; + time_t expiry; + MYBYTE bp_chaddr[16]; + char hostname[64]; }; #else #define MY_MAX_TIME INT_MAX -struct data8 //client +struct data8//client { - union - { - MYDWORD filler; - struct - { - unsigned dhcpInd: 20; - unsigned bp_hlen: 8; - unsigned fixed: 1; - unsigned local: 1; - unsigned display: 1; - unsigned subnetFlg: 1; - }; - }; - MYDWORD ip; - time_t expiry; - MYDWORD filler1; - MYBYTE bp_chaddr[16]; - char hostname[64]; + union { + MYDWORD filler; + struct { + unsigned dhcpInd : 20; + unsigned bp_hlen : 8; + unsigned fixed : 1; + unsigned local : 1; + unsigned display : 1; + unsigned subnetFlg : 1; + }; + }; + MYDWORD ip; + time_t expiry; + MYDWORD filler1; + MYBYTE bp_chaddr[16]; + char hostname[64]; }; #endif -struct msg_control -{ - ulong cmsg_len; - int cmsg_level; - int cmsg_type; - in_pktinfo pktinfo; +struct msg_control { + ulong cmsg_len; + int cmsg_level; + int cmsg_type; + in_pktinfo pktinfo; }; -struct dhcp_header -{ - MYBYTE bp_op; - MYBYTE bp_htype; - MYBYTE bp_hlen; - MYBYTE bp_hops; - MYDWORD bp_xid; - MYWORD bp_secs; - MYBYTE bp_broadcast; - MYBYTE bp_spare; - MYDWORD bp_ciaddr; - MYDWORD bp_yiaddr; - MYDWORD bp_siaddr; - MYDWORD bp_giaddr; - MYBYTE bp_chaddr[16]; - char bp_sname[64]; - MYBYTE bp_file[128]; - MYBYTE bp_magic_num[4]; +struct dhcp_header { + MYBYTE bp_op; + MYBYTE bp_htype; + MYBYTE bp_hlen; + MYBYTE bp_hops; + MYDWORD bp_xid; + MYWORD bp_secs; + MYBYTE bp_broadcast; + MYBYTE bp_spare; + MYDWORD bp_ciaddr; + MYDWORD bp_yiaddr; + MYDWORD bp_siaddr; + MYDWORD bp_giaddr; + MYBYTE bp_chaddr[16]; + char bp_sname[64]; + MYBYTE bp_file[128]; + MYBYTE bp_magic_num[4]; }; -struct dhcp_packet -{ - dhcp_header header; - MYBYTE vend_data[1024 - sizeof(dhcp_header)]; +struct dhcp_packet { + dhcp_header header; + MYBYTE vend_data[1024 - sizeof(dhcp_header)]; }; -struct data13 //dhcp range +struct data13//dhcp range { - MYBYTE rangeSetInd; - MYDWORD rangeStart; - MYDWORD rangeEnd; - MYDWORD mask; - MYBYTE *options; - time_t *expiry; - data7 **dhcpEntry; + MYBYTE rangeSetInd; + MYDWORD rangeStart; + MYDWORD rangeEnd; + MYDWORD mask; + MYBYTE *options; + time_t *expiry; + data7 **dhcpEntry; }; -struct data14 //rangeSet +struct data14//rangeSet { - MYBYTE active; - MYBYTE *macStart[MAX_RANGE_FILTERS]; - MYBYTE *macEnd[MAX_RANGE_FILTERS]; - MYBYTE macSize[MAX_RANGE_FILTERS]; - MYBYTE *vendClass[MAX_RANGE_FILTERS]; - MYBYTE vendClassSize[MAX_RANGE_FILTERS]; - MYBYTE *userClass[MAX_RANGE_FILTERS]; - MYBYTE userClassSize[MAX_RANGE_FILTERS]; - MYDWORD subnetIP[MAX_RANGE_FILTERS]; - MYDWORD targetIP; + MYBYTE active; + MYBYTE *macStart[MAX_RANGE_FILTERS]; + MYBYTE *macEnd[MAX_RANGE_FILTERS]; + MYBYTE macSize[MAX_RANGE_FILTERS]; + MYBYTE *vendClass[MAX_RANGE_FILTERS]; + MYBYTE vendClassSize[MAX_RANGE_FILTERS]; + MYBYTE *userClass[MAX_RANGE_FILTERS]; + MYBYTE userClassSize[MAX_RANGE_FILTERS]; + MYDWORD subnetIP[MAX_RANGE_FILTERS]; + MYDWORD targetIP; }; -struct data15 -{ - union - { - //MYDWORD ip; - unsigned ip:32; - MYBYTE octate[4]; - }; +struct data15 { + union { + //MYDWORD ip; + unsigned ip : 32; + MYBYTE octate[4]; + }; }; -struct data17 -{ - MYBYTE macArray[MAX_RANGE_SETS]; - MYBYTE vendArray[MAX_RANGE_SETS]; - MYBYTE userArray[MAX_RANGE_SETS]; - MYBYTE subnetArray[MAX_RANGE_SETS]; - bool macFound; - bool vendFound; - bool userFound; - bool subnetFound; +struct data17 { + MYBYTE macArray[MAX_RANGE_SETS]; + MYBYTE vendArray[MAX_RANGE_SETS]; + MYBYTE userArray[MAX_RANGE_SETS]; + MYBYTE subnetArray[MAX_RANGE_SETS]; + bool macFound; + bool vendFound; + bool userFound; + bool subnetFound; }; -struct data19 -{ - SOCKET sock; - socklen_t sockLen; - SOCKADDR_IN remote; - linger ling; - int memSize; - int bytes; - char *dp; +struct data19 { + SOCKET sock; + socklen_t sockLen; + SOCKADDR_IN remote; + linger ling; + int memSize; + int bytes; + char *dp; }; -struct data20 -{ - MYBYTE options[sizeof(dhcp_packet)]; - MYWORD optionSize; - MYDWORD ip; - MYDWORD mask; - MYBYTE rangeSetInd; +struct data20 { + MYBYTE options[sizeof(dhcp_packet)]; + MYWORD optionSize; + MYDWORD ip; + MYDWORD mask; + MYBYTE rangeSetInd; }; -struct data9 //dhcpRequst +struct data9//dhcpRequst { - MYDWORD lease; - union - { - char raw[sizeof(dhcp_packet)]; - dhcp_packet dhcpp; - }; - char hostname[256]; - char chaddr[64]; - MYDWORD server; - MYDWORD reqIP; - int bytes; - SOCKADDR_IN remote; - socklen_t sockLen; - MYWORD messsize; - msghdr msg; - iovec iov[1]; - msg_control msgcontrol; - MYBYTE *vp; - data7 *dhcpEntry; - data3 agentOption; - data3 clientId; - data3 subnet; - data3 vendClass; - data3 userClass; - MYDWORD subnetIP; - MYDWORD targetIP; - MYDWORD rebind; - MYBYTE paramreqlist[256]; - MYBYTE opAdded[256]; - MYBYTE req_type; - MYBYTE resp_type; - MYBYTE sockInd; + MYDWORD lease; + union { + char raw[sizeof(dhcp_packet)]; + dhcp_packet dhcpp; + }; + char hostname[256]; + char chaddr[64]; + MYDWORD server; + MYDWORD reqIP; + int bytes; + SOCKADDR_IN remote; + socklen_t sockLen; + MYWORD messsize; + msghdr msg; + iovec iov[1]; + msg_control msgcontrol; + MYBYTE *vp; + data7 *dhcpEntry; + data3 agentOption; + data3 clientId; + data3 subnet; + data3 vendClass; + data3 userClass; + MYDWORD subnetIP; + MYDWORD targetIP; + MYDWORD rebind; + MYBYTE paramreqlist[256]; + MYBYTE opAdded[256]; + MYBYTE req_type; + MYBYTE resp_type; + MYBYTE sockInd; }; -struct DhcpConnType -{ - SOCKET sock; - SOCKADDR_IN addr; - MYDWORD server; - MYWORD port; - bool loaded; - bool ready; - MYDWORD mask; - int reUseVal; - int reUseSize; - union - { - int broadCastVal; - bool pktinfoVal; - }; - union - { - int broadCastSize; - unsigned int pktinfoSize; - }; +struct DhcpConnType { + SOCKET sock; + SOCKADDR_IN addr; + MYDWORD server; + MYWORD port; + bool loaded; + bool ready; + MYDWORD mask; + int reUseVal; + int reUseSize; + union { + int broadCastVal; + bool pktinfoVal; + }; + union { + int broadCastSize; + unsigned int pktinfoSize; + }; }; -struct data1 -{ - DhcpConnType dhcpConn[MAX_SERVERS]; - DhcpConnType dhcpListener; - ConnType httpConn; - MYDWORD allServers[MAX_SERVERS]; - MYDWORD allMasks[MAX_SERVERS]; - MYDWORD listenServers[MAX_SERVERS]; - MYDWORD listenMasks[MAX_SERVERS]; - MYDWORD staticServers[MAX_SERVERS]; - SOCKET maxFD; - time_t dhcpRepl; - bool ready; - bool busy; +struct data1 { + DhcpConnType dhcpConn[MAX_SERVERS]; + DhcpConnType dhcpListener; + ConnType httpConn; + MYDWORD allServers[MAX_SERVERS]; + MYDWORD allMasks[MAX_SERVERS]; + MYDWORD listenServers[MAX_SERVERS]; + MYDWORD listenMasks[MAX_SERVERS]; + MYDWORD staticServers[MAX_SERVERS]; + SOCKET maxFD; + time_t dhcpRepl; + bool ready; + bool busy; }; -struct data2 -{ - char servername[128]; - char servername_fqn[256]; - char zone[256]; - MYBYTE zLen; - MYDWORD failureCount; - MYDWORD failureCycle; - bool ifspecified; - ConnType dhcpReplConn; - MYDWORD oldservers[MAX_SERVERS]; - MYDWORD specifiedServers[MAX_SERVERS]; - MYDWORD zoneServers[2]; - MYDWORD httpClients[8]; - char logFileName[256]; - data13 dhcpRanges[MAX_DHCP_RANGES]; - data14 rangeSet[MAX_RANGE_SETS]; - MYDWORD mask; - MYDWORD lease; - SOCKET fixedSocket; - MYDWORD serial; - MYDWORD dhcpSize; - time_t dhcpRepl; - MYDWORD rangeStart; - MYDWORD rangeEnd; - MYBYTE *options; - MYWORD dhcpInd; - MYBYTE replication; - MYBYTE dhcpLogLevel; +struct data2 { + char servername[128]; + char servername_fqn[256]; + char zone[256]; + MYBYTE zLen; + MYDWORD failureCount; + MYDWORD failureCycle; + bool ifspecified; + ConnType dhcpReplConn; + MYDWORD oldservers[MAX_SERVERS]; + MYDWORD specifiedServers[MAX_SERVERS]; + MYDWORD zoneServers[2]; + MYDWORD httpClients[8]; + char logFileName[256]; + data13 dhcpRanges[MAX_DHCP_RANGES]; + data14 rangeSet[MAX_RANGE_SETS]; + MYDWORD mask; + MYDWORD lease; + SOCKET fixedSocket; + MYDWORD serial; + MYDWORD dhcpSize; + time_t dhcpRepl; + MYDWORD rangeStart; + MYDWORD rangeEnd; + MYBYTE *options; + MYWORD dhcpInd; + MYBYTE replication; + MYBYTE dhcpLogLevel; struct ifreq IfcBuf[MAX_SERVERS]; - MYBYTE ifc_len; - pid_t ppid; - bool hasFilter; - char rangeCount; + MYBYTE ifc_len; + pid_t ppid; + bool hasFilter; + char rangeCount; }; - //Function Prototypes -bool checkIP(data9 *req, data17 *rangeData, MYDWORD ip); -bool checkMask(MYDWORD); -bool checkRange(data17*, char); -bool detectChange(); -bool getSection(const char*, char*, MYBYTE, char*); -bool isInt(char*); -bool isIP(char*); -FILE *openSection(const char*, MYBYTE); -MYBYTE pIP(void*, MYDWORD); -MYBYTE pUInt(void*, MYDWORD); -MYBYTE pUShort(void*, MYWORD); -MYBYTE addServer(MYDWORD*, MYBYTE, MYDWORD); -int getRangeInd(MYDWORD); -char* myTrim(char*, char*); -char* myGetToken(char*, MYBYTE); -char* cloneString(char*); -char* getHexValue(MYBYTE*, char*, MYBYTE*); -char* genHostName(char*, const MYBYTE*, MYBYTE); -char* hex2String(char*, const MYBYTE*, MYBYTE); -char* IP2String(char*, MYDWORD); -char* IP2arpa(char*, MYDWORD); -char* IP62String(char*, MYBYTE*); -char* myUpper(char* string); -char* myLower(char* string); -char* readSection(char*, FILE*); -data7* findDHCPEntry(char*); -data7 *createCache(data71 *pLump); -MYDWORD alad(data9*); -MYDWORD calcMask(MYDWORD, MYDWORD); -MYDWORD chaddr(data9*); -MYDWORD resad(data9*); -MYDWORD fUInt(void *raw); -MYDWORD sdmess(data9*); -MYDWORD sendRepl(data9 *req); -MYDWORD* findServer(MYDWORD*, MYBYTE, MYDWORD); -int getIndex(char, MYDWORD); -void addDHCPRange(char *dp); -void addVendClass(MYBYTE rangeSetInd, char *vendClass, MYBYTE vendClassSize); -void addUserClass(MYBYTE rangeSetInd, char *userClass, MYBYTE userClassSize); -void addMacRange(MYBYTE rangeSetInd, char *macRange); -void addOptions(data9 *req); -void calcRangeLimits(MYDWORD, MYDWORD, MYDWORD*, MYDWORD*); +bool checkIP(data9 *req, data17 *rangeData, MYDWORD ip); +bool checkMask(MYDWORD); +bool checkRange(data17 *, char); +bool detectChange(); +bool getSection(const char *, char *, MYBYTE, char *); +bool isInt(char *); +bool isIP(char *); +FILE *openSection(const char *, MYBYTE); +MYBYTE pIP(void *, MYDWORD); +MYBYTE pUInt(void *, MYDWORD); +MYBYTE pUShort(void *, MYWORD); +MYBYTE addServer(MYDWORD *, MYBYTE, MYDWORD); +int getRangeInd(MYDWORD); +char *myTrim(char *, char *); +char *myGetToken(char *, MYBYTE); +char *cloneString(char *); +char *getHexValue(MYBYTE *, char *, MYBYTE *); +char *genHostName(char *, const MYBYTE *, MYBYTE); +char *hex2String(char *, const MYBYTE *, MYBYTE); +char *IP2String(char *, MYDWORD); +char *IP2arpa(char *, MYDWORD); +char *IP62String(char *, MYBYTE *); +char *myUpper(char *string); +char *myLower(char *string); +char *readSection(char *, FILE *); +data7 *findDHCPEntry(char *); +data7 *createCache(data71 *pLump); +MYDWORD alad(data9 *); +MYDWORD calcMask(MYDWORD, MYDWORD); +MYDWORD chaddr(data9 *); +MYDWORD resad(data9 *); +MYDWORD fUInt(void *raw); +MYDWORD sdmess(data9 *); +MYDWORD sendRepl(data9 *req); +MYDWORD *findServer(MYDWORD *, MYBYTE, MYDWORD); +int getIndex(char, MYDWORD); +void addDHCPRange(char *dp); +void addVendClass(MYBYTE rangeSetInd, char *vendClass, MYBYTE vendClassSize); +void addUserClass(MYBYTE rangeSetInd, char *userClass, MYBYTE userClassSize); +void addMacRange(MYBYTE rangeSetInd, char *macRange); +void addOptions(data9 *req); +void calcRangeLimits(MYDWORD, MYDWORD, MYDWORD *, MYDWORD *); void catch_system_interupt(int sig_num); -void checkSize(MYBYTE); -void closeConn(); -void getInterfaces(data1*); -void getSecondary(); -void *init(void*); -void lockOptions(FILE*); -void loadOptions(FILE*, const char*, data20*); -void logDHCPMess(char*, MYBYTE); -void mySplit(char*, char*, const char*, char); -void *sendHTTP(void *lpParam); -void procHTTP(data19*); -void pvdata(data9*, data3*); -void recvRepl(data9*); -void runProg(); -void lockIP(MYDWORD); -void sendScopeStatus(data19 *req); -void sendStatus(data19 *req); -void setTempLease(data7*); -void setLeaseExpiry(data7*); -void setLeaseExpiry(data7*, MYDWORD); -void *updateStateFile(void*); -MYWORD fUShort(void*); -MYWORD gdmess(data9*, MYBYTE); -MYWORD myTokenize(char*, char*, const char*, bool); -MYWORD pQu(char*, char*); -MYWORD qLen(char*); +void checkSize(MYBYTE); +void closeConn(); +void getInterfaces(data1 *); +void getSecondary(); +void *init(void *); +void lockOptions(FILE *); +void loadOptions(FILE *, const char *, data20 *); +void logDHCPMess(char *, MYBYTE); +void mySplit(char *, char *, const char *, char); +void *sendHTTP(void *lpParam); +void procHTTP(data19 *); +void pvdata(data9 *, data3 *); +void recvRepl(data9 *); +void runProg(); +void lockIP(MYDWORD); +void sendScopeStatus(data19 *req); +void sendStatus(data19 *req); +void setTempLease(data7 *); +void setLeaseExpiry(data7 *); +void setLeaseExpiry(data7 *, MYDWORD); +void *updateStateFile(void *); +MYWORD fUShort(void *); +MYWORD gdmess(data9 *, MYBYTE); +MYWORD myTokenize(char *, char *, const char *, bool); +MYWORD pQu(char *, char *); +MYWORD qLen(char *); const char *getRequestMethod(const char *buffer); -void prepareUserHtmlRespStatus(data19 *req); -void opendhcp_init_http_server(); -void sendUserList(data19 *req, const char *pRequest, dhcpMap *dhcpCache, data2 *cfig, time_t t); -void sendAllLists(data19 *req, bool kRunning, dhcpMap *dhcpCache, data2 *cfig); -int getHwAddr(char *buff, char *mac); -int arpSet(const char *ifname, char *ipStr, char *mac); +void prepareUserHtmlRespStatus(data19 *req); +void opendhcp_init_http_server(); +void sendUserList(data19 *req, const char *pRequest, dhcpMap *dhcpCache, data2 *cfig, time_t t); +void sendAllLists(data19 *req, bool kRunning, dhcpMap *dhcpCache, data2 *cfig); +int getHwAddr(char *buff, char *mac); +int arpSet(const char *ifname, char *ipStr, char *mac); sockaddr_in get_cliAddr(char *nicif, char *tempbuff, data9 *req); \ No newline at end of file diff --git a/srcs/open_dhcp/query.cpp b/srcs/open_dhcp/query.cpp index 286ba4a..5eb13a8 100644 --- a/srcs/open_dhcp/query.cpp +++ b/srcs/open_dhcp/query.cpp @@ -257,7 +257,7 @@ static void opendhcp_http_info(http_request *request, hw_http_response *response auto *req = (data19 *)malloc(sizeof(struct data19)); - if(req == nullptr) { + if (req == nullptr) { hw_http_response_send_error(response, HTTP_STATUS_500, "memory error"); return; } @@ -300,7 +300,7 @@ static void opendhcp_http_get_userinfo(http_request *request, hw_http_response * auto *req = (data19 *)malloc(sizeof(struct data19)); - if(req == nullptr) { + if (req == nullptr) { hw_http_response_send_error(response, HTTP_STATUS_500, "memory error"); return; } @@ -343,7 +343,7 @@ static void opendhcp_http_get_alluser(http_request *request, hw_http_response *r auto *req = (data19 *)malloc(sizeof(struct data19)); - if(req == nullptr) { + if (req == nullptr) { hw_http_response_send_error(response, HTTP_STATUS_500, "memory error"); return; }