2006-06-08 18:58:15 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2006 Bjorn Andersson <flex@kryo.se>, Erik Ekman <yarrick@kryo.se>
|
|
|
|
*
|
|
|
|
* 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>
|
|
|
|
|
|
|
|
int
|
2006-08-16 18:12:15 +00:00
|
|
|
readname(char *packet, char **src, char *dst, size_t length)
|
2006-06-08 18:58:15 +00:00
|
|
|
{
|
2006-08-16 18:12:15 +00:00
|
|
|
char *dummy;
|
2006-06-08 18:58:15 +00:00
|
|
|
int len;
|
2006-08-16 18:12:15 +00:00
|
|
|
char *p;
|
|
|
|
char c;
|
2006-06-08 18:58:15 +00:00
|
|
|
|
|
|
|
len = 0;
|
2006-08-16 18:12:15 +00:00
|
|
|
p = *src;
|
|
|
|
while(*p && len < length) {
|
|
|
|
c = *p++;
|
2006-08-16 20:18:16 +00:00
|
|
|
len++;
|
2006-08-16 18:12:15 +00:00
|
|
|
|
|
|
|
/* is this a compressed label? */
|
|
|
|
if((c & 0xc0) == 0xc0) {
|
|
|
|
dummy = packet + (((p[-1] & 0x3f) << 8) | p[0]);
|
|
|
|
readname(packet, &dummy, dst, length - len);
|
2006-06-08 18:58:15 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2006-08-16 18:12:15 +00:00
|
|
|
while(c && len < length) {
|
|
|
|
*dst++ = *p++;
|
2006-06-08 18:58:15 +00:00
|
|
|
len++;
|
2006-08-16 18:12:15 +00:00
|
|
|
|
|
|
|
c--;
|
2006-06-08 18:58:15 +00:00
|
|
|
}
|
|
|
|
|
2006-08-16 18:12:15 +00:00
|
|
|
if (*p != 0)
|
2006-08-16 20:18:16 +00:00
|
|
|
*dst++ = '.';
|
2006-08-16 18:12:15 +00:00
|
|
|
else
|
2006-08-16 20:18:16 +00:00
|
|
|
*dst++ = '\0';
|
2006-06-08 18:58:15 +00:00
|
|
|
}
|
2006-08-16 18:12:15 +00:00
|
|
|
(*src) = p+1;
|
|
|
|
|
|
|
|
return strlen(dst);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
readshort(char *packet, char **src, short *dst)
|
|
|
|
{
|
|
|
|
char *p;
|
|
|
|
|
|
|
|
p = *src;
|
|
|
|
*dst = ((short)p[0] << 8)
|
|
|
|
| ((short)p[1]);
|
|
|
|
|
|
|
|
(*src) += sizeof(short);
|
|
|
|
return sizeof(short);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
readlong(char *packet, char **src, long *dst)
|
|
|
|
{
|
|
|
|
char *p;
|
|
|
|
|
|
|
|
p = *src;
|
|
|
|
|
|
|
|
*dst = ((long)p[0] << 24)
|
|
|
|
| ((long)p[1] << 16)
|
|
|
|
| ((long)p[2] << 8)
|
|
|
|
| ((long)p[3]);
|
|
|
|
|
|
|
|
(*src) += sizeof(long);
|
|
|
|
return sizeof(long);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
readdata(char *packet, char **src, char *dst, size_t len)
|
|
|
|
{
|
|
|
|
memcpy(dst, *src, len);
|
|
|
|
|
|
|
|
(*src) += len;
|
|
|
|
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
putbyte(char **dst, char value)
|
|
|
|
{
|
|
|
|
**dst = value;
|
|
|
|
(*dst)++;
|
|
|
|
|
|
|
|
return sizeof(char);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
putshort(char **dst, short value)
|
|
|
|
{
|
|
|
|
char *p;
|
|
|
|
|
|
|
|
p = *dst;
|
2006-06-08 18:58:15 +00:00
|
|
|
|
2006-08-16 18:12:15 +00:00
|
|
|
*p++ = (value >> 8);
|
|
|
|
*p++ = value;
|
2006-06-08 18:58:15 +00:00
|
|
|
|
2006-08-16 18:12:15 +00:00
|
|
|
(*dst) = p;
|
|
|
|
return sizeof(short);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
putlong(char **dst, long value)
|
|
|
|
{
|
|
|
|
char *p;
|
|
|
|
|
|
|
|
p = *dst;
|
|
|
|
|
|
|
|
*p++ = (value >> 24);
|
|
|
|
*p++ = (value >> 16);
|
|
|
|
*p++ = (value >> 8);
|
|
|
|
*p++ = (value);
|
|
|
|
|
|
|
|
(*dst) = p;
|
|
|
|
return sizeof(long);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
putdata(char **dst, char *data, size_t len)
|
|
|
|
{
|
|
|
|
memcpy(*dst, data, len);
|
|
|
|
|
|
|
|
(*dst) += len;
|
2006-06-08 18:58:15 +00:00
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|