yuanwyue 2017-04-11 12:58 采纳率: 0%
浏览 1736

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

代码如下:

#include<stdio.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<stdlib.h>
#include<string.h>
#include<net/if.h>
#include "arpa/inet.h"
#include "linux/sockios.h"
#include<sys/ioctl.h>


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条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥50 如何增强飞上天的树莓派的热点信号强度,以使得笔记本可以在地面实现远程桌面连接
    • ¥15 MCNP里如何定义多个源?
    • ¥20 双层网络上信息-疾病传播
    • ¥50 paddlepaddle pinn
    • ¥20 idea运行测试代码报错问题
    • ¥15 网络监控:网络故障告警通知
    • ¥15 django项目运行报编码错误
    • ¥15 请问这个是什么意思?
    • ¥15 STM32驱动继电器
    • ¥15 Windows server update services