#7 Move packet handling out of iodine.c and into packet.c

This commit is contained in:
Erik Ekman 2007-12-01 21:23:59 +00:00
parent 2d8b2655ea
commit c4a2b0c27f
6 changed files with 116 additions and 35 deletions

View File

@ -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)

View File

@ -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;

View File

@ -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;

62
src/packet.c Normal file
View File

@ -0,0 +1,62 @@
#include <string.h>
#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;
}

36
src/packet.h Normal file
View File

@ -0,0 +1,36 @@
/*
* Copyright (c) 2006-2007 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 __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

View File

@ -17,6 +17,8 @@
#ifndef __USER_H__
#define __USER_H__
#include "packet.h"
#define USERS 8
struct user {