Fix misleading indentation
[trust_router.git] / common / tr_aaa_server.c
index e8df13d..dedf054 100644 (file)
@@ -38,6 +38,8 @@
 #include <tr_name_internal.h>
 #include <tr_aaa_server.h>
 #include <trust_router/tid.h>
+#include <tr_util.h>
+#include <tr_inet_util.h>
 
 static int tr_aaa_server_destructor(void *obj)
 {
@@ -47,12 +49,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 +66,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 +99,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 +112,71 @@ 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
+ *
+ * If hostname or port are invalid, hostname will be empty and port will be -1.
+ *
+ * @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);
+  char *hostname;
+  int port;
+
+  if (aaa == NULL)
+    goto failed;
 
+  hostname = tr_parse_host(tmp_ctx, s, &port);
+  if (NULL == hostname) {
+    hostname = "";
+    port = -1;
+  }
+
+  tr_aaa_server_set_hostname(aaa, tr_new_name(hostname));
+  if (tr_aaa_server_get_hostname(aaa) == NULL)
+    goto failed;
+
+  tr_aaa_server_set_port(aaa, port); /* port = 0 uses default TID port */
+  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;
+}