使用C程序发送自定义的MDNS请求帧

本文最后更新于 2025年5月21日 下午

要求:为了通过C编程发送mdns帧请求测试模块是否正常工作,特编写此程序,可以用于模拟多种压力测试场景。

Linux Socket C编程相较于Python编程,操作更接近底层硬件,可以显著提高测试帧的发送速率

程序实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/time.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <net/ethernet.h>
#include <netpacket/packet.h>
#include <net/if.h>
#include <sys/ioctl.h>
#include <arpa/inet.h>

// Ethernet definitions
#define ETHERTYPE_IPV4 0x0800
#define LLC_ETHER_HDR_LEN 14
#define MAC_ADDR_LEN 6
#define MDNS_RESPONSE_FRAME_LEN_MAX 1000

// IP definitions
#define IPV4_HEADER_LEN 20
#define IPV4_ADDR_LEN 4
#define IPV6_HEADER_LEN 40
#define IPV6_ADDR_LEN 16

// UDP definitions
#define UDP_SRC_PORT 1234
#define UDP_DEST_PORT 1234
#define UDP_HDR_LEN 8
#define UDP_IPPROTO 17

// MDNS definitions
#define MDNS_SRC_PORT 5353
#define MDNS_DEST_PORT 5353
#define MDNS_HDR_LEN 12
#define MDNS_QNAME_LENGTH_MAX 80

// Payload definitions
#define payload_LENGTH_MAX 50

enum mdns_type {
// a host address
MDNS_TYPE_A = 1,
// an authoritative name server
MDNS_TYPE_NS = 2,
// a mail destination (Obsolete - use MX)
MDNS_TYPE_MD = 3,
// a mail forwarder (Obsolete - use MX)
MDNS_TYPE_MF = 4,
// the canonical name for an alias
MDNS_TYPE_CNAME = 5,
// marks the start of a zone of authority
MDNS_TYPE_SOA = 6,
// a mailbox domain name (EXPERIMENTAL)
MDNS_TYPE_MB = 7,
// a mail group member (EXPERIMENTAL)
MDNS_TYPE_MG = 8,
// a mail rename domain name (EXPERIMENTAL)
MDNS_TYPE_MR = 9,
// a null RR (EXPERIMENTAL)
MDNS_TYPE_NULL = 10,
// a well known service description
MDNS_TYPE_WKS = 11,
// a domain name pointer
MDNS_TYPE_PTR = 12,
// host information
MDNS_TYPE_HINFO = 13,
// mailbox or mail list information
MDNS_TYPE_MINFO = 14,
// mail exchange
MDNS_TYPE_MX = 15,
// text strings
MDNS_TYPE_TXT = 16,

// type ANY
MDNS_TYPE_ANY = 255,
};

struct ethernet_header {
uint8_t dst[MAC_ADDR_LEN];
uint8_t src[MAC_ADDR_LEN];
uint16_t type;
};

struct ipv4_header {
uint8_t header_len:4,
version:4;
uint8_t tos;
uint16_t tot_len;
uint16_t id;
uint16_t frag_off;
uint8_t ttl;
uint8_t protocol;
uint16_t checksum;
uint8_t src_ip_addr[IPV4_ADDR_LEN];
uint8_t dst_ip_addr[IPV4_ADDR_LEN];
};

struct udp_header {
uint16_t src_port;
uint16_t dst_port;
uint16_t len;
uint16_t checksum;
};

struct mdns_header {
uint16_t trans_id;
uint16_t flags;
uint16_t questions;
uint16_t answer_rrs;
uint16_t auth_rrs;
uint16_t additional_rrs;
};

struct mdns_queries {
uint16_t qtype;
uint16_t qclass;
};

// ipv4 mulit mac dst addr 01:00:5e:00:00:fb
const uint8_t mulit_ipv4_mac_dst_addr[MAC_ADDR_LEN] = {
0x01, 0x00, 0x5e, 0x00, 0x00, 0xfb
};

// ipv4 mac src addr 08:00:27:89:f4:b4
uint8_t ipv4_mac_src_addr[MAC_ADDR_LEN] = {
0x08, 0x00, 0x27, 0x89, 0xf4, 0xb4
};

// ipv4 mulit ip dst addr 224.0.0.251
const uint8_t mulit_ipv4_ip_dst_addr[IPV4_ADDR_LEN] = {
0xe0, 0x00, 0x00, 0xfb
};

// ipv4 ip src addr 192.168.1.1
uint8_t ipv4_ip_src_addr[IPV4_ADDR_LEN] = {
192, 168, 1, 1
};

static uint16_t co_htons(uint16_t value)
{
return ((value << 8) & 0xFF00) | ((value >> 8) & 0xFF);
}

static uint16_t co_ntohs(uint16_t value)
{
return co_htons(value);
}

static void mem_dump(uint8_t *addr, uint16_t len)
{
uint16_t i;
uint8_t *c;

if (!addr)
return;

c = (uint8_t *)addr;
printf("Memory Dump Addr: %p, Size: %d:\n", addr, len);
for (i = 0; i < len; i++) {
printf("%02x ", *(c + i));
if (((i + 1) & 0x0f) == 0)
printf("\r\n");
else if (((i + 1) & 0x07) == 0)
printf(" ");
}
printf("\r\n");
}

static uint32_t compute_ip_checksum(const void *vptr, uint32_t nbytes)
{
uint32_t sum;
uint16_t oddbyte;
const uint16_t *ptr = vptr;

sum = 0;
while (nbytes > 1) {
sum += *ptr++;
nbytes -= 2;
}
if (nbytes == 1) {
oddbyte = 0;
((uint8_t *)&oddbyte)[0] = *(uint8_t *)ptr;
((uint8_t *)&oddbyte)[1] = 0;
sum += oddbyte;
}
sum = (sum >> 16) + (sum & 0xffff);
sum += (sum >> 16);
sum = ~sum & 0xffff;

return sum;
}

static void build_dns_name(const char *domain, char *buf) {
const char *start = domain;
const char *end;
char *p = buf;

while (*domain) {
end = strchr(domain, '.');
if (!end)
end = domain + strlen(domain);

*p++ = end - domain;
memcpy(p, domain, end - domain);
p += end - domain;

if (*end == '.')
domain = end + 1;
else
domain = end;
}

*p = '\0';
}

static int32_t build_mdns_query_frame(uint8_t *txframe, enum mdns_type mtype)
{
uint16_t offset;
uint16_t udp_len, mdns_len;
struct ethernet_header *llc_eth_hdr;
struct ipv4_header *ipv4_hdr;
struct udp_header *udp;
struct mdns_header *mdns;
struct mdns_queries *question;
const char *target_domain = "_googlecast._tcp.local";
char dns_name[MDNS_QNAME_LENGTH_MAX];

// ethernet
offset = 0;
llc_eth_hdr = (struct ethernet_header *)txframe;
memcpy(llc_eth_hdr->dst, mulit_ipv4_mac_dst_addr, MAC_ADDR_LEN);
memcpy(llc_eth_hdr->src, ipv4_mac_src_addr, MAC_ADDR_LEN);
llc_eth_hdr->type = co_htons(ETHERTYPE_IPV4);
offset += LLC_ETHER_HDR_LEN;

// ip
ipv4_hdr = (struct ipv4_header *)(txframe + offset);
offset += IPV4_HEADER_LEN;

// udp
udp = (struct udp_header *)(txframe + offset);
udp->dst_port = co_htons(MDNS_SRC_PORT);
udp->src_port = co_htons(MDNS_DEST_PORT);
udp_len = UDP_HDR_LEN;
udp->len = co_htons(udp_len);
offset += UDP_HDR_LEN;

// mdns
mdns = (struct mdns_header *)(txframe + offset);
mdns->trans_id = htons(0);
mdns->flags = htons(0x0000);
mdns->questions = htons(1);
mdns->answer_rrs = htons(0);
mdns->auth_rrs = htons(0);
mdns->additional_rrs = htons(0);
build_dns_name(target_domain, dns_name);
memcpy((uint8_t *)mdns + MDNS_HDR_LEN, dns_name, strlen(dns_name) + 1);
question = (struct mdns_queries *)(txframe + offset + \
MDNS_HDR_LEN + strlen(dns_name) + 1);
question->qtype = htons(mtype);
question->qclass = htons(1);
mdns_len = MDNS_HDR_LEN + strlen(dns_name) + 1 + \
sizeof(struct mdns_queries);
offset += mdns_len;
udp_len = UDP_HDR_LEN + mdns_len;
udp->len = co_htons(udp_len);

// build struct ipv4_header
ipv4_hdr->version = 0x4;
ipv4_hdr->header_len = 0x5;
ipv4_hdr->tos = 0x00;
ipv4_hdr->tot_len = co_htons(udp_len + IPV4_HEADER_LEN);
ipv4_hdr->id = co_htons(0x0000);
ipv4_hdr->frag_off = co_htons(0x0000);
ipv4_hdr->ttl = 0x00;
ipv4_hdr->protocol = UDP_IPPROTO;
ipv4_hdr->checksum = co_htons(0x0000);
memcpy(ipv4_hdr->src_ip_addr, ipv4_ip_src_addr, IPV4_ADDR_LEN);
memcpy(ipv4_hdr->dst_ip_addr, mulit_ipv4_ip_dst_addr, IPV4_ADDR_LEN);

// UDP checksum
ipv4_hdr->checksum = udp->len;
ipv4_hdr->ttl = 0x00;
udp->checksum = co_htons(0x0000);
udp->checksum = compute_ip_checksum(((uint8_t *)udp - 12), udp_len + 12);

// IPv4 checksum
ipv4_hdr->checksum = co_htons(0x0000);
ipv4_hdr->ttl = 0xff;
ipv4_hdr->checksum = compute_ip_checksum(ipv4_hdr, IPV4_HEADER_LEN);

return offset;
}

static int32_t build_udp_frame(uint8_t *txframe)
{
uint16_t offset;
uint16_t udp_len;
struct ethernet_header *llc_eth_hdr;
struct ipv4_header *ipv4_hdr;
struct udp_header *udp;
uint8_t payload[payload_LENGTH_MAX];

// ethernet
offset = 0;
llc_eth_hdr = (struct ethernet_header *)txframe;
memcpy(llc_eth_hdr->dst, mulit_ipv4_mac_dst_addr, MAC_ADDR_LEN);
memcpy(llc_eth_hdr->src, ipv4_mac_src_addr, MAC_ADDR_LEN);
llc_eth_hdr->type = co_htons(ETHERTYPE_IPV4);
offset += LLC_ETHER_HDR_LEN;

// ip
ipv4_hdr = (struct ipv4_header *)(txframe + offset);
offset += IPV4_HEADER_LEN;

// udp
udp = (struct udp_header *)(txframe + offset);
udp->dst_port = co_htons(1234);
udp->src_port = co_htons(1234);
udp_len = UDP_HDR_LEN;
udp->len = co_htons(udp_len);
offset += UDP_HDR_LEN;

// build struct ipv4_header
ipv4_hdr->version = 0x4;
ipv4_hdr->header_len = 0x5;
ipv4_hdr->tos = 0x00;
ipv4_hdr->tot_len = co_htons(udp_len + IPV4_HEADER_LEN);
ipv4_hdr->id = co_htons(0x0000);
ipv4_hdr->frag_off = co_htons(0x0000);
ipv4_hdr->ttl = 0x00;
ipv4_hdr->protocol = UDP_IPPROTO;
ipv4_hdr->checksum = co_htons(0x0000);
memcpy(ipv4_hdr->src_ip_addr, ipv4_ip_src_addr, IPV4_ADDR_LEN);
memcpy(ipv4_hdr->dst_ip_addr, mulit_ipv4_ip_dst_addr, IPV4_ADDR_LEN);

// UDP checksum
ipv4_hdr->checksum = udp->len;
ipv4_hdr->ttl = 0x00;
udp->checksum = co_htons(0x0000);
udp->checksum = compute_ip_checksum(((uint8_t *)udp - 12), udp_len + 12);

// IPv4 checksum
ipv4_hdr->checksum = co_htons(0x0000);
ipv4_hdr->ttl = 0xff;
ipv4_hdr->checksum = compute_ip_checksum(ipv4_hdr, IPV4_HEADER_LEN);

// payload
memset(payload, 0xFD, sizeof(payload));
memcpy((uint8_t *)txframe + offset, payload, sizeof(payload));
offset += sizeof(payload);

return offset;
}

int send_frame(char *ifname, long long count, long long mdns,
float inter, int scene, int verbose)
{
int sockfd;
struct ifreq if_idx, if_mac, ifr;
struct sockaddr_in *sin;
struct sockaddr_ll socket_address;
char *buffer_normal, *buffer_mdns;
int frame_normal_len, frame_mdns_len;
long long i, j;
struct timeval start, end;
long long msec;
float sec;

sockfd = socket(AF_PACKET, SOCK_RAW, htons(ETHERTYPE_IPV4));
if (sockfd == -1) {
perror("socket error");
return -1;
}

memset(&if_idx, 0, sizeof(struct ifreq));
strncpy(if_idx.ifr_name, ifname, IFNAMSIZ-1);
if (ioctl(sockfd, SIOCGIFINDEX, &if_idx) < 0)
perror("SIOCGIFINDEX");

memset(&if_mac, 0, sizeof(struct ifreq));
strncpy(if_mac.ifr_name, ifname, IFNAMSIZ-1);
if (ioctl(sockfd, SIOCGIFHWADDR, &if_mac) < 0)
perror("SIOCGIFHWADDR");

memset(&ifr, 0, sizeof(struct ifreq));
strncpy(ifr.ifr_name, ifname, IFNAMSIZ-1);
if (ioctl(sockfd, SIOCGIFADDR, &ifr) < 0)
perror("SIOCGIFHWADDR");

sin = (struct sockaddr_in *)&ifr.ifr_addr;
memcpy(ipv4_ip_src_addr, &sin->sin_addr.s_addr, IPV4_ADDR_LEN);
if (verbose)
mem_dump(ipv4_ip_src_addr, IPV4_ADDR_LEN);

memcpy(ipv4_mac_src_addr, if_mac.ifr_hwaddr.sa_data, MAC_ADDR_LEN);
if (verbose)
mem_dump(ipv4_mac_src_addr, MAC_ADDR_LEN);

// normal frame
buffer_normal = (char *)malloc(sizeof(char) * 1500);
if (!buffer_normal) {
perror("malloc error");
close(sockfd);
return -1;
}
memset(buffer_normal, 0, 1500);
switch (scene) {
case 0:
frame_normal_len = build_udp_frame(buffer_normal);
break;
case 1:
frame_normal_len = build_mdns_query_frame(buffer_normal, MDNS_TYPE_NULL);
break;
case 2:
frame_normal_len = build_mdns_query_frame(buffer_normal, MDNS_TYPE_PTR);
break;
}
if (verbose)
mem_dump(buffer_normal, frame_normal_len);

// mdns frame
if (mdns > 0) {
buffer_mdns = (char *)malloc(sizeof(char) * 1500);
if (!buffer_mdns) {
perror("malloc buffer_mdns error");
free(buffer_normal);
close(sockfd);
return -1;
}
memset(buffer_mdns, 0, 1500);
frame_mdns_len = build_mdns_query_frame(buffer_mdns, MDNS_TYPE_PTR);
if (verbose)
mem_dump(buffer_mdns, frame_mdns_len);
}

socket_address.sll_ifindex = if_idx.ifr_ifindex;
socket_address.sll_halen = ETH_ALEN;
memset(socket_address.sll_addr, 0xff, MAC_ADDR_LEN);

gettimeofday(&start, NULL);
for (i=0; i < count; i++) {
if (sendto(sockfd, buffer_normal, frame_normal_len, 0,
(struct sockaddr*)&socket_address,
sizeof(struct sockaddr_ll)) < 0) {
perror("socket sendto buffer_normal occur error\n");
break;
}
if (inter > 0)
usleep(inter * 1000 * 1000);
}

for (j=0; j < mdns; j++) {
if (sendto(sockfd, buffer_mdns, frame_mdns_len, 0,
(struct sockaddr*)&socket_address,
sizeof(struct sockaddr_ll)) < 0) {
perror("socket sendto buffer_mdns occur error\n");
break;
}
if (inter > 0)
usleep(inter * 1000 * 1000);
}

gettimeofday(&end, NULL);

msec = (end.tv_sec - start.tv_sec) * 1000LL + \
(end.tv_usec - start.tv_usec) / 1000LL;

if (i >= count && j >= mdns)
printf("run time: %lldms, %.3fs, tx rate: %lld frame/s\n",
msec, msec * 0.001, (count + mdns) * 1000LL / msec);

free(buffer_mdns);
free(buffer_normal);
close(sockfd);
return 0;
}

int main(int argc, char *argv[])
{
char ifname[10] = "enp0s3";
long long count = 8000;
long long mdns = 0;
float inter = 0.5;
int verbose = 0;
int scene = 0;
int c;

while ((c = getopt(argc, argv, "hi:c:m:r:s:v:")) != -1) {
switch (c) {
case 'h':
printf("Usage:\n");
printf("\t-h show help information\n");
printf("\t-i the network interface name, example: eth0\n");
printf("\t-c the count of send normal frame, example: 8000\n");
printf("\t-m the count of send mdns frame, example: 2\n");
printf("\t-r the interval seconds of send frame, example: 0.01\n");
printf("\t-s the scenarios of send frame, option: [0, 1, 2]\n");
printf("\t-v show more information, option: [0, 1]\n");
exit(0);
case 'i':
memset(ifname, 0, 10);
strncpy(ifname, optarg, strlen(optarg));
break;
case 'c':
count = atoll(optarg);
break;
case 'm':
mdns = atoi(optarg);
break;
case 'r':
inter = atof(optarg);
break;
case 's':
scene = atoi(optarg);
break;
case 'v':
verbose = atoi(optarg);
break;
case '?':
break;
}
}
if (verbose) {
printf("Program running parameters:\n");
printf("\t-i ifname: %s\n", ifname);
printf("\t-c count: %lld\n", count);
printf("\t-m mdns: %lld\n", mdns);
printf("\t-r inter: %f\n", inter);
printf("\t-s scene: %d\n", scene);
printf("\t-v verbose: %d\n", verbose);
}
send_frame(ifname, count, mdns, inter, scene, verbose);
return 0;
}

使用方法

(1)编译

在Linux系统上(例如ubuntu 22.04)使用gcc编译

1
gcc -o frame_test frame_test.c

(2)参数解释

运行frame_test程序的参数解释如下

1
2
3
4
5
6
7
8
9
10
11
12
13
Usage:
-h 显示帮助信息
-i 指定网络接口名称, 例如: eth0
-c 发送普通帧的数量, 例如: 8000
-m 发送mdns帧的数量, 例如: 2
-r 发送帧的间隔,以秒为单位,最小值为0.000001, 例如: 0.01
-s 发送帧的测试场景, 可选的: [0, 1, 2]
0:指定普通帧为udp帧
1:指定普通帧为mdns请求帧(type为10)
2:指定普通帧为mdns请求帧(type为12)
-v 是否显示更多的调试信息, 可选的: [0, 1]
0:不显示
1:显示

(3)运行程序

例如,运行以下命令

1
./frame_test -i eth0 -c 10 -m 1 -r 0.01 -s 1 -v 1

上述命令表示:

  • 指定帧发送接口为”eth0”
  • 发送测试帧数量10
  • 发送mdns请求帧数量1
  • 发送帧间隔0.01秒
  • 测试场景为1,即测试帧为type不同的mdns请求帧
  • 显示更多提示信息

使用C程序发送自定义的MDNS请求帧
https://www.bitzerone.com/2025/05/17/linux-mdns/
作者
酸菜肉丝
发布于
2025年5月17日
更新于
2025年5月21日
许可协议