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-2022 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.2.0
29  **/
30 
31 //Switch to the appropriate trace level
32 #define TRACE_LEVEL NBNS_TRACE_LEVEL
33 
34 //Dependencies
35 #include <stdlib.h>
36 #include <string.h>
37 #include "core/net.h"
38 #include "netbios/nbns_client.h"
39 #include "netbios/nbns_responder.h"
40 #include "netbios/nbns_common.h"
41 #include "dns/dns_debug.h"
42 #include "debug.h"
43 
44 //Check TCP/IP stack configuration
45 #if (NBNS_CLIENT_SUPPORT == ENABLED || NBNS_RESPONDER_SUPPORT == ENABLED)
46 #if (IPV4_SUPPORT == ENABLED)
47 
48 
49 /**
50  * @brief NBNS related initialization
51  * @param[in] interface Underlying network interface
52  * @return Error code
53  **/
54 
56 {
57  error_t error;
58 
59  //Callback function to be called when a NBNS message is received
60  error = udpAttachRxCallback(interface, NBNS_PORT, nbnsProcessMessage, NULL);
61  //Any error to report?
62  if(error)
63  return error;
64 
65  //Successful initialization
66  return NO_ERROR;
67 }
68 
69 
70 /**
71  * @brief Process incoming NBNS message
72  * @param[in] interface Underlying network interface
73  * @param[in] pseudoHeader UDP pseudo header
74  * @param[in] udpHeader UDP header
75  * @param[in] buffer Multi-part buffer containing the incoming NBNS message
76  * @param[in] offset Offset to the first byte of the NBNS message
77  * @param[in] ancillary Additional options passed to the stack along with
78  * the packet
79  * @param[in] param Callback function parameter (not used)
80  **/
81 
83  const IpPseudoHeader *pseudoHeader, const UdpHeader *udpHeader,
84  const NetBuffer *buffer, size_t offset, const NetRxAncillary *ancillary,
85  void *param)
86 {
87  size_t length;
89 
90  //Make sure the NBNS message was received from an IPv4 peer
91  if(pseudoHeader->length != sizeof(Ipv4PseudoHeader))
92  return;
93 
94  //Retrieve the length of the NBNS message
95  length = netBufferGetLength(buffer) - offset;
96 
97  //Ensure the NBNS message is valid
98  if(length < sizeof(NbnsHeader))
99  return;
101  return;
102 
103  //Point to the NBNS message header
104  message = netBufferAt(buffer, offset);
105  //Sanity check
106  if(message == NULL)
107  return;
108 
109  //Debug message
110  TRACE_INFO("NBNS message received (%" PRIuSIZE " bytes)...\r\n", length);
111  //Dump message
113 
114  //NBNS messages received with an opcode other than zero must be silently
115  //ignored
116  if(message->opcode != DNS_OPCODE_QUERY)
117  return;
118 
119  //NBNS messages received with non-zero response codes must be silently
120  //ignored
121  if(message->rcode != DNS_RCODE_NO_ERROR)
122  return;
123 
124  //NBNS query received?
125  if(!message->qr)
126  {
127 #if (NBNS_RESPONDER_SUPPORT == ENABLED)
128  //Process incoming NBNS query message
129  nbnsProcessQuery(interface, &pseudoHeader->ipv4Data,
130  udpHeader, message, length);
131 #endif
132  }
133  //NBNS response received?
134  else
135  {
136 #if (NBNS_CLIENT_SUPPORT == ENABLED)
137  //Process incoming NBNS response message
138  nbnsProcessResponse(interface, &pseudoHeader->ipv4Data,
139  udpHeader, message, length);
140 #endif
141  }
142 }
143 
144 
145 /**
146  * @brief Encode a NetBIOS name
147  * @param[in] src Pointer to the name to encode
148  * @param[out] dest Pointer to the encoded NetBIOS name
149  * @return Length of the encoded NetBIOS name
150  **/
151 
152 size_t nbnsEncodeName(const char_t *src, uint8_t *dest)
153 {
154  size_t i;
155  size_t j;
156  char_t c;
157 
158  //Point to first byte of the output buffer
159  j = 0;
160 
161  //NetBIOS names are 32-byte long
162  dest[j++] = 32;
163 
164  //Parse input name
165  for(i = 0; i < 15 && src[i] != '\0'; i++)
166  {
167  //Convert current character to uppercase
168  c = osToupper(src[i]);
169 
170  //Encode character
171  dest[j++] = NBNS_ENCODE_H(c);
172  dest[j++] = NBNS_ENCODE_L(c);
173  }
174 
175  //Pad NetBIOS name with space characters
176  for(; i < 15; i++)
177  {
178  //Encoded space character
179  dest[j++] = NBNS_ENCODE_H(' ');
180  dest[j++] = NBNS_ENCODE_L(' ');
181  }
182 
183  //The 16th character is the NetBIOS suffix
184  dest[j++] = NBNS_ENCODE_H(0);
185  dest[j++] = NBNS_ENCODE_L(0);
186 
187  //Terminate the NetBIOS name with a zero length count
188  dest[j++] = 0;
189 
190  //Return the length of the encoded NetBIOS name
191  return j;
192 }
193 
194 
195 /**
196  * @brief Decode a NetBIOS name
197  * @param[in] message Pointer to the NBNS message
198  * @param[in] length Length of the NBNS message
199  * @param[in] pos Offset of the name to decode
200  * @param[out] dest Pointer to the decoded name (optional)
201  * @return The position of the resource record that immediately follows the NetBIOS name
202  **/
203 
205  size_t length, size_t pos, char_t *dest)
206 {
207  size_t i;
208  size_t n;
209  char_t c;
210 
211  //Cast the input NBNS message to byte array
212  uint8_t *src = (uint8_t *) message;
213 
214  //Malformed NBNS message?
215  if((pos + 34) >= length)
216  return 0;
217 
218  //Retrieve the length of the first label
219  n = src[pos++];
220 
221  //NetBIOS names must be 32-byte long
222  if(n != 32)
223  return 0;
224 
225  //Parse the NetBIOS name
226  for(i = 0; i < 15; i++)
227  {
228  //Make sure the characters of the sequence are valid
229  if(src[pos] < 'A' || src[pos] > 'P')
230  return 0;
231  if(src[pos + 1] < 'A' || src[pos + 1] > 'P')
232  return 0;
233 
234  //Combine nibbles to restore the original ASCII character
235  c = ((src[pos] - 'A') << 4) | (src[pos + 1] - 'A');
236 
237  //Padding character found?
238  if(c == ' ')
239  break;
240 
241  //Save current ASCII character
242  if(dest != NULL)
243  *(dest++) = c;
244 
245  //Advance data pointer
246  pos += 2;
247  }
248 
249  //Skip padding characters
250  for(; i < 16; i++)
251  {
252  //Make sure the nibbles are valid
253  if(src[pos] < 'A' || src[pos] > 'P')
254  return 0;
255  if(src[pos + 1] < 'A' || src[pos + 1] > 'P')
256  return 0;
257 
258  //Advance data pointer
259  pos += 2;
260  }
261 
262  //Retrieve the length of the next label
263  n = src[pos++];
264 
265  //NetBIOS names are terminated with a zero length count
266  if(n != 0)
267  return 0;
268 
269  //Properly terminate the string
270  if(dest != NULL)
271  *(dest++) = '\0';
272 
273  //Return the position of the resource record that
274  //is immediately following the NetBIOS name
275  return pos;
276 }
277 
278 
279 /**
280  * @brief Compare NetBIOS names
281  * @param[in] message Pointer to the NBNS message
282  * @param[in] length Length of the NBNS message
283  * @param[in] pos Offset of the encoded domain name
284  * @param[in] name NULL-terminated string that holds a domain name
285  * @return TRUE if the NetBIOS names match, else FALSE
286  **/
287 
289  size_t length, size_t pos, const char_t *name)
290 {
291  size_t i;
292  size_t n;
293  char_t c;
294 
295  //Cast the input NBNS message to byte array
296  uint8_t *src = (uint8_t *) message;
297 
298  //Malformed NBNS message?
299  if((pos + 34) >= length)
300  return FALSE;
301 
302  //Retrieve the length of the first label
303  n = src[pos++];
304 
305  //NetBIOS names must be 32-byte long
306  if(n != 32)
307  return FALSE;
308 
309  //Parse the NetBIOS name
310  for(i = 0; i < 15; i++)
311  {
312  //Make sure the characters of the sequence are valid
313  if(src[pos] < 'A' || src[pos] > 'P')
314  return FALSE;
315  if(src[pos + 1] < 'A' || src[pos + 1] > 'P')
316  return FALSE;
317 
318  //Combine nibbles to restore the original ASCII character
319  c = ((src[pos] - 'A') << 4) | (src[pos + 1] - 'A');
320 
321  //Padding character found?
322  if(c == ' ' && *name == '\0')
323  break;
324 
325  //Perform case insensitive comparison
326  if(osToupper(c) != osToupper(*name))
327  return FALSE;
328 
329  //Advance data pointer
330  pos += 2;
331  name++;
332  }
333 
334  //Skip padding characters
335  for(; i < 16; i++)
336  {
337  //Make sure the nibbles are valid
338  if(src[pos] < 'A' || src[pos] > 'P')
339  return FALSE;
340  if(src[pos + 1] < 'A' || src[pos + 1] > 'P')
341  return FALSE;
342 
343  //Advance data pointer
344  pos += 2;
345  }
346 
347  //Retrieve the length of the next label
348  n = src[pos];
349 
350  //NetBIOS names are terminated with a zero length count
351  if(n != 0)
352  return FALSE;
353 
354  //The NetBIOS names match
355  return TRUE;
356 }
357 
358 #endif
359 #endif
void nbnsProcessQuery(NetInterface *interface, const Ipv4PseudoHeader *pseudoHeader, const UdpHeader *udpHeader, const NbnsHeader *message, size_t length)
Process NBNS query message.
uint8_t length
Definition: coap_common.h:193
int bool_t
Definition: compiler_port.h:48
#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:204
__start_packed struct @0 UdpHeader
UDP header.
@ DNS_OPCODE_QUERY
Definition: dns_common.h:81
Structure describing a buffer that spans multiple chunks.
Definition: net_mem.h:89
#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:282
__start_packed struct @0 DnsHeader
DNS message header.
char_t name[]
#define NBNS_PORT
Definition: nbns_common.h:46
size_t length
Definition: ip.h:99
IP pseudo header.
Definition: ip.h:98
#define FALSE
Definition: os_port.h:46
#define osToupper(c)
Definition: os_port.h:251
__start_packed struct @0 NbnsHeader
NBNS message header.
error_t
Error codes.
Definition: error.h:43
Definitions common to NBNS client and NBNS responder.
void * netBufferAt(const NetBuffer *buffer, size_t offset)
Returns a pointer to the data at the specified position.
Definition: net_mem.c:413
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:877
#define Ipv4PseudoHeader
Definition: ipv4.h:39
#define TRACE_INFO(...)
Definition: debug.h:95
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:82
size_t nbnsEncodeName(const char_t *src, uint8_t *dest)
Encode a NetBIOS name.
Definition: nbns_common.c:152
char char_t
Definition: compiler_port.h:43
uint8_t n
#define NBNS_ENCODE_L(c)
Definition: nbns_common.h:50
#define DNS_MESSAGE_MAX_SIZE
Definition: dns_common.h:45
uint8_t message[]
Definition: chap.h:152
error_t nbnsInit(NetInterface *interface)
NBNS related initialization.
Definition: nbns_common.c:55
#define PRIuSIZE
TCP/IP stack core.
@ DNS_RCODE_NO_ERROR
Definition: dns_common.h:95
Data logging functions for debugging purpose (DNS)
Ipv4PseudoHeader ipv4Data
Definition: ip.h:103
@ NO_ERROR
Success.
Definition: error.h:44
uint8_t c
Definition: ndp.h:512
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:288