42 #if LWIP_IPV4 && LWIP_UDP 50 #define NETBIOS_PORT 137 53 #define NETBIOS_NAME_LEN 16 57 #define NETBIOS_NAME_TTL 300000u 60 #define NETB_HFLAG_RESPONSE 0x8000U 61 #define NETB_HFLAG_OPCODE 0x7800U 62 #define NETB_HFLAG_OPCODE_NAME_QUERY 0x0000U 63 #define NETB_HFLAG_AUTHORATIVE 0x0400U 64 #define NETB_HFLAG_TRUNCATED 0x0200U 65 #define NETB_HFLAG_RECURS_DESIRED 0x0100U 66 #define NETB_HFLAG_RECURS_AVAILABLE 0x0080U 67 #define NETB_HFLAG_BROADCAST 0x0010U 68 #define NETB_HFLAG_REPLYCODE 0x0008U 69 #define NETB_HFLAG_REPLYCODE_NOERROR 0x0000U 72 #define NETB_NFLAG_UNIQUE 0x8000U 73 #define NETB_NFLAG_NODETYPE 0x6000U 74 #define NETB_NFLAG_NODETYPE_HNODE 0x6000U 75 #define NETB_NFLAG_NODETYPE_MNODE 0x4000U 76 #define NETB_NFLAG_NODETYPE_PNODE 0x2000U 77 #define NETB_NFLAG_NODETYPE_BNODE 0x0000U 80 #ifdef PACK_STRUCT_USE_INCLUDES 93 #ifdef PACK_STRUCT_USE_INCLUDES 98 #ifdef PACK_STRUCT_USE_INCLUDES 102 struct netbios_name_hdr {
113 #ifdef PACK_STRUCT_USE_INCLUDES 118 #ifdef PACK_STRUCT_USE_INCLUDES 124 struct netbios_hdr resp_hdr;
125 struct netbios_name_hdr resp_name;
128 #ifdef PACK_STRUCT_USE_INCLUDES 132 #ifdef NETBIOS_LWIP_NAME 133 #define NETBIOS_LOCAL_NAME NETBIOS_LWIP_NAME 135 static char netbiosns_local_name[NETBIOS_NAME_LEN];
136 #define NETBIOS_LOCAL_NAME netbiosns_local_name 139 struct udp_pcb *netbiosns_pcb;
143 netbiosns_name_decode(
char *name_enc,
char *name_dec,
int name_dec_len)
162 if (cname < 'A' || cname >
'Z') {
167 cnbname = cname << 4;
171 if (cname ==
'\0' || cname ==
'.') {
176 if (cname < 'A' || cname >
'Z') {
185 if (idx < NETBIOS_NAME_LEN) {
187 name_dec[idx++] = (cnbname!=
' '?cnbname:
'\0');
198 netbiosns_name_encode(
char *name_enc,
char *name_dec,
int name_dec_len)
202 unsigned char ucname;
216 if ((cname < 'A' || cname >
'Z') && (cname < '0' || cname >
'9')) {
222 if (idx >= name_dec_len) {
228 name_dec[idx++] = (
'A'+((ucname>>4) & 0x0F));
229 name_dec[idx++] = (
'A'+( ucname & 0x0F));
234 for (;idx < name_dec_len - 1;) {
235 name_dec[idx++] =
'C';
236 name_dec[idx++] =
'A';
240 name_dec[
idx] =
'\0';
248 netbiosns_recv(
void *arg,
struct udp_pcb *upcb,
struct pbuf *p,
const ip_addr_t *addr,
u16_t port)
254 char netbios_name[NETBIOS_NAME_LEN+1];
255 struct netbios_hdr* netbios_hdr = (
struct netbios_hdr*)p->
payload;
256 struct netbios_name_hdr* netbios_name_hdr = (
struct netbios_name_hdr*)(netbios_hdr+1);
262 if (((netbios_hdr->flags &
PP_NTOHS(NETB_HFLAG_OPCODE)) ==
PP_NTOHS(NETB_HFLAG_OPCODE_NAME_QUERY)) &&
263 ((netbios_hdr->flags &
PP_NTOHS(NETB_HFLAG_RESPONSE)) == 0) &&
264 (netbios_hdr->questions ==
PP_NTOHS(1))) {
266 netbiosns_name_decode((
char*)(netbios_name_hdr->encname), netbios_name,
sizeof(netbios_name));
270 struct netbios_resp *resp;
274 resp = (
struct netbios_resp*)q->
payload;
277 resp->resp_hdr.trans_id = netbios_hdr->trans_id;
278 resp->resp_hdr.flags =
PP_HTONS(NETB_HFLAG_RESPONSE |
279 NETB_HFLAG_OPCODE_NAME_QUERY |
280 NETB_HFLAG_AUTHORATIVE |
281 NETB_HFLAG_RECURS_DESIRED);
282 resp->resp_hdr.questions = 0;
283 resp->resp_hdr.answerRRs =
PP_HTONS(1);
284 resp->resp_hdr.authorityRRs = 0;
285 resp->resp_hdr.additionalRRs = 0;
288 MEMCPY( resp->resp_name.encname, netbios_name_hdr->encname,
sizeof(netbios_name_hdr->encname));
289 resp->resp_name.nametype = netbios_name_hdr->nametype;
290 resp->resp_name.type = netbios_name_hdr->type;
291 resp->resp_name.cls = netbios_name_hdr->cls;
292 resp->resp_name.ttl =
PP_HTONL(NETBIOS_NAME_TTL);
293 resp->resp_name.datalen =
PP_HTONS(
sizeof(resp->resp_name.flags)+
sizeof(resp->resp_name.addr));
294 resp->resp_name.flags =
PP_HTONS(NETB_NFLAG_NODETYPE_BNODE);
295 ip4_addr_copy(resp->resp_name.addr, *netif_ip4_addr(
netif_default));
298 udp_sendto(upcb, q, addr, port);
314 #ifdef NETBIOS_LWIP_NAME 315 LWIP_ASSERT(
"NetBIOS name is too long!", strlen(NETBIOS_LWIP_NAME) < NETBIOS_NAME_LEN);
318 netbiosns_pcb = udp_new();
319 if (netbiosns_pcb !=
NULL) {
322 udp_bind(netbiosns_pcb, IP_ADDR_ANY, NETBIOS_PORT);
323 udp_recv(netbiosns_pcb, netbiosns_recv, netbiosns_pcb);
327 #ifndef NETBIOS_LWIP_NAME 332 size_t copy_len = strlen(hostname);
333 LWIP_ASSERT(
"NetBIOS name is too long!", copy_len < NETBIOS_NAME_LEN);
334 if(copy_len >= NETBIOS_NAME_LEN) {
335 copy_len = NETBIOS_NAME_LEN - 1;
337 memcpy(netbiosns_local_name, hostname, copy_len + 1);
344 if (netbiosns_pcb !=
NULL) {
345 udp_remove(netbiosns_pcb);
346 netbiosns_pcb =
NULL;
#define PACK_STRUCT_STRUCT
struct netif * netif_default
#define PACK_STRUCT_FLD_8(x)
void netbiosns_init(void)
#define MEMCPY(dst, src, len)
#define PACK_STRUCT_FIELD(x)
struct pbuf * pbuf_alloc(pbuf_layer layer, u16_t length, pbuf_type type)
u8_t pbuf_free(struct pbuf *p)
#define LWIP_ASSERT(message, assertion)
void netbiosns_stop(void)
#define NETBIOS_STRCMP(str1, str2)
#define PACK_STRUCT_FLD_S(x)
#define PACK_STRUCT_BEGIN
void netbiosns_set_name(const char *hostname)
#define LWIP_UNUSED_ARG(x)