2 * "$Id: snmp.c 7810 2008-07-29 01:11:15Z mike $"
4 * SNMP discovery backend for CUPS.
6 * Copyright 2007-2012 by Apple Inc.
7 * Copyright 2006-2007 by Easy Software Products, all rights reserved.
9 * These coded instructions, statements, and computer programs are the
10 * property of Apple Inc. and are protected by Federal copyright
11 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
12 * "LICENSE" which should have been included with this file. If this
13 * file is missing or damaged, see the license at "http://www.cups.org/".
15 * This file is subject to the Apple OS-Developed Software exception.
19 * main() - Discover printers via SNMP.
20 * add_array() - Add a string to an array.
21 * add_cache() - Add a cached device...
22 * add_device_uri() - Add a device URI to the cache.
23 * alarm_handler() - Handle alarm signals...
24 * compare_cache() - Compare two cache entries.
25 * debug_printf() - Display some debugging information.
26 * fix_make_model() - Fix common problems in the make-and-model
28 * free_array() - Free an array of strings.
29 * free_cache() - Free the array of cached devices.
30 * get_interface_addresses() - Get the broadcast address(es) associated with
32 * list_device() - List a device we found...
33 * password_cb() - Handle authentication requests.
34 * probe_device() - Probe a device to discover whether it is a
36 * read_snmp_conf() - Read the snmp.conf file.
37 * read_snmp_response() - Read and parse a SNMP response...
38 * run_time() - Return the total running time...
39 * scan_devices() - Scan for devices using SNMP.
40 * try_connect() - Try connecting on a port...
41 * update_cache() - Update a cached device...
45 * Include necessary headers.
48 #include "backend-private.h"
49 #include <cups/array.h>
50 #include <cups/file.h>
51 #include <cups/http-private.h>
56 * This backend implements SNMP printer discovery. It uses a broadcast-
57 * based approach to get SNMP response packets from potential printers,
58 * requesting OIDs from the Host and Port Monitor MIBs, does a URI
59 * lookup based on the device description string, and finally a probe of
60 * port 9100 (AppSocket) and 515 (LPD).
62 * The current focus is on printers with internal network cards, although
63 * the code also works with many external print servers as well.
65 * The backend reads the snmp.conf file from the CUPS_SERVERROOT directory
66 * which can contain comments, blank lines, or any number of the following
74 * DeviceURI "regex pattern" uri
79 * The default is to use:
87 * This backend is known to work with the following network printers and
90 * Axis OfficeBasic, 5400, 5600
100 * It does not currently work with:
107 * (for all of these, they do not support the Host MIB)
114 enum /**** Request IDs for each field ****/
124 typedef struct device_uri_s
/**** DeviceURI values ****/
126 regex_t re
; /* Regular expression to match */
127 cups_array_t
*uris
; /* URIs */
130 typedef struct snmp_cache_s
/**** SNMP scan cache ****/
132 http_addr_t address
; /* Address of device */
133 char *addrname
, /* Name of device */
134 *uri
, /* device-uri */
136 *info
, /* device-info */
137 *location
, /* device-location */
138 *make_and_model
; /* device-make-and-model */
139 int sent
; /* Has this device been listed? */
147 static char *add_array(cups_array_t
*a
, const char *s
);
148 static void add_cache(http_addr_t
*addr
, const char *addrname
,
149 const char *uri
, const char *id
,
150 const char *make_and_model
);
151 static device_uri_t
*add_device_uri(char *value
);
152 static void alarm_handler(int sig
);
153 static int compare_cache(snmp_cache_t
*a
, snmp_cache_t
*b
);
154 static void debug_printf(const char *format
, ...);
155 static void fix_make_model(char *make_model
,
156 const char *old_make_model
,
157 int make_model_size
);
158 static void free_array(cups_array_t
*a
);
159 static void free_cache(void);
160 static http_addrlist_t
*get_interface_addresses(const char *ifname
);
161 static void list_device(snmp_cache_t
*cache
);
162 static const char *password_cb(const char *prompt
);
163 static void probe_device(snmp_cache_t
*device
);
164 static void read_snmp_conf(const char *address
);
165 static void read_snmp_response(int fd
);
166 static double run_time(void);
167 static void scan_devices(int ipv4
, int ipv6
);
168 static int try_connect(http_addr_t
*addr
, const char *addrname
,
170 static void update_cache(snmp_cache_t
*device
, const char *uri
,
171 const char *id
, const char *make_model
);
178 static cups_array_t
*Addresses
= NULL
;
179 static cups_array_t
*Communities
= NULL
;
180 static cups_array_t
*Devices
= NULL
;
181 static int DebugLevel
= 0;
182 static const int DescriptionOID
[] = { CUPS_OID_hrDeviceDescr
, 1, -1 };
183 static const int LocationOID
[] = { CUPS_OID_sysLocation
, 0, -1 };
184 static const int DeviceTypeOID
[] = { CUPS_OID_hrDeviceType
, 1, -1 };
185 static const int DeviceIdOID
[] = { CUPS_OID_ppmPrinterIEEE1284DeviceId
, 1, -1 };
186 static const int UriOID
[] = { CUPS_OID_ppmPortServiceNameOrURI
, 1, 1, -1 };
187 static const int LexmarkProductOID
[] = { 1,3,6,1,4,1,641,2,1,2,1,2,1,-1 };
188 static const int LexmarkProductOID2
[] = { 1,3,6,1,4,1,674,10898,100,2,1,2,1,2,1,-1 };
189 static const int LexmarkDeviceIdOID
[] = { 1,3,6,1,4,1,641,2,1,2,1,3,1,-1 };
190 static const int XeroxProductOID
[] = { 1,3,6,1,4,1,128,2,1,3,1,2,0,-1 };
191 static cups_array_t
*DeviceURIs
= NULL
;
192 static int HostNameLookups
= 0;
193 static int MaxRunTime
= 120;
194 static struct timeval StartTime
;
198 * 'main()' - Discover printers via SNMP.
201 int /* O - Exit status */
202 main(int argc
, /* I - Number of command-line arguments (6 or 7) */
203 char *argv
[]) /* I - Command-line arguments */
205 int ipv4
, /* SNMP IPv4 socket */
206 ipv6
; /* SNMP IPv6 socket */
207 #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
208 struct sigaction action
; /* Actions for POSIX signals */
209 #endif /* HAVE_SIGACTION && !HAVE_SIGSET */
213 * Check command-line options...
218 _cupsLangPuts(stderr
, _("Usage: snmp [host-or-ip-address]"));
223 * Set the password callback for IPP operations...
226 cupsSetPasswordCB(password_cb
);
229 * Catch SIGALRM signals...
233 sigset(SIGALRM
, alarm_handler
);
234 #elif defined(HAVE_SIGACTION)
235 memset(&action
, 0, sizeof(action
));
237 sigemptyset(&action
.sa_mask
);
238 sigaddset(&action
.sa_mask
, SIGALRM
);
239 action
.sa_handler
= alarm_handler
;
240 sigaction(SIGALRM
, &action
, NULL
);
242 signal(SIGALRM
, alarm_handler
);
243 #endif /* HAVE_SIGSET */
246 * Open the SNMP socket...
249 if ((ipv4
= _cupsSNMPOpen(AF_INET
)) < 0)
253 if ((ipv6
= _cupsSNMPOpen(AF_INET6
)) < 0)
254 perror("DEBUG: Unable to create IPv6 socket");
257 #endif /* AF_INET6 */
260 * Read the configuration file and any cache data...
263 read_snmp_conf(argv
[1]);
265 _cupsSNMPSetDebug(DebugLevel
);
267 Devices
= cupsArrayNew((cups_array_func_t
)compare_cache
, NULL
);
270 * Scan for devices...
273 scan_devices(ipv4
, ipv6
);
276 * Close, free, and return with no errors...
279 _cupsSNMPClose(ipv4
);
281 _cupsSNMPClose(ipv6
);
283 free_array(Addresses
);
284 free_array(Communities
);
292 * 'add_array()' - Add a string to an array.
295 static char * /* O - New string */
296 add_array(cups_array_t
*a
, /* I - Array */
297 const char *s
) /* I - String to add */
299 char *dups
; /* New string */
304 cupsArrayAdd(a
, dups
);
311 * 'add_cache()' - Add a cached device...
315 add_cache(http_addr_t
*addr
, /* I - Device IP address */
316 const char *addrname
, /* I - IP address or name string */
317 const char *uri
, /* I - Device URI */
318 const char *id
, /* I - 1284 device ID */
319 const char *make_and_model
) /* I - Make and model */
321 snmp_cache_t
*temp
; /* New device entry */
324 debug_printf("DEBUG: add_cache(addr=%p, addrname=\"%s\", uri=\"%s\", "
325 "id=\"%s\", make_and_model=\"%s\")\n",
326 addr
, addrname
, uri
? uri
: "(null)", id
? id
: "(null)",
327 make_and_model
? make_and_model
: "(null)");
329 temp
= calloc(1, sizeof(snmp_cache_t
));
330 memcpy(&(temp
->address
), addr
, sizeof(temp
->address
));
332 temp
->addrname
= strdup(addrname
);
335 temp
->uri
= strdup(uri
);
338 temp
->id
= strdup(id
);
341 temp
->make_and_model
= strdup(make_and_model
);
343 cupsArrayAdd(Devices
, temp
);
351 * 'add_device_uri()' - Add a device URI to the cache.
353 * The value string is modified (chopped up) as needed.
356 static device_uri_t
* /* O - Device URI */
357 add_device_uri(char *value
) /* I - Value from snmp.conf */
359 device_uri_t
*device_uri
; /* Device URI */
360 char *start
; /* Start of value */
364 * Allocate memory as needed...
368 DeviceURIs
= cupsArrayNew(NULL
, NULL
);
373 if ((device_uri
= calloc(1, sizeof(device_uri_t
))) == NULL
)
376 if ((device_uri
->uris
= cupsArrayNew(NULL
, NULL
)) == NULL
)
383 * Scan the value string for the regular expression and URI(s)...
386 value
++; /* Skip leading " */
388 for (start
= value
; *value
&& *value
!= '\"'; value
++)
389 if (*value
== '\\' && value
[1])
390 _cups_strcpy(value
, value
+ 1);
394 fputs("ERROR: Missing end quote for DeviceURI!\n", stderr
);
396 cupsArrayDelete(device_uri
->uris
);
404 if (regcomp(&(device_uri
->re
), start
, REG_EXTENDED
| REG_ICASE
))
406 fputs("ERROR: Bad regular expression for DeviceURI!\n", stderr
);
408 cupsArrayDelete(device_uri
->uris
);
416 while (isspace(*value
& 255))
422 for (start
= value
; *value
&& !isspace(*value
& 255); value
++);
427 cupsArrayAdd(device_uri
->uris
, strdup(start
));
431 * Add the device URI to the list and return it...
434 cupsArrayAdd(DeviceURIs
, device_uri
);
441 * 'alarm_handler()' - Handle alarm signals...
445 alarm_handler(int sig
) /* I - Signal number */
453 #if !defined(HAVE_SIGSET) && !defined(HAVE_SIGACTION)
454 signal(SIGALRM
, alarm_handler
);
455 #endif /* !HAVE_SIGSET && !HAVE_SIGACTION */
458 write(2, "DEBUG: ALARM!\n", 14);
463 * 'compare_cache()' - Compare two cache entries.
466 static int /* O - Result of comparison */
467 compare_cache(snmp_cache_t
*a
, /* I - First cache entry */
468 snmp_cache_t
*b
) /* I - Second cache entry */
470 return (_cups_strcasecmp(a
->addrname
, b
->addrname
));
475 * 'debug_printf()' - Display some debugging information.
479 debug_printf(const char *format
, /* I - Printf-style format string */
480 ...) /* I - Additional arguments as needed */
482 va_list ap
; /* Pointer to arguments */
488 va_start(ap
, format
);
489 vfprintf(stderr
, format
, ap
);
495 * 'fix_make_model()' - Fix common problems in the make-and-model string.
500 char *make_model
, /* I - New make-and-model string */
501 const char *old_make_model
, /* I - Old make-and-model string */
502 int make_model_size
) /* I - Size of new string buffer */
504 char *mmptr
; /* Pointer into make-and-model string */
508 * Fix some common problems with the make-and-model string so
509 * that printer driver detection works better...
512 if (!_cups_strncasecmp(old_make_model
, "Hewlett-Packard", 15))
515 * Strip leading Hewlett-Packard and hp prefixes and replace
516 * with a single HP manufacturer prefix...
519 mmptr
= (char *)old_make_model
+ 15;
521 while (isspace(*mmptr
& 255))
524 if (!_cups_strncasecmp(mmptr
, "hp", 2))
528 while (isspace(*mmptr
& 255))
535 strlcpy(make_model
+ 3, mmptr
, make_model_size
- 3);
537 else if (!_cups_strncasecmp(old_make_model
, "deskjet", 7))
538 snprintf(make_model
, make_model_size
, "HP DeskJet%s", old_make_model
+ 7);
539 else if (!_cups_strncasecmp(old_make_model
, "officejet", 9))
540 snprintf(make_model
, make_model_size
, "HP OfficeJet%s", old_make_model
+ 9);
541 else if (!_cups_strncasecmp(old_make_model
, "stylus_pro_", 11))
542 snprintf(make_model
, make_model_size
, "EPSON Stylus Pro %s",
543 old_make_model
+ 11);
545 strlcpy(make_model
, old_make_model
, make_model_size
);
547 if ((mmptr
= strstr(make_model
, ", Inc.,")) != NULL
)
550 * Strip inc. from name, e.g. "Tektronix, Inc., Phaser 560"
551 * becomes "Tektronix Phaser 560"...
554 _cups_strcpy(mmptr
, mmptr
+ 7);
557 if ((mmptr
= strstr(make_model
, " Network")) != NULL
)
560 * Drop unnecessary informational text, e.g. "Xerox DocuPrint N2025
561 * Network LaserJet - 2.12" becomes "Xerox DocuPrint N2025"...
567 if ((mmptr
= strchr(make_model
, ',')) != NULL
)
570 * Drop anything after a trailing comma...
579 * 'free_array()' - Free an array of strings.
583 free_array(cups_array_t
*a
) /* I - Array */
585 char *s
; /* Current string */
588 for (s
= (char *)cupsArrayFirst(a
); s
; s
= (char *)cupsArrayNext(a
))
596 * 'free_cache()' - Free the array of cached devices.
602 snmp_cache_t
*cache
; /* Cached device */
605 for (cache
= (snmp_cache_t
*)cupsArrayFirst(Devices
);
607 cache
= (snmp_cache_t
*)cupsArrayNext(Devices
))
609 free(cache
->addrname
);
617 if (cache
->make_and_model
)
618 free(cache
->make_and_model
);
623 cupsArrayDelete(Devices
);
629 * 'get_interface_addresses()' - Get the broadcast address(es) associated
633 static http_addrlist_t
* /* O - List of addresses */
634 get_interface_addresses(
635 const char *ifname
) /* I - Interface name */
637 struct ifaddrs
*addrs
, /* Interface address list */
638 *addr
; /* Current interface address */
639 http_addrlist_t
*first
, /* First address in list */
640 *last
, /* Last address in list */
641 *current
; /* Current address */
644 if (getifaddrs(&addrs
) < 0)
647 for (addr
= addrs
, first
= NULL
, last
= NULL
; addr
; addr
= addr
->ifa_next
)
648 if ((addr
->ifa_flags
& IFF_BROADCAST
) && addr
->ifa_broadaddr
&&
649 addr
->ifa_broadaddr
->sa_family
== AF_INET
&&
650 (!ifname
|| !strcmp(ifname
, addr
->ifa_name
)))
652 current
= calloc(1, sizeof(http_addrlist_t
));
654 memcpy(&(current
->addr
), addr
->ifa_broadaddr
,
655 sizeof(struct sockaddr_in
));
660 last
->next
= current
;
672 * 'list_device()' - List a device we found...
676 list_device(snmp_cache_t
*cache
) /* I - Cached device */
679 cupsBackendReport("network", cache
->uri
, cache
->make_and_model
,
680 cache
->info
, cache
->id
, cache
->location
);
685 * 'password_cb()' - Handle authentication requests.
687 * All we do right now is return NULL, indicating that no authentication
691 static const char * /* O - Password (NULL) */
692 password_cb(const char *prompt
) /* I - Prompt message */
694 (void)prompt
; /* Anti-compiler-warning-code */
701 * 'probe_device()' - Probe a device to discover whether it is a printer.
703 * TODO: Try using the Port Monitor MIB to discover the correct protocol
704 * to use - first need a commercially-available printer that supports
709 probe_device(snmp_cache_t
*device
) /* I - Device */
711 char uri
[1024], /* Full device URI */
712 *uriptr
, /* Pointer into URI */
713 *format
; /* Format string for device */
714 device_uri_t
*device_uri
; /* Current DeviceURI match */
717 debug_printf("DEBUG: %.3f Probing %s...\n", run_time(), device
->addrname
);
721 * If the printer supports Bonjour/mDNS, don't report it from the SNMP backend.
724 if (!try_connect(&(device
->address
), device
->addrname
, 5353))
726 debug_printf("DEBUG: %s supports mDNS, not reporting!\n", device
->addrname
);
729 #endif /* __APPLE__ */
732 * Lookup the device in the match table...
735 for (device_uri
= (device_uri_t
*)cupsArrayFirst(DeviceURIs
);
737 device_uri
= (device_uri_t
*)cupsArrayNext(DeviceURIs
))
738 if (device
->make_and_model
&&
739 !regexec(&(device_uri
->re
), device
->make_and_model
, 0, NULL
, 0))
742 * Found a match, add the URIs...
745 for (format
= (char *)cupsArrayFirst(device_uri
->uris
);
747 format
= (char *)cupsArrayNext(device_uri
->uris
))
749 for (uriptr
= uri
; *format
&& uriptr
< (uri
+ sizeof(uri
) - 1);)
750 if (*format
== '%' && format
[1] == 's')
753 * Insert hostname/address...
756 strlcpy(uriptr
, device
->addrname
, sizeof(uri
) - (uriptr
- uri
));
757 uriptr
+= strlen(uriptr
);
761 *uriptr
++ = *format
++;
765 update_cache(device
, uri
, NULL
, NULL
);
772 * Then try the standard ports...
775 if (!try_connect(&(device
->address
), device
->addrname
, 9100))
777 debug_printf("DEBUG: %s supports AppSocket!\n", device
->addrname
);
779 snprintf(uri
, sizeof(uri
), "socket://%s", device
->addrname
);
780 update_cache(device
, uri
, NULL
, NULL
);
782 else if (!try_connect(&(device
->address
), device
->addrname
, 515))
784 debug_printf("DEBUG: %s supports LPD!\n", device
->addrname
);
786 snprintf(uri
, sizeof(uri
), "lpd://%s/", device
->addrname
);
787 update_cache(device
, uri
, NULL
, NULL
);
793 * 'read_snmp_conf()' - Read the snmp.conf file.
797 read_snmp_conf(const char *address
) /* I - Single address to probe */
799 cups_file_t
*fp
; /* File pointer */
800 char filename
[1024], /* Filename */
801 line
[1024], /* Line from file */
802 *value
; /* Value on line */
803 int linenum
; /* Line number */
804 const char *cups_serverroot
; /* CUPS_SERVERROOT env var */
805 const char *debug
; /* CUPS_DEBUG_LEVEL env var */
806 const char *runtime
; /* CUPS_MAX_RUN_TIME env var */
810 * Initialize the global address and community lists...
813 Addresses
= cupsArrayNew(NULL
, NULL
);
814 Communities
= cupsArrayNew(NULL
, NULL
);
817 add_array(Addresses
, address
);
819 if ((debug
= getenv("CUPS_DEBUG_LEVEL")) != NULL
)
820 DebugLevel
= atoi(debug
);
822 if ((runtime
= getenv("CUPS_MAX_RUN_TIME")) != NULL
)
823 MaxRunTime
= atoi(runtime
);
826 * Find the snmp.conf file...
829 if ((cups_serverroot
= getenv("CUPS_SERVERROOT")) == NULL
)
830 cups_serverroot
= CUPS_SERVERROOT
;
832 snprintf(filename
, sizeof(filename
), "%s/snmp.conf", cups_serverroot
);
834 if ((fp
= cupsFileOpen(filename
, "r")) != NULL
)
837 * Read the snmp.conf file...
842 while (cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
))
845 fprintf(stderr
, "ERROR: Missing value on line %d of %s!\n", linenum
,
847 else if (!_cups_strcasecmp(line
, "Address"))
850 add_array(Addresses
, value
);
852 else if (!_cups_strcasecmp(line
, "Community"))
853 add_array(Communities
, value
);
854 else if (!_cups_strcasecmp(line
, "DebugLevel"))
855 DebugLevel
= atoi(value
);
856 else if (!_cups_strcasecmp(line
, "DeviceURI"))
860 "ERROR: Missing double quote for regular expression on "
861 "line %d of %s!\n", linenum
, filename
);
863 add_device_uri(value
);
865 else if (!_cups_strcasecmp(line
, "HostNameLookups"))
866 HostNameLookups
= !_cups_strcasecmp(value
, "on") ||
867 !_cups_strcasecmp(value
, "yes") ||
868 !_cups_strcasecmp(value
, "true") ||
869 !_cups_strcasecmp(value
, "double");
870 else if (!_cups_strcasecmp(line
, "MaxRunTime"))
871 MaxRunTime
= atoi(value
);
873 fprintf(stderr
, "ERROR: Unknown directive %s on line %d of %s!\n",
874 line
, linenum
, filename
);
881 * Use defaults if parameters are undefined...
884 if (cupsArrayCount(Addresses
) == 0)
887 * If we have no addresses, exit immediately...
891 "DEBUG: No address specified and no Address line in %s...\n",
896 if (cupsArrayCount(Communities
) == 0)
898 fputs("INFO: Using default SNMP Community public\n", stderr
);
899 add_array(Communities
, "public");
905 * 'read_snmp_response()' - Read and parse a SNMP response...
909 read_snmp_response(int fd
) /* I - SNMP socket file descriptor */
911 char addrname
[256]; /* Source address name */
912 cups_snmp_t packet
; /* Decoded packet */
913 snmp_cache_t key
, /* Search key */
914 *device
; /* Matching device */
918 * Read the response data...
921 if (!_cupsSNMPRead(fd
, &packet
, -1.0))
923 fprintf(stderr
, "ERROR: Unable to read data from socket: %s\n",
929 httpAddrLookup(&(packet
.address
), addrname
, sizeof(addrname
));
931 httpAddrString(&(packet
.address
), addrname
, sizeof(addrname
));
933 debug_printf("DEBUG: %.3f Received data from %s...\n", run_time(), addrname
);
936 * Look for the response status code in the SNMP message header...
941 fprintf(stderr
, "ERROR: Bad SNMP packet from %s: %s\n", addrname
,
947 debug_printf("DEBUG: community=\"%s\"\n", packet
.community
);
948 debug_printf("DEBUG: request-id=%d\n", packet
.request_id
);
949 debug_printf("DEBUG: error-status=%d\n", packet
.error_status
);
951 if (packet
.error_status
&& packet
.request_id
!= DEVICE_TYPE
)
955 * Find a matching device in the cache...
958 key
.addrname
= addrname
;
959 device
= (snmp_cache_t
*)cupsArrayFind(Devices
, &key
);
962 * Process the message...
965 switch (packet
.request_id
)
969 * Got the device type response...
974 debug_printf("DEBUG: Discarding duplicate device type for \"%s\"...\n",
980 * Add the device and request the device data...
983 add_cache(&(packet
.address
), addrname
, NULL
, NULL
, NULL
);
985 _cupsSNMPWrite(fd
, &(packet
.address
), CUPS_SNMP_VERSION_1
,
986 packet
.community
, CUPS_ASN1_GET_REQUEST
,
987 DEVICE_DESCRIPTION
, DescriptionOID
);
988 _cupsSNMPWrite(fd
, &(packet
.address
), CUPS_SNMP_VERSION_1
,
989 packet
.community
, CUPS_ASN1_GET_REQUEST
,
990 DEVICE_ID
, DeviceIdOID
);
991 _cupsSNMPWrite(fd
, &(packet
.address
), CUPS_SNMP_VERSION_1
,
992 packet
.community
, CUPS_ASN1_GET_REQUEST
,
994 _cupsSNMPWrite(fd
, &(packet
.address
), CUPS_SNMP_VERSION_1
,
995 packet
.community
, CUPS_ASN1_GET_REQUEST
,
996 DEVICE_LOCATION
, LocationOID
);
997 _cupsSNMPWrite(fd
, &(packet
.address
), CUPS_SNMP_VERSION_1
,
998 packet
.community
, CUPS_ASN1_GET_REQUEST
,
999 DEVICE_PRODUCT
, LexmarkProductOID
);
1000 _cupsSNMPWrite(fd
, &(packet
.address
), CUPS_SNMP_VERSION_1
,
1001 packet
.community
, CUPS_ASN1_GET_REQUEST
,
1002 DEVICE_PRODUCT
, LexmarkProductOID2
);
1003 _cupsSNMPWrite(fd
, &(packet
.address
), CUPS_SNMP_VERSION_1
,
1004 packet
.community
, CUPS_ASN1_GET_REQUEST
,
1005 DEVICE_ID
, LexmarkDeviceIdOID
);
1006 _cupsSNMPWrite(fd
, &(packet
.address
), CUPS_SNMP_VERSION_1
,
1007 packet
.community
, CUPS_ASN1_GET_REQUEST
,
1008 DEVICE_PRODUCT
, XeroxProductOID
);
1011 case DEVICE_DESCRIPTION
:
1012 if (device
&& packet
.object_type
== CUPS_ASN1_OCTET_STRING
)
1015 * Update an existing cache entry...
1018 char make_model
[256]; /* Make and model */
1021 if (strchr((char *)packet
.object_value
.string
.bytes
, ':') &&
1022 strchr((char *)packet
.object_value
.string
.bytes
, ';'))
1025 * Description is the IEEE-1284 device ID...
1028 char *ptr
; /* Pointer into device ID */
1030 for (ptr
= (char *)packet
.object_value
.string
.bytes
; *ptr
; ptr
++)
1032 *ptr
= ';'; /* A lot of bad printers put a newline */
1034 device
->id
= strdup((char *)packet
.object_value
.string
.bytes
);
1036 backendGetMakeModel((char *)packet
.object_value
.string
.bytes
,
1037 make_model
, sizeof(make_model
));
1042 device
->info
= strdup(make_model
);
1047 * Description is plain text...
1050 fix_make_model(make_model
, (char *)packet
.object_value
.string
.bytes
,
1051 sizeof(make_model
));
1056 device
->info
= strdup((char *)packet
.object_value
.string
.bytes
);
1059 if (!device
->make_and_model
)
1060 device
->make_and_model
= strdup(make_model
);
1065 if (device
&& packet
.object_type
== CUPS_ASN1_OCTET_STRING
&&
1067 strlen(device
->id
) < packet
.object_value
.string
.num_bytes
))
1070 * Update an existing cache entry...
1073 char make_model
[256]; /* Make and model */
1074 char *ptr
; /* Pointer into device ID */
1076 for (ptr
= (char *)packet
.object_value
.string
.bytes
; *ptr
; ptr
++)
1078 *ptr
= ';'; /* A lot of bad printers put a newline */
1082 device
->id
= strdup((char *)packet
.object_value
.string
.bytes
);
1085 * Convert the ID to a make and model string...
1088 backendGetMakeModel((char *)packet
.object_value
.string
.bytes
,
1089 make_model
, sizeof(make_model
));
1090 if (device
->make_and_model
)
1091 free(device
->make_and_model
);
1093 device
->make_and_model
= strdup(make_model
);
1097 case DEVICE_LOCATION
:
1098 if (device
&& packet
.object_type
== CUPS_ASN1_OCTET_STRING
&&
1100 device
->location
= strdup((char *)packet
.object_value
.string
.bytes
);
1103 case DEVICE_PRODUCT
:
1104 if (device
&& packet
.object_type
== CUPS_ASN1_OCTET_STRING
&&
1108 * Update an existing cache entry...
1112 device
->info
= strdup((char *)packet
.object_value
.string
.bytes
);
1114 if (device
->make_and_model
)
1115 free(device
->make_and_model
);
1117 device
->make_and_model
= strdup((char *)packet
.object_value
.string
.bytes
);
1122 if (device
&& packet
.object_type
== CUPS_ASN1_OCTET_STRING
&&
1123 !device
->uri
&& packet
.object_value
.string
.num_bytes
> 3)
1126 * Update an existing cache entry...
1129 char scheme
[32], /* URI scheme */
1130 userpass
[256], /* Username:password in URI */
1131 hostname
[256], /* Hostname in URI */
1132 resource
[1024]; /* Resource path in URI */
1133 int port
; /* Port number in URI */
1135 if (!strncmp((char *)packet
.object_value
.string
.bytes
, "lpr:", 4))
1138 * We want "lpd://..." for the URI...
1141 packet
.object_value
.string
.bytes
[2] = 'd';
1144 if (httpSeparateURI(HTTP_URI_CODING_ALL
,
1145 (char *)packet
.object_value
.string
.bytes
,
1146 scheme
, sizeof(scheme
),
1147 userpass
, sizeof(userpass
),
1148 hostname
, sizeof(hostname
), &port
,
1149 resource
, sizeof(resource
)) >= HTTP_URI_OK
)
1150 device
->uri
= strdup((char *)packet
.object_value
.string
.bytes
);
1158 * 'run_time()' - Return the total running time...
1161 static double /* O - Number of seconds */
1164 struct timeval curtime
; /* Current time */
1167 gettimeofday(&curtime
, NULL
);
1169 return (curtime
.tv_sec
- StartTime
.tv_sec
+
1170 0.000001 * (curtime
.tv_usec
- StartTime
.tv_usec
));
1175 * 'scan_devices()' - Scan for devices using SNMP.
1179 scan_devices(int ipv4
, /* I - SNMP IPv4 socket */
1180 int ipv6
) /* I - SNMP IPv6 socket */
1182 int fd
, /* File descriptor for this address */
1183 busy
; /* Are we busy processing something? */
1184 char *address
, /* Current address */
1185 *community
; /* Current community */
1186 fd_set input
; /* Input set for select() */
1187 struct timeval timeout
; /* Timeout for select() */
1188 time_t endtime
; /* End time for scan */
1189 http_addrlist_t
*addrs
, /* List of addresses */
1190 *addr
; /* Current address */
1191 snmp_cache_t
*device
; /* Current device */
1192 char temp
[1024]; /* Temporary address string */
1195 gettimeofday(&StartTime
, NULL
);
1198 * First send all of the broadcast queries...
1201 for (address
= (char *)cupsArrayFirst(Addresses
);
1203 address
= (char *)cupsArrayNext(Addresses
))
1205 if (!strcmp(address
, "@LOCAL"))
1206 addrs
= get_interface_addresses(NULL
);
1207 else if (!strncmp(address
, "@IF(", 4))
1209 char ifname
[255]; /* Interface name */
1211 strlcpy(ifname
, address
+ 4, sizeof(ifname
));
1213 ifname
[strlen(ifname
) - 1] = '\0';
1215 addrs
= get_interface_addresses(ifname
);
1218 addrs
= httpAddrGetList(address
, AF_UNSPEC
, NULL
);
1222 fprintf(stderr
, "ERROR: Unable to scan \"%s\"!\n", address
);
1226 for (community
= (char *)cupsArrayFirst(Communities
);
1228 community
= (char *)cupsArrayNext(Communities
))
1230 debug_printf("DEBUG: Scanning for devices in \"%s\" via \"%s\"...\n",
1231 community
, address
);
1233 for (addr
= addrs
; addr
; addr
= addr
->next
)
1236 if (_httpAddrFamily(&(addr
->addr
)) == AF_INET6
)
1239 #endif /* AF_INET6 */
1242 debug_printf("DEBUG: Sending get request to %s...\n",
1243 httpAddrString(&(addr
->addr
), temp
, sizeof(temp
)));
1245 _cupsSNMPWrite(fd
, &(addr
->addr
), CUPS_SNMP_VERSION_1
, community
,
1246 CUPS_ASN1_GET_REQUEST
, DEVICE_TYPE
, DeviceTypeOID
);
1250 httpAddrFreeList(addrs
);
1254 * Then read any responses that come in over the next 3 seconds...
1257 endtime
= time(NULL
) + MaxRunTime
;
1261 while (time(NULL
) < endtime
)
1264 timeout
.tv_usec
= 0;
1266 FD_SET(ipv4
, &input
);
1268 FD_SET(ipv6
, &input
);
1270 fd
= ipv4
> ipv6
? ipv4
: ipv6
;
1271 if (select(fd
+ 1, &input
, NULL
, NULL
, &timeout
) < 0)
1273 fprintf(stderr
, "ERROR: %.3f select() for %d/%d failed: %s\n", run_time(),
1274 ipv4
, ipv6
, strerror(errno
));
1280 if (FD_ISSET(ipv4
, &input
))
1282 read_snmp_response(ipv4
);
1286 if (ipv6
>= 0 && FD_ISSET(ipv6
, &input
))
1288 read_snmp_response(ipv6
);
1295 * List devices with complete information...
1298 int sent_something
= 0;
1300 for (device
= (snmp_cache_t
*)cupsArrayFirst(Devices
);
1302 device
= (snmp_cache_t
*)cupsArrayNext(Devices
))
1303 if (!device
->sent
&& device
->info
&& device
->make_and_model
)
1306 list_device(device
);
1308 probe_device(device
);
1310 device
->sent
= sent_something
= 1;
1313 if (!sent_something
)
1318 debug_printf("DEBUG: %.3f Scan complete!\n", run_time());
1323 * 'try_connect()' - Try connecting on a port...
1326 static int /* O - 0 on success or -1 on error */
1327 try_connect(http_addr_t
*addr
, /* I - Socket address */
1328 const char *addrname
, /* I - Hostname or IP address */
1329 int port
) /* I - Port number */
1331 int fd
; /* Socket */
1332 int status
; /* Connection status */
1335 debug_printf("DEBUG: %.3f Trying %s://%s:%d...\n", run_time(),
1336 port
== 515 ? "lpd" : "socket", addrname
, port
);
1338 if ((fd
= socket(_httpAddrFamily(addr
), SOCK_STREAM
, 0)) < 0)
1340 fprintf(stderr
, "ERROR: Unable to create socket: %s\n",
1345 _httpAddrSetPort(addr
, port
);
1349 status
= connect(fd
, (void *)addr
, httpAddrLength(addr
));
1359 * 'update_cache()' - Update a cached device...
1363 update_cache(snmp_cache_t
*device
, /* I - Device */
1364 const char *uri
, /* I - Device URI */
1365 const char *id
, /* I - Device ID */
1366 const char *make_model
) /* I - Device make and model */
1371 device
->uri
= strdup(uri
);
1378 device
->id
= strdup(id
);
1383 if (device
->make_and_model
)
1384 free(device
->make_and_model
);
1386 device
->make_and_model
= strdup(make_model
);
1389 list_device(device
);
1394 * End of "$Id: snmp.c 7810 2008-07-29 01:11:15Z mike $".