]> git.ipfire.org Git - people/ms/libloc.git/blame - src/libloc.c
Refactor parsing IP addresses
[people/ms/libloc.git] / src / libloc.c
CommitLineData
46aded9a
MT
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
2a30e4de
MT
17#include <arpa/inet.h>
18#include <netinet/in.h>
46aded9a
MT
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
c12a1385
MT
28#include <libloc/libloc.h>
29#include <libloc/compat.h>
30#include <libloc/private.h>
46aded9a
MT
31
32struct 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
40void 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
50static 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
57static 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
77LOC_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
97LOC_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
106LOC_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
119LOC_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
126LOC_EXPORT int loc_get_log_priority(struct loc_ctx* ctx) {
127 return ctx->log_priority;
128}
129
130LOC_EXPORT void loc_set_log_priority(struct loc_ctx* ctx, int priority) {
131 ctx->log_priority = priority;
132}