2 * SNMP discovery backend for CUPS.
4 * Copyright 2007-2014 by Apple Inc.
5 * Copyright 2006-2007 by Easy Software Products, all rights reserved.
7 * These coded instructions, statements, and computer programs are the
8 * property of Apple Inc. and are protected by Federal copyright
9 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
10 * "LICENSE" which should have been included with this file. If this
11 * file is missing or damaged, see the license at "http://www.cups.org/".
15 * Include necessary headers.
18 #include "backend-private.h"
19 #include <cups/array.h>
20 #include <cups/file.h>
21 #include <cups/http-private.h>
26 * This backend implements SNMP printer discovery. It uses a broadcast-
27 * based approach to get SNMP response packets from potential printers,
28 * requesting OIDs from the Host and Port Monitor MIBs, does a URI
29 * lookup based on the device description string, and finally a probe of
30 * port 9100 (AppSocket) and 515 (LPD).
32 * The current focus is on printers with internal network cards, although
33 * the code also works with many external print servers as well.
35 * The backend reads the snmp.conf file from the CUPS_SERVERROOT directory
36 * which can contain comments, blank lines, or any number of the following
44 * DeviceURI "regex pattern" uri
49 * The default is to use:
57 * This backend is known to work with the following network printers and
60 * Axis OfficeBasic, 5400, 5600
70 * It does not currently work with:
77 * (for all of these, they do not support the Host MIB)
84 enum /**** Request IDs for each field ****/
94 typedef struct device_uri_s
/**** DeviceURI values ****/
96 regex_t re
; /* Regular expression to match */
97 cups_array_t
*uris
; /* URIs */
100 typedef struct snmp_cache_s
/**** SNMP scan cache ****/
102 http_addr_t address
; /* Address of device */
103 char *addrname
, /* Name of device */
104 *uri
, /* device-uri */
106 *info
, /* device-info */
107 *location
, /* device-location */
108 *make_and_model
; /* device-make-and-model */
109 int sent
; /* Has this device been listed? */
117 static char *add_array(cups_array_t
*a
, const char *s
);
118 static void add_cache(http_addr_t
*addr
, const char *addrname
,
119 const char *uri
, const char *id
,
120 const char *make_and_model
);
121 static device_uri_t
*add_device_uri(char *value
);
122 static void alarm_handler(int sig
);
123 static int compare_cache(snmp_cache_t
*a
, snmp_cache_t
*b
);
124 static void debug_printf(const char *format
, ...);
125 static void fix_make_model(char *make_model
,
126 const char *old_make_model
,
127 int make_model_size
);
128 static void free_array(cups_array_t
*a
);
129 static void free_cache(void);
130 static http_addrlist_t
*get_interface_addresses(const char *ifname
);
131 static void list_device(snmp_cache_t
*cache
);
132 static const char *password_cb(const char *prompt
);
133 static void probe_device(snmp_cache_t
*device
);
134 static void read_snmp_conf(const char *address
);
135 static void read_snmp_response(int fd
);
136 static double run_time(void);
137 static void scan_devices(int ipv4
, int ipv6
);
138 static int try_connect(http_addr_t
*addr
, const char *addrname
,
140 static void update_cache(snmp_cache_t
*device
, const char *uri
,
141 const char *id
, const char *make_model
);
148 static cups_array_t
*Addresses
= NULL
;
149 static cups_array_t
*Communities
= NULL
;
150 static cups_array_t
*Devices
= NULL
;
151 static int DebugLevel
= 0;
152 static const int DescriptionOID
[] = { CUPS_OID_hrDeviceDescr
, 1, -1 };
153 static const int LocationOID
[] = { CUPS_OID_sysLocation
, 0, -1 };
154 static const int DeviceTypeOID
[] = { CUPS_OID_hrDeviceType
, 1, -1 };
155 static const int DeviceIdOID
[] = { CUPS_OID_ppmPrinterIEEE1284DeviceId
, 1, -1 };
156 static const int UriOID
[] = { CUPS_OID_ppmPortServiceNameOrURI
, 1, 1, -1 };
157 static const int LexmarkProductOID
[] = { 1,3,6,1,4,1,641,2,1,2,1,2,1,-1 };
158 static const int LexmarkProductOID2
[] = { 1,3,6,1,4,1,674,10898,100,2,1,2,1,2,1,-1 };
159 static const int LexmarkDeviceIdOID
[] = { 1,3,6,1,4,1,641,2,1,2,1,3,1,-1 };
160 static const int XeroxProductOID
[] = { 1,3,6,1,4,1,128,2,1,3,1,2,0,-1 };
161 static cups_array_t
*DeviceURIs
= NULL
;
162 static int HostNameLookups
= 0;
163 static int MaxRunTime
= 120;
164 static struct timeval StartTime
;
168 * 'main()' - Discover printers via SNMP.
171 int /* O - Exit status */
172 main(int argc
, /* I - Number of command-line arguments (6 or 7) */
173 char *argv
[]) /* I - Command-line arguments */
175 int ipv4
, /* SNMP IPv4 socket */
176 ipv6
; /* SNMP IPv6 socket */
177 #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
178 struct sigaction action
; /* Actions for POSIX signals */
179 #endif /* HAVE_SIGACTION && !HAVE_SIGSET */
183 * Check command-line options...
188 _cupsLangPuts(stderr
, _("Usage: snmp [host-or-ip-address]"));
193 * Set the password callback for IPP operations...
196 cupsSetPasswordCB(password_cb
);
199 * Catch SIGALRM signals...
203 sigset(SIGALRM
, alarm_handler
);
204 #elif defined(HAVE_SIGACTION)
205 memset(&action
, 0, sizeof(action
));
207 sigemptyset(&action
.sa_mask
);
208 sigaddset(&action
.sa_mask
, SIGALRM
);
209 action
.sa_handler
= alarm_handler
;
210 sigaction(SIGALRM
, &action
, NULL
);
212 signal(SIGALRM
, alarm_handler
);
213 #endif /* HAVE_SIGSET */
216 * Open the SNMP socket...
219 if ((ipv4
= _cupsSNMPOpen(AF_INET
)) < 0)
223 if ((ipv6
= _cupsSNMPOpen(AF_INET6
)) < 0)
224 perror("DEBUG: Unable to create IPv6 socket");
227 #endif /* AF_INET6 */
230 * Read the configuration file and any cache data...
233 read_snmp_conf(argv
[1]);
235 _cupsSNMPSetDebug(DebugLevel
);
237 Devices
= cupsArrayNew((cups_array_func_t
)compare_cache
, NULL
);
240 * Scan for devices...
243 scan_devices(ipv4
, ipv6
);
246 * Close, free, and return with no errors...
249 _cupsSNMPClose(ipv4
);
251 _cupsSNMPClose(ipv6
);
253 free_array(Addresses
);
254 free_array(Communities
);
262 * 'add_array()' - Add a string to an array.
265 static char * /* O - New string */
266 add_array(cups_array_t
*a
, /* I - Array */
267 const char *s
) /* I - String to add */
269 char *dups
; /* New string */
274 cupsArrayAdd(a
, dups
);
281 * 'add_cache()' - Add a cached device...
285 add_cache(http_addr_t
*addr
, /* I - Device IP address */
286 const char *addrname
, /* I - IP address or name string */
287 const char *uri
, /* I - Device URI */
288 const char *id
, /* I - 1284 device ID */
289 const char *make_and_model
) /* I - Make and model */
291 snmp_cache_t
*temp
; /* New device entry */
294 debug_printf("DEBUG: add_cache(addr=%p, addrname=\"%s\", uri=\"%s\", "
295 "id=\"%s\", make_and_model=\"%s\")\n",
296 addr
, addrname
, uri
? uri
: "(null)", id
? id
: "(null)",
297 make_and_model
? make_and_model
: "(null)");
299 temp
= calloc(1, sizeof(snmp_cache_t
));
300 memcpy(&(temp
->address
), addr
, sizeof(temp
->address
));
302 temp
->addrname
= strdup(addrname
);
305 temp
->uri
= strdup(uri
);
308 temp
->id
= strdup(id
);
311 temp
->make_and_model
= strdup(make_and_model
);
313 cupsArrayAdd(Devices
, temp
);
321 * 'add_device_uri()' - Add a device URI to the cache.
323 * The value string is modified (chopped up) as needed.
326 static device_uri_t
* /* O - Device URI */
327 add_device_uri(char *value
) /* I - Value from snmp.conf */
329 device_uri_t
*device_uri
; /* Device URI */
330 char *start
; /* Start of value */
334 * Allocate memory as needed...
338 DeviceURIs
= cupsArrayNew(NULL
, NULL
);
343 if ((device_uri
= calloc(1, sizeof(device_uri_t
))) == NULL
)
346 if ((device_uri
->uris
= cupsArrayNew(NULL
, NULL
)) == NULL
)
353 * Scan the value string for the regular expression and URI(s)...
356 value
++; /* Skip leading " */
358 for (start
= value
; *value
&& *value
!= '\"'; value
++)
359 if (*value
== '\\' && value
[1])
360 _cups_strcpy(value
, value
+ 1);
364 fputs("ERROR: Missing end quote for DeviceURI!\n", stderr
);
366 cupsArrayDelete(device_uri
->uris
);
374 if (regcomp(&(device_uri
->re
), start
, REG_EXTENDED
| REG_ICASE
))
376 fputs("ERROR: Bad regular expression for DeviceURI!\n", stderr
);
378 cupsArrayDelete(device_uri
->uris
);
386 while (isspace(*value
& 255))
392 for (start
= value
; *value
&& !isspace(*value
& 255); value
++);
397 cupsArrayAdd(device_uri
->uris
, strdup(start
));
401 * Add the device URI to the list and return it...
404 cupsArrayAdd(DeviceURIs
, device_uri
);
411 * 'alarm_handler()' - Handle alarm signals...
415 alarm_handler(int sig
) /* I - Signal number */
423 #if !defined(HAVE_SIGSET) && !defined(HAVE_SIGACTION)
424 signal(SIGALRM
, alarm_handler
);
425 #endif /* !HAVE_SIGSET && !HAVE_SIGACTION */
428 write(2, "DEBUG: ALARM!\n", 14);
433 * 'compare_cache()' - Compare two cache entries.
436 static int /* O - Result of comparison */
437 compare_cache(snmp_cache_t
*a
, /* I - First cache entry */
438 snmp_cache_t
*b
) /* I - Second cache entry */
440 return (_cups_strcasecmp(a
->addrname
, b
->addrname
));
445 * 'debug_printf()' - Display some debugging information.
449 debug_printf(const char *format
, /* I - Printf-style format string */
450 ...) /* I - Additional arguments as needed */
452 va_list ap
; /* Pointer to arguments */
458 va_start(ap
, format
);
459 vfprintf(stderr
, format
, ap
);
465 * 'fix_make_model()' - Fix common problems in the make-and-model string.
470 char *make_model
, /* I - New make-and-model string */
471 const char *old_make_model
, /* I - Old make-and-model string */
472 int make_model_size
) /* I - Size of new string buffer */
474 char *mmptr
; /* Pointer into make-and-model string */
478 * Fix some common problems with the make-and-model string so
479 * that printer driver detection works better...
482 if (!_cups_strncasecmp(old_make_model
, "Hewlett-Packard", 15))
485 * Strip leading Hewlett-Packard and hp prefixes and replace
486 * with a single HP manufacturer prefix...
489 mmptr
= (char *)old_make_model
+ 15;
491 while (isspace(*mmptr
& 255))
494 if (!_cups_strncasecmp(mmptr
, "hp", 2))
498 while (isspace(*mmptr
& 255))
505 strlcpy(make_model
+ 3, mmptr
, (size_t)make_model_size
- 3);
507 else if (!_cups_strncasecmp(old_make_model
, "deskjet", 7))
508 snprintf(make_model
, (size_t)make_model_size
, "HP DeskJet%s", old_make_model
+ 7);
509 else if (!_cups_strncasecmp(old_make_model
, "officejet", 9))
510 snprintf(make_model
, (size_t)make_model_size
, "HP OfficeJet%s", old_make_model
+ 9);
511 else if (!_cups_strncasecmp(old_make_model
, "stylus_pro_", 11))
512 snprintf(make_model
, (size_t)make_model_size
, "EPSON Stylus Pro %s", old_make_model
+ 11);
514 strlcpy(make_model
, old_make_model
, (size_t)make_model_size
);
516 if ((mmptr
= strstr(make_model
, ", Inc.,")) != NULL
)
519 * Strip inc. from name, e.g. "Tektronix, Inc., Phaser 560"
520 * becomes "Tektronix Phaser 560"...
523 _cups_strcpy(mmptr
, mmptr
+ 7);
526 if ((mmptr
= strstr(make_model
, " Network")) != NULL
)
529 * Drop unnecessary informational text, e.g. "Xerox DocuPrint N2025
530 * Network LaserJet - 2.12" becomes "Xerox DocuPrint N2025"...
536 if ((mmptr
= strchr(make_model
, ',')) != NULL
)
539 * Drop anything after a trailing comma...
548 * 'free_array()' - Free an array of strings.
552 free_array(cups_array_t
*a
) /* I - Array */
554 char *s
; /* Current string */
557 for (s
= (char *)cupsArrayFirst(a
); s
; s
= (char *)cupsArrayNext(a
))
565 * 'free_cache()' - Free the array of cached devices.
571 snmp_cache_t
*cache
; /* Cached device */
574 for (cache
= (snmp_cache_t
*)cupsArrayFirst(Devices
);
576 cache
= (snmp_cache_t
*)cupsArrayNext(Devices
))
578 free(cache
->addrname
);
586 if (cache
->make_and_model
)
587 free(cache
->make_and_model
);
592 cupsArrayDelete(Devices
);
598 * 'get_interface_addresses()' - Get the broadcast address(es) associated
602 static http_addrlist_t
* /* O - List of addresses */
603 get_interface_addresses(
604 const char *ifname
) /* I - Interface name */
606 struct ifaddrs
*addrs
, /* Interface address list */
607 *addr
; /* Current interface address */
608 http_addrlist_t
*first
, /* First address in list */
609 *last
, /* Last address in list */
610 *current
; /* Current address */
613 if (getifaddrs(&addrs
) < 0)
616 for (addr
= addrs
, first
= NULL
, last
= NULL
; addr
; addr
= addr
->ifa_next
)
617 if ((addr
->ifa_flags
& IFF_BROADCAST
) && addr
->ifa_broadaddr
&&
618 addr
->ifa_broadaddr
->sa_family
== AF_INET
&&
619 (!ifname
|| !strcmp(ifname
, addr
->ifa_name
)))
621 current
= calloc(1, sizeof(http_addrlist_t
));
623 memcpy(&(current
->addr
), addr
->ifa_broadaddr
,
624 sizeof(struct sockaddr_in
));
629 last
->next
= current
;
641 * 'list_device()' - List a device we found...
645 list_device(snmp_cache_t
*cache
) /* I - Cached device */
648 cupsBackendReport("network", cache
->uri
, cache
->make_and_model
,
649 cache
->info
, cache
->id
, cache
->location
);
654 * 'password_cb()' - Handle authentication requests.
656 * All we do right now is return NULL, indicating that no authentication
660 static const char * /* O - Password (NULL) */
661 password_cb(const char *prompt
) /* I - Prompt message */
663 (void)prompt
; /* Anti-compiler-warning-code */
670 * 'probe_device()' - Probe a device to discover whether it is a printer.
672 * TODO: Try using the Port Monitor MIB to discover the correct protocol
673 * to use - first need a commercially-available printer that supports
678 probe_device(snmp_cache_t
*device
) /* I - Device */
680 char uri
[1024], /* Full device URI */
681 *uriptr
, /* Pointer into URI */
682 *format
; /* Format string for device */
683 device_uri_t
*device_uri
; /* Current DeviceURI match */
686 debug_printf("DEBUG: %.3f Probing %s...\n", run_time(), device
->addrname
);
690 * If the printer supports Bonjour/mDNS, don't report it from the SNMP backend.
693 if (!try_connect(&(device
->address
), device
->addrname
, 5353))
695 debug_printf("DEBUG: %s supports mDNS, not reporting!\n", device
->addrname
);
698 #endif /* __APPLE__ */
701 * Lookup the device in the match table...
704 for (device_uri
= (device_uri_t
*)cupsArrayFirst(DeviceURIs
);
706 device_uri
= (device_uri_t
*)cupsArrayNext(DeviceURIs
))
707 if (device
->make_and_model
&&
708 !regexec(&(device_uri
->re
), device
->make_and_model
, 0, NULL
, 0))
711 * Found a match, add the URIs...
714 for (format
= (char *)cupsArrayFirst(device_uri
->uris
);
716 format
= (char *)cupsArrayNext(device_uri
->uris
))
718 for (uriptr
= uri
; *format
&& uriptr
< (uri
+ sizeof(uri
) - 1);)
719 if (*format
== '%' && format
[1] == 's')
722 * Insert hostname/address...
725 strlcpy(uriptr
, device
->addrname
, sizeof(uri
) - (size_t)(uriptr
- uri
));
726 uriptr
+= strlen(uriptr
);
730 *uriptr
++ = *format
++;
734 update_cache(device
, uri
, NULL
, NULL
);
741 * Then try the standard ports...
744 if (!try_connect(&(device
->address
), device
->addrname
, 9100))
746 debug_printf("DEBUG: %s supports AppSocket!\n", device
->addrname
);
748 snprintf(uri
, sizeof(uri
), "socket://%s", device
->addrname
);
749 update_cache(device
, uri
, NULL
, NULL
);
751 else if (!try_connect(&(device
->address
), device
->addrname
, 515))
753 debug_printf("DEBUG: %s supports LPD!\n", device
->addrname
);
755 snprintf(uri
, sizeof(uri
), "lpd://%s/", device
->addrname
);
756 update_cache(device
, uri
, NULL
, NULL
);
762 * 'read_snmp_conf()' - Read the snmp.conf file.
766 read_snmp_conf(const char *address
) /* I - Single address to probe */
768 cups_file_t
*fp
; /* File pointer */
769 char filename
[1024], /* Filename */
770 line
[1024], /* Line from file */
771 *value
; /* Value on line */
772 int linenum
; /* Line number */
773 const char *cups_serverroot
; /* CUPS_SERVERROOT env var */
774 const char *debug
; /* CUPS_DEBUG_LEVEL env var */
775 const char *runtime
; /* CUPS_MAX_RUN_TIME env var */
779 * Initialize the global address and community lists...
782 Addresses
= cupsArrayNew(NULL
, NULL
);
783 Communities
= cupsArrayNew(NULL
, NULL
);
786 add_array(Addresses
, address
);
788 if ((debug
= getenv("CUPS_DEBUG_LEVEL")) != NULL
)
789 DebugLevel
= atoi(debug
);
791 if ((runtime
= getenv("CUPS_MAX_RUN_TIME")) != NULL
)
792 MaxRunTime
= atoi(runtime
);
795 * Find the snmp.conf file...
798 if ((cups_serverroot
= getenv("CUPS_SERVERROOT")) == NULL
)
799 cups_serverroot
= CUPS_SERVERROOT
;
801 snprintf(filename
, sizeof(filename
), "%s/snmp.conf", cups_serverroot
);
803 if ((fp
= cupsFileOpen(filename
, "r")) != NULL
)
806 * Read the snmp.conf file...
811 while (cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
))
814 fprintf(stderr
, "ERROR: Missing value on line %d of %s!\n", linenum
,
816 else if (!_cups_strcasecmp(line
, "Address"))
819 add_array(Addresses
, value
);
821 else if (!_cups_strcasecmp(line
, "Community"))
822 add_array(Communities
, value
);
823 else if (!_cups_strcasecmp(line
, "DebugLevel"))
824 DebugLevel
= atoi(value
);
825 else if (!_cups_strcasecmp(line
, "DeviceURI"))
829 "ERROR: Missing double quote for regular expression on "
830 "line %d of %s!\n", linenum
, filename
);
832 add_device_uri(value
);
834 else if (!_cups_strcasecmp(line
, "HostNameLookups"))
835 HostNameLookups
= !_cups_strcasecmp(value
, "on") ||
836 !_cups_strcasecmp(value
, "yes") ||
837 !_cups_strcasecmp(value
, "true") ||
838 !_cups_strcasecmp(value
, "double");
839 else if (!_cups_strcasecmp(line
, "MaxRunTime"))
840 MaxRunTime
= atoi(value
);
842 fprintf(stderr
, "ERROR: Unknown directive %s on line %d of %s!\n",
843 line
, linenum
, filename
);
850 * Use defaults if parameters are undefined...
853 if (cupsArrayCount(Addresses
) == 0)
856 * If we have no addresses, exit immediately...
860 "DEBUG: No address specified and no Address line in %s...\n",
865 if (cupsArrayCount(Communities
) == 0)
867 fputs("INFO: Using default SNMP Community public\n", stderr
);
868 add_array(Communities
, "public");
874 * 'read_snmp_response()' - Read and parse a SNMP response...
878 read_snmp_response(int fd
) /* I - SNMP socket file descriptor */
880 char addrname
[256]; /* Source address name */
881 cups_snmp_t packet
; /* Decoded packet */
882 snmp_cache_t key
, /* Search key */
883 *device
; /* Matching device */
887 * Read the response data...
890 if (!_cupsSNMPRead(fd
, &packet
, -1.0))
892 fprintf(stderr
, "ERROR: Unable to read data from socket: %s\n",
898 httpAddrLookup(&(packet
.address
), addrname
, sizeof(addrname
));
900 httpAddrString(&(packet
.address
), addrname
, sizeof(addrname
));
902 debug_printf("DEBUG: %.3f Received data from %s...\n", run_time(), addrname
);
905 * Look for the response status code in the SNMP message header...
910 fprintf(stderr
, "ERROR: Bad SNMP packet from %s: %s\n", addrname
,
916 debug_printf("DEBUG: community=\"%s\"\n", packet
.community
);
917 debug_printf("DEBUG: request-id=%d\n", packet
.request_id
);
918 debug_printf("DEBUG: error-status=%d\n", packet
.error_status
);
920 if (packet
.error_status
&& packet
.request_id
!= DEVICE_TYPE
)
924 * Find a matching device in the cache...
927 key
.addrname
= addrname
;
928 device
= (snmp_cache_t
*)cupsArrayFind(Devices
, &key
);
931 * Process the message...
934 switch (packet
.request_id
)
938 * Got the device type response...
943 debug_printf("DEBUG: Discarding duplicate device type for \"%s\"...\n",
949 * Add the device and request the device data...
952 add_cache(&(packet
.address
), addrname
, NULL
, NULL
, NULL
);
954 _cupsSNMPWrite(fd
, &(packet
.address
), CUPS_SNMP_VERSION_1
,
955 packet
.community
, CUPS_ASN1_GET_REQUEST
,
956 DEVICE_DESCRIPTION
, DescriptionOID
);
957 _cupsSNMPWrite(fd
, &(packet
.address
), CUPS_SNMP_VERSION_1
,
958 packet
.community
, CUPS_ASN1_GET_REQUEST
,
959 DEVICE_ID
, DeviceIdOID
);
960 _cupsSNMPWrite(fd
, &(packet
.address
), CUPS_SNMP_VERSION_1
,
961 packet
.community
, CUPS_ASN1_GET_REQUEST
,
963 _cupsSNMPWrite(fd
, &(packet
.address
), CUPS_SNMP_VERSION_1
,
964 packet
.community
, CUPS_ASN1_GET_REQUEST
,
965 DEVICE_LOCATION
, LocationOID
);
966 _cupsSNMPWrite(fd
, &(packet
.address
), CUPS_SNMP_VERSION_1
,
967 packet
.community
, CUPS_ASN1_GET_REQUEST
,
968 DEVICE_PRODUCT
, LexmarkProductOID
);
969 _cupsSNMPWrite(fd
, &(packet
.address
), CUPS_SNMP_VERSION_1
,
970 packet
.community
, CUPS_ASN1_GET_REQUEST
,
971 DEVICE_PRODUCT
, LexmarkProductOID2
);
972 _cupsSNMPWrite(fd
, &(packet
.address
), CUPS_SNMP_VERSION_1
,
973 packet
.community
, CUPS_ASN1_GET_REQUEST
,
974 DEVICE_ID
, LexmarkDeviceIdOID
);
975 _cupsSNMPWrite(fd
, &(packet
.address
), CUPS_SNMP_VERSION_1
,
976 packet
.community
, CUPS_ASN1_GET_REQUEST
,
977 DEVICE_PRODUCT
, XeroxProductOID
);
980 case DEVICE_DESCRIPTION
:
981 if (device
&& packet
.object_type
== CUPS_ASN1_OCTET_STRING
)
984 * Update an existing cache entry...
987 char make_model
[256]; /* Make and model */
990 if (strchr((char *)packet
.object_value
.string
.bytes
, ':') &&
991 strchr((char *)packet
.object_value
.string
.bytes
, ';'))
994 * Description is the IEEE-1284 device ID...
997 char *ptr
; /* Pointer into device ID */
999 for (ptr
= (char *)packet
.object_value
.string
.bytes
; *ptr
; ptr
++)
1001 *ptr
= ';'; /* A lot of bad printers put a newline */
1003 device
->id
= strdup((char *)packet
.object_value
.string
.bytes
);
1005 backendGetMakeModel((char *)packet
.object_value
.string
.bytes
,
1006 make_model
, sizeof(make_model
));
1011 device
->info
= strdup(make_model
);
1016 * Description is plain text...
1019 fix_make_model(make_model
, (char *)packet
.object_value
.string
.bytes
,
1020 sizeof(make_model
));
1025 device
->info
= strdup((char *)packet
.object_value
.string
.bytes
);
1028 if (!device
->make_and_model
)
1029 device
->make_and_model
= strdup(make_model
);
1034 if (device
&& packet
.object_type
== CUPS_ASN1_OCTET_STRING
&&
1036 strlen(device
->id
) < packet
.object_value
.string
.num_bytes
))
1039 * Update an existing cache entry...
1042 char make_model
[256]; /* Make and model */
1043 char *ptr
; /* Pointer into device ID */
1045 for (ptr
= (char *)packet
.object_value
.string
.bytes
; *ptr
; ptr
++)
1047 *ptr
= ';'; /* A lot of bad printers put a newline */
1051 device
->id
= strdup((char *)packet
.object_value
.string
.bytes
);
1054 * Convert the ID to a make and model string...
1057 backendGetMakeModel((char *)packet
.object_value
.string
.bytes
,
1058 make_model
, sizeof(make_model
));
1059 if (device
->make_and_model
)
1060 free(device
->make_and_model
);
1062 device
->make_and_model
= strdup(make_model
);
1066 case DEVICE_LOCATION
:
1067 if (device
&& packet
.object_type
== CUPS_ASN1_OCTET_STRING
&&
1069 device
->location
= strdup((char *)packet
.object_value
.string
.bytes
);
1072 case DEVICE_PRODUCT
:
1073 if (device
&& packet
.object_type
== CUPS_ASN1_OCTET_STRING
&&
1077 * Update an existing cache entry...
1081 device
->info
= strdup((char *)packet
.object_value
.string
.bytes
);
1083 if (device
->make_and_model
)
1084 free(device
->make_and_model
);
1086 device
->make_and_model
= strdup((char *)packet
.object_value
.string
.bytes
);
1091 if (device
&& packet
.object_type
== CUPS_ASN1_OCTET_STRING
&&
1092 !device
->uri
&& packet
.object_value
.string
.num_bytes
> 3)
1095 * Update an existing cache entry...
1098 char scheme
[32], /* URI scheme */
1099 userpass
[256], /* Username:password in URI */
1100 hostname
[256], /* Hostname in URI */
1101 resource
[1024]; /* Resource path in URI */
1102 int port
; /* Port number in URI */
1104 if (!strncmp((char *)packet
.object_value
.string
.bytes
, "lpr:", 4))
1107 * We want "lpd://..." for the URI...
1110 packet
.object_value
.string
.bytes
[2] = 'd';
1113 if (httpSeparateURI(HTTP_URI_CODING_ALL
,
1114 (char *)packet
.object_value
.string
.bytes
,
1115 scheme
, sizeof(scheme
),
1116 userpass
, sizeof(userpass
),
1117 hostname
, sizeof(hostname
), &port
,
1118 resource
, sizeof(resource
)) >= HTTP_URI_OK
)
1119 device
->uri
= strdup((char *)packet
.object_value
.string
.bytes
);
1127 * 'run_time()' - Return the total running time...
1130 static double /* O - Number of seconds */
1133 struct timeval curtime
; /* Current time */
1136 gettimeofday(&curtime
, NULL
);
1138 return (curtime
.tv_sec
- StartTime
.tv_sec
+
1139 0.000001 * (curtime
.tv_usec
- StartTime
.tv_usec
));
1144 * 'scan_devices()' - Scan for devices using SNMP.
1148 scan_devices(int ipv4
, /* I - SNMP IPv4 socket */
1149 int ipv6
) /* I - SNMP IPv6 socket */
1151 int fd
, /* File descriptor for this address */
1152 busy
; /* Are we busy processing something? */
1153 char *address
, /* Current address */
1154 *community
; /* Current community */
1155 fd_set input
; /* Input set for select() */
1156 struct timeval timeout
; /* Timeout for select() */
1157 time_t endtime
; /* End time for scan */
1158 http_addrlist_t
*addrs
, /* List of addresses */
1159 *addr
; /* Current address */
1160 snmp_cache_t
*device
; /* Current device */
1161 char temp
[1024]; /* Temporary address string */
1164 gettimeofday(&StartTime
, NULL
);
1167 * First send all of the broadcast queries...
1170 for (address
= (char *)cupsArrayFirst(Addresses
);
1172 address
= (char *)cupsArrayNext(Addresses
))
1174 if (!strcmp(address
, "@LOCAL"))
1175 addrs
= get_interface_addresses(NULL
);
1176 else if (!strncmp(address
, "@IF(", 4))
1178 char ifname
[255]; /* Interface name */
1180 strlcpy(ifname
, address
+ 4, sizeof(ifname
));
1182 ifname
[strlen(ifname
) - 1] = '\0';
1184 addrs
= get_interface_addresses(ifname
);
1187 addrs
= httpAddrGetList(address
, AF_UNSPEC
, NULL
);
1191 fprintf(stderr
, "ERROR: Unable to scan \"%s\"!\n", address
);
1195 for (community
= (char *)cupsArrayFirst(Communities
);
1197 community
= (char *)cupsArrayNext(Communities
))
1199 debug_printf("DEBUG: Scanning for devices in \"%s\" via \"%s\"...\n",
1200 community
, address
);
1202 for (addr
= addrs
; addr
; addr
= addr
->next
)
1205 if (httpAddrFamily(&(addr
->addr
)) == AF_INET6
)
1208 #endif /* AF_INET6 */
1211 debug_printf("DEBUG: Sending get request to %s...\n",
1212 httpAddrString(&(addr
->addr
), temp
, sizeof(temp
)));
1214 _cupsSNMPWrite(fd
, &(addr
->addr
), CUPS_SNMP_VERSION_1
, community
,
1215 CUPS_ASN1_GET_REQUEST
, DEVICE_TYPE
, DeviceTypeOID
);
1219 httpAddrFreeList(addrs
);
1223 * Then read any responses that come in over the next 3 seconds...
1226 endtime
= time(NULL
) + MaxRunTime
;
1230 while (time(NULL
) < endtime
)
1233 timeout
.tv_usec
= 0;
1235 FD_SET(ipv4
, &input
);
1237 FD_SET(ipv6
, &input
);
1239 fd
= ipv4
> ipv6
? ipv4
: ipv6
;
1240 if (select(fd
+ 1, &input
, NULL
, NULL
, &timeout
) < 0)
1242 fprintf(stderr
, "ERROR: %.3f select() for %d/%d failed: %s\n", run_time(),
1243 ipv4
, ipv6
, strerror(errno
));
1249 if (FD_ISSET(ipv4
, &input
))
1251 read_snmp_response(ipv4
);
1255 if (ipv6
>= 0 && FD_ISSET(ipv6
, &input
))
1257 read_snmp_response(ipv6
);
1264 * List devices with complete information...
1267 int sent_something
= 0;
1269 for (device
= (snmp_cache_t
*)cupsArrayFirst(Devices
);
1271 device
= (snmp_cache_t
*)cupsArrayNext(Devices
))
1272 if (!device
->sent
&& device
->info
&& device
->make_and_model
)
1275 list_device(device
);
1277 probe_device(device
);
1279 device
->sent
= sent_something
= 1;
1282 if (!sent_something
)
1287 debug_printf("DEBUG: %.3f Scan complete!\n", run_time());
1292 * 'try_connect()' - Try connecting on a port...
1295 static int /* O - 0 on success or -1 on error */
1296 try_connect(http_addr_t
*addr
, /* I - Socket address */
1297 const char *addrname
, /* I - Hostname or IP address */
1298 int port
) /* I - Port number */
1300 int fd
; /* Socket */
1301 int status
; /* Connection status */
1304 debug_printf("DEBUG: %.3f Trying %s://%s:%d...\n", run_time(),
1305 port
== 515 ? "lpd" : "socket", addrname
, port
);
1307 if ((fd
= socket(httpAddrFamily(addr
), SOCK_STREAM
, 0)) < 0)
1309 fprintf(stderr
, "ERROR: Unable to create socket: %s\n",
1314 _httpAddrSetPort(addr
, port
);
1318 status
= connect(fd
, (void *)addr
, (socklen_t
)httpAddrLength(addr
));
1328 * 'update_cache()' - Update a cached device...
1332 update_cache(snmp_cache_t
*device
, /* I - Device */
1333 const char *uri
, /* I - Device URI */
1334 const char *id
, /* I - Device ID */
1335 const char *make_model
) /* I - Device make and model */
1340 device
->uri
= strdup(uri
);
1347 device
->id
= strdup(id
);
1352 if (device
->make_and_model
)
1353 free(device
->make_and_model
);
1355 device
->make_and_model
= strdup(make_model
);
1358 list_device(device
);