-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqnap-finder.c
636 lines (500 loc) · 17.3 KB
/
qnap-finder.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
/*
* qnap-finder
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <pthread.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <ifaddrs.h>
#include <limits.h>
#include "qnap-finder.h"
#include "list.h"
#ifdef DEBUG
#define D(x) printf(x)
#else
#define D(x)
#endif
#define BROADCAST_IP "255.255.255.255"
#define BROADCAST_PORT 8097
#define MAX_PACKET_SIZE 1000
#define HASH_TABLE_LEN 2000
#define HASH_KEY 997
#define FALSE 0
#define TRUE 1
#ifndef _POSIX_HOST_NAME_MAX
#define _POSIX_HOST_NAME_MAX 255
#endif
/* QNAP Response */
struct qnap_response_hdr {
uint64_t magic[2];
#define QNAP_MAGIC1 0x65536AECC39B0800
#define QNAP_MAGIC2 0x0425000000010001 /* basic */
#define QNAP_MAGIC3 0x0425000000050001 /* detail */
};
#define QNAP_PACKET_UNPACK(a) \
(void)memcpy((a), &buf[len], sizeof(a)), len += sizeof(a)
/* Globals */
int sendsock;
int recvsock;
LIST *response_list;
int verbose = FALSE;
int query_detail_info = FALSE;
struct sockaddr_in broadcast_addr;
int send_done = 0;
/* hastable */
in_addr_t add_tab[HASH_TABLE_LEN] = { 0 };
/* Offsets in bytes */
/* Endianness */
static union {
char s[4];
uint32_t u;
} endian_data;
/* short */
static uint16_t _data_to_le2(uint16_t in)
{
uint16_t out;
uint8_t *s = (uint8_t *)(void *)∈
uint8_t *d = (uint8_t *)(void *)&out;
if (verbose)
printf("-->_data_to_le2\n");
d[0] = s[1];
d[1] = s[0];
if (verbose)
printf("<--_data_to_le2\n");
return out;
}
/* int */
static uint32_t _data_to_le4(uint32_t in)
{
uint32_t out;
uint8_t *s = (uint8_t *)(void *)∈
uint8_t *d = (uint8_t *)(void *)&out;
if (verbose)
printf("-->_data_to_le4\n");
d[0] = s[3];
d[1] = s[2];
d[2] = s[1];
d[3] = s[0];
if (verbose)
printf("<--_data_to_le4\n");
return out;
}
/* long */
static uint64_t _data_to_le8(uint64_t in)
{
uint64_t out;
uint8_t *s = (uint8_t *)(void *)∈
uint8_t *d = (uint8_t *)(void *)&out;
if (verbose)
printf("-->_data_to_le8\n");
d[0] = s[7];
d[1] = s[6];
d[2] = s[5];
d[3] = s[4];
d[4] = s[3];
d[5] = s[2];
d[6] = s[1];
d[7] = s[0];
if (verbose)
printf("-->_data_to_le8\n");
return out;
}
#define SWAP_DATA (endian_data.u == (uint32_t)0x01020304)
#define DATA_TO_LE8(x) ((uint64_t)(SWAP_DATA ? _data_to_le8(x) : (uint64_t)(x)))
#define DATA_TO_LE4(x) ((uint32_t)(SWAP_DATA ? _data_to_le4(x) : (uint32_t)(x)))
#define DATA_TO_LE2(x) ((uint16_t)(SWAP_DATA ? _data_to_le2(x) : (uint16_t)(x)))
int get_and_stash_local_ip_addr(void)
{
struct ifaddrs *ifaddr, *ifa;
if (verbose)
printf("-->get_and_stash_local_ip_addr\n");
if (getifaddrs(&ifaddr) == -1) {
perror("getifaddrs");
return -1;
}
for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) {
if (ifa->ifa_addr == NULL)
continue;
if (ifa->ifa_addr->sa_family == AF_INET) {
int pos;
in_addr_t addr;
struct sockaddr_in *sin = (struct sockaddr_in *)ifa->ifa_addr;
addr = sin->sin_addr.s_addr;
pos = addr % HASH_KEY;
add_tab[pos] = addr;
}
}
freeifaddrs(ifaddr);
if (verbose)
printf("<--get_and_stash_local_ip_addr\n");
return 0;
}
int send_msg(char *data, int len)
{
int num;
if (verbose)
printf("-->send_msg\n");
num = sendto(sendsock, data, len, 0,
(struct sockaddr *)&broadcast_addr,
sizeof(broadcast_addr));
if (verbose)
printf("<--send_msg\n");
if (num != len) {
perror("sendto");
return -1;
} else
return 0;
}
void *recv_func(void *arg)
{
char data[MAX_PACKET_SIZE];
char hostip[INET_ADDRSTRLEN];
struct sockaddr_in fromsockaddr_struct;
struct sockaddr * fromsockaddr = (struct sockaddr *) &fromsockaddr_struct;
socklen_t fromsockaddrsize;
int ret;
if (verbose)
printf("-->recv_func\n");
(void)memcpy(endian_data.s, "\01\02\03\04", 4);
fromsockaddrsize = sizeof (struct sockaddr_in);
while (!send_done) {
int pos;
in_addr_t addr;
NODE *node;
sleep(1);
memset(&data, 0, MAX_PACKET_SIZE);
memset(&hostip, 0, INET_ADDRSTRLEN);
node = create_node();
node->ntype = NODE_TYPE_NONE;
if (verbose)
printf("\t-->recvfrom\n");
ret = recvfrom(recvsock, data, MAX_PACKET_SIZE, 0,
fromsockaddr, &fromsockaddrsize);
addr = fromsockaddr_struct.sin_addr.s_addr;
pos = addr % HASH_KEY;
inet_ntop(AF_INET, &(fromsockaddr_struct.sin_addr), hostip, INET_ADDRSTRLEN);
if (add_tab[pos] == 0) {
struct qnap_response_hdr h;
char *buf;
size_t len = 0;
buf = data;
QNAP_PACKET_UNPACK(h.magic);
h.magic[0] = DATA_TO_LE8(h.magic[0]);
h.magic[1] = DATA_TO_LE8(h.magic[1]);
if (h.magic[0] == QNAP_MAGIC1) {
if (h.magic[1] == QNAP_MAGIC2) {
if (verbose)
printf("\t\t-->It is a Query reponse.\n");
node->ntype = NODE_TYPE_BRIEF;
} else if (h.magic[1] == QNAP_MAGIC3) {
if (verbose)
printf("\t\t-->It is a Detail reponse.\n");
node->ntype = NODE_TYPE_BRIEF;
} else {
if (verbose)
printf("\t\t-->Nothing?\n");
}
} else {
if (verbose)
printf("Not a QNAP response\n");
}
if (node->ntype != NODE_TYPE_NONE) {
node->len = ret;
node->addr = addr;
node->hostip = strdup(hostip);
node->msg = (unsigned char *)malloc(ret);
node->msg = memcpy(node->msg, data, ret);
add_node(response_list, node);
} else {
free(node);
}
if (verbose)
printf("Magic: 0x%llx:0x%llx\n",
(unsigned long long)h.magic[0],
(unsigned long long)h.magic[1]);
add_tab[pos] = addr; /* Add to hash table */
}
}
if (verbose)
printf("<--recv_func\n");
return NULL;
}
int net_init(char *broadcast_ip, unsigned short broadcast_port)
{
int rv;
int bcast = 1;
int loop = 1;
struct sockaddr_in si_me;
struct timeval tv;
if (verbose)
printf("-->net_init\n");
/** Sending socket **/
sendsock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if (sendsock < 0) {
perror("socket");
return -1;
}
/* Initialize the broadcast socket addr */
memset(&broadcast_addr, 0, sizeof(broadcast_addr));
broadcast_addr.sin_family = AF_INET;
broadcast_addr.sin_addr.s_addr = inet_addr(broadcast_ip);
broadcast_addr.sin_port = htons(broadcast_port);
/* Set broadcast on socket */
rv = setsockopt(sendsock, SOL_SOCKET, SO_BROADCAST, (void *)&bcast, sizeof(bcast));
if (rv < 0){
perror("setsockopt:SO_BROADCAST");
return -1;
}
/* Disable loopback so that we don't receieve our own datagrams */
rv = setsockopt(sendsock, IPPROTO_IP, IP_MULTICAST_LOOP, (void *)&loop, sizeof(loop));
if (rv < 0){
perror("setsockopt:IP_MULTICAST_LOOP");
return -1;
}
/** Receving socket **/
recvsock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if (recvsock < 0) {
perror("socket");
return -1;
}
/* Set broadcast on socket */
rv = setsockopt(recvsock, SOL_SOCKET, SO_BROADCAST, (void *)&bcast, sizeof(bcast));
if (rv < 0){
perror("setsockopt:SO_BROADCAST");
return -1;
}
/* Disable loopback so that we don't receieve our own datagrams */
rv = setsockopt(recvsock, IPPROTO_IP, IP_MULTICAST_LOOP, (void *)&loop, sizeof(loop));
if (rv < 0){
perror("setsockopt:IP_MULTICAST_LOOP");
return -1;
}
/* Time out on recv socket */
tv.tv_sec = 30; /* 30 Secs Timeout */
tv.tv_usec = 0;
rv = setsockopt(recvsock, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv,sizeof(struct timeval));
if (rv < 0){
perror("setsockopt:SO_RCVTIMEO");
return -1;
}
memset(&si_me, 0, sizeof(si_me));
si_me.sin_family = AF_INET;
si_me.sin_port = htons(broadcast_port);
si_me.sin_addr.s_addr = INADDR_ANY;
bind(recvsock, (struct sockaddr *)&si_me, sizeof(struct sockaddr));
if (verbose)
printf("<--net_init\n");
return 0;
}
void net_fin(void)
{
if (verbose)
printf("-->net_fin\n");
if (sendsock >= 0)
close(sendsock);
if (recvsock >= 0)
close(recvsock);
if (verbose)
printf("<--net_fin\n");
}
#define QNAP_FIELD_HOSTNAME 01
#define QNAP_FIELD_BRIEF_HOSTNAME_LEN 33
#define QNAP_FIELD_BRIEF_HOSTNAME 34
void parse_brief_response(unsigned char *resp, int len, char *hostip)
{
int hostname_len;
char hostname[_POSIX_HOST_NAME_MAX] = { 0 };
int i;
#if 0
i = 0;
while (i < len) {
fprintf(stdout, ">>> %d: %2x : %c\n", i, resp[i], resp[i]);
i++;
}
return;
#endif
if (verbose)
printf("-->parse_brief_response\n");
hostname_len = resp[QNAP_FIELD_BRIEF_HOSTNAME_LEN];
(void)memcpy(hostname, resp+QNAP_FIELD_BRIEF_HOSTNAME, hostname_len);
fprintf(stdout, "\tHostname : %s\n", hostname);
fprintf(stdout, "\tIP Address : %s\n", hostip);
i = QNAP_FIELD_BRIEF_HOSTNAME + hostname_len + 2;
fprintf(stdout, "\tType : ");
while (i < len) {
fprintf(stdout, "%c", resp[i]);
i++;
if (resp[i] == 202 && resp[i+1] == 9) {/* check for 0xca 0x09*/
i+=2;
break;
}
}
fprintf(stdout, "\n");
fprintf(stdout, "\tURL : https://%s/cgi-bin/login.html\n",
hostip);
fprintf(stdout, "\n");
if (verbose)
printf("<--parse_brief_response\n");
return;
}
void parse_detail_response(unsigned char *resp, int len, char *hostip)
{
if (verbose)
printf("-->parse_detail_response\n");
if (verbose)
printf("<--parse_detail_response\n");
return;
}
void print_qnap_list(LIST *list)
{
NODE *node;
int count = 0;
if (verbose)
printf("-->print_qnap_list\n");
if (!list)
return;
if (!list->num_entries) {
fprintf(stdout, "No QNAP boxes found. \n");
return;
}
node = list->first;
while (node != NULL) {
count++;
fprintf(stdout, "%d) \n", count);
if (node->ntype == NODE_TYPE_BRIEF) {
parse_brief_response(node->msg, node->len, node->hostip);
} else if(node->ntype == NODE_TYPE_DETAIL)
parse_detail_response(node->msg, node->len, node->hostip);
node = node->next;
}
if (verbose)
printf("<--print_qnap_list\n");
}
static void print_version(void)
{
printf("qnap-finder v%s\n", VERSION_STRING);
}
static void print_help(void)
{
print_version();
printf("\nUsage: qnap-finder [options]");
printf("\n\noptions include:");
printf("\n --help|-h This help text");
printf("\n --detail|-d Query for detailed information.(default is brief)");
printf("\n --verbose|-v Verbose debug");
printf("\n --version|-V Prints current version\n\n");
}
static void parse_argument(const char *arg)
{
const char *p = arg+1;
if (verbose)
printf("-->parse_argument\n");
do {
switch (*p) {
case 'd':
query_detail_info = TRUE;
continue;
case 'h':
print_help();
exit(EXIT_SUCCESS);
case 'v':
verbose = TRUE;
continue;
case 'V':
print_version();
exit(EXIT_SUCCESS);
case '-': /* long options */
if (strcmp(arg, "--detail") == 0) {
query_detail_info = TRUE;
return;
}
if (strcmp(arg, "--help") == 0) {
print_help();
exit(EXIT_SUCCESS);
}
if (strcmp(arg, "--verbose") == 0) {
verbose = TRUE;
return;
}
if (strcmp(arg, "--version") == 0) {
print_version();
exit(EXIT_SUCCESS);
}
default:
fprintf(stderr, "Bad argument '%s'\n", arg);
exit(1);
}
} while(*++p);
if (verbose)
printf("<--parse_argument\n");
}
int main(int argc, char **argv)
{
pthread_t recv_thread;
int ret;
int i;
for (i = 1; i < argc; i++) {
const char *a = argv[i];
if (a[0] == '-') {
parse_argument(a);
continue;
}
}
if (verbose)
printf("-->main\n");
fprintf(stdout, "Looking for QNAP boxes.....\r");
fflush(stdout);
response_list = create_list();
get_and_stash_local_ip_addr();
net_init(BROADCAST_IP,BROADCAST_PORT);
/* create recv_thread first */
ret = pthread_create(&recv_thread, NULL, recv_func, NULL);
if (ret) {
fprintf(stderr, "Error: pthread_create(recv_func): %d\n", ret);
ret = EXIT_FAILURE;
goto cleanup;
}
if (query_detail_info) {
/* request detail packet */
send_msg(msg[2], SEND_MESG_LEN);
} else {
/* request brief packet */
send_msg(msg[0], SEND_MESG_LEN);
}
sleep(3);
send_done = TRUE;
if (pthread_join(recv_thread, NULL)) {
fprintf(stderr, "error joining thread recv_thread\n");
ret = EXIT_FAILURE;
goto cleanup;
}
print_qnap_list(response_list);
ret = EXIT_SUCCESS;
cleanup:
net_fin();
free_list(response_list);
if (verbose)
printf("<--main\n");
exit(ret);
}