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 * Licensed under Apache License v2.0. See the file "LICENSE" for more
12 * Include necessary headers.
15 #include "backend-private.h"
16 #include <cups/array.h>
17 #include <cups/file.h>
18 #include <cups/http-private.h>
23 * This backend implements SNMP printer discovery. It uses a broadcast-
24 * based approach to get SNMP response packets from potential printers,
25 * requesting OIDs from the Host and Port Monitor MIBs, does a URI
26 * lookup based on the device description string, and finally a probe of
27 * port 9100 (AppSocket) and 515 (LPD).
29 * The current focus is on printers with internal network cards, although
30 * the code also works with many external print servers as well.
32 * The backend reads the snmp.conf file from the CUPS_SERVERROOT directory
33 * which can contain comments, blank lines, or any number of the following
41 * DeviceURI "regex pattern" uri
46 * The default is to use:
54 * This backend is known to work with the following network printers and
57 * Axis OfficeBasic, 5400, 5600
67 * It does not currently work with:
74 * (for all of these, they do not support the Host MIB)
81 enum /**** Request IDs for each field ****/
91 typedef struct device_uri_s
/**** DeviceURI values ****/
93 regex_t re
; /* Regular expression to match */
94 cups_array_t
*uris
; /* URIs */
97 typedef struct snmp_cache_s
/**** SNMP scan cache ****/
99 http_addr_t address
; /* Address of device */
100 char *addrname
, /* Name of device */
101 *uri
, /* device-uri */
103 *info
, /* device-info */
104 *location
, /* device-location */
105 *make_and_model
; /* device-make-and-model */
106 int sent
; /* Has this device been listed? */
114 static char *add_array(cups_array_t
*a
, const char *s
);
115 static void add_cache(http_addr_t
*addr
, const char *addrname
,
116 const char *uri
, const char *id
,
117 const char *make_and_model
);
118 static device_uri_t
*add_device_uri(char *value
);
119 static void alarm_handler(int sig
);
120 static int compare_cache(snmp_cache_t
*a
, snmp_cache_t
*b
);
121 static void debug_printf(const char *format
, ...);
122 static void fix_make_model(char *make_model
,
123 const char *old_make_model
,
124 int make_model_size
);
125 static void free_array(cups_array_t
*a
);
126 static void free_cache(void);
127 static http_addrlist_t
*get_interface_addresses(const char *ifname
);
128 static void list_device(snmp_cache_t
*cache
);
129 static const char *password_cb(const char *prompt
);
130 static void probe_device(snmp_cache_t
*device
);
131 static void read_snmp_conf(const char *address
);
132 static void read_snmp_response(int fd
);
133 static double run_time(void);
134 static void scan_devices(int ipv4
, int ipv6
);
135 static int try_connect(http_addr_t
*addr
, const char *addrname
,
137 static void update_cache(snmp_cache_t
*device
, const char *uri
,
138 const char *id
, const char *make_model
);
145 static cups_array_t
*Addresses
= NULL
;
146 static cups_array_t
*Communities
= NULL
;
147 static cups_array_t
*Devices
= NULL
;
148 static int DebugLevel
= 0;
149 static const int DescriptionOID
[] = { CUPS_OID_hrDeviceDescr
, 1, -1 };
150 static const int LocationOID
[] = { CUPS_OID_sysLocation
, 0, -1 };
151 static const int DeviceTypeOID
[] = { CUPS_OID_hrDeviceType
, 1, -1 };
152 static const int DeviceIdOID
[] = { CUPS_OID_ppmPrinterIEEE1284DeviceId
, 1, -1 };
153 static const int UriOID
[] = { CUPS_OID_ppmPortServiceNameOrURI
, 1, 1, -1 };
154 static const int LexmarkProductOID
[] = { 1,3,6,1,4,1,641,2,1,2,1,2,1,-1 };
155 static const int LexmarkProductOID2
[] = { 1,3,6,1,4,1,674,10898,100,2,1,2,1,2,1,-1 };
156 static const int LexmarkDeviceIdOID
[] = { 1,3,6,1,4,1,641,2,1,2,1,3,1,-1 };
157 static const int XeroxProductOID
[] = { 1,3,6,1,4,1,128,2,1,3,1,2,0,-1 };
158 static cups_array_t
*DeviceURIs
= NULL
;
159 static int HostNameLookups
= 0;
160 static int MaxRunTime
= 120;
161 static struct timeval StartTime
;
165 * 'main()' - Discover printers via SNMP.
168 int /* O - Exit status */
169 main(int argc
, /* I - Number of command-line arguments (6 or 7) */
170 char *argv
[]) /* I - Command-line arguments */
172 int ipv4
, /* SNMP IPv4 socket */
173 ipv6
; /* SNMP IPv6 socket */
174 #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
175 struct sigaction action
; /* Actions for POSIX signals */
176 #endif /* HAVE_SIGACTION && !HAVE_SIGSET */
180 * Check command-line options...
185 _cupsLangPuts(stderr
, _("Usage: snmp [host-or-ip-address]"));
190 * Set the password callback for IPP operations...
193 cupsSetPasswordCB(password_cb
);
196 * Catch SIGALRM signals...
200 sigset(SIGALRM
, alarm_handler
);
201 #elif defined(HAVE_SIGACTION)
202 memset(&action
, 0, sizeof(action
));
204 sigemptyset(&action
.sa_mask
);
205 sigaddset(&action
.sa_mask
, SIGALRM
);
206 action
.sa_handler
= alarm_handler
;
207 sigaction(SIGALRM
, &action
, NULL
);
209 signal(SIGALRM
, alarm_handler
);
210 #endif /* HAVE_SIGSET */
213 * Open the SNMP socket...
216 if ((ipv4
= _cupsSNMPOpen(AF_INET
)) < 0)
220 if ((ipv6
= _cupsSNMPOpen(AF_INET6
)) < 0)
221 perror("DEBUG: Unable to create IPv6 socket");
224 #endif /* AF_INET6 */
227 * Read the configuration file and any cache data...
230 read_snmp_conf(argv
[1]);
232 _cupsSNMPSetDebug(DebugLevel
);
234 Devices
= cupsArrayNew((cups_array_func_t
)compare_cache
, NULL
);
237 * Scan for devices...
240 scan_devices(ipv4
, ipv6
);
243 * Close, free, and return with no errors...
246 _cupsSNMPClose(ipv4
);
248 _cupsSNMPClose(ipv6
);
250 free_array(Addresses
);
251 free_array(Communities
);
259 * 'add_array()' - Add a string to an array.
262 static char * /* O - New string */
263 add_array(cups_array_t
*a
, /* I - Array */
264 const char *s
) /* I - String to add */
266 char *dups
; /* New string */
271 cupsArrayAdd(a
, dups
);
278 * 'add_cache()' - Add a cached device...
282 add_cache(http_addr_t
*addr
, /* I - Device IP address */
283 const char *addrname
, /* I - IP address or name string */
284 const char *uri
, /* I - Device URI */
285 const char *id
, /* I - 1284 device ID */
286 const char *make_and_model
) /* I - Make and model */
288 snmp_cache_t
*temp
; /* New device entry */
291 debug_printf("DEBUG: add_cache(addr=%p, addrname=\"%s\", uri=\"%s\", "
292 "id=\"%s\", make_and_model=\"%s\")\n",
293 addr
, addrname
, uri
? uri
: "(null)", id
? id
: "(null)",
294 make_and_model
? make_and_model
: "(null)");
296 temp
= calloc(1, sizeof(snmp_cache_t
));
297 memcpy(&(temp
->address
), addr
, sizeof(temp
->address
));
299 temp
->addrname
= strdup(addrname
);
302 temp
->uri
= strdup(uri
);
305 temp
->id
= strdup(id
);
308 temp
->make_and_model
= strdup(make_and_model
);
310 cupsArrayAdd(Devices
, temp
);
318 * 'add_device_uri()' - Add a device URI to the cache.
320 * The value string is modified (chopped up) as needed.
323 static device_uri_t
* /* O - Device URI */
324 add_device_uri(char *value
) /* I - Value from snmp.conf */
326 device_uri_t
*device_uri
; /* Device URI */
327 char *start
; /* Start of value */
331 * Allocate memory as needed...
335 DeviceURIs
= cupsArrayNew(NULL
, NULL
);
340 if ((device_uri
= calloc(1, sizeof(device_uri_t
))) == NULL
)
343 if ((device_uri
->uris
= cupsArrayNew(NULL
, NULL
)) == NULL
)
350 * Scan the value string for the regular expression and URI(s)...
353 value
++; /* Skip leading " */
355 for (start
= value
; *value
&& *value
!= '\"'; value
++)
356 if (*value
== '\\' && value
[1])
357 _cups_strcpy(value
, value
+ 1);
361 fputs("ERROR: Missing end quote for DeviceURI!\n", stderr
);
363 cupsArrayDelete(device_uri
->uris
);
371 if (regcomp(&(device_uri
->re
), start
, REG_EXTENDED
| REG_ICASE
))
373 fputs("ERROR: Bad regular expression for DeviceURI!\n", stderr
);
375 cupsArrayDelete(device_uri
->uris
);
383 while (isspace(*value
& 255))
389 for (start
= value
; *value
&& !isspace(*value
& 255); value
++);
394 cupsArrayAdd(device_uri
->uris
, strdup(start
));
398 * Add the device URI to the list and return it...
401 cupsArrayAdd(DeviceURIs
, device_uri
);
408 * 'alarm_handler()' - Handle alarm signals...
412 alarm_handler(int sig
) /* I - Signal number */
420 #if !defined(HAVE_SIGSET) && !defined(HAVE_SIGACTION)
421 signal(SIGALRM
, alarm_handler
);
422 #endif /* !HAVE_SIGSET && !HAVE_SIGACTION */
425 write(2, "DEBUG: ALARM!\n", 14);
430 * 'compare_cache()' - Compare two cache entries.
433 static int /* O - Result of comparison */
434 compare_cache(snmp_cache_t
*a
, /* I - First cache entry */
435 snmp_cache_t
*b
) /* I - Second cache entry */
437 return (_cups_strcasecmp(a
->addrname
, b
->addrname
));
442 * 'debug_printf()' - Display some debugging information.
446 debug_printf(const char *format
, /* I - Printf-style format string */
447 ...) /* I - Additional arguments as needed */
449 va_list ap
; /* Pointer to arguments */
455 va_start(ap
, format
);
456 vfprintf(stderr
, format
, ap
);
462 * 'fix_make_model()' - Fix common problems in the make-and-model string.
467 char *make_model
, /* I - New make-and-model string */
468 const char *old_make_model
, /* I - Old make-and-model string */
469 int make_model_size
) /* I - Size of new string buffer */
471 char *mmptr
; /* Pointer into make-and-model string */
475 * Fix some common problems with the make-and-model string so
476 * that printer driver detection works better...
479 if (!_cups_strncasecmp(old_make_model
, "Hewlett-Packard", 15))
482 * Strip leading Hewlett-Packard and hp prefixes and replace
483 * with a single HP manufacturer prefix...
486 mmptr
= (char *)old_make_model
+ 15;
488 while (isspace(*mmptr
& 255))
491 if (!_cups_strncasecmp(mmptr
, "hp", 2))
495 while (isspace(*mmptr
& 255))
502 strlcpy(make_model
+ 3, mmptr
, (size_t)make_model_size
- 3);
504 else if (!_cups_strncasecmp(old_make_model
, "deskjet", 7))
505 snprintf(make_model
, (size_t)make_model_size
, "HP DeskJet%s", old_make_model
+ 7);
506 else if (!_cups_strncasecmp(old_make_model
, "officejet", 9))
507 snprintf(make_model
, (size_t)make_model_size
, "HP OfficeJet%s", old_make_model
+ 9);
508 else if (!_cups_strncasecmp(old_make_model
, "stylus_pro_", 11))
509 snprintf(make_model
, (size_t)make_model_size
, "EPSON Stylus Pro %s", old_make_model
+ 11);
511 strlcpy(make_model
, old_make_model
, (size_t)make_model_size
);
513 if ((mmptr
= strstr(make_model
, ", Inc.,")) != NULL
)
516 * Strip inc. from name, e.g. "Tektronix, Inc., Phaser 560"
517 * becomes "Tektronix Phaser 560"...
520 _cups_strcpy(mmptr
, mmptr
+ 7);
523 if ((mmptr
= strstr(make_model
, " Network")) != NULL
)
526 * Drop unnecessary informational text, e.g. "Xerox DocuPrint N2025
527 * Network LaserJet - 2.12" becomes "Xerox DocuPrint N2025"...
533 if ((mmptr
= strchr(make_model
, ',')) != NULL
)
536 * Drop anything after a trailing comma...
545 * 'free_array()' - Free an array of strings.
549 free_array(cups_array_t
*a
) /* I - Array */
551 char *s
; /* Current string */
554 for (s
= (char *)cupsArrayFirst(a
); s
; s
= (char *)cupsArrayNext(a
))
562 * 'free_cache()' - Free the array of cached devices.
568 snmp_cache_t
*cache
; /* Cached device */
571 for (cache
= (snmp_cache_t
*)cupsArrayFirst(Devices
);
573 cache
= (snmp_cache_t
*)cupsArrayNext(Devices
))
575 free(cache
->addrname
);
583 if (cache
->make_and_model
)
584 free(cache
->make_and_model
);
589 cupsArrayDelete(Devices
);
595 * 'get_interface_addresses()' - Get the broadcast address(es) associated
599 static http_addrlist_t
* /* O - List of addresses */
600 get_interface_addresses(
601 const char *ifname
) /* I - Interface name */
603 struct ifaddrs
*addrs
, /* Interface address list */
604 *addr
; /* Current interface address */
605 http_addrlist_t
*first
, /* First address in list */
606 *last
, /* Last address in list */
607 *current
; /* Current address */
610 if (getifaddrs(&addrs
) < 0)
613 for (addr
= addrs
, first
= NULL
, last
= NULL
; addr
; addr
= addr
->ifa_next
)
614 if ((addr
->ifa_flags
& IFF_BROADCAST
) && addr
->ifa_broadaddr
&&
615 addr
->ifa_broadaddr
->sa_family
== AF_INET
&&
616 (!ifname
|| !strcmp(ifname
, addr
->ifa_name
)))
618 current
= calloc(1, sizeof(http_addrlist_t
));
620 memcpy(&(current
->addr
), addr
->ifa_broadaddr
,
621 sizeof(struct sockaddr_in
));
626 last
->next
= current
;
638 * 'list_device()' - List a device we found...
642 list_device(snmp_cache_t
*cache
) /* I - Cached device */
645 cupsBackendReport("network", cache
->uri
, cache
->make_and_model
,
646 cache
->info
, cache
->id
, cache
->location
);
651 * 'password_cb()' - Handle authentication requests.
653 * All we do right now is return NULL, indicating that no authentication
657 static const char * /* O - Password (NULL) */
658 password_cb(const char *prompt
) /* I - Prompt message */
660 (void)prompt
; /* Anti-compiler-warning-code */
667 * 'probe_device()' - Probe a device to discover whether it is a printer.
669 * TODO: Try using the Port Monitor MIB to discover the correct protocol
670 * to use - first need a commercially-available printer that supports
675 probe_device(snmp_cache_t
*device
) /* I - Device */
677 char uri
[1024], /* Full device URI */
678 *uriptr
, /* Pointer into URI */
679 *format
; /* Format string for device */
680 device_uri_t
*device_uri
; /* Current DeviceURI match */
683 debug_printf("DEBUG: %.3f Probing %s...\n", run_time(), device
->addrname
);
687 * If the printer supports Bonjour/mDNS, don't report it from the SNMP backend.
690 if (!try_connect(&(device
->address
), device
->addrname
, 5353))
692 debug_printf("DEBUG: %s supports mDNS, not reporting!\n", device
->addrname
);
695 #endif /* __APPLE__ */
698 * Lookup the device in the match table...
701 for (device_uri
= (device_uri_t
*)cupsArrayFirst(DeviceURIs
);
703 device_uri
= (device_uri_t
*)cupsArrayNext(DeviceURIs
))
704 if (device
->make_and_model
&&
705 !regexec(&(device_uri
->re
), device
->make_and_model
, 0, NULL
, 0))
708 * Found a match, add the URIs...
711 for (format
= (char *)cupsArrayFirst(device_uri
->uris
);
713 format
= (char *)cupsArrayNext(device_uri
->uris
))
715 for (uriptr
= uri
; *format
&& uriptr
< (uri
+ sizeof(uri
) - 1);)
716 if (*format
== '%' && format
[1] == 's')
719 * Insert hostname/address...
722 strlcpy(uriptr
, device
->addrname
, sizeof(uri
) - (size_t)(uriptr
- uri
));
723 uriptr
+= strlen(uriptr
);
727 *uriptr
++ = *format
++;
731 update_cache(device
, uri
, NULL
, NULL
);
738 * Then try the standard ports...
741 if (!try_connect(&(device
->address
), device
->addrname
, 9100))
743 debug_printf("DEBUG: %s supports AppSocket!\n", device
->addrname
);
745 snprintf(uri
, sizeof(uri
), "socket://%s", device
->addrname
);
746 update_cache(device
, uri
, NULL
, NULL
);
748 else if (!try_connect(&(device
->address
), device
->addrname
, 515))
750 debug_printf("DEBUG: %s supports LPD!\n", device
->addrname
);
752 snprintf(uri
, sizeof(uri
), "lpd://%s/", device
->addrname
);
753 update_cache(device
, uri
, NULL
, NULL
);
759 * 'read_snmp_conf()' - Read the snmp.conf file.
763 read_snmp_conf(const char *address
) /* I - Single address to probe */
765 cups_file_t
*fp
; /* File pointer */
766 char filename
[1024], /* Filename */
767 line
[1024], /* Line from file */
768 *value
; /* Value on line */
769 int linenum
; /* Line number */
770 const char *cups_serverroot
; /* CUPS_SERVERROOT env var */
771 const char *debug
; /* CUPS_DEBUG_LEVEL env var */
772 const char *runtime
; /* CUPS_MAX_RUN_TIME env var */
776 * Initialize the global address and community lists...
779 Addresses
= cupsArrayNew(NULL
, NULL
);
780 Communities
= cupsArrayNew(NULL
, NULL
);
783 add_array(Addresses
, address
);
785 if ((debug
= getenv("CUPS_DEBUG_LEVEL")) != NULL
)
786 DebugLevel
= atoi(debug
);
788 if ((runtime
= getenv("CUPS_MAX_RUN_TIME")) != NULL
)
789 MaxRunTime
= atoi(runtime
);
792 * Find the snmp.conf file...
795 if ((cups_serverroot
= getenv("CUPS_SERVERROOT")) == NULL
)
796 cups_serverroot
= CUPS_SERVERROOT
;
798 snprintf(filename
, sizeof(filename
), "%s/snmp.conf", cups_serverroot
);
800 if ((fp
= cupsFileOpen(filename
, "r")) != NULL
)
803 * Read the snmp.conf file...
808 while (cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
))
811 fprintf(stderr
, "ERROR: Missing value on line %d of %s!\n", linenum
,
813 else if (!_cups_strcasecmp(line
, "Address"))
816 add_array(Addresses
, value
);
818 else if (!_cups_strcasecmp(line
, "Community"))
819 add_array(Communities
, value
);
820 else if (!_cups_strcasecmp(line
, "DebugLevel"))
821 DebugLevel
= atoi(value
);
822 else if (!_cups_strcasecmp(line
, "DeviceURI"))
826 "ERROR: Missing double quote for regular expression on "
827 "line %d of %s!\n", linenum
, filename
);
829 add_device_uri(value
);
831 else if (!_cups_strcasecmp(line
, "HostNameLookups"))
832 HostNameLookups
= !_cups_strcasecmp(value
, "on") ||
833 !_cups_strcasecmp(value
, "yes") ||
834 !_cups_strcasecmp(value
, "true") ||
835 !_cups_strcasecmp(value
, "double");
836 else if (!_cups_strcasecmp(line
, "MaxRunTime"))
837 MaxRunTime
= atoi(value
);
839 fprintf(stderr
, "ERROR: Unknown directive %s on line %d of %s!\n",
840 line
, linenum
, filename
);
847 * Use defaults if parameters are undefined...
850 if (cupsArrayCount(Addresses
) == 0)
853 * If we have no addresses, exit immediately...
857 "DEBUG: No address specified and no Address line in %s...\n",
862 if (cupsArrayCount(Communities
) == 0)
864 fputs("INFO: Using default SNMP Community public\n", stderr
);
865 add_array(Communities
, "public");
871 * 'read_snmp_response()' - Read and parse a SNMP response...
875 read_snmp_response(int fd
) /* I - SNMP socket file descriptor */
877 char addrname
[256]; /* Source address name */
878 cups_snmp_t packet
; /* Decoded packet */
879 snmp_cache_t key
, /* Search key */
880 *device
; /* Matching device */
884 * Read the response data...
887 if (!_cupsSNMPRead(fd
, &packet
, -1.0))
889 fprintf(stderr
, "ERROR: Unable to read data from socket: %s\n",
895 httpAddrLookup(&(packet
.address
), addrname
, sizeof(addrname
));
897 httpAddrString(&(packet
.address
), addrname
, sizeof(addrname
));
899 debug_printf("DEBUG: %.3f Received data from %s...\n", run_time(), addrname
);
902 * Look for the response status code in the SNMP message header...
907 fprintf(stderr
, "ERROR: Bad SNMP packet from %s: %s\n", addrname
,
913 debug_printf("DEBUG: community=\"%s\"\n", packet
.community
);
914 debug_printf("DEBUG: request-id=%d\n", packet
.request_id
);
915 debug_printf("DEBUG: error-status=%d\n", packet
.error_status
);
917 if (packet
.error_status
&& packet
.request_id
!= DEVICE_TYPE
)
921 * Find a matching device in the cache...
924 key
.addrname
= addrname
;
925 device
= (snmp_cache_t
*)cupsArrayFind(Devices
, &key
);
928 * Process the message...
931 switch (packet
.request_id
)
935 * Got the device type response...
940 debug_printf("DEBUG: Discarding duplicate device type for \"%s\"...\n",
946 * Add the device and request the device data...
949 add_cache(&(packet
.address
), addrname
, NULL
, NULL
, NULL
);
951 _cupsSNMPWrite(fd
, &(packet
.address
), CUPS_SNMP_VERSION_1
,
952 packet
.community
, CUPS_ASN1_GET_REQUEST
,
953 DEVICE_DESCRIPTION
, DescriptionOID
);
954 _cupsSNMPWrite(fd
, &(packet
.address
), CUPS_SNMP_VERSION_1
,
955 packet
.community
, CUPS_ASN1_GET_REQUEST
,
956 DEVICE_ID
, DeviceIdOID
);
957 _cupsSNMPWrite(fd
, &(packet
.address
), CUPS_SNMP_VERSION_1
,
958 packet
.community
, CUPS_ASN1_GET_REQUEST
,
960 _cupsSNMPWrite(fd
, &(packet
.address
), CUPS_SNMP_VERSION_1
,
961 packet
.community
, CUPS_ASN1_GET_REQUEST
,
962 DEVICE_LOCATION
, LocationOID
);
963 _cupsSNMPWrite(fd
, &(packet
.address
), CUPS_SNMP_VERSION_1
,
964 packet
.community
, CUPS_ASN1_GET_REQUEST
,
965 DEVICE_PRODUCT
, LexmarkProductOID
);
966 _cupsSNMPWrite(fd
, &(packet
.address
), CUPS_SNMP_VERSION_1
,
967 packet
.community
, CUPS_ASN1_GET_REQUEST
,
968 DEVICE_PRODUCT
, LexmarkProductOID2
);
969 _cupsSNMPWrite(fd
, &(packet
.address
), CUPS_SNMP_VERSION_1
,
970 packet
.community
, CUPS_ASN1_GET_REQUEST
,
971 DEVICE_ID
, LexmarkDeviceIdOID
);
972 _cupsSNMPWrite(fd
, &(packet
.address
), CUPS_SNMP_VERSION_1
,
973 packet
.community
, CUPS_ASN1_GET_REQUEST
,
974 DEVICE_PRODUCT
, XeroxProductOID
);
977 case DEVICE_DESCRIPTION
:
978 if (device
&& packet
.object_type
== CUPS_ASN1_OCTET_STRING
)
981 * Update an existing cache entry...
984 char make_model
[256]; /* Make and model */
987 if (strchr((char *)packet
.object_value
.string
.bytes
, ':') &&
988 strchr((char *)packet
.object_value
.string
.bytes
, ';'))
991 * Description is the IEEE-1284 device ID...
994 char *ptr
; /* Pointer into device ID */
996 for (ptr
= (char *)packet
.object_value
.string
.bytes
; *ptr
; ptr
++)
998 *ptr
= ';'; /* A lot of bad printers put a newline */
1000 device
->id
= strdup((char *)packet
.object_value
.string
.bytes
);
1002 backendGetMakeModel((char *)packet
.object_value
.string
.bytes
,
1003 make_model
, sizeof(make_model
));
1008 device
->info
= strdup(make_model
);
1013 * Description is plain text...
1016 fix_make_model(make_model
, (char *)packet
.object_value
.string
.bytes
,
1017 sizeof(make_model
));
1022 device
->info
= strdup((char *)packet
.object_value
.string
.bytes
);
1025 if (!device
->make_and_model
)
1026 device
->make_and_model
= strdup(make_model
);
1031 if (device
&& packet
.object_type
== CUPS_ASN1_OCTET_STRING
&&
1033 strlen(device
->id
) < packet
.object_value
.string
.num_bytes
))
1036 * Update an existing cache entry...
1039 char make_model
[256]; /* Make and model */
1040 char *ptr
; /* Pointer into device ID */
1042 for (ptr
= (char *)packet
.object_value
.string
.bytes
; *ptr
; ptr
++)
1044 *ptr
= ';'; /* A lot of bad printers put a newline */
1048 device
->id
= strdup((char *)packet
.object_value
.string
.bytes
);
1051 * Convert the ID to a make and model string...
1054 backendGetMakeModel((char *)packet
.object_value
.string
.bytes
,
1055 make_model
, sizeof(make_model
));
1056 if (device
->make_and_model
)
1057 free(device
->make_and_model
);
1059 device
->make_and_model
= strdup(make_model
);
1063 case DEVICE_LOCATION
:
1064 if (device
&& packet
.object_type
== CUPS_ASN1_OCTET_STRING
&&
1066 device
->location
= strdup((char *)packet
.object_value
.string
.bytes
);
1069 case DEVICE_PRODUCT
:
1070 if (device
&& packet
.object_type
== CUPS_ASN1_OCTET_STRING
&&
1074 * Update an existing cache entry...
1078 device
->info
= strdup((char *)packet
.object_value
.string
.bytes
);
1080 if (device
->make_and_model
)
1081 free(device
->make_and_model
);
1083 device
->make_and_model
= strdup((char *)packet
.object_value
.string
.bytes
);
1088 if (device
&& packet
.object_type
== CUPS_ASN1_OCTET_STRING
&&
1089 !device
->uri
&& packet
.object_value
.string
.num_bytes
> 3)
1092 * Update an existing cache entry...
1095 char scheme
[32], /* URI scheme */
1096 userpass
[256], /* Username:password in URI */
1097 hostname
[256], /* Hostname in URI */
1098 resource
[1024]; /* Resource path in URI */
1099 int port
; /* Port number in URI */
1101 if (!strncmp((char *)packet
.object_value
.string
.bytes
, "lpr:", 4))
1104 * We want "lpd://..." for the URI...
1107 packet
.object_value
.string
.bytes
[2] = 'd';
1110 if (httpSeparateURI(HTTP_URI_CODING_ALL
,
1111 (char *)packet
.object_value
.string
.bytes
,
1112 scheme
, sizeof(scheme
),
1113 userpass
, sizeof(userpass
),
1114 hostname
, sizeof(hostname
), &port
,
1115 resource
, sizeof(resource
)) >= HTTP_URI_OK
)
1116 device
->uri
= strdup((char *)packet
.object_value
.string
.bytes
);
1124 * 'run_time()' - Return the total running time...
1127 static double /* O - Number of seconds */
1130 struct timeval curtime
; /* Current time */
1133 gettimeofday(&curtime
, NULL
);
1135 return (curtime
.tv_sec
- StartTime
.tv_sec
+
1136 0.000001 * (curtime
.tv_usec
- StartTime
.tv_usec
));
1141 * 'scan_devices()' - Scan for devices using SNMP.
1145 scan_devices(int ipv4
, /* I - SNMP IPv4 socket */
1146 int ipv6
) /* I - SNMP IPv6 socket */
1148 int fd
, /* File descriptor for this address */
1149 busy
; /* Are we busy processing something? */
1150 char *address
, /* Current address */
1151 *community
; /* Current community */
1152 fd_set input
; /* Input set for select() */
1153 struct timeval timeout
; /* Timeout for select() */
1154 time_t endtime
; /* End time for scan */
1155 http_addrlist_t
*addrs
, /* List of addresses */
1156 *addr
; /* Current address */
1157 snmp_cache_t
*device
; /* Current device */
1158 char temp
[1024]; /* Temporary address string */
1161 gettimeofday(&StartTime
, NULL
);
1164 * First send all of the broadcast queries...
1167 for (address
= (char *)cupsArrayFirst(Addresses
);
1169 address
= (char *)cupsArrayNext(Addresses
))
1171 if (!strcmp(address
, "@LOCAL"))
1172 addrs
= get_interface_addresses(NULL
);
1173 else if (!strncmp(address
, "@IF(", 4))
1175 char ifname
[255]; /* Interface name */
1177 strlcpy(ifname
, address
+ 4, sizeof(ifname
));
1179 ifname
[strlen(ifname
) - 1] = '\0';
1181 addrs
= get_interface_addresses(ifname
);
1184 addrs
= httpAddrGetList(address
, AF_UNSPEC
, NULL
);
1188 fprintf(stderr
, "ERROR: Unable to scan \"%s\"!\n", address
);
1192 for (community
= (char *)cupsArrayFirst(Communities
);
1194 community
= (char *)cupsArrayNext(Communities
))
1196 debug_printf("DEBUG: Scanning for devices in \"%s\" via \"%s\"...\n",
1197 community
, address
);
1199 for (addr
= addrs
; addr
; addr
= addr
->next
)
1202 if (httpAddrFamily(&(addr
->addr
)) == AF_INET6
)
1205 #endif /* AF_INET6 */
1208 debug_printf("DEBUG: Sending get request to %s...\n",
1209 httpAddrString(&(addr
->addr
), temp
, sizeof(temp
)));
1211 _cupsSNMPWrite(fd
, &(addr
->addr
), CUPS_SNMP_VERSION_1
, community
,
1212 CUPS_ASN1_GET_REQUEST
, DEVICE_TYPE
, DeviceTypeOID
);
1216 httpAddrFreeList(addrs
);
1220 * Then read any responses that come in over the next 3 seconds...
1223 endtime
= time(NULL
) + MaxRunTime
;
1227 while (time(NULL
) < endtime
)
1230 timeout
.tv_usec
= 0;
1232 FD_SET(ipv4
, &input
);
1234 FD_SET(ipv6
, &input
);
1236 fd
= ipv4
> ipv6
? ipv4
: ipv6
;
1237 if (select(fd
+ 1, &input
, NULL
, NULL
, &timeout
) < 0)
1239 fprintf(stderr
, "ERROR: %.3f select() for %d/%d failed: %s\n", run_time(),
1240 ipv4
, ipv6
, strerror(errno
));
1246 if (FD_ISSET(ipv4
, &input
))
1248 read_snmp_response(ipv4
);
1252 if (ipv6
>= 0 && FD_ISSET(ipv6
, &input
))
1254 read_snmp_response(ipv6
);
1261 * List devices with complete information...
1264 int sent_something
= 0;
1266 for (device
= (snmp_cache_t
*)cupsArrayFirst(Devices
);
1268 device
= (snmp_cache_t
*)cupsArrayNext(Devices
))
1269 if (!device
->sent
&& device
->info
&& device
->make_and_model
)
1272 list_device(device
);
1274 probe_device(device
);
1276 device
->sent
= sent_something
= 1;
1279 if (!sent_something
)
1284 debug_printf("DEBUG: %.3f Scan complete!\n", run_time());
1289 * 'try_connect()' - Try connecting on a port...
1292 static int /* O - 0 on success or -1 on error */
1293 try_connect(http_addr_t
*addr
, /* I - Socket address */
1294 const char *addrname
, /* I - Hostname or IP address */
1295 int port
) /* I - Port number */
1297 int fd
; /* Socket */
1298 int status
; /* Connection status */
1301 debug_printf("DEBUG: %.3f Trying %s://%s:%d...\n", run_time(),
1302 port
== 515 ? "lpd" : "socket", addrname
, port
);
1304 if ((fd
= socket(httpAddrFamily(addr
), SOCK_STREAM
, 0)) < 0)
1306 fprintf(stderr
, "ERROR: Unable to create socket: %s\n",
1311 _httpAddrSetPort(addr
, port
);
1315 status
= connect(fd
, (void *)addr
, (socklen_t
)httpAddrLength(addr
));
1325 * 'update_cache()' - Update a cached device...
1329 update_cache(snmp_cache_t
*device
, /* I - Device */
1330 const char *uri
, /* I - Device URI */
1331 const char *id
, /* I - Device ID */
1332 const char *make_model
) /* I - Device make and model */
1337 device
->uri
= strdup(uri
);
1344 device
->id
= strdup(id
);
1349 if (device
->make_and_model
)
1350 free(device
->make_and_model
);
1352 device
->make_and_model
= strdup(make_model
);
1355 list_device(device
);