-
Notifications
You must be signed in to change notification settings - Fork 2
/
aSocket.h
129 lines (93 loc) · 2.97 KB
/
aSocket.h
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
/*
-------------------------------------------------------------------
aSocket.h, TCP/IP stack implementation for Arduino
-------------------------------------------------------------------
Version: 1.1
Author: Adrian Brzezinski <[email protected]> (C)2010
Copyright: GPL V2 (http://www.gnu.org/licenses/gpl.html)
TODO: TCP and UDP checksum for received packets?
*/
#ifndef __ASOCKET_H__
#define __ASOCKET_H__
#define ASOCKET_COMPILE_UDP
#define ASOCKET_COMPILE_TCP
#define ASOCKET_BUFSIZE 160
#define ASOCKET_CONTO 30000 // time out for whole connection
#define ASOCKET_REQTO 3000 // time out for various requests
#define ASOCKET_RETRIES 3
#define ASOCKET_NOFLAGS 0x0
#define ASOCKET_PGM_DATA 0x1
#define ASOCKET_MORE_DATA 0x2
#define ASOCKET_TCP_OPT 0x4
#define ASOCKET_CHECKSUM 0x8
#include <inttypes.h>
#include <avr/pgmspace.h>
#include "aInet.h"
extern "C" {
#include "enc28j60.h"
}
//#define __ASOCK_DBG__
//#define __ASOCK_DBG_ETH__
//#define __ASOCK_DBG_ARP__
//#define __ASOCK_DBG_IP__
//#define __ASOCK_DBG_ICMP__
//#define __ASOCK_DBG_UDP__
//#define __ASOCK_DBG_TCP__
typedef enum constate_e {
ASOCK_LISTEN=0,
ASOCK_QUERYARP,
ASOCK_INIT,
ASOCK_ESTABLISHED,
ASOCK_CLOSED
} constate_t;
class aSocket {
// transmission control block, all of those data are in network order (big endian)
uint8_t hwaddr[ETH_ALEN];
uint32_t ipaddr;
uint8_t netmask;
uint16_t port;
uint32_t gatewayip;
uint8_t peerhwaddr[ETH_ALEN];
uint32_t peeripaddr;
uint16_t peerport;
uint8_t protocol;
constate_t constate;
#ifdef ASOCKET_COMPILE_TCP
uint32_t seq;
uint16_t seq_adv; // expected advance for seq number
uint32_t ack;
#endif
uint16_t availdata;
uint8_t pktbuf[ASOCKET_BUFSIZE];
void MakeEthReply( struct ethhdr *eth );
void MakeIpReply( struct iphdr *ip, uint16_t tot_len );
void MakeEth( struct ethhdr *eth, uint16_t h_proto );
void MakeIp( struct iphdr *ip, uint16_t tot_len, uint8_t protocol );
#ifdef ASOCKET_COMPILE_TCP
void MakeTcp( struct tcphdr *tcp, uint8_t tcpflags, uint16_t datalen, uint8_t flags );
void InitSEQ();
uint32_t IncNetNum( uint32_t &num, uint16_t addval );
void SendTCPSYN();
#endif
#ifdef ASOCKET_COMPILE_UDP
void MakeUdp( struct udphdr *udp, uint16_t datalen, uint8_t flags );
#endif
uint16_t OnChipChecksum( uint16_t pktaddr, uint8_t prot, uint16_t datalen );
void DispatchPacket( uint16_t pktlen );
void copyhwa( uint8_t *srchwa, uint8_t *dsthwa );
uint32_t subnet( uint32_t ip );
void QueryARP();
void HandleInetStack( uint32_t timeout );
public:
aSocket( void );
constate_t state() { return constate; }
void setup( uint32_t ip, uint8_t hwa[ETH_ALEN], uint8_t mask, uint32_t gw );
uint32_t listen( uint16_t portnum, uint8_t prot );
uint8_t connect( uint32_t ip, uint16_t portnum, uint8_t prot );
void flush() { availdata = 0; }
uint16_t available();
uint8_t* read( uint16_t *datasize );
uint16_t write( uint8_t *data, uint16_t datasize, uint8_t flags );
void close();
};
#endif * __ASOCKET_H__ */