Ejemplo n.º 1
0
int pingPong(PacketSender& ps, int adr) 
{
    PingPacket ping(0, adr);

    verbose("PING(%d)...", adr);
    do {
        ps.SendPacketVal(ping);
    } while (!ps.ReceivePacket());
    info("PONG from %d\n", adr);
    return 1;
}
Ejemplo n.º 2
0
void Dot11::send(PacketSender &sender, const NetworkInterface &iface) {
    if(!iface)
        throw invalid_interface();
    
    #if !defined(BSD) && !defined(__FreeBSD_kernel__)
        sockaddr_ll addr;

        memset(&addr, 0, sizeof(struct sockaddr_ll));

        addr.sll_family = Endian::host_to_be<uint16_t>(PF_PACKET);
        addr.sll_protocol = Endian::host_to_be<uint16_t>(ETH_P_ALL);
        addr.sll_halen = 6;
        addr.sll_ifindex = iface.id();
        memcpy(&(addr.sll_addr), _header.addr1, 6);
        sender.send_l2(*this, (struct sockaddr*)&addr, (uint32_t)sizeof(addr));
    #else
        sender.send_l2(*this, 0, 0, iface);
    #endif
}
Ejemplo n.º 3
0
void IPv6::send(PacketSender& sender, const NetworkInterface &) {
    struct sockaddr_in6 link_addr;
    PacketSender::SocketType type = PacketSender::IPV6_SOCKET;
    link_addr.sin6_family = AF_INET6;
    link_addr.sin6_port = 0;
    copy(header_.dst_addr, header_.dst_addr + address_type::address_size, (uint8_t*)&link_addr.sin6_addr);
    if (inner_pdu() && inner_pdu()->pdu_type() == PDU::ICMP) {
        type = PacketSender::ICMP_SOCKET;
    }

    sender.send_l3(*this, (struct sockaddr*)&link_addr, sizeof(link_addr), type);
}
Ejemplo n.º 4
0
int main(int argc, char *argv[]) 
{
    if(argc != 2) {
        std::cout << "Usage: " << *argv << " <interface>" << std::endl;
        return 1;
    }
    // Sniff on the provided interface, maximum packet size 2000
    // in promiscuos mode and only udp packets sent to port 53
    Sniffer sniffer(argv[1], 2000, true, "udp and dst port 53");
    // All packets will be sent through the provided interface
    sender.default_interface(argv[1]);
    sniffer.sniff_loop(callback);
}
Ejemplo n.º 5
0
 void send_packets(PacketSender &sender) {
     // ICMPs are icmp-requests by default
     IP ip = IP(addr, iface.addresses().ip_addr) / ICMP();
     // We'll find at most 10 hops.
     
     for(auto i = 1; i <= 10; ++i) {
         // Set this "unique" id
         ip.id(i);
         // Set the time-to-live option
         ip.ttl(i);
         
         // Critical section
         {
             std::lock_guard<std::mutex> _(lock);
             ttls[i] = i;
         }
         
         sender.send(ip);
         // Give him a little time
         std::this_thread::sleep_for(std::chrono::milliseconds(100));
     }
     running = false;
     sender.send(ip);
 }
Ejemplo n.º 6
0
void EthernetII::send(PacketSender &sender, const NetworkInterface &iface) {
    if(!iface)
        throw invalid_interface();
    #if defined(HAVE_PACKET_SENDER_PCAP_SENDPACKET) || defined(BSD) || defined(__FreeBSD_kernel__)
        // Sending using pcap_sendpacket/BSD bpf packet mode is the same here
        sender.send_l2(*this, 0, 0, iface);
    #elif defined(WIN32)
        // On Windows we can only send l2 PDUs using pcap_sendpacket
        throw std::runtime_error("LIBTINS_USE_PCAP_SENDPACKET is not enabled");
    #else
        // Default GNU/Linux behaviour
        struct sockaddr_ll addr;

        memset(&addr, 0, sizeof(struct sockaddr_ll));

        addr.sll_family = Endian::host_to_be<uint16_t>(PF_PACKET);
        addr.sll_protocol = Endian::host_to_be<uint16_t>(ETH_P_ALL);
        addr.sll_halen = address_type::address_size;
        addr.sll_ifindex = iface.id();
        memcpy(&(addr.sll_addr), _eth.dst_mac, address_type::address_size);

        sender.send_l2(*this, (struct sockaddr*)&addr, (uint32_t)sizeof(addr));
    #endif
}
Ejemplo n.º 7
0
bool callback(const PDU &pdu) 
{
    // The packet probably looks like this:
    //
    // EthernetII / IP / UDP / RawPDU
    //
    // So we retrieve each layer, and construct a 
    // DNS PDU from the RawPDU layer contents.
    EthernetII eth = pdu.rfind_pdu<EthernetII>();
    IP ip = eth.rfind_pdu<IP>();
    UDP udp = ip.rfind_pdu<UDP>();
    DNS dns = udp.rfind_pdu<RawPDU>().to<DNS>();

    // Is it a DNS query?
    if(dns.type() == DNS::QUERY) {
        // Let's see if there's any query for an "A" record.
        for(const auto &query : dns.queries()) {
            if(query.type() == DNS::A) {
                // Here's one! Let's add an answer.
                dns.add_answer(
                    DNS::Resource(
                        query.dname(), 
                        "127.0.0.1",
                        DNS::A, 
                        query.query_class(), 
                        // 777 is just a random TTL
                        777
                    )
                );
            }
        }
        // Have we added some answers?
        if(dns.answers_count() > 0) {
            // It's a response now
            dns.type(DNS::RESPONSE);
            // Recursion is available(just in case)
            dns.recursion_available(1);
            // Build our packet
            auto pkt = EthernetII(eth.src_addr(), eth.dst_addr()) /
                        IP(ip.src_addr(), ip.dst_addr()) /
                        UDP(udp.sport(), udp.dport()) /
                        dns;
            // Send it!
            sender.send(pkt);
        }
    }
    return true;
}
Ejemplo n.º 8
0
//-------------------------------------------------------------------------------------
Reason PacketFilter::send(Channel * pChannel, PacketSender& sender, Packet * pPacket)
{
	return sender.processFilterPacket(pChannel, pPacket);
}
Ejemplo n.º 9
0
void sendFile(const char* fileName, PacketSender& packetSender, int studentNo)
{
    static const char progressChar[] = "...--oo*OO0";

    uint8_t Sector[SECTORSIZE];
    FILE* infile;
    struct stat stat_p;
    int sectNo = 0;

    // check the size of file
    stat(fileName, &stat_p);

    infile = fopen(fileName, "rb");
    if (infile == 0) {
        eggog("Error: cannot open %s\n", fileName);
    }

    info("Sending file %s to workstation %d\n", fileName, studentNo);

    // Send ping
    if (!pingPong(packetSender, studentNo)) eggog("No reply from workstation %d\n", studentNo);

    // Create file on the net disk
    {
        NetCreateFilePacket createFile(0, studentNo, fileName);
        packetSender.SendPacketVal(createFile);
        if (!packetSender.ReceivePacket()) {
            eggog("No ack of create file from %d\n", studentNo);
        }
    }
    verbose("\nGot re: NET_CREATE_FILE.\n");

    // Reading the file sector-by-sector and writing them onto the net disk

    info("\nNumber of sectors to send: %zd\n", 
        (size_t) ((stat_p.st_size / sizeof (Sector)) + (stat_p.st_size % sizeof (Sector))));

    while (!feof(infile))
    {
        if (fread(Sector, sizeof(Sector), 1, infile) == 0) break;

        {
            NetMasterDataPacket data(0, studentNo, Sector, SECTORSIZE);
            packetSender.SendPacketVal(data);

            NetWriteFilePacket writeFile(0, studentNo, packetSender.GetNetFCB());
            packetSender.SendPacketVal(writeFile);
            if (!packetSender.ReceivePacket()) {
                eggog("No ack of file write from %d\n", studentNo);
            }
        }
        verbose("\nSent sector No.%d", sectNo);

        if ((sectNo+1) % 10 == 0) {
            info("%3d%c", sectNo+1, ((sectNo+1) % 100 == 0) ? '\n' : ' ');
        }
        else {
            info("%c\010", progressChar[sectNo % 10]);
        }

        sectNo++;
    }

    // close the file on the net disk
    {
        NetCloseFilePacket closeFile(0, studentNo, packetSender.GetNetFCB());
        packetSender.SendPacketVal(closeFile);
        if (!packetSender.ReceivePacket()) {
            eggog("No ack of file close from %d\n", studentNo);
        }
    }
    info("\nSendFile Done.\n");

}