forked from allinurl/goaccess
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gdns.c
244 lines (211 loc) · 5.38 KB
/
gdns.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
/**
* gdns.c -- hosts resolver
* Copyright (C) 2009-2013 by Gerardo Orellana <[email protected]>
* GoAccess - An Ncurses apache weblog analyzer & interactive viewer
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of
* the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* A copy of the GNU General Public License is attached to this
* source distribution for its full text.
*
* Visit http://goaccess.prosoftcorp.com for new releases.
*/
#define _MULTI_THREADED
#ifdef __FreeBSD__
#include <sys/socket.h>
#endif
#include <pthread.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <ctype.h>
#include <errno.h>
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <time.h>
#include <unistd.h>
#include "gdns.h"
#include "error.h"
#include "xmalloc.h"
#include "goaccess.h"
#include "parser.h"
#include "util.h"
GDnsThread gdns_thread;
static GDnsQueue *gdns_queue;
/* initialize queue */
void
gqueue_init (GDnsQueue * q, int capacity)
{
q->head = 0;
q->tail = -1;
q->size = 0;
q->capacity = capacity;
}
/* get current size of queue */
int
gqueue_size (GDnsQueue * q)
{
return q->size;
}
/* is the queue empty */
int
gqueue_empty (GDnsQueue * q)
{
return q->size == 0;
}
/* is the queue full */
int
gqueue_full (GDnsQueue * q)
{
return q->size == q->capacity;
}
/* destroy queue */
void
gqueue_destroy (GDnsQueue * q)
{
free (q);
}
/* add item to the queue */
int
gqueue_enqueue (GDnsQueue * q, char *item)
{
if (gqueue_full (q))
return -1;
q->tail = (q->tail + 1) % q->capacity;
strcpy (q->buffer[q->tail], item);
q->size++;
return 0;
}
int
gqueue_find (GDnsQueue * q, const char *item)
{
if (gqueue_empty (q))
return 0;
int i;
for (i = 0; i < q->size; i++) {
if (strcmp (item, q->buffer[i]) == 0)
return 1;
}
return 0;
}
/* remove an item from the queue */
char *
gqueue_dequeue (GDnsQueue * q)
{
if (gqueue_empty (q))
return NULL;
char *item = q->buffer[q->head];
q->head = (q->head + 1) % q->capacity;
q->size--;
return item;
}
/* get the corresponding host given an address */
char *
reverse_host (const struct sockaddr *a, socklen_t length)
{
char h[H_SIZE];
int flags, st;
flags = NI_NAMEREQD;
st = getnameinfo (a, length, h, H_SIZE, NULL, 0, flags);
if (!st)
return alloc_string (h);
return alloc_string (gai_strerror (st));
}
/* determine if IPv4 or IPv6 and resolve */
char *
reverse_ip (char *str)
{
if (str == NULL || *str == '\0')
return NULL;
union
{
struct sockaddr addr;
struct sockaddr_in6 addr6;
struct sockaddr_in addr4;
} a;
memset (&a, 0, sizeof (a));
if (1 == inet_pton (AF_INET, str, &a.addr4.sin_addr)) {
a.addr4.sin_family = AF_INET;
return reverse_host (&a.addr, sizeof (a.addr4));
} else if (1 == inet_pton (AF_INET6, str, &a.addr6.sin6_addr)) {
a.addr6.sin6_family = AF_INET6;
return reverse_host (&a.addr, sizeof (a.addr6));
}
return NULL;
}
/* producer */
void
dns_resolver (char *addr)
{
pthread_mutex_lock (&gdns_thread.mutex);
if (!gqueue_full (gdns_queue) && !gqueue_find (gdns_queue, addr)) {
g_hash_table_replace (ht_hostnames, g_strdup (addr), NULL);
gqueue_enqueue (gdns_queue, addr);
}
pthread_mutex_unlock (&gdns_thread.mutex);
}
/* consumer */
void
dns_worker (void GO_UNUSED (*ptr_data))
{
char *ip = NULL, *host = NULL;
while (1) {
pthread_mutex_lock (&gdns_thread.mutex);
/* wait until an item has been added to the queue */
while (gqueue_empty (gdns_queue))
pthread_cond_wait (&gdns_thread.not_empty, &gdns_thread.mutex);
ip = gqueue_dequeue (gdns_queue);
pthread_mutex_unlock (&gdns_thread.mutex);
host = reverse_ip (ip);
pthread_mutex_lock (&gdns_thread.mutex);
if (!active_gdns) {
if (host)
free (host);
break;
}
if (host != NULL && active_gdns) {
g_hash_table_replace (ht_hostnames, g_strdup (ip), host);
allocate_hosts_holder (ip);
}
pthread_cond_signal (&gdns_thread.not_full);
pthread_mutex_unlock (&gdns_thread.mutex);
}
}
/* init queue and dns thread */
void
gdns_init (void)
{
gdns_queue = xmalloc (sizeof (GDnsQueue));
gqueue_init (gdns_queue, QUEUE_SIZE);
if (pthread_cond_init (&(gdns_thread.not_empty), NULL))
error_handler (__PRETTY_FUNCTION__, __FILE__, __LINE__, "Failed cond");
if (pthread_cond_init (&(gdns_thread.not_full), NULL))
error_handler (__PRETTY_FUNCTION__, __FILE__, __LINE__, "Failed cond");
if (pthread_mutex_init (&(gdns_thread.mutex), NULL))
error_handler (__PRETTY_FUNCTION__, __FILE__, __LINE__, "Failed mutex");
}
/* destroy queue */
void
gdns_free_queue (void)
{
gqueue_destroy (gdns_queue);
}
/* create a dns thread */
void
gdns_thread_create ()
{
pthread_create (&(gdns_thread.thread), NULL, (void *) &dns_worker, NULL);
pthread_detach (gdns_thread.thread);
}