ppp,mac帧模拟封装

#include
#include
#include
#include
#include
#include
#include
#include
#include
#pragma comment(lib, "IPHlpApi.lib")
using namespace std;
#define min 0
#define max 255
#define cunit 64

char code[cunit+4]; //原始64字节数据+4字节crc
DWORD crc32tb[256];//crc32的表
FILE *infofile; //原始数据的01文本文件
FILE *mac; //封装成mac帧文件
FILE *pppcode; //ppp异步字节填充文件
FILE *pppbit; //ppp同步字符填充文件

bool openinfofile(char * style)
{
if((infofile=fopen("info.txt",style))==NULL)
{
cout<<"输出文件打不开!";
return false;
}
else
return true;
}

bool generateinfo()//生成随机数据的01文本文件
{
srand(time(NULL));//播种
int temp;
for(int i=0; i{
temp=rand()%(max+1-min)+min;//从0~255
code[i]=temp;
}//生成随机数据
if(!openinfofile("w"))
{
return false;
}
for(int i=0; i{
int k=0;
char tempc;
for(int j=0; j<8; ++j)
{
k=0x80&(code[i]<tempc=(k==0)?'0':'1';
fputc(tempc,infofile);
cout<}
}
fclose(infofile);
return true;
}

void gencrc32tb()
{
DWORD CRC;
for(int i=0; i<256; i++) //用++i以提高效率
{
CRC=i;
//这个循环实际上就是用"计算法"来求取CRC的校验码
for(int j=0; j<8; j++)
{
if(CRC&1)
CRC=(CRC>>1)^0xEDB88320; //0xEDB88320就是CRC-32多项表达式的值
else
CRC>>=1;
}
crc32tb[i]=CRC;
}
}

DWORD calccrc32(void *data,DWORD len)
{
DWORD CRC32 = 0; //设置初始值
for(DWORD i=0; iCRC32 = crc32tb[ (CRC32^((BYTE*)data)[i]) & 0xff] ^ (CRC32>>8);
return CRC32;
}

void putinto(FILE * &p,char a)
{
char tempc;
int kk=0;
for(int j=0; j<8; ++j)
{
kk=0x80&(a<tempc=(kk==0)?'0':'1';
fputc(tempc,p);
}
}


bool addcrc32info(DWORD crc)
{
if(!openinfofile("a+"))
return false;
int k=0;
char tempc;
for(int j=0; j<32; ++j)
{
k=0x80000000&(crc<tempc=(k==0)?'0':'1';
fputc(tempc,infofile);
}
fclose(infofile);
return true;
}
bool pppgenbit()
{
if((pppbit=fopen("bit.txt","w"))==NULL)
{
cout<<"输出文件打不开!";
return false;
}
if(!openinfofile("r"))
return false;
char c1_5[5]= {0x7e,0xff,0x03,0x00,0x21};//前5字节的填充
char cur;//当前bit
int count=0;//连续1的计数
fputs("01111110",pppbit);//边界字符
for(int i=1; i<5; ++i)
{
int re=0;
for(

int j=0; j<8; ++j)
{
re=0x80&(c1_5[i]<if(re)
{
count++;
if(count==5)
{
fputc('1',pppbit);
fputc('0',pppbit);
count=0;
}
else
fputc('1',pppbit);
}
else
{
fputc('0',pppbit);
count=0;
}
}
}
while((cur=fgetc(infofile))!=EOF)
{
if(cur=='1')
{
count++;
if(count==5)
{
fputc('1',pppbit);
fputc('0',pppbit);
count=0;
}
else
fputc('1',pppbit);
}
else
{
fputc('0',pppbit);
count=0;
}
}
fputs("01111110",pppbit);//7E结尾,边界
fclose(pppbit);
fclose(infofile);
return true;
}

void insertmacadd()
{
IP_ADAPTER_ADDRESSES_LH * pAddresses=NULL;
ULONG outBufLen = 0;
GetAdaptersAddresses(AF_UNSPEC,0, NULL, pAddresses,&outBufLen);
pAddresses = (IP_ADAPTER_ADDRESSES_LH *) malloc(outBufLen);
if (GetAdaptersAddresses(AF_INET,GAA_FLAG_SKIP_ANYCAST,NULL,pAddresses,&outBufLen) == NO_ERROR)
{
//寻找物理网卡
while (pAddresses)
{
char tmp[128];
WideCharToMultiByte( CP_ACP, 0, pAddresses->Description, -1,tmp, 128, NULL, NULL );
if(strstr(tmp,"EtherNet")>0)//以太网适配器
{
char temp;
for(int i=0; i<6; ++i)
{
temp= pAddresses->PhysicalAddress[i];
putinto(mac,temp);
}
break;
}
pAddresses = pAddresses->Next;
}
}
//释放内存空间
if (pAddresses)
{
delete pAddresses;
}
}

bool genmac()
{
openinfofile("r");
if((mac=fopen("mac.txt","w"))==NULL)
{
cout<<"输出文件打不开!";
return false;
}
char cur;
fputs("111111111111111111111111111111111111111111111111",mac);//广播地址全为1,6字节个1
insertmacadd();
fputs("0000100000000000",mac);//表示上层是ip数据报0x0800
while((cur=fgetc(infofile))!=EOF)
{
fputc(cur,mac);
}
fclose(infofile);
fclose(mac);
return true;
}
void pppgencode(DWORD crc)
{
if((pppcode=fopen("code.txt","w"))==NULL)
{
cout<<"输出文件打不开!";
return ;
}
char *a=(char *)malloc(sizeof(char)*4);
ultoa(crc,a,10);
code[cunit]=a[0];
code[cunit+1]=a[1];
code[cunit+2]=a[2];
code[cunit+3]=a[3];
//for(int i=0;i//{
// cout<//}
//cout<//char c1_5[5]= {0x7e,0xff

,0x03,0x00,0x21};//前5字节的填充
putinto(pppcode,0x7d);//
putinto(pppcode,0x5e);
putinto(pppcode,0xff);
putinto(pppcode,0x7d);
putinto(pppcode,0x5d);
putinto(pppcode,0x7d);
putinto(pppcode,0x20);
putinto(pppcode,0x21);
for(int i=0; i{
if(code[i]==0x7d)//7d
{
putinto(pppcode,0x7d) ;
putinto(pppcode,0x5d) ;
}
else if(code[i]==0x7e)
{
putinto(pppcode,0x7d) ;
putinto(pppcode,0x5e) ;
}
else if(code[i]<0x20)
{
putinto(pppcode,0x7d) ;
putinto(pppcode,code[i]+32) ;
}
else
{
putinto(pppcode,code[i]) ;
}
}
putinto(pppcode,0x7d) ;
putinto(pppcode,0x5e) ;//结束符号
fclose(pppcode);
}
int main()
{
cout<<"生成的64字节的数据二进制形式如下:"<generateinfo();//随机生成64字节数据
gencrc32tb();//查表法计算32为crc校验码。生成表
DWORD crc32=0;//32位crc的fcs
crc32=calccrc32(code,cunit);//生成crc验证码,
addcrc32info(crc32);//生成临时文本文件

cout<<"\n封装成的同步比特填充的ppp帧如下:"<pppgenbit();

cout<<"\n封装成的异步字节填充的ppp帧如下:"<pppgencode(crc32);

cout<<"\n封装成的MAC广播帧如下:"<genmac();

system("PAUSE");
return 0;
}


相关主题
相关文档
最新文档