首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在sniffer.h库中获取VS C2664

在sniffer.h库中获取VS C2664
EN

Stack Overflow用户
提问于 2019-07-28 21:26:48
回答 2查看 130关注 0票数 0

我正在开发一个数据包捕获程序。我从http http://libtins.github.io/examples/http-requests/上的一个libtins示例开始。

但VS提示a

C2664错误运算符'bool C2664 main:operator ()(Tins::Packet &) const':无法将参数1从'Tins::PDU‘转换为'Tins::Packet &’packetSniff path- to -tins\sniffer.h 681

在sniffer.h的以下部分

代码语言:javascript
复制
try {
            // If the functor returns false, we're done
            #if TINS_IS_CXX11 && !defined(_MSC_VER)
            if (!Tins::Internals::invoke_loop_cb(function, *it)) {
                return;
            }
            //here
            #else
            if (!function(*it->pdu())) {
                return;
            }
            #endif
        }

我已经从首页http://libtins.github.io/构建并运行了这个示例

但是下面的代码会生成C2664

代码语言:javascript
复制
#define WIN32
#define TINS_STATIC
#define NOMINMAX
#pragma warning(disable : 4996)
#include <string>
#include <iostream>
#include <stdexcept>
#include <boost/regex.hpp>
#include <tins/tcp_ip/stream_follower.h>
#include <tins/sniffer.h>
#include <tins/tins.h>
#include "color.h"
#include <vector>
#include <sstream>

using std::string;
using std::cout;
using std::cerr;
using std::endl;
using std::exception;
using std::vector;
using std::wcout;
using std::stringstream;

using boost::regex;
using boost::match_results;

using Tins::Packet;
using Tins::Sniffer;
using Tins::SnifferConfiguration;
using Tins::TCPIP::Stream;
using Tins::TCPIP::StreamFollower;
using Tins::NetworkInterface;
using termcolor::on_red;
using termcolor::on_green;
using termcolor::reset;

// This example captures and follows TCP streams seen on port 80. It will
// wait until both the client and server send data and then apply a regex
// to both payloads, extrating some information and printing it.

// Don't buffer more than 3kb of data in either request/response
const size_t MAX_PAYLOAD = 3 * 1024;
// The regex to be applied on the request. This will extract the HTTP
// method being used, the request's path and the Host header value.
regex request_regex("([\\w]+) ([^ ]+).+\r\nHost: ([\\d\\w\\.-]+)\r\n");
// The regex to be applied on the response. This finds the response code.
regex response_regex("HTTP/[^ ]+ ([\\d]+)");

void on_server_data(Stream& stream) {
    match_results<Stream::payload_type::const_iterator> client_match;
    match_results<Stream::payload_type::const_iterator> server_match;
    const Stream::payload_type& client_payload = stream.client_payload();
    const Stream::payload_type& server_payload = stream.server_payload();
    // Run the regexes on client/server payloads
    bool valid = regex_search(server_payload.begin(), server_payload.end(),
        server_match, response_regex) &&
        regex_search(client_payload.begin(), client_payload.end(),
            client_match, request_regex);

    stringstream ss;
    for (char c : server_payload) {
        ss << c;
    }

    cout << on_green << "Server raw payload " <<
        ss.str() << reset << endl;

    // If we matched both the client and the server regexes
    if (valid) {
        // Extract all fields
        string method = string(client_match[1].first, client_match[1].second);
        string url = string(client_match[2].first, client_match[2].second);
        string host = string(client_match[3].first, client_match[3].second);
        string response_code = string(server_match[1].first, server_match[1].second);
        // Now print them
        cout << method << " http://" << host << url << " -> " << response_code << endl;

        // Once we've seen the first request on this stream, ignore it
        stream.ignore_client_data();
        stream.ignore_server_data();
    }

    // Just in case the server returns invalid data, stop at 3kb
    if (stream.server_payload().size() > MAX_PAYLOAD) {
        stream.ignore_server_data();
    }
}

void on_client_data(Stream& stream) {
    // Don't hold more than 3kb of data from the client's flow
    if (stream.client_payload().size() > MAX_PAYLOAD) {
        stream.ignore_client_data();
    }
}

void on_new_connection(Stream& stream) {
    stream.client_data_callback(&on_client_data);
    stream.server_data_callback(&on_server_data);
    // Don't automatically cleanup the stream's data, as we'll manage
    // the buffer ourselves and let it grow until we see a full request
    // and response
    stream.auto_cleanup_payloads(false);
}

int main(int argc, char* argv[]) {
    // First fetch all network interfaces
    vector<NetworkInterface> interfaces = NetworkInterface::all();
    // Now iterate them
    int i = 0;
    for (const NetworkInterface& iface : interfaces) {
        // First print the name (GUID)
        cout << i++ << ' ' << "Interface name: " << termcolor::on_red << iface.name() <<
            termcolor::on_cyan << ' ' << iface.addresses().ip_addr << termcolor::reset;

        // Now print the friendly name, a wstring that will contain something like 
        // "Local Area Connection 2"
        wcout << " (" << iface.friendly_name() << ")" << endl;
    }

    try {
        // Construct the sniffer configuration object
        SnifferConfiguration config;
        // Only capture TCP traffic sent from/to port 80
        config.set_filter("tcp port 5000");
        // Construct the sniffer we'll use
        Sniffer sniffer(interfaces[5].name(), config);

        wcout << on_green <<"Starting capture on interface " << 
            interfaces[5].friendly_name() << reset << endl;

        // Now construct the stream follower
        StreamFollower follower;
        // We just need to specify the callback to be executed when a new 
        // stream is captured. In this stream, you should define which callbacks
        // will be executed whenever new data is sent on that stream 
        // (see on_new_connection)
        follower.new_stream_callback(&on_new_connection);
        // Now start capturing. Every time there's a new packet, call 
        // follower.process_packet
        sniffer.sniff_loop([&](Packet& packet) {
            follower.process_packet(packet);
            return true;
        });
    }
    catch (exception& ex) {
        cerr << "Error: " << ex.what() << endl;
        return 1;
    }
}
EN

回答 2

Stack Overflow用户

发布于 2019-07-28 21:47:30

根据sniffer.h中的代码,

代码语言:javascript
复制
try {
        // If the functor returns false, we're done
        #if TINS_IS_CXX11 && !defined(_MSC_VER)
        if (!Tins::Internals::invoke_loop_cb(function, *it)) {
            return;
        }
        //here
        #else
        if (!function(*it->pdu())) {
            return;
        }
        #endif
    }

当您使用VS进行编译时,默认情况下定义了宏_MSC_VER,并且您的代码将转到#else分支,因此它将使用PDU对象调用您的回调。为了避免这种情况,我建议您选择不同的编译器,或者查看他们专门针对MSVC的说明。

票数 0
EN

Stack Overflow用户

发布于 2019-07-29 17:47:39

我最终找到了一个解决方案,将其传递给sniffer.h中的Tins::Packet &

代码语言:javascript
复制
if (!function((Tins::Packet &)*it->pdu())) {
                return;
            }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57241345

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档