From a5fee5b12bf1df48749d683ca717f71209a62746 Mon Sep 17 00:00:00 2001 From: huangxin Date: Wed, 7 Dec 2022 15:58:32 +0800 Subject: [PATCH 1/9] =?UTF-8?q?OCT=201.=20=E4=BF=AE=E6=AD=A3JSON=E5=A4=B4?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E5=BC=95=E7=94=A8=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- srcs/libs/include/proto.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/srcs/libs/include/proto.h b/srcs/libs/include/proto.h index efd45e2..89cf5e8 100644 --- a/srcs/libs/include/proto.h +++ b/srcs/libs/include/proto.h @@ -8,7 +8,7 @@ #ifdef __cplusplus extern "C" { #endif -#include +#include typedef enum { CRYPTO_NONE = 0, From b49e3595db08223f91ab4cc9963a2e1dba47b0f7 Mon Sep 17 00:00:00 2001 From: huangxin Date: Wed, 7 Dec 2022 15:58:52 +0800 Subject: [PATCH 2/9] =?UTF-8?q?OCT=201.=20=E7=A7=BB=E9=99=A4=E5=A4=9A?= =?UTF-8?q?=E4=BD=99=E5=A4=B4=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- srcs/libs/mq/mq_proto.c | 1 - 1 file changed, 1 deletion(-) diff --git a/srcs/libs/mq/mq_proto.c b/srcs/libs/mq/mq_proto.c index 33d9b92..c72b960 100644 --- a/srcs/libs/mq/mq_proto.c +++ b/srcs/libs/mq/mq_proto.c @@ -1,7 +1,6 @@ // // Created by xajhu on 2021/8/11 0011. // -#include #include #include From fe0b3573003dac288966e783bc433e6ccc9841ef Mon Sep 17 00:00:00 2001 From: huangxin Date: Wed, 7 Dec 2022 15:59:16 +0800 Subject: [PATCH 3/9] =?UTF-8?q?OCT=201.=20=E4=BF=AE=E6=AD=A3=E7=BC=93?= =?UTF-8?q?=E5=86=B2=E5=8C=BA=E5=8F=AF=E8=83=BD=E6=BA=A2=E5=87=BA=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- srcs/libs/hardware/disk.c | 2 +- srcs/libs/hardware/memory.c | 2 +- srcs/libs/misc/zvector.c | 2 ++ 3 files changed, 4 insertions(+), 2 deletions(-) diff --git a/srcs/libs/hardware/disk.c b/srcs/libs/hardware/disk.c index 7679624..c5fee99 100644 --- a/srcs/libs/hardware/disk.c +++ b/srcs/libs/hardware/disk.c @@ -36,7 +36,7 @@ static int disk_info_refresh() { int i = 0; int errCode = ERR_SUCCESS; FILE *fp; - char buf[MAX_PATH]; + char buf[MAX_PATH * 2]; fp = popen(CMD_DISK_FILESYSTEM, "r"); diff --git a/srcs/libs/hardware/memory.c b/srcs/libs/hardware/memory.c index a3fdeb3..10193ea 100644 --- a/srcs/libs/hardware/memory.c +++ b/srcs/libs/hardware/memory.c @@ -44,7 +44,7 @@ unsigned int get_sys_total_memory() { static int memory_info_refresh() { int errCode = ERR_SUCCESS; FILE *fp; - char buf[MAX_PATH]; + char buf[MAX_PATH * 2]; fp = popen(CMD_MEMORY_INFO, "r"); diff --git a/srcs/libs/misc/zvector.c b/srcs/libs/misc/zvector.c index 405ce45..054215a 100644 --- a/srcs/libs/misc/zvector.c +++ b/srcs/libs/misc/zvector.c @@ -701,6 +701,8 @@ static zvect_retval p_vect_set_capacity(vector const v, const zvect_index direct return ZVERR_OUTOFMEM; } + memset(new_data, 0, sizeof(void *) * (new_capacity + v->cap_right)); + zvect_index nb; zvect_index ne; nb = v->cap_left; From f9fa369457d0dfe728fcd83ee2304fb19c6ef4b1 Mon Sep 17 00:00:00 2001 From: huangxin Date: Wed, 7 Dec 2022 15:59:45 +0800 Subject: [PATCH 4/9] =?UTF-8?q?OCT=201.=20=E6=9B=B4=E6=AD=A3DHCP=20HTTP=20?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3=E8=B7=AF=E5=BE=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- srcs/open_dhcp/query.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/srcs/open_dhcp/query.cpp b/srcs/open_dhcp/query.cpp index aa9ef26..ad6b591 100644 --- a/srcs/open_dhcp/query.cpp +++ b/srcs/open_dhcp/query.cpp @@ -684,9 +684,9 @@ void opendhcp_init_http_server() { if (!added) { hw_http_add_route("/", opendhcp_http_info, nullptr); - hw_http_add_route("dchp/info/getuser", opendhcp_http_get_userinfo, nullptr); - hw_http_add_route("dchp/info/allusers", opendhcp_http_get_alluser, nullptr); - hw_http_add_route("dchp/config/rangeset", opendhcp_http_expand_rangeset, nullptr); + hw_http_add_route("dhcp/info/getuser", opendhcp_http_get_userinfo, nullptr); + hw_http_add_route("dhcp/info/allusers", opendhcp_http_get_alluser, nullptr); + hw_http_add_route("dhcp/config/rangeset", opendhcp_http_expand_rangeset, nullptr); added = TRUE; } } \ No newline at end of file From 900d86eb6c266c470919c53133ea169bd502a222 Mon Sep 17 00:00:00 2001 From: huangxin Date: Wed, 7 Dec 2022 16:00:28 +0800 Subject: [PATCH 5/9] =?UTF-8?q?OCT=201.=20=E6=9B=B4=E6=AD=A3HTTP=20Server?= =?UTF-8?q?=E6=9C=8D=E5=8A=A1=E5=9B=9E=E8=B0=83=E5=87=BD=E6=95=B0=E5=AE=9A?= =?UTF-8?q?=E4=B9=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/haywire/connection_consumer.c | 1 + srcs/httpserver/src/haywire/http_response.c | 6 ++-- srcs/httpserver/src/haywire/http_server.c | 30 ++++++++++--------- srcs/httpserver/src/haywire/http_server.h | 3 +- 4 files changed, 22 insertions(+), 18 deletions(-) diff --git a/srcs/httpserver/src/haywire/connection_consumer.c b/srcs/httpserver/src/haywire/connection_consumer.c index 2cdcfa1..bdd9bd1 100644 --- a/srcs/httpserver/src/haywire/connection_consumer.c +++ b/srcs/httpserver/src/haywire/connection_consumer.c @@ -7,6 +7,7 @@ #include "http_response_cache.h" static bool tcp_nodelay; +extern uv_read_cb http_stream_on_read; void ipc_read_cb(uv_stream_t *handle, ssize_t nread, const uv_buf_t *buf) { int rc; diff --git a/srcs/httpserver/src/haywire/http_response.c b/srcs/httpserver/src/haywire/http_response.c index 9310abb..08d1627 100644 --- a/srcs/httpserver/src/haywire/http_response.c +++ b/srcs/httpserver/src/haywire/http_response.c @@ -7,6 +7,8 @@ #include "http_response_cache.h" #include "hw_string.h" +extern http_server_write_response_cb http_server_write_response; + #define CRLF "\r\n" KHASH_MAP_INIT_STR(string_hashmap, char *) @@ -78,8 +80,8 @@ hw_string *create_response_buffer(hw_http_response *response) { int header_buffer_incr = 512; int body_size = resp->body.length; int header_size_remaining = header_buffer_incr; - int response_size = header_size_remaining + sizeof(length_header) + num_chars(resp->body.length) + 2 * line_sep_size - + body_size + line_sep_size; + int response_size = header_size_remaining + sizeof(length_header) + num_chars(resp->body.length) + + 2 * line_sep_size + body_size + line_sep_size; response_string->value = malloc(response_size); diff --git a/srcs/httpserver/src/haywire/http_server.c b/srcs/httpserver/src/haywire/http_server.c index fbf0c3a..495f5d0 100644 --- a/srcs/httpserver/src/haywire/http_server.c +++ b/srcs/httpserver/src/haywire/http_server.c @@ -1,6 +1,6 @@ #ifdef PLATFORM_POSIX #include -#endif// PLATFORM_POSIX +#endif // PLATFORM_POSIX #include #include @@ -39,15 +39,17 @@ 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_barrier_t *listeners_created_barrier; +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; +uv_read_cb http_stream_on_read; +http_server_write_response_cb http_server_write_response; int hw_init_with_config(configuration *c) { #ifdef DEBUG @@ -132,7 +134,7 @@ void hw_http_add_route(char *route, http_request_callback callback, void *user_d routes = kh_init(string_hashmap); } set_route(routes, route, route_entry); - dzlog_debug("Added route path: [%s]\n", route);// TODO: Replace with logging instead. + dzlog_debug("Added route path: [%s]\n", route); // TODO: Replace with logging instead. } void free_http_server() { @@ -145,13 +147,13 @@ void free_http_server() { free((char *)v); }); kh_destroy(string_hashmap, routes); - uv_close((uv_handle_t*)&server, NULL); + uv_close((uv_handle_t *)&server, NULL); uninit_http_request_cache(); dzlog_debug("HTTP Server Close http://%s:%d\n", config->http_listen_address, config->http_listen_port); } int hw_http_open() { - int threads = (int)config->thread_count; + int threads = (int)config->thread_count; static uv_async_t service_handle; if (routes == NULL) { @@ -168,7 +170,7 @@ int hw_http_open() { #ifdef UNIX signal(SIGPIPE, SIG_IGN); -#endif// UNIX +#endif // UNIX listener_count = threads; diff --git a/srcs/httpserver/src/haywire/http_server.h b/srcs/httpserver/src/haywire/http_server.h index ae41451..217ca4d 100644 --- a/srcs/httpserver/src/haywire/http_server.h +++ b/srcs/httpserver/src/haywire/http_server.h @@ -25,8 +25,7 @@ extern uv_async_t *listener_async_handles; extern uv_loop_t *listener_event_loops; extern uv_barrier_t *listeners_created_barrier; -void (*http_stream_on_read)(uv_stream_t *, ssize_t, const uv_buf_t *); -int (*http_server_write_response)(hw_write_context *, hw_string *); +typedef int (*http_server_write_response_cb)(hw_write_context *, hw_string *); http_connection *create_http_connection(); void http_stream_on_connect(uv_stream_t *stream, int status); From 548787003fa3f3b5dcecd30daf4ddcb816533e4a Mon Sep 17 00:00:00 2001 From: huangxin Date: Wed, 7 Dec 2022 16:01:07 +0800 Subject: [PATCH 6/9] =?UTF-8?q?OCT=201.=20=E6=9B=B4=E6=96=B0=E5=B7=A5?= =?UTF-8?q?=E7=A8=8B=E4=BE=9D=E8=B5=96=E9=A1=B9=E6=9F=A5=E6=89=BE=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- srcs/CMakeLists.txt | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/srcs/CMakeLists.txt b/srcs/CMakeLists.txt index a4ffa77..4949d9a 100644 --- a/srcs/CMakeLists.txt +++ b/srcs/CMakeLists.txt @@ -14,10 +14,15 @@ FIND_PACKAGE(PkgConfig) PKG_SEARCH_MODULE(LIBCURL REQUIRED libcurl) PKG_SEARCH_MODULE(LIBSSL REQUIRED libssl) PKG_SEARCH_MODULE(LIBCRYPTO REQUIRED libcrypto) -LIST(APPEND COMMON_LIBS "${LIBCURL_LDFLAGS} ${LIBSSL_LDFLAGS} ${LIBCRYPTO_LDFLAGS}") -LIST(APPEND COMMON_LIBS "-ldl -lpthread -lzlog -lm -luv -lzmq -luuid -lconfig") +PKG_SEARCH_MODULE(LIBUV REQUIRED libuv) +PKG_SEARCH_MODULE(LIBCONFIG REQUIRED libconfig) +PKG_SEARCH_MODULE(LIBZMQ REQUIRED libzmq) +FIND_LIBRARY(LIBZLOG zlog REQUIRED) +FIND_LIBRARY(LIBUUID uuid REQUIRED) -LIST(APPEND COMMON_LIBS "-lpthread") +LIST(APPEND COMMON_LIBS "${LIBUV_LDFLAGS} ${LIBCONFIG_LDFLAGS} ${LIBCURL_LDFLAGS} ${LIBSSL_LDFLAGS} ${LIBCRYPTO_LDFLAGS}") +LIST(APPEND COMMON_LIBS "${LIBZMQ_LDFLAGS} ${LIBZLOG} ${LIBUUID}") +LIST(APPEND COMMON_LIBS "-lm -lpthread") ADD_DEFINITIONS(${COMMON_DEFINE}) From 00da37446d3800e3d432f7a9ca2068f01e932652 Mon Sep 17 00:00:00 2001 From: huangxin Date: Wed, 7 Dec 2022 16:03:02 +0800 Subject: [PATCH 7/9] =?UTF-8?q?OCT=201.=20=E6=89=8B=E5=8A=A8=E4=BF=AE?= =?UTF-8?q?=E6=94=B9=E5=90=88=E5=B9=B6=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- srcs/open_dhcp/query.cpp | 4 ---- 1 file changed, 4 deletions(-) diff --git a/srcs/open_dhcp/query.cpp b/srcs/open_dhcp/query.cpp index ad3e8ab..0bb508f 100644 --- a/srcs/open_dhcp/query.cpp +++ b/srcs/open_dhcp/query.cpp @@ -861,12 +861,8 @@ void opendhcp_init_http_server() { hw_http_add_route("/", opendhcp_http_info, nullptr); hw_http_add_route("dhcp/info/getuser", opendhcp_http_get_userinfo, nullptr); hw_http_add_route("dhcp/info/allusers", opendhcp_http_get_alluser, nullptr); -<<<<<<< HEAD - hw_http_add_route("dhcp/config/rangeset", opendhcp_http_expand_rangeset, nullptr); -======= hw_http_add_route("dhcp/config/rangeset", opendhcp_http_add_rangeset, nullptr); hw_http_add_route("dhcp/delete/rangeset", opendhcp_http_delete_rangeset, nullptr); ->>>>>>> origin/master added = TRUE; } } \ No newline at end of file From b6c91279a57506befb30a98c5428512a0708f365 Mon Sep 17 00:00:00 2001 From: huangxin Date: Thu, 8 Dec 2022 15:12:23 +0800 Subject: [PATCH 8/9] =?UTF-8?q?OCT=201.=20=E5=88=A0=E9=99=A4OpenDHCP?= =?UTF-8?q?=E6=9C=8D=E5=8A=A1ini=E9=85=8D=E7=BD=AE=E6=96=87=E4=BB=B6=202.?= =?UTF-8?q?=20=E5=A2=9E=E5=8A=A0=20DHCP=20=E4=B8=BB=E3=80=81=E4=BB=8E?= =?UTF-8?q?=E6=9C=8D=E5=8A=A1=E5=99=A8=E9=85=8D=E7=BD=AE=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E8=87=AA=E5=8A=A8=E5=8A=A0=E8=BD=BD=E9=85=8D=E7=BD=AE=E9=A1=B9?= =?UTF-8?q?=E5=8A=9F=E8=83=BD=203.=20=E5=A2=9E=E5=8A=A0=20DHCP=20=E5=9C=B0?= =?UTF-8?q?=E5=9D=80=E6=B1=A0=E9=85=8D=E7=BD=AE=E5=90=AF=E5=8A=A8=E8=87=AA?= =?UTF-8?q?=E5=8A=A8=E5=8A=A0=E8=BD=BD=E9=85=8D=E7=BD=AE=E9=A1=B9=E5=8A=9F?= =?UTF-8?q?=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config/opendhcp.ini | 38 ++++---- config/vcpe.cfg | 10 +- config/vcpe.service | 2 +- srcs/open_dhcp/opendhcpd.cpp | 28 ++++-- srcs/open_dhcp/opendhcpd.h | 20 +++- srcs/open_dhcp/query.cpp | 172 +++++++++++++++++++++-------------- 6 files changed, 165 insertions(+), 105 deletions(-) diff --git a/config/opendhcp.ini b/config/opendhcp.ini index b90da89..71e4c45 100644 --- a/config/opendhcp.ini +++ b/config/opendhcp.ini @@ -4,7 +4,7 @@ # own values below if you need change settings. -[LISTEN_ON] +#[LISTEN_ON] # Specify the Interfaces you would like Server to listen # if you have more than one NIC card on your server # always specify which cards will listen DHCP/DNS requests @@ -16,16 +16,16 @@ # 192.168.100.1 -[LOGGING] +#[LOGGING] # LogLevel can be set as None, Errors or All # It is advisable to keep logging to Normal, Normal include errors # and DHCP renewal messages. Normal is defaul logging also. ;LogLevel=None ;LogLevel=Normal -LogLevel=All +#LogLevel=All -[REPLICATION_SERVERS] +#[REPLICATION_SERVERS] # You can have 2 instances of Open DHCP Servers in a network. Open DHCP Server # will send replication inform messages to other instance of Open DHCP # server and leases will be replicated. The IP address alloted by one server @@ -35,11 +35,11 @@ LogLevel=All # Make sure that Primary & Secondary Server entries are identical on both # servers. You may copy the entire ini file on both servers and change the # LISTEN_ON on individual servers, if needed. -;Primary=192.168.0.253 -;Secondary=192.168.0.254 +#Primary=192.168.30.1 +#Secondary=192.168.30.3 -[HTTP_INTERFACE] +#[HTTP_INTERFACE] # This is http inerface for viewing lease status, # Default is 127.0.0.1:6789 # You can change it here to any network interface. @@ -107,30 +107,30 @@ LogLevel=All # with relay Agent IP. If not relay agent IP is sent, Listening Interface's IP # will be matched. You can also override the Target Relay Agent using TargetRelayAgent option. -[RANGE_SET] +#[RANGE_SET] # This is first and simple DHCP range section example, # This example may be good enough for simple/home use. # If you need range filters, look at example below -DHCPRange=192.168.30.110-192.168.30.120 +#DHCPRange=192.168.30.110-192.168.30.120 ;DHCPRange=192.168.5.1-192.168.5.254 # Following are range specific DHCP options. # You can copy more options names from [GLOBAL_OPTIONS] -SubnetMask=255.255.255.0 -DomainServer=114.114.114.114,8.8.8.8 +#SubnetMask=255.255.255.0 +#DomainServer=114.114.114.114,8.8.8.8 #Router is default gateway -Router=192.168.30.1 +#Router=192.168.30.1 # Lease Time can be different for this Range -;AddressTime=360 +#AddressTime=450 -[RANGE_SET] -DHCPRange=192.168.100.110-192.168.100.200 +#[RANGE_SET] +#DHCPRange=192.168.100.110-192.168.100.200 ;DHCPRange=192.168.5.1-192.168.5.254 # Following are range specific DHCP options. # You can copy more options names from [GLOBAL_OPTIONS] -SubnetMask=255.255.255.0 -DomainServer=114.114.114.114,8.8.8.8 +#SubnetMask=255.255.255.0 +#DomainServer=114.114.114.114,8.8.8.8 #Router is default gateway -Router=192.168.100.1 +#Router=192.168.100.1 [RANGE_SET] # This section is also simple [RANGE_SET] section @@ -149,7 +149,7 @@ Router=192.168.100.1 ;51=11000 -[RANGE_SET] +#[RANGE_SET] # This is filtered [RANGE_SET] section. # First eight entries in this example are filters. # Currently only following types of filters are supported diff --git a/config/vcpe.cfg b/config/vcpe.cfg index 67dc4b5..67df75c 100644 --- a/config/vcpe.cfg +++ b/config/vcpe.cfg @@ -91,22 +91,22 @@ application: # DHCP Server Config dhcp_server: { - listen_on = ["192.168.30.1", "192.168.100.1"]; + listen_on = ["192.168.30.1"]; # 主备服务器设置 - # replication = ["192.168.100.211", "192.168.100.215"]; + replication = ["192.168.30.1", "192.168.30.2"]; range_set: ( - { dhcp_range = "192.168.30.110-192.168.30.120"; + { dhcp_range = "192.168.30.150-192.168.30.155"; subnet_mask = "255.255.255.0"; domain_server = "114.114.114.114,8.8.8.8"; gateway = "192.168.30.1"; - lease_time = 360; + lease_time = 36000; }, { dhcp_range = "192.168.100.110-192.168.100.200"; subnet_mask = "255.255.255.0"; domain_server = "114.114.114.114,8.8.8.8"; gateway = "192.168.100.1"; - lease_time = 360; + lease_time = 36000; } ); }; diff --git a/config/vcpe.service b/config/vcpe.service index 0da3c38..533a1d7 100644 --- a/config/vcpe.service +++ b/config/vcpe.service @@ -12,7 +12,7 @@ Restart=always RestartSec=5 User=root WorkingDirectory=/home/jw/vcpe/srcs -ExecStart=/usr/bin/env /home/jw/vcpe/srcs/vcpe_main dhcpd -m -n vxlan0 -i ./config/opendhcp.ini -c ./config/vcpe.cfg -d ./config/ -k pqwAQ81rxg70aoy3v+Jjrw== +ExecStart=/usr/bin/env /home/jw/vcpe/srcs/vcpe_main dhcpd -m -n vxlan0 -c ./config/vcpe.cfg -d ./config/ -k pqwAQ81rxg70aoy3v+Jjrw== [Install] WantedBy=multi-user.target \ No newline at end of file diff --git a/srcs/open_dhcp/opendhcpd.cpp b/srcs/open_dhcp/opendhcpd.cpp index f0a3f03..c993225 100644 --- a/srcs/open_dhcp/opendhcpd.cpp +++ b/srcs/open_dhcp/opendhcpd.cpp @@ -56,7 +56,6 @@ data71 lump; bool kRunning = true; dhcpMap dhcpCache; char serviceName[] = "OpenDHCPServer"; -int ERR_PARAM; //char tempbuff[512] = ""; //char logBuff[256]; //char extbuff[256] = ""; @@ -3027,7 +3026,7 @@ void lockOptions(FILE *f) { } } -void addDHCPRange(char *dp) { +int addDHCPRange(char *dp) { char logBuff[256]; MYDWORD rs = 0; @@ -3053,8 +3052,7 @@ void addDHCPRange(char *dp) { (range->rangeEnd >= rs && range->rangeEnd <= re)) { sprintf(logBuff, "Warning: DHCP Range %s overlaps with another range, ignored", dp); logDHCPMess(logBuff, 1); - ERR_PARAM = ERR_ITEM_EXISTS; - return; + return ERR_ITEM_EXISTS; } } @@ -3077,18 +3075,21 @@ void addDHCPRange(char *dp) { sprintf(logBuff, "DHCP Ranges Load, Memory Allocation Error"); logDHCPMess(logBuff, 1); - ERR_PARAM = ERR_MALLOC_MEMORY; - return; + return ERR_MALLOC_MEMORY; } } } else { sprintf(logBuff, "Section [%s] Invalid DHCP range %s in ini file, ignored", RANGESET, dp); logDHCPMess(logBuff, 1); + return ERR_INPUT_PARAMS; } } else { sprintf(logBuff, "Section [%s] Invalid DHCP range %s in ini file, ignored", RANGESET, dp); logDHCPMess(logBuff, 1); + return ERR_INPUT_PARAMS; } + + return ERR_SUCCESS; } void addVendClass(MYBYTE rangeSetInd, char *vendClass, MYBYTE vendClassSize) { @@ -3236,6 +3237,8 @@ void loadDHCP() { cfig.mask = inet_addr("255.255.255.0"); } + opendhcp_add_ip_pool_set(); +#if 0 for (MYBYTE i = 1; i <= MAX_RANGE_SETS; i++) { if ((f = openSection(RANGESET, i))) { MYBYTE m = cfig.rangeCount; @@ -3260,7 +3263,7 @@ void loadDHCP() { break; } } - +#endif //printf("%s\n", IP2String(ipbuff, cfig.mask)); for (char rangeInd = 0; rangeInd < cfig.rangeCount; rangeInd++) { @@ -4337,9 +4340,9 @@ void *init(void *lparam) { FILE *f = nullptr; if ((f = openSection("LOGGING", 1))) { - cfig.dhcpLogLevel = 1; + cfig.dhcpLogLevel = 2; tempbuff[0] = 0; - +#if 0 while (readSection(raw, f)) { mySplit(name, value, raw, '='); @@ -4361,6 +4364,7 @@ void *init(void *lparam) { sprintf(tempbuff, "Section [LOGGING], Invalid Entry %s ignored", raw); } } +#endif if (tempbuff[0]) { logDHCPMess(logBuff, 1); } @@ -4447,6 +4451,8 @@ void *init(void *lparam) { getInterfaces(&newNetwork); memcpy(cfig.oldservers, newNetwork.staticServers, (MAX_SERVERS * sizeof(MYDWORD))); + opendhcp_set_replication_svr(); +#if 0 if ((f = openSection("REPLICATION_SERVERS", 1))) { while (readSection(raw, f)) { mySplit(name, value, raw, '='); @@ -4471,7 +4477,7 @@ void *init(void *lparam) { } } } - +#endif if (!cfig.zoneServers[0] && cfig.zoneServers[1]) { sprintf(logBuff, "Section [REPLICATION_SERVERS] Missing Primary Server"); logDHCPMess(logBuff, 1); @@ -5357,6 +5363,7 @@ void *logThread(void *lpParam) { } #endif +#if 0 void logDHCPMess(char *logBuff, MYBYTE logLevel) { // if (verbatim) // printf("%s\n", logBuff); @@ -5379,6 +5386,7 @@ void logDHCPMess(char *logBuff, MYBYTE logLevel) { // pthread_attr_destroy(&attr); // } } +#endif data7 *createCache(data71 *pLump) { MYWORD dataSize = 70 + sizeof(data7) + strlen(pLump->mapname) + pLump->optionSize; diff --git a/srcs/open_dhcp/opendhcpd.h b/srcs/open_dhcp/opendhcpd.h index 13dd46e..81e8839 100644 --- a/srcs/open_dhcp/opendhcpd.h +++ b/srcs/open_dhcp/opendhcpd.h @@ -72,6 +72,21 @@ typedef struct in_pktinfo IN_PKTINFO; #define STR2INT(val) ((int)strtol((val), nullptr, 10)) +#define logDHCPMess(logBuff, logLevel) \ + do { \ + switch ((logLevel)) { \ + case 2: \ + dzlog_debug("%s\n", logBuff); \ + break; \ + case 0: \ + dzlog_error("%s\n", logBuff); \ + break; \ + default: \ + dzlog_info("%s\n", logBuff); \ + break; \ + } \ + } while (0) + struct data7 //cache { char *mapname; @@ -573,7 +588,7 @@ MYDWORD sdmess(data9 *); MYDWORD sendRepl(data9 *req); MYDWORD *findServer(MYDWORD *, MYBYTE, MYDWORD); int getIndex(char, MYDWORD); -void addDHCPRange(char *dp); +int 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); @@ -587,7 +602,6 @@ 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 *); @@ -609,6 +623,8 @@ MYWORD qLen(char *); const char *getRequestMethod(const char *buffer); void prepareUserHtmlRespStatus(data19 *req); void opendhcp_init_http_server(); +void opendhcp_set_replication_svr(); +void opendhcp_add_ip_pool_set(); int opendhcp_add_listener(); void sendUserList(data19 *req, const char *pRequest, dhcpMap *dhcpCache, data2 *cfig, time_t t); void sendAllLists(data19 *req, bool kRunning, dhcpMap *dhcpCache, data2 *cfig); diff --git a/srcs/open_dhcp/query.cpp b/srcs/open_dhcp/query.cpp index 0bb508f..9995447 100644 --- a/srcs/open_dhcp/query.cpp +++ b/srcs/open_dhcp/query.cpp @@ -28,7 +28,6 @@ extern data2 cfig; extern bool kRunning; extern dhcpMap dhcpCache; extern time_t t; -extern int ERR_PARAM; static int dhcp_get_user_info(data19 *req, const char *pRequest) { char logBuff[512]; @@ -205,42 +204,55 @@ static int dhcp_get_all_user(data19 *req) { } \ } while (0) -static void add_options(OBJ_DHCP_RNG pRange, data20 *optionData) { +static int dhcp_add_rangeset_to_options(POBJ_DHCP_RNG pRange) { + int ret; char buff[1024]; MYBYTE hoption[256]; - MYBYTE *dp = optionData->options; + MYBYTE *dp; MYWORD buffSize = sizeof(dhcp_packet) - sizeof(dhcp_header); char value[256] = {0}; + MYBYTE *options = nullptr; + data20 optionData {}; + if (!pRange) { + return -ERR_INPUT_PARAMS; + } + + memset(&optionData, 0, sizeof(data20)); + + optionData.rangeSetInd = cfig.rangeCount; + cfig.rangeSet[cfig.rangeCount].active = true; + + dp = optionData.options; *dp = 0; dp++; //dhcp_range - addDHCPRange(pRange.rangAddr); - if(ERR_PARAM != ERR_SUCCESS){ - return; + ret = addDHCPRange(pRange->rangAddr); + if (ret != ERR_SUCCESS) { + return ret; } - if (strlen(pRange.subnet) != 0) { - strcpy(value, pRange.subnet); + if (strlen(pRange->subnet) != 0) { + strcpy(value, pRange->subnet); VALUE_TO_DHCP_TLV(buff, value, DHCP_OPTION_NETMASK); - optionData->mask = (*((MYDWORD *)value)); + optionData.mask = (*((MYDWORD *)value)); } - if (strlen(pRange.dnsSvr) != 0) { - strcpy(value, pRange.dnsSvr); + if (strlen(pRange->dnsSvr) != 0) { + strcpy(value, pRange->dnsSvr); VALUE_TO_DHCP_TLV(buff, value, DHCP_OPTION_DNS); } - if (strlen(pRange.gateway) != 0) { - strcpy(value, pRange.gateway); + if (strlen(pRange->gateway) != 0) { + strcpy(value, pRange->gateway); VALUE_TO_DHCP_TLV(buff, value, DHCP_OPTION_ROUTER); } //lease - if (pRange.lease != 0) { + if (pRange->lease != 0) { MYDWORD j; - j = pRange.lease; + j = pRange->lease; if (buffSize > 6) { *dp = DHCP_OPTION_IPADDRLEASE; dp++; @@ -252,18 +264,32 @@ static void add_options(OBJ_DHCP_RNG pRange, data20 *optionData) { *dp = DHCP_OPTION_END; dp++; - optionData->optionSize = (dp - optionData->options); + optionData.optionSize = (dp - optionData.options); + + if (optionData.optionSize > 3) { + options = (MYBYTE *)calloc(1, optionData.optionSize); + memcpy(options, optionData.options, optionData.optionSize); + } + + cfig.dhcpRanges[cfig.rangeCount].rangeSetInd = optionData.rangeSetInd; + cfig.dhcpRanges[cfig.rangeCount].options = options; + cfig.dhcpRanges[cfig.rangeCount].mask = optionData.mask; + if (!cfig.dhcpRanges[cfig.rangeCount].mask) { + cfig.dhcpRanges[cfig.rangeCount].mask = cfig.mask; + } + cfig.rangeCount = (char)(cfig.rangeCount + 1); + + return ERR_SUCCESS; } static int add_dhcpd_rangeset(data19 *req, const char *pRequest) { char logBuff[512]; const char *pStrContent; - OBJ_DHCP_RNG pRange; + OBJ_DHCP_RNG range; char *fp; cJSON *pRspRoot; cJSON *pExpandArray; - ERR_PARAM = ERR_SUCCESS; dzlog_debug("Input: %s\n", pRequest); if (pRequest == nullptr || strlen(pRequest) == 0) { @@ -305,6 +331,7 @@ static int add_dhcpd_rangeset(data19 *req, const char *pRequest) { cJSON_AddItemToObject(pRspRoot, "rangeSet", pExpandArray); for (int i = 0; i < cJSON_GetArraySize(prange_set); i++) { + int ret; cJSON *pItem = cJSON_GetArrayItem(prange_set, i); cJSON *pdhcp_range = cJSON_GetObjectItem(pItem, "dhcpRange"); cJSON *pEx_range = cJSON_CreateObject(); @@ -320,54 +347,34 @@ static int add_dhcpd_rangeset(data19 *req, const char *pRequest) { cJSON *pgateway = cJSON_GetObjectItem(pItem, "gateway"); cJSON *please_time = cJSON_GetObjectItem(pItem, "leaseTime"); - memset(&pRange, 0, sizeof(OBJ_DHCP_RNG)); + memset(&range, 0, sizeof(OBJ_DHCP_RNG)); - strcpy(pRange.rangAddr, pdhcp_range->valuestring); + strcpy(range.rangAddr, pdhcp_range->valuestring); if (psubnet_mask) { - strcpy(pRange.subnet, psubnet_mask->valuestring); + strcpy(range.subnet, psubnet_mask->valuestring); } if (pdomain_server) { - strcpy(pRange.dnsSvr, pdomain_server->valuestring); + strcpy(range.dnsSvr, pdomain_server->valuestring); } if (pgateway) { - strcpy(pRange.gateway, pgateway->valuestring); + strcpy(range.gateway, pgateway->valuestring); } if (please_time) { - pRange.lease = please_time->valueint; + range.lease = please_time->valueint; } //写入cfig - MYBYTE m = cfig.rangeCount; - data20 optionData {}; - optionData.rangeSetInd = m; - - MYBYTE *options = nullptr; - cfig.rangeSet[optionData.rangeSetInd].active = true; - - add_options(pRange, &optionData); - if(ERR_PARAM != ERR_SUCCESS){ - cJSON_AddNumberToObject(pEx_range, "status", ERR_PARAM); - cJSON_AddStringToObject(pEx_range, "message", getErrorEnumDesc(ERR_PARAM)); + ret = dhcp_add_rangeset_to_options(&range); + if (ret != ERR_SUCCESS) { + cJSON_AddNumberToObject(pEx_range, "status", ret); + cJSON_AddStringToObject(pEx_range, "message", getErrorEnumDesc(ret)); cJSON_AddItemToArray(pExpandArray, pEx_range); break; } - if (optionData.optionSize > 3) { - options = (MYBYTE *)calloc(1, optionData.optionSize); - memcpy(options, optionData.options, optionData.optionSize); - } - - cfig.dhcpRanges[m].rangeSetInd = optionData.rangeSetInd; - cfig.dhcpRanges[m].options = options; - cfig.dhcpRanges[m].mask = optionData.mask; - if (!cfig.dhcpRanges[m].mask) { - cfig.dhcpRanges[m].mask = cfig.mask; - } - cfig.rangeCount = (char)(m + 1); - cJSON_AddNumberToObject(pEx_range, "status", ERR_SUCCESS); cJSON_AddStringToObject(pEx_range, "message", getErrorEnumDesc(ERR_SUCCESS)); cJSON_AddItemToArray(pExpandArray, pEx_range); @@ -385,7 +392,7 @@ static int add_dhcpd_rangeset(data19 *req, const char *pRequest) { return ERR_SUCCESS; } -static int delete_dhcpd_rangeset(data19 *req, const char *pRequest){ +static int delete_dhcpd_rangeset(data19 *req, const char *pRequest) { char logBuff[512]; const char *pStrContent; char *fp; @@ -425,18 +432,18 @@ static int delete_dhcpd_rangeset(data19 *req, const char *pRequest){ return ERR_MALLOC_MEMORY; } - fp = req->dp; - pRspRoot = cJSON_CreateObject(); + fp = req->dp; + pRspRoot = cJSON_CreateObject(); pdelArray = cJSON_CreateArray(); cJSON_AddItemToObject(pRspRoot, "rangeSet", pdelArray); - for(int i = 0; i < cJSON_GetArraySize(pdhcp_range); i++){ - cJSON *pdelRange = cJSON_GetArrayItem(pdhcp_range, i); + for (int i = 0; i < cJSON_GetArraySize(pdhcp_range); i++) { + cJSON *pdelRange = cJSON_GetArrayItem(pdhcp_range, i); cJSON *pdel_Item = cJSON_CreateObject(); char del_range[256]; bool deleted_flag = false; - if(!pdelRange){ + if (!pdelRange) { continue; } @@ -444,42 +451,42 @@ static int delete_dhcpd_rangeset(data19 *req, const char *pRequest){ MYDWORD st_addr; MYDWORD en_addr; - char start[128]; - char end[128]; + char start[128]; + char end[128]; mySplit(start, end, del_range, '-'); st_addr = htonl(inet_addr(start)); en_addr = htonl(inet_addr(end)); cJSON_AddStringToObject(pdel_Item, "dhcpRange", pdelRange->valuestring); - if(st_addr && en_addr && st_addr <= en_addr){ - MYBYTE m = 0; + if (st_addr && en_addr && st_addr <= en_addr) { + MYBYTE m = 0; for (; m < cfig.rangeCount && cfig.dhcpRanges[m].rangeStart; m++) { - if(!deleted_flag){ - if((st_addr == cfig.dhcpRanges[m].rangeStart) && (en_addr == cfig.dhcpRanges[m].rangeEnd)){ + if (!deleted_flag) { + if ((st_addr == cfig.dhcpRanges[m].rangeStart) && (en_addr == cfig.dhcpRanges[m].rangeEnd)) { deleted_flag = true; - if(m == cfig.rangeCount-1){ + if (m == cfig.rangeCount - 1) { memset(&cfig.dhcpRanges[m], 0, sizeof(struct data19)); } continue; - }else if(st_addr == cfig.dhcpRanges[m].rangeStart||en_addr == cfig.dhcpRanges[m].rangeEnd){ + } else if (st_addr == cfig.dhcpRanges[m].rangeStart || en_addr == cfig.dhcpRanges[m].rangeEnd) { deleted_flag = false; break; } - }else{ - memcpy(&(cfig.dhcpRanges[m-1]), &(cfig.dhcpRanges[m]), sizeof(struct data19)); + } else { + memcpy(&(cfig.dhcpRanges[m - 1]), &(cfig.dhcpRanges[m]), sizeof(struct data19)); } } - }else{ + } else { cJSON_AddNumberToObject(pdel_Item, "status", ERR_INPUT_PARAMS); cJSON_AddStringToObject(pdel_Item, "message", getErrorEnumDesc(ERR_INPUT_PARAMS)); cJSON_AddItemToArray(pdelArray, pdel_Item); break; } - if(deleted_flag){ + if (deleted_flag) { cfig.rangeCount--; - }else{ + } else { cJSON_AddNumberToObject(pdel_Item, "status", ERR_INPUT_PARAMS); cJSON_AddStringToObject(pdel_Item, "message", getErrorEnumDesc(ERR_INPUT_PARAMS)); cJSON_AddItemToArray(pdelArray, pdel_Item); @@ -496,7 +503,7 @@ static int delete_dhcpd_rangeset(data19 *req, const char *pRequest){ cJSON_Delete(pRoot); req->bytes = (int)(fp - req->dp); - free((void*)pStrPro); + free((void *)pStrPro); return ERR_SUCCESS; } @@ -865,4 +872,33 @@ void opendhcp_init_http_server() { hw_http_add_route("dhcp/delete/rangeset", opendhcp_http_delete_rangeset, nullptr); added = TRUE; } +} + +/** + * 增加 DHCP 主、从服务器配置 + */ +void opendhcp_set_replication_svr() { + vector replication = config_get_dhcp_replication_svr(); + + if (replication && vect_size(replication) == 2) { + cfig.zoneServers[0] = inet_addr((const char *)vect_get_at(replication, 0)); + cfig.zoneServers[1] = inet_addr((const char *)vect_get_at(replication, 1)); + } +} + +/** + * 添加配置文件中的 DHCP 地址池池配置到服务中 + */ +void opendhcp_add_ip_pool_set() { + vector pool = config_get_dhcp_server_range_set(); + + for (int i = 0; (pool && i < vect_size(pool)); i++) { + auto pRange = (POBJ_DHCP_RNG)vect_get_at(pool, i); + + if (pRange) { + if (dhcp_add_rangeset_to_options(pRange) != ERR_SUCCESS) { + dzlog_error("Add rangeset(\"%s\") failed!", pRange->rangAddr); + } + } + } } \ No newline at end of file From 26dac02936efce48c51dd2bca29e63e92a73883e Mon Sep 17 00:00:00 2001 From: huangxin Date: Thu, 8 Dec 2022 15:21:26 +0800 Subject: [PATCH 9/9] =?UTF-8?q?OCT=201.=20=E6=9B=B4=E6=96=B0=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3=E9=94=99=E8=AF=AF=E7=A0=81=E8=BF=94=E5=9B=9E=E6=B6=88?= =?UTF-8?q?=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- srcs/libs/misc/err.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/srcs/libs/misc/err.c b/srcs/libs/misc/err.c index ba0a71d..4332930 100644 --- a/srcs/libs/misc/err.c +++ b/srcs/libs/misc/err.c @@ -8,10 +8,14 @@ static const char *g_enumStrVal[][MAX_DESC_LENGTH] = {DEF_ERR_CODE(GENERATE_STRING)}; -const char *getErrorEnumString(int errCode) { +const char *getErrorEnumNameString(int errCode) { return g_enumStrVal[errCode][0]; } const char *getErrorEnumDesc(int errCode) { + return g_enumStrVal[errCode][0]; +} + +const char *getErrorEnumDescripty(int errCode) { return g_enumStrVal[errCode][1]; }