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-2025 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.5.4
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
58  error = udpAttachRxCallback(interface, NBNS_PORT, nbnsProcessMessage, NULL);
59  //Any error to report?
60  if(error)
61  return error;
62 
63  //Successful initialization
64  return NO_ERROR;
65 }
66 
67 
68 /**
69  * @brief Process incoming NBNS message
70  * @param[in] interface Underlying network interface
71  * @param[in] pseudoHeader UDP pseudo header
72  * @param[in] udpHeader UDP header
73  * @param[in] buffer Multi-part buffer containing the incoming NBNS message
74  * @param[in] offset Offset to the first byte of the NBNS message
75  * @param[in] ancillary Additional options passed to the stack along with
76  * the packet
77  * @param[in] param Callback function parameter (not used)
78  **/
79 
81  const IpPseudoHeader *pseudoHeader, const UdpHeader *udpHeader,
82  const NetBuffer *buffer, size_t offset, const NetRxAncillary *ancillary,
83  void *param)
84 {
85  size_t length;
87 
88  //Make sure the NBNS message was received from an IPv4 peer
89  if(pseudoHeader->length != sizeof(Ipv4PseudoHeader))
90  return;
91 
92  //Retrieve the length of the NBNS message
93  length = netBufferGetLength(buffer) - offset;
94 
95  //Malformed NBNS message?
96  if(length < sizeof(NbnsHeader))
97  return;
98 
99  //Point to the NBNS message header
100  message = netBufferAt(buffer, offset, length);
101  //Sanity check
102  if(message == NULL)
103  return;
104 
105  //Debug message
106  TRACE_INFO("NBNS message received (%" PRIuSIZE " bytes)...\r\n", length);
107  //Dump message
109 
110  //NBNS messages received with an opcode other than zero must be silently
111  //ignored
112  if(message->opcode != DNS_OPCODE_QUERY)
113  return;
114 
115  //NBNS messages received with non-zero response codes must be silently
116  //ignored
117  if(message->rcode != DNS_RCODE_NOERROR)
118  return;
119 
120  //NBNS query received?
121  if(!message->qr)
122  {
123 #if (NBNS_RESPONDER_SUPPORT == ENABLED)
124  //Process incoming NBNS query message
125  nbnsProcessQuery(interface, &pseudoHeader->ipv4Data,
126  udpHeader, message, length);
127 #endif
128  }
129  //NBNS response received?
130  else
131  {
132 #if (NBNS_CLIENT_SUPPORT == ENABLED)
133  //Process incoming NBNS response message
134  nbnsProcessResponse(interface, &pseudoHeader->ipv4Data,
135  udpHeader, message, length);
136 #endif
137  }
138 }
139 
140 
141 /**
142  * @brief Encode a NetBIOS name
143  * @param[in] src Pointer to the name to encode
144  * @param[out] dest Pointer to the encoded NetBIOS name
145  * @return Length of the encoded NetBIOS name
146  **/
147 
148 size_t nbnsEncodeName(const char_t *src, uint8_t *dest)
149 {
150  size_t i;
151  size_t j;
152  char_t c;
153 
154  //Point to first byte of the output buffer
155  j = 0;
156 
157  //NetBIOS names are 32-byte long
158  dest[j++] = 32;
159 
160  //Broadcast NetBIOS name?
161  if(osStrcmp(src, "*") == 0)
162  {
163  //Format Broadcast NetBIOS name
164  dest[j++] = NBNS_ENCODE_H('*');
165  dest[j++] = NBNS_ENCODE_L('*');
166 
167  //The '*' character (hexadecimal 2A) is followed by 15 bytes of
168  //hexidecimal 00 (refer to RFC 1001, section 17.2)
169  for(i = 1; i < 15; i++)
170  {
171  dest[j++] = NBNS_ENCODE_H(0);
172  dest[j++] = NBNS_ENCODE_L(0);
173  }
174  }
175  else
176  {
177  //The 16 byte NetBIOS name is mapped into a 32 byte wide field using a
178  //reversible, half-ASCII, biased encoding (refer to RFC 1001,
179  //section 14.1)
180  for(i = 0; i < 15 && src[i] != '\0'; i++)
181  {
182  //Convert current character to uppercase
183  c = osToupper(src[i]);
184 
185  //Each half-octet of the NetBIOS name is encoded into one byte of the
186  //32 byte field. The first half octet is encoded into the first byte,
187  //the second half-octet into the second byte, etc
188  dest[j++] = NBNS_ENCODE_H(c);
189  dest[j++] = NBNS_ENCODE_L(c);
190  }
191 
192  //Pad NetBIOS name with space characters
193  for(; i < 15; i++)
194  {
195  dest[j++] = NBNS_ENCODE_H(' ');
196  dest[j++] = NBNS_ENCODE_L(' ');
197  }
198  }
199 
200  //The 16th character is the NetBIOS suffix
201  dest[j++] = NBNS_ENCODE_H(0);
202  dest[j++] = NBNS_ENCODE_L(0);
203 
204  //Terminate the NetBIOS name with a zero length count
205  dest[j++] = 0;
206 
207  //Return the length of the encoded NetBIOS name
208  return j;
209 }
210 
211 
212 /**
213  * @brief Decode a NetBIOS name
214  * @param[in] message Pointer to the NBNS message
215  * @param[in] length Length of the NBNS message
216  * @param[in] pos Offset of the name to decode
217  * @param[out] dest Pointer to the decoded name (optional)
218  * @return The position of the resource record that immediately follows the
219  * NetBIOS name
220  **/
221 
222 size_t nbnsParseName(const NbnsHeader *message, size_t length, size_t pos,
223  char_t *dest)
224 {
225  size_t i;
226  size_t n;
227  char_t c;
228 
229  //Cast the input NBNS message to byte array
230  uint8_t *src = (uint8_t *) message;
231 
232  //Malformed NBNS message?
233  if((pos + 34) >= length)
234  return 0;
235 
236  //Retrieve the length of the first label
237  n = src[pos++];
238 
239  //NetBIOS names must be 32-byte long
240  if(n != 32)
241  return 0;
242 
243  //The 16 byte NetBIOS name is mapped into a 32 byte wide field using a
244  //reversible, half-ASCII, biased encoding (refer to RFC 1001, section 14.1)
245  for(i = 0; i < 15; i++)
246  {
247  //The NetBIOS name is represented as a sequence of 32 ASCII, upper-case
248  //characters from the set {A,B,C...N,O,P}
249  if(src[pos] < 'A' || src[pos + 1] < 'A' ||
250  src[pos] > 'P' || src[pos + 1] > 'P')
251  {
252  return 0;
253  }
254 
255  //Each half-octet of the NetBIOS name is encoded into one byte of the
256  //32 byte field. The first half octet is encoded into the first byte,
257  //the second half-octet into the second byte, etc
258  c = ((src[pos] - 'A') << 4) | (src[pos + 1] - 'A');
259 
260  //Padding character found?
261  if(c == ' ')
262  break;
263 
264  //Save current ASCII character
265  if(dest != NULL)
266  {
267  *(dest++) = c;
268  }
269 
270  //Advance data pointer
271  pos += 2;
272  }
273 
274  //Skip padding characters
275  for(; i < 16; i++)
276  {
277  //Make sure the nibbles are valid
278  if(src[pos] < 'A' || src[pos + 1] < 'A' ||
279  src[pos] > 'P' || src[pos + 1] > 'P')
280  {
281  return 0;
282  }
283 
284  //Advance data pointer
285  pos += 2;
286  }
287 
288  //Retrieve the length of the next label
289  n = src[pos++];
290 
291  //NetBIOS names are terminated with a zero length count
292  if(n != 0)
293  return 0;
294 
295  //Properly terminate the string
296  if(dest != NULL)
297  {
298  *(dest++) = '\0';
299  }
300 
301  //Return the position of the resource record that is immediately following
302  //the NetBIOS name
303  return pos;
304 }
305 
306 
307 /**
308  * @brief Compare NetBIOS names
309  * @param[in] message Pointer to the NBNS message
310  * @param[in] length Length of the NBNS message
311  * @param[in] pos Offset of the encoded domain name
312  * @param[in] name NULL-terminated string that holds a domain name
313  * @return TRUE if the NetBIOS names match, else FALSE
314  **/
315 
316 bool_t nbnsCompareName(const NbnsHeader *message, size_t length, size_t pos,
317  const char_t *name)
318 {
319  size_t i;
320  size_t n;
321  char_t c;
322 
323  //Cast the input NBNS message to byte array
324  uint8_t *src = (uint8_t *) message;
325 
326  //Malformed NBNS message?
327  if((pos + 34) >= length)
328  return FALSE;
329 
330  //Retrieve the length of the first label
331  n = src[pos++];
332 
333  //NetBIOS names must be 32-byte long
334  if(n != 32)
335  return FALSE;
336 
337  //The 16 byte NetBIOS name is mapped into a 32 byte wide field using a
338  //reversible, half-ASCII, biased encoding (refer to RFC 1001, section 14.1)
339  for(i = 0; i < 15; i++)
340  {
341  //The NetBIOS name is represented as a sequence of 32 ASCII, upper-case
342  //characters from the set {A,B,C...N,O,P}
343  if(src[pos] < 'A' || src[pos + 1] < 'A' ||
344  src[pos] > 'P' || src[pos + 1] > 'P')
345  {
346  return FALSE;
347  }
348 
349  //Each half-octet of the NetBIOS name is encoded into one byte of the
350  //32 byte field. The first half octet is encoded into the first byte,
351  //the second half-octet into the second byte, etc
352  c = ((src[pos] - 'A') << 4) | (src[pos + 1] - 'A');
353 
354  //Padding character found?
355  if(c == ' ' && *name == '\0')
356  break;
357 
358  //Perform case insensitive comparison
359  if(osToupper(c) != osToupper(*name))
360  return FALSE;
361 
362  //Advance data pointer
363  pos += 2;
364  name++;
365  }
366 
367  //Skip padding characters
368  for(; i < 16; i++)
369  {
370  //Make sure the nibbles are valid
371  if(src[pos] < 'A' || src[pos + 1] < 'A' ||
372  src[pos] > 'P' || src[pos + 1] > 'P')
373  {
374  return FALSE;
375  }
376 
377  //Advance data pointer
378  pos += 2;
379  }
380 
381  //Retrieve the length of the next label
382  n = src[pos];
383 
384  //NetBIOS names are terminated with a zero length count
385  if(n != 0)
386  return FALSE;
387 
388  //The NetBIOS names match
389  return TRUE;
390 }
391 
392 #endif
393 #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:61
#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:222
@ DNS_OPCODE_QUERY
Query.
Definition: dns_common.h:81
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:309
char_t name[]
DnsHeader
Definition: dns_common.h:202
#define osStrcmp(s1, s2)
Definition: os_port.h:174
#define NBNS_PORT
Definition: nbns_common.h:46
size_t length
Definition: ip.h:111
IP pseudo header.
Definition: ip.h:110
@ DNS_RCODE_NOERROR
No error.
Definition: dns_common.h:95
#define FALSE
Definition: os_port.h:46
#define osToupper(c)
Definition: os_port.h:276
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:36
error_t udpAttachRxCallback(NetInterface *interface, uint16_t port, UdpRxCallback callback, void *param)
Register user callback.
Definition: udp.c:1021
#define Ipv4PseudoHeader
Definition: ipv4.h:39
#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:80
UdpHeader
Definition: udp.h:85
size_t nbnsEncodeName(const char_t *src, uint8_t *dest)
Encode a NetBIOS name.
Definition: nbns_common.c:148
char char_t
Definition: compiler_port.h:55
uint8_t n
#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
#define PRIuSIZE
TCP/IP stack core.
Data logging functions for debugging purpose (DNS)
Ipv4PseudoHeader ipv4Data
Definition: ip.h:115
@ 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:316