-
Notifications
You must be signed in to change notification settings - Fork 0
/
net.c
133 lines (116 loc) · 3.48 KB
/
net.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/*! \file net.c \brief Network support library. */
//*****************************************************************************
//
// File Name : 'net.c'
// Title : Network support library
// Version : 1.0
// Target MCU : Atmel AVR series
//
//*****************************************************************************
#include "avrlibtypes.h"
#include "global.h"
//#include "rprintf.h"
#include "net.h"
uint16_t htons(uint16_t val)
{
return (val<<8) | (val>>8);
}
uint32_t htonl(uint32_t val)
{
return (htons(val>>16) | (uint32_t)htons(val&0x0000FFFF)<<16);
}
//uint16_t netChecksum(uint16_t *data, uint16_t len)
uint16_t netChecksum(uint8_t *data, uint16_t len)
{
register uint32_t sum = 0;
uint16_t temp=0;
for (;;) {
if (len < 2)
break;
temp=*(data+1);
temp=temp<<8;
temp=temp+(*(data));
sum +=temp;
//sum += *((uint16_t *)data);
data+=2;
len -= 2;
}
if (len)
sum +=(*(data));
// sum += *(uint8_t *) data;
while ((len = (uint16_t) (sum >> 16)) != 0)
sum = (uint16_t) sum + len;
return (uint16_t) sum ^ 0xFFFF;
}
/*
void netPrintEthAddr(struct netEthAddr* ethaddr)
{
rprintfu08(ethaddr->addr[0]);
rprintfChar(':');
rprintfu08(ethaddr->addr[1]);
rprintfChar(':');
rprintfu08(ethaddr->addr[2]);
rprintfChar(':');
rprintfu08(ethaddr->addr[3]);
rprintfChar(':');
rprintfu08(ethaddr->addr[4]);
rprintfChar(':');
rprintfu08(ethaddr->addr[5]);
}
void netPrintIPAddr(uint32_t ipaddr)
{
rprintf("%d.%d.%d.%d",
((unsigned char*)&ipaddr)[3],
((unsigned char*)&ipaddr)[2],
((unsigned char*)&ipaddr)[1],
((unsigned char*)&ipaddr)[0]);
}
void netPrintEthHeader(struct netEthHeader* eth_hdr)
{
rprintfProgStrM("Eth Packet Type: 0x");
rprintfu16(eth_hdr->type);
rprintfProgStrM(" SRC:");
netPrintEthAddr(ð_hdr->src);
rprintfProgStrM("->DST:");
netPrintEthAddr(ð_hdr->dest);
rprintfCRLF();
}
void netPrintIpHeader(struct netIpHeader* ipheader)
{
rprintfProgStrM("IP Header\r\n");
rprintf("Ver : %d\r\n", (ipheader->vhl)>>4);
rprintf("Length : %d\r\n", htons(ipheader->len));
if(ipheader->proto == IP_PROTO_ICMP)
rprintfProgStrM("Protocol: ICMP\r\n");
else if(ipheader->proto == IP_PROTO_TCP)
rprintfProgStrM("Protocol: TCP\r\n");
else if(ipheader->proto == IP_PROTO_UDP)
rprintfProgStrM("Protocol: UDP\r\n");
else
rprintf("Protocol: %d\r\n", ipheader->proto);
rprintfProgStrM("SourceIP: "); netPrintIPAddr(htonl(ipheader->srcipaddr)); rprintfCRLF();
rprintfProgStrM("Dest IP: "); netPrintIPAddr(htonl(ipheader->destipaddr)); rprintfCRLF();
}
void netPrintTcpHeader(struct netTcpHeader* tcpheader)
{
rprintfProgStrM("TCP Header\r\n");
rprintf("Src Port: %d\r\n", htons(tcpheader->srcport));
rprintf("Dst Port: %d\r\n", htons(tcpheader->destport));
rprintfProgStrM("Seq Num : 0x"); rprintfu32(htonl(tcpheader->seqno)); rprintfCRLF();
rprintfProgStrM("Ack Num : 0x"); rprintfu32(htonl(tcpheader->ackno)); rprintfCRLF();
rprintfProgStrM("Flags : ");
if(tcpheader->flags & TCP_FLAGS_FIN)
rprintfProgStrM("FIN ");
if(tcpheader->flags & TCP_FLAGS_SYN)
rprintfProgStrM("SYN ");
if(tcpheader->flags & TCP_FLAGS_RST)
rprintfProgStrM("RST ");
if(tcpheader->flags & TCP_FLAGS_PSH)
rprintfProgStrM("PSH ");
if(tcpheader->flags & TCP_FLAGS_ACK)
rprintfProgStrM("ACK ");
if(tcpheader->flags & TCP_FLAGS_URG)
rprintfProgStrM("URG ");
rprintfCRLF();
}
*/