OCT 1. 修正DHCP测试工具xid不正确问题

2. 修正DHCP服务器分配的IP地址不正确问题
This commit is contained in:
huangxin 2023-04-21 16:04:43 +08:00
parent 68a5d8df80
commit 1ebb14845f
6 changed files with 18 additions and 108 deletions

View File

@ -380,21 +380,8 @@ G_MODULE_EXPORT void __mainWnd_on_tb_start(GObject *object, gpointer user_data)
G_MODULE_EXPORT void __mainWnd_on_tb_stop(GObject *object, gpointer user_data) { G_MODULE_EXPORT void __mainWnd_on_tb_stop(GObject *object, gpointer user_data) {
GtkWidget *startButton = GTK_WIDGET(gtk_builder_get_object(g_mainBuilder, "tbStart")); GtkWidget *startButton = GTK_WIDGET(gtk_builder_get_object(g_mainBuilder, "tbStart"));
GtkWidget *nicSelect = GTK_WIDGET(gtk_builder_get_object(g_mainBuilder, "cbNicName")); GtkWidget *nicSelect = GTK_WIDGET(gtk_builder_get_object(g_mainBuilder, "cbNicName"));
// PDHCP_INFO pInfo;
// GRand *pRnd = g_rand_new_with_seed(time(NULL));
// unsigned int val = g_rand_int(pRnd) % 100;
//
// HASH_FIND_INT(g_pDhcpInfo, &val, pInfo);
//
// if (pInfo) {
// tree_view_data_store_upgade(pInfo);
// printf("Upgrade treeview row %u\n", val);
// }
printf("__mainWnd_on_tb_stop\n");
g_runTask = FALSE; g_runTask = FALSE;
//g_thread_join(g_pDHCPSTMThread);
gtk_widget_set_sensitive(GTK_WIDGET(object), FALSE); gtk_widget_set_sensitive(GTK_WIDGET(object), FALSE);
gtk_widget_set_sensitive(startButton, TRUE); gtk_widget_set_sensitive(startButton, TRUE);
gtk_widget_set_sensitive(nicSelect, TRUE); gtk_widget_set_sensitive(nicSelect, TRUE);
@ -480,7 +467,6 @@ int main(int args, char **argv) {
#endif #endif
gtk_builder_connect_signals(g_mainBuilder, NULL); gtk_builder_connect_signals(g_mainBuilder, NULL);
//g_object_unref(G_OBJECT(g_mainBuilder));
gtk_widget_show(mainWnd); gtk_widget_show(mainWnd);
g_pEvLoopThread = g_thread_new("uv_loop", uv_loop_thread, NULL); g_pEvLoopThread = g_thread_new("uv_loop", uv_loop_thread, NULL);

View File

@ -15,86 +15,6 @@
static U8 g_dhcpReqParams[] = {0x01, 0x1c, 0x02, 0x03, 0x0f, 0x06, 0x77, 0x0c, 0x2c, 0x2f, 0x1a, 0x79, 0x2a}; static U8 g_dhcpReqParams[] = {0x01, 0x1c, 0x02, 0x03, 0x0f, 0x06, 0x77, 0x0c, 0x2c, 0x2f, 0x1a, 0x79, 0x2a};
static char *g_pNicName = NULL; static char *g_pNicName = NULL;
static struct sock_filter g_filterCode[] = {
// region BPF code
// create by: tcpdump "vlan and udp and port 67 and port 68" -dd
{0x0, 0, 0, 0x00000000},
{0x2, 0, 0, 0x00000000},
{0x2, 0, 0, 0x00000001},
{0x30, 0, 0, 0xfffff030},
{0x15, 7, 0, 0x00000001},
{0x0, 0, 0, 0x00000004},
{0x2, 0, 0, 0x00000000},
{0x2, 0, 0, 0x00000001},
{0x28, 0, 0, 0x0000000c},
{0x15, 2, 0, 0x00008100},
{0x15, 1, 0, 0x000088a8},
{0x15, 0, 56, 0x00009100},
{0x61, 0, 0, 0x00000001},
{0x48, 0, 0, 0x0000000c},
{0x15, 0, 13, 0x000086dd},
{0x61, 0, 0, 0x00000000},
{0x50, 0, 0, 0x00000014},
{0x15, 0, 50, 0x00000011},
{0x61, 0, 0, 0x00000000},
{0x48, 0, 0, 0x00000036},
{0x15, 0, 3, 0x00000043},
{0x61, 0, 0, 0x00000000},
{0x48, 0, 0, 0x00000038},
{0x15, 43, 44, 0x00000044},
{0x15, 0, 43, 0x00000044},
{0x61, 0, 0, 0x00000000},
{0x48, 0, 0, 0x00000038},
{0x15, 39, 40, 0x00000043},
{0x15, 0, 39, 0x00000800},
{0x61, 0, 0, 0x00000000},
{0x50, 0, 0, 0x00000017},
{0x15, 0, 36, 0x00000011},
{0x61, 0, 0, 0x00000000},
{0x48, 0, 0, 0x00000014},
{0x45, 33, 0, 0x00001fff},
{0x61, 0, 0, 0x00000000},
{0x50, 0, 0, 0x0000000e},
{0x54, 0, 0, 0x0000000f},
{0x64, 0, 0, 0x00000002},
{0xc, 0, 0, 0x00000000},
{0x7, 0, 0, 0x00000000},
{0x48, 0, 0, 0x0000000e},
{0x15, 0, 8, 0x00000043},
{0x61, 0, 0, 0x00000000},
{0x50, 0, 0, 0x0000000e},
{0x54, 0, 0, 0x0000000f},
{0x64, 0, 0, 0x00000002},
{0xc, 0, 0, 0x00000000},
{0x7, 0, 0, 0x00000000},
{0x48, 0, 0, 0x00000010},
{0x15, 16, 17, 0x00000044},
{0x61, 0, 0, 0x00000000},
{0x50, 0, 0, 0x0000000e},
{0x54, 0, 0, 0x0000000f},
{0x64, 0, 0, 0x00000002},
{0xc, 0, 0, 0x00000000},
{0x7, 0, 0, 0x00000000},
{0x48, 0, 0, 0x0000000e},
{0x15, 0, 9, 0x00000044},
{0x61, 0, 0, 0x00000000},
{0x50, 0, 0, 0x0000000e},
{0x54, 0, 0, 0x0000000f},
{0x64, 0, 0, 0x00000002},
{0xc, 0, 0, 0x00000000},
{0x7, 0, 0, 0x00000000},
{0x48, 0, 0, 0x00000010},
{0x15, 0, 1, 0x00000043},
{0x6, 0, 0, 0x00040000},
{0x6, 0, 0, 0x00000000},
// endregion
};
static struct sock_fprog bpf = {
.len = sizeof(g_filterCode) / (sizeof(struct sock_filter)),
.filter = g_filterCode,
};
static void pkg_init_head(PDHCP_PACKAGE p, PDHCP_INFO pInfo) { static void pkg_init_head(PDHCP_PACKAGE p, PDHCP_INFO pInfo) {
// 目的地 MAC 地址 // 目的地 MAC 地址
memset(p->vlan_hdr.eth.h_dest, 0xFF, ETH_ALEN); memset(p->vlan_hdr.eth.h_dest, 0xFF, ETH_ALEN);
@ -131,7 +51,7 @@ static void pkg_init_head(PDHCP_PACKAGE p, PDHCP_INFO pInfo) {
p->dhcp.htype = 0x01; p->dhcp.htype = 0x01;
p->dhcp.hlen = ETH_ALEN; p->dhcp.hlen = ETH_ALEN;
p->dhcp.hops = 0; p->dhcp.hops = 0;
p->dhcp.xid = htonl((rand_number() & 0xFF000000) + pInfo->index); p->dhcp.xid = 0;
p->dhcp.secs = 0; p->dhcp.secs = 0;
p->dhcp.flags = 0; p->dhcp.flags = 0;
p->dhcp.ciaddr = INADDR_ANY; p->dhcp.ciaddr = INADDR_ANY;
@ -169,6 +89,8 @@ U8 *dhcp_create_request_req(PDHCP_INFO pInfo, int *pOutSize) {
pkg_init_head(p, pInfo); pkg_init_head(p, pInfo);
p->dhcp.xid = htonl(pInfo->offerRsp.xid);
// DHCP Options // DHCP Options
pOpt = p->dhcp.options; pOpt = p->dhcp.options;
@ -206,6 +128,7 @@ U8 *dhcp_create_discover_req(PDHCP_INFO pInfo, int *pOutSize) {
memset(pReqData, 0, MAX_DHCP_PKG_SIZE); memset(pReqData, 0, MAX_DHCP_PKG_SIZE);
pkg_init_head(p, pInfo); pkg_init_head(p, pInfo);
p->dhcp.xid = htonl((rand_number() & 0xFF000000) + pInfo->index);
// DHCP Options // DHCP Options
pOpt = p->dhcp.options; pOpt = p->dhcp.options;
@ -286,21 +209,16 @@ static void on_dhcp_recv(uv_work_t *req) {
ret = dhcp_get_option(OPT_ROUTER, pkg->dhcp.options, optSize, &opt); ret = dhcp_get_option(OPT_ROUTER, pkg->dhcp.options, optSize, &opt);
if (ret == ERR_SUCCESS) { if (ret == ERR_SUCCESS) {
rspDhcp.route = ntohl(*((U32 *)opt.pValue)); rspDhcp.route = ntohl(*((U32 *)opt.pValue));
DEBUG_CODE_LINE();
} }
ret = dhcp_get_option(OPT_IPADDRLEASE, pkg->dhcp.options, optSize, &opt); ret = dhcp_get_option(OPT_IPADDRLEASE, pkg->dhcp.options, optSize, &opt);
if (ret == ERR_SUCCESS) { if (ret == ERR_SUCCESS) {
rspDhcp.leaseTime = ntohl(*((U32 *)opt.pValue)); rspDhcp.leaseTime = ntohl(*((U32 *)opt.pValue));
DEBUG_CODE_LINE();
} }
ret = dhcp_get_option(OPT_SERVERID, pkg->dhcp.options, optSize, &opt); ret = dhcp_get_option(OPT_SERVERID, pkg->dhcp.options, optSize, &opt);
if (ret == ERR_SUCCESS) { if (ret == ERR_SUCCESS) {
rspDhcp.svrIp = ntohl(*((U32 *)opt.pValue)); rspDhcp.svrIp = ntohl(*((U32 *)opt.pValue));
DEBUG_CODE_LINE();
} else {
printf("++++++++++++++++++++++++ret = %d\n", ret);
} }
ret = dhcp_get_option(OPT_DOMAINNAME, pkg->dhcp.options, optSize, &opt); ret = dhcp_get_option(OPT_DOMAINNAME, pkg->dhcp.options, optSize, &opt);
@ -312,8 +230,6 @@ static void on_dhcp_recv(uv_work_t *req) {
pInfo->step = STEP_OFFER; pInfo->step = STEP_OFFER;
pInfo->status = STA_RECV_RSP; pInfo->status = STA_RECV_RSP;
printf("rspIp = %08X, svrIp = %08X\n", rspDhcp.ipAddr, rspDhcp.svrIp);
cacheDhcpOfferBuffer(pInfo, pWork->pPkgBase, pWork->nSize); cacheDhcpOfferBuffer(pInfo, pWork->pPkgBase, pWork->nSize);
memcpy(&pInfo->offerRsp, &rspDhcp, sizeof(DHCP_RSP)); memcpy(&pInfo->offerRsp, &rspDhcp, sizeof(DHCP_RSP));
break; break;
@ -340,7 +256,7 @@ int dhcp_tools_init_network(const char *pNicName) {
return ERR_SUCCESS; return ERR_SUCCESS;
} }
init_filter("vlan and udp and port 67 and port 68"); init_filter("vlan and udp and dst port 68");
ret = create_udp_raw_socket(g_pNicName); ret = create_udp_raw_socket(g_pNicName);
if (ret != ERR_SUCCESS) { if (ret != ERR_SUCCESS) {

View File

@ -41,7 +41,7 @@ extern "C" {
typeof(a) __max1__ = (a); \ typeof(a) __max1__ = (a); \
typeof(b) __max2__ = (b); \ typeof(b) __max2__ = (b); \
(void)(&__max1__ == &__max2__); \ (void)(&__max1__ == &__max2__); \
__max1__ < __max2__ ? __max1__ : __max2__; \ __max1__ > __max2__ ? __max1__ : __max2__; \
}) })
#endif #endif

View File

@ -61,7 +61,7 @@ static const DHCP_OPTION_CFG g_opCfg[] = {
{"AddressTime", OPT_IPADDRLEASE, 4, TRUE }, {"AddressTime", OPT_IPADDRLEASE, 4, TRUE },
{"OverLoad", OPT_OVERLOAD, 7, FALSE}, {"OverLoad", OPT_OVERLOAD, 7, FALSE},
{"DHCPMsgType", OPT_MESSAGETYPE, 6, TRUE }, {"DHCPMsgType", OPT_MESSAGETYPE, 6, TRUE },
{"DHCPServerId", OPT_SERVERID, 3, FALSE}, {"DHCPServerId", OPT_SERVERID, 3, TRUE },
{"ParameterList", OPT_PARAMREQLIST, 2, TRUE }, {"ParameterList", OPT_PARAMREQLIST, 2, TRUE },
{"DHCPMessage", OPT_MESSAGE, 1, FALSE}, {"DHCPMessage", OPT_MESSAGE, 1, FALSE},
{"DHCPMaxMsgSize", OPT_MAXDHCPMSGSIZE, 5, FALSE}, {"DHCPMaxMsgSize", OPT_MAXDHCPMSGSIZE, 5, FALSE},

View File

@ -112,6 +112,8 @@ int pre_alloc_dhcp_res(U32 uid, const char *pMac, U32 *pOutIp, PIPPOOL_INFO *pOu
*pOutPool = pPool; *pOutPool = pPool;
bitset_cls_bit(pPool->assignPool, addr); bitset_cls_bit(pPool->assignPool, addr);
LOG_MOD(trace, ZLOG_MOD_DHCPD, "Select ipaddr %08X at %d of %p\n", ipAddr, addr, pPool->assignPool);
return ERR_SUCCESS; return ERR_SUCCESS;
} else { } else {
if (time(NULL) - pNewIp->timeStamp < PREALLOC_IP_TIMEOUT) { if (time(NULL) - pNewIp->timeStamp < PREALLOC_IP_TIMEOUT) {
@ -121,6 +123,13 @@ int pre_alloc_dhcp_res(U32 uid, const char *pMac, U32 *pOutIp, PIPPOOL_INFO *pOu
*pOutIp = pNewIp->ipAddr; *pOutIp = pNewIp->ipAddr;
*pOutPool = pPool; *pOutPool = pPool;
LOG_MOD(trace,
ZLOG_MOD_DHCPD,
"Used prepard ipaddr %08X at %d of %p\n",
ipAddr,
addr,
pPool->assignPool);
return ERR_SUCCESS; return ERR_SUCCESS;
} }
} }

View File

@ -243,12 +243,11 @@ int user_add_ip_pool(U32 uId, PIPPOOL_INFO pPool) {
} }
// 计算可用的IP // 计算可用的IP
begin = ntohl(pPoolCfg->minAddr) & ntohl(~pPoolCfg->netMask); begin = pPoolCfg->minAddr & (~pPoolCfg->netMask);
end = ntohl(pPoolCfg->maxAddr) & ntohl(~pPoolCfg->netMask); end = pPoolCfg->maxAddr & (~pPoolCfg->netMask);
for (k = MAX(begin, 1U); k <= MIN(end, ntohl(~pPoolCfg->netMask) - 1); k++) { for (k = MAX(begin, 1U); k <= MIN(end, ntohl(~pPoolCfg->netMask) - 1); k++) {
bitset_set(pPoolCfg->assignPool, k); bitset_set(pPoolCfg->assignPool, k);
} }
return ERR_SUCCESS; return ERR_SUCCESS;
} }