X-Git-Url: http://git.ipfire.org/?p=people%2Fms%2Flibloc.git;a=blobdiff_plain;f=src%2Flibloc.c;h=61f7c5600787dd2e9ff29e97cb0f91a11fa97cb2;hp=8b8872c404da93571bab75ea25e95b5428a65dd0;hb=67cb758ec5cb116888b96581ef7d723ba24816e1;hpb=9fc7f0017d4d09eefa3fc601b3b7af3a797f72c1 diff --git a/src/libloc.c b/src/libloc.c index 8b8872c..61f7c56 100644 --- a/src/libloc.c +++ b/src/libloc.c @@ -14,6 +14,8 @@ Lesser General Public License for more details. */ +#include +#include #include #include #include @@ -24,7 +26,6 @@ #include #include -#include #include struct loc_ctx { @@ -33,8 +34,6 @@ struct loc_ctx { int priority, const char *file, int line, const char *fn, const char *format, va_list args); int log_priority; - - struct loc_database* db; }; void loc_log(struct loc_ctx* ctx, @@ -83,8 +82,6 @@ LOC_EXPORT int loc_new(struct loc_ctx** ctx) { c->log_fn = log_stderr; c->log_priority = LOG_ERR; - c->db = NULL; - const char* env = secure_getenv("LOC_LOG"); if (env) loc_set_log_priority(c, log_priority(env)); @@ -112,10 +109,6 @@ LOC_EXPORT struct loc_ctx* loc_unref(struct loc_ctx* ctx) { if (--ctx->refcount > 0) return NULL; - // Release any loaded databases - if (ctx->db) - loc_database_unref(ctx->db); - INFO(ctx, "context %p released\n", ctx); free(ctx); @@ -137,22 +130,33 @@ LOC_EXPORT void loc_set_log_priority(struct loc_ctx* ctx, int priority) { ctx->log_priority = priority; } -LOC_EXPORT int loc_load(struct loc_ctx* ctx, const char* path) { - FILE* f = fopen(path, "r"); - if (!f) - return -errno; +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); - // Release any previously openend database - if (ctx->db) - loc_database_unref(ctx->db); + // Try parsing this as an IPv6 address + int r = inet_pton(AF_INET6, string, address); - // Open the new database - int r = loc_database_new(ctx, &ctx->db, f); - if (r) - return r; + // If inet_pton returns one it has been successful + if (r == 1) { + DEBUG(ctx, "%s is an IPv6 address\n", string); + return 0; + } - // Close the file - fclose(f); + // 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); - return 0; + // 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; }