+/*********************************************************************
+ *
+ * Function : parse_forwarder_address
+ *
+ * Description : Parse out the host and port from a forwarder address.
+ *
+ * Parameters :
+ * 1 : address = The forwarder address to parse.
+ * 2 : hostname = Used to return the hostname. NULL on error.
+ * 3 : port = Used to return the port. Untouched if no port
+ * is specified.
+ *
+ * Returns : JB_ERR_OK on success
+ * JB_ERR_MEMORY on out of memory
+ * JB_ERR_PARSE on malformed address.
+ *
+ *********************************************************************/
+jb_err parse_forwarder_address(char *address, char **hostname, int *port)
+{
+ char *p = address;
+
+ if ((*address == '[') && (NULL == strchr(address, ']')))
+ {
+ /* XXX: Should do some more validity checks here. */
+ return JB_ERR_PARSE;
+ }
+
+ *hostname = strdup(address);
+ if (NULL == *hostname)
+ {
+ return JB_ERR_MEMORY;
+ }
+
+ if ((**hostname == '[') && (NULL != (p = strchr(*hostname, ']'))))
+ {
+ *p++ = '\0';
+ memmove(*hostname, (*hostname + 1), (size_t)(p - *hostname));
+ if (*p == ':')
+ {
+ *port = (int)strtol(++p, NULL, 0);
+ }
+ }
+ else if (NULL != (p = strchr(*hostname, ':')))
+ {
+ *p++ = '\0';
+ *port = (int)strtol(p, NULL, 0);
+ }
+
+ return JB_ERR_OK;
+
+}
+
+