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-2024 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.4.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  //Parse input name
161  for(i = 0; i < 15 && src[i] != '\0'; i++)
162  {
163  //Convert current character to uppercase
164  c = osToupper(src[i]);
165 
166  //Encode character
167  dest[j++] = NBNS_ENCODE_H(c);
168  dest[j++] = NBNS_ENCODE_L(c);
169  }
170 
171  //Pad NetBIOS name with space characters
172  for(; i < 15; i++)
173  {
174  //Encoded space character
175  dest[j++] = NBNS_ENCODE_H(' ');
176  dest[j++] = NBNS_ENCODE_L(' ');
177  }
178 
179  //The 16th character is the NetBIOS suffix
180  dest[j++] = NBNS_ENCODE_H(0);
181  dest[j++] = NBNS_ENCODE_L(0);
182 
183  //Terminate the NetBIOS name with a zero length count
184  dest[j++] = 0;
185 
186  //Return the length of the encoded NetBIOS name
187  return j;
188 }
189 
190 
191 /**
192  * @brief Decode a NetBIOS name
193  * @param[in] message Pointer to the NBNS message
194  * @param[in] length Length of the NBNS message
195  * @param[in] pos Offset of the name to decode
196  * @param[out] dest Pointer to the decoded name (optional)
197  * @return The position of the resource record that immediately follows the NetBIOS name
198  **/
199 
201  size_t length, size_t pos, char_t *dest)
202 {
203  size_t i;
204  size_t n;
205  char_t c;
206 
207  //Cast the input NBNS message to byte array
208  uint8_t *src = (uint8_t *) message;
209 
210  //Malformed NBNS message?
211  if((pos + 34) >= length)
212  return 0;
213 
214  //Retrieve the length of the first label
215  n = src[pos++];
216 
217  //NetBIOS names must be 32-byte long
218  if(n != 32)
219  return 0;
220 
221  //Parse the NetBIOS name
222  for(i = 0; i < 15; i++)
223  {
224  //Make sure the characters of the sequence are valid
225  if(src[pos] < 'A' || src[pos] > 'P')
226  return 0;
227  if(src[pos + 1] < 'A' || src[pos + 1] > 'P')
228  return 0;
229 
230  //Combine nibbles to restore the original ASCII character
231  c = ((src[pos] - 'A') << 4) | (src[pos + 1] - 'A');
232 
233  //Padding character found?
234  if(c == ' ')
235  break;
236 
237  //Save current ASCII character
238  if(dest != NULL)
239  *(dest++) = c;
240 
241  //Advance data pointer
242  pos += 2;
243  }
244 
245  //Skip padding characters
246  for(; i < 16; i++)
247  {
248  //Make sure the nibbles are valid
249  if(src[pos] < 'A' || src[pos] > 'P')
250  return 0;
251  if(src[pos + 1] < 'A' || src[pos + 1] > 'P')
252  return 0;
253 
254  //Advance data pointer
255  pos += 2;
256  }
257 
258  //Retrieve the length of the next label
259  n = src[pos++];
260 
261  //NetBIOS names are terminated with a zero length count
262  if(n != 0)
263  return 0;
264 
265  //Properly terminate the string
266  if(dest != NULL)
267  *(dest++) = '\0';
268 
269  //Return the position of the resource record that
270  //is immediately following the NetBIOS name
271  return pos;
272 }
273 
274 
275 /**
276  * @brief Compare NetBIOS names
277  * @param[in] message Pointer to the NBNS message
278  * @param[in] length Length of the NBNS message
279  * @param[in] pos Offset of the encoded domain name
280  * @param[in] name NULL-terminated string that holds a domain name
281  * @return TRUE if the NetBIOS names match, else FALSE
282  **/
283 
285  size_t length, size_t pos, const char_t *name)
286 {
287  size_t i;
288  size_t n;
289  char_t c;
290 
291  //Cast the input NBNS message to byte array
292  uint8_t *src = (uint8_t *) message;
293 
294  //Malformed NBNS message?
295  if((pos + 34) >= length)
296  return FALSE;
297 
298  //Retrieve the length of the first label
299  n = src[pos++];
300 
301  //NetBIOS names must be 32-byte long
302  if(n != 32)
303  return FALSE;
304 
305  //Parse the NetBIOS name
306  for(i = 0; i < 15; i++)
307  {
308  //Make sure the characters of the sequence are valid
309  if(src[pos] < 'A' || src[pos] > 'P')
310  return FALSE;
311  if(src[pos + 1] < 'A' || src[pos + 1] > 'P')
312  return FALSE;
313 
314  //Combine nibbles to restore the original ASCII character
315  c = ((src[pos] - 'A') << 4) | (src[pos + 1] - 'A');
316 
317  //Padding character found?
318  if(c == ' ' && *name == '\0')
319  break;
320 
321  //Perform case insensitive comparison
322  if(osToupper(c) != osToupper(*name))
323  return FALSE;
324 
325  //Advance data pointer
326  pos += 2;
327  name++;
328  }
329 
330  //Skip padding characters
331  for(; i < 16; i++)
332  {
333  //Make sure the nibbles are valid
334  if(src[pos] < 'A' || src[pos] > 'P')
335  return FALSE;
336  if(src[pos + 1] < 'A' || src[pos + 1] > 'P')
337  return FALSE;
338 
339  //Advance data pointer
340  pos += 2;
341  }
342 
343  //Retrieve the length of the next label
344  n = src[pos];
345 
346  //NetBIOS names are terminated with a zero length count
347  if(n != 0)
348  return FALSE;
349 
350  //The NetBIOS names match
351  return TRUE;
352 }
353 
354 #endif
355 #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:53
#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:200
@ 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:290
char_t name[]
DnsHeader
Definition: dns_common.h:199
#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:273
NbnsHeader
Definition: nbns_common.h:113
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:95
uint8_t length
Definition: tcp.h:368
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:48
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:284