src/dns.c File Reference

#include "main.h"
#include <netdb.h>
#include <setjmp.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include "dns.h"
Include dependency graph for dns.c:

Go to the source code of this file.

Functions

void dcc_dnswait (int idx, char *buf, int len)
void eof_dcc_dnswait (int idx)
static void display_dcc_dnswait (int idx, char *buf)
static int expmem_dcc_dnswait (void *x)
static void kill_dcc_dnswait (int idx, void *x)
static void dns_dcchostbyip (IP ip, char *hostn, int ok, void *other)
static void dns_dccipbyhost (IP ip, char *hostn, int ok, void *other)
static int dns_dccexpmem (void *other)
void dcc_dnsipbyhost (char *hostn)
void dcc_dnshostbyip (IP ip)
void dnstclcallback (char *context, char *script, int code, const char *result, int dofree)
static void dns_tcl_iporhostres (IP ip, char *hostn, int ok, void *other)
static int dns_tclexpmem (void *other)
static void tcl_dnsipbyhost (char *hostn, char *proc, char *paras)
static void tcl_dnshostbyip (IP ip, char *proc, char *paras)
static int dnsevent_expmem (void)
void call_hostbyip (IP ip, char *hostn, int ok)
void call_ipbyhost (char *hostn, IP ip, int ok)
void block_dns_hostbyip (IP ip)
void block_dns_ipbyhost (char *host)
int expmem_dns (void)

Variables

struct dcc_tdcc
int dcc_total
int resolve_timeout
time_t now
sigjmp_buf alarmret
Tcl_Interpinterp
devent_tdns_events = 0
struct dcc_table DCC_DNSWAIT
devent_type DNS_DCCEVENT_HOSTBYIP
devent_type DNS_DCCEVENT_IPBYHOST
devent_type DNS_TCLEVENT_HOSTBYIP
devent_type DNS_TCLEVENT_IPBYHOST

Function Documentation

void block_dns_hostbyip ( IP  ip  ) 

Definition at line 458 of file dns.c.

References alarmret, call_hostbyip, iptostr, NULL, resolve_timeout, strncpyz, and UHOSTLEN.

00459 {
00460   struct hostent *hp;
00461   unsigned long addr = htonl(ip);
00462   static char s[UHOSTLEN];
00463 
00464   if (!sigsetjmp(alarmret, 1)) {
00465     alarm(resolve_timeout);
00466     hp = gethostbyaddr((char *) &addr, sizeof(addr), AF_INET);
00467     alarm(0);
00468     if (hp)
00469       strncpyz(s, hp->h_name, sizeof s);
00470     else
00471       strcpy(s, iptostr(addr));
00472   } else {
00473     hp = NULL;
00474     strcpy(s, iptostr(addr));
00475   }
00476   /* Call hooks. */
00477   call_hostbyip(ip, s, hp ? 1 : 0);
00478 }

void block_dns_ipbyhost ( char *  host  ) 

Definition at line 480 of file dns.c.

References alarmret, call_ipbyhost, egg_inet_aton, and resolve_timeout.

00481 {
00482   struct in_addr inaddr;
00483 
00484   /* Check if someone passed us an IP address as hostname
00485    * and return it straight away */
00486   if (egg_inet_aton(host, &inaddr)) {
00487     call_ipbyhost(host, ntohl(inaddr.s_addr), 1);
00488     return;
00489   }
00490   if (!sigsetjmp(alarmret, 1)) {
00491     struct hostent *hp;
00492     struct in_addr *in;
00493     IP ip = 0;
00494 
00495     alarm(resolve_timeout);
00496     hp = gethostbyname(host);
00497     alarm(0);
00498 
00499     if (hp) {
00500       in = (struct in_addr *) (hp->h_addr_list[0]);
00501       ip = (IP) (in->s_addr);
00502       call_ipbyhost(host, ntohl(ip), 1);
00503       return;
00504     }
00505     /* Fall through. */
00506   }
00507   call_ipbyhost(host, 0, 0);
00508 }

void call_hostbyip ( IP  ip,
char *  hostn,
int  ok 
)

Definition at line 394 of file dns.c.

References devent_type::event, devent_str::ip_addr, LOG_MISC, devent_str::lookup, devent_type::name, devent_str::next, nfree, NULL, devent_str::other, putlog, devent_str::res_data, RES_HOSTBYIP, and devent_str::type.

00395 {
00396   devent_t *de = dns_events, *ode = NULL, *nde = NULL;
00397 
00398   while (de) {
00399     nde = de->next;
00400     if ((de->lookup == RES_HOSTBYIP) &&
00401         (!de->res_data.ip_addr || (de->res_data.ip_addr == ip))) {
00402       /* Remove the event from the list here, to avoid conflicts if one of
00403        * the event handlers re-adds another event. */
00404       if (ode)
00405         ode->next = de->next;
00406       else
00407         dns_events = de->next;
00408 
00409       if (de->type && de->type->event)
00410         de->type->event(ip, hostn, ok, de->other);
00411       else
00412         putlog(LOG_MISC, "*", "(!) Unknown DNS event type found: %s",
00413                (de->type && de->type->name) ? de->type->name : "<empty>");
00414       nfree(de);
00415       de = ode;
00416     }
00417     ode = de;
00418     de = nde;
00419   }
00420 }

void call_ipbyhost ( char *  hostn,
IP  ip,
int  ok 
)

Definition at line 422 of file dns.c.

References egg_strcasecmp, devent_type::event, devent_str::hostname, LOG_MISC, devent_str::lookup, devent_type::name, devent_str::next, nfree, NULL, devent_str::other, putlog, devent_str::res_data, RES_IPBYHOST, and devent_str::type.

00423 {
00424   devent_t *de = dns_events, *ode = NULL, *nde = NULL;
00425 
00426   while (de) {
00427     nde = de->next;
00428     if ((de->lookup == RES_IPBYHOST) && (!de->res_data.hostname ||
00429         !egg_strcasecmp(de->res_data.hostname, hostn))) {
00430       /* Remove the event from the list here, to avoid conflicts if one of
00431        * the event handlers re-adds another event. */
00432       if (ode)
00433         ode->next = de->next;
00434       else
00435         dns_events = de->next;
00436 
00437       if (de->type && de->type->event)
00438         de->type->event(ip, hostn, ok, de->other);
00439       else
00440         putlog(LOG_MISC, "*", "(!) Unknown DNS event type found: %s",
00441                (de->type && de->type->name) ? de->type->name : "<empty>");
00442 
00443       if (de->res_data.hostname)
00444         nfree(de->res_data.hostname);
00445       nfree(de);
00446       de = ode;
00447     }
00448     ode = de;
00449     de = nde;
00450   }
00451 }

void dcc_dnshostbyip ( IP  ip  ) 

Definition at line 206 of file dns.c.

References dns_hostbyip, egg_bzero, devent_str::ip_addr, devent_str::lookup, devent_str::next, nmalloc, devent_str::res_data, RES_HOSTBYIP, and devent_str::type.

00207 {
00208   devent_t *de;
00209 
00210   for (de = dns_events; de; de = de->next) {
00211     if (de->type && (de->type == &DNS_DCCEVENT_HOSTBYIP) &&
00212         (de->lookup == RES_HOSTBYIP)) {
00213       if (de->res_data.ip_addr == ip)
00214         /* No need to add anymore. */
00215         return;
00216     }
00217   }
00218 
00219   de = nmalloc(sizeof(devent_t));
00220   egg_bzero(de, sizeof(devent_t));
00221 
00222   /* Link into list. */
00223   de->next = dns_events;
00224   dns_events = de;
00225 
00226   de->type = &DNS_DCCEVENT_HOSTBYIP;
00227   de->lookup = RES_HOSTBYIP;
00228   de->res_data.ip_addr = ip;
00229 
00230   /* Send request. */
00231   dns_hostbyip(ip);
00232 }

void dcc_dnsipbyhost ( char *  hostn  ) 

Definition at line 176 of file dns.c.

References dns_ipbyhost, egg_bzero, egg_strcasecmp, devent_str::hostname, devent_str::lookup, devent_str::next, nmalloc, devent_str::res_data, RES_IPBYHOST, and devent_str::type.

00177 {
00178   devent_t *de;
00179 
00180   for (de = dns_events; de; de = de->next) {
00181     if (de->type && (de->type == &DNS_DCCEVENT_IPBYHOST) &&
00182         (de->lookup == RES_IPBYHOST)) {
00183       if (de->res_data.hostname &&
00184           !egg_strcasecmp(de->res_data.hostname, hostn))
00185         /* No need to add anymore. */
00186         return;
00187     }
00188   }
00189 
00190   de = nmalloc(sizeof(devent_t));
00191   egg_bzero(de, sizeof(devent_t));
00192 
00193   /* Link into list. */
00194   de->next = dns_events;
00195   dns_events = de;
00196 
00197   de->type = &DNS_DCCEVENT_IPBYHOST;
00198   de->lookup = RES_IPBYHOST;
00199   de->res_data.hostname = nmalloc(strlen(hostn) + 1);
00200   strcpy(de->res_data.hostname, hostn);
00201 
00202   /* Send request. */
00203   dns_ipbyhost(hostn);
00204 }

void dcc_dnswait ( int  idx,
char *  buf,
int  len 
)

Definition at line 52 of file dns.c.

00053 {
00054   /* Ignore anything now. */
00055 }

static void display_dcc_dnswait ( int  idx,
char *  buf 
) [static]

Definition at line 65 of file dns.c.

References now, and dcc_t::timeval.

00066 {
00067   sprintf(buf, "dns   waited %lis", (long) now - dcc[idx].timeval);
00068 }

static int dns_dccexpmem ( void *  other  )  [static]

Definition at line 159 of file dns.c.

00160 {
00161   return 0;
00162 }

static void dns_dcchostbyip ( IP  ip,
char *  hostn,
int  ok,
void *  other 
) [static]

Definition at line 119 of file dns.c.

References dcc_total, dcc_t::dns, dns_info::dns_failure, dns_info::dns_success, dns_info::dns_type, get_data_ptr, dns_info::host, dns_info::ip, nfree, RES_HOSTBYIP, and dcc_t::u.

00120 {
00121   int idx;
00122 
00123   for (idx = 0; idx < dcc_total; idx++) {
00124     if ((dcc[idx].type == &DCC_DNSWAIT) &&
00125         (dcc[idx].u.dns->dns_type == RES_HOSTBYIP) &&
00126         (dcc[idx].u.dns->ip == ip)) {
00127       if (dcc[idx].u.dns->host)
00128         nfree(dcc[idx].u.dns->host);
00129       dcc[idx].u.dns->host = get_data_ptr(strlen(hostn) + 1);
00130       strcpy(dcc[idx].u.dns->host, hostn);
00131       if (ok)
00132         dcc[idx].u.dns->dns_success(idx);
00133       else
00134         dcc[idx].u.dns->dns_failure(idx);
00135     }
00136   }
00137 }

static void dns_dccipbyhost ( IP  ip,
char *  hostn,
int  ok,
void *  other 
) [static]

Definition at line 142 of file dns.c.

References dcc_total, dcc_t::dns, dns_info::dns_failure, dns_info::dns_success, dns_info::dns_type, egg_strcasecmp, dns_info::host, dns_info::ip, RES_IPBYHOST, and dcc_t::u.

00143 {
00144   int idx;
00145 
00146   for (idx = 0; idx < dcc_total; idx++) {
00147     if ((dcc[idx].type == &DCC_DNSWAIT) &&
00148         (dcc[idx].u.dns->dns_type == RES_IPBYHOST) &&
00149         !egg_strcasecmp(dcc[idx].u.dns->host, hostn)) {
00150       dcc[idx].u.dns->ip = ip;
00151       if (ok)
00152         dcc[idx].u.dns->dns_success(idx);
00153       else
00154         dcc[idx].u.dns->dns_failure(idx);
00155     }
00156   }
00157 }

static void dns_tcl_iporhostres ( IP  ip,
char *  hostn,
int  ok,
void *  other 
) [static]

Definition at line 252 of file dns.c.

References argv, dnstclcallback(), do_tcl_async(), EGG_CONST, iptostr, nfree, dcc_table::output, devent_tclinfo_t::paras, devent_tclinfo_t::proc, and Tcl_Free.

00253 {
00254   devent_tclinfo_t *tclinfo = (devent_tclinfo_t *) other;
00255   Tcl_DString list;
00256 
00257   Tcl_DStringInit(&list);
00258   Tcl_DStringAppendElement(&list, tclinfo->proc);
00259   Tcl_DStringAppendElement(&list, iptostr(htonl(ip)));
00260   Tcl_DStringAppendElement(&list, hostn);
00261   Tcl_DStringAppendElement(&list, ok ? "1" : "0");
00262 
00263   if (tclinfo->paras) {
00264     EGG_CONST char *argv[2];
00265     char *output;
00266 
00267     argv[0] = Tcl_DStringValue(&list);
00268     argv[1] = tclinfo->paras;
00269     output = Tcl_Concat(2, argv);
00270 
00271     do_tcl_async(tclinfo->proc, output, dnstclcallback);
00272     Tcl_Free(output);
00273   } else
00274     do_tcl_async(tclinfo->proc, Tcl_DStringValue(&list), dnstclcallback);
00275 
00276   Tcl_DStringFree(&list);
00277 
00278   nfree(tclinfo->proc);
00279   if (tclinfo->paras)
00280     nfree(tclinfo->paras);
00281   nfree(tclinfo);
00282 }

Here is the call graph for this function:

static int dns_tclexpmem ( void *  other  )  [static]

Definition at line 284 of file dns.c.

References devent_tclinfo_t::paras, and devent_tclinfo_t::proc.

00285 {
00286   devent_tclinfo_t *tclinfo = (devent_tclinfo_t *) other;
00287   int l = 0;
00288 
00289   if (tclinfo) {
00290     l = sizeof(devent_tclinfo_t);
00291     if (tclinfo->proc)
00292       l += strlen(tclinfo->proc) + 1;
00293     if (tclinfo->paras)
00294       l += strlen(tclinfo->paras) + 1;
00295   }
00296   return l;
00297 }

static int dnsevent_expmem ( void   )  [inline, static]

Definition at line 379 of file dns.c.

References devent_type::expmem, devent_str::hostname, devent_str::lookup, devent_str::next, devent_str::other, devent_str::res_data, RES_IPBYHOST, and devent_str::type.

Referenced by expmem_dns().

00380 {
00381   devent_t *de;
00382   int tot = 0;
00383 
00384   for (de = dns_events; de; de = de->next) {
00385     tot += sizeof(devent_t);
00386     if ((de->lookup == RES_IPBYHOST) && de->res_data.hostname)
00387       tot += strlen(de->res_data.hostname) + 1;
00388     if (de->type && de->type->expmem)
00389       tot += de->type->expmem(de->other);
00390   }
00391   return tot;
00392 }

Here is the caller graph for this function:

void dnstclcallback ( char *  context,
char *  script,
int  code,
const char *  result,
int  dofree 
)

Definition at line 241 of file dns.c.

References DCC_TCLERROR, LOG_MISC, nfree, and putlog.

Referenced by dns_tcl_iporhostres().

00242                                                     {
00243   if (code == TCL_ERROR)
00244     putlog(LOG_MISC, "*", DCC_TCLERROR, context, result);
00245   if (dofree) {
00246     nfree(context);
00247     nfree(script);
00248   }
00249 }

Here is the caller graph for this function:

void eof_dcc_dnswait ( int  idx  ) 

Definition at line 57 of file dns.c.

References dcc_t::addr, iptostr, killsock, LOG_MISC, lostdcc, dcc_t::port, putlog, and dcc_t::sock.

00058 {
00059   putlog(LOG_MISC, "*", "Lost connection while resolving hostname [%s/%d]",
00060          iptostr(htonl(dcc[idx].addr)), dcc[idx].port);
00061   killsock(dcc[idx].sock);
00062   lostdcc(idx);
00063 }

static int expmem_dcc_dnswait ( void *  x  )  [static]

Definition at line 70 of file dns.c.

References dns_info::cbuf, and dns_info::host.

00071 {
00072   register struct dns_info *p = (struct dns_info *) x;
00073   int size = 0;
00074 
00075   if (p) {
00076     size = sizeof(struct dns_info);
00077     if (p->host)
00078       size += strlen(p->host) + 1;
00079     if (p->cbuf)
00080       size += strlen(p->cbuf) + 1;
00081   }
00082   return size;
00083 }

int expmem_dns ( void   ) 

Definition at line 515 of file dns.c.

References dnsevent_expmem().

Referenced by debug_mem_to_dcc().

00516 {
00517   return dnsevent_expmem();
00518 }

Here is the call graph for this function:

Here is the caller graph for this function:

static void kill_dcc_dnswait ( int  idx,
void *  x 
) [static]

Definition at line 85 of file dns.c.

References dns_info::cbuf, dns_info::host, and nfree.

00086 {
00087   register struct dns_info *p = (struct dns_info *) x;
00088 
00089   if (p) {
00090     if (p->host)
00091       nfree(p->host);
00092     if (p->cbuf)
00093       nfree(p->cbuf);
00094     nfree(p);
00095   }
00096 }

static void tcl_dnshostbyip ( IP  ip,
char *  proc,
char *  paras 
) [static]

Definition at line 343 of file dns.c.

References dns_hostbyip, egg_bzero, devent_str::ip_addr, devent_str::lookup, devent_str::next, nmalloc, NULL, devent_str::other, devent_tclinfo_t::paras, devent_tclinfo_t::proc, devent_str::res_data, RES_HOSTBYIP, and devent_str::type.

00344 {
00345   devent_t *de;
00346   devent_tclinfo_t *tclinfo;
00347 
00348   de = nmalloc(sizeof(devent_t));
00349   egg_bzero(de, sizeof(devent_t));
00350 
00351   /* Link into list. */
00352   de->next = dns_events;
00353   dns_events = de;
00354 
00355   de->type = &DNS_TCLEVENT_HOSTBYIP;
00356   de->lookup = RES_HOSTBYIP;
00357   de->res_data.ip_addr = ip;
00358 
00359   /* Store additional data. */
00360   tclinfo = nmalloc(sizeof(devent_tclinfo_t));
00361   tclinfo->proc = nmalloc(strlen(proc) + 1);
00362   strcpy(tclinfo->proc, proc);
00363   if (paras) {
00364     tclinfo->paras = nmalloc(strlen(paras) + 1);
00365     strcpy(tclinfo->paras, paras);
00366   } else
00367     tclinfo->paras = NULL;
00368   de->other = tclinfo;
00369 
00370   /* Send request. */
00371   dns_hostbyip(ip);
00372 }

static void tcl_dnsipbyhost ( char *  hostn,
char *  proc,
char *  paras 
) [static]

Definition at line 311 of file dns.c.

References dns_ipbyhost, egg_bzero, devent_str::hostname, devent_str::lookup, devent_str::next, nmalloc, NULL, devent_str::other, devent_tclinfo_t::paras, devent_tclinfo_t::proc, devent_str::res_data, RES_IPBYHOST, and devent_str::type.

00312 {
00313   devent_t *de;
00314   devent_tclinfo_t *tclinfo;
00315 
00316   de = nmalloc(sizeof(devent_t));
00317   egg_bzero(de, sizeof(devent_t));
00318 
00319   /* Link into list. */
00320   de->next = dns_events;
00321   dns_events = de;
00322 
00323   de->type = &DNS_TCLEVENT_IPBYHOST;
00324   de->lookup = RES_IPBYHOST;
00325   de->res_data.hostname = nmalloc(strlen(hostn) + 1);
00326   strcpy(de->res_data.hostname, hostn);
00327 
00328   /* Store additional data. */
00329   tclinfo = nmalloc(sizeof(devent_tclinfo_t));
00330   tclinfo->proc = nmalloc(strlen(proc) + 1);
00331   strcpy(tclinfo->proc, proc);
00332   if (paras) {
00333     tclinfo->paras = nmalloc(strlen(paras) + 1);
00334     strcpy(tclinfo->paras, paras);
00335   } else
00336     tclinfo->paras = NULL;
00337   de->other = tclinfo;
00338 
00339   /* Send request. */
00340   dns_ipbyhost(hostn);
00341 }


Variable Documentation

sigjmp_buf alarmret

Definition at line 69 of file net.c.

Referenced by block_dns_hostbyip(), block_dns_ipbyhost(), and got_alarm().

struct dcc_t* dcc

Definition at line 46 of file dcc.c.

Initial value:

Definition at line 98 of file dns.c.

int dcc_total

Definition at line 47 of file dcc.c.

Initial value:
 {
  "DCCEVENT_HOSTBYIP",
  dns_dccexpmem,
  dns_dcchostbyip
}

Definition at line 164 of file dns.c.

Initial value:
 {
  "DCCEVENT_IPBYHOST",
  dns_dccexpmem,
  dns_dccipbyhost
}

Definition at line 170 of file dns.c.

Definition at line 45 of file dns.c.

Initial value:
 {
  "TCLEVENT_HOSTBYIP",
  dns_tclexpmem,
  dns_tcl_iporhostres
}

Definition at line 299 of file dns.c.

Initial value:
 {
  "TCLEVENT_IPBYHOST",
  dns_tclexpmem,
  dns_tcl_iporhostres
}

Definition at line 305 of file dns.c.

Definition at line 88 of file main.c.

Definition at line 135 of file main.c.

Referenced by block_dns_hostbyip(), and block_dns_ipbyhost().


Generated on 7 Sep 2016 for Eggdrop by  doxygen 1.6.1