-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUDP_API.c
More file actions
349 lines (250 loc) · 9.25 KB
/
UDP_API.c
File metadata and controls
349 lines (250 loc) · 9.25 KB
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
/*
2020 © Copyright (c) BiDaE Technology Inc.
Provided under BiDaE SHAREWARE LICENSE-1.0 in the LICENSE.
Project Name:
BeDIS
File Name:
UDP_API.c
File Description:
This file contains the program to transmission data using UDP protcol. The
device communicates by this UDP API should be in the same network.
Version:
2.0, 20190608
Abstract:
BeDIS uses LBeacons to deliver 3D coordinates and textual descriptions of
their locations to users' devices. Basically, a LBeacon is an inexpensive,
Bluetooth Smart Ready device. The 3D coordinates and location description
of every LBeacon are retrieved from BeDIS (Building/environment Data and
Information System) and stored locally during deployment and maintenance
times. Once initialized, each LBeacon broadcasts its coordinates and
location description to Bluetooth enabled user devices within its coverage
area.
Authors:
Gary Xiao , garyh0205@hotmail.com
*/
#include "UDP_API.h"
#include "libEncrypt.h"
int udp_initial(pudp_config udp_config, int recv_port)
{
int return_value;
int timeout;
#ifdef _WIN32
udp_config -> sockVersion = MAKEWORD(2,2);
if(WSAStartup(udp_config -> sockVersion, &udp_config -> wsaData) != 0)
return 0;
#endif
/* zero out the structure */
memset((char *) &udp_config -> si_server, 0,
sizeof(udp_config -> si_server));
if (return_value = init_Packet_Queue( &udp_config -> pkt_Queue) !=
pkt_Queue_SUCCESS)
return return_value;
if (return_value = init_Packet_Queue( &udp_config -> Received_Queue) !=
pkt_Queue_SUCCESS)
return return_value;
/* create a send UDP socket */
if ((udp_config -> send_socket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP))
== -1)
return send_socket_error;
/* create a recv UDP socket */
if ((udp_config -> recv_socket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP))
== -1)
return recv_socket_error;
timeout = UDP_SELECT_TIMEOUT; // sec
setsockopt(udp_config -> recv_socket, SOL_SOCKET, SO_RCVTIMEO, &timeout,
sizeof(timeout));
udp_config -> si_server.sin_family = AF_INET;
udp_config -> si_server.sin_port = htons(recv_port);
udp_config -> si_server.sin_addr.s_addr = htonl(INADDR_ANY);
udp_config -> shutdown = false;
udp_config -> recv_port = recv_port;
/* bind recv socket to the port */
if( bind(udp_config -> recv_socket, (struct sockaddr *)&udp_config ->
si_server, sizeof(udp_config -> si_server) ) == -1)
return recv_socket_bind_error;
/* The thread is used for receiving data */
pthread_create(&udp_config -> udp_receive_thread, NULL,
udp_recv_pkt_routine, (void*) udp_config);
pthread_detach(udp_config -> udp_receive_thread);
/* The thread is used for sending data */
pthread_create(&udp_config -> udp_send_thread, NULL, udp_send_pkt_routine,
(void*) udp_config);
pthread_detach(udp_config -> udp_send_thread);
return 0;
}
int udp_addpkt_without_encoding(pudp_config udp_config, char *address, unsigned int port,
char *content, int size)
{
if(size > MESSAGE_LENGTH)
return addpkt_msg_oversize;
addpkt(&udp_config -> pkt_Queue, address, port, content, size);
return 0;
}
sPkt udp_getrecv_without_encoding(pudp_config udp_config)
{
sPkt tmp = get_pkt(&udp_config -> Received_Queue);
return tmp;
}
int udp_addpkt(pudp_config udp_config, char *address, unsigned int port,
char *content, int size)
{
int ret = 0;
char content_sha256[LENGTH_OF_SHA256];
char ciphertext[LENGTH_OF_ENCODED_WIFI_MESSAGE];
memset(content_sha256, 0, sizeof(content_sha256));
ret = SHA_256_Hash(content, content_sha256, sizeof(content_sha256));
memset(ciphertext, 0, sizeof(ciphertext));
ret = AES_ECB_Encoder_With_Token_Prefix(content_sha256, ciphertext, sizeof(ciphertext));
strcat(ciphertext, DELIMITER_SEMICOLON);
strcat(ciphertext, content);
size = strlen(ciphertext);
if(size > MESSAGE_LENGTH)
return addpkt_msg_oversize;
addpkt(&udp_config -> pkt_Queue, address, port, ciphertext, size);
return 0;
}
sPkt udp_getrecv(pudp_config udp_config)
{
int ret = 0;
char content_sha256[LENGTH_OF_SHA256];
char decodedtext[LENGTH_OF_ENCODED_WIFI_MESSAGE];
char *ciphertext = NULL;
char *save_ptr = NULL;
sPkt empty_pkt;
sPkt tmp = get_pkt(&udp_config -> Received_Queue);
if(tmp.is_null == true)
return tmp;
ciphertext = strtok_save(tmp.content, DELIMITER_SEMICOLON, &save_ptr);
memset(decodedtext, 0, sizeof(decodedtext));
if(1 != AES_ECB_Decoder_With_Token_Prefix(ciphertext, decodedtext, sizeof(decodedtext)))
return empty_pkt;
memset(content_sha256, 0, sizeof(content_sha256));
ret = SHA_256_Hash(save_ptr, content_sha256, sizeof(content_sha256));
if(0 != strncmp(decodedtext, content_sha256, strlen(content_sha256)))
return empty_pkt;
strcpy(tmp.content, save_ptr);
tmp.content_size = strlen(tmp.content);
return tmp;
}
void *udp_send_pkt_routine(void *udpconfig)
{
pudp_config udp_config = (pudp_config) udpconfig;
sPkt current_send_pkt;
struct sockaddr_in si_send;
while((udp_config -> shutdown) == false)
{
if(!(is_null( &udp_config -> pkt_Queue)))
{
current_send_pkt = get_pkt(&udp_config -> pkt_Queue);
if(current_send_pkt.is_null == false)
{
memset(&si_send, 0, sizeof(si_send));
si_send.sin_family = AF_INET;
si_send.sin_port = htons(current_send_pkt.port);
si_send.sin_addr.s_addr = inet_addr(current_send_pkt.address);
#ifdef debugging
zlog_info(category_debug, "Start Send pkts\n(sendto [%s] msg [",
current_send_pkt.address);
print_content(current_send_pkt.content,
current_send_pkt.content_size);
zlog_info(category_debug, "])\n");
#endif
if (sendto(udp_config -> send_socket, current_send_pkt.content,
current_send_pkt.content_size, 0,
(struct sockaddr *)&si_send, sizeof(struct sockaddr)) == -1)
{
#ifdef debugging
zlog_info(category_debug, "sendto error.[%s]\n", strerror(errno));
#endif
}
else
{
#ifdef debugging
zlog_info(category_debug, "Send pkt success\n");
#endif
}
}
else
{
sleep_t(SEND_THREAD_IDLE_SLEEP_TIME);
}
}
else
{
sleep_t(SEND_THREAD_IDLE_SLEEP_TIME);
}
}
return (void *)NULL;
}
void *udp_recv_pkt_routine(void *udpconfig)
{
pudp_config udp_config = (pudp_config) udpconfig;
int recv_len;
char recv_buf[MESSAGE_LENGTH];
char *address_ntoa_ptr;
char address_ntoa[NETWORK_ADDR_LENGTH];
int port;
struct sockaddr_in si_recv;
int socketaddr_len = sizeof(si_recv);
/* keep listening for data */
while((udp_config -> shutdown) == false)
{
memset(&si_recv, 0, sizeof(si_recv));
memset(&recv_buf, 0, sizeof(char) * MESSAGE_LENGTH);
recv_len = 0;
#ifdef debugging
zlog_info(category_debug, "recv pkt.");
#endif
/* try to receive some data, this is a non-blocking call */
if ((recv_len = recvfrom(udp_config -> recv_socket, recv_buf,
MESSAGE_LENGTH, 0, (struct sockaddr *) &si_recv,
(socklen_t *)&socketaddr_len)) == -1)
{
#ifdef debugging
zlog_info(category_debug, "No data received.");
#endif
sleep_t(RECEIVE_THREAD_IDLE_SLEEP_TIME);
}
else if(recv_len > 0)
{
memset(address_ntoa, 0, sizeof(address_ntoa));
address_ntoa_ptr = inet_ntoa(si_recv.sin_addr);
memcpy(address_ntoa, address_ntoa_ptr, strlen(address_ntoa_ptr) *
sizeof(char));
port = ntohs(si_recv.sin_port);
#ifdef debugging
/* print details of the client/peer and the data received */
printf("Received packet from %s:%d\n", address_ntoa, port);
printf("Data: [");
print_content(recv_buf, recv_len);
printf("]\n");
printf("Data Length %d\n", recv_len);
#endif
addpkt(&udp_config -> Received_Queue, address_ntoa, port,
recv_buf, recv_len);
}
#ifdef debugging
else
zlog_info(category_debug, "else recvfrom error.");
#endif
}
#ifdef debugging
zlog_info(category_debug, "Exit Receive.");
#endif
return (void *)NULL;
}
int udp_release(pudp_config udp_config)
{
udp_config -> shutdown = true;
#ifdef _WIN32
closesocket(udp_config -> send_socket);
closesocket(udp_config -> recv_socket);
WSACleanup();
#else
close(udp_config -> send_socket);
close(udp_config -> recv_socket);
#endif
Free_Packet_Queue( &udp_config -> pkt_Queue);
Free_Packet_Queue( &udp_config -> Received_Queue);
return 0;
}