diff --git a/srcs/service/dhcpd/dhcpd_network.c b/srcs/service/dhcpd/dhcpd_network.c index e6f7c61..a13ac26 100644 --- a/srcs/service/dhcpd/dhcpd_network.c +++ b/srcs/service/dhcpd/dhcpd_network.c @@ -227,7 +227,7 @@ static PACKET_MMAP_RING g_pkgRing; static NIC_INFO g_nicInfo; static uv_udp_t g_uvRawSockReq; -U32 pkg_mmap_tx(U8 *pData, U32 nBytes) { +void *get_pkg_memory() { int i; ssize_t ret; struct tpacket3_hdr *hdr; @@ -237,24 +237,28 @@ U32 pkg_mmap_tx(U8 *pData, U32 nBytes) { g_pkgRing.index = (g_pkgRing.index + 1) % g_pkgRing.send.tp_frame_nr; if (!(hdr->tp_status & (TP_STATUS_SEND_REQUEST | TP_STATUS_SENDING))) { - U8 *p = (U8 *)hdr + TPACKET3_HDRLEN - sizeof(struct sockaddr_ll); - memcpy(p, pData, nBytes); - hdr->tp_next_offset = 0; - hdr->tp_len = nBytes; - hdr->tp_snaplen = nBytes; - hdr->tp_status = TP_STATUS_SEND_REQUEST; - return hdr->tp_len; + return (U8 *)hdr + TPACKET3_HDRLEN - sizeof(struct sockaddr_ll); } } - return 0; + return NULL; +} + +U32 pkg_mmap_tx(U8 *pData, U32 nBytes) { + 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; + return hdr->tp_len; } static int dhcp_resp_offer(PDHCP_PACKAGE pReq, PIPPOOL_INFO pIpInfo, U32 ip) { + U8 *pOpt; U16 csum; int tolSize; - PDHCP_PACKAGE pRsp = (PDHCP_PACKAGE)malloc(MAX_DHCP_PKG_SIZE); - U8 *pOpt = pRsp->dhcp.options; + PDHCP_PACKAGE pRsp = get_pkg_memory(); if (pRsp == NULL) { LOG_MOD(error, ZLOG_MOD_DHCPD, "Malloc memory error: %u\n", MAX_DHCP_PKG_SIZE); @@ -319,6 +323,8 @@ static int dhcp_resp_offer(PDHCP_PACKAGE pReq, PIPPOOL_INFO pIpInfo, U32 ip) { pRsp->dhcp.giaddr = 0; // DHCP Options + pOpt = pRsp->dhcp.options; + // DHCP 消息类型 pOpt += dhcp_add_u8_option(pOpt, OPT_MESSAGETYPE, DHCP_MSG_OFFER); // 子网掩码