利用Libnet开发包构造DNS响应报文?

1.有没有大佬会用Libnet网络开发包构造DNS响应报文啊??求助
现在在DNS响应报文的负载部分没搞懂,想填充回答部分的时候,用Wireshark抓到的包总显示格式错误。

图片说明
下面是我自己写的构造响应报文的代码:
#include "libnet.h"
#include "./libnet/libnet-functions.h"
#include "/usr/local/include/libnet/libnet-functions.h"
int main(int argc, char *argv[])
{
char payload1[512]="";
int payload_s1=0;
char payload2[512]="";
int payload_s2=0;
char payload[512]="";
int payload_s=0;

// char payload[1024]="";
// int payload_s=0;

char err_buf[100] = "";
libnet_t *lib_net=NULL;//声明一个lib_net句柄

char query[]="www.baidu.com";
char answer[]="172.18.246.34";

libnet_ptag_t dns=0;//协议标记
libnet_ptag_t udp=0;//协议标记
libnet_ptag_t ip=0;//协议标记
libnet_ptag_t ethernet=0;//协议标记

unsigned char src_mac[6] = {0xf8,0xe9,0x03,0xb7,0xee,0x40};
unsigned char dst_mac[6] = {0x4c,0x34,0x88,0xda,0xb3,0x0d};
char *src_ip_str = "192.168.0.1"; //源主机IP地址
char *dst_ip_str = "192.168.0.108"; //目的主机IP地址
unsigned long src_ip,dst_ip = 0;

// payload_s = sprintf(payload, "%s", "this is for the test");

payload_s1 = snprintf(payload1, sizeof payload1, "%c%s%c%c%c%c%c",
                     (char)(strlen(query)&0xff), query, 0x00, 0x00, 0x01, 0x00, 0x01);
payload_s2 = snprintf(payload2, sizeof payload2, "%c%s%c%c%c%c%c%c%c%s", (char)(strlen(query)&0xff), query,
                       0x00, 0x00, 0x01, 0x00, 0x01,0x00,(char)(strlen(answer)&0xff),answer);
strcat(payload,payload1);
strcat(payload,payload2),
payload_s=payload_s1+payload_s2;

// payload_s = snprintf(payload, sizeof payload, "%c%s%c%c%c%c%c",
// (char)(strlen(query)&0xff), query, 0x00, 0x00, 0x01, 0x00, 0x01);

lib_net = libnet_init(LIBNET_LINK_ADV, "eth1", err_buf); //初始化,返回一个libnet的句柄!
if(NULL == lib_net)
{
    perror("libnet_init");
    exit(-1);
}

src_ip = libnet_name2addr4(lib_net,src_ip_str,LIBNET_RESOLVE); //将字符串类型的ip转换为顺序网络字节流
dst_ip = libnet_name2addr4(lib_net,dst_ip_str,LIBNET_RESOLVE);

dns=libnet_build_dnsv4(
        12,
        0xe62e,
        0x8180,
        1,
        1,
        0,
        0,
        payload,
        payload_s,
        lib_net,
        dns
);

udp= libnet_build_udp( //构造udp数据包
        53,
        8080,
        8+12+payload_s,
        0,
        NULL,
        0,
        lib_net,
        udp
);

ip= libnet_build_ipv4( //构造ip数据包
        20+8+12+payload_s,
        0,
        500,
        0,
        10,
        17,
        0,
        src_ip,
        dst_ip,
        NULL,
        0,
        lib_net,
        ip
);

ethernet= libnet_build_ethernet( //构造以太网数据包
        (u_int8_t *)dst_mac,
        (u_int8_t *)src_mac,
        0x800,
        NULL,
        0,
        lib_net,
        ethernet
);

int res = 0;
res = libnet_write(lib_net); //发送数据包
if(res ==-1)
{
    perror("libnet_write");
    exit(-1);
}

libnet_destroy(lib_net); //销毁资源
printf("----ok-----\n");
return 0;

}

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问