Line No. | Rev | Author | Line |
---|---|---|---|
1 | 6 | kaklik | /*! \file net.c \brief Network support library. */ |
2 | //***************************************************************************** |
||
3 | // |
||
4 | // File Name : 'net.c' |
||
5 | // Title : Network support library |
||
6 | // Author : Pascal Stang |
||
7 | // Created : 8/30/2004 |
||
8 | // Revised : 7/3/2005 |
||
9 | // Version : 0.1 |
||
10 | // Target MCU : Atmel AVR series |
||
11 | // Editor Tabs : 4 |
||
12 | // |
||
13 | //***************************************************************************** |
||
14 | |||
15 | #include <inttypes.h> |
||
16 | #include "global.h" |
||
17 | #include "rprintf.h" |
||
18 | |||
19 | #include "net.h" |
||
20 | |||
21 | uint16_t htons(uint16_t val) |
||
22 | { |
||
23 | return (val<<8) | (val>>8); |
||
24 | } |
||
25 | |||
26 | uint32_t htonl(uint32_t val) |
||
27 | { |
||
28 | return (htons(val>>16) | (uint32_t)htons(val&0x0000FFFF)<<16); |
||
29 | } |
||
30 | |||
31 | |||
32 | uint16_t netChecksum(void *data, uint16_t len) |
||
33 | { |
||
34 | register uint32_t sum = 0; |
||
35 | |||
36 | for (;;) { |
||
37 | if (len < 2) |
||
38 | break; |
||
39 | //sum += *((uint16_t *)data)++; |
||
40 | sum += *((uint16_t *)data); |
||
41 | data+=2; |
||
42 | len -= 2; |
||
43 | } |
||
44 | if (len) |
||
45 | sum += *(uint8_t *) data; |
||
46 | |||
47 | while ((len = (uint16_t) (sum >> 16)) != 0) |
||
48 | sum = (uint16_t) sum + len; |
||
49 | |||
50 | return (uint16_t) sum ^ 0xFFFF; |
||
51 | } |
||
52 | |||
53 | void netPrintEthAddr(struct netEthAddr* ethaddr) |
||
54 | { |
||
55 | rprintfu08(ethaddr->addr[0]); |
||
56 | rprintfChar(':'); |
||
57 | rprintfu08(ethaddr->addr[1]); |
||
58 | rprintfChar(':'); |
||
59 | rprintfu08(ethaddr->addr[2]); |
||
60 | rprintfChar(':'); |
||
61 | rprintfu08(ethaddr->addr[3]); |
||
62 | rprintfChar(':'); |
||
63 | rprintfu08(ethaddr->addr[4]); |
||
64 | rprintfChar(':'); |
||
65 | rprintfu08(ethaddr->addr[5]); |
||
66 | } |
||
67 | |||
68 | void netPrintIPAddr(uint32_t ipaddr) |
||
69 | { |
||
70 | rprintf("%d.%d.%d.%d", |
||
71 | ((unsigned char*)&ipaddr)[3], |
||
72 | ((unsigned char*)&ipaddr)[2], |
||
73 | ((unsigned char*)&ipaddr)[1], |
||
74 | ((unsigned char*)&ipaddr)[0]); |
||
75 | } |
||
76 | |||
77 | /* |
||
78 | void netPrintEthHeader(struct netEthHeader* eth_hdr) |
||
79 | { |
||
80 | rprintfProgStrM("Eth Packet Type: 0x"); |
||
81 | rprintfu16(eth_hdr->type); |
||
82 | |||
83 | rprintfProgStrM(" SRC:"); |
||
84 | netPrintEthAddr(ð_hdr->src); |
||
85 | rprintfProgStrM("->DST:"); |
||
86 | netPrintEthAddr(ð_hdr->dest); |
||
87 | rprintfCRLF(); |
||
88 | } |
||
89 | |||
90 | void netPrintIpHeader(struct netIpHeader* ipheader) |
||
91 | { |
||
92 | rprintfProgStrM("IP Header\r\n"); |
||
93 | rprintf("Ver : %d\r\n", (ipheader->vhl)>>4); |
||
94 | rprintf("Length : %d\r\n", htons(ipheader->len)); |
||
95 | if(ipheader->proto == IP_PROTO_ICMP) |
||
96 | rprintfProgStrM("Protocol: ICMP\r\n"); |
||
97 | else if(ipheader->proto == IP_PROTO_TCP) |
||
98 | rprintfProgStrM("Protocol: TCP\r\n"); |
||
99 | else if(ipheader->proto == IP_PROTO_UDP) |
||
100 | rprintfProgStrM("Protocol: UDP\r\n"); |
||
101 | else |
||
102 | rprintf("Protocol: %d\r\n", ipheader->proto); |
||
103 | |||
104 | rprintfProgStrM("SourceIP: "); netPrintIPAddr(htonl(ipheader->srcipaddr)); rprintfCRLF(); |
||
105 | rprintfProgStrM("Dest IP: "); netPrintIPAddr(htonl(ipheader->destipaddr)); rprintfCRLF(); |
||
106 | } |
||
107 | |||
108 | void netPrintTcpHeader(struct netTcpHeader* tcpheader) |
||
109 | { |
||
110 | rprintfProgStrM("TCP Header\r\n"); |
||
111 | rprintf("Src Port: %d\r\n", htons(tcpheader->srcport)); |
||
112 | rprintf("Dst Port: %d\r\n", htons(tcpheader->destport)); |
||
113 | rprintfProgStrM("Seq Num : 0x"); rprintfu32(htonl(tcpheader->seqno)); rprintfCRLF(); |
||
114 | rprintfProgStrM("Ack Num : 0x"); rprintfu32(htonl(tcpheader->ackno)); rprintfCRLF(); |
||
115 | rprintfProgStrM("Flags : "); |
||
116 | if(tcpheader->flags & TCP_FLAGS_FIN) |
||
117 | rprintfProgStrM("FIN "); |
||
118 | if(tcpheader->flags & TCP_FLAGS_SYN) |
||
119 | rprintfProgStrM("SYN "); |
||
120 | if(tcpheader->flags & TCP_FLAGS_RST) |
||
121 | rprintfProgStrM("RST "); |
||
122 | if(tcpheader->flags & TCP_FLAGS_PSH) |
||
123 | rprintfProgStrM("PSH "); |
||
124 | if(tcpheader->flags & TCP_FLAGS_ACK) |
||
125 | rprintfProgStrM("ACK "); |
||
126 | if(tcpheader->flags & TCP_FLAGS_URG) |
||
127 | rprintfProgStrM("URG "); |
||
128 | rprintfCRLF(); |
||
129 | } |
||
130 | */ |
||
131 |
Powered by WebSVN v2.8.3