-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathmain.c
1456 lines (1256 loc) · 38.3 KB
/
main.c
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
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* @@@LICENSE
*
* Copyright (c) 2008-2012 Hewlett-Packard Development Company, L.P.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* LICENSE@@@ */
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <signal.h>
#include <getopt.h>
#include <debug.h>
#include <unistd.h>
#include <stdint.h>
#include <platform.h>
#include <novacom.h>
#include "novacom/mux.h"
#include <transport.h>
#include <transport_inet.h>
#include <errno.h>
#include "novacomd_p.h"
#include <log.h>
#if HOST
#include "host/novacom_host.h"
#include "lib/cksum.h"
#endif
#include <termios.h>
#include <sys/socket.h>
#include <sys/select.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/queue.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <fcntl.h>
#include <syslog.h>
int g_listen_all;
int g_recovery_timeout = TRANSPORT_RECOVERY_TIMEOUT;
int g_usbio_retry_timeout = TRANSPORT_USBIO_RETRY_TIMEOUT;
int g_usbio_retry_delay = 50; //RETRANSMIT_WAIT_PERIOD=50ms
int g_cpuaffinity = 0;
#if !defined(W32)
int g_daemonize = 0;
#endif
#if HOST
#define LOCAL_TRACE 0
#define LOCAL_TRACE_PROCESSCMD 0
#elif DEVICE
#define LOCAL_TRACE 0
#define LOCAL_TRACE_PROCESSCMD 0
#endif
/* upper and lower tx queue thresholds */
#define TX_QUEUE_UPPER (64*1024)
#define TX_QUEUE_LOWER (32*1024)
typedef struct socketchan {
struct socketchan *next;
int close;
SOCKET socket;
size_t partial_pos;
uint32_t channel;
device_handle_t device_handle;
bool tx_squelch;
} socketchan_t;
static struct socketchan *socketchan_list;
#ifdef HOST
typedef struct socketcmd_s {
int active; /* being processed */
uint32_t cmd_id; /* just a number, so we can track in logs */
uint32_t channel; /* channel number (do we really need it) */
int close; /* closing flag */
SOCKET socket; /* client socket */
char *sockreply; /* reply to socket client */
size_t partial_pos; /* partial pos (output to client) */
struct novacom_command_url *url; /* command */
device_handle_t device_handle; /* device handle */
TAILQ_ENTRY(socketcmd_s) entries; /* holds pointers to prev, next entries */
} socketcmd_t;
#endif
typedef struct active_device {
struct active_device *next;
struct active_device *listen_next;
bool closing;
bool inactive; /* true: cert auth is taking place(should not be populated to clients) */
int ref;
SOCKET socket;
int socket_port;
char *devid;
char *conntype;
char *devtype;
char *sessionid;
device_handle_t device_handle;
#ifdef HOST
uint32_t cmd_serviced; /* number of processed service control commands */
socketchan_t *cmd_chan; /* service command channel */
TAILQ_HEAD(socketcmd_queue_s, socketcmd_s) cmd_queue;
#endif
} active_device_t;
static SOCKET opt_sock = INVALID_SOCKET;
static SOCKET device_list_socket = INVALID_SOCKET;
static SOCKET log_socket = INVALID_SOCKET;
static SOCKET ctrl_socket = INVALID_SOCKET;
static SOCKET unblock_fd = INVALID_SOCKET;
static SOCKET unblock_fd_write = INVALID_SOCKET;
static SOCKET unblock_fd_read = INVALID_SOCKET;
static platform_mutex_t device_list_mutex;
static struct active_device *device_list;
// local functions
static void add_device_ref(struct active_device *dev);
static void remove_device_ref(struct active_device *dev);
static int socketchan_read_callback(device_handle_t device_handle, uint32_t channel, int error, const void *buf, size_t len, void *cookie);
static void destroy_socketchan(socketchan_t *sc);
static int novacom_register_client(active_device_t *dev, SOCKET newsocket);
static int novacom_process_cmdqueue(active_device_t *dev);
int handleopt(const char *opt, const char *val);
int getnextopt(int argc, char **argv, const char **opt, const char **val);
#if HOST
void *novacom_deviceinit_thread(void *arg);
static int novacom_register_command(SOCKET cmdsocket, const char *_cmd);
static int socketcmd_read_callback(device_handle_t device_handle, uint32_t channel, int err, const void *buf, size_t len, void *cookie);
static void destroy_socketcmd(socketcmd_t *sc);
#endif
void usage(void)
{
TRACEL(LOG_ALWAYS, "\n");
TRACEL(LOG_ALWAYS, "Usage: novacomd [option]...\n");
TRACEL(LOG_ALWAYS, "options:\n");
TRACEL(LOG_ALWAYS, " -b bind ipaddress for remote access, default is 127.0.0.1 \n");
TRACEL(LOG_ALWAYS, " -t <seconds> set recovery timeout interval, valid range 2 ~ 5, default is 5\n");
TRACEL(LOG_ALWAYS, " -c <host>:<port> connect to specified IP address\n");
TRACEL(LOG_ALWAYS, " -e <io-retry-timeout> timeout to retry packet I/O in milliesecond\n");
TRACEL(LOG_ALWAYS, " -s <io-retry-delay> delay to retry packet I/O in milliesecond\n");
TRACEL(LOG_ALWAYS, " -d run in the background\n");
TRACEL(LOG_ALWAYS, " -V print version info\n");
TRACEL(LOG_ALWAYS, " -h display this help\n");
}
/*
* unblock main_loop
*/
static void unblock_main_loop(void)
{
if (unblock_fd_write == INVALID_SOCKET ) {
struct sockaddr_in saddr;
memset(&saddr,0,sizeof(saddr));
saddr.sin_family = AF_INET;
saddr.sin_port = htons(NOVACOM_UNBLOCKPORT);
saddr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
unblock_fd_write = socket(AF_INET, SOCK_STREAM, 0);
if (unblock_fd_write == INVALID_SOCKET) {
TRACEL(LOG_ERROR, "unable to create socket\n");
return;
}
if ( connect(unblock_fd_write, (struct sockaddr *)&saddr, sizeof(saddr)) < 0) {
close(unblock_fd_write);
unblock_fd_write = INVALID_SOCKET;
TRACEL(LOG_ERROR, "unable to connect\n");
return;
}
}
char c = 0;
if (send(unblock_fd_write, &c, 1, 0) < 0) {
close(unblock_fd_write);
unblock_fd_write = INVALID_SOCKET;
}
}
static int socketchan_read_callback(device_handle_t device_handle, uint32_t channel, int err, const void *buf, size_t len, void *cookie)
{
struct socketchan *sc = (struct socketchan *)cookie;
int written;
/* channel closed: ignore cb */
if (err < 0) {
TRACEL(LOG_TRACE, "novacom channel %d closed\n", channel);
sc->close = 1;
sc->channel = -1;
unblock_main_loop();
return 0;
}
/* channel closing: ignore cb */
if (sc->close) {
TRACEL(LOG_TRACE, "novacom channel %d closing\n", channel);
unblock_main_loop();
return 0;
}
written = send(sc->socket, (char *)buf + sc->partial_pos, len - sc->partial_pos, 0);
if ((written == 0)|| ((written == -1) && (errno != EAGAIN) && (errno != 0))) {
TRACEL(LOG_TRACE, "novacom channel %d error: %d %d %d %d\n", channel, written, errno, len, sc->partial_pos);
sc->close = 1;
unblock_main_loop();
}
if (written <= 0)
return -1;
sc->partial_pos += written;
if (sc->partial_pos == len) {
sc->partial_pos = 0;
return 0;
} else {
// Couldn't make a complete write, wait for a retransmit
return -1;
}
return 0;
}
static void socketchan_write_callback(device_handle_t device_handle, uint32_t channel, int err, void *cookie)
{
unblock_main_loop();
}
/*
* @brief destroys socketchan_t
*/
static void destroy_socketchan(socketchan_t *sc)
{
/* unregister read callback */
if (sc->channel > 0) {
novacom_set_read_callback(sc->device_handle, sc->channel, NULL, NULL);
novacom_close_channel(sc->device_handle, sc->channel);
}
TRACEF("socketchan::destroy: %p, socket %d, novacom %d, device %p\n", sc, sc->socket, sc->channel, sc->device_handle);
/* inform client */
if (sc->socket != INVALID_SOCKET) {
shutdown(sc->socket, SHUT_RDWR);
close(sc->socket);
sc->socket = INVALID_SOCKET;
}
novacom_release_device_handle(sc->device_handle);
platform_free(sc);
}
void dump_device_list(SOCKET socket)
{
char buf[256];
struct active_device *dev;
// sprintf(buf, "6969 AFDE123400004321DEADBEEFC001CAFE00000000 usb\n");
// write(socket, buf, strlen(buf));
platform_mutex_lock(&device_list_mutex);
for (dev = device_list; dev; dev = dev->next) {
// skip devices that are shutting down
if (dev->closing) continue;
if (!dev->sessionid) {
snprintf(buf, sizeof(buf), "%d %s %s %s\n", dev->socket_port, dev->devid, dev->conntype, dev->devtype);
} else {
snprintf(buf, sizeof(buf), "%d %s %s %s %s\n", dev->socket_port,
dev->devid, dev->conntype, dev->devtype, dev->sessionid);
}
send(socket, buf, strlen(buf), 0);
}
platform_mutex_unlock(&device_list_mutex);
}
/* check device list for duplicate entries */
void novacom_removedup_device(device_handle_t device_handle, novacom_device_reginfo_t *t_drinfo)
{
struct active_device *dev = NULL;
/* issue occurs only on tcp connection due bug in VirtualBox: LTP-624 */
if ( t_drinfo->conntype && (0 == strncasecmp(t_drinfo->conntype, "tcp", 3)) ) {
int len;
platform_mutex_lock(&device_list_mutex);
for (dev = device_list; dev; dev = dev->next) {
len = MIN(strlen(dev->devid), NOVACOM_NDUID_CHRLEN);
if ( t_drinfo->devid_string && (0 == strncasecmp(t_drinfo->devid_string, dev->devid, len) ) ) {
TRACEL(LOG_ALWAYS, "duplicate: '%s', port %d\n", dev->devid, dev->socket_port);
break;
}
}
platform_mutex_unlock(&device_list_mutex);
if(dev) {
novacom_unregister_device(dev->device_handle);
}
}
}
void novacom_register_device(device_handle_t device_handle, novacom_device_reginfo_t *t_dev_reginfo)
{
struct active_device *dev;
/* bogus device registration info */
if (!t_dev_reginfo)
return;
TRACEL(LOG_ALWAYS, "dev '%s' via %s type %s\n",
t_dev_reginfo->devid_string,
t_dev_reginfo->conntype,
t_dev_reginfo->devtype);
/*check duplicate entries(vitrual box bug:does not close sockets leaving dups) */
novacom_removedup_device(device_handle, t_dev_reginfo);
/* device record */
dev = (struct active_device*)platform_calloc(sizeof(struct active_device));
platform_assert(dev != NULL);
dev->device_handle = device_handle;
novacom_retain_device_handle(device_handle);
dev->devid = platform_strdup(t_dev_reginfo->devid_string);
dev->ref = 1;
dev->closing = false;
/* conntype */
if(t_dev_reginfo->conntype)
dev->conntype = platform_strdup(t_dev_reginfo->conntype);
/* devtype */
if(t_dev_reginfo->devtype)
dev->devtype = platform_strdup(t_dev_reginfo->devtype);
#if HOST
/* command queue */
TAILQ_INIT(&dev->cmd_queue);
/* session id */
if( (t_dev_reginfo->sessionid) && ( strlen(t_dev_reginfo->sessionid)) ) {
dev->sessionid = platform_strdup(t_dev_reginfo->sessionid);
/* add device reference */
add_device_ref(dev);
/* initialization thread */
dev->inactive = 1; /* inactive device:: should be skipped from device list */
platform_create_thread(NULL, &novacom_deviceinit_thread, dev);
}
#endif
dev->socket = create_listen_socket(0, g_listen_all);
if(dev->socket != INVALID_SOCKET) {
dev->socket_port = get_socket_port(dev->socket);
}
// TRACEF("socket %d, port %d\n", dev->socket, dev->socket_port);
platform_mutex_lock(&device_list_mutex);
dev->next = device_list;
device_list = dev;
platform_mutex_unlock(&device_list_mutex);
// TRACEF("handle %p\n", dev->device_handle);
}
int novacom_unregister_device(device_handle_t device_handle)
{
int err = -1;
struct active_device *dev;
struct active_device *last;
// TRACEF("handle %p\n", device_handle);
platform_mutex_lock(&device_list_mutex);
last = NULL;
for (dev = device_list; dev; last = dev, dev = dev->next) {
if (dev->device_handle == device_handle) {
TRACEL(LOG_ALWAYS, "removing id %s\n", dev->devid);
if (last != NULL) {
last->next = dev->next;
} else {
device_list = dev->next;
}
dev->next = NULL;
err = 0;
break;
}
}
if (dev) {
#if HOST
/* close command queue */
if ( !TAILQ_EMPTY(&dev->cmd_queue) ) {
socketcmd_t *item;
socketcmd_t *tmp_item;
/* mark active commands */
item = TAILQ_FIRST(&dev->cmd_queue);
item->close = 1;
/* remove commands not processed yet */
for (item = TAILQ_NEXT(item, entries); item != NULL; item = tmp_item) {
tmp_item = TAILQ_NEXT(item, entries);
/* Remove the item from queue. */
TAILQ_REMOVE(&dev->cmd_queue, item, entries);
/* release memory */
destroy_socketcmd( item );
}
}
/* close service channel */
if (dev->cmd_chan) {
//destroy_socketchan(dev->cmd_chan);
//remove_device_ref(dev);
}
#endif
/* device closing */
dev->closing = true;
remove_device_ref(dev);
}
platform_mutex_unlock(&device_list_mutex);
unblock_main_loop();
return err;
}
/*
* @brief: return remote nduid
*/
char *novacom_rnduid(device_handle_t device_handle)
{
char *rnduid = NULL;
struct active_device *dev;
platform_mutex_lock(&device_list_mutex);
for (dev = device_list; dev; dev = dev->next) {
if (dev->device_handle == device_handle) {
LTRACEF("found match: rnduid %s\n", dev->devid);
rnduid = platform_strdup(dev->devid);
break;
}
}
platform_mutex_unlock(&device_list_mutex);
return rnduid;
}
static void add_device_ref(struct active_device *dev)
{
platform_atomic_add(&dev->ref, 1);
//LTRACEF("dev->ref=%d(%s)\n", dev->ref, dev->devtype);
}
static void remove_device_ref(struct active_device *dev)
{
if (platform_atomic_add(&dev->ref, -1) == 1) {
// last ref, make sure it's not in the list
platform_assert(dev->next == NULL);
LTRACEF("removing last ref to dev '%s'\n", dev->devid);
novacom_release_device_handle(dev->device_handle);
close(dev->socket);
platform_free(dev->devid);
platform_free(dev->conntype);
platform_free(dev->devtype);
if(dev->sessionid)
platform_free(dev->sessionid);
platform_free(dev);
} else {
//LTRACEF("dev->ref=%d(%s)\n", dev->ref, dev->devtype);
}
}
/*
* @brief: handle accepting new client to novacomd
*/
static int novacom_register_client(active_device_t *dev, SOCKET newsocket)
{
int rc = -1;
socketchan_t *sc = (socketchan_t *)platform_calloc(sizeof(socketchan_t));
if(sc) {
sc->close = 0;
sc->partial_pos = 0;
sc->socket = newsocket;
sc->tx_squelch = false;
sc->next = socketchan_list;
novacom_retain_device_handle(dev->device_handle);
sc->device_handle = dev->device_handle;
socketchan_list = sc;
TRACEF("Opening temp channel, device %p\n", dev->device_handle);
rc = novacom_open_temp_channel(sc->device_handle, &sc->channel, 1);
if (rc >= 0) {
rc = novacom_set_read_callback(sc->device_handle, sc->channel, (novacom_read_callback)&socketchan_read_callback, sc);
if (rc < 0) {
/*
* error creating or setting the callback, which can happen as
* a result of a race with the device going away, abort
*/
sc->channel = -1;
sc->close = 1;
TRACEF("hit create channel/device going away race\n");
}
TRACEF("socketchan::created: %p, socket %d, novacom %d, device %p\n", sc, sc->socket, sc->channel, dev->device_handle);
} else {
TRACEF("Unable to create channel: rc(%d)\n", rc);
sc->channel = -1;
}
}
return rc;
}
static int main_loop(void)
{
fd_set fds;
struct socketchan *sc;
struct socketchan *last;
SOCKET highest_socket;
struct active_device *dev;
struct active_device *listen_devices;
char sdata[8*1024];
#define PICK_MAXSOCKET(insocket) if(insocket > highest_socket) highest_socket = insocket;
for (;;) {
highest_socket = INVALID_SOCKET;
FD_ZERO(&fds);
if(device_list_socket != INVALID_SOCKET) {
FD_SET(device_list_socket, &fds);
highest_socket = device_list_socket;
}
if(opt_sock != INVALID_SOCKET) {
FD_SET(opt_sock, &fds);
PICK_MAXSOCKET(opt_sock);
}
if(unblock_fd != INVALID_SOCKET) {
FD_SET(unblock_fd, &fds);
PICK_MAXSOCKET(unblock_fd);
}
if(log_socket != INVALID_SOCKET) {
FD_SET(log_socket, &fds);
PICK_MAXSOCKET(log_socket);
}
if (unblock_fd_read != INVALID_SOCKET) {
FD_SET(unblock_fd_read, &fds);
PICK_MAXSOCKET(unblock_fd_read);
}
if(ctrl_socket != INVALID_SOCKET) {
FD_SET(ctrl_socket, &fds);
PICK_MAXSOCKET(ctrl_socket);
}
/* add the transfer sockets to the list */
last = NULL;
sc = socketchan_list;
while (sc) {
if (sc->close) {
struct socketchan *temp;
/* socket has been closed, destroy the socketchan */
if (last) {
last->next = sc->next;
} else {
socketchan_list = sc->next;
}
temp = sc;
sc = sc->next;
destroy_socketchan(temp);
} else {
/* see if we should start listening to a previously squelched channel */
if (sc->tx_squelch) {
if (novacom_tx_queue_len(sc->device_handle, sc->channel) < TX_QUEUE_LOWER) {
sc->tx_squelch = false;
}
}
/* add it to the list of sockets to listen to if it isn't squelched */
if (!sc->tx_squelch) {
FD_SET(sc->socket, &fds);
PICK_MAXSOCKET(sc->socket);
// TRACEF("queuing socket %d\n", sc->socket);
}
last = sc;
sc = sc->next;
}
}
/* add the listen sockets to the list */
listen_devices = NULL;
platform_mutex_lock(&device_list_mutex);
for (dev = device_list; dev; dev = dev->next) {
FD_SET(dev->socket, &fds);
PICK_MAXSOCKET(dev->socket);
/*
* add this device to a temporary list of devices
* we're listening for on this pass of select
*/
dev->listen_next = listen_devices;
listen_devices = dev;
add_device_ref(dev);
}
platform_mutex_unlock(&device_list_mutex);
/* wait for input */
if(select(highest_socket + 1, &fds, NULL, NULL, NULL) <= 0) {
/* remove device references */
dev = listen_devices;
platform_mutex_lock(&device_list_mutex);
while (dev) {
struct active_device *temp_dev = dev;
dev = dev->listen_next;
remove_device_ref(temp_dev);
}
platform_mutex_unlock(&device_list_mutex);
/* restart loop */
continue;
}
/* see if our pipe has any data. used to unblock the select loop. */
if (FD_ISSET(unblock_fd, &fds)) {
unblock_fd_read = accept_socket(unblock_fd);
}
if( (unblock_fd_read != INVALID_SOCKET) && (FD_ISSET(unblock_fd_read, &fds)) ) {
char c;
if( recv(unblock_fd_read, &c, 1, 0) < 0) {
close(unblock_fd_read);
unblock_fd_read = INVALID_SOCKET;
}
}
if (FD_ISSET(opt_sock, &fds)) {
SOCKET newsocket = accept(opt_sock, NULL, NULL);
if (newsocket != INVALID_SOCKET) {
uint32_t lengths[2] = {0,0};
char *buf;
int rc;
int sock_opt = 4096;
setsockopt(newsocket, SOL_SOCKET, SO_RCVLOWAT, (char *)&sock_opt, sizeof(sock_opt));
// only one option pair per connection
rc = recv(newsocket, (char*)lengths, sizeof(lengths), 0);
if(rc == sizeof(lengths)) {
// 4k options should be enough for anyone
if ((lengths[0] != 0) && ((lengths[0] + lengths[1]) < 4096)) {
size_t l = lengths[0] + lengths[1] + 1;
buf = (char*)platform_calloc(l);
if(buf) {
rc = recv(newsocket, buf, lengths[0] + lengths[1], 0);
if(rc > 0) {
buf[lengths[0] + lengths[1]] = '\0';
handleopt(buf, &buf[lengths[0]]);
}
platform_free(buf);
} else {
TRACEF("unable to allocate memory to accept options, ignore...\n");
}
}
}
close(newsocket);
}
}
/* process device list requests */
//ychen: typo: if ( (log_socket != INVALID_SOCKET) && (FD_ISSET(device_list_socket, &fds)) ) {
if ( (device_list_socket != INVALID_SOCKET) && (FD_ISSET(device_list_socket, &fds)) ) {
SOCKET newsocket = accept_socket(device_list_socket);
if(newsocket != INVALID_SOCKET) {
dump_device_list(newsocket);
close(newsocket);
}
}
/* process logging request */
if ( (log_socket != INVALID_SOCKET) && (FD_ISSET(log_socket, &fds)) ) {
SOCKET newsocket = accept_socket(log_socket);
if(newsocket != INVALID_SOCKET) {
log_add_socket(newsocket);
}
}
/* process control requests */
if ( (ctrl_socket != INVALID_SOCKET) && (FD_ISSET(ctrl_socket, &fds)) ) {
SOCKET newsocket = accept_socket(ctrl_socket);
if ( newsocket != INVALID_SOCKET ) {
#if HOST
int rc = novacom_register_command(newsocket, NULL);
/* -1, 1: command is done */
if (rc)
close(newsocket);
#else
close(newsocket);
#endif
}
}
/* see if any of our device listener channels opened */
dev = listen_devices;
while (dev) {
struct active_device *temp_dev;
/* device */
if (FD_ISSET(dev->socket, &fds) ) {
SOCKET newsocket = accept_socket(dev->socket);
TRACEL(LOG_TRACE, "data socket %d\n", newsocket);
if(newsocket != INVALID_SOCKET) {
// Set non-blocking or novacomd will drop offline if the socket fills up
fcntl(newsocket, F_SETFL, fcntl(newsocket, F_GETFL) | O_NONBLOCK);
novacom_register_client(dev, newsocket);
}
}
/* we're done with this device this time around, remove its ref */
temp_dev = dev;
dev = dev->listen_next;
platform_mutex_lock(&device_list_mutex);
remove_device_ref(temp_dev);
platform_mutex_unlock(&device_list_mutex);
}
/* process any pending data on the data sockets */
last = NULL;
for (sc = socketchan_list; sc; last = sc, sc = sc->next) {
if (sc->close)
continue; // skip it
if (FD_ISSET(sc->socket, &fds)) {
// got some data on this socket
int len;
FD_CLR(sc->socket, &fds);
len = recv_socket(sc->socket, sdata, sizeof(sdata), 0);
if (len <= 0) {
// error, socketchan will get cleaned up in the next cycle around the loop
TRACEF(/*log_printf(LOG_ERROR,*/ "%s:%d: recv: errno=%d, ret=%d, sc->socket=%d\n", __FILE__, __LINE__, platform_socket_getlasterrno(), len, sc->socket);
sc->close = 1;
}
if (len > 0) {
size_t queuelen;
//print cmds
if (strncmp(sdata, "run",3)==0 || strncmp(sdata, "open", 4)==0 || strncmp(sdata, "get", 3)==0 || strncmp(sdata, "put", 3)==0 || strncmp(sdata, "connect", 7)==0 || strncmp(sdata, "boot", 4)==0) {
if (len < (int)sizeof(sdata)) {
sdata[len] = 0;
}
TRACEF(/*log_printf(LOG_ALWAYS, */"%s:%d: sock=%x, cmd/data recved: %s, len=%d\n",__FILE__, __LINE__, (int)sc->socket, sdata, len);
}
// it puts the data in the basket
novacom_write_channel_async(sc->device_handle, sc->channel, sdata, len, ASYNC_FLAG_COPY, (novacom_async_callback)&socketchan_write_callback, (void *)sc);
queuelen = novacom_tx_queue_len(sc->device_handle, sc->channel);
if (queuelen > TX_QUEUE_UPPER) {
// TRACEF("queue len %zd\n", novacom_tx_queue_len(sc->channel));
sc->tx_squelch = true;
}
}
}
}
}
return 0;
}
// commandline parsing stuff below
static void novacomopt(int argc, char **argv)
{
SOCKET optsock;
struct sockaddr_in saddr;
memset(&saddr,0,sizeof(saddr));
saddr.sin_family = AF_INET;
saddr.sin_port = htons(NOVACOM_OPTPORT);
saddr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
/* send options to existing process */
optsock = socket(AF_INET, SOCK_STREAM, 0);
if(optsock != INVALID_SOCKET) {
if (connect(optsock, (struct sockaddr *)&saddr, sizeof(saddr)) == 0) {
#if 0
//we may change this potion of code in future.
const char *opt = NULL;
const char *val = NULL;
uint32_t nemo[2] = {0, 0};
TRACEL(LOG_ALWAYS, "sending options to existing novacomd process\n");
while (getnextopt(argc, argv, &opt, &val) > 0) {
// include nulls
int valLen = 0;
if (val) valLen = strlen(val) + 1;
uint32_t lengths[2] = {strlen(opt) + 1, valLen};
send(optsock, (const char*)lengths, sizeof(lengths), 0);
send(optsock, opt, lengths[0], 0);
if(valLen) {
send(optsock, val, lengths[1], 0);
}
close(optsock);
optsock = socket(AF_INET, SOCK_STREAM, 0);
if(optsock != INVALID_SOCKET) {
if (connect(optsock, (struct sockaddr *)&saddr, sizeof(saddr)) != 0) {
TRACEL(LOG_ERROR, "Error connecting to existing novacomd\n");
exit(1);
}
} else {
break;
}
}
if(optsock != INVALID_SOCKET) {
send(optsock, (const char*)nemo, sizeof(nemo), 0);
close(optsock);
}
#endif
TRACEL(LOG_ALWAYS, "Cannot start novacomd twice!\n");
exit(0);
} else {
close(optsock);
}
} else {
TRACEL(LOG_ERROR, "unable to create socket\n");
}
}
int handleopt(const char *opt, const char *val)
{
//TRACEL(LOG_ALWAYS, "Got option %s with value %s\n", opt, val?val:"(null)");
if( val && (0 == strcmp("connect-ip", opt) ) ) {
char *addr = platform_strdup(val);
char *port;
if ((port = strpbrk(addr, ": ")) != NULL) {
*port = '\0';
port++;
}
inetconnect_connect_to(addr, port);
platform_free(addr);
} else if (strcmp("version", opt) == 0) {
TRACEL(LOG_ALWAYS, "novacomd version %s\n", BUILDVERSION);
exit(1);
} else if (strcmp("bind-all-interfaces", opt) == 0) {
g_listen_all = 1;
} else if (strcmp("daemonize", opt) == 0) {
g_daemonize = 1;
} else if (strcmp("recoverytimeout", opt) == 0) {
g_recovery_timeout = atol(val);
if (g_recovery_timeout < 2 || g_recovery_timeout > 5) {
//timeout out of range
g_recovery_timeout = TRANSPORT_RECOVERY_TIMEOUT;
TRACEL(LOG_ERROR, "Invalid timeout out of range(2~5 seconds): %s \n", val);
return -1;
}
} else if (strcmp("ioretry_timeout", opt) == 0) {
g_usbio_retry_timeout = atol(val);
if (g_usbio_retry_timeout > TRANSPORT_MAX_USBIO_RETRY_TIMEOUT) {
g_usbio_retry_timeout = TRANSPORT_MAX_USBIO_RETRY_TIMEOUT;
}
} else if (strcmp("ioretry_delay", opt) == 0) {
g_usbio_retry_delay = atol(val);
if (g_usbio_retry_delay > TRANSPORT_MAX_USBIO_RETRY_TIMEOUT) {
g_usbio_retry_delay = TRANSPORT_MAX_USBIO_RETRY_TIMEOUT;
}
} else if (strcmp("no-cpuaffinity", opt) == 0) {
g_cpuaffinity = 0;
} else if (strcmp("help", opt) == 0) {
usage();
exit(1);
} else {
/* bad option */
return -1;
}
return 0;
}
struct option longopts[] = {
{"connect-ip", required_argument, 0, 'c'},
{"bind-all-interfaces", no_argument, 0, 'b'},
{"daemonize", no_argument, 0, 'd'},
{"version", no_argument, 0, 'V'},
{0,0,0,0}
};
int getnextopt(int argc, char **argv, const char **opt, const char **val)
{
#if !defined(W32)
int c = getopt_long(argc, argv, "ht:c:e:s:bdCV", longopts, NULL);
#else
int c = getopt_long(argc, argv, "ht:c:e:s:bCV", longopts, NULL);
#endif
*val = optarg;
switch (c) {
case 'c':
*opt = "connect-ip";
return 1;
case 'V':
*opt = "version";
return 1;
case 'b':
*opt = "bind-all-interfaces";
return 1;
case 'd':
*opt = "daemonize";
return 1;
case 't':
*opt="recoverytimeout";
return 1;
case 'e':
*opt = "ioretry_timeout";
return 1;
case 's':
*opt = "ioretry_delay";
return 1;
case 'C':
*opt = "no-cpuaffinity";
return 1;
case 'h':
*opt = "help";
return 1;
/* done with args */
case -1:
return 0;
/* something we don't recognize */
default:
return -1;
}
}
int parse_commandline (int argc, char **argv)
{
const char *opt, *val;
int err;
while ((err = getnextopt(argc, argv, &opt, &val)) > 0) {
if (handleopt(opt, val) < 0)
return -1;
}
if (g_usbio_retry_timeout < g_usbio_retry_delay) {
g_usbio_retry_delay = g_usbio_retry_timeout;
}
return err;
}
int main(int argc, char **argv)
{
int err;
signal(SIGPIPE, SIG_IGN);
platform_init();
#if HOST
openlog("novacomd(" BUILDVERSION ")", 0, LOG_LOCAL7);
#endif //HOST
log_init();
//TRACEL(LOG_ALWAYS, "novacomd version %s starting...\n", BUILDVERSION);
//initialize nduid
err = novacom_init();
if (err < 0)
return 1;
if (parse_commandline(argc, argv) < 0) {
TRACEL(LOG_ERROR, "Invalid command line option or parameter\n");
log_mask(LOG_OUTPUT_CONSOLE, 1);
usage();
exit(1);
}