From 1ebb14845f3b5390d11e11f9955e1650ffeb4b00 Mon Sep 17 00:00:00 2001 From: huangxin Date: Fri, 21 Apr 2023 16:04:43 +0800 Subject: [PATCH] =?UTF-8?q?OCT=201.=20=E4=BF=AE=E6=AD=A3DHCP=E6=B5=8B?= =?UTF-8?q?=E8=AF=95=E5=B7=A5=E5=85=B7xid=E4=B8=8D=E6=AD=A3=E7=A1=AE?= =?UTF-8?q?=E9=97=AE=E9=A2=98=202.=20=E4=BF=AE=E6=AD=A3DHCP=E6=9C=8D?= =?UTF-8?q?=E5=8A=A1=E5=99=A8=E5=88=86=E9=85=8D=E7=9A=84IP=E5=9C=B0?= =?UTF-8?q?=E5=9D=80=E4=B8=8D=E6=AD=A3=E7=A1=AE=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- dhcp_tools/main_wnd.c | 14 ----- dhcp_tools/uv_rawsocket.c | 94 ++------------------------------ srcs/libs/include/common.h | 2 +- srcs/service/dhcpd/dhcp_option.c | 2 +- srcs/service/dhcpd/lease.c | 9 +++ srcs/service/dhcpd/user_mgr.c | 5 +- 6 files changed, 18 insertions(+), 108 deletions(-) diff --git a/dhcp_tools/main_wnd.c b/dhcp_tools/main_wnd.c index 6be786b..d64d57f 100644 --- a/dhcp_tools/main_wnd.c +++ b/dhcp_tools/main_wnd.c @@ -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) { GtkWidget *startButton = GTK_WIDGET(gtk_builder_get_object(g_mainBuilder, "tbStart")); 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_thread_join(g_pDHCPSTMThread); - gtk_widget_set_sensitive(GTK_WIDGET(object), FALSE); gtk_widget_set_sensitive(startButton, TRUE); gtk_widget_set_sensitive(nicSelect, TRUE); @@ -480,7 +467,6 @@ int main(int args, char **argv) { #endif gtk_builder_connect_signals(g_mainBuilder, NULL); - //g_object_unref(G_OBJECT(g_mainBuilder)); gtk_widget_show(mainWnd); g_pEvLoopThread = g_thread_new("uv_loop", uv_loop_thread, NULL); diff --git a/dhcp_tools/uv_rawsocket.c b/dhcp_tools/uv_rawsocket.c index 5c0d616..1d65486 100644 --- a/dhcp_tools/uv_rawsocket.c +++ b/dhcp_tools/uv_rawsocket.c @@ -15,86 +15,6 @@ static U8 g_dhcpReqParams[] = {0x01, 0x1c, 0x02, 0x03, 0x0f, 0x06, 0x77, 0x0c, 0x2c, 0x2f, 0x1a, 0x79, 0x2a}; 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) { // 目的地 MAC 地址 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.hlen = ETH_ALEN; p->dhcp.hops = 0; - p->dhcp.xid = htonl((rand_number() & 0xFF000000) + pInfo->index); + p->dhcp.xid = 0; p->dhcp.secs = 0; p->dhcp.flags = 0; p->dhcp.ciaddr = INADDR_ANY; @@ -169,6 +89,8 @@ U8 *dhcp_create_request_req(PDHCP_INFO pInfo, int *pOutSize) { pkg_init_head(p, pInfo); + p->dhcp.xid = htonl(pInfo->offerRsp.xid); + // 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); pkg_init_head(p, pInfo); + p->dhcp.xid = htonl((rand_number() & 0xFF000000) + pInfo->index); // 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); if (ret == ERR_SUCCESS) { rspDhcp.route = ntohl(*((U32 *)opt.pValue)); - DEBUG_CODE_LINE(); } ret = dhcp_get_option(OPT_IPADDRLEASE, pkg->dhcp.options, optSize, &opt); if (ret == ERR_SUCCESS) { rspDhcp.leaseTime = ntohl(*((U32 *)opt.pValue)); - DEBUG_CODE_LINE(); } ret = dhcp_get_option(OPT_SERVERID, pkg->dhcp.options, optSize, &opt); if (ret == ERR_SUCCESS) { 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); @@ -312,8 +230,6 @@ static void on_dhcp_recv(uv_work_t *req) { pInfo->step = STEP_OFFER; pInfo->status = STA_RECV_RSP; - printf("rspIp = %08X, svrIp = %08X\n", rspDhcp.ipAddr, rspDhcp.svrIp); - cacheDhcpOfferBuffer(pInfo, pWork->pPkgBase, pWork->nSize); memcpy(&pInfo->offerRsp, &rspDhcp, sizeof(DHCP_RSP)); break; @@ -340,7 +256,7 @@ int dhcp_tools_init_network(const char *pNicName) { 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); if (ret != ERR_SUCCESS) { diff --git a/srcs/libs/include/common.h b/srcs/libs/include/common.h index e52ee57..00b8221 100644 --- a/srcs/libs/include/common.h +++ b/srcs/libs/include/common.h @@ -41,7 +41,7 @@ extern "C" { typeof(a) __max1__ = (a); \ typeof(b) __max2__ = (b); \ (void)(&__max1__ == &__max2__); \ - __max1__ < __max2__ ? __max1__ : __max2__; \ + __max1__ > __max2__ ? __max1__ : __max2__; \ }) #endif diff --git a/srcs/service/dhcpd/dhcp_option.c b/srcs/service/dhcpd/dhcp_option.c index bd60423..c3ebf7e 100644 --- a/srcs/service/dhcpd/dhcp_option.c +++ b/srcs/service/dhcpd/dhcp_option.c @@ -61,7 +61,7 @@ static const DHCP_OPTION_CFG g_opCfg[] = { {"AddressTime", OPT_IPADDRLEASE, 4, TRUE }, {"OverLoad", OPT_OVERLOAD, 7, FALSE}, {"DHCPMsgType", OPT_MESSAGETYPE, 6, TRUE }, - {"DHCPServerId", OPT_SERVERID, 3, FALSE}, + {"DHCPServerId", OPT_SERVERID, 3, TRUE }, {"ParameterList", OPT_PARAMREQLIST, 2, TRUE }, {"DHCPMessage", OPT_MESSAGE, 1, FALSE}, {"DHCPMaxMsgSize", OPT_MAXDHCPMSGSIZE, 5, FALSE}, diff --git a/srcs/service/dhcpd/lease.c b/srcs/service/dhcpd/lease.c index 0cd0690..4a67004 100644 --- a/srcs/service/dhcpd/lease.c +++ b/srcs/service/dhcpd/lease.c @@ -112,6 +112,8 @@ int pre_alloc_dhcp_res(U32 uid, const char *pMac, U32 *pOutIp, PIPPOOL_INFO *pOu *pOutPool = pPool; 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; } else { 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; *pOutPool = pPool; + LOG_MOD(trace, + ZLOG_MOD_DHCPD, + "Used prepard ipaddr %08X at %d of %p\n", + ipAddr, + addr, + pPool->assignPool); + return ERR_SUCCESS; } } diff --git a/srcs/service/dhcpd/user_mgr.c b/srcs/service/dhcpd/user_mgr.c index d51980e..7adfb0d 100644 --- a/srcs/service/dhcpd/user_mgr.c +++ b/srcs/service/dhcpd/user_mgr.c @@ -243,12 +243,11 @@ int user_add_ip_pool(U32 uId, PIPPOOL_INFO pPool) { } // 计算可用的IP - begin = ntohl(pPoolCfg->minAddr) & ntohl(~pPoolCfg->netMask); - end = ntohl(pPoolCfg->maxAddr) & ntohl(~pPoolCfg->netMask); + begin = pPoolCfg->minAddr & (~pPoolCfg->netMask); + end = pPoolCfg->maxAddr & (~pPoolCfg->netMask); for (k = MAX(begin, 1U); k <= MIN(end, ntohl(~pPoolCfg->netMask) - 1); k++) { bitset_set(pPoolCfg->assignPool, k); } - return ERR_SUCCESS; }