Dillo v3.1.1-46-g8a360e32
Loading...
Searching...
No Matches
dns.c File Reference
#include <assert.h>
#include <netdb.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "msg.h"
#include "dns.h"
#include "list.h"
#include "IO/iowatch.hh"
Include dependency graph for dns.c:

Go to the source code of this file.

Macros

#define D_DNS_MAX_SERVERS   1
 

Enumerations

enum  DnsServerState_t { DNS_SERVER_IDLE , DNS_SERVER_PROCESSING , DNS_SERVER_RESOLVED }
 

Functions

static void Dns_timeout_client (int fd, void *data)
 This function is called on the main thread and reads the DNS results.
 
static void Dns_queue_add (int channel, const char *hostname, DnsCallback_t cb_func, void *cb_data)
 
static int Dns_queue_find (const char *hostname)
 Find hostname index in dns_queue (if found, returns queue index; -1 if not)
 
static void Dns_queue_remove (int index)
 Given an index, remove an entry from the dns_queue.
 
static void Dns_cache_add (char *hostname, Dlist *addr_list)
 Add an IP/hostname pair to Dns-cache.
 
void a_Dns_init (void)
 Initializer function.
 
static void Dns_note_hosts (Dlist *list, struct addrinfo *res0)
 Allocate a host structure and add it to the list.
 
static void * Dns_server (void *data)
 Server function (runs on its own thread)
 
static void Dns_server_req (int channel, const char *hostname)
 Request function (spawn a server and let it handle the request)
 
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_serve_channel (int channel)
 Give answer to all queued callbacks on this channel.
 
static void Dns_assign_channels (void)
 Assign free channels to waiting clients (-2)
 
void a_Dns_freeall (void)
 Dns memory-deallocation.
 
void a_Dns_dillohost_to_string (DilloHost *host, char *dst, size_t size)
 Writes a string representation of the given DilloHost into dst.
 

Variables

static DnsServer dns_server [D_DNS_MAX_SERVERS]
 
static int num_servers
 
static GDnsCache * dns_cache
 
static int dns_cache_size
 
static int dns_cache_size_max
 
static GDnsQueue * dns_queue
 
static int dns_queue_size
 
static int dns_queue_size_max
 
static int dns_notify_pipe [2]
 

Macro Definition Documentation

◆ D_DNS_MAX_SERVERS

#define D_DNS_MAX_SERVERS   1

Definition at line 50 of file dns.c.

Enumeration Type Documentation

◆ DnsServerState_t

Enumerator
DNS_SERVER_IDLE 
DNS_SERVER_PROCESSING 
DNS_SERVER_RESOLVED 

Definition at line 53 of file dns.c.

Function Documentation

◆ a_Dns_dillohost_to_string()

void a_Dns_dillohost_to_string ( DilloHost host,
char *  dst,
size_t  size 
)

Writes a string representation of the given DilloHost into dst.

dst will be \0 terminated. Please note that dst must be at least 40 bytes long for IPv6 addresses.

Definition at line 502 of file dns.c.

References DilloHost::af, and DilloHost::data.

Referenced by Dns_server().

◆ a_Dns_freeall()

void a_Dns_freeall ( void  )

Dns memory-deallocation.

(Call this one at exit time) The Dns_queue is deallocated at execution time (no need to do that here) 'dns_cache' is the only one that grows dynamically

Definition at line 480 of file dns.c.

References a_IOwatch_remove_fd(), dClose(), dFree(), DIO_READ, dList_free(), dList_length(), dList_nth_data(), dns_cache, dns_cache_size, and dns_notify_pipe.

Referenced by main().

◆ a_Dns_init()

◆ a_Dns_resolve()

void a_Dns_resolve ( const char *  hostname,
DnsCallback_t  cb_func,
void *  cb_data 
)

Return the IP for the given hostname using a callback.

Side effect: a thread is spawned when hostname is not cached.

Definition at line 361 of file dns.c.

References dns_cache, dns_cache_size, dns_queue, Dns_queue_add(), Dns_queue_find(), dns_server, DNS_SERVER_IDLE, Dns_server_req(), dStrAsciiCasecmp(), and num_servers.

Referenced by Http_get().

◆ Dns_assign_channels()

static void Dns_assign_channels ( void  )
static

Assign free channels to waiting clients (-2)

Definition at line 420 of file dns.c.

References dns_queue, dns_queue_size, dns_server, DNS_SERVER_IDLE, Dns_server_req(), dStrAsciiCasecmp(), and num_servers.

Referenced by Dns_timeout_client().

◆ Dns_cache_add()

static void Dns_cache_add ( char *  hostname,
Dlist addr_list 
)
static

Add an IP/hostname pair to Dns-cache.

Definition at line 164 of file dns.c.

References _MSG, a_List_add, dns_cache, dns_cache_size, dns_cache_size_max, and dStrdup().

Referenced by Dns_timeout_client().

◆ Dns_note_hosts()

static void Dns_note_hosts ( Dlist list,
struct addrinfo *  res0 
)
static

Allocate a host structure and add it to the list.

Definition at line 219 of file dns.c.

References DilloHost::af, DilloHost::alen, DilloHost::data, dList_append(), and dNew0.

Referenced by Dns_server().

◆ Dns_queue_add()

static void Dns_queue_add ( int  channel,
const char *  hostname,
DnsCallback_t  cb_func,
void *  cb_data 
)
static

Definition at line 103 of file dns.c.

References a_List_add, dns_queue, dns_queue_size, dns_queue_size_max, and dStrdup().

Referenced by a_Dns_resolve().

◆ Dns_queue_find()

static int Dns_queue_find ( const char *  hostname)
static

Find hostname index in dns_queue (if found, returns queue index; -1 if not)

Definition at line 118 of file dns.c.

References dns_queue, dns_queue_size, and dStrAsciiCasecmp().

Referenced by a_Dns_resolve().

◆ Dns_queue_remove()

static void Dns_queue_remove ( int  index)
static

Given an index, remove an entry from the dns_queue.

Definition at line 132 of file dns.c.

References _MSG, dFree(), dns_queue, and dns_queue_size.

Referenced by Dns_serve_channel().

◆ Dns_serve_channel()

static void Dns_serve_channel ( int  channel)
static

Give answer to all queued callbacks on this channel.

Definition at line 402 of file dns.c.

References dns_queue, Dns_queue_remove(), dns_queue_size, and dns_server.

Referenced by Dns_timeout_client().

◆ Dns_server()

static void * Dns_server ( void *  data)
static

Server function (runs on its own thread)

Definition at line 265 of file dns.c.

References _MSG, a_Dns_dillohost_to_string(), dList_free(), dList_length(), dList_new(), dList_nth_data(), Dns_note_hosts(), dns_notify_pipe, dns_server, DNS_SERVER_RESOLVED, error(), MSG, and VOIDP2INT.

Referenced by Dns_server_req().

◆ Dns_server_req()

static void Dns_server_req ( int  channel,
const char *  hostname 
)
static

Request function (spawn a server and let it handle the request)

Definition at line 330 of file dns.c.

References dFree(), Dns_server(), dns_server, DNS_SERVER_PROCESSING, dStrdup(), and INT2VOIDP.

Referenced by a_Dns_resolve(), and Dns_assign_channels().

◆ Dns_timeout_client()

static void Dns_timeout_client ( int  fd,
void *  data 
)
static

This function is called on the main thread and reads the DNS results.

Definition at line 451 of file dns.c.

References Dns_assign_channels(), Dns_cache_add(), dns_notify_pipe, Dns_serve_channel(), dns_server, DNS_SERVER_IDLE, DNS_SERVER_RESOLVED, and num_servers.

Referenced by a_Dns_init().

Variable Documentation

◆ dns_cache

GDnsCache* dns_cache
static

Definition at line 93 of file dns.c.

Referenced by a_Dns_freeall(), a_Dns_init(), a_Dns_resolve(), and Dns_cache_add().

◆ dns_cache_size

int dns_cache_size
static

Definition at line 94 of file dns.c.

Referenced by a_Dns_freeall(), a_Dns_init(), a_Dns_resolve(), and Dns_cache_add().

◆ dns_cache_size_max

int dns_cache_size_max
static

Definition at line 94 of file dns.c.

Referenced by a_Dns_init(), and Dns_cache_add().

◆ dns_notify_pipe

int dns_notify_pipe[2]
static

Definition at line 97 of file dns.c.

Referenced by a_Dns_freeall(), a_Dns_init(), Dns_server(), and Dns_timeout_client().

◆ dns_queue

GDnsQueue* dns_queue
static

◆ dns_queue_size

int dns_queue_size
static

◆ dns_queue_size_max

int dns_queue_size_max
static

Definition at line 96 of file dns.c.

Referenced by a_Dns_init(), and Dns_queue_add().

◆ dns_server

DnsServer dns_server[D_DNS_MAX_SERVERS]
static

◆ num_servers

int num_servers
static

Definition at line 92 of file dns.c.

Referenced by a_Dns_init(), a_Dns_resolve(), Dns_assign_channels(), and Dns_timeout_client().