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 as published by
24 * the Free Software Foundation, either version 3 of the License, or
27 * uAnytun is distributed in the hope that it will be useful,
28 * but WITHOUT ANY WARRANTY; without even the implied warranty of
29 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30 * GNU General Public License for more details.
32 * You should have received a copy of the GNU General Public License
33 * along with uAnytun. If not, see <http://www.gnu.org/licenses/>.
36 #include "datatypes.h"
38 #include "seq_window.h"
45 int seq_win_init(seq_win_t* win, window_size_t size)
56 void seq_win_clear(seq_win_t* win)
61 seq_win_element_t* ptr = win->first_;
63 seq_win_element_t* to_free = ptr;
66 free(to_free->window_);
72 seq_win_element_t* seq_win_new_element(sender_id_t sender_id, seq_nr_t max, window_size_t size)
77 seq_win_element_t* e = malloc(sizeof(seq_win_element_t));
81 e->sender_id_ = sender_id;
84 e->window_ = malloc(sizeof(seq_nr_t)*size);
89 memset(e->window_, 0, size);
90 e->window_[e->pos_] = 1;
96 int seq_win_check_and_add(seq_win_t* win, sender_id_t sender_id, seq_nr_t seq_nr)
104 seq_win_element_t* ptr = win->first_;
106 if(ptr->sender_id_ == sender_id) {
109 if(ptr->max_ < win->size_) {
110 ptr->max_ += SEQ_NR_MAX/2;
111 seq_nr += SEQ_NR_MAX/2;
114 else if(ptr->max_ > (SEQ_NR_MAX - win->size_)) {
115 ptr->max_ -= SEQ_NR_MAX/2;
116 seq_nr -= SEQ_NR_MAX/2;
120 seq_nr_t min = ptr->max_ - win->size_ + 1;
121 if(seq_nr < min || seq_nr == ptr->max_) {
123 ptr->max_ -= SEQ_NR_MAX/2;
124 else if(shifted == 2)
125 ptr->max_ += SEQ_NR_MAX/2;
129 if(seq_nr > ptr->max_) {
130 seq_nr_t diff = seq_nr - ptr->max_;
131 if(diff >= win->size_)
134 window_size_t new_pos = ptr->pos_ + diff;
136 if(new_pos >= win->size_) {
137 new_pos -= win->size_;
139 if(ptr->pos_ < win->size_ - 1)
140 memset(&(ptr->window_[ptr->pos_ + 1]), 0, win->size_ - ptr->pos_ - 1);
142 memset(ptr->window_, 0, new_pos);
145 memset(&(ptr->window_[ptr->pos_ + 1]), 0, diff);
148 ptr->window_[ptr->pos_] = 1;
152 ptr->max_ -= SEQ_NR_MAX/2;
153 else if(shifted == 2)
154 ptr->max_ += SEQ_NR_MAX/2;
159 seq_nr_t diff = ptr->max_ - seq_nr;
160 window_size_t pos = diff > ptr->pos_ ? ptr->pos_ + win->size_ : ptr->pos_;
164 ptr->max_ -= SEQ_NR_MAX/2;
165 else if(shifted == 2)
166 ptr->max_ += SEQ_NR_MAX/2;
168 int ret = ptr->window_[pos];
169 ptr->window_[pos] = 1;
175 win->first_ = seq_win_new_element(sender_id, seq_nr, win->size_);
183 ptr->next_ = seq_win_new_element(sender_id, seq_nr, win->size_);
191 void seq_win_print(seq_win_t* win)
193 printf("Sequence Window:\n");
198 seq_win_element_t* ptr = win->first_;
200 printf(" [%u]: (%u)-", ptr->sender_id_, ptr->max_);
201 window_size_t i = ptr->pos_;