-void trp_route_req_set_realm(TRP_ROUTE_REQ *req, TR_NAME *realm)
-{
- req->realm=realm;
-}
-
-/* TODO: clean up return codes.
- * TODO: should take a body, not a msg */
-static TRP_RC trp_parse_route_req(TRP_MSG *msg, json_t *jbody)
-{
- TALLOC_CTX *tmp_ctx=talloc_new(NULL);
- TRP_ROUTE_REQ *msg_body=NULL;
- char *s=NULL;
- TRP_RC rc=TRP_ERROR;
-
- /* check message type and body type for agreement */
- if (msg->type != TRP_MSG_TYPE_ROUTE_REQ) {
- rc=TRP_BADTYPE;
- goto cleanup;
- }
- msg_body=talloc_get_type(msg->body, TRP_ROUTE_REQ);
- if (msg_body==NULL) {
- rc=TRP_BADTYPE;
- goto cleanup;
- }
-
- rc=trp_get_json_string(jbody, "community", &s, tmp_ctx);
- if (rc!=TRP_SUCCESS)
- goto cleanup;
- msg_body->comm=tr_new_name(s);
- talloc_free(s); s=NULL;
-
- rc=trp_get_json_string(jbody, "realm", &s, tmp_ctx);
- if (rc!=TRP_SUCCESS)
- goto cleanup;
- msg_body->realm=tr_new_name(s);
- talloc_free(s); s=NULL;
-
- rc=TRP_SUCCESS;
-cleanup:
- talloc_free(tmp_ctx);
- return rc;
-}
-
-static json_t *trp_encode_body(TRP_MSG_TYPE type, void *body)
-{
- struct trp_msg_type_entry *msgtype=get_trp_msg_type_entry(type);
-
- if ((msgtype->type==TRP_MSG_TYPE_UNKNOWN) || (msgtype->encode==NULL))
- return NULL;
-
- return msgtype->encode(body);
-}
-
-char *trp_encode_msg(TRP_MSG *msg)
-{
- json_t *jmsg=NULL;
- json_t *jtype=NULL;
- json_t *jbody=NULL;
- char *encoded=NULL;
-
- jbody=trp_encode_body(msg->type, msg->body);
- if (jbody!=NULL) {
- jmsg=json_object();
- if (jmsg==NULL) {
- /* failed */
- json_decref(jbody);
- } else {
- jtype=json_string(trp_msg_type_to_string(msg->type));
- if (jtype==NULL) {
- json_decref(jbody);
- } else {
- json_object_set_new(jmsg, "message_type", jtype);
- json_object_set_new(jmsg, "body", jbody);
- encoded=json_dumps(jmsg, 0);
- }
- json_decref(jmsg);
- }
- }
- return encoded;
-}
-
-/* add record data to jrec */
-static TRP_RC trp_encode_info_route(json_t *jrec, void *route_in)
-{
- TRP_MSG_INFO_ROUTE *route=talloc_get_type(route_in, TRP_MSG_INFO_ROUTE);
- json_t *jstr=NULL;
- json_t *jint=NULL;
- char *s=NULL;
-
- if (route==NULL)
- return TRP_BADTYPE;
-
- s=tr_name_strdup(route->comm);
- if (s==NULL)
- return TRP_NOMEM;
- jstr=json_string(s);
- free(s);s=NULL;
- if(jstr==NULL)
- return TRP_ERROR;
- json_object_set_new(jrec, "community", jstr);
-
- s=tr_name_strdup(route->realm);
- if (s==NULL)
- return TRP_NOMEM;
- jstr=json_string(s);
- free(s);s=NULL;
- if(jstr==NULL)
- return TRP_ERROR;
- json_object_set_new(jrec, "realm", jstr);
-
- s=tr_name_strdup(route->trust_router);
- if (s==NULL)
- return TRP_NOMEM;
- jstr=json_string(s);
- free(s);s=NULL;
- if(jstr==NULL)
- return TRP_ERROR;
- json_object_set_new(jrec, "trust_router", jstr);
-
- jint=json_integer(route->metric);
- if(jint==NULL)
- return TRP_ERROR;
- json_object_set_new(jrec, "metric", jint);
-
- jint=json_integer(route->interval);
- if(jint==NULL)
- return TRP_ERROR;
- json_object_set_new(jrec, "interval", jint);
-
- return TRP_SUCCESS;
-}
-
-static json_t *trp_encode_info_rec(TRP_MSG_INFO_REC *rec)
-{
- struct trp_msg_info_type_entry *rectype=get_trp_msg_info_type_entry(rec->type);
- json_t *jrec=NULL;
- json_t *jstr=NULL;
-
- if (rectype->type==TRP_MSG_INFO_TYPE_UNKNOWN)
- return NULL;
-
- jrec=json_object();
- if (jrec==NULL)
- return NULL;
-
- jstr=json_string(trp_msg_info_type_to_string(rec->type));
- if (jstr==NULL) {
- json_decref(jrec);
- return NULL;
- }
- json_object_set_new(jrec, "record_type", jstr);
-
- if ((rectype->encode==NULL) || (TRP_SUCCESS!=rectype->encode(jrec, rec->data))) {
- json_decref(jrec);
- return NULL;
- }
-
- return jrec;
-}
-
-static json_t *trp_encode_route_update(void *update_in)
-{
- TRP_ROUTE_UPDATE *update=talloc_get_type(update_in, TRP_ROUTE_UPDATE); /* null if wrong type */
- json_t *jbody=NULL;
- json_t *jrecords=NULL;
- json_t *jrec=NULL;
- TRP_MSG_INFO_REC *rec;
-
- if (update==NULL)
- return NULL;
-
- jbody=json_object();
- if (jbody==NULL)
- return NULL;
-
- jrecords=json_array();
- if (jrecords==NULL) {
- json_decref(jbody);
- return NULL;
- }
- json_object_set_new(jbody, "records", jrecords); /* jrecords now a "borrowed" reference */
- for (rec=update->records; rec!=NULL; rec=rec->next) {
- jrec=trp_encode_info_rec(rec);
- if (jrec==NULL) {
- json_decref(jbody); /* also decs jrecords and any elements */
- return NULL;
- }
- if (0!=json_array_append_new(jrecords, jrec)) {
- json_decref(jbody); /* also decs jrecords and any elements */
- json_decref(jrec); /* this one did not get added so dec explicitly */
- return NULL;
- }
- }
-
- return jbody;
-}
-
-static json_t *trp_encode_route_req(void *req_in)
-{
- TRP_ROUTE_REQ *req=talloc_get_type(req_in, TRP_ROUTE_REQ); /* null if wrong type */
- json_t *jbody=NULL;
- json_t *jstr=NULL;
- char *s=NULL;
-
- if (req==NULL)
- return NULL;
-
- jbody=json_object();
- if (jbody==NULL)
- return NULL;
-
- s=tr_name_strdup(req->comm); /* ensures null termination */
- if (s==NULL) {
- json_decref(jbody);
- return NULL;
- }
- jstr=json_string(s);
- free(s); s=NULL;
- if (jstr==NULL) {
- json_decref(jbody);
- return NULL;
- }
- json_object_set_new(jbody, "community", jstr);
-
- s=tr_name_strdup(req->realm); /* ensures null termination */
- if (s==NULL) {
- json_decref(jbody);
- return NULL;
- }
- jstr=json_string(s);
- free(s); s=NULL;
- if (jstr==NULL) {
- json_decref(jbody);
- return NULL;
- }
- json_object_set_new(jbody, "realm", jstr);
-
- return jbody;
-}
-
-static void trp_msg_info_rec_print(TRP_MSG_INFO_REC *rec)
-{
- struct trp_msg_info_type_entry *rectype=get_trp_msg_info_type_entry(rec->type);
-
- if (rectype->print != NULL) {
- printf(" [record_type=%s\n", trp_msg_info_type_to_string(rec->type));
- rectype->print(rec->data);
- }
- else
- printf(" [record_type=%s, (no print method)]\n");
-}
-
-static void trp_route_update_print(void *body_in)
-{
- TRP_ROUTE_UPDATE *body=talloc_get_type(body_in, TRP_ROUTE_UPDATE); /* null if wrong type */
- TRP_MSG_INFO_REC *rec=NULL;
-
- if (body!=NULL) {
- printf(" {records=\n");
- for (rec=body->records; rec!=NULL; rec=rec->next)
- trp_msg_info_rec_print(rec);
- printf(" }\n");
- }
-}
-
-static void trp_route_req_print(void *body_in)
-{
- TRP_ROUTE_REQ *body=talloc_get_type(body_in, TRP_ROUTE_REQ); /* null if wrong type */
-
- if (body!=NULL) {
- printf(" {community=%.*s\n realm=%.*s}\n",
- body->comm->len, body->comm->buf,
- body->realm->len, body->realm->buf);
- }
-}
-
-static void trp_msg_body_print(void *body, TRP_MSG_TYPE msgtype)
-{
- struct trp_msg_type_entry *info=get_trp_msg_type_entry(msgtype);
- info->print(body);
-}
-
-void trp_msg_print(TRP_MSG *msg)
-{
- /* right now just assumes update */
- printf("{message_type=%s\n", trp_msg_type_to_string(msg->type));
- trp_msg_body_print(msg->body, msg->type);
- printf("}\n");
-}
-
-/* returns a pointer to one of the message body types, or NULL on error/unknown type */
-static void *trp_msg_body_new(TALLOC_CTX *mem_ctx, TRP_MSG_TYPE msgtype)
-{
- void *new_body=NULL;
- struct trp_msg_type_entry *info=get_trp_msg_type_entry(msgtype);
-
- if (info->type==TRP_MSG_TYPE_UNKNOWN) {
- tr_debug("trp_msg_body_new: Unknown type %d.", info->type);
- return NULL;
- }
-
- new_body=info->allocate(mem_ctx);
- msg_body_type_check(msgtype, new_body); /* aborts program on type violation */
- return new_body;
-}
-
-/* call the correct parser */
-static TRP_RC trp_parse_msg_body(TRP_MSG *msg, json_t *jbody)
-{
- struct trp_msg_type_entry *info=get_trp_msg_type_entry(msg->type);
-
- if (info->type==TRP_MSG_TYPE_UNKNOWN) {
- tr_debug("trp_msg_body_parse: Unknown type %d.", info->type);
- return TRP_ERROR;
- }
-
- return info->parse(msg, jbody);
-}
-
-
-TRP_RC trp_parse_msg(TALLOC_CTX *mem_ctx, const char *buf, size_t buflen, TRP_MSG **msg)
-{
- TALLOC_CTX *tmp_ctx=talloc_new(NULL);
- TRP_MSG *new_msg=NULL;
- TRP_RC msg_rc=TRP_ERROR;
- json_error_t json_err;
- json_t *jmsg=NULL; /* handle for the whole msg */
- json_t *jbody=NULL;
- char *s;
-
- tr_debug("trp_parse_msg: parsing %d bytes", buflen);
-
- jmsg=json_loadb(buf, buflen, 0, &json_err);
- if (jmsg == NULL) {
- tr_debug("trp_parse_msg: Error parsing message.");
- msg_rc=TRP_NOPARSE;
- goto cleanup;
- }
-
- /* parse the common part of the message */
- new_msg=trp_msg_new(tmp_ctx);
- if (new_msg == NULL) {
- tr_debug("trp_parse_msg: Error allocating message.");
- msg_rc=TRP_NOMEM;
- goto cleanup;
- }
-
- switch (trp_get_json_string(jmsg, "message_type", &s, new_msg)) {
- case TRP_SUCCESS:
- break;
- case TRP_NOPARSE:
- tr_debug("trp_parse_msg: required attribute 'message_type' not present.");
- msg_rc=TRP_NOPARSE;
- goto cleanup;
- case TRP_BADTYPE:
- tr_debug("trp_parse_msg: required attribute 'message_type' is not a string.");
- msg_rc=TRP_NOPARSE;
- goto cleanup;
- default:
- tr_debug("trp_parse_msg: error parsing 'message_type'.");
- msg_rc=TRP_NOPARSE;
- goto cleanup;
- }
-
- tr_debug("trp_parse_msg: 'message_type' is '%s'", s);
- new_msg->type = trp_msg_type_from_string(s);
- if (new_msg->type==TRP_MSG_TYPE_UNKNOWN) {
- tr_debug("trp_parse_msg: Parsing error, unknown message_type (%s).", s);
- msg_rc=TRP_NOPARSE;
- goto cleanup;
- }
-
- new_msg->body=trp_msg_body_new(new_msg, new_msg->type);
- if (new_msg->body==NULL) {
- tr_debug("trp_parse_msg: Error allocating message body for message_type %d.", new_msg->type);
- msg_rc=TRP_NOMEM;
- goto cleanup;
- }
- jbody=json_object_get(jmsg, "body");
- if (jbody==NULL) {
- tr_debug("trp_parse_msg: Message body not found.");
- msg_rc=TRP_NOPARSE;
- goto cleanup;
- }
-
- switch (trp_parse_msg_body(new_msg, jbody)) {
- case TRP_SUCCESS:
- break;
- default:
- tr_debug("trp_parse_msg: Error parsing message body.");
- goto cleanup;
- }
-
- /* success! */
- (*msg)=new_msg;
- new_msg=NULL;
- talloc_steal(mem_ctx, *msg);
- msg_rc=TRP_SUCCESS;
-
-cleanup:
- talloc_free(tmp_ctx);
- json_decref(jmsg);
- return msg_rc;
-}