-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathip.c
109 lines (96 loc) · 4.34 KB
/
ip.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
//********************************************************************************************
//
// File : ip.c implement for Internet Protocol
//
//********************************************************************************************
#include "includes.h"
//********************************************************************************************
//
// +------------+-----------+----------+
// + MAC header + IP header + Data ::: +
// +------------+-----------+----------+
//
// IP Header
//
// +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
// +00+01+02+03+04+05+06+07+08+09+10+11+12+13+14+15+16+17+18+19+20+21+22+23+24+25+26+27+28+29+30+31+
// +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
// + Version + IHL + TOS + Total length +
// +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
// + Identification + Flags + Fragment offset +
// +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
// + TTL + Protocol + Header checksum +
// +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
// + Source IP address +
// +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
// + Destination IP address +
// +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
// + Options and padding ::: +
// +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
//
//********************************************************************************************
static WORD_BYTES ip_identfier=(WORD_BYTES){1};
//********************************************************************************************
//
// Function : ip_generate_packet
// Description : generate all ip header
//
//********************************************************************************************
void ip_generate_header ( BYTE *rxtx_buffer, WORD_BYTES total_length, BYTE protocol, BYTE *dest_ip )
{
BYTE i;
WORD_BYTES ck;
// set ipv4 and header length
rxtx_buffer[ IP_P ] = IP_V4_V | IP_HEADER_LENGTH_V;
// set TOS to default 0x00
rxtx_buffer[ IP_TOS_P ] = 0x00;
// set total length
rxtx_buffer [ IP_TOTLEN_H_P ] = total_length.byte.high;
rxtx_buffer [ IP_TOTLEN_L_P ] = total_length.byte.low;
// set packet identification
rxtx_buffer [ IP_ID_H_P ] = ip_identfier.byte.high;
rxtx_buffer [ IP_ID_L_P ] = ip_identfier.byte.low;
ip_identfier.word++;
// set fragment flags
rxtx_buffer [ IP_FLAGS_H_P ] = 0x00;
rxtx_buffer [ IP_FLAGS_L_P ] = 0x00;
// set Time To Live
rxtx_buffer [ IP_TTL_P ] = 128;
// set ip packettype to tcp/udp/icmp...
rxtx_buffer [ IP_PROTO_P ] = protocol;
// set source and destination ip address
for ( i=0; i<4; i++ )
{
rxtx_buffer[ IP_DST_IP_P + i ] = dest_ip[i];
rxtx_buffer[ IP_SRC_IP_P + i ] = avr_ip.byte[ i ];
}
// clear the 2 byte checksum
rxtx_buffer[ IP_CHECKSUM_H_P ] = 0;
rxtx_buffer[ IP_CHECKSUM_L_P ] = 0;
// fill checksum value
// calculate the checksum:
ck.word = software_checksum ( &rxtx_buffer[ IP_P ], sizeof(IP_HEADER), 0 );
rxtx_buffer[ IP_CHECKSUM_H_P ] = ck.byte.high;
rxtx_buffer[ IP_CHECKSUM_L_P ] = ck.byte.low;
}
//********************************************************************************************
//
// Function : ip_check_ip
// Description : Check incoming packet
//
//********************************************************************************************
BYTE ip_packet_is_ip ( BYTE *rxtx_buffer )
{
unsigned char i;
// if ethernet type is not ip
if ( rxtx_buffer[ ETH_TYPE_H_P ] != ETH_TYPE_IP_H_V || rxtx_buffer[ ETH_TYPE_L_P ] != ETH_TYPE_IP_L_V)
return 0;
// if ip packet not send to avr
for ( i=0; i<sizeof(IP_ADDR); i++ )
{
if ( rxtx_buffer[ IP_DST_IP_P + i ] != avr_ip.byte[i] )
return 0;
}
// destination ip address match with avr ip address
return 1;
}