Dependencies:
NetServices
Dependents:
HvZ
net/net.h@0:7e2eb93442e7, 2010-12-12 (annotated)
- Committer:
- etherealflaim
- Date:
- Sun Dec 12 19:34:41 2010 +0000
- Revision:
- 0:7e2eb93442e7
Who changed what in which revision?
User | Revision | Line number | New contents of line |
etherealflaim |
0:7e2eb93442e7
|
1
|
/**
|
etherealflaim |
0:7e2eb93442e7
|
2
|
@file net.h
|
etherealflaim |
0:7e2eb93442e7
|
3
|
@brief This file encompasses all of the networking headers and includes them automatically
|
etherealflaim |
0:7e2eb93442e7
|
4
|
|
etherealflaim |
0:7e2eb93442e7
|
5
|
This file has some utility functions and definitions used by all of the networking headers,
|
etherealflaim |
0:7e2eb93442e7
|
6
|
and includes them all. This is the only file necessary to include to use all of the networking
|
etherealflaim |
0:7e2eb93442e7
|
7
|
facilities in nettool
|
etherealflaim |
0:7e2eb93442e7
|
8
|
*/
|
etherealflaim |
0:7e2eb93442e7
|
9
|
|
etherealflaim |
0:7e2eb93442e7
|
10
|
#ifndef NETWORK_H
|
etherealflaim |
0:7e2eb93442e7
|
11
|
#define NETWORK_H
|
etherealflaim |
0:7e2eb93442e7
|
12
|
|
etherealflaim |
0:7e2eb93442e7
|
13
|
#include "mbed.h"
|
etherealflaim |
0:7e2eb93442e7
|
14
|
|
etherealflaim |
0:7e2eb93442e7
|
15
|
#include "ctype.h"
|
etherealflaim |
0:7e2eb93442e7
|
16
|
|
etherealflaim |
0:7e2eb93442e7
|
17
|
/// General networking checksum - Used for IP, TCP, UDP, ICMP, etc.
|
etherealflaim |
0:7e2eb93442e7
|
18
|
/// Computes the one's complement of the one's complement sum of all of the given bytes except for the memory
|
etherealflaim |
0:7e2eb93442e7
|
19
|
/// at skip_byte for skip_count bytes (e.g. the checksum). Optionally resumes computation from the (given) last checksum.
|
etherealflaim |
0:7e2eb93442e7
|
20
|
inline u16 checksum(void *_mem, unsigned int bytes, void *skip_byte = NULL, unsigned int skip_count = 0, u16 last = 0)
|
etherealflaim |
0:7e2eb93442e7
|
21
|
{
|
etherealflaim |
0:7e2eb93442e7
|
22
|
u32 sum = 0;
|
etherealflaim |
0:7e2eb93442e7
|
23
|
u16 *mem = (u16*)_mem;
|
etherealflaim |
0:7e2eb93442e7
|
24
|
unsigned int skip_start = (u16*)skip_byte - mem;
|
etherealflaim |
0:7e2eb93442e7
|
25
|
|
etherealflaim |
0:7e2eb93442e7
|
26
|
if (last)
|
etherealflaim |
0:7e2eb93442e7
|
27
|
sum = last ^ 0xFFFF;
|
etherealflaim |
0:7e2eb93442e7
|
28
|
|
etherealflaim |
0:7e2eb93442e7
|
29
|
//main_log.printf("CK: 0x%8X", sum);
|
etherealflaim |
0:7e2eb93442e7
|
30
|
for (register unsigned int i = 0; i < bytes/sizeof(u16); ++i)
|
etherealflaim |
0:7e2eb93442e7
|
31
|
{
|
etherealflaim |
0:7e2eb93442e7
|
32
|
// Skip bytes we don't use (e.g. the checksum itself)
|
etherealflaim |
0:7e2eb93442e7
|
33
|
if (i == skip_start)
|
etherealflaim |
0:7e2eb93442e7
|
34
|
i += skip_count/sizeof(u16);
|
etherealflaim |
0:7e2eb93442e7
|
35
|
|
etherealflaim |
0:7e2eb93442e7
|
36
|
// Sum them up
|
etherealflaim |
0:7e2eb93442e7
|
37
|
sum += mem[i];
|
etherealflaim |
0:7e2eb93442e7
|
38
|
|
etherealflaim |
0:7e2eb93442e7
|
39
|
//main_log.printf("CK: + 0x%04X = 0x%8X", mem[i], sum);
|
etherealflaim |
0:7e2eb93442e7
|
40
|
}
|
etherealflaim |
0:7e2eb93442e7
|
41
|
|
etherealflaim |
0:7e2eb93442e7
|
42
|
// One's complement of the one's complement sum
|
etherealflaim |
0:7e2eb93442e7
|
43
|
sum += sum >> 16;
|
etherealflaim |
0:7e2eb93442e7
|
44
|
sum &= 0xFFFF;
|
etherealflaim |
0:7e2eb93442e7
|
45
|
sum ^= 0xFFFF;
|
etherealflaim |
0:7e2eb93442e7
|
46
|
|
etherealflaim |
0:7e2eb93442e7
|
47
|
//main_log.printf("CK: ~ 0x%8X", sum);
|
etherealflaim |
0:7e2eb93442e7
|
48
|
|
etherealflaim |
0:7e2eb93442e7
|
49
|
return (u16)(sum);
|
etherealflaim |
0:7e2eb93442e7
|
50
|
}
|
etherealflaim |
0:7e2eb93442e7
|
51
|
|
etherealflaim |
0:7e2eb93442e7
|
52
|
/// Generic u16 endian swapping
|
etherealflaim |
0:7e2eb93442e7
|
53
|
inline void fix_endian_u16(u16 *p)
|
etherealflaim |
0:7e2eb93442e7
|
54
|
{
|
etherealflaim |
0:7e2eb93442e7
|
55
|
char *bytes = (char*)p;
|
etherealflaim |
0:7e2eb93442e7
|
56
|
bytes[0] ^= bytes[1];
|
etherealflaim |
0:7e2eb93442e7
|
57
|
bytes[1] ^= bytes[0];
|
etherealflaim |
0:7e2eb93442e7
|
58
|
bytes[0] ^= bytes[1];
|
etherealflaim |
0:7e2eb93442e7
|
59
|
}
|
etherealflaim |
0:7e2eb93442e7
|
60
|
|
etherealflaim |
0:7e2eb93442e7
|
61
|
/// Generic u32 endian swapping
|
etherealflaim |
0:7e2eb93442e7
|
62
|
inline void fix_endian_u32(u32 *p)
|
etherealflaim |
0:7e2eb93442e7
|
63
|
{
|
etherealflaim |
0:7e2eb93442e7
|
64
|
char *bytes = (char*)p;
|
etherealflaim |
0:7e2eb93442e7
|
65
|
// Swap outer bytes
|
etherealflaim |
0:7e2eb93442e7
|
66
|
bytes[0] ^= bytes[3];
|
etherealflaim |
0:7e2eb93442e7
|
67
|
bytes[3] ^= bytes[0];
|
etherealflaim |
0:7e2eb93442e7
|
68
|
bytes[0] ^= bytes[3];
|
etherealflaim |
0:7e2eb93442e7
|
69
|
// Swap inner bytes
|
etherealflaim |
0:7e2eb93442e7
|
70
|
bytes[1] ^= bytes[2];
|
etherealflaim |
0:7e2eb93442e7
|
71
|
bytes[2] ^= bytes[1];
|
etherealflaim |
0:7e2eb93442e7
|
72
|
bytes[1] ^= bytes[2];
|
etherealflaim |
0:7e2eb93442e7
|
73
|
}
|
etherealflaim |
0:7e2eb93442e7
|
74
|
|
etherealflaim |
0:7e2eb93442e7
|
75
|
/* Printing character */
|
etherealflaim |
0:7e2eb93442e7
|
76
|
#define PCHAR(x) (isprint(x)?(x):'.')
|
etherealflaim |
0:7e2eb93442e7
|
77
|
|
etherealflaim |
0:7e2eb93442e7
|
78
|
|
etherealflaim |
0:7e2eb93442e7
|
79
|
// Ethernet
|
etherealflaim |
0:7e2eb93442e7
|
80
|
#include "ethernet.h"
|
etherealflaim |
0:7e2eb93442e7
|
81
|
|
etherealflaim |
0:7e2eb93442e7
|
82
|
// ARP and IP
|
etherealflaim |
0:7e2eb93442e7
|
83
|
#include "ip.h"
|
etherealflaim |
0:7e2eb93442e7
|
84
|
|
etherealflaim |
0:7e2eb93442e7
|
85
|
// TCP, UDP, and ICMP
|
etherealflaim |
0:7e2eb93442e7
|
86
|
#include "udp.h"
|
etherealflaim |
0:7e2eb93442e7
|
87
|
|
etherealflaim |
0:7e2eb93442e7
|
88
|
#endif // NETWORK_H |