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-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 #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_ONT_BNODE = 0x0000,
65  NBNS_ONT_PNODE = 0x2000,
66  NBNS_ONT_MNODE = 0x4000,
67  NBNS_G_UNIQUE = 0x0000,
68  NBNS_G_GROUP = 0x8000
70 
71 
72 //CC-RX, CodeWarrior or Win32 compiler?
73 #if defined(__CCRX__)
74  #pragma pack
75 #elif defined(__CWCC__) || defined(_WIN32)
76  #pragma pack(push, 1)
77 #endif
78 
79 
80 /**
81  * @brief NBNS message header
82  **/
83 
85 {
86  uint16_t id; //0-1
87 #if defined(_CPU_BIG_ENDIAN) && !defined(__ICCRX__)
88  uint8_t qr : 1; //2
89  uint8_t opcode : 4;
90  uint8_t aa : 1;
91  uint8_t tc : 1;
92  uint8_t rd : 1;
93  uint8_t ra : 1; //3
94  uint8_t z : 2;
95  uint8_t b : 1;
96  uint8_t rcode : 4;
97 #else
98  uint8_t rd : 1; //2
99  uint8_t tc : 1;
100  uint8_t aa : 1;
101  uint8_t opcode : 4;
102  uint8_t qr : 1;
103  uint8_t rcode : 4; //3
104  uint8_t b : 1;
105  uint8_t z : 2;
106  uint8_t ra : 1;
107 #endif
108  uint16_t qdcount; //4-5
109  uint16_t ancount; //6-7
110  uint16_t nscount; //8-9
111  uint16_t arcount; //10-11
112  uint8_t questions[]; //12
114 
115 
116 /**
117  * @brief NBNS address entry
118  **/
119 
120 typedef __packed_struct
121 {
122  uint16_t flags;
125 
126 
127 //CC-RX, CodeWarrior or Win32 compiler?
128 #if defined(__CCRX__)
129  #pragma unpack
130 #elif defined(__CWCC__) || defined(_WIN32)
131  #pragma pack(pop)
132 #endif
133 
134 //NBNS related functions
135 error_t nbnsInit(NetInterface *interface);
136 
137 void nbnsProcessMessage(NetInterface *interface,
138  const IpPseudoHeader *pseudoHeader, const UdpHeader *udpHeader,
139  const NetBuffer *buffer, size_t offset, const NetRxAncillary *ancillary,
140  void *param);
141 
142 size_t nbnsEncodeName(const char_t *src, uint8_t *dest);
143 
144 size_t nbnsParseName(const NbnsHeader *message,
145  size_t length, size_t pos, char_t *dest);
146 
148  size_t length, size_t pos, const char_t *name);
149 
150 //C++ guard
151 #ifdef __cplusplus
152 }
153 #endif
154 
155 #endif
uint8_t message[]
Definition: chap.h:154
char char_t
Definition: compiler_port.h:48
int bool_t
Definition: compiler_port.h:53
Common DNS routines.
error_t
Error codes.
Definition: error.h:43
uint32_t Ipv4Addr
IPv4 network address.
Definition: ipv4.h:267
uint16_t qdcount
Definition: nbns_common.h:108
uint8_t rd
Definition: nbns_common.h:98
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
uint8_t questions[]
Definition: nbns_common.h:112
NbnsAddrEntry
Definition: nbns_common.h:124
uint16_t ancount
Definition: nbns_common.h:109
size_t nbnsEncodeName(const char_t *src, uint8_t *dest)
Encode a NetBIOS name.
Definition: nbns_common.c:150
uint8_t b
Definition: nbns_common.h:104
uint8_t aa
Definition: nbns_common.h:100
uint8_t qr
Definition: nbns_common.h:102
uint8_t opcode
Definition: nbns_common.h:101
uint8_t tc
Definition: nbns_common.h:99
uint16_t arcount
Definition: nbns_common.h:111
DnsFlags
NBNS flags.
Definition: nbns_common.h:63
@ NBNS_G_GROUP
Definition: nbns_common.h:68
@ NBNS_ONT_BNODE
Definition: nbns_common.h:64
@ NBNS_ONT_MNODE
Definition: nbns_common.h:66
@ NBNS_G_UNIQUE
Definition: nbns_common.h:67
@ NBNS_ONT_PNODE
Definition: nbns_common.h:65
size_t nbnsParseName(const NbnsHeader *message, size_t length, size_t pos, char_t *dest)
Decode a NetBIOS name.
Definition: nbns_common.c:202
uint8_t ra
Definition: nbns_common.h:106
uint16_t nscount
Definition: nbns_common.h:110
NbnsHeader
Definition: nbns_common.h:113
uint8_t z
Definition: nbns_common.h:105
Ipv4Addr addr
Definition: nbns_common.h:123
typedef __packed_struct
NBNS message header.
Definition: nbns_common.h:85
error_t nbnsInit(NetInterface *interface)
NBNS related initialization.
Definition: nbns_common.c:53
uint8_t rcode
Definition: nbns_common.h:103
bool_t nbnsCompareName(const NbnsHeader *message, size_t length, size_t pos, const char_t *name)
Compare NetBIOS names.
Definition: nbns_common.c:286
TCP/IP stack core.
#define NetInterface
Definition: net.h:36
#define NetRxAncillary
Definition: net_misc.h:40
char_t name[]
IP pseudo header.
Definition: ip.h:99
Structure describing a buffer that spans multiple chunks.
Definition: net_mem.h:89
uint8_t length
Definition: tcp.h:368
uint8_t flags
Definition: tcp.h:351
UdpHeader
Definition: udp.h:85