parent
e02c12f582
commit
395f199ff7
|
@ -4,8 +4,7 @@ INCLUDE(CMakeDependentOption)
|
|||
OPTION(VCPE_AGENT "Enable vCPE agent test application" OFF)
|
||||
OPTION(VCPE_PPPOE "Enable vCPE work befof PPPoE mode" OFF)
|
||||
OPTION(DHCP_TOOLS "Enable dhcp_tools for test dhcp servers with concurrency and multi-user" OFF)
|
||||
OPTION(USED_OPENDHCPD "DHCP server for vCPE" OFF)
|
||||
OPTION(USED_OPENDHCPDDNS "DHCP And DNS server for vCPE" OFF)
|
||||
OPTION(USED_DHCPSERVER "DHCP server for vCPE" OFF)
|
||||
OPTION(USED_USER_VNI "Support pass user vni id from console command line" OFF)
|
||||
OPTION(USED_JSON_VALIDATE "Support json protocol field validate" OFF)
|
||||
OPTION(BUILD_TESTING "Enable tests" OFF)
|
||||
|
@ -14,7 +13,7 @@ OPTION(BUILD_TESTING "Enable tests" OFF)
|
|||
OPTION(USED_REDIS "Add redis database support for vCPE" OFF)
|
||||
OPTION(USED_MYSQL "Add mysql database support for vCPE" OFF)
|
||||
OPTION(USED_SQLITE_CRYPTO "Sqlite3 database support crypto" OFF)
|
||||
CMAKE_DEPENDENT_OPTION(USED_HTTP_SVR "Build-in http(s) server support" ON "USED_OPENDHCPD OR USED_OPENDHCPDDNS" ON)
|
||||
CMAKE_DEPENDENT_OPTION(USED_HTTP_SVR "Build-in http(s) server support" ON "USED_DHCPSERVER" ON)
|
||||
CMAKE_DEPENDENT_OPTION(USED_LWIP "PPPoE of LWIP support for vCPE" ON "VCPE_PPPOE" OFF)
|
||||
CMAKE_DEPENDENT_OPTION(USED_ZMQ "ZeroMQ support for vCPE" ON "VCPE_PPPOE" OFF)
|
||||
CMAKE_DEPENDENT_OPTION(USED_SQLITE "Add sqlite3 database support for vCPE" ON "USED_SQLITE_CRYPTO" ON)
|
||||
|
@ -115,16 +114,10 @@ IF (USED_LWIP)
|
|||
MESSAGE("Select Option USED_LWIP")
|
||||
ENDIF ()
|
||||
|
||||
IF (USED_OPENDHCPD)
|
||||
LIST(APPEND COMMON_DEFINE "-DOPENDHCPD_ON")
|
||||
MESSAGE("Select Option USED_OPENDHCPD")
|
||||
ADD_SUBDIRECTORY(srcs/opendhcp183)
|
||||
ENDIF ()
|
||||
|
||||
IF (USED_OPENDHCPDDNS)
|
||||
LIST(APPEND COMMON_DEFINE "-DOPENDHCPDDNS_ON")
|
||||
MESSAGE("Select Option USED_OPENDHCPDDNS")
|
||||
ADD_SUBDIRECTORY(srcs/dual_server)
|
||||
IF (USED_DHCPSERVER)
|
||||
LIST(APPEND COMMON_DEFINE "-DDHCPD_ON")
|
||||
MESSAGE("Select Option USED_DHCPSERVER")
|
||||
ADD_SUBDIRECTORY(srcs/service)
|
||||
ENDIF ()
|
||||
|
||||
IF (VCPE_AGENT)
|
||||
|
@ -157,7 +150,6 @@ ENDIF ()
|
|||
|
||||
ADD_SUBDIRECTORY(srcs)
|
||||
ADD_SUBDIRECTORY(srcs/libs)
|
||||
ADD_SUBDIRECTORY(srcs/service)
|
||||
|
||||
IF (DHCP_TOOLS)
|
||||
ADD_SUBDIRECTORY(dhcp_tools)
|
||||
|
|
|
@ -31,10 +31,6 @@ IF (USED_LWIP)
|
|||
TARGET_LINK_LIBRARIES(${PROJECT_TARGET} lwip_linux)
|
||||
ENDIF ()
|
||||
|
||||
IF (USED_OPENDHCPDDNS)
|
||||
TARGET_LINK_LIBRARIES(${PROJECT_TARGET} dualserverd)
|
||||
ENDIF ()
|
||||
|
||||
IF (USED_OPENDHCPD)
|
||||
TARGET_LINK_LIBRARIES(${PROJECT_TARGET} opendhcpd)
|
||||
ENDIF ()
|
||||
|
|
|
@ -1,13 +0,0 @@
|
|||
SET(DHCPD_PROJECT_TARGET dualserverd)
|
||||
|
||||
PROJECT(${DHCPD_PROJECT_TARGET} CXX)
|
||||
|
||||
INCLUDE_DIRECTORIES(../libs/include ./)
|
||||
|
||||
FILE(GLOB DHCPD_HEADS ./*.h)
|
||||
|
||||
ADD_DEFINITIONS(-Wno-format-overflow -std=c++11)
|
||||
#ADD_EXECUTABLE(${PROJECT_TARGET} opendhcpd.cpp ${DHCPD_HEADS})
|
||||
ADD_LIBRARY(${DHCPD_PROJECT_TARGET} dualserverd.cpp ${DHCPD_HEADS})
|
||||
|
||||
#TARGET_LINK_LIBRARIES(${PROJECT_TARGET} -lpthread)
|
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
|
@ -1,18 +0,0 @@
|
|||
//
|
||||
// Created by xajhuang on 2022/10/26.
|
||||
//
|
||||
|
||||
#ifndef VCPE_PROJECT_DUALSVR_H
|
||||
#define VCPE_PROJECT_DUALSVR_H
|
||||
#define API_PUBLIC __attribute__((visibility("default")))
|
||||
#define API_EXPORT API_PUBLIC
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
API_EXPORT int dual_server_main(int argc, char **argv);
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
#endif//VCPE_PROJECT_DUALSVR_H
|
|
@ -166,7 +166,7 @@ typedef enum {
|
|||
MODE_DHCP_CLIENT,
|
||||
MODE_DHCP_SERVER
|
||||
} DHCP_WORK_MODE;
|
||||
int dhcpd_init(DHCP_WORK_MODE workMode);
|
||||
int dhcpd_init(DHCP_WORK_MODE workMode, const char *pIfName);
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
|
|
@ -78,8 +78,12 @@ IF (USED_JSON_VALIDATE)
|
|||
ENDIF ()
|
||||
|
||||
IF (USED_OPENDHCPD)
|
||||
TARGET_LINK_LIBRARIES(${LIB_PROJECT_TARGET} opendhcpd)
|
||||
ENDIF ()
|
||||
|
||||
IF (USED_DHCPSERVER)
|
||||
#TARGET_LINK_LIBRARIES(${LIB_PROJECT_TARGET} opendhcpd)
|
||||
TARGET_LINK_LIBRARIES(${LIB_PROJECT_TARGET} dhcpd opendhcpd)
|
||||
TARGET_LINK_LIBRARIES(${LIB_PROJECT_TARGET} dhcpd)
|
||||
ENDIF ()
|
||||
|
||||
TARGET_INCLUDE_DIRECTORIES(${LIB_PROJECT_TARGET} PUBLIC ${PROJECT_BINARY_DIR}/ ${CMAKE_BINARY_DIR}/)
|
||||
|
|
|
@ -3,8 +3,11 @@
|
|||
//
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#if !defined(OPENSSL_VERSION_NUMBER) || OPENSSL_VERSION_NUMBER < 0x10100000L
|
||||
|
||||
#include <openssl/crypto.h>
|
||||
|
||||
#else
|
||||
#include <openssl/opensslv.h>
|
||||
#endif
|
||||
|
@ -26,6 +29,7 @@
|
|||
#define REG_ICASE (ARG_REX_ICASE)
|
||||
|
||||
typedef void (*HelpCmdCb)(void *, const char *, void *);
|
||||
|
||||
typedef int (*ProcessCmdCb)(void **, const char *, void *);
|
||||
|
||||
typedef struct {
|
||||
|
@ -364,6 +368,34 @@ static int on_cmd6(void *pTbl[], const char *pName, void *pInfo) {
|
|||
return ERR_MENU_EXIT;
|
||||
}
|
||||
|
||||
#ifdef DHCPD_ON
|
||||
static int on_cmd8(void *pTbl[], const char *pName, void *pInfo) {
|
||||
int ret;
|
||||
PARG_TBL_INFO pArg = (PARG_TBL_INFO)pInfo;
|
||||
const char *pCfgFile = ((arg_file_t *)pTbl[1])->filename[0];
|
||||
const char *pCfgDir = ((arg_file_t *)pTbl[2])->filename[0];
|
||||
const char *pIfName = ((arg_str_t *)pTbl[3])->sval[0];
|
||||
const char *pKey = ((arg_str_t *)pTbl[4])->sval[0];
|
||||
|
||||
int help = get_help_cmd(pTbl, pArg)->count;
|
||||
|
||||
if (pArg->pHelp && help > 0) {
|
||||
pArg->pHelp(pArg->argTbl, pName, pTbl);
|
||||
return ERR_MENU_EXIT;
|
||||
}
|
||||
|
||||
if ((ret = user_init(pCfgFile, pCfgDir, pKey, 0)) != ERR_SUCCESS) {
|
||||
printf("System init error: %d\n", ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
dhcpd_init(MODE_DHCP_SERVER, pIfName);
|
||||
return ERR_SUCCESS;
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef OPENDHCPD_ON
|
||||
|
||||
typedef struct {
|
||||
int mode;
|
||||
const char *pIni;
|
||||
|
@ -371,12 +403,11 @@ typedef struct {
|
|||
const char *pIfName;
|
||||
} DHCPD_SETUP, *PDHCPD_SETUP;
|
||||
|
||||
#ifdef OPENDHCPD_ON
|
||||
static void dhcpd_task(void *pArg) {
|
||||
PDHCPD_SETUP p = (PDHCPD_SETUP)pArg;
|
||||
|
||||
if (p) {
|
||||
dhcpd_init(MODE_DHCP_SERVER);
|
||||
//dhcpd_init(MODE_DHCP_SERVER, p->pIfName);
|
||||
//dhcpd_main(p->mode, p->pIni, p->pStatus, p->pIfName);
|
||||
|
||||
if (p->pIni) {
|
||||
|
@ -397,7 +428,7 @@ static void dhcpd_task(void *pArg) {
|
|||
|
||||
static int on_cmd7(void *pTbl[], const char *pName, void *pInfo) {
|
||||
int ret;
|
||||
PDHCPD_SETUP p;
|
||||
// PDHCPD_SETUP p;
|
||||
static uv_thread_t uvThread;
|
||||
PARG_TBL_INFO pArg = (PARG_TBL_INFO)pInfo;
|
||||
int mode = ((arg_lit_t *)pTbl[1])->count;
|
||||
|
@ -432,6 +463,8 @@ static int on_cmd7(void *pTbl[], const char *pName, void *pInfo) {
|
|||
return ret;
|
||||
}
|
||||
|
||||
dhcpd_init(MODE_DHCP_SERVER, pIfName);
|
||||
#if 0
|
||||
p = (PDHCPD_SETUP)malloc(sizeof(DHCPD_SETUP));
|
||||
|
||||
if (p) {
|
||||
|
@ -441,7 +474,7 @@ static int on_cmd7(void *pTbl[], const char *pName, void *pInfo) {
|
|||
p->pIfName = pIfName ? strdup(pIfName) : NULL;
|
||||
uv_thread_create(&uvThread, dhcpd_task, p);
|
||||
}
|
||||
|
||||
#endif
|
||||
return ERR_SUCCESS;
|
||||
}
|
||||
#endif
|
||||
|
@ -454,22 +487,14 @@ static int on_cmd_(void *pTbl[], const char *pName, void *pInfo) {
|
|||
if (help > 0) {
|
||||
show_help(pName);
|
||||
} else {
|
||||
printf("Application information: %s (Build: %s %s GCC Ver:%s) With %lu(bits) OS\n",
|
||||
VCPE_GIT_VERSION,
|
||||
__DATE__,
|
||||
__TIME__,
|
||||
__VERSION__,
|
||||
sizeof(int *) * 8);
|
||||
printf("Application information: %s (Build: %s %s GCC Ver:%s) With %lu(bits) OS\n", VCPE_GIT_VERSION, __DATE__,
|
||||
__TIME__, __VERSION__, sizeof(int *) * 8);
|
||||
|
||||
#if !defined(OPENSSL_VERSION_NUMBER) || OPENSSL_VERSION_NUMBER < 0x10100000L
|
||||
printf("OpenSSL information: %s (%s, %s)\n",
|
||||
OPENSSL_VERSION_TEXT,
|
||||
SSLeay_version(SSLEAY_BUILT_ON),
|
||||
printf("OpenSSL information: %s (%s, %s)\n", OPENSSL_VERSION_TEXT, SSLeay_version(SSLEAY_BUILT_ON),
|
||||
SSLeay_version(SSLEAY_PLATFORM));
|
||||
#else
|
||||
printf("OpenSSL information: %s (%s, %s)\n",
|
||||
OPENSSL_VERSION_TEXT,
|
||||
OpenSSL_version(OPENSSL_BUILT_ON),
|
||||
printf("OpenSSL information: %s (%s, %s)\n", OPENSSL_VERSION_TEXT, OpenSSL_version(OPENSSL_BUILT_ON),
|
||||
OpenSSL_version(OPENSSL_PLATFORM));
|
||||
#endif
|
||||
}
|
||||
|
@ -486,39 +511,40 @@ int menu_run(int argc, char **argv) {
|
|||
* 系统服务启动参数
|
||||
******************************************************************/
|
||||
// clang-format off
|
||||
arg_rex_t *cmd1 = arg_rex1(NULL, NULL, "start", NULL, REG_ICASE, NULL);
|
||||
arg_file_t *cfgfile = arg_filen("c", "config", "[file]", 0, 1, "\tSpecify the current application configuration file path");
|
||||
arg_file_t *cfgdir = arg_file1("d", "directory", "<dir>", "\tSpecify the configuration directory path");
|
||||
arg_str_t *key1 = arg_str0("k", "key", "Key", "\tSpecify the configuration file cryptographic key.");
|
||||
arg_lit_t *helpCmd1 = arg_lit0("h", "help", NULL);
|
||||
arg_end_t *end1 = arg_end(20);
|
||||
void *pArgTbl1[] = {cmd1, cfgfile, cfgdir, key1, helpCmd1, end1};
|
||||
int errCode1 = 0;
|
||||
arg_rex_t *cmd1 = arg_rex1(NULL, NULL, "start", NULL, REG_ICASE, NULL);
|
||||
arg_file_t *cfgfile = arg_filen("c", "config", "[file]", 0, 1,
|
||||
"\tSpecify the current application configuration file path");
|
||||
arg_file_t *cfgdir = arg_file1("d", "directory", "<dir>", "\tSpecify the configuration directory path");
|
||||
arg_str_t *key1 = arg_str0("k", "key", "Key", "\tSpecify the configuration file cryptographic key.");
|
||||
arg_lit_t *helpCmd1 = arg_lit0("h", "help", NULL);
|
||||
arg_end_t *end1 = arg_end(20);
|
||||
void *pArgTbl1[] = {cmd1, cfgfile, cfgdir, key1, helpCmd1, end1};
|
||||
int errCode1 = 0;
|
||||
|
||||
arg_rex_t *cmd2 = arg_rex1(NULL, NULL, "if", NULL, REG_ICASE, NULL);
|
||||
arg_str_t *server = arg_str1("s", "server", "<svr>", "\tMessage queue server address.");
|
||||
arg_str_t *type = arg_str1("t", "command", "<get/set>", "\tMessage queue command.");
|
||||
arg_str_t *key2 = arg_str1("k", "key", "<key>", "\tSystem predefine command \033[1;7mKEY\033[0m value.");
|
||||
arg_str_t *value2 = arg_str0("v", "value", "value", "\tValue of key when command is need");
|
||||
arg_lit_t *helpCmd2 = arg_lit0("h", "help", NULL);
|
||||
arg_end_t *end2 = arg_end(20);
|
||||
void *pArgTbl2[] = {cmd2, server, type, key2, value2, helpCmd2, end2};
|
||||
int errCode2 = 0;
|
||||
arg_rex_t *cmd2 = arg_rex1(NULL, NULL, "if", NULL, REG_ICASE, NULL);
|
||||
arg_str_t *server = arg_str1("s", "server", "<svr>", "\tMessage queue server address.");
|
||||
arg_str_t *type = arg_str1("t", "command", "<get/set>", "\tMessage queue command.");
|
||||
arg_str_t *key2 = arg_str1("k", "key", "<key>", "\tSystem predefine command \033[1;7mKEY\033[0m value.");
|
||||
arg_str_t *value2 = arg_str0("v", "value", "value", "\tValue of key when command is need");
|
||||
arg_lit_t *helpCmd2 = arg_lit0("h", "help", NULL);
|
||||
arg_end_t *end2 = arg_end(20);
|
||||
void *pArgTbl2[] = {cmd2, server, type, key2, value2, helpCmd2, end2};
|
||||
int errCode2 = 0;
|
||||
|
||||
arg_rex_t *cmd3 = arg_rex1(NULL, NULL, "base64", NULL, REG_ICASE, NULL);
|
||||
arg_lit_t *operate3 = arg_litn("o", "operate", 0, 1, "\tBase64 decode, otherwise mean base64 encode");
|
||||
arg_str_t *value3 = arg_str0(NULL, NULL, "<base64 string>", NULL);
|
||||
arg_lit_t *helpCmd3 = arg_lit0("h", "help", NULL);
|
||||
arg_end_t *end3 = arg_end(20);
|
||||
void *pArgTbl3[] = {cmd3, operate3, value3, helpCmd3, end3};
|
||||
int errCode3 = 0;
|
||||
arg_rex_t *cmd3 = arg_rex1(NULL, NULL, "base64", NULL, REG_ICASE, NULL);
|
||||
arg_lit_t *operate3 = arg_litn("o", "operate", 0, 1, "\tBase64 decode, otherwise mean base64 encode");
|
||||
arg_str_t *value3 = arg_str0(NULL, NULL, "<base64 string>", NULL);
|
||||
arg_lit_t *helpCmd3 = arg_lit0("h", "help", NULL);
|
||||
arg_end_t *end3 = arg_end(20);
|
||||
void *pArgTbl3[] = {cmd3, operate3, value3, helpCmd3, end3};
|
||||
int errCode3 = 0;
|
||||
|
||||
arg_rex_t *cmd4 = arg_rex1(NULL, NULL, "hash", NULL, REG_ICASE, NULL);
|
||||
arg_file_t *file4 = arg_file0(NULL, NULL, "<file>", NULL);
|
||||
arg_str_t *alg4 = arg_str0("a", "algorithms", "algorithms", "\tChose \033[1;7mHASH\033[0m algorithms.");
|
||||
arg_lit_t *helpCmd4 = arg_lit0("h", "help", NULL);
|
||||
arg_end_t *end4 = arg_end(20);
|
||||
void *pArgTbl4[] = {cmd4,
|
||||
arg_rex_t *cmd4 = arg_rex1(NULL, NULL, "hash", NULL, REG_ICASE, NULL);
|
||||
arg_file_t *file4 = arg_file0(NULL, NULL, "<file>", NULL);
|
||||
arg_str_t *alg4 = arg_str0("a", "algorithms", "algorithms", "\tChose \033[1;7mHASH\033[0m algorithms.");
|
||||
arg_lit_t *helpCmd4 = arg_lit0("h", "help", NULL);
|
||||
arg_end_t *end4 = arg_end(20);
|
||||
void *pArgTbl4[] = {cmd4,
|
||||
alg4,
|
||||
file4,
|
||||
arg_rem(NULL, "\tSupport algorithms:"),
|
||||
|
@ -527,16 +553,16 @@ int menu_run(int argc, char **argv) {
|
|||
arg_rem(NULL, "\t \033[4msha256\033[0m: file sha256 value"),
|
||||
helpCmd4,
|
||||
end4};
|
||||
int errCode4 = 0;
|
||||
int errCode4 = 0;
|
||||
|
||||
arg_rex_t *cmd5 = arg_rex1(NULL, NULL, "cipher", NULL, REG_ICASE, NULL);
|
||||
arg_lit_t *operate5 = arg_litn("e", "encrypt", 0, 1, "\tEncryption, Ignore means decryption.");
|
||||
arg_str_t *alg5 = arg_str0("a", "algorithms", "algorithms", "\tChose \033[1;7mCryptography\033[0m algorithms.");
|
||||
arg_str_t *key5 = arg_str0("k", "key", "Key", "\tCryptographic key");
|
||||
arg_str_t *value5 = arg_str0(NULL, NULL, "<AES string>", NULL);
|
||||
arg_lit_t *helpCmd5 = arg_lit0("h", "help", NULL);
|
||||
arg_end_t *end5 = arg_end(20);
|
||||
void *pArgTbl5[] = {cmd5,
|
||||
arg_rex_t *cmd5 = arg_rex1(NULL, NULL, "cipher", NULL, REG_ICASE, NULL);
|
||||
arg_lit_t *operate5 = arg_litn("e", "encrypt", 0, 1, "\tEncryption, Ignore means decryption.");
|
||||
arg_str_t *alg5 = arg_str0("a", "algorithms", "algorithms", "\tChose \033[1;7mCryptography\033[0m algorithms.");
|
||||
arg_str_t *key5 = arg_str0("k", "key", "Key", "\tCryptographic key");
|
||||
arg_str_t *value5 = arg_str0(NULL, NULL, "<AES string>", NULL);
|
||||
arg_lit_t *helpCmd5 = arg_lit0("h", "help", NULL);
|
||||
arg_end_t *end5 = arg_end(20);
|
||||
void *pArgTbl5[] = {cmd5,
|
||||
operate5,
|
||||
alg5,
|
||||
key5,
|
||||
|
@ -546,7 +572,8 @@ int menu_run(int argc, char **argv) {
|
|||
arg_rem(NULL, "\t \033[4m3des_cbc\033[0m: 3DES CBC with PKCS7 padding"),
|
||||
arg_rem(NULL, "\t \033[4m3des_ofb\033[0m: 3DES OFB with PKCS7 padding"),
|
||||
arg_rem(NULL, "\t \033[4maes128_ecb\033[0m: 128 bit AES ECB with PKCS7 padding"),
|
||||
arg_rem(NULL, "\t \033[4maes128_ecb_sha1\033[0m: 128 bit AES ECB and used SHA1 key with PKCS7 padding"),
|
||||
arg_rem(NULL,
|
||||
"\t \033[4maes128_ecb_sha1\033[0m: 128 bit AES ECB and used SHA1 key with PKCS7 padding"),
|
||||
arg_rem(NULL, "\t \033[4maes128_cbc\033[0m: 128 bit AES CBC with PKCS7 padding"),
|
||||
arg_rem(NULL, "\t \033[4maes128_ofb\033[0m: 128 bit AES OFB with PKCS7 padding"),
|
||||
arg_rem(NULL, "\t \033[4maes192_ecb\033[0m: 192 bit AES ECB with PKCS7 padding"),
|
||||
|
@ -557,14 +584,26 @@ int menu_run(int argc, char **argv) {
|
|||
arg_rem(NULL, "\t \033[4maes256_ofb\033[0m: 256 bit AES OFB with PKCS7 padding"),
|
||||
helpCmd5,
|
||||
end5};
|
||||
int errCode5 = 0;
|
||||
int errCode5 = 0;
|
||||
|
||||
arg_rex_t *cmd6 = arg_rex1(NULL, NULL, "config", NULL, REG_ICASE, NULL);
|
||||
arg_str_t *keyGen = arg_str0("k", "keygen", "<Key>", "\tCreate configuration file cryptographic key.");
|
||||
arg_lit_t *helpCmd6 = arg_lit0("h", "help", NULL);
|
||||
arg_end_t *end6 = arg_end(20);
|
||||
void *pArgTbl6[] = {cmd6, keyGen, helpCmd6, end6};
|
||||
int errCode6 = 0;
|
||||
arg_rex_t *cmd6 = arg_rex1(NULL, NULL, "config", NULL, REG_ICASE, NULL);
|
||||
arg_str_t *keyGen = arg_str0("k", "keygen", "<Key>", "\tCreate configuration file cryptographic key.");
|
||||
arg_lit_t *helpCmd6 = arg_lit0("h", "help", NULL);
|
||||
arg_end_t *end6 = arg_end(20);
|
||||
void *pArgTbl6[] = {cmd6, keyGen, helpCmd6, end6};
|
||||
int errCode6 = 0;
|
||||
|
||||
#ifdef DHCPD_ON
|
||||
arg_rex_t *cmd8 = arg_rex1(NULL, NULL, "dhcpd", NULL, REG_ICASE, NULL);
|
||||
arg_file_t *cfgfile8 = arg_filen("c", "config", "[file]", 0, 1, "\tSpecify the current application configuration file path");
|
||||
arg_file_t *cfgdir8 = arg_file1("d", "directory", "<dir>", "\tSpecify the configuration directory path");
|
||||
arg_str_t *ifname = arg_str1("n", "nic", "<ifname>", "\tSpecify the dhcpd service network interface name.");
|
||||
arg_str_t *key8 = arg_str0("k", "key", "Key", "\tSpecify the configuration file cryptographic key.");
|
||||
arg_lit_t *helpCmd8 = arg_lit0("h", "help", NULL);
|
||||
arg_end_t *end8 = arg_end(20);
|
||||
void *pArgTbl8[] = {cmd8, cfgfile8, cfgdir8,ifname, key8, helpCmd8, end8};
|
||||
int errCode8 = 0;
|
||||
#endif
|
||||
|
||||
#ifdef OPENDHCPD_ON
|
||||
arg_rex_t *cmd7 = arg_rex1(NULL, NULL, "dhcpd", NULL, REG_ICASE, NULL);
|
||||
|
@ -585,23 +624,33 @@ int menu_run(int argc, char **argv) {
|
|||
/*****************************************************************
|
||||
* 系统帮助参数定义
|
||||
******************************************************************/
|
||||
arg_lit_t *help_ = arg_lit0("h", "help", "\tDisplay this help and exit");
|
||||
arg_lit_t *version_ = arg_lit0("v", "version", "\tDisplay application version information and exit");
|
||||
arg_end_t *end_ = arg_end(20);
|
||||
void *pArgTbl_[] = {version_, help_, end_};
|
||||
int errCode_ = 0;
|
||||
arg_lit_t *help_ = arg_lit0("h", "help", "\tDisplay this help and exit");
|
||||
arg_lit_t *version_ = arg_lit0("v", "version", "\tDisplay application version information and exit");
|
||||
arg_end_t *end_ = arg_end(20);
|
||||
void *pArgTbl_[] = {version_, help_, end_};
|
||||
int errCode_ = 0;
|
||||
|
||||
ARG_TBL_INFO argTblInfo[] = {
|
||||
{pArgTbl1, ARRAY_SIZE(pArgTbl1), &errCode1, on_cmd1, cmdn_help, "\r \033[1;4;36mstart\033[0m: \033[1mRun agent service normally.\033[0m\n"},
|
||||
{pArgTbl1, ARRAY_SIZE(pArgTbl1), &errCode1, on_cmd1, cmdn_help,
|
||||
"\r \033[1;4;36mstart\033[0m: \033[1mRun agent service normally.\033[0m\n"},
|
||||
#ifdef OPENDHCPD_ON
|
||||
{pArgTbl7, ARRAY_SIZE(pArgTbl7), &errCode7, on_cmd7, cmdn_help, "\n \033[1;4;36mdhcpd\033[0m: \033[1mRun DHCP service normally.\033[0m\n"},
|
||||
{pArgTbl7, ARRAY_SIZE(pArgTbl7), &errCode7, on_cmd7, cmdn_help, "\n \033[1;4;36mdhcpd\033[0m: \033[1mRun DHCP service normally.\033[0m\n"},
|
||||
#endif
|
||||
{pArgTbl2, ARRAY_SIZE(pArgTbl2), &errCode2, on_cmd2, cmd2_help, "\n \033[1;4;36mif\033[0m: \033[1mRun agent interface tools.\033[0m\n"},
|
||||
{pArgTbl3, ARRAY_SIZE(pArgTbl3), &errCode3, on_cmd3, cmdn_help, "\n \033[1;4;36mbase64\033[0m: \033[1mString base64 encode/decode.\033[0m\n"},
|
||||
{pArgTbl4, ARRAY_SIZE(pArgTbl4), &errCode4, on_cmd4, cmdn_help, "\n \033[1;4;36mhash\033[0m: \033[1mCalculate file's hash value.\033[0m\n"},
|
||||
{pArgTbl5, ARRAY_SIZE(pArgTbl5), &errCode5, on_cmd5, cmdn_help, "\n \033[1;4;36mcipher\033[0m: \033[1mSymmetric cryptography for string.\033[0m\n"},
|
||||
{pArgTbl6, ARRAY_SIZE(pArgTbl6), &errCode6, on_cmd6, cmdn_help, "\n \033[1;4;36mconfig\033[0m: \033[1mConfiguration file tools.\033[0m\n"},
|
||||
{pArgTbl_, ARRAY_SIZE(pArgTbl_), &errCode_, on_cmd_, NULL, NULL},
|
||||
#ifdef DHCPD_ON
|
||||
{pArgTbl8, ARRAY_SIZE(pArgTbl8), &errCode8, on_cmd8, cmdn_help,
|
||||
"\n \033[1;4;36mdhcpd\033[0m: \033[1mRun DHCP service normally.\033[0m\n"},
|
||||
#endif
|
||||
{pArgTbl2, ARRAY_SIZE(pArgTbl2), &errCode2, on_cmd2, cmd2_help,
|
||||
"\n \033[1;4;36mif\033[0m: \033[1mRun agent interface tools.\033[0m\n"},
|
||||
{pArgTbl3, ARRAY_SIZE(pArgTbl3), &errCode3, on_cmd3, cmdn_help,
|
||||
"\n \033[1;4;36mbase64\033[0m: \033[1mString base64 encode/decode.\033[0m\n"},
|
||||
{pArgTbl4, ARRAY_SIZE(pArgTbl4), &errCode4, on_cmd4, cmdn_help,
|
||||
"\n \033[1;4;36mhash\033[0m: \033[1mCalculate file's hash value.\033[0m\n"},
|
||||
{pArgTbl5, ARRAY_SIZE(pArgTbl5), &errCode5, on_cmd5, cmdn_help,
|
||||
"\n \033[1;4;36mcipher\033[0m: \033[1mSymmetric cryptography for string.\033[0m\n"},
|
||||
{pArgTbl6, ARRAY_SIZE(pArgTbl6), &errCode6, on_cmd6, cmdn_help,
|
||||
"\n \033[1;4;36mconfig\033[0m: \033[1mConfiguration file tools.\033[0m\n"},
|
||||
{pArgTbl_, ARRAY_SIZE(pArgTbl_), &errCode_, on_cmd_, NULL, NULL},
|
||||
};
|
||||
|
||||
// clang-format on
|
||||
|
|
|
@ -114,7 +114,7 @@ static CFG_ITEM g_cfgItem[] = {
|
|||
#endif
|
||||
DEF_CFG_ITEM(CFG_PROTO_CRYPTO, "protocol.crypto_type", VAL_INT, "0", "Protocol crypto algorithm"),
|
||||
DEF_CFG_ITEM(CFG_PROTO_CRYPTO_KEY, "protocol.crypto_key", VAL_STR, "", "Protocol crypto keys"),
|
||||
#ifdef OPENDHCPD_ON
|
||||
#ifdef DHCPD_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"),
|
||||
|
@ -257,7 +257,7 @@ static int load_array_obj(const char *pKeyName, PCONFIG_ITEM pValue) {
|
|||
config_setting_t *pObj = config_setting_get_elem(pItem, i);
|
||||
|
||||
switch (pValue->cfgId) {
|
||||
#if OPENDHCPD_ON
|
||||
#if DHCPD_ON
|
||||
case CFG_DHCP_RANGE_SET: {
|
||||
zvect_index idx = -1;
|
||||
const char *rangAddr, *subnet, *dnsSvr, *gateway;
|
||||
|
|
|
@ -29,7 +29,7 @@ const char *config_get_proto_crypto_key() {
|
|||
return cfg_get_string_value(CFG_PROTO_CRYPTO_KEY);
|
||||
}
|
||||
|
||||
#if OPENDHCPD_ON
|
||||
#if DHCPD_ON
|
||||
unsigned int config_get_dhcp_server_lease_time() {
|
||||
return cfg_get_integral_value(CFG_DHCP_LEASE_TIME);
|
||||
}
|
||||
|
|
|
@ -75,7 +75,7 @@ typedef enum {
|
|||
#endif
|
||||
CFG_PROTO_CRYPTO,
|
||||
CFG_PROTO_CRYPTO_KEY,
|
||||
#ifdef OPENDHCPD_ON
|
||||
#ifdef DHCPD_ON
|
||||
CFG_DHCP_LEASE_TIME,
|
||||
CFG_DHCP_LISTEN_ON,
|
||||
CFG_DHCP_REPLICATION_SVR,
|
||||
|
@ -148,7 +148,7 @@ const char *config_get_proto_crypto_key();
|
|||
void cfg_set_user_vni_id(int vni);
|
||||
int cfg_get_user_vni_id();
|
||||
#endif
|
||||
#ifdef OPENDHCPD_ON
|
||||
#ifdef DHCPD_ON
|
||||
unsigned int config_get_dhcp_server_lease_time();
|
||||
c_vector config_get_dhcp_server_range_set();
|
||||
c_vector config_get_dhcp_listen_on();
|
||||
|
|
|
@ -30,7 +30,7 @@ STRING(REPLACE ";" "," SUPPORTED_OPTIONAL_STR "${SUPPORTED_OPTIONAL}")
|
|||
|
||||
ADD_LIBRARY(jsoncdac STATIC ${jsoncdac_SOURCES})
|
||||
TARGET_COMPILE_DEFINITIONS(jsoncdac PRIVATE "SUPPORTED_KEYWORDS_BASE=\"${SUPPORTED_BASE_STR}\"" "SUPPORTED_KEYWORDS_OPTIONAL=\"${SUPPORTED_OPTIONAL_STR}\"")
|
||||
TARGET_LINK_LIBRARIES(jsoncdac ${EXTRA_LIBS})
|
||||
TARGET_LINK_LIBRARIES(jsoncdac ${EXTRA_LIBS} -lm)
|
||||
|
||||
SET_TARGET_PROPERTIES(
|
||||
jsoncdac
|
||||
|
|
|
@ -3,7 +3,7 @@ INCLUDE_DIRECTORIES(. ../include ../libs/include ./dhcpd/include ../libs/mongoos
|
|||
|
||||
AUX_SOURCE_DIRECTORY(dhcpd DHCPD_SRC)
|
||||
|
||||
ADD_DEFINITIONS(-DOPENDHCPD_ON)
|
||||
ADD_DEFINITIONS(-DDHCPD_ON)
|
||||
ADD_DEFINITIONS(${COMMON_DEFINE})
|
||||
|
||||
ADD_LIBRARY(dhcpd ${DHCPD_SRC} ${DHCPD_HEADS} dhcpd/db_interface.c dhcpd/include/db_interface.h)
|
|
@ -147,13 +147,13 @@ static struct sock_filter g_filterCode[] = {
|
|||
#endif
|
||||
|
||||
static struct sock_fprog bpf;
|
||||
static PACKET_MMAP_RING g_pkgRing;
|
||||
static NIC_INFO g_nicInfo;
|
||||
static uv_udp_t g_uvRawSockReq;
|
||||
static DHCP_WORK_MODE g_dhcpMode;
|
||||
static PACKET_MMAP_RING g_pkgRing;
|
||||
static NIC_INFO g_nicInfo;
|
||||
static uv_udp_t g_uvRawSockReq;
|
||||
static DHCP_WORK_MODE g_dhcpMode;
|
||||
|
||||
VLAN_TYPE get_package_vlan_type(void *pBuf) {
|
||||
U8 *p = (U8 *)pBuf;
|
||||
U8 *p = (U8 *) pBuf;
|
||||
|
||||
if (p[12] == 0x81 && p[13] == 0x00 && p[16] == 0x81 && p[17] == 0x00) {
|
||||
return VLAN_LEVEL2;
|
||||
|
@ -169,15 +169,15 @@ U32 dhcp_get_default_netmask() {
|
|||
}
|
||||
|
||||
void *get_pkg_free_buf() {
|
||||
int i;
|
||||
int i;
|
||||
struct tpacket3_hdr *hdr;
|
||||
|
||||
for (i = 0; i < g_pkgRing.send.tp_frame_nr; i++) {
|
||||
hdr = (struct tpacket3_hdr *)(g_pkgRing.tx[0].iov_base + (g_pkgRing.send.tp_frame_size * g_pkgRing.index));
|
||||
hdr = (struct tpacket3_hdr *) (g_pkgRing.tx[0].iov_base + (g_pkgRing.send.tp_frame_size * g_pkgRing.index));
|
||||
g_pkgRing.index = (g_pkgRing.index + 1) % g_pkgRing.send.tp_frame_nr;
|
||||
|
||||
if (!(hdr->tp_status & (TP_STATUS_SEND_REQUEST | TP_STATUS_SENDING))) {
|
||||
return (U8 *)hdr + TPACKET3_HDRLEN - sizeof(struct sockaddr_ll);
|
||||
return (U8 *) hdr + TPACKET3_HDRLEN - sizeof(struct sockaddr_ll);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -185,17 +185,17 @@ void *get_pkg_free_buf() {
|
|||
}
|
||||
|
||||
U32 pkg_mmap_tx(U8 *pData, U32 nBytes) {
|
||||
struct tpacket3_hdr *hdr = (struct tpacket3_hdr *)(pData + sizeof(struct sockaddr_ll) - TPACKET3_HDRLEN);
|
||||
struct tpacket3_hdr *hdr = (struct tpacket3_hdr *) (pData + sizeof(struct sockaddr_ll) - TPACKET3_HDRLEN);
|
||||
|
||||
hdr->tp_next_offset = 0;
|
||||
hdr->tp_len = nBytes;
|
||||
hdr->tp_snaplen = nBytes;
|
||||
hdr->tp_status = TP_STATUS_SEND_REQUEST;
|
||||
hdr->tp_len = nBytes;
|
||||
hdr->tp_snaplen = nBytes;
|
||||
hdr->tp_status = TP_STATUS_SEND_REQUEST;
|
||||
return hdr->tp_len;
|
||||
}
|
||||
|
||||
static void add_eth_head(U8 *pRsp, struct ethhdr *pEth, int isBst) {
|
||||
struct ethhdr *p = (struct ethhdr *)pRsp;
|
||||
struct ethhdr *p = (struct ethhdr *) pRsp;
|
||||
|
||||
// 二层头
|
||||
// 目的 MAC 地址
|
||||
|
@ -212,28 +212,28 @@ static void add_eth_head(U8 *pRsp, struct ethhdr *pEth, int isBst) {
|
|||
}
|
||||
|
||||
static void add_vlan_head(U8 *pRsp, PVLAN_HDR pvHdr) {
|
||||
PVLAN_HDR p = (PVLAN_HDR)(pRsp + sizeof(struct ethhdr));
|
||||
PVLAN_HDR p = (PVLAN_HDR) (pRsp + sizeof(struct ethhdr));
|
||||
|
||||
// QinQ 隧道
|
||||
p->id = pvHdr->id;
|
||||
p->id = pvHdr->id;
|
||||
p->type = pvHdr->type;
|
||||
}
|
||||
|
||||
static void add_vlan2_head(U8 *pRsp, PVLAN_HDR2 pvHdr) {
|
||||
PVLAN_HDR2 p = (PVLAN_HDR2)(pRsp + sizeof(struct ethhdr));
|
||||
PVLAN_HDR2 p = (PVLAN_HDR2) (pRsp + sizeof(struct ethhdr));
|
||||
|
||||
// QinQ 隧道
|
||||
p->id1 = pvHdr->id1;
|
||||
p->id1 = pvHdr->id1;
|
||||
p->h_type = pvHdr->h_type;
|
||||
p->id2 = pvHdr->id2;
|
||||
p->type = pvHdr->type;
|
||||
p->id2 = pvHdr->id2;
|
||||
p->type = pvHdr->type;
|
||||
}
|
||||
|
||||
static void add_ip_head(struct iphdr *p, struct iphdr *pIp) {
|
||||
// IP 头
|
||||
memcpy(p, pIp, sizeof(struct iphdr));
|
||||
// TOS
|
||||
p->tos = 0;
|
||||
p->tos = 0;
|
||||
// 更新源IP
|
||||
p->saddr = g_nicInfo.ipAddr;
|
||||
// 更新目的IP地址,广播255.255.255.255
|
||||
|
@ -247,7 +247,7 @@ static void add_ip_head(struct iphdr *p, struct iphdr *pIp) {
|
|||
static void add_udp_head(struct udphdr *p, struct udphdr *pUdp) {
|
||||
// UDP 头
|
||||
// 目的端口
|
||||
p->dest = pUdp->source;
|
||||
p->dest = pUdp->source;
|
||||
// 源端口
|
||||
p->source = pUdp->dest;
|
||||
}
|
||||
|
@ -255,13 +255,13 @@ static void add_udp_head(struct udphdr *p, struct udphdr *pUdp) {
|
|||
static void add_dhcp_common(PDHCP_PROTO p, PDHCP_PROTO pDhcp) {
|
||||
//DHCP 协议
|
||||
// 返回类型
|
||||
p->op = BOOTP_REPLY;
|
||||
p->op = BOOTP_REPLY;
|
||||
// 地址类型长度
|
||||
p->htype = pDhcp->htype;
|
||||
// 地址长度
|
||||
p->hlen = pDhcp->hlen;
|
||||
p->hlen = pDhcp->hlen;
|
||||
// xid
|
||||
p->xid = pDhcp->xid;
|
||||
p->xid = pDhcp->xid;
|
||||
// 客户端 MAC 地址
|
||||
memcpy(p->chaddr, pDhcp->chaddr, ETH_ALEN);
|
||||
// DHCP服务端主机名
|
||||
|
@ -271,81 +271,81 @@ static void add_dhcp_common(PDHCP_PROTO p, PDHCP_PROTO pDhcp) {
|
|||
// memcpy(&pRsp->dhcp.cookie, DHCP_COOKIE_VAL, 4);
|
||||
p->cookie = htonl(DHCP_COOKIE_VAL);
|
||||
|
||||
p->hops = 0;
|
||||
p->secs = 0;
|
||||
p->flags = 0;
|
||||
p->hops = 0;
|
||||
p->secs = 0;
|
||||
p->flags = 0;
|
||||
p->ciaddr = 0;
|
||||
p->siaddr = 0;
|
||||
p->giaddr = 0;
|
||||
}
|
||||
|
||||
static PDHCP_PROTO fill_pkg(U8 *pRsp, U8 *pReq) {
|
||||
U8 *p;
|
||||
struct ethhdr *pEth = (struct ethhdr *)pReq;
|
||||
struct iphdr *pIp = NULL;
|
||||
struct udphdr *pUdp = NULL;
|
||||
PDHCP_PROTO pDhcp = NULL;
|
||||
U8 *p;
|
||||
struct ethhdr *pEth = (struct ethhdr *) pReq;
|
||||
struct iphdr *pIp = NULL;
|
||||
struct udphdr *pUdp = NULL;
|
||||
PDHCP_PROTO pDhcp = NULL;
|
||||
|
||||
memset(pRsp, 0, MAX_DHCP_PKG_SIZE);
|
||||
switch (get_package_vlan_type(pReq)) {
|
||||
default:
|
||||
pIp = (struct iphdr *)(pReq + IP_HDR_OFFSET);
|
||||
pUdp = (struct udphdr *)(pReq + UDP_HDR_OFFSET);
|
||||
pDhcp = (PDHCP_PROTO)((U8 *)pReq + DHCP_OFFSET);
|
||||
p = pRsp + sizeof(struct ethhdr);
|
||||
pIp = (struct iphdr *) (pReq + IP_HDR_OFFSET);
|
||||
pUdp = (struct udphdr *) (pReq + UDP_HDR_OFFSET);
|
||||
pDhcp = (PDHCP_PROTO) ((U8 *) pReq + DHCP_OFFSET);
|
||||
p = pRsp + sizeof(struct ethhdr);
|
||||
break;
|
||||
case VLAN_LEVEL1:
|
||||
pIp = (struct iphdr *)(pReq + IP_HDR_OFFSET + sizeof(VLAN_HDR));
|
||||
pUdp = (struct udphdr *)(pReq + UDP_HDR_OFFSET + sizeof(VLAN_HDR));
|
||||
pDhcp = (PDHCP_PROTO)((U8 *)pReq + DHCP_OFFSET + sizeof(VLAN_HDR));
|
||||
add_vlan_head(pRsp, (PVLAN_HDR)(pReq + IP_HDR_OFFSET));
|
||||
pIp = (struct iphdr *) (pReq + IP_HDR_OFFSET + sizeof(VLAN_HDR));
|
||||
pUdp = (struct udphdr *) (pReq + UDP_HDR_OFFSET + sizeof(VLAN_HDR));
|
||||
pDhcp = (PDHCP_PROTO) ((U8 *) pReq + DHCP_OFFSET + sizeof(VLAN_HDR));
|
||||
add_vlan_head(pRsp, (PVLAN_HDR) (pReq + IP_HDR_OFFSET));
|
||||
p = pRsp + sizeof(struct ethhdr) + sizeof(VLAN_HDR);
|
||||
break;
|
||||
case VLAN_LEVEL2:
|
||||
pIp = (struct iphdr *)(pReq + IP_HDR_OFFSET + sizeof(VLAN_HDR2));
|
||||
pUdp = (struct udphdr *)(pReq + UDP_HDR_OFFSET + sizeof(VLAN_HDR2));
|
||||
pDhcp = (PDHCP_PROTO)((U8 *)pReq + DHCP_OFFSET + sizeof(VLAN_HDR2));
|
||||
add_vlan2_head(pRsp, (PVLAN_HDR2)(pReq + IP_HDR_OFFSET));
|
||||
pIp = (struct iphdr *) (pReq + IP_HDR_OFFSET + sizeof(VLAN_HDR2));
|
||||
pUdp = (struct udphdr *) (pReq + UDP_HDR_OFFSET + sizeof(VLAN_HDR2));
|
||||
pDhcp = (PDHCP_PROTO) ((U8 *) pReq + DHCP_OFFSET + sizeof(VLAN_HDR2));
|
||||
add_vlan2_head(pRsp, (PVLAN_HDR2) (pReq + IP_HDR_OFFSET));
|
||||
p = pRsp + sizeof(struct ethhdr) + sizeof(VLAN_HDR2);
|
||||
break;
|
||||
}
|
||||
|
||||
add_eth_head(pRsp, pEth, (pIp->saddr == 0) ? TRUE : FALSE);
|
||||
add_ip_head((struct iphdr *)p, pIp);
|
||||
add_ip_head((struct iphdr *) p, pIp);
|
||||
p += sizeof(struct iphdr);
|
||||
add_udp_head((struct udphdr *)p, pUdp);
|
||||
add_udp_head((struct udphdr *) p, pUdp);
|
||||
p += sizeof(struct udphdr);
|
||||
add_dhcp_common((PDHCP_PROTO)p, pDhcp);
|
||||
add_dhcp_common((PDHCP_PROTO) p, pDhcp);
|
||||
|
||||
return (PDHCP_PROTO)p;
|
||||
return (PDHCP_PROTO) p;
|
||||
}
|
||||
|
||||
static int dhcp_prepare_tx(U8 *pRsp, U32 optSize) {
|
||||
struct iphdr *pIp = NULL;
|
||||
struct iphdr *pIp = NULL;
|
||||
struct udphdr *pUdp = NULL;
|
||||
U16 csum;
|
||||
U32 tolSize;
|
||||
U8 vlanSize = 0;
|
||||
U16 csum;
|
||||
U32 tolSize;
|
||||
U8 vlanSize = 0;
|
||||
|
||||
switch (get_package_vlan_type(pRsp)) {
|
||||
default:
|
||||
pIp = (struct iphdr *)(pRsp + IP_HDR_OFFSET);
|
||||
pUdp = (struct udphdr *)(pRsp + UDP_HDR_OFFSET);
|
||||
pIp = (struct iphdr *) (pRsp + IP_HDR_OFFSET);
|
||||
pUdp = (struct udphdr *) (pRsp + UDP_HDR_OFFSET);
|
||||
// 计算包总长度
|
||||
tolSize = optSize + sizeof(DHCP_PROTO) + DHCP_OFFSET;
|
||||
break;
|
||||
case VLAN_LEVEL1:
|
||||
pIp = (struct iphdr *)(pRsp + IP_HDR_OFFSET + sizeof(VLAN_HDR));
|
||||
pUdp = (struct udphdr *)(pRsp + UDP_HDR_OFFSET + sizeof(VLAN_HDR));
|
||||
pIp = (struct iphdr *) (pRsp + IP_HDR_OFFSET + sizeof(VLAN_HDR));
|
||||
pUdp = (struct udphdr *) (pRsp + UDP_HDR_OFFSET + sizeof(VLAN_HDR));
|
||||
// 计算包总长度
|
||||
tolSize = optSize + sizeof(DHCP_PROTO) + DHCP_OFFSET + sizeof(VLAN_HDR);
|
||||
tolSize = optSize + sizeof(DHCP_PROTO) + DHCP_OFFSET + sizeof(VLAN_HDR);
|
||||
vlanSize = sizeof(VLAN_HDR);
|
||||
break;
|
||||
case VLAN_LEVEL2:
|
||||
pIp = (struct iphdr *)(pRsp + IP_HDR_OFFSET + sizeof(VLAN_HDR2));
|
||||
pUdp = (struct udphdr *)(pRsp + UDP_HDR_OFFSET + sizeof(VLAN_HDR2));
|
||||
pIp = (struct iphdr *) (pRsp + IP_HDR_OFFSET + sizeof(VLAN_HDR2));
|
||||
pUdp = (struct udphdr *) (pRsp + UDP_HDR_OFFSET + sizeof(VLAN_HDR2));
|
||||
// 计算包总长度
|
||||
tolSize = optSize + sizeof(DHCP_PROTO) + DHCP_OFFSET + sizeof(VLAN_HDR2);
|
||||
tolSize = optSize + sizeof(DHCP_PROTO) + DHCP_OFFSET + sizeof(VLAN_HDR2);
|
||||
vlanSize = sizeof(VLAN_HDR2);
|
||||
break;
|
||||
}
|
||||
|
@ -353,20 +353,20 @@ static int dhcp_prepare_tx(U8 *pRsp, U32 optSize) {
|
|||
// 计算 IP 数据长度
|
||||
pIp->tot_len = htons(tolSize - sizeof(struct ethhdr) - vlanSize);
|
||||
// 计算 UDP 数据长度
|
||||
pUdp->len = htons(tolSize - sizeof(struct ethhdr) - vlanSize - sizeof(struct iphdr));
|
||||
pUdp->len = htons(tolSize - sizeof(struct ethhdr) - vlanSize - sizeof(struct iphdr));
|
||||
|
||||
// 计算 IP 校验和
|
||||
csum = htons(ip_checksum((unsigned char *)pIp));
|
||||
csum = htons(ip_checksum((unsigned char *) pIp));
|
||||
pIp->check = htons(csum);
|
||||
|
||||
// 计算 UDP 校验和
|
||||
csum = htons(udp_checksum(pIp->saddr, pIp->daddr, (unsigned char *)pUdp));
|
||||
csum = htons(udp_checksum(pIp->saddr, pIp->daddr, (unsigned char *) pUdp));
|
||||
pUdp->check = htons(csum);
|
||||
|
||||
LOG_MOD(trace, ZM_DHCP_NET, "Total size: %d\n", tolSize);
|
||||
|
||||
// 发送数据
|
||||
if (pkg_mmap_tx((U8 *)pRsp, tolSize) != tolSize) {
|
||||
if (pkg_mmap_tx((U8 *) pRsp, tolSize) != tolSize) {
|
||||
LOG_MOD(error, ZM_DHCP_NET, "Send package(%u bytes) error\n", tolSize);
|
||||
return -ERR_SOCK_SEND;
|
||||
}
|
||||
|
@ -375,38 +375,38 @@ static int dhcp_prepare_tx(U8 *pRsp, U32 optSize) {
|
|||
}
|
||||
|
||||
static int dhcp_resp_nack(void *pReq) {
|
||||
U8 *pOpt;
|
||||
U8 *pOpt;
|
||||
PDHCP_PROTO pDhcp = NULL;
|
||||
U8 *pRsp = get_pkg_free_buf();
|
||||
U8 *pRsp = get_pkg_free_buf();
|
||||
|
||||
if (pRsp == NULL) {
|
||||
LOG_MOD(error, ZM_DHCP_NET, "Malloc memory error: %u\n", MAX_DHCP_PKG_SIZE);
|
||||
return -ERR_MALLOC_MEMORY;
|
||||
}
|
||||
|
||||
pDhcp = fill_pkg(pRsp, (U8 *)pReq);
|
||||
pDhcp = fill_pkg(pRsp, (U8 *) pReq);
|
||||
|
||||
// DHCP Options
|
||||
pOpt = pDhcp->options;
|
||||
|
||||
// DHCP 消息类型
|
||||
pOpt += dhcp_add_u8_option(pOpt, OPT_MESSAGETYPE, DHCP_MSG_NAK);
|
||||
*pOpt = OPT_END;
|
||||
pOpt += dhcp_add_u8_option(pOpt, OPT_MESSAGETYPE, DHCP_MSG_NAK);
|
||||
*pOpt = OPT_END;
|
||||
|
||||
return dhcp_prepare_tx(pRsp, (int)((pOpt - pDhcp->options) + 1));
|
||||
return dhcp_prepare_tx(pRsp, (int) ((pOpt - pDhcp->options) + 1));
|
||||
}
|
||||
|
||||
static int dhcp_resp_ack(void *pReq, PPOOL_CTX pIpInfo) {
|
||||
U8 *pOpt;
|
||||
U8 *pOpt;
|
||||
PDHCP_PROTO pDhcp = NULL;
|
||||
U8 *pRsp = get_pkg_free_buf();
|
||||
U8 *pRsp = get_pkg_free_buf();
|
||||
|
||||
if (pRsp == NULL) {
|
||||
LOG_MOD(error, ZM_DHCP_NET, "Malloc memory error: %u\n", MAX_DHCP_PKG_SIZE);
|
||||
return -ERR_MALLOC_MEMORY;
|
||||
}
|
||||
|
||||
pDhcp = fill_pkg((U8 *)pRsp, (U8 *)pReq);
|
||||
pDhcp = fill_pkg((U8 *) pRsp, (U8 *) pReq);
|
||||
|
||||
// 分配的 IP 地址
|
||||
pDhcp->yiaddr = htonl(pIpInfo->minAddr);
|
||||
|
@ -427,11 +427,11 @@ static int dhcp_resp_ack(void *pReq, PPOOL_CTX pIpInfo) {
|
|||
if (pIpInfo->salveDNS == 0) {
|
||||
pOpt += dhcp_add_u32_option(pOpt, OPT_DNS, htonl(pIpInfo->primeDNS));
|
||||
} else {
|
||||
U8 buf[8] = {0};
|
||||
U32 *pVal = (U32 *)buf;
|
||||
*pVal++ = htonl(pIpInfo->primeDNS);
|
||||
*pVal = htonl(pIpInfo->salveDNS);
|
||||
pOpt += dhcp_add_buf_option(pOpt, OPT_DNS, buf, 8);
|
||||
U8 buf[8] = {0};
|
||||
U32 *pVal = (U32 *) buf;
|
||||
*pVal++ = htonl(pIpInfo->primeDNS);
|
||||
*pVal = htonl(pIpInfo->salveDNS);
|
||||
pOpt += dhcp_add_buf_option(pOpt, OPT_DNS, buf, 8);
|
||||
}
|
||||
}
|
||||
// 网关
|
||||
|
@ -439,23 +439,23 @@ static int dhcp_resp_ack(void *pReq, PPOOL_CTX pIpInfo) {
|
|||
pOpt += dhcp_add_u32_option(pOpt, OPT_ROUTER, htonl(pIpInfo->gwAddr));
|
||||
}
|
||||
// DHCP 主机名
|
||||
pOpt += dhcp_add_string_option(pOpt, OPT_DOMAINNAME, "workgroup");
|
||||
*pOpt = OPT_END;
|
||||
pOpt += dhcp_add_string_option(pOpt, OPT_DOMAINNAME, "workgroup");
|
||||
*pOpt = OPT_END;
|
||||
|
||||
return dhcp_prepare_tx(pRsp, (int)((pOpt - pDhcp->options) + 1));
|
||||
return dhcp_prepare_tx(pRsp, (int) ((pOpt - pDhcp->options) + 1));
|
||||
}
|
||||
|
||||
static int dhcp_resp_offer(void *pReq, PPOOL_CTX pIpInfo, U32 ip) {
|
||||
U8 *pOpt;
|
||||
U8 *pOpt;
|
||||
PDHCP_PROTO pDhcp = NULL;
|
||||
U8 *pRsp = get_pkg_free_buf();
|
||||
U8 *pRsp = get_pkg_free_buf();
|
||||
|
||||
if (pRsp == NULL) {
|
||||
LOG_MOD(error, ZM_DHCP_NET, "Malloc memory error: %u\n", MAX_DHCP_PKG_SIZE);
|
||||
return -ERR_MALLOC_MEMORY;
|
||||
}
|
||||
|
||||
pDhcp = fill_pkg(pRsp, (U8 *)pReq);
|
||||
pDhcp = fill_pkg(pRsp, (U8 *) pReq);
|
||||
|
||||
// 分配的 IP 地址
|
||||
pDhcp->yiaddr = htonl(ip);
|
||||
|
@ -476,11 +476,11 @@ static int dhcp_resp_offer(void *pReq, PPOOL_CTX pIpInfo, U32 ip) {
|
|||
if (pIpInfo->salveDNS == 0) {
|
||||
pOpt += dhcp_add_u32_option(pOpt, OPT_DNS, htonl(pIpInfo->primeDNS));
|
||||
} else {
|
||||
U8 buf[8] = {0};
|
||||
U32 *pVal = (U32 *)buf;
|
||||
*pVal++ = htonl(pIpInfo->primeDNS);
|
||||
*pVal = htonl(pIpInfo->salveDNS);
|
||||
pOpt += dhcp_add_buf_option(pOpt, OPT_DNS, buf, 8);
|
||||
U8 buf[8] = {0};
|
||||
U32 *pVal = (U32 *) buf;
|
||||
*pVal++ = htonl(pIpInfo->primeDNS);
|
||||
*pVal = htonl(pIpInfo->salveDNS);
|
||||
pOpt += dhcp_add_buf_option(pOpt, OPT_DNS, buf, 8);
|
||||
}
|
||||
}
|
||||
// 网关
|
||||
|
@ -488,34 +488,34 @@ static int dhcp_resp_offer(void *pReq, PPOOL_CTX pIpInfo, U32 ip) {
|
|||
pOpt += dhcp_add_u32_option(pOpt, OPT_ROUTER, htonl(pIpInfo->gwAddr));
|
||||
}
|
||||
// DHCP 主机名
|
||||
pOpt += dhcp_add_string_option(pOpt, OPT_DOMAINNAME, "workgroup");
|
||||
*pOpt = OPT_END;
|
||||
pOpt += dhcp_add_string_option(pOpt, OPT_DOMAINNAME, "workgroup");
|
||||
*pOpt = OPT_END;
|
||||
|
||||
return dhcp_prepare_tx(pRsp, (int)((pOpt - pDhcp->options) + 1));
|
||||
return dhcp_prepare_tx(pRsp, (int) ((pOpt - pDhcp->options) + 1));
|
||||
}
|
||||
|
||||
PDHCP_PROTO get_dhcp_date(void *pBuf, U32 *pOptSize, U32 bufSize) {
|
||||
U8 *p = (U8 *)pBuf;
|
||||
U8 *p = (U8 *) pBuf;
|
||||
U32 hdrCom = sizeof(struct ethhdr) + sizeof(struct iphdr) + sizeof(struct udphdr);
|
||||
|
||||
if (p[12] == 0x81 && p[13] == 0x00 && p[16] == 0x81 && p[17] == 0x00) {
|
||||
*pOptSize = bufSize - hdrCom - sizeof(DHCP_PROTO) - sizeof(VLAN_HDR2);
|
||||
return (PDHCP_PROTO)(p + hdrCom + 8);
|
||||
return (PDHCP_PROTO) (p + hdrCom + 8);
|
||||
} else if (p[12] == 0x81 && p[13] == 0x00) {
|
||||
*pOptSize = bufSize - hdrCom - sizeof(DHCP_PROTO) - sizeof(VLAN_HDR);
|
||||
return (PDHCP_PROTO)(p + hdrCom + 4);
|
||||
return (PDHCP_PROTO) (p + hdrCom + 4);
|
||||
} else {
|
||||
*pOptSize = bufSize - hdrCom - sizeof(DHCP_PROTO);
|
||||
return (PDHCP_PROTO)(p + hdrCom);
|
||||
return (PDHCP_PROTO) (p + hdrCom);
|
||||
}
|
||||
}
|
||||
|
||||
U32 dhcp_get_uid(void *pBuf) {
|
||||
U8 *p = (U8 *)pBuf;
|
||||
PVLAN_HDR pQinQ = (PVLAN_HDR)(p + sizeof(struct ethhdr));
|
||||
U8 *p = (U8 *) pBuf;
|
||||
PVLAN_HDR pQinQ = (PVLAN_HDR) (p + sizeof(struct ethhdr));
|
||||
|
||||
if (p[12] == 0x81 && p[13] == 0x00 && p[16] == 0x81 && p[17] == 0x00) {
|
||||
PVLAN_HDR2 pHdr = (PVLAN_HDR2)pQinQ;
|
||||
PVLAN_HDR2 pHdr = (PVLAN_HDR2) pQinQ;
|
||||
return (pHdr->id1 & 0x0FFF) >> 12 | (pHdr->id2 & 0x0FFF) << 12;
|
||||
} else if (p[12] == 0x81 && p[13] == 0x00) {
|
||||
return (pQinQ->id & 0x0FFF);
|
||||
|
@ -525,16 +525,16 @@ U32 dhcp_get_uid(void *pBuf) {
|
|||
}
|
||||
|
||||
static void on_sock_recv(uv_work_t *req) {
|
||||
U32 optSize = 0;
|
||||
char macStr[20] = {0};
|
||||
U32 ip;
|
||||
PPOOL_CTX pIpInfo;
|
||||
DHCP_REQ reqDhcp;
|
||||
DHCP_OPT optMsg, opt;
|
||||
int ret;
|
||||
PPKG_PROCESS_INFO pWork = (PPKG_PROCESS_INFO)req->data;
|
||||
PDHCP_PROTO pkg = get_dhcp_date(pWork->pPkgBase, &optSize, pWork->nSize);
|
||||
struct iphdr *pIpHdr = (struct iphdr *)((U8 *)pkg - sizeof(struct iphdr) - sizeof(struct udphdr));
|
||||
U32 optSize = 0;
|
||||
char macStr[20] = {0};
|
||||
U32 ip;
|
||||
PPOOL_CTX pIpInfo;
|
||||
DHCP_REQ reqDhcp;
|
||||
DHCP_OPT optMsg, opt;
|
||||
int ret;
|
||||
PPKG_PROCESS_INFO pWork = (PPKG_PROCESS_INFO) req->data;
|
||||
PDHCP_PROTO pkg = get_dhcp_date(pWork->pPkgBase, &optSize, pWork->nSize);
|
||||
struct iphdr *pIpHdr = (struct iphdr *) ((U8 *) pkg - sizeof(struct iphdr) - sizeof(struct udphdr));
|
||||
|
||||
//LOG_MSG(info, "Recv, xid: 0x%08X\n", DHCP_XID(pkg->xid));
|
||||
|
||||
|
@ -555,11 +555,11 @@ static void on_sock_recv(uv_work_t *req) {
|
|||
}
|
||||
|
||||
memset(&reqDhcp, 0, sizeof(DHCP_REQ));
|
||||
reqDhcp.unicast = pkg->flags != 0 ? TRUE : FALSE;
|
||||
reqDhcp.xid = DHCP_XID(pkg->xid);
|
||||
reqDhcp.uid = dhcp_get_uid(pWork->pPkgBase);
|
||||
reqDhcp.unicast = pkg->flags != 0 ? TRUE : FALSE;
|
||||
reqDhcp.xid = DHCP_XID(pkg->xid);
|
||||
reqDhcp.uid = dhcp_get_uid(pWork->pPkgBase);
|
||||
reqDhcp.serverAddr = g_nicInfo.ipAddr;
|
||||
reqDhcp.cliAddr = ntohl(pkg->ciaddr);
|
||||
reqDhcp.cliAddr = ntohl(pkg->ciaddr);
|
||||
memcpy(reqDhcp.cliMac, pkg->chaddr, ETH_ALEN);
|
||||
MAC_TO_STR(reqDhcp.cliMac, macStr);
|
||||
|
||||
|
@ -568,32 +568,32 @@ static void on_sock_recv(uv_work_t *req) {
|
|||
// region DHCP Discover 处理
|
||||
ret = dhcp_get_option(OPT_REQUESTEDIPADDR, pkg->options, optSize, &opt);
|
||||
if (ret == ERR_SUCCESS && opt.len == sizeof(U32)) {
|
||||
reqDhcp.reqIpAddr = ntohl(*((U32 *)opt.pValue));
|
||||
reqDhcp.reqIpAddr = ntohl(*((U32 *) opt.pValue));
|
||||
}
|
||||
|
||||
ret = dhcp_get_option(OPT_IPADDRLEASE, pkg->options, optSize, &opt);
|
||||
if (ret == ERR_SUCCESS && opt.len == sizeof(U32)) {
|
||||
reqDhcp.leaseTime = ntohl(*((U32 *)opt.pValue));
|
||||
reqDhcp.leaseTime = ntohl(*((U32 *) opt.pValue));
|
||||
}
|
||||
|
||||
ret = dhcp_get_option(OPT_CLIENTID, pkg->options, optSize, &opt);
|
||||
if (ret == ERR_SUCCESS) {
|
||||
strncpy(reqDhcp.clientId, (char *)opt.pValue, MIN((int)opt.len, 256));
|
||||
strncpy(reqDhcp.clientId, (char *) opt.pValue, MIN((int) opt.len, 256));
|
||||
}
|
||||
|
||||
ret = dhcp_get_option(OPT_VENDORCLASSID, pkg->options, optSize, &opt);
|
||||
if (ret == ERR_SUCCESS) {
|
||||
memcpy(reqDhcp.vendorClassId, (char *)opt.pValue, MIN((int)opt.len, 256));
|
||||
memcpy(reqDhcp.vendorClassId, (char *) opt.pValue, MIN((int) opt.len, 256));
|
||||
|
||||
// IPTV 组播设备不用分配IP地址
|
||||
if (analyze_iptv_multicast_dev((const U8 *)reqDhcp.vendorClassId, opt.len, macStr, reqDhcp.uid)) {
|
||||
if (analyze_iptv_multicast_dev((const U8 *) reqDhcp.vendorClassId, opt.len, macStr, reqDhcp.uid)) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
ret = dhcp_get_option(OPT_HOSTNAME, pkg->options, optSize, &opt);
|
||||
if (ret == ERR_SUCCESS) {
|
||||
strncpy(reqDhcp.hostName, (char *)opt.pValue, MIN((int)opt.len, 256));
|
||||
strncpy(reqDhcp.hostName, (char *) opt.pValue, MIN((int) opt.len, 256));
|
||||
}
|
||||
|
||||
ret = pre_alloc_dhcp_res(&reqDhcp, pWork->pUser, &ip, &pIpInfo);
|
||||
|
@ -624,7 +624,7 @@ static void on_sock_recv(uv_work_t *req) {
|
|||
// 客户端请求的服务器
|
||||
ret = dhcp_get_option(OPT_SERVERID, pkg->options, optSize, &opt);
|
||||
if (ret == ERR_SUCCESS && opt.len == sizeof(U32)) {
|
||||
reqDhcp.reqServer = ntohl(*((U32 *)opt.pValue));
|
||||
reqDhcp.reqServer = ntohl(*((U32 *) opt.pValue));
|
||||
} else {
|
||||
// Request 服务器IP可以取目的 IP 地址
|
||||
if (pIpHdr->saddr != 0) {
|
||||
|
@ -635,7 +635,7 @@ static void on_sock_recv(uv_work_t *req) {
|
|||
// 客户端请求的IP
|
||||
ret = dhcp_get_option(OPT_REQUESTEDIPADDR, pkg->options, optSize, &opt);
|
||||
if (ret == ERR_SUCCESS && opt.len == sizeof(U32)) {
|
||||
reqDhcp.reqIpAddr = ntohl(*((U32 *)opt.pValue));
|
||||
reqDhcp.reqIpAddr = ntohl(*((U32 *) opt.pValue));
|
||||
} else {
|
||||
reqDhcp.reqIpAddr = reqDhcp.cliAddr;
|
||||
}
|
||||
|
@ -643,18 +643,18 @@ static void on_sock_recv(uv_work_t *req) {
|
|||
// 客户端主机名
|
||||
ret = dhcp_get_option(OPT_HOSTNAME, pkg->options, optSize, &opt);
|
||||
if (ret == ERR_SUCCESS) {
|
||||
strncpy(reqDhcp.hostName, (char *)opt.pValue, MIN((int)opt.len, 256));
|
||||
strncpy(reqDhcp.hostName, (char *) opt.pValue, MIN((int) opt.len, 256));
|
||||
}
|
||||
|
||||
// 当改请求的服务端不是本机时,忽略该消息
|
||||
if (reqDhcp.reqServer != ntohl(g_nicInfo.ipAddr) && reqDhcp.reqServer != 0) {
|
||||
const char *pReq = u32_to_str_ip_safe(htonl(reqDhcp.reqServer));
|
||||
const char *pReq = u32_to_str_ip_safe(htonl(reqDhcp.reqServer));
|
||||
const char *pLocal = u32_to_str_ip_safe(g_nicInfo.ipAddr);
|
||||
LOG_MOD_HEX(error, ZM_DHCP_NET, pWork->pPkgBase, pWork->nSize);
|
||||
LOG_MOD(debug, ZM_DHCP_NET, "Receive others server's DHCP request: request need %s, localhost %s\n",
|
||||
pReq, pLocal);
|
||||
free((void *)pReq);
|
||||
free((void *)pLocal);
|
||||
free((void *) pReq);
|
||||
free((void *) pLocal);
|
||||
} else {
|
||||
POOL_CTX ctx = {0};
|
||||
if ((ret = is_pre_assigned(&reqDhcp, &ctx)) == ERR_SUCCESS) {
|
||||
|
@ -680,7 +680,7 @@ static void on_sock_recv(uv_work_t *req) {
|
|||
// 客户端请求的服务器
|
||||
ret = dhcp_get_option(OPT_SERVERID, pkg->options, optSize, &opt);
|
||||
if (ret == ERR_SUCCESS && opt.len == sizeof(U32)) {
|
||||
reqDhcp.reqServer = ntohl(*((U32 *)opt.pValue));
|
||||
reqDhcp.reqServer = ntohl(*((U32 *) opt.pValue));
|
||||
} else {
|
||||
// Request 服务器IP可以取目的 IP 地址
|
||||
if (pIpHdr->saddr != 0) {
|
||||
|
@ -699,7 +699,7 @@ static void on_sock_recv(uv_work_t *req) {
|
|||
// 客户端主机名
|
||||
ret = dhcp_get_option(OPT_HOSTNAME, pkg->options, optSize, &opt);
|
||||
if (ret == ERR_SUCCESS) {
|
||||
strncpy(reqDhcp.hostName, (char *)opt.pValue, MIN((int)opt.len, 256));
|
||||
strncpy(reqDhcp.hostName, (char *) opt.pValue, MIN((int) opt.len, 256));
|
||||
}
|
||||
|
||||
// 当前分配的租约添加到本地数据库中, 后续分配能够发现对应的主机信息
|
||||
|
@ -723,9 +723,9 @@ static void on_sock_recv(uv_work_t *req) {
|
|||
break;
|
||||
}
|
||||
|
||||
//dhcp_option_prase(optMsg, pkg->options, pWork->nSize - sizeof(DHCP_PACKAGE));
|
||||
//dhcp_option_prase(optMsg, pkg->options, pWork->nSize - sizeof(DHCP_PACKAGE));
|
||||
|
||||
//LOG_MSG_HEX(trace, pkg, pWork->nSize);
|
||||
//LOG_MSG_HEX(trace, pkg, pWork->nSize);
|
||||
|
||||
#if 0
|
||||
LOG_MOD(info, ZM_DHCP_NET, "vlan = %u\n", VXLAN_VIN_ID_PACK(pkg->hdr.vlan.id));
|
||||
|
@ -750,8 +750,8 @@ static void on_sock_recv(uv_work_t *req) {
|
|||
}
|
||||
|
||||
static void after_msg_recv(uv_work_t *req, int UNUSED(status)) {
|
||||
PPKG_PROCESS_INFO pInfo = (PPKG_PROCESS_INFO)req->data;
|
||||
PPKG_MSG pMsg = (PPKG_MSG)pInfo->pData;
|
||||
PPKG_PROCESS_INFO pInfo = (PPKG_PROCESS_INFO) req->data;
|
||||
PPKG_MSG pMsg = (PPKG_MSG) pInfo->pData;
|
||||
|
||||
pMsg->nf -= 1;
|
||||
if (pMsg->nf == 0) {
|
||||
|
@ -763,16 +763,16 @@ static void after_msg_recv(uv_work_t *req, int UNUSED(status)) {
|
|||
|
||||
void raw_sock_recv_cb(uv_poll_t *handle, int status, int events) {
|
||||
static unsigned int block_num = 0;
|
||||
PRECV_CB_DATA pCbData = handle->data;
|
||||
PRECV_CB_DATA pCbData = handle->data;
|
||||
|
||||
if (status >= 0 && (events & UV_READABLE)) {
|
||||
struct block_desc *pbd = (struct block_desc *)g_pkgRing.rx[block_num].iov_base;
|
||||
struct block_desc *pbd = (struct block_desc *) g_pkgRing.rx[block_num].iov_base;
|
||||
|
||||
if ((pbd->h1.block_status & TP_STATUS_USER) && (pbd->h1.num_pkts > 0)) {
|
||||
int i;
|
||||
int i;
|
||||
struct tpacket3_hdr *ppd;
|
||||
unsigned int memSize = sizeof(PKG_PROCESS_INFO) * pbd->h1.num_pkts;
|
||||
PPKG_MSG pMsg = (PPKG_MSG)malloc(sizeof(PKG_MSG));
|
||||
unsigned int memSize = sizeof(PKG_PROCESS_INFO) * pbd->h1.num_pkts;
|
||||
PPKG_MSG pMsg = (PPKG_MSG) malloc(sizeof(PKG_MSG));
|
||||
|
||||
if (pMsg == NULL) {
|
||||
LOG_MOD(error, ZM_DHCP_NET, "Malloc memory error: %lu\n", sizeof(PKG_MSG));
|
||||
|
@ -780,7 +780,7 @@ void raw_sock_recv_cb(uv_poll_t *handle, int status, int events) {
|
|||
}
|
||||
|
||||
memset(pMsg, 0, sizeof(PKG_MSG));
|
||||
pMsg->pPkgInfo = (PPKG_PROCESS_INFO)malloc(memSize);
|
||||
pMsg->pPkgInfo = (PPKG_PROCESS_INFO) malloc(memSize);
|
||||
|
||||
if (pMsg->pPkgInfo == NULL) {
|
||||
LOG_MOD(error, ZM_DHCP_NET, "Malloc memory error: %u\n", memSize);
|
||||
|
@ -791,13 +791,13 @@ void raw_sock_recv_cb(uv_poll_t *handle, int status, int events) {
|
|||
memset(pMsg->pPkgInfo, 0, memSize);
|
||||
|
||||
pMsg->nf = pbd->h1.num_pkts;
|
||||
ppd = (struct tpacket3_hdr *)((uint8_t *)pbd + pbd->h1.offset_to_first_pkt);
|
||||
ppd = (struct tpacket3_hdr *) ((uint8_t *) pbd + pbd->h1.offset_to_first_pkt);
|
||||
for (i = 0; i < pbd->h1.num_pkts; i++) {
|
||||
U32 uid;
|
||||
pMsg->pPkgInfo[i].pPkgBase = ((uint8_t *)ppd + ppd->tp_mac);
|
||||
pMsg->pPkgInfo[i].nSize = ppd->tp_snaplen;
|
||||
pMsg->pPkgInfo[i].pPkgBase = ((uint8_t *) ppd + ppd->tp_mac);
|
||||
pMsg->pPkgInfo[i].nSize = ppd->tp_snaplen;
|
||||
pMsg->pPkgInfo[i].uvWork.data = &pMsg->pPkgInfo[i];
|
||||
pMsg->pPkgInfo[i].pData = pMsg;
|
||||
pMsg->pPkgInfo[i].pData = pMsg;
|
||||
if (g_dhcpMode == MODE_DHCP_SERVER) {
|
||||
uid = dhcp_get_uid(pMsg->pPkgInfo[i].pPkgBase);
|
||||
|
||||
|
@ -814,21 +814,23 @@ void raw_sock_recv_cb(uv_poll_t *handle, int status, int events) {
|
|||
uv_queue_work(get_task_manager(), &(pMsg->pPkgInfo[i].uvWork), on_sock_recv, after_msg_recv);
|
||||
}
|
||||
|
||||
ppd = (struct tpacket3_hdr *)((uint8_t *)ppd + ppd->tp_next_offset);
|
||||
ppd = (struct tpacket3_hdr *) ((uint8_t *) ppd + ppd->tp_next_offset);
|
||||
}
|
||||
}
|
||||
|
||||
pbd->h1.block_status = TP_STATUS_KERNEL;
|
||||
block_num = (block_num + 1) % PKG_MMAP_BLOCKNUM;
|
||||
block_num = (block_num + 1) % PKG_MMAP_BLOCKNUM;
|
||||
}
|
||||
}
|
||||
|
||||
int create_udp_raw_socket(const char *pNicName) {
|
||||
struct sockaddr_ll addr;
|
||||
unsigned int size;
|
||||
int i;
|
||||
int err;
|
||||
int v = TPACKET_V3;
|
||||
unsigned int size;
|
||||
int i;
|
||||
int err;
|
||||
int v = TPACKET_V3;
|
||||
|
||||
LOG_MOD(info, ZM_DHCP_NET, "DHCP working on interface %s\n", pNicName);
|
||||
|
||||
// 1. create socket
|
||||
int sock_fd = socket(PF_PACKET, SOCK_RAW, 0);
|
||||
|
@ -851,11 +853,11 @@ int create_udp_raw_socket(const char *pNicName) {
|
|||
|
||||
// 4. setup PAKCET_MMAP rx ring
|
||||
memset(&g_pkgRing.recv, 0, sizeof(g_pkgRing.recv));
|
||||
g_pkgRing.recv.tp_block_size = PKG_MMAP_BLOCKSIZ;
|
||||
g_pkgRing.recv.tp_frame_size = PKG_MMAP_FRAMESIZ;
|
||||
g_pkgRing.recv.tp_block_nr = PKG_MMAP_BLOCKNUM;
|
||||
g_pkgRing.recv.tp_frame_nr = (PKG_MMAP_BLOCKSIZ * PKG_MMAP_BLOCKNUM) / PKG_MMAP_FRAMESIZ;
|
||||
g_pkgRing.recv.tp_retire_blk_tov = 60;
|
||||
g_pkgRing.recv.tp_block_size = PKG_MMAP_BLOCKSIZ;
|
||||
g_pkgRing.recv.tp_frame_size = PKG_MMAP_FRAMESIZ;
|
||||
g_pkgRing.recv.tp_block_nr = PKG_MMAP_BLOCKNUM;
|
||||
g_pkgRing.recv.tp_frame_nr = (PKG_MMAP_BLOCKSIZ * PKG_MMAP_BLOCKNUM) / PKG_MMAP_FRAMESIZ;
|
||||
g_pkgRing.recv.tp_retire_blk_tov = 60;
|
||||
g_pkgRing.recv.tp_feature_req_word = TP_FT_REQ_FILL_RXHASH;
|
||||
|
||||
if ((err = setsockopt(sock_fd, SOL_PACKET, PACKET_RX_RING, &g_pkgRing.recv, sizeof(g_pkgRing.recv))) < 0) {
|
||||
|
@ -867,8 +869,8 @@ int create_udp_raw_socket(const char *pNicName) {
|
|||
memset(&g_pkgRing.send, 0, sizeof(g_pkgRing.send));
|
||||
g_pkgRing.send.tp_block_size = PKG_MMAP_BLOCKSIZ;
|
||||
g_pkgRing.send.tp_frame_size = PKG_MMAP_FRAMESIZ;
|
||||
g_pkgRing.send.tp_block_nr = PKG_MMAP_BLOCKNUM;
|
||||
g_pkgRing.send.tp_frame_nr = (PKG_MMAP_BLOCKSIZ * PKG_MMAP_BLOCKNUM) / PKG_MMAP_FRAMESIZ;
|
||||
g_pkgRing.send.tp_block_nr = PKG_MMAP_BLOCKNUM;
|
||||
g_pkgRing.send.tp_frame_nr = (PKG_MMAP_BLOCKSIZ * PKG_MMAP_BLOCKNUM) / PKG_MMAP_FRAMESIZ;
|
||||
|
||||
if ((err = setsockopt(sock_fd, SOL_PACKET, PACKET_TX_RING, &g_pkgRing.send, sizeof(g_pkgRing.recv))) < 0) {
|
||||
LOG_MOD(error, ZM_DHCP_NET, "Set PACKET_TX_RING option failed: %d\n", err);
|
||||
|
@ -877,7 +879,7 @@ int create_udp_raw_socket(const char *pNicName) {
|
|||
|
||||
// 6. mmap RX/TX ring
|
||||
size = g_pkgRing.recv.tp_block_size * g_pkgRing.recv.tp_block_nr +
|
||||
g_pkgRing.send.tp_block_size * g_pkgRing.send.tp_block_nr;
|
||||
g_pkgRing.send.tp_block_size * g_pkgRing.send.tp_block_nr;
|
||||
|
||||
g_pkgRing.map_recv = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_LOCKED | MAP_POPULATE, sock_fd, 0);
|
||||
g_pkgRing.map_send = g_pkgRing.map_recv + g_pkgRing.recv.tp_block_size * g_pkgRing.recv.tp_block_nr;
|
||||
|
@ -901,7 +903,7 @@ int create_udp_raw_socket(const char *pNicName) {
|
|||
memset(g_pkgRing.rx, 0, g_pkgRing.recv.tp_block_nr * sizeof(struct iovec));
|
||||
for (i = 0; i < g_pkgRing.recv.tp_block_nr; ++i) {
|
||||
g_pkgRing.rx[i].iov_base = g_pkgRing.map_recv + (i * g_pkgRing.recv.tp_block_size);
|
||||
g_pkgRing.rx[i].iov_len = g_pkgRing.recv.tp_block_size;
|
||||
g_pkgRing.rx[i].iov_len = g_pkgRing.recv.tp_block_size;
|
||||
}
|
||||
|
||||
// 8. malloc send buffer
|
||||
|
@ -916,19 +918,19 @@ int create_udp_raw_socket(const char *pNicName) {
|
|||
memset(g_pkgRing.tx, 0, g_pkgRing.send.tp_block_nr * sizeof(struct iovec));
|
||||
for (i = 0; i < g_pkgRing.send.tp_block_nr; ++i) {
|
||||
g_pkgRing.tx[i].iov_base = g_pkgRing.map_send + (i * g_pkgRing.send.tp_block_size);
|
||||
g_pkgRing.tx[i].iov_len = g_pkgRing.send.tp_block_size;
|
||||
g_pkgRing.tx[i].iov_len = g_pkgRing.send.tp_block_size;
|
||||
}
|
||||
|
||||
// 9. bind socket
|
||||
memset(&addr, 0, sizeof(struct sockaddr_ll));
|
||||
addr.sll_ifindex = (int)if_nametoindex(pNicName);
|
||||
addr.sll_family = PF_PACKET;
|
||||
addr.sll_ifindex = (int) if_nametoindex(pNicName);
|
||||
addr.sll_family = PF_PACKET;
|
||||
addr.sll_protocol = htons(ETH_P_ALL);
|
||||
addr.sll_hatype = 0;
|
||||
addr.sll_pkttype = 0;
|
||||
addr.sll_halen = 0;
|
||||
addr.sll_hatype = 0;
|
||||
addr.sll_pkttype = 0;
|
||||
addr.sll_halen = 0;
|
||||
|
||||
if ((err = bind(sock_fd, (struct sockaddr *)&addr, sizeof(struct sockaddr_ll))) < 0) {
|
||||
if ((err = bind(sock_fd, (struct sockaddr *) &addr, sizeof(struct sockaddr_ll))) < 0) {
|
||||
LOG_MOD(error, ZM_DHCP_NET, "Bind raw socket failed: %d\n", err);
|
||||
return -ERR_SOCK_SETOPT;
|
||||
}
|
||||
|
@ -939,11 +941,11 @@ int create_udp_raw_socket(const char *pNicName) {
|
|||
}
|
||||
|
||||
void socket_send_task(uv_timer_t *UNUSED(pArg)) {
|
||||
int i;
|
||||
int i;
|
||||
struct tpacket3_hdr *hdr;
|
||||
|
||||
for (i = 0; i < g_pkgRing.send.tp_frame_nr && g_pkgRing.sock != 0; i++) {
|
||||
hdr = (struct tpacket3_hdr *)(g_pkgRing.tx[0].iov_base + (g_pkgRing.send.tp_frame_size * i));
|
||||
hdr = (struct tpacket3_hdr *) (g_pkgRing.tx[0].iov_base + (g_pkgRing.send.tp_frame_size * i));
|
||||
if ((hdr->tp_status & TP_STATUS_SEND_REQUEST) || (hdr->tp_status & TP_STATUS_SENDING)) {
|
||||
ssize_t ret = sendto(g_pkgRing.sock, NULL, 0, MSG_DONTWAIT, NULL, sizeof(struct sockaddr_ll));
|
||||
|
||||
|
@ -966,16 +968,16 @@ void init_filter(const char *pNetFilter) {
|
|||
|
||||
pcap_compile(pd, &fcode, pNetFilter, 1, 0);
|
||||
|
||||
struct bpf_insn *insn = fcode.bf_insns;
|
||||
struct sock_filter *g_filters = (struct sock_filter *)malloc(fcode.bf_len * sizeof(struct sock_filter));
|
||||
struct bpf_insn *insn = fcode.bf_insns;
|
||||
struct sock_filter *g_filters = (struct sock_filter *) malloc(fcode.bf_len * sizeof(struct sock_filter));
|
||||
for (int i = 0; i < fcode.bf_len; ++insn, ++i) {
|
||||
g_filters[i].code = insn->code;
|
||||
g_filters[i].jt = insn->jt;
|
||||
g_filters[i].jf = insn->jf;
|
||||
g_filters[i].k = insn->k;
|
||||
g_filters[i].jt = insn->jt;
|
||||
g_filters[i].jf = insn->jf;
|
||||
g_filters[i].k = insn->k;
|
||||
}
|
||||
|
||||
bpf.len = fcode.bf_len;
|
||||
bpf.len = fcode.bf_len;
|
||||
bpf.filter = g_filters;
|
||||
|
||||
pcap_close(pd);
|
||||
|
@ -985,16 +987,16 @@ void init_filter(const char *pNetFilter) {
|
|||
|
||||
void init_raw_socket_poll(void *pRecv, void *pClean) {
|
||||
static RECV_CB_DATA rcData;
|
||||
static uv_poll_t uvSocket;
|
||||
static uv_timer_t uvTm;
|
||||
static uv_poll_t uvSocket;
|
||||
static uv_timer_t uvTm;
|
||||
|
||||
uv_udp_init(get_task_manager(), &g_uvRawSockReq);
|
||||
uv_udp_open(&g_uvRawSockReq, g_pkgRing.sock);
|
||||
|
||||
uv_poll_init_socket(get_task_manager(), &uvSocket, g_pkgRing.sock);
|
||||
rcData.work_cb = pRecv;
|
||||
rcData.work_cb = pRecv;
|
||||
rcData.after_work_cb = pClean;
|
||||
uvSocket.data = &rcData;
|
||||
uvSocket.data = &rcData;
|
||||
|
||||
uv_poll_start(&uvSocket, UV_READABLE, raw_sock_recv_cb);
|
||||
|
||||
|
@ -1002,14 +1004,19 @@ void init_raw_socket_poll(void *pRecv, void *pClean) {
|
|||
uv_timer_start(&uvTm, socket_send_task, 3000, 100);
|
||||
}
|
||||
|
||||
int dhcpd_init(DHCP_WORK_MODE workMode) {
|
||||
int ret;
|
||||
int dhcpd_init(DHCP_WORK_MODE workMode, const char *pIfName) {
|
||||
int ret;
|
||||
size_t size = MAX_PATH;
|
||||
|
||||
g_dhcpMode = workMode;
|
||||
|
||||
memset(&g_nicInfo, 0, sizeof(NIC_INFO));
|
||||
g_nicInfo.pIfName = (char *)config_get_dhcp_nic_name();
|
||||
if (pIfName && strlen(pIfName) > 0) {
|
||||
g_nicInfo.pIfName = strdup(pIfName);
|
||||
} else {
|
||||
g_nicInfo.pIfName = strdup((char *) config_get_dhcp_nic_name());
|
||||
}
|
||||
|
||||
uv_os_gethostname(g_nicInfo.hostname, &size);
|
||||
|
||||
get_nic_info(g_nicInfo.pIfName, &g_nicInfo.ipAddr, &g_nicInfo.netmask, NULL, g_nicInfo.macAddr);
|
||||
|
@ -1034,7 +1041,7 @@ int dhcpd_init(DHCP_WORK_MODE workMode) {
|
|||
|
||||
int dhcp_uninit() {
|
||||
unsigned int size = g_pkgRing.recv.tp_block_size * g_pkgRing.recv.tp_block_nr +
|
||||
g_pkgRing.send.tp_block_size * g_pkgRing.send.tp_block_nr;
|
||||
g_pkgRing.send.tp_block_size * g_pkgRing.send.tp_block_nr;
|
||||
close(g_pkgRing.sock);
|
||||
g_pkgRing.sock = -1;
|
||||
if (g_pkgRing.tx) {
|
||||
|
@ -1047,5 +1054,9 @@ int dhcp_uninit() {
|
|||
|
||||
munmap(g_pkgRing.map_recv, size);
|
||||
|
||||
if (g_nicInfo.pIfName) {
|
||||
free(g_nicInfo.pIfName);
|
||||
}
|
||||
|
||||
return ERR_SUCCESS;
|
||||
}
|
Loading…
Reference in New Issue