【无标题】

/*************************************************************************
    > File Name: icmp.cpp
    > Author: hsz
    > Brief:
    > Created Time: Mon 15 Nov 2021 06:19:56 PM CST
 ************************************************************************/

#include <stdio.h>
#include <errno.h>  
#include <unistd.h>
#include <sys/socket.h>
#include <sys/types.h>  
#include <arpa/inet.h>
#include <cstdlib>
//协议相关结构体
#include <netinet/if_ether.h>
#include <string.h>
#include <sys/ioctl.h>
#include <netinet/ip.h>
#include <netinet/tcp.h>
#include <net/if.h>
#include <netdb.h>

#include <log/log.h>
#include <utils/string8.h>
#include <utils/Errors.h>

#define LOG_TAG "icmp"

int main()
{
    int port = 6000;
    struct protoent *protocol = nullptr;
    protocol = getprotobyname("tcp");
    if (protocol == nullptr) {
        LOGE("getprotobyname error code = %d, error message: %s", errno, strerror(errno));
        return 0;
    }
    int sock = ::socket(AF_INET, SOCK_RAW, protocol->p_proto);
    if (sock < 0) {
        LOGE("socket error. error code = %d, error message: %s", errno, strerror(errno));
        return Jarvis::UNKNOWN_ERROR;
    }
    sockaddr_in server_addr;
    bzero(&server_addr, sizeof(server_addr));
    server_addr.sin_family = AF_INET;
    server_addr.sin_port = htons(port);
    server_addr.sin_addr.s_addr = inet_addr("172.25.12.215"); // htonl(INADDR_ANY);

    int opt = 1;
    setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));

    if (::bind(sock, (sockaddr *)&server_addr, sizeof(server_addr)) < 0) {
        LOGE("bind error. error code = %d, error message: %s", errno, strerror(errno));
        return 0;
    }
    // if (::listen(sock, 128) < 0) { // Operation not supported
    //     LOGE("listen error. error code = %d, error message: %s", errno, strerror(errno));
    //     return 0;
    // }

    // sockaddr_in client_addr;
    // bzero(&client_addr, sizeof(client_addr));
    // socklen_t addrLen = 0;
    // int client = ::accept(sock, (struct sockaddr *)&client_addr, &addrLen); // Operation not supported
    // if (client < 0) {
    //     LOGE("accept error. error code = %d, error message: %s", errno, strerror(errno));
    //     return 0;
    // }

    struct timeval tv;
    tv.tv_usec = 0;  //设置select函数的超时时间为200us
    tv.tv_sec = 3;

    fd_set readfd;
    FD_ZERO(&readfd);
    FD_SET(sock, &readfd);

    int selectRet = 0;
    uint8_t buf[1024] = {0};
    while (1) {
        bzero(buf, sizeof(buf));
        FD_ZERO(&readfd);
        FD_SET(sock, &readfd);
        selectRet = ::select(sock + 1, &readfd, nullptr, nullptr, &tv);
        if (selectRet < 0) {
            LOGE("select error. error code = %d, error message: %s", errno, strerror(errno));
            return 0;
        }
        if (selectRet > 0) {
            int size = recv(sock, buf, sizeof(buf), 0);
            LOGI("recv size = %d", size);
            Jarvis::String8 str;
            for (int i = 0; i < size; ++i) {
                str.appendFormat("%02x ", buf[i]);
                if (i != 0 && i % 10 == 0) {
                    str.appendFormat("\n");
                }
            }
            LOGI("\n%s", str.c_str());

            struct ip *ipHeader = (struct ip *)buf;
            int ipHeaderLen = ipHeader->ip_hl * 4;
            LOGI("\nheader:\n"
                "length: %d;\n"
                "version: %d;\n"
                "total len: %d\n"
                "dst ip: %s\n"
                "src ip: %s\n", ipHeaderLen, ipHeader->ip_v, ipHeader->ip_len,
                inet_ntoa(ipHeader->ip_dst), inet_ntoa(ipHeader->ip_src));

            struct tcphdr *tcpHeader = (struct tcphdr *)(buf + ipHeaderLen);
            LOGI("tcp:\nsrc port: %d\ndst port: %d\n", tcpHeader->th_sport, tcpHeader->th_dport);
            
        }
        sleep(1);
        
    }

    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值