nbns_common.c
Go to the documentation of this file.
1 /**
2  * @file nbns_common.c
3  * @brief Definitions common to NBNS client and NBNS responder
4  *
5  * @section License
6  *
7  * SPDX-License-Identifier: GPL-2.0-or-later
8  *
9  * Copyright (C) 2010-2026 Oryx Embedded SARL. All rights reserved.
10  *
11  * This file is part of CycloneTCP Open.
12  *
13  * This program is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU General Public License
15  * as published by the Free Software Foundation; either version 2
16  * of the License, or (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software Foundation,
25  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
26  *
27  * @author Oryx Embedded SARL (www.oryx-embedded.com)
28  * @version 2.6.6
29  **/
30 
31 //Switch to the appropriate trace level
32 #define TRACE_LEVEL NBNS_TRACE_LEVEL
33 
34 //Dependencies
35 #include "core/net.h"
36 #include "netbios/nbns_client.h"
37 #include "netbios/nbns_responder.h"
38 #include "netbios/nbns_common.h"
39 #include "dns/dns_debug.h"
40 #include "debug.h"
41 
42 //Check TCP/IP stack configuration
43 #if (NBNS_CLIENT_SUPPORT == ENABLED || NBNS_RESPONDER_SUPPORT == ENABLED)
44 #if (IPV4_SUPPORT == ENABLED)
45 
46 
47 /**
48  * @brief NBNS related initialization
49  * @param[in] interface Underlying network interface
50  * @return Error code
51  **/
52 
54 {
55  error_t error;
56 
57  //Callback function to be called when a NBNS message is received
59  NULL);
60  //Any error to report?
61  if(error)
62  return error;
63 
64  //Successful initialization
65  return NO_ERROR;
66 }
67 
68 
69 /**
70  * @brief Process incoming NBNS message
71  * @param[in] interface Underlying network interface
72  * @param[in] pseudoHeader UDP pseudo header
73  * @param[in] udpHeader UDP header
74  * @param[in] buffer Multi-part buffer containing the incoming NBNS message
75  * @param[in] offset Offset to the first byte of the NBNS message
76  * @param[in] ancillary Additional options passed to the stack along with
77  * the packet
78  * @param[in] param Callback function parameter (not used)
79  **/
80 
82  const IpPseudoHeader *pseudoHeader, const UdpHeader *udpHeader,
83  const NetBuffer *buffer, size_t offset, const NetRxAncillary *ancillary,
84  void *param)
85 {
86  size_t length;
88 
89  //Make sure the NBNS message was received from an IPv4 peer
90  if(pseudoHeader->length != sizeof(Ipv4PseudoHeader))
91  return;
92 
93  //Retrieve the length of the NBNS message
94  length = netBufferGetLength(buffer) - offset;
95 
96  //Malformed NBNS message?
97  if(length < sizeof(NbnsHeader))
98  return;
99 
100  //Point to the NBNS message header
101  message = netBufferAt(buffer, offset, length);
102  //Malformed NBNS message?
103  if(message == NULL)
104  return;
105 
106  //Debug message
107  TRACE_INFO("NBNS message received (%" PRIuSIZE " bytes)...\r\n", length);
108  //Dump message
110 
111  //NBNS messages received with an opcode other than zero must be silently
112  //ignored
113  if(message->opcode != DNS_OPCODE_QUERY)
114  return;
115 
116  //NBNS messages received with non-zero response codes must be silently
117  //ignored
118  if(message->rcode != DNS_RCODE_NOERROR)
119  return;
120 
121  //NBNS query received?
122  if(!message->qr)
123  {
124 #if (NBNS_RESPONDER_SUPPORT == ENABLED)
125  //Process incoming NBNS query message
126  nbnsProcessQuery(interface, &pseudoHeader->ipv4Data,
127  udpHeader, message, length);
128 #endif
129  }
130  //NBNS response received?
131  else
132  {
133 #if (NBNS_CLIENT_SUPPORT == ENABLED)
134  //Process incoming NBNS response message
135  nbnsProcessResponse(interface, &pseudoHeader->ipv4Data,
136  udpHeader, message, length);
137 #endif
138  }
139 }
140 
141 
142 /**
143  * @brief Encode a NetBIOS name
144  * @param[in] src Pointer to the name to encode
145  * @param[out] dest Pointer to the encoded NetBIOS name
146  * @return Length of the encoded NetBIOS name
147  **/
148 
149 size_t nbnsEncodeName(const char_t *src, uint8_t *dest)
150 {
151  size_t i;
152  size_t j;
153  char_t c;
154 
155  //Point to first byte of the output buffer
156  j = 0;
157 
158  //NetBIOS names are 32-byte long
159  dest[j++] = 32;
160 
161  //Broadcast NetBIOS name?
162  if(osStrcmp(src, "*") == 0)
163  {
164  //Format Broadcast NetBIOS name
165  dest[j++] = NBNS_ENCODE_H('*');
166  dest[j++] = NBNS_ENCODE_L('*');
167 
168  //The '*' character (hexadecimal 2A) is followed by 15 bytes of
169  //hexidecimal 00 (refer to RFC 1001, section 17.2)
170  for(i = 1; i < 15; i++)
171  {
172  dest[j++] = NBNS_ENCODE_H(0);
173  dest[j++] = NBNS_ENCODE_L(0);
174  }
175  }
176  else
177  {
178  //The 16 byte NetBIOS name is mapped into a 32 byte wide field using a
179  //reversible, half-ASCII, biased encoding (refer to RFC 1001,
180  //section 14.1)
181  for(i = 0; i < 15 && src[i] != '\0'; i++)
182  {
183  //Convert current character to uppercase
184  c = osToupper(src[i]);
185 
186  //Each half-octet of the NetBIOS name is encoded into one byte of the
187  //32 byte field. The first half octet is encoded into the first byte,
188  //the second half-octet into the second byte, etc
189  dest[j++] = NBNS_ENCODE_H(c);
190  dest[j++] = NBNS_ENCODE_L(c);
191  }
192 
193  //Pad NetBIOS name with space characters
194  for(; i < 15; i++)
195  {
196  dest[j++] = NBNS_ENCODE_H(' ');
197  dest[j++] = NBNS_ENCODE_L(' ');
198  }
199  }
200 
201  //The 16th character is the NetBIOS suffix
202  dest[j++] = NBNS_ENCODE_H(0);
203  dest[j++] = NBNS_ENCODE_L(0);
204 
205  //Terminate the NetBIOS name with a zero length count
206  dest[j++] = 0;
207 
208  //Return the length of the encoded NetBIOS name
209  return j;
210 }
211 
212 
213 /**
214  * @brief Decode a NetBIOS name
215  * @param[in] message Pointer to the NBNS message
216  * @param[in] length Length of the NBNS message
217  * @param[in] pos Offset of the name to decode
218  * @param[out] dest Pointer to the decoded name (optional)
219  * @return The position of the resource record that immediately follows the
220  * NetBIOS name
221  **/
222 
223 size_t nbnsParseName(const NbnsHeader *message, size_t length, size_t pos,
224  char_t *dest)
225 {
226  size_t i;
227  size_t n;
228  char_t c;
229 
230  //Cast the input NBNS message to byte array
231  uint8_t *src = (uint8_t *) message;
232 
233  //Malformed NBNS message?
234  if((pos + 34) >= length)
235  return 0;
236 
237  //Retrieve the length of the first label
238  n = src[pos++];
239 
240  //NetBIOS names must be 32-byte long
241  if(n != 32)
242  return 0;
243 
244  //The 16 byte NetBIOS name is mapped into a 32 byte wide field using a
245  //reversible, half-ASCII, biased encoding (refer to RFC 1001, section 14.1)
246  for(i = 0; i < 15; i++)
247  {
248  //The NetBIOS name is represented as a sequence of 32 ASCII, upper-case
249  //characters from the set {A,B,C...N,O,P}
250  if(src[pos] < 'A' || src[pos + 1] < 'A' ||
251  src[pos] > 'P' || src[pos + 1] > 'P')
252  {
253  return 0;
254  }
255 
256  //Each half-octet of the NetBIOS name is encoded into one byte of the
257  //32 byte field. The first half octet is encoded into the first byte,
258  //the second half-octet into the second byte, etc
259  c = ((src[pos] - 'A') << 4) | (src[pos + 1] - 'A');
260 
261  //Padding character found?
262  if(c == ' ')
263  break;
264 
265  //Save current ASCII character
266  if(dest != NULL)
267  {
268  *(dest++) = c;
269  }
270 
271  //Advance data pointer
272  pos += 2;
273  }
274 
275  //Skip padding characters
276  for(; i < 16; i++)
277  {
278  //Make sure the nibbles are valid
279  if(src[pos] < 'A' || src[pos + 1] < 'A' ||
280  src[pos] > 'P' || src[pos + 1] > 'P')
281  {
282  return 0;
283  }
284 
285  //Advance data pointer
286  pos += 2;
287  }
288 
289  //Retrieve the length of the next label
290  n = src[pos++];
291 
292  //NetBIOS names are terminated with a zero length count
293  if(n != 0)
294  return 0;
295 
296  //Properly terminate the string
297  if(dest != NULL)
298  {
299  *(dest++) = '\0';
300  }
301 
302  //Return the position of the resource record that is immediately following
303  //the NetBIOS name
304  return pos;
305 }
306 
307 
308 /**
309  * @brief Compare NetBIOS names
310  * @param[in] message Pointer to the NBNS message
311  * @param[in] length Length of the NBNS message
312  * @param[in] pos Offset of the encoded domain name
313  * @param[in] name NULL-terminated string that holds a domain name
314  * @return TRUE if the NetBIOS names match, else FALSE
315  **/
316 
317 bool_t nbnsCompareName(const NbnsHeader *message, size_t length, size_t pos,
318  const char_t *name)
319 {
320  size_t i;
321  size_t n;
322  char_t c;
323 
324  //Cast the input NBNS message to byte array
325  uint8_t *src = (uint8_t *) message;
326 
327  //Malformed NBNS message?
328  if((pos + 34) >= length)
329  return FALSE;
330 
331  //Retrieve the length of the first label
332  n = src[pos++];
333 
334  //NetBIOS names must be 32-byte long
335  if(n != 32)
336  return FALSE;
337 
338  //The 16 byte NetBIOS name is mapped into a 32 byte wide field using a
339  //reversible, half-ASCII, biased encoding (refer to RFC 1001, section 14.1)
340  for(i = 0; i < 15; i++)
341  {
342  //The NetBIOS name is represented as a sequence of 32 ASCII, upper-case
343  //characters from the set {A,B,C...N,O,P}
344  if(src[pos] < 'A' || src[pos + 1] < 'A' ||
345  src[pos] > 'P' || src[pos + 1] > 'P')
346  {
347  return FALSE;
348  }
349 
350  //Each half-octet of the NetBIOS name is encoded into one byte of the
351  //32 byte field. The first half octet is encoded into the first byte,
352  //the second half-octet into the second byte, etc
353  c = ((src[pos] - 'A') << 4) | (src[pos + 1] - 'A');
354 
355  //Padding character found?
356  if(c == ' ' && *name == '\0')
357  break;
358 
359  //Perform case insensitive comparison
360  if(osToupper(c) != osToupper(*name))
361  return FALSE;
362 
363  //Advance data pointer
364  pos += 2;
365  name++;
366  }
367 
368  //Skip padding characters
369  for(; i < 16; i++)
370  {
371  //Make sure the nibbles are valid
372  if(src[pos] < 'A' || src[pos + 1] < 'A' ||
373  src[pos] > 'P' || src[pos + 1] > 'P')
374  {
375  return FALSE;
376  }
377 
378  //Advance data pointer
379  pos += 2;
380  }
381 
382  //Retrieve the length of the next label
383  n = src[pos];
384 
385  //NetBIOS names are terminated with a zero length count
386  if(n != 0)
387  return FALSE;
388 
389  //The NetBIOS names match
390  return TRUE;
391 }
392 
393 #endif
394 #endif
void nbnsProcessQuery(NetInterface *interface, const Ipv4PseudoHeader *pseudoHeader, const UdpHeader *udpHeader, const NbnsHeader *message, size_t length)
Process NBNS query message.
int bool_t
Definition: compiler_port.h:63
#define NBNS_ENCODE_H(c)
Definition: nbns_common.h:49
size_t nbnsParseName(const NbnsHeader *message, size_t length, size_t pos, char_t *dest)
Decode a NetBIOS name.
Definition: nbns_common.c:223
@ DNS_OPCODE_QUERY
Query.
Definition: dns_common.h:83
Structure describing a buffer that spans multiple chunks.
Definition: net_mem.h:89
uint8_t message[]
Definition: chap.h:154
#define TRUE
Definition: os_port.h:50
void nbnsProcessResponse(NetInterface *interface, const Ipv4PseudoHeader *pseudoHeader, const UdpHeader *udpHeader, const NbnsHeader *message, size_t length)
Process NBNS response message.
Definition: nbns_client.c:333
char_t name[]
DnsHeader
Definition: dns_common.h:204
#define osStrcmp(s1, s2)
Definition: os_port.h:177
#define NBNS_PORT
Definition: nbns_common.h:46
@ DNS_RCODE_NOERROR
No error.
Definition: dns_common.h:97
#define FALSE
Definition: os_port.h:46
#define osToupper(c)
Definition: os_port.h:279
NbnsHeader
Definition: nbns_common.h:131
error_t
Error codes.
Definition: error.h:43
Definitions common to NBNS client and NBNS responder.
NBNS client (NetBIOS Name Service)
#define NetRxAncillary
Definition: net_misc.h:40
#define NetInterface
Definition: net.h:40
#define Ipv4PseudoHeader
Definition: ipv4.h:40
#define TRACE_INFO(...)
Definition: debug.h:105
uint8_t length
Definition: tcp.h:375
size_t netBufferGetLength(const NetBuffer *buffer)
Get the actual length of a multi-part buffer.
Definition: net_mem.c:297
void dnsDumpMessage(const DnsHeader *message, size_t length)
Dump DNS message for debugging purpose.
Definition: dns_debug.c:52
void nbnsProcessMessage(NetInterface *interface, const IpPseudoHeader *pseudoHeader, const UdpHeader *udpHeader, const NetBuffer *buffer, size_t offset, const NetRxAncillary *ancillary, void *param)
Process incoming NBNS message.
Definition: nbns_common.c:81
UdpHeader
Definition: udp.h:85
size_t nbnsEncodeName(const char_t *src, uint8_t *dest)
Encode a NetBIOS name.
Definition: nbns_common.c:149
char char_t
Definition: compiler_port.h:55
uint8_t n
#define IpPseudoHeader
Definition: ip.h:36
#define NBNS_ENCODE_L(c)
Definition: nbns_common.h:50
error_t nbnsInit(NetInterface *interface)
NBNS related initialization.
Definition: nbns_common.c:53
void * netBufferAt(const NetBuffer *buffer, size_t offset, size_t length)
Returns a pointer to a data segment.
Definition: net_mem.c:418
error_t udpRegisterRxCallback(NetInterface *interface, uint16_t port, UdpRxCallback callback, void *param)
Register user callback.
Definition: udp.c:1031
#define PRIuSIZE
TCP/IP stack core.
Data logging functions for debugging purpose (DNS)
@ NO_ERROR
Success.
Definition: error.h:44
uint8_t c
Definition: ndp.h:514
Debugging facilities.
NBNS responder (NetBIOS Name Service)
bool_t nbnsCompareName(const NbnsHeader *message, size_t length, size_t pos, const char_t *name)
Compare NetBIOS names.
Definition: nbns_common.c:317