From bc5f0a7fb709ad4c6803eb0e5cd9ae4119ed240f Mon Sep 17 00:00:00 2001 From: Erik Ekman Date: Sat, 1 Dec 2007 21:23:59 +0000 Subject: [PATCH] #7 Move packet handling out of iodine.c and into packet.c --- src/Makefile | 4 ++-- src/common.h | 8 ------- src/iodine.c | 39 ++++++++++++--------------------- src/packet.c | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++++ src/packet.h | 36 ++++++++++++++++++++++++++++++ src/user.h | 2 ++ 6 files changed, 116 insertions(+), 35 deletions(-) create mode 100644 src/packet.c create mode 100644 src/packet.h diff --git a/src/Makefile b/src/Makefile index 958aa70..9ca5753 100644 --- a/src/Makefile +++ b/src/Makefile @@ -1,5 +1,5 @@ CC = gcc -COMMONOBJS = tun.o dns.o read.o encoding.o login.o base32.o base64.o md5.o common.o +COMMONOBJS = tun.o dns.o read.o encoding.o login.o base32.o base64.o md5.o common.o packet.o CLIENTOBJS = iodine.o CLIENT = ../bin/iodine SERVEROBJS = iodined.o user.o @@ -11,7 +11,7 @@ ARCH = `uname -m` LDFLAGS = -lz CFLAGS = -c -g -Wall -D$(OS) -pedantic -all: stateos $(CLIENT) $(SERVER) $(TESTSUITE) +all: stateos $(CLIENT) $(SERVER) stateos: @echo OS is $(OS), arch is $(ARCH) diff --git a/src/common.h b/src/common.h index 811aa40..e6100ab 100644 --- a/src/common.h +++ b/src/common.h @@ -30,14 +30,6 @@ #define QUERY_NAME_SIZE 256 -struct packet -{ - int len; /* Total packet length */ - int sentlen; /* Length of chunk currently transmitted */ - int offset; /* Current offset */ - char data[64*1024]; /* The data */ -}; - struct query { char name[QUERY_NAME_SIZE]; short type; diff --git a/src/iodine.c b/src/iodine.c index 250913e..9830c7d 100644 --- a/src/iodine.c +++ b/src/iodine.c @@ -141,12 +141,6 @@ build_hostname(char *buf, size_t buflen, return space; } -int -is_sending() -{ - return (packet.len != 0); -} - int read_dns(int fd, char *buf, int buflen) { @@ -166,15 +160,10 @@ read_dns(int fd, char *buf, int buflen) rv = dns_decode(buf, buflen, &q, QR_ANSWER, data, r); - if (is_sending() && chunkid == q.id) { + if (packet_sending(&packet) && chunkid == q.id) { /* Got ACK on sent packet */ - packet.offset += packet.sentlen; - if (packet.offset == packet.len) { - /* Packet completed */ - packet.offset = 0; - packet.len = 0; - packet.sentlen = 0; - } else { + packet_advance(&packet); + if (packet_sending(&packet)) { /* More to send */ send_chunk(fd); } @@ -199,11 +188,8 @@ tunnel_tun(int tun_fd, int dns_fd) inlen = read; compress2((uint8_t*)out, &outlen, (uint8_t*)in, inlen, 9); - memcpy(packet.data, out, MIN(outlen, sizeof(packet.data))); - packet.sentlen = 0; - packet.offset = 0; - packet.len = outlen; - + packet_fill(&packet, out, outlen); + send_chunk(dns_fd); return read; @@ -227,7 +213,7 @@ tunnel_dns(int tun_fd, int dns_fd) return -1; write_tun(tun_fd, out, outlen); - if (!is_sending()) + if (!packet_sending(&packet)) send_ping(dns_fd); return read; @@ -248,7 +234,7 @@ tunnel(int tun_fd, int dns_fd) tv.tv_usec = 0; FD_ZERO(&fds); - if (!is_sending()) + if (!packet_sending(&packet)) FD_SET(tun_fd, &fds); FD_SET(dns_fd, &fds); @@ -284,15 +270,18 @@ send_chunk(int fd) char buf[4096]; int avail; int code; + int sentlen; char *p; p = packet.data; p += packet.offset; - avail = packet.len - packet.offset; + avail = packet_len_to_send(&packet); - packet.sentlen = build_hostname(buf + 1, sizeof(buf) - 1, p, avail, topdomain, dataenc); + sentlen = build_hostname(buf + 1, sizeof(buf) - 1, p, avail, topdomain, dataenc); - if (packet.sentlen == avail) + packet_send_len(&packet, sentlen); + + if (sentlen == avail) code = 1; else code = 0; @@ -325,7 +314,7 @@ send_ping(int fd) { char data[3]; - if (is_sending()) { + if (packet_sending(&packet)) { packet.sentlen = 0; packet.offset = 0; packet.len = 0; diff --git a/src/packet.c b/src/packet.c new file mode 100644 index 0000000..f2ef5ed --- /dev/null +++ b/src/packet.c @@ -0,0 +1,62 @@ +#include + +#include "common.h" +#include "packet.h" + +/** + * Is some part of this packet sent? + */ +int +packet_sending(struct packet *packet) +{ + return (packet->len != 0); +} + +/** + * Acknowledge that the latest send was succesful + */ +void +packet_advance(struct packet *packet) +{ + packet->offset += packet->sentlen; + if (packet->offset == packet->len) { + /* Packet completed */ + packet->offset = 0; + packet->len = 0; + packet->sentlen = 0; + } +} + + +/** + * The length to left to send + */ +int +packet_len_to_send(struct packet *packet) +{ + return packet->len - packet->offset; +} + +/** + * Fill the packet with data + */ +int +packet_fill(struct packet *packet, char *data, unsigned long datalen) +{ + memcpy(packet->data, data, MIN(datalen, PKTSIZE)); + packet->sentlen = 0; + packet->offset = 0; + packet->len = datalen; + + return packet->len; +} + +/** + * Mark len number of bytes as being sent + */ +void +packet_send_len(struct packet *packet, int len) +{ + packet->sentlen = len; +} + diff --git a/src/packet.h b/src/packet.h new file mode 100644 index 0000000..2df702c --- /dev/null +++ b/src/packet.h @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2006-2007 Bjorn Andersson , Erik Ekman + * + * 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 __PACKET_H__ +#define __PACKET_H__ + +#define PKTSIZE (64*1024) + +struct packet +{ + int len; /* Total packet length */ + int sentlen; /* Length of chunk currently transmitted */ + int offset; /* Current offset */ + char data[PKTSIZE]; /* The data */ +}; + +int packet_sending(struct packet *); +void packet_advance(struct packet *); +int packet_len_to_send(struct packet *); +int packet_fill(struct packet *, char *, unsigned long); +void packet_send_len(struct packet *, int); + +#endif diff --git a/src/user.h b/src/user.h index 2d0d2ad..f2dfd44 100644 --- a/src/user.h +++ b/src/user.h @@ -17,6 +17,8 @@ #ifndef __USER_H__ #define __USER_H__ +#include "packet.h" + #define USERS 8 struct user {