-
Notifications
You must be signed in to change notification settings - Fork 4
/
client_input.c
163 lines (150 loc) · 3.8 KB
/
client_input.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
/*
* Part of `snmp-query-engine`.
*
* Copyright 2012-2013, Anton Berezin <[email protected]>
* Modified BSD license.
* (See LICENSE file in the distribution.)
*
*/
#include "sqe.h"
static void
client_gone(struct socket_info *si)
{
struct client_connection *c = si->udata;
PS.active_client_connections--;
si->udata = NULL;
free_all_client_request_info_for_fd(si->fd);
delete_socket_info(si);
if (c) {
msgpack_unpacked_destroy(&c->input);
msgpack_unpacker_destroy(&c->unpacker);
free(c);
}
if (!opt_quiet)
fprintf(stderr, "client disconnect\n");
}
static void
client_input(struct socket_info *si)
{
struct client_connection *c = si->udata;
char buf[1500];
int n;
int got = 0;
int ok;
if (!c)
croak(1, "client_input: no client_connection information");
if ( (n = read(si->fd, buf, 1500)) == -1) {
switch (errno) {
case EPIPE:
client_gone(si);
return;
case ECONNRESET:
client_gone(si);
return;
}
croak(1, "client_input: read error");
}
if (n == 0) {
client_gone(si);
return;
}
msgpack_unpacker_reserve_buffer(&c->unpacker, n);
memcpy(msgpack_unpacker_buffer(&c->unpacker), buf, n);
msgpack_unpacker_buffer_consumed(&c->unpacker, n);
while (msgpack_unpacker_next(&c->unpacker, &c->input)) {
msgpack_object *o;
uint32_t cid;
uint32_t type;
got = 1;
ok = -1;
PS.client_requests++;
si->PS.client_requests++;
//if (!opt_quiet) {
// printf("got client input: ");
// msgpack_object_print(stdout, c->input.data);
// printf("\n");
//}
o = &c->input.data;
if (o->type != MSGPACK_OBJECT_ARRAY) {
error_reply(si, RT_ERROR, 0, "Request is not an array");
goto end;
}
if (o->via.array.size < 1) {
error_reply(si, RT_ERROR, 0, "Request is an empty array");
goto end;
}
if (o->via.array.size < 2) {
error_reply(si, RT_ERROR, 0, "Request without an id");
goto end;
}
if (o->via.array.ptr[RI_CID].type != MSGPACK_OBJECT_POSITIVE_INTEGER) {
error_reply(si, RT_ERROR, 0, "Request id is not a positive integer");
goto end;
}
cid = o->via.array.ptr[RI_CID].via.u64;
if (o->via.array.ptr[RI_TYPE].type != MSGPACK_OBJECT_POSITIVE_INTEGER) {
error_reply(si, RT_ERROR, cid, "Request type is not a positive integer");
goto end;
}
type = o->via.array.ptr[RI_TYPE].via.u64;
switch (type) {
case RT_SETOPT:
ok = handle_setopt_request(si, cid, o);
break;
case RT_GETOPT:
ok = handle_getopt_request(si, cid, o);
break;
case RT_INFO:
ok = handle_info_request(si, cid, o);
break;
case RT_DEST_INFO:
ok = handle_dest_info_request(si, cid, o);
break;
case RT_GET:
ok = handle_get_request(si, cid, o);
break;
case RT_GETTABLE:
ok = handle_gettable_request(si, cid, o);
break;
default:
error_reply(si, type|RT_ERROR, cid, "Unknown request type");
}
end:
if (ok < 0) {
PS.invalid_requests++;
si->PS.invalid_requests++;
}
}
if (got) {
msgpack_unpacker_expand_buffer(&c->unpacker, 0);
}
}
void new_client_connection(int fd)
{
struct socket_info *si;
struct client_connection *c;
int flags;
if ( (flags = fcntl(fd, F_GETFL, 0)) < 0)
croak(1, "new_client_connection: fcntl(F_GETFL)");
if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) < 0)
croak(1, "new_client_connection: fcntl(F_SETFL)");
#if defined(SO_NOSIGPIPE)
{
int no_sigpipe = 1;
if (setsockopt(fd, SOL_SOCKET, SO_NOSIGPIPE, &no_sigpipe, sizeof (no_sigpipe)) < 0)
croak(1, "new_client_connection: setsockopt of SO_NOSIGPIPE error");
}
#endif
si = new_socket_info(fd);
c = malloc(sizeof(*c));
if (!c)
croak(1, "new_client_connection: malloc(client_connection)");
bzero(c, sizeof(*c));
si->udata = c;
PS.active_client_connections++;
PS.total_client_connections++;
msgpack_unpacker_init(&c->unpacker, MSGPACK_UNPACKER_INIT_BUFFER_SIZE);
msgpack_unpacked_init(&c->input);
on_eof(si, client_gone);
on_read(si, client_input);
}