regress.gen.c revision 1.8 1 1.7 christos /* $NetBSD: regress.gen.c,v 1.8 2024/08/18 20:47:23 christos Exp $ */
2 1.7 christos
3 1.8 christos
4 1.7 christos /*
5 1.7 christos * Automatically generated from /ntpbuild/data/snaps/ntp-stable/sntp/libevent/test/regress.rpc
6 1.7 christos * by event_rpcgen.py/0.1. DO NOT EDIT THIS FILE.
7 1.7 christos */
8 1.7 christos
9 1.7 christos #include <stdlib.h>
10 1.7 christos #include <string.h>
11 1.7 christos #include <assert.h>
12 1.7 christos #include <event2/event-config.h>
13 1.7 christos #include <event2/event.h>
14 1.7 christos #include <event2/buffer.h>
15 1.7 christos #include <event2/tag.h>
16 1.7 christos
17 1.8 christos #if defined(EVENT__HAVE___func__)
18 1.8 christos # ifndef __func__
19 1.8 christos # define __func__ __func__
20 1.8 christos # endif
21 1.8 christos #elif defined(EVENT__HAVE___FUNCTION__)
22 1.8 christos # define __func__ __FUNCTION__
23 1.8 christos #else
24 1.8 christos # define __func__ __FILE__
25 1.7 christos #endif
26 1.7 christos
27 1.7 christos
28 1.7 christos #include "regress.gen.h"
29 1.7 christos
30 1.7 christos void event_warn(const char *fmt, ...);
31 1.7 christos void event_warnx(const char *fmt, ...);
32 1.7 christos
33 1.7 christos /*
34 1.7 christos * Implementation of msg
35 1.7 christos */
36 1.7 christos
37 1.7 christos static struct msg_access_ msg_base__ = {
38 1.7 christos msg_from_name_assign,
39 1.7 christos msg_from_name_get,
40 1.7 christos msg_to_name_assign,
41 1.7 christos msg_to_name_get,
42 1.7 christos msg_attack_assign,
43 1.7 christos msg_attack_get,
44 1.7 christos msg_run_assign,
45 1.7 christos msg_run_get,
46 1.7 christos msg_run_add,
47 1.7 christos };
48 1.7 christos
49 1.7 christos struct msg *
50 1.7 christos msg_new(void)
51 1.7 christos {
52 1.7 christos return msg_new_with_arg(NULL);
53 1.7 christos }
54 1.7 christos
55 1.7 christos struct msg *
56 1.7 christos msg_new_with_arg(void *unused)
57 1.7 christos {
58 1.7 christos struct msg *tmp;
59 1.7 christos if ((tmp = malloc(sizeof(struct msg))) == NULL) {
60 1.7 christos event_warn("%s: malloc", __func__);
61 1.7 christos return (NULL);
62 1.7 christos }
63 1.7 christos tmp->base = &msg_base__;
64 1.7 christos
65 1.7 christos tmp->from_name_data = NULL;
66 1.7 christos tmp->from_name_set = 0;
67 1.7 christos
68 1.7 christos tmp->to_name_data = NULL;
69 1.7 christos tmp->to_name_set = 0;
70 1.7 christos
71 1.7 christos tmp->attack_data = NULL;
72 1.7 christos tmp->attack_set = 0;
73 1.7 christos
74 1.7 christos tmp->run_data = NULL;
75 1.7 christos tmp->run_length = 0;
76 1.7 christos tmp->run_num_allocated = 0;
77 1.7 christos tmp->run_set = 0;
78 1.7 christos
79 1.7 christos return (tmp);
80 1.7 christos }
81 1.7 christos
82 1.7 christos
83 1.7 christos
84 1.7 christos
85 1.7 christos static int
86 1.7 christos msg_run_expand_to_hold_more(struct msg *msg)
87 1.7 christos {
88 1.7 christos int tobe_allocated = msg->run_num_allocated;
89 1.7 christos struct run** new_data = NULL;
90 1.7 christos tobe_allocated = !tobe_allocated ? 1 : tobe_allocated << 1;
91 1.7 christos new_data = (struct run**) realloc(msg->run_data,
92 1.7 christos tobe_allocated * sizeof(struct run*));
93 1.7 christos if (new_data == NULL)
94 1.7 christos return -1;
95 1.7 christos msg->run_data = new_data;
96 1.7 christos msg->run_num_allocated = tobe_allocated;
97 1.8 christos return 0;
98 1.8 christos }
99 1.7 christos
100 1.7 christos struct run*
101 1.7 christos msg_run_add(struct msg *msg)
102 1.7 christos {
103 1.7 christos if (++msg->run_length >= msg->run_num_allocated) {
104 1.7 christos if (msg_run_expand_to_hold_more(msg)<0)
105 1.7 christos goto error;
106 1.7 christos }
107 1.7 christos msg->run_data[msg->run_length - 1] = run_new();
108 1.7 christos if (msg->run_data[msg->run_length - 1] == NULL)
109 1.7 christos goto error;
110 1.7 christos msg->run_set = 1;
111 1.7 christos return (msg->run_data[msg->run_length - 1]);
112 1.7 christos error:
113 1.7 christos --msg->run_length;
114 1.7 christos return (NULL);
115 1.7 christos }
116 1.7 christos
117 1.7 christos int
118 1.7 christos msg_from_name_assign(struct msg *msg,
119 1.7 christos const char * value)
120 1.7 christos {
121 1.7 christos if (msg->from_name_data != NULL)
122 1.7 christos free(msg->from_name_data);
123 1.7 christos if ((msg->from_name_data = strdup(value)) == NULL)
124 1.7 christos return (-1);
125 1.7 christos msg->from_name_set = 1;
126 1.7 christos return (0);
127 1.7 christos }
128 1.7 christos
129 1.7 christos int
130 1.7 christos msg_to_name_assign(struct msg *msg,
131 1.7 christos const char * value)
132 1.7 christos {
133 1.7 christos if (msg->to_name_data != NULL)
134 1.7 christos free(msg->to_name_data);
135 1.7 christos if ((msg->to_name_data = strdup(value)) == NULL)
136 1.7 christos return (-1);
137 1.7 christos msg->to_name_set = 1;
138 1.7 christos return (0);
139 1.7 christos }
140 1.7 christos
141 1.7 christos int
142 1.7 christos msg_attack_assign(struct msg *msg,
143 1.7 christos const struct kill* value)
144 1.7 christos {
145 1.7 christos struct evbuffer *tmp = NULL;
146 1.7 christos if (msg->attack_set) {
147 1.7 christos kill_clear(msg->attack_data);
148 1.7 christos msg->attack_set = 0;
149 1.7 christos } else {
150 1.7 christos msg->attack_data = kill_new();
151 1.7 christos if (msg->attack_data == NULL) {
152 1.7 christos event_warn("%s: kill_new()", __func__);
153 1.7 christos goto error;
154 1.7 christos }
155 1.7 christos }
156 1.7 christos if ((tmp = evbuffer_new()) == NULL) {
157 1.7 christos event_warn("%s: evbuffer_new()", __func__);
158 1.7 christos goto error;
159 1.7 christos }
160 1.7 christos kill_marshal(tmp, value);
161 1.7 christos if (kill_unmarshal(msg->attack_data, tmp) == -1) {
162 1.7 christos event_warnx("%s: kill_unmarshal", __func__);
163 1.7 christos goto error;
164 1.7 christos }
165 1.7 christos msg->attack_set = 1;
166 1.7 christos evbuffer_free(tmp);
167 1.7 christos return (0);
168 1.7 christos error:
169 1.7 christos if (tmp != NULL)
170 1.7 christos evbuffer_free(tmp);
171 1.7 christos if (msg->attack_data != NULL) {
172 1.7 christos kill_free(msg->attack_data);
173 1.7 christos msg->attack_data = NULL;
174 1.7 christos }
175 1.7 christos return (-1);
176 1.7 christos }
177 1.7 christos
178 1.7 christos int
179 1.7 christos msg_run_assign(struct msg *msg, int off,
180 1.8 christos const struct run* value)
181 1.7 christos {
182 1.7 christos if (!msg->run_set || off < 0 || off >= msg->run_length)
183 1.7 christos return (-1);
184 1.7 christos
185 1.7 christos {
186 1.7 christos int had_error = 0;
187 1.7 christos struct evbuffer *tmp = NULL;
188 1.7 christos run_clear(msg->run_data[off]);
189 1.7 christos if ((tmp = evbuffer_new()) == NULL) {
190 1.7 christos event_warn("%s: evbuffer_new()", __func__);
191 1.7 christos had_error = 1;
192 1.7 christos goto done;
193 1.7 christos }
194 1.7 christos run_marshal(tmp, value);
195 1.7 christos if (run_unmarshal(msg->run_data[off], tmp) == -1) {
196 1.7 christos event_warnx("%s: run_unmarshal", __func__);
197 1.7 christos had_error = 1;
198 1.7 christos goto done;
199 1.7 christos }
200 1.8 christos done:
201 1.8 christos if (tmp != NULL)
202 1.7 christos evbuffer_free(tmp);
203 1.7 christos if (had_error) {
204 1.7 christos run_clear(msg->run_data[off]);
205 1.7 christos return (-1);
206 1.7 christos }
207 1.7 christos }
208 1.7 christos return (0);
209 1.7 christos }
210 1.7 christos
211 1.7 christos int
212 1.7 christos msg_from_name_get(struct msg *msg, char * *value)
213 1.7 christos {
214 1.7 christos if (msg->from_name_set != 1)
215 1.7 christos return (-1);
216 1.7 christos *value = msg->from_name_data;
217 1.7 christos return (0);
218 1.7 christos }
219 1.7 christos
220 1.7 christos int
221 1.7 christos msg_to_name_get(struct msg *msg, char * *value)
222 1.7 christos {
223 1.7 christos if (msg->to_name_set != 1)
224 1.7 christos return (-1);
225 1.7 christos *value = msg->to_name_data;
226 1.7 christos return (0);
227 1.7 christos }
228 1.7 christos
229 1.7 christos int
230 1.7 christos msg_attack_get(struct msg *msg, struct kill* *value)
231 1.7 christos {
232 1.7 christos if (msg->attack_set != 1) {
233 1.7 christos msg->attack_data = kill_new();
234 1.7 christos if (msg->attack_data == NULL)
235 1.7 christos return (-1);
236 1.7 christos msg->attack_set = 1;
237 1.7 christos }
238 1.7 christos *value = msg->attack_data;
239 1.7 christos return (0);
240 1.7 christos }
241 1.7 christos
242 1.7 christos int
243 1.7 christos msg_run_get(struct msg *msg, int offset,
244 1.7 christos struct run* *value)
245 1.7 christos {
246 1.7 christos if (!msg->run_set || offset < 0 || offset >= msg->run_length)
247 1.7 christos return (-1);
248 1.7 christos *value = msg->run_data[offset];
249 1.7 christos return (0);
250 1.7 christos }
251 1.7 christos
252 1.7 christos void
253 1.7 christos msg_clear(struct msg *tmp)
254 1.7 christos {
255 1.7 christos if (tmp->from_name_set == 1) {
256 1.7 christos free(tmp->from_name_data);
257 1.7 christos tmp->from_name_data = NULL;
258 1.7 christos tmp->from_name_set = 0;
259 1.7 christos }
260 1.7 christos if (tmp->to_name_set == 1) {
261 1.7 christos free(tmp->to_name_data);
262 1.7 christos tmp->to_name_data = NULL;
263 1.7 christos tmp->to_name_set = 0;
264 1.7 christos }
265 1.7 christos if (tmp->attack_set == 1) {
266 1.7 christos kill_free(tmp->attack_data);
267 1.7 christos tmp->attack_data = NULL;
268 1.7 christos tmp->attack_set = 0;
269 1.7 christos }
270 1.7 christos if (tmp->run_set == 1) {
271 1.7 christos int i;
272 1.7 christos for (i = 0; i < tmp->run_length; ++i) {
273 1.7 christos run_free(tmp->run_data[i]);
274 1.7 christos }
275 1.7 christos free(tmp->run_data);
276 1.7 christos tmp->run_data = NULL;
277 1.7 christos tmp->run_set = 0;
278 1.7 christos tmp->run_length = 0;
279 1.7 christos tmp->run_num_allocated = 0;
280 1.7 christos }
281 1.7 christos }
282 1.7 christos
283 1.7 christos void
284 1.7 christos msg_free(struct msg *tmp)
285 1.7 christos {
286 1.7 christos if (tmp->from_name_data != NULL)
287 1.7 christos free (tmp->from_name_data);
288 1.7 christos if (tmp->to_name_data != NULL)
289 1.7 christos free (tmp->to_name_data);
290 1.7 christos if (tmp->attack_data != NULL)
291 1.7 christos kill_free(tmp->attack_data);
292 1.7 christos if (tmp->run_set == 1) {
293 1.7 christos int i;
294 1.7 christos for (i = 0; i < tmp->run_length; ++i) {
295 1.7 christos run_free(tmp->run_data[i]);
296 1.7 christos }
297 1.7 christos free(tmp->run_data);
298 1.7 christos tmp->run_data = NULL;
299 1.7 christos tmp->run_set = 0;
300 1.7 christos tmp->run_length = 0;
301 1.7 christos tmp->run_num_allocated = 0;
302 1.7 christos }
303 1.7 christos free(tmp->run_data);
304 1.7 christos free(tmp);
305 1.7 christos }
306 1.7 christos
307 1.7 christos void
308 1.8 christos msg_marshal(struct evbuffer *evbuf, const struct msg *tmp) {
309 1.7 christos evtag_marshal_string(evbuf, MSG_FROM_NAME, tmp->from_name_data);
310 1.7 christos evtag_marshal_string(evbuf, MSG_TO_NAME, tmp->to_name_data);
311 1.7 christos if (tmp->attack_set) {
312 1.7 christos evtag_marshal_kill(evbuf, MSG_ATTACK, tmp->attack_data);
313 1.7 christos }
314 1.7 christos if (tmp->run_set) {
315 1.7 christos {
316 1.7 christos int i;
317 1.7 christos for (i = 0; i < tmp->run_length; ++i) {
318 1.7 christos evtag_marshal_run(evbuf, MSG_RUN, tmp->run_data[i]);
319 1.7 christos }
320 1.7 christos }
321 1.7 christos }
322 1.7 christos }
323 1.7 christos
324 1.7 christos int
325 1.8 christos msg_unmarshal(struct msg *tmp, struct evbuffer *evbuf)
326 1.7 christos {
327 1.7 christos ev_uint32_t tag;
328 1.7 christos while (evbuffer_get_length(evbuf) > 0) {
329 1.7 christos if (evtag_peek(evbuf, &tag) == -1)
330 1.7 christos return (-1);
331 1.7 christos switch (tag) {
332 1.7 christos
333 1.7 christos case MSG_FROM_NAME:
334 1.7 christos if (tmp->from_name_set)
335 1.7 christos return (-1);
336 1.7 christos if (evtag_unmarshal_string(evbuf, MSG_FROM_NAME, &tmp->from_name_data) == -1) {
337 1.7 christos event_warnx("%s: failed to unmarshal from_name", __func__);
338 1.7 christos return (-1);
339 1.7 christos }
340 1.7 christos tmp->from_name_set = 1;
341 1.7 christos break;
342 1.7 christos case MSG_TO_NAME:
343 1.7 christos if (tmp->to_name_set)
344 1.7 christos return (-1);
345 1.7 christos if (evtag_unmarshal_string(evbuf, MSG_TO_NAME, &tmp->to_name_data) == -1) {
346 1.7 christos event_warnx("%s: failed to unmarshal to_name", __func__);
347 1.7 christos return (-1);
348 1.7 christos }
349 1.7 christos tmp->to_name_set = 1;
350 1.7 christos break;
351 1.7 christos case MSG_ATTACK:
352 1.7 christos if (tmp->attack_set)
353 1.7 christos return (-1);
354 1.7 christos tmp->attack_data = kill_new();
355 1.7 christos if (tmp->attack_data == NULL)
356 1.7 christos return (-1);
357 1.8 christos if (evtag_unmarshal_kill(evbuf, MSG_ATTACK,
358 1.8 christos tmp->attack_data) == -1) {
359 1.7 christos event_warnx("%s: failed to unmarshal attack", __func__);
360 1.7 christos return (-1);
361 1.7 christos }
362 1.7 christos tmp->attack_set = 1;
363 1.7 christos break;
364 1.7 christos case MSG_RUN:
365 1.7 christos if (tmp->run_length >= tmp->run_num_allocated &&
366 1.7 christos msg_run_expand_to_hold_more(tmp) < 0) {
367 1.7 christos puts("HEY NOW");
368 1.7 christos return (-1);
369 1.7 christos }
370 1.7 christos tmp->run_data[tmp->run_length] = run_new();
371 1.7 christos if (tmp->run_data[tmp->run_length] == NULL)
372 1.7 christos return (-1);
373 1.8 christos if (evtag_unmarshal_run(evbuf, MSG_RUN,
374 1.8 christos tmp->run_data[tmp->run_length]) == -1) {
375 1.7 christos event_warnx("%s: failed to unmarshal run", __func__);
376 1.7 christos return (-1);
377 1.7 christos }
378 1.7 christos ++tmp->run_length;
379 1.7 christos tmp->run_set = 1;
380 1.7 christos break;
381 1.7 christos default:
382 1.7 christos return -1;
383 1.7 christos }
384 1.7 christos }
385 1.7 christos
386 1.7 christos if (msg_complete(tmp) == -1)
387 1.7 christos return (-1);
388 1.7 christos return (0);
389 1.7 christos }
390 1.7 christos
391 1.7 christos int
392 1.7 christos msg_complete(struct msg *msg)
393 1.7 christos {
394 1.7 christos if (!msg->from_name_set)
395 1.7 christos return (-1);
396 1.7 christos if (!msg->to_name_set)
397 1.7 christos return (-1);
398 1.7 christos if (msg->attack_set && kill_complete(msg->attack_data) == -1)
399 1.7 christos return (-1);
400 1.7 christos {
401 1.7 christos int i;
402 1.7 christos for (i = 0; i < msg->run_length; ++i) {
403 1.7 christos if (msg->run_set && run_complete(msg->run_data[i]) == -1)
404 1.7 christos return (-1);
405 1.7 christos }
406 1.7 christos }
407 1.7 christos return (0);
408 1.7 christos }
409 1.7 christos
410 1.7 christos int
411 1.8 christos evtag_unmarshal_msg(struct evbuffer *evbuf, ev_uint32_t need_tag,
412 1.8 christos struct msg *msg)
413 1.7 christos {
414 1.7 christos ev_uint32_t tag;
415 1.7 christos int res = -1;
416 1.7 christos
417 1.7 christos struct evbuffer *tmp = evbuffer_new();
418 1.7 christos
419 1.7 christos if (evtag_unmarshal(evbuf, &tag, tmp) == -1 || tag != need_tag)
420 1.7 christos goto error;
421 1.7 christos
422 1.7 christos if (msg_unmarshal(msg, tmp) == -1)
423 1.7 christos goto error;
424 1.7 christos
425 1.7 christos res = 0;
426 1.7 christos
427 1.7 christos error:
428 1.7 christos evbuffer_free(tmp);
429 1.7 christos return (res);
430 1.7 christos }
431 1.7 christos
432 1.7 christos void
433 1.8 christos evtag_marshal_msg(struct evbuffer *evbuf, ev_uint32_t tag,
434 1.8 christos const struct msg *msg)
435 1.7 christos {
436 1.7 christos struct evbuffer *buf_ = evbuffer_new();
437 1.7 christos assert(buf_ != NULL);
438 1.7 christos msg_marshal(buf_, msg);
439 1.7 christos evtag_marshal_buffer(evbuf, tag, buf_);
440 1.8 christos evbuffer_free(buf_);
441 1.7 christos }
442 1.7 christos
443 1.7 christos /*
444 1.7 christos * Implementation of kill
445 1.7 christos */
446 1.7 christos
447 1.7 christos static struct kill_access_ kill_base__ = {
448 1.7 christos kill_weapon_assign,
449 1.7 christos kill_weapon_get,
450 1.7 christos kill_action_assign,
451 1.7 christos kill_action_get,
452 1.7 christos kill_how_often_assign,
453 1.7 christos kill_how_often_get,
454 1.7 christos kill_how_often_add,
455 1.7 christos };
456 1.7 christos
457 1.7 christos struct kill *
458 1.7 christos kill_new(void)
459 1.7 christos {
460 1.7 christos return kill_new_with_arg(NULL);
461 1.7 christos }
462 1.7 christos
463 1.7 christos struct kill *
464 1.7 christos kill_new_with_arg(void *unused)
465 1.7 christos {
466 1.7 christos struct kill *tmp;
467 1.7 christos if ((tmp = malloc(sizeof(struct kill))) == NULL) {
468 1.7 christos event_warn("%s: malloc", __func__);
469 1.7 christos return (NULL);
470 1.7 christos }
471 1.7 christos tmp->base = &kill_base__;
472 1.7 christos
473 1.7 christos tmp->weapon_data = NULL;
474 1.7 christos tmp->weapon_set = 0;
475 1.7 christos
476 1.7 christos tmp->action_data = NULL;
477 1.7 christos tmp->action_set = 0;
478 1.7 christos
479 1.7 christos tmp->how_often_data = NULL;
480 1.7 christos tmp->how_often_length = 0;
481 1.7 christos tmp->how_often_num_allocated = 0;
482 1.7 christos tmp->how_often_set = 0;
483 1.7 christos
484 1.7 christos return (tmp);
485 1.7 christos }
486 1.7 christos
487 1.7 christos
488 1.7 christos
489 1.7 christos static int
490 1.7 christos kill_how_often_expand_to_hold_more(struct kill *msg)
491 1.7 christos {
492 1.7 christos int tobe_allocated = msg->how_often_num_allocated;
493 1.7 christos ev_uint32_t* new_data = NULL;
494 1.7 christos tobe_allocated = !tobe_allocated ? 1 : tobe_allocated << 1;
495 1.7 christos new_data = (ev_uint32_t*) realloc(msg->how_often_data,
496 1.7 christos tobe_allocated * sizeof(ev_uint32_t));
497 1.7 christos if (new_data == NULL)
498 1.7 christos return -1;
499 1.7 christos msg->how_often_data = new_data;
500 1.7 christos msg->how_often_num_allocated = tobe_allocated;
501 1.8 christos return 0;
502 1.8 christos }
503 1.7 christos
504 1.7 christos ev_uint32_t *
505 1.7 christos kill_how_often_add(struct kill *msg, const ev_uint32_t value)
506 1.7 christos {
507 1.7 christos if (++msg->how_often_length >= msg->how_often_num_allocated) {
508 1.7 christos if (kill_how_often_expand_to_hold_more(msg)<0)
509 1.7 christos goto error;
510 1.7 christos }
511 1.7 christos msg->how_often_data[msg->how_often_length - 1] = value;
512 1.7 christos msg->how_often_set = 1;
513 1.7 christos return &(msg->how_often_data[msg->how_often_length - 1]);
514 1.7 christos error:
515 1.7 christos --msg->how_often_length;
516 1.7 christos return (NULL);
517 1.7 christos }
518 1.7 christos
519 1.7 christos int
520 1.7 christos kill_weapon_assign(struct kill *msg,
521 1.7 christos const char * value)
522 1.7 christos {
523 1.7 christos if (msg->weapon_data != NULL)
524 1.7 christos free(msg->weapon_data);
525 1.7 christos if ((msg->weapon_data = strdup(value)) == NULL)
526 1.7 christos return (-1);
527 1.7 christos msg->weapon_set = 1;
528 1.7 christos return (0);
529 1.7 christos }
530 1.7 christos
531 1.7 christos int
532 1.7 christos kill_action_assign(struct kill *msg,
533 1.7 christos const char * value)
534 1.7 christos {
535 1.7 christos if (msg->action_data != NULL)
536 1.7 christos free(msg->action_data);
537 1.7 christos if ((msg->action_data = strdup(value)) == NULL)
538 1.7 christos return (-1);
539 1.7 christos msg->action_set = 1;
540 1.7 christos return (0);
541 1.7 christos }
542 1.7 christos
543 1.7 christos int
544 1.7 christos kill_how_often_assign(struct kill *msg, int off,
545 1.8 christos const ev_uint32_t value)
546 1.7 christos {
547 1.7 christos if (!msg->how_often_set || off < 0 || off >= msg->how_often_length)
548 1.7 christos return (-1);
549 1.7 christos
550 1.7 christos {
551 1.7 christos msg->how_often_data[off] = value;
552 1.7 christos }
553 1.7 christos return (0);
554 1.7 christos }
555 1.7 christos
556 1.7 christos int
557 1.7 christos kill_weapon_get(struct kill *msg, char * *value)
558 1.7 christos {
559 1.7 christos if (msg->weapon_set != 1)
560 1.7 christos return (-1);
561 1.7 christos *value = msg->weapon_data;
562 1.7 christos return (0);
563 1.7 christos }
564 1.7 christos
565 1.7 christos int
566 1.7 christos kill_action_get(struct kill *msg, char * *value)
567 1.7 christos {
568 1.7 christos if (msg->action_set != 1)
569 1.7 christos return (-1);
570 1.7 christos *value = msg->action_data;
571 1.7 christos return (0);
572 1.7 christos }
573 1.7 christos
574 1.7 christos int
575 1.7 christos kill_how_often_get(struct kill *msg, int offset,
576 1.7 christos ev_uint32_t *value)
577 1.7 christos {
578 1.7 christos if (!msg->how_often_set || offset < 0 || offset >= msg->how_often_length)
579 1.7 christos return (-1);
580 1.7 christos *value = msg->how_often_data[offset];
581 1.7 christos return (0);
582 1.7 christos }
583 1.7 christos
584 1.7 christos void
585 1.7 christos kill_clear(struct kill *tmp)
586 1.7 christos {
587 1.7 christos if (tmp->weapon_set == 1) {
588 1.7 christos free(tmp->weapon_data);
589 1.7 christos tmp->weapon_data = NULL;
590 1.7 christos tmp->weapon_set = 0;
591 1.7 christos }
592 1.7 christos if (tmp->action_set == 1) {
593 1.7 christos free(tmp->action_data);
594 1.7 christos tmp->action_data = NULL;
595 1.7 christos tmp->action_set = 0;
596 1.7 christos }
597 1.7 christos if (tmp->how_often_set == 1) {
598 1.7 christos free(tmp->how_often_data);
599 1.7 christos tmp->how_often_data = NULL;
600 1.7 christos tmp->how_often_set = 0;
601 1.7 christos tmp->how_often_length = 0;
602 1.7 christos tmp->how_often_num_allocated = 0;
603 1.7 christos }
604 1.7 christos }
605 1.7 christos
606 1.7 christos void
607 1.7 christos kill_free(struct kill *tmp)
608 1.7 christos {
609 1.7 christos if (tmp->weapon_data != NULL)
610 1.7 christos free (tmp->weapon_data);
611 1.7 christos if (tmp->action_data != NULL)
612 1.7 christos free (tmp->action_data);
613 1.7 christos if (tmp->how_often_set == 1) {
614 1.7 christos free(tmp->how_often_data);
615 1.7 christos tmp->how_often_data = NULL;
616 1.7 christos tmp->how_often_set = 0;
617 1.7 christos tmp->how_often_length = 0;
618 1.7 christos tmp->how_often_num_allocated = 0;
619 1.7 christos }
620 1.7 christos free(tmp->how_often_data);
621 1.7 christos free(tmp);
622 1.7 christos }
623 1.7 christos
624 1.7 christos void
625 1.8 christos kill_marshal(struct evbuffer *evbuf, const struct kill *tmp) {
626 1.7 christos evtag_marshal_string(evbuf, KILL_WEAPON, tmp->weapon_data);
627 1.7 christos evtag_marshal_string(evbuf, KILL_ACTION, tmp->action_data);
628 1.7 christos if (tmp->how_often_set) {
629 1.7 christos {
630 1.7 christos int i;
631 1.7 christos for (i = 0; i < tmp->how_often_length; ++i) {
632 1.7 christos evtag_marshal_int(evbuf, KILL_HOW_OFTEN, tmp->how_often_data[i]);
633 1.7 christos }
634 1.7 christos }
635 1.7 christos }
636 1.7 christos }
637 1.7 christos
638 1.7 christos int
639 1.8 christos kill_unmarshal(struct kill *tmp, struct evbuffer *evbuf)
640 1.7 christos {
641 1.7 christos ev_uint32_t tag;
642 1.7 christos while (evbuffer_get_length(evbuf) > 0) {
643 1.7 christos if (evtag_peek(evbuf, &tag) == -1)
644 1.7 christos return (-1);
645 1.7 christos switch (tag) {
646 1.7 christos
647 1.7 christos case KILL_WEAPON:
648 1.7 christos if (tmp->weapon_set)
649 1.7 christos return (-1);
650 1.7 christos if (evtag_unmarshal_string(evbuf, KILL_WEAPON, &tmp->weapon_data) == -1) {
651 1.7 christos event_warnx("%s: failed to unmarshal weapon", __func__);
652 1.7 christos return (-1);
653 1.7 christos }
654 1.7 christos tmp->weapon_set = 1;
655 1.7 christos break;
656 1.7 christos case KILL_ACTION:
657 1.7 christos if (tmp->action_set)
658 1.7 christos return (-1);
659 1.7 christos if (evtag_unmarshal_string(evbuf, KILL_ACTION, &tmp->action_data) == -1) {
660 1.7 christos event_warnx("%s: failed to unmarshal action", __func__);
661 1.7 christos return (-1);
662 1.7 christos }
663 1.7 christos tmp->action_set = 1;
664 1.7 christos break;
665 1.7 christos case KILL_HOW_OFTEN:
666 1.7 christos if (tmp->how_often_length >= tmp->how_often_num_allocated &&
667 1.7 christos kill_how_often_expand_to_hold_more(tmp) < 0) {
668 1.7 christos puts("HEY NOW");
669 1.7 christos return (-1);
670 1.7 christos }
671 1.7 christos if (evtag_unmarshal_int(evbuf, KILL_HOW_OFTEN, &tmp->how_often_data[tmp->how_often_length]) == -1) {
672 1.7 christos event_warnx("%s: failed to unmarshal how_often", __func__);
673 1.7 christos return (-1);
674 1.7 christos }
675 1.7 christos ++tmp->how_often_length;
676 1.7 christos tmp->how_often_set = 1;
677 1.7 christos break;
678 1.7 christos default:
679 1.7 christos return -1;
680 1.7 christos }
681 1.7 christos }
682 1.7 christos
683 1.7 christos if (kill_complete(tmp) == -1)
684 1.7 christos return (-1);
685 1.7 christos return (0);
686 1.7 christos }
687 1.7 christos
688 1.7 christos int
689 1.7 christos kill_complete(struct kill *msg)
690 1.7 christos {
691 1.7 christos if (!msg->weapon_set)
692 1.7 christos return (-1);
693 1.7 christos if (!msg->action_set)
694 1.7 christos return (-1);
695 1.7 christos return (0);
696 1.7 christos }
697 1.7 christos
698 1.7 christos int
699 1.8 christos evtag_unmarshal_kill(struct evbuffer *evbuf, ev_uint32_t need_tag,
700 1.8 christos struct kill *msg)
701 1.7 christos {
702 1.7 christos ev_uint32_t tag;
703 1.7 christos int res = -1;
704 1.7 christos
705 1.7 christos struct evbuffer *tmp = evbuffer_new();
706 1.7 christos
707 1.7 christos if (evtag_unmarshal(evbuf, &tag, tmp) == -1 || tag != need_tag)
708 1.7 christos goto error;
709 1.7 christos
710 1.7 christos if (kill_unmarshal(msg, tmp) == -1)
711 1.7 christos goto error;
712 1.7 christos
713 1.7 christos res = 0;
714 1.7 christos
715 1.7 christos error:
716 1.7 christos evbuffer_free(tmp);
717 1.7 christos return (res);
718 1.7 christos }
719 1.7 christos
720 1.7 christos void
721 1.8 christos evtag_marshal_kill(struct evbuffer *evbuf, ev_uint32_t tag,
722 1.8 christos const struct kill *msg)
723 1.7 christos {
724 1.7 christos struct evbuffer *buf_ = evbuffer_new();
725 1.7 christos assert(buf_ != NULL);
726 1.7 christos kill_marshal(buf_, msg);
727 1.7 christos evtag_marshal_buffer(evbuf, tag, buf_);
728 1.8 christos evbuffer_free(buf_);
729 1.7 christos }
730 1.7 christos
731 1.7 christos /*
732 1.7 christos * Implementation of run
733 1.7 christos */
734 1.7 christos
735 1.7 christos static struct run_access_ run_base__ = {
736 1.7 christos run_how_assign,
737 1.7 christos run_how_get,
738 1.7 christos run_some_bytes_assign,
739 1.7 christos run_some_bytes_get,
740 1.7 christos run_fixed_bytes_assign,
741 1.7 christos run_fixed_bytes_get,
742 1.7 christos run_notes_assign,
743 1.7 christos run_notes_get,
744 1.7 christos run_notes_add,
745 1.7 christos run_large_number_assign,
746 1.7 christos run_large_number_get,
747 1.7 christos run_other_numbers_assign,
748 1.7 christos run_other_numbers_get,
749 1.7 christos run_other_numbers_add,
750 1.7 christos };
751 1.7 christos
752 1.7 christos struct run *
753 1.7 christos run_new(void)
754 1.7 christos {
755 1.7 christos return run_new_with_arg(NULL);
756 1.7 christos }
757 1.7 christos
758 1.7 christos struct run *
759 1.7 christos run_new_with_arg(void *unused)
760 1.7 christos {
761 1.7 christos struct run *tmp;
762 1.7 christos if ((tmp = malloc(sizeof(struct run))) == NULL) {
763 1.7 christos event_warn("%s: malloc", __func__);
764 1.7 christos return (NULL);
765 1.7 christos }
766 1.7 christos tmp->base = &run_base__;
767 1.7 christos
768 1.7 christos tmp->how_data = NULL;
769 1.7 christos tmp->how_set = 0;
770 1.7 christos
771 1.7 christos tmp->some_bytes_data = NULL;
772 1.7 christos tmp->some_bytes_length = 0;
773 1.7 christos tmp->some_bytes_set = 0;
774 1.7 christos
775 1.7 christos memset(tmp->fixed_bytes_data, 0, sizeof(tmp->fixed_bytes_data));
776 1.7 christos tmp->fixed_bytes_set = 0;
777 1.7 christos
778 1.7 christos tmp->notes_data = NULL;
779 1.7 christos tmp->notes_length = 0;
780 1.7 christos tmp->notes_num_allocated = 0;
781 1.7 christos tmp->notes_set = 0;
782 1.7 christos
783 1.7 christos tmp->large_number_data = 0;
784 1.7 christos tmp->large_number_set = 0;
785 1.7 christos
786 1.7 christos tmp->other_numbers_data = NULL;
787 1.7 christos tmp->other_numbers_length = 0;
788 1.7 christos tmp->other_numbers_num_allocated = 0;
789 1.7 christos tmp->other_numbers_set = 0;
790 1.7 christos
791 1.7 christos return (tmp);
792 1.7 christos }
793 1.7 christos
794 1.7 christos
795 1.7 christos
796 1.7 christos
797 1.7 christos static int
798 1.7 christos run_notes_expand_to_hold_more(struct run *msg)
799 1.7 christos {
800 1.7 christos int tobe_allocated = msg->notes_num_allocated;
801 1.7 christos char ** new_data = NULL;
802 1.7 christos tobe_allocated = !tobe_allocated ? 1 : tobe_allocated << 1;
803 1.7 christos new_data = (char **) realloc(msg->notes_data,
804 1.7 christos tobe_allocated * sizeof(char *));
805 1.7 christos if (new_data == NULL)
806 1.7 christos return -1;
807 1.7 christos msg->notes_data = new_data;
808 1.7 christos msg->notes_num_allocated = tobe_allocated;
809 1.8 christos return 0;
810 1.8 christos }
811 1.7 christos
812 1.7 christos char * *
813 1.7 christos run_notes_add(struct run *msg, const char * value)
814 1.7 christos {
815 1.7 christos if (++msg->notes_length >= msg->notes_num_allocated) {
816 1.7 christos if (run_notes_expand_to_hold_more(msg)<0)
817 1.7 christos goto error;
818 1.7 christos }
819 1.7 christos if (value != NULL) {
820 1.7 christos msg->notes_data[msg->notes_length - 1] = strdup(value);
821 1.7 christos if (msg->notes_data[msg->notes_length - 1] == NULL) {
822 1.7 christos goto error;
823 1.7 christos }
824 1.7 christos } else {
825 1.7 christos msg->notes_data[msg->notes_length - 1] = NULL;
826 1.7 christos }
827 1.7 christos msg->notes_set = 1;
828 1.7 christos return &(msg->notes_data[msg->notes_length - 1]);
829 1.7 christos error:
830 1.7 christos --msg->notes_length;
831 1.7 christos return (NULL);
832 1.7 christos }
833 1.7 christos
834 1.7 christos
835 1.7 christos static int
836 1.7 christos run_other_numbers_expand_to_hold_more(struct run *msg)
837 1.7 christos {
838 1.7 christos int tobe_allocated = msg->other_numbers_num_allocated;
839 1.7 christos ev_uint32_t* new_data = NULL;
840 1.7 christos tobe_allocated = !tobe_allocated ? 1 : tobe_allocated << 1;
841 1.7 christos new_data = (ev_uint32_t*) realloc(msg->other_numbers_data,
842 1.7 christos tobe_allocated * sizeof(ev_uint32_t));
843 1.7 christos if (new_data == NULL)
844 1.7 christos return -1;
845 1.7 christos msg->other_numbers_data = new_data;
846 1.7 christos msg->other_numbers_num_allocated = tobe_allocated;
847 1.8 christos return 0;
848 1.8 christos }
849 1.7 christos
850 1.7 christos ev_uint32_t *
851 1.7 christos run_other_numbers_add(struct run *msg, const ev_uint32_t value)
852 1.7 christos {
853 1.7 christos if (++msg->other_numbers_length >= msg->other_numbers_num_allocated) {
854 1.7 christos if (run_other_numbers_expand_to_hold_more(msg)<0)
855 1.7 christos goto error;
856 1.7 christos }
857 1.7 christos msg->other_numbers_data[msg->other_numbers_length - 1] = value;
858 1.7 christos msg->other_numbers_set = 1;
859 1.7 christos return &(msg->other_numbers_data[msg->other_numbers_length - 1]);
860 1.7 christos error:
861 1.7 christos --msg->other_numbers_length;
862 1.7 christos return (NULL);
863 1.7 christos }
864 1.7 christos
865 1.7 christos int
866 1.7 christos run_how_assign(struct run *msg,
867 1.7 christos const char * value)
868 1.7 christos {
869 1.7 christos if (msg->how_data != NULL)
870 1.7 christos free(msg->how_data);
871 1.7 christos if ((msg->how_data = strdup(value)) == NULL)
872 1.7 christos return (-1);
873 1.7 christos msg->how_set = 1;
874 1.7 christos return (0);
875 1.7 christos }
876 1.7 christos
877 1.7 christos int
878 1.7 christos run_some_bytes_assign(struct run *msg, const ev_uint8_t * value, ev_uint32_t len)
879 1.7 christos {
880 1.7 christos if (msg->some_bytes_data != NULL)
881 1.7 christos free (msg->some_bytes_data);
882 1.7 christos msg->some_bytes_data = malloc(len);
883 1.7 christos if (msg->some_bytes_data == NULL)
884 1.7 christos return (-1);
885 1.7 christos msg->some_bytes_set = 1;
886 1.7 christos msg->some_bytes_length = len;
887 1.7 christos memcpy(msg->some_bytes_data, value, len);
888 1.7 christos return (0);
889 1.7 christos }
890 1.7 christos
891 1.7 christos int
892 1.7 christos run_fixed_bytes_assign(struct run *msg, const ev_uint8_t *value)
893 1.7 christos {
894 1.7 christos msg->fixed_bytes_set = 1;
895 1.7 christos memcpy(msg->fixed_bytes_data, value, 24);
896 1.7 christos return (0);
897 1.7 christos }
898 1.7 christos
899 1.7 christos int
900 1.7 christos run_notes_assign(struct run *msg, int off,
901 1.8 christos const char * value)
902 1.7 christos {
903 1.7 christos if (!msg->notes_set || off < 0 || off >= msg->notes_length)
904 1.7 christos return (-1);
905 1.7 christos
906 1.7 christos {
907 1.7 christos if (msg->notes_data[off] != NULL)
908 1.7 christos free(msg->notes_data[off]);
909 1.7 christos msg->notes_data[off] = strdup(value);
910 1.7 christos if (msg->notes_data[off] == NULL) {
911 1.7 christos event_warnx("%s: strdup", __func__);
912 1.7 christos return (-1);
913 1.7 christos }
914 1.7 christos }
915 1.7 christos return (0);
916 1.7 christos }
917 1.7 christos
918 1.7 christos int
919 1.7 christos run_large_number_assign(struct run *msg, const ev_uint64_t value)
920 1.7 christos {
921 1.7 christos msg->large_number_set = 1;
922 1.7 christos msg->large_number_data = value;
923 1.7 christos return (0);
924 1.7 christos }
925 1.7 christos
926 1.7 christos int
927 1.7 christos run_other_numbers_assign(struct run *msg, int off,
928 1.8 christos const ev_uint32_t value)
929 1.7 christos {
930 1.7 christos if (!msg->other_numbers_set || off < 0 || off >= msg->other_numbers_length)
931 1.7 christos return (-1);
932 1.7 christos
933 1.7 christos {
934 1.7 christos msg->other_numbers_data[off] = value;
935 1.7 christos }
936 1.7 christos return (0);
937 1.7 christos }
938 1.7 christos
939 1.7 christos int
940 1.7 christos run_how_get(struct run *msg, char * *value)
941 1.7 christos {
942 1.7 christos if (msg->how_set != 1)
943 1.7 christos return (-1);
944 1.7 christos *value = msg->how_data;
945 1.7 christos return (0);
946 1.7 christos }
947 1.7 christos
948 1.7 christos int
949 1.7 christos run_some_bytes_get(struct run *msg, ev_uint8_t * *value, ev_uint32_t *plen)
950 1.7 christos {
951 1.7 christos if (msg->some_bytes_set != 1)
952 1.7 christos return (-1);
953 1.7 christos *value = msg->some_bytes_data;
954 1.7 christos *plen = msg->some_bytes_length;
955 1.7 christos return (0);
956 1.7 christos }
957 1.7 christos
958 1.7 christos int
959 1.7 christos run_fixed_bytes_get(struct run *msg, ev_uint8_t **value)
960 1.7 christos {
961 1.7 christos if (msg->fixed_bytes_set != 1)
962 1.7 christos return (-1);
963 1.7 christos *value = msg->fixed_bytes_data;
964 1.7 christos return (0);
965 1.7 christos }
966 1.7 christos
967 1.7 christos int
968 1.7 christos run_notes_get(struct run *msg, int offset,
969 1.7 christos char * *value)
970 1.7 christos {
971 1.7 christos if (!msg->notes_set || offset < 0 || offset >= msg->notes_length)
972 1.7 christos return (-1);
973 1.7 christos *value = msg->notes_data[offset];
974 1.7 christos return (0);
975 1.7 christos }
976 1.7 christos
977 1.7 christos int
978 1.7 christos run_large_number_get(struct run *msg, ev_uint64_t *value)
979 1.7 christos {
980 1.7 christos if (msg->large_number_set != 1)
981 1.7 christos return (-1);
982 1.7 christos *value = msg->large_number_data;
983 1.7 christos return (0);
984 1.7 christos }
985 1.7 christos
986 1.7 christos int
987 1.7 christos run_other_numbers_get(struct run *msg, int offset,
988 1.7 christos ev_uint32_t *value)
989 1.7 christos {
990 1.7 christos if (!msg->other_numbers_set || offset < 0 || offset >= msg->other_numbers_length)
991 1.7 christos return (-1);
992 1.7 christos *value = msg->other_numbers_data[offset];
993 1.7 christos return (0);
994 1.7 christos }
995 1.7 christos
996 1.7 christos void
997 1.7 christos run_clear(struct run *tmp)
998 1.7 christos {
999 1.7 christos if (tmp->how_set == 1) {
1000 1.7 christos free(tmp->how_data);
1001 1.7 christos tmp->how_data = NULL;
1002 1.7 christos tmp->how_set = 0;
1003 1.7 christos }
1004 1.7 christos if (tmp->some_bytes_set == 1) {
1005 1.7 christos free (tmp->some_bytes_data);
1006 1.7 christos tmp->some_bytes_data = NULL;
1007 1.7 christos tmp->some_bytes_length = 0;
1008 1.7 christos tmp->some_bytes_set = 0;
1009 1.7 christos }
1010 1.7 christos tmp->fixed_bytes_set = 0;
1011 1.7 christos memset(tmp->fixed_bytes_data, 0, sizeof(tmp->fixed_bytes_data));
1012 1.7 christos if (tmp->notes_set == 1) {
1013 1.7 christos int i;
1014 1.7 christos for (i = 0; i < tmp->notes_length; ++i) {
1015 1.7 christos if (tmp->notes_data[i] != NULL) free(tmp->notes_data[i]);
1016 1.7 christos }
1017 1.7 christos free(tmp->notes_data);
1018 1.7 christos tmp->notes_data = NULL;
1019 1.7 christos tmp->notes_set = 0;
1020 1.7 christos tmp->notes_length = 0;
1021 1.7 christos tmp->notes_num_allocated = 0;
1022 1.7 christos }
1023 1.7 christos tmp->large_number_set = 0;
1024 1.7 christos if (tmp->other_numbers_set == 1) {
1025 1.7 christos free(tmp->other_numbers_data);
1026 1.7 christos tmp->other_numbers_data = NULL;
1027 1.7 christos tmp->other_numbers_set = 0;
1028 1.7 christos tmp->other_numbers_length = 0;
1029 1.7 christos tmp->other_numbers_num_allocated = 0;
1030 1.7 christos }
1031 1.7 christos }
1032 1.7 christos
1033 1.7 christos void
1034 1.7 christos run_free(struct run *tmp)
1035 1.7 christos {
1036 1.7 christos if (tmp->how_data != NULL)
1037 1.7 christos free (tmp->how_data);
1038 1.7 christos if (tmp->some_bytes_data != NULL)
1039 1.7 christos free(tmp->some_bytes_data);
1040 1.7 christos if (tmp->notes_set == 1) {
1041 1.7 christos int i;
1042 1.7 christos for (i = 0; i < tmp->notes_length; ++i) {
1043 1.7 christos if (tmp->notes_data[i] != NULL) free(tmp->notes_data[i]);
1044 1.7 christos }
1045 1.7 christos free(tmp->notes_data);
1046 1.7 christos tmp->notes_data = NULL;
1047 1.7 christos tmp->notes_set = 0;
1048 1.7 christos tmp->notes_length = 0;
1049 1.7 christos tmp->notes_num_allocated = 0;
1050 1.7 christos }
1051 1.7 christos free(tmp->notes_data);
1052 1.7 christos if (tmp->other_numbers_set == 1) {
1053 1.7 christos free(tmp->other_numbers_data);
1054 1.7 christos tmp->other_numbers_data = NULL;
1055 1.7 christos tmp->other_numbers_set = 0;
1056 1.7 christos tmp->other_numbers_length = 0;
1057 1.7 christos tmp->other_numbers_num_allocated = 0;
1058 1.7 christos }
1059 1.7 christos free(tmp->other_numbers_data);
1060 1.7 christos free(tmp);
1061 1.7 christos }
1062 1.7 christos
1063 1.7 christos void
1064 1.8 christos run_marshal(struct evbuffer *evbuf, const struct run *tmp) {
1065 1.7 christos evtag_marshal_string(evbuf, RUN_HOW, tmp->how_data);
1066 1.7 christos if (tmp->some_bytes_set) {
1067 1.7 christos evtag_marshal(evbuf, RUN_SOME_BYTES, tmp->some_bytes_data, tmp->some_bytes_length);
1068 1.7 christos }
1069 1.7 christos evtag_marshal(evbuf, RUN_FIXED_BYTES, tmp->fixed_bytes_data, (24));
1070 1.7 christos if (tmp->notes_set) {
1071 1.7 christos {
1072 1.7 christos int i;
1073 1.7 christos for (i = 0; i < tmp->notes_length; ++i) {
1074 1.7 christos evtag_marshal_string(evbuf, RUN_NOTES, tmp->notes_data[i]);
1075 1.7 christos }
1076 1.7 christos }
1077 1.7 christos }
1078 1.7 christos if (tmp->large_number_set) {
1079 1.7 christos evtag_marshal_int64(evbuf, RUN_LARGE_NUMBER, tmp->large_number_data);
1080 1.7 christos }
1081 1.7 christos if (tmp->other_numbers_set) {
1082 1.7 christos {
1083 1.7 christos int i;
1084 1.7 christos for (i = 0; i < tmp->other_numbers_length; ++i) {
1085 1.7 christos evtag_marshal_int(evbuf, RUN_OTHER_NUMBERS, tmp->other_numbers_data[i]);
1086 1.7 christos }
1087 1.7 christos }
1088 1.7 christos }
1089 1.7 christos }
1090 1.7 christos
1091 1.7 christos int
1092 1.8 christos run_unmarshal(struct run *tmp, struct evbuffer *evbuf)
1093 1.7 christos {
1094 1.7 christos ev_uint32_t tag;
1095 1.7 christos while (evbuffer_get_length(evbuf) > 0) {
1096 1.7 christos if (evtag_peek(evbuf, &tag) == -1)
1097 1.7 christos return (-1);
1098 1.7 christos switch (tag) {
1099 1.7 christos
1100 1.7 christos case RUN_HOW:
1101 1.7 christos if (tmp->how_set)
1102 1.7 christos return (-1);
1103 1.7 christos if (evtag_unmarshal_string(evbuf, RUN_HOW, &tmp->how_data) == -1) {
1104 1.7 christos event_warnx("%s: failed to unmarshal how", __func__);
1105 1.7 christos return (-1);
1106 1.7 christos }
1107 1.7 christos tmp->how_set = 1;
1108 1.7 christos break;
1109 1.7 christos case RUN_SOME_BYTES:
1110 1.7 christos if (tmp->some_bytes_set)
1111 1.7 christos return (-1);
1112 1.7 christos if (evtag_payload_length(evbuf, &tmp->some_bytes_length) == -1)
1113 1.7 christos return (-1);
1114 1.7 christos if (tmp->some_bytes_length > evbuffer_get_length(evbuf))
1115 1.7 christos return (-1);
1116 1.7 christos if ((tmp->some_bytes_data = malloc(tmp->some_bytes_length)) == NULL)
1117 1.7 christos return (-1);
1118 1.7 christos if (evtag_unmarshal_fixed(evbuf, RUN_SOME_BYTES, tmp->some_bytes_data, tmp->some_bytes_length) == -1) {
1119 1.7 christos event_warnx("%s: failed to unmarshal some_bytes", __func__);
1120 1.7 christos return (-1);
1121 1.7 christos }
1122 1.7 christos tmp->some_bytes_set = 1;
1123 1.7 christos break;
1124 1.7 christos case RUN_FIXED_BYTES:
1125 1.7 christos if (tmp->fixed_bytes_set)
1126 1.7 christos return (-1);
1127 1.7 christos if (evtag_unmarshal_fixed(evbuf, RUN_FIXED_BYTES, tmp->fixed_bytes_data, (24)) == -1) {
1128 1.7 christos event_warnx("%s: failed to unmarshal fixed_bytes", __func__);
1129 1.7 christos return (-1);
1130 1.7 christos }
1131 1.7 christos tmp->fixed_bytes_set = 1;
1132 1.7 christos break;
1133 1.7 christos case RUN_NOTES:
1134 1.7 christos if (tmp->notes_length >= tmp->notes_num_allocated &&
1135 1.7 christos run_notes_expand_to_hold_more(tmp) < 0) {
1136 1.7 christos puts("HEY NOW");
1137 1.7 christos return (-1);
1138 1.7 christos }
1139 1.7 christos if (evtag_unmarshal_string(evbuf, RUN_NOTES, &tmp->notes_data[tmp->notes_length]) == -1) {
1140 1.7 christos event_warnx("%s: failed to unmarshal notes", __func__);
1141 1.7 christos return (-1);
1142 1.7 christos }
1143 1.7 christos ++tmp->notes_length;
1144 1.7 christos tmp->notes_set = 1;
1145 1.7 christos break;
1146 1.7 christos case RUN_LARGE_NUMBER:
1147 1.7 christos if (tmp->large_number_set)
1148 1.7 christos return (-1);
1149 1.7 christos if (evtag_unmarshal_int64(evbuf, RUN_LARGE_NUMBER, &tmp->large_number_data) == -1) {
1150 1.7 christos event_warnx("%s: failed to unmarshal large_number", __func__);
1151 1.7 christos return (-1);
1152 1.7 christos }
1153 1.7 christos tmp->large_number_set = 1;
1154 1.7 christos break;
1155 1.7 christos case RUN_OTHER_NUMBERS:
1156 1.7 christos if (tmp->other_numbers_length >= tmp->other_numbers_num_allocated &&
1157 1.7 christos run_other_numbers_expand_to_hold_more(tmp) < 0) {
1158 1.7 christos puts("HEY NOW");
1159 1.7 christos return (-1);
1160 1.7 christos }
1161 1.7 christos if (evtag_unmarshal_int(evbuf, RUN_OTHER_NUMBERS, &tmp->other_numbers_data[tmp->other_numbers_length]) == -1) {
1162 1.7 christos event_warnx("%s: failed to unmarshal other_numbers", __func__);
1163 1.7 christos return (-1);
1164 1.7 christos }
1165 1.7 christos ++tmp->other_numbers_length;
1166 1.7 christos tmp->other_numbers_set = 1;
1167 1.7 christos break;
1168 1.7 christos default:
1169 1.7 christos return -1;
1170 1.7 christos }
1171 1.7 christos }
1172 1.7 christos
1173 1.7 christos if (run_complete(tmp) == -1)
1174 1.7 christos return (-1);
1175 1.7 christos return (0);
1176 1.7 christos }
1177 1.7 christos
1178 1.7 christos int
1179 1.7 christos run_complete(struct run *msg)
1180 1.7 christos {
1181 1.7 christos if (!msg->how_set)
1182 1.7 christos return (-1);
1183 1.7 christos if (!msg->fixed_bytes_set)
1184 1.7 christos return (-1);
1185 1.7 christos return (0);
1186 1.7 christos }
1187 1.7 christos
1188 1.7 christos int
1189 1.8 christos evtag_unmarshal_run(struct evbuffer *evbuf, ev_uint32_t need_tag,
1190 1.8 christos struct run *msg)
1191 1.7 christos {
1192 1.7 christos ev_uint32_t tag;
1193 1.7 christos int res = -1;
1194 1.7 christos
1195 1.7 christos struct evbuffer *tmp = evbuffer_new();
1196 1.7 christos
1197 1.7 christos if (evtag_unmarshal(evbuf, &tag, tmp) == -1 || tag != need_tag)
1198 1.7 christos goto error;
1199 1.7 christos
1200 1.7 christos if (run_unmarshal(msg, tmp) == -1)
1201 1.7 christos goto error;
1202 1.7 christos
1203 1.7 christos res = 0;
1204 1.7 christos
1205 1.7 christos error:
1206 1.7 christos evbuffer_free(tmp);
1207 1.7 christos return (res);
1208 1.7 christos }
1209 1.7 christos
1210 1.7 christos void
1211 1.8 christos evtag_marshal_run(struct evbuffer *evbuf, ev_uint32_t tag,
1212 1.8 christos const struct run *msg)
1213 1.7 christos {
1214 1.7 christos struct evbuffer *buf_ = evbuffer_new();
1215 1.7 christos assert(buf_ != NULL);
1216 1.7 christos run_marshal(buf_, msg);
1217 1.7 christos evtag_marshal_buffer(evbuf, tag, buf_);
1218 1.8 christos evbuffer_free(buf_);
1219 1.7 christos }
1220 1.7 christos
1221