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