Lesser General Public License for more details.
*/
+#include <arpa/inet.h>
+#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <ctype.h>
#include <loc/libloc.h>
-#include "libloc-private.h"
-#include "database.h"
+#include <loc/private.h>
struct loc_ctx {
int refcount;
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,
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));
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);
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_open(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;
}