-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsocket.c
More file actions
547 lines (531 loc) · 17.3 KB
/
Copy pathsocket.c
File metadata and controls
547 lines (531 loc) · 17.3 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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
/*
* geany_encoding=koi8-r
* socket.c - socket IO (both client & server)
*
* Copyright 2017 Edward V. Emelianov <eddy@sao.ru, edward.emelianoff@gmail.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*
*/
#if defined CLIENT || defined DAEMON
#include "socket.h"
#include "term.h"
#include "usefull_macros.h"
#include <arpa/inet.h> // inet_ntop
#include <limits.h> // INT_xxx
#include <netdb.h> // addrinfo
#include <pthread.h>
#include <signal.h> // pthread_kill
#include <sys/prctl.h> //prctl
#include <sys/wait.h> // wait
#include <unistd.h> // daemon
#define BUFLEN (10240)
#define BUFLEN10 (1048576)
// Max amount of connections
#define BACKLOG (30)
/**************** COMMON FUNCTIONS ****************/
/**
* wait for answer from socket
* @param sock - socket fd
* @return 0 if data is absent, 1 in case of socket ready, -1 if socket closed or error occured
*/
static int waittoread(int sock){
fd_set fds, efds;
struct timeval timeout;
int rc;
timeout.tv_sec = 1; // wait not more than 1 second
timeout.tv_usec = 0;
FD_ZERO(&fds);
FD_ZERO(&efds);
FD_SET(sock, &fds);
FD_SET(sock, &efds);
do{
rc = select(sock+1, &fds, NULL, &efds, &timeout);
if(rc < 0){
if(errno != EINTR){
putlog("Server not available");
WARN("select()");
return -1;
}
continue;
}
break;
}while(1);
if(FD_ISSET(sock, &efds)) return -1; // exception - socket closed
if(FD_ISSET(sock, &fds)) return 1;
return 0;
}
static uint8_t *findpar(uint8_t *str, char *par){
size_t L = strlen(par);
char *f = strstr((char*)str, par);
if(!f) return NULL;
f += L;
if(*f != '=') return NULL;
return (uint8_t*)(f + 1);
}
/**
* get integer & double `parameter` value from string `str`, put value to `ret`
* @return 1 if all OK
*/
static int getintpar(uint8_t *str, char *parameter, long *ret){
long tmp;
char *endptr;
if(!(str = findpar(str, parameter))) return 0;
tmp = strtol((char*)str, &endptr, 0);
if(endptr == (char*)str || *str == 0 )
return 0;
if(ret) *ret = tmp;
DBG("get par: %s = %ld", parameter, tmp);
return 1;
}
#ifdef CLIENT
static int getdpar(uint8_t *str, char *parameter, double *ret){
double tmp;
char *endptr;
if(!(str = findpar(str, parameter))) return 0;
tmp = strtod((char*)str, &endptr);
if(endptr == (char*)str || *str == 0)
return 0;
if(ret) *ret = tmp;
DBG("get par: %s = %g", parameter, tmp);
return 1;
}
#endif // CLIENT
/**************** CLIENT/SERVER FUNCTIONS ****************/
#ifdef DAEMON
static double min_dark_exp, dark_interval;
static imstorage *storedima = NULL;
static uint64_t imctr = 0; // image counter
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
// setter for min_dark_exp, dark_interval
void set_darks(double exp, double dt){
min_dark_exp = exp;
dark_interval = dt;
}
static void clearimstorage(){
if(!storedima) return;
FREE(storedima->imname);
FREE(storedima->subframe);
FREE(storedima->imdata);
FREE(storedima);
}
static imstorage *copyima(imstorage *im){
FNAME();
clearimstorage();
if(!im || !im->imdata) return NULL;
storedima = MALLOC(imstorage, 1);
#define CLR() do{clearimstorage(); return NULL;}while(0)
if(!memcpy(storedima, im, sizeof(imstorage))) CLR();
if(im->imname){
if(!(storedima->imname = strdup(im->imname))) CLR();
}
if(im->subframe){
storedima->subframe = MALLOC(imsubframe, 1);
if(!memcpy(storedima->subframe, im->subframe, sizeof(imsubframe))) CLR();
}
if(im->imdata){
size_t S = im->W*im->H*sizeof(uint16_t);
if(!(storedima->imdata = malloc(S))) CLR();
if(!memcpy(storedima->imdata, im->imdata, S)) CLR();
}else{
WARNX("Where is imdata?");
CLR();
}
#undef CLR
DBG("ALL ok");
return storedima;
}
static int addwebhdr(char *buf, size_t buflen, char *conttype, size_t contlen){
return snprintf(buf, buflen,
"HTTP/2.0 200 OK\r\n"
"Access-Control-Allow-Origin: *\r\n"
"Access-Control-Allow-Methods: GET, POST\r\n"
"Access-Control-Allow-Credentials: true\r\n"
"Content-type: %s\r\nContent-Length: %zd\r\n\r\n", conttype, contlen);
}
int send_ima(int sock, int webquery){
uint8_t buf[BUFLEN10], *bptr = buf, obuff[BUFLEN];
int Len, rest = BUFLEN10, imS = storedima->W * storedima->H * sizeof(uint16_t);
#define PUT(key, val) do{Len = snprintf((char*)bptr, rest, "%s=%i\n", key, (int)storedima->val); \
if(Len > 0){rest -= Len; bptr += Len;}}while(0)
PUT("binning", binning);
if(storedima->binning == 0xff){
PUT("subX", subframe->Xstart);
PUT("subY", subframe->Xstart);
PUT("subS", subframe->size);
}
Len = snprintf((char*)bptr, rest, "%s=%g\n", "exptime", storedima->exptime);
if(Len > 0){rest -= Len; bptr += Len;}
PUT("imtype", imtype);
PUT("imW", W);
PUT("imH", H);
PUT("exposetime", exposetime);
Len = snprintf((char*)bptr, rest, "imdata=");
if(Len){rest -= Len; bptr += Len;}
if(rest < imS){
red("rest = %d, need %d\n", rest, imS);
return 0; // not enough memory - HOW???
}
if(!memcpy(bptr, storedima->imdata, imS)){
WARN("memcpy()");
return 0;
}
rest -= imS;
// send data
int send = BUFLEN10 - rest;
// OK buffer ready, prepare to send it
if(webquery){
Len = addwebhdr((char*)obuff, BUFLEN, "multipart/form-data", send);
if(Len < 0){
WARN("sprintf()");
return 0;
}
if(Len != write(sock, obuff, Len)){
WARN("write");
return 0;
}
DBG("%s", obuff);
}
red("send %d bytes\n", send);
if(send != write(sock, buf, send)){
WARN("write()");
return 0;
}
putlog("image sent to client");
return 1;
}
// search a first word after needle without spaces
char* stringscan(char *str, char *needle){
char *a, *e;
char *end = str + strlen(str);
a = strstr(str, needle);
if(!a) return NULL;
a += strlen(needle);
while (a < end && (*a == ' ' || *a == '\r' || *a == '\t' || *a == '\r')) a++;
if(a >= end) return NULL;
e = strchr(a, ' ');
if(e) *e = 0;
return a;
}
void *handle_socket(void *asock){
FNAME();
uint64_t locctr = 0;
int sock = *((int*)asock);
int webquery = 0; // whether query is web or regular
char buff[BUFLEN];
ssize_t _read;
while(1){
int rd = waittoread(sock);
if(rd < 0){
putlog("Disconnected");
break;
}
if(!rd){ // no data incoming
pthread_mutex_lock(&mutex);
if(imctr != locctr){
red("Send image, imctr = %ld, locctr = %ld\n", imctr, locctr);
if(send_ima(sock, webquery)){
locctr = imctr;
if(webquery){
pthread_mutex_unlock(&mutex);
break; // end of transmission
}
}
}
pthread_mutex_unlock(&mutex);
continue;
}
_read = read(sock, buff, BUFLEN);
if(_read < 1){ // error or disconnect
putlog("Client disconnected");
DBG("Nothing to read from fd %d (ret: %zd)", sock, _read);
break;
}
DBG("Got %zd bytes", _read);
// add trailing zero to be on the safe side
buff[_read] = 0;
// now we should check what do user want
char *got, *found = buff;
if((got = stringscan(buff, "GET")) || (got = stringscan(buff, "POST"))){ // web query
webquery = 1;
char *slash = strchr(got, '/');
if(slash) found = slash + 1;
// web query have format GET /some.resource
}
// here we can process user data
printf("user send: %s\n", found);
long htr;
// double dd;
//if(getdpar((uint8_t*)found, "exptime", &dd)) printf("exptime: %g\n", dd);
if(getintpar((uint8_t*)found, "heater", &htr)){
putlog("got command: heater=%ld", htr);
if(htr == 0) heater_off();
else heater_on();
int Len = addwebhdr(buff, BUFLEN, "text/html", 12);
if(Len > 0){
Len += snprintf(buff+Len, BUFLEN, "HEATER %s\r\n", htr ? "ON " : "OFF");
write(sock, buff, Len);
}
break; // disconnect after command receiving
}
}
close(sock);
//DBG("closed");
pthread_exit(NULL);
return NULL;
}
void *server(void *asock){
int sock = *((int*)asock);
if(listen(sock, BACKLOG) == -1){
WARN("listen");
return NULL;
}
while(1){
socklen_t size = sizeof(struct sockaddr_in);
struct sockaddr_in their_addr;
int newsock;
int rd = waittoread(sock);
if(rd < 0){
putlog("Socket error");
return NULL;
}
if(!rd) continue;
red("Got connection\n");
newsock = accept(sock, (struct sockaddr*)&their_addr, &size);
if(newsock <= 0){
WARN("accept()");
continue;
}
putlog("Got connection from %s\n", inet_ntoa(their_addr.sin_addr));
pthread_t handler_thread;
if(pthread_create(&handler_thread, NULL, handle_socket, (void*) &newsock))
WARN("pthread_create()");
else{
DBG("Thread created, detouch");
pthread_detach(handler_thread); // don't care about thread state
}
}
}
static void daemon_(imstorage *img, int sock){
FNAME();
if(sock < 0) return;
pthread_t sock_thread;
static double lastDT = 0.; // last time dark was taken
if(pthread_create(&sock_thread, NULL, server, (void*) &sock))
ERR("pthread_create()");
int errcntr = 0;
do{
if(pthread_kill(sock_thread, 0) == ESRCH){ // died
WARNX("Sockets thread died");
putlog("Sockets thread died");
pthread_join(sock_thread, NULL);
if(pthread_create(&sock_thread, NULL, server, (void*) &sock))
ERR("pthread_create()");
}
if(exp_calculated > 0.) img->exptime = exp_calculated;
if(img->imtype != IMTYPE_AUTODARK){ // check for darks
if(img->imtype == IMTYPE_DARK){
putlog("First light frame after dark");
img->imtype = IMTYPE_LIGHT; // last was dark
}
else if(img->exptime > min_dark_exp){ // need to store dark frame?
putlog("exptime > %g; dtime()=%.1f, lastDT = %.1f", min_dark_exp, dtime(), lastDT);
if(dtime() - lastDT > dark_interval){
putlog("Take dark image");
lastDT = dtime();
img->imtype = IMTYPE_DARK;
}
}
}
if(start_exposition(img, NULL)){
putlog("Error starting exposition, try later");
WARNX(_("Error starting exposition, try later"));
++errcntr;
}else{
FREE(img->imdata);
if(!get_imdata(img)){
++errcntr;
putlog("Error image transfer");
WARNX(_("Error image transfer"));
}else{
errcntr = 0;
pthread_mutex_lock(&mutex);
if(copyima(img)){
++imctr;
if(img->imtype != IMTYPE_DARK)
save_histo(NULL, img); // calculate next optimal exposition
}
pthread_mutex_unlock(&mutex);
}
}
if(errcntr >= 33){
putlog("Unrecoverable error, errcntr=%d. Exit", errcntr);
ERRX(_("Unrecoverable error"));
}
}while(1);
}
#endif
#ifdef CLIENT
static imstorage *get_imstorage(imstorage *img, uint8_t *buf, size_t L){
imsubframe F;
long i; double d;
img->subframe = NULL;
if(getintpar(buf, "binning", &i)){
img->binning = i;
DBG("bin: %d", img->binning);
if(i == 0xff){ // subframe
if(getintpar(buf, "subX", &i)) F.Xstart = i;
if(getintpar(buf, "subY", &i)) F.Ystart = i;
if(getintpar(buf, "subS", &i)) F.size = i;
img->subframe = &F;
}
}
if(getdpar(buf, "exptime", &d)) img->exptime = d;
if(getintpar(buf, "imtype", &i)) img->imtype = i;
if(getintpar(buf, "imW", &i)) img->W = i;
if(getintpar(buf, "imH", &i)) img->H = i;
if(getintpar(buf, "exposetime", &i)) img->exposetime = i;
uint8_t *par = findpar(buf, "imdata");
if(par){
img->imdata = (uint16_t*)par;
size_t datasz = img->W * img->H * sizeof(uint16_t);
if(datasz > L - (par - buf)) return NULL; // buffer too small for given image
DBG("1st pix: %u; W=%zd, H=%zd", *img->imdata, img->W, img->H);
}else return NULL;
return img;
}
static void client_(imstorage *img, int sock){
FNAME();
if(sock < 0) return;
size_t Bufsiz = BUFLEN10;
uint8_t *recvBuff = MALLOC(uint8_t, Bufsiz);
time_t wd_time = time(NULL); // watchdog time
time_t wd_period = get_wd_period(); // watchdog period: about 3minutes + 2*max_exptime
while(1){
// check watchdog
if(time(NULL) - wd_time > wd_period){
putlog("Watchdog triggered, exit!");
return;
}
int rd = waittoread(sock);
if(rd < 0){
putlog("Server disconnected");
break;
}
if(!rd) continue;
size_t offset = 0;
do{
if(offset >= Bufsiz){
Bufsiz += 1024;
recvBuff = realloc(recvBuff, Bufsiz);
if(!recvBuff){
WARN("realloc()");
return;
}
DBG("Buffer reallocated, new size: %zd\n", Bufsiz);
}
ssize_t n = read(sock, &recvBuff[offset], Bufsiz - offset);
if(!n){
putlog("Socket closed");
break;
}
if(n < 0){
putlog("error in read()");
WARN("read");
return;
}
offset += n;
}while(waittoread(sock));
if(!offset){
putlog("Socket closed");
WARN("Socket closed\n");
return;
}
DBG("read %zd bytes\n", offset);
wd_time = time(NULL); // refresh watchdog - socket OK
if(get_imstorage(img, recvBuff, offset)){
if(store_image(img)){
putlog("Error storing image");
WARNX(_("Error storing image"));
}else{
putlog("Image saved");
}
}
if(img->once) break;
}
}
#endif
/**
* Run daemon service
*/
void daemonize(imstorage *img, char *hostname, char *port){
FNAME();
int sock = -1;
struct addrinfo hints, *res, *p;
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_PASSIVE;
DBG("connect to %s:%s", hostname, port);
if(getaddrinfo(hostname, port, &hints, &res) != 0){
ERR("getaddrinfo");
}
struct sockaddr_in *ia = (struct sockaddr_in*)res->ai_addr;
char str[INET_ADDRSTRLEN];
inet_ntop(AF_INET, &(ia->sin_addr), str, INET_ADDRSTRLEN);
DBG("canonname: %s, port: %u, addr: %s\n", res->ai_canonname, ntohs(ia->sin_port), str);
putlog("connect to port %s", port);
// loop through all the results and bind to the first we can
for(p = res; p != NULL; p = p->ai_next){
if((sock = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1){
WARN("socket");
continue;
}
#ifdef DAEMON
int reuseaddr = 1;
if(setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &reuseaddr, sizeof(int)) == -1){
ERR("setsockopt");
}
if(bind(sock, p->ai_addr, p->ai_addrlen) == -1){
close(sock);
WARN("bind");
continue;
}
#else
if(connect(sock, p->ai_addr, p->ai_addrlen) == -1){
WARN("connect()");
close(sock);
continue;
}
#endif
break; // if we get here, we have a successfull connection
}
if(p == NULL){
// looped off the end of the list with no successful bind
putlog("failed to bind socket");
ERRX("failed to bind socket");
}
freeaddrinfo(res);
#ifdef CLIENT
client_(img, sock);
#else
daemon_(img, sock);
#endif
putlog("Close socket");
close(sock);
signals(0);
}
#endif // CLIENT || DAEMON