2006-06-08 22:58:15 +04:00
|
|
|
/*
|
2009-01-04 02:27:21 +03:00
|
|
|
* Copyright (c) 2006-2009 Bjorn Andersson <flex@kryo.se>, Erik Ekman <yarrick@kryo.se>
|
2006-06-08 22:58:15 +04:00
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <string.h>
|
2006-11-08 01:54:29 +03:00
|
|
|
#include <stdint.h>
|
2007-02-11 01:30:40 +03:00
|
|
|
#include <stdlib.h>
|
2006-06-08 22:58:15 +04:00
|
|
|
|
2006-08-25 02:04:27 +04:00
|
|
|
static int
|
2006-11-06 22:30:35 +03:00
|
|
|
readname_loop(char *packet, int packetlen, char **src, char *dst, size_t length, size_t loop)
|
2006-06-08 22:58:15 +04:00
|
|
|
{
|
2006-08-16 22:12:15 +04:00
|
|
|
char *dummy;
|
2006-08-25 02:18:24 +04:00
|
|
|
char *s;
|
|
|
|
char *d;
|
2006-06-08 22:58:15 +04:00
|
|
|
int len;
|
2006-11-07 13:07:53 +03:00
|
|
|
int offset;
|
2006-08-16 22:12:15 +04:00
|
|
|
char c;
|
2006-06-08 22:58:15 +04:00
|
|
|
|
2006-08-25 02:04:27 +04:00
|
|
|
if (loop <= 0)
|
|
|
|
return 0;
|
|
|
|
|
2006-06-08 22:58:15 +04:00
|
|
|
len = 0;
|
2006-08-25 02:18:24 +04:00
|
|
|
s = *src;
|
|
|
|
d = dst;
|
|
|
|
while(*s && len < length - 2) {
|
|
|
|
c = *s++;
|
2006-08-16 22:12:15 +04:00
|
|
|
|
|
|
|
/* is this a compressed label? */
|
|
|
|
if((c & 0xc0) == 0xc0) {
|
2006-11-07 13:07:53 +03:00
|
|
|
offset = (((s[-1] & 0x3f) << 8) | (s[0] & 0xff));
|
2006-11-06 22:30:35 +03:00
|
|
|
if (offset > packetlen) {
|
|
|
|
if (len == 0) {
|
2007-02-11 16:54:40 +03:00
|
|
|
/* Bad jump first in packet */
|
2006-11-06 22:30:35 +03:00
|
|
|
return 0;
|
|
|
|
} else {
|
2007-02-11 16:54:40 +03:00
|
|
|
/* Bad jump after some data */
|
2006-11-06 22:30:35 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
dummy = packet + offset;
|
|
|
|
len += readname_loop(packet, packetlen, &dummy, d, length - len, loop - 1);
|
2006-08-25 02:56:19 +04:00
|
|
|
goto end;
|
2006-06-08 22:58:15 +04:00
|
|
|
}
|
|
|
|
|
2006-08-25 02:21:32 +04:00
|
|
|
while(c && len < length - 1) {
|
2006-08-25 02:18:24 +04:00
|
|
|
*d++ = *s++;
|
2006-06-08 22:58:15 +04:00
|
|
|
len++;
|
2006-08-16 22:12:15 +04:00
|
|
|
|
|
|
|
c--;
|
2006-06-08 22:58:15 +04:00
|
|
|
}
|
2006-08-25 02:36:06 +04:00
|
|
|
|
2006-08-25 02:56:19 +04:00
|
|
|
if (len >= length - 1) {
|
2006-08-25 02:36:06 +04:00
|
|
|
break; /* We used up all space */
|
|
|
|
}
|
2006-06-08 22:58:15 +04:00
|
|
|
|
2006-08-25 02:56:19 +04:00
|
|
|
if (*s != 0) {
|
2006-08-25 02:18:24 +04:00
|
|
|
*d++ = '.';
|
2006-08-25 02:56:19 +04:00
|
|
|
len++;
|
|
|
|
}
|
2006-06-08 22:58:15 +04:00
|
|
|
}
|
2006-08-25 02:21:32 +04:00
|
|
|
dst[len++] = '\0';
|
2006-08-25 02:56:19 +04:00
|
|
|
|
|
|
|
end:
|
|
|
|
(*src) = s+1;
|
2006-08-25 02:18:24 +04:00
|
|
|
return len;
|
2006-08-16 22:12:15 +04:00
|
|
|
}
|
|
|
|
|
2006-08-25 02:04:27 +04:00
|
|
|
int
|
2006-11-06 22:30:35 +03:00
|
|
|
readname(char *packet, int packetlen, char **src, char *dst, size_t length)
|
2006-08-25 02:04:27 +04:00
|
|
|
{
|
2006-11-06 22:30:35 +03:00
|
|
|
return readname_loop(packet, packetlen, src, dst, length, 10);
|
2006-08-25 02:04:27 +04:00
|
|
|
}
|
|
|
|
|
2006-08-16 22:12:15 +04:00
|
|
|
int
|
|
|
|
readshort(char *packet, char **src, short *dst)
|
|
|
|
{
|
2006-08-23 20:03:24 +04:00
|
|
|
unsigned char *p;
|
2006-08-16 22:12:15 +04:00
|
|
|
|
2007-01-28 15:58:23 +03:00
|
|
|
p = (unsigned char *) *src;
|
2006-08-23 20:03:24 +04:00
|
|
|
*dst = (p[0] << 8) | p[1];
|
2006-08-16 22:12:15 +04:00
|
|
|
|
|
|
|
(*src) += sizeof(short);
|
|
|
|
return sizeof(short);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2006-11-08 01:54:29 +03:00
|
|
|
readlong(char *packet, char **src, uint32_t *dst)
|
2006-08-16 22:12:15 +04:00
|
|
|
{
|
2007-02-11 16:54:40 +03:00
|
|
|
/* A long as described in dns protocol is always 32 bits */
|
2006-08-23 20:03:24 +04:00
|
|
|
unsigned char *p;
|
2006-08-16 22:12:15 +04:00
|
|
|
|
2007-01-28 15:58:23 +03:00
|
|
|
p = (unsigned char *) *src;
|
2006-08-16 22:12:15 +04:00
|
|
|
|
2006-11-08 01:54:29 +03:00
|
|
|
*dst = ((uint32_t)p[0] << 24)
|
|
|
|
| ((uint32_t)p[1] << 16)
|
|
|
|
| ((uint32_t)p[2] << 8)
|
|
|
|
| ((uint32_t)p[3]);
|
2006-08-16 22:12:15 +04:00
|
|
|
|
2006-11-08 01:54:29 +03:00
|
|
|
(*src) += sizeof(uint32_t);
|
|
|
|
return sizeof(uint32_t);
|
2006-08-16 22:12:15 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
readdata(char *packet, char **src, char *dst, size_t len)
|
|
|
|
{
|
2006-08-23 20:03:24 +04:00
|
|
|
if (len < 0)
|
2006-08-23 20:38:58 +04:00
|
|
|
return 0;
|
2006-08-23 20:03:24 +04:00
|
|
|
|
2006-08-16 22:12:15 +04:00
|
|
|
memcpy(dst, *src, len);
|
|
|
|
|
|
|
|
(*src) += len;
|
|
|
|
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
2009-09-20 19:11:04 +04:00
|
|
|
int
|
|
|
|
readtxtbin(char *packet, char **src, size_t srcremain, char *dst, size_t dstremain)
|
|
|
|
{
|
|
|
|
unsigned char *uc;
|
|
|
|
int tocopy;
|
|
|
|
int dstused = 0;
|
|
|
|
|
|
|
|
while (srcremain > 0)
|
|
|
|
{
|
|
|
|
uc = (unsigned char*) (*src);
|
|
|
|
tocopy = *uc;
|
|
|
|
(*src)++;
|
|
|
|
srcremain--;
|
|
|
|
|
|
|
|
if (tocopy > srcremain)
|
|
|
|
return 0; /* illegal, better have nothing */
|
|
|
|
if (tocopy > dstremain)
|
|
|
|
return 0; /* doesn't fit, better have nothing */
|
|
|
|
|
|
|
|
memcpy(dst, *src, tocopy);
|
|
|
|
dst += tocopy;
|
|
|
|
(*src) += tocopy;
|
|
|
|
srcremain -= tocopy;
|
|
|
|
dstremain -= tocopy;
|
|
|
|
dstused += tocopy;
|
|
|
|
}
|
|
|
|
return dstused;
|
|
|
|
}
|
|
|
|
|
2007-02-11 01:30:40 +03:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2006-08-16 22:12:15 +04:00
|
|
|
int
|
2007-02-11 16:54:40 +03:00
|
|
|
putbyte(char **dst, unsigned char value)
|
2006-08-16 22:12:15 +04:00
|
|
|
{
|
|
|
|
**dst = value;
|
|
|
|
(*dst)++;
|
|
|
|
|
|
|
|
return sizeof(char);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2007-02-11 16:54:40 +03:00
|
|
|
putshort(char **dst, unsigned short value)
|
2006-08-16 22:12:15 +04:00
|
|
|
{
|
2006-08-23 20:03:24 +04:00
|
|
|
unsigned char *p;
|
2006-08-16 22:12:15 +04:00
|
|
|
|
2007-01-28 15:58:23 +03:00
|
|
|
p = (unsigned char *) *dst;
|
2006-06-08 22:58:15 +04:00
|
|
|
|
2006-08-16 22:12:15 +04:00
|
|
|
*p++ = (value >> 8);
|
|
|
|
*p++ = value;
|
2006-06-08 22:58:15 +04:00
|
|
|
|
2007-01-28 15:58:23 +03:00
|
|
|
(*dst) = (char *) p;
|
2006-08-16 22:12:15 +04:00
|
|
|
return sizeof(short);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2006-11-08 01:54:29 +03:00
|
|
|
putlong(char **dst, uint32_t value)
|
2006-08-16 22:12:15 +04:00
|
|
|
{
|
2007-02-11 16:54:40 +03:00
|
|
|
/* A long as described in dns protocol is always 32 bits */
|
2006-08-23 20:03:24 +04:00
|
|
|
unsigned char *p;
|
2006-08-16 22:12:15 +04:00
|
|
|
|
2007-01-28 15:58:23 +03:00
|
|
|
p = (unsigned char *) *dst;
|
2006-08-16 22:12:15 +04:00
|
|
|
|
|
|
|
*p++ = (value >> 24);
|
|
|
|
*p++ = (value >> 16);
|
|
|
|
*p++ = (value >> 8);
|
|
|
|
*p++ = (value);
|
|
|
|
|
2007-01-28 15:58:23 +03:00
|
|
|
(*dst) = (char *) p;
|
2006-11-08 01:54:29 +03:00
|
|
|
return sizeof(uint32_t);
|
2006-08-16 22:12:15 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
putdata(char **dst, char *data, size_t len)
|
|
|
|
{
|
2006-08-23 20:03:24 +04:00
|
|
|
if (len < 0)
|
2006-08-23 20:38:58 +04:00
|
|
|
return 0;
|
2006-08-23 20:03:24 +04:00
|
|
|
|
2006-08-16 22:12:15 +04:00
|
|
|
memcpy(*dst, data, len);
|
|
|
|
|
|
|
|
(*dst) += len;
|
2006-06-08 22:58:15 +04:00
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
2009-09-20 19:11:04 +04:00
|
|
|
int
|
|
|
|
puttxtbin(char **buf, size_t bufremain, char *from, size_t fromremain)
|
|
|
|
{
|
|
|
|
unsigned char uc;
|
|
|
|
unsigned char *ucp = &uc;
|
|
|
|
char *cp = (char *) ucp;
|
|
|
|
int tocopy;
|
|
|
|
int bufused = 0;
|
|
|
|
|
|
|
|
while (fromremain > 0)
|
|
|
|
{
|
|
|
|
tocopy = fromremain;
|
|
|
|
if (tocopy > 252)
|
|
|
|
tocopy = 252; /* allow off-by-1s in caches etc */
|
|
|
|
if (tocopy + 1 > bufremain)
|
|
|
|
return -1; /* doesn't fit, better have nothing */
|
|
|
|
|
|
|
|
uc = tocopy;
|
|
|
|
**buf = *cp;
|
|
|
|
(*buf)++;
|
|
|
|
bufremain--;
|
|
|
|
bufused++;
|
|
|
|
|
|
|
|
memcpy(*buf, from, tocopy);
|
|
|
|
(*buf) += tocopy;
|
|
|
|
from += tocopy;
|
|
|
|
bufremain -= tocopy;
|
|
|
|
fromremain -= tocopy;
|
|
|
|
bufused += tocopy;
|
|
|
|
}
|
|
|
|
return bufused;
|
|
|
|
}
|