mirror of
https://github.com/yarrick/iodine.git
synced 2024-11-22 06:41:26 +00:00
Move encoding to its own file
This commit is contained in:
parent
c7dae6e979
commit
b14b23c936
4
Makefile
4
Makefile
|
@ -1,8 +1,8 @@
|
||||||
CC = gcc
|
CC = gcc
|
||||||
CLIENT = iodine
|
CLIENT = iodine
|
||||||
CLIENTOBJS = iodine.o tun.o dns.o read.o
|
CLIENTOBJS = iodine.o tun.o dns.o read.o encoding.o
|
||||||
SERVER = iodined
|
SERVER = iodined
|
||||||
SERVEROBJS = iodined.o tun.o dns.o read.o
|
SERVEROBJS = iodined.o tun.o dns.o read.o encoding.o
|
||||||
|
|
||||||
OS = `uname | tr "a-z" "A-Z"`
|
OS = `uname | tr "a-z" "A-Z"`
|
||||||
|
|
||||||
|
|
77
dns.c
77
dns.c
|
@ -35,11 +35,8 @@
|
||||||
#include "read.h"
|
#include "read.h"
|
||||||
#include "structs.h"
|
#include "structs.h"
|
||||||
#include "dns.h"
|
#include "dns.h"
|
||||||
|
#include "encoding.h"
|
||||||
|
|
||||||
// For FreeBSD
|
|
||||||
#ifndef MIN
|
|
||||||
#define MIN(a,b) ((a)<(b)?(a):(b))
|
|
||||||
#endif
|
|
||||||
|
|
||||||
static int host2dns(const char *, char *, int);
|
static int host2dns(const char *, char *, int);
|
||||||
static int dns_write(int, int, char *, int, char);
|
static int dns_write(int, int, char *, int, char);
|
||||||
|
@ -221,66 +218,28 @@ dns_query(int fd, int id, char *host, int type)
|
||||||
sendto(fd, buf, len, 0, (struct sockaddr*)&peer, peerlen);
|
sendto(fd, buf, len, 0, (struct sockaddr*)&peer, peerlen);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
|
||||||
put_hex(char *p, char h)
|
|
||||||
{
|
|
||||||
int t;
|
|
||||||
static const char to_hex[] = "0123456789ABCDEF";
|
|
||||||
|
|
||||||
t = (h & 0xF0) >> 4;
|
|
||||||
p[0] = to_hex[t];
|
|
||||||
t = h & 0x0F;
|
|
||||||
p[1] = to_hex[t];
|
|
||||||
}
|
|
||||||
|
|
||||||
static int
|
static int
|
||||||
dns_write(int fd, int id, char *buf, int len, char flag)
|
dns_write(int fd, int id, char *buf, int len, char flag)
|
||||||
{
|
{
|
||||||
int avail;
|
int avail;
|
||||||
int i;
|
int written;
|
||||||
int final;
|
int encoded;
|
||||||
char data[257];
|
char data[257];
|
||||||
char *d;
|
char *d;
|
||||||
|
|
||||||
#define CHUNK 31
|
|
||||||
// 31 bytes expands to 62 chars in domain
|
|
||||||
// We just use hex as encoding right now
|
|
||||||
|
|
||||||
avail = 0xFF - strlen(topdomain) - 2;
|
avail = 0xFF - strlen(topdomain) - 2;
|
||||||
|
|
||||||
avail /= 2; // use two chars per byte in encoding
|
|
||||||
avail -= (avail/CHUNK); // make space for parts
|
|
||||||
|
|
||||||
avail = MIN(avail, len); // do not use more bytes than is available;
|
|
||||||
final = (avail == len); // is this the last block?
|
|
||||||
bzero(data, sizeof(data));
|
bzero(data, sizeof(data));
|
||||||
d = data;
|
d = data;
|
||||||
|
written = encode_data(buf, len, avail, d, flag);
|
||||||
if (flag != 0) {
|
encoded = strlen(data);
|
||||||
*d = flag;
|
d += encoded;
|
||||||
} else {
|
|
||||||
// First byte is 0 for middle packet and 1 for last packet
|
|
||||||
*d = '0' + final;
|
|
||||||
}
|
|
||||||
d++;
|
|
||||||
|
|
||||||
if (len > 0) {
|
|
||||||
for (i = 0; i < avail; i++) {
|
|
||||||
if (i > 0 && i % 31 == 0) {
|
|
||||||
*d = '.';
|
|
||||||
d++;
|
|
||||||
}
|
|
||||||
put_hex(d, buf[i]);
|
|
||||||
d += 2;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (*d != '.') {
|
if (*d != '.') {
|
||||||
*d++ = '.';
|
*d++ = '.';
|
||||||
}
|
}
|
||||||
strncpy(d, topdomain, strlen(topdomain)+1);
|
strncpy(d, topdomain, strlen(topdomain)+1);
|
||||||
|
|
||||||
dns_query(fd, id, data, T_NULL);
|
dns_query(fd, id, data, T_NULL);
|
||||||
return avail;
|
return written;
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
|
@ -433,32 +392,12 @@ dnsd_send(int fd, struct query *q, char *data, int datalen)
|
||||||
static int
|
static int
|
||||||
decodepacket(const char *name, char *buf, int buflen)
|
decodepacket(const char *name, char *buf, int buflen)
|
||||||
{
|
{
|
||||||
int r;
|
|
||||||
int len;
|
int len;
|
||||||
char *dp;
|
|
||||||
char *domain;
|
char *domain;
|
||||||
const char *np;
|
|
||||||
|
|
||||||
len = 1;
|
|
||||||
domain = strstr(name, topdomain);
|
domain = strstr(name, topdomain);
|
||||||
|
|
||||||
buf[0] = name[0];
|
len = decode_data(buf, buflen, name, domain);
|
||||||
|
|
||||||
dp = buf + 1;
|
|
||||||
np = name + 1;
|
|
||||||
|
|
||||||
while(len < buflen && np < domain) {
|
|
||||||
if(*np == '.') {
|
|
||||||
np++;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
sscanf(np, "%02X", &r);
|
|
||||||
*dp++ = (char)r;
|
|
||||||
np+=2;
|
|
||||||
len++;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (len == buflen)
|
if (len == buflen)
|
||||||
return -1;
|
return -1;
|
||||||
return len;
|
return len;
|
||||||
|
|
91
encoding.c
Normal file
91
encoding.c
Normal file
|
@ -0,0 +1,91 @@
|
||||||
|
/*
|
||||||
|
* 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 <stdio.h>
|
||||||
|
|
||||||
|
// For FreeBSD
|
||||||
|
#ifndef MIN
|
||||||
|
#define MIN(a,b) ((a)<(b)?(a):(b))
|
||||||
|
#endif
|
||||||
|
|
||||||
|
static const char to_hex[] = "0123456789ABCDEF";
|
||||||
|
|
||||||
|
int
|
||||||
|
encode_data(char *buf, int len, int space, char *dest, char flag)
|
||||||
|
{
|
||||||
|
int final;
|
||||||
|
int write;
|
||||||
|
int i;
|
||||||
|
int t;
|
||||||
|
|
||||||
|
#define CHUNK 31
|
||||||
|
// 31 bytes expands to 62 chars in domain
|
||||||
|
// We just use hex as encoding right now
|
||||||
|
|
||||||
|
write = space / 2; // use two chars per byte in encoding
|
||||||
|
write -= (write/CHUNK); // make space for parts
|
||||||
|
|
||||||
|
write = MIN(write, len); // do not use more bytes than is available;
|
||||||
|
final = (write == len); // is this the last block?
|
||||||
|
|
||||||
|
if (flag != 0) {
|
||||||
|
*dest = flag;
|
||||||
|
} else {
|
||||||
|
// First byte is 0 for middle packet and 1 for last packet
|
||||||
|
*dest = '0' + final;
|
||||||
|
}
|
||||||
|
dest++;
|
||||||
|
|
||||||
|
if (len > 0) {
|
||||||
|
for (i = 0; i < write; i++) {
|
||||||
|
if (i > 0 && i % CHUNK == 0) {
|
||||||
|
*dest = '.';
|
||||||
|
dest++;
|
||||||
|
}
|
||||||
|
t = (buf[i] & 0xF0) >> 4;
|
||||||
|
*dest++ = to_hex[t];
|
||||||
|
t = buf[i] & 0x0F;
|
||||||
|
*dest++ = to_hex[t];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return write;
|
||||||
|
}
|
||||||
|
|
||||||
|
int
|
||||||
|
decode_data(char *dest, int size, const char *src, char *srcend)
|
||||||
|
{
|
||||||
|
int r;
|
||||||
|
int len;
|
||||||
|
|
||||||
|
len = 1;
|
||||||
|
*dest = *src;
|
||||||
|
dest++;
|
||||||
|
src++;
|
||||||
|
|
||||||
|
while(len < size && src < srcend) {
|
||||||
|
if(*src == '.') {
|
||||||
|
src++;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
sscanf(src, "%02X", &r);
|
||||||
|
*dest++ = (char)r;
|
||||||
|
src+=2;
|
||||||
|
len++;
|
||||||
|
}
|
||||||
|
return len;
|
||||||
|
}
|
||||||
|
|
23
encoding.h
Normal file
23
encoding.h
Normal file
|
@ -0,0 +1,23 @@
|
||||||
|
/*
|
||||||
|
* 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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef _ENCODING_H_
|
||||||
|
#define _ENCODING_H_
|
||||||
|
|
||||||
|
int encode_data(char *, int, int, char *, char);
|
||||||
|
int decode_data(char *, int, const char *, char *);
|
||||||
|
|
||||||
|
#endif /* _ENCODING_H_ */
|
Loading…
Reference in a new issue