NetBurner 3.1
netbios.h
1 /*NB_REVISION*/
2 
3 /*NB_COPYRIGHT*/
4 
5 #ifndef _NETBIOS_H_
6 #define _NETBIOS_H_
7 
8 #include <basictypes.h>
9 #include <buffers.h>
10 #include <nettypes.h>
11 
12 /*
13  ******************************************************************************
14  *
15  * Reference
16  * RFC 883 - Domain Names - Implementation and Specification, November 1983
17  *
18  * RFC 1001 - Protocol standard for a NetBIOS service on a TCP/UDP
19  * transport: Concepts and methods, March 1987
20  * RFC 1002 - Protocol standard for a NetBIOS service on a TCP/UDP
21  * transport: Detailed specifications, March 1987
22  * IANA - WELL KNOWN PORT NUMBERS, July 10, 2008
23  *
24  * Microsoft Help and Support - NetBIOS Suffixes (16th Character of NetBIOS
25  * Name) Article ID: 163409, February 26, 2007 Revision: 4.2
26  *
27  * Note: NetBIOS names are described as "compressed" which is a second level
28  * encoding described in RFC 883. All name comparisons are case
29  * insensitive.
30  *
31  ******************************************************************************
32  */
33 
34 /*
35  ******************************************************************************
36  *
37  * Definitions
38  *
39  ******************************************************************************
40  */
41 
42 /*
43  ******************************************************************************
44  * Microsoft variant (comment to remove)
45  ******************************************************************************
46  */
47 #define NETBIOS_NAME_MICROSOFT_VARIANT (1)
48 
49 /* NetBIOS Name size in upper case alphanumerics */
50 #ifdef NETBIOS_NAME_MICROSOFT_VARIANT
51 #define NETBIOS_NAME_SIZE_IN_CHARS (15)
52 #define NETBIOS_NAME_MICROSOFT_SUFFIX_IN_CHARS (1)
53 #else /* #ifdef NETBIOS_NAME_MICROSOFT_VARIANT */
54 #define NETBIOS_NAME_SIZE_IN_CHARS (16)
55 #endif /* #ifdef NETBIOS_NAME_MICROSOFT_VARIANT */
56 
57 /* NetBIOS name suffixes (Microsoft variant ) */
58 #define NETBIOS_NAME_SUFFIX_WORKSTATION (0x00)
59 
60 /* Operation specifier */
61 #define NETBIOS_OPCODE_QUERY (0)
62 #define NETBIOS_OPCODE_REGISTRATION (5)
63 #define NETBIOS_OPCODE_RELEASE (6)
64 #define NETBIOS_OPCODE_WACK (7)
65 #define NETBIOS_OPCODE_REFRESH (8)
66 
67 /* Owner node type */
68 #define NETBIOS_OWNER_NODE_TYPE_B_NODE (0x0)
69 #define NETBIOS_OWNER_NODE_TYPE_P_NODE (0x1)
70 #define NETBIOS_OWNER_NODE_TYPE_M_NODE (0x2)
71 #define NETBIOS_OWNER_NODE_TYPE_H_NODE (0x3)
72 
73 /*
74  Name first byte bit patterns
75 
76  NETBIOS_NAME_LABEL_NAME
77  NETBIOS_NAME_LABEL_POINTER
78 
79  */
80 #define NETBIOS_NAME_LABEL_NAME (0x20)
81 #define NETBIOS_NAME_LABEL_POINTER (0xC0)
82 
83 /* Compressed name length number of bytes */
84 #define NETBIOS_COMPRESSED_NAME_LENGTH (NETBIOS_NAME_SIZE_IN_CHARS * 2)
85 
86 /*
87  Entry Type
88 
89  NETBIOS_REQUEST_TYPE_NB - General name service resource record
90  NETBIOS_REQUEST_TYPE_NBSTAT - Node status resource record
91 
92  */
93 #define NETBIOS_REQUEST_TYPE_NB (0x0020)
94 #define NETBIOS_REQUEST_TYPE_NBSTAT (0x0021)
95 
96 /*
97  Entry Class
98 
99  NETBIOS_REQUEST_CLASS_IN - Internet class
100 
101  */
102 #define NETBIOS_REQUEST_CLASS_IN (0x0001)
103 
104 /* Infinite TTL */
105 #define NETBIOS_INFINITE_TTL (0)
106 
107 /* Node Status Response Statistics unit ID */
108 #define NETBIOS_UNIT_ID_IN_BYTES (6)
109 
110 /*
111  ******************************************************************************
112  *
113  * Enumerations
114  *
115  ******************************************************************************
116  */
117 /*
118  ******************************************************************************
119  *
120  * Structures
121  *
122  ******************************************************************************
123  */
124 /*
125  Packet type code (OPCODE)
126  r - Response flag, 0 - request, 1 - response
127  opcode - Operation specifier
128 
129  Operation flags (NM_FLAGS)
130  aa - Authorative answer flag,
131  tc - Truncation flag
132  rd - Recursion desired flag
133  ra - Recursion available flag
134  b - Broadcast flag, 0 - Unicast, 1 - Broadcast
135 
136  Result codes (RCODE)
137  rcode - Result code
138 
139 
140  */
141 typedef struct _OpCodeNmFlagsRCode
142 {
143  uint16_t r : 1;
144  uint16_t opcode : 4;
145  uint16_t aa : 1;
146  uint16_t tc : 1;
147  uint16_t rd : 1;
148  uint16_t ra : 1;
149  uint16_t mbz : 2;
150  uint16_t b : 1;
151  uint16_t rcode : 4;
152 
153 } __attribute__((packed)) OpCodeNmFlagsRCode;
154 
155 /*
156  Name Service Packet Header
157 
158  name_trn_id - Transaction id, unique for requestor
159  opcode_nmflags_rcode - Packet type code, operation flags, results
160  qdcount - Question entry count
161  ancount - Resource record count
162  nscount - Authority record count
163  arcount - Additional record count
164 
165 */
166 typedef struct _NameServicePacketHeader
167 {
168  uint16_t name_trn_id;
169  OpCodeNmFlagsRCode opcode_nmflags_rcode;
170  uint16_t qdcount;
171  uint16_t ancount;
172  uint16_t nscount;
173  uint16_t arcount;
174 
175 } __attribute__((packed)) NameServicePacketHeader;
176 
177 /*
178  Name Service Name
179 
180  labelLength - Label length
181  name - Compressed name
182  zero_termination - Zero termination
183 
184 */
185 typedef struct _NameServiceName
186 {
187  uint8_t label_length_count;
188  uint8_t name[NETBIOS_COMPRESSED_NAME_LENGTH];
189 #ifdef NETBIOS_NAME_MICROSOFT_VARIANT
190  uint8_t suffix[(NETBIOS_NAME_MICROSOFT_SUFFIX_IN_CHARS * 2)];
191 #endif /* #ifdef NETBIOS_NAME_MICROSOFT_VARIANT */
192  uint8_t zero_termination;
193 
194 } __attribute__((packed)) NameServiceName;
195 
196 /*
197  Name Service Question Entry
198 
199  name - Name
200  question_type - Request type (rr_type for answer)
201  question_class - Class of request (rr_class for answer)
202 
203 */
204 typedef struct _NameServiceQuestionEntry
205 {
206  NameServiceName name;
207  uint16_t question_type;
208  uint16_t question_class;
209 
210 } __attribute__((packed)) NameServiceQuestionEntry;
211 
212 /*
213  Netbios flags
214  g - Group name flag
215  0 - Unique name, 1 - Group name
216  ont - Owner node type
217 
218  */
219 typedef struct _NbFlags
220 {
221  uint16_t g : 1;
222  uint16_t ont : 2;
223  uint16_t mbz : 13;
224 
225 } __attribute__((packed)) NbFlags;
226 
227 /*
228  Name Service Resource Record Header (Name Pointer)
229 
230  rr_type - Request type
231  rr_class - Class of request
232 
233  */
234 typedef struct _NameServiceResourceRecordNamePointerHeader
235 {
236  uint8_t rr_nameLabel;
237  uint8_t rr_nameIndex;
238  uint16_t rr_type;
239  uint16_t rr_class;
240 
241 } __attribute__((packed)) NameServiceResourceRecordNamePointerHeader;
242 
243 /*
244  Name Service Resource Record Trailer
245 
246  rr_type - Request type
247  rr_class - Class of request
248  ttl - Time to live for resource name
249  rdlength - Bytes in RDATA field
250  nb_flags - Flags (RDATA)
251  nb_address - Address
252 
253  */
254 typedef struct _NameServiceResourceRecordTrailer
255 {
256  uint32_t ttl;
257  uint16_t rdlength;
258  NbFlags nb_flags;
259  uint32_t nb_address;
260 
261 } __attribute__((packed)) NameServiceResourceRecordTrailer;
262 
263 /*
264  Node Entry Name Flags
265  g - Group name flag
266  0 - Unique name, 1 - Group name
267  ont - Owner node type
268  drg - Deregister flag
269  0 - None, 1 - Name is delete process
270  cnf - Conflict flag
271  0 - None, 1 - Name in conflict
272  act - Active name flag (must be one)
273  prm - Permanent name flag
274  1 - permanent node name, 0 - all others
275 
276  */
277 typedef struct _NameFlags
278 {
279  uint16_t g : 1;
280  uint16_t ont : 2;
281  uint16_t drg : 1;
282  uint16_t cnf : 1;
283  uint16_t act : 1;
284  uint16_t prm : 1;
285  uint16_t mbz : 9;
286 
287 } __attribute__((packed)) Name_Flags;
288 
289 /*
290  Node Name Entry
291 
292  name - ASCII name
293  question_type - Request type (rr_type for answer)
294  question_class - Class of request (rr_class for answer)
295 
296 */
297 typedef struct _NodeNameEntry
298 {
299  char name[NETBIOS_NAME_SIZE_IN_CHARS];
300 #ifdef NETBIOS_NAME_MICROSOFT_VARIANT
301  char suffix;
302 #endif /* #ifdef NETBIOS_NAME_MICROSOFT_VARIANT */
303  Name_Flags name_flags;
304 
305 } __attribute__((packed)) NodeNameEntry;
306 
307 /*
308  Name Service Node Status Response Trailer
309 
310  ttl - Time to live for resource name
311  rdlength - Bytes in RDATA field
312  num_names - Number of names
313 
314  */
315 typedef struct _NameServiceNodeStatusResponseTrailer
316 {
317  uint32_t ttl;
318  uint16_t rdlength;
319  uint8_t num_names;
320 
321 } __attribute__((packed)) NameServiceNodeStatusResponseTrailer;
322 
323 /*
324  Name Service Node Status Response Statistics
325 
326  unit_id - Unique unit id
327  jumpers - jumpers
328  test_result - Test results
329  version_number - Version
330  period_of_statistics - Period
331  number_of_crc - CRCs
332  number_alignment_errors - Alignment errors
333  number_of_collisions - Collisions
334  number_send_aborts - Send aborts
335  number_good_sends - Good sends
336  number_good_receives - Good receives
337  number_retransmits - Re-transmits
338  number_of_conditions - Resource conditions
339  number_free_command_blocks - Number of free command blocks
340  total_number_command_blocks - Number of command blocks
341  nax_total_command_blocks - Maxmimum number of command blocks
342  number_pending_sessions - Pending sessions
343  max_number_pending_sessions - Maximum pending sessions
344  max_total_pending_sessions - Maximum total pending sessions
345  session_data_packet_size - Session packet size
346 
347  */
348 typedef struct _NameServiceNodeStatusResponseStatistics
349 {
350  uint8_t unit_id[NETBIOS_UNIT_ID_IN_BYTES];
351  uint8_t jumpers;
352  uint8_t test_result;
353  uint16_t version_number;
354  uint16_t number_of_crc;
355  uint16_t period_of_statistics;
356  uint16_t number_alignment_errors;
357  uint16_t number_of_collisions;
358  uint16_t number_send_aborts;
359  uint32_t number_good_sends;
360  uint32_t number_good_receives;
361  uint16_t number_retransmits;
362  uint16_t number_of_conditions;
363  uint16_t number_free_command_blocks;
364  uint16_t total_number_command_blocks;
365  uint16_t nax_total_command_blocks;
366  uint16_t number_pending_sessions;
367  uint16_t max_number_pending_sessions;
368  uint16_t max_total_pending_sessions;
369  uint16_t session_data_packet_size;
370 
371 } __attribute__((packed)) NameServiceNodeStatusResponseStatistics;
372 
373 /*
374  ******************************************************************************
375  *
376  * NetBIOS callback declaration
377  *
378  ******************************************************************************
379  */
380 typedef void(UdpNetbiosNameServiceFunc)(PoolPtr poolPtr);
381 extern UdpNetbiosNameServiceFunc *UdpNetbiosNameServicePtr;
382 
383 /*
384  ******************************************************************************
385  *
386  * netbios "C" Library Interface
387  *
388  ******************************************************************************
389  */
390 
391 /*
392  *****************************************************************************
393  *
394  * Runtime Library Routines
395  *
396  *****************************************************************************
397  */
398 
399 /*
400  ******************************************************************************
401 
402  Convert string into NETBIOS name
403 
404  Parameters:
405  netBIOSnamePtr - Correctly formed name
406  namePtr - Name to convert
407  netBIOSnameSize - NetBIOS name (netBIOSnamePtr) size in bytes
408 
409  Return:
410  None
411 
412  Notes:
413  Name can contain any alphanumeric character except spaces
414  and \ : / * ? ; | . and it will be truncated if greater than 15
415  characters. The resulting name is all uppercase all excepted characters
416  will be set to '0'.
417 
418  ******************************************************************************
419  */
420 void NetbiosConvertName(char *netBIOSnamePtr, const char *namePtr, int netBIOSnameSize);
421 
422 /*
423  ******************************************************************************
424 
425  Get NETBIOS name in ASCII formatted IAW RFC 883
426 
427  Parameters:
428  netBIOSnamePtr - Correctly formed name
429  netBIOSnameSize - NetBIOS name size capacity in bytes
430  should be at least
431  NETBIOS_NAME_SIZE_IN_CHARS + 1
432 
433  Return:
434  Number of bytes in name
435 
436  Notes:
437  Name can contain any alphanumeric character except spaces
438  and \ : / * ? ; | . and it will be truncated if greater than 15
439  characters. The resulting name is all uppercase all excepted characters
440  will be set to '0'.
441 
442  ******************************************************************************
443  */
444 int NetbiosGetNetbiosName(char *netBIOSnamePtr, int netBIOSnameSize);
445 
446 /*
447  ******************************************************************************
448 
449  Enable the NETBIOS name service
450 
451  Parameters:
452  name - Name to register, NULL pointer disables service
453  initialRegister - Send registration packet upon enabling.
454 
455  Return:
456  None
457 
458  Notes:
459  Enable name service responding to name service queries.
460  Name can contain any alphanumeric character except spaces
461  and \ : / * ? ; | . and it will be truncated if greater than 15
462  characters.
463 
464  ******************************************************************************
465  */
466 void NetbiosEnableNameService(const char *name, BOOL initialRegister);
467 
468 #endif
NetBurner Buffers API.