mirror of
https://github.com/yarrick/iodine.git
synced 2024-11-24 00:59:19 +02:00
converted iodine.c to use packet struct
This commit is contained in:
parent
5f6b7131cc
commit
11748b2963
|
@ -30,9 +30,10 @@
|
||||||
|
|
||||||
struct packet
|
struct packet
|
||||||
{
|
{
|
||||||
int len;
|
int len; /* Total packet length */
|
||||||
int offset;
|
int sentlen; /* Length of chunk currently transmitted */
|
||||||
char data[64*1024];
|
int offset; /* Current offset */
|
||||||
|
char data[64*1024]; /* The data */
|
||||||
};
|
};
|
||||||
|
|
||||||
struct query {
|
struct query {
|
||||||
|
|
55
src/iodine.c
55
src/iodine.c
|
@ -58,11 +58,8 @@ static char *topdomain;
|
||||||
uint16_t rand_seed;
|
uint16_t rand_seed;
|
||||||
|
|
||||||
/* Current IP packet */
|
/* Current IP packet */
|
||||||
static char activepacket[4096];
|
static struct packet packet;
|
||||||
static char userid;
|
static char userid;
|
||||||
static int lastlen;
|
|
||||||
static int packetpos;
|
|
||||||
static int packetlen;
|
|
||||||
static uint16_t chunkid;
|
static uint16_t chunkid;
|
||||||
|
|
||||||
/* Base32 encoder used for non-data packets */
|
/* Base32 encoder used for non-data packets */
|
||||||
|
@ -131,36 +128,36 @@ build_hostname(char *buf, size_t buflen,
|
||||||
int
|
int
|
||||||
is_sending()
|
is_sending()
|
||||||
{
|
{
|
||||||
return (packetlen != 0);
|
return (packet.len != 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
read_dns(int fd, char *buf, int buflen)
|
read_dns(int fd, char *buf, int buflen)
|
||||||
{
|
{
|
||||||
struct sockaddr_in from;
|
struct sockaddr_in from;
|
||||||
char packet[64*1024];
|
char data[64*1024];
|
||||||
socklen_t addrlen;
|
socklen_t addrlen;
|
||||||
struct query q;
|
struct query q;
|
||||||
int rv;
|
int rv;
|
||||||
int r;
|
int r;
|
||||||
|
|
||||||
addrlen = sizeof(struct sockaddr);
|
addrlen = sizeof(struct sockaddr);
|
||||||
if ((r = recvfrom(fd, packet, sizeof(packet), 0,
|
if ((r = recvfrom(fd, data, sizeof(data), 0,
|
||||||
(struct sockaddr*)&from, &addrlen)) == -1) {
|
(struct sockaddr*)&from, &addrlen)) == -1) {
|
||||||
warn("recvfrom");
|
warn("recvfrom");
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
rv = dns_decode(buf, buflen, &q, QR_ANSWER, packet, r);
|
rv = dns_decode(buf, buflen, &q, QR_ANSWER, data, r);
|
||||||
|
|
||||||
if (is_sending() && chunkid == q.id) {
|
if (is_sending() && chunkid == q.id) {
|
||||||
/* Got ACK on sent packet */
|
/* Got ACK on sent packet */
|
||||||
packetpos += lastlen;
|
packet.offset += packet.sentlen;
|
||||||
if (packetpos == packetlen) {
|
if (packet.offset == packet.len) {
|
||||||
/* Packet completed */
|
/* Packet completed */
|
||||||
packetpos = 0;
|
packet.offset = 0;
|
||||||
packetlen = 0;
|
packet.len = 0;
|
||||||
lastlen = 0;
|
packet.sentlen = 0;
|
||||||
} else {
|
} else {
|
||||||
/* More to send */
|
/* More to send */
|
||||||
send_chunk(fd);
|
send_chunk(fd);
|
||||||
|
@ -186,10 +183,10 @@ tunnel_tun(int tun_fd, int dns_fd)
|
||||||
inlen = read;
|
inlen = read;
|
||||||
compress2((uint8_t*)out, &outlen, (uint8_t*)in, inlen, 9);
|
compress2((uint8_t*)out, &outlen, (uint8_t*)in, inlen, 9);
|
||||||
|
|
||||||
memcpy(activepacket, out, MIN(outlen, sizeof(activepacket)));
|
memcpy(packet.data, out, MIN(outlen, sizeof(packet.data)));
|
||||||
lastlen = 0;
|
packet.sentlen = 0;
|
||||||
packetpos = 0;
|
packet.offset = 0;
|
||||||
packetlen = outlen;
|
packet.len = outlen;
|
||||||
|
|
||||||
send_chunk(dns_fd);
|
send_chunk(dns_fd);
|
||||||
|
|
||||||
|
@ -268,7 +265,7 @@ static void
|
||||||
send_chunk(int fd)
|
send_chunk(int fd)
|
||||||
{
|
{
|
||||||
char hex[] = "0123456789ABCDEF";
|
char hex[] = "0123456789ABCDEF";
|
||||||
char packet[4096];
|
char data[4096];
|
||||||
struct query q;
|
struct query q;
|
||||||
char buf[4096];
|
char buf[4096];
|
||||||
int avail;
|
int avail;
|
||||||
|
@ -279,22 +276,22 @@ send_chunk(int fd)
|
||||||
q.id = ++chunkid;
|
q.id = ++chunkid;
|
||||||
q.type = T_NULL;
|
q.type = T_NULL;
|
||||||
|
|
||||||
p = activepacket;
|
p = packet.data;
|
||||||
p += packetpos;
|
p += packet.offset;
|
||||||
avail = packetlen - packetpos;
|
avail = packet.len - packet.offset;
|
||||||
|
|
||||||
lastlen = build_hostname(buf + 1, sizeof(buf) - 1, p, avail, topdomain, dataenc);
|
packet.sentlen = build_hostname(buf + 1, sizeof(buf) - 1, p, avail, topdomain, dataenc);
|
||||||
|
|
||||||
if (lastlen == avail)
|
if (packet.sentlen == avail)
|
||||||
code = 1;
|
code = 1;
|
||||||
else
|
else
|
||||||
code = 0;
|
code = 0;
|
||||||
|
|
||||||
code |= (userid << 1);
|
code |= (userid << 1);
|
||||||
buf[0] = hex[code];
|
buf[0] = hex[code];
|
||||||
len = dns_encode(packet, sizeof(packet), &q, QR_QUERY, buf, strlen(buf));
|
len = dns_encode(data, sizeof(data), &q, QR_QUERY, buf, strlen(buf));
|
||||||
|
|
||||||
sendto(fd, packet, len, 0, (struct sockaddr*)&peer, sizeof(peer));
|
sendto(fd, data, len, 0, (struct sockaddr*)&peer, sizeof(peer));
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
|
@ -320,9 +317,9 @@ send_ping(int fd)
|
||||||
char data[3];
|
char data[3];
|
||||||
|
|
||||||
if (is_sending()) {
|
if (is_sending()) {
|
||||||
lastlen = 0;
|
packet.sentlen = 0;
|
||||||
packetpos = 0;
|
packet.offset = 0;
|
||||||
packetlen = 0;
|
packet.len = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
data[0] = userid;
|
data[0] = userid;
|
||||||
|
|
Loading…
Reference in New Issue