zqbnqsdsmd 2017-10-24 04:33 采纳率: 0%
浏览 1213
已结题

原始套接字捕获的包如何提取出数据

代码如下:
#include
#include
#include
#include
#include
#include
#include
#include "arpa/inet.h"
#include "linux/sockios.h"
#include

struct ip{
unsigned int ip_length:4;
unsigned int ip_version:4;
unsigned char ip_tos;
unsigned short ip_total_length;
unsigned short ip_id;
unsigned short ip_flags;
unsigned char ip_ttl;
unsigned char ip_protocol;
unsigned short ip_cksum;
unsigned int ip_source;
};
struct tcp{
unsigned short tcp_source_port;
unsigned short tcp_dest_port;
unsigned short tcp_seqno;
unsigned int tcp_ackno;
unsigned int tcp_res1:4,
tcp_hlen:4,
tcp_fin:1,
tcp_syn:1,
tcp_rst:1,
tcp_psh:1,
tcp_ack:1,
tcp_urg:1,
tcp_res2:2;
unsigned short tcp_winsize;
unsigned short tcp_cksum;
unsigned short tcp_urgent;
};
#define INTERFACE "eth0"
int Set_Promisc(char interface, int sock);
int main()
{
int sock,bytes_recieved,fromlen;
char buffer[65535];
struct sockaddr_in from;
struct ip *ip;
struct tcp *tcp;
sock=Open_Raw_Socket();//socket(AF_INET,SOCK_RAW,IPPROTO_TCP);
//Set_Promisc(INTERFACE, sock);
while(1)
{
fromlen=sizeof(from);
bytes_recieved=recvfrom(sock,buffer,sizeof(buffer),0,(struct sockaddr
)&from,&fromlen);
ip=(struct ip *)buffer;

printf("\nApplications++++++++++++++++++++++++++++++++++++++\n");
printf("Data received ::: %s\n",buffer);

printf("Transportation++++++++++++++++++++++++++++++++++++\n");
tcp=(struct tcp *)(buffer + (4*ip->ip_length));
printf("Source port ::: %d\n",ntohs(tcp->tcp_source_port));
printf("Dest prot ::: %d\n",ntohs(tcp->tcp_dest_port));

printf("Network+++++++++++++++++++++++++++++++++++++++++++\n");
printf("source address ::: %s\n",inet_ntoa(from.sin_addr));

}}
int Open_Raw_Socket(){
int sock;
if((sock=socket(AF_INET,SOCK_RAW,IPPROTO_TCP))<0){//
perror("The raw socket was not created");
exit(0);
};
return(sock);
}

为什么我输出的buffer只有一个E?如果获取的是整个数据包的话怎么提取出数据部分,也就是发送的明文?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 乘性高斯噪声在深度学习网络中的应用
    • ¥15 运筹学排序问题中的在线排序
    • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
    • ¥30 求一段fortran代码用IVF编译运行的结果
    • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
    • ¥15 C++ 头文件/宏冲突问题解决
    • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
    • ¥50 安卓adb backup备份子用户应用数据失败
    • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
    • ¥30 python代码,帮调试,帮帮忙吧