Merge branch 'libuv_dhcpd_dev' of http://git.komect.net/ZNJK/vcpe into libuv_dhcpd_dev

This commit is contained in:
dongwenze 2023-05-05 10:15:43 +08:00
commit cc622ebd68
8 changed files with 150 additions and 120 deletions

View File

@ -189,48 +189,50 @@ static void create_dhcp_tree_mode(PDHCP_PACKAGE p, GtkWidget *treeView) {
// 添加 Ethernet II 头相关内容 // 添加 Ethernet II 头相关内容
s = sdsempty(); 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); ADD_SUB_STRING("Destination", s);
sdsfree(s); sdsfree(s);
s = sdsempty(); 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); ADD_SUB_STRING("Source", s);
sdsfree(s); sdsfree(s);
#if VLAN_SUPPORT
// 添加 VLan 头相关内容 // 添加 VLan 头相关内容
gtk_tree_store_append(store, &iter, NULL); gtk_tree_store_append(store, &iter, NULL);
gtk_tree_store_set(store, &iter, 0, itemTitle[1], 1, "", -1); 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("ID", "%u", VLAN_VNI_ID(p->hdr.vlan.id));
ADD_SUB_INT("Type", "0x%04X", ntohs(p->vlan_hdr.vlan.type)); ADD_SUB_INT("Type", "0x%04X", ntohs(p->hdr.vlan.type));
#endif
// 添加 IP 头 // 添加 IP 头
gtk_tree_store_append(store, &iter, NULL); gtk_tree_store_append(store, &iter, NULL);
gtk_tree_store_set(store, &iter, 0, itemTitle[2], 1, "", -1); 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(); 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); ADD_SUB_STRING("Header Length", s);
sdsfree(s); sdsfree(s);
ADD_SUB_INT("Differentiated Services Field", "0x%02X", p->vlan_hdr.ip.tos); ADD_SUB_INT("Differentiated Services Field", "0x%02X", p->hdr.ip.tos);
ADD_SUB_INT("Total Length", "%u", ntohs(p->vlan_hdr.ip.tot_len)); ADD_SUB_INT("Total Length", "%u", ntohs(p->hdr.ip.tot_len));
ADD_SUB_INT("Identification", "0x%04X", ntohs(p->vlan_hdr.ip.id)); ADD_SUB_INT("Identification", "0x%04X", ntohs(p->hdr.ip.id));
ADD_SUB_INT("Flags", "0x%04X", ntohs(p->vlan_hdr.ip.frag_off)); ADD_SUB_INT("Flags", "0x%04X", ntohs(p->hdr.ip.frag_off));
ADD_SUB_INT("Time to live", "%u", p->vlan_hdr.ip.ttl); ADD_SUB_INT("Time to live", "%u", p->hdr.ip.ttl);
ADD_SUB_STRING("Protocol", "UDP"); ADD_SUB_STRING("Protocol", "UDP");
ADD_SUB_INT("Header Checksum", "0x%04X", ntohs(p->vlan_hdr.ip.check)); ADD_SUB_INT("Header Checksum", "0x%04X", ntohs(p->hdr.ip.check));
ADD_SUB_STRING("Source", inet_ntoa(*(struct in_addr*)&p->vlan_hdr.ip.saddr)); ADD_SUB_STRING("Source", inet_ntoa(*(struct in_addr*)&p->hdr.ip.saddr));
ADD_SUB_STRING("Destination", inet_ntoa(*(struct in_addr*)&p->vlan_hdr.ip.daddr)); ADD_SUB_STRING("Destination", inet_ntoa(*(struct in_addr*)&p->hdr.ip.daddr));
// 添加 UDP 头 // 添加 UDP 头
gtk_tree_store_append(store, &iter, NULL); gtk_tree_store_append(store, &iter, NULL);
gtk_tree_store_set(store, &iter, 0, itemTitle[3], 1, "", -1); 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("Source Port", "%u", ntohs(p->hdr.udp.source));
ADD_SUB_INT("Destination Port", "%u", ntohs(p->vlan_hdr.udp.dest)); ADD_SUB_INT("Destination Port", "%u", ntohs(p->hdr.udp.dest));
ADD_SUB_INT("Length", "%u", ntohs(p->vlan_hdr.udp.len)); ADD_SUB_INT("Length", "%u", ntohs(p->hdr.udp.len));
ADD_SUB_INT("Checksum", "0x%04X", ntohs(p->vlan_hdr.udp.check)); ADD_SUB_INT("Checksum", "0x%04X", ntohs(p->hdr.udp.check));
// 添加 DHCP 内容 // 添加 DHCP 内容
gtk_tree_store_append(store, &iter, NULL); gtk_tree_store_append(store, &iter, NULL);
@ -386,7 +388,7 @@ static gboolean button_release_event(GtkWidget *self, GdkEventButton event, gpoi
GtkTextMark *mark = gtk_text_buffer_get_insert(buffer); 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_mark(buffer, &start_sel, mark);
gtk_text_buffer_get_iter_at_line_offset(buffer, &end_sel, row, 59); 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); //printf("Select %s(%d, %d)\n", text, row, col);
} }
} }
@ -429,12 +431,13 @@ void details_wnd_create(GtkBuilder *builder) {
GtkWidget *scMsg = gtk_scrolled_window_new(NULL, NULL); GtkWidget *scMsg = gtk_scrolled_window_new(NULL, NULL);
// 上下分割面板 // 上下分割面板
GtkWidget *vpaned = gtk_paned_new(GTK_ORIENTATION_VERTICAL); GtkWidget *vpaned = gtk_paned_new(GTK_ORIENTATION_VERTICAL);
gtk_container_add(GTK_CONTAINER(scMsg), vpaned);
// 上半部分控件 // 上半部分控件
GtkWidget *tvDhcp = gtk_tree_view_new(); GtkWidget *tvDhcp = gtk_tree_view_new();
gtk_tree_view_set_grid_lines(GTK_TREE_VIEW(tvDhcp), GTK_TREE_VIEW_GRID_LINES_BOTH); 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_widget_set_size_request(tvDhcp, -1, 300);
gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scMsg), GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC); gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scMsg), GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
@ -464,7 +467,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), scHex, TRUE, TRUE, 1);
gtk_box_pack_start(GTK_BOX(boxTab), vSepar, FALSE, FALSE, 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(scHex, 580, -1);
gtk_widget_set_size_request(tvDhcp, 300, -1); gtk_widget_set_size_request(tvDhcp, 300, -1);
@ -484,7 +487,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_signal_connect(G_OBJECT(window), "delete_event", G_CALLBACK(delete_event), builder);
g_detailWnd = window; g_detailWnd = window;
} }

View File

@ -51,8 +51,8 @@ typedef enum {
} DHCP_STATUS; } DHCP_STATUS;
typedef struct { typedef struct {
U8 *p; U8 *p;
int buf_size; int buf_size;
unsigned int tm; unsigned int tm;
} BUF_INFO, *PBUF_INFO; } BUF_INFO, *PBUF_INFO;
@ -73,13 +73,16 @@ typedef struct {
UT_hash_handle hh; UT_hash_handle hh;
} DHCP_INFO, *PDHCP_INFO; } DHCP_INFO, *PDHCP_INFO;
U32 rand_number(); U32 rand_number();
PDHCP_INFO get_dhcp_info_by_id(U32 xid); PDHCP_INFO get_dhcp_info_by_id(U32 xid);
int dhcp_tools_init_network(const char *pNicName); int dhcp_tools_init_network(const char *pNicName);
U8 *dhcp_create_discover_req(PDHCP_INFO pInfo, int *pOutSize); U8 *dhcp_create_discover_req(PDHCP_INFO pInfo, int *pOutSize);
U8 *dhcp_create_request_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 cacheDhcpOfferBuffer(PDHCP_INFO pInfo, U8 *pBuf, int size);
int cacheDhcpAckBuffer(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_create(GtkBuilder *builder);
void details_wnd_show(PDHCP_INFO pInfo); 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 #endif //VCPE_MAIN_H

View File

@ -19,6 +19,10 @@ static PDHCP_INFO g_pDhcpInfo = NULL;
static GThread *g_pEvLoopThread = NULL; static GThread *g_pEvLoopThread = NULL;
static int g_runTask = FALSE; static int g_runTask = FALSE;
GtkBuilder *get_main_builder() {
return g_mainBuilder;
}
U32 rand_number() { U32 rand_number() {
GRand *pRand = g_rand_new_with_seed(time(NULL)); 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; 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) { 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")); //GtkWidget *view = GTK_WIDGET(gtk_builder_get_object(g_mainBuilder, "treeResult"));
details_wnd_create(g_mainBuilder); details_wnd_create(g_mainBuilder);
//hex_wnd_create(g_mainBuilder);
#if 0 #if 0
gtk_list_store_new(NUM_COLS, gtk_list_store_new(NUM_COLS,
G_TYPE_INT, G_TYPE_INT,

View File

@ -16,34 +16,40 @@ static char *g_pNicName = NULL;
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->hdr.eth.h_dest, 0xFF, ETH_ALEN);
// 源 MAC 地址 // 源 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 // 协议 VLAN
p->vlan_hdr.eth.h_proto = htons(ETH_P_8021Q); p->hdr.eth.h_proto = htons(ETH_P_8021Q);
// VLAN 隧道信息 // VLAN 隧道信息
p->vlan_hdr.vlan.id = htons(pInfo->vni); p->hdr.vlan.id = htons(pInfo->vni);
p->vlan_hdr.vlan.type = htons(ETH_P_IP); p->hdr.vlan.type = htons(ETH_P_IP);
#else
// 协议 VLAN
p->hdr.eth.h_proto = htons(ETH_P_IP);
#endif
// IP 头 // IP 头
p->vlan_hdr.ip.version = IPVERSION; p->hdr.ip.version = IPVERSION;
p->vlan_hdr.ip.ihl = 5; p->hdr.ip.ihl = 5;
p->vlan_hdr.ip.tos = 0x10; p->hdr.ip.tos = 0;
p->vlan_hdr.ip.tot_len = 0; p->hdr.ip.tot_len = 0;
p->vlan_hdr.ip.id = 0; p->hdr.ip.id = 0;
p->vlan_hdr.ip.frag_off = 0; p->hdr.ip.frag_off = 0;
p->vlan_hdr.ip.ttl = 128; p->hdr.ip.ttl = 128;
p->vlan_hdr.ip.protocol = IPPROTO_UDP; p->hdr.ip.protocol = IPPROTO_UDP;
p->vlan_hdr.ip.check = 0; p->hdr.ip.check = 0;
p->vlan_hdr.ip.saddr = INADDR_ANY; p->hdr.ip.saddr = INADDR_ANY;
p->vlan_hdr.ip.daddr = INADDR_BROADCAST; p->hdr.ip.daddr = INADDR_BROADCAST;
// UDP 头 // UDP 头
p->vlan_hdr.udp.source = htons(DHCP_CLI_PORT); p->hdr.udp.source = htons(DHCP_CLI_PORT);
p->vlan_hdr.udp.dest = htons(DHCP_SVR_PORT); p->hdr.udp.dest = htons(DHCP_SVR_PORT);
p->vlan_hdr.udp.len = 0; p->hdr.udp.len = 0;
p->vlan_hdr.udp.check = 0; p->hdr.udp.check = 0;
// DHCP 协议内容 // DHCP 协议内容
p->dhcp.op = BOOTP_REQUEST; 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) { static void cacl_package_checksum(PDHCP_PACKAGE p, int tolSize) {
U16 csum; U16 csum;
// 计算 IP 数据长度 // 计算 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 数据长度 // 计算 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 校验和 // 计算 IP 校验和
csum = htons(ip_checksum((unsigned char *)&p->vlan_hdr.ip)); csum = htons(ip_checksum((unsigned char *)&p->hdr.ip));
p->vlan_hdr.ip.check = htons(csum); p->hdr.ip.check = htons(csum);
// 计算 UDP 校验和 // 计算 UDP 校验和
csum = htons(udp_checksum(p->vlan_hdr.ip.saddr, p->vlan_hdr.ip.daddr, (unsigned char *)&p->vlan_hdr.udp)); csum = htons(udp_checksum(p->hdr.ip.saddr, p->hdr.ip.daddr, (unsigned char *)&p->hdr.udp));
p->vlan_hdr.udp.check = htons(csum); p->hdr.udp.check = htons(csum);
} }
U8 *dhcp_create_request_req(PDHCP_INFO pInfo, int *pOutSize) { 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; return ERR_SUCCESS;
} }
#if VLAN_SUPPORT
init_filter("vlan and udp and dst port 68"); 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); ret = create_udp_raw_socket(g_pNicName);
if (ret != ERR_SUCCESS) { if (ret != ERR_SUCCESS) {

View File

@ -13,21 +13,27 @@ extern "C" {
#include <linux/if_ether.h> #include <linux/if_ether.h>
#include "common.h" #include "common.h"
#define VLAN_SUPPORT (1)
#pragma pack(push) #pragma pack(push)
#pragma pack(1) #pragma pack(1)
#define VXLAN_VIN_ID_PACK(x) ((ntohs(x)) & 0xFFF) #define VXLAN_VIN_ID_PACK(x) ((ntohs(x)) & 0xFFF)
typedef struct vlan_hdr { typedef struct vlan_hdr {
#if VLAN_SUPPORT
U16 id; U16 id;
U16 type; U16 type;
#endif
} VLAN_HDR, *PVLAN_HDR; } VLAN_HDR, *PVLAN_HDR;
typedef struct { typedef struct {
struct ethhdr eth; struct ethhdr eth;
#if VLAN_SUPPORT
struct vlan_hdr vlan; struct vlan_hdr vlan;
struct iphdr ip; #endif
struct udphdr udp; struct iphdr ip;
struct udphdr udp;
} VLAN_PKG_HDR, *PVLAN_PKG_HDR; } VLAN_PKG_HDR, *PVLAN_PKG_HDR;
#pragma pack(pop) #pragma pack(pop)

View File

@ -32,10 +32,10 @@
static pid_t g_pid; static pid_t g_pid;
static int g_isInited = FALSE; 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()) { if (g_pid == uv_os_getpid()) {
printf("\n"); 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(); task_manager_exit();
sleep(1); sleep(1);
} }
@ -56,7 +56,7 @@ int user_init(const char *pAppCfgFile, const char *pCfgDirectory, const char *pK
signal(SIGQUIT, catch_system_interupt); signal(SIGQUIT, catch_system_interupt);
signal(SIGTSTP, catch_system_interupt); signal(SIGTSTP, catch_system_interupt);
signal(SIGHUP, SIG_IGN); signal(SIGHUP, SIG_IGN);
signal(SIGPIPE, catch_system_interupt); signal(SIGPIPE, SIG_IGN);
signal(SIGKILL, catch_system_interupt); signal(SIGKILL, catch_system_interupt);
// 初始化 libuv loop // 初始化 libuv loop
@ -107,16 +107,8 @@ int user_init(const char *pAppCfgFile, const char *pCfgDirectory, const char *pK
return -ERR_CONFIG_INIT; return -ERR_CONFIG_INIT;
} }
LOG_MOD(info, LOG_MOD(info, ZLOG_MOD_INIT, "%s library version %s information: %s (Build: %s %s GCC Ver:%s) With %lu(bits) OS\n",
ZLOG_MOD_INIT, VCPE_LIB_NAME, VCPE_LIB_VER, VCPE_GIT_VERSION, __DATE__, __TIME__, __VERSION__, sizeof(int *) * 8);
"%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); LOG_MOD(info, ZLOG_MOD_INIT, "Application build configure: [%s]\n", VCPE_BUILD_CONFIG);

View File

@ -187,40 +187,41 @@ static void fill_package(PDHCP_PACKAGE pRsp, PDHCP_PACKAGE pReq) {
// 二层头 // 二层头
// 目的IP地址 // 目的IP地址
if (pReq->vlan_hdr.ip.saddr != 0) { if (pReq->hdr.ip.saddr != 0) {
memcpy(pRsp->vlan_hdr.eth.h_dest, pRsp->vlan_hdr.eth.h_source, ETH_ALEN); memcpy(pRsp->hdr.eth.h_dest, pRsp->hdr.eth.h_source, ETH_ALEN);
} else { } else {
memset(pRsp->vlan_hdr.eth.h_dest, 0xFF, ETH_ALEN); memset(pRsp->hdr.eth.h_dest, 0xFF, ETH_ALEN);
} }
// 源 IP 地址 // 源 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 // 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 隧道 // QinQ 隧道
pRsp->vlan_hdr.vlan.id = pReq->vlan_hdr.vlan.id; pRsp->hdr.vlan.id = pReq->hdr.vlan.id;
pRsp->vlan_hdr.vlan.type = pReq->vlan_hdr.vlan.type; pRsp->hdr.vlan.type = pReq->hdr.vlan.type;
// TODO 可能的二层QinQ隧道 // TODO 可能的二层QinQ隧道
#endif
// IP 头 // IP 头
memcpy(&pRsp->vlan_hdr.ip, &pReq->vlan_hdr.ip, sizeof(struct iphdr)); memcpy(&pRsp->hdr.ip, &pReq->hdr.ip, sizeof(struct iphdr));
// TOS // TOS
pRsp->vlan_hdr.ip.tos = 0; pRsp->hdr.ip.tos = 0;
// 更新源IP // 更新源IP
pRsp->vlan_hdr.ip.saddr = g_nicInfo.ipAddr; pRsp->hdr.ip.saddr = g_nicInfo.ipAddr;
// 更新目的IP地址广播255.255.255.255 // 更新目的IP地址广播255.255.255.255
if (pReq->vlan_hdr.ip.saddr == 0) { if (pReq->hdr.ip.saddr == 0) {
pRsp->vlan_hdr.ip.daddr = 0xFFFFFFFF; pRsp->hdr.ip.daddr = 0xFFFFFFFF;
} else { } else {
pRsp->vlan_hdr.ip.daddr = pReq->vlan_hdr.ip.saddr; pRsp->hdr.ip.daddr = pReq->hdr.ip.saddr;
} }
// UDP 头 // 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 协议 //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)); tolSize = (int)((pOpt - pRsp->dhcp.options) + 1 + sizeof(DHCP_PACKAGE));
// 计算 IP 数据长度 // 计算 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 数据长度 // 计算 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 校验和 // 计算 IP 校验和
csum = htons(ip_checksum((unsigned char *)&pRsp->vlan_hdr.ip)); csum = htons(ip_checksum((unsigned char *)&pRsp->hdr.ip));
pRsp->vlan_hdr.ip.check = htons(csum); pRsp->hdr.ip.check = htons(csum);
// 计算 UDP 校验和 // 计算 UDP 校验和
csum = htons(udp_checksum(pRsp->vlan_hdr.ip.saddr, pRsp->vlan_hdr.ip.daddr, (unsigned char *)&pRsp->vlan_hdr.udp)); csum = htons(udp_checksum(pRsp->hdr.ip.saddr, pRsp->hdr.ip.daddr, (unsigned char *)&pRsp->hdr.udp));
pRsp->vlan_hdr.udp.check = htons(csum); 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, "OPTIONS size: %ld\n", (intptr_t)(pOpt - pRsp->dhcp.options) + 1);
LOG_MOD(trace, ZM_DHCP_NET, "Total size: %d\n", tolSize); 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; PDHCP_PACKAGE pkg = (PDHCP_PACKAGE)pWork->pPkgBase;
U32 optSize = pWork->nSize - sizeof(DHCP_PACKAGE); 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 // Check op flag
if (pkg->dhcp.op != BOOTP_REQUEST) { 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)); memset(&reqDhcp, 0, sizeof(DHCP_REQ));
reqDhcp.unicast = pkg->dhcp.flags != 0 ? TRUE : FALSE; reqDhcp.unicast = pkg->dhcp.flags != 0 ? TRUE : FALSE;
reqDhcp.xid = DHCP_XID(pkg->dhcp.xid); reqDhcp.xid = DHCP_XID(pkg->dhcp.xid);
reqDhcp.uid = VLAN_VNI_ID(pkg->vlan_hdr.vlan.id); #if VLAN_SUPPORT
reqDhcp.uid = VLAN_VNI_ID(pkg->hdr.vlan.id);
#else
reqDhcp.uid = 0;
#endif
reqDhcp.serverAddr = g_nicInfo.ipAddr; reqDhcp.serverAddr = g_nicInfo.ipAddr;
reqDhcp.cliAddr = ntohl(pkg->dhcp.ciaddr); reqDhcp.cliAddr = ntohl(pkg->dhcp.ciaddr);
memcpy(reqDhcp.cliMac, pkg->dhcp.chaddr, ETH_ALEN); 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)); reqDhcp.reqServer = ntohl(*((U32 *)opt.pValue));
} else { } else {
// Request 服务器IP可以取目的 IP 地址 // Request 服务器IP可以取目的 IP 地址
if (pkg->vlan_hdr.ip.saddr != 0) { if (pkg->hdr.ip.saddr != 0) {
reqDhcp.reqServer = ntohl(pkg->vlan_hdr.ip.daddr); 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)); reqDhcp.reqServer = ntohl(*((U32 *)opt.pValue));
} else { } else {
// Request 服务器IP可以取目的 IP 地址 // Request 服务器IP可以取目的 IP 地址
if (pkg->vlan_hdr.ip.saddr != 0) { if (pkg->hdr.ip.saddr != 0) {
reqDhcp.reqServer = ntohl(pkg->vlan_hdr.ip.saddr); reqDhcp.reqServer = ntohl(pkg->hdr.ip.saddr);
} }
} }
@ -577,22 +582,22 @@ static void on_sock_recv(uv_work_t *req) {
break; 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_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(info, "vni: %d, xid: 0x%08X\n", VLAN_VNI_ID(pkg->hdr.vlan.id), DHCP_XID(pkg->dhcp.xid));
#if 0 #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, "xid: 0x%08X\n", ntohl(pkg->dhcp.xid));
LOG_MSG(info, LOG_MSG(info,
"dest mac: %02X:%02X:%02X:%02X:%02X:%02X\n", "dest mac: %02X:%02X:%02X:%02X:%02X:%02X\n",
pkg->vlan_hdr.eth.h_dest[0], pkg->hdr.eth.h_dest[0],
pkg->vlan_hdr.eth.h_dest[1], pkg->hdr.eth.h_dest[1],
pkg->vlan_hdr.eth.h_dest[2], pkg->hdr.eth.h_dest[2],
pkg->vlan_hdr.eth.h_dest[3], pkg->hdr.eth.h_dest[3],
pkg->vlan_hdr.eth.h_dest[4], pkg->hdr.eth.h_dest[4],
pkg->vlan_hdr.eth.h_dest[5]); pkg->hdr.eth.h_dest[5]);
LOG_MSG(info, LOG_MSG(info,
"client mac: %02X:%02X:%02X:%02X:%02X:%02X\n", "client mac: %02X:%02X:%02X:%02X:%02X:%02X\n",
pkg->dhcp.chaddr[0], 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].uvWork.data = &pMsg->pPkgInfo[i];
pMsg->pPkgInfo[i].pData = pMsg; pMsg->pPkgInfo[i].pData = pMsg;
if (g_dhcpMode == MODE_DHCP_SERVER) { if (g_dhcpMode == MODE_DHCP_SERVER) {
pkg = (PDHCP_PACKAGE)pMsg->pPkgInfo[i].pPkgBase; #if VLAN_SUPPORT
uid = VLAN_VNI_ID(pkg->vlan_hdr.vlan.id); 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); 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), // LOG_MOD(trace, ZM_DHCP_NET, ">>> User %u xid %08X addr %p\n", uid, DHCP_XID(pkg->dhcp.xid),

View File

@ -53,7 +53,7 @@ typedef struct {
#pragma pack(1) #pragma pack(1)
typedef struct { typedef struct {
VLAN_PKG_HDR vlan_hdr; VLAN_PKG_HDR hdr;
DHCP_PROTO dhcp; DHCP_PROTO dhcp;
} DHCP_PACKAGE, *PDHCP_PACKAGE; } DHCP_PACKAGE, *PDHCP_PACKAGE;