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