]> git.ipfire.org Git - people/ms/libloc.git/blob - src/libloc.c
importer: Drop EDROP as it has been merged into DROP
[people/ms/libloc.git] / src / libloc.c
1 /*
2 libloc - A library to determine the location of someone on the Internet
3
4 Copyright (C) 2017 IPFire Development Team <info@ipfire.org>
5
6 This library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
10
11 This library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15 */
16
17 #include <arpa/inet.h>
18 #include <netinet/in.h>
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <stddef.h>
22 #include <stdarg.h>
23 #include <unistd.h>
24 #include <errno.h>
25 #include <string.h>
26 #include <ctype.h>
27
28 #include <loc/libloc.h>
29 #include <loc/compat.h>
30 #include <loc/private.h>
31
32 struct loc_ctx {
33 int refcount;
34 void (*log_fn)(struct loc_ctx* ctx,
35 int priority, const char *file, int line, const char *fn,
36 const char *format, va_list args);
37 int log_priority;
38 };
39
40 void loc_log(struct loc_ctx* ctx,
41 int priority, const char* file, int line, const char* fn,
42 const char* format, ...) {
43 va_list args;
44
45 va_start(args, format);
46 ctx->log_fn(ctx, priority, file, line, fn, format, args);
47 va_end(args);
48 }
49
50 static void log_stderr(struct loc_ctx* ctx,
51 int priority, const char* file, int line, const char* fn,
52 const char* format, va_list args) {
53 fprintf(stderr, "libloc: %s: ", fn);
54 vfprintf(stderr, format, args);
55 }
56
57 static int log_priority(const char* priority) {
58 char *endptr;
59
60 int prio = strtol(priority, &endptr, 10);
61
62 if (endptr[0] == '\0' || isspace(endptr[0]))
63 return prio;
64
65 if (strncmp(priority, "err", 3) == 0)
66 return LOG_ERR;
67
68 if (strncmp(priority, "info", 4) == 0)
69 return LOG_INFO;
70
71 if (strncmp(priority, "debug", 5) == 0)
72 return LOG_DEBUG;
73
74 return 0;
75 }
76
77 LOC_EXPORT int loc_new(struct loc_ctx** ctx) {
78 struct loc_ctx* c = calloc(1, sizeof(*c));
79 if (!c)
80 return -ENOMEM;
81
82 c->refcount = 1;
83 c->log_fn = log_stderr;
84 c->log_priority = LOG_ERR;
85
86 const char* env = secure_getenv("LOC_LOG");
87 if (env)
88 loc_set_log_priority(c, log_priority(env));
89
90 INFO(c, "ctx %p created\n", c);
91 DEBUG(c, "log_priority=%d\n", c->log_priority);
92 *ctx = c;
93
94 return 0;
95 }
96
97 LOC_EXPORT struct loc_ctx* loc_ref(struct loc_ctx* ctx) {
98 if (!ctx)
99 return NULL;
100
101 ctx->refcount++;
102
103 return ctx;
104 }
105
106 LOC_EXPORT struct loc_ctx* loc_unref(struct loc_ctx* ctx) {
107 if (!ctx)
108 return NULL;
109
110 if (--ctx->refcount > 0)
111 return NULL;
112
113 INFO(ctx, "context %p released\n", ctx);
114 free(ctx);
115
116 return NULL;
117 }
118
119 LOC_EXPORT void loc_set_log_fn(struct loc_ctx* ctx,
120 void (*log_fn)(struct loc_ctx* ctx, int priority, const char* file,
121 int line, const char* fn, const char* format, va_list args)) {
122 ctx->log_fn = log_fn;
123 INFO(ctx, "custom logging function %p registered\n", log_fn);
124 }
125
126 LOC_EXPORT int loc_get_log_priority(struct loc_ctx* ctx) {
127 return ctx->log_priority;
128 }
129
130 LOC_EXPORT void loc_set_log_priority(struct loc_ctx* ctx, int priority) {
131 ctx->log_priority = priority;
132 }
133
134 LOC_EXPORT int loc_parse_address(struct loc_ctx* ctx, const char* string, struct in6_addr* address) {
135 DEBUG(ctx, "Parsing IP address %s\n", string);
136
137 // Try parsing this as an IPv6 address
138 int r = inet_pton(AF_INET6, string, address);
139
140 // If inet_pton returns one it has been successful
141 if (r == 1) {
142 DEBUG(ctx, "%s is an IPv6 address\n", string);
143 return 0;
144 }
145
146 // Try parsing this as an IPv4 address
147 struct in_addr ipv4_address;
148 r = inet_pton(AF_INET, string, &ipv4_address);
149 if (r == 1) {
150 DEBUG(ctx, "%s is an IPv4 address\n", string);
151
152 // Convert to IPv6-mapped address
153 address->s6_addr32[0] = htonl(0x0000);
154 address->s6_addr32[1] = htonl(0x0000);
155 address->s6_addr32[2] = htonl(0xffff);
156 address->s6_addr32[3] = ipv4_address.s_addr;
157
158 return 0;
159 }
160
161 DEBUG(ctx, "%s is not an valid IP address\n", string);
162 return -EINVAL;
163 }