summaryrefslogtreecommitdiff
path: root/src/libs6dns/s6dns_debug_dumpdt_pre_send.c
blob: 25c887a0064caab3d448271f989c7fc94630bc96 (plain)
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
/* ISC license */

/* For EOVERFLOW in OpenBSD */
#ifndef _BSD_SOURCE
#define _BSD_SOURCE
#endif

#include <string.h>
#include <stdint.h>
#include <errno.h>
#include <skalibs/uint16.h>
#include <skalibs/genwrite.h>
#include <skalibs/djbtime.h>
#include <skalibs/ip46.h>
#include <s6-dns/s6dns-ip46.h>
#include <s6-dns/s6dns-analyze.h>
#include <s6-dns/s6dns-debug.h>

#ifdef SKALIBS_IPV6_ENABLED
# define s6dns_ipfmt(buf, ip, is6) ((is6) ? ip6_fmt(buf, ip) : ip4_fmt(buf, ip))
#else
# define s6dns_ipfmt(buf, ip, is6) ip4_fmt(buf, ip)
#endif

int s6dns_debug_dumpdt_pre_send (s6dns_engine_t const *dt, void *data)
{
  genwrite_t *gp = data ;
  size_t len ;
  char buf[LOCALTMN_FMT] ;
  if ((*gp->put)(gp->target, "Preparing to send via ", 22) < 22) return 0 ;
  if ((*gp->put)(gp->target, dt->flagtcp ? "TCP" : "UDP", 3) < 3) return 0 ;
  if ((*gp->put)(gp->target, " to ", 4) < 4) return 0 ;
  len = dt->sa.s[4] & 1 ;
  if ((*gp->put)(gp->target, len ? "cache" : "server", len ? 5 : 6) < (len ? 5 : 6)) return 0 ;
  if ((*gp->put)(gp->target, " ", 1) < 1) return 0 ;
  len = s6dns_ipfmt(buf, s6dns_ip46list_ip(&dt->servers, dt->curserver), s6dns_ip46list_is6(&dt->servers, dt->curserver)) ;
  if ((*gp->put)(gp->target, buf, len) < (ssize_t)len) return 0 ;
  if ((*gp->put)(gp->target, " with deadline ", 15) < 15) return 0 ;
  {
    localtmn_t l ;
    if (!localtmn_from_tain(&l, &dt->localdeadline, 0))
    {
      if (errno != EOVERFLOW) return 0 ;
      memcpy(buf, "\"infinite\"", 10) ; len = 10 ;
    }
    else len = localtmn_fmt(buf, &l) ;
  }
  if ((*gp->put)(gp->target, buf, len) < (ssize_t)len) return 0 ;
  if ((*gp->put)(gp->target, ", ", 2) < 2) return 0 ;
  if (dt->flagstrict && (*gp->put)(gp->target, "strict, ", 8) < 8) return 0 ;
  if ((*gp->put)(gp->target, "query id ", 9) < 9) return 0 ;
  {
    uint16_t id ;
    uint16_unpack_big(dt->sa.s + 2, &id) ;
    len = uint16_fmt(buf, id) ;
  }
  if ((*gp->put)(gp->target, buf, len) < (ssize_t)len) return 0 ;
  if ((*gp->put)(gp->target, ":\n", 2) < 2) return 0 ;
  if (!s6dns_analyze_packet(gp, dt->sa.s + 2, dt->querylen - 2, 1)) return 0 ;
  if ((*gp->put)(gp->target, "\n", 1) < 1) return 0 ;
  return (*gp->flush)(gp->target) ;
}