/usr/include/libktorrent/utp/utpprotocol.h is in libktorrent-dev 1.3.1-5.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 | /***************************************************************************
* Copyright (C) 2009 by Joris Guisson *
* joris.guisson@gmail.com *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
***************************************************************************/
#ifndef UTP_UTPPROTOCOL_H
#define UTP_UTPPROTOCOL_H
#include <QtGlobal>
#include <QString>
#include <ktorrent_export.h>
#include <util/constants.h>
namespace utp
{
/*
UTP header:
0 4 8 16 24 32
+-------+-------+---------------+---------------+---------------+
| ver | type | extension | connection_id |
+-------+-------+---------------+---------------+---------------+
| timestamp_microseconds |
+---------------+---------------+---------------+---------------+
| timestamp_difference_microseconds |
+---------------+---------------+---------------+---------------+
| wnd_size |
+---------------+---------------+---------------+---------------+
| seq_nr | ack_nr |
+---------------+---------------+---------------+---------------+
*/
struct KTORRENT_EXPORT Header
{
unsigned int version: 4;
unsigned int type: 4;
bt::Uint8 extension;
bt::Uint16 connection_id;
bt::Uint32 timestamp_microseconds;
bt::Uint32 timestamp_difference_microseconds;
bt::Uint32 wnd_size;
bt::Uint16 seq_nr;
bt::Uint16 ack_nr;
void read(const bt::Uint8* data);
void write(bt::Uint8* data) const;
static bt::Uint32 size();
};
struct SelectiveAck
{
bt::Uint8 extension;
bt::Uint8 length;
bt::Uint8* bitmask;
};
struct ExtensionBits
{
bt::Uint8 extension;
bt::Uint8 length;
bt::Uint8 extension_bitmask[8];
};
struct UnknownExtension
{
bt::Uint8 extension;
bt::Uint8 length;
};
const bt::Uint8 SELECTIVE_ACK_ID = 1;
const bt::Uint8 EXTENSION_BITS_ID = 2;
// type field values
const bt::Uint8 ST_DATA = 0;
const bt::Uint8 ST_FIN = 1;
const bt::Uint8 ST_STATE = 2;
const bt::Uint8 ST_RESET = 3;
const bt::Uint8 ST_SYN = 4;
KTORRENT_EXPORT QString TypeToString(bt::Uint8 type);
enum ConnectionState
{
CS_IDLE,
CS_SYN_SENT,
CS_CONNECTED,
CS_FINISHED,
CS_CLOSED
};
const bt::Uint32 MIN_PACKET_SIZE = 150;
const bt::Uint32 MAX_PACKET_SIZE = 16384;
const bt::Uint32 DELAY_WINDOW_SIZE = 2*60*1000; // 2 minutes
const bt::Uint32 CCONTROL_TARGET = 100;
const bt::Uint32 MAX_CWND_INCREASE_PACKETS_PER_RTT = 500;
const bt::Uint32 MAX_TIMEOUT = 30000;
const bt::Uint32 CONNECT_TIMEOUT = 30000;
const bt::Uint32 KEEP_ALIVE_TIMEOUT = 30000;
const bt::Uint32 IP_AND_UDP_OVERHEAD = 28;
/*
Test if a bit is acked
UTP standard:
The bitmask has reverse byte order. The first byte represents packets [ack_nr + 2, ack_nr + 2 + 7] in reverse order. The least significant bit in the byte represents ack_nr + 2, the most significant bit in the byte represents ack_nr + 2 + 7. The next byte in the mask represents [ack_nr + 2 + 8, ack_nr + 2 + 15] in reverse order, and so on. The bitmask is not limited to 32 bits but can be of any size.
Here is the layout of a bitmask representing the first 32 packet acks represented in a selective ACK bitfield:
0 8 16
+---------------+---------------+---------------+---------------+
| 9 8 ... 3 2 | 17 ... 10 | 25 ... 18 | 33 ... 26 |
+---------------+---------------+---------------+---------------+
The number in the diagram maps the bit in the bitmask to the offset to add to ack_nr in order to calculate the sequence number that the bit is ACKing.
*/
inline bool Acked(const SelectiveAck* sack, bt::Uint16 bit)
{
// check bounds
if (bit < 2 || bit > 8*sack->length + 1)
return false;
const bt::Uint8* bitset = sack->bitmask;
int byte = (bit - 2) / 8;
int bit_off = (bit - 2) % 8;
return bitset[byte] & (0x01 << bit_off);
}
// Turn on a bit in the SelectiveAck
inline void Ack(SelectiveAck* sack, bt::Uint16 bit)
{
// check bounds
if (bit < 2 || bit > 8*sack->length + 1)
return;
bt::Uint8* bitset = sack->bitmask;
int byte = (bit - 2) / 8;
int bit_off = (bit - 2) % 8;
bitset[byte] |= (0x01 << bit_off);
}
/**
Helper class to parse packets
*/
class KTORRENT_EXPORT PacketParser
{
public:
PacketParser(const QByteArray & packet);
PacketParser(const bt::Uint8* packet, bt::Uint32 size);
~PacketParser();
/// Parses the packet, returns false on error
bool parse();
const Header* header() const {return &hdr;}
const SelectiveAck* selectiveAck() const;
bt::Uint32 dataOffset() const {return data_off;}
bt::Uint32 dataSize() const {return data_size;}
private:
const bt::Uint8* packet;
bt::Uint32 size;
Header hdr;
bool sack_found;
SelectiveAck sack;
bt::Uint32 data_off;
bt::Uint32 data_size;
};
/// <= for sequence numbers (taking into account wrapping around)
inline bool SeqNrCmpSE(bt::Uint16 a, bt::Uint16 b)
{
if (qAbs(b - a) < 32768)
return a <= b;
else
return a > b;
}
/// < for sequence numbers (taking into account wrapping around)
inline bool SeqNrCmpS(bt::Uint16 a, bt::Uint16 b)
{
if (qAbs(b - a) < 32768)
return a < b;
else
return a > b;
}
/// Calculates difference between two sequence numbers (taking into account wrapping around)
inline bt::Uint16 SeqNrDiff(bt::Uint16 a, bt::Uint16 b)
{
if (qAbs(b - a) < 32768)
return b - a;
else if (b > a)
return a + (65536 - b);
else
return b + (65536 - a);
}
}
#endif // UTP_UTPPROTOCOL_H
|