首页 > 系统相关 >【工具】结合tcpdump/wireshark捕获码流快速构造测试场景

【工具】结合tcpdump/wireshark捕获码流快速构造测试场景

时间:2024-02-03 11:46:25浏览次数:30  
标签:码流 peerIp return int RET Client include tcpdump wireshark

源码如下:

CommonHeader.h

#ifndef TESTAPP_COMMONHEADER_H
#define TESTAPP_COMMONHEADER_H

#include <array>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>

#include <iostream>

#include <algorithm>
#include <numeric>

#include <climits>
#include <cstdarg>

using namespace std;

static constexpr int INVALID = -1;
static constexpr int RECV_BUFFER_SIZE = 10240;

static const int RET_FAILURE = -1;
static const int RET_SUCCESS = 0;

static const int BUFF_SIZE_1024 = 1024;

static void Print(const char *str, ...)
{
    char buff[BUFF_SIZE_1024] = {0};
    va_list values;
    va_start(values, str);
    vsnprintf(buff, BUFF_SIZE_1024 - 1, str, values);
    va_end(values);
    cout << buff << endl;
}

#endif

 

Client.h

#ifndef TESTAPP_CLIENT_H
#define TESTAPP_CLIENT_H

#include "CommonHeader.h"

class Client
{
public:
    Client(string peerIp, int peerPort);

    ~Client();

    void ResetPeer(string peerIp, int peerPort);

    int Connect();

    int ReConnect();

    int Send(const string &msg);

    int Recv();

private:
    int m_socket = INVALID;
    string m_peerIp;
    int m_peerPort { INVALID };
    char m_recvBuffer[RECV_BUFFER_SIZE] {};
};

#endif //TESTAPP_CLIENT_H

Client.cpp

#include <cstdio>
#include <cstring>

#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#include "Client.h"

Client::Client(string peerIp, int peerPort) :
        m_peerIp(peerIp),
        m_peerPort(peerPort)
{
}

Client::~Client()
{
    if (m_socket != INVALID) {
        close(m_socket);
    }

}

void Client::ResetPeer(string peerIp, int peerPort)
{
    m_peerIp = peerIp;
    m_peerPort = peerPort;
}

int Client::Connect()
{
    if (m_socket != INVALID) {
        close(m_socket);
    }

    struct sockaddr_in addr;
    memset(&addr, 0, sizeof(addr));
    addr.sin_family = AF_INET;
    addr.sin_port = htons(m_peerPort);
    addr.sin_addr.s_addr = inet_addr(m_peerIp.c_str());

    m_socket = socket(AF_INET,SOCK_STREAM,0);

    if (connect(m_socket, reinterpret_cast<sockaddr *>(&addr), sizeof(addr)) != 0) {
        perror("connect");
        return RET_FAILURE;
    }

    return RET_SUCCESS;
}

int Client::Send(const string &msg)
{
    if (m_socket == INVALID) {
        return RET_FAILURE;
    }

    int ret = send(m_socket, msg.c_str(), msg.length(), 0);
    if (ret == RET_FAILURE)
    {
        perror("send");
        return RET_FAILURE;
    }

    Print("Send %d bytes to peer [%s:%d]", ret, m_peerIp.c_str(), m_peerPort);
    return RET_SUCCESS;
}

int Client::Recv()
{
    if (m_socket == INVALID) {
        return RET_FAILURE;
    }

    memset(m_recvBuffer, 0, sizeof(m_recvBuffer));
    int ret = recv(m_socket, m_recvBuffer, sizeof(m_recvBuffer), 0);
    if (ret < 0)
    {
        perror("recv");
        return RET_FAILURE;
    }

    Print("Recv %d bytes [%s] from peer [%s:%d]", ret, m_recvBuffer, m_peerIp.c_str(), m_peerPort);
    return RET_SUCCESS;
}

main.cpp

#include <iostream>
#include <fstream>
#include <unordered_map>
#include <vector>
#include <csignal>

#include "Client.h"

using namespace std;

unordered_map<int, char> g_asciiMap = {};

string packet = "474554202f20485454502f312e310d0a486f73743a203130332e38352e38342e3134313a38390d0a436f6e6e656374696f6e3a206b6565702d616c6976650d0a43616368652d436f6e74726f6c3a206d61782d6167653d300d0a557067726164652d496e7365637572652d52657175657374733a20310d0a557365722d4167656e743a204d6f7a696c6c612f352e30202857696e646f7773204e542031302e303b2057696e36343b2078363429204170706c655765624b69742f3533372e333620284b48544d4c2c206c696b65204765636b6f29204368726f6d652f3132302e302e302e30205361666172692f3533372e3336204564672f3132302e302e302e300d0a4163636570743a20746578742f68746d6c2c6170706c69636174696f6e2f7868746d6c2b786d6c2c6170706c69636174696f6e2f786d6c3b713d302e392c696d6167652f776562702c696d6167652f61706e672c2a2f2a3b713d302e382c6170706c69636174696f6e2f7369676e65642d65786368616e67653b763d62333b713d302e370d0a4163636570742d456e636f64696e673a20677a69702c206465666c6174650d0a4163636570742d4c616e67756167653a207a682d434e2c7a683b713d302e392c656e3b713d302e382c656e2d47423b713d302e372c656e2d55533b713d302e360d0a436f6f6b69653a204a53455353494f4e49443d44453335413542373541323338444238344543333630464533413233304242380d0a49662d4d6f6469666965642d53696e63653a205361742c2032362041756720323032332030323a34323a313020474d540d0a0d0a";

int GetDecimalVal(char c) {
    if (c >= '0' && c <= '9') {
        return c - '0';
    }

    if (c >= 'a' && c <= 'f') {
        return c - 'a' + 10;
    }

    Print("Get decimal val [%s] failed", c);
    return RET_FAILURE;
}

int TransalatePacket(string packet, string &output)
{
    if (packet.length() % 2 != 0) {
        Print("Packet length [%lu] invalid", packet.length());
        return RET_FAILURE;
    }

    for (int i = 0; i < packet.length(); i += 2) {
        int high = GetDecimalVal(packet[i]);
        if (high == RET_FAILURE) {
            return RET_FAILURE;
        }

        int low = GetDecimalVal(packet[i + 1]);
        if (low == RET_FAILURE) {
            return RET_FAILURE;
        }

        auto it = g_asciiMap.find(high * 16 + low);
        if (it == g_asciiMap.end()) {
            Print("[%d] not exist in asiic map", it->first);
            return RET_FAILURE;
        }

        output += it->second;
    }

    return RET_SUCCESS;
}

int main()
{
    char space = ' ';
    for (int i = 0; i < 127; ++i) {
        g_asciiMap[i] = space + (i - 32);
    }

    string msg;
    TransalatePacket(packet, msg);

    Client client("192.168.18.129", 38000);
    if (client.Connect() == RET_FAILURE) {
        return RET_FAILURE;
    }

    client.Send(msg);

    client.Recv();

    sleep(10);
    return RET_SUCCESS;
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.22)
project(TestApp)

set(CMAKE_CXX_STANDARD 17)
aux_source_directory(. SOURCES)
include_directories(./)
add_executable(TestApp
        ${SOURCES})

 

标签:码流,peerIp,return,int,RET,Client,include,tcpdump,wireshark
From: https://www.cnblogs.com/pond-flower/p/17944201

相关文章

  • Homebrew 卸载 Wireshark 报错
    我在使用Homebrew安装Wireshark的时候,Homebrew要求我输入密码。此时我又不想安转Wireshark了,于是我没有输入密码并且按下了Ctrl+C。后来,我又尝试安装Wireshark,但此时Homebrew提示我已经安装了Wireshark:$brewinstallwireshark--caskWarning:Notupgradingwir......
  • Wireshark中的http协议包分析
    Wireshark可以跟踪网络协议的通讯过程,本节通过http协议,在了解Wireshark使用的基础上,重温http协议的通讯过程。TCP(TransmissionControlProtocol,传输控制协议)是一种面向连接的、可靠的、基于字节流的传输层通信协议。HTTP(HyperTextTransferProtocol,超文本传输协议)是一种用于分布......
  • 《熬夜整理》保姆级系列教程-玩转Wireshark抓包神器教程(1)-初识Wireshark
    1.简介前边已经介绍过两款抓包工具,应该是够用了,也能够处理在日常工作中遇到的问题了,但是还是有人留言让宏哥要讲解讲解Wireshark这一款抓包工具,说实话宏哥之前也没有用过这款工具,只能边研究边分享。换句话说就是现学现卖,希望大家不要介意,宏哥这里的分享仅供你参考学习,有错误......
  • Windows Server2012安装WireShark
    一、环境说明WindowsServer2012R2版本64位;安装wireshark二、问题描述安装wireshark提示TheVisualC++Redistributableinstallerfailedwitherror23PleasemakesureyouhaveKB2999226orKB3118401installed.Unabletocontinueinstallation.三、解决方法1......
  • Wireshark中的ARP协议包分析
    Wireshark可以跟踪网络协议的通讯过程,本节通过ARP协议,在了解Wireshark使用的基础上,重温ARP协议的通讯过程。ARP(AddressResolutionProtocol)地址解析协议,是根据IP地址获取物理地址的一个TCP/IP协议。主机发送信息时将包含目标IP地址的ARP请求广播到局域网络上的所有主机,并接收返回......
  • H264码流分析
    1.frame_num的检测frame_num被用作图片的标识符,应由比特流中的log2_max_frame_num_minus4+4位表示。frame_num的约束如下:变量PrevRefFrameNum的导出方式如下:如果当前图片是IDR图片,PrevRefFrameNum被设为0。否则(当前图片不是IDR图片),PrevRefFrameNum被设为:如果在8.2......
  • wireshark实践 - 调试spring连接mysql失败问题
    问题描述spring:datasource:driver-class-name:com.mysql.jdbc.Driverurl:jdbc:jdbc:mysql://122.224.147.xxx:90/dev?characterEncoding=utf8username:xxxpassword:xxxtype:com.alibaba.druid.pool.DruidDataSourceurl格式写错了(两个jdbc),然......
  • tcpdump抓包(linux)和丢包分析
    1、下载安装官网:https://www.tcpdump.org/index.html#latest-releases编译和安装[root@mysql01~]#tar-zxvftcpdump-4.99.4.tar.gz[root@mysql01~]#tar-zxvflibpcap-1.10.4.tar.gz[root@mysql01~]#cdlibpcap-1.10.4/[root@mysql01~/libpcap-1.10.4]#./configur......
  • Wireshark网络工具是什么?
    Wireshark是网络包分析工具。网络包分析工具的主要作用是尝试捕获网络包,并尝试显示包的尽可能详细的情况。Wireshark是一个免费开源软件,不需要付费,免费使用,可以直接登陆到Wireshark的官网下载安装。在windows环境中,Wireshark使用WinPCAP作为接口,直接与网卡进行数据报文交换,这个工具......
  • 抓包Tcpdump 学习与理解
    常用参数:-w文件名,可以把报文保存到文件;-c数量,可以抓取固定数量的报文,这在流量较高时,可以避免一不小心抓取过多报文;-s长度,可以只抓取每个报文的一定长度,后面我会介绍相关的使用场景;-n,不做地址转换(比如IP地址转换为主机名,port80转换为http);-v/-vv/-vvv,可以打印更加详细......