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

    报告相同问题?

    悬赏问题

    • ¥15 这个电路是如何实现路灯控制器的,原理是什么,怎么求解灯亮起后熄灭的时间如图?
    • ¥15 matlab数字图像处理频率域滤波
    • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
    • ¥15 ELGamal和paillier计算效率谁快?
    • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?
    • ¥15 Arcgis相交分析无法绘制一个或多个图形
    • ¥15 关于#r语言#的问题:差异分析前数据准备,报错Error in data[, sampleName1] : subscript out of bounds请问怎么解决呀以下是全部代码:
    • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
    • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
    • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误