Conflicts:
	srcs/open_dhcp/query.cpp
This commit is contained in:
dongwenze 2022-12-08 15:41:23 +08:00
commit 9e222f9481
17 changed files with 205 additions and 131 deletions

View File

@ -4,7 +4,7 @@
# own values below if you need change settings. # own values below if you need change settings.
[LISTEN_ON] #[LISTEN_ON]
# Specify the Interfaces you would like Server to listen # Specify the Interfaces you would like Server to listen
# if you have more than one NIC card on your server # if you have more than one NIC card on your server
# always specify which cards will listen DHCP/DNS requests # always specify which cards will listen DHCP/DNS requests
@ -16,16 +16,16 @@
# 192.168.100.1 # 192.168.100.1
[LOGGING] #[LOGGING]
# LogLevel can be set as None, Errors or All # LogLevel can be set as None, Errors or All
# It is advisable to keep logging to Normal, Normal include errors # It is advisable to keep logging to Normal, Normal include errors
# and DHCP renewal messages. Normal is defaul logging also. # and DHCP renewal messages. Normal is defaul logging also.
;LogLevel=None ;LogLevel=None
;LogLevel=Normal ;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 # 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 # will send replication inform messages to other instance of Open DHCP
# server and leases will be replicated. The IP address alloted by one server # 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 # 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 # servers. You may copy the entire ini file on both servers and change the
# LISTEN_ON on individual servers, if needed. # LISTEN_ON on individual servers, if needed.
;Primary=192.168.0.253 #Primary=192.168.30.1
;Secondary=192.168.0.254 #Secondary=192.168.30.3
[HTTP_INTERFACE] #[HTTP_INTERFACE]
# This is http inerface for viewing lease status, # This is http inerface for viewing lease status,
# Default is 127.0.0.1:6789 # Default is 127.0.0.1:6789
# You can change it here to any network interface. # 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 # 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. # 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 is first and simple DHCP range section example,
# This example may be good enough for simple/home use. # This example may be good enough for simple/home use.
# If you need range filters, look at example below # 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 ;DHCPRange=192.168.5.1-192.168.5.254
# Following are range specific DHCP options. # Following are range specific DHCP options.
# You can copy more options names from [GLOBAL_OPTIONS] # You can copy more options names from [GLOBAL_OPTIONS]
SubnetMask=255.255.255.0 #SubnetMask=255.255.255.0
DomainServer=114.114.114.114,8.8.8.8 #DomainServer=114.114.114.114,8.8.8.8
#Router is default gateway #Router is default gateway
Router=192.168.30.1 #Router=192.168.30.1
# Lease Time can be different for this Range # Lease Time can be different for this Range
;AddressTime=360 #AddressTime=450
[RANGE_SET] #[RANGE_SET]
DHCPRange=192.168.100.110-192.168.100.200 #DHCPRange=192.168.100.110-192.168.100.200
;DHCPRange=192.168.5.1-192.168.5.254 ;DHCPRange=192.168.5.1-192.168.5.254
# Following are range specific DHCP options. # Following are range specific DHCP options.
# You can copy more options names from [GLOBAL_OPTIONS] # You can copy more options names from [GLOBAL_OPTIONS]
SubnetMask=255.255.255.0 #SubnetMask=255.255.255.0
DomainServer=114.114.114.114,8.8.8.8 #DomainServer=114.114.114.114,8.8.8.8
#Router is default gateway #Router is default gateway
Router=192.168.100.1 #Router=192.168.100.1
[RANGE_SET] [RANGE_SET]
# This section is also simple [RANGE_SET] section # This section is also simple [RANGE_SET] section
@ -149,7 +149,7 @@ Router=192.168.100.1
;51=11000 ;51=11000
[RANGE_SET] #[RANGE_SET]
# This is filtered [RANGE_SET] section. # This is filtered [RANGE_SET] section.
# First eight entries in this example are filters. # First eight entries in this example are filters.
# Currently only following types of filters are supported # Currently only following types of filters are supported

View File

@ -91,22 +91,22 @@ application:
# DHCP Server Config # DHCP Server Config
dhcp_server: { 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: ( 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"; subnet_mask = "255.255.255.0";
domain_server = "114.114.114.114,8.8.8.8"; domain_server = "114.114.114.114,8.8.8.8";
gateway = "192.168.30.1"; gateway = "192.168.30.1";
lease_time = 360; lease_time = 36000;
}, },
{ dhcp_range = "192.168.100.110-192.168.100.200"; { dhcp_range = "192.168.100.110-192.168.100.200";
subnet_mask = "255.255.255.0"; subnet_mask = "255.255.255.0";
domain_server = "114.114.114.114,8.8.8.8"; domain_server = "114.114.114.114,8.8.8.8";
gateway = "192.168.100.1"; gateway = "192.168.100.1";
lease_time = 360; lease_time = 36000;
} }
); );
}; };

View File

@ -12,7 +12,7 @@ Restart=always
RestartSec=5 RestartSec=5
User=root User=root
WorkingDirectory=/home/jw/vcpe/srcs 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] [Install]
WantedBy=multi-user.target WantedBy=multi-user.target

View File

@ -14,10 +14,15 @@ FIND_PACKAGE(PkgConfig)
PKG_SEARCH_MODULE(LIBCURL REQUIRED libcurl) PKG_SEARCH_MODULE(LIBCURL REQUIRED libcurl)
PKG_SEARCH_MODULE(LIBSSL REQUIRED libssl) PKG_SEARCH_MODULE(LIBSSL REQUIRED libssl)
PKG_SEARCH_MODULE(LIBCRYPTO REQUIRED libcrypto) PKG_SEARCH_MODULE(LIBCRYPTO REQUIRED libcrypto)
LIST(APPEND COMMON_LIBS "${LIBCURL_LDFLAGS} ${LIBSSL_LDFLAGS} ${LIBCRYPTO_LDFLAGS}") PKG_SEARCH_MODULE(LIBUV REQUIRED libuv)
LIST(APPEND COMMON_LIBS "-ldl -lpthread -lzlog -lm -luv -lzmq -luuid -lconfig") 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}) ADD_DEFINITIONS(${COMMON_DEFINE})

View File

@ -7,6 +7,7 @@
#include "http_response_cache.h" #include "http_response_cache.h"
static bool tcp_nodelay; 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) { void ipc_read_cb(uv_stream_t *handle, ssize_t nread, const uv_buf_t *buf) {
int rc; int rc;

View File

@ -7,6 +7,8 @@
#include "http_response_cache.h" #include "http_response_cache.h"
#include "hw_string.h" #include "hw_string.h"
extern http_server_write_response_cb http_server_write_response;
#define CRLF "\r\n" #define CRLF "\r\n"
KHASH_MAP_INIT_STR(string_hashmap, char *) 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 header_buffer_incr = 512;
int body_size = resp->body.length; int body_size = resp->body.length;
int header_size_remaining = header_buffer_incr; 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 int response_size = header_size_remaining + sizeof(length_header) + num_chars(resp->body.length) +
+ body_size + line_sep_size; 2 * line_sep_size + body_size + line_sep_size;
response_string->value = malloc(response_size); response_string->value = malloc(response_size);

View File

@ -1,6 +1,6 @@
#ifdef PLATFORM_POSIX #ifdef PLATFORM_POSIX
#include <signal.h> #include <signal.h>
#endif// PLATFORM_POSIX #endif // PLATFORM_POSIX
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
@ -39,15 +39,17 @@ static uv_tcp_t server;
static http_parser_settings parser_settings; static http_parser_settings parser_settings;
static struct sockaddr_in listen_address; static struct sockaddr_in listen_address;
uv_loop_t *uv_loop; uv_loop_t *uv_loop;
void *routes; void *routes;
hw_string *http_v1_0; hw_string *http_v1_0;
hw_string *http_v1_1; hw_string *http_v1_1;
hw_string *server_name; hw_string *server_name;
int listener_count; int listener_count;
uv_async_t *listener_async_handles; uv_async_t *listener_async_handles;
uv_loop_t *listener_event_loops; uv_loop_t *listener_event_loops;
uv_barrier_t *listeners_created_barrier; 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) { int hw_init_with_config(configuration *c) {
#ifdef DEBUG #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); routes = kh_init(string_hashmap);
} }
set_route(routes, route, route_entry); 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() { void free_http_server() {
@ -145,13 +147,13 @@ void free_http_server() {
free((char *)v); free((char *)v);
}); });
kh_destroy(string_hashmap, routes); kh_destroy(string_hashmap, routes);
uv_close((uv_handle_t*)&server, NULL); uv_close((uv_handle_t *)&server, NULL);
uninit_http_request_cache(); uninit_http_request_cache();
dzlog_debug("HTTP Server Close http://%s:%d\n", config->http_listen_address, config->http_listen_port); dzlog_debug("HTTP Server Close http://%s:%d\n", config->http_listen_address, config->http_listen_port);
} }
int hw_http_open() { int hw_http_open() {
int threads = (int)config->thread_count; int threads = (int)config->thread_count;
static uv_async_t service_handle; static uv_async_t service_handle;
if (routes == NULL) { if (routes == NULL) {
@ -168,7 +170,7 @@ int hw_http_open() {
#ifdef UNIX #ifdef UNIX
signal(SIGPIPE, SIG_IGN); signal(SIGPIPE, SIG_IGN);
#endif// UNIX #endif // UNIX
listener_count = threads; listener_count = threads;

View File

@ -25,8 +25,7 @@ extern uv_async_t *listener_async_handles;
extern uv_loop_t *listener_event_loops; extern uv_loop_t *listener_event_loops;
extern uv_barrier_t *listeners_created_barrier; extern uv_barrier_t *listeners_created_barrier;
void (*http_stream_on_read)(uv_stream_t *, ssize_t, const uv_buf_t *); typedef int (*http_server_write_response_cb)(hw_write_context *, hw_string *);
int (*http_server_write_response)(hw_write_context *, hw_string *);
http_connection *create_http_connection(); http_connection *create_http_connection();
void http_stream_on_connect(uv_stream_t *stream, int status); void http_stream_on_connect(uv_stream_t *stream, int status);

View File

@ -36,7 +36,7 @@ static int disk_info_refresh() {
int i = 0; int i = 0;
int errCode = ERR_SUCCESS; int errCode = ERR_SUCCESS;
FILE *fp; FILE *fp;
char buf[MAX_PATH]; char buf[MAX_PATH * 2];
fp = popen(CMD_DISK_FILESYSTEM, "r"); fp = popen(CMD_DISK_FILESYSTEM, "r");

View File

@ -44,7 +44,7 @@ unsigned int get_sys_total_memory() {
static int memory_info_refresh() { static int memory_info_refresh() {
int errCode = ERR_SUCCESS; int errCode = ERR_SUCCESS;
FILE *fp; FILE *fp;
char buf[MAX_PATH]; char buf[MAX_PATH * 2];
fp = popen(CMD_MEMORY_INFO, "r"); fp = popen(CMD_MEMORY_INFO, "r");

View File

@ -8,7 +8,7 @@
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif
#include <cjson/cJSON.h> #include <s2j/cJSON.h>
typedef enum { typedef enum {
CRYPTO_NONE = 0, CRYPTO_NONE = 0,

View File

@ -8,10 +8,14 @@
static const char *g_enumStrVal[][MAX_DESC_LENGTH] = {DEF_ERR_CODE(GENERATE_STRING)}; 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]; return g_enumStrVal[errCode][0];
} }
const char *getErrorEnumDesc(int errCode) { const char *getErrorEnumDesc(int errCode) {
return g_enumStrVal[errCode][0];
}
const char *getErrorEnumDescripty(int errCode) {
return g_enumStrVal[errCode][1]; return g_enumStrVal[errCode][1];
} }

View File

@ -701,6 +701,8 @@ static zvect_retval p_vect_set_capacity(vector const v, const zvect_index direct
return ZVERR_OUTOFMEM; return ZVERR_OUTOFMEM;
} }
memset(new_data, 0, sizeof(void *) * (new_capacity + v->cap_right));
zvect_index nb; zvect_index nb;
zvect_index ne; zvect_index ne;
nb = v->cap_left; nb = v->cap_left;

View File

@ -1,7 +1,6 @@
// //
// Created by xajhu on 2021/8/11 0011. // Created by xajhu on 2021/8/11 0011.
// //
#include <jemalloc/jemalloc.h>
#include <uv.h> #include <uv.h>
#include <sds/sds.h> #include <sds/sds.h>

View File

@ -56,7 +56,6 @@ data71 lump;
bool kRunning = true; bool kRunning = true;
dhcpMap dhcpCache; dhcpMap dhcpCache;
char serviceName[] = "OpenDHCPServer"; char serviceName[] = "OpenDHCPServer";
int ERR_PARAM;
//char tempbuff[512] = ""; //char tempbuff[512] = "";
//char logBuff[256]; //char logBuff[256];
//char extbuff[256] = ""; //char extbuff[256] = "";
@ -3027,7 +3026,7 @@ void lockOptions(FILE *f) {
} }
} }
void addDHCPRange(char *dp) { int addDHCPRange(char *dp) {
char logBuff[256]; char logBuff[256];
MYDWORD rs = 0; MYDWORD rs = 0;
@ -3053,8 +3052,7 @@ void addDHCPRange(char *dp) {
(range->rangeEnd >= rs && range->rangeEnd <= re)) { (range->rangeEnd >= rs && range->rangeEnd <= re)) {
sprintf(logBuff, "Warning: DHCP Range %s overlaps with another range, ignored", dp); sprintf(logBuff, "Warning: DHCP Range %s overlaps with another range, ignored", dp);
logDHCPMess(logBuff, 1); logDHCPMess(logBuff, 1);
ERR_PARAM = ERR_ITEM_EXISTS; return ERR_ITEM_EXISTS;
return;
} }
} }
@ -3077,18 +3075,21 @@ void addDHCPRange(char *dp) {
sprintf(logBuff, "DHCP Ranges Load, Memory Allocation Error"); sprintf(logBuff, "DHCP Ranges Load, Memory Allocation Error");
logDHCPMess(logBuff, 1); logDHCPMess(logBuff, 1);
ERR_PARAM = ERR_MALLOC_MEMORY; return ERR_MALLOC_MEMORY;
return;
} }
} }
} else { } else {
sprintf(logBuff, "Section [%s] Invalid DHCP range %s in ini file, ignored", RANGESET, dp); sprintf(logBuff, "Section [%s] Invalid DHCP range %s in ini file, ignored", RANGESET, dp);
logDHCPMess(logBuff, 1); logDHCPMess(logBuff, 1);
return ERR_INPUT_PARAMS;
} }
} else { } else {
sprintf(logBuff, "Section [%s] Invalid DHCP range %s in ini file, ignored", RANGESET, dp); sprintf(logBuff, "Section [%s] Invalid DHCP range %s in ini file, ignored", RANGESET, dp);
logDHCPMess(logBuff, 1); logDHCPMess(logBuff, 1);
return ERR_INPUT_PARAMS;
} }
return ERR_SUCCESS;
} }
void addVendClass(MYBYTE rangeSetInd, char *vendClass, MYBYTE vendClassSize) { void addVendClass(MYBYTE rangeSetInd, char *vendClass, MYBYTE vendClassSize) {
@ -3236,6 +3237,8 @@ void loadDHCP() {
cfig.mask = inet_addr("255.255.255.0"); cfig.mask = inet_addr("255.255.255.0");
} }
opendhcp_add_ip_pool_set();
#if 0
for (MYBYTE i = 1; i <= MAX_RANGE_SETS; i++) { for (MYBYTE i = 1; i <= MAX_RANGE_SETS; i++) {
if ((f = openSection(RANGESET, i))) { if ((f = openSection(RANGESET, i))) {
MYBYTE m = cfig.rangeCount; MYBYTE m = cfig.rangeCount;
@ -3260,7 +3263,7 @@ void loadDHCP() {
break; break;
} }
} }
#endif
//printf("%s\n", IP2String(ipbuff, cfig.mask)); //printf("%s\n", IP2String(ipbuff, cfig.mask));
for (char rangeInd = 0; rangeInd < cfig.rangeCount; rangeInd++) { for (char rangeInd = 0; rangeInd < cfig.rangeCount; rangeInd++) {
@ -4337,9 +4340,9 @@ void *init(void *lparam) {
FILE *f = nullptr; FILE *f = nullptr;
if ((f = openSection("LOGGING", 1))) { if ((f = openSection("LOGGING", 1))) {
cfig.dhcpLogLevel = 1; cfig.dhcpLogLevel = 2;
tempbuff[0] = 0; tempbuff[0] = 0;
#if 0
while (readSection(raw, f)) { while (readSection(raw, f)) {
mySplit(name, value, raw, '='); mySplit(name, value, raw, '=');
@ -4361,6 +4364,7 @@ void *init(void *lparam) {
sprintf(tempbuff, "Section [LOGGING], Invalid Entry %s ignored", raw); sprintf(tempbuff, "Section [LOGGING], Invalid Entry %s ignored", raw);
} }
} }
#endif
if (tempbuff[0]) { if (tempbuff[0]) {
logDHCPMess(logBuff, 1); logDHCPMess(logBuff, 1);
} }
@ -4447,6 +4451,8 @@ void *init(void *lparam) {
getInterfaces(&newNetwork); getInterfaces(&newNetwork);
memcpy(cfig.oldservers, newNetwork.staticServers, (MAX_SERVERS * sizeof(MYDWORD))); memcpy(cfig.oldservers, newNetwork.staticServers, (MAX_SERVERS * sizeof(MYDWORD)));
opendhcp_set_replication_svr();
#if 0
if ((f = openSection("REPLICATION_SERVERS", 1))) { if ((f = openSection("REPLICATION_SERVERS", 1))) {
while (readSection(raw, f)) { while (readSection(raw, f)) {
mySplit(name, value, raw, '='); mySplit(name, value, raw, '=');
@ -4471,7 +4477,7 @@ void *init(void *lparam) {
} }
} }
} }
#endif
if (!cfig.zoneServers[0] && cfig.zoneServers[1]) { if (!cfig.zoneServers[0] && cfig.zoneServers[1]) {
sprintf(logBuff, "Section [REPLICATION_SERVERS] Missing Primary Server"); sprintf(logBuff, "Section [REPLICATION_SERVERS] Missing Primary Server");
logDHCPMess(logBuff, 1); logDHCPMess(logBuff, 1);
@ -5357,6 +5363,7 @@ void *logThread(void *lpParam) {
} }
#endif #endif
#if 0
void logDHCPMess(char *logBuff, MYBYTE logLevel) { void logDHCPMess(char *logBuff, MYBYTE logLevel) {
// if (verbatim) // if (verbatim)
// printf("%s\n", logBuff); // printf("%s\n", logBuff);
@ -5379,6 +5386,7 @@ void logDHCPMess(char *logBuff, MYBYTE logLevel) {
// pthread_attr_destroy(&attr); // pthread_attr_destroy(&attr);
// } // }
} }
#endif
data7 *createCache(data71 *pLump) { data7 *createCache(data71 *pLump) {
MYWORD dataSize = 70 + sizeof(data7) + strlen(pLump->mapname) + pLump->optionSize; MYWORD dataSize = 70 + sizeof(data7) + strlen(pLump->mapname) + pLump->optionSize;

View File

@ -72,6 +72,21 @@ typedef struct in_pktinfo IN_PKTINFO;
#define STR2INT(val) ((int)strtol((val), nullptr, 10)) #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 struct data7 //cache
{ {
char *mapname; char *mapname;
@ -573,7 +588,7 @@ MYDWORD sdmess(data9 *);
MYDWORD sendRepl(data9 *req); MYDWORD sendRepl(data9 *req);
MYDWORD *findServer(MYDWORD *, MYBYTE, MYDWORD); MYDWORD *findServer(MYDWORD *, MYBYTE, MYDWORD);
int getIndex(char, MYDWORD); int getIndex(char, MYDWORD);
void addDHCPRange(char *dp); int addDHCPRange(char *dp);
void addVendClass(MYBYTE rangeSetInd, char *vendClass, MYBYTE vendClassSize); void addVendClass(MYBYTE rangeSetInd, char *vendClass, MYBYTE vendClassSize);
void addUserClass(MYBYTE rangeSetInd, char *userClass, MYBYTE userClassSize); void addUserClass(MYBYTE rangeSetInd, char *userClass, MYBYTE userClassSize);
void addMacRange(MYBYTE rangeSetInd, char *macRange); void addMacRange(MYBYTE rangeSetInd, char *macRange);
@ -587,7 +602,6 @@ void getSecondary();
void *init(void *); void *init(void *);
void lockOptions(FILE *); void lockOptions(FILE *);
void loadOptions(FILE *, const char *, data20 *); void loadOptions(FILE *, const char *, data20 *);
void logDHCPMess(char *, MYBYTE);
void mySplit(char *, char *, const char *, char); void mySplit(char *, char *, const char *, char);
void *sendHTTP(void *lpParam); void *sendHTTP(void *lpParam);
void procHTTP(data19 *); void procHTTP(data19 *);
@ -609,6 +623,8 @@ MYWORD qLen(char *);
const char *getRequestMethod(const char *buffer); const char *getRequestMethod(const char *buffer);
void prepareUserHtmlRespStatus(data19 *req); void prepareUserHtmlRespStatus(data19 *req);
void opendhcp_init_http_server(); void opendhcp_init_http_server();
void opendhcp_set_replication_svr();
void opendhcp_add_ip_pool_set();
int opendhcp_add_listener(); int opendhcp_add_listener();
void sendUserList(data19 *req, const char *pRequest, dhcpMap *dhcpCache, data2 *cfig, time_t t); void sendUserList(data19 *req, const char *pRequest, dhcpMap *dhcpCache, data2 *cfig, time_t t);
void sendAllLists(data19 *req, bool kRunning, dhcpMap *dhcpCache, data2 *cfig); void sendAllLists(data19 *req, bool kRunning, dhcpMap *dhcpCache, data2 *cfig);

View File

@ -28,7 +28,6 @@ extern data2 cfig;
extern bool kRunning; extern bool kRunning;
extern dhcpMap dhcpCache; extern dhcpMap dhcpCache;
extern time_t t; extern time_t t;
extern int ERR_PARAM;
static int dhcp_get_user_info(data19 *req, const char *pRequest) { static int dhcp_get_user_info(data19 *req, const char *pRequest) {
char logBuff[512]; char logBuff[512];
@ -205,42 +204,55 @@ static int dhcp_get_all_user(data19 *req) {
} \ } \
} while (0) } 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]; char buff[1024];
MYBYTE hoption[256]; MYBYTE hoption[256];
MYBYTE *dp = optionData->options; MYBYTE *dp;
MYWORD buffSize = sizeof(dhcp_packet) - sizeof(dhcp_header); MYWORD buffSize = sizeof(dhcp_packet) - sizeof(dhcp_header);
char value[256] = {0}; 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 = 0;
dp++; dp++;
//dhcp_range //dhcp_range
addDHCPRange(pRange.rangAddr); ret = addDHCPRange(pRange->rangAddr);
if(ERR_PARAM != ERR_SUCCESS){ if (ret != ERR_SUCCESS) {
return; return ret;
} }
if (strlen(pRange.subnet) != 0) { if (strlen(pRange->subnet) != 0) {
strcpy(value, pRange.subnet); strcpy(value, pRange->subnet);
VALUE_TO_DHCP_TLV(buff, value, DHCP_OPTION_NETMASK); VALUE_TO_DHCP_TLV(buff, value, DHCP_OPTION_NETMASK);
optionData->mask = (*((MYDWORD *)value)); optionData.mask = (*((MYDWORD *)value));
} }
if (strlen(pRange.dnsSvr) != 0) { if (strlen(pRange->dnsSvr) != 0) {
strcpy(value, pRange.dnsSvr); strcpy(value, pRange->dnsSvr);
VALUE_TO_DHCP_TLV(buff, value, DHCP_OPTION_DNS); VALUE_TO_DHCP_TLV(buff, value, DHCP_OPTION_DNS);
} }
if (strlen(pRange.gateway) != 0) { if (strlen(pRange->gateway) != 0) {
strcpy(value, pRange.gateway); strcpy(value, pRange->gateway);
VALUE_TO_DHCP_TLV(buff, value, DHCP_OPTION_ROUTER); VALUE_TO_DHCP_TLV(buff, value, DHCP_OPTION_ROUTER);
} }
//lease //lease
if (pRange.lease != 0) { if (pRange->lease != 0) {
MYDWORD j; MYDWORD j;
j = pRange.lease; j = pRange->lease;
if (buffSize > 6) { if (buffSize > 6) {
*dp = DHCP_OPTION_IPADDRLEASE; *dp = DHCP_OPTION_IPADDRLEASE;
dp++; dp++;
@ -252,18 +264,32 @@ static void add_options(OBJ_DHCP_RNG pRange, data20 *optionData) {
*dp = DHCP_OPTION_END; *dp = DHCP_OPTION_END;
dp++; 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) { static int add_dhcpd_rangeset(data19 *req, const char *pRequest) {
char logBuff[512]; char logBuff[512];
const char *pStrContent; const char *pStrContent;
OBJ_DHCP_RNG pRange; OBJ_DHCP_RNG range;
char *fp; char *fp;
cJSON *pRspRoot; cJSON *pRspRoot;
cJSON *pExpandArray; cJSON *pExpandArray;
ERR_PARAM = ERR_SUCCESS;
dzlog_debug("Input: %s\n", pRequest); dzlog_debug("Input: %s\n", pRequest);
if (pRequest == nullptr || strlen(pRequest) == 0) { 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); cJSON_AddItemToObject(pRspRoot, "rangeSet", pExpandArray);
for (int i = 0; i < cJSON_GetArraySize(prange_set); i++) { for (int i = 0; i < cJSON_GetArraySize(prange_set); i++) {
int ret;
cJSON *pItem = cJSON_GetArrayItem(prange_set, i); cJSON *pItem = cJSON_GetArrayItem(prange_set, i);
cJSON *pdhcp_range = cJSON_GetObjectItem(pItem, "dhcpRange"); cJSON *pdhcp_range = cJSON_GetObjectItem(pItem, "dhcpRange");
cJSON *pEx_range = cJSON_CreateObject(); 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 *pgateway = cJSON_GetObjectItem(pItem, "gateway");
cJSON *please_time = cJSON_GetObjectItem(pItem, "leaseTime"); 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) { if (psubnet_mask) {
strcpy(pRange.subnet, psubnet_mask->valuestring); strcpy(range.subnet, psubnet_mask->valuestring);
} }
if (pdomain_server) { if (pdomain_server) {
strcpy(pRange.dnsSvr, pdomain_server->valuestring); strcpy(range.dnsSvr, pdomain_server->valuestring);
} }
if (pgateway) { if (pgateway) {
strcpy(pRange.gateway, pgateway->valuestring); strcpy(range.gateway, pgateway->valuestring);
} }
if (please_time) { if (please_time) {
pRange.lease = please_time->valueint; range.lease = please_time->valueint;
} }
//写入cfig //写入cfig
MYBYTE m = cfig.rangeCount; ret = dhcp_add_rangeset_to_options(&range);
data20 optionData {}; if (ret != ERR_SUCCESS) {
optionData.rangeSetInd = m; cJSON_AddNumberToObject(pEx_range, "status", ret);
cJSON_AddStringToObject(pEx_range, "message", getErrorEnumDesc(ret));
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));
cJSON_AddItemToArray(pExpandArray, pEx_range); cJSON_AddItemToArray(pExpandArray, pEx_range);
break; 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_AddNumberToObject(pEx_range, "status", ERR_SUCCESS);
cJSON_AddStringToObject(pEx_range, "message", getErrorEnumDesc(ERR_SUCCESS)); cJSON_AddStringToObject(pEx_range, "message", getErrorEnumDesc(ERR_SUCCESS));
cJSON_AddItemToArray(pExpandArray, pEx_range); cJSON_AddItemToArray(pExpandArray, pEx_range);
@ -385,7 +392,7 @@ static int add_dhcpd_rangeset(data19 *req, const char *pRequest) {
return ERR_SUCCESS; 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]; char logBuff[512];
const char *pStrContent; const char *pStrContent;
char *fp; char *fp;
@ -425,18 +432,18 @@ static int delete_dhcpd_rangeset(data19 *req, const char *pRequest){
return ERR_MALLOC_MEMORY; return ERR_MALLOC_MEMORY;
} }
fp = req->dp; fp = req->dp;
pRspRoot = cJSON_CreateObject(); pRspRoot = cJSON_CreateObject();
pdelArray = cJSON_CreateArray(); pdelArray = cJSON_CreateArray();
cJSON_AddItemToObject(pRspRoot, "rangeSet", pdelArray); cJSON_AddItemToObject(pRspRoot, "rangeSet", pdelArray);
for(int i = 0; i < cJSON_GetArraySize(pdhcp_range); i++){ for (int i = 0; i < cJSON_GetArraySize(pdhcp_range); i++) {
cJSON *pdelRange = cJSON_GetArrayItem(pdhcp_range, i); cJSON *pdelRange = cJSON_GetArrayItem(pdhcp_range, i);
cJSON *pdel_Item = cJSON_CreateObject(); cJSON *pdel_Item = cJSON_CreateObject();
char del_range[256]; char del_range[256];
bool deleted_flag = false; bool deleted_flag = false;
if(!pdelRange){ if (!pdelRange) {
continue; continue;
} }
@ -444,43 +451,43 @@ static int delete_dhcpd_rangeset(data19 *req, const char *pRequest){
MYDWORD st_addr; MYDWORD st_addr;
MYDWORD en_addr; MYDWORD en_addr;
char start[128]; char start[128];
char end[128]; char end[128];
mySplit(start, end, del_range, '-'); mySplit(start, end, del_range, '-');
st_addr = htonl(inet_addr(start)); st_addr = htonl(inet_addr(start));
en_addr = htonl(inet_addr(end)); en_addr = htonl(inet_addr(end));
cJSON_AddStringToObject(pdel_Item, "dhcpRange", pdelRange->valuestring); cJSON_AddStringToObject(pdel_Item, "dhcpRange", pdelRange->valuestring);
if(st_addr && en_addr && st_addr <= en_addr){ if (st_addr && en_addr && st_addr <= en_addr) {
MYBYTE m = 0; MYBYTE m = 0;
for (; m < cfig.rangeCount && cfig.dhcpRanges[m].rangeStart; m++) { for (; m < cfig.rangeCount && cfig.dhcpRanges[m].rangeStart; m++) {
if(!deleted_flag){ if (!deleted_flag) {
if((st_addr == cfig.dhcpRanges[m].rangeStart) && (en_addr == cfig.dhcpRanges[m].rangeEnd)){ if ((st_addr == cfig.dhcpRanges[m].rangeStart) && (en_addr == cfig.dhcpRanges[m].rangeEnd)) {
deleted_flag = true; deleted_flag = true;
if(m == cfig.rangeCount-1){ if (m == cfig.rangeCount - 1) {
memset(&cfig.dhcpRanges[m], 0, sizeof(struct data19)); memset(&cfig.dhcpRanges[m], 0, sizeof(struct data19));
} }
continue; 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; deleted_flag = false;
break; break;
} }
}else{ } else {
cfig.dhcpRanges[m].rangeSetInd = m-1; cfig.dhcpRanges[m].rangeSetInd = m-1;
memcpy(&(cfig.dhcpRanges[m-1]), &(cfig.dhcpRanges[m]), sizeof(struct data19)); memcpy(&(cfig.dhcpRanges[m - 1]), &(cfig.dhcpRanges[m]), sizeof(struct data19));
} }
} }
}else{ } else {
cJSON_AddNumberToObject(pdel_Item, "status", ERR_INPUT_PARAMS); cJSON_AddNumberToObject(pdel_Item, "status", ERR_INPUT_PARAMS);
cJSON_AddStringToObject(pdel_Item, "message", getErrorEnumDesc(ERR_INPUT_PARAMS)); cJSON_AddStringToObject(pdel_Item, "message", getErrorEnumDesc(ERR_INPUT_PARAMS));
cJSON_AddItemToArray(pdelArray, pdel_Item); cJSON_AddItemToArray(pdelArray, pdel_Item);
break; break;
} }
if(deleted_flag){ if (deleted_flag) {
cfig.rangeCount--; cfig.rangeCount--;
}else{ } else {
cJSON_AddNumberToObject(pdel_Item, "status", ERR_INPUT_PARAMS); cJSON_AddNumberToObject(pdel_Item, "status", ERR_INPUT_PARAMS);
cJSON_AddStringToObject(pdel_Item, "message", getErrorEnumDesc(ERR_INPUT_PARAMS)); cJSON_AddStringToObject(pdel_Item, "message", getErrorEnumDesc(ERR_INPUT_PARAMS));
cJSON_AddItemToArray(pdelArray, pdel_Item); cJSON_AddItemToArray(pdelArray, pdel_Item);
@ -497,7 +504,7 @@ static int delete_dhcpd_rangeset(data19 *req, const char *pRequest){
cJSON_Delete(pRoot); cJSON_Delete(pRoot);
req->bytes = (int)(fp - req->dp); req->bytes = (int)(fp - req->dp);
free((void*)pStrPro); free((void *)pStrPro);
return ERR_SUCCESS; return ERR_SUCCESS;
} }
@ -1020,3 +1027,32 @@ void opendhcp_init_http_server() {
added = TRUE; 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);
}
}
}
}