← Back to team overview

rohc team mailing list archive

[Question #249394]: Packets cannot be compressed while using RAW socket packets

 

New question #249394 on rohc:
https://answers.launchpad.net/rohc/+question/249394

Hi, I were using the following code for compressing UDP RAW socket packets using ROHC library, but I got the error as the packet does not matches the fromat..

#include<stdio.h> 
#include<stdlib.h>    
#include<string.h>    
#include<netinet/ip_icmp.h>   
#include<netinet/udp.h>   
#include<netinet/tcp.h>   
#include<netinet/ip.h>   
#include<sys/socket.h>
#include<arpa/inet.h>
#include<rohc.h>
#include<rohc_comp.h>
#include<rohc_decomp.h>
#define BUFFER_SIZE 2048
 
void ProcessPacket(unsigned char* , int);
void print_ip_header(unsigned char* , int);
void print_tcp_packet(unsigned char* , int);
int print_udp_packet(unsigned char * , int);
void print_icmp_packet(unsigned char* , int);
void PrintData (unsigned char* , int);

static int gen_random_num(const struct rohc_comp *const comp,void *const user_context);
 
int sock_raw;
FILE *logfile;
int tcp=0,udp=0,icmp=0,others=0,igmp=0,total=0,i,j;
struct sockaddr_in source,dest;

int main()
{
    int saddr_size , data_size;
    struct sockaddr saddr;
    struct in_addr in;
	     
    unsigned char *buffer = (unsigned char *)malloc(65536); 
     
    logfile=fopen("log.txt","w");
    if(logfile==NULL) printf("Unable to create file.");
    printf("Starting...\n");
    //Create a raw socket that shall sniff
    sock_raw = socket(AF_INET , SOCK_RAW , IPPROTO_UDP);
    if(sock_raw < 0)
    {
        printf("Socket Error\n");
        return 1;
    }
    while(1)
    {
        saddr_size = sizeof saddr;
        data_size = recvfrom(sock_raw , buffer , 65536 , 0 , &saddr , &saddr_size);
        if(data_size <0 )
        {
            printf("Recvfrom error , failed to get packets\n");
            return 1;
        }
        
        ProcessPacket(buffer , data_size);
    }
    close(sock_raw);
    printf("Finished");
    return 0;
}
 
void ProcessPacket(unsigned char* buffer, int size)
{
    //Get the IP Header part of this packet
    struct iphdr *iph = (struct iphdr*)buffer;
	++total;
    switch (iph->protocol) 
    {
        case 1:  
            ++icmp;
            break;
         
        case 2:  
            ++igmp;
            break;
         
        case 6:  
            ++tcp;
            print_tcp_packet(buffer , size);
            break;
         
        case 17: 
            ++udp;
            print_udp_packet(buffer , size);
            break;
         
        default: 
            ++others;
            break;
    }
    printf("TCP : %d   UDP : %d   ICMP : %d   IGMP : %d   Others : %d   Total : %d\r",tcp,udp,icmp,igmp,others,total);
}
 
void print_ip_header(unsigned char* Buffer, int Size)
{
    unsigned short iphdrlen;
         
    struct iphdr *iph = (struct iphdr *)Buffer;
    iphdrlen =iph->ihl*4;
	iph->protocol = (unsigned int)17;
     
    memset(&source, 0, sizeof(source));
    source.sin_addr.s_addr = iph->saddr;
     
    memset(&dest, 0, sizeof(dest));
    dest.sin_addr.s_addr = iph->daddr;
     
    fprintf(logfile,"\n");
    fprintf(logfile,"IP Header\n");
    fprintf(logfile,"   |-IP Version        : %d\n",(unsigned int)iph->version);
    fprintf(logfile,"   |-IP Header Length  : %d DWORDS or %d Bytes\n",(unsigned int)iph->ihl,((unsigned int)(iph->ihl))*4);
    fprintf(logfile,"   |-Type Of Service   : %d\n",(unsigned int)iph->tos);
    fprintf(logfile,"   |-IP Total Length   : %d  Bytes(Size of Packet)\n",ntohs(iph->tot_len));
    fprintf(logfile,"   |-Identification    : %d\n",ntohs(iph->id));
    fprintf(logfile,"   |-TTL      : %d\n",(unsigned int)iph->ttl);
    fprintf(logfile,"   |-Protocol : %d\n",(unsigned int)iph->protocol);
    fprintf(logfile,"   |-Checksum : %d\n",ntohs(iph->check));
    fprintf(logfile,"   |-Source IP        : %s\n",inet_ntoa(source.sin_addr));
    fprintf(logfile,"   |-Destination IP   : %s\n",inet_ntoa(dest.sin_addr));
}
 
void print_tcp_packet(unsigned char* Buffer, int Size)
{
    unsigned short iphdrlen;
     
    struct iphdr *iph = (struct iphdr *)Buffer;
    iphdrlen = iph->ihl*4;
	iph->protocol = 17;
     
    struct tcphdr *tcph=(struct tcphdr*)(Buffer + iphdrlen);
             
    fprintf(logfile,"\n\n***********************TCP Packet*************************\n");    
         
    print_ip_header(Buffer,Size);
         
    fprintf(logfile,"\n");
    fprintf(logfile,"TCP Header\n");
    fprintf(logfile,"   |-Source Port      : %u\n",ntohs(tcph->source));
    fprintf(logfile,"   |-Destination Port : %u\n",ntohs(tcph->dest));
    fprintf(logfile,"   |-Sequence Number    : %u\n",ntohl(tcph->seq));
    fprintf(logfile,"   |-Acknowledge Number : %u\n",ntohl(tcph->ack_seq));
    fprintf(logfile,"   |-Header Length      : %d DWORDS or %d BYTES\n" ,(unsigned int)tcph->doff,(unsigned int)tcph->doff*4);
    fprintf(logfile,"   |-Urgent Flag          : %d\n",(unsigned int)tcph->urg);
    fprintf(logfile,"   |-Acknowledgement Flag : %d\n",(unsigned int)tcph->ack);
    fprintf(logfile,"   |-Push Flag            : %d\n",(unsigned int)tcph->psh);
    fprintf(logfile,"   |-Reset Flag           : %d\n",(unsigned int)tcph->rst);
    fprintf(logfile,"   |-Synchronise Flag     : %d\n",(unsigned int)tcph->syn);
    fprintf(logfile,"   |-Finish Flag          : %d\n",(unsigned int)tcph->fin);
    fprintf(logfile,"   |-Window         : %d\n",ntohs(tcph->window));
    fprintf(logfile,"   |-Checksum       : %d\n",ntohs(tcph->check));
    fprintf(logfile,"   |-Urgent Pointer : %d\n",tcph->urg_ptr);
    fprintf(logfile,"\n");
    fprintf(logfile,"                        DATA Dump                         ");
    fprintf(logfile,"\n");
         
    fprintf(logfile,"IP Header\n");
    PrintData(Buffer,iphdrlen);
         
    fprintf(logfile,"TCP Header\n");
    PrintData(Buffer+iphdrlen,tcph->doff*4);
         
    fprintf(logfile,"Data Payload\n");  
    PrintData(Buffer + iphdrlen + tcph->doff*4 , (Size - tcph->doff*4-iph->ihl*4) );
                         
    fprintf(logfile,"\n###########################################################");

}
 
int print_udp_packet(unsigned char *Buffer , int Size)
{
     
    unsigned short iphdrlen;
	unsigned int ip_packet_len, rohc_packet_len;
	unsigned char rohc_packet[BUFFER_SIZE], op_packet[BUFFER_SIZE];
	int ret;
     
    struct iphdr *iph = (struct iphdr *)Buffer;
    iphdrlen = iph->ihl*4;
     
    struct udphdr *udph = (struct udphdr*)(Buffer + iphdrlen);

	struct rohc_comp *compressor;
	struct rohc_decomp *decompressor;
     
    fprintf(logfile,"\n\n***********************UDP Packet*************************\n");
     
    print_ip_header(Buffer,Size);  
	       
    fprintf(logfile,"\nUDP Header\n");
    fprintf(logfile,"   |-Source Port      : %d\n" , ntohs(udph->source));
    fprintf(logfile,"   |-Destination Port : %d\n" , ntohs(udph->dest));
    fprintf(logfile,"   |-UDP Length       : %d\n" , ntohs(udph->len));
    fprintf(logfile,"   |-UDP Checksum     : %d\n" , ntohs(udph->check));
     
    fprintf(logfile,"\n");
    fprintf(logfile,"IP Header\n");
    PrintData(Buffer , iphdrlen);
         
    fprintf(logfile,"UDP Header\n");
    PrintData(Buffer+iphdrlen , sizeof udph);
         
    fprintf(logfile,"Data Payload\n");  
    PrintData(Buffer + iphdrlen + sizeof udph ,( Size - sizeof udph - iph->ihl * 4 ));
     
    fprintf(logfile,"\n###########################################################");
	
	printf("\n Creating ROHC compressor......\n\n");
	compressor = rohc_alloc_compressor(15,0,0,0);
	if(compressor == NULL)
		{ 
			fprintf(stderr,"\n Failed creating compressor!!\n");
			rohc_free_compressor(compressor);
		}
		
			if(!rohc_comp_set_random_cb(compressor, gen_random_num, NULL))
			{
						fprintf(stderr, "\n Failed to set the compressor callback random numbers!!\n");
						rohc_free_compressor(compressor);
			 }
	
	rohc_activate_profile(compressor, ROHC_PROFILE_IP);
	rohc_activate_profile(compressor, ROHC_PROFILE_UDP);
	printf("\n Compressing the UDP/IP packet...\n\n");
	
	ip_packet_len = Size - sizeof udph - iph->ihl * 4;

	ret=rohc_compress2(compressor,Buffer,ip_packet_len,rohc_packet, BUFFER_SIZE, &rohc_packet_len);
	
	if(ret!=ROHC_OK)
	{
		fprintf(stderr, "Compression of IP packet failed\n");
	    rohc_free_compressor(compressor);
		return 1;
	}
	
	printf("\n ROHC packet after compression:\n");
	for(i = 0; i < ((unsigned int) rohc_packet_len); i++)
   	{
      	printf("0x%02x ", rohc_packet[i]);
      	if(i != 0 && ((i + 1) % 8) == 0)
      	{
         	printf("\n");
      	}
   	}
   	if(i != 0 && (i % 8) != 0) 
   	{
      	printf("\n");
   	}
	
	printf("\n Creating ROHC Decompressor.....");
	decompressor=rohc_alloc_decompressor(compressor);
	if(decompressor == NULL)
	{
		fprintf(stderr,"\n Failed creating decompressor!!\n");
		rohc_free_decompressor(decompressor);
	}
	
		if(!rohc_decomp_set_traces_cb(decompressor, NULL))
		{
			fprintf(stderr,"\n Failed to set callback traces for decompressor!!");
			rohc_free_decompressor(decompressor);
		} 
	printf("\n Decompressing the UDP/IP packet....\n");
	ret=rohc_decompress(decompressor,rohc_packet,rohc_packet_len,op_packet,BUFFER_SIZE);
	if(ret <= 0)
	{
		fprintf(stderr, "Decompression failed\n");
		rohc_free_decompressor(decompressor);
	}
	
	printf("\n IP packet after Decompression:\n\n");
	for(i = 0; i < ((unsigned int) ret); i++)
   	{
      	printf("0x%02x ", op_packet[i]);
      	if(i != 0 && ((i + 1) % 8) == 0)
      	{
         	printf("\n");
      	}
   	}
   	if(i != 0 && (i % 8) != 0) 
   	{
      	printf("\n");
   	}
	printf("\n Destroy the Compressor and Decompressor");
	rohc_free_compressor(compressor);
	rohc_free_decompressor(decompressor);
return 0;
}

static int gen_random_num(const struct rohc_comp *const comp,void *const user_context)
{
	return rand();
}

 
void print_icmp_packet(unsigned char* Buffer , int Size)
{
    unsigned short iphdrlen;
     
    struct iphdr *iph = (struct iphdr *)Buffer;
    iphdrlen = iph->ihl*4;
     
    struct icmphdr *icmph = (struct icmphdr *)(Buffer + iphdrlen);
             
    fprintf(logfile,"\n\n***********************ICMP Packet*************************\n");   
     
    print_ip_header(Buffer , Size);
             
    fprintf(logfile,"\n");
         
    fprintf(logfile,"ICMP Header\n");
    fprintf(logfile,"   |-Type : %d",(unsigned int)(icmph->type));
             
    if((unsigned int)(icmph->type) == 11) 
        fprintf(logfile,"  (TTL Expired)\n");
    else if((unsigned int)(icmph->type) == ICMP_ECHOREPLY) 
        fprintf(logfile,"  (ICMP Echo Reply)\n");
    fprintf(logfile,"   |-Code : %d\n",(unsigned int)(icmph->code));
    fprintf(logfile,"   |-Checksum : %d\n",ntohs(icmph->checksum));
    fprintf(logfile,"\n");
 
    fprintf(logfile,"IP Header\n");
    PrintData(Buffer,iphdrlen);
         
    fprintf(logfile,"UDP Header\n");
    PrintData(Buffer + iphdrlen , sizeof icmph);
         
    fprintf(logfile,"Data Payload\n");  
    PrintData(Buffer + iphdrlen + sizeof icmph , (Size - sizeof icmph - iph->ihl * 4));
     
    fprintf(logfile,"\n###########################################################");

}
 
void PrintData (unsigned char* data , int Size)
{
     
    for(i=0 ; i < Size ; i++)
    {
        if( i!=0 && i%16==0)   
        {
            fprintf(logfile,"         ");
            for(j=i-16 ; j<i ; j++)
            {
                if(data[j]>=32 && data[j]<=128)
                    fprintf(logfile,"%c",(unsigned char)data[j]); 
                 
                else fprintf(logfile,"."); 
            }
            fprintf(logfile,"\n");
        } 
         
        if(i%16==0) fprintf(logfile,"   ");
            fprintf(logfile," %02X",(unsigned int)data[i]);
                 
        if( i==Size-1)  
        {
            for(j=0;j<15-i%16;j++) fprintf(logfile,"   "); 
             
            fprintf(logfile,"         ");
             
            for(j=i-i%16 ; j<=i ; j++)
            {
                if(data[j]>=32 && data[j]<=128) fprintf(logfile,"%c",(unsigned char)data[j]);
                else fprintf(logfile,".");
            }
            fprintf(logfile,"\n");
        }
    }
}



Creating ROHC compressor......

please define a callback for compressor traces
[rohc_comp.c:1102 rohc_comp_set_wlsb_window_width()] width of W-LSB sliding window set to 4
[rohc_comp.c:1159 rohc_comp_set_periodic_refreshes()] IR timeout for context periodic refreshes set to 1700
[rohc_comp.c:1161 rohc_comp_set_periodic_refreshes()] FO timeout for context periodic refreshes set to 700
[rohc_comp.c:1585 rohc_comp_add_rtp_port()] port 1234 added to the UDP port list for RTP traffic
[rohc_comp.c:1585 rohc_comp_add_rtp_port()] port 36780 added to the UDP port list for RTP traffic
[rohc_comp.c:1585 rohc_comp_add_rtp_port()] port 33238 added to the UDP port list for RTP traffic
[rohc_comp.c:1585 rohc_comp_add_rtp_port()] port 5020 added to the UDP port list for RTP traffic
[rohc_comp.c:1585 rohc_comp_add_rtp_port()] port 5002 added to the UDP port list for RTP traffic
[rohc_comp.c:2955 c_create_contexts()] create enough room for 16 contexts (MAX_CID = 15)

 Compressing the UDP/IP packet...

[rohc_traces_internal.c:68 rohc_dump_packet()] uncompressed data, max 100 bytes (100 bytes):
[rohc_traces_internal.c:77 rohc_dump_packet()] 45 00 00 84 07 7a 00 00   80 11 17 91 0a b0 10 af 
[rohc_traces_internal.c:77 rohc_dump_packet()] ff ff ff ff 44 5c 44 5c   00 70 80 4b 7b 22 68 6f 
[rohc_traces_internal.c:77 rohc_dump_packet()] 73 74 5f 69 6e 74 22 3a   20 31 34 32 30 31 33 32 
[rohc_traces_internal.c:77 rohc_dump_packet()] 32 37 30 2c 20 22 76 65   72 73 69 6f 6e 22 3a 20 
[rohc_traces_internal.c:77 rohc_dump_packet()] 5b 31 2c 20 38 5d 2c 20   22 64 69 73 70 6c 61 79 
[rohc_traces_internal.c:77 rohc_dump_packet()] 6e 61 6d 65 22 3a 20 22   22 2c 20 22 70 6f 72 74 
[rohc_traces_internal.c:97 rohc_dump_packet()] 22 3a 20 31 
[rohc_comp.c:609 rohc_compress2()] size of uncompressed packet = 108 bytes
[rohc_comp.c:640 rohc_compress2()] try to find the best profile for packet with transport protocol 0
[rohc_comp.c:2692 c_get_profile_from_packet()] skip disabled profile 'RTP / Compressor' (0x0001)
[rohc_comp.c:2707 c_get_profile_from_packet()] skip profile 'UDP / Compressor' (0x0002) because it does not match packet
[rohc_comp.c:2692 c_get_profile_from_packet()] skip disabled profile 'UDP-Lite / Compressor' (0x0008)
[rohc_comp.c:2692 c_get_profile_from_packet()] skip disabled profile 'ESP / Compressor' (0x0003)
[rohc_comp.c:2692 c_get_profile_from_packet()] skip disabled profile 'IP / Compressor' (0x0004)
[rohc_comp.c:2692 c_get_profile_from_packet()] skip disabled profile 'Uncompressed / Compressor' (0x0000)
[rohc_comp.c:645 rohc_compress2()] no profile found for packet, giving up
Compression of IP packet failed
[rohc_comp.c:328 rohc_free_compressor()] free compressor
TCP : 0   UDP : 1   ICMP : 0   IGMP : 0   Others : 0   Total : 1
 Creating ROHC compressor......

[rohc_comp.c:1102 rohc_comp_set_wlsb_window_width()] width of W-LSB sliding window set to 4
[rohc_comp.c:1159 rohc_comp_set_periodic_refreshes()] IR timeout for context periodic refreshes set to 1700
[rohc_comp.c:1161 rohc_comp_set_periodic_refreshes()] FO timeout for context periodic refreshes set to 700
[rohc_comp.c:1585 rohc_comp_add_rtp_port()] port 1234 added to the UDP port list for RTP traffic
[rohc_comp.c:1585 rohc_comp_add_rtp_port()] port 36780 added to the UDP port list for RTP traffic
[rohc_comp.c:1585 rohc_comp_add_rtp_port()] port 33238 added to the UDP port list for RTP traffic
[rohc_comp.c:1585 rohc_comp_add_rtp_port()] port 5020 added to the UDP port list for RTP traffic
[rohc_comp.c:1585 rohc_comp_add_rtp_port()] port 5002 added to the UDP port list for RTP traffic
[rohc_comp.c:2955 c_create_contexts()] create enough room for 16 contexts (MAX_CID = 15)

 Compressing the UDP/IP packet...

[rohc_traces_internal.c:68 rohc_dump_packet()] uncompressed data, max 100 bytes (100 bytes):
[rohc_traces_internal.c:77 rohc_dump_packet()] 45 00 00 84 07 7b 00 00   80 11 fa e0 0a b0 10 af 
[rohc_traces_internal.c:77 rohc_dump_packet()] 0a b0 11 ff 44 5c 44 5c   00 70 63 9c 7b 22 68 6f 
[rohc_traces_internal.c:77 rohc_dump_packet()] 73 74 5f 69 6e 74 22 3a   20 31 34 32 30 31 33 32 
[rohc_traces_internal.c:77 rohc_dump_packet()] 32 37 30 2c 20 22 76 65   72 73 69 6f 6e 22 3a 20 
[rohc_traces_internal.c:77 rohc_dump_packet()] 5b 31 2c 20 38 5d 2c 20   22 64 69 73 70 6c 61 79 
[rohc_traces_internal.c:77 rohc_dump_packet()] 6e 61 6d 65 22 3a 20 22   22 2c 20 22 70 6f 72 74 
[rohc_traces_internal.c:97 rohc_dump_packet()] 22 3a 20 31 
[rohc_comp.c:609 rohc_compress2()] size of uncompressed packet = 108 bytes
[rohc_comp.c:640 rohc_compress2()] try to find the best profile for packet with transport protocol 0
[rohc_comp.c:2692 c_get_profile_from_packet()] skip disabled profile 'RTP / Compressor' (0x0001)
[rohc_comp.c:2707 c_get_profile_from_packet()] skip profile 'UDP / Compressor' (0x0002) because it does not match packet
[rohc_comp.c:2692 c_get_profile_from_packet()] skip disabled profile 'UDP-Lite / Compressor' (0x0008)
[rohc_comp.c:2692 c_get_profile_from_packet()] skip disabled profile 'ESP / Compressor' (0x0003)
[rohc_comp.c:2692 c_get_profile_from_packet()] skip disabled profile 'IP / Compressor' (0x0004)
[rohc_comp.c:2692 c_get_profile_from_packet()] skip disabled profile 'Uncompressed / Compressor' (0x0000)
[rohc_comp.c:645 rohc_compress2()] no profile found for packet, giving up
Compression of IP packet failed
[rohc_comp.c:328 rohc_free_compressor()] free compressor
TCP : 0   UDP : 2   ICMP : 0   IGMP : 0   Others : 0   Total : 2





-- 
You received this question notification because you are a member of ROHC
Team, which is an answer contact for rohc.