4 * uAnytun is a tiny implementation of SATP. Unlike Anytun which is a full
5 * featured implementation uAnytun has no support for multiple connections
6 * or synchronisation. It is a small single threaded implementation intended
7 * to act as a client on small platforms.
8 * The secure anycast tunneling protocol (satp) defines a protocol used
9 * for communication between any combination of unicast and anycast
10 * tunnel endpoints. It has less protocol overhead than IPSec in Tunnel
11 * mode and allows tunneling of every ETHER TYPE protocol (e.g.
12 * ethernet, ip, arp ...). satp directly includes cryptography and
13 * message authentication based on the methodes used by SRTP. It is
14 * intended to deliver a generic, scaleable and secure solution for
15 * tunneling and relaying of packets of any protocol.
18 * Copyright (C) 2007-2008 Christian Pointner <equinox@anytun.org>
20 * This file is part of uAnytun.
22 * uAnytun is free software: you can redistribute it and/or modify
23 * it under the terms of the GNU General Public License version 3 as
24 * published by the Free Software Foundation.
26 * uAnytun is distributed in the hope that it will be useful,
27 * but WITHOUT ANY WARRANTY; without even the implied warranty of
28 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
29 * GNU General Public License for more details.
31 * You should have received a copy of the GNU General Public License
32 * along with uAnytun. If not, see <http://www.gnu.org/licenses/>.
35 #include "datatypes.h"
37 #include "seq_window.h"
44 int seq_win_init(seq_win_t* win, window_size_t size)
55 void seq_win_clear(seq_win_t* win)
60 seq_win_element_t* ptr = win->first_;
62 seq_win_element_t* to_free = ptr;
65 free(to_free->window_);
71 seq_win_element_t* seq_win_new_element(sender_id_t sender_id, seq_nr_t max, window_size_t size)
76 seq_win_element_t* e = malloc(sizeof(seq_win_element_t));
80 e->sender_id_ = sender_id;
83 e->window_ = malloc(sizeof(seq_nr_t)*size);
88 memset(e->window_, 0, size);
89 e->window_[e->pos_] = 1;
95 int seq_win_check_and_add(seq_win_t* win, sender_id_t sender_id, seq_nr_t seq_nr)
103 seq_win_element_t* ptr = win->first_;
105 if(ptr->sender_id_ == sender_id) {
108 if(ptr->max_ < win->size_) {
109 ptr->max_ += SEQ_NR_MAX/2;
110 seq_nr += SEQ_NR_MAX/2;
113 else if(ptr->max_ > (SEQ_NR_MAX - win->size_)) {
114 ptr->max_ -= SEQ_NR_MAX/2;
115 seq_nr -= SEQ_NR_MAX/2;
119 seq_nr_t min = ptr->max_ - win->size_ + 1;
120 if(seq_nr < min || seq_nr == ptr->max_) {
122 ptr->max_ -= SEQ_NR_MAX/2;
123 else if(shifted == 2)
124 ptr->max_ += SEQ_NR_MAX/2;
128 if(seq_nr > ptr->max_) {
129 seq_nr_t diff = seq_nr - ptr->max_;
130 if(diff >= win->size_)
133 window_size_t new_pos = ptr->pos_ + diff;
135 if(new_pos >= win->size_) {
136 new_pos -= win->size_;
138 if(ptr->pos_ < win->size_ - 1)
139 memset(&(ptr->window_[ptr->pos_ + 1]), 0, win->size_ - ptr->pos_ - 1);
141 memset(ptr->window_, 0, new_pos);
144 memset(&(ptr->window_[ptr->pos_ + 1]), 0, diff);
147 ptr->window_[ptr->pos_] = 1;
151 ptr->max_ -= SEQ_NR_MAX/2;
152 else if(shifted == 2)
153 ptr->max_ += SEQ_NR_MAX/2;
158 seq_nr_t diff = ptr->max_ - seq_nr;
159 window_size_t pos = diff > ptr->pos_ ? ptr->pos_ + win->size_ : ptr->pos_;
163 ptr->max_ -= SEQ_NR_MAX/2;
164 else if(shifted == 2)
165 ptr->max_ += SEQ_NR_MAX/2;
167 int ret = ptr->window_[pos];
168 ptr->window_[pos] = 1;
174 win->first_ = seq_win_new_element(sender_id, seq_nr, win->size_);
182 ptr->next_ = seq_win_new_element(sender_id, seq_nr, win->size_);
190 void seq_win_print(seq_win_t* win)
192 printf("Sequence Window:\n");
197 seq_win_element_t* ptr = win->first_;
199 printf(" [%u]: (%u)-", ptr->sender_id_, ptr->max_);
200 window_size_t i = ptr->pos_;