forked from alexpevzner/sane-airscan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
airscan.h
2524 lines (2130 loc) · 60.6 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-glib/glib-watch.h>
#include <sane/sane.h>
#include <sane/saneopts.h>
#include <math.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <netinet/in.h>
#include <sys/socket.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
/******************** 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)
{
node->ll_prev->ll_next = node->ll_next;
node->ll_next->ll_prev = node->ll_prev;
/* 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;
if (ll_empty(head)) {
return NULL;
}
node = head->node.ll_next;
ll_del(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;
if (ll_empty(head)) {
return NULL;
}
node = head->node.ll_prev;
ll_del(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 (ll_head *head, ll_node *node)
{
node = node->ll_next;
return node == &head->node ? NULL : node;
}
/* 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 (ll_head *head, ll_node *node)
{
node = node->ll_prev;
return node == &head->node ? NULL : node;
}
/* Get first node of the list.
* Returns NULL if list is empty
*/
static inline ll_node*
ll_first (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 (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)
/******************** 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;
/* 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
*/
ip_straddr
ip_straddr_from_sockaddr(const struct sockaddr *addr);
/* Format ip_straddr from struct sockaddr.
* Port will not be appended, if it matches provided default port
* Both AF_INET and AF_INET6 are supported
*/
ip_straddr
ip_straddr_from_sockaddr_dport(const struct sockaddr *addr, int dport);
/* 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
*/
bool
ip_addrset_lookup (const ip_addrset *addrset, ip_addr addr);
/* Add address to the set. Returns true, if address was
* actually added, false if it was already in the set
*/
bool
ip_addrset_add (ip_addrset *addrset, ip_addr addr);
/* Add address to the set without checking for duplicates
*/
void
ip_addrset_add_unsafe (ip_addrset *addrset, ip_addr addr);
/* Del address from the set.
*/
void
ip_addrset_del (ip_addrset *addrset, ip_addr addr);
/******************** Network interfaces addresses ********************/
/* Network interface name, wrapped into structure, so
* it can be passed by value
*/
typedef struct {
char text[32];
} netif_name;
/* Network interface address
*/
typedef struct netif_addr netif_addr;
struct netif_addr {
netif_addr *next; /* Next address in the list */
int ifindex; /* Interface index */
netif_name ifname; /* Interface name, for logging */
bool ipv6; /* This is an IPv6 address */
void *data; /* Placeholder for user data */
char straddr[64]; /* Address string */
union {
struct in_addr v4; /* IPv4 address */
struct in6_addr v6; /* IPv6 address */
} ip;
};
/* Get list of network interfaces addresses
* The returned list is sorted
*/
netif_addr*
netif_addr_list_get (void);
/* Free list of network interfaces addresses
*/
void
netif_addr_list_free (netif_addr *list);
/* netif_diff represents a difference between two
* lists of network interface addresses
*/
typedef struct {
netif_addr *added, *removed; /* What was added/removed */
netif_addr *preserved;
} netif_diff;
/* Compute a difference between two lists of addresses.
*
* It works by tossing nodes between 3 output lists:
* * if node is present in list2 only, it is moved
* to netif_diff.added
* * if node is present in list1 only, it is moved
* to netif_diff.removed
* * if node is present in both lists, node from
* list1 is moved to preserved, and node from
* list2 is released
*
* It assumes, both lists are sorted, as returned
* by netif_addr_get(). Returned lists are also sorted
*/
netif_diff
netif_diff_compute (netif_addr *list1, netif_addr *list2);
/* Merge two lists of addresses
*
* Input lists are consumed and new list is created.
*
* Input lists are assumed to be sorted, and output
* list will be sorted as well
*/
netif_addr*
netif_addr_list_merge (netif_addr *list1, netif_addr *list2);
/* Network interfaces addresses change notifier
*/
typedef struct netif_notifier netif_notifier;
/* Create netif_notifier
*/
netif_notifier*
netif_notifier_create (void (*callback) (void*), void *data);
/* Destroy netif_notifier
*/
void
netif_notifier_free (netif_notifier *notifier);
/******************** Pollable events ********************/
/* The pollable event
*
* Pollable events allow to wait until some event happens
* and can be used in combination with select()/poll()
* system calls
*/
typedef struct pollable pollable;
/* Create new pollable event
*/
pollable*
pollable_new (void);
/* Free pollable event
*/
void
pollable_free (pollable *p);
/* Get file descriptor for poll()/select().
*
* When pollable event becomes "ready", this file descriptor
* becomes readable from the select/poll point of view
*/
int
pollable_get_fd (pollable *p);
/* Make pollable event "ready"
*/
void
pollable_signal (pollable *p);
/* Make pollable event "not ready"
*/
void
pollable_reset (pollable *p);
/* Wait until pollable event is ready
*/
void
pollable_wait (pollable *p);
/******************** Event loop ********************/
/* Initialize event loop
*/
SANE_Status
eloop_init (void);
/* Cleanup event loop
*/
void
eloop_cleanup (void);
/* Add start/stop callback. This callback is called
* on a event loop thread context, once when event
* loop is started, and second time when it is stopped
*
* Start callbacks are called in the same order as
* they were added. Stop callbacks are called in a
* reverse order
*/
void
eloop_add_start_stop_callback (void (*callback) (bool start));
/* Start event loop thread.
*
* Callback is called from the thread context twice:
* callback(true) - when thread is started
* callback(false) - when thread is about to exit
*/
void
eloop_thread_start (void);
/* Stop event loop thread and wait until its termination
*/
void
eloop_thread_stop (void);
/* Acquire event loop mutex
*/
void
eloop_mutex_lock (void);
/* Release event loop mutex
*/
void
eloop_mutex_unlock (void);
/* Wait on conditional variable under the event loop mutex
*/
void
eloop_cond_wait (GCond *cond);
/* eloop_cond_wait() with timeout in seconds
*/
bool
eloop_cond_wait_until (GCond *cond, gint64 timeout);
/* Create AvahiGLibPoll that runs in context of the event loop
*/
AvahiGLibPoll*
eloop_new_avahi_poll (void);
/* Call function on a context of event loop thread
*/
void
eloop_call (GSourceFunc func, gpointer data);
/* Event notifier. Calls user-defined function on a context
* of event loop thread, when event is triggered. This is
* safe to trigger the event from a context of any thread
* or even from a signal handler
*/
typedef struct eloop_event eloop_event;
/* Create new event notifier. May return NULL
*/
eloop_event*
eloop_event_new (void (*callback)(void *), void *data);
/* Destroy event notifier
*/
void
eloop_event_free (eloop_event *event);
/* Trigger an event
*/
void
eloop_event_trigger (eloop_event *event);
/* Timer. Calls user-defined function after a specified
* interval
*/
typedef struct eloop_timer eloop_timer;
/* Create new timer. Timeout is in milliseconds
*/
eloop_timer*
eloop_timer_new (int timeout, void (*callback)(void *), void *data);
/* Cancel a timer
*
* Caller SHOULD NOT cancel expired timer (timer with called
* callback) -- this is done automatically
*/
void
eloop_timer_cancel (eloop_timer *timer);
/* eloop_fdpoll notifies user when file becomes
* readable, writable or both, depending on its
* event mask
*/
typedef struct eloop_fdpoll eloop_fdpoll;
/* Mask of file events user interested in
*/
typedef enum {
ELOOP_FDPOLL_READ = (1 << 0),
ELOOP_FDPOLL_WRITE = (1 << 1),
ELOOP_FDPOLL_BOTH = ELOOP_FDPOLL_READ | ELOOP_FDPOLL_WRITE
} ELOOP_FDPOLL_MASK;
/* Create eloop_fdpoll
*
* Callback will be called, when file will be ready for read/write/both,
* depending on mask
*
* Initial mask value is 0, and it can be changed, using
* eloop_fdpoll_set_mask() function
*/
eloop_fdpoll*
eloop_fdpoll_new (int fd,
void (*callback) (int, void*, ELOOP_FDPOLL_MASK), void *data);
/* Destroy eloop_fdpoll
*/
void
eloop_fdpoll_free (eloop_fdpoll *fdpoll);
/* Set eloop_fdpoll event mask
*/
void
eloop_fdpoll_set_mask (eloop_fdpoll *fdpoll, ELOOP_FDPOLL_MASK mask);
/* Format error string, as printf() does and save result
* in the memory, owned by the event loop
*
* Caller should not free returned string. This is safe
* to use the returned string as an argument to the
* subsequent eloop_eprintf() call.
*
* The returned string remains valid until next call
* to eloop_eprintf(), which makes it usable to
* report errors up by the stack. However, it should
* not be assumed, that the string will remain valid
* on a next eloop roll, so don't save this string
* anywhere, if you need to do so, create a copy!
*/
error
eloop_eprintf(const char *fmt, ...);
/******************** HTTP Client ********************/
/* Create new URI, by parsing URI string
*/
http_uri*
http_uri_new (const char *str, bool strip_fragment);
/* Clone an URI
*/
http_uri*
http_uri_clone (const http_uri *old);
/* Create URI, relative to base URI. If `path_only' is
* true, scheme, host and port are taken from the
* base URI
*/
http_uri*
http_uri_new_relative (const http_uri *base, const char *path,
bool strip_fragment, bool path_only);
/* Free the URI
*/
void
http_uri_free (http_uri *uri);
/* Get URI string
*/
const char*
http_uri_str (http_uri *uri);
/* Get URI's host address. If Host address is not literal, returns NULL
*/
const struct sockaddr*
http_uri_addr (http_uri *uri);
/* Get URI path
*/
const char*
http_uri_get_path (const http_uri *uri);
/* Set URI path
*/
void
http_uri_set_path (http_uri *uri, const char *path);
/* Fix IPv6 address zone suffix
*/
void
http_uri_fix_ipv6_zone (http_uri *uri, int ifindex);