Merge pull request #86 from painless-security/jennifer/aaa_server_port
[trust_router.git] / common / tr_idp_encoders.c
index fec129a..4b3d845 100644 (file)
 #include <tr_idp.h>
 #include <tr_config.h>
 #include <tr_debug.h>
+#include <tr_json_util.h>
 
 static char *tr_aaa_server_to_str(TALLOC_CTX *mem_ctx, TR_AAA_SERVER *aaa)
 {
-  return talloc_strndup(mem_ctx, aaa->hostname->buf, aaa->hostname->len);
+  char *aaa_hostname = tr_name_strdup( tr_aaa_server_get_hostname(aaa) );
+  char *result = NULL;
+
+  if (aaa_hostname == NULL)
+    return NULL;
+
+  result = talloc_asprintf(mem_ctx,
+                           "%s:%d",
+                           aaa_hostname,
+                           tr_aaa_server_get_port(aaa));
+  free(aaa_hostname);
+  return result;
 }
 
 
@@ -121,23 +133,6 @@ char *tr_idp_realm_to_str(TALLOC_CTX *mem_ctx, TR_IDP_REALM *idp)
 }
 
 
-/* helper for below */
-#define OBJECT_SET_OR_FAIL(jobj, key, val)     \
-do {                                           \
-  if (val)                                     \
-    json_object_set_new((jobj),(key),(val));   \
-  else                                         \
-    goto cleanup;                              \
-} while (0)
-
-#define ARRAY_APPEND_OR_FAIL(jary, val)        \
-do {                                           \
-  if (val)                                     \
-    json_array_append_new((jary),(val));       \
-  else                                         \
-    goto cleanup;                              \
-} while (0)
-
 static json_t *tr_apcs_to_json(TR_APC *apcs)
 {
   json_t *jarray = json_array();
@@ -148,10 +143,10 @@ static json_t *tr_apcs_to_json(TR_APC *apcs)
   if ((jarray == NULL) || (iter == NULL))
     goto cleanup;
 
-  apc = tr_apc_iter_first(iter, apcs);
-  while (apc) {
+  for (apc = tr_apc_iter_first(iter, apcs);
+       apc != NULL;
+       apc = tr_apc_iter_next(iter)) {
     ARRAY_APPEND_OR_FAIL(jarray, tr_name_to_json_string(tr_apc_get_id(apc)));
-    apc = tr_apc_iter_next(iter);
   }
 
   /* success */
@@ -167,14 +162,9 @@ cleanup:
 
 static json_t *tr_aaa_server_to_json(TR_AAA_SERVER *aaa)
 {
-  char *hostname = tr_name_strdup(aaa->hostname);
-  char *s = NULL;
+  char *s = tr_aaa_server_to_str(NULL, aaa);
   json_t *jstr = NULL;
 
-  if (hostname == NULL)
-    return NULL;
-
-  s = talloc_asprintf(NULL, "%s:%d", hostname, TID_PORT);
   if (s) {
     jstr = json_string(s);
     talloc_free(s);
@@ -192,10 +182,10 @@ static json_t *tr_aaa_servers_to_json(TR_AAA_SERVER *aaas)
   if ((jarray == NULL) || (iter == NULL))
     goto cleanup;
 
-  aaa = tr_aaa_server_iter_first(iter, aaas);
-  while (aaa) {
+  for (aaa = tr_aaa_server_iter_first(iter, aaas);
+       aaa != NULL;
+       aaa = tr_aaa_server_iter_next(iter)) {
     ARRAY_APPEND_OR_FAIL(jarray, tr_aaa_server_to_json(aaa));
-    aaa = tr_aaa_server_iter_next(iter);
   }
 
   /* success */