]> git.ipfire.org Git - people/ms/libloc.git/blobdiff - src/libloc.c
Drop loc_load function
[people/ms/libloc.git] / src / libloc.c
index fe44b3c159d5ab92cf5496c30f8b9ab0ad971ef7..61f7c5600787dd2e9ff29e97cb0f91a11fa97cb2 100644 (file)
@@ -14,6 +14,8 @@
        Lesser General Public License for more details.
 */
 
+#include <arpa/inet.h>
+#include <netinet/in.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <stddef.h>
@@ -24,7 +26,7 @@
 #include <ctype.h>
 
 #include <loc/libloc.h>
-#include "libloc-private.h"
+#include <loc/private.h>
 
 struct loc_ctx {
        int refcount;
@@ -127,3 +129,34 @@ LOC_EXPORT int loc_get_log_priority(struct loc_ctx* ctx) {
 LOC_EXPORT void loc_set_log_priority(struct loc_ctx* ctx, int priority) {
        ctx->log_priority = priority;
 }
+
+LOC_EXPORT int loc_parse_address(struct loc_ctx* ctx, const char* string, struct in6_addr* address) {
+       DEBUG(ctx, "Paring IP address %s\n", string);
+
+       // Try parsing this as an IPv6 address
+       int r = inet_pton(AF_INET6, string, address);
+
+       // If inet_pton returns one it has been successful
+       if (r == 1) {
+               DEBUG(ctx, "%s is an IPv6 address\n", string);
+               return 0;
+       }
+
+       // Try parsing this as an IPv4 address
+       struct in_addr ipv4_address;
+       r = inet_pton(AF_INET, string, &ipv4_address);
+       if (r == 1) {
+               DEBUG(ctx, "%s is an IPv4 address\n", string);
+
+               // Convert to IPv6-mapped address
+               address->s6_addr32[0] = htonl(0x0000);
+               address->s6_addr32[1] = htonl(0x0000);
+               address->s6_addr32[2] = htonl(0xffff);
+               address->s6_addr32[3] = ipv4_address.s_addr;
+
+               return 0;
+       }
+
+       DEBUG(ctx, "%s is not an valid IP address\n", string);
+       return 1;
+}