This file is indexed.

/usr/include/tins/packet_sender.h is in libtins-dev 3.4-2+b1.

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
/*
 * 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_PACKET_SENDER_H
#define TINS_PACKET_SENDER_H


#include <string>
#include <stdexcept>
#include <vector>
#include <stdint.h>
#include <map>
#include "config.h"
#ifdef TINS_HAVE_PACKET_SENDER_PCAP_SENDPACKET
    #include <pcap.h>
#endif // TINS_HAVE_PACKET_SENDER_PCAP_SENDPACKET
#include "network_interface.h"
#include "macros.h"
#include "cxxstd.h"

struct timeval;
struct sockaddr;

namespace Tins {

class PDU;

/**
 * \class PacketSender
 * \brief Sends packets through a network interface.
 *
 * This class allows sending packets through a network interface.
 * It can send basically two types of packets:
 *
 * - Those that contain a link layer PDU (EthernetII, SLL, etc). These
 * will be serialized and sent through an interface that has to be 
 * specified. This can be done by providing it when you call 
 * PacketSender::send, or set a default one using 
 * PacketSender::default_interface.
 * - Those that don't contain a link layer PDU. In this case, the 
 * kernel will be responsible for picking the appropriate network interface
 * based on the destination address.
 *
 * \par Note for Windows users:
 * Sending layer 3 PDUs (without a link layer protocol) is very restricted
 * on Windows (<a href="https://msdn.microsoft.com/en-us/library/windows/desktop/ms740548(v=vs.85).aspx">link</a>).
 * Therefore it's recommended you always send packets which contain link layer PDUs.
 * This will use Winpcap's pcap_sendpacket to inject the packets.
 *
 * Sending packets can be done via PacketSender::send:
 *
 * \code
 * // Construct a packet which uses an EthernetII link layer.
 * EthernetII pkt1 = ...;
 *
 * // Construct a packet sender, which we'll use to send packets.
 * PacketSender sender;
 * 
 * // Send it through interface eth0
 * sender.send(pkt1, "eth0");
 *
 * // Set the default interface to eth0
 * sender.default_interface("eth0");
 * 
 * // This is now equivalent to the previous send.
 * sender.send(pkt1);
 *
 * // Construct a packet which has no link layer protocol.
 * IP ip = IP("192.168.0.1") / TCP(22, 928);
 *
 * // Here the kernel will figure out which interface to use and it will
 * // append the appropriate link layer protocol PDU. It will also perform
 * // the necessary ARP lookups in order to use the destination host's
 * // hardware address.
 * //
 * // libtins will find which is the appropriate source IP address to use.
 * // This will be done by the kernel as well, but it's required when 
 * // calculating checksums.
 * sender.send(ip);
 * \endcode
 *
 * PacketSender also supports sending a packet and waiting for a response.
 * This can be done by using PacketSender::send_recv.
 *
 * This class opens sockets as it needs to, and closes them when the object
 * is destructed.
 *
 * \sa PacketSender::send
 * \sa PacketSender::send_recv
 */
class TINS_API PacketSender {
public:
    /** 
     * The default timeout for receive actions.
     */
    static const uint32_t DEFAULT_TIMEOUT;

    /** 
     * Flags to indicate the socket type.
     */
    enum SocketType {
        ETHER_SOCKET,
        IP_TCP_SOCKET,
        IP_UDP_SOCKET,
        IP_RAW_SOCKET,
        ARP_SOCKET,
        ICMP_SOCKET,
        IPV6_SOCKET,
        SOCKETS_END
    };

    /**
     * \brief Constructor for PacketSender objects.
     * 
     * \param iface The default interface in which to send the packets.
     * \param recv_timeout The timeout which will be used when receiving responses.
     */
    PacketSender(const NetworkInterface& iface = NetworkInterface(),
                 uint32_t recv_timeout = DEFAULT_TIMEOUT,
                 uint32_t usec = 0);
    
    #if TINS_IS_CXX11
        /**
         * \brief Move constructor.
         * \param rhs The sender to be moved.
         */
        PacketSender(PacketSender &&rhs) TINS_NOEXCEPT {
            *this = std::move(rhs);
        }
        
        /**
         * \brief Move assignment operator.
         * \param rhs The sender to be moved.
         */
        PacketSender& operator=(PacketSender &&rhs) TINS_NOEXCEPT {
            sockets_ = std::move(rhs.sockets_);
            rhs.sockets_ = std::vector<int>(SOCKETS_END, INVALID_RAW_SOCKET);
            #ifndef _WIN32
                #if defined(BSD) || defined(__FreeBSD_kernel__)
                ether_socket_ = std::move(rhs.ether_socket_);
                #else
                ether_socket_ = rhs.ether_socket_;
                rhs.ether_socket_ = INVALID_RAW_SOCKET;
                #endif
            #endif
            types_ = rhs.types_; // no move
            _timeout = rhs._timeout;
            timeout_usec_ = rhs.timeout_usec_;
            default_iface_ = rhs.default_iface_;
            return* this;
        }
    #endif
    
    /** 
     * \brief PacketSender destructor.
     * 
     * This gracefully closes all open sockets.
     */
    ~PacketSender();

    #if !defined(_WIN32) || defined(TINS_HAVE_PACKET_SENDER_PCAP_SENDPACKET)
    /** 
     * \brief Opens a layer 2 socket.
     * 
     * If this operation fails, then a socket_open_error will be thrown.
     */
    void open_l2_socket(const NetworkInterface& iface = NetworkInterface());
    #endif // !_WIN32 || defined(TINS_HAVE_PACKET_SENDER_PCAP_SENDPACKET)

    /** 
     * \brief Opens a layer 3 socket, using the corresponding protocol
     * for the given flag.
     * 
     * If this operation fails, then a socket_open_error will be thrown.
     * If the provided socket type is not valid, an invalid_socket_type
     * exception will be throw.
     * 
     * \param type The type of socket which will be used to pick the protocol flag
     * for this socket.
     */
    void open_l3_socket(SocketType type);

    /** 
     * \brief Closes the socket associated with the given flag.
     * 
     * If the provided type is invalid, meaning no such open socket
     * exists, an invalid_socket_type exception is thrown.
     * 
     * If any socket close errors are encountered, a socket_close_error
     * is thrown.
     * 
     * \param type The type of the socket to be closed.
     */
    void close_socket(SocketType type, const NetworkInterface& iface = NetworkInterface());

    /**
     * \brief Sets the default interface.
     * 
     * The interface will be used whenever PacketSender::send(PDU&) 
     * is called.
     */
    void default_interface(const NetworkInterface& iface);
    
    /**
     * \brief Gets the default interface.
     * 
     * \sa PacketSender::default_interface
     */
    const NetworkInterface& default_interface() const;

    /** 
     * \brief Sends a PDU. 
     * 
     * This method opens the appropriate socket, if it's not open yet, 
     * and sends the PDU on the open socket.
     * 
     * If any send error occurs, then a socket_write_error is thrown.
     * 
     * If the PDU contains a link layer protocol, then default_interface
     * is used.
     * 
     * \sa PacketSender::default_interface
     * 
     * \param pdu The PDU to be sent.
     */
    void send(PDU& pdu);
    
    /** 
     * \brief Sends a PDU. 
     * 
     * \sa PacketSender::send
     * 
     * This overload takes a NetworkInterface. The packet is sent
     * through that interface if a link-layer PDU is present, 
     * otherwise this call is equivalent to send(PDU&).
     * 
     * The interface stored in the link layer PDU(if any), is restored
     * after this method ends.
     * 
     * \param pdu The PDU to be sent.
     * \param iface The network interface to use.
     */
    void send(PDU& pdu, const NetworkInterface& iface);

    /** 
     * \brief Sends a PDU and waits for its response. 
     * 
     * This method is used to send PDUs and receive their response. 
     * The packet is sent, and then a response is awaited. 
     * PDU::matches_pdu is called on the packet sent in order to
     * check whether a packet received is a response. 
     *
     * This will match every response to a packet. For example,
     * if you send a TCP packet, any response matching the same
     * IP addresses and ports will be taken as a response to it.
     * This also happens for other protocols, such as ARP, ICMP, 
     * DHCP, DNS, IP, etc.
     *
     * If you send a packet and get an ICMP response indicating
     * an error (such as host unreachable, ttl exceeded, etc),
     * that packet will be considered a response.
     * 
     * \param pdu The PDU to send.
     * \return Returns the response PDU, 0 if not response was received.
     */
    PDU* send_recv(PDU& pdu);
    
    /** 
     * \brief Sends a PDU and waits for its response. 
     *
     * Sends a packet and receives a response. This overload takes 
     * a NetworkInterface. 
     * 
     * \sa PacketSender::send_recv(PDU&);
     * \param pdu The PDU to send.
     * \param iface The network interface in which to send and receive.
     * \return Returns the response PDU, 0 if not response was received.
     */
    PDU* send_recv(PDU& pdu, const NetworkInterface& iface);

    #ifndef _WIN32
    /** 
     * \brief Receives a layer 2 PDU response to a previously sent PDU.
     *
     * This method is used internally. You should just use PacketSender::send_recv.
     * 
     * This PacketSender will receive data from a raw socket, open using 
     * the corresponding flag, according to the given type of protocol, until 
     * a match for the given PDU is received. 
     * 
     * \param pdu The PDU which will try to match the responses.
     * \param link_addr The sockaddr struct which will be used to receive the PDU.
     * \param len_addr The sockaddr struct length.
     * \return Returns the response PDU. If no response is received, then 0 is returned.
     */
    PDU* recv_l2(PDU& pdu, struct sockaddr* link_addr, uint32_t len_addr,
      const NetworkInterface& iface = NetworkInterface());

    #endif // _WIN32

    #if !defined(_WIN32) || defined(TINS_HAVE_PACKET_SENDER_PCAP_SENDPACKET)
    /** 
     * \brief Sends a level 2 PDU.
     *
     * This method is used internally. You should just use PacketSender::send.
     * 
     * This method sends a layer 2 PDU, using a raw socket, open 
     * using the corresponding flag, according to the given type of 
     * protocol. 
     * 
     * If any socket write error occurs, a socket_write_error is thrown.
     * 
     * \param pdu The PDU to send.
     * \param link_addr The sockaddr struct which will be used to send the PDU.
     * \param len_addr The sockaddr struct length.
     */
    void send_l2(PDU& pdu, struct sockaddr* link_addr, uint32_t len_addr, 
      const NetworkInterface& iface = NetworkInterface());
    #endif // !_WIN32 || TINS_HAVE_PACKET_SENDER_PCAP_SENDPACKET

    /** 
     * \brief Receives a layer 3 PDU response to a previously sent PDU.
     *
     * This method is used internally. You should just use PacketSender::send_recv.
     * 
     * This PacketSender will receive data from a raw socket, open using the corresponding flag,
     * according to the given type of protocol, until a match for the given PDU is received. 
     * 
     * \param pdu The PDU which will try to match the responses.
     * \param link_addr The sockaddr struct which will be used to receive the PDU.
     * \param len_addr The sockaddr struct length.
     * \param type The socket protocol type.
     * \return Returns the response PDU. If no response is received, then 0 is returned.
     */
    PDU* recv_l3(PDU& pdu, struct sockaddr* link_addr, uint32_t len_addr, SocketType type);

    /** 
     * \brief Sends a level 3 PDU.
     *
     * This method is used internally. You should just use PacketSender::send.
     * 
     * This method sends a layer 3 PDU, using a raw socket, open using the corresponding flag,
     * according to the given type of protocol.
     * 
     * If any socket write error occurs, a socket_write_error is thrown.
     * 
     * \param pdu The PDU to send.
     * \param link_addr The sockaddr struct which will be used to send the PDU.
     * \param len_addr The sockaddr struct length.
     * \param type The socket protocol type.
     */
    void send_l3(PDU& pdu, struct sockaddr* link_addr, uint32_t len_addr, SocketType type);
private:
    static const int INVALID_RAW_SOCKET;

    typedef std::map<SocketType, int> SocketTypeMap;

    PacketSender(const PacketSender&);
    PacketSender& operator=(const PacketSender&);
    int find_type(SocketType type);
    #ifndef _WIN32
        bool ether_socket_initialized(const NetworkInterface& iface = NetworkInterface()) const;
        int get_ether_socket(const NetworkInterface& iface = NetworkInterface());
    #endif
    template<typename T>
    void send(PDU& pdu, const NetworkInterface& iface) {
        static_cast<T&>(pdu).send(*this, iface);
    }
    #ifdef TINS_HAVE_PACKET_SENDER_PCAP_SENDPACKET
        pcap_t* make_pcap_handle(const NetworkInterface& iface) const;
    #endif // TINS_HAVE_PACKET_SENDER_PCAP_SENDPACKET
    
    PDU* recv_match_loop(const std::vector<int>& sockets, 
                         PDU& pdu,
                         struct sockaddr* link_addr, 
                         uint32_t addrlen);

    std::vector<int> sockets_;
    #ifndef _WIN32
        #if defined(BSD) || defined(__FreeBSD_kernel__)
        typedef std::map<uint32_t, int> BSDEtherSockets;
        BSDEtherSockets ether_socket_;
        #else
        int ether_socket_;
        #endif
    #endif
    SocketTypeMap types_;
    uint32_t _timeout, timeout_usec_;
    NetworkInterface default_iface_;
    // In BSD we need to store the buffer size, retrieved using BIOCGBLEN
    #if defined(BSD) || defined(__FreeBSD_kernel__)
    int buffer_size_;
    #endif // BSD
    #ifdef TINS_HAVE_PACKET_SENDER_PCAP_SENDPACKET
        typedef std::map<NetworkInterface, pcap_t*> PcapHandleMap; 
        PcapHandleMap pcap_handles_;
    #endif // TINS_HAVE_PACKET_SENDER_PCAP_SENDPACKET
};

} // Tins

#endif // TINS_PACKET_SENDER_H