michael@0: /* michael@0: * Automatically generated from ./regress.rpc michael@0: * by event_rpcgen.py/0.1. DO NOT EDIT THIS FILE. michael@0: */ michael@0: michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: michael@0: #ifdef _EVENT___func__ michael@0: #define __func__ _EVENT___func__ michael@0: #endif michael@0: michael@0: michael@0: #include "regress.gen.h" michael@0: michael@0: void event_warn(const char *fmt, ...); michael@0: void event_warnx(const char *fmt, ...); michael@0: michael@0: michael@0: /* michael@0: * Implementation of msg michael@0: */ michael@0: michael@0: static struct msg_access_ __msg_base = { michael@0: msg_from_name_assign, michael@0: msg_from_name_get, michael@0: msg_to_name_assign, michael@0: msg_to_name_get, michael@0: msg_attack_assign, michael@0: msg_attack_get, michael@0: msg_run_assign, michael@0: msg_run_get, michael@0: msg_run_add, michael@0: }; michael@0: michael@0: struct msg * michael@0: msg_new(void) michael@0: { michael@0: return msg_new_with_arg(NULL); michael@0: } michael@0: michael@0: struct msg * michael@0: msg_new_with_arg(void *unused) michael@0: { michael@0: struct msg *tmp; michael@0: if ((tmp = malloc(sizeof(struct msg))) == NULL) { michael@0: event_warn("%s: malloc", __func__); michael@0: return (NULL); michael@0: } michael@0: tmp->base = &__msg_base; michael@0: michael@0: tmp->from_name_data = NULL; michael@0: tmp->from_name_set = 0; michael@0: michael@0: tmp->to_name_data = NULL; michael@0: tmp->to_name_set = 0; michael@0: michael@0: tmp->attack_data = NULL; michael@0: tmp->attack_set = 0; michael@0: michael@0: tmp->run_data = NULL; michael@0: tmp->run_length = 0; michael@0: tmp->run_num_allocated = 0; michael@0: tmp->run_set = 0; michael@0: michael@0: return (tmp); michael@0: } michael@0: michael@0: michael@0: michael@0: michael@0: static int michael@0: msg_run_expand_to_hold_more(struct msg *msg) michael@0: { michael@0: int tobe_allocated = msg->run_num_allocated; michael@0: struct run** new_data = NULL; michael@0: tobe_allocated = !tobe_allocated ? 1 : tobe_allocated << 1; michael@0: new_data = (struct run**) realloc(msg->run_data, michael@0: tobe_allocated * sizeof(struct run*)); michael@0: if (new_data == NULL) michael@0: return -1; michael@0: msg->run_data = new_data; michael@0: msg->run_num_allocated = tobe_allocated; michael@0: return 0;} michael@0: michael@0: struct run* michael@0: msg_run_add(struct msg *msg) michael@0: { michael@0: if (++msg->run_length >= msg->run_num_allocated) { michael@0: if (msg_run_expand_to_hold_more(msg)<0) michael@0: goto error; michael@0: } michael@0: msg->run_data[msg->run_length - 1] = run_new(); michael@0: if (msg->run_data[msg->run_length - 1] == NULL) michael@0: goto error; michael@0: msg->run_set = 1; michael@0: return (msg->run_data[msg->run_length - 1]); michael@0: error: michael@0: --msg->run_length; michael@0: return (NULL); michael@0: } michael@0: michael@0: int michael@0: msg_from_name_assign(struct msg *msg, michael@0: const char * value) michael@0: { michael@0: if (msg->from_name_data != NULL) michael@0: free(msg->from_name_data); michael@0: if ((msg->from_name_data = strdup(value)) == NULL) michael@0: return (-1); michael@0: msg->from_name_set = 1; michael@0: return (0); michael@0: } michael@0: michael@0: int michael@0: msg_to_name_assign(struct msg *msg, michael@0: const char * value) michael@0: { michael@0: if (msg->to_name_data != NULL) michael@0: free(msg->to_name_data); michael@0: if ((msg->to_name_data = strdup(value)) == NULL) michael@0: return (-1); michael@0: msg->to_name_set = 1; michael@0: return (0); michael@0: } michael@0: michael@0: int michael@0: msg_attack_assign(struct msg *msg, michael@0: const struct kill* value) michael@0: { michael@0: struct evbuffer *tmp = NULL; michael@0: if (msg->attack_set) { michael@0: kill_clear(msg->attack_data); michael@0: msg->attack_set = 0; michael@0: } else { michael@0: msg->attack_data = kill_new(); michael@0: if (msg->attack_data == NULL) { michael@0: event_warn("%s: kill_new()", __func__); michael@0: goto error; michael@0: } michael@0: } michael@0: if ((tmp = evbuffer_new()) == NULL) { michael@0: event_warn("%s: evbuffer_new()", __func__); michael@0: goto error; michael@0: } michael@0: kill_marshal(tmp, value); michael@0: if (kill_unmarshal(msg->attack_data, tmp) == -1) { michael@0: event_warnx("%s: kill_unmarshal", __func__); michael@0: goto error; michael@0: } michael@0: msg->attack_set = 1; michael@0: evbuffer_free(tmp); michael@0: return (0); michael@0: error: michael@0: if (tmp != NULL) michael@0: evbuffer_free(tmp); michael@0: if (msg->attack_data != NULL) { michael@0: kill_free(msg->attack_data); michael@0: msg->attack_data = NULL; michael@0: } michael@0: return (-1); michael@0: } michael@0: michael@0: int michael@0: msg_run_assign(struct msg *msg, int off, michael@0: const struct run* value) michael@0: { michael@0: if (!msg->run_set || off < 0 || off >= msg->run_length) michael@0: return (-1); michael@0: michael@0: { michael@0: int had_error = 0; michael@0: struct evbuffer *tmp = NULL; michael@0: run_clear(msg->run_data[off]); michael@0: if ((tmp = evbuffer_new()) == NULL) { michael@0: event_warn("%s: evbuffer_new()", __func__); michael@0: had_error = 1; michael@0: goto done; michael@0: } michael@0: run_marshal(tmp, value); michael@0: if (run_unmarshal(msg->run_data[off], tmp) == -1) { michael@0: event_warnx("%s: run_unmarshal", __func__); michael@0: had_error = 1; michael@0: goto done; michael@0: } michael@0: done:if (tmp != NULL) michael@0: evbuffer_free(tmp); michael@0: if (had_error) { michael@0: run_clear(msg->run_data[off]); michael@0: return (-1); michael@0: } michael@0: } michael@0: return (0); michael@0: } michael@0: michael@0: int michael@0: msg_from_name_get(struct msg *msg, char * *value) michael@0: { michael@0: if (msg->from_name_set != 1) michael@0: return (-1); michael@0: *value = msg->from_name_data; michael@0: return (0); michael@0: } michael@0: michael@0: int michael@0: msg_to_name_get(struct msg *msg, char * *value) michael@0: { michael@0: if (msg->to_name_set != 1) michael@0: return (-1); michael@0: *value = msg->to_name_data; michael@0: return (0); michael@0: } michael@0: michael@0: int michael@0: msg_attack_get(struct msg *msg, struct kill* *value) michael@0: { michael@0: if (msg->attack_set != 1) { michael@0: msg->attack_data = kill_new(); michael@0: if (msg->attack_data == NULL) michael@0: return (-1); michael@0: msg->attack_set = 1; michael@0: } michael@0: *value = msg->attack_data; michael@0: return (0); michael@0: } michael@0: michael@0: int michael@0: msg_run_get(struct msg *msg, int offset, michael@0: struct run* *value) michael@0: { michael@0: if (!msg->run_set || offset < 0 || offset >= msg->run_length) michael@0: return (-1); michael@0: *value = msg->run_data[offset]; michael@0: return (0); michael@0: } michael@0: michael@0: void michael@0: msg_clear(struct msg *tmp) michael@0: { michael@0: if (tmp->from_name_set == 1) { michael@0: free(tmp->from_name_data); michael@0: tmp->from_name_data = NULL; michael@0: tmp->from_name_set = 0; michael@0: } michael@0: if (tmp->to_name_set == 1) { michael@0: free(tmp->to_name_data); michael@0: tmp->to_name_data = NULL; michael@0: tmp->to_name_set = 0; michael@0: } michael@0: if (tmp->attack_set == 1) { michael@0: kill_free(tmp->attack_data); michael@0: tmp->attack_data = NULL; michael@0: tmp->attack_set = 0; michael@0: } michael@0: if (tmp->run_set == 1) { michael@0: int i; michael@0: for (i = 0; i < tmp->run_length; ++i) { michael@0: run_free(tmp->run_data[i]); michael@0: } michael@0: free(tmp->run_data); michael@0: tmp->run_data = NULL; michael@0: tmp->run_set = 0; michael@0: tmp->run_length = 0; michael@0: tmp->run_num_allocated = 0; michael@0: } michael@0: } michael@0: michael@0: void michael@0: msg_free(struct msg *tmp) michael@0: { michael@0: if (tmp->from_name_data != NULL) michael@0: free (tmp->from_name_data); michael@0: if (tmp->to_name_data != NULL) michael@0: free (tmp->to_name_data); michael@0: if (tmp->attack_data != NULL) michael@0: kill_free(tmp->attack_data); michael@0: if (tmp->run_set == 1) { michael@0: int i; michael@0: for (i = 0; i < tmp->run_length; ++i) { michael@0: run_free(tmp->run_data[i]); michael@0: } michael@0: free(tmp->run_data); michael@0: tmp->run_data = NULL; michael@0: tmp->run_set = 0; michael@0: tmp->run_length = 0; michael@0: tmp->run_num_allocated = 0; michael@0: } michael@0: free(tmp->run_data); michael@0: free(tmp); michael@0: } michael@0: michael@0: void michael@0: msg_marshal(struct evbuffer *evbuf, const struct msg *tmp){ michael@0: evtag_marshal_string(evbuf, MSG_FROM_NAME, tmp->from_name_data); michael@0: evtag_marshal_string(evbuf, MSG_TO_NAME, tmp->to_name_data); michael@0: if (tmp->attack_set) { michael@0: evtag_marshal_kill(evbuf, MSG_ATTACK, tmp->attack_data); michael@0: } michael@0: if (tmp->run_set) { michael@0: { michael@0: int i; michael@0: for (i = 0; i < tmp->run_length; ++i) { michael@0: evtag_marshal_run(evbuf, MSG_RUN, tmp->run_data[i]); michael@0: } michael@0: } michael@0: } michael@0: } michael@0: michael@0: int michael@0: msg_unmarshal(struct msg *tmp, struct evbuffer *evbuf) michael@0: { michael@0: ev_uint32_t tag; michael@0: while (evbuffer_get_length(evbuf) > 0) { michael@0: if (evtag_peek(evbuf, &tag) == -1) michael@0: return (-1); michael@0: switch (tag) { michael@0: michael@0: case MSG_FROM_NAME: michael@0: michael@0: if (tmp->from_name_set) michael@0: return (-1); michael@0: if (evtag_unmarshal_string(evbuf, MSG_FROM_NAME, &tmp->from_name_data) == -1) { michael@0: event_warnx("%s: failed to unmarshal from_name", __func__); michael@0: return (-1); michael@0: } michael@0: tmp->from_name_set = 1; michael@0: break; michael@0: michael@0: case MSG_TO_NAME: michael@0: michael@0: if (tmp->to_name_set) michael@0: return (-1); michael@0: if (evtag_unmarshal_string(evbuf, MSG_TO_NAME, &tmp->to_name_data) == -1) { michael@0: event_warnx("%s: failed to unmarshal to_name", __func__); michael@0: return (-1); michael@0: } michael@0: tmp->to_name_set = 1; michael@0: break; michael@0: michael@0: case MSG_ATTACK: michael@0: michael@0: if (tmp->attack_set) michael@0: return (-1); michael@0: tmp->attack_data = kill_new(); michael@0: if (tmp->attack_data == NULL) michael@0: return (-1); michael@0: if (evtag_unmarshal_kill(evbuf, MSG_ATTACK, tmp->attack_data) == -1) { michael@0: event_warnx("%s: failed to unmarshal attack", __func__); michael@0: return (-1); michael@0: } michael@0: tmp->attack_set = 1; michael@0: break; michael@0: michael@0: case MSG_RUN: michael@0: michael@0: if (tmp->run_length >= tmp->run_num_allocated && michael@0: msg_run_expand_to_hold_more(tmp) < 0) { michael@0: puts("HEY NOW"); michael@0: return (-1); michael@0: } michael@0: tmp->run_data[tmp->run_length] = run_new(); michael@0: if (tmp->run_data[tmp->run_length] == NULL) michael@0: return (-1); michael@0: if (evtag_unmarshal_run(evbuf, MSG_RUN, tmp->run_data[tmp->run_length]) == -1) { michael@0: event_warnx("%s: failed to unmarshal run", __func__); michael@0: return (-1); michael@0: } michael@0: ++tmp->run_length; michael@0: tmp->run_set = 1; michael@0: break; michael@0: michael@0: default: michael@0: return -1; michael@0: } michael@0: } michael@0: michael@0: if (msg_complete(tmp) == -1) michael@0: return (-1); michael@0: return (0); michael@0: } michael@0: michael@0: int michael@0: msg_complete(struct msg *msg) michael@0: { michael@0: if (!msg->from_name_set) michael@0: return (-1); michael@0: if (!msg->to_name_set) michael@0: return (-1); michael@0: if (msg->attack_set && kill_complete(msg->attack_data) == -1) michael@0: return (-1); michael@0: { michael@0: int i; michael@0: for (i = 0; i < msg->run_length; ++i) { michael@0: if (msg->run_set && run_complete(msg->run_data[i]) == -1) michael@0: return (-1); michael@0: } michael@0: } michael@0: return (0); michael@0: } michael@0: michael@0: int michael@0: evtag_unmarshal_msg(struct evbuffer *evbuf, ev_uint32_t need_tag, struct msg *msg) michael@0: { michael@0: ev_uint32_t tag; michael@0: int res = -1; michael@0: michael@0: struct evbuffer *tmp = evbuffer_new(); michael@0: michael@0: if (evtag_unmarshal(evbuf, &tag, tmp) == -1 || tag != need_tag) michael@0: goto error; michael@0: michael@0: if (msg_unmarshal(msg, tmp) == -1) michael@0: goto error; michael@0: michael@0: res = 0; michael@0: michael@0: error: michael@0: evbuffer_free(tmp); michael@0: return (res); michael@0: } michael@0: michael@0: void michael@0: evtag_marshal_msg(struct evbuffer *evbuf, ev_uint32_t tag, const struct msg *msg) michael@0: { michael@0: struct evbuffer *_buf = evbuffer_new(); michael@0: assert(_buf != NULL); michael@0: msg_marshal(_buf, msg); michael@0: evtag_marshal_buffer(evbuf, tag, _buf); michael@0: evbuffer_free(_buf); michael@0: } michael@0: michael@0: /* michael@0: * Implementation of kill michael@0: */ michael@0: michael@0: static struct kill_access_ __kill_base = { michael@0: kill_weapon_assign, michael@0: kill_weapon_get, michael@0: kill_action_assign, michael@0: kill_action_get, michael@0: kill_how_often_assign, michael@0: kill_how_often_get, michael@0: kill_how_often_add, michael@0: }; michael@0: michael@0: struct kill * michael@0: kill_new(void) michael@0: { michael@0: return kill_new_with_arg(NULL); michael@0: } michael@0: michael@0: struct kill * michael@0: kill_new_with_arg(void *unused) michael@0: { michael@0: struct kill *tmp; michael@0: if ((tmp = malloc(sizeof(struct kill))) == NULL) { michael@0: event_warn("%s: malloc", __func__); michael@0: return (NULL); michael@0: } michael@0: tmp->base = &__kill_base; michael@0: michael@0: tmp->weapon_data = NULL; michael@0: tmp->weapon_set = 0; michael@0: michael@0: tmp->action_data = NULL; michael@0: tmp->action_set = 0; michael@0: michael@0: tmp->how_often_data = NULL; michael@0: tmp->how_often_length = 0; michael@0: tmp->how_often_num_allocated = 0; michael@0: tmp->how_often_set = 0; michael@0: michael@0: return (tmp); michael@0: } michael@0: michael@0: michael@0: michael@0: static int michael@0: kill_how_often_expand_to_hold_more(struct kill *msg) michael@0: { michael@0: int tobe_allocated = msg->how_often_num_allocated; michael@0: ev_uint32_t* new_data = NULL; michael@0: tobe_allocated = !tobe_allocated ? 1 : tobe_allocated << 1; michael@0: new_data = (ev_uint32_t*) realloc(msg->how_often_data, michael@0: tobe_allocated * sizeof(ev_uint32_t)); michael@0: if (new_data == NULL) michael@0: return -1; michael@0: msg->how_often_data = new_data; michael@0: msg->how_often_num_allocated = tobe_allocated; michael@0: return 0;} michael@0: michael@0: ev_uint32_t * michael@0: kill_how_often_add(struct kill *msg, const ev_uint32_t value) michael@0: { michael@0: if (++msg->how_often_length >= msg->how_often_num_allocated) { michael@0: if (kill_how_often_expand_to_hold_more(msg)<0) michael@0: goto error; michael@0: } michael@0: msg->how_often_data[msg->how_often_length - 1] = value; michael@0: msg->how_often_set = 1; michael@0: return &(msg->how_often_data[msg->how_often_length - 1]); michael@0: error: michael@0: --msg->how_often_length; michael@0: return (NULL); michael@0: } michael@0: michael@0: int michael@0: kill_weapon_assign(struct kill *msg, michael@0: const char * value) michael@0: { michael@0: if (msg->weapon_data != NULL) michael@0: free(msg->weapon_data); michael@0: if ((msg->weapon_data = strdup(value)) == NULL) michael@0: return (-1); michael@0: msg->weapon_set = 1; michael@0: return (0); michael@0: } michael@0: michael@0: int michael@0: kill_action_assign(struct kill *msg, michael@0: const char * value) michael@0: { michael@0: if (msg->action_data != NULL) michael@0: free(msg->action_data); michael@0: if ((msg->action_data = strdup(value)) == NULL) michael@0: return (-1); michael@0: msg->action_set = 1; michael@0: return (0); michael@0: } michael@0: michael@0: int michael@0: kill_how_often_assign(struct kill *msg, int off, michael@0: const ev_uint32_t value) michael@0: { michael@0: if (!msg->how_often_set || off < 0 || off >= msg->how_often_length) michael@0: return (-1); michael@0: michael@0: { michael@0: msg->how_often_data[off] = value; michael@0: } michael@0: return (0); michael@0: } michael@0: michael@0: int michael@0: kill_weapon_get(struct kill *msg, char * *value) michael@0: { michael@0: if (msg->weapon_set != 1) michael@0: return (-1); michael@0: *value = msg->weapon_data; michael@0: return (0); michael@0: } michael@0: michael@0: int michael@0: kill_action_get(struct kill *msg, char * *value) michael@0: { michael@0: if (msg->action_set != 1) michael@0: return (-1); michael@0: *value = msg->action_data; michael@0: return (0); michael@0: } michael@0: michael@0: int michael@0: kill_how_often_get(struct kill *msg, int offset, michael@0: ev_uint32_t *value) michael@0: { michael@0: if (!msg->how_often_set || offset < 0 || offset >= msg->how_often_length) michael@0: return (-1); michael@0: *value = msg->how_often_data[offset]; michael@0: return (0); michael@0: } michael@0: michael@0: void michael@0: kill_clear(struct kill *tmp) michael@0: { michael@0: if (tmp->weapon_set == 1) { michael@0: free(tmp->weapon_data); michael@0: tmp->weapon_data = NULL; michael@0: tmp->weapon_set = 0; michael@0: } michael@0: if (tmp->action_set == 1) { michael@0: free(tmp->action_data); michael@0: tmp->action_data = NULL; michael@0: tmp->action_set = 0; michael@0: } michael@0: if (tmp->how_often_set == 1) { michael@0: free(tmp->how_often_data); michael@0: tmp->how_often_data = NULL; michael@0: tmp->how_often_set = 0; michael@0: tmp->how_often_length = 0; michael@0: tmp->how_often_num_allocated = 0; michael@0: } michael@0: } michael@0: michael@0: void michael@0: kill_free(struct kill *tmp) michael@0: { michael@0: if (tmp->weapon_data != NULL) michael@0: free (tmp->weapon_data); michael@0: if (tmp->action_data != NULL) michael@0: free (tmp->action_data); michael@0: if (tmp->how_often_set == 1) { michael@0: free(tmp->how_often_data); michael@0: tmp->how_often_data = NULL; michael@0: tmp->how_often_set = 0; michael@0: tmp->how_often_length = 0; michael@0: tmp->how_often_num_allocated = 0; michael@0: } michael@0: free(tmp->how_often_data); michael@0: free(tmp); michael@0: } michael@0: michael@0: void michael@0: kill_marshal(struct evbuffer *evbuf, const struct kill *tmp){ michael@0: evtag_marshal_string(evbuf, KILL_WEAPON, tmp->weapon_data); michael@0: evtag_marshal_string(evbuf, KILL_ACTION, tmp->action_data); michael@0: if (tmp->how_often_set) { michael@0: { michael@0: int i; michael@0: for (i = 0; i < tmp->how_often_length; ++i) { michael@0: evtag_marshal_int(evbuf, KILL_HOW_OFTEN, tmp->how_often_data[i]); michael@0: } michael@0: } michael@0: } michael@0: } michael@0: michael@0: int michael@0: kill_unmarshal(struct kill *tmp, struct evbuffer *evbuf) michael@0: { michael@0: ev_uint32_t tag; michael@0: while (evbuffer_get_length(evbuf) > 0) { michael@0: if (evtag_peek(evbuf, &tag) == -1) michael@0: return (-1); michael@0: switch (tag) { michael@0: michael@0: case KILL_WEAPON: michael@0: michael@0: if (tmp->weapon_set) michael@0: return (-1); michael@0: if (evtag_unmarshal_string(evbuf, KILL_WEAPON, &tmp->weapon_data) == -1) { michael@0: event_warnx("%s: failed to unmarshal weapon", __func__); michael@0: return (-1); michael@0: } michael@0: tmp->weapon_set = 1; michael@0: break; michael@0: michael@0: case KILL_ACTION: michael@0: michael@0: if (tmp->action_set) michael@0: return (-1); michael@0: if (evtag_unmarshal_string(evbuf, KILL_ACTION, &tmp->action_data) == -1) { michael@0: event_warnx("%s: failed to unmarshal action", __func__); michael@0: return (-1); michael@0: } michael@0: tmp->action_set = 1; michael@0: break; michael@0: michael@0: case KILL_HOW_OFTEN: michael@0: michael@0: if (tmp->how_often_length >= tmp->how_often_num_allocated && michael@0: kill_how_often_expand_to_hold_more(tmp) < 0) { michael@0: puts("HEY NOW"); michael@0: return (-1); michael@0: } michael@0: if (evtag_unmarshal_int(evbuf, KILL_HOW_OFTEN, &tmp->how_often_data[tmp->how_often_length]) == -1) { michael@0: event_warnx("%s: failed to unmarshal how_often", __func__); michael@0: return (-1); michael@0: } michael@0: ++tmp->how_often_length; michael@0: tmp->how_often_set = 1; michael@0: break; michael@0: michael@0: default: michael@0: return -1; michael@0: } michael@0: } michael@0: michael@0: if (kill_complete(tmp) == -1) michael@0: return (-1); michael@0: return (0); michael@0: } michael@0: michael@0: int michael@0: kill_complete(struct kill *msg) michael@0: { michael@0: if (!msg->weapon_set) michael@0: return (-1); michael@0: if (!msg->action_set) michael@0: return (-1); michael@0: return (0); michael@0: } michael@0: michael@0: int michael@0: evtag_unmarshal_kill(struct evbuffer *evbuf, ev_uint32_t need_tag, struct kill *msg) michael@0: { michael@0: ev_uint32_t tag; michael@0: int res = -1; michael@0: michael@0: struct evbuffer *tmp = evbuffer_new(); michael@0: michael@0: if (evtag_unmarshal(evbuf, &tag, tmp) == -1 || tag != need_tag) michael@0: goto error; michael@0: michael@0: if (kill_unmarshal(msg, tmp) == -1) michael@0: goto error; michael@0: michael@0: res = 0; michael@0: michael@0: error: michael@0: evbuffer_free(tmp); michael@0: return (res); michael@0: } michael@0: michael@0: void michael@0: evtag_marshal_kill(struct evbuffer *evbuf, ev_uint32_t tag, const struct kill *msg) michael@0: { michael@0: struct evbuffer *_buf = evbuffer_new(); michael@0: assert(_buf != NULL); michael@0: kill_marshal(_buf, msg); michael@0: evtag_marshal_buffer(evbuf, tag, _buf); michael@0: evbuffer_free(_buf); michael@0: } michael@0: michael@0: /* michael@0: * Implementation of run michael@0: */ michael@0: michael@0: static struct run_access_ __run_base = { michael@0: run_how_assign, michael@0: run_how_get, michael@0: run_some_bytes_assign, michael@0: run_some_bytes_get, michael@0: run_fixed_bytes_assign, michael@0: run_fixed_bytes_get, michael@0: run_notes_assign, michael@0: run_notes_get, michael@0: run_notes_add, michael@0: run_large_number_assign, michael@0: run_large_number_get, michael@0: run_other_numbers_assign, michael@0: run_other_numbers_get, michael@0: run_other_numbers_add, michael@0: }; michael@0: michael@0: struct run * michael@0: run_new(void) michael@0: { michael@0: return run_new_with_arg(NULL); michael@0: } michael@0: michael@0: struct run * michael@0: run_new_with_arg(void *unused) michael@0: { michael@0: struct run *tmp; michael@0: if ((tmp = malloc(sizeof(struct run))) == NULL) { michael@0: event_warn("%s: malloc", __func__); michael@0: return (NULL); michael@0: } michael@0: tmp->base = &__run_base; michael@0: michael@0: tmp->how_data = NULL; michael@0: tmp->how_set = 0; michael@0: michael@0: tmp->some_bytes_data = NULL; michael@0: tmp->some_bytes_length = 0; michael@0: tmp->some_bytes_set = 0; michael@0: michael@0: memset(tmp->fixed_bytes_data, 0, sizeof(tmp->fixed_bytes_data)); michael@0: tmp->fixed_bytes_set = 0; michael@0: michael@0: tmp->notes_data = NULL; michael@0: tmp->notes_length = 0; michael@0: tmp->notes_num_allocated = 0; michael@0: tmp->notes_set = 0; michael@0: michael@0: tmp->large_number_data = 0; michael@0: tmp->large_number_set = 0; michael@0: michael@0: tmp->other_numbers_data = NULL; michael@0: tmp->other_numbers_length = 0; michael@0: tmp->other_numbers_num_allocated = 0; michael@0: tmp->other_numbers_set = 0; michael@0: michael@0: return (tmp); michael@0: } michael@0: michael@0: michael@0: michael@0: michael@0: static int michael@0: run_notes_expand_to_hold_more(struct run *msg) michael@0: { michael@0: int tobe_allocated = msg->notes_num_allocated; michael@0: char ** new_data = NULL; michael@0: tobe_allocated = !tobe_allocated ? 1 : tobe_allocated << 1; michael@0: new_data = (char **) realloc(msg->notes_data, michael@0: tobe_allocated * sizeof(char *)); michael@0: if (new_data == NULL) michael@0: return -1; michael@0: msg->notes_data = new_data; michael@0: msg->notes_num_allocated = tobe_allocated; michael@0: return 0;} michael@0: michael@0: char * * michael@0: run_notes_add(struct run *msg, const char * value) michael@0: { michael@0: if (++msg->notes_length >= msg->notes_num_allocated) { michael@0: if (run_notes_expand_to_hold_more(msg)<0) michael@0: goto error; michael@0: } michael@0: if (value != NULL) { michael@0: msg->notes_data[msg->notes_length - 1] = strdup(value); michael@0: if (msg->notes_data[msg->notes_length - 1] == NULL) { michael@0: goto error; michael@0: } michael@0: } else { michael@0: msg->notes_data[msg->notes_length - 1] = NULL; michael@0: } michael@0: msg->notes_set = 1; michael@0: return &(msg->notes_data[msg->notes_length - 1]); michael@0: error: michael@0: --msg->notes_length; michael@0: return (NULL); michael@0: } michael@0: michael@0: michael@0: static int michael@0: run_other_numbers_expand_to_hold_more(struct run *msg) michael@0: { michael@0: int tobe_allocated = msg->other_numbers_num_allocated; michael@0: ev_uint32_t* new_data = NULL; michael@0: tobe_allocated = !tobe_allocated ? 1 : tobe_allocated << 1; michael@0: new_data = (ev_uint32_t*) realloc(msg->other_numbers_data, michael@0: tobe_allocated * sizeof(ev_uint32_t)); michael@0: if (new_data == NULL) michael@0: return -1; michael@0: msg->other_numbers_data = new_data; michael@0: msg->other_numbers_num_allocated = tobe_allocated; michael@0: return 0;} michael@0: michael@0: ev_uint32_t * michael@0: run_other_numbers_add(struct run *msg, const ev_uint32_t value) michael@0: { michael@0: if (++msg->other_numbers_length >= msg->other_numbers_num_allocated) { michael@0: if (run_other_numbers_expand_to_hold_more(msg)<0) michael@0: goto error; michael@0: } michael@0: msg->other_numbers_data[msg->other_numbers_length - 1] = value; michael@0: msg->other_numbers_set = 1; michael@0: return &(msg->other_numbers_data[msg->other_numbers_length - 1]); michael@0: error: michael@0: --msg->other_numbers_length; michael@0: return (NULL); michael@0: } michael@0: michael@0: int michael@0: run_how_assign(struct run *msg, michael@0: const char * value) michael@0: { michael@0: if (msg->how_data != NULL) michael@0: free(msg->how_data); michael@0: if ((msg->how_data = strdup(value)) == NULL) michael@0: return (-1); michael@0: msg->how_set = 1; michael@0: return (0); michael@0: } michael@0: michael@0: int michael@0: run_some_bytes_assign(struct run *msg, const ev_uint8_t * value, ev_uint32_t len) michael@0: { michael@0: if (msg->some_bytes_data != NULL) michael@0: free (msg->some_bytes_data); michael@0: msg->some_bytes_data = malloc(len); michael@0: if (msg->some_bytes_data == NULL) michael@0: return (-1); michael@0: msg->some_bytes_set = 1; michael@0: msg->some_bytes_length = len; michael@0: memcpy(msg->some_bytes_data, value, len); michael@0: return (0); michael@0: } michael@0: michael@0: int michael@0: run_fixed_bytes_assign(struct run *msg, const ev_uint8_t *value) michael@0: { michael@0: msg->fixed_bytes_set = 1; michael@0: memcpy(msg->fixed_bytes_data, value, 24); michael@0: return (0); michael@0: } michael@0: michael@0: int michael@0: run_notes_assign(struct run *msg, int off, michael@0: const char * value) michael@0: { michael@0: if (!msg->notes_set || off < 0 || off >= msg->notes_length) michael@0: return (-1); michael@0: michael@0: { michael@0: if (msg->notes_data[off] != NULL) michael@0: free(msg->notes_data[off]); michael@0: msg->notes_data[off] = strdup(value); michael@0: if (msg->notes_data[off] == NULL) { michael@0: event_warnx("%s: strdup", __func__); michael@0: return (-1); michael@0: } michael@0: } michael@0: return (0); michael@0: } michael@0: michael@0: int michael@0: run_large_number_assign(struct run *msg, const ev_uint64_t value) michael@0: { michael@0: msg->large_number_set = 1; michael@0: msg->large_number_data = value; michael@0: return (0); michael@0: } michael@0: michael@0: int michael@0: run_other_numbers_assign(struct run *msg, int off, michael@0: const ev_uint32_t value) michael@0: { michael@0: if (!msg->other_numbers_set || off < 0 || off >= msg->other_numbers_length) michael@0: return (-1); michael@0: michael@0: { michael@0: msg->other_numbers_data[off] = value; michael@0: } michael@0: return (0); michael@0: } michael@0: michael@0: int michael@0: run_how_get(struct run *msg, char * *value) michael@0: { michael@0: if (msg->how_set != 1) michael@0: return (-1); michael@0: *value = msg->how_data; michael@0: return (0); michael@0: } michael@0: michael@0: int michael@0: run_some_bytes_get(struct run *msg, ev_uint8_t * *value, ev_uint32_t *plen) michael@0: { michael@0: if (msg->some_bytes_set != 1) michael@0: return (-1); michael@0: *value = msg->some_bytes_data; michael@0: *plen = msg->some_bytes_length; michael@0: return (0); michael@0: } michael@0: michael@0: int michael@0: run_fixed_bytes_get(struct run *msg, ev_uint8_t **value) michael@0: { michael@0: if (msg->fixed_bytes_set != 1) michael@0: return (-1); michael@0: *value = msg->fixed_bytes_data; michael@0: return (0); michael@0: } michael@0: michael@0: int michael@0: run_notes_get(struct run *msg, int offset, michael@0: char * *value) michael@0: { michael@0: if (!msg->notes_set || offset < 0 || offset >= msg->notes_length) michael@0: return (-1); michael@0: *value = msg->notes_data[offset]; michael@0: return (0); michael@0: } michael@0: michael@0: int michael@0: run_large_number_get(struct run *msg, ev_uint64_t *value) michael@0: { michael@0: if (msg->large_number_set != 1) michael@0: return (-1); michael@0: *value = msg->large_number_data; michael@0: return (0); michael@0: } michael@0: michael@0: int michael@0: run_other_numbers_get(struct run *msg, int offset, michael@0: ev_uint32_t *value) michael@0: { michael@0: if (!msg->other_numbers_set || offset < 0 || offset >= msg->other_numbers_length) michael@0: return (-1); michael@0: *value = msg->other_numbers_data[offset]; michael@0: return (0); michael@0: } michael@0: michael@0: void michael@0: run_clear(struct run *tmp) michael@0: { michael@0: if (tmp->how_set == 1) { michael@0: free(tmp->how_data); michael@0: tmp->how_data = NULL; michael@0: tmp->how_set = 0; michael@0: } michael@0: if (tmp->some_bytes_set == 1) { michael@0: free (tmp->some_bytes_data); michael@0: tmp->some_bytes_data = NULL; michael@0: tmp->some_bytes_length = 0; michael@0: tmp->some_bytes_set = 0; michael@0: } michael@0: tmp->fixed_bytes_set = 0; michael@0: memset(tmp->fixed_bytes_data, 0, sizeof(tmp->fixed_bytes_data)); michael@0: if (tmp->notes_set == 1) { michael@0: int i; michael@0: for (i = 0; i < tmp->notes_length; ++i) { michael@0: if (tmp->notes_data[i] != NULL) free(tmp->notes_data[i]); michael@0: } michael@0: free(tmp->notes_data); michael@0: tmp->notes_data = NULL; michael@0: tmp->notes_set = 0; michael@0: tmp->notes_length = 0; michael@0: tmp->notes_num_allocated = 0; michael@0: } michael@0: tmp->large_number_set = 0; michael@0: if (tmp->other_numbers_set == 1) { michael@0: free(tmp->other_numbers_data); michael@0: tmp->other_numbers_data = NULL; michael@0: tmp->other_numbers_set = 0; michael@0: tmp->other_numbers_length = 0; michael@0: tmp->other_numbers_num_allocated = 0; michael@0: } michael@0: } michael@0: michael@0: void michael@0: run_free(struct run *tmp) michael@0: { michael@0: if (tmp->how_data != NULL) michael@0: free (tmp->how_data); michael@0: if (tmp->some_bytes_data != NULL) michael@0: free(tmp->some_bytes_data); michael@0: if (tmp->notes_set == 1) { michael@0: int i; michael@0: for (i = 0; i < tmp->notes_length; ++i) { michael@0: if (tmp->notes_data[i] != NULL) free(tmp->notes_data[i]); michael@0: } michael@0: free(tmp->notes_data); michael@0: tmp->notes_data = NULL; michael@0: tmp->notes_set = 0; michael@0: tmp->notes_length = 0; michael@0: tmp->notes_num_allocated = 0; michael@0: } michael@0: free(tmp->notes_data); michael@0: if (tmp->other_numbers_set == 1) { michael@0: free(tmp->other_numbers_data); michael@0: tmp->other_numbers_data = NULL; michael@0: tmp->other_numbers_set = 0; michael@0: tmp->other_numbers_length = 0; michael@0: tmp->other_numbers_num_allocated = 0; michael@0: } michael@0: free(tmp->other_numbers_data); michael@0: free(tmp); michael@0: } michael@0: michael@0: void michael@0: run_marshal(struct evbuffer *evbuf, const struct run *tmp){ michael@0: evtag_marshal_string(evbuf, RUN_HOW, tmp->how_data); michael@0: if (tmp->some_bytes_set) { michael@0: evtag_marshal(evbuf, RUN_SOME_BYTES, tmp->some_bytes_data, tmp->some_bytes_length); michael@0: } michael@0: evtag_marshal(evbuf, RUN_FIXED_BYTES, tmp->fixed_bytes_data, (24)); michael@0: if (tmp->notes_set) { michael@0: { michael@0: int i; michael@0: for (i = 0; i < tmp->notes_length; ++i) { michael@0: evtag_marshal_string(evbuf, RUN_NOTES, tmp->notes_data[i]); michael@0: } michael@0: } michael@0: } michael@0: if (tmp->large_number_set) { michael@0: evtag_marshal_int64(evbuf, RUN_LARGE_NUMBER, tmp->large_number_data); michael@0: } michael@0: if (tmp->other_numbers_set) { michael@0: { michael@0: int i; michael@0: for (i = 0; i < tmp->other_numbers_length; ++i) { michael@0: evtag_marshal_int(evbuf, RUN_OTHER_NUMBERS, tmp->other_numbers_data[i]); michael@0: } michael@0: } michael@0: } michael@0: } michael@0: michael@0: int michael@0: run_unmarshal(struct run *tmp, struct evbuffer *evbuf) michael@0: { michael@0: ev_uint32_t tag; michael@0: while (evbuffer_get_length(evbuf) > 0) { michael@0: if (evtag_peek(evbuf, &tag) == -1) michael@0: return (-1); michael@0: switch (tag) { michael@0: michael@0: case RUN_HOW: michael@0: michael@0: if (tmp->how_set) michael@0: return (-1); michael@0: if (evtag_unmarshal_string(evbuf, RUN_HOW, &tmp->how_data) == -1) { michael@0: event_warnx("%s: failed to unmarshal how", __func__); michael@0: return (-1); michael@0: } michael@0: tmp->how_set = 1; michael@0: break; michael@0: michael@0: case RUN_SOME_BYTES: michael@0: michael@0: if (tmp->some_bytes_set) michael@0: return (-1); michael@0: if (evtag_payload_length(evbuf, &tmp->some_bytes_length) == -1) michael@0: return (-1); michael@0: if (tmp->some_bytes_length > evbuffer_get_length(evbuf)) michael@0: return (-1); michael@0: if ((tmp->some_bytes_data = malloc(tmp->some_bytes_length)) == NULL) michael@0: return (-1); michael@0: if (evtag_unmarshal_fixed(evbuf, RUN_SOME_BYTES, tmp->some_bytes_data, tmp->some_bytes_length) == -1) { michael@0: event_warnx("%s: failed to unmarshal some_bytes", __func__); michael@0: return (-1); michael@0: } michael@0: tmp->some_bytes_set = 1; michael@0: break; michael@0: michael@0: case RUN_FIXED_BYTES: michael@0: michael@0: if (tmp->fixed_bytes_set) michael@0: return (-1); michael@0: if (evtag_unmarshal_fixed(evbuf, RUN_FIXED_BYTES, tmp->fixed_bytes_data, (24)) == -1) { michael@0: event_warnx("%s: failed to unmarshal fixed_bytes", __func__); michael@0: return (-1); michael@0: } michael@0: tmp->fixed_bytes_set = 1; michael@0: break; michael@0: michael@0: case RUN_NOTES: michael@0: michael@0: if (tmp->notes_length >= tmp->notes_num_allocated && michael@0: run_notes_expand_to_hold_more(tmp) < 0) { michael@0: puts("HEY NOW"); michael@0: return (-1); michael@0: } michael@0: if (evtag_unmarshal_string(evbuf, RUN_NOTES, &tmp->notes_data[tmp->notes_length]) == -1) { michael@0: event_warnx("%s: failed to unmarshal notes", __func__); michael@0: return (-1); michael@0: } michael@0: ++tmp->notes_length; michael@0: tmp->notes_set = 1; michael@0: break; michael@0: michael@0: case RUN_LARGE_NUMBER: michael@0: michael@0: if (tmp->large_number_set) michael@0: return (-1); michael@0: if (evtag_unmarshal_int64(evbuf, RUN_LARGE_NUMBER, &tmp->large_number_data) == -1) { michael@0: event_warnx("%s: failed to unmarshal large_number", __func__); michael@0: return (-1); michael@0: } michael@0: tmp->large_number_set = 1; michael@0: break; michael@0: michael@0: case RUN_OTHER_NUMBERS: michael@0: michael@0: if (tmp->other_numbers_length >= tmp->other_numbers_num_allocated && michael@0: run_other_numbers_expand_to_hold_more(tmp) < 0) { michael@0: puts("HEY NOW"); michael@0: return (-1); michael@0: } michael@0: if (evtag_unmarshal_int(evbuf, RUN_OTHER_NUMBERS, &tmp->other_numbers_data[tmp->other_numbers_length]) == -1) { michael@0: event_warnx("%s: failed to unmarshal other_numbers", __func__); michael@0: return (-1); michael@0: } michael@0: ++tmp->other_numbers_length; michael@0: tmp->other_numbers_set = 1; michael@0: break; michael@0: michael@0: default: michael@0: return -1; michael@0: } michael@0: } michael@0: michael@0: if (run_complete(tmp) == -1) michael@0: return (-1); michael@0: return (0); michael@0: } michael@0: michael@0: int michael@0: run_complete(struct run *msg) michael@0: { michael@0: if (!msg->how_set) michael@0: return (-1); michael@0: if (!msg->fixed_bytes_set) michael@0: return (-1); michael@0: return (0); michael@0: } michael@0: michael@0: int michael@0: evtag_unmarshal_run(struct evbuffer *evbuf, ev_uint32_t need_tag, struct run *msg) michael@0: { michael@0: ev_uint32_t tag; michael@0: int res = -1; michael@0: michael@0: struct evbuffer *tmp = evbuffer_new(); michael@0: michael@0: if (evtag_unmarshal(evbuf, &tag, tmp) == -1 || tag != need_tag) michael@0: goto error; michael@0: michael@0: if (run_unmarshal(msg, tmp) == -1) michael@0: goto error; michael@0: michael@0: res = 0; michael@0: michael@0: error: michael@0: evbuffer_free(tmp); michael@0: return (res); michael@0: } michael@0: michael@0: void michael@0: evtag_marshal_run(struct evbuffer *evbuf, ev_uint32_t tag, const struct run *msg) michael@0: { michael@0: struct evbuffer *_buf = evbuffer_new(); michael@0: assert(_buf != NULL); michael@0: run_marshal(_buf, msg); michael@0: evtag_marshal_buffer(evbuf, tag, _buf); michael@0: evbuffer_free(_buf); michael@0: } michael@0: