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-2019 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 1.9.6
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 <ctype.h>
38 #include "core/net.h"
39 #include "netbios/nbns_client.h"
40 #include "netbios/nbns_responder.h"
41 #include "netbios/nbns_common.h"
42 #include "dns/dns_debug.h"
43 #include "debug.h"
44 
45 //Check TCP/IP stack configuration
46 #if (NBNS_CLIENT_SUPPORT == ENABLED || NBNS_RESPONDER_SUPPORT == ENABLED)
47 #if (IPV4_SUPPORT == ENABLED)
48 
49 
50 /**
51  * @brief NBNS related initialization
52  * @param[in] interface Underlying network interface
53  * @return Error code
54  **/
55 
57 {
58  error_t error;
59 
60  //Callback function to be called when a NBNS message is received
61  error = udpAttachRxCallback(interface, NBNS_PORT, nbnsProcessMessage, NULL);
62  //Any error to report?
63  if(error)
64  return error;
65 
66  //Successful initialization
67  return NO_ERROR;
68 }
69 
70 
71 /**
72  * @brief Process incoming NBNS message
73  * @param[in] interface Underlying network interface
74  * @param[in] pseudoHeader UDP pseudo header
75  * @param[in] udpHeader UDP header
76  * @param[in] buffer Multi-part buffer containing the incoming NBNS message
77  * @param[in] offset Offset to the first byte of the NBNS message
78  * @param[in] param Callback function parameter (not used)
79  **/
80 
81 void nbnsProcessMessage(NetInterface *interface, const IpPseudoHeader *pseudoHeader,
82  const UdpHeader *udpHeader, const NetBuffer *buffer, size_t offset, void *param)
83 {
84  size_t length;
86 
87  //Make sure the NBNS message was received from an IPv4 peer
88  if(pseudoHeader->length != sizeof(Ipv4PseudoHeader))
89  return;
90 
91  //Retrieve the length of the NBNS message
92  length = netBufferGetLength(buffer) - offset;
93 
94  //Ensure the NBNS message is valid
95  if(length < sizeof(NbnsHeader))
96  return;
98  return;
99 
100  //Point to the NBNS message header
101  message = netBufferAt(buffer, offset);
102  //Sanity check
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_NO_ERROR)
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  //Parse input name
162  for(i = 0; i < 15 && src[i] != '\0'; i++)
163  {
164  //Convert current character to uppercase
165  c = toupper((uint8_t) src[i]);
166 
167  //Encode character
168  dest[j++] = NBNS_ENCODE_H(c);
169  dest[j++] = NBNS_ENCODE_L(c);
170  }
171 
172  //Pad NetBIOS name with space characters
173  for(; i < 15; i++)
174  {
175  //Encoded space character
176  dest[j++] = NBNS_ENCODE_H(' ');
177  dest[j++] = NBNS_ENCODE_L(' ');
178  }
179 
180  //The 16th character is the NetBIOS suffix
181  dest[j++] = NBNS_ENCODE_H(0);
182  dest[j++] = NBNS_ENCODE_L(0);
183 
184  //Terminate the NetBIOS name with a zero length count
185  dest[j++] = 0;
186 
187  //Return the length of the encoded NetBIOS name
188  return j;
189 }
190 
191 
192 /**
193  * @brief Decode a NetBIOS name
194  * @param[in] message Pointer to the NBNS message
195  * @param[in] length Length of the NBNS message
196  * @param[in] pos Offset of the name to decode
197  * @param[out] dest Pointer to the decoded name (optional)
198  * @return The position of the resource record that immediately follows the NetBIOS name
199  **/
200 
202  size_t length, size_t pos, char_t *dest)
203 {
204  size_t i;
205  size_t n;
206  char_t c;
207 
208  //Cast the input NBNS message to byte array
209  uint8_t *src = (uint8_t *) message;
210 
211  //Malformed NBNS message?
212  if((pos + 34) >= length)
213  return 0;
214 
215  //Retrieve the length of the first label
216  n = src[pos++];
217 
218  //NetBIOS names must be 32-byte long
219  if(n != 32)
220  return 0;
221 
222  //Parse the NetBIOS name
223  for(i = 0; i < 15; i++)
224  {
225  //Make sure the characters of the sequence are valid
226  if(src[pos] < 'A' || src[pos] > 'P')
227  return 0;
228  if(src[pos + 1] < 'A' || src[pos + 1] > 'P')
229  return 0;
230 
231  //Combine nibbles to restore the original ASCII character
232  c = ((src[pos] - 'A') << 4) | (src[pos + 1] - 'A');
233 
234  //Padding character found?
235  if(c == ' ')
236  break;
237 
238  //Save current ASCII character
239  if(dest != NULL)
240  *(dest++) = c;
241 
242  //Advance data pointer
243  pos += 2;
244  }
245 
246  //Skip padding characters
247  for(; i < 16; i++)
248  {
249  //Make sure the nibbles are valid
250  if(src[pos] < 'A' || src[pos] > 'P')
251  return 0;
252  if(src[pos + 1] < 'A' || src[pos + 1] > 'P')
253  return 0;
254 
255  //Advance data pointer
256  pos += 2;
257  }
258 
259  //Retrieve the length of the next label
260  n = src[pos++];
261 
262  //NetBIOS names are terminated with a zero length count
263  if(n != 0)
264  return 0;
265 
266  //Properly terminate the string
267  if(dest != NULL)
268  *(dest++) = '\0';
269 
270  //Return the position of the resource record that
271  //is immediately following the NetBIOS name
272  return pos;
273 }
274 
275 
276 /**
277  * @brief Compare NetBIOS names
278  * @param[in] message Pointer to the NBNS message
279  * @param[in] length Length of the NBNS message
280  * @param[in] pos Offset of the encoded domain name
281  * @param[in] name NULL-terminated string that holds a domain name
282  * @return TRUE if the NetBIOS names match, else FALSE
283  **/
284 
286  size_t length, size_t pos, const char_t *name)
287 {
288  size_t i;
289  size_t n;
290  char_t c;
291 
292  //Cast the input NBNS message to byte array
293  uint8_t *src = (uint8_t *) message;
294 
295  //Malformed NBNS message?
296  if((pos + 34) >= length)
297  return FALSE;
298 
299  //Retrieve the length of the first label
300  n = src[pos++];
301 
302  //NetBIOS names must be 32-byte long
303  if(n != 32)
304  return FALSE;
305 
306  //Parse the NetBIOS name
307  for(i = 0; i < 15; i++)
308  {
309  //Make sure the characters of the sequence are valid
310  if(src[pos] < 'A' || src[pos] > 'P')
311  return FALSE;
312  if(src[pos + 1] < 'A' || src[pos + 1] > 'P')
313  return FALSE;
314 
315  //Combine nibbles to restore the original ASCII character
316  c = ((src[pos] - 'A') << 4) | (src[pos + 1] - 'A');
317 
318  //Padding character found?
319  if(c == ' ' && *name == '\0')
320  break;
321 
322  //Perform case insensitive comparison
323  if(toupper((uint8_t) c) != toupper((uint8_t) *name))
324  return FALSE;
325 
326  //Advance data pointer
327  pos += 2;
328  name++;
329  }
330 
331  //Skip padding characters
332  for(; i < 16; i++)
333  {
334  //Make sure the nibbles are valid
335  if(src[pos] < 'A' || src[pos] > 'P')
336  return FALSE;
337  if(src[pos + 1] < 'A' || src[pos + 1] > 'P')
338  return FALSE;
339 
340  //Advance data pointer
341  pos += 2;
342  }
343 
344  //Retrieve the length of the next label
345  n = src[pos];
346 
347  //NetBIOS names are terminated with a zero length count
348  if(n != 0)
349  return FALSE;
350 
351  //The NetBIOS names match
352  return TRUE;
353 }
354 
355 #endif
356 #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: dtls_misc.h:149
int bool_t
Definition: compiler_port.h:49
#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:201
@ DNS_OPCODE_QUERY
Definition: dns_common.h:81
Structure describing a buffer that spans multiple chunks.
Definition: net_mem.h:88
#define TRUE
Definition: os_port.h:50
__start_packed struct @146 DnsHeader
DNS message header.
void nbnsProcessResponse(NetInterface *interface, const Ipv4PseudoHeader *pseudoHeader, const UdpHeader *udpHeader, const NbnsHeader *message, size_t length)
Process NBNS response message.
Definition: nbns_client.c:277
char_t name[]
#define NBNS_PORT
Definition: nbns_common.h:46
size_t length
Definition: ip.h:92
IP pseudo header.
Definition: ip.h:90
#define FALSE
Definition: os_port.h:46
error_t
Error codes.
Definition: error.h:42
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 NetInterface
Definition: net.h:36
error_t udpAttachRxCallback(NetInterface *interface, uint16_t port, UdpRxCallback callback, void *param)
Register user callback.
Definition: udp.c:764
__start_packed struct @122 UdpHeader
UDP header.
#define Ipv4PseudoHeader
Definition: ipv4.h:39
#define TRACE_INFO(...)
Definition: debug.h:94
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
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: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:56
#define PRIuSIZE
Definition: compiler_port.h:78
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:96
@ NO_ERROR
Success.
Definition: error.h:44
uint8_t c
Definition: ndp.h:513
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:285
__start_packed struct @260 NbnsHeader
NBNS message header.
void nbnsProcessMessage(NetInterface *interface, const IpPseudoHeader *pseudoHeader, const UdpHeader *udpHeader, const NetBuffer *buffer, size_t offset, void *param)
Process incoming NBNS message.
Definition: nbns_common.c:81