libuv-udp-dhcp

libuv udp dhcp example


更新記錄

item note
20160517 第一版

目錄


libuv udp dhcp example

範例說明

  • 參考來源
  • User Datagram Protocol 提供了无连接, 不可靠网络通信协议, 因此 libuv 并不提供流式 UDP 服务
  • 是通过 uv_udp_t 结构体(用于接收)和 uv_udp_send_t 结构体(用于发送)以及相关的函数给开发人员提供了非阻塞的 UDP 服务
  • source code
  • wireshark raw data

範例內容

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
int main() {
loop = uv_default_loop();

uv_udp_init(loop, &recv_socket);
struct sockaddr_in recv_addr;
uv_ip4_addr("0.0.0.0", 68, &recv_addr);
uv_udp_bind(&recv_socket, (const struct sockaddr *)&recv_addr, UV_UDP_REUSEADDR);
uv_udp_recv_start(&recv_socket, alloc_buffer, on_read);

uv_udp_init(loop, &send_socket);
struct sockaddr_in broadcast_addr;
uv_ip4_addr("0.0.0.0", 0, &broadcast_addr);
uv_udp_bind(&send_socket, (const struct sockaddr *)&broadcast_addr, 0);
uv_udp_set_broadcast(&send_socket, 1);

uv_udp_send_t send_req;
uv_buf_t discover_msg = make_discover_msg();

struct sockaddr_in send_addr;
uv_ip4_addr("255.255.255.255", 67, &send_addr);
uv_udp_send(&send_req, &send_socket, &discover_msg, 1, (const struct sockaddr *)&send_addr, on_send);

return uv_run(loop, UV_RUN_DEFAULT);
}

send data

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
uv_buf_t make_discover_msg() {
uv_buf_t buffer;
alloc_buffer(NULL, 256, &buffer);
memset(buffer.base, 0, buffer.len);

// BOOTREQUEST
buffer.base[0] = 0x1;
// HTYPE ethernet
buffer.base[1] = 0x1;
// HLEN
buffer.base[2] = 0x6;
// HOPS
buffer.base[3] = 0x0;
// XID 4 bytes
buffer.base[4] = (unsigned int) random();
// SECS
buffer.base[8] = 0x0;
// FLAGS
buffer.base[10] = 0x80;
// CIADDR 12-15 is all zeros
// YIADDR 16-19 is all zeros
// SIADDR 20-23 is all zeros
// GIADDR 24-27 is all zeros
// CHADDR 28-43 is the MAC address, use your own
buffer.base[28] = 0xe4;
buffer.base[29] = 0xce;
buffer.base[30] = 0x8f;
buffer.base[31] = 0x13;
buffer.base[32] = 0xf6;
buffer.base[33] = 0xd4;
// SNAME 64 bytes zero
// FILE 128 bytes zero
// OPTIONS
// - magic cookie
buffer.base[236] = 99;
buffer.base[237] = 130;
buffer.base[238] = 83;
buffer.base[239] = 99;

// DHCP Message type
buffer.base[240] = 53;
buffer.base[241] = 1;
buffer.base[242] = 1; // DHCPDISCOVER

// DHCP Parameter request list
buffer.base[243] = 55;
buffer.base[244] = 4;
buffer.base[245] = 1;
buffer.base[246] = 3;
buffer.base[247] = 15;
buffer.base[248] = 6;

return buffer;
}

udp

範例測試

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
[ubuntu](master-1f56a9b)0h20m root@e8b0cd737680:[t08_udp-dhcp]$ ifconfig
eth0 Link encap:Ethernet HWaddr 02:42:ac:11:00:02
inet addr:172.17.0.2 Bcast:0.0.0.0 Mask:255.255.0.0
inet6 addr: fe80::42:acff:fe11:2/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:2360 errors:0 dropped:0 overruns:0 frame:0
TX packets:10 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:715840 (715.8 KB) TX bytes:1244 (1.2 KB)

lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:65536 Metric:1
RX packets:67 errors:0 dropped:0 overruns:0 frame:0
TX packets:67 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:4268 (4.2 KB) TX bytes:4268 (4.2 KB)

參考來源