1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
|
// This code is derived from jcifs smb client library <jcifs at samba dot org>
// Ported by J. Arturo <webmaster at komodosoft dot net>
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
using System.Net;
using SharpCifs.Util;
using SharpCifs.Util.Sharpen;
namespace SharpCifs.Netbios
{
internal abstract class NameServicePacket
{
internal const int Query = 0;
internal const int Wack = 7;
internal const int FmtErr = 0x1;
internal const int SrvErr = 0x2;
internal const int ImpErr = 0x4;
internal const int RfsErr = 0x5;
internal const int ActErr = 0x6;
internal const int CftErr = 0x7;
internal const int NbIn = 0x00200001;
internal const int NbstatIn = 0x00210001;
internal const int Nb = 0x0020;
internal const int Nbstat = 0x0021;
internal const int In = 0x0001;
internal const int A = 0x0001;
internal const int Ns = 0x0002;
internal const int Null = 0x000a;
internal const int HeaderLength = 12;
internal const int OpcodeOffset = 2;
internal const int QuestionOffset = 4;
internal const int AnswerOffset = 6;
internal const int AuthorityOffset = 8;
internal const int AdditionalOffset = 10;
// opcode
// rcode
// type/class
// header field offsets
internal static void WriteInt2(int val, byte[] dst, int dstIndex)
{
dst[dstIndex++] = unchecked((byte)((val >> 8) & unchecked(0xFF)));
dst[dstIndex] = unchecked((byte)(val & unchecked(0xFF)));
}
internal static void WriteInt4(int val, byte[] dst, int dstIndex)
{
dst[dstIndex++] = unchecked((byte)((val >> 24) & unchecked(0xFF)));
dst[dstIndex++] = unchecked((byte)((val >> 16) & unchecked(0xFF)));
dst[dstIndex++] = unchecked((byte)((val >> 8) & unchecked(0xFF)));
dst[dstIndex] = unchecked((byte)(val & unchecked(0xFF)));
}
internal static int ReadInt2(byte[] src, int srcIndex)
{
return ((src[srcIndex] & unchecked(0xFF)) << 8) + (src[srcIndex + 1] & unchecked(
0xFF));
}
internal static int ReadInt4(byte[] src, int srcIndex)
{
return ((src[srcIndex] & unchecked(0xFF)) << 24) + ((src[srcIndex + 1] & unchecked(
0xFF)) << 16) + ((src[srcIndex + 2] & unchecked(0xFF)) << 8) + (src
[srcIndex + 3] & unchecked(0xFF));
}
internal static int ReadNameTrnId(byte[] src, int srcIndex)
{
return ReadInt2(src, srcIndex);
}
internal int AddrIndex;
internal NbtAddress[] AddrEntry;
internal int NameTrnId;
internal int OpCode;
internal int ResultCode;
internal int QuestionCount;
internal int AnswerCount;
internal int AuthorityCount;
internal int AdditionalCount;
internal bool Received;
internal bool IsResponse;
internal bool IsAuthAnswer;
internal bool IsTruncated;
internal bool IsRecurDesired;
internal bool IsRecurAvailable;
internal bool IsBroadcast;
internal Name QuestionName;
internal Name RecordName;
internal int QuestionType;
internal int QuestionClass;
internal int RecordType;
internal int RecordClass;
internal int Ttl;
internal int RDataLength;
internal IPAddress Addr;
public NameServicePacket()
{
IsRecurDesired = true;
IsBroadcast = true;
QuestionCount = 1;
QuestionClass = In;
}
internal virtual int WriteWireFormat(byte[] dst, int dstIndex)
{
int start = dstIndex;
dstIndex += WriteHeaderWireFormat(dst, dstIndex);
dstIndex += WriteBodyWireFormat(dst, dstIndex);
return dstIndex - start;
}
internal virtual int ReadWireFormat(byte[] src, int srcIndex)
{
int start = srcIndex;
srcIndex += ReadHeaderWireFormat(src, srcIndex);
srcIndex += ReadBodyWireFormat(src, srcIndex);
return srcIndex - start;
}
internal virtual int WriteHeaderWireFormat(byte[] dst, int dstIndex)
{
int start = dstIndex;
WriteInt2(NameTrnId, dst, dstIndex);
dst[dstIndex + OpcodeOffset] = unchecked((byte)((IsResponse ? unchecked(0x80) : unchecked(0x00)) + ((OpCode << 3) & unchecked(0x78)) + (IsAuthAnswer
? unchecked(0x04) : unchecked(0x00)) + (IsTruncated ? unchecked(0x02) : unchecked(0x00)) + (IsRecurDesired ? unchecked(0x01)
: unchecked(0x00))));
dst[dstIndex + OpcodeOffset + 1] = unchecked((byte)((IsRecurAvailable ? unchecked(
0x80) : unchecked(0x00)) + (IsBroadcast ? unchecked(0x10) :
unchecked(0x00)) + (ResultCode & unchecked(0x0F))));
WriteInt2(QuestionCount, dst, start + QuestionOffset);
WriteInt2(AnswerCount, dst, start + AnswerOffset);
WriteInt2(AuthorityCount, dst, start + AuthorityOffset);
WriteInt2(AdditionalCount, dst, start + AdditionalOffset);
return HeaderLength;
}
internal virtual int ReadHeaderWireFormat(byte[] src, int srcIndex)
{
NameTrnId = ReadInt2(src, srcIndex);
IsResponse = ((src[srcIndex + OpcodeOffset] & unchecked(0x80)) == 0) ? false
: true;
OpCode = (src[srcIndex + OpcodeOffset] & unchecked(0x78)) >> 3;
IsAuthAnswer = ((src[srcIndex + OpcodeOffset] & unchecked(0x04)) == 0) ?
false : true;
IsTruncated = ((src[srcIndex + OpcodeOffset] & unchecked(0x02)) == 0) ? false
: true;
IsRecurDesired = ((src[srcIndex + OpcodeOffset] & unchecked(0x01)) == 0) ?
false : true;
IsRecurAvailable = ((src[srcIndex + OpcodeOffset + 1] & unchecked(0x80))
== 0) ? false : true;
IsBroadcast = ((src[srcIndex + OpcodeOffset + 1] & unchecked(0x10)) == 0)
? false : true;
ResultCode = src[srcIndex + OpcodeOffset + 1] & unchecked(0x0F);
QuestionCount = ReadInt2(src, srcIndex + QuestionOffset);
AnswerCount = ReadInt2(src, srcIndex + AnswerOffset);
AuthorityCount = ReadInt2(src, srcIndex + AuthorityOffset);
AdditionalCount = ReadInt2(src, srcIndex + AdditionalOffset);
return HeaderLength;
}
internal virtual int WriteQuestionSectionWireFormat(byte[] dst, int dstIndex)
{
int start = dstIndex;
dstIndex += QuestionName.WriteWireFormat(dst, dstIndex);
WriteInt2(QuestionType, dst, dstIndex);
dstIndex += 2;
WriteInt2(QuestionClass, dst, dstIndex);
dstIndex += 2;
return dstIndex - start;
}
internal virtual int ReadQuestionSectionWireFormat(byte[] src, int srcIndex)
{
int start = srcIndex;
srcIndex += QuestionName.ReadWireFormat(src, srcIndex);
QuestionType = ReadInt2(src, srcIndex);
srcIndex += 2;
QuestionClass = ReadInt2(src, srcIndex);
srcIndex += 2;
return srcIndex - start;
}
internal virtual int WriteResourceRecordWireFormat(byte[] dst, int dstIndex)
{
int start = dstIndex;
if (RecordName == QuestionName)
{
dst[dstIndex++] = unchecked(unchecked(0xC0));
// label string pointer to
dst[dstIndex++] = unchecked(unchecked(0x0C));
}
else
{
// questionName (offset 12)
dstIndex += RecordName.WriteWireFormat(dst, dstIndex);
}
WriteInt2(RecordType, dst, dstIndex);
dstIndex += 2;
WriteInt2(RecordClass, dst, dstIndex);
dstIndex += 2;
WriteInt4(Ttl, dst, dstIndex);
dstIndex += 4;
RDataLength = WriteRDataWireFormat(dst, dstIndex + 2);
WriteInt2(RDataLength, dst, dstIndex);
dstIndex += 2 + RDataLength;
return dstIndex - start;
}
internal virtual int ReadResourceRecordWireFormat(byte[] src, int srcIndex)
{
int start = srcIndex;
int end;
if ((src[srcIndex] & unchecked(0xC0)) == unchecked(0xC0))
{
RecordName = QuestionName;
// label string pointer to questionName
srcIndex += 2;
}
else
{
srcIndex += RecordName.ReadWireFormat(src, srcIndex);
}
RecordType = ReadInt2(src, srcIndex);
srcIndex += 2;
RecordClass = ReadInt2(src, srcIndex);
srcIndex += 2;
Ttl = ReadInt4(src, srcIndex);
srcIndex += 4;
RDataLength = ReadInt2(src, srcIndex);
srcIndex += 2;
AddrEntry = new NbtAddress[RDataLength / 6];
end = srcIndex + RDataLength;
for (AddrIndex = 0; srcIndex < end; AddrIndex++)
{
srcIndex += ReadRDataWireFormat(src, srcIndex);
}
return srcIndex - start;
}
internal abstract int WriteBodyWireFormat(byte[] dst, int dstIndex);
internal abstract int ReadBodyWireFormat(byte[] src, int srcIndex);
internal abstract int WriteRDataWireFormat(byte[] dst, int dstIndex);
internal abstract int ReadRDataWireFormat(byte[] src, int srcIndex);
public override string ToString()
{
string opCodeString;
string resultCodeString;
string questionTypeString;
string recordTypeString;
switch (OpCode)
{
case Query:
{
opCodeString = "QUERY";
break;
}
case Wack:
{
opCodeString = "WACK";
break;
}
default:
{
opCodeString = Extensions.ToString(OpCode);
break;
}
}
switch (ResultCode)
{
case FmtErr:
{
resultCodeString = "FMT_ERR";
break;
}
case SrvErr:
{
resultCodeString = "SRV_ERR";
break;
}
case ImpErr:
{
resultCodeString = "IMP_ERR";
break;
}
case RfsErr:
{
resultCodeString = "RFS_ERR";
break;
}
case ActErr:
{
resultCodeString = "ACT_ERR";
break;
}
case CftErr:
{
resultCodeString = "CFT_ERR";
break;
}
default:
{
resultCodeString = "0x" + Hexdump.ToHexString(ResultCode, 1);
break;
}
}
switch (QuestionType)
{
case Nb:
{
questionTypeString = "NB";
break;
}
case Nbstat:
{
questionTypeString = "NBSTAT";
break;
}
default:
{
questionTypeString = "0x" + Hexdump.ToHexString(QuestionType, 4);
break;
}
}
switch (RecordType)
{
case A:
{
recordTypeString = "A";
break;
}
case Ns:
{
recordTypeString = "NS";
break;
}
case Null:
{
recordTypeString = "NULL";
break;
}
case Nb:
{
recordTypeString = "NB";
break;
}
case Nbstat:
{
recordTypeString = "NBSTAT";
break;
}
default:
{
recordTypeString = "0x" + Hexdump.ToHexString(RecordType, 4);
break;
}
}
return "nameTrnId=" + NameTrnId + ",isResponse=" + IsResponse + ",opCode="
+ opCodeString + ",isAuthAnswer=" + IsAuthAnswer + ",isTruncated=" + IsTruncated
+ ",isRecurAvailable=" + IsRecurAvailable + ",isRecurDesired=" + IsRecurDesired
+ ",isBroadcast=" + IsBroadcast + ",resultCode=" + ResultCode + ",questionCount="
+ QuestionCount + ",answerCount=" + AnswerCount + ",authorityCount=" + AuthorityCount
+ ",additionalCount=" + AdditionalCount + ",questionName=" + QuestionName + ",questionType="
+ questionTypeString + ",questionClass=" + (QuestionClass == In ? "IN" : "0x" +
Hexdump.ToHexString(QuestionClass, 4)) + ",recordName=" + RecordName + ",recordType="
+ recordTypeString + ",recordClass=" + (RecordClass == In ? "IN" : "0x" + Hexdump
.ToHexString(RecordClass, 4)) + ",ttl=" + Ttl + ",rDataLength=" + RDataLength;
}
}
}
|