- Notifications
You must be signed in to change notification settings - Fork 7.6k
/
Copy pathNetBIOS.cpp
146 lines (130 loc) · 3.71 KB
/
NetBIOS.cpp
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
#include"NetBIOS.h"
#include<functional>
extern"C" {
#include<lwip/netif.h>
}; // extern "C"
#defineNBNS_PORT137
#defineNBNS_MAX_HOSTNAME_LEN32
typedefstruct {
uint16_t id;
uint8_t flags1;
uint8_t flags2;
uint16_t qcount;
uint16_t acount; // codespell:ignore acount
uint16_t nscount;
uint16_t adcount;
uint8_t name_len;
char name[NBNS_MAX_HOSTNAME_LEN + 1];
uint16_t type;
uint16_t clas;
} __attribute__((packed)) nbns_question_t;
typedefstruct {
uint16_t id;
uint8_t flags1;
uint8_t flags2;
uint16_t qcount;
uint16_t acount; // codespell:ignore acount
uint16_t nscount;
uint16_t adcount;
uint8_t name_len;
char name[NBNS_MAX_HOSTNAME_LEN + 1];
uint16_t type;
uint16_t clas;
uint32_t ttl;
uint16_t data_len;
uint16_t flags;
uint32_t addr;
} __attribute__((packed)) nbns_answer_t;
staticvoid_getnbname(constchar *nbname, char *name, uint8_t maxlen) {
uint8_t b;
uint8_t c = 0;
while ((*nbname) && (c < maxlen)) {
b = (*nbname++ - 'A') << 4;
c++;
if (*nbname) {
b |= *nbname++ - 'A';
c++;
}
if (!b || b == '') {
break;
}
*name++ = b;
}
*name = 0;
}
staticvoidappend_16(void *dst, uint16_t value) {
uint8_t *d = (uint8_t *)dst;
*d++ = (value >> 8) & 0xFF;
*d++ = value & 0xFF;
}
staticvoidappend_32(void *dst, uint32_t value) {
uint8_t *d = (uint8_t *)dst;
*d++ = (value >> 24) & 0xFF;
*d++ = (value >> 16) & 0xFF;
*d++ = (value >> 8) & 0xFF;
*d++ = value & 0xFF;
}
voidNetBIOS::_onPacket(AsyncUDPPacket &packet) {
if (packet.length() >= sizeof(nbns_question_t)) {
nbns_question_t *question = (nbns_question_t *)packet.data();
if (0 == (question->flags1 & 0x80)) {
char name[NBNS_MAX_HOSTNAME_LEN + 1];
_getnbname(&question->name[0], (char *)&name, question->name_len);
if (_name.equals(name)) {
nbns_answer_t nbnsa;
nbnsa.id = question->id;
nbnsa.flags1 = 0x85;
nbnsa.flags2 = 0;
append_16((void *)&nbnsa.qcount, 0);
append_16((void *)&nbnsa.acount, 1); // codespell:ignore acount
append_16((void *)&nbnsa.nscount, 0);
append_16((void *)&nbnsa.adcount, 0);
nbnsa.name_len = question->name_len;
memcpy(&nbnsa.name[0], &question->name[0], question->name_len + 1);
append_16((void *)&nbnsa.type, 0x20);
append_16((void *)&nbnsa.clas, 1);
append_32((void *)&nbnsa.ttl, 300000);
append_16((void *)&nbnsa.data_len, 6);
append_16((void *)&nbnsa.flags, 0);
nbnsa.addr = packet.localIP(); // By default, should be overridden below
// Iterate over all netifs, see if the incoming address matches one of the netmaskes networks
for (auto netif = netif_list; netif; netif = netif->next) {
auto maskedip = ip4_addr_get_u32(netif_ip4_addr(netif)) & ip4_addr_get_u32(netif_ip4_netmask(netif));
auto maskedin = ((uint32_t)packet.localIP()) & ip4_addr_get_u32(netif_ip4_netmask(netif));
if (maskedip == maskedin) {
nbnsa.addr = ip4_addr_get_u32(netif_ip4_addr(netif));
break;
}
}
_udp.writeTo((uint8_t *)&nbnsa, sizeof(nbnsa), packet.remoteIP(), NBNS_PORT);
}
}
}
}
NetBIOS::NetBIOS() {}
NetBIOS::~NetBIOS() {
end();
}
boolNetBIOS::begin(constchar *name) {
_name = name;
_name.toUpperCase();
if (_udp.connected()) {
returntrue;
}
_udp.onPacket(
[](void *arg, AsyncUDPPacket &packet) {
((NetBIOS *)(arg))->_onPacket(packet);
},
this
);
return _udp.listen(NBNS_PORT);
}
voidNetBIOS::end() {
if (_udp.connected()) {
_udp.close();
}
}
#if !defined(NO_GLOBAL_INSTANCES) && !defined(NO_GLOBAL_NETBIOS)
NetBIOS NBNS;
#endif
// EOF