[windows] - fixups for building on windows
[deb_shairplay.git] / src / lib / netutils.c
index 255822bb436f9cd7f0cf35494c701370acc477cc..8226e4f32d8c926ea3b2e746babda0d0f56f1da9 100644 (file)
@@ -1,3 +1,17 @@
+/**
+ *  Copyright (C) 2011-2012  Juho Vähä-Herttua
+ *
+ *  This library is free software; you can redistribute it and/or
+ *  modify it under the terms of the GNU Lesser General Public
+ *  License as published by the Free Software Foundation; either
+ *  version 2.1 of the License, or (at your option) any later version.
+ *
+ *  This library is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ *  Lesser General Public License for more details.
+ */
+
 #include <stdlib.h>
 #include <string.h>
 #include <assert.h>
@@ -137,3 +151,39 @@ netutils_get_address(void *sockaddr, int *length)
        return NULL;
 }
 
+int
+netutils_parse_address(int family, const char *src, void *dst, int dstlen)
+{
+       struct addrinfo *result;
+       struct addrinfo *ptr;
+       struct addrinfo hints;
+       int length;
+       int ret;
+
+       if (family != AF_INET && family != AF_INET6) {
+               return -1;
+       }
+       if (!src || !dst) {
+               return -1;
+       }
+
+       memset(&hints, 0, sizeof(hints));
+       hints.ai_family = family;
+       hints.ai_flags = AI_PASSIVE | AI_NUMERICHOST;
+
+       ret = getaddrinfo(src, NULL, &hints, &result);
+       if (ret != 0) {
+               return -1;
+       }
+
+       length = -1;
+       for (ptr=result; ptr!=NULL; ptr=ptr->ai_next) {
+               if (family == ptr->ai_family && (unsigned int)dstlen >= ptr->ai_addrlen) {
+                       memcpy(dst, ptr->ai_addr, ptr->ai_addrlen);
+                       length = ptr->ai_addrlen;
+                       break;
+               }
+       }
+       freeaddrinfo(result);
+       return length;
+}