Correctly set peer when an update is received
[trust_router.git] / common / tr_aaa_server.c
index e8df13d..193f018 100644 (file)
@@ -38,6 +38,7 @@
 #include <tr_name_internal.h>
 #include <tr_aaa_server.h>
 #include <trust_router/tid.h>
+#include <tr_util.h>
 
 static int tr_aaa_server_destructor(void *obj)
 {
@@ -47,12 +48,13 @@ static int tr_aaa_server_destructor(void *obj)
   return 0;
 }
 
-TR_AAA_SERVER *tr_aaa_server_new(TALLOC_CTX *mem_ctx, TR_NAME *hostname)
+TR_AAA_SERVER *tr_aaa_server_new(TALLOC_CTX *mem_ctx)
 {
   TR_AAA_SERVER *aaa=talloc(mem_ctx, TR_AAA_SERVER);
   if (aaa!=NULL) {
     aaa->next=NULL;
-    aaa->hostname=hostname;
+    aaa->hostname = NULL;
+    tr_aaa_server_set_port(aaa, 0); /* go through setter to guarantee consistent default */
     talloc_set_destructor((void *)aaa, tr_aaa_server_destructor);
   }
   return aaa;
@@ -63,30 +65,6 @@ void tr_aaa_server_free(TR_AAA_SERVER *aaa)
   talloc_free(aaa);
 }
 
-TR_AAA_SERVER_ITER *tr_aaa_server_iter_new(TALLOC_CTX *mem_ctx)
-{
-  return talloc(mem_ctx, TR_AAA_SERVER_ITER);
-}
-
-void tr_aaa_server_iter_free(TR_AAA_SERVER_ITER *iter)
-{
-  talloc_free(iter);
-}
-
-TR_AAA_SERVER *tr_aaa_server_iter_first(TR_AAA_SERVER_ITER *iter, TR_AAA_SERVER *aaa)
-{
-  iter->this=aaa;
-  return iter->this;
-}
-
-TR_AAA_SERVER *tr_aaa_server_iter_next(TR_AAA_SERVER_ITER *iter)
-{
-  if (iter->this!=NULL) {
-    iter->this=iter->this->next;
-  }
-  return iter->this;
-}
-
 TR_NAME *tr_aaa_server_get_hostname(TR_AAA_SERVER *aaa)
 {
   return aaa->hostname;
@@ -120,7 +98,9 @@ int tr_aaa_server_get_port(TR_AAA_SERVER *aaa)
 /**
  * Set the port for a AAA server
  *
- * If port is outside the range 1-65535, uses the standard TID port (12309).
+ * If port is 0, uses the standard TID port (12309). Other invalid values are stored
+ * as-is.
+ *
  * Does nothing if aaa is null.
  *
  * @param aaa
@@ -131,10 +111,64 @@ void tr_aaa_server_set_port(TR_AAA_SERVER *aaa, int port)
   if (aaa == NULL)
     return;
 
-  if ((port <= 0) || (port > 65535))
+  if (port == 0)
     port = TID_PORT;
 
   aaa->port = port;
 }
 
+/**
+ * Allocate a AAA server record and fill it in by parsing a hostname:port string
+ *
+ * Does not validate hostname or port values. The port will be -1 if the port
+ * could not be parsed properly.
+ *
+ * @return newly allocated TR_AAA_SERVER in the mem_ctx context, or NULL on error
+ */
+TR_AAA_SERVER *tr_aaa_server_from_string(TALLOC_CTX *mem_ctx, const char *s)
+{
+  TALLOC_CTX *tmp_ctx = talloc_new(NULL);
+  TR_AAA_SERVER *aaa = tr_aaa_server_new(tmp_ctx);
 
+  if (aaa == NULL)
+    goto failed;
+
+  tr_aaa_server_set_hostname(aaa, tr_parse_hostname(s));
+  if (tr_aaa_server_get_hostname(aaa) == NULL)
+    goto failed;
+
+  tr_aaa_server_set_port(aaa, tr_parse_port(s));
+  talloc_steal(mem_ctx, aaa); /*put this in the caller's context */
+  goto succeeded;
+
+failed:
+  aaa = NULL; /* talloc will free the memory if it was allocated */
+
+succeeded:
+  talloc_free(tmp_ctx);
+  return aaa;
+}
+
+TR_AAA_SERVER_ITER *tr_aaa_server_iter_new(TALLOC_CTX *mem_ctx)
+{
+  return talloc(mem_ctx, TR_AAA_SERVER_ITER);
+}
+
+void tr_aaa_server_iter_free(TR_AAA_SERVER_ITER *iter)
+{
+  talloc_free(iter);
+}
+
+TR_AAA_SERVER *tr_aaa_server_iter_first(TR_AAA_SERVER_ITER *iter, TR_AAA_SERVER *aaa)
+{
+  iter->this=aaa;
+  return iter->this;
+}
+
+TR_AAA_SERVER *tr_aaa_server_iter_next(TR_AAA_SERVER_ITER *iter)
+{
+  if (iter->this!=NULL) {
+    iter->this=iter->this->next;
+  }
+  return iter->this;
+}