mirror of
https://github.com/yarrick/iodine.git
synced 2024-11-23 18:15:13 +00:00
dns: improve code style
Signed-off-by: Ralf Ramsauer <ralf@ramses-pyramidenbau.de>
This commit is contained in:
parent
4591cafd27
commit
119d1b2da1
60
src/dns.c
60
src/dns.c
@ -49,8 +49,8 @@ int dnsc_use_edns0 = 1;
|
|||||||
|
|
||||||
#define CHECKLEN(x) if (buflen < (x) + (unsigned)(p-buf)) return 0
|
#define CHECKLEN(x) if (buflen < (x) + (unsigned)(p-buf)) return 0
|
||||||
|
|
||||||
int
|
int dns_encode(char *buf, size_t buflen, struct query *q, qr_t qr,
|
||||||
dns_encode(char *buf, size_t buflen, struct query *q, qr_t qr, const char *data, size_t datalen)
|
const char *data, size_t datalen)
|
||||||
{
|
{
|
||||||
HEADER *header;
|
HEADER *header;
|
||||||
short name;
|
short name;
|
||||||
@ -91,7 +91,8 @@ dns_encode(char *buf, size_t buflen, struct query *q, qr_t qr, const char *data,
|
|||||||
/* Answer section */
|
/* Answer section */
|
||||||
|
|
||||||
if (q->type == T_CNAME || q->type == T_A) {
|
if (q->type == T_CNAME || q->type == T_A) {
|
||||||
/* data is expected to be like "Hblabla.host.name.com\0" */
|
/* data is expected to be like
|
||||||
|
* "Hblabla.host.name.com\0" */
|
||||||
|
|
||||||
char *startp;
|
char *startp;
|
||||||
int namelen;
|
int namelen;
|
||||||
@ -130,12 +131,12 @@ dns_encode(char *buf, size_t buflen, struct query *q, qr_t qr, const char *data,
|
|||||||
putshort(&p, name);
|
putshort(&p, name);
|
||||||
putshort(&p, q->type);
|
putshort(&p, q->type);
|
||||||
putshort(&p, C_IN);
|
putshort(&p, C_IN);
|
||||||
putlong(&p, 0); /* TTL */
|
putlong(&p, 0); /* TTL */
|
||||||
|
|
||||||
startp = p;
|
startp = p;
|
||||||
p += 2; /* skip 2 bytes length */
|
p += 2; /* skip 2 bytes length */
|
||||||
CHECKLEN(2);
|
CHECKLEN(2);
|
||||||
putshort(&p, 10 * ancnt); /* preference */
|
putshort(&p, 10 * ancnt); /* preference */
|
||||||
|
|
||||||
if (q->type == T_SRV) {
|
if (q->type == T_SRV) {
|
||||||
/* weight, port (5060 = SIP) */
|
/* weight, port (5060 = SIP) */
|
||||||
@ -165,10 +166,10 @@ dns_encode(char *buf, size_t buflen, struct query *q, qr_t qr, const char *data,
|
|||||||
putshort(&p, name);
|
putshort(&p, name);
|
||||||
putshort(&p, q->type);
|
putshort(&p, q->type);
|
||||||
putshort(&p, C_IN);
|
putshort(&p, C_IN);
|
||||||
putlong(&p, 0); /* TTL */
|
putlong(&p, 0); /* TTL */
|
||||||
|
|
||||||
startp = p;
|
startp = p;
|
||||||
p += 2; /* skip 2 bytes length */
|
p += 2; /* skip 2 bytes length */
|
||||||
puttxtbin(&p, buflen - (p - buf), data, datalen);
|
puttxtbin(&p, buflen - (p - buf), data, datalen);
|
||||||
CHECKLEN(0);
|
CHECKLEN(0);
|
||||||
txtlen = p - startp;
|
txtlen = p - startp;
|
||||||
@ -177,12 +178,11 @@ dns_encode(char *buf, size_t buflen, struct query *q, qr_t qr, const char *data,
|
|||||||
ancnt = 1;
|
ancnt = 1;
|
||||||
} else {
|
} else {
|
||||||
/* NULL has raw binary data */
|
/* NULL has raw binary data */
|
||||||
|
|
||||||
CHECKLEN(10);
|
CHECKLEN(10);
|
||||||
putshort(&p, name);
|
putshort(&p, name);
|
||||||
putshort(&p, q->type);
|
putshort(&p, q->type);
|
||||||
putshort(&p, C_IN);
|
putshort(&p, C_IN);
|
||||||
putlong(&p, 0); /* TTL */
|
putlong(&p, 0); /* TTL */
|
||||||
|
|
||||||
datalen = MIN(datalen, buflen - (p - buf));
|
datalen = MIN(datalen, buflen - (p - buf));
|
||||||
CHECKLEN(2);
|
CHECKLEN(2);
|
||||||
@ -227,10 +227,10 @@ dns_encode(char *buf, size_t buflen, struct query *q, qr_t qr, const char *data,
|
|||||||
return len;
|
return len;
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
|
||||||
dns_encode_ns_response(char *buf, size_t buflen, struct query *q, char *topdomain)
|
|
||||||
/* Only used when iodined gets an NS type query */
|
/* Only used when iodined gets an NS type query */
|
||||||
/* Mostly same as dns_encode_a_response() below */
|
/* Mostly same as dns_encode_a_response() below */
|
||||||
|
int dns_encode_ns_response(char *buf, size_t buflen, struct query *q,
|
||||||
|
char *topdomain)
|
||||||
{
|
{
|
||||||
HEADER *header;
|
HEADER *header;
|
||||||
int len;
|
int len;
|
||||||
@ -319,10 +319,9 @@ dns_encode_ns_response(char *buf, size_t buflen, struct query *q, char *topdomai
|
|||||||
return len;
|
return len;
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
/* Only used when iodined gets an A type query for ns.topdomain or
|
||||||
dns_encode_a_response(char *buf, size_t buflen, struct query *q)
|
* www.topdomain . Mostly same as dns_encode_ns_response() above */
|
||||||
/* Only used when iodined gets an A type query for ns.topdomain or www.topdomain */
|
int dns_encode_a_response(char *buf, size_t buflen, struct query *q)
|
||||||
/* Mostly same as dns_encode_ns_response() above */
|
|
||||||
{
|
{
|
||||||
HEADER *header;
|
HEADER *header;
|
||||||
int len;
|
int len;
|
||||||
@ -354,18 +353,18 @@ dns_encode_a_response(char *buf, size_t buflen, struct query *q)
|
|||||||
name = 0xc000 | ((p - buf) & 0x3fff);
|
name = 0xc000 | ((p - buf) & 0x3fff);
|
||||||
|
|
||||||
/* Query section */
|
/* Query section */
|
||||||
putname(&p, buflen - (p - buf), q->name); /* Name */
|
putname(&p, buflen - (p - buf), q->name); /* Name */
|
||||||
CHECKLEN(4);
|
CHECKLEN(4);
|
||||||
putshort(&p, q->type); /* Type */
|
putshort(&p, q->type); /* Type */
|
||||||
putshort(&p, C_IN); /* Class */
|
putshort(&p, C_IN); /* Class */
|
||||||
|
|
||||||
/* Answer section */
|
/* Answer section */
|
||||||
CHECKLEN(12);
|
CHECKLEN(12);
|
||||||
putshort(&p, name); /* Name */
|
putshort(&p, name); /* Name */
|
||||||
putshort(&p, q->type); /* Type */
|
putshort(&p, q->type); /* Type */
|
||||||
putshort(&p, C_IN); /* Class */
|
putshort(&p, C_IN); /* Class */
|
||||||
putlong(&p, 3600); /* TTL */
|
putlong(&p, 3600); /* TTL */
|
||||||
putshort(&p, 4); /* Data length */
|
putshort(&p, 4); /* Data length */
|
||||||
|
|
||||||
/* ugly hack to output IP address */
|
/* ugly hack to output IP address */
|
||||||
ipp = (char *) &q->destination;
|
ipp = (char *) &q->destination;
|
||||||
@ -381,8 +380,7 @@ dns_encode_a_response(char *buf, size_t buflen, struct query *q)
|
|||||||
|
|
||||||
#undef CHECKLEN
|
#undef CHECKLEN
|
||||||
|
|
||||||
unsigned short
|
unsigned short dns_get_id(char *packet, size_t packetlen)
|
||||||
dns_get_id(char *packet, size_t packetlen)
|
|
||||||
{
|
{
|
||||||
HEADER *header;
|
HEADER *header;
|
||||||
header = (HEADER*)packet;
|
header = (HEADER*)packet;
|
||||||
@ -395,8 +393,8 @@ dns_get_id(char *packet, size_t packetlen)
|
|||||||
|
|
||||||
#define CHECKLEN(x) if (packetlen < (x) + (unsigned)(data-packet)) return 0
|
#define CHECKLEN(x) if (packetlen < (x) + (unsigned)(data-packet)) return 0
|
||||||
|
|
||||||
int
|
int dns_decode(char *buf, size_t buflen, struct query *q, qr_t qr, char *packet,
|
||||||
dns_decode(char *buf, size_t buflen, struct query *q, qr_t qr, char *packet, size_t packetlen)
|
size_t packetlen)
|
||||||
{
|
{
|
||||||
char name[QUERY_NAME_SIZE];
|
char name[QUERY_NAME_SIZE];
|
||||||
char rdata[4*1024];
|
char rdata[4*1024];
|
||||||
@ -536,7 +534,8 @@ dns_decode(char *buf, size_t buflen, struct query *q, qr_t qr, char *packet, siz
|
|||||||
readname(packet, packetlen, &data,
|
readname(packet, packetlen, &data,
|
||||||
names[pref / 10 - 1],
|
names[pref / 10 - 1],
|
||||||
QUERY_NAME_SIZE - 1);
|
QUERY_NAME_SIZE - 1);
|
||||||
names[pref / 10 - 1][QUERY_NAME_SIZE-1] = '\0';
|
names[pref / 10 - 1]
|
||||||
|
[QUERY_NAME_SIZE-1] = '\0';
|
||||||
}
|
}
|
||||||
|
|
||||||
/* always trust rlen, not name encoding */
|
/* always trust rlen, not name encoding */
|
||||||
@ -569,7 +568,8 @@ dns_decode(char *buf, size_t buflen, struct query *q, qr_t qr, char *packet, siz
|
|||||||
readlong(packet, &data, &ttl);
|
readlong(packet, &data, &ttl);
|
||||||
readshort(packet, &data, &rlen);
|
readshort(packet, &data, &rlen);
|
||||||
|
|
||||||
rv = readtxtbin(packet, &data, rlen, rdata, sizeof(rdata));
|
rv = readtxtbin(packet, &data, rlen, rdata,
|
||||||
|
sizeof(rdata));
|
||||||
if (rv >= 1) {
|
if (rv >= 1) {
|
||||||
rv = MIN(rv, buflen);
|
rv = MIN(rv, buflen);
|
||||||
memcpy(buf, rdata, rv);
|
memcpy(buf, rdata, rv);
|
||||||
|
Loading…
Reference in New Issue
Block a user