From be64836de83855382af2ac12f5e9199d58137285 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E6=98=95?= Date: Thu, 4 May 2023 18:07:23 +0800 Subject: [PATCH 1/4] =?UTF-8?q?OCT=201.=20=E4=BF=AE=E6=94=B9DHCP=20?= =?UTF-8?q?=E6=B5=8B=E8=AF=95=E5=B7=A5=E5=85=B7=E6=95=B0=E6=8D=AE=E5=8C=85?= =?UTF-8?q?=E6=B5=8F=E8=A7=88=E7=AA=97=E5=8F=A3=E6=BB=9A=E5=8A=A8=E6=9D=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- dhcp_tools/detail_wnd.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/dhcp_tools/detail_wnd.c b/dhcp_tools/detail_wnd.c index d141a9d..2cd42a0 100644 --- a/dhcp_tools/detail_wnd.c +++ b/dhcp_tools/detail_wnd.c @@ -384,7 +384,7 @@ static gboolean button_release_event(GtkWidget *self, GdkEventButton event, gpoi GtkTextMark *mark = gtk_text_buffer_get_insert(buffer); gtk_text_buffer_get_iter_at_mark(buffer, &start_sel, mark); gtk_text_buffer_get_iter_at_line_offset(buffer, &end_sel, row, 59); - char *text = (char *)gtk_text_buffer_get_text(buffer, &iter, &end_sel, FALSE); + //char *text = (char *)gtk_text_buffer_get_text(buffer, &iter, &end_sel, FALSE); //printf("Select %s(%d, %d)\n", text, row, col); } } @@ -427,12 +427,13 @@ void details_wnd_create(GtkBuilder *builder) { GtkWidget *scMsg = gtk_scrolled_window_new(NULL, NULL); // 上下分割面板 GtkWidget *vpaned = gtk_paned_new(GTK_ORIENTATION_VERTICAL); - gtk_container_add(GTK_CONTAINER(scMsg), vpaned); + // 上半部分控件 GtkWidget *tvDhcp = gtk_tree_view_new(); gtk_tree_view_set_grid_lines(GTK_TREE_VIEW(tvDhcp), GTK_TREE_VIEW_GRID_LINES_BOTH); + gtk_container_add(GTK_CONTAINER(scMsg), tvDhcp); - gtk_paned_pack1(GTK_PANED(vpaned), tvDhcp, TRUE, FALSE); + gtk_paned_pack1(GTK_PANED(vpaned), scMsg, TRUE, FALSE); gtk_widget_set_size_request(tvDhcp, -1, 300); gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scMsg), GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC); @@ -462,7 +463,7 @@ void details_wnd_create(GtkBuilder *builder) { // 控件布局 gtk_box_pack_start(GTK_BOX(boxTab), scHex, TRUE, TRUE, 1); gtk_box_pack_start(GTK_BOX(boxTab), vSepar, FALSE, FALSE, 1); - gtk_box_pack_start(GTK_BOX(boxTab), scMsg, TRUE, TRUE, 1); + gtk_box_pack_start(GTK_BOX(boxTab), vpaned, TRUE, TRUE, 1); gtk_widget_set_size_request(scHex, 580, -1); gtk_widget_set_size_request(tvDhcp, 300, -1); @@ -482,7 +483,6 @@ void details_wnd_create(GtkBuilder *builder) { } g_signal_connect(G_OBJECT(window), "delete_event", G_CALLBACK(delete_event), builder); - //g_signal_connect(G_OBJECT(window), "delete_event", G_CALLBACK(delete_event), builder); g_detailWnd = window; } \ No newline at end of file From 9c13c71e965c6e5dc40de06869787409fb636416 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E6=98=95?= Date: Fri, 5 May 2023 09:43:58 +0800 Subject: [PATCH 2/4] =?UTF-8?q?OCT=201.=20=E5=A2=9E=E5=8A=A0=E7=BC=96?= =?UTF-8?q?=E8=AF=91=E5=BC=80=E5=85=B3=EF=BC=8C=E6=94=AF=E6=8C=81=E4=B8=8D?= =?UTF-8?q?=E5=B8=A6VLAN=E6=97=B6=20DHCP=20=E6=9C=8D=E5=8A=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config/vcpe.cfg | 2 +- dhcp_tools/detail_wnd.c | 38 ++++----- dhcp_tools/main.h | 25 +++--- dhcp_tools/main_wnd.c | 9 ++- dhcp_tools/uv_rawsocket.c | 63 ++++++++------- srcs/libs/include/network/vlan.h | 12 ++- srcs/service/dhcpd/dhcpd_network.c | 93 +++++++++++++---------- srcs/service/dhcpd/include/dhcp_network.h | 2 +- 8 files changed, 141 insertions(+), 103 deletions(-) diff --git a/config/vcpe.cfg b/config/vcpe.cfg index 23dc248..5c92fcc 100644 --- a/config/vcpe.cfg +++ b/config/vcpe.cfg @@ -100,7 +100,7 @@ application: # MAC地址黑名单 # mac_filter = ["00:01:02:03:04:07", "00:01:02:03:04:01"]; # 数据包过滤器 - net_filter = "vlan and udp and dst port 67"; + net_filter = "udp and dst port 67"; # IP地址池配置 range_set: ( { dhcp_range = "192.168.101.2-192.168.101.4"; diff --git a/dhcp_tools/detail_wnd.c b/dhcp_tools/detail_wnd.c index 2cd42a0..3481213 100644 --- a/dhcp_tools/detail_wnd.c +++ b/dhcp_tools/detail_wnd.c @@ -187,48 +187,50 @@ static void create_dhcp_tree_mode(PDHCP_PACKAGE p, GtkWidget *treeView) { // 添加 Ethernet II 头相关内容 s = sdsempty(); - MAC_TO_STR(p->vlan_hdr.eth.h_dest, s); + MAC_TO_STR(p->hdr.eth.h_dest, s); ADD_SUB_STRING("Destination", s); sdsfree(s); s = sdsempty(); - MAC_TO_STR(p->vlan_hdr.eth.h_source, s); + MAC_TO_STR(p->hdr.eth.h_source, s); ADD_SUB_STRING("Source", s); sdsfree(s); +#if VLAN_SUPPORT // 添加 VLan 头相关内容 gtk_tree_store_append(store, &iter, NULL); gtk_tree_store_set(store, &iter, 0, itemTitle[1], 1, "", -1); - ADD_SUB_INT("ID", "%u", VLAN_VNI_ID(p->vlan_hdr.vlan.id)); - ADD_SUB_INT("Type", "0x%04X", ntohs(p->vlan_hdr.vlan.type)); + ADD_SUB_INT("ID", "%u", VLAN_VNI_ID(p->hdr.vlan.id)); + ADD_SUB_INT("Type", "0x%04X", ntohs(p->hdr.vlan.type)); +#endif // 添加 IP 头 gtk_tree_store_append(store, &iter, NULL); gtk_tree_store_set(store, &iter, 0, itemTitle[2], 1, "", -1); - ADD_SUB_INT("Version", "%u", p->vlan_hdr.ip.version); + ADD_SUB_INT("Version", "%u", p->hdr.ip.version); s = sdsempty(); - sprintf(s, "%u %s (%u)", p->vlan_hdr.ip.ihl*4, "bytes", p->vlan_hdr.ip.ihl); + sprintf(s, "%u %s (%u)", p->hdr.ip.ihl*4, "bytes", p->hdr.ip.ihl); ADD_SUB_STRING("Header Length", s); sdsfree(s); - ADD_SUB_INT("Differentiated Services Field", "0x%02X", p->vlan_hdr.ip.tos); - ADD_SUB_INT("Total Length", "%u", ntohs(p->vlan_hdr.ip.tot_len)); - ADD_SUB_INT("Identification", "0x%04X", ntohs(p->vlan_hdr.ip.id)); - ADD_SUB_INT("Flags", "0x%04X", ntohs(p->vlan_hdr.ip.frag_off)); - ADD_SUB_INT("Time to live", "%u", p->vlan_hdr.ip.ttl); + ADD_SUB_INT("Differentiated Services Field", "0x%02X", p->hdr.ip.tos); + ADD_SUB_INT("Total Length", "%u", ntohs(p->hdr.ip.tot_len)); + ADD_SUB_INT("Identification", "0x%04X", ntohs(p->hdr.ip.id)); + ADD_SUB_INT("Flags", "0x%04X", ntohs(p->hdr.ip.frag_off)); + ADD_SUB_INT("Time to live", "%u", p->hdr.ip.ttl); ADD_SUB_STRING("Protocol", "UDP"); - ADD_SUB_INT("Header Checksum", "0x%04X", ntohs(p->vlan_hdr.ip.check)); - ADD_SUB_STRING("Source", inet_ntoa(*(struct in_addr*)&p->vlan_hdr.ip.saddr)); - ADD_SUB_STRING("Destination", inet_ntoa(*(struct in_addr*)&p->vlan_hdr.ip.daddr)); + ADD_SUB_INT("Header Checksum", "0x%04X", ntohs(p->hdr.ip.check)); + ADD_SUB_STRING("Source", inet_ntoa(*(struct in_addr*)&p->hdr.ip.saddr)); + ADD_SUB_STRING("Destination", inet_ntoa(*(struct in_addr*)&p->hdr.ip.daddr)); // 添加 UDP 头 gtk_tree_store_append(store, &iter, NULL); gtk_tree_store_set(store, &iter, 0, itemTitle[3], 1, "", -1); - ADD_SUB_INT("Source Port", "%u", ntohs(p->vlan_hdr.udp.source)); - ADD_SUB_INT("Destination Port", "%u", ntohs(p->vlan_hdr.udp.dest)); - ADD_SUB_INT("Length", "%u", ntohs(p->vlan_hdr.udp.len)); - ADD_SUB_INT("Checksum", "0x%04X", ntohs(p->vlan_hdr.udp.check)); + ADD_SUB_INT("Source Port", "%u", ntohs(p->hdr.udp.source)); + ADD_SUB_INT("Destination Port", "%u", ntohs(p->hdr.udp.dest)); + ADD_SUB_INT("Length", "%u", ntohs(p->hdr.udp.len)); + ADD_SUB_INT("Checksum", "0x%04X", ntohs(p->hdr.udp.check)); // 添加 DHCP 内容 gtk_tree_store_append(store, &iter, NULL); diff --git a/dhcp_tools/main.h b/dhcp_tools/main.h index f21d7ed..f16000e 100644 --- a/dhcp_tools/main.h +++ b/dhcp_tools/main.h @@ -51,8 +51,8 @@ typedef enum { } DHCP_STATUS; typedef struct { - U8 *p; - int buf_size; + U8 *p; + int buf_size; unsigned int tm; } BUF_INFO, *PBUF_INFO; @@ -73,13 +73,16 @@ typedef struct { UT_hash_handle hh; } DHCP_INFO, *PDHCP_INFO; -U32 rand_number(); -PDHCP_INFO get_dhcp_info_by_id(U32 xid); -int dhcp_tools_init_network(const char *pNicName); -U8 *dhcp_create_discover_req(PDHCP_INFO pInfo, int *pOutSize); -U8 *dhcp_create_request_req(PDHCP_INFO pInfo, int *pOutSize); -int cacheDhcpOfferBuffer(PDHCP_INFO pInfo, U8 *pBuf, int size); -int cacheDhcpAckBuffer(PDHCP_INFO pInfo, U8 *pBuf, int size); -void details_wnd_create(GtkBuilder *builder); -void details_wnd_show(PDHCP_INFO pInfo); +U32 rand_number(); +PDHCP_INFO get_dhcp_info_by_id(U32 xid); +int dhcp_tools_init_network(const char *pNicName); +U8 *dhcp_create_discover_req(PDHCP_INFO pInfo, int *pOutSize); +U8 *dhcp_create_request_req(PDHCP_INFO pInfo, int *pOutSize); +int cacheDhcpOfferBuffer(PDHCP_INFO pInfo, U8 *pBuf, int size); +int cacheDhcpAckBuffer(PDHCP_INFO pInfo, U8 *pBuf, int size); +void details_wnd_create(GtkBuilder *builder); +void details_wnd_show(PDHCP_INFO pInfo); +void hex_wnd_create(GtkBuilder *builder); +void hex_wnd_show(PDHCP_INFO pInfo); +GtkBuilder *get_main_builder(); #endif //VCPE_MAIN_H diff --git a/dhcp_tools/main_wnd.c b/dhcp_tools/main_wnd.c index ef621fa..572f942 100644 --- a/dhcp_tools/main_wnd.c +++ b/dhcp_tools/main_wnd.c @@ -19,6 +19,10 @@ static PDHCP_INFO g_pDhcpInfo = NULL; static GThread *g_pEvLoopThread = NULL; static int g_runTask = FALSE; +GtkBuilder *get_main_builder() { + return g_mainBuilder; +} + U32 rand_number() { GRand *pRand = g_rand_new_with_seed(time(NULL)); @@ -67,7 +71,9 @@ static gboolean calc_step_progress(PDHCP_INFO pInfo, DHCP_STEP step) { return FALSE; } -void view_popup_menu_onStatistics(GtkWidget *UNUSED(menuitem), gpointer UNUSED(userdata)) { +void view_popup_menu_onStatistics(GtkWidget *UNUSED(menuitem), gpointer userdata) { + //PDHCP_INFO pInfo = (PDHCP_INFO)userdata; + //hex_wnd_show(pInfo); } void view_popup_menu_onBinaryPkg(GtkWidget *UNUSED(menuitem), gpointer userdata) { @@ -616,6 +622,7 @@ int main(int args, char **argv) { //GtkWidget *view = GTK_WIDGET(gtk_builder_get_object(g_mainBuilder, "treeResult")); details_wnd_create(g_mainBuilder); + //hex_wnd_create(g_mainBuilder); #if 0 gtk_list_store_new(NUM_COLS, G_TYPE_INT, diff --git a/dhcp_tools/uv_rawsocket.c b/dhcp_tools/uv_rawsocket.c index c8db6f7..909c699 100644 --- a/dhcp_tools/uv_rawsocket.c +++ b/dhcp_tools/uv_rawsocket.c @@ -16,34 +16,40 @@ static char *g_pNicName = NULL; static void pkg_init_head(PDHCP_PACKAGE p, PDHCP_INFO pInfo) { // 目的地 MAC 地址 - memset(p->vlan_hdr.eth.h_dest, 0xFF, ETH_ALEN); + memset(p->hdr.eth.h_dest, 0xFF, ETH_ALEN); // 源 MAC 地址 - memcpy(p->vlan_hdr.eth.h_source, pInfo->mac, ETH_ALEN); + memcpy(p->hdr.eth.h_source, pInfo->mac, ETH_ALEN); + +#if VLAN_SUPPORT // 协议 VLAN - p->vlan_hdr.eth.h_proto = htons(ETH_P_8021Q); + p->hdr.eth.h_proto = htons(ETH_P_8021Q); // VLAN 隧道信息 - p->vlan_hdr.vlan.id = htons(pInfo->vni); - p->vlan_hdr.vlan.type = htons(ETH_P_IP); + p->hdr.vlan.id = htons(pInfo->vni); + p->hdr.vlan.type = htons(ETH_P_IP); +#else + // 协议 VLAN + p->hdr.eth.h_proto = htons(ETH_P_IP); +#endif // IP 头 - p->vlan_hdr.ip.version = IPVERSION; - p->vlan_hdr.ip.ihl = 5; - p->vlan_hdr.ip.tos = 0x10; - p->vlan_hdr.ip.tot_len = 0; - p->vlan_hdr.ip.id = 0; - p->vlan_hdr.ip.frag_off = 0; - p->vlan_hdr.ip.ttl = 128; - p->vlan_hdr.ip.protocol = IPPROTO_UDP; - p->vlan_hdr.ip.check = 0; - p->vlan_hdr.ip.saddr = INADDR_ANY; - p->vlan_hdr.ip.daddr = INADDR_BROADCAST; + p->hdr.ip.version = IPVERSION; + p->hdr.ip.ihl = 5; + p->hdr.ip.tos = 0; + p->hdr.ip.tot_len = 0; + p->hdr.ip.id = 0; + p->hdr.ip.frag_off = 0; + p->hdr.ip.ttl = 128; + p->hdr.ip.protocol = IPPROTO_UDP; + p->hdr.ip.check = 0; + p->hdr.ip.saddr = INADDR_ANY; + p->hdr.ip.daddr = INADDR_BROADCAST; // UDP 头 - p->vlan_hdr.udp.source = htons(DHCP_CLI_PORT); - p->vlan_hdr.udp.dest = htons(DHCP_SVR_PORT); - p->vlan_hdr.udp.len = 0; - p->vlan_hdr.udp.check = 0; + p->hdr.udp.source = htons(DHCP_CLI_PORT); + p->hdr.udp.dest = htons(DHCP_SVR_PORT); + p->hdr.udp.len = 0; + p->hdr.udp.check = 0; // DHCP 协议内容 p->dhcp.op = BOOTP_REQUEST; @@ -63,18 +69,19 @@ static void pkg_init_head(PDHCP_PACKAGE p, PDHCP_INFO pInfo) { static void cacl_package_checksum(PDHCP_PACKAGE p, int tolSize) { U16 csum; + // 计算 IP 数据长度 - p->vlan_hdr.ip.tot_len = htons(tolSize - sizeof(struct ethhdr) - sizeof(struct vlan_hdr)); + p->hdr.ip.tot_len = htons(tolSize - sizeof(struct ethhdr) - sizeof(struct vlan_hdr)); // 计算 UDP 数据长度 - p->vlan_hdr.udp.len = htons(tolSize - sizeof(struct ethhdr) - sizeof(struct vlan_hdr) - sizeof(struct iphdr)); + p->hdr.udp.len = htons(tolSize - sizeof(struct ethhdr) - sizeof(struct vlan_hdr) - sizeof(struct iphdr)); // 计算 IP 校验和 - csum = htons(ip_checksum((unsigned char *)&p->vlan_hdr.ip)); - p->vlan_hdr.ip.check = htons(csum); + csum = htons(ip_checksum((unsigned char *)&p->hdr.ip)); + p->hdr.ip.check = htons(csum); // 计算 UDP 校验和 - csum = htons(udp_checksum(p->vlan_hdr.ip.saddr, p->vlan_hdr.ip.daddr, (unsigned char *)&p->vlan_hdr.udp)); - p->vlan_hdr.udp.check = htons(csum); + csum = htons(udp_checksum(p->hdr.ip.saddr, p->hdr.ip.daddr, (unsigned char *)&p->hdr.udp)); + p->hdr.udp.check = htons(csum); } U8 *dhcp_create_request_req(PDHCP_INFO pInfo, int *pOutSize) { @@ -257,7 +264,11 @@ int dhcp_tools_init_network(const char *pNicName) { return ERR_SUCCESS; } +#if VLAN_SUPPORT init_filter("vlan and udp and dst port 68"); +#else + init_filter("udp and dst port 68"); +#endif ret = create_udp_raw_socket(g_pNicName); if (ret != ERR_SUCCESS) { diff --git a/srcs/libs/include/network/vlan.h b/srcs/libs/include/network/vlan.h index d435ea7..e47b869 100644 --- a/srcs/libs/include/network/vlan.h +++ b/srcs/libs/include/network/vlan.h @@ -13,21 +13,27 @@ extern "C" { #include #include "common.h" +#define VLAN_SUPPORT (0) + #pragma pack(push) #pragma pack(1) #define VXLAN_VIN_ID_PACK(x) ((ntohs(x)) & 0xFFF) typedef struct vlan_hdr { +#if VLAN_SUPPORT U16 id; U16 type; +#endif } VLAN_HDR, *PVLAN_HDR; typedef struct { - struct ethhdr eth; + struct ethhdr eth; +#if VLAN_SUPPORT struct vlan_hdr vlan; - struct iphdr ip; - struct udphdr udp; +#endif + struct iphdr ip; + struct udphdr udp; } VLAN_PKG_HDR, *PVLAN_PKG_HDR; #pragma pack(pop) diff --git a/srcs/service/dhcpd/dhcpd_network.c b/srcs/service/dhcpd/dhcpd_network.c index 8e43239..68fee7c 100644 --- a/srcs/service/dhcpd/dhcpd_network.c +++ b/srcs/service/dhcpd/dhcpd_network.c @@ -187,40 +187,41 @@ static void fill_package(PDHCP_PACKAGE pRsp, PDHCP_PACKAGE pReq) { // 二层头 // 目的IP地址 - if (pReq->vlan_hdr.ip.saddr != 0) { - memcpy(pRsp->vlan_hdr.eth.h_dest, pRsp->vlan_hdr.eth.h_source, ETH_ALEN); + if (pReq->hdr.ip.saddr != 0) { + memcpy(pRsp->hdr.eth.h_dest, pRsp->hdr.eth.h_source, ETH_ALEN); } else { - memset(pRsp->vlan_hdr.eth.h_dest, 0xFF, ETH_ALEN); + memset(pRsp->hdr.eth.h_dest, 0xFF, ETH_ALEN); } // 源 IP 地址 - memcpy(pRsp->vlan_hdr.eth.h_source, g_nicInfo.macAddr, ETH_ALEN); + memcpy(pRsp->hdr.eth.h_source, g_nicInfo.macAddr, ETH_ALEN); // protol - pRsp->vlan_hdr.eth.h_proto = pReq->vlan_hdr.eth.h_proto; + pRsp->hdr.eth.h_proto = pReq->hdr.eth.h_proto; +#if VLAN_SUPPORT // QinQ 隧道 - pRsp->vlan_hdr.vlan.id = pReq->vlan_hdr.vlan.id; - pRsp->vlan_hdr.vlan.type = pReq->vlan_hdr.vlan.type; + pRsp->hdr.vlan.id = pReq->hdr.vlan.id; + pRsp->hdr.vlan.type = pReq->hdr.vlan.type; // TODO 可能的二层QinQ隧道 - +#endif // IP 头 - memcpy(&pRsp->vlan_hdr.ip, &pReq->vlan_hdr.ip, sizeof(struct iphdr)); + memcpy(&pRsp->hdr.ip, &pReq->hdr.ip, sizeof(struct iphdr)); // TOS - pRsp->vlan_hdr.ip.tos = 0; + pRsp->hdr.ip.tos = 0; // 更新源IP - pRsp->vlan_hdr.ip.saddr = g_nicInfo.ipAddr; + pRsp->hdr.ip.saddr = g_nicInfo.ipAddr; // 更新目的IP地址,广播255.255.255.255 - if (pReq->vlan_hdr.ip.saddr == 0) { - pRsp->vlan_hdr.ip.daddr = 0xFFFFFFFF; + if (pReq->hdr.ip.saddr == 0) { + pRsp->hdr.ip.daddr = 0xFFFFFFFF; } else { - pRsp->vlan_hdr.ip.daddr = pReq->vlan_hdr.ip.saddr; + pRsp->hdr.ip.daddr = pReq->hdr.ip.saddr; } // UDP 头 // 目的端口 - pRsp->vlan_hdr.udp.dest = pReq->vlan_hdr.udp.source; + pRsp->hdr.udp.dest = pReq->hdr.udp.source; // 源端口 - pRsp->vlan_hdr.udp.source = pReq->vlan_hdr.udp.dest; + pRsp->hdr.udp.source = pReq->hdr.udp.dest; //DHCP 协议 // 返回类型 @@ -255,17 +256,17 @@ static int dhcp_prepare_tx(U8 *pOpt, PDHCP_PACKAGE pRsp) { tolSize = (int)((pOpt - pRsp->dhcp.options) + 1 + sizeof(DHCP_PACKAGE)); // 计算 IP 数据长度 - pRsp->vlan_hdr.ip.tot_len = htons(tolSize - sizeof(struct ethhdr) - sizeof(struct vlan_hdr)); + pRsp->hdr.ip.tot_len = htons(tolSize - sizeof(struct ethhdr) - sizeof(struct vlan_hdr)); // 计算 UDP 数据长度 - pRsp->vlan_hdr.udp.len = htons(tolSize - sizeof(struct ethhdr) - sizeof(struct vlan_hdr) - sizeof(struct iphdr)); + pRsp->hdr.udp.len = htons(tolSize - sizeof(struct ethhdr) - sizeof(struct vlan_hdr) - sizeof(struct iphdr)); // 计算 IP 校验和 - csum = htons(ip_checksum((unsigned char *)&pRsp->vlan_hdr.ip)); - pRsp->vlan_hdr.ip.check = htons(csum); + csum = htons(ip_checksum((unsigned char *)&pRsp->hdr.ip)); + pRsp->hdr.ip.check = htons(csum); // 计算 UDP 校验和 - csum = htons(udp_checksum(pRsp->vlan_hdr.ip.saddr, pRsp->vlan_hdr.ip.daddr, (unsigned char *)&pRsp->vlan_hdr.udp)); - pRsp->vlan_hdr.udp.check = htons(csum); + csum = htons(udp_checksum(pRsp->hdr.ip.saddr, pRsp->hdr.ip.daddr, (unsigned char *)&pRsp->hdr.udp)); + pRsp->hdr.udp.check = htons(csum); LOG_MOD(trace, ZM_DHCP_NET, "OPTIONS size: %ld\n", (intptr_t)(pOpt - pRsp->dhcp.options) + 1); LOG_MOD(trace, ZM_DHCP_NET, "Total size: %d\n", tolSize); @@ -406,7 +407,7 @@ static void on_sock_recv(uv_work_t *req) { PDHCP_PACKAGE pkg = (PDHCP_PACKAGE)pWork->pPkgBase; U32 optSize = pWork->nSize - sizeof(DHCP_PACKAGE); - sprintf(macStr, "%02X:%02X:%02X:%02X:%02X:%02X", macStr[0], macStr[1], macStr[2], macStr[3], macStr[4], macStr[5]); + //sprintf(macStr, "%02X:%02X:%02X:%02X:%02X:%02X", macStr[0], macStr[1], macStr[2], macStr[3], macStr[4], macStr[5]); // Check op flag if (pkg->dhcp.op != BOOTP_REQUEST) { @@ -422,9 +423,13 @@ static void on_sock_recv(uv_work_t *req) { } memset(&reqDhcp, 0, sizeof(DHCP_REQ)); - reqDhcp.unicast = pkg->dhcp.flags != 0 ? TRUE : FALSE; - reqDhcp.xid = DHCP_XID(pkg->dhcp.xid); - reqDhcp.uid = VLAN_VNI_ID(pkg->vlan_hdr.vlan.id); + reqDhcp.unicast = pkg->dhcp.flags != 0 ? TRUE : FALSE; + reqDhcp.xid = DHCP_XID(pkg->dhcp.xid); +#if VLAN_SUPPORT + reqDhcp.uid = VLAN_VNI_ID(pkg->hdr.vlan.id); +#else + reqDhcp.uid = 0; +#endif reqDhcp.serverAddr = g_nicInfo.ipAddr; reqDhcp.cliAddr = ntohl(pkg->dhcp.ciaddr); memcpy(reqDhcp.cliMac, pkg->dhcp.chaddr, ETH_ALEN); @@ -482,8 +487,8 @@ static void on_sock_recv(uv_work_t *req) { reqDhcp.reqServer = ntohl(*((U32 *)opt.pValue)); } else { // Request 服务器IP可以取目的 IP 地址 - if (pkg->vlan_hdr.ip.saddr != 0) { - reqDhcp.reqServer = ntohl(pkg->vlan_hdr.ip.daddr); + if (pkg->hdr.ip.saddr != 0) { + reqDhcp.reqServer = ntohl(pkg->hdr.ip.daddr); } } @@ -537,8 +542,8 @@ static void on_sock_recv(uv_work_t *req) { reqDhcp.reqServer = ntohl(*((U32 *)opt.pValue)); } else { // Request 服务器IP可以取目的 IP 地址 - if (pkg->vlan_hdr.ip.saddr != 0) { - reqDhcp.reqServer = ntohl(pkg->vlan_hdr.ip.saddr); + if (pkg->hdr.ip.saddr != 0) { + reqDhcp.reqServer = ntohl(pkg->hdr.ip.saddr); } } @@ -577,22 +582,22 @@ static void on_sock_recv(uv_work_t *req) { break; } - //dhcp_option_prase(optMsg, pkg->dhcp.options, pWork->nSize - sizeof(DHCP_PACKAGE)); + //dhcp_option_prase(optMsg, pkg->dhcp.options, pWork->nSize - sizeof(DHCP_PACKAGE)); - //LOG_MSG_HEX(trace, pkg, pWork->nSize); - // LOG_MSG(info, "vni: %d, xid: 0x%08X\n", VLAN_VNI_ID(pkg->vlan_hdr.vlan.id), DHCP_XID(pkg->dhcp.xid)); + LOG_MSG_HEX(trace, pkg, pWork->nSize); + //LOG_MSG(info, "vni: %d, xid: 0x%08X\n", VLAN_VNI_ID(pkg->hdr.vlan.id), DHCP_XID(pkg->dhcp.xid)); #if 0 - LOG_MOD(info, ZM_DHCP_NET, "vlan = %u\n", VXLAN_VIN_ID_PACK(pkg->vlan_hdr.vlan.id)); + LOG_MOD(info, ZM_DHCP_NET, "vlan = %u\n", VXLAN_VIN_ID_PACK(pkg->hdr.vlan.id)); LOG_MSG(info, "xid: 0x%08X\n", ntohl(pkg->dhcp.xid)); LOG_MSG(info, "dest mac: %02X:%02X:%02X:%02X:%02X:%02X\n", - pkg->vlan_hdr.eth.h_dest[0], - pkg->vlan_hdr.eth.h_dest[1], - pkg->vlan_hdr.eth.h_dest[2], - pkg->vlan_hdr.eth.h_dest[3], - pkg->vlan_hdr.eth.h_dest[4], - pkg->vlan_hdr.eth.h_dest[5]); + pkg->hdr.eth.h_dest[0], + pkg->hdr.eth.h_dest[1], + pkg->hdr.eth.h_dest[2], + pkg->hdr.eth.h_dest[3], + pkg->hdr.eth.h_dest[4], + pkg->hdr.eth.h_dest[5]); LOG_MSG(info, "client mac: %02X:%02X:%02X:%02X:%02X:%02X\n", pkg->dhcp.chaddr[0], @@ -655,8 +660,12 @@ void raw_sock_recv_cb(uv_poll_t *handle, int status, int events) { pMsg->pPkgInfo[i].uvWork.data = &pMsg->pPkgInfo[i]; pMsg->pPkgInfo[i].pData = pMsg; if (g_dhcpMode == MODE_DHCP_SERVER) { - pkg = (PDHCP_PACKAGE)pMsg->pPkgInfo[i].pPkgBase; - uid = VLAN_VNI_ID(pkg->vlan_hdr.vlan.id); +#if VLAN_SUPPORT + pkg = (PDHCP_PACKAGE)pMsg->pPkgInfo[i].pPkgBase; + uid = VLAN_VNI_ID(pkg->hdr.vlan.id); +#else + uid = 0; +#endif pMsg->pPkgInfo[i].pUser = dhcp_user_create(uid); // LOG_MOD(trace, ZM_DHCP_NET, ">>> User %u xid %08X addr %p\n", uid, DHCP_XID(pkg->dhcp.xid), diff --git a/srcs/service/dhcpd/include/dhcp_network.h b/srcs/service/dhcpd/include/dhcp_network.h index 6ba05c4..59a0739 100644 --- a/srcs/service/dhcpd/include/dhcp_network.h +++ b/srcs/service/dhcpd/include/dhcp_network.h @@ -53,7 +53,7 @@ typedef struct { #pragma pack(1) typedef struct { - VLAN_PKG_HDR vlan_hdr; + VLAN_PKG_HDR hdr; DHCP_PROTO dhcp; } DHCP_PACKAGE, *PDHCP_PACKAGE; From 13b447daac2ce2396f5865ebfcdf61d53cc5e946 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E6=98=95?= Date: Fri, 5 May 2023 09:47:14 +0800 Subject: [PATCH 3/4] =?UTF-8?q?OCT=201.=20=E5=A2=9E=E5=8A=A0=E7=BC=96?= =?UTF-8?q?=E8=AF=91=E5=BC=80=E5=85=B3=EF=BC=8C=E6=94=AF=E6=8C=81=E4=B8=8D?= =?UTF-8?q?=E5=B8=A6VLAN=E6=97=B6=20DHCP=20=E6=9C=8D=E5=8A=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config/vcpe.cfg | 2 +- srcs/libs/include/network/vlan.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/config/vcpe.cfg b/config/vcpe.cfg index 5c92fcc..23dc248 100644 --- a/config/vcpe.cfg +++ b/config/vcpe.cfg @@ -100,7 +100,7 @@ application: # MAC地址黑名单 # mac_filter = ["00:01:02:03:04:07", "00:01:02:03:04:01"]; # 数据包过滤器 - net_filter = "udp and dst port 67"; + net_filter = "vlan and udp and dst port 67"; # IP地址池配置 range_set: ( { dhcp_range = "192.168.101.2-192.168.101.4"; diff --git a/srcs/libs/include/network/vlan.h b/srcs/libs/include/network/vlan.h index e47b869..4d591b7 100644 --- a/srcs/libs/include/network/vlan.h +++ b/srcs/libs/include/network/vlan.h @@ -13,7 +13,7 @@ extern "C" { #include #include "common.h" -#define VLAN_SUPPORT (0) +#define VLAN_SUPPORT (1) #pragma pack(push) #pragma pack(1) From d91e80acb3b37aec78ceb016eae1dd67783515a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E6=98=95?= Date: Fri, 5 May 2023 09:59:20 +0800 Subject: [PATCH 4/4] =?UTF-8?q?OCT=201.=20=E5=B1=8F=E8=94=BDSIGPIPE?= =?UTF-8?q?=E8=BF=9B=E7=A8=8B=E9=80=80=E5=87=BA=EF=BC=8C=E5=A2=9E=E5=8A=A0?= =?UTF-8?q?=E4=BA=8B=E4=BB=B6=E6=8D=95=E8=8E=B7=E8=B0=83=E8=AF=95=E4=BF=A1?= =?UTF-8?q?=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- srcs/libs/init/init_runtime.c | 18 +++++------------- 1 file changed, 5 insertions(+), 13 deletions(-) diff --git a/srcs/libs/init/init_runtime.c b/srcs/libs/init/init_runtime.c index 4beaf77..604fed3 100644 --- a/srcs/libs/init/init_runtime.c +++ b/srcs/libs/init/init_runtime.c @@ -32,10 +32,10 @@ static pid_t g_pid; static int g_isInited = FALSE; -static void catch_system_interupt(int UNUSED(sig_num)) { +static void catch_system_interupt(int sig_num) { if (g_pid == uv_os_getpid()) { printf("\n"); - LOG_MOD(warn, ZLOG_MOD_INIT, "System close, clearing system resources..........\n\n"); + LOG_MOD(warn, ZLOG_MOD_INIT, "(%d): System close, clearing system resources..........\n\n", sig_num); task_manager_exit(); sleep(1); } @@ -56,7 +56,7 @@ int user_init(const char *pAppCfgFile, const char *pCfgDirectory, const char *pK signal(SIGQUIT, catch_system_interupt); signal(SIGTSTP, catch_system_interupt); signal(SIGHUP, SIG_IGN); - signal(SIGPIPE, catch_system_interupt); + signal(SIGPIPE, SIG_IGN); signal(SIGKILL, catch_system_interupt); // 初始化 libuv loop @@ -107,16 +107,8 @@ int user_init(const char *pAppCfgFile, const char *pCfgDirectory, const char *pK return -ERR_CONFIG_INIT; } - LOG_MOD(info, - ZLOG_MOD_INIT, - "%s library version %s information: %s (Build: %s %s GCC Ver:%s) With %lu(bits) OS\n", - VCPE_LIB_NAME, - VCPE_LIB_VER, - VCPE_GIT_VERSION, - __DATE__, - __TIME__, - __VERSION__, - sizeof(int *) * 8); + LOG_MOD(info, ZLOG_MOD_INIT, "%s library version %s information: %s (Build: %s %s GCC Ver:%s) With %lu(bits) OS\n", + VCPE_LIB_NAME, VCPE_LIB_VER, VCPE_GIT_VERSION, __DATE__, __TIME__, __VERSION__, sizeof(int *) * 8); LOG_MOD(info, ZLOG_MOD_INIT, "Application build configure: [%s]\n", VCPE_BUILD_CONFIG);