30#include <sys/socket.h>
32#include <netinet/in.h>
48# define D_DNS_MAX_SERVERS 4
50# define D_DNS_MAX_SERVERS 1
136 _MSG(
"Dns_queue_remove: deleting client [%d] [queue_size=%d]\n",
182 MSG(
"dillo_dns_init: Here we go! (threaded)\n");
184 MSG(
"dillo_dns_init: Here we go! (not threaded)\n");
221 struct addrinfo *res;
224 for (res = res0; res; res = res->ai_next) {
226 if (res->ai_family == AF_INET) {
227 struct sockaddr_in *in_addr;
229 if (res->ai_addrlen <
sizeof(
struct sockaddr_in)) {
236 in_addr = (
struct sockaddr_in*) res->ai_addr;
237 dh->
alen =
sizeof (
struct in_addr);
238 memcpy(&dh->
data[0], &in_addr->sin_addr.s_addr, dh->
alen);
242 }
else if (res->ai_family == AF_INET6) {
243 struct sockaddr_in6 *in6_addr;
245 if (res->ai_addrlen <
sizeof(
struct sockaddr_in6)) {
252 in6_addr = (
struct sockaddr_in6*) res->ai_addr;
253 dh->
alen =
sizeof (
struct in6_addr);
254 memcpy(&dh->
data[0], &in6_addr->sin6_addr.s6_addr, dh->
alen);
268 struct addrinfo hints, *res0;
272 char addr_string[40];
274 memset(&hints, 0,
sizeof(hints));
276 hints.ai_family = AF_UNSPEC;
278 hints.ai_family = AF_INET;
280 hints.ai_socktype = SOCK_STREAM;
284 _MSG(
"Dns_server: starting...\n ch: %d host: %s\n",
287 error = getaddrinfo(
dns_server[channel].hostname, NULL, &hints, &res0);
291 MSG(
"DNS error: %s\n", gai_strerror(
error));
306 MSG(
"Dns_server [%d]: %s is", channel,
309 for (i = 0; i < length; i++) {
311 addr_string,
sizeof(addr_string));
312 MSG(
" %s", addr_string);
333 static pthread_attr_t thrATTR;
334 static int thrATTRInitialized = 0;
344 if (!thrATTRInitialized) {
345 pthread_attr_init(&thrATTR);
346 pthread_attr_setdetachstate(&thrATTR, PTHREAD_CREATE_DETACHED);
347 thrATTRInitialized = 1;
375 cb_func(0,
dns_cache[i].addr_list, cb_data);
385 for (channel = 0; channel <
num_servers; channel++)
409 dns_queue[i].cb_func(srv->status, srv->addr_list,
462 if (srv->addr_list != NULL) {
504 if (!inet_ntop(host->
af, host->
data, dst, size)) {
507 snprintf(dst, size,
"Unknown address family");
510 snprintf(dst, size,
"Buffer too small");
int dStrAsciiCasecmp(const char *s1, const char *s2)
char * dStrdup(const char *s)
Dlist * dList_new(int size)
Create a new empty list.
int dList_length(Dlist *lp)
For completing the ADT.
void * dList_nth_data(Dlist *lp, int n0)
Return the nth data item, NULL when not found or 'n0' is out of range.
int dClose(int fd)
Close a FD handling EINTR.
void dList_append(Dlist *lp, void *data)
Append a data item to the list.
void dList_free(Dlist *lp)
Free a list (not its elements)
#define dNew0(type, count)
#define dNew(type, count)
static int dns_cache_size_max
static int dns_queue_size
void a_Dns_dillohost_to_string(DilloHost *host, char *dst, size_t size)
Writes a string representation of the given DilloHost into dst.
static DnsServer dns_server[D_DNS_MAX_SERVERS]
static void Dns_serve_channel(int channel)
Give answer to all queued callbacks on this channel.
static void * Dns_server(void *data)
Server function (runs on its own thread)
void a_Dns_init(void)
Initializer function.
static void Dns_queue_add(int channel, const char *hostname, DnsCallback_t cb_func, void *cb_data)
static int dns_queue_size_max
void a_Dns_freeall(void)
Dns memory-deallocation.
static void Dns_queue_remove(int index)
Given an index, remove an entry from the dns_queue.
static void Dns_note_hosts(Dlist *list, struct addrinfo *res0)
Allocate a host structure and add it to the list.
static void Dns_server_req(int channel, const char *hostname)
Request function (spawn a server and let it handle the request)
static GDnsCache * dns_cache
static void Dns_assign_channels(void)
Assign free channels to waiting clients (-2)
static int Dns_queue_find(const char *hostname)
Find hostname index in dns_queue (if found, returns queue index; -1 if not)
#define D_DNS_MAX_SERVERS
static int dns_cache_size
static int dns_notify_pipe[2]
void a_Dns_resolve(const char *hostname, DnsCallback_t cb_func, void *cb_data)
Return the IP for the given hostname using a callback.
static void Dns_cache_add(char *hostname, Dlist *addr_list)
Add an IP/hostname pair to Dns-cache.
static GDnsQueue * dns_queue
static void Dns_timeout_client(int fd, void *data)
This function is called on the main thread and reads the DNS results.
void(* DnsCallback_t)(int status, Dlist *addr_list, void *data)
#define a_List_add(list, num_items, alloc_step)
static void error(char *msg)
void a_IOwatch_add_fd(int fd, int when, Fl_FD_Handler Callback, void *usr_data=0)
Hook a Callback for a certain activities in a FD.
void a_IOwatch_remove_fd(int fd, int when)
Remove a Callback for a given FD (or just remove some events)
char data[DILLO_ADDR_MAX]