📄 ip.i
字号:
#line 1 "..\ip.c" /0 #error *** WARNING C318 IN LINE 70 OF ..\ip.c: can't open file 'inet/debug.h' #error *** WARNING C318 IN LINE 71 OF ..\ip.c: can't open file 'inet/datatypes.h' #error *** WARNING C318 IN LINE 72 OF ..\ip.c: can't open file 'inet/ethernet.h' #error *** WARNING C318 IN LINE 73 OF ..\ip.c: can't open file 'inet/arp.h' #error *** WARNING C318 IN LINE 74 OF ..\ip.c: can't open file 'inet/ip.h' #error *** WARNING C318 IN LINE 75 OF ..\ip.c: can't open file 'inet/system.h' struct ip_frame received_ip_packet; struct ip_frame send_ip_packet; UINT16 ip_id; INT16 process_ip_in (struct ethernet_frame* frame) { UINT8 olen; UINT8 i; IP_DEBUGOUT("Checking if IP Protocol\n\r"); if( frame->protocol != PROTOCOL_IP ) return(-1); IP_DEBUGOUT("It's IP\n\r"); if( frame->frame_size < ETH_HEADER_LEN ) return(-1); if( (frame->frame_size - ETH_HEADER_LEN) < IP_HLEN ) return(-1); NETWORK_RECEIVE_INITIALIZE(frame->buf_index); received_ip_packet.vihl = RECEIVE_NETWORK_B(); if( (received_ip_packet.vihl & 0xF0) != 0x40 ) { IP_DEBUGOUT("ERROR: IP is not version 4!\n\r"); return(-1); } IP_DEBUGOUT("IP Version 4 OK!\n\r"); received_ip_packet.tos = RECEIVE_NETWORK_B(); received_ip_packet.tlen = ((UINT16)RECEIVE_NETWORK_B()) << 8; received_ip_packet.tlen |= RECEIVE_NETWORK_B(); received_ip_packet.id = RECEIVE_NETWORK_B() << 8; received_ip_packet.id |= RECEIVE_NETWORK_B(); received_ip_packet.frags = ((UINT16)RECEIVE_NETWORK_B()) << 8; received_ip_packet.frags |= RECEIVE_NETWORK_B(); received_ip_packet.ttl= RECEIVE_NETWORK_B(); received_ip_packet.protocol= RECEIVE_NETWORK_B(); received_ip_packet.checksum = ((UINT16)RECEIVE_NETWORK_B()) << 8; received_ip_packet.checksum |= RECEIVE_NETWORK_B(); received_ip_packet.sip = (((UINT32)RECEIVE_NETWORK_B()) << 24); received_ip_packet.sip |= (((UINT32)RECEIVE_NETWORK_B()) << 16); received_ip_packet.sip |= (((UINT32)RECEIVE_NETWORK_B()) << 8); received_ip_packet.sip |= RECEIVE_NETWORK_B(); received_ip_packet.dip = (((UINT32)RECEIVE_NETWORK_B()) << 24); received_ip_packet.dip |= (((UINT32)RECEIVE_NETWORK_B()) << 16); received_ip_packet.dip |= (((UINT32)RECEIVE_NETWORK_B()) << 8); received_ip_packet.dip |= RECEIVE_NETWORK_B(); if((received_ip_packet.dip != localmachine.localip )&& (received_ip_packet.dip != IP_BROADCAST_ADDRESS)) { IP_DEBUGOUT("IP address does not match!\n\r"); if( received_ip_packet.protocol != IP_ICMP) return(-1); for(i=0; i<PHY_ADR_LEN; i++) { if(frame->destination[i] != localmachine.localHW[i]) return(-1); } } olen = ((received_ip_packet.vihl & 0x0F) << 2) - IP_MIN_HLEN; if(olen > MAX_IP_OPTLEN) { IP_DEBUGOUT("ERROR:Size of maximum allowed IP option lengt exceeded!\n\r"); return(-1); } if( olen > (frame->frame_size - ETH_HEADER_LEN - IP_HLEN) ) { IP_DEBUGOUT("ERROR:IP option field too long!\n\r"); return(-1); } for( i=0; i < olen; i++ ) { received_ip_packet.opt[i] = RECEIVE_NETWORK_B(); IP_DEBUGOUT("IP Options..\n\r"); } if(received_ip_packet.tlen > (frame->frame_size - ETH_HEADER_LEN) ) { IP_DEBUGOUT("ERROR: Total len too long\r\n"); return(-1); } IP_DEBUGOUT("Validating the IP checksum..\n\r"); if ( ip_check_cs(&received_ip_packet) != TRUE ) { IP_DEBUGOUT("IP Checksum Corrupted..\n\r"); return(-1); } IP_DEBUGOUT("..Checksum OK!\n\r"); if( received_ip_packet.sip != IP_BROADCAST_ADDRESS) arp_add( received_ip_packet.sip, &frame->source[0], ARP_TEMP_IP); received_ip_packet.buf_index = frame->buf_index + IP_HLEN + olen; if( received_ip_packet.frags & (IP_MOREFRAGS | IP_FRAGOFF) ) { IP_DEBUGOUT("Fragmented IP packet\r\n"); return(-1); } IP_DEBUGOUT("Leaving IP succesfully..\n\r"); return(received_ip_packet.tlen - IP_HLEN - olen); } INT16 process_ip_out (UINT32 ipadr, UINT8 pcol, UINT8 tos, UINT8 ttl, UINT8* dat, UINT16 len) { struct arp_entry *qstruct; UINT16 i; qstruct = arp_find(ipadr, &localmachine, ARP_TEMP_IP); if( qstruct == 0 ) return(-2); switch(pcol) { case IP_ICMP: NETWORK_SEND_INITIALIZE(ICMP_BUF); IP_DEBUGOUT("Assembling IP packet to ICMP buffer\n\r"); break; case IP_UDP: NETWORK_SEND_INITIALIZE(UDP_BUF); IP_DEBUGOUT("Assembling IP packet to UDP buffer\n\r"); break; case IP_TCP: NETWORK_SEND_INITIALIZE(TCP_BUF); IP_DEBUGOUT("Assembling IP packet to TCP buffer\n\r"); break; default: return(-1); } for( i=0; i<MAXHWALEN; i++) { send_frame.destination[i] = qstruct->hwadr[i]; send_frame.source[i] = localmachine.localHW[i]; } send_frame.protocol = PROTOCOL_IP; NETWORK_ADD_DATALINK(&send_frame); send_ip_packet.vihl = IP_DEF_VIHL; send_ip_packet.tos = tos; send_ip_packet.tlen = IP_HLEN + len; send_ip_packet.id = ip_id++; send_ip_packet.frags = 0; send_ip_packet.ttl = ttl; send_ip_packet.protocol = pcol; send_ip_packet.checksum = 0; send_ip_packet.sip = localmachine.localip; send_ip_packet.dip = ipadr; send_ip_packet.checksum = ip_construct_cs( &send_ip_packet ); SEND_NETWORK_B(send_ip_packet.vihl); SEND_NETWORK_B(send_ip_packet.tos); SEND_NETWORK_B( (UINT8)(send_ip_packet.tlen >> 8) ); SEND_NETWORK_B( (UINT8)send_ip_packet.tlen ); SEND_NETWORK_B( (UINT8)(send_ip_packet.id >> 8) ); SEND_NETWORK_B( (UINT8)send_ip_packet.id ); SEND_NETWORK_B( (UINT8)(send_ip_packet.frags >> 8) ); SEND_NETWORK_B( (UINT8)send_ip_packet.frags ); SEND_NETWORK_B(send_ip_packet.ttl); SEND_NETWORK_B(send_ip_packet.protocol); SEND_NETWORK_B( (UINT8)(send_ip_packet.checksum >> 8) ); SEND_NETWORK_B( (UINT8)send_ip_packet.checksum ); SEND_NETWORK_B( (UINT8)(send_ip_packet.sip >> 24) ); SEND_NETWORK_B( (UINT8)(send_ip_packet.sip >> 16) ); SEND_NETWORK_B( (UINT8)(send_ip_packet.sip >> 8) ); SEND_NETWORK_B( (UINT8)send_ip_packet.sip ); SEND_NETWORK_B( (UINT8)(send_ip_packet.dip >> 24) ); SEND_NETWORK_B( (UINT8)(send_ip_packet.dip >> 16) ); SEND_NETWORK_B( (UINT8)(send_ip_packet.dip >> 8) ); SEND_NETWORK_B( (UINT8)send_ip_packet.dip ); for(i=0; i<len; i++) { SEND_NETWORK_B(*dat++); } NETWORK_COMPLETE_SEND( send_ip_packet.tlen + ETH_HEADER_LEN); return(len); } UINT32 ip_construct_cs (struct ip_frame* frame) { UINT16 ip_cs; UINT8 cs_cnt; UINT8 olen; UINT8 i; ip_cs = 0; cs_cnt = 0; ip_cs = ip_checksum(ip_cs, frame->vihl, cs_cnt++); ip_cs = ip_checksum(ip_cs, frame->tos, cs_cnt++); ip_cs = ip_checksum(ip_cs, (UINT8)(frame->tlen >> 8), cs_cnt++); ip_cs = ip_checksum(ip_cs, (UINT8)frame->tlen, cs_cnt++); ip_cs = ip_checksum(ip_cs, (UINT8)(frame->id >> 8), cs_cnt++); ip_cs = ip_checksum(ip_cs, (UINT8)frame->id, cs_cnt++); ip_cs = ip_checksum(ip_cs, (UINT8)(frame->frags >> 8), cs_cnt++); ip_cs = ip_checksum(ip_cs, (UINT8)frame->frags, cs_cnt++); ip_cs = ip_checksum(ip_cs, frame->ttl, cs_cnt++); ip_cs = ip_checksum(ip_cs, frame->protocol, cs_cnt++); ip_cs = ip_checksum(ip_cs, (UINT8)(frame->sip >> 24), cs_cnt++); ip_cs = ip_checksum(ip_cs, (UINT8)(frame->sip >> 16), cs_cnt++); ip_cs = ip_checksum(ip_cs, (UINT8)(frame->sip >> 8), cs_cnt++); ip_cs = ip_checksum(ip_cs, (UINT8)frame->sip, cs_cnt++); ip_cs = ip_checksum(ip_cs, (UINT8)(frame->dip >> 24), cs_cnt++); ip_cs = ip_checksum(ip_cs, (UINT8)(frame->dip >> 16), cs_cnt++); ip_cs = ip_checksum(ip_cs, (UINT8)(frame->dip >> 8), cs_cnt++); ip_cs = ip_checksum(ip_cs, (UINT8)frame->dip, cs_cnt++); olen = ((frame->vihl & 0x0F) << 2) - IP_MIN_HLEN; for( i=0; i<olen; i++) ip_cs = ip_checksum(ip_cs, (UINT8)frame->opt[i], cs_cnt++); ip_cs = ~ ip_cs; return(ip_cs); } UINT8 ip_check_cs (struct ip_frame* frame) { UINT16 ip_cs; UINT8 cs_cnt; UINT8 olen; UINT8 i; ip_cs = 0; cs_cnt = 0; ip_cs = ip_checksum(ip_cs, frame->vihl, cs_cnt++); ip_cs = ip_checksum(ip_cs, frame->tos, cs_cnt++); ip_cs = ip_checksum(ip_cs, (UINT8)(frame->tlen >> 8), cs_cnt++); ip_cs = ip_checksum(ip_cs, (UINT8)frame->tlen, cs_cnt++); ip_cs = ip_checksum(ip_cs, (UINT8)(frame->id >> 8), cs_cnt++); ip_cs = ip_checksum(ip_cs, (UINT8)frame->id, cs_cnt++); ip_cs = ip_checksum(ip_cs, (UINT8)(frame->frags >> 8), cs_cnt++); ip_cs = ip_checksum(ip_cs, (UINT8)frame->frags, cs_cnt++); ip_cs = ip_checksum(ip_cs, frame->ttl, cs_cnt++); ip_cs = ip_checksum(ip_cs, frame->protocol, cs_cnt++); ip_cs = ip_checksum(ip_cs, (UINT8)(frame->checksum >> 8), cs_cnt++); ip_cs = ip_checksum(ip_cs, (UINT8)frame->checksum, cs_cnt++); ip_cs = ip_checksum(ip_cs, (UINT8)(frame->sip >> 24), cs_cnt++); ip_cs = ip_checksum(ip_cs, (UINT8)(frame->sip >> 16), cs_cnt++); ip_cs = ip_checksum(ip_cs, (UINT8)(frame->sip >> 8), cs_cnt++); ip_cs = ip_checksum(ip_cs, (UINT8)frame->sip, cs_cnt++); ip_cs = ip_checksum(ip_cs, (UINT8)(frame->dip >> 24), cs_cnt++); ip_cs = ip_checksum(ip_cs, (UINT8)(frame->dip >> 16), cs_cnt++); ip_cs = ip_checksum(ip_cs, (UINT8)(frame->dip >> 8), cs_cnt++); ip_cs = ip_checksum(ip_cs, (UINT8)frame->dip, cs_cnt++); olen = ((frame->vihl & 0x0F) << 2) - IP_MIN_HLEN; for( i=0; i<olen; i++) ip_cs = ip_checksum(ip_cs, (UINT8)frame->opt[i], cs_cnt++); ip_cs = ~ ip_cs; if(ip_cs == IP_GOOD_CS) return 1; return 0; } UINT16 ip_checksum (UINT16 cs, UINT8 dat, UINT8 count) { UINT8 b = dat; UINT8 cs_l; UINT8 cs_h; cs_h = (UINT8)(cs >> 8); cs_l = (UINT8)cs; if( count & 0x01 ) { if( (cs_l = cs_l + b) < b ) { if( ++cs_h == 0 ) cs_l++; } } else { if( (cs_h = cs_h + b) < b ) { if( ++cs_l == 0 ) cs_h++; } } return( ( (UINT16)cs_h << 8 ) + cs_l); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -