This file is indexed.

/usr/include/tins/ip.h is in libtins-dev 1.1-1.

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
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
/*
 * Copyright (c) 2012, 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_IP_H
#define TINS_IP_H

#include <list>
#include "pdu.h"
#include "small_uint.h"
#include "endianness.h"
#include "ip_address.h"
#include "pdu_option.h"
#include "macros.h"
#include "cxxstd.h"

namespace Tins {

    /**
     * \brief Class that represents an IP PDU.
     * 
     * By default, IP PDUs are initialized, setting TTL to IP::DEFAULT_TTL,
     * id field to 1 and version to 4. Taking this into account, users
     * should set destination and source port and would be enough to send one.
     */
    class IP : public PDU {
    public:
        /**
         * This PDU's flag.
         */
        static const PDU::PDUType pdu_flag = PDU::IP;
    
        /**
         * The type used to store addresses.
         */
        typedef IPv4Address address_type;

        /**
         * \brief Enum indicating the option's class.
         *
         * Enum OptionClass represents the different classes of
         * IP Options.
         */
        enum OptionClass {
            CONTROL = 0,
            MEASUREMENT = 2
        };

        /**
         * \brief Enum indicating the option's id number.
         *
         * Enum Option indicates the possible IP Options.
         */
        enum OptionNumber {
            END = 0,
            NOOP = 1,
            SEC = 2,
            LSSR = 3,
            TIMESTAMP = 4,
            EXTSEC = 5,
            RR = 7,
            SID = 8,
            SSRR = 9,
            MTUPROBE = 11,
            MTUREPLY = 12,
            EIP = 17,
            TR = 18,
            ADDEXT = 19,
            RTRALT = 20,
            SDB = 21,
            DPS = 23,
            UMP = 24,
            QS = 25
        };
        
        /**
         * \brief The type used to represent an option's type.
         */
        TINS_BEGIN_PACK
        struct option_identifier {
        #if TINS_IS_LITTLE_ENDIAN
            uint8_t number:5,
                    op_class:2,
                    copied:1;
        #elif TINS_IS_BIG_ENDIAN
            uint8_t copied:1,
                    op_class:2,
                    number:5;
        #endif
            /**
             * \brief Default constructor.
             * 
             * Initializes every field to 0.
             */
            option_identifier() 
            #if TINS_IS_LITTLE_ENDIAN
            : number(0), op_class(0), copied(0) {}
            #else
            : copied(0), op_class(0), number(0) {}
            #endif
            
            /**
             * \brief Constructs this option from a single uint8_t value.
             * 
             * This parses the value and initializes each field with the
             * appropriate value.
             * 
             * \param value The value to be parsed and used for 
             * initialization
             */
            option_identifier(uint8_t value) 
            #if TINS_IS_LITTLE_ENDIAN
            : number(value & 0x1f), 
              op_class((value >> 5) & 0x03), 
              copied((value >> 7) & 0x01) {}
            #elif TINS_IS_BIG_ENDIAN
            : copied((value >> 7) & 0x01),
              op_class((value >> 5) & 0x03), 
              number(value & 0x1f) {}
            #endif
            
            /**
             * Constructor using user provided values for each field.
             * \param number The number field value.
             * \param op_class The option class field value.
             * \param copied The copied field value.
             */
            option_identifier(OptionNumber number, OptionClass op_class,
              small_uint<1> copied) 
            #if TINS_IS_LITTLE_ENDIAN
            : number(number), op_class(op_class), copied(copied) {}
            #else
            : copied(copied), op_class(op_class), number(number) {}
            #endif
            
            /**
             * \brief Equality operator.
             */
            bool operator==(const option_identifier &rhs) const {
                return number == rhs.number && op_class == rhs.op_class && copied == rhs.copied;
            }
        } TINS_END_PACK;
        
        /**
         * The IP options type.
         */
        typedef PDUOption<option_identifier> option;

        /**
         * The type of the security option.
         */
        struct security_type {
            uint16_t security, compartments;
            uint16_t handling_restrictions;
            small_uint<24> transmission_control;
            
            security_type(uint16_t sec = 0, uint16_t comp = 0,
              uint16_t hand_res = 0, small_uint<24> tcc = 0)
            : security(sec), compartments(comp), 
              handling_restrictions(hand_res), transmission_control(tcc) 
              {}
        };
        
        /**
         * The type of the Loose Source and Record Route
         */
        struct generic_route_option_type {
            typedef std::vector<address_type> routes_type;
            
            uint8_t pointer;
            routes_type routes;
            
            generic_route_option_type(uint8_t ptr = 0, 
              routes_type rts = routes_type())
            : pointer(ptr), routes(rts) {}
        };
        
        /**
         * The type of the Loose Source and Record Route
         */
        typedef generic_route_option_type lsrr_type;
        
        /**
         * The type of the Strict Source and Record Route
         */
        typedef generic_route_option_type ssrr_type;
        
        /**
         * The type of the Record Route
         */
        typedef generic_route_option_type record_route_type;

        /**
         * The type used to store IP options.
         */
        typedef std::list<option> options_type;

        /**
         * \brief Constructor for building the IP PDU.
         *
         * Both the destination and source IP address can be supplied.
         * By default, those fields are initialized using the IP 
         * address 0.0.0.0.
         *
         * \param ip_dst The destination ip address(optional).
         * \param ip_src The source ip address(optional).
         */
        IP(address_type ip_dst = address_type(), 
            address_type ip_src = address_type());

        /**
         * \brief Constructs an IP object from a buffer and adds all 
         * identifiable PDUs found in the buffer as children of this 
         * one.
         * 
         * If there is not enough size for an IP header, a 
         * malformed_packet exception is thrown.
         * 
         * \param buffer The buffer from which this PDU will be constructed.
         * \param total_sz The total size of the buffer.
         */
        IP(const uint8_t *buffer, uint32_t total_sz);

        /* Getters */

        /**
         * \brief Getter for the header length field.
         *
         * \return The number of dwords the header occupies in an uin8_t.
         */
        small_uint<4> head_len() const { return this->_ip.ihl; }

        /**
         * \brief Getter for the type of service field.
         *
         * \return The this IP PDU's type of service.
         */
        uint8_t tos() const { return _ip.tos; }

        /**
         * \brief Getter for the total length field.
         *
         * \return The total length of this IP PDU.
         */
        uint16_t tot_len() const { 
            return Endian::be_to_host(_ip.tot_len); 
        }

        /**
         * \brief Getter for the id field.
         *
         * \return The id for this IP PDU.
         */
        uint16_t id() const { return Endian::be_to_host(_ip.id); }

        /**
         * \brief Getter for the fragment offset field.
         *
         * \return The fragment offset for this IP PDU.
         */
        uint16_t frag_off() const { return Endian::be_to_host(_ip.frag_off); }

        /**
         * \brief Getter for the time to live field.
         *
         * \return The time to live for this IP PDU.
         */
        uint8_t ttl() const { return _ip.ttl; }

        /**
         * \brief Getter for the protocol field.
         *
         * \return The protocol for this IP PDU.
         */
        uint8_t protocol() const { return _ip.protocol; }

        /**
         * \brief Getter for the checksum field.
         *
         * \return The checksum for this IP PDU.
         */
        uint16_t checksum() const { return Endian::be_to_host(_ip.check); }

        /**
         * \brief Getter for the source address field.
         *
         * \return The source address for this IP PDU.
         */
        address_type src_addr() const { return address_type(_ip.saddr); }

        /** 
         * \brief Getter for the destination address field.
         * \return The destination address for this IP PDU.
         */
        address_type dst_addr() const  { return address_type(_ip.daddr); }
        
        /** 
         * \brief Getter for the version field.
         * \return The version for this IP PDU.
         */
        small_uint<4> version() const  { return _ip.version; }

        /** 
         * \brief Getter for the IP options.
         * \return The stored options.
         */
        const options_type &options() const  { return _ip_options; }

        /* Setters */

        /**
         * \brief Setter for the header length field.
         *
         * \param new_head_len The new header length.
         */
        void head_len(small_uint<4> new_head_len);

        /**
         * \brief Setter for the type of service field.
         *
         * \param new_tos The new type of service.
         */
        void tos(uint8_t new_tos);

        /**
         * \brief Setter for the total length field.
         *
         * \param new_tot_len The new total length.
         */
        void tot_len(uint16_t new_tot_len);

        /**
         * \brief Setter for the id field.
         *
         * \param new_id The new id.
         */
        void id(uint16_t new_id);

        /**
         * \brief Setter for the fragment offset field.
         *
         * \param new_frag_off The new fragment offset.
         */
        void frag_off(uint16_t new_frag_off);

        /**
         * \brief Setter for the time to live field.
         *
         * \param new_ttl The new time to live.
         */
        void ttl(uint8_t new_ttl);

        /**
         * \brief Setter for the protocol field.
         *
         * \param new_protocol The new protocol.
         */
        void protocol(uint8_t new_protocol);

        /**
         * \brief Setter for the source address field.
         *
         * \param ip The source address to be set.
         */
        void src_addr(address_type ip);

        /**
         * \brief Setter for the destination address field.
         *
         * \param ip The destination address to be set.
         */
        void dst_addr(address_type ip);
        
        /**
         * \brief Setter for the version field.
         *
         * \param ver The version field to be set.
         */
        void version(small_uint<4> ver);

        /**
         * \brief Adds an IP option.
         * 
         * The option is added after the last option in the option 
         * fields.
         * 
         * \param opt The option to be added
         */
        void add_option(const option &opt);
        
        #if TINS_IS_CXX11
            /**
             * \brief Adds an IP option.
             * 
             * The option is move-constructed.
             * 
             * \param opt The option to be added.
             */
            void add_option(option &&opt) {
                internal_add_option(opt);
                _ip_options.push_back(std::move(opt));
            }
        #endif

        /**
         * \brief Searchs for an option that matchs the given flag.
         * 
         * If the option is not found, a null pointer is returned. 
         * Deleting the returned pointer will result in <b>undefined 
         * behaviour</b>.
         * 
         * \param id The option identifier to be searched.
         */
        const option *search_option(option_identifier id) const;

        // Option setters
        
        /**
         * \brief Adds an End Of List option.
         */
        void eol();

        /**
         * \brief Adds a NOP option.
         */
        void noop();

        /**
         * \brief Adds a security option.
         *
         * \param data The data to be stored in this option.
         */
        void security(const security_type &data);
        
        /**
         * \brief Adds a Loose Source and Record Route option.
         *
         * \param data The data to be stored in this option.
         */
        void lsrr(const lsrr_type &data) {
            add_route_option(131, data);
        }
        
        /**
         * \brief Adds a Strict Source and Record Route option.
         *
         * \param data The data to be stored in this option.
         */
        void ssrr(const ssrr_type &data) {
            add_route_option(137, data);
        }
        
        /**
         * \brief Adds a Record Route option.
         *
         * \param data The data to be stored in this option.
         */
        void record_route(const record_route_type &data) {
            add_route_option(7, data);
        }
        
        /**
         * \brief Adds a Stream Identifier option.
         *
         * \param stream_id The stream id to be stored in this option.
         */
        void stream_identifier(uint16_t stream_id);
        
        // Option getters
        
        /**
         * \brief Searchs and returns a security option.
         * 
         * If no such option exists, an option_not_found exception
         * is thrown.
         * 
         * \return security_type containing the option found.
         */
        security_type security() const;
        
        /**
         * \brief Searchs and returns a Loose Source and Record Route 
         * option.
         * 
         * If no such option exists, an option_not_found exception
         * is thrown.
         * 
         * \return lsrr_type containing the option found.
         */
        lsrr_type lsrr() const {
            return search_route_option(131);
        }
        
        /**
         * \brief Searchs and returns a Strict Source and Record Route 
         * option.
         * 
         * If no such option exists, an option_not_found exception
         * is thrown.
         * 
         * \return ssrr_type containing the option found.
         */
        ssrr_type ssrr() const {
            return search_route_option(137);
        }
        
        /**
         * \brief Searchs and returns a Record Route option.
         * 
         * If no such option exists, an option_not_found exception
         * is thrown.
         * 
         * \return record_route_type containing the option found.
         */
        record_route_type record_route() const {
            return search_route_option(7);
        }

        /**
         * \brief Searchs and returns a Stream Identifier option.
         * 
         * If no such option exists, an option_not_found exception
         * is thrown.
         * 
         * \return uint16_t containing the option found.
         */
        uint16_t stream_identifier() const;

        /* Virtual methods */

        /**
         * \brief Returns the header size.
         *
         * This metod overrides PDU::header_size. \sa PDU::header_size
         */
        uint32_t header_size() const;

        /**
         * \sa PDU::send()
         */
        void send(PacketSender &sender, const NetworkInterface &);

        /**
         * \brief Check wether ptr points to a valid response for this PDU.
         *
         * \sa PDU::matches_response
         * \param ptr The pointer to the buffer.
         * \param total_sz The size of the buffer.
         */
        bool matches_response(const uint8_t *ptr, uint32_t total_sz) const;

        /**
         * \brief Receives a matching response for this packet.
         *
         * \sa PDU::recv_response
         * \param sender The packet sender which will receive the packet.
         */
        PDU *recv_response(PacketSender &sender, const NetworkInterface &);

        /**
         * \brief Getter for the PDU's type.
         * \sa PDU::pdu_type
         */
        PDUType pdu_type() const { return PDU::IP; }

        /**
         * \sa PDU::clone
         */
        IP *clone() const {
            return new IP(*this);
        }
    private:
        static const uint8_t DEFAULT_TTL;

        TINS_BEGIN_PACK
        struct iphdr {
        #if TINS_IS_LITTLE_ENDIAN
            uint8_t ihl:4,
                    version:4;
        #else
            uint8_t version:4,
                    ihl:4;
        #endif
            uint8_t tos;
            uint16_t tot_len;
            uint16_t id;
            uint16_t frag_off;
            uint8_t ttl;
            uint8_t protocol;
            uint16_t check;
            uint32_t saddr;
            uint32_t daddr;
            /*The options start here. */
        } TINS_END_PACK;

        void prepare_for_serialize(const PDU *parent);
        void internal_add_option(const option &option);
        void init_ip_fields();
        void write_serialization(uint8_t *buffer, uint32_t total_sz, const PDU *parent);
        uint8_t* write_option(const option &opt, uint8_t* buffer);
        void add_route_option(option_identifier id, const generic_route_option_type &data);
        generic_route_option_type search_route_option(option_identifier id) const;
        void checksum(uint16_t new_check);

        iphdr _ip;
        uint16_t _options_size, _padded_options_size;
        options_type _ip_options;
    };
}

#endif // TINS_IP_H