Fix community table sweep / removal. Trust router now stable.
[trust_router.git] / common / tr_msg.c
index 9591cd4..ac57ebb 100644 (file)
@@ -41,6 +41,8 @@
 #include <talloc.h>
 
 
+#include <tr_apc.h>
+#include <tr_comm.h>
 #include <tr_msg.h>
 #include <trust_router/tr_name.h>
 #include <trp_internal.h>
@@ -68,23 +70,23 @@ static int tr_msg_get_json_integer(json_t *jmsg, const char *attr, int *dest)
 
 /* Read attribute attr from msg as a string. Copies string into mem_ctx context so jmsg can
  * be destroyed safely. Returns nonzero on error. */
-static int tr_msg_get_json_string(json_t *jmsg, const char *attr, char **dest, TALLOC_CTX *mem_ctx)
+static TRP_RC tr_msg_get_json_string(json_t *jmsg, const char *attr, char **dest, TALLOC_CTX *mem_ctx)
 {
   json_t *obj;
 
   obj=json_object_get(jmsg, attr);
   if (obj == NULL)
-    return -1;
+    return TRP_ERROR;
 
   /* check type */
   if (!json_is_string(obj))
-    return -1;
+    return TRP_ERROR;
 
   *dest=talloc_strdup(mem_ctx, json_string_value(obj));
   if (*dest==NULL)
-    return -1;
+    return TRP_ERROR;
 
-  return 0;
+  return TRP_SUCCESS;
 }
 
 enum msg_type tr_msg_get_msg_type(TR_MSG *msg) 
@@ -133,6 +135,7 @@ TRP_UPD *tr_msg_get_trp_upd(TR_MSG *msg)
 void tr_msg_set_trp_upd(TR_MSG *msg, TRP_UPD *update)
 {
   msg->msg_rep=update;
+  talloc_steal(NULL, update); /* should attach to msg, but TR_MSG not usually talloc'ed */
   msg->msg_type=TRP_UPDATE;
 }
 
@@ -534,7 +537,7 @@ static TID_RESP *tr_msg_decode_tidresp(json_t *jresp)
 
 /* Information records for TRP update msg 
  * requires that jrec already be allocated */
-static TRP_RC tr_msg_encode_inforec_route(json_t *jrec, TRP_INFOREC *rec )
+static TRP_RC tr_msg_encode_inforec_route(json_t *jrec, TRP_INFOREC *rec)
 {
   json_t *jstr=NULL;
   json_t *jint=NULL;
@@ -543,43 +546,155 @@ static TRP_RC tr_msg_encode_inforec_route(json_t *jrec, TRP_INFOREC *rec )
   if (rec==NULL)
     return TRP_BADTYPE;
 
-  if ((trp_inforec_get_comm(rec)==NULL)
-     || (trp_inforec_get_realm(rec)==NULL)
-     || (trp_inforec_get_trust_router(rec)==NULL)) {
+  if (trp_inforec_get_trust_router(rec)==NULL)
     return TRP_ERROR;
-  }
 
-  s=tr_name_strdup(trp_inforec_get_comm(rec));
+  s=tr_name_strdup(trp_inforec_get_trust_router(rec));
   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);
+  json_object_set_new(jrec, "trust_router", jstr);
 
-  s=tr_name_strdup(trp_inforec_get_realm(rec));
-  if (s==NULL)
-    return TRP_NOMEM;
-  jstr=json_string(s);
-  free(s);s=NULL;
+  jint=json_integer(trp_inforec_get_metric(rec));
+  if(jint==NULL)
+    return TRP_ERROR;
+  json_object_set_new(jrec, "metric", jint);
+
+  jint=json_integer(trp_inforec_get_interval(rec));
+  if(jint==NULL)
+    return TRP_ERROR;
+  json_object_set_new(jrec, "interval", jint);
+
+  return TRP_SUCCESS;
+}
+
+/* returns a json array */
+static json_t *tr_msg_encode_apcs(TR_APC *apcs)
+{
+  TALLOC_CTX *tmp_ctx=talloc_new(NULL);
+  TR_APC_ITER *iter=tr_apc_iter_new(tmp_ctx);
+  TR_APC *apc=NULL;
+  json_t *jarray=NULL;
+  json_t *jid=NULL;
+
+  if (iter==NULL)
+    goto cleanup;
+
+  jarray=json_array();
+  if (jarray==NULL)
+    goto cleanup;
+
+  for (apc=tr_apc_iter_first(iter, apcs); apc!=NULL; apc=tr_apc_iter_next(iter)) {
+    jid=tr_name_to_json_string(tr_apc_get_id(apc));
+    if ((jid==NULL) || (json_array_append_new(jarray, jid)!=0)) {
+      json_decref(jarray);
+      jarray=NULL;
+      goto cleanup;
+    }
+  }
+  
+cleanup:
+  talloc_free(tmp_ctx);
+  return jarray;
+}
+
+static TR_APC *tr_msg_decode_apcs(TALLOC_CTX *mem_ctx, json_t *jarray, TRP_RC *rc)
+{
+  TALLOC_CTX *tmp_ctx=talloc_new(NULL);
+  size_t ii=0;
+  TR_APC *apc_list=NULL;
+  TR_APC *new=NULL;
+  json_t *jstr=NULL;
+
+  *rc=TRP_ERROR;
+
+  for (ii=0; ii<json_array_size(jarray); ii++) {
+    jstr=json_array_get(jarray, ii);
+    new=tr_apc_new(tmp_ctx);
+    if ((jstr==NULL) || (new==NULL) || (!json_is_string(jstr))) {
+      apc_list=NULL; /* these are all in tmp_ctx, so they'll still get cleaned up */
+      goto cleanup;
+    }
+
+    tr_apc_set_id(new, tr_new_name(json_string_value(jstr)));
+    if (tr_apc_get_id(new)==NULL) {
+      apc_list=NULL; /* these are all in tmp_ctx, so they'll still get cleaned up */
+      goto cleanup;
+    }
+
+    tr_apc_add(apc_list, new);
+  }
+
+  *rc=TRP_SUCCESS;
+
+  if (apc_list!=NULL)
+    talloc_steal(mem_ctx, apc_list);
+
+cleanup:
+  talloc_free(tmp_ctx);
+  return apc_list;
+}
+
+static TRP_RC tr_msg_encode_inforec_comm(json_t *jrec, TRP_INFOREC *rec)
+{
+  json_t *jstr=NULL;
+  json_t *jint=NULL;
+  json_t *japcs=NULL;
+  const char *sconst=NULL;
+  TR_COMM_TYPE commtype=TR_COMM_UNKNOWN;
+
+  if (rec==NULL)
+    return TRP_BADTYPE;
+
+  commtype=trp_inforec_get_comm_type(rec);
+  if (commtype==TR_COMM_UNKNOWN) {
+    tr_notice("tr_msg_encode_inforec_comm: unknown community type.");
+    return TRP_ERROR;
+  }
+  sconst=tr_comm_type_to_str(commtype);
+  if (sconst==NULL)
+    return TRP_ERROR;
+  jstr=json_string(sconst);
   if(jstr==NULL)
     return TRP_ERROR;
-  json_object_set_new(jrec, "realm", jstr);
+  json_object_set_new(jrec, "type", jstr);
 
-  s=tr_name_strdup(trp_inforec_get_trust_router(rec));
-  if (s==NULL)
-    return TRP_NOMEM;
-  jstr=json_string(s);
-  free(s);s=NULL;
+  sconst=tr_realm_role_to_str(trp_inforec_get_role(rec));
+  if (sconst==NULL) {
+    tr_notice("tr_msg_encode_inforec_comm: unknown realm role.");
+    return TRP_ERROR;
+  }
+  jstr=json_string(sconst);
   if(jstr==NULL)
     return TRP_ERROR;
-  json_object_set_new(jrec, "trust_router", jstr);
+  json_object_set_new(jrec, "role", jstr);
 
-  jint=json_integer(trp_inforec_get_metric(rec));
-  if(jint==NULL)
+  japcs=tr_msg_encode_apcs(trp_inforec_get_apcs(rec));
+  if (japcs==NULL) {
+    tr_notice("tr_msg_encode_inforec_comm: error encoding APCs.");
     return TRP_ERROR;
-  json_object_set_new(jrec, "metric", jint);
+  }
+  json_object_set_new(jrec, "apcs", japcs);
+  
+
+  if (trp_inforec_get_owner_realm(rec)!=NULL) {
+    jstr=tr_name_to_json_string(trp_inforec_get_owner_realm(rec));
+    if(jstr==NULL)
+      return TRP_ERROR;
+    json_object_set_new(jrec, "owner_realm", jstr);
+  }  
+
+  if (trp_inforec_get_owner_contact(rec)!=NULL) {
+    jstr=tr_name_to_json_string(trp_inforec_get_owner_contact(rec));
+    if(jstr==NULL)
+      return TRP_ERROR;
+    json_object_set_new(jrec, "owner_contact", jstr);
+  }  
+
+  json_object_set(jrec, "provenance", trp_inforec_get_provenance(rec));
 
   jint=json_integer(trp_inforec_get_interval(rec));
   if(jint==NULL)
@@ -615,6 +730,12 @@ static json_t *tr_msg_encode_inforec(TRP_INFOREC *rec)
       return NULL;
     }
     break;
+  case TRP_INFOREC_TYPE_COMMUNITY:
+    if (TRP_SUCCESS!=tr_msg_encode_inforec_comm(jrec, rec)) {
+      json_decref(jrec);
+      return NULL;
+    }
+    break;
   default:
     json_decref(jrec);
     return NULL;
@@ -622,66 +743,141 @@ static json_t *tr_msg_encode_inforec(TRP_INFOREC *rec)
   return jrec;
 }
 
-/* decode a single record */
-static TRP_INFOREC *tr_msg_decode_trp_inforec(TALLOC_CTX *mem_ctx, json_t *jrecord)
+static TRP_RC tr_msg_decode_trp_inforec_route(json_t *jrecord, TRP_INFOREC *rec)
 {
   TALLOC_CTX *tmp_ctx=talloc_new(NULL);
-  TRP_INFOREC_TYPE rectype;
-  TRP_INFOREC *rec=NULL;
   TRP_RC rc=TRP_ERROR;
   char *s=NULL;
   int num=0;
-  
-  if (0!=tr_msg_get_json_string(jrecord, "record_type", &s, tmp_ctx))
-    goto cleanup;
 
-  rectype=trp_inforec_type_from_string(s);
+  rc=tr_msg_get_json_string(jrecord, "trust_router", &s, tmp_ctx);
+  if (rc != TRP_SUCCESS)
+    goto cleanup;
+  if (TRP_SUCCESS!=trp_inforec_set_trust_router(rec, tr_new_name(s))) {
+    rc=TRP_ERROR;
+    goto cleanup;
+  }
   talloc_free(s); s=NULL;
 
-  rec=trp_inforec_new(tmp_ctx, rectype);
-  if (rec==NULL) {
-    rc=TRP_NOMEM;
+  trp_inforec_set_next_hop(rec, NULL); /* make sure this is null (filled in later) */
+
+  rc=tr_msg_get_json_integer(jrecord, "metric", &num);
+  if ((rc != TRP_SUCCESS) || (TRP_SUCCESS!=trp_inforec_set_metric(rec,num)))
     goto cleanup;
-  }
 
-  /* We only support route_info records for now*/
-  if (trp_inforec_get_type(rec)!=TRP_INFOREC_TYPE_ROUTE) {
-    rc=TRP_UNSUPPORTED;
+  rc=tr_msg_get_json_integer(jrecord, "interval", &num);
+  if ((rc != TRP_SUCCESS) || (TRP_SUCCESS!=trp_inforec_set_interval(rec,num)))
     goto cleanup;
-  }
 
-  tr_debug("tr_msg_decode_trp_inforec: '%s' record found.", trp_inforec_type_to_string(rec->type));
+cleanup:
+  talloc_free(tmp_ctx);
+  return rc;
+}
+
+static TRP_RC tr_msg_decode_trp_inforec_comm(json_t *jrecord, TRP_INFOREC *rec)
+{
+  TALLOC_CTX *tmp_ctx=talloc_new(NULL);
+  TRP_RC rc=TRP_ERROR;
+  char *s=NULL;
+  int num=0;
+  TR_APC *apcs=NULL;
 
-  rc=tr_msg_get_json_string(jrecord, "community", &s, tmp_ctx);
+  rc=tr_msg_get_json_string(jrecord, "type", &s, tmp_ctx);
   if (rc != TRP_SUCCESS)
     goto cleanup;
-  if (TRP_SUCCESS!=trp_inforec_set_comm(rec, tr_new_name(s)))
+  if (TRP_SUCCESS!=trp_inforec_set_comm_type(rec, tr_comm_type_from_str(s))) {
+    rc=TRP_ERROR;
     goto cleanup;
+  }
   talloc_free(s); s=NULL;
 
-  rc=tr_msg_get_json_string(jrecord, "realm", &s, tmp_ctx);
+  rc=tr_msg_get_json_string(jrecord, "role", &s, tmp_ctx);
   if (rc != TRP_SUCCESS)
     goto cleanup;
-  if (TRP_SUCCESS!=trp_inforec_set_realm(rec, tr_new_name(s)))
+  if (TRP_SUCCESS!=trp_inforec_set_role(rec, tr_realm_role_from_str(s))) {
+    rc=TRP_ERROR;
     goto cleanup;
+  }
   talloc_free(s); s=NULL;
 
-  rc=tr_msg_get_json_string(jrecord, "trust_router", &s, tmp_ctx);
-  if (rc != TRP_SUCCESS)
+  apcs=tr_msg_decode_apcs(rec, json_object_get(jrecord, "apcs"), &rc);
+  if (rc!=TRP_SUCCESS) {
+    rc=TRP_ERROR;
     goto cleanup;
-  if (TRP_SUCCESS!=trp_inforec_set_trust_router(rec, tr_new_name(s)))
+  }
+  trp_inforec_set_apcs(rec, apcs);
+
+  rc=tr_msg_get_json_integer(jrecord, "interval", &num);
+  tr_debug("tr_msg_decode_trp_inforec_comm: interval=%u", num);
+  if ((rc != TRP_SUCCESS) || (TRP_SUCCESS!=trp_inforec_set_interval(rec,num)))
     goto cleanup;
-  talloc_free(s); s=NULL;
 
-  trp_inforec_set_next_hop(rec, NULL); /* make sure this is null (filled in later) */
+  trp_inforec_set_provenance(rec, json_object_get(jrecord, "provenance"));
 
-  rc=tr_msg_get_json_integer(jrecord, "metric", &num);
-  if ((rc != TRP_SUCCESS) || (TRP_SUCCESS!=trp_inforec_set_metric(rec,num)))
+  /* optional */
+  rc=tr_msg_get_json_string(jrecord, "owner_realm", &s, tmp_ctx);
+  if (rc == TRP_SUCCESS) {
+    if (TRP_SUCCESS!=trp_inforec_set_owner_realm(rec, tr_new_name(s))) {
+      rc=TRP_ERROR;
+      goto cleanup;
+    }
+    if (s!=NULL) {
+      talloc_free(s);
+      s=NULL;
+    }
+  }
+
+  rc=tr_msg_get_json_string(jrecord, "owner_contact", &s, tmp_ctx);
+  if (rc == TRP_SUCCESS) {
+    if (TRP_SUCCESS!=trp_inforec_set_owner_contact(rec, tr_new_name(s))) {
+      rc=TRP_ERROR;
+      goto cleanup;
+    }
+    if (s!=NULL) {
+      talloc_free(s);
+      s=NULL;
+    }
+  }
+
+cleanup:
+  talloc_free(tmp_ctx);
+  return rc;
+}
+
+/* decode a single record */
+static TRP_INFOREC *tr_msg_decode_trp_inforec(TALLOC_CTX *mem_ctx, json_t *jrecord)
+{
+  TALLOC_CTX *tmp_ctx=talloc_new(NULL);
+  TRP_INFOREC_TYPE rectype;
+  TRP_INFOREC *rec=NULL;
+  TRP_RC rc=TRP_ERROR;
+  char *s=NULL;
+  
+  if (TRP_SUCCESS!=tr_msg_get_json_string(jrecord, "record_type", &s, tmp_ctx))
     goto cleanup;
 
-  rc=tr_msg_get_json_integer(jrecord, "interval", &num);
-  if ((rc != TRP_SUCCESS) || (TRP_SUCCESS!=trp_inforec_set_interval(rec,num)))
+  rectype=trp_inforec_type_from_string(s);
+  talloc_free(s); s=NULL;
+
+  rec=trp_inforec_new(tmp_ctx, rectype);
+  if (rec==NULL) {
+    rc=TRP_NOMEM;
+    goto cleanup;
+  }
+
+  tr_debug("tr_msg_decode_trp_inforec: '%s' record found.", trp_inforec_type_to_string(rec->type));
+
+  switch(trp_inforec_get_type(rec)) {
+  case TRP_INFOREC_TYPE_ROUTE:
+    rc=tr_msg_decode_trp_inforec_route(jrecord, rec);
+    break;
+  case TRP_INFOREC_TYPE_COMMUNITY:
+    rc=tr_msg_decode_trp_inforec_comm(jrecord, rec);
+    break;
+  default:
+    rc=TRP_UNSUPPORTED;
     goto cleanup;
+  }
 
   talloc_steal(mem_ctx, rec);
   rc=TRP_SUCCESS;
@@ -701,7 +897,9 @@ static json_t *tr_msg_encode_trp_upd(TRP_UPD *update)
   json_t *jupdate=NULL;
   json_t *jrecords=NULL;
   json_t *jrec=NULL;
+  json_t *jstr=NULL;
   TRP_INFOREC *rec;
+  char *s=NULL;
 
   if (update==NULL)
     return NULL;
@@ -710,6 +908,32 @@ static json_t *tr_msg_encode_trp_upd(TRP_UPD *update)
   if (jupdate==NULL)
     return NULL;
 
+  s=tr_name_strdup(trp_upd_get_comm(update));
+  if (s==NULL) {
+    json_decref(jupdate);
+    return NULL;
+  }
+  jstr=json_string(s);
+  free(s);s=NULL;
+  if(jstr==NULL) {
+    json_decref(jupdate);
+    return NULL;
+  }
+  json_object_set_new(jupdate, "community", jstr);
+
+  s=tr_name_strdup(trp_upd_get_realm(update));
+  if (s==NULL) {
+    json_decref(jupdate);
+    return NULL;
+  }
+  jstr=json_string(s);
+  free(s);s=NULL;
+  if(jstr==NULL) {
+    json_decref(jupdate);
+    return NULL;
+  }
+  json_object_set_new(jupdate, "realm", jstr);
+
   jrecords=json_array();
   if (jrecords==NULL) {
     json_decref(jupdate);
@@ -733,7 +957,7 @@ static json_t *tr_msg_encode_trp_upd(TRP_UPD *update)
   return jupdate;
 }
 
-/*Creates a linked list of records in the msg->body talloc context.
+/* Creates a linked list of records in the msg->body talloc context.
  * An error will be returned if any unparseable records are encountered. 
  */
 static TRP_UPD *tr_msg_decode_trp_upd(TALLOC_CTX *mem_ctx, json_t *jupdate)
@@ -744,6 +968,8 @@ static TRP_UPD *tr_msg_decode_trp_upd(TALLOC_CTX *mem_ctx, json_t *jupdate)
   TRP_UPD *update=NULL;
   TRP_INFOREC *new_rec=NULL;
   TRP_INFOREC *list_tail=NULL;
+  char *s=NULL;
+  TR_NAME *name;
   TRP_RC rc=TRP_ERROR;
 
   update=trp_upd_new(tmp_ctx);
@@ -752,6 +978,36 @@ static TRP_UPD *tr_msg_decode_trp_upd(TALLOC_CTX *mem_ctx, json_t *jupdate)
     goto cleanup;
   }
 
+  rc=tr_msg_get_json_string(jupdate, "community", &s, tmp_ctx);
+  if (rc != TRP_SUCCESS) {
+    tr_debug("tr_msg_decode_trp_upd: no community in TRP update message.");
+    rc=TRP_NOPARSE;
+    goto cleanup;
+  }
+  name=tr_new_name(s);
+  if (name==NULL) {
+    tr_debug("tr_msg_decode_trp_upd: could not allocate community name.");
+    rc=TRP_NOMEM;
+    goto cleanup;
+  }
+  talloc_free(s); s=NULL;
+  trp_upd_set_comm(update, name);
+
+  rc=tr_msg_get_json_string(jupdate, "realm", &s, tmp_ctx);
+  if (rc != TRP_SUCCESS) {
+    tr_debug("tr_msg_decode_trp_upd: no realm in TRP update message.");
+    rc=TRP_NOPARSE;
+    goto cleanup;
+  }
+  name=tr_new_name(s);
+  if (name==NULL) {
+    tr_debug("tr_msg_decode_trp_upd: could not allocate realm name.");
+    rc=TRP_NOMEM;
+    goto cleanup;
+  }
+  talloc_free(s); s=NULL;
+  trp_upd_set_realm(update, name);
+
   jrecords=json_object_get(jupdate, "records");
   if ((jrecords==NULL) || (!json_is_array(jrecords))) {
     rc=TRP_NOPARSE;
@@ -919,6 +1175,7 @@ char *tr_msg_encode(TR_MSG *msg)
     }
 
   encoded=json_dumps(jmsg, 0);
+  tr_debug("tr_msg_encode: outgoing msg=%s", encoded);
   json_decref(jmsg);
   return encoded;
 }