-
Notifications
You must be signed in to change notification settings - Fork 120
/
cups-browsed.c
12466 lines (11314 loc) · 404 KB
/
cups-browsed.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
/***
This file is part of cups-filters.
This file 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.1 of the
License, or (at your option) any later version.
This file 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 avahi; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
USA.
***/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <ctype.h>
#include <errno.h>
#if defined(__OpenBSD__)
#include <sys/socket.h>
#endif /* __OpenBSD__ */
#include <sys/types.h>
#include <net/if.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <ifaddrs.h>
#include <resolv.h>
#include <stdio.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <time.h>
#include <signal.h>
#include <regex.h>
#include <pthread.h>
#include <glib.h>
#ifdef HAVE_AVAHI
#include <avahi-client/client.h>
#include <avahi-client/lookup.h>
#include <avahi-glib/glib-watch.h>
#include <avahi-common/malloc.h>
#include <avahi-common/error.h>
#endif /* HAVE_AVAHI */
#include <gio/gio.h>
#ifdef HAVE_LDAP
# ifdef __sun
# include <lber.h>
# endif /* __sun */
# include <ldap.h>
# ifdef HAVE_LDAP_SSL_H
# include <ldap_ssl.h>
# endif /* HAVE_LDAP_SSL_H */
#endif /* HAVE_LDAP */
#ifdef HAVE_LDAP
LDAP *BrowseLDAPHandle = NULL;
/* Handle to LDAP server */
char *BrowseLDAPBindDN = NULL,
/* LDAP login DN */
*BrowseLDAPDN = NULL,
/* LDAP search DN */
*BrowseLDAPPassword = NULL,
/* LDAP login password */
*BrowseLDAPServer = NULL,
/* LDAP server to use */
*BrowseLDAPFilter = NULL;
/* LDAP query filter */
int BrowseLDAPUpdate = TRUE,
/* enables LDAP updates */
BrowseLDAPInitialised = FALSE;
/* the init stuff has been done */
# ifdef HAVE_LDAP_SSL
char *BrowseLDAPCACertFile = NULL;
/* LDAP CA CERT file to use */
# endif /* HAVE_LDAP_SSL */
#endif /* HAVE_LDAP */
#ifdef HAVE_LDAP
#define LDAP_BROWSE_FILTER "(objectclass=cupsPrinter)"
static LDAP *ldap_connect(void);
static LDAP *ldap_reconnect(void);
static void ldap_disconnect(LDAP *ld);
static int ldap_search_rec(LDAP *ld, char *base, int scope,
char *filter, char *attrs[],
int attrsonly, LDAPMessage **res);
static int ldap_getval_firststring(LDAP *ld, LDAPMessage *entry,
char *attr, char *retval,
unsigned long maxsize);
static void ldap_freeres(LDAPMessage *entry);
# ifdef HAVE_LDAP_REBIND_PROC
# if defined(LDAP_API_FEATURE_X_OPENLDAP) && (LDAP_API_VERSION > 2000)
static int ldap_rebind_proc(LDAP *RebindLDAPHandle,
LDAP_CONST char *refsp,
ber_tag_t request,
ber_int_t msgid,
void *params);
# else
static int ldap_rebind_proc(LDAP *RebindLDAPHandle,
char **dnp,
char **passwdp,
int *authmethodp,
int freeit,
void *arg);
# endif /* defined(LDAP_API_FEATURE_X_OPENLDAP) && (LDAP_API_VERSION > 2000) */
# endif /* HAVE_LDAP_REBIND_PROC */
#endif /* HAVE_LDAP */
#include <cups/cups.h>
#include <cups/ppd.h>
#include <cups/raster.h>
#include <cupsfilters/ipp.h>
#include <cupsfilters/ppdgenerator.h>
#include "cups-notifier.h"
/* Attribute to mark a CUPS queue as created by us */
#define CUPS_BROWSED_MARK "cups-browsed"
#define AUTO_OPTION "auto"
/* Attribute to tell the implicitclass backend the destination queue for
the current job */
#define CUPS_BROWSED_DEST_PRINTER "cups-browsed-dest-printer"
/* Timeout values in sec */
#define TIMEOUT_IMMEDIATELY -1
#define TIMEOUT_CONFIRM 10
#define TIMEOUT_RETRY 10
#define TIMEOUT_REMOVE -1
#define TIMEOUT_CHECK_LIST 2
#define NOTIFY_LEASE_DURATION (24 * 60 * 60)
#define CUPS_DBUS_NAME "org.cups.cupsd.Notifier"
#define CUPS_DBUS_PATH "/org/cups/cupsd/Notifier"
#define CUPS_DBUS_INTERFACE "org.cups.cupsd.Notifier"
#define DEFAULT_CACHEDIR "/var/cache/cups"
#define DEFAULT_LOGDIR "/var/log/cups"
#define LOCAL_DEFAULT_PRINTER_FILE "/cups-browsed-local-default-printer"
#define REMOTE_DEFAULT_PRINTER_FILE "/cups-browsed-remote-default-printer"
#define SAVE_OPTIONS_FILE "/cups-browsed-options-%s"
#define DEBUG_LOG_FILE "/cups-browsed_log"
/* Status of remote printer */
typedef enum printer_status_e {
STATUS_UNCONFIRMED = 0, /* Generated in a previous session */
STATUS_CONFIRMED, /* Avahi confirms UNCONFIRMED printer */
STATUS_TO_BE_CREATED, /* Scheduled for creation */
STATUS_DISAPPEARED, /* Scheduled for removal */
STATUS_TO_BE_RELEASED /* Scheduled for release from cups-browsed */
} printer_status_t;
/* Data structure for taking note of each time the remote printer
appears as a discovered IPP service */
typedef struct ipp_discovery_s {
char *interface;
char *type;
int family;
} ipp_discovery_t;
/* Data structure for remote printers */
typedef struct remote_printer_s {
char *queue_name;
char *location;
char *info;
char *uri;
char *make_model;
char *pdl;
int color;
int duplex;
ipp_t *prattrs;
char *nickname;
int num_options;
cups_option_t *options;
printer_status_t status;
time_t timeout;
void *slave_of;
int last_printer;
char *host;
char *ip;
int port;
char *service_name;
char *type;
char *domain;
cups_array_t *ipp_discoveries;
int no_autosave;
int overwritten;
int netprinter;
int is_legacy;
int timeouted;
} remote_printer_t;
/* Data structure for network interfaces */
typedef struct netif_s {
char *address;
http_addr_t broadcast;
} netif_t;
/* Data structures for browse allow/deny rules */
typedef enum browse_order_e {
ORDER_ALLOW_DENY,
ORDER_DENY_ALLOW
} browse_order_t;
typedef enum allow_type_e {
ALLOW_IP,
ALLOW_NET,
ALLOW_INVALID
} allow_type_t;
typedef enum allow_sense_e {
ALLOW_ALLOW,
ALLOW_DENY
} allow_sense_t;
typedef struct allow_s {
allow_type_t type;
allow_sense_t sense;
http_addr_t addr;
http_addr_t mask;
} allow_t;
/* Data structures for browse filter rules */
typedef enum filter_sense_s {
FILTER_MATCH,
FILTER_NOT_MATCH
} filter_sense_t;
typedef struct browse_filter_s {
filter_sense_t sense;
char *field;
char *regexp;
regex_t *cregexp;
} browse_filter_t;
/* Data structure for a printer discovered using BrowsePoll */
typedef struct browsepoll_printer_s {
char *uri_supported;
char *location;
char *info;
} browsepoll_printer_t;
/* Data structure for a BrowsePoll server */
typedef struct browsepoll_s {
char *server;
int port;
int major;
int minor;
gboolean can_subscribe;
int subscription_id;
int sequence_number;
/* Remember which printers we discovered. This way we can just ask
* if anything has changed, and if not we know these printers are
* still there. */
GList *printers; /* of browsepoll_printer_t */
} browsepoll_t;
/* Data structure for destination list obtained with cupsEnumDests() */
typedef struct dest_list_s {
int num_dests;
cups_dest_t *dests;
} dest_list_t;
/* Local printer (key is name) */
typedef struct local_printer_s {
char *device_uri;
char *uuid;
gboolean cups_browsed_controlled;
} local_printer_t;
/* Browse data to send for local printer */
typedef struct browse_data_s {
int type;
int state;
char *uri;
char *location;
char *info;
char *make_model;
char *browse_options;
} browse_data_t;
/* Data structure for manual definition of load-balancing clusters */
typedef struct cluster_s {
char *local_queue_name;
cups_array_t *members;
} cluster_t;
/* Ways how to represent the remote printer's IP in the device URI */
typedef enum ip_based_uris_e {
IP_BASED_URIS_NO,
IP_BASED_URIS_ANY,
IP_BASED_URIS_IPV4_ONLY,
IP_BASED_URIS_IPV6_ONLY
} ip_based_uris_t;
/* Ways how to name local queues for remote printers */
typedef enum local_queue_naming_e {
LOCAL_QUEUE_NAMING_DNSSD,
LOCAL_QUEUE_NAMING_MAKE_MODEL,
LOCAL_QUEUE_NAMING_REMOTE_NAME
} local_queue_naming_t;
/* Automatically create queues for IPP network printers: No, only for
IPP printers, for all printers */
typedef enum create_ipp_printer_queues_e {
IPP_PRINTERS_NO,
IPP_PRINTERS_LOCAL_ONLY,
IPP_PRINTERS_PWGRASTER,
IPP_PRINTERS_APPLERASTER,
IPP_PRINTERS_PCLM,
IPP_PRINTERS_PDF,
IPP_PRINTERS_DRIVERLESS,
IPP_PRINTERS_ALL
} create_ipp_printer_queues_t;
/* Ways how to set up a queue for an IPP network printer */
typedef enum ipp_queue_type_e {
PPD_YES,
PPD_NO
} ipp_queue_type_t;
/* Ways how we can do load balancing on remote queues with the same name */
typedef enum load_balancing_type_e {
QUEUE_ON_CLIENT,
QUEUE_ON_SERVERS
} load_balancing_type_t;
/* Ways how inactivity for auto-shutdown is defined */
typedef enum autoshutdown_inactivity_type_e {
NO_QUEUES,
NO_JOBS
} autoshutdown_inactivity_type_t;
typedef struct media_size_s{
int x;
int y;
}media_size_t;
typedef struct pagesize_range_s{
int x_dim_min;
int x_dim_max;
int y_dim_min;
int y_dim_max;
}pagesize_range_t;
typedef struct media_col_s{
int x,y,top_margin,bottom_margin,left_margin,right_margin;
char *media_source,*media_type;
}media_col_t;
typedef struct default_str_attribute_s{
char* value;
int count;
}default_str_attribute_t;
typedef struct resolution_count_s{
res_t *res;
int count;
}resolution_count_t;
typedef struct mediacol_count_s{
media_col_t *data;
int count;
}mediacol_count_t;
typedef struct pagesize_count_s{
char* pagesize;
int count;
}pagesize_count_t;
cups_array_t *remote_printers;
static char *alt_config_file = NULL;
static cups_array_t *command_line_config;
static cups_array_t *netifs;
static cups_array_t *local_hostnames;
static cups_array_t *browseallow;
static gboolean browseallow_all = FALSE;
static gboolean browsedeny_all = FALSE;
static browse_order_t browse_order;
static cups_array_t *browsefilter;
static GHashTable *local_printers;
static GHashTable *cups_supported_remote_printers;
static browsepoll_t *local_printers_context = NULL;
static http_t *local_conn = NULL;
static gboolean inhibit_local_printers_update = FALSE;
static GList *browse_data = NULL;
static CupsNotifier *cups_notifier = NULL;
static GMainLoop *gmainloop = NULL;
#ifdef HAVE_AVAHI
static AvahiGLibPoll *glib_poll = NULL;
static AvahiClient *client = NULL;
static AvahiServiceBrowser *sb1 = NULL, *sb2 = NULL;
static int avahi_present = 0;
#endif /* HAVE_AVAHI */
#ifdef HAVE_LDAP
static const char * const ldap_attrs[] =/* CUPS LDAP attributes */
{
"printerDescription",
"printerLocation",
"printerMakeAndModel",
"printerType",
"printerURI",
NULL
};
#endif /* HAVE_LDAP */
static guint queues_timer_id = 0;
static int browsesocket = -1;
#define BROWSE_DNSSD (1<<0)
#define BROWSE_CUPS (1<<1)
#define BROWSE_LDAP (1<<2)
static unsigned int BrowseLocalProtocols = 0;
static unsigned int BrowseRemoteProtocols = BROWSE_DNSSD;
static unsigned int BrowseInterval = 60;
static unsigned int BrowseTimeout = 300;
static uint16_t BrowsePort = 631;
static browsepoll_t **BrowsePoll = NULL;
static size_t NumBrowsePoll = 0;
static guint update_netifs_sourceid = 0;
static char local_server_str[1024];
static char *DomainSocket = NULL;
static unsigned int HttpLocalTimeout = 5;
static unsigned int HttpRemoteTimeout = 10;
static unsigned int HttpMaxRetries = 5;
static unsigned int DNSSDBasedDeviceURIs = 1;
static ip_based_uris_t IPBasedDeviceURIs = IP_BASED_URIS_NO;
static local_queue_naming_t LocalQueueNamingRemoteCUPS=LOCAL_QUEUE_NAMING_DNSSD;
static local_queue_naming_t LocalQueueNamingIPPPrinter=LOCAL_QUEUE_NAMING_DNSSD;
static unsigned int OnlyUnsupportedByCUPS = 0;
static unsigned int UseCUPSGeneratedPPDs = 0;
static unsigned int CreateRemoteRawPrinterQueues = 0;
static unsigned int CreateRemoteCUPSPrinterQueues = 1;
#ifdef ONLY_LOCAL_IPP_PRINTERS_AUTO_SETUP
static create_ipp_printer_queues_t CreateIPPPrinterQueues = IPP_PRINTERS_LOCAL_ONLY;
#else
#ifdef ONLY_DRIVERLESS_IPP_PRINTERS_AUTO_SETUP
static create_ipp_printer_queues_t CreateIPPPrinterQueues = IPP_PRINTERS_DRIVERLESS;
#else
static create_ipp_printer_queues_t CreateIPPPrinterQueues = IPP_PRINTERS_ALL;
#endif
#endif
static ipp_queue_type_t IPPPrinterQueueType = PPD_YES;
static int NewIPPPrinterQueuesShared = 0;
static int AutoClustering = 1;
static cups_array_t *clusters;
static load_balancing_type_t LoadBalancingType = QUEUE_ON_CLIENT;
static char *DefaultOptions = NULL;
static int update_cups_queues_max_per_call = 10;
static int pause_between_cups_queue_updates = 1;
static remote_printer_t *deleted_master = NULL;
static int terminating = 0; /* received SIGTERM, ignore callbacks,
break loops */
static int in_shutdown = 0;
static int autoshutdown = 0;
static int autoshutdown_avahi = 0;
static int autoshutdown_timeout = 30;
static autoshutdown_inactivity_type_t autoshutdown_on = NO_QUEUES;
static guint autoshutdown_exec_id = 0;
static const char *default_printer = NULL;
static int debug_stderr = 0;
static int debug_logfile = 0;
static FILE *lfp = NULL;
static char cachedir[1024];
static char logdir[1024];
static char local_default_printer_file[2048];
static char remote_default_printer_file[2048];
static char save_options_file[2048];
static char debug_log_file[2048];
/*Contains ppd keywords which are written by ppdgenerator.c in the ppd file.*/
static char* ppd_keywords[] =
{
"PageSize",
"PageRegion",
"InputSlot",
"MediaType",
"ColorModel",
"Duplex",
"OutputBin",
"StapleLocation",
"FoldType",
"PunchMedia",
"Booklet",
"cupsFinishingTemplate",
"cupsPrintQuality",
"print-content-optimize",
"print-rendering-intent",
"print-scaling",
};
/* Static global variable for indicating we have reached the HTTP timeout */
static int timeout_reached = 0;
static void recheck_timer (void);
static void browse_poll_create_subscription (browsepoll_t *context,
http_t *conn);
static gboolean browse_poll_get_notifications (browsepoll_t *context,
http_t *conn);
static remote_printer_t
*examine_discovered_printer_record(const char *host,
const char *ip,
uint16_t port,
char *resource,
const char *service_name,
const char *location,
const char *info,
const char *type,
const char *domain,
const char *interface,
int family,
void *txt);
#if (CUPS_VERSION_MAJOR > 1) || (CUPS_VERSION_MINOR > 5)
#define HAVE_CUPS_1_6 1
#endif
/*
* Option 'printer-is-shared' cannot be set on remote CUPS
* queue and requests for setting it ends with error since
* 2.1.1. Define HAVE_CUPS_2_2 to do not send IPP request
* for setting 'printer-is-shared' option on remote CUPS queues
* for newer versions of CUPS.
*/
#if (CUPS_VERSION_MAJOR > 2) || (CUPS_VERSION_MINOR > 1)
#define HAVE_CUPS_2_2 1
#else
#define HAVE_CUPS_2_2 0
#endif
/*
* CUPS 1.6 makes various structures private and
* introduces these ippGet and ippSet functions
* for all of the fields in these structures.
* http://www.cups.org/str.php?L3928
* We define (same signatures) our own accessors when CUPS < 1.6.
*/
#ifndef HAVE_CUPS_1_6
const char *
ippGetName(ipp_attribute_t *attr)
{
return (attr->name);
}
ipp_op_t
ippGetOperation(ipp_t *ipp)
{
return (ipp->request.op.operation_id);
}
ipp_status_t
ippGetStatusCode(ipp_t *ipp)
{
return (ipp->request.status.status_code);
}
ipp_tag_t
ippGetGroupTag(ipp_attribute_t *attr)
{
return (attr->group_tag);
}
ipp_tag_t
ippGetValueTag(ipp_attribute_t *attr)
{
return (attr->value_tag);
}
int
ippGetCount(ipp_attribute_t *attr)
{
return (attr->num_values);
}
int
ippGetInteger(ipp_attribute_t *attr,
int element)
{
return (attr->values[element].integer);
}
int
ippGetBoolean(ipp_attribute_t *attr,
int element)
{
return (attr->values[element].boolean);
}
const char *
ippGetString(ipp_attribute_t *attr,
int element,
const char **language)
{
return (attr->values[element].string.text);
}
ipp_attribute_t *
ippFirstAttribute(ipp_t *ipp)
{
if (!ipp)
return (NULL);
return (ipp->current = ipp->attrs);
}
ipp_attribute_t *
ippNextAttribute(ipp_t *ipp)
{
if (!ipp || !ipp->current)
return (NULL);
return (ipp->current = ipp->current->next);
}
int
ippSetVersion(ipp_t *ipp, int major, int minor)
{
if (!ipp || major < 0 || minor < 0)
return (0);
ipp->request.any.version[0] = major;
ipp->request.any.version[1] = minor;
return (1);
}
#endif
#if (CUPS_VERSION_MAJOR > 1) || (CUPS_VERSION_MINOR > 6)
#define HAVE_CUPS_1_7 1
#endif
/*
* The httpAddrPort() function was only introduced in CUPS 1.7.x
*/
#ifndef HAVE_CUPS_1_7
int /* O - Port number */
httpAddrPort(http_addr_t *addr) /* I - Address */
{
if (!addr)
return (-1);
#ifdef AF_INET6
else if (addr->addr.sa_family == AF_INET6)
return (ntohs(addr->ipv6.sin6_port));
#endif /* AF_INET6 */
else if (addr->addr.sa_family == AF_INET)
return (ntohs(addr->ipv4.sin_port));
else
return (0);
}
#endif
#if (CUPS_VERSION_MAJOR > 1)
#define HAVE_CUPS_2_0 1
#endif
void
start_debug_logging()
{
if (debug_log_file[0] == '\0')
return;
if (lfp == NULL)
lfp = fopen(debug_log_file, "a+");
if (lfp == NULL) {
fprintf(stderr, "cups-browsed: ERROR: Failed creating debug log file %s\n",
debug_log_file);
exit(1);
}
}
void
stop_debug_logging()
{
debug_logfile = 0;
if (lfp)
fclose(lfp);
lfp = NULL;
}
void
debug_printf(const char *format, ...) {
if (debug_stderr || debug_logfile) {
time_t curtime = time(NULL);
char buf[64];
ctime_r(&curtime, buf);
while(isspace(buf[strlen(buf)-1])) buf[strlen(buf)-1] = '\0';
va_list arglist;
if (debug_stderr) {
va_start(arglist, format);
fprintf(stderr, "%s ", buf);
vfprintf(stderr, format, arglist);
fflush(stderr);
va_end(arglist);
}
if (debug_logfile && lfp) {
va_start(arglist, format);
fprintf(lfp, "%s ", buf);
vfprintf(lfp, format, arglist);
fflush(lfp);
va_end(arglist);
}
}
}
void
debug_log_out(char *log) {
if (debug_stderr || debug_logfile) {
time_t curtime = time(NULL);
char buf[64];
char *ptr1, *ptr2;
ctime_r(&curtime, buf);
while(isspace(buf[strlen(buf)-1])) buf[strlen(buf)-1] = '\0';
ptr1 = log;
while(ptr1) {
ptr2 = strchr(ptr1, '\n');
if (ptr2) *ptr2 = '\0';
if (debug_stderr)
fprintf(stderr, "%s %s\n", buf, ptr1);
if (debug_logfile && lfp)
fprintf(lfp, "%s %s\n", buf, ptr1);
if (ptr2) *ptr2 = '\n';
ptr1 = ptr2 ? (ptr2 + 1) : NULL;
}
}
}
/*
* 'create_media_size()' - Create a media-size value.
*/
static ipp_t * /* O - media-col collection */
create_media_size(int width, /* I - x-dimension in 2540ths */
int length) /* I - y-dimension in 2540ths */
{
ipp_t *media_size = ippNew(); /* media-size value */
ippAddInteger(media_size, IPP_TAG_PRINTER, IPP_TAG_INTEGER, "x-dimension",
width);
ippAddInteger(media_size, IPP_TAG_PRINTER, IPP_TAG_INTEGER, "y-dimension",
length);
return (media_size);
}
/*
* 'create_media_range()' - Create a pagesize-range value.
*/
static ipp_t *
create_media_range(int x_dim_min_width,
int x_dim_max_width,
int y_dim_min_height,
int y_dim_max_height)
{
ipp_t *media_size = ippNew();
ippAddRange(media_size, IPP_TAG_PRINTER, "x-dimension",
x_dim_min_width, x_dim_max_width);
ippAddRange(media_size, IPP_TAG_PRINTER, "y-dimension",
y_dim_min_height, y_dim_max_height);
return (media_size);
}
void *
copy_media_size(void *size, void *user_data)
{
media_size_t *data = (media_size_t *)size;
media_size_t *copy;
copy = (media_size_t *)calloc(1, sizeof(media_size_t));
if (copy) {
copy->x = data->x;
copy->y = data->y;
}
return copy;
}
void*
copy_range_size(void *range, void* user_data)
{
pagesize_range_t *data = (pagesize_range_t *)range;
pagesize_range_t *copy;
copy = (pagesize_range_t *)calloc(1,sizeof(pagesize_range_t));
if (copy) {
copy->x_dim_min = data->x_dim_min;
copy->x_dim_max = data->x_dim_max;
copy->y_dim_min = data->y_dim_min;
copy->y_dim_max = data->y_dim_max;
}
return copy;
}
void *
copy_media(void *media, void *user_data)
{
media_col_t *data = (media_col_t *)media;
media_col_t *copy;
copy = (media_col_t *)calloc(1, sizeof(media_col_t));
if (copy) {
copy->x = data->x;
copy->y = data->y;
copy->left_margin=data->left_margin;
copy->right_margin=data->right_margin;
copy->top_margin=data->top_margin;
copy->bottom_margin=data->bottom_margin;
copy->media_source = NULL;
copy->media_type = NULL;
if (data->media_source != NULL) {
copy->media_source = (char *)malloc(sizeof(char)*32);
strcpy(copy->media_source,data->media_source);
}
if (data->media_type != NULL) {
copy->media_type = (char *)malloc(sizeof(char)*32);;
strcpy(copy->media_type,data->media_type);
}
}
return copy;
}
void* copy_media_count(void *media, void* user_data)
{
mediacol_count_t *prev = (mediacol_count_t *)media;
mediacol_count_t *copy;
copy = (mediacol_count_t*)calloc(1,sizeof(mediacol_count_t));
if (copy) {
copy->data = copy_media(prev->data,NULL);
copy->count = prev->count;
}
return copy;
}
void* copy_pagesize_count(void *pagesize_count, void* user_data)
{
pagesize_count_t *prev = (pagesize_count_t *)pagesize_count;
pagesize_count_t *copy;
copy = (pagesize_count_t*)calloc(1,sizeof(pagesize_count_t));
copy->pagesize = malloc(sizeof(char)*32);
if (copy) {
strcpy(copy->pagesize,prev->pagesize);
copy->count = prev->count;
}
return copy;
}
int compare_pagesize_count(void* pagesize_a, void* pagesize_b,void* user_data)
{
pagesize_count_t *a = (pagesize_count_t *) pagesize_a;
pagesize_count_t *b = (pagesize_count_t *) pagesize_b;
if (!strcmp(a->pagesize,b->pagesize))
return 0;
return 1;
}
/*
* 'create_media_col()' - Create a media-col value.
*/
static ipp_t *
create_media_col(int width,
int length,
int left_margin,
int right_margin,
int top_margin,
int bottom_margin,
char *media_source,
char *media_type)
{
ipp_t *media_col = ippNew(), /* media-col value */
*media_size = create_media_size(width, length);
ippAddCollection(media_col, IPP_TAG_PRINTER, "media-size", media_size);
ippAddInteger(media_col, IPP_TAG_PRINTER, IPP_TAG_INTEGER,
"media-bottom-margin",bottom_margin);
ippAddInteger(media_col, IPP_TAG_PRINTER, IPP_TAG_INTEGER,
"media-left-margin", left_margin);
ippAddInteger(media_col, IPP_TAG_PRINTER, IPP_TAG_INTEGER,
"media-right-margin",right_margin);
ippAddInteger(media_col, IPP_TAG_PRINTER, IPP_TAG_INTEGER,
"media-top-margin", top_margin);
if (media_source != NULL)
ippAddString(media_col, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
"media-source", NULL,media_source);
if (media_type != NULL)
ippAddString(media_col, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
"media-type", NULL,media_type);
ippDelete(media_size);
return (media_col);
}
int
compare_mediasize(void *media_a, void *media_b,
void *user_data)
{
media_size_t *a=(media_size_t *)media_a;
media_size_t *b=(media_size_t *)media_b;
if (a->x < b->x)
return -1;
else if (a->x > b->x)
return 1;
else{
if (a->y == b->y)
return 0;
else if (a->y < b->y)
return -1;
return 1;
}
}
int compare_int(int a, int b)
{
if (a < b)
return -1;
else if (a > b)
return 1;
return 0;
}
int compare_rangesize(void *range_a,void *range_b,
void *user_data)
{
pagesize_range_t *a = (pagesize_range_t *)range_a;
pagesize_range_t *b = (pagesize_range_t *)range_b;
int value;
if ((value = compare_int(a->x_dim_min, b->x_dim_min)) == 0) {
if ((value = compare_int(a->x_dim_max, b->x_dim_max)) == 0) {
if ((value = compare_int(a->y_dim_min, b->y_dim_min)) == 0) {
if ((value = compare_int(a->y_dim_max, b->y_dim_max)) == 0) {
return 0;
}
}
}
}
return value;
}
int compare_media(void *media_a, void *media_b,
void *user_data)
{
media_col_t *a=(media_col_t *)media_a;
media_col_t *b=(media_col_t *)media_b;
int value;
if ((value = compare_int(a->x, b->x)) == 0) {
if ((value = compare_int(a->y, b->y)) == 0) {
if ((value = compare_int(a->top_margin, b->top_margin)) == 0) {
if ((value = compare_int(a->bottom_margin, b->bottom_margin)) == 0) {
if ((value = compare_int(a->right_margin, b->right_margin)) == 0) {
if ((value = compare_int(a->left_margin, b->left_margin)) == 0) {
if (a->media_source == NULL && b->media_source == NULL) {
if (a->media_type == NULL && b->media_type == NULL)
return 0;
if (a->media_type == NULL)
return -1;
if (b->media_type == NULL)
return 1;
return strcmp(a->media_type, b->media_type);
}
if (a->media_source == NULL)
return -1;
if (b->media_source == NULL)
return 1;
if (!strcmp(a->media_source, b->media_source)) {
if (a->media_type==NULL && b->media_type == NULL)
return 0;
if (a->media_type==NULL)
return -1;
if (b->media_type==NULL)
return 1;
return strcmp(a->media_type, b->media_type);
}
else
return strcmp(a->media_source, b->media_source);
}
}
}
}
}
}