File: braa.c

package info (click to toggle)
braa 0.82-8
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 400 kB
  • sloc: ansic: 3,097; perl: 155; makefile: 12; sh: 10
file content (282 lines) | stat: -rw-r--r-- 5,906 bytes parent folder | download | duplicates (5)
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
#include <sys/types.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <netinet/in.h>
#include <sys/timeb.h>
#include "braaasn.h"
#include "braaclientnet.h"

void help(unsigned char * name)
{
	printf("braa 0.42 - Mateusz 'mteg' Golicz <mtg@elsat.net.pl>, 2003\n", name);
	printf("usage: %s [options]\n", name);
	printf("  -h        Show this help\n");
	printf("  -r <ret>  Number of retries per host (default: 5)\n");
	printf("  -s <rate> Query rate - specify how many hosts query simultaneously (default: 50).\n");
	printf("  -t <time> Quit after time ms (default: 3000)\n");
	printf("  -q <query>Add a query to the query list\n");
	printf("  -f <file> Add queries from this file (one/line)\n");
	printf("  -2        Identify as a SNMP V2c agent.\n");
	printf("  -a        Alternative output format\n");
	printf("  -d        Show response times.\n");
	printf("\n");
	printf("Query format: [community@]host[:port]:oid[/id]\n");
	printf("Output format: host:oid:result\n");
					
}

void addquery(struct braa_hclient *c, unsigned char *q)
{
	unsigned char * community = NULL;
	unsigned char * host = NULL;
	unsigned char * oid = NULL;
	unsigned char * port;
	int i_port = 161;
	unsigned char * t;
	unsigned char * mem;
	unsigned char * orig = q;
	char * id = NULL;
	
	u_int32_t * numoid;
	u_int16_t oidlen;

	u_int32_t starthost, endhost, current;
	
	int i;
	
	q = mem = strdup(q);
	
	if((t = index(q, '@')))
	{
		*t = 0;
		community = q;
		q = t + 1;
	}
	if((t = index(q, ':')))
	{
		*t = 0;
		host = q;
		q = t + 1;
	}
	
	if((port = index(q, ':')))
	{
		char * eptr;
		*port = 0;
		i_port = strtol(q, &eptr, 10);
		if(*eptr)
		{
			fprintf(stderr, "'%s' - invalid port number, ignoring query '%s'\n", q, orig);
			free(mem);
			return;
		}
		q = port + 1;
	}
	
	oid = q;
	if(!(host && oid))
	{
		fprintf(stderr, "'%s' - invalid query, ignoring.\n", orig);
		free(mem);
		return;
	}

	debug("Decoding OID...\n");
	
	if((id = index(oid, '/')))
	{
		*id = 0;
		id++;
	}
	
	if(!(numoid = braa_StringToOID(oid, &oidlen)))
	{
		fprintf(stderr, "'%s' in '%s' - invalid OID, ignoring.\n", oid, orig);
		free(mem);
		return;
	}
#ifdef DEBUG
	printf("OID LEN: %d\n", oidlen);
	for(i = 0; i<oidlen; i++)
		printf("%d.\n", numoid[i]);
#endif
	debug("DONE\n");

	if(!community) community = strdup("public");
	if((t = index(host, '-')))
	{
		unsigned char * sendhost;
		*t = 0;
		sendhost = t + 1;
		starthost = inet_addr(host);
		endhost = inet_addr(sendhost);
	}
	else
	{
		starthost = inet_addr(host);
		endhost = starthost;
	}
	
	if(starthost == -1 || endhost == -1)
	{
		fprintf(stderr, "Invalid IP address in '%s', ignoring\n", orig);
		free(community);
		free(mem);
		return;
	}
	
	endhost = ntohl(endhost);
	starthost = ntohl(starthost);


	
	if(endhost < starthost)
	{
		fprintf(stderr, "'%s' - invalid host range, ignoring.\n", orig);
		free(mem);
		return;
	}

	debug("Adding queries...\n");
#ifdef DEBUG
	for(i = 0; i<oidlen; i++)
		printf("%d.\n", numoid[i]);
#endif
	
	for(current = starthost; current <= endhost; current++)
		braa_HClient_AddQueryTo(c, htonl(current), i_port, community, oidlen, numoid, id);


#ifdef DEBUG
	for(i = 0; i<oidlen; i++)
		printf("%d.", numoid[i]);
#endif

	debug("OK\n");

}

int main(int argc, char ** argv)
{
	struct braa_hclient * client;
	struct braa_hqhost * host;
	struct braa_hcquery * query;
	int c, v, snmpver = 0;
	int timeout = 3000;
	int alternat = 0;
	int rtt = 0;
	
	client = braa_HClient_Create(50, 5);

        while((c = getopt(argc, argv, "hq:f:r:s:t:ad2")) != EOF)
        {
		switch(c)
		{
			case '2': snmpver = 1; break;
			case 'a': alternat = 1;
				break;
			case 'd': rtt = 1; break;
			case 'h':
				help(argv[0]);
				return(0);
			case 'r':
				v = atoi(optarg);
				if(v < 1 || v > 1000)
					fprintf(stderr, "Invalid retry count, setting default of 5.\n");
				else
					client->retries = v;
				break;
			case 's':
				v = atoi(optarg);
				if(v < 1 || v > 1000)
					fprintf(stderr, "Invalid request rate, setting default of 50\n");
				else
					client->requestrate = v;
				break;
			case 't':
				v = atoi(optarg);
				if(v < 500 || v > 300000)
					fprintf(stderr, "Invalid timeout, setting default of 3s\n");
				else
					timeout = v;
				break;
			case 'q':
				addquery(client, optarg);
				break;
			case 'f':
			{
				unsigned char line[1024];
				FILE * fh;
				fh = fopen(optarg, "r");
				if(!fh)
				{
					perror("fopen");
					fprintf(stderr, "Unable to open query list: '%s'\n", optarg); 
					return 1;
				}
				
				while(fgets(line, 1024, fh))
				{
					unsigned char * eol;
					if((eol = index(line, '\n'))) *eol = 0;
					addquery(client, line);
				}
				
				
				fclose(fh);
			}
				
		}
	}
	
	braa_HClient_Process(client, timeout, snmpver);
	
	for(host = client->queries; host; host = host->next)
	{
		unsigned char shost[30];
		struct braa_hcquery * qstack[50];
		int stackpos;
		struct in_addr ina; 

		if(!host->responded) continue;

		ina.s_addr = host->host;
		snprintf(shost, 30, "%s", inet_ntoa(ina));
		
		if(alternat) printf("%s", shost);
		
		for(stackpos = 0, query = host->queries; query && stackpos < 50; query = query->next)
			qstack[stackpos++] = query;

		if(!stackpos) continue;
		
		for(query = qstack[--stackpos]; stackpos >= 0; query = qstack[--stackpos])
		{
			unsigned char oidbuf[200];
			unsigned char resultbuf[200];
			if(query->response)
			{
				braa_OIDToString(query->oid, query->oidlen, oidbuf, 200);

				braa_ASNToString(query->response, resultbuf, 200);
				if(alternat)
					printf(" %s", resultbuf);
				else
					if(query->data)
						printf("%s/%s:%s:%s\n", (char*) query->data, shost, oidbuf, resultbuf);
					else
						printf("%s:%s:%s\n", shost, oidbuf, resultbuf);
			}
		}
		if(rtt)
		{
			if(alternat)
				printf(" %dms", host->responded);
			else
				printf("%s:delay:%dms\n", shost, host->responded);
		}
		if(alternat) printf("\n");
	}
	
}