example: make ustream-ssl optional (load via dlopen)
[project/uclient.git] / uclient.c
index 5b71bf4..bcb7357 100644 (file)
--- a/uclient.c
+++ b/uclient.c
  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  */
+#include <arpa/inet.h>
 #include <libubox/ustream-ssl.h>
 #include "uclient.h"
 #include "uclient-utils.h"
 #include "uclient-backend.h"
 
+char *uclient_get_addr(char *dest, int *port, union uclient_addr *a)
+{
+       int portval;
+       void *ptr;
+
+       switch(a->sa.sa_family) {
+       case AF_INET:
+               ptr = &a->sin.sin_addr;
+               portval = a->sin.sin_port;
+               break;
+       case AF_INET6:
+               ptr = &a->sin6.sin6_addr;
+               portval = a->sin6.sin6_port;
+               break;
+       default:
+               return strcpy(dest, "Unknown");
+       }
+
+       inet_ntop(a->sa.sa_family, ptr, dest, INET6_ADDRSTRLEN);
+       if (port)
+               *port = ntohs(portval);
+
+       return dest;
+}
+
+
 struct uclient_url __hidden *
 uclient_get_url(const char *url_str, const char *auth_str)
 {
@@ -101,8 +128,10 @@ uclient_get_url(const char *url_str, const char *auth_str)
                        url->port = next + 1;
        } else {
                next = strrchr(url->host, ':');
-               if (next)
+               if (next) {
+                       *next = 0;
                        url->port = next + 1;
+               }
        }
 
        return url;
@@ -132,12 +161,12 @@ struct uclient *uclient_new(const char *url_str, const char *auth_str, const str
        return cl;
 }
 
-int uclient_set_url(struct uclient *cl, const char *url_str)
+int uclient_set_url(struct uclient *cl, const char *url_str, const char *auth_str)
 {
        const struct uclient_backend *backend = cl->backend;
        struct uclient_url *url = cl->url;
 
-       url = uclient_get_url(url_str, NULL);
+       url = uclient_get_url(url_str, auth_str);
        if (!url)
                return -1;