//作者laogu http://www.laogu.com //程序的功能是根据多播地址multicast_address[6]的值,计算出MAR0-MAR7,就是multicast_filter[8]; //本程序指计算一个多播地址。如果有多个多播地址,将每个多播地址生成的multicast_filter[8]相或就可以了, //例如根据多播地址 01:00:5e:00:00:01生成的 value1=multicast_filter[8]; //根据多播地址 01:00:5e:00:00:02生成的 value2=multicast_filter[8]; //那么对这两个多播地址生成的multicast_filter[8]=value1 | value2 ;将两个值相或 //很容易从这里得到如果要接收所有多播地址的数据包,MAR0--MAR7必须设置为0xff,就是说 //multicast_filter[8]={0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff}; #define al ax_value.bytes.low #define bl bx_value.bytes.low #define cl cx_value.bytes.low #define ah ax_value.bytes.high #define bh bx_value.bytes.high #define dh dx_value.bytes.high #define ax ax_value.word #define bx bx_value.word #define cx cx_value.word #define dx dx_value.word #define shl_bx if((bx&0x8000)!=0){cf=1;}else{cf=0;};bx=bx<<1; #define shl_al if((al&0x80)!=0){cf=1;}else{cf=0;};al=al<<1; #define shr_al if((al&0x01)!=0){cf=1;}else{cf=0;};al=al>>1; #define rcl_dx if((dx&0x8000)!=0){cf_temp=1;}else{cf_temp=0;};dx=dx<<1;if(cf){dx=dx+1;};cf=cf_temp; #define rcl_ah if((ah&0x80)!=0){cf_temp=1;}else{cf_temp=0;};ah=ah<<1;if(cf){ah=ah+1;};cf=cf_temp; #define rcr_ah if((ah&0x01)!=0){cf_temp=1;}else{cf_temp=0;};ah=ah>>1;if(cf){ah=ah+0x80;};cf=cf_temp; #define rol_al if((al&0x80)!=0){cf=1;}else{cf=0;};al=al<<1;if(cf){al=al+1;}; #define ror_al if((al&0x01)!=0){cf=1;}else{cf=0;};al=al>>1;if(cf){al=al+0x80;}; union u {unsigned int word; struct{unsigned char high;unsigned char low;}bytes;//字节顺序为高位在前的2byte结构 }; union u ax_value; union u bx_value; union u cx_value; union u dx_value; bit cf; bit cf_temp; unsigned char multicast_address[6]={0x01,0x00,0x5e,0x00,0x00,0x00}; //unsigned char multicast_address[6]={0x00,0x00,0x00,0x5e,0x00,0x01}; //只计算一个多播地址 //unsigned char multicast_address[6]={0x01,0x80,0xc2,0x00,0x00,0x00}; unsigned char multicast_filter[8]={0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}; //结果放在这里,就是MAR0--MAR7的值 //;;0x41,0x00,0x00,0x80,0x00,0x00,0x00,0x00; void up_crc(unsigned char al_byte) { al=al_byte; ah=0; for (cx=0;cx<8;cx++) { shl_bx; rcl_dx; rcl_ah; ah=ah^al; rcr_ah; if(cf){ dx=dx^0x04c1;// 0000,0100,1100,0001b bx=bx^0x1db7; //0001,1101,1011,0110b + 1 ;plus one for end-around carry. } shr_al; } } void add_mc_bits() { unsigned char i; bx=0xffff; dx=0xffff; for(i=0;i<6;i++) { up_crc(multicast_address[i]); } cl=8; al=dh; rol_al; rol_al; rol_al; al=al&0x07; bl=al; bh=bh^bh; al=dh; ror_al; ror_al; al=al&0x07; cl=al; al=1; for(i=0;i