Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <string>
- #include <cmath>
- #include <limits.h>
- #include <iomanip>
- using namespace std;
- //Activa o desactiva el formato HEX en COUT<<
- //#define HEX_ON std::cout.setf ( std::ios::hex, std::ios::basefield );std::cout.setf ( std::ios::showbase );
- #define HEX_ON std::cout.setf ( std::ios::hex, std::ios::basefield );
- #define HEX_OFF std::cout.unsetf ( std::ios::showbase );std::cout.setf ( std::ios::dec, std::ios::basefield );
- //Pseudo header DATOS PARA EFECTUAR EL TEST
- #define IP_SRC_H 0xC0A8
- #define IP_SRC_L 0x010C
- #define IP_DST_H 0xC0A8
- #define IP_DST_L 0x0101
- #define ZER_PROT 0x0011 //17 UDP
- //#define LEN_UDP 0X0028
- #define SRC_PORT 0xEEFC //0xEEFC 61180
- #define DST_PORT 0x0035 //53
- #define LONGITUD 0x0028 //40 bytes taille
- //Datos para efectuar tests....
- unsigned short DATA[]={0xD334, 0x0100 ,0x0001
- ,0x0000, 0x0000 ,0x0000 ,0x0370, 0x6F70 ,0x0777, 0x616E ,0x6164,
- 0x6F6F ,0x0266 ,0x7200 ,0x0001 ,0x0001};//16*2 =32 bytes
- typedef struct{
- //Pseudo header
- unsigned short IP_SOURCE_H,IP_SOURCE_L;
- unsigned short IP_DEST_H,IP_DEST_L;
- unsigned short ZEROES_PROTOCOL;
- unsigned short UDP_LENGTH;
- //
- unsigned short source,dest;//16+16 4bytes
- unsigned short length,checksum;//16+16 4bytes
- unsigned short * data;//32 bytes test
- //total 32+4+4=40
- }h_UDP;
- //-----------------------------------------------------------------------------------------------------------
- //-----------------------------------------------------------------------------------------------------------
- class headerUDP{
- public:
- headerUDP(unsigned short source=SRC_PORT ,unsigned short dest=DST_PORT ,unsigned short length =LONGITUD);
- void HexDump();
- unsigned short checksum();
- private:
- h_UDP UDPheader;
- };
- //-----------------------------------------------------------------------------------------------------------
- headerUDP::headerUDP(unsigned short source ,unsigned short dest ,unsigned short length ):
- // UDPheader({source,dest,length,0x0000,NULL})
- UDPheader({IP_SRC_H,IP_SRC_L,IP_DST_H,IP_DST_L,ZER_PROT,LONGITUD,source,dest,length,0x0000,NULL})
- {
- if (DATA!=NULL) {UDPheader.data=DATA; }//Asigna algunos datos
- }
- //-----------------------------------------------------------------------------------------------------------
- void headerUDP::HexDump(){
- int count=((UDPheader.length -8)/2);//OJO REVISAR SI CAMBIO DE DATA
- unsigned short *data=UDPheader.data;
- HEX_ON
- cout<< uppercase
- <<"PSEUDO HEADER "<<endl
- <<setfill('0') << setw(4) << UDPheader.IP_SOURCE_H<<' '
- <<setfill('0') << setw(4) << UDPheader.IP_SOURCE_L<<' '
- <<setfill('0') << setw(4) << UDPheader.IP_DEST_H<<' '
- <<setfill('0') << setw(4) << UDPheader.IP_DEST_L<<' '
- <<setfill('0') << setw(4) << UDPheader.ZEROES_PROTOCOL<<' '
- <<setfill('0') << setw(4) << UDPheader.UDP_LENGTH<<' '
- <<endl<<endl
- <<setfill('0') << setw(4) << UDPheader.source<<' '
- <<setfill('0') << setw(4) <<UDPheader.dest<<' '
- <<setfill('0') << setw(4) <<UDPheader.length<<' '
- <<setfill('0') << setw(4) <<UDPheader.checksum<<endl;
- while (count >0){//Data
- cout<< uppercase <<setfill('0') << setw(4)<<*data++<<" ";
- count--;
- }
- cout <<endl;
- HEX_OFF
- cout <<endl<<"...ASCII DATOS..."<<endl;
- //Analisis ASCII
- count=((UDPheader.length -8)/2);//OJO REVISAR SI CAMBIO DE DATA
- data=UDPheader.data;
- while (count >0){//Data
- if (isgraph((char) (*data >>8) )){cout<<static_cast<char>(*data>>8);}
- if (isgraph((char) (*data & 0xFF) )){cout<<static_cast<char>(*data&0xFF);}
- data++;
- count--;
- }
- }
- //-----------------------------------------------------------------------------------------------------------
- unsigned short headerUDP::checksum(){//0x97D6
- #define CARRY if (buffer>USHRT_MAX){buffer&= 0xFFFF ;buffer++;};//carry >65 535 = 0xFFFF
- int buffer(0),count(((UDPheader.length -8)/2));
- unsigned short *data=UDPheader.data;
- UDPheader.checksum=0;//Reset
- //PseudoHeader
- buffer+=UDPheader.IP_SOURCE_H;CARRY
- buffer+=UDPheader.IP_SOURCE_L;CARRY
- buffer+=UDPheader.IP_DEST_H;CARRY
- buffer+=UDPheader.IP_DEST_L;CARRY
- buffer+=UDPheader.ZEROES_PROTOCOL;CARRY
- buffer+=UDPheader.UDP_LENGTH;CARRY
- buffer+=UDPheader.source;CARRY
- buffer+=UDPheader.dest;CARRY
- buffer+=UDPheader.length;CARRY
- //buffer+=UDPheader.checksum;CARRY
- while (count >0){
- buffer+=*data++;CARRY
- count--;
- }
- UDPheader.checksum=~buffer;
- return UDPheader.checksum;
- }
- //-----------------------------------------------------------------------------------------------------------
- //-----------------------------------------------------------------------------------------------------------
- //-----------------------------------------------------------------------------------------------------------
- int main (){
- headerUDP x;
- // x.HexDump();
- x.checksum();
- x.HexDump();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement