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  * Copyright (C) 2010-2018 Oryx Embedded SARL. All rights reserved.
8  *
9  * This file is part of CycloneTCP Open.
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License
13  * as published by the Free Software Foundation; either version 2
14  * of the License, or (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software Foundation,
23  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
24  *
25  * @author Oryx Embedded SARL (www.oryx-embedded.com)
26  * @version 1.9.0
27  **/
28 
29 #ifndef _NBNS_COMMON_H
30 #define _NBNS_COMMON_H
31 
32 //Dependencies
33 #include "core/net.h"
34 #include "dns/dns_common.h"
35 
36 //Default TTL value for NBNS resource records
37 #ifndef NBNS_DEFAULT_RESOURCE_RECORD_TTL
38  #define NBNS_DEFAULT_RESOURCE_RECORD_TTL 120
39 #elif (NBNS_DEFAULT_RESOURCE_RECORD_TTL < 1)
40  #error NBNS_DEFAULT_RESOURCE_RECORD_TTL parameter is not valid
41 #endif
42 
43 //NBNS port number
44 #define NBNS_PORT 137
45 
46 //Macro definition
47 #define NBNS_ENCODE_H(c) ('A' + (((c) >> 4) & 0x0F))
48 #define NBNS_ENCODE_L(c) ('A' + ((c) & 0x0F))
49 
50 //C++ guard
51 #ifdef __cplusplus
52  extern "C" {
53 #endif
54 
55 
56 /**
57  * @brief NBNS flags
58  **/
59 
60 typedef enum
61 {
62  NBNS_ONT_BNODE = 0x0000,
63  NBNS_ONT_PNODE = 0x2000,
64  NBNS_ONT_MNODE = 0x4000,
65  NBNS_G_UNIQUE = 0x0000,
66  NBNS_G_GROUP = 0x8000
67 }DnsFlags;
68 
69 
70 //CodeWarrior or Win32 compiler?
71 #if defined(__CWCC__) || defined(_WIN32)
72  #pragma pack(push, 1)
73 #endif
74 
75 
76 /**
77  * @brief NBNS message header
78  **/
79 
80 typedef __start_packed struct
81 {
82  uint16_t id; //0-1
83 #ifdef _CPU_BIG_ENDIAN
84  uint16_t qr : 1; //2
85  uint16_t opcode : 4;
86  uint16_t aa : 1;
87  uint16_t tc : 1;
88  uint16_t rd : 1;
89  uint16_t ra : 1; //3
90  uint16_t z : 2;
91  uint16_t b : 1;
92  uint16_t rcode : 4;
93 #else
94  uint16_t rd : 1; //2
95  uint16_t tc : 1;
96  uint16_t aa : 1;
97  uint16_t opcode : 4;
98  uint16_t qr : 1;
99  uint16_t rcode : 4; //3
100  uint16_t b : 1;
101  uint16_t z : 2;
102  uint16_t ra : 1;
103 #endif
104  uint16_t qdcount; //4-5
105  uint16_t ancount; //6-7
106  uint16_t nscount; //8-9
107  uint16_t arcount; //10-11
108  uint8_t questions[]; //12
110 
111 
112 /**
113  * @brief NBNS address entry
114  **/
115 
116 typedef __start_packed struct
117 {
118  uint16_t flags;
121 
122 
123 //CodeWarrior or Win32 compiler?
124 #if defined(__CWCC__) || defined(_WIN32)
125  #pragma pack(pop)
126 #endif
127 
128 
129 //NBNS related functions
130 error_t nbnsInit(NetInterface *interface);
131 
132 void nbnsProcessMessage(NetInterface *interface, const IpPseudoHeader *pseudoHeader,
133  const UdpHeader *udpHeader, const NetBuffer *buffer, size_t offset, void *param);
134 
135 size_t nbnsEncodeName(const char_t *src, uint8_t *dest);
136 
137 size_t nbnsParseName(const NbnsHeader *message,
138  size_t length, size_t pos, char_t *dest);
139 
141  size_t length, size_t pos, const char_t *name);
142 
143 //C++ guard
144 #ifdef __cplusplus
145  }
146 #endif
147 
148 #endif
uint32_t Ipv4Addr
IPv4 network address.
Definition: ipv4.h:232
uint16_t nscount
Definition: nbns_common.h:106
char char_t
Definition: compiler_port.h:41
TCP/IP stack core.
uint16_t rcode
Definition: nbns_common.h:99
uint16_t rd
Definition: nbns_common.h:94
uint8_t message[]
Definition: chap.h:150
size_t nbnsEncodeName(const char_t *src, uint8_t *dest)
Encode a NetBIOS name.
Definition: nbns_common.c:144
uint16_t arcount
Definition: nbns_common.h:107
DnsFlags
NBNS flags.
Definition: nbns_common.h:60
size_t nbnsParseName(const NbnsHeader *message, size_t length, size_t pos, char_t *dest)
Decode a NetBIOS name.
Definition: nbns_common.c:196
uint16_t ancount
Definition: nbns_common.h:105
uint8_t questions[]
Definition: nbns_common.h:108
uint16_t aa
Definition: nbns_common.h:96
error_t nbnsInit(NetInterface *interface)
NBNS related initialization.
Definition: nbns_common.c:54
char_t name[]
uint16_t qr
Definition: nbns_common.h:98
__start_packed struct @263 NbnsHeader
NBNS message header.
__start_packed struct @126 UdpHeader
UDP 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:79
Structure describing a buffer that spans multiple chunks.
Definition: net_mem.h:86
uint16_t b
Definition: nbns_common.h:100
uint16_t id
Definition: nbns_common.h:82
uint16_t flags
Definition: nbns_common.h:118
uint16_t ra
Definition: nbns_common.h:102
__start_packed struct _Ipv4Header __end_packed
bool_t nbnsCompareName(const NbnsHeader *message, size_t length, size_t pos, const char_t *name)
Compare NetBIOS names.
Definition: nbns_common.c:280
error_t
Error codes.
Definition: error.h:40
#define NetInterface
Definition: net.h:34
uint16_t z
Definition: nbns_common.h:101
__start_packed struct @264 NbnsAddrEntry
NBNS address entry.
IP pseudo header.
Definition: ip.h:76
uint16_t qdcount
Definition: nbns_common.h:104
uint8_t length
Definition: dtls_misc.h:140
Common DNS routines.
uint16_t opcode
Definition: nbns_common.h:97
int bool_t
Definition: compiler_port.h:47
uint16_t tc
Definition: nbns_common.h:95
Ipv4Addr addr
Definition: nbns_common.h:119