Work with new hostname parsing and improve error reports
[trust_router.git] / common / tr_config_orgs.c
index 60674c5..e9b2c3c 100644 (file)
@@ -49,6 +49,7 @@
 #include <tr.h>
 #include <trust_router/trp.h>
 #include <tr_util.h>
+#include <tr_inet_util.h>
 
 #if JANSSON_VERSION_HEX < 0x020500
 #include "jansson_iterators.h"
@@ -171,8 +172,7 @@ static TR_CFG_RC tr_cfg_parse_one_peer_org(TR_CFG *trc, json_t *jporg)
   TR_GSS_NAMES *names=NULL;
   TR_FILTER_SET *filt_set=NULL;
   TR_CFG_RC rc=TR_CFG_ERROR;
-  TR_NAME *hostname=NULL;
-  char *s_hostname=NULL;
+  char *hostname=NULL;
   int port;
 
   jhost=json_object_get(jporg, "hostname");
@@ -204,13 +204,15 @@ static TR_CFG_RC tr_cfg_parse_one_peer_org(TR_CFG *trc, json_t *jporg)
     goto cleanup;
   }
 
-  if (0 != tr_parse_hostname_and_port(json_string_value(jhost), &hostname, &port)) {
+  /* parse / validate the hostname and port */
+  hostname = tr_parse_host(tmp_ctx, json_string_value(jhost), &port);
+  if (NULL == hostname) {
     tr_err("tr_cfg_parse_one_peer_org: error parsing hostname (%s)", json_string_value(jhost));
     rc=TR_CFG_NOPARSE;
     goto cleanup;
   }
 
-  if ((port < 0) || (port > 65535)) {
+  if (port < 0) {
     tr_err("tr_cfg_parse_one_peer_org: invalid port (%s)", json_string_value(jhost));
     rc=TR_CFG_NOPARSE;
     goto cleanup;
@@ -220,21 +222,7 @@ static TR_CFG_RC tr_cfg_parse_one_peer_org(TR_CFG *trc, json_t *jporg)
     port = TRP_PORT;
   trp_peer_set_port(new_peer, port);
 
-
-  if (hostname->len == 0) {
-    tr_err("tr_cfg_parse_one_peer_org: no hostname specified (%s)", json_string_value(jhost));
-    rc=TR_CFG_NOPARSE;
-    goto cleanup;
-  }
-
-  s_hostname = tr_name_strdup(hostname);
-  if (s_hostname == NULL) {
-    tr_err("tr_cfg_parse_one_peer_org: could not allocate hostname string.");
-    rc = TR_CFG_NOMEM;
-    goto cleanup;
-  }
-
-  trp_peer_set_server(new_peer, s_hostname); /* string is strdup'ed in _set_server() */
+  trp_peer_set_server(new_peer, hostname); /* string is strdup'ed in _set_server() */
   if (trp_peer_get_server(new_peer) == NULL) {
     tr_err("tr_cfg_parse_one_peer: could not set server hostname for new peer");
     rc = TR_CFG_NOMEM;
@@ -264,12 +252,6 @@ static TR_CFG_RC tr_cfg_parse_one_peer_org(TR_CFG *trc, json_t *jporg)
   rc=TR_CFG_SUCCESS;
 
 cleanup:
-  if (hostname)
-    tr_free_name(hostname);
-
-  if (s_hostname)
-    free(s_hostname);
-
   talloc_free(tmp_ctx);
   return rc;
 }