nbns_common.h
Go to the documentation of this file.
1 /**
2  * @file nbns_common.h
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 #ifndef _NBNS_COMMON_H
32 #define _NBNS_COMMON_H
33 
34 //Dependencies
35 #include "core/net.h"
36 #include "dns/dns_common.h"
37 
38 //Default resource record TTL (cache lifetime)
39 #ifndef NBNS_DEFAULT_RESOURCE_RECORD_TTL
40  #define NBNS_DEFAULT_RESOURCE_RECORD_TTL 120
41 #elif (NBNS_DEFAULT_RESOURCE_RECORD_TTL < 1)
42  #error NBNS_DEFAULT_RESOURCE_RECORD_TTL parameter is not valid
43 #endif
44 
45 //NBNS port number
46 #define NBNS_PORT 137
47 
48 //Macro definition
49 #define NBNS_ENCODE_H(c) ('A' + (((c) >> 4) & 0x0F))
50 #define NBNS_ENCODE_L(c) ('A' + ((c) & 0x0F))
51 
52 //C++ guard
53 #ifdef __cplusplus
54 extern "C" {
55 #endif
56 
57 
58 /**
59  * @brief NBNS flags
60  **/
61 
62 typedef enum
63 {
64  NBNS_FLAG_ONT = 0x6000, ///<Owner node type
65  NBNS_FLAG_ONT_BNODE = 0x0000, ///<Owner node type (B node)
66  NBNS_FLAG_ONT_PNODE = 0x2000, ///<Owner node type (P node)
67  NBNS_FLAG_ONT_MNODE = 0x4000, ///<Owner node type (M node)
68  NBNS_FLAG_G = 0x8000 ///<Group name flag
70 
71 
72 /**
73  * @brief NBNS name flags
74  **/
75 
76 typedef enum
77 {
78  NBNS_NAME_FLAG_PRM = 0x0200, ///<Permanent name flag
79  NBNS_NAME_FLAG_ACT = 0x0400, ///<Active name flag
80  NBNS_NAME_FLAG_CNF = 0x0800, ///<Conflict flag
81  NBNS_NAME_FLAG_DRG = 0x1000, ///<Deregister flag
82  NBNS_NAME_FLAG_ONT = 0x6000, ///<Owner node type
83  NBNS_NAME_FLAG_ONT_BNODE = 0x0000, ///<Owner node type (B node)
84  NBNS_NAME_FLAG_ONT_PNODE = 0x2000, ///<Owner node type (P node)
85  NBNS_NAME_FLAG_ONT_MNODE = 0x4000, ///<Owner node type (M node)
86  NBNS_NAME_FLAG_G = 0x8000 ///<Group name flag
88 
89 
90 //CC-RX, CodeWarrior or Win32 compiler?
91 #if defined(__CCRX__)
92  #pragma pack
93 #elif defined(__CWCC__) || defined(_WIN32)
94  #pragma pack(push, 1)
95 #endif
96 
97 
98 /**
99  * @brief NBNS message header
100  **/
101 
103 {
104  uint16_t id; //0-1
105 #if defined(_CPU_BIG_ENDIAN) && !defined(__ICCRX__)
106  uint8_t qr : 1; //2
107  uint8_t opcode : 4;
108  uint8_t aa : 1;
109  uint8_t tc : 1;
110  uint8_t rd : 1;
111  uint8_t ra : 1; //3
112  uint8_t z : 2;
113  uint8_t b : 1;
114  uint8_t rcode : 4;
115 #else
116  uint8_t rd : 1; //2
117  uint8_t tc : 1;
118  uint8_t aa : 1;
119  uint8_t opcode : 4;
120  uint8_t qr : 1;
121  uint8_t rcode : 4; //3
122  uint8_t b : 1;
123  uint8_t z : 2;
124  uint8_t ra : 1;
125 #endif
126  uint16_t qdcount; //4-5
127  uint16_t ancount; //6-7
128  uint16_t nscount; //8-9
129  uint16_t arcount; //10-11
130  uint8_t questions[]; //12
132 
133 
134 /**
135  * @brief Address entry
136  **/
137 
138 typedef __packed_struct
139 {
140  uint16_t flags;
143 
144 
145 /**
146  * @brief Node name entry
147  **/
148 
149 typedef __packed_struct
150 {
151  uint8_t name[16];
152  uint16_t flags;
154 
155 
156 /**
157  * @brief Node name array
158  **/
159 
160 typedef __packed_struct
161 {
162  uint8_t numNames;
165 
166 
167 /**
168  * @brief Statistics
169  **/
170 
171 typedef __packed_struct
172 {
173  MacAddr unitId;
174  uint8_t jumpers;
175  uint8_t testResult;
176  uint16_t versionNumber;
178  uint16_t numberOfCrcs;
182  uint32_t numberGoodSends;
194 
195 
196 //CC-RX, CodeWarrior or Win32 compiler?
197 #if defined(__CCRX__)
198  #pragma unpack
199 #elif defined(__CWCC__) || defined(_WIN32)
200  #pragma pack(pop)
201 #endif
202 
203 //NBNS related functions
204 error_t nbnsInit(NetInterface *interface);
205 
206 void nbnsProcessMessage(NetInterface *interface,
207  const IpPseudoHeader *pseudoHeader, const UdpHeader *udpHeader,
208  const NetBuffer *buffer, size_t offset, const NetRxAncillary *ancillary,
209  void *param);
210 
211 size_t nbnsEncodeName(const char_t *src, uint8_t *dest);
212 
213 size_t nbnsParseName(const NbnsHeader *message, size_t length, size_t pos,
214  char_t *dest);
215 
216 bool_t nbnsCompareName(const NbnsHeader *message, size_t length, size_t pos,
217  const char_t *name);
218 
219 //C++ guard
220 #ifdef __cplusplus
221 }
222 #endif
223 
224 #endif
bool_t nbnsCompareName(const NbnsHeader *message, size_t length, size_t pos, const char_t *name)
Compare NetBIOS names.
Definition: nbns_common.c:316
int bool_t
Definition: compiler_port.h:61
uint8_t b
Definition: nbns_common.h:122
NbnsNameFlags
NBNS name flags.
Definition: nbns_common.h:77
NbnsNodeNameArray
Definition: nbns_common.h:164
uint8_t testResult
Definition: nbns_common.h:175
uint16_t numberSendAborts
Definition: nbns_common.h:181
uint16_t ancount
Definition: nbns_common.h:127
uint8_t ra
Definition: nbns_common.h:124
error_t nbnsInit(NetInterface *interface)
NBNS related initialization.
Definition: nbns_common.c:53
Structure describing a buffer that spans multiple chunks.
Definition: net_mem.h:89
uint8_t message[]
Definition: chap.h:154
NbnsAddrEntry
Definition: nbns_common.h:142
NbnsFlags
NBNS flags.
Definition: nbns_common.h:63
@ NBNS_FLAG_G
Group name flag.
Definition: nbns_common.h:68
uint16_t maxNumberPendingSessions
Definition: nbns_common.h:190
@ NBNS_FLAG_ONT_PNODE
Owner node type (P node)
Definition: nbns_common.h:66
char_t name[]
NbnsNodeNameEntry
Definition: nbns_common.h:153
uint8_t z
Definition: nbns_common.h:123
uint8_t rcode
Definition: nbns_common.h:121
uint32_t Ipv4Addr
IPv4 network address.
Definition: ipv4.h:298
@ NBNS_NAME_FLAG_PRM
Permanent name flag.
Definition: nbns_common.h:78
size_t nbnsParseName(const NbnsHeader *message, size_t length, size_t pos, char_t *dest)
Decode a NetBIOS name.
Definition: nbns_common.c:222
IP pseudo header.
Definition: ip.h:110
uint16_t totalNumberCommandBlocks
Definition: nbns_common.h:187
uint16_t numberOfCrcs
Definition: nbns_common.h:178
@ NBNS_FLAG_ONT_BNODE
Owner node type (B node)
Definition: nbns_common.h:65
uint16_t sessionDataPacketSize
Definition: nbns_common.h:192
NbnsHeader
Definition: nbns_common.h:131
error_t
Error codes.
Definition: error.h:43
@ NBNS_FLAG_ONT
Owner node type.
Definition: nbns_common.h:64
uint16_t numberNoResourceConditions
Definition: nbns_common.h:185
@ NBNS_NAME_FLAG_ACT
Active name flag.
Definition: nbns_common.h:79
uint16_t nscount
Definition: nbns_common.h:128
@ NBNS_FLAG_ONT_MNODE
Owner node type (M node)
Definition: nbns_common.h:67
uint16_t numberAlignmentErrors
Definition: nbns_common.h:179
@ NBNS_NAME_FLAG_DRG
Deregister flag.
Definition: nbns_common.h:81
uint16_t numberPendingSessions
Definition: nbns_common.h:189
uint8_t tc
Definition: nbns_common.h:117
#define NetRxAncillary
Definition: net_misc.h:40
#define NetInterface
Definition: net.h:36
uint8_t questions[]
Definition: nbns_common.h:130
uint8_t qr
Definition: nbns_common.h:120
@ NBNS_NAME_FLAG_CNF
Conflict flag.
Definition: nbns_common.h:80
uint16_t maxTotalNumberCommandBlocks
Definition: nbns_common.h:188
uint8_t opcode
Definition: nbns_common.h:119
uint8_t length
Definition: tcp.h:375
uint16_t numberFreeCommandBlocks
Definition: nbns_common.h:186
MacAddr
Definition: ethernet.h:197
uint32_t numberGoodSends
Definition: nbns_common.h:182
uint16_t arcount
Definition: nbns_common.h:129
UdpHeader
Definition: udp.h:85
uint16_t versionNumber
Definition: nbns_common.h:176
char char_t
Definition: compiler_port.h:55
uint8_t unitId
uint8_t jumpers
Definition: nbns_common.h:174
NbnsStatistics
Definition: nbns_common.h:193
uint16_t qdcount
Definition: nbns_common.h:126
uint8_t rd
Definition: nbns_common.h:116
uint16_t numberOfCollisions
Definition: nbns_common.h:180
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
uint16_t periodOfStatistics
Definition: nbns_common.h:177
uint8_t aa
Definition: nbns_common.h:118
uint16_t numberRetransmits
Definition: nbns_common.h:184
NbnsNodeNameEntry names[]
Definition: nbns_common.h:163
Ipv4Addr addr
Definition: nbns_common.h:141
uint32_t numberGoodReceives
Definition: nbns_common.h:183
TCP/IP stack core.
@ NBNS_NAME_FLAG_ONT_BNODE
Owner node type (B node)
Definition: nbns_common.h:83
Common DNS routines.
@ NBNS_NAME_FLAG_G
Group name flag.
Definition: nbns_common.h:86
size_t nbnsEncodeName(const char_t *src, uint8_t *dest)
Encode a NetBIOS name.
Definition: nbns_common.c:148
uint16_t flags
Definition: nbns_common.h:152
uint16_t maxTotalSessionsPossible
Definition: nbns_common.h:191
@ NBNS_NAME_FLAG_ONT
Owner node type.
Definition: nbns_common.h:82
typedef __packed_struct
NBNS message header.
Definition: nbns_common.h:103
@ NBNS_NAME_FLAG_ONT_PNODE
Owner node type (P node)
Definition: nbns_common.h:84
@ NBNS_NAME_FLAG_ONT_MNODE
Owner node type (M node)
Definition: nbns_common.h:85