moved method that encoded strings to dnsnames to read.c and renamed it to putname

This commit is contained in:
Bjorn Andersson 2007-02-10 22:30:40 +00:00
parent c538bd0b6d
commit 16e9df4164
4 changed files with 41 additions and 42 deletions

View file

@ -60,7 +60,9 @@ dns_encode(char *buf, size_t buflen, struct query *q, qr_t qr, char *data, size_
header->qdcount = htons(1); header->qdcount = htons(1);
name = 0xc000 | ((p - buf) & 0x3fff); name = 0xc000 | ((p - buf) & 0x3fff);
p += dns_encode_hostname(q->name, p, strlen(q->name));
putname(&p, 256, q->name);
putshort(&p, q->type); putshort(&p, q->type);
putshort(&p, C_IN); putshort(&p, C_IN);
@ -69,11 +71,6 @@ dns_encode(char *buf, size_t buflen, struct query *q, qr_t qr, char *data, size_
putshort(&p, C_IN); putshort(&p, C_IN);
putlong(&p, 0); putlong(&p, 0);
/*
* XXX: This is jidder! This is used to detect if there's packets to be sent.
*/
q->id = 0;
putshort(&p, datalen); putshort(&p, datalen);
putdata(&p, data, datalen); putdata(&p, data, datalen);
break; break;
@ -81,7 +78,7 @@ dns_encode(char *buf, size_t buflen, struct query *q, qr_t qr, char *data, size_
header->qdcount = htons(1); header->qdcount = htons(1);
header->arcount = htons(1); header->arcount = htons(1);
p += dns_encode_hostname(data, p, datalen); putname(&p, 256, data);
putshort(&p, q->type); putshort(&p, q->type);
putshort(&p, C_IN); putshort(&p, C_IN);
@ -215,37 +212,3 @@ dns_build_hostname(char *buf, size_t buflen,
return written; return written;
} }
int
dns_encode_hostname(const char *host, char *buffer, int size)
{
char *h;
char *p;
char *word;
int left;
h = strdup(host);
memset(buffer, 0, size);
p = buffer;
left = size;
word = strtok(h, ".");
while(word) {
if (strlen(word) > 63 || strlen(word) > left) {
free(h);
return -1;
}
left -= (strlen(word) + 1);
*p++ = (char)strlen(word);
memcpy(p, word, strlen(word));
p += strlen(word);
word = strtok(NULL, ".");
}
*p++ = 0;
free(h);
return p - buffer;
}

View file

@ -25,7 +25,6 @@ typedef enum {
} qr_t; } qr_t;
int dns_build_hostname(char *, size_t, const char *, const size_t, const char *); int dns_build_hostname(char *, size_t, const char *, const size_t, const char *);
int dns_encode_hostname(const char *, char *, int);
int dns_encode(char *, size_t, struct query *, qr_t, char *, size_t); int dns_encode(char *, size_t, struct query *, qr_t, char *, size_t);
int dns_decode(char *, size_t, struct query *, qr_t, char *, size_t); int dns_decode(char *, size_t, struct query *, qr_t, char *, size_t);

View file

@ -16,6 +16,7 @@
#include <string.h> #include <string.h>
#include <stdint.h> #include <stdint.h>
#include <stdlib.h>
static int static int
readname_loop(char *packet, int packetlen, char **src, char *dst, size_t length, size_t loop) readname_loop(char *packet, int packetlen, char **src, char *dst, size_t length, size_t loop)
@ -124,6 +125,41 @@ readdata(char *packet, char **src, char *dst, size_t len)
return len; return len;
} }
int
putname(char **buf, size_t buflen, const char *host)
{
char *word;
int left;
char *h;
char *p;
h = strdup(host);
left = buflen;
p = *buf;
word = strtok(h, ".");
while(word) {
if (strlen(word) > 63 || strlen(word) > left) {
free(h);
return -1;
}
left -= (strlen(word) + 1);
*p++ = (char)strlen(word);
memcpy(p, word, strlen(word));
p += strlen(word);
word = strtok(NULL, ".");
}
*p++ = 0;
free(h);
*buf = p;
return buflen - left;
}
int int
putbyte(char **dst, char value) putbyte(char **dst, char value)
{ {

View file

@ -22,6 +22,7 @@ int readshort(char *, char **, short *);
int readlong(char *, char **, uint32_t *); int readlong(char *, char **, uint32_t *);
int readdata(char *, char **, char *, size_t); int readdata(char *, char **, char *, size_t);
int putname(char **, size_t, const char *);
int putbyte(char **, char); int putbyte(char **, char);
int putshort(char **, short); int putshort(char **, short);
int putlong(char **, uint32_t); int putlong(char **, uint32_t);