]> git.ipfire.org Git - people/ms/libloc.git/blob - src/libloc.c
fc05eb0f976c81fdbbfee94b1f1deb3452bd678f
[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/private.h>
30
31 struct loc_ctx {
32 int refcount;
33 void (*log_fn)(struct loc_ctx* ctx,
34 int priority, const char *file, int line, const char *fn,
35 const char *format, va_list args);
36 int log_priority;
37 };
38
39 void loc_log(struct loc_ctx* ctx,
40 int priority, const char* file, int line, const char* fn,
41 const char* format, ...) {
42 va_list args;
43
44 va_start(args, format);
45 ctx->log_fn(ctx, priority, file, line, fn, format, args);
46 va_end(args);
47 }
48
49 static void log_stderr(struct loc_ctx* ctx,
50 int priority, const char* file, int line, const char* fn,
51 const char* format, va_list args) {
52 fprintf(stderr, "libloc: %s: ", fn);
53 vfprintf(stderr, format, args);
54 }
55
56 static int log_priority(const char* priority) {
57 char *endptr;
58
59 int prio = strtol(priority, &endptr, 10);
60
61 if (endptr[0] == '\0' || isspace(endptr[0]))
62 return prio;
63
64 if (strncmp(priority, "err", 3) == 0)
65 return LOG_ERR;
66
67 if (strncmp(priority, "info", 4) == 0)
68 return LOG_INFO;
69
70 if (strncmp(priority, "debug", 5) == 0)
71 return LOG_DEBUG;
72
73 return 0;
74 }
75
76 LOC_EXPORT int loc_new(struct loc_ctx** ctx) {
77 struct loc_ctx* c = calloc(1, sizeof(*c));
78 if (!c)
79 return -ENOMEM;
80
81 c->refcount = 1;
82 c->log_fn = log_stderr;
83 c->log_priority = LOG_ERR;
84
85 const char* env = secure_getenv("LOC_LOG");
86 if (env)
87 loc_set_log_priority(c, log_priority(env));
88
89 INFO(c, "ctx %p created\n", c);
90 DEBUG(c, "log_priority=%d\n", c->log_priority);
91 *ctx = c;
92
93 return 0;
94 }
95
96 LOC_EXPORT struct loc_ctx* loc_ref(struct loc_ctx* ctx) {
97 if (!ctx)
98 return NULL;
99
100 ctx->refcount++;
101
102 return ctx;
103 }
104
105 LOC_EXPORT struct loc_ctx* loc_unref(struct loc_ctx* ctx) {
106 if (!ctx)
107 return NULL;
108
109 if (--ctx->refcount > 0)
110 return NULL;
111
112 INFO(ctx, "context %p released\n", ctx);
113 free(ctx);
114
115 return NULL;
116 }
117
118 LOC_EXPORT void loc_set_log_fn(struct loc_ctx* ctx,
119 void (*log_fn)(struct loc_ctx* ctx, int priority, const char* file,
120 int line, const char* fn, const char* format, va_list args)) {
121 ctx->log_fn = log_fn;
122 INFO(ctx, "custom logging function %p registered\n", log_fn);
123 }
124
125 LOC_EXPORT int loc_get_log_priority(struct loc_ctx* ctx) {
126 return ctx->log_priority;
127 }
128
129 LOC_EXPORT void loc_set_log_priority(struct loc_ctx* ctx, int priority) {
130 ctx->log_priority = priority;
131 }
132
133 LOC_EXPORT int loc_parse_address(struct loc_ctx* ctx, const char* string, struct in6_addr* address) {
134 DEBUG(ctx, "Parsing IP address %s\n", string);
135
136 // Try parsing this as an IPv6 address
137 int r = inet_pton(AF_INET6, string, address);
138
139 // If inet_pton returns one it has been successful
140 if (r == 1) {
141 DEBUG(ctx, "%s is an IPv6 address\n", string);
142 return 0;
143 }
144
145 // Try parsing this as an IPv4 address
146 struct in_addr ipv4_address;
147 r = inet_pton(AF_INET, string, &ipv4_address);
148 if (r == 1) {
149 DEBUG(ctx, "%s is an IPv4 address\n", string);
150
151 // Convert to IPv6-mapped address
152 address->s6_addr32[0] = htonl(0x0000);
153 address->s6_addr32[1] = htonl(0x0000);
154 address->s6_addr32[2] = htonl(0xffff);
155 address->s6_addr32[3] = ipv4_address.s_addr;
156
157 return 0;
158 }
159
160 DEBUG(ctx, "%s is not an valid IP address\n", string);
161 return -EINVAL;
162 }