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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
|
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include "eos.h"
#include "event.h"
#include "dev/net.h"
#include "sock.h"
static eos_evt_handler_t evt_handler[EOS_SOCK_MAX_SOCK];
static void sock_handle_msg(unsigned char type, unsigned char *buffer, uint16_t len) {
unsigned char sock;
if ((buffer == NULL) || (len < 2)) {
eos_net_bad_handler(type, buffer, len);
return;
}
sock = buffer[1];
if ((sock == 0) || (sock > EOS_SOCK_MAX_SOCK) || (evt_handler[sock - 1] == NULL)) {
eos_net_bad_handler(type, buffer, len);
return;
}
switch(buffer[0]) {
case EOS_SOCK_MTYPE_PKT:
evt_handler[sock - 1](type, buffer, len);
break;
default:
eos_net_bad_handler(type, buffer, len);
break;
}
}
void eos_sock_init(void) {
int i;
for (i=0; i<EOS_SOCK_MAX_SOCK; i++) {
evt_handler[i] = NULL;
}
eos_net_set_handler(EOS_NET_MTYPE_SOCK, sock_handle_msg);
}
void eos_sock_set_handler(unsigned char sock, eos_evt_handler_t handler) {
if (sock && (sock <= EOS_SOCK_MAX_SOCK)) evt_handler[sock - 1] = handler;
}
eos_evt_handler_t eos_sock_get_handler(unsigned char sock) {
if (sock && (sock <= EOS_SOCK_MAX_SOCK)) return evt_handler[sock - 1];
return NULL;
}
int eos_sock_open_udp(eos_evt_handler_t handler, unsigned char *buffer) {
unsigned char type;
uint16_t len;
int do_release;
int rv, sock;
do_release = 0;
if (buffer == NULL) {
buffer = eos_net_alloc();
do_release = 1;
}
type = EOS_NET_MTYPE_SOCK;
len = 1;
buffer[0] = EOS_SOCK_MTYPE_OPEN_DGRAM;
rv = eos_net_xchg(&type, buffer, &len);
if (rv) goto sock_open_udp_fin;
if (type != EOS_NET_MTYPE_SOCK) {
rv = EOS_ERR_NET;
goto sock_open_udp_fin;
}
if (len < 2) {
rv = EOS_ERR_SIZE;
goto sock_open_udp_fin;
}
sock = buffer[1];
if (sock == 0) {
rv = EOS_ERR_NET;
goto sock_open_udp_fin;
}
rv = sock;
eos_sock_set_handler(sock, handler);
sock_open_udp_fin:
if (do_release) eos_net_free(buffer, 1);
return rv;
}
void eos_sock_close(unsigned char sock, unsigned char *buffer) {
int async;
async = 0;
if (buffer == NULL) {
buffer = eos_net_alloc();
async = 1;
}
buffer[0] = EOS_SOCK_MTYPE_CLOSE;
buffer[1] = sock;
_eos_net_send(EOS_NET_MTYPE_SOCK, buffer, 2, async, 1);
eos_sock_set_handler(sock, NULL);
}
static int sock_send(unsigned char sock, unsigned char *msg, uint16_t msg_len, EOSNetAddr *addr, unsigned char *buffer) {
buffer[0] = EOS_SOCK_MTYPE_PKT;
buffer[1] = sock;
buffer += 2;
memcpy(buffer, addr->host, sizeof(addr->host));
buffer += sizeof(addr->host);
buffer[0] = addr->port >> 8;
buffer[1] = addr->port;
buffer += sizeof(addr->port);
if (msg) {
if (msg_len + EOS_SOCK_SIZE_UDP_HDR > EOS_NET_MTU) return EOS_ERR_SIZE;
memcpy(buffer, msg, msg_len);
}
return EOS_OK;
}
int eos_sock_sendto(unsigned char sock, unsigned char *msg, size_t msg_len, EOSNetAddr *addr, unsigned char *buffer) {
int rv;
rv = sock_send(sock, msg, msg_len, addr, buffer);
if (rv) return rv;
return eos_net_send(EOS_NET_MTYPE_SOCK, buffer, msg_len + EOS_SOCK_SIZE_UDP_HDR);
}
int eos_sock_sendto_async(unsigned char sock, unsigned char *msg, size_t msg_len, EOSNetAddr *addr, unsigned char *buffer, unsigned char more) {
int rv;
rv = sock_send(sock, msg, msg_len, addr, buffer);
if (rv) return rv;
return eos_net_send_async(EOS_NET_MTYPE_SOCK, buffer, msg_len + EOS_SOCK_SIZE_UDP_HDR, more);
}
int eos_sock_recvfrom(unsigned char *buffer, uint16_t len, unsigned char *msg, size_t msg_size, EOSNetAddr *addr) {
if (len < EOS_SOCK_SIZE_UDP_HDR) return EOS_ERR_SIZE;
if (buffer[0] != EOS_SOCK_MTYPE_PKT) return EOS_ERR_NET;
buffer += 2;
memcpy(addr->host, buffer, sizeof(addr->host));
buffer += sizeof(addr->host);
addr->port = (uint16_t)buffer[0] << 8;
addr->port |= (uint16_t)buffer[1];
buffer += sizeof(addr->port);
if (msg) {
if (msg_size < len - EOS_SOCK_SIZE_UDP_HDR) return EOS_ERR_SIZE;
memcpy(msg, buffer, len - EOS_SOCK_SIZE_UDP_HDR);
}
return EOS_OK;
}
|