-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse_nbtstat.c
226 lines (191 loc) · 7.12 KB
/
parse_nbtstat.c
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
/*
* $Id: //devel/tools/main/nbtscan/parse_nbtstat.c#1 $
*
* written by : Stephen J. Friedl
* Software Consultant
*
* Given a response from the remote end, decode it to learn what we can
* about the services it's offering up. The goal here is to move the data
* to a reasonable format only, and only later will we try to actually
* understand it. The only output from this module is debugging, which
* is enabled by the verbose output.
*
* The overall format of this is defined in RFC1002 in section 4.2.18,
* "NODE STATUS RESPONSE", and we repeat a bit of it here.
*
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* | name trans ID |1| 0x0 |1|0|0|0|0 0|0| 0x0 |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* | num_questions (0x00) | num_answers (0x01) |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* | ns_count (0x00) | addl_rec_count (0x00) |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* | |
* ~ RR_name of question ~
* | |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* | qtype: "NBSTAT" (0x0021) | qclass: "IN" (0x0001) |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* | 0x00000000 |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* | rdlength | # names | |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ ~
* | ~
* ~ NODE_NAME array ~
* | |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* | |
* ~ statistics ~
* | |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
*
* All names are in the stupid encoded format -- what a crock -- and
* the RR_name of question (in our case) is always a "*". What we
* most care about is the NODE_NAME array, which is the answer to the
* question.
*
*/
#include "nbtscan_common.h"
#include <stdio.h>
#include <string.h>
#include "nbtdefs.h"
/*
* getshort()
*
* Given a handle to a pointer to two bytes, fetch it as an unsigned short
* in network order and convert to host order. We advance the pointer.
*/
static unsigned short getshort(const char **p)
{
unsigned short s;
assert( p != 0);
assert(*p != 0);
memcpy(&s, *p, 2);
*p += 2;
return ntohs(s);
}
int parse_nbtstat(const struct NMBpacket *pak, int paklen,
struct NMB_query_response *rsp,
char *errbuf)
{
const char *p,
*pmax,
*nmax,
*pstats;
int rdlength,
remaining,
nnames;
int qtype, /* query type (always "NBSTAT") */
qclass; /* query class (always "IN") */
char tmpbuf[256]; /* random buffer */
assert(pak != 0);
assert(rsp != 0);
assert(paklen > 0);
assert(errbuf != 0);
memset(rsp, 0, sizeof *rsp);
/*----------------------------------------------------------------
* Set up our initial pointers into the received record. We are
* trying to be very careful about not running away with our
* memory, so we set a pointer to the very end of the valid part
* of the data from the other end, and we try to never look past
* this.
*
* +-----------------------------------------------------------+
* | headers | response data |
* +-----------------------------------------------------------+
* ^--pak ^--p pmax-^
*
* Note that we do >nothing< with the headers, but probably should
* (to verify that there is actually an answer?).
*/
pmax = paklen + (char *)pak;
p = pak->data;
/*----------------------------------------------------------------
* The first thing we should see is the "question" section, which
* should simply echo what we gave them. Parse this out to skip
* past it. We decode it only for the benefit of the debugging
* code.
*/
NETBIOS_unpack(&p, tmpbuf, sizeof tmpbuf);
qtype = getshort(&p); /* question type */
qclass = getshort(&p); /* question class */
if ( verbose > 1 )
{
printf(" QUESTION SECTION:\n");
printf(" name = \"%s\"\n", tmpbuf);
printf(" type = %s\n",
printable_NETBIOS_question_type (tmpbuf, qtype));
printf(" class = %s\n",
printable_NETBIOS_question_class(tmpbuf, qclass));
}
p += 4; /* skip past TTL (always zero) */
/*----------------------------------------------------------------
* Fetch the length of the rest of this packet and make sure that
* we actually have this much room left. If we don't, we must have
* gotten a short UDP packet and won't be able to finish off this
* processing. The max size is ~~500 bytes or so.
*/
rdlength = getshort(&p);
remaining = (int)(pmax - p);
if ( rdlength > remaining )
{
printf(" ERROR: rdlength = %d, remaining bytes = %d\n",
rdlength,
remaining);
return -1;
}
/*----------------------------------------------------------------
* Fetch the number of names to be found in the rest of this node
* object. Sometimes we get >zero< and it's not clear why this is.
* Perhaps it means that there is no NETBIOS nameserver running
* but it will answer status requests. Hmmm.
*/
nnames = *(unsigned char *)p; p++;
if ( verbose > 1 )
printf(" NODE COUNT = %d\n", nnames);
if ( nnames < 0 )
{
sprintf(errbuf, "bad NETBIOS response (count=%d)", nnames);
return FALSE;
}
pstats = p + (nnames * NODE_RECORD_SIZE);
if (nnames > TBLSIZE(rsp->nodes))
{
nnames = TBLSIZE(rsp->nodes);
rsp->nametrunc = TRUE;
}
nmax = p + (nnames * NODE_RECORD_SIZE);
for ( ; p < nmax; p += NODE_RECORD_SIZE )
{
struct node_name_record nr;
struct nodeinfo *ni = &rsp->nodes[ rsp->nnodes++ ];
/* Solaris has alignment problems, gotta copy */
memcpy(&nr, p, NODE_RECORD_SIZE);
ni->flags = ntohs(nr.flags);
ni->type = nr.type;
strncpy(ni->name, nr.name, 15)[15] = '\0';
strip(ni->name);
}
/*----------------------------------------------------------------
* Now we've finished processing the node information and gathered
* up everything we can find, so now look for the statistics. We
* ONLY try to gather these stats if there is actually any room
* left in our buffer.
*/
if ( (int) (pmax - pstats) >= NODE_STATS_SIZE )
{
memcpy( &rsp->nodestats, pstats, NODE_STATS_SIZE );
byteswap_nodestats( &rsp->nodestats );
sprintf(rsp->ether, "%02x:%02x:%02x:%02x:%02x:%02x",
rsp->nodestats.uniqueid[0],
rsp->nodestats.uniqueid[1],
rsp->nodestats.uniqueid[2],
rsp->nodestats.uniqueid[3],
rsp->nodestats.uniqueid[4],
rsp->nodestats.uniqueid[5]);
}
/* postprocessing for good measure */
process_response(rsp);
return TRUE;
}