From 77b0fc884354ef84be84bb971d1efdcff5d825de Mon Sep 17 00:00:00 2001 From: huangxin Date: Mon, 12 Dec 2022 16:00:39 +0800 Subject: [PATCH] =?UTF-8?q?OCT=201.=20=E9=87=8D=E5=91=BD=E5=90=8D=20c=20ve?= =?UTF-8?q?ctor=20=E9=81=BF=E5=85=8D=E5=92=8Cstd=E5=BA=93=E5=86=B2?= =?UTF-8?q?=E7=AA=81=202.=20=E5=A2=9E=E5=8A=A0=E5=85=A8=E5=B1=80=E7=A7=9F?= =?UTF-8?q?=E7=BA=A6=E9=85=8D=E7=BD=AE=E9=A1=B9=203.=20=E5=A2=9E=E5=8A=A0D?= =?UTF-8?q?HCP=20MAC=E5=9C=B0=E5=9D=80=E9=BB=91=E5=90=8D=E5=8D=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config/vcpe.cfg | 7 +- srcs/libs/configure/config.c | 9 +- srcs/libs/configure/config_help.c | 14 +- srcs/libs/include/config.h | 15 +- srcs/libs/include/misc.h | 6 +- srcs/libs/include/sds/sds.h | 10 + srcs/libs/include/zvector/zvector.h | 320 +++++++++++---------- srcs/libs/include/zvector/zvector_config.h | 8 +- srcs/libs/misc/zvector.c | 183 ++++++------ srcs/open_dhcp/opendhcpd.cpp | 10 +- srcs/open_dhcp/opendhcpd.h | 210 +++++++------- srcs/open_dhcp/query.cpp | 88 ++++-- 12 files changed, 497 insertions(+), 383 deletions(-) diff --git a/config/vcpe.cfg b/config/vcpe.cfg index 67df75c..4ad71d4 100644 --- a/config/vcpe.cfg +++ b/config/vcpe.cfg @@ -91,10 +91,15 @@ application: # DHCP Server Config dhcp_server: { + # 全局租约时间 + lease_time = 86400; + # 监听网卡 listen_on = ["192.168.30.1"]; # 主备服务器设置 replication = ["192.168.30.1", "192.168.30.2"]; - + # MAC地址黑名单 + mac_filter = ["00:01:02:03:04:07", "00:01:02:03:04:01"]; + # IP地址池配置 range_set: ( { dhcp_range = "192.168.30.150-192.168.30.155"; subnet_mask = "255.255.255.0"; diff --git a/srcs/libs/configure/config.c b/srcs/libs/configure/config.c index 26b01a5..632c17c 100644 --- a/srcs/libs/configure/config.c +++ b/srcs/libs/configure/config.c @@ -34,7 +34,7 @@ typedef union { long long longValue; char *strValue; long double floatValue; - vector array; + c_vector array; } CFG_VALUE, *PCFG_VALUE; typedef struct { @@ -113,11 +113,13 @@ static CFG_ITEM g_cfgItem[] = { DEF_CFG_ITEM(CFG_PROTO_CRYPTO_KEY, "protocol.crypto_key", VAL_STR, "", "Protocol crypto keys"), #ifdef OPENDHCPD_ON // 配置DHCP服务器 + DEF_CFG_ITEM(CFG_DHCP_LEASE_TIME, "dhcp_server.lease_time", VAL_INT, "36000", "DHCP server lease time"), DEF_CFG_ITEM(CFG_DHCP_LISTEN_ON, "dhcp_server.listen_on", VAL_ARRAY_STR, "", "DHCP listen interface"), DEF_CFG_ITEM(CFG_DHCP_REPLICATION_SVR, "dhcp_server.replication", VAL_ARRAY_STR, "", "DHCP replication server configure"), DEF_CFG_ITEM(CFG_DHCP_RANGE_SET, "dhcp_server.range_set", VAL_ARRAY_OBJ, "", "DHCP IP pool"), + DEF_CFG_ITEM(CFG_DHCP_MAC_FILTER, "dhcp_server.mac_filter", VAL_ARRAY_STR, "", "DHCP client MAC address black list"), #endif -}; +}; // clang-format on static int cfg_is_upgrade(PCONFIG_ITEM pItem) { @@ -128,7 +130,6 @@ static int cfg_is_upgrade(PCONFIG_ITEM pItem) { return FALSE; } -// clang-format on static const char *load_string_value(const char *pKeyName) { const char *pCfgVal; @@ -688,7 +689,7 @@ const char *cfg_get_string_value(CONFIG_ITEM_ID id) { } } -vector cfg_get_vector(CONFIG_ITEM_ID id) { +c_vector cfg_get_vector(CONFIG_ITEM_ID id) { PCFG_VALUE pVal = cfg_get_value(id); if (pVal) { diff --git a/srcs/libs/configure/config_help.c b/srcs/libs/configure/config_help.c index 26419ce..192bd66 100644 --- a/srcs/libs/configure/config_help.c +++ b/srcs/libs/configure/config_help.c @@ -12,17 +12,25 @@ const char *config_get_proto_crypto_key() { } #ifdef OPENDHCPD_ON -vector config_get_dhcp_server_range_set() { +unsigned int config_get_dhcp_server_lease_time() { + return cfg_get_integral_value(CFG_DHCP_LEASE_TIME); +} + +c_vector config_get_dhcp_server_range_set() { return cfg_get_vector(CFG_DHCP_RANGE_SET); } -vector config_get_dhcp_listen_on() { +c_vector config_get_dhcp_listen_on() { return cfg_get_vector(CFG_DHCP_LISTEN_ON); } -vector config_get_dhcp_replication_svr() { +c_vector config_get_dhcp_replication_svr() { return cfg_get_vector(CFG_DHCP_REPLICATION_SVR); } + +c_vector config_get_dhcp_mac_filter() { + return cfg_get_vector(CFG_DHCP_MAC_FILTER); +} #endif const char *config_get_http_server_addr() { diff --git a/srcs/libs/include/config.h b/srcs/libs/include/config.h index 5931641..e4b340c 100644 --- a/srcs/libs/include/config.h +++ b/srcs/libs/include/config.h @@ -71,9 +71,13 @@ typedef enum { CFG_HTTP_SVR_TCP_NODELAY, CFG_PROTO_CRYPTO, CFG_PROTO_CRYPTO_KEY, +#ifdef OPENDHCPD_ON + CFG_DHCP_LEASE_TIME, CFG_DHCP_LISTEN_ON, CFG_DHCP_REPLICATION_SVR, CFG_DHCP_RANGE_SET, + CFG_DHCP_MAC_FILTER, +#endif CONFIG_ITEM_ID_MAX } CONFIG_ITEM_ID; @@ -113,7 +117,7 @@ int cfg_get_zero_mq_port(); const char *cfg_get_zero_mq_data_path(); const char *cfg_get_string_value(CONFIG_ITEM_ID id); -vector cfg_get_vector(CONFIG_ITEM_ID id); +c_vector cfg_get_vector(CONFIG_ITEM_ID id); long double cfg_get_float_value(CONFIG_ITEM_ID id); int cfg_get_bool_value(CONFIG_ITEM_ID id); long long cfg_get_integral_value(CONFIG_ITEM_ID id); @@ -134,10 +138,13 @@ int config_get_http_server_tcp_nodelay(); unsigned int config_get_proto_crypto_type(); const char *config_get_proto_crypto_key(); #ifdef OPENDHCPD_ON -vector config_get_dhcp_server_range_set(); -vector config_get_dhcp_listen_on(); -vector config_get_dhcp_replication_svr(); +unsigned int config_get_dhcp_server_lease_time(); +c_vector config_get_dhcp_server_range_set(); +c_vector config_get_dhcp_listen_on(); +c_vector config_get_dhcp_replication_svr(); +c_vector config_get_dhcp_mac_filter(); #endif + #ifdef __cplusplus } #endif diff --git a/srcs/libs/include/misc.h b/srcs/libs/include/misc.h index 03829b1..282b956 100644 --- a/srcs/libs/include/misc.h +++ b/srcs/libs/include/misc.h @@ -39,8 +39,10 @@ extern "C" { #define VERIFY_STRING(s) (((s) != NULL) && (strlen(s) > 0)) #define SAFETY_STR_STRING(s) ((s) == NULL ? "NULL" : (s)) -#define MAX_IP_V4_STR (16) -#define MAX_MAC_ADDR_STR (18) +#define MAX_IP_V4_STR (16) +#define MAX_MAC_ADDR_STR (18) + +#define DEBUG_CODE_LINE() (dzlog_debug("\n")) int file_exists(const char *pPath); const char *basename_v2(const char *path); diff --git a/srcs/libs/include/sds/sds.h b/srcs/libs/include/sds/sds.h index cdd8c63..f745db4 100644 --- a/srcs/libs/include/sds/sds.h +++ b/srcs/libs/include/sds/sds.h @@ -32,6 +32,9 @@ #ifndef __SDS_H #define __SDS_H +#ifdef __cplusplus +extern "C" { +#endif #define SDS_MAX_PREALLOC (1024 * 1024) #ifdef _MSC_VER @@ -52,24 +55,28 @@ struct __attribute__((__packed__)) sdshdr5 { unsigned char flags; /* 3 lsb of type, and 5 msb of string length */ char buf[]; }; + struct __attribute__((__packed__)) sdshdr8 { uint8_t len; /* used */ uint8_t alloc; /* excluding the header and null terminator */ unsigned char flags; /* 3 lsb of type, 5 unused bits */ char buf[]; }; + struct __attribute__((__packed__)) sdshdr16 { uint16_t len; /* used */ uint16_t alloc; /* excluding the header and null terminator */ unsigned char flags; /* 3 lsb of type, 5 unused bits */ char buf[]; }; + struct __attribute__((__packed__)) sdshdr32 { uint32_t len; /* used */ uint32_t alloc; /* excluding the header and null terminator */ unsigned char flags; /* 3 lsb of type, 5 unused bits */ char buf[]; }; + struct __attribute__((__packed__)) sdshdr64 { uint64_t len; /* used */ uint64_t alloc; /* excluding the header and null terminator */ @@ -270,4 +277,7 @@ void sds_free(void *ptr); int sdsTest(int argc, char *argv[]); #endif +#ifdef __cplusplus +} +#endif #endif diff --git a/srcs/libs/include/zvector/zvector.h b/srcs/libs/include/zvector/zvector.h index ffabc09..a9c5074 100644 --- a/srcs/libs/include/zvector/zvector.h +++ b/srcs/libs/include/zvector/zvector.h @@ -28,36 +28,36 @@ extern "C" { // we can use: #include "zvector_checks.h" -// Include vector configuration header +// Include c_vector configuration header #include "zvector_config.h" // Declare required structs: -typedef struct p_vector *vector; +typedef struct p_vector *c_vector; // Declare required enums: /* * Vector Properties Flags can be used to tell ZVector * which types of properties we want to enable for each - * given vector we are creating. - * Each vector can have multiple properties enabled at the + * given c_vector we are creating. + * Each c_vector can have multiple properties enabled at the * same time, you can use the typical C form to specify multiple - * properties for the same vector: + * properties for the same c_vector: * * ZV_SEC_WIPE | ZV_BYREF * - * The above will create a vector that supports passing items to - * it by reference (instead of copying them into the vector) and + * The above will create a c_vector that supports passing items to + * it by reference (instead of copying them into the c_vector) and * having Secure Wipe enabled, so that when an element is deleted * its reference will also be fully zeroed out before freeing it. */ enum ZVECT_PROPERTIES { - ZV_NONE = 0, // Sets or Resets all vector's properties to 0. - ZV_SEC_WIPE = 1 << 0, // Sets the vector for automatic Secure Wipe of items. - ZV_BYREF = 1 << 1, // Sets the vector to store items by reference instead of copying them as per default. + ZV_NONE = 0, // Sets or Resets all c_vector's properties to 0. + ZV_SEC_WIPE = 1 << 0, // Sets the c_vector for automatic Secure Wipe of items. + ZV_BYREF = 1 << 1, // Sets the c_vector to store items by reference instead of copying them as per default. ZV_CIRCULAR = 1 - << 2, // Sets the vector to be a circular vector (so it will not grow in capacity automatically). Elements will be overwritten as in typical circular buffers! - ZV_NOLOCKING = 1 << 3, // This Property means the vector will not use mutexes, be careful using it! + << 2, // Sets the c_vector to be a circular c_vector (so it will not grow in capacity automatically). Elements will be overwritten as in typical circular buffers! + ZV_NOLOCKING = 1 << 3, // This Property means the c_vector will not use mutexes, be careful using it! }; enum ZVECT_ERR { @@ -79,7 +79,7 @@ enum ZVECT_ERR { // Vector construction/Destruction and memory control: /* - * vect_create creates and returns a new vector + * vect_create creates and returns a new c_vector * of the specified "capacity", with a storage area that * can store items of "item_size" size and, if we want to * have an automatic secure erasing enabled (ZV_SEC_WIPE @@ -87,56 +87,56 @@ enum ZVECT_ERR { * after item_size. Flags syntax is the usual C flag sets: * ZV_SEC_WIPE | ZV_BYREF etc. */ -vector vect_create(zvect_index capacity, size_t item_size, uint32_t properties); +c_vector vect_create(zvect_index capacity, size_t item_size, uint32_t properties); /* - * vect_destroy destroys the specified vector and, if + * vect_destroy destroys the specified c_vector and, if * secure_wipe is enabled, also ensure erasing each single - * value in the vector before destroying it. + * value in the c_vector before destroying it. */ -void vect_destroy(vector); +void vect_destroy(c_vector); /* * vect_shrink is useful when operating on systems with * small amount of RAM, and it basically allows to shrink - * the vector capacity to match the actual used size, to + * the c_vector capacity to match the actual used size, to * save unused memory locations. */ -void vect_shrink(vector const v); +void vect_shrink(c_vector const v); #define vect_shrink_to_fit(x) vect_shrink(x) /* * vect_set_wipefunct allows you to pass ZVector a pointer to a custom - * function (of your creation) to securely wipe data from the vector v + * function (of your creation) to securely wipe data from the c_vector v * when automatic safe wipe is called. */ -void vect_set_wipefunct(vector const v, void (*f1)(const void *item, size_t size)); +void vect_set_wipefunct(c_vector const v, void (*f1)(const void *item, size_t size)); // Vector state checks: /* - * vect_is_empty returns true if the vector is empty - * and false if the vector is NOT empty. + * vect_is_empty returns true if the c_vector is empty + * and false if the c_vector is NOT empty. */ -bool vect_is_empty(vector const v); +bool vect_is_empty(c_vector const v); /* * vect_size returns the actual size (the number of) - * USED slots in the vector storage. + * USED slots in the c_vector storage. */ -zvect_index vect_size(vector const v); +zvect_index vect_size(c_vector const v); /* - * vect_clear clears out a vector and also resizes it + * vect_clear clears out a c_vector and also resizes it * to its initial capacity. */ -void vect_clear(vector const v); +void vect_clear(c_vector const v); -bool vect_check_status(const vector v, zvect_index flag_id); +bool vect_check_status(const c_vector v, zvect_index flag_id); -bool vect_set_status(const vector v, zvect_index flag_id); +bool vect_set_status(const c_vector v, zvect_index flag_id); -bool vect_clear_status(const vector v, zvect_index flag_id); +bool vect_clear_status(const c_vector v, zvect_index flag_id); #if (ZVECT_THREAD_SAFE == 1) // Vector Thread Safe functions: @@ -164,57 +164,57 @@ void vect_lock_enable(void); void vect_lock_disable(void); /* - * vect_lock allows you to lock the given vector to + * vect_lock allows you to lock the given c_vector to * have exclusive write access from your own thread. - * When you lock a vector directly then ZVector will + * When you lock a c_vector directly then ZVector will * NOT use its internal locking mechanism for that - * specific vector. + * specific c_vector. * - * Example of use: To lock a vector called v + * Example of use: To lock a c_vector called v * vect_lock(v); */ -zvect_retval vect_lock(vector const v); +zvect_retval vect_lock(c_vector const v); /* - * vect_trylock will try to lock the given vector to + * vect_trylock will try to lock the given c_vector to * have exclusive write access from your own thread. - * When you lock a vector directly then ZVector will + * When you lock a c_vector directly then ZVector will * NOT use its internal locking mechanism for that - * specific vector. + * specific c_vector. * - * Example of use: To lock a vector called v + * Example of use: To lock a c_vector called v * vect_trylock(v); */ -zvect_retval vect_trylock(vector const v); +zvect_retval vect_trylock(c_vector const v); /* - * vect_lock allows you to unlock the given vector that + * vect_lock allows you to unlock the given c_vector that * you have previously locked with vect_lock. * - * Example of use: To unlock a vector called v + * Example of use: To unlock a c_vector called v * vect_unlock(v); */ -zvect_retval vect_unlock(vector const v); +zvect_retval vect_unlock(c_vector const v); /* TODO: - * zvect_retval vect_wait_for_signal(const vector v); + * zvect_retval vect_wait_for_signal(const c_vector v); * - * zvect_retval vect_lock_after_signal(const vector v); + * zvect_retval vect_lock_after_signal(const c_vector v); */ -zvect_retval vect_move_on_signal(vector const v1, - vector v2, +zvect_retval vect_move_on_signal(c_vector const v1, + c_vector v2, const zvect_index s2, const zvect_index e2, zvect_retval (*f2)(void *, void *)); -zvect_retval vect_send_signal(const vector v); +zvect_retval vect_send_signal(const c_vector v); -zvect_retval vect_broadcast_signal(const vector v); +zvect_retval vect_broadcast_signal(const c_vector v); -zvect_retval vect_sem_wait(const vector v); +zvect_retval vect_sem_wait(const c_vector v); -zvect_retval vect_sem_post(const vector v); +zvect_retval vect_sem_post(const c_vector v); #endif // ( ZVECT_THREAD_SAFE == 1 ) @@ -223,174 +223,174 @@ zvect_retval vect_sem_post(const vector v); /* * vect_push and vect_pop are used to use the - * vector as a dynamic stack. + * c_vector as a dynamic stack. * * int i = 3; * vect_push(v, &i) pushes the element 3 at the - * back of the vector v, which + * back of the c_vector v, which * corresponds to the top of a * Stack. */ -void vect_push(vector const v, const void *item); +void vect_push(c_vector const v, const void *item); /* * vect_pop(v) "pops" (returns) the element - * at the back of the vector as + * at the back of the c_vector as * a regular pop would do with * an element at the top of a * stack. Remember when you use * vect_pop the element you * receive is also removed from - * the vector! + * the c_vector! */ -void *vect_pop(vector const v); +void *vect_pop(c_vector const v); #define vect_pop_back(x) vect_pop(x) /* - * vect_add adds a new item into the vector and, - * if required, will also reorganize the vector. + * vect_add adds a new item into the c_vector and, + * if required, will also reorganize the c_vector. * * int i = 3; * vect_add(v, &i) will add the new item 3 in - * the vector v at the end - * (or back) of the vector v. + * the c_vector v at the end + * (or back) of the c_vector v. */ -void vect_add(vector const v, const void *item); +void vect_add(c_vector const v, const void *item); #define vect_push_back(x, y) vect_add(x, y) /* * int i = 4; * vect_add_at(v, &i, 2) will add the new item 4 at - * position 2 in the vector v + * position 2 in the c_vector v * and move all the elements * from the original 2nd onward * of a position to make space * for the new item 4. */ -void vect_add_at(vector const v, const void *item, zvect_index index); +void vect_add_at(c_vector const v, const void *item, zvect_index index); /* * int i = 5; * vect_add_front(v, &i) will add the new item 5 at - * the beginning of the vector + * the beginning of the c_vector * v (or front) and will also * move all the existing * elements of one position in - * the vector to make space for + * the c_vector to make space for * the new item 5 at the front - * of vector v. + * of c_vector v. */ -void vect_add_front(vector const v, const void *item); +void vect_add_front(c_vector const v, const void *item); #define vect_push_front(x, y) vect_add_front(x, y) /* - * vect_get returns an item from the specified vector + * vect_get returns an item from the specified c_vector * * vect_get(v) will return the ast element in - * the v vector (but will not remove + * the v c_vector (but will not remove * the element as it happens in * vect_pop(v)). */ -void *vect_get(vector const v); +void *vect_get(c_vector const v); #define vect_back(v) vect_get(v) /* * * vect_get_at(v, 3) will return the element at location - * 3 in the vector v. + * 3 in the c_vector v. */ -void *vect_get_at(vector const v, const zvect_index i); +void *vect_get_at(c_vector const v, const zvect_index i); #define vect_at(v, x) vect_get_at(v, x) /* * vect_get_front(v) will return the first element in - * the vector v. + * the c_vector v. */ -void *vect_get_front(vector const v); +void *vect_get_front(c_vector const v); #define vect_front(v) vect_get_front(v) /* *vect_put allows you to REPLACE an item - * in the vector. + * in the c_vector. * * int i = 3; * vect_put(v, &i) will replace the last element - * in the vector with 3. + * in the c_vector with 3. */ -void vect_put(vector const v, const void *item); +void vect_put(c_vector const v, const void *item); /* * * int i = 4; * vect_put_at(v, &i, 2) will replace the 3rd element - * (2 + 1, as vector's 1st item + * (2 + 1, as c_vector's 1st item * starts at v[0]) with the * item 4. */ -void vect_put_at(vector const v, const void *item, const zvect_index i); +void vect_put_at(c_vector const v, const void *item, const zvect_index i); /* * * int i = 5; * vect_put_front(v, &i) will replace the 1st element - * of the vector with the item + * of the c_vector with the item * 5. */ -void vect_put_front(vector const v, const void *item); +void vect_put_front(c_vector const v, const void *item); /* - * vect_remove removes an item from the vector - * and reorganise the vector. It also returns - * the item remove from the vector, so you can + * vect_remove removes an item from the c_vector + * and reorganise the c_vector. It also returns + * the item remove from the c_vector, so you can * use it to simulate a stack behaviour as well. * * vect_remove(v) will remove and return the - * last item in the vector. + * last item in the c_vector. */ -void *vect_remove(vector const v); +void *vect_remove(c_vector const v); /* * vect_remove_at(v, 3) will remove the 3rd item in - * the vector and return it. + * the c_vector and return it. */ -void *vect_remove_at(vector const v, const zvect_index i); +void *vect_remove_at(c_vector const v, const zvect_index i); /* * vect_remove_front(v) will remove the 1st item in - * the vector and return it. + * the c_vector and return it. */ -void *vect_remove_front(vector const v); +void *vect_remove_front(c_vector const v); /* - * vect_delete deletes an item from the vector - * and reorganise the vector. It does not return + * vect_delete deletes an item from the c_vector + * and reorganise the c_vector. It does not return * the item like remove. * * vect_delete(v) will delete and the last - * item in the vector. + * item in the c_vector. */ -void vect_delete(vector const v); +void vect_delete(c_vector const v); /* * vect_delete_at(v, 3) will delete the 3rd item in - * the vector. + * the c_vector. */ -void vect_delete_at(vector const v, const zvect_index i); +void vect_delete_at(c_vector const v, const zvect_index i); /* * vect_delete_range(v, 20, 30) * will delete items from item - * 20 to item 30 in the vector + * 20 to item 30 in the c_vector * v. */ -void vect_delete_range(vector const v, const zvect_index first_element, const zvect_index last_element); +void vect_delete_range(c_vector const v, const zvect_index first_element, const zvect_index last_element); /* * * vect_delete_front(v) will delete the 1st item in - * the vector. + * the c_vector. */ -void vect_delete_front(vector const v); +void vect_delete_front(c_vector const v); //////////// // Vector Data manipulation functions: @@ -401,70 +401,70 @@ void vect_delete_front(vector const v); /* * vect_swap is a function that allows you to swap two - * items in the same vector. - * You just pass the vector and the index of both the + * items in the same c_vector. + * You just pass the c_vector and the index of both the * two items to swap. * - * For example to swap item 3 with item 22 on vector v + * For example to swap item 3 with item 22 on c_vector v * use: * vect_swap(v, 3, 22); */ -void vect_swap(vector const v, const zvect_index s, const zvect_index e); +void vect_swap(c_vector const v, const zvect_index s, const zvect_index e); /* * vect_swap_range is a function that allows to swap - * a range of items in the same vector. - * You just pass the vector, the index of the first item + * a range of items in the same c_vector. + * You just pass the c_vector, the index of the first item * to swap, the index of the last item to swap and the * index of the first item to swap with. * * For example to swap items from 10 to 20 with items - * from 30 to 40 on vector v, use: + * from 30 to 40 on c_vector v, use: * vect_swap_range(v, 10, 20, 30); */ -void vect_swap_range(vector const v, const zvect_index s1, const zvect_index e1, const zvect_index s2); +void vect_swap_range(c_vector const v, const zvect_index s1, const zvect_index e1, const zvect_index s2); /* * vect_rotate_left is a function that allows to rotate - * a vector of "i" positions to the left (or from the + * a c_vector of "i" positions to the left (or from the * "front" to the "end"). * - * For example to rotate a vector called v of 5 positions + * For example to rotate a c_vector called v of 5 positions * to the left, use: * vect_rotate_left(v, 5); */ -void vect_rotate_left(vector const v, const zvect_index i); +void vect_rotate_left(c_vector const v, const zvect_index i); /* * vect_rotate_right is a function that allows to rotate - * a vector of "i" positions to the right (or from the + * a c_vector of "i" positions to the right (or from the * "end" to the "front"). * - * For example to rotate a vector called v of 5 positions + * For example to rotate a c_vector called v of 5 positions * to the right, use: * vect_rotate_right(v, 5); */ -void vect_rotate_right(vector const v, const zvect_index i); +void vect_rotate_right(c_vector const v, const zvect_index i); /* - * vect_qsort allows you to sort a given vector. - * The algorithm used to sort a vector is Quicksort with + * vect_qsort allows you to sort a given c_vector. + * The algorithm used to sort a c_vector is Quicksort with * 3 ways partitioning which is generally much faster than * traditional quicksort. * - * To sort a vector you need to provide a custom function + * To sort a c_vector you need to provide a custom function * that allows vect_sort to determine the order and which - * elements of a vector are used to order it in the way + * elements of a c_vector are used to order it in the way * you desire. It pretty much works as a regular C qsort * function. It quite fast given that it only reorders - * pointers to your datastructures stored in the vector. + * pointers to your datastructures stored in the c_vector. * */ -void vect_qsort(vector const v, int (*compare_func)(const void *, const void *)); +void vect_qsort(c_vector const v, int (*compare_func)(const void *, const void *)); /* * vect_bsearch is a function that allows to perform - * a binary search over the vector we pass to it to + * a binary search over the c_vector we pass to it to * find the item "key" using the comparison function * "f1". * @@ -474,7 +474,7 @@ void vect_qsort(vector const v, int (*compare_func)(const void *, const void *)) * some improvements over the original one (look at the * sources for more details). * - * For example to search for the number 5 in a vector + * For example to search for the number 5 in a c_vector * called v using a compare function called "my_compare" * use: * int i = 5; @@ -483,7 +483,7 @@ void vect_qsort(vector const v, int (*compare_func)(const void *, const void *)) #ifdef __cplusplus extern "C" { #endif -bool vect_bsearch(vector const v, const void *key, int (*f1)(const void *, const void *), zvect_index *item_index); +bool vect_bsearch(c_vector const v, const void *key, int (*f1)(const void *, const void *), zvect_index *item_index); #ifdef __cplusplus } #endif @@ -491,20 +491,20 @@ bool vect_bsearch(vector const v, const void *key, int (*f1)(const void *, const * vect_add_ordered allows the insertion of new items in * an ordered fashion. Please note that for this to work * fine you should always use only ordered vectors or if - * an empty vector use vect_add_ordered only to add new + * an empty c_vector use vect_add_ordered only to add new * values to it! * * As for any other ordered function you must provide * your own compare function (syntax is the usual one, * and it's the same as for regular CLib qsort function) * - * To add item 3 to a vector called v using vect_add_ordered + * To add item 3 to a c_vector called v using vect_add_ordered * (assuming your compare function is called my_compare), * use: * * vect_Add_ordered(v, 3, my_compare); */ -void vect_add_ordered(vector const v, const void *value, int (*f1)(const void *, const void *)); +void vect_add_ordered(c_vector const v, const void *value, int (*f1)(const void *, const void *)); #endif // ZVECT_DMF_EXTENSIONS @@ -513,23 +513,23 @@ void vect_add_ordered(vector const v, const void *value, int (*f1)(const void *, /* * vect_apply allows you to apply a C function to - * each item in the vector, so you just pass the vector, + * each item in the c_vector, so you just pass the c_vector, * the function you want to execute against each item on - * a vector and make sure such function is declared and + * a c_vector and make sure such function is declared and * defined to accept a "void *" pointer which will be the - * pointer to the single item in the vector passed to your + * pointer to the single item in the c_vector passed to your * function. The function has no return value because it's * not necessary if you receive the pointer to the item. * You can simply update the content of the memory pointed * by the item passed and that is much faster than having * to deal with return values especially when you'll be - * using complex data structures as item of the vector. + * using complex data structures as item of the c_vector. */ -void vect_apply(vector const v, void (*f1)(void *)); +void vect_apply(c_vector const v, void (*f1)(void *)); /* * vect_apply_if is a function that will apply "f1" C function - * to each and every item in vector v1, IF the return value of + * to each and every item in c_vector v1, IF the return value of * function f2 is true. So it allows what is known as conditional * application. f2 will receive an item from v1 as first parameter * and an item from v2 (at the same position of the item in v1) as @@ -555,51 +555,55 @@ void vect_apply(vector const v, void (*f1)(void *)); * return false; * } */ -void vect_apply_if(vector const v1, vector const v2, void (*f1)(void *), bool (*f2)(void *, void *)); +void vect_apply_if(c_vector const v1, c_vector const v2, void (*f1)(void *), bool (*f2)(void *, void *)); // Operations with multiple vectors: /* * vect_copy is a function that allows to copy a specified - * set of elements from a vector to another. + * set of elements from a c_vector to another. * Please note: only vectors with the same data size (the * parameter we pass during the creation of both vectors) * can be copied into the other! * * vect_copy(v1, v2, 3, 5) will copy all the items in - * vector v2, from the 4th item + * c_vector v2, from the 4th item * till the 9th (3 + 5, remember - * vector items start from 0) in - * the vector v1. So at the end + * c_vector items start from 0) in + * the c_vector v1. So at the end * of the process you'll have such * items copied at the end of v1. */ -void vect_copy(vector const v1, vector const v2, zvect_index start, zvect_index max_elements); +void vect_copy(c_vector const v1, c_vector const v2, zvect_index start, zvect_index max_elements); /* * vect_insert is a function that allows to copy a specified - * set of elements from a vector to another and "insert" - * them from a specified position in the destination vector. + * set of elements from a c_vector to another and "insert" + * them from a specified position in the destination c_vector. * Please note: only vectors with the same data size (the * parameter we pass during the creation of both vectors) * can be copied into the other! * * vect_insert(v1, v2, 3, 5, 7) will copy all the items in - * vector v2, from the 4th item + * c_vector v2, from the 4th item * till the 9th (3 + 5, remember - * vector items start from 0) in - * the vector v1 from position 7. + * c_vector items start from 0) in + * the c_vector v1 from position 7. * So at the end of the process * you'll have such items "inserted" * inside v1. */ -void vect_insert(vector const v1, vector const v2, const zvect_index s2, const zvect_index e2, const zvect_index s1); +void vect_insert(c_vector const v1, + c_vector const v2, + const zvect_index s2, + const zvect_index e2, + const zvect_index s1); /* * vect_move is a function that allows to move a specified - * set of items from one vector to another. - * It will also re-organise the source vector and (obviously) - * expand the destination vector if needed. + * set of items from one c_vector to another. + * It will also re-organise the source c_vector and (obviously) + * expand the destination c_vector if needed. * Please note: only vectors of the same data size can be moved * one into the other! * @@ -607,15 +611,15 @@ void vect_insert(vector const v1, vector const v2, const zvect_index s2, const z * 3rd item in v2 till the 5th at * the end of v1. */ -void vect_move(vector const v1, vector const v2, zvect_index start, zvect_index max_elements); +void vect_move(c_vector const v1, c_vector const v2, zvect_index start, zvect_index max_elements); /* * vect_move_if is a function that allows to move a specified - * set of items from one vector to another if the condition + * set of items from one c_vector to another if the condition * returned by the function pointed by f2 function pointer * is true. - * It will also re-organise the source vector and (obviously) - * expand the destination vector if needed. + * It will also re-organise the source c_vector and (obviously) + * expand the destination c_vector if needed. * Please note: only vectors of the same data size can be moved * one into the other! * @@ -624,8 +628,8 @@ void vect_move(vector const v1, vector const v2, zvect_index start, zvect_index * the end of v1 if check_data returns * true. */ -zvect_retval vect_move_if(vector const v1, - vector v2, +zvect_retval vect_move_if(c_vector const v1, + c_vector v2, const zvect_index s2, const zvect_index e2, zvect_retval (*f2)(void *, void *)); @@ -633,14 +637,14 @@ zvect_retval vect_move_if(vector const v1, /* * vect_merge is a function that merges together 2 vectors of * the same data size. At the end of the process, the source - * vector will be destroyed. + * c_vector will be destroyed. * - * vect_merge(v1, v2) will merge vector v2 to v1 and then + * vect_merge(v1, v2) will merge c_vector v2 to v1 and then * destroy v2. So at the end of the job * v1 will contain the old v1 items + * all v2 items. */ -void vect_merge(vector const v1, vector v2); +void vect_merge(c_vector const v1, c_vector v2); #endif // ZVECT_SFMD_EXTENSIONS diff --git a/srcs/libs/include/zvector/zvector_config.h b/srcs/libs/include/zvector/zvector_config.h index cdc3e5f..802f3ef 100644 --- a/srcs/libs/include/zvector/zvector_config.h +++ b/srcs/libs/include/zvector/zvector_config.h @@ -29,7 +29,7 @@ #define ZVECT_ALWAYSINLINE #endif -// Default vector Index type +// Default c_vector Index type // This is set to unsigned int of 32bit // size (so all different architectures // and OS behaves in a similar way) @@ -40,7 +40,7 @@ typedef uint32_t zvect_index; // it's the maximum number that can be stored in a zvect_index. #define zvect_index_max (4294967295) -// Default vector return type for +// Default c_vector return type for // error codes. // Generally negative numbers identify // an error. @@ -49,13 +49,13 @@ typedef uint32_t zvect_index; // attributes, like 1 is generally true typedef int32_t zvect_retval; -// Default vector storage size +// Default c_vector storage size // This will be used when the user // specifies 0 (zero) as data type // size. #define ZVECT_DEFAULT_DATA_SIZE sizeof(int) -// Default vector capacity +// Default c_vector capacity // This will be used when the user // does NOT specify an Initial Capacity // or set it to 0 (zero): diff --git a/srcs/libs/misc/zvector.c b/srcs/libs/misc/zvector.c index 054215a..7b59aa2 100644 --- a/srcs/libs/misc/zvector.c +++ b/srcs/libs/misc/zvector.c @@ -288,13 +288,13 @@ p_throw_error(const zvect_retval error_code, const char *error_message) { if (locally_allocated) { switch (error_code) { case ZVERR_VECTUNDEF: - message = (char *)safe_strncpy("Undefined or uninitialized vector.\n\0", msg_len); + message = (char *)safe_strncpy("Undefined or uninitialized c_vector.\n\0", msg_len); break; case ZVERR_IDXOUTOFBOUND: message = (char *)safe_strncpy("Index out of bound.\n\0", msg_len); break; case ZVERR_OUTOFMEM: - message = (char *)safe_strncpy("Not enough memory to allocate space for the vector.\n\0", msg_len); + message = (char *)safe_strncpy("Not enough memory to allocate space for the c_vector.\n\0", msg_len); break; case ZVERR_VECTCORRUPTED: message = (char *)safe_strncpy("Vector corrupted.\n\0", msg_len); @@ -303,7 +303,7 @@ p_throw_error(const zvect_retval error_code, const char *error_message) { message = (char *)safe_strncpy("Race condition detected, cannot continue.\n\0", msg_len); break; case ZVERR_VECTTOOSMALL: - message = (char *)safe_strncpy("Destination vector is smaller than source.\n\0", msg_len); + message = (char *)safe_strncpy("Destination c_vector is smaller than source.\n\0", msg_len); break; case ZVERR_VECTDATASIZE: message = (char *)safe_strncpy( @@ -492,7 +492,7 @@ static inline void mutex_destroy(CRITICAL_SECTION *lock) { * uses ZVector primitives. * level 3 is the priority of the User's locks. */ -static inline zvect_retval get_mutex_lock(const vector v, const int32_t lock_type) { +static inline zvect_retval get_mutex_lock(const c_vector v, const int32_t lock_type) { if (lock_type >= v->lock_type) { mutex_lock(&(v->lock)); v->lock_type = lock_type; @@ -501,7 +501,7 @@ static inline zvect_retval get_mutex_lock(const vector v, const int32_t lock_typ return 0; } -static inline zvect_retval check_mutex_trylock(const vector v, const int32_t lock_type) { +static inline zvect_retval check_mutex_trylock(const c_vector v, const int32_t lock_type) { if ((lock_type >= v->lock_type) && (!mutex_trylock(&(v->lock)))) { v->lock_type = lock_type; return 1; @@ -509,7 +509,7 @@ static inline zvect_retval check_mutex_trylock(const vector v, const int32_t loc return 0; } -static inline zvect_retval lock_after_signal(const vector v, const int32_t lock_type) { +static inline zvect_retval lock_after_signal(const c_vector v, const int32_t lock_type) { if (lock_type >= v->lock_type) { //if (!mutex_trylock(&(v->lock))) { while (!pthread_cond_wait(&(v->cond), &(v->lock))) { @@ -540,15 +540,15 @@ static inline zvect_retval wait_for_signal(const vector v, const int32_t lock_ty } */ -static inline zvect_retval send_signal(const vector v, const int32_t lock_type) { +static inline zvect_retval send_signal(const c_vector v, const int32_t lock_type) { return (lock_type >= v->lock_type) ? pthread_cond_signal(&(v->cond)) : 0; } -static inline zvect_retval broadcast_signal(const vector v, const int32_t lock_type) { +static inline zvect_retval broadcast_signal(const c_vector v, const int32_t lock_type) { return (lock_type >= v->lock_type) ? pthread_cond_broadcast(&(v->cond)) : 0; } -static inline zvect_retval get_mutex_unlock(const vector v, const int32_t lock_type) { +static inline zvect_retval get_mutex_unlock(const c_vector v, const int32_t lock_type) { if (lock_type == v->lock_type) { v->lock_type = 0; mutex_unlock(&(v->lock)); @@ -584,21 +584,21 @@ void p_init_zvect(void) { // Vector's Utilities: ZVECT_ALWAYSINLINE -static inline zvect_retval p_vect_check(const vector x) { +static inline zvect_retval p_vect_check(const c_vector x) { return (x == NULL) ? ZVERR_VECTUNDEF : 0; } ZVECT_ALWAYSINLINE -static inline zvect_index p_vect_capacity(const vector v) { +static inline zvect_index p_vect_capacity(const c_vector v) { return (v->cap_left + v->cap_right); } ZVECT_ALWAYSINLINE -static inline zvect_index p_vect_size(const vector v) { +static inline zvect_index p_vect_size(const c_vector v) { return (v->end > v->begin) ? (v->end - v->begin) : (v->begin - v->end); } -static inline void p_item_safewipe(vector const v, void *const item) { +static inline void p_item_safewipe(c_vector const v, void *const item) { if (item != NULL) { if (!(v->status & ZVS_CUST_WIPE_ON)) { memset(item, 0, v->data_size); @@ -619,11 +619,11 @@ static inline zvect_retval p_usr_status(zvect_index flag_id) { } } -bool vect_check_status(const vector v, zvect_index flag_id) { +bool vect_check_status(const c_vector v, zvect_index flag_id) { return (v->status >> flag_id) & 1U; } -bool vect_set_status(const vector v, zvect_index flag_id) { +bool vect_set_status(const c_vector v, zvect_index flag_id) { zvect_retval rval = p_usr_status(flag_id); if (!rval) { @@ -633,7 +633,7 @@ bool vect_set_status(const vector v, zvect_index flag_id) { return (bool)rval; } -bool vect_clear_status(const vector v, zvect_index flag_id) { +bool vect_clear_status(const c_vector v, zvect_index flag_id) { zvect_retval rval = p_usr_status(flag_id); if (!rval) { @@ -643,7 +643,7 @@ bool vect_clear_status(const vector v, zvect_index flag_id) { return (bool)rval; } -bool vect_toggle_status(const vector v, zvect_index flag_id) { +bool vect_toggle_status(const c_vector v, zvect_index flag_id) { zvect_retval rval = p_usr_status(flag_id); if (!rval) { @@ -653,7 +653,7 @@ bool vect_toggle_status(const vector v, zvect_index flag_id) { return rval; } -static void p_free_items(vector const v, zvect_index first, zvect_index offset) { +static void p_free_items(c_vector const v, zvect_index first, zvect_index offset) { if (p_vect_size(v) == 0) { return; } @@ -685,7 +685,7 @@ static void p_free_items(vector const v, zvect_index first, zvect_index offset) /* * Set vector capacity to a specific new_capacity value */ -static zvect_retval p_vect_set_capacity(vector const v, const zvect_index direction, const zvect_index new_capacity_) { +static zvect_retval p_vect_set_capacity(c_vector const v, const zvect_index direction, const zvect_index new_capacity_) { zvect_index new_capacity = new_capacity_; if (new_capacity <= v->init_capacity) { @@ -739,7 +739,7 @@ static zvect_retval p_vect_set_capacity(vector const v, const zvect_index direct /* * This function increase the CAPACITY of a vector. */ -static zvect_retval p_vect_increase_capacity(vector const v, const zvect_index direction) { +static zvect_retval p_vect_increase_capacity(c_vector const v, const zvect_index direction) { zvect_index new_capacity; if (!direction) { @@ -772,7 +772,7 @@ static zvect_retval p_vect_increase_capacity(vector const v, const zvect_index d /* * This function decrease the CAPACITY of a vector. */ -static zvect_retval p_vect_decrease_capacity(vector const v, const zvect_index direction) { +static zvect_retval p_vect_decrease_capacity(c_vector const v, const zvect_index direction) { // Check if new capacity is smaller than initial capacity if (p_vect_capacity(v) <= v->init_capacity) { return 0; @@ -847,7 +847,7 @@ static zvect_retval p_vect_decrease_capacity(vector const v, const zvect_index d * not its size. To reduce the size of a vector we * need to remove items from it. */ -static zvect_retval p_vect_shrink(vector const v) { +static zvect_retval p_vect_shrink(c_vector const v) { if (v->init_capacity < 2) { v->init_capacity = 2; } @@ -898,7 +898,7 @@ static zvect_retval p_vect_shrink(vector const v) { /*---------------------------------------------------------------------------*/ // Creation and destruction primitives: -zvect_retval p_vect_clear(vector const v) { +zvect_retval p_vect_clear(c_vector const v) { // Clear the vector: if (!vect_is_empty(v)) { p_free_items(v, 0, (p_vect_size(v) - 1)); @@ -913,7 +913,7 @@ zvect_retval p_vect_clear(vector const v) { return 0; } -static zvect_retval p_vect_destroy(vector v, uint32_t flags) { +static zvect_retval p_vect_destroy(c_vector v, uint32_t flags) { // p_destroy is an exception to the rule of handling // locking from the public methods. This because // p_destroy has to destroy the vector mutex too and @@ -971,7 +971,7 @@ static zvect_retval p_vect_destroy(vector v, uint32_t flags) { // Vector data storage primitives: // inline implementation for all put: -static inline zvect_retval p_vect_put_at(vector const v, const void *value, const zvect_index i) { +static inline zvect_retval p_vect_put_at(c_vector const v, const void *value, const zvect_index i) { // Check if the index passed is out of bounds: zvect_index idx = i; if (!(v->flags & ZV_CIRCULAR)) { @@ -1001,7 +1001,7 @@ static inline zvect_retval p_vect_put_at(vector const v, const void *value, cons } // inline implementation for all add(s): -static inline zvect_retval p_vect_add_at(vector const v, const void *value, const zvect_index i) { +static inline zvect_retval p_vect_add_at(c_vector const v, const void *value, const zvect_index i) { zvect_index idx = i; // Get vector size: @@ -1120,7 +1120,7 @@ static inline zvect_retval p_vect_add_at(vector const v, const void *value, cons } // This is the inline implementation for all the remove and pop -static inline zvect_retval p_vect_remove_at(vector const v, const zvect_index i, void **item) { +static inline zvect_retval p_vect_remove_at(c_vector const v, const zvect_index i, void **item) { zvect_index idx = i; // Get the vector size: @@ -1235,7 +1235,7 @@ static inline zvect_retval p_vect_remove_at(vector const v, const zvect_index i, } // This is the inline implementation for all the "delete" methods -static inline zvect_retval p_vect_delete_at(vector const v, +static inline zvect_retval p_vect_delete_at(c_vector const v, const zvect_index start, const zvect_index offset, uint32_t flags) { @@ -1341,7 +1341,7 @@ static inline zvect_retval p_vect_delete_at(vector const v, * Public method to request ZVector to * shrink a vector. */ -void vect_shrink(vector const v) { +void vect_shrink(c_vector const v) { #if (ZVECT_THREAD_SAFE == 1) zvect_retval lock_owner = get_mutex_lock(v, 1); #endif @@ -1363,23 +1363,23 @@ void vect_shrink(vector const v) { /*---------------------------------------------------------------------------*/ // Vector Structural Information report: -bool vect_is_empty(vector const v) { +bool vect_is_empty(c_vector const v) { return !p_vect_check(v) ? (p_vect_size(v) == 0) : (bool)ZVERR_VECTUNDEF; } -zvect_index vect_size(vector const v) { +zvect_index vect_size(c_vector const v) { return !p_vect_check(v) ? p_vect_size(v) : 0; } -zvect_index vect_max_size(vector const v) { +zvect_index vect_max_size(c_vector const v) { return !p_vect_check(v) ? zvect_index_max : 0; } -void *vect_begin(vector const v) { +void *vect_begin(c_vector const v) { return !p_vect_check(v) ? v->data[v->begin] : NULL; } -void *vect_end(vector const v) { +void *vect_end(c_vector const v) { return !p_vect_check(v) ? v->data[v->end] : NULL; } @@ -1388,7 +1388,7 @@ void *vect_end(vector const v) { /*---------------------------------------------------------------------------*/ // Vector Creation and Destruction: -vector vect_create(const zvect_index init_capacity, const size_t item_size, const uint32_t properties) { +c_vector vect_create(const zvect_index init_capacity, const size_t item_size, const uint32_t properties) { // If ZVector has not been initialised yet, then initialise it // when creating the first vector: if (p_init_state == 0) { @@ -1396,7 +1396,7 @@ vector vect_create(const zvect_index init_capacity, const size_t item_size, cons } // Create the vector first: - vector v = (vector)malloc(sizeof(struct p_vector)); + c_vector v = (c_vector)malloc(sizeof(struct p_vector)); if (v == NULL) { p_throw_error(ZVERR_OUTOFMEM, NULL); } @@ -1457,7 +1457,7 @@ vector vect_create(const zvect_index init_capacity, const size_t item_size, cons return v; } -void vect_destroy(vector v) { +void vect_destroy(c_vector v) { // Call p_vect_destroy with flags set to 1 // to destroy data according to the vector // properties: @@ -1488,31 +1488,31 @@ void vect_lock_disable(void) { locking_disabled = true; } -static inline zvect_retval p_vect_lock(vector const v) { +static inline zvect_retval p_vect_lock(c_vector const v) { return (locking_disabled || (v->flags & ZV_NOLOCKING)) ? 0 : get_mutex_lock(v, 3); } -zvect_retval vect_lock(vector const v) { +zvect_retval vect_lock(c_vector const v) { return p_vect_lock(v); } -static inline zvect_retval p_vect_unlock(vector const v) { +static inline zvect_retval p_vect_unlock(c_vector const v) { return (locking_disabled || (v->flags & ZV_NOLOCKING)) ? 0 : get_mutex_unlock(v, 3); } -zvect_retval vect_unlock(vector const v) { +zvect_retval vect_unlock(c_vector const v) { return p_vect_unlock(v); } -static inline zvect_retval p_vect_trylock(vector const v) { +static inline zvect_retval p_vect_trylock(c_vector const v) { return (locking_disabled || (v->flags & ZV_NOLOCKING)) ? 0 : check_mutex_trylock(v, 3); } -zvect_retval vect_trylock(vector const v) { +zvect_retval vect_trylock(c_vector const v) { return p_vect_trylock(v); } -zvect_retval vect_sem_wait(const vector v) { +zvect_retval vect_sem_wait(const c_vector v) { #if !defined(macOS) return sem_wait(&(v->semaphore)); #else @@ -1520,7 +1520,7 @@ zvect_retval vect_sem_wait(const vector v) { #endif } -zvect_retval vect_sem_post(const vector v) { +zvect_retval vect_sem_post(const c_vector v) { #if !defined(macOS) return sem_post(&(v->semaphore)); #else @@ -1538,19 +1538,19 @@ inline zvect_retval vect_wait_for_signal(const vector v) { } */ -static inline zvect_retval p_vect_lock_after_signal(const vector v) { +static inline zvect_retval p_vect_lock_after_signal(const c_vector v) { return (locking_disabled || (v->flags & ZV_NOLOCKING)) ? 1 : lock_after_signal(v, 3); } -zvect_retval vect_lock_after_signal(const vector v) { +zvect_retval vect_lock_after_signal(const c_vector v) { return p_vect_lock_after_signal(v); } -zvect_retval vect_send_signal(const vector v) { +zvect_retval vect_send_signal(const c_vector v) { return send_signal(v, 3); } -zvect_retval vect_broadcast_signal(const vector v) { +zvect_retval vect_broadcast_signal(const c_vector v) { return broadcast_signal(v, 3); } @@ -1561,7 +1561,7 @@ zvect_retval vect_broadcast_signal(const vector v) { /*---------------------------------------------------------------------------*/ // Vector Data Storage functions: -void vect_clear(vector const v) { +void vect_clear(c_vector const v) { // check if the vector exists: zvect_retval rval = p_vect_check(v); if (rval) { @@ -1587,7 +1587,7 @@ JOB_DONE: } } -void vect_set_wipefunct(const vector v, void (*f1)(const void *, size_t)) { +void vect_set_wipefunct(const c_vector v, void (*f1)(const void *, size_t)) { v->SfWpFunc = (void *)malloc(sizeof(void *)); if (v->SfWpFunc == NULL) { p_throw_error(ZVERR_OUTOFMEM, NULL); @@ -1600,7 +1600,7 @@ void vect_set_wipefunct(const vector v, void (*f1)(const void *, size_t)) { } // Add an item at the END (top) of the vector -inline void vect_push(const vector v, const void *value) { +inline void vect_push(const c_vector v, const void *value) { zvect_retval rval = p_vect_check(v); if (rval) { goto JOB_DONE; @@ -1646,12 +1646,12 @@ JOB_DONE: } // Add an item at the END of the vector -void vect_add(const vector v, const void *value) { +void vect_add(const c_vector v, const void *value) { vect_push(v, value); } // Add an item at position "i" of the vector -void vect_add_at(const vector v, const void *value, const zvect_index i) { +void vect_add_at(const c_vector v, const void *value, const zvect_index i) { zvect_retval rval = p_vect_check(v); if (rval) { goto JOB_DONE; @@ -1701,7 +1701,7 @@ JOB_DONE: } // Add an item at the FRONT of the vector -void vect_add_front(vector const v, const void *value) { +void vect_add_front(c_vector const v, const void *value) { zvect_retval rval = p_vect_check(v); if (rval) { goto JOB_DONE; @@ -1739,7 +1739,7 @@ JOB_DONE: } // inline implementation for all get(s): -static inline void *p_vect_get_at(vector const v, const zvect_index i) { +static inline void *p_vect_get_at(c_vector const v, const zvect_index i) { // Check if passed index is out of bounds: if (i >= p_vect_size(v)) { p_throw_error(ZVERR_IDXOUTOFBOUND, NULL); @@ -1749,7 +1749,7 @@ static inline void *p_vect_get_at(vector const v, const zvect_index i) { return v->data[v->begin + i]; } -void *vect_get(vector const v) { +void *vect_get(c_vector const v) { // check if the vector exists: zvect_retval rval = p_vect_check(v); if (!rval) { @@ -1761,7 +1761,7 @@ void *vect_get(vector const v) { return NULL; } -void *vect_get_at(vector const v, const zvect_index i) { +void *vect_get_at(c_vector const v, const zvect_index i) { // check if the vector exists: zvect_retval rval = p_vect_check(v); if (!rval) { @@ -1773,7 +1773,7 @@ void *vect_get_at(vector const v, const zvect_index i) { return NULL; } -void *vect_get_front(vector const v) { +void *vect_get_front(c_vector const v) { // check if the vector exists: zvect_retval rval = p_vect_check(v); if (!rval) { @@ -1785,7 +1785,7 @@ void *vect_get_front(vector const v) { return NULL; } -void vect_put(vector const v, const void *value) { +void vect_put(c_vector const v, const void *value) { zvect_retval rval = p_vect_check(v); if (rval) { goto JOB_DONE; @@ -1809,7 +1809,7 @@ JOB_DONE: } } -void vect_put_at(vector const v, const void *value, const zvect_index i) { +void vect_put_at(c_vector const v, const void *value, const zvect_index i) { zvect_retval rval = p_vect_check(v); if (rval) { goto JOB_DONE; @@ -1833,7 +1833,7 @@ JOB_DONE: } } -void vect_put_front(vector const v, const void *value) { +void vect_put_front(c_vector const v, const void *value) { zvect_retval rval = p_vect_check(v); if (rval) { goto JOB_DONE; @@ -1857,7 +1857,7 @@ JOB_DONE: } } -inline void *vect_pop(vector const v) { +inline void *vect_pop(c_vector const v) { void *item = NULL; zvect_retval rval = p_vect_check(v); if (rval) { @@ -1887,7 +1887,7 @@ JOB_DONE: return item; } -void *vect_remove(vector const v) { +void *vect_remove(c_vector const v) { void *item = NULL; zvect_retval rval = p_vect_check(v); if (rval) { @@ -1917,7 +1917,7 @@ JOB_DONE: return item; } -void *vect_remove_at(vector const v, const zvect_index i) { +void *vect_remove_at(c_vector const v, const zvect_index i) { void *item = NULL; zvect_retval rval = p_vect_check(v); if (rval) { @@ -1946,7 +1946,7 @@ JOB_DONE: return item; } -void *vect_remove_front(vector const v) { +void *vect_remove_front(c_vector const v) { void *item = NULL; zvect_retval rval = p_vect_check(v); if (rval) { @@ -1976,7 +1976,7 @@ JOB_DONE: } // Delete an item at the END of the vector -void vect_delete(vector const v) { +void vect_delete(c_vector const v) { zvect_retval rval = p_vect_check(v); if (rval) { goto JOB_DONE; @@ -2001,7 +2001,7 @@ JOB_DONE: } // Delete an item at position "i" on the vector -void vect_delete_at(const vector v, const zvect_index i) { +void vect_delete_at(const c_vector v, const zvect_index i) { zvect_retval rval = p_vect_check(v); if (rval) { goto JOB_DONE; @@ -2026,7 +2026,7 @@ JOB_DONE: } // Delete a range of items from "first_element" to "last_element" on the vector v -void vect_delete_range(vector const v, const zvect_index first_element, const zvect_index last_element) { +void vect_delete_range(c_vector const v, const zvect_index first_element, const zvect_index last_element) { zvect_retval rval = p_vect_check(v); if (rval) { goto JOB_DONE; @@ -2052,7 +2052,7 @@ JOB_DONE: } // Delete an item at the BEGINNING of a vector v -void vect_delete_front(vector const v) { +void vect_delete_front(c_vector const v) { zvect_retval rval = p_vect_check(v); if (rval) { goto JOB_DONE; @@ -2082,7 +2082,7 @@ JOB_DONE: // Vector Data Manipulation functions #ifdef ZVECT_DMF_EXTENSIONS -void vect_swap(vector const v, const zvect_index i1, const zvect_index i2) { +void vect_swap(c_vector const v, const zvect_index i1, const zvect_index i2) { // Check parameters: if (i1 == i2) { return; @@ -2123,7 +2123,7 @@ JOB_DONE: } } -void vect_swap_range(vector const v, const zvect_index s1, const zvect_index e1, const zvect_index s2) { +void vect_swap_range(c_vector const v, const zvect_index s1, const zvect_index e1, const zvect_index s2) { // Check parameters: if (s1 == s2) { return; @@ -2172,7 +2172,7 @@ JOB_DONE: } } -void vect_rotate_left(vector const v, const zvect_index i) { +void vect_rotate_left(c_vector const v, const zvect_index i) { // check if the vector exists: zvect_retval rval = p_vect_check(v); @@ -2228,7 +2228,7 @@ JOB_DONE: } } -void vect_rotate_right(vector const v, const zvect_index i) { +void vect_rotate_right(c_vector const v, const zvect_index i) { // check if the vector exists: zvect_retval rval = p_vect_check(v); if (rval) { @@ -2321,7 +2321,7 @@ static void p_vect_qsort(vector v, zvect_index low, zvect_index high, int (*comp // it fundamentally uses the 3 ways partitioning adapted and improved // to deal with arrays of pointers together with having a custom // compare function: -static void p_vect_qsort(const vector v, +static void p_vect_qsort(const c_vector v, zvect_index l, zvect_index r, int (*compare_func)(const void *, const void *)) { @@ -2384,7 +2384,7 @@ static void p_vect_qsort(const vector v, } #endif // ! TRADITIONAL_QSORT -void vect_qsort(const vector v, int (*compare_func)(const void *, const void *)) { +void vect_qsort(const c_vector v, int (*compare_func)(const void *, const void *)) { // Check parameters: if (compare_func == NULL) { return; @@ -2459,7 +2459,7 @@ static bool p_standard_binary_search(vector v, // original design, most notably the use of custom compare // function that makes it suitable also to search through strings // and other types of vectors. -static bool p_adaptive_binary_search(vector const v, +static bool p_adaptive_binary_search(c_vector const v, const void *key, zvect_index *item_index, int (*f1)(const void *, const void *)) { @@ -2538,7 +2538,7 @@ MONOBOUND: } #endif // ! TRADITIONAL_BINARY_SEARCH -bool vect_bsearch(vector const v, const void *key, int (*f1)(const void *, const void *), zvect_index *item_index) { +bool vect_bsearch(c_vector const v, const void *key, int (*f1)(const void *, const void *), zvect_index *item_index) { // Check parameters: if ((key == NULL) || (f1 == NULL) || (p_vect_size(v) == 0)) { return false; @@ -2586,7 +2586,7 @@ JOB_DONE: * functions, the vect_add_ordered is an exception because it * requires vect_bserach and vect_qsort to be available. */ -void vect_add_ordered(vector const v, const void *value, int (*f1)(const void *, const void *)) { +void vect_add_ordered(c_vector const v, const void *value, int (*f1)(const void *, const void *)) { // Check parameters: if (value == NULL) { return; @@ -2656,7 +2656,7 @@ JOB_DONE: #ifdef ZVECT_SFMD_EXTENSIONS // Single Function Call Multiple Data operations extensions: -void vect_apply(vector const v, void (*f)(void *)) { +void vect_apply(c_vector const v, void (*f)(void *)) { // Check Parameters: if (f == NULL) { return; @@ -2689,7 +2689,7 @@ JOB_DONE: } } -void vect_apply_range(vector const v, void (*f)(void *), const zvect_index x, const zvect_index y) { +void vect_apply_range(c_vector const v, void (*f)(void *), const zvect_index x, const zvect_index y) { // Check parameters: if (f == NULL) { return; @@ -2738,7 +2738,7 @@ JOB_DONE: } } -void vect_apply_if(vector const v1, vector const v2, void (*f1)(void *), bool (*f2)(void *, void *)) { +void vect_apply_if(c_vector const v1, c_vector const v2, void (*f1)(void *), bool (*f2)(void *, void *)) { // Check parameters: if (f1 == NULL || f2 == NULL) { return; @@ -2780,7 +2780,7 @@ JOB_DONE: } } -void vect_copy(vector const v1, vector const v2, const zvect_index s2, const zvect_index e2) { +void vect_copy(c_vector const v1, c_vector const v2, const zvect_index s2, const zvect_index e2) { // check if the vectors v1 and v2 exist: zvect_retval rval = p_vect_check(v1) | p_vect_check(v2); if (rval) { @@ -2844,7 +2844,8 @@ JOB_DONE: * position s1). * */ -void vect_insert(vector const v1, vector const v2, const zvect_index s2, const zvect_index e2, const zvect_index s1) { +void vect_insert(c_vector const v1, + c_vector const v2, const zvect_index s2, const zvect_index e2, const zvect_index s1) { // check if the vectors v1 and v2 exist: zvect_retval rval = p_vect_check(v1) | p_vect_check(v2); if (rval) { @@ -2943,7 +2944,7 @@ JOB_DONE: * the end of it). * */ -static inline zvect_retval p_vect_move(vector const v1, vector v2, const zvect_index s2, const zvect_index e2) { +static inline zvect_retval p_vect_move(c_vector const v1, c_vector v2, const zvect_index s2, const zvect_index e2) { zvect_retval rval = 0; #ifdef DEBUG @@ -3075,7 +3076,7 @@ DONE_PROCESSING: ////////////////////////////////////////////////////////////////// // vect_move moves a portion of a vector (v2) into another (v1): -void vect_move(vector const v1, vector v2, const zvect_index s2, const zvect_index e2) { +void vect_move(c_vector const v1, c_vector v2, const zvect_index s2, const zvect_index e2) { // check if the vectors v1 and v2 exist: zvect_retval rval = p_vect_check(v1) | p_vect_check(v2); if (rval) { @@ -3128,8 +3129,8 @@ JOB_DONE: ////////////////////////////////////////////////////////////////// // vect_move_if moves a portion of a vector (v2) into another (v1) if f2 is true: -zvect_retval vect_move_if(vector const v1, - vector v2, +zvect_retval vect_move_if(c_vector const v1, + c_vector v2, const zvect_index s2, const zvect_index e2, zvect_retval (*f2)(void *, void *)) { @@ -3189,8 +3190,8 @@ JOB_DONE: ///////////////////////////////////////////////////////////////// // vect_move_on_signal -zvect_retval vect_move_on_signal(vector const v1, - vector v2, +zvect_retval vect_move_on_signal(c_vector const v1, + c_vector v2, const zvect_index s2, const zvect_index e2, zvect_retval (*f2)(void *, void *)) { @@ -3262,7 +3263,7 @@ JOB_DONE: ///////////////////////////////////////////////////////////////// // vect_merge merges a vector (v2) into another (v1) -void vect_merge(vector const v1, vector v2) { +void vect_merge(c_vector const v1, c_vector v2) { // check if the vector v1 exists: zvect_retval rval = p_vect_check(v1) | p_vect_check(v2); if (rval) { diff --git a/srcs/open_dhcp/opendhcpd.cpp b/srcs/open_dhcp/opendhcpd.cpp index 7300b65..53e7581 100644 --- a/srcs/open_dhcp/opendhcpd.cpp +++ b/srcs/open_dhcp/opendhcpd.cpp @@ -41,6 +41,7 @@ using namespace std; #include "dhcpd.h" #include "task_manager.h" #include "user_errno.h" +#include "misc.h" void on_system_exit(void *p); //Global Variables @@ -3293,8 +3294,9 @@ void loadDHCP() { } } + opendhcp_add_mac_filter(); +#if 0 ff = fopen(iniFile, "rt"); - if (ff) { char sectionName[512]; @@ -3345,7 +3347,7 @@ void loadDHCP() { if (p == dhcpCache.end()) { memset(&lump, 0, sizeof(data71)); - lump.mapname = mapname; + lump.mapname = mapname; lump.optionSize = optionData.optionSize; lump.options = optionData.options; dhcpEntry = createCache(&lump); @@ -3390,7 +3392,7 @@ void loadDHCP() { fclose(ff); } - +#endif f = fopen(leaFile, "rb"); if (f) { @@ -4384,7 +4386,7 @@ void *init(void *lparam) { logDHCPMess(logBuff, 1); } - cfig.lease = 36000; + cfig.lease = opendhcp_set_lease_time(); loadDHCP(); if (cfig.dhcpLogLevel > 1) { diff --git a/srcs/open_dhcp/opendhcpd.h b/srcs/open_dhcp/opendhcpd.h index c413ae3..878237d 100644 --- a/srcs/open_dhcp/opendhcpd.h +++ b/srcs/open_dhcp/opendhcpd.h @@ -70,7 +70,7 @@ typedef struct in_pktinfo IN_PKTINFO; #define LPSOCKADDR sockaddr * #define closesocket close -#define STR2INT(val) ((int)strtol((val), nullptr, 10)) +#define STR2INT(val) ((int)strtol((val), nullptr, 10)) #define logDHCPMess(logBuff, logLevel) \ do { \ @@ -91,6 +91,7 @@ struct data7 //cache { char *mapname; time_t expiry; + union { struct { MYBYTE cType; @@ -98,6 +99,7 @@ struct data7 //cache MYBYTE sockInd; MYBYTE dnsIndex; }; + struct { unsigned fixed : 1; unsigned local : 1; @@ -107,20 +109,24 @@ struct data7 //cache 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; }; @@ -151,18 +157,18 @@ struct ConnType { bool ready; }; -#define BOOTP_REQUEST 1 -#define BOOTP_REPLY 2 +#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 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 @@ -291,10 +297,10 @@ 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 VM_STANFORD 0x5354414EUL -#define VM_RFC1048 0x63825363UL +#define IPPORT_DHCPS 67 +#define IPPORT_DHCPC 68 +#define VM_STANFORD 0x5354414EUL +#define VM_RFC1048 0x63825363UL struct data3 { MYBYTE opt_code; @@ -312,10 +318,12 @@ struct data4 { #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 { union { MYDWORD filler; + struct { unsigned dhcpInd : 20; unsigned bp_hlen : 8; @@ -325,6 +333,7 @@ struct data8 //client unsigned subnetFlg : 1; }; }; + MYDWORD ip; time_t expiry; MYBYTE bp_chaddr[16]; @@ -332,10 +341,12 @@ struct data8 //client }; #else #define MY_MAX_TIME INT_MAX + struct data8 //client { union { MYDWORD filler; + struct { unsigned dhcpInd : 20; unsigned bp_hlen : 8; @@ -345,6 +356,7 @@ struct data8 //client unsigned subnetFlg : 1; }; }; + MYDWORD ip; time_t expiry; MYDWORD filler1; @@ -449,10 +461,12 @@ struct data20 { struct data9 //dhcpRequst { MYDWORD lease; + union { char raw[sizeof(dhcp_packet)]; dhcp_packet dhcpp; }; + char hostname[256]; char chaddr[64]; MYDWORD server; @@ -491,10 +505,12 @@ struct DhcpConnType { MYDWORD mask; int reUseVal; int reUseSize; + union { int broadCastVal; bool pktinfoVal; }; + union { int broadCastSize; unsigned int pktinfoSize; @@ -552,83 +568,85 @@ struct data2 { }; //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); -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); -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 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 *); -MYDWORD fIP(void *raw); -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); -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 +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); +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); +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 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 *); +MYDWORD fIP(void *raw); +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(); +void opendhcp_add_mac_filter(); +int opendhcp_add_listener(); +unsigned int opendhcp_set_lease_time(); +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 c79a02a..39d201f 100644 --- a/srcs/open_dhcp/query.cpp +++ b/srcs/open_dhcp/query.cpp @@ -24,11 +24,13 @@ using namespace std; #include "proto.h" #include "user_errno.h" #include "uthash/uthash.h" +#include "sds/sds.h" extern data2 cfig; extern bool kRunning; extern dhcpMap dhcpCache; extern time_t t; +extern data71 lump; static int dhcp_get_user_info(data19 *req, const char *pRequest) { char logBuff[512]; @@ -466,7 +468,7 @@ static int delete_dhcpd_rangeset(data19 *req, const char *pRequest) { mySplit(start, end, del_range, '-'); st_addr = htonl(inet_addr(start)); en_addr = htonl(inet_addr(end)); - if(en_addr == 0) { + if (en_addr == 0) { cJSON_AddStringToObject(pdel_Item, "dhcpRange", del_range); cJSON_AddNumberToObject(pdel_Item, "status", ERR_INPUT_PARAMS); @@ -476,9 +478,9 @@ static int delete_dhcpd_rangeset(data19 *req, const char *pRequest) { HASH_FIND_INT(delMap, &st_addr, delItem); if (delItem == nullptr) { - auto *s = (struct hash_map *)malloc(sizeof(struct hash_map)); - s->key = st_addr; - s->value = en_addr; + auto *s = (struct hash_map *)malloc(sizeof(struct hash_map)); + s->key = st_addr; + s->value = en_addr; HASH_ADD_INT(delMap, key, s); } } @@ -499,10 +501,10 @@ static int delete_dhcpd_rangeset(data19 *req, const char *pRequest) { sprintf(del_range, "%s-%s", saddr, eaddr); cJSON_AddStringToObject(pdel_Item, "dhcpRange", del_range); //judge whether the input has error - if(delRange->value == cfig.dhcpRanges[i].rangeEnd) { - cJSON_AddNumberToObject(pdel_Item, "status", ERR_SUCCESS); - cJSON_AddStringToObject(pdel_Item, "message", getErrorEnumDesc(ERR_SUCCESS)); - cJSON_AddItemToArray(pdelArray, pdel_Item); + if (delRange->value == cfig.dhcpRanges[i].rangeEnd) { + cJSON_AddNumberToObject(pdel_Item, "status", ERR_SUCCESS); + cJSON_AddStringToObject(pdel_Item, "message", getErrorEnumDesc(ERR_SUCCESS)); + cJSON_AddItemToArray(pdelArray, pdel_Item); } else { memcpy(&dhcpRanges[resCount], &cfig.dhcpRanges[i], sizeof(struct data13)); @@ -521,16 +523,16 @@ static int delete_dhcpd_rangeset(data19 *req, const char *pRequest) { } //输入参数不存在的情况 - if(resCount > cfig.rangeCount - cJSON_GetArraySize(pdhcp_range)){ + if (resCount > cfig.rangeCount - cJSON_GetArraySize(pdhcp_range)) { cJSON *pdel_Item = cJSON_CreateObject(); - hash_map *s = (struct hash_map*)malloc(sizeof(struct hash_map)); + hash_map *s = (struct hash_map *)malloc(sizeof(struct hash_map)); hash_map *tmp; char saddr[128]; char eaddr[128]; char del_range[256]; memset(del_range, 0, 256); - HASH_ITER(hh, delMap, s, tmp){ + HASH_ITER(hh, delMap, s, tmp) { IP2String(saddr, ntohl(s->key)); IP2String(eaddr, ntohl(s->value)); @@ -1044,13 +1046,17 @@ static void opendhcp_http_query_rangeset(http_request *request, hw_http_response hw_http_response_send(response, req, response_complete); } +unsigned int opendhcp_set_lease_time() { + return config_get_dhcp_server_lease_time(); +} + /** * 添加配置文件监听接口 * @return */ int opendhcp_add_listener() { - int i; - vector listen_ip = config_get_dhcp_listen_on(); + int i; + c_vector listen_ip = config_get_dhcp_listen_on(); for (i = 0; listen_ip && i < vect_size(listen_ip); i++) { const char *pIp = (const char *)vect_get_at(listen_ip, i); @@ -1084,7 +1090,7 @@ void opendhcp_init_http_server() { * 增加 DHCP 主、从服务器配置 */ void opendhcp_set_replication_svr() { - vector replication = config_get_dhcp_replication_svr(); + c_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)); @@ -1096,15 +1102,65 @@ void opendhcp_set_replication_svr() { * 添加配置文件中的 DHCP 地址池池配置到服务中 */ void opendhcp_add_ip_pool_set() { - vector pool = config_get_dhcp_server_range_set(); + c_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); + dzlog_error("Add rangeset(\"%s\") failed!\n", pRange->rangAddr); } } } +} + +/** + * 增加MAC地址黑名单 + */ +void opendhcp_add_mac_filter() { + c_vector pool = config_get_dhcp_mac_filter(); + data20 optionData {}; + optionData.options[0] = 0; + optionData.options[1] = DHCP_OPTION_END; + sds add = sdsempty(); + sds err = sdsempty(); + + for (int i = 0; (pool && i < vect_size(pool)); i++) { + auto pRange = (char *)vect_get_at(pool, i); + + if (pRange) { + data7 *dhcpEntry; + + memset(&lump, 0, sizeof(data71)); + lump.mapname = pRange; + lump.optionSize = 2; + lump.options = optionData.options; + dhcpEntry = createCache(&lump); + + if (dhcpEntry) { + dhcpEntry->ip = 0; + dhcpEntry->rangeInd = -1; + dhcpEntry->fixed = 1; + dhcpEntry->expiry = 0; + dhcpCache[dhcpEntry->mapname] = dhcpEntry; + add = sdscatfmt(add, "\"%s\", ", pRange); + } else { + err = sdscatfmt(err, "\"%s\", ", pRange); + } + } + } + + if (sdslen(add) > 0) { + sdsrange(add, 0, -3); + dzlog_debug("Add MAC [%s] for black list\n", add); + } + + if (sdslen(err) > 0) { + sdsrange(err, 0, -2); + dzlog_debug("Add MAC [%s] for black list error\n", err); + } + + sdsfree(add); + sdsfree(err); } \ No newline at end of file