]>
git.ipfire.org Git - people/ms/libloc.git/blob - src/libloc.c
2 libloc - A library to determine the location of someone on the Internet
4 Copyright (C) 2017 IPFire Development Team <info@ipfire.org>
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.
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.
17 #include <arpa/inet.h>
18 #include <netinet/in.h>
28 #include <loc/libloc.h>
29 #include <loc/private.h>
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
);
39 void loc_log(struct loc_ctx
* ctx
,
40 int priority
, const char* file
, int line
, const char* fn
,
41 const char* format
, ...) {
44 va_start(args
, format
);
45 ctx
->log_fn(ctx
, priority
, file
, line
, fn
, format
, args
);
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
);
56 static int log_priority(const char* priority
) {
59 int prio
= strtol(priority
, &endptr
, 10);
61 if (endptr
[0] == '\0' || isspace(endptr
[0]))
64 if (strncmp(priority
, "err", 3) == 0)
67 if (strncmp(priority
, "info", 4) == 0)
70 if (strncmp(priority
, "debug", 5) == 0)
76 LOC_EXPORT
int loc_new(struct loc_ctx
** ctx
) {
77 struct loc_ctx
* c
= calloc(1, sizeof(*c
));
82 c
->log_fn
= log_stderr
;
83 c
->log_priority
= LOG_ERR
;
85 const char* env
= secure_getenv("LOC_LOG");
87 loc_set_log_priority(c
, log_priority(env
));
89 INFO(c
, "ctx %p created\n", c
);
90 DEBUG(c
, "log_priority=%d\n", c
->log_priority
);
96 LOC_EXPORT
struct loc_ctx
* loc_ref(struct loc_ctx
* ctx
) {
105 LOC_EXPORT
struct loc_ctx
* loc_unref(struct loc_ctx
* ctx
) {
109 if (--ctx
->refcount
> 0)
112 INFO(ctx
, "context %p released\n", ctx
);
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
);
125 LOC_EXPORT
int loc_get_log_priority(struct loc_ctx
* ctx
) {
126 return ctx
->log_priority
;
129 LOC_EXPORT
void loc_set_log_priority(struct loc_ctx
* ctx
, int priority
) {
130 ctx
->log_priority
= priority
;
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
);
136 // Try parsing this as an IPv6 address
137 int r
= inet_pton(AF_INET6
, string
, address
);
139 // If inet_pton returns one it has been successful
141 DEBUG(ctx
, "%s is an IPv6 address\n", string
);
145 // Try parsing this as an IPv4 address
146 struct in_addr ipv4_address
;
147 r
= inet_pton(AF_INET
, string
, &ipv4_address
);
149 DEBUG(ctx
, "%s is an IPv4 address\n", string
);
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
;
160 DEBUG(ctx
, "%s is not an valid IP address\n", string
);