Add copyright statement missing from recently added files.
[trust_router.git] / trp / trp_upd.c
index 7c6a1e7..927c993 100644 (file)
@@ -1,3 +1,37 @@
+/*
+ * Copyright (c) 2016, JANET(UK)
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * 3. Neither the name of JANET(UK) nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
+ * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
+ * OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ */
+
 #include <jansson.h>
 #include <talloc.h>
 
@@ -76,6 +110,11 @@ static int trp_inforec_route_destructor(void *object)
     body->trust_router=NULL;
     tr_debug("trp_inforec_route_destructor: freed trust_router");
   }
+  if (body->next_hop != NULL) {
+    tr_free_name(body->next_hop);
+    body->next_hop=NULL;
+    tr_debug("trp_inforec_route_destructor: freed next_hop");
+  }
 
   return 0;
 }
@@ -88,6 +127,8 @@ static void *trp_inforec_route_new(TALLOC_CTX *mem_ctx)
     new_rec->comm=NULL;
     new_rec->realm=NULL;
     new_rec->trust_router=NULL;
+    new_rec->next_hop=NULL;
+    new_rec->next_hop_port=0;
     new_rec->metric=TRP_METRIC_INFINITY;
     new_rec->interval=0;
     talloc_set_destructor((void *)new_rec, trp_inforec_route_destructor);
@@ -95,6 +136,41 @@ static void *trp_inforec_route_new(TALLOC_CTX *mem_ctx)
   return new_rec;
 }
 
+TRP_INFOREC *trp_inforec_get_next(TRP_INFOREC *rec)
+{
+  if (rec!=NULL)
+    return rec->next;
+  else
+    return NULL;
+}
+
+static TRP_INFOREC *trp_inforec_get_tail(TRP_INFOREC *rec)
+{
+  while ((rec->next)!=NULL)
+    rec=trp_inforec_get_next(rec);
+  return rec;
+}
+
+void trp_inforec_set_next(TRP_INFOREC *rec, TRP_INFOREC *next_rec)
+{
+  if (rec !=NULL)
+    rec->next=next_rec;
+}
+
+TRP_INFOREC_TYPE trp_inforec_get_type(TRP_INFOREC *rec)
+{
+  if (rec)
+    return rec->type;
+  else
+    return TRP_INFOREC_TYPE_UNKNOWN;
+}
+
+void trp_inforec_set_type(TRP_INFOREC *rec, TRP_INFOREC_TYPE type)
+{
+  if (rec!=NULL)
+    rec->type=type;
+}
+
 TR_NAME *trp_inforec_get_comm(TRP_INFOREC *rec)
 {
   switch (rec->type) {
@@ -108,6 +184,11 @@ TR_NAME *trp_inforec_get_comm(TRP_INFOREC *rec)
   return NULL;
 }
 
+TR_NAME *trp_inforec_dup_comm(TRP_INFOREC *rec)
+{
+  return tr_dup_name(trp_inforec_get_comm(rec));
+}
+
 TRP_RC trp_inforec_set_comm(TRP_INFOREC *rec, TR_NAME *comm)
 {
   switch (rec->type) {
@@ -136,6 +217,11 @@ TR_NAME *trp_inforec_get_realm(TRP_INFOREC *rec)
   return NULL;
 }
 
+TR_NAME *trp_inforec_dup_realm(TRP_INFOREC *rec)
+{
+  return tr_dup_name(trp_inforec_get_realm(rec));
+}
+
 TRP_RC trp_inforec_set_realm(TRP_INFOREC *rec, TR_NAME *realm)
 {
   switch (rec->type) {
@@ -164,6 +250,11 @@ TR_NAME *trp_inforec_get_trust_router(TRP_INFOREC *rec)
   return NULL;
 }
 
+TR_NAME *trp_inforec_dup_trust_router(TRP_INFOREC *rec)
+{
+  return tr_dup_name(trp_inforec_get_trust_router(rec));
+}
+
 TRP_RC trp_inforec_set_trust_router(TRP_INFOREC *rec, TR_NAME *trust_router)
 {
   switch (rec->type) {
@@ -179,6 +270,40 @@ TRP_RC trp_inforec_set_trust_router(TRP_INFOREC *rec, TR_NAME *trust_router)
   return TRP_ERROR;
 }
 
+/* TODO: need to return hostname/port --jlr */
+TR_NAME *trp_inforec_get_next_hop(TRP_INFOREC *rec)
+{
+  switch (rec->type) {
+  case TRP_INFOREC_TYPE_ROUTE:
+    if (rec->data.route!=NULL)
+      return rec->data.route->next_hop;
+    break;
+  default:
+    break;
+  }
+  return NULL;
+}
+
+TR_NAME *trp_inforec_dup_next_hop(TRP_INFOREC *rec)
+{
+  return tr_dup_name(trp_inforec_get_next_hop(rec));
+}
+
+TRP_RC trp_inforec_set_next_hop(TRP_INFOREC *rec, TR_NAME *next_hop)
+{
+  switch (rec->type) {
+  case TRP_INFOREC_TYPE_ROUTE:
+    if (rec->data.route!=NULL) {
+      rec->data.route->next_hop=next_hop;
+      return TRP_SUCCESS;
+    }
+    break;
+  default:
+    break;
+  }
+  return TRP_ERROR;
+}
+
 unsigned int trp_inforec_get_metric(TRP_INFOREC *rec)
 {
   switch (rec->type) {
@@ -258,8 +383,8 @@ TRP_INFOREC *trp_inforec_new(TALLOC_CTX *mem_ctx, TRP_INFOREC_TYPE type)
   struct trp_inforec_type_entry *dtype=get_trp_inforec_type_entry(type);
 
   if ((new_rec != NULL) && (dtype->type != TRP_INFOREC_TYPE_UNKNOWN)) {
-    new_rec->next=NULL;
-    new_rec->type=type;
+    trp_inforec_set_type(new_rec, type);
+    trp_inforec_set_next(new_rec, NULL);
     if (dtype->allocate!=NULL) {
       if (TRP_SUCCESS!=trp_inforec_set_data(new_rec, dtype->allocate(new_rec))) {
         talloc_free(new_rec);
@@ -276,12 +401,22 @@ void trp_inforec_free(TRP_INFOREC *rec)
     talloc_free(rec);
 }
 
+static int trp_upd_destructor(void *object)
+{
+  TRP_UPD *upd=talloc_get_type_abort(object, TRP_UPD);
+  if (upd->peer!=NULL)
+    tr_free_name(upd->peer);
+  return 0;
+}
+
 TRP_UPD *trp_upd_new(TALLOC_CTX *mem_ctx)
 {
   TRP_UPD *new_body=talloc(mem_ctx, TRP_UPD);
 
   if (new_body!=NULL) {
     new_body->records=NULL;
+    new_body->peer=NULL;
+    talloc_set_destructor((void *)new_body, trp_upd_destructor);
   }
   return new_body;
 }
@@ -292,6 +427,57 @@ void trp_upd_free(TRP_UPD *update)
     talloc_free(update);
 }
 
+TRP_INFOREC *trp_upd_get_inforec(TRP_UPD *upd)
+{
+  if (upd!=NULL)
+    return upd->records;
+  else
+    return NULL;
+}
+
+void trp_upd_set_inforec(TRP_UPD *upd, TRP_INFOREC *rec)
+{
+  if (upd!=NULL)
+    upd->records=rec;
+}
+
+void trp_upd_add_inforec(TRP_UPD *upd, TRP_INFOREC *rec)
+{
+  tr_debug("trp_upd_add_inforec: adding record.");
+  if (upd->records==NULL)
+    upd->records=rec;
+  else
+    trp_inforec_set_next(trp_inforec_get_tail(upd->records), rec);
+  talloc_steal(upd, rec);
+}
+
+TR_NAME *trp_upd_get_peer(TRP_UPD *upd)
+{
+  return upd->peer;
+}
+
+TR_NAME *trp_upd_dup_peer(TRP_UPD *upd)
+{
+  return tr_dup_name(upd->peer);
+}
+
+void trp_upd_set_peer(TRP_UPD *upd, TR_NAME *peer)
+{
+  upd->peer=peer;
+}
+
+void trp_upd_set_next_hop(TRP_UPD *upd, const char *hostname, unsigned int port)
+{
+  TRP_INFOREC *rec=NULL;
+  TR_NAME *cpy=NULL;
+  
+  for (rec=trp_upd_get_inforec(upd); rec!=NULL; rec=trp_inforec_get_next(rec)) {
+    if (trp_inforec_set_next_hop(rec, cpy=tr_new_name(hostname)) != TRP_SUCCESS) {
+      tr_err("trp_upd_set_peer: error setting peer.");
+      tr_free_name(cpy);
+    }
+  }
+}
 
 /* pretty print */
 static void trp_inforec_route_print(TRP_INFOREC_DATA data)