forked from alexpevzner/sane-airscan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
airscan.h
3098 lines (2626 loc) · 74.8 KB
/
airscan.h
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
/* AirScan (a.k.a. eSCL) backend for SANE
*
* Copyright (C) 2019 and up by Alexander Pevzner ([email protected])
* See LICENSE for license terms and conditions
*/
#ifndef airscan_h
#define airscan_h
#include <avahi-common/address.h>
#include <avahi-common/strlst.h>
#include <avahi-common/watch.h>
#include <sane/sane.h>
#include <sane/saneopts.h>
#include <ctype.h>
#include <math.h>
#include <pthread.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/types.h>
/******************** Static configuration ********************/
/* Configuration path in environment
*/
#define CONFIG_PATH_ENV "SANE_CONFIG_DIR"
/* Standard SANE configuration directory
*/
#define CONFIG_SANE_CONFIG_DIR "/etc/sane.d/"
/* Sane-airscan configuration file and subdirectory names
*/
#define CONFIG_AIRSCAN_CONF "airscan.conf"
#define CONFIG_AIRSCAN_D "airscan.d"
/* Environment variables
*/
#define CONFIG_ENV_AIRSCAN_DEBUG "SANE_DEBUG_AIRSCAN"
/* Default resolution, DPI
*/
#define CONFIG_DEFAULT_RESOLUTION 300
/* Minimal interval between subsequent sane_start()
* attempts, if previous sane_start was failed
*/
#define CONFIG_START_RETRY_INTERVAL 2500
/******************** Forward declarations ********************/
/* log_ctx represents logging context
*/
typedef struct log_ctx log_ctx;
/* Type http_uri represents HTTP URI
*/
typedef struct http_uri http_uri;
/******************** Utility macros ********************/
/* Obtain pointer to outer structure from pointer to
* its known member
*/
#define OUTER_STRUCT(member_p,struct_t,field) \
((struct_t*)((char*)(member_p) - ((ptrdiff_t) &(((struct_t*) 0)->field))))
/******************** Circular Linked Lists ********************/
/* ll_node represents a linked data node.
* Data nodes are embedded into the corresponding data structures:
* struct data {
* ll_node chain; // Linked list chain
* ...
* };
*
* Use OUTER_STRUCT() macro to obtain pointer to containing
* structure from the pointer to the list node
*/
typedef struct ll_node ll_node;
struct ll_node {
ll_node *ll_prev, *ll_next;
};
/* ll_head represents a linked list head node
* ll_head must be initialized before use with ll_init() function
*/
typedef struct {
ll_node node;
} ll_head;
/* Initialize list head
*/
static inline void
ll_init (ll_head *head)
{
head->node.ll_next = head->node.ll_prev = &head->node;
}
/* Check if list is empty
*/
static inline bool
ll_empty (const ll_head *head)
{
return head->node.ll_next == &head->node;
}
/* Push node to the end of the list, represented
* by its head node
*/
static inline void
ll_push_end (ll_head *head, ll_node *node)
{
node->ll_prev = head->node.ll_prev;
node->ll_next = &head->node;
head->node.ll_prev->ll_next = node;
head->node.ll_prev = node;
}
/* Push node to the beginning of the list, represented
* by its head node
*/
static inline void
ll_push_beg (ll_head *head, ll_node *node)
{
node->ll_next = head->node.ll_next;
node->ll_prev = &head->node;
head->node.ll_next->ll_prev = node;
head->node.ll_next = node;
}
/* Delete node from the list
*/
static inline void
ll_del (ll_node *node)
{
ll_node *p = node->ll_prev, *n = node->ll_next;
p->ll_next = n;
n->ll_prev = p;
/* Make double-delete safe */
node->ll_next = node->ll_prev = node;
}
/* Pop node from the beginning of the list.
* Returns NULL if list is empty
*/
static inline ll_node*
ll_pop_beg (ll_head *head)
{
ll_node *node, *next;
node = head->node.ll_next;
if (node == &head->node) {
return NULL; /* List is empty if it is looped to itself */
}
next = node->ll_next;
next->ll_prev = &head->node;
head->node.ll_next = next;
/* Make double-delete safe */
node->ll_next = node->ll_prev = node;
return node;
}
/* Pop node from the end of the list.
* Returns NULL if list is empty
*/
static inline ll_node*
ll_pop_end (ll_head *head)
{
ll_node *node, *prev;
node = head->node.ll_prev;
if (node == &head->node) {
return NULL; /* List is empty if it is looped to itself */
}
prev = node->ll_prev;
prev->ll_next = &head->node;
head->node.ll_prev = prev;
/* Make double-delete safe */
node->ll_next = node->ll_prev = node;
return node;
}
/* Get next (from the beginning to the end) node of
* the list. Returns NULL, if end of list is reached
*/
static inline ll_node*
ll_next (const ll_head *head, const ll_node *node)
{
ll_node *next = node->ll_next;
return next == &head->node ? NULL : next;
}
/* Get previous (from the beginning to the end) node of
* the list. Returns NULL, if end of list is reached
*/
static inline ll_node*
ll_prev (const ll_head *head, const ll_node *node)
{
ll_node *prev = node->ll_prev;
return prev == &head->node ? NULL : prev;
}
/* Get first node of the list.
* Returns NULL if list is empty
*/
static inline ll_node*
ll_first (const ll_head *head)
{
return ll_next(head, &head->node);
}
/* Get last node of the list.
* Returns NULL if list is empty
*/
static inline ll_node*
ll_last (const ll_head *head)
{
return ll_prev(head, &head->node);
}
/* Concatenate lists:
* list1 += list2
* list2 = empty
*/
static inline void
ll_cat (ll_head *list1, ll_head *list2)
{
if (ll_empty(list2)) {
return;
}
list2->node.ll_prev->ll_next = &list1->node;
list2->node.ll_next->ll_prev = list1->node.ll_prev;
list1->node.ll_prev->ll_next = list2->node.ll_next;
list1->node.ll_prev = list2->node.ll_prev;
ll_init(list2);
}
/* Helper macro for list iteration.
* Usage:
* for (LL_FOR_EACH(node, list)) {
* // do something with the node
* }
*/
#define LL_FOR_EACH(node,list) \
node = ll_first(list); node != NULL; node = ll_next(list, node)
/******************** Memory allocation ********************/
/* Allocate `len' elements of type T
*/
#define mem_new(T,len) ((T*) __mem_alloc(len, 0, sizeof(T), true))
/* Resize memory. The returned memory block has length of `len' and
* capacity at least of `len' + `extra'
*
* If p is NULL, new memory block will be allocated. Otherwise,
* existent memory block will be resized, new pointer is returned,
* while old becomes invalid (similar to how realloc() works).
*
* This function never returns NULL, it panics in a case of
* memory allocation error.
*/
#define mem_resize(p,len,extra) __mem_resize(p,len,extra,sizeof(*p),true)
/* Try to resize memory. It works like mem_resize() but may
* return NULL if memory allocation failed.
*/
#define mem_try_resize(p,len,extra) __mem_resize(p,len,extra,sizeof(*p),false)
/* Truncate the memory block length, preserving its capacity
*/
void
mem_trunc (void *p);
/* Shrink the memory block length, preserving its capacity
*/
#define mem_shrink(p,len) __mem_shrink(p,len, sizeof(*p))
/* Free memory block, obtained from mem_new() or mem_resize()
* `p' can be NULL
*/
void
mem_free (void *p);
/* Get memory block length/capacity, in bytes
* For NULL pointer return 0
*/
size_t mem_len_bytes (const void *p);
size_t mem_cap_bytes (const void *p);
/* Get memory block length/capacity, in elements
* For NULL pointer return 0
*/
#define mem_len(v) (mem_len_bytes(v) / sizeof(*v))
#define mem_cap(v) (mem_cap_bytes(v) / sizeof(*v))
/* Helper functions for memory allocation, don't use directly
*/
void* __attribute__ ((__warn_unused_result__))
__mem_alloc (size_t len, size_t extra, size_t elsize, bool must);
void* __attribute__ ((__warn_unused_result__))
__mem_resize (void *p, size_t len, size_t cap, size_t elsize, bool must);
void
__mem_shrink (void *p, size_t len, size_t elsize);
/******************** Strings ********************/
/* Create new string
*/
static inline char*
str_new (void) {
char *s = mem_resize((char*) NULL, 0, 1);
*s = '\0';
return s;
}
/* Create new string as a copy of existent string
*/
static inline char*
str_dup (const char *s1)
{
size_t len = strlen(s1);
char *s = mem_resize((char*) NULL, len, 1);
memcpy(s, s1, len + 1);
return s;
}
/* Create new string as a lowercase copy of existent string
*/
char*
str_dup_tolower (const char *s1);
/* Create new string and print to it
*/
char*
str_printf (const char *format, ...);
/* Create new string and print to it, va_list version
*/
char*
str_vprintf (const char *format, va_list ap);
/* Truncate the string
*/
static inline void
str_trunc (char *s)
{
mem_trunc(s);
*s = '\0';
}
/* Resize the string
*
* s1 must be previously created by some of str_XXX functions,
* s1 will be consumed and the new pointer will be returned
*/
static inline char*
str_resize (char *s, size_t len)
{
s = mem_resize(s, len, 1);
s[len] = '\0';
return s;
}
/* Append memory to string:
* s1 += s2[:l2]
*
* s1 must be previously created by some of str_XXX functions,
* s1 will be consumed and the new pointer will be returned
*/
static inline char*
str_append_mem (char *s1, const char *s2, size_t l2)
{
size_t l1 = mem_len(s1);
s1 = mem_resize(s1, l1 + l2, 1);
memcpy(s1 + l1, s2, l2);
s1[l1+l2] = '\0';
return s1;
}
/* Append string to string:
* s1 += s2
*
* s1 must be previously created by some of str_XXX functions,
* s1 will be consumed and the new pointer will be returned
*/
static inline char*
str_append (char *s1, const char *s2)
{
return str_append_mem(s1, s2, strlen(s2));
}
/* Append character to string:
* s1 += c
*
* `s' must be previously created by some of str_XXX functions,
* `s' will be consumed and the new pointer will be returned
*/
static inline char*
str_append_c (char *s, char c)
{
return str_append_mem(s, &c, 1);
}
/* Append formatted string to string
*
* `s' must be previously created by some of str_XXX functions,
* `s' will be consumed and the new pointer will be returned
*/
char*
str_append_printf (char *s, const char *format, ...);
/* Append formatted string to string -- va_list version
*/
char*
str_append_vprintf (char *s, const char *format, va_list ap);
/* Assign value to string
*
* `s1' must be previously created by some of str_XXX functions,
* `s1' will be consumed and the new pointer will be returned
*/
static inline char*
str_assign (char *s1, const char *s2)
{
mem_trunc(s1);
return str_append(s1, s2);
}
/* Concatenate several strings. Last pointer must be NULL.
* The returned pointer must be eventually freed by mem_free
*/
char*
str_concat (const char *s, ...);
/* Make sure that string is terminated with the `c' character:
* if string is not empty and the last character is not `c`,
* append `c' to the string
*
* `s' must be previously created by some of str_XXX functions,
* `s' will be consumed and the new pointer will be returned
*/
static inline char*
str_terminate (char *s, char c)
{
if (s[0] != '\0' && s[mem_len(s) - 1] != c) {
s = str_append_c(s, c);
}
return s;
}
/* Check if string has a specified prefix
*/
bool
str_has_prefix (const char *s, const char *prefix);
/* Check if string has a specified suffix
*/
bool
str_has_suffix (const char *s, const char *suffix);
/* Remove leading and trailing white space.
* This function modifies string in place, and returns pointer
* to original string, for convenience
*/
char*
str_trim (char *s);
/******************** NULL-terminated pointer arrays ********************/
/* Create NULL-terminated array of pointers of type *T
*/
#define ptr_array_new(T) mem_resize((T*) NULL, 0, 1)
/* Append pointer to the NULL-terminated array of pointers.
* Returns new, potentially reallocated array
*/
#define ptr_array_append(a,p) \
((__typeof__(a)) __ptr_array_append((void**)a, p))
/* Truncate NULL-terminated array of pointers
*/
#define ptr_array_trunc(a) \
do { \
mem_trunc(a); \
a[0] = NULL; \
} while(0)
/* Find pointer within array of pointers.
* Return non-negative index if pointer was found, -1 otherwise
*/
#define ptr_array_find(a,p) __ptr_array_find((void**) a, p)
/* Delete element at given index.
* Returns value of deleted pointer or NULL, if index is out of range
*/
#define ptr_array_del(a,i) \
((__typeof__(*a)) __ptr_array_del((void**) a, i))
/* Helper function for ptr_array_append, don't use directly
*/
static inline void**
__ptr_array_append (void **a, void *p)
{
size_t len = mem_len(a) + 1;
a = mem_resize(a, len, 1);
a[len - 1] = p;
a[len] = NULL;
return a;
}
/* Helper function for ptr_array_find, don't use directly
*/
static inline int
__ptr_array_find (void **a, void *p)
{
size_t len = mem_len(a), i;
for (i = 0; i < len; i ++) {
if (a[i] == p) {
return (int) i;
}
}
return -1;
}
/* Helper function for ptr_array_del, don't use directly
*/
static inline void*
__ptr_array_del (void **a, int i)
{
size_t len = mem_len(a);
void *p;
if (i < 0 || i >= (int) len) {
return NULL;
}
len --;
p = a[i];
memmove(&a[i], &a[i + 1], sizeof(void*) * (len - i));
mem_shrink(a, len);
a[len] = NULL;
return p;
}
/******************** Safe ctype macros ********************/
#define safe_isspace(c) isspace((unsigned char) c)
#define safe_isxdigit(c) isxdigit((unsigned char) c)
#define safe_toupper(c) toupper((unsigned char) c)
#define safe_tolower(c) tolower((unsigned char) c)
/******************** OS Facilities ********************/
/* Get user's home directory. There is no need to
* free the returned string
*
* May return NULL in a case of error
*/
const char *
os_homedir (void);
/* Make directory with parents
*/
int
os_mkdir (const char *path, mode_t mode);
/******************** Error handling ********************/
/* Type error represents an error. Its value either NULL,
* which indicates "no error" condition, or some opaque
* non-null pointer, which can be converted to string
* with textual description of the error, using the ESTRING()
* function
*
* Caller should not attempt to free the memory, referred
* by error or string, obtained from an error using the
* ESTRING() function
*/
typedef struct {} *error;
/* Standard errors
*/
extern error ERROR_ENOMEM;
/* Construct error from a string
*/
static inline error
ERROR (const char *s)
{
return (error) s;
}
/* Obtain textual representation of the error
*/
static inline const char*
ESTRING (error err)
{
return (const char*) err;
}
/******************** Various identifiers ********************/
/* ID_PROTO represents protocol identifier
*/
typedef enum {
ID_PROTO_UNKNOWN = -1,
ID_PROTO_ESCL,
ID_PROTO_WSD,
NUM_ID_PROTO
} ID_PROTO;
/* id_proto_name returns protocol name
* For unknown ID returns NULL
*/
const char*
id_proto_name (ID_PROTO proto);
/* id_proto_by_name returns protocol identifier by name
* For unknown name returns ID_PROTO_UNKNOWN
*/
ID_PROTO
id_proto_by_name (const char* name);
/* ID_SOURCE represents scanning source
*/
typedef enum {
ID_SOURCE_UNKNOWN = -1,
ID_SOURCE_PLATEN,
ID_SOURCE_ADF_SIMPLEX,
ID_SOURCE_ADF_DUPLEX,
NUM_ID_SOURCE
} ID_SOURCE;
/* id_source_sane_name returns SANE name for the source
* For unknown ID returns NULL
*/
const char*
id_source_sane_name (ID_SOURCE id);
/* id_source_by_sane_name returns ID_SOURCE by its SANE name
* For unknown name returns ID_SOURCE_UNKNOWN
*/
ID_SOURCE
id_source_by_sane_name (const char *name);
/* ID_COLORMODE represents color mode
*/
typedef enum {
ID_COLORMODE_UNKNOWN = -1,
ID_COLORMODE_COLOR,
ID_COLORMODE_GRAYSCALE,
ID_COLORMODE_BW1,
NUM_ID_COLORMODE
} ID_COLORMODE;
/* id_colormode_sane_name returns SANE name for the color mode
* For unknown ID returns NULL
*/
const char*
id_colormode_sane_name (ID_COLORMODE id);
/* id_colormode_by_sane_name returns ID_COLORMODE by its SANE name
* For unknown name returns ID_COLORMODE_UNKNOWN
*/
ID_COLORMODE
id_colormode_by_sane_name (const char *name);
/* ID_FORMAT represents image format
*/
typedef enum {
ID_FORMAT_UNKNOWN = -1,
ID_FORMAT_JPEG,
ID_FORMAT_TIFF,
ID_FORMAT_PNG,
ID_FORMAT_PDF,
ID_FORMAT_BMP,
NUM_ID_FORMAT
} ID_FORMAT;
/* id_format_mime_name returns MIME name for the image format
*/
const char*
id_format_mime_name (ID_FORMAT id);
/* id_format_by_mime_name returns ID_FORMAT by its MIME name
* For unknown name returns ID_FORMAT_UNKNOWN
*/
ID_FORMAT
id_format_by_mime_name (const char *name);
/* if_format_short_name returns short name for ID_FORMAT
*/
const char*
id_format_short_name (ID_FORMAT id);
/******************** Device ID ********************/
/* Allocate unique device ID
*/
unsigned int
devid_alloc (void);
/* Free device ID
*/
void
devid_free (unsigned int id);
/* Initialize device ID allocator
*/
void
devid_init (void);
/******************** Random bytes ********************/
/* Get N random bytes
*/
void
rand_bytes (void *buf, size_t n);
/* Initialize random bytes generator
*/
SANE_Status
rand_init (void);
/* Cleanup random bytes generator
*/
void
rand_cleanup (void);
/******************** UUID utilities ********************/
/* Type uuid represents a random UUID string.
*
* It is wrapped into struct, so it can be returned
* by value, without need to mess with memory allocation
*/
typedef struct {
char text[sizeof("urn:uuid:ede05377-460e-4b4a-a5c0-423f9e02e8fa")];
} uuid;
/* Check if uuid is valid
*/
static inline bool
uuid_valid (uuid u)
{
return u.text[0] != '\0';
}
/* Generate random UUID. Generated UUID has a following form:
* urn:uuid:ede05377-460e-4b4a-a5c0-423f9e02e8fa
*/
uuid
uuid_rand (void);
/* Parse UUID. This function ignores all "decorations", like
* urn:uuid: prefix and so on, and takes only hexadecimal digits
* into considerations
*
* Check the returned uuid with uuid_valid() for possible parse errors
*/
uuid
uuid_parse (const char *in);
/* Generate uuid by cryptographically cacheing input string
*/
uuid
uuid_hash (const char *s);
/* Compare two uuids
*/
static inline bool
uuid_equal (uuid u1, uuid u2)
{
return !strcmp(u1.text, u2.text);
}
/******************** Configuration file loader ********************/
/* Device URI for manually disabled device
*/
#define CONF_DEVICE_DISABLE "disable"
/* Device configuration, for manually added devices
*/
typedef struct conf_device conf_device;
struct conf_device {
unsigned int devid; /* Device ident */
const char *name; /* Device name */
ID_PROTO proto; /* Protocol to use */
http_uri *uri; /* Device URI, parsed; NULL if device disabled */
conf_device *next; /* Next device in the list */
};
/* WSDD_MODE represents WS-Discovery mode
*/
typedef enum {
WSDD_FAST, /* Use hints from DNS-SD to speed up WSDD */
WSDD_FULL, /* Full discovery, slow and fair */
WSDD_OFF /* Disable WSDD */
} WSDD_MODE;
/* Backend configuration
*/
typedef struct {
bool dbg_enabled; /* Debugging enabled */
const char *dbg_trace; /* Trace directory */
conf_device *devices; /* Manually configured devices */
bool discovery; /* Scanners discovery enabled */
bool model_is_netname; /* Use network name instead of model */
bool proto_auto; /* Auto protocol selection */
WSDD_MODE wsdd_mode; /* WS-Discovery mode */
} conf_data;
#define CONF_INIT { \
.dbg_enabled = false, \
.dbg_trace = NULL, \
.devices = NULL, \
.discovery = true, \
.model_is_netname = true, \
.proto_auto = true, \
.wsdd_mode = WSDD_FAST \
}
extern conf_data conf;
/* Load configuration. It updates content of a global conf variable
*/
void
conf_load (void);
/* Free resources, allocated by conf_load, and reset configuration
* data into initial state
*/
void
conf_unload (void);
/******************** Utility functions for IP addresses ********************/
/* Address string, wrapped into structure so can
* be passed by value
*/
typedef struct {
char text[64];
} ip_straddr;
/* Format ip_straddr from IP address (struct in_addr or struct in6_addr)
* af must be AF_INET or AF_INET6
*/
ip_straddr
ip_straddr_from_ip (int af, const void *addr);
/* Format ip_straddr from struct sockaddr.
* Both AF_INET and AF_INET6 are supported
*
* If `withzone' is true, zone suffix will be appended, when appropriate
*/
ip_straddr
ip_straddr_from_sockaddr(const struct sockaddr *addr, bool withzone);
/* Format ip_straddr from struct sockaddr.
* Both AF_INET and AF_INET6 are supported
*
* Port will not be appended, if it matches provided default port
* If `withzone' is true, zone suffix will be appended, when appropriate
*/
ip_straddr
ip_straddr_from_sockaddr_dport (const struct sockaddr *addr,
int dport, bool withzone);
/* Check if address is link-local
* af must be AF_INET or AF_INET6
*/
bool
ip_is_linklocal (int af, const void *addr);
/* Check if sockaddr is link-local
*/
bool
ip_sockaddr_is_linklocal (const struct sockaddr *addr);
/* Check if address is loopback
* af must be AF_INET or AF_INET6
*/
bool
ip_is_loopback (int af, const void *addr);
/* ip_addr represents IPv4 or IPv6 address
*/
typedef struct {
int af; /* AF_INET or AF_INET6 */
int ifindex; /* For IPv6 link-local addresses */
union {
struct in_addr v4; /* IPv4 address */
struct in6_addr v6; /* IPv4 address */
} ip;
} ip_addr;
/* Make ip_addr
*/
static inline ip_addr
ip_addr_make (int ifindex, int af, const void *addr)
{
ip_addr ip_addr;
memset(&ip_addr, 0, sizeof(ip_addr));
ip_addr.af = af;
switch (ip_addr.af) {
case AF_INET:
memcpy(&ip_addr.ip.v4, addr, 4);
break;
case AF_INET6:
memcpy(&ip_addr.ip, addr, 16);
if (ip_is_linklocal(AF_INET6, &ip_addr.ip.v6)) {
ip_addr.ifindex = ifindex;
}
break;
}
return ip_addr;
}
/* Extract ip_addr from sockaddr
*/
static inline ip_addr
ip_addr_from_sockaddr (const struct sockaddr *sockaddr)
{
ip_addr addr;
memset(&addr, 0, sizeof(addr));
addr.af = sockaddr->sa_family;
switch (addr.af) {
case AF_INET:
addr.ip.v4 = ((struct sockaddr_in*) sockaddr)->sin_addr;
break;
case AF_INET6:
addr.ip.v6 = ((struct sockaddr_in6*) sockaddr)->sin6_addr;
if (ip_is_linklocal(AF_INET6, &addr.ip.v6)) {
addr.ifindex = ((struct sockaddr_in6*) sockaddr)->sin6_scope_id;
}
break;
}
return addr;
}
/* Check if two addresses are equal
*/
static inline bool
ip_addr_equal (ip_addr a1, ip_addr a2)
{
if (a1.af != a2.af) {
return false;
}
switch (a1.af) {
case AF_INET:
return a1.ip.v4.s_addr == a2.ip.v4.s_addr;
case AF_INET6:
return a1.ifindex == a2.ifindex &&
!memcmp(a1.ip.v6.s6_addr, a2.ip.v6.s6_addr, 16);
}
return false;
}
/* ip_addr_set represents a set of IP addresses
*/
typedef struct ip_addrset ip_addrset;
/* Create new ip_addrset
*/
ip_addrset*
ip_addrset_new (void);
/* Free ip_addrset
*/
void
ip_addrset_free (ip_addrset *addrset);
/* Check if address is in set