Debian Codegraph @

examples/daemon_sender_uv.c

raw
1 #include <tinyframe/tinyframe.h>
2
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <string.h>
6 #include <uv.h>
7 #include <errno.h>
8 #include <stdbool.h>
9
10 #include "create_dnstap.c"
11
12 #define BUF_SIZE 4096
13
14 enum client_state {
15 no_state,
16 writing_start,
17 started,
18 writing_frame,
19 };
20
21 struct client;
22 struct client {
23 struct client* next;
24 size_t id;
25 enum client_state state;
26 uv_tcp_t conn;
27 char rbuf[BUF_SIZE];
28 uv_write_t wreq;
29 uv_buf_t wbuf;
30 uint8_t buf[BUF_SIZE];
31 };
32
33 struct client* clients = 0;
34 size_t client_id = 1;
35
36 static char content_type[] = "protobuf:dnstap.Dnstap";
37
38 uv_loop_t* loop;
39
40 struct client* client_new()
41 {
42 struct client* c = malloc(sizeof(struct client));
43 if (c) {
44 c->conn.data = c;
45 c->next = clients;
46 c->id = client_id++;
47 c->state = no_state;
48 c->wbuf.base = (void*)c->buf;
49 clients = c;
50 }
51 return c;
52 }
53
54 void client_close(uv_handle_t* handle)
55 {
56 struct client* c = handle->data;
57
58 if (clients == c) {
59 clients = c->next;
60 } else {
61 struct client* prev = clients;
62
63 while (prev) {
64 if (prev->next == c) {
65 prev->next = c->next;
66 break;
67 }
68 prev = prev->next;
69 }
70 }
71
72 free(c);
73 }
74
75 void client_alloc_buffer(uv_handle_t* handle, size_t suggested_size, uv_buf_t* buf)
76 {
77 buf->base = ((struct client*)handle->data)->rbuf;
78 buf->len = BUF_SIZE;
79 }
80
81 void client_read(uv_stream_t* client, ssize_t nread, const uv_buf_t* buf)
82 {
83 /*
84 * We discard any input from the client and only check for errors or
85 * if the connection was closed.
86 */
87
88 if (nread < 0) {
89 if (nread != UV_EOF) {
90 fprintf(stderr, "client_read() error: %s\n", uv_err_name(nread));
91 } else {
92 printf("client %zu disconnected\n", ((struct client*)client->data)->id);
93 }
94 uv_close((uv_handle_t*)client, client_close);
95 }
96 }
97
98 void client_write(uv_write_t* req, int status)
99 {
100 /*
101 * After a write we check that there was no errors and then set the
102 * client in a state that allows `tick()` to send DNSTAP messages to it.
103 */
104
105 if (status) {
106 fprintf(stderr, "client_write() error: %s\n", uv_strerror(status));
107 uv_close((uv_handle_t*)req->handle, client_close);
108 return;
109 }
110
111 ((struct client*)req->handle->data)->state = started;
112 }
113
114 void on_new_connection(uv_stream_t* server, int status)
115 {
116 if (status < 0) {
117 fprintf(stderr, "on_new_connection() error: %s\n", uv_strerror(status));
118 return;
119 }
120
121 /*
122 * We have a new client connecting, create a client struct to hold the
123 * connection, accept it and send the control start frame.
124 */
125
126 struct client* client = client_new();
127 if (!client) {
128 fprintf(stderr, "on_new_connection() out of memory\n");
129 return;
130 }
131
132 uv_tcp_init(loop, &client->conn);
133 if (uv_accept(server, (uv_stream_t*)&client->conn) == 0) {
134 printf("client %zu connected\n", client->id);
135
136 uv_read_start((uv_stream_t*)&client->conn, client_alloc_buffer, client_read);
137
138 struct tinyframe_writer writer = TINYFRAME_WRITER_INITIALIZER;
139
140 /*
141 * First we write, to the buffer, a control start with a content type
142 * control field for the DNSTAP protobuf content type.
143 *
144 * Then we send it.
145 */
146
147 if (tinyframe_write_control_start(&writer, client->buf, BUF_SIZE, content_type, sizeof(content_type) - 1) != tinyframe_ok) {
148 fprintf(stderr, "tinyframe_write_control_start() failed\n");
149 uv_close((uv_handle_t*)&client->conn, client_close);
150 return;
151 }
152 printf("client %zu: sending control start and content type\n", client->id);
153
154 client->wbuf.len = writer.bytes_wrote;
155 uv_write((uv_write_t*)&client->wreq, (uv_stream_t*)&client->conn, &client->wbuf, 1, client_write);
156 client->state = writing_start;
157 } else {
158 uv_close((uv_handle_t*)&client->conn, client_close);
159 }
160 }
161
162 /*
163 * This function is called every second and will create a DNSTAP message
164 * and send it to all available clients.
165 */
166
167 void tick(uv_timer_t* handle)
168 {
169 /*
170 * Now we create a DNSTAP message.
171 */
172
173 struct dnstap d = create_dnstap("daemon_sender_uv");
174
175 /*
176 * Now that the message is prepared we can begin encapsulating it in
177 * protobuf and Frame Streams.
178 *
179 * First we ask what the encoded size of the protobuf message would be
180 * and then we allocate a buffer with of that size plus the size of
181 * a Frame Streams frame header.
182 *
183 * Then we encode the DNSTAP message and put it after the frame header
184 * and call `tinyframe_set_header()` to set the header.
185 */
186
187 size_t frame_len = dnstap_encode_protobuf_size(&d);
188 uint8_t frame[TINYFRAME_HEADER_SIZE + frame_len];
189 dnstap_encode_protobuf(&d, &frame[TINYFRAME_HEADER_SIZE]);
190 tinyframe_set_header(frame, frame_len);
191
192 if (sizeof(frame) > BUF_SIZE) {
193 fprintf(stderr, "frame larger the client's buffers\n");
194 exit(1);
195 }
196
197 /*
198 * We now loop over all the connected clients and send the message
199 * to those that are currently not busy.
200 */
201
202 struct client* c = clients;
203 while (c) {
204 if (c->state == started) {
205 c->wbuf.len = sizeof(frame);
206 memcpy(c->buf, frame, sizeof(frame));
207 uv_write((uv_write_t*)&c->wreq, (uv_stream_t*)&c->conn, &c->wbuf, 1, client_write);
208 c->state = writing_frame;
209
210 printf("client %zu: sending DNSTAP\n", c->id);
211 }
212 c = c->next;
213 }
214 }
215
216 int main(int argc, const char* argv[])
217 {
218 if (argc < 3) {
219 fprintf(stderr, "usage: daemon_sender_uv <IP> <port>\n");
220 return 1;
221 }
222
223 /*
224 * We setup a TCP server using libuv and listen for connections,
225 * along with a timer that calls the function to send DNSTAP messages
226 * to all clients.
227 */
228
229 struct sockaddr_storage addr;
230 int port = atoi(argv[2]);
231
232 if (strchr(argv[1], ':')) {
233 uv_ip6_addr(argv[1], port, (struct sockaddr_in6*)&addr);
234 } else {
235 uv_ip4_addr(argv[1], port, (struct sockaddr_in*)&addr);
236 }
237
238 loop = uv_default_loop();
239
240 uv_tcp_t server;
241 uv_tcp_init(loop, &server);
242 uv_tcp_bind(&server, (const struct sockaddr*)&addr, 0);
243 int r = uv_listen((uv_stream_t*)&server, 128, on_new_connection);
244 if (r) {
245 fprintf(stderr, "uv_listen() failed: %s\n", uv_strerror(r));
246 return 1;
247 }
248
249 uv_timer_t ticker;
250 uv_timer_init(loop, &ticker);
251 uv_timer_start(&ticker, tick, 1000, 1000);
252
253 return uv_run(loop, UV_RUN_DEFAULT);
254 }