Skip to content

Commit

Permalink
exit 111 if unable to write to subfdout
Browse files Browse the repository at this point in the history
  • Loading branch information
mbhangui committed Sep 23, 2023
1 parent 2ad4daa commit 0bfd2ab
Show file tree
Hide file tree
Showing 18 changed files with 906 additions and 893 deletions.
126 changes: 68 additions & 58 deletions tinydnssec-x/dns_ip.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,73 +3,83 @@
#include "byte.h"
#include "dns.h"

int dns_ip4_packet(stralloc *out,const char *buf,unsigned int len)
int
dns_ip4_packet(stralloc *out, const char *buf, unsigned int len)
{
unsigned int pos;
char header[12];
uint16 numanswers;
uint16 datalen;
unsigned int pos;
char header[12];
uint16 numanswers;
uint16 datalen;

if (!stralloc_copys(out,"")) return -1;

pos = dns_packet_copy(buf,len,0,header,12); if (!pos) return -1;
uint16_unpack_big(header + 6,&numanswers);
pos = dns_packet_skipname(buf,len,pos); if (!pos) return -1;
pos += 4;

while (numanswers--) {
pos = dns_packet_skipname(buf,len,pos); if (!pos) return -1;
pos = dns_packet_copy(buf,len,pos,header,10); if (!pos) return -1;
uint16_unpack_big(header + 8,&datalen);
if (byte_equal(header,2,DNS_T_A))
if (byte_equal(header + 2,2,DNS_C_IN))
if (datalen == 4) {
if (!dns_packet_copy(buf,len,pos,header,4)) return -1;
if (!stralloc_catb(out,header,4)) return -1;
if (!stralloc_copys(out, ""))
return -1;
if (!(pos = dns_packet_copy(buf, len, 0, header, 12)))
return -1;
uint16_unpack_big(header + 6, &numanswers);
if (!(pos = dns_packet_skipname(buf, len, pos)))
return -1;
pos += 4;
while (numanswers--) {
if (!(pos = dns_packet_skipname(buf, len, pos)))
return -1;
if (!(pos = dns_packet_copy(buf, len, pos, header, 10)))
return -1;
uint16_unpack_big(header + 8, &datalen);
if (byte_equal(header, 2, DNS_T_A) && byte_equal(header + 2, 2, DNS_C_IN) && datalen == 4) {
if (!dns_packet_copy(buf, len, pos, header, 4))
return -1;
if (!stralloc_catb(out, header, 4))
return -1;
}
pos += datalen;
}
pos += datalen;
}

dns_sortip(out->s,out->len);
return 0;
dns_sortip(out->s, out->len);
return 0;
}

static char *q = 0;
static char *q = 0;

int dns_ip4(stralloc *out,const stralloc *fqdn)
int
dns_ip4(stralloc *out, const stralloc *fqdn)
{
unsigned int i;
char code;
char ch;
unsigned int i;
char code;
char ch;

if (!stralloc_copys(out,"")) return -1;
code = 0;
for (i = 0;i <= fqdn->len;++i) {
if (i < fqdn->len)
ch = fqdn->s[i];
else
ch = '.';
if (!stralloc_copys(out, ""))
return -1;
code = 0;
for (i = 0; i <= fqdn->len; ++i) {
if (i < fqdn->len)
ch = fqdn->s[i];
else
ch = '.';

if ((ch == '[') || (ch == ']')) continue;
if (ch == '.') {
if (!stralloc_append(out,&code)) return -1;
code = 0;
continue;
}
if ((ch >= '0') && (ch <= '9')) {
code *= 10;
code += ch - '0';
continue;
}
if ((ch == '[') || (ch == ']'))
continue;
if (ch == '.') {
if (!stralloc_append(out, &code))
return -1;
code = 0;
continue;
}
if ((ch >= '0') && (ch <= '9')) {
code *= 10;
code += ch - '0';
continue;
}

if (!dns_domain_fromdot(&q,fqdn->s,fqdn->len)) return -1;
if (dns_resolve(q,DNS_T_A) == -1) return -1;
if (dns_ip4_packet(out,dns_resolve_tx.packet,dns_resolve_tx.packetlen) == -1) return -1;
dns_transmit_free(&dns_resolve_tx);
dns_domain_free(&q);
return 0;
}
if (!dns_domain_fromdot(&q, fqdn->s, fqdn->len))
return -1;
if (dns_resolve(q, DNS_T_A) == -1)
return -1;
if (dns_ip4_packet(out, dns_resolve_tx.packet, dns_resolve_tx.packetlen) == -1)
return -1;
dns_transmit_free(&dns_resolve_tx);
dns_domain_free(&q);
return 0;
}

out->len &= ~3;
return 0;
out->len &= ~3;
return 0;
}
34 changes: 10 additions & 24 deletions tinydnssec-x/dnsip.1
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.\" vim: tw=75
.TH dnsip 1

.SH NAME
Expand All @@ -10,31 +11,16 @@ dnsip
]

.SH DESCRIPTION
.B dnsip
resolves
.I fqdn
and prints the IP addresses of
.I fqdn
on a single line.
If
.I fqdn
does not exist,
.B dnsip
prints a blank line.
You can list several
.IR fqdn s;
.B dnsip
prints each result on a separate line.
\fBdnsip\fR resolves \fIfqdn\fR and prints the IP addresses of \fIfqdn\fR
on a single line. If \fIfqdn\fR does not exist, \fBdnsip\fR prints a blank
line. You can list several \fIfqdn\fR's; \fBdnsip\fR prints each result on
a separate line.

Normally
.B dnsip
exits 0.
If
.B dnsip
encounters a temporary problem
that prevents it from determining the list of IP addresses,
it prints an error message and exits 111.
The same comments apply to the other programs described here.
Normally \fBdnsip\fR exits 0. If \fBdnsip\fR encounters a temporary problem
that prevents it from determining the list of IP addresses, it prints an
error message and exits 111. It also exits 111 if it is not able to write
the ip address to descriptor 1. The same comments apply to the other
programs described here.

.SH SEE ALSO
dnsipq(1),
Expand Down
52 changes: 26 additions & 26 deletions tinydnssec-x/dnsip.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,35 +7,35 @@

#define FATAL "dnsip: fatal: "

static char seed[128];
static char seed[128];

static stralloc fqdn;
static stralloc out;
char str[IP4_FMT];
char str[IP4_FMT];

int main(int argc,char **argv)
int
main(int argc, char **argv)
{
int i;

dns_random_init(seed);

if (*argv) ++argv;

while (*argv) {
if (!stralloc_copys(&fqdn,*argv))
strerr_die2x(111,FATAL,"out of memory");
if (dns_ip4(&out,&fqdn) == -1)
strerr_die4sys(111,FATAL,"unable to find IP address for ",*argv,": ");

for (i = 0;i + 4 <= out.len;i += 4) {
substdio_put(subfdout,str,ip4_fmt(str,out.s + i));
substdio_puts(subfdout," ");
}
substdio_puts(subfdout,"\n");

++argv;
}

substdio_flush(subfdout);
_exit(0);
int i;

dns_random_init(seed);
if (*argv)
++argv;
while (*argv) {
if (!stralloc_copys(&fqdn, *argv))
strerr_die2x(111, FATAL, "out of memory");
if (dns_ip4(&out, &fqdn) == -1)
strerr_die4sys(111, FATAL, "unable to find IP address for ", *argv, ": ");
for (i = 0; i + 4 <= out.len; i += 4) {
if (substdio_put(subfdout, str, ip4_fmt(str, out.s + i)) == -1 ||
substdio_puts(subfdout, " ") == -1)
_exit(111);
}
if (substdio_puts(subfdout, "\n") == -1)
_exit(111);
++argv;
}
if (substdio_flush(subfdout) == -1)
_exit(111);
_exit(0);
}
32 changes: 10 additions & 22 deletions tinydnssec-x/dnsipq.1
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.\" vim: tw=75
.TH dnsipq 1

.SH NAME
Expand All @@ -10,29 +11,16 @@ dnsipq \- dns lookup tool
]

.SH DESCRIPTION
\fBdnsipq\fR
feeds the name
.I udn
through
.BR qualification (5).
It prints the fully qualified domain name and IP addresses
on a single line.
If the fully qualified domain name does not exist,
.B dnsipq
prints no addresses.
You can list several
.IR udn s;
.B dnsipq
prints each result on a separate line.
\fBdnsipq\fR feeds the name \fIudn\fR through \fBqualification\fR(5). It
prints the fully qualified domain name and IP addresses on a single line.
If the fully qualified domain name does not exist, \fBdnsipq\R prints no
addresses. You can list several \fIudn\fR s; \fBdnsipq\fR prints each
result on a separate line.

Normally
.B dnsipq
exits 0.
If
.B dnsipq
encounters a temporary problem
that prevents it from determining the list of IP addresses,
it prints an error message and exits 111.
Normally \fBdnsipq\fR exits 0. If \fdnsipq\fR encounters a temporary
problem that prevents it from determining the list of IP addresses, it
prints an error message and exits 111. It also exits 111 if it is not able
to write the ip address to descriptor 1.

.SH SEE ALSO
qualification(5),
Expand Down
57 changes: 29 additions & 28 deletions tinydnssec-x/dnsipq.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,38 +7,39 @@

#define FATAL "dnsipq: fatal: "

static char seed[128];
static char seed[128];

static stralloc in;
static stralloc fqdn;
static stralloc out;
char str[IP4_FMT];
char str[IP4_FMT];

int main(int argc,char **argv)
int
main(int argc, char **argv)
{
int i;

dns_random_init(seed);

if (*argv) ++argv;

while (*argv) {
if (!stralloc_copys(&in,*argv))
strerr_die2x(111,FATAL,"out of memory");
if (dns_ip4_qualify(&out,&fqdn,&in) == -1)
strerr_die4sys(111,FATAL,"unable to find IP address for ",*argv,": ");

substdio_put(subfdout,fqdn.s,fqdn.len);
substdio_puts(subfdout," ");
for (i = 0;i + 4 <= out.len;i += 4) {
substdio_put(subfdout,str,ip4_fmt(str,out.s + i));
substdio_puts(subfdout," ");
}
substdio_puts(subfdout,"\n");

++argv;
}

substdio_flush(subfdout);
_exit(0);
int i;

dns_random_init(seed);
if (*argv)
++argv;
while (*argv) {
if (!stralloc_copys(&in, *argv))
strerr_die2x(111, FATAL, "out of memory");
if (dns_ip4_qualify(&out, &fqdn, &in) == -1)
strerr_die4sys(111, FATAL, "unable to find IP address for ", *argv, ": ");
if (substdio_put(subfdout, fqdn.s, fqdn.len) == -1 ||
substdio_put(subfdout, " ", 1) == -1)
_exit(111);
for (i = 0; i + 4 <= out.len; i += 4) {
if (substdio_put(subfdout, str, ip4_fmt(str, out.s + i)) == -1 ||
substdio_put(subfdout, " ", 1) == -1)
_exit(111);
}
if (substdio_puts(subfdout, "\n") == -1)
_exit(111);
++argv;
}
if (substdio_flush(subfdout) == -1)
_exit(111);
_exit(0);
}
23 changes: 8 additions & 15 deletions tinydnssec-x/dnsmx.1
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.\" vim: tw=75
.TH dnsmx 1

.SH NAME
Expand All @@ -9,22 +10,14 @@ dnsmx \- prints the MX records of fqdn

.SH DESCRIPTION

.B dnsmx
prints the MX records of
.IR fqdn .
If there are no MX records,
.B dnsmx
prints an artificial MX record,
simulating the behavior of MTAs.
\fBdnsmx\fR prints the MX records of \fIfqdn\fR. If there are no MX
records, \fBdnsmx\fR prints an artificial MX record, simulating the
behavior of MTAs.

Normally
.B dnsmx
exits 0.
If
.B dnsmx
encounters a temporary problem
that prevents it from determining the list of MX records,
it prints an error message and exits 111.
Normally \fBdnsmx\fR exits 0. If \fBdnsmx\fR encounters a temporary problem
that prevents it from determining the list of MX records, it prints an
error message and exits 111. It also exits 111 if it is not able to write
the ip address to descriptor 1.

.SH SEE ALSO
dnsip(1),
Expand Down
Loading

0 comments on commit 0bfd2ab

Please sign in to comment.