-
Notifications
You must be signed in to change notification settings - Fork 4
/
request_getopt.c
66 lines (57 loc) · 1.82 KB
/
request_getopt.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
/*
* Part of `snmp-query-engine`.
*
* Copyright 2012-2013, Anton Berezin <[email protected]>
* Modified BSD license.
* (See LICENSE file in the distribution.)
*
*/
#include "sqe.h"
/*
* getopt request:
* [ RT_GETOPT, $cid, $ip, $port ]
*
* reply:
* [ RT_GETOPT|RT_REPLY, $cid, { options } ]
* where options has the following keys:
* - ip: same as $ip in the request
* - port: same as $port in the request
* - community: snmp community
* - version: snmp version (1 or 2)
* - max_packets: max packets on the wire to this destination
* - max_req_size: max request packet size, in bytes, including IP & UDP header
* - timeout: timeout waiting for reply in milliseconds
* - retries: number of times to send a request before giving up
* - XXX more stuff
*
*/
int
handle_getopt_request(struct socket_info *si, unsigned cid, msgpack_object *o)
{
unsigned port = 65536;
struct in_addr ip;
struct client_requests_info *cri;
msgpack_sbuffer* buffer;
msgpack_packer* pk;
if (o->via.array.size != 4)
return error_reply(si, RT_GETOPT|RT_ERROR, cid, "bad request length");
if (o->via.array.ptr[RI_GETOPT_PORT].type == MSGPACK_OBJECT_POSITIVE_INTEGER)
port = o->via.array.ptr[RI_GETOPT_PORT].via.u64;
if (port > 65535)
return error_reply(si, RT_GETOPT|RT_ERROR, cid, "bad port number");
if (!object2ip(&o->via.array.ptr[RI_GETOPT_IP], &ip))
return error_reply(si, RT_GETOPT|RT_ERROR, cid, "bad IP");
PS.getopt_requests++;
si->PS.getopt_requests++;
cri = get_client_requests_info(&ip, port, si);
buffer = msgpack_sbuffer_new();
pk = msgpack_packer_new(buffer, msgpack_sbuffer_write);
msgpack_pack_array(pk, 3);
msgpack_pack_int(pk, RT_GETOPT|RT_REPLY);
msgpack_pack_int(pk, cid);
msgpack_pack_options(pk, cri);
tcp_send(si, buffer->data, buffer->size);
msgpack_sbuffer_free(buffer);
msgpack_packer_free(pk);
return 0;
}