This file is indexed.

/usr/include/tins/pdu_option.h is in libtins-dev 3.4-2build1.

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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
/*
 * Copyright (c) 2016, Matias Fontanini
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 * 
 * * Redistributions of source code must retain the above copyright
 *   notice, this list of conditions and the following disclaimer.
 * * Redistributions in binary form must reproduce the above
 *   copyright notice, this list of conditions and the following disclaimer
 *   in the documentation and/or other materials provided with the
 *   distribution.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 */

#ifndef TINS_PDU_OPTION_H
#define TINS_PDU_OPTION_H

#include <vector>
#include <iterator>
#include <cstring>
#include <algorithm>
#include <string>
#include <limits>
#include <stdint.h>
#include "exceptions.h"
#include "endianness.h"
#include "internals.h"
#include "ip_address.h"
#include "ipv6_address.h"
#include "hw_address.h"

namespace Tins {
/**
 * \cond
 */
template <typename OptionType, typename PDUType>
class PDUOption;

namespace Internals {
    template <typename T, typename X, typename PDUType>
    T convert_to_integral(const PDUOption<X, PDUType> & opt) {
        if (opt.data_size() != sizeof(T)) {
            throw malformed_option();
        }
        T data = *(T*)opt.data_ptr();
        if (PDUType::endianness == PDUType::BE) {
            data = Endian::be_to_host(data);
        }
        else {
            data = Endian::le_to_host(data);
        }
        return data;
    }
    
    template <typename T, typename = void>
    struct converter {
        template <typename X, typename PDUType>
        static T convert(const PDUOption<X, PDUType>& opt) {
            return T::from_option(opt);
        }
    };
    
    template <>
    struct converter<uint8_t> {
        template <typename X, typename PDUType>
        static uint8_t convert(const PDUOption<X, PDUType>& opt) {
            if (opt.data_size() != 1) {
                throw malformed_option();
            }
            return* opt.data_ptr();
        }
    };
    
    template<>
    struct converter<uint16_t> {
        template<typename X, typename PDUType>
        static uint16_t convert(const PDUOption<X, PDUType>& opt) {
            return convert_to_integral<uint16_t>(opt);
        }
    };
    
    template<>
    struct converter<uint32_t> {
        template<typename X, typename PDUType>
        static uint32_t convert(const PDUOption<X, PDUType>& opt) {
            return convert_to_integral<uint32_t>(opt);
        }
    };
    
    template<>
    struct converter<uint64_t> {
        template<typename X, typename PDUType>
        static uint64_t convert(const PDUOption<X, PDUType>& opt) {
            return convert_to_integral<uint64_t>(opt);
        }
    };

    template<size_t n>
    struct converter<HWAddress<n> > {
        template<typename X, typename PDUType>
        static HWAddress<n> convert(const PDUOption<X, PDUType>& opt) {
            if (opt.data_size() != n) {
                throw malformed_option();
            }
            return HWAddress<n>(opt.data_ptr());
        }
    };

    template<>
    struct converter<IPv4Address> {
        template<typename X, typename PDUType>
        static IPv4Address convert(const PDUOption<X, PDUType>& opt) {
            if (opt.data_size() != sizeof(uint32_t)) {
                throw malformed_option();
            }
            const uint32_t* ptr = (const uint32_t*)opt.data_ptr();
            if (PDUType::endianness == PDUType::BE) {
                return IPv4Address(*ptr);
            }
            else {
                return IPv4Address(Endian::change_endian(*ptr));
            }
        }
    };

    template<>
    struct converter<IPv6Address> {
        template<typename X, typename PDUType>
        static IPv6Address convert(const PDUOption<X, PDUType>& opt) {
            if (opt.data_size() != IPv6Address::address_size) {
                throw malformed_option();
            }
            return IPv6Address(opt.data_ptr());
        }
    };
    
    template<>
    struct converter<std::string> {
        template<typename X, typename PDUType>
        static std::string convert(const PDUOption<X, PDUType>& opt) {
            return std::string(
                opt.data_ptr(),
                opt.data_ptr() + opt.data_size()
            );
        }
    };
    
    template<>
    struct converter<std::vector<float> > {
        template<typename X, typename PDUType>
        static std::vector<float> convert(const PDUOption<X, PDUType>& opt) {
            std::vector<float> output;
            const uint8_t* ptr = opt.data_ptr(), *end = ptr + opt.data_size();
            while (ptr != end) {
                output.push_back(float(*(ptr++) & 0x7f) / 2);
            }
            return output;
        }
    };
    
    template<typename T>
    struct converter<std::vector<T>, typename enable_if<is_unsigned_integral<T>::value>::type> {
        template<typename X, typename PDUType>
        static std::vector<T> convert(const PDUOption<X, PDUType>& opt) {
            if (opt.data_size() % sizeof(T) != 0) {
                throw malformed_option();
            }
            const T* ptr = (const T*)opt.data_ptr();
            const T* end = (const T*)(opt.data_ptr() + opt.data_size());
            
            std::vector<T> output(std::distance(ptr, end));
            typename std::vector<T>::iterator it = output.begin();
            while (ptr < end) {
                if (PDUType::endianness == PDUType::BE) {
                    *it++ = Endian::be_to_host(*ptr++);
                }
                else {
                    *it++ = Endian::le_to_host(*ptr++);
                }
            }
            return output;
        }
    };
    
    template<typename T, typename U>
    struct converter<
            std::vector<std::pair<T, U> >, 
            typename enable_if<
                is_unsigned_integral<T>::value && is_unsigned_integral<U>::value
            >::type
    > {
        template<typename X, typename PDUType>
        static std::vector<std::pair<T, U> > convert(const PDUOption<X, PDUType>& opt) {
            if (opt.data_size() % (sizeof(T) + sizeof(U)) != 0) {
                throw malformed_option();
            }
            const uint8_t* ptr = opt.data_ptr(), *end = ptr + opt.data_size();
            
            std::vector<std::pair<T, U> > output;
            while (ptr < end) {
                std::pair<T, U> data;
                data.first = *(const T*)ptr;
                ptr += sizeof(T);
                data.second = *(const U*)ptr;
                ptr += sizeof(U);
                if (PDUType::endianness == PDUType::BE) {
                    data.first = Endian::be_to_host(data.first);
                    data.second = Endian::be_to_host(data.second);
                }
                else {
                    data.first = Endian::le_to_host(data.first);
                    data.second = Endian::le_to_host(data.second);
                }
                output.push_back(data);
            }
            return output;
        }
    };
    
    template<>
    struct converter<std::vector<IPv4Address> > {
        template<typename X, typename PDUType>
        static std::vector<IPv4Address> convert(const PDUOption<X, PDUType>& opt) {
            if (opt.data_size() % 4 != 0) {
                throw malformed_option();
            }
            const uint32_t* ptr = (const uint32_t*)opt.data_ptr();
            const uint32_t* end = (const uint32_t*)(opt.data_ptr() + opt.data_size());
            
            std::vector<IPv4Address> output(std::distance(ptr, end));
            std::vector<IPv4Address>::iterator it = output.begin();
            while (ptr < end) {
                if (PDUType::endianness == PDUType::BE) {
                    *it++ = IPv4Address(*ptr++);
                }
                else {
                    *it++ = IPv4Address(Endian::change_endian(*ptr++));
                }
            }
            return output;
        }
    };
    
    template<>
    struct converter<std::vector<IPv6Address> > {
        template<typename X, typename PDUType>
        static std::vector<IPv6Address> convert(const PDUOption<X, PDUType>& opt) {
            if (opt.data_size() % IPv6Address::address_size != 0) {
                throw malformed_option();
            }
            const uint8_t* ptr = opt.data_ptr(), *end = opt.data_ptr() + opt.data_size();
            std::vector<IPv6Address> output;
            while (ptr < end) {
                output.push_back(IPv6Address(ptr));
                ptr += IPv6Address::address_size;
            }
            return output;
        }
    };
    
    template<typename T, typename U>
    struct converter<
            std::pair<T, U>, 
            typename enable_if<
                is_unsigned_integral<T>::value && is_unsigned_integral<U>::value
            >::type
    > {
        template<typename X, typename PDUType>
        static std::pair<T, U> convert(const PDUOption<X, PDUType>& opt) {
            if (opt.data_size() != sizeof(T) + sizeof(U)) {
                throw malformed_option();
            }
            std::pair<T, U> output;
            std::memcpy(&output.first, opt.data_ptr(), sizeof(T));
            std::memcpy(&output.second, opt.data_ptr() + sizeof(T), sizeof(U));
            if (PDUType::endianness == PDUType::BE) {
                output.first = Endian::be_to_host(output.first);
                output.second = Endian::be_to_host(output.second);
            }
            else {
                output.first = Endian::le_to_host(output.first);
                output.second = Endian::le_to_host(output.second);
            }
            return output;
        }
    };
}

/**
 * \endcond
 */

/**
 * \class PDUOption
 * \brief Represents a PDU option field.
 * 
 * Several PDUs, such as TCP, IP, Dot11 or DHCP contain options. All
 * of them behave exactly the same way. This class represents those
 * options.
 * 
 * The OptionType template parameter indicates the type that will be
 * used to store this option's identifier.
 */
template <typename OptionType, typename PDUType>
class PDUOption {
private:
    static const int small_buffer_size = 8;
public:
    typedef uint8_t data_type;
    typedef OptionType option_type;

    /**
     * \brief Constructs a PDUOption.
     * \param opt The option type.
     * \param length The option's data length.
     * \param data The option's data(if any).
     */
    PDUOption(option_type opt = option_type(), 
              size_t length = 0,
              const data_type* data = 0) 
    : option_(opt), size_(static_cast<uint16_t>(length)) {
        set_payload_contents(data, data + (data ? length : 0));
    }
    
    /**
     * \brief Copy constructor.
     * \param rhs The PDUOption to be copied.
     */
    PDUOption(const PDUOption& rhs) {
        real_size_ = 0;
        *this = rhs;
    }
    
    #if TINS_IS_CXX11
    /**
     * \brief Move constructor.
     * \param rhs The PDUOption to be moved.
     */
    PDUOption(PDUOption&& rhs) {
        real_size_ = 0;
        *this = std::move(rhs);
    }
    
    /**
     * \brief Move assignment operator.
     * \param rhs The PDUOption to be moved.
     */
    PDUOption& operator=(PDUOption&& rhs) {
        option_ = rhs.option_;
        size_ = rhs.size_;
        if (real_size_ > small_buffer_size) {
            delete[] payload_.big_buffer_ptr;
        }
        real_size_ = rhs.real_size_;
        if (real_size_ > small_buffer_size) {
            payload_.big_buffer_ptr = 0;
            std::swap(payload_.big_buffer_ptr, rhs.payload_.big_buffer_ptr);
            rhs.real_size_ = 0;
        }
        else {
            std::copy(
                rhs.data_ptr(),
                rhs.data_ptr() + rhs.data_size(),
                payload_.small_buffer
            );
        }
        return* this;
    }
    
    #endif // TINS_IS_CXX11
    
    /**
     * \brief Copy assignment operator.
     * \param rhs The PDUOption to be copied.
     */
    PDUOption& operator=(const PDUOption& rhs) {
        option_ = rhs.option_;
        size_ = rhs.size_;
        if (real_size_ > small_buffer_size) {
            delete[] payload_.big_buffer_ptr;
        }
        real_size_ = rhs.real_size_;
        set_payload_contents(rhs.data_ptr(), rhs.data_ptr() + rhs.data_size());
        return* this;
    }
    
    /**
     * \brief Destructor.
     */
    ~PDUOption() {
        if (real_size_ > small_buffer_size) {
            delete[] payload_.big_buffer_ptr;
        }
    }
    
    /**
     * \brief Constructs a PDUOption from iterators, which 
     * indicate the data to be stored in it.
     * 
     * \param opt The option type.
     * \param start The beginning of the option data.
     * \param end The end of the option data.
     */
    template<typename ForwardIterator>
    PDUOption(option_type opt, ForwardIterator start, ForwardIterator end) 
    : option_(opt), size_(static_cast<uint16_t>(std::distance(start, end))) {
        set_payload_contents(start, end);
    }
    
    /**
     * \brief Constructs a PDUOption from iterators, which 
     * indicate the data to be stored in it.
     * 
     * The length parameter indicates the contents of the length field
     * when this option is serialized. Note that this can be different
     * to std::distance(start, end).
     * 
     * \sa length_field
     * 
     * \param opt The option type.
     * \param length The length of this option.
     * \param start The beginning of the option data.
     * \param end The end of the option data.
     */
    template<typename ForwardIterator>
    PDUOption(option_type opt, uint16_t length, ForwardIterator start, ForwardIterator end) 
    : option_(opt), size_(length) {
        set_payload_contents(start, end);
    }
    
    /**
     * Retrieves this option's type.
     * \return uint8_t containing this option's size.
     */
    option_type option() const {
        return option_;
    }
    
    /**
     * Sets this option's type
     * \param opt The option type to be set.
     */
    void option(option_type opt) {
        option_ = opt;
    }
    
    /**
     * Retrieves this option's data.
     * 
     * If this method is called when data_size() == 0, 
     * dereferencing the returned pointer will result in undefined
     * behaviour.
     * 
     * \return const data_type& containing this option's value.
     */
    const data_type* data_ptr() const {
        return real_size_ <= small_buffer_size ?
               payload_.small_buffer : 
               payload_.big_buffer_ptr;
    }
    
    /**
     * \brief Retrieves the length of this option's data.
     * 
     * This is the actual size of the data.
     */
    size_t data_size() const {
        return real_size_;
    }
    
    /**
     * \brief Retrieves the data length field.
     * 
     * This is what the size field will contain when this option is 
     * serialized. It can differ from the actual data size. 
     * 
     * This will be equal to data_size unless the constructor that takes
     * both a data length and two iterators is used.

     * 
     * \sa data_size.
     */
    size_t length_field() const {
        return size_;
    }
    
    /**
     * \brief Constructs a T from this PDUOption.
     * 
     * Use this method to convert a PDUOption to the specific type that
     * represents it. For example, if you know an option is of type
     * PDU::SACK, you could use option.to<TCP::sack_type>().
     */
    template<typename T>
    T to() const {
        return Internals::converter<T>::convert(*this);
    }
private:
    template<typename ForwardIterator>
    void set_payload_contents(ForwardIterator start, ForwardIterator end) {
        size_t total_size = std::distance(start, end);
        if (total_size > std::numeric_limits<uint16_t>::max()) {
            throw option_payload_too_large();
        }
        real_size_ = static_cast<uint16_t>(total_size);
        if (real_size_ <= small_buffer_size) {
            std::copy(
                start,
                end,
                payload_.small_buffer
            );
        }
        else {
            payload_.big_buffer_ptr = new data_type[real_size_];
            uint8_t* ptr = payload_.big_buffer_ptr;
            while (start < end) {
                *ptr = *start;
                ++ptr;
                ++start;
            }
        }
    }

    option_type option_;
    uint16_t size_, real_size_;
    union {
        data_type small_buffer[small_buffer_size];
        data_type* big_buffer_ptr;
    } payload_;
};

namespace Internals {
    /*
     * \cond
     */
    template <typename Option>
    struct option_type_equality_comparator {
        option_type_equality_comparator(typename Option::option_type type) : type(type) { }

        bool operator()(const Option& opt) const {
            return opt.option() == type;
        }

        typename Option::option_type type; 
    };
    /*
     * \endcond
     */
} // Internals

} // namespace Tins
#endif // TINS_PDU_OPTION_H