void CRainDes::cipher(BYTE* key_ptr, BYTE* input, BYTE* output, BYTE mode) { BYTE i; BYTE last[8], temp[4]; memcpy (m_key, key_ptr, 8 ); gen_keys(); /* perform 'Initial Permutation' */ do_ip(input, last, m_ip_table); /* Generate 'Preoutput' */ for (i=0; i<16; i++) { if (mode) function_rk(&last[4], temp, m_perm_keys[15-i]); else function_rk(&last[4], temp, m_perm_keys[i]); /* XOR result with lower bits of last iteration */ temp[0] ^= last[0]; temp[1] ^= last[1]; temp[2] ^= last[2]; temp[3] ^= last[3]; /* copy high 32 bits of previous iteration to low bits */ last[0] = last[4]; last[1] = last[5]; last[2] = last[6]; last[3] = last[7]; /* copy result of XOR to high bits */ last[4] = temp[0]; last[5] = temp[1]; last[6] = temp[2]; last[7] = temp[3]; } i = last[4]; last[4] = last[0]; last[0] = i; i = last[5]; last[5] = last[1]; last[1] = i; i = last[6]; last[6] = last[2]; last[2] = i; i = last[7]; last[7] = last[3]; last[3] = i; /* perform 'Inverse Initial Permutation' */ do_ip(last, output, m_inv_ip_table); }
void do_frame(int sd) { int len; char buf[2000]; struct ether_header *eth = NULL; bzero(buf, sizeof(buf)); len = recvfrom(sd, buf, sizeof(buf), 0, NULL, NULL); if(len <= 0) return; g.packet_frame++; g.bytes += len; printf("\033[H\033[2J"); fflush(stdout); printf("***************************************************\n"); printf("****************** 第 %03d 个frame *******************\n", g.packet_frame); printf("*****************************************************\n"); if(g.print_flag_frame) print_frame(buf); eth = (void *)buf; switch(ntohs(eth->ether_type)) { case ETHERTYPE_IP: do_ip(eth + 1); break; case ETHERTYPE_ARP: do_arp(eth + 1); break; case ETHERTYPE_REVARP: do_rarp(eth + 1); break; } }
/* 用于从网卡接受一帧数据,同时根据以太网协议字段传递数据给相应的上层协议处理 */ void do_frame( int sock ) { /* 用于存储一帧数据 */ char frame_buf[ 2000 ]; /* 清空帧数据缓冲区 */ bzero( frame_buf, sizeof(frame_buf) ); int len = sizeof( frame_buf ); /* 用于存储接受字节数 */ int recv_num; /* 用于存储发送方的地址信息 */ struct sockaddr_in addr; /* 从网卡接收一帧数据 */ recv_num = recvfrom( sock, (char *)frame_buf, sizeof( frame_buf ), 0, ( struct sockaddr * )&addr, &len ); /* 所接收的包的总数自加1 */ global.packet_num ++; /* 从网卡接收的字节总数 */ global.bytes += recv_num; /* 打印接收的包是第几个包 */ //printf("此帧数据长度: %d\n", recv_num ); /* 定义一个用于指向以太网帧的指针 (这里我们只考虑最常见的以太网帧的情况) */ struct ether_header * peth; /* 让以太网头指针指向从网卡接受到的帧的数据的开头 */ peth = (struct ether_header *)frame_buf; /* 传递以太网帧首地址给打印以太网帧信息的打印函数 */ if( global.print_flag_frame ) print_frame( peth ); /* 定义一个数据指针,用于指向以太网帧的数据部分 */ char * pdata; /* 让 pdata 指向以太网帧的数据部分 */ pdata = frame_buf + sizeof( struct ether_header ); /* 根据以太网帧的协议字段进行数据分用 - 也就是进行数据拆封,根据协议字段调用相应层的处理函数 */ switch( ntohs( peth->ether_type ) ){ case ETHERTYPE_PUP: break; case ETHERTYPE_IP: do_ip( pdata ); break; case ETHERTYPE_ARP: do_arp( pdata ); break; case ETHERTYPE_REVARP: do_rarp( pdata ); break; default: printf("Unkonw ethernet type %d %x.\n", ntohs(peth->ether_type), ntohs(peth->ether_type) ); break; } }