source: packages/net/djbdns/patches/130-dnscache-multiple-ip.patch @ 15612

Last change on this file since 15612 was 15612, checked in by markus, 7 years ago

added djbdns patch provided by Johnny Halfmoon

removed dnsmasq.patch as the options were included upstream already.

File size: 7.5 KB
  • djbdns-1.05

    old new  
    55#include "strerr.h" 
    66#include "error.h" 
    77#include "ip4.h" 
     8#include "str.h" 
    89#include "uint16.h" 
    910#include "uint64.h" 
    1011#include "socket.h" 
     
    4748 
    4849 
    4950static char myipoutgoing[4]; 
    50 static char myipincoming[4]; 
    5151static char buf[1024]; 
    5252uint64 numqueries = 0; 
    5353 
     54struct interf { 
     55  char ip[4]; 
     56  int udp53; 
     57  int tcp53; 
     58  iopause_fd *udp53io; 
     59  iopause_fd *tcp53io; 
     60   
     61  struct interf *next; 
     62} ; 
    5463 
    55 static int udp53; 
     64struct interf *interhead = 0; 
    5665 
    5766#define MAXUDP 200 
    5867static struct udpclient { 
     
    6069  struct taia start; 
    6170  uint64 active; /* query number, if active; otherwise 0 */ 
    6271  iopause_fd *io; 
     72  int fd; 
    6373  char ip[4]; 
    6474  uint16 port; 
    6575  char id[2]; 
     
    7888  if (!u[j].active) return; 
    7989  response_id(u[j].id); 
    8090  if (response_len > 512) response_tc(); 
    81   socket_send4(udp53,response,response_len,u[j].ip,u[j].port); 
     91  socket_send4(u[j].fd,response,response_len,u[j].ip,u[j].port); 
    8292  log_querydone(&u[j].active,response_len); 
    8393  u[j].active = 0; --uactive; 
    8494} 
    8595 
    86 void u_new(void) 
     96void u_new(int fd) 
    8797{ 
    8898  int j; 
    8999  int i; 
     
    108118 
    109119  x = u + j; 
    110120  taia_now(&x->start); 
     121  x->fd = fd; 
    111122 
    112   len = socket_recv4(udp53,buf,sizeof buf,x->ip,&x->port); 
     123  len = socket_recv4(x->fd,buf,sizeof buf,x->ip,&x->port); 
    113124  if (len == -1) return; 
    114125  if (len >= sizeof buf) return; 
    115126  if (x->port < 1024) if (x->port != 53) return; 
     
    129140} 
    130141 
    131142 
    132 static int tcp53; 
    133  
    134143#define MAXTCP 20 
    135144struct tcpclient { 
    136145  struct query q; 
     
    138147  struct taia timeout; 
    139148  uint64 active; /* query number or 1, if active; otherwise 0 */ 
    140149  iopause_fd *io; 
     150  int fd; 
    141151  char ip[4]; /* send response to this address */ 
    142152  uint16 port; /* send response to this port */ 
    143153  char id[2]; 
     
    266276  x->state = 0; 
    267277} 
    268278 
    269 void t_new(void) 
     279void t_new(int fd) 
    270280{ 
    271281  int i; 
    272282  int j; 
     
    290300 
    291301  x = t + j; 
    292302  taia_now(&x->start); 
     303  x->fd = fd; 
    293304 
    294   x->tcp = socket_accept4(tcp53,x->ip,&x->port); 
     305  x->tcp = socket_accept4(x->fd,x->ip,&x->port); 
    295306  if (x->tcp == -1) return; 
    296307  if (x->port < 1024) if (x->port != 53) { close(x->tcp); return; } 
    297308  if (!okclient(x->ip)) { close(x->tcp); return; } 
     
    304315  log_tcpopen(x->ip,x->port); 
    305316} 
    306317 
     318#define FATAL "dnscache: fatal: " 
    307319 
    308 iopause_fd io[3 + MAXUDP + MAXTCP]; 
    309 iopause_fd *udp53io; 
    310 iopause_fd *tcp53io; 
     320iopause_fd *io = 0; 
     321int numio; 
    311322 
    312323static void doit(void) 
    313324{ 
    314325  int j; 
    315326  struct taia deadline; 
    316327  struct taia stamp; 
     328  struct interf *inter; 
    317329  int iolen; 
    318330  int r; 
    319331 
     332  io = (iopause_fd *) alloc((numio + 1 + MAXUDP + MAXTCP) * sizeof(iopause_fd)); 
     333  if (!io) 
     334    strerr_die2sys(111,FATAL,"unable to alloc io: "); 
     335 
    320336  for (;;) { 
    321337    taia_now(&stamp); 
    322338    taia_uint(&deadline,120); 
     
    324340 
    325341    iolen = 0; 
    326342 
    327     udp53io = io + iolen++; 
    328     udp53io->fd = udp53; 
    329     udp53io->events = IOPAUSE_READ; 
    330  
    331     tcp53io = io + iolen++; 
    332     tcp53io->fd = tcp53; 
    333     tcp53io->events = IOPAUSE_READ; 
     343    for (inter = interhead; inter != 0; inter = inter->next) { 
     344      inter->udp53io = io + iolen++; 
     345      inter->udp53io->fd = inter->udp53; 
     346      inter->udp53io->events = IOPAUSE_READ; 
     347 
     348      inter->tcp53io = io + iolen++; 
     349      inter->tcp53io->fd = inter->tcp53; 
     350      inter->tcp53io->events = IOPAUSE_READ; 
     351    } 
    334352 
    335353    for (j = 0;j < MAXUDP;++j) 
    336354      if (u[j].active) { 
     
    372390            t_rw(j); 
    373391      } 
    374392 
    375     if (udp53io) 
    376       if (udp53io->revents) 
    377         u_new(); 
    378  
    379     if (tcp53io) 
    380       if (tcp53io->revents) 
    381         t_new(); 
     393    for (inter = interhead; inter != 0; inter = inter->next) { 
     394      if (inter->udp53io) 
     395        if (inter->udp53io->revents) 
     396          u_new(inter->udp53); 
     397 
     398      if (inter->tcp53io) 
     399        if (inter->tcp53io->revents) 
     400          t_new(inter->tcp53); 
     401    } 
    382402  } 
    383403} 
    384404   
    385 #define FATAL "dnscache: fatal: " 
    386  
    387405char seed[128]; 
    388406 
    389407int main() 
    390408{ 
    391409  char *x; 
     410  int len; 
     411  int pos; 
     412  int oldpos; 
     413  char iptmp[4]; 
     414  char iperr[IP4_FMT]; 
     415  struct interf *inter; 
     416  struct interf *itmp; 
    392417  unsigned long cachesize; 
    393418 
    394419  x = env_get("IP"); 
    395420  if (!x) 
    396421    strerr_die2x(111,FATAL,"$IP not set"); 
    397   if (!ip4_scan(x,myipincoming)) 
    398     strerr_die3x(111,FATAL,"unable to parse IP address ",x); 
    399422 
    400   udp53 = socket_udp(); 
    401   if (udp53 == -1) 
    402     strerr_die2sys(111,FATAL,"unable to create UDP socket: "); 
    403   if (socket_bind4_reuse(udp53,myipincoming,53) == -1) 
    404     strerr_die2sys(111,FATAL,"unable to bind UDP socket: "); 
    405  
    406   tcp53 = socket_tcp(); 
    407   if (tcp53 == -1) 
    408     strerr_die2sys(111,FATAL,"unable to create TCP socket: "); 
    409   if (socket_bind4_reuse(tcp53,myipincoming,53) == -1) 
    410     strerr_die2sys(111,FATAL,"unable to bind TCP socket: "); 
     423  len = str_len(x); 
     424  numio = pos = oldpos = 0; 
     425   
     426  while (pos < len) { 
     427    if (pos) oldpos = pos + 1; 
     428    pos = oldpos + str_chr(x + oldpos,'/'); 
     429    x[pos] = 0; 
     430    if (!str_len(x + oldpos)) continue; 
     431     
     432    if (!ip4_scan(x + oldpos,iptmp)) 
     433      strerr_die3x(111,FATAL,"unable to parse IP address ",x + oldpos); 
     434       
     435    inter = (struct interf *) alloc(sizeof(struct interf)); 
     436     
     437    if (interhead == 0) interhead = inter; 
     438    else if (interhead->next == 0) interhead->next = inter; 
     439    else { 
     440      for (itmp = interhead; itmp->next != 0; itmp = itmp->next); 
     441      itmp->next = inter; 
     442    } 
     443     
     444    inter->next = 0; 
     445     
     446    inter->udp53 = socket_udp(); 
     447    if (inter->udp53 == -1) 
     448      strerr_die4sys(111,FATAL,"unable to create UDP socket for IP address ",x + oldpos,": "); 
     449    if (socket_bind4_reuse(inter->udp53,iptmp,53) == -1) 
     450      strerr_die4sys(111,FATAL,"unable to bind UDP socket for IP address ",x + oldpos,": "); 
     451       
     452    inter->tcp53 = socket_tcp(); 
     453    if (inter->tcp53 == -1) 
     454      strerr_die4sys(111,FATAL,"unable to create TCP socket for IP address ",x + oldpos,": "); 
     455    if (socket_bind4_reuse(inter->tcp53,iptmp,53) == -1) 
     456      strerr_die4sys(111,FATAL,"unable to bind TCP socket for IP address ",x + oldpos,": "); 
     457       
     458    numio++; 
     459    log_listen(iptmp); 
     460  } 
     461 
     462  if (interhead == 0) 
     463    strerr_die2x(111,FATAL,"no interfaces to listen on"); 
    411464 
    412465  droproot(FATAL); 
    413466 
    414   socket_tryreservein(udp53,131072); 
     467  for (inter = interhead; inter != 0; inter = inter->next) 
     468    socket_tryreservein(inter->udp53,131072); 
    415469 
    416470  byte_zero(seed,sizeof seed); 
    417471  read(0,seed,sizeof seed); 
     
    439493  if (!roots_init()) 
    440494    strerr_die2sys(111,FATAL,"unable to read servers: "); 
    441495 
    442   if (socket_listen(tcp53,20) == -1) 
    443     strerr_die2sys(111,FATAL,"unable to listen on TCP socket: "); 
     496  for (inter = interhead; inter != 0; inter = inter->next) 
     497    if (socket_listen(inter->tcp53,20) == -1) { 
     498      iperr[ip4_fmt(iperr,inter->ip)] = 0; 
     499      strerr_die4sys(111,FATAL,"unable to listen on TCP socket for IP ",iperr,": "); 
     500    } 
    444501 
    445502  log_startup(); 
    446503  doit(); 
  • djbdns-1.05

    old new  
    9494  line(); 
    9595} 
    9696 
     97void log_listen(const char addr[4]) 
     98{ 
     99  string("listening on "); 
     100  ip(addr); 
     101  line(); 
     102} 
     103 
    97104void log_query(uint64 *qnum,const char client[4],unsigned int port,const char id[2],const char *q,const char qtype[2]) 
    98105{ 
    99106  string("query "); number(*qnum); space(); 
  • djbdns-1.05

    old new  
    44#include "uint64.h" 
    55 
    66extern void log_startup(void); 
     7extern void log_listen(const char *); 
    78 
    89extern void log_query(uint64 *,const char *,unsigned int,const char *,const char *,const char *); 
    910extern void log_querydrop(uint64 *); 
Note: See TracBrowser for help on using the repository browser.