/*************************************************************************
> 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;
}
【无标题】
最新推荐文章于 2024-01-18 23:25:36 发布