Advertisement
AntonioVillanueva

UDP Header

Aug 29th, 2016
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.55 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4. #include <cmath>
  5. #include <limits.h>
  6. #include  <iomanip>
  7. using namespace std;
  8.  
  9. //Activa o desactiva el formato HEX en COUT<<
  10. //#define HEX_ON std::cout.setf ( std::ios::hex, std::ios::basefield );std::cout.setf ( std::ios::showbase );
  11. #define HEX_ON std::cout.setf ( std::ios::hex, std::ios::basefield );
  12. #define HEX_OFF std::cout.unsetf ( std::ios::showbase );std::cout.setf ( std::ios::dec, std::ios::basefield );
  13.  
  14. //Pseudo header DATOS PARA EFECTUAR EL TEST
  15. #define IP_SRC_H 0xC0A8
  16. #define IP_SRC_L 0x010C
  17.  
  18. #define IP_DST_H 0xC0A8
  19. #define IP_DST_L 0x0101
  20.  
  21. #define ZER_PROT 0x0011 //17 UDP
  22. //#define LEN_UDP 0X0028
  23.  
  24.  
  25. #define SRC_PORT 0xEEFC //0xEEFC 61180
  26. #define DST_PORT 0x0035 //53
  27. #define LONGITUD 0x0028 //40 bytes taille
  28. //Datos para efectuar tests....
  29. unsigned short DATA[]={0xD334, 0x0100 ,0x0001
  30.     ,0x0000, 0x0000 ,0x0000 ,0x0370, 0x6F70 ,0x0777, 0x616E ,0x6164,
  31.     0x6F6F ,0x0266 ,0x7200 ,0x0001 ,0x0001};//16*2 =32 bytes
  32.  
  33. typedef struct{
  34.     //Pseudo header
  35.     unsigned short IP_SOURCE_H,IP_SOURCE_L;
  36.     unsigned short IP_DEST_H,IP_DEST_L;
  37.     unsigned short ZEROES_PROTOCOL;
  38.     unsigned short UDP_LENGTH;
  39.     //
  40.     unsigned short source,dest;//16+16 4bytes
  41.     unsigned short length,checksum;//16+16 4bytes
  42.     unsigned short * data;//32 bytes test
  43.     //total 32+4+4=40
  44.    
  45. }h_UDP;
  46. //-----------------------------------------------------------------------------------------------------------
  47. //-----------------------------------------------------------------------------------------------------------
  48.  
  49. class headerUDP{
  50.     public:
  51.     headerUDP(unsigned short source=SRC_PORT ,unsigned short dest=DST_PORT ,unsigned short length =LONGITUD);
  52.     void HexDump();
  53.     unsigned short checksum();
  54.    
  55.     private:
  56.     h_UDP UDPheader;
  57. };
  58. //-----------------------------------------------------------------------------------------------------------
  59.     headerUDP::headerUDP(unsigned short source ,unsigned short dest ,unsigned short length ):
  60.   //  UDPheader({source,dest,length,0x0000,NULL})
  61.       UDPheader({IP_SRC_H,IP_SRC_L,IP_DST_H,IP_DST_L,ZER_PROT,LONGITUD,source,dest,length,0x0000,NULL})
  62.     {
  63.         if (DATA!=NULL) {UDPheader.data=DATA;  }//Asigna algunos datos
  64.     }
  65.  
  66. //-----------------------------------------------------------------------------------------------------------    
  67.      void headerUDP::HexDump(){    
  68.          
  69.          int count=((UDPheader.length -8)/2);//OJO REVISAR SI CAMBIO DE DATA
  70.          unsigned short *data=UDPheader.data;
  71.          
  72.          HEX_ON
  73.          cout<< uppercase
  74.          <<"PSEUDO HEADER "<<endl
  75.          <<setfill('0') << setw(4) << UDPheader.IP_SOURCE_H<<' '
  76.          <<setfill('0') << setw(4) << UDPheader.IP_SOURCE_L<<' '    
  77.          
  78.          <<setfill('0') << setw(4) << UDPheader.IP_DEST_H<<' '
  79.          <<setfill('0') << setw(4) << UDPheader.IP_DEST_L<<' '
  80.          
  81.          <<setfill('0') << setw(4) << UDPheader.ZEROES_PROTOCOL<<' '
  82.          <<setfill('0') << setw(4) << UDPheader.UDP_LENGTH<<' '
  83.          
  84.          <<endl<<endl                    
  85.                  
  86.          <<setfill('0') << setw(4) << UDPheader.source<<' '
  87.          <<setfill('0') << setw(4) <<UDPheader.dest<<' '  
  88.          <<setfill('0') << setw(4) <<UDPheader.length<<' '
  89.          <<setfill('0') << setw(4) <<UDPheader.checksum<<endl;
  90.      
  91.          while (count >0){//Data         
  92.             cout<<  uppercase <<setfill('0') << setw(4)<<*data++<<" ";
  93.             count--;
  94.          }
  95.    
  96.          cout <<endl;
  97.          HEX_OFF        
  98.          
  99.          cout <<endl<<"...ASCII DATOS..."<<endl;
  100.          //Analisis ASCII
  101.          count=((UDPheader.length -8)/2);//OJO REVISAR SI CAMBIO DE DATA
  102.          data=UDPheader.data;
  103.         while (count >0){//Data
  104.  
  105.             if (isgraph((char) (*data >>8) )){cout<<static_cast<char>(*data>>8);}
  106.             if (isgraph((char) (*data & 0xFF) )){cout<<static_cast<char>(*data&0xFF);}  
  107.                
  108.             data++;
  109.             count--;
  110.         }
  111.          
  112.          
  113.      }
  114. //-----------------------------------------------------------------------------------------------------------    
  115.     unsigned short headerUDP::checksum(){//0x97D6
  116.         #define CARRY if (buffer>USHRT_MAX){buffer&= 0xFFFF ;buffer++;};//carry >65 535 = 0xFFFF
  117.        
  118.         int buffer(0),count(((UDPheader.length -8)/2));
  119.         unsigned short *data=UDPheader.data;
  120.        
  121.         UDPheader.checksum=0;//Reset
  122.         //PseudoHeader
  123.         buffer+=UDPheader.IP_SOURCE_H;CARRY
  124.         buffer+=UDPheader.IP_SOURCE_L;CARRY
  125.        
  126.         buffer+=UDPheader.IP_DEST_H;CARRY
  127.         buffer+=UDPheader.IP_DEST_L;CARRY
  128.        
  129.         buffer+=UDPheader.ZEROES_PROTOCOL;CARRY    
  130.         buffer+=UDPheader.UDP_LENGTH;CARRY        
  131.        
  132.        
  133.         buffer+=UDPheader.source;CARRY
  134.         buffer+=UDPheader.dest;CARRY
  135.         buffer+=UDPheader.length;CARRY
  136.         //buffer+=UDPheader.checksum;CARRY
  137.    
  138.         while (count >0){
  139.             buffer+=*data++;CARRY
  140.             count--;
  141.         }                  
  142.        
  143.         UDPheader.checksum=~buffer;
  144.         return UDPheader.checksum;
  145.        
  146.     }
  147. //-----------------------------------------------------------------------------------------------------------
  148. //-----------------------------------------------------------------------------------------------------------
  149. //-----------------------------------------------------------------------------------------------------------
  150. int main (){
  151.     headerUDP x;
  152.    // x.HexDump();
  153.     x.checksum();
  154.     x.HexDump();
  155.     return 0;
  156. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement