数据得到应用程序使用原始的袜子是凌乱的代码?

时间:2016-04-18 15:43:12

标签: c sockets raw-sockets

我编写的代码如下所示,它用于通过原始sock获取应用程序数据,但是我得到了混乱的代码。

#include<errno.h>
#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include<string.h>
#include<memory.h>
#include<sys/socket.h> //socket()
#include<sys/ioctl.h>
#include<net/ethernet.h>
#include<net/if.h>
#include<netinet/in.h>
#include<netinet/ether.h>
#include<netinet/ip.h>
#include<netinet/tcp.h>
#include<arpa/inet.h>//inet_ntoa() struct in_addr

struct context{
   char str[1024];
};

int analyData( char *data )
{
  struct iphdr *ip;
  struct tcphdr *tcp;
  struct ether_header *ehter;
  struct context *temp_data;

  ip = (struct iphdr *)data;
  switch( ip->protocol )
  {
     case 6:
     printf( "Protocal ---- TCP Segment\n" );
     break;
     case 17:
     printf( "Protocal ---- UDP Segment\n" );
     break;
     case 1:
     printf( "Protocal ---- ICMP Segment\n" );
     break;
     default:
     printf( "Protocal ---- Unknown Segment\n");
     break;
  }
  printf("Source IP ---- %s\n", inet_ntoa( *( ( struct in_addr * )&ip->saddr ) ) );
  printf("Dest IP ---- %s\n", inet_ntoa( *( ( struct in_addr * )&ip->daddr ) ) );

  tcp = (struct tcphdr *) ( data + sizeof( *ip ) );//结构体
  printf("Source Port ---- %d\n", ntohs( tcp->source ) );
  printf("Dest Port ---- %d\n", ntohs( tcp->dest ) );
  temp_data = ( struct context *)( data + sizeof( *ip ) + sizeof( *tcp ) );
  printf("Data ---- %s\n", temp_data->str );
  return 1;
}
int get_datagram( char argv[])
{
  unsigned char buffer[1024];
  struct sockaddr_in recvaddr;
  int count = 0;

  if( argv == NULL )
  {
    printf("please enter the ecterface!");
    return -1;
  }
  else if( strcmp( argv, "eth0") != 0)
  {
    printf( "please enter the eth0!" );
    return -1;
  }

  memset( buffer, 0, sizeof(buffer) );

  int sock_raw_fd = socket( PF_INET , SOCK_RAW, IPPROTO_TCP );
  if( sock_raw_fd < 0 )
  {
    printf("created raw socket error:%s !!!\n", strerror( errno ) );
    exit( 1 );
  }
  else
    printf("create raw socket %d sucess !!!\n", sock_raw_fd );


  int recv_len = sizeof( struct sockaddr_in );

  while(1)
  {
    int len = recvfrom( sock_raw_fd, buffer, sizeof(buffer), 0,   (struct sockaddr *)&recvaddr, &recv_len );
    if( len > 0 )
    {
      printf("This packet get %d bytes !!!\n", strlen( buffer ) );
      analyData( buffer );
      printf("Already get %d packet !!!\n", ++count );
      printf("\n");
    }
  }
  close( sock_raw_fd );
  return 0;
}

int main(int argc, char const *argv[]) {
   get_datagram("eth0");
 return 0;
}

首先我运行一个服务器程序和一个clinet程序,当我运行它时,它显示如下: enter image description here

为什么数据结果是乱码?以及如何解决它?

1 个答案:

答案 0 :(得分:1)

打印出数据时,假设temp_data-&gt; str是普通的ASCII字符串

printf("Data ---- %s\n", temp_data->str );

尝试这样的事情(你需要将长度传递给analyData,并计算结果长度):

int i;
for (i=0; i < length; i++)
{
  printf("%02x", data[i]);
}
printf ("\n");