2 * "$Id: snmp.c 7810 2008-07-29 01:11:15Z mike $"
4 * SNMP discovery backend for the Common UNIX Printing System (CUPS).
6 * Copyright 2007-2009 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 fd
);
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 fd
; /* SNMP socket */
206 #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
207 struct sigaction action
; /* Actions for POSIX signals */
208 #endif /* HAVE_SIGACTION && !HAVE_SIGSET */
212 * Check command-line options...
217 fputs(_("Usage: snmp [host-or-ip-address]\n"), stderr
);
222 * Set the password callback for IPP operations...
225 cupsSetPasswordCB(password_cb
);
228 * Catch SIGALRM signals...
232 sigset(SIGALRM
, alarm_handler
);
233 #elif defined(HAVE_SIGACTION)
234 memset(&action
, 0, sizeof(action
));
236 sigemptyset(&action
.sa_mask
);
237 sigaddset(&action
.sa_mask
, SIGALRM
);
238 action
.sa_handler
= alarm_handler
;
239 sigaction(SIGALRM
, &action
, NULL
);
241 signal(SIGALRM
, alarm_handler
);
242 #endif /* HAVE_SIGSET */
245 * Open the SNMP socket...
248 if ((fd
= _cupsSNMPOpen(AF_INET
)) < 0)
252 * Read the configuration file and any cache data...
255 read_snmp_conf(argv
[1]);
257 _cupsSNMPSetDebug(DebugLevel
);
259 Devices
= cupsArrayNew((cups_array_func_t
)compare_cache
, NULL
);
262 * Scan for devices...
268 * Close, free, and return with no errors...
273 free_array(Addresses
);
274 free_array(Communities
);
282 * 'add_array()' - Add a string to an array.
285 static char * /* O - New string */
286 add_array(cups_array_t
*a
, /* I - Array */
287 const char *s
) /* I - String to add */
289 char *dups
; /* New string */
294 cupsArrayAdd(a
, dups
);
301 * 'add_cache()' - Add a cached device...
305 add_cache(http_addr_t
*addr
, /* I - Device IP address */
306 const char *addrname
, /* I - IP address or name string */
307 const char *uri
, /* I - Device URI */
308 const char *id
, /* I - 1284 device ID */
309 const char *make_and_model
) /* I - Make and model */
311 snmp_cache_t
*temp
; /* New device entry */
314 debug_printf("DEBUG: add_cache(addr=%p, addrname=\"%s\", uri=\"%s\", "
315 "id=\"%s\", make_and_model=\"%s\")\n",
316 addr
, addrname
, uri
? uri
: "(null)", id
? id
: "(null)",
317 make_and_model
? make_and_model
: "(null)");
319 temp
= calloc(1, sizeof(snmp_cache_t
));
320 memcpy(&(temp
->address
), addr
, sizeof(temp
->address
));
322 temp
->addrname
= strdup(addrname
);
325 temp
->uri
= strdup(uri
);
328 temp
->id
= strdup(id
);
331 temp
->make_and_model
= strdup(make_and_model
);
333 cupsArrayAdd(Devices
, temp
);
341 * 'add_device_uri()' - Add a device URI to the cache.
343 * The value string is modified (chopped up) as needed.
346 static device_uri_t
* /* O - Device URI */
347 add_device_uri(char *value
) /* I - Value from snmp.conf */
349 device_uri_t
*device_uri
; /* Device URI */
350 char *start
; /* Start of value */
354 * Allocate memory as needed...
358 DeviceURIs
= cupsArrayNew(NULL
, NULL
);
363 if ((device_uri
= calloc(1, sizeof(device_uri_t
))) == NULL
)
366 if ((device_uri
->uris
= cupsArrayNew(NULL
, NULL
)) == NULL
)
373 * Scan the value string for the regular expression and URI(s)...
376 value
++; /* Skip leading " */
378 for (start
= value
; *value
&& *value
!= '\"'; value
++)
379 if (*value
== '\\' && value
[1])
380 _cups_strcpy(value
, value
+ 1);
384 fputs("ERROR: Missing end quote for DeviceURI!\n", stderr
);
386 cupsArrayDelete(device_uri
->uris
);
394 if (regcomp(&(device_uri
->re
), start
, REG_EXTENDED
| REG_ICASE
))
396 fputs("ERROR: Bad regular expression for DeviceURI!\n", stderr
);
398 cupsArrayDelete(device_uri
->uris
);
406 while (isspace(*value
& 255))
412 for (start
= value
; *value
&& !isspace(*value
& 255); value
++);
417 cupsArrayAdd(device_uri
->uris
, strdup(start
));
421 * Add the device URI to the list and return it...
424 cupsArrayAdd(DeviceURIs
, device_uri
);
431 * 'alarm_handler()' - Handle alarm signals...
435 alarm_handler(int sig
) /* I - Signal number */
443 #if !defined(HAVE_SIGSET) && !defined(HAVE_SIGACTION)
444 signal(SIGALRM
, alarm_handler
);
445 #endif /* !HAVE_SIGSET && !HAVE_SIGACTION */
448 write(2, "DEBUG: ALARM!\n", 14);
453 * 'compare_cache()' - Compare two cache entries.
456 static int /* O - Result of comparison */
457 compare_cache(snmp_cache_t
*a
, /* I - First cache entry */
458 snmp_cache_t
*b
) /* I - Second cache entry */
460 return (strcasecmp(a
->addrname
, b
->addrname
));
465 * 'debug_printf()' - Display some debugging information.
469 debug_printf(const char *format
, /* I - Printf-style format string */
470 ...) /* I - Additional arguments as needed */
472 va_list ap
; /* Pointer to arguments */
478 va_start(ap
, format
);
479 vfprintf(stderr
, format
, ap
);
485 * 'fix_make_model()' - Fix common problems in the make-and-model string.
490 char *make_model
, /* I - New make-and-model string */
491 const char *old_make_model
, /* I - Old make-and-model string */
492 int make_model_size
) /* I - Size of new string buffer */
494 char *mmptr
; /* Pointer into make-and-model string */
498 * Fix some common problems with the make-and-model string so
499 * that printer driver detection works better...
502 if (!strncasecmp(old_make_model
, "Hewlett-Packard", 15))
505 * Strip leading Hewlett-Packard and hp prefixes and replace
506 * with a single HP manufacturer prefix...
509 mmptr
= (char *)old_make_model
+ 15;
511 while (isspace(*mmptr
& 255))
514 if (!strncasecmp(mmptr
, "hp", 2))
518 while (isspace(*mmptr
& 255))
525 strlcpy(make_model
+ 3, mmptr
, make_model_size
- 3);
527 else if (!strncasecmp(old_make_model
, "deskjet", 7))
528 snprintf(make_model
, make_model_size
, "HP DeskJet%s", old_make_model
+ 7);
529 else if (!strncasecmp(old_make_model
, "officejet", 9))
530 snprintf(make_model
, make_model_size
, "HP OfficeJet%s", old_make_model
+ 9);
531 else if (!strncasecmp(old_make_model
, "stylus_pro_", 11))
532 snprintf(make_model
, make_model_size
, "EPSON Stylus Pro %s",
533 old_make_model
+ 11);
535 strlcpy(make_model
, old_make_model
, make_model_size
);
537 if ((mmptr
= strstr(make_model
, ", Inc.,")) != NULL
)
540 * Strip inc. from name, e.g. "Tektronix, Inc., Phaser 560"
541 * becomes "Tektronix Phaser 560"...
544 _cups_strcpy(mmptr
, mmptr
+ 7);
547 if ((mmptr
= strstr(make_model
, " Network")) != NULL
)
550 * Drop unnecessary informational text, e.g. "Xerox DocuPrint N2025
551 * Network LaserJet - 2.12" becomes "Xerox DocuPrint N2025"...
557 if ((mmptr
= strchr(make_model
, ',')) != NULL
)
560 * Drop anything after a trailing comma...
569 * 'free_array()' - Free an array of strings.
573 free_array(cups_array_t
*a
) /* I - Array */
575 char *s
; /* Current string */
578 for (s
= (char *)cupsArrayFirst(a
); s
; s
= (char *)cupsArrayNext(a
))
586 * 'free_cache()' - Free the array of cached devices.
592 snmp_cache_t
*cache
; /* Cached device */
595 for (cache
= (snmp_cache_t
*)cupsArrayFirst(Devices
);
597 cache
= (snmp_cache_t
*)cupsArrayNext(Devices
))
599 free(cache
->addrname
);
607 if (cache
->make_and_model
)
608 free(cache
->make_and_model
);
613 cupsArrayDelete(Devices
);
619 * 'get_interface_addresses()' - Get the broadcast address(es) associated
623 static http_addrlist_t
* /* O - List of addresses */
624 get_interface_addresses(
625 const char *ifname
) /* I - Interface name */
627 struct ifaddrs
*addrs
, /* Interface address list */
628 *addr
; /* Current interface address */
629 http_addrlist_t
*first
, /* First address in list */
630 *last
, /* Last address in list */
631 *current
; /* Current address */
634 if (getifaddrs(&addrs
) < 0)
637 for (addr
= addrs
, first
= NULL
, last
= NULL
; addr
; addr
= addr
->ifa_next
)
638 if ((addr
->ifa_flags
& IFF_BROADCAST
) && addr
->ifa_broadaddr
&&
639 addr
->ifa_broadaddr
->sa_family
== AF_INET
&&
640 (!ifname
|| !strcmp(ifname
, addr
->ifa_name
)))
642 current
= calloc(1, sizeof(http_addrlist_t
));
644 memcpy(&(current
->addr
), addr
->ifa_broadaddr
,
645 sizeof(struct sockaddr_in
));
650 last
->next
= current
;
662 * 'list_device()' - List a device we found...
666 list_device(snmp_cache_t
*cache
) /* I - Cached device */
669 cupsBackendReport("network", cache
->uri
, cache
->make_and_model
,
670 cache
->info
, cache
->id
, cache
->location
);
675 * 'password_cb()' - Handle authentication requests.
677 * All we do right now is return NULL, indicating that no authentication
681 static const char * /* O - Password (NULL) */
682 password_cb(const char *prompt
) /* I - Prompt message */
684 (void)prompt
; /* Anti-compiler-warning-code */
691 * 'probe_device()' - Probe a device to discover whether it is a printer.
693 * TODO: Try using the Port Monitor MIB to discover the correct protocol
694 * to use - first need a commercially-available printer that supports
699 probe_device(snmp_cache_t
*device
) /* I - Device */
701 char uri
[1024], /* Full device URI */
702 *uriptr
, /* Pointer into URI */
703 *format
; /* Format string for device */
704 device_uri_t
*device_uri
; /* Current DeviceURI match */
707 debug_printf("DEBUG: %.3f Probing %s...\n", run_time(), device
->addrname
);
711 * TODO: Try an mDNS query first, and then fallback on direct probes...
714 if (!try_connect(&(device
->address
), device
->addrname
, 5353))
716 debug_printf("DEBUG: %s supports mDNS, not reporting!\n", device
->addrname
);
719 #endif /* __APPLE__ */
722 * Lookup the device in the match table...
725 for (device_uri
= (device_uri_t
*)cupsArrayFirst(DeviceURIs
);
727 device_uri
= (device_uri_t
*)cupsArrayNext(DeviceURIs
))
728 if (device
->make_and_model
&&
729 !regexec(&(device_uri
->re
), device
->make_and_model
, 0, NULL
, 0))
732 * Found a match, add the URIs...
735 for (format
= (char *)cupsArrayFirst(device_uri
->uris
);
737 format
= (char *)cupsArrayNext(device_uri
->uris
))
739 for (uriptr
= uri
; *format
&& uriptr
< (uri
+ sizeof(uri
) - 1);)
740 if (*format
== '%' && format
[1] == 's')
743 * Insert hostname/address...
746 strlcpy(uriptr
, device
->addrname
, sizeof(uri
) - (uriptr
- uri
));
747 uriptr
+= strlen(uriptr
);
751 *uriptr
++ = *format
++;
755 update_cache(device
, uri
, NULL
, NULL
);
762 * Then try the standard ports...
765 if (!try_connect(&(device
->address
), device
->addrname
, 9100))
767 debug_printf("DEBUG: %s supports AppSocket!\n", device
->addrname
);
769 snprintf(uri
, sizeof(uri
), "socket://%s", device
->addrname
);
770 update_cache(device
, uri
, NULL
, NULL
);
772 else if (!try_connect(&(device
->address
), device
->addrname
, 515))
774 debug_printf("DEBUG: %s supports LPD!\n", device
->addrname
);
776 snprintf(uri
, sizeof(uri
), "lpd://%s/", device
->addrname
);
777 update_cache(device
, uri
, NULL
, NULL
);
783 * 'read_snmp_conf()' - Read the snmp.conf file.
787 read_snmp_conf(const char *address
) /* I - Single address to probe */
789 cups_file_t
*fp
; /* File pointer */
790 char filename
[1024], /* Filename */
791 line
[1024], /* Line from file */
792 *value
; /* Value on line */
793 int linenum
; /* Line number */
794 const char *cups_serverroot
; /* CUPS_SERVERROOT env var */
795 const char *debug
; /* CUPS_DEBUG_LEVEL env var */
796 const char *runtime
; /* CUPS_MAX_RUN_TIME env var */
800 * Initialize the global address and community lists...
803 Addresses
= cupsArrayNew(NULL
, NULL
);
804 Communities
= cupsArrayNew(NULL
, NULL
);
807 add_array(Addresses
, address
);
809 if ((debug
= getenv("CUPS_DEBUG_LEVEL")) != NULL
)
810 DebugLevel
= atoi(debug
);
812 if ((runtime
= getenv("CUPS_MAX_RUN_TIME")) != NULL
)
813 MaxRunTime
= atoi(runtime
);
816 * Find the snmp.conf file...
819 if ((cups_serverroot
= getenv("CUPS_SERVERROOT")) == NULL
)
820 cups_serverroot
= CUPS_SERVERROOT
;
822 snprintf(filename
, sizeof(filename
), "%s/snmp.conf", cups_serverroot
);
824 if ((fp
= cupsFileOpen(filename
, "r")) != NULL
)
827 * Read the snmp.conf file...
832 while (cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
))
835 fprintf(stderr
, "ERROR: Missing value on line %d of %s!\n", linenum
,
837 else if (!strcasecmp(line
, "Address"))
840 add_array(Addresses
, value
);
842 else if (!strcasecmp(line
, "Community"))
843 add_array(Communities
, value
);
844 else if (!strcasecmp(line
, "DebugLevel"))
845 DebugLevel
= atoi(value
);
846 else if (!strcasecmp(line
, "DeviceURI"))
850 "ERROR: Missing double quote for regular expression on "
851 "line %d of %s!\n", linenum
, filename
);
853 add_device_uri(value
);
855 else if (!strcasecmp(line
, "HostNameLookups"))
856 HostNameLookups
= !strcasecmp(value
, "on") ||
857 !strcasecmp(value
, "yes") ||
858 !strcasecmp(value
, "true") ||
859 !strcasecmp(value
, "double");
860 else if (!strcasecmp(line
, "MaxRunTime"))
861 MaxRunTime
= atoi(value
);
863 fprintf(stderr
, "ERROR: Unknown directive %s on line %d of %s!\n",
864 line
, linenum
, filename
);
871 * Use defaults if parameters are undefined...
874 if (cupsArrayCount(Addresses
) == 0)
877 * If we have no addresses, exit immediately...
881 "DEBUG: No address specified and no Address line in %s...\n",
886 if (cupsArrayCount(Communities
) == 0)
888 fputs("INFO: Using default SNMP Community public\n", stderr
);
889 add_array(Communities
, "public");
895 * 'read_snmp_response()' - Read and parse a SNMP response...
899 read_snmp_response(int fd
) /* I - SNMP socket file descriptor */
901 char addrname
[256]; /* Source address name */
902 cups_snmp_t packet
; /* Decoded packet */
903 snmp_cache_t key
, /* Search key */
904 *device
; /* Matching device */
908 * Read the response data...
911 if (!_cupsSNMPRead(fd
, &packet
, -1.0))
913 fprintf(stderr
, "ERROR: Unable to read data from socket: %s\n",
919 httpAddrLookup(&(packet
.address
), addrname
, sizeof(addrname
));
921 httpAddrString(&(packet
.address
), addrname
, sizeof(addrname
));
923 debug_printf("DEBUG: %.3f Received data from %s...\n", run_time(), addrname
);
926 * Look for the response status code in the SNMP message header...
931 fprintf(stderr
, "ERROR: Bad SNMP packet from %s: %s\n", addrname
,
937 debug_printf("DEBUG: community=\"%s\"\n", packet
.community
);
938 debug_printf("DEBUG: request-id=%d\n", packet
.request_id
);
939 debug_printf("DEBUG: error-status=%d\n", packet
.error_status
);
941 if (packet
.error_status
)
945 * Find a matching device in the cache...
948 key
.addrname
= addrname
;
949 device
= (snmp_cache_t
*)cupsArrayFind(Devices
, &key
);
952 * Process the message...
955 switch (packet
.request_id
)
959 * Got the device type response...
964 debug_printf("DEBUG: Discarding duplicate device type for \"%s\"...\n",
970 * Add the device and request the device data...
973 add_cache(&(packet
.address
), addrname
, NULL
, NULL
, NULL
);
975 _cupsSNMPWrite(fd
, &(packet
.address
), CUPS_SNMP_VERSION_1
,
976 packet
.community
, CUPS_ASN1_GET_REQUEST
,
977 DEVICE_DESCRIPTION
, DescriptionOID
);
978 _cupsSNMPWrite(fd
, &(packet
.address
), CUPS_SNMP_VERSION_1
,
979 packet
.community
, CUPS_ASN1_GET_REQUEST
,
980 DEVICE_ID
, DeviceIdOID
);
981 _cupsSNMPWrite(fd
, &(packet
.address
), CUPS_SNMP_VERSION_1
,
982 packet
.community
, CUPS_ASN1_GET_REQUEST
,
984 _cupsSNMPWrite(fd
, &(packet
.address
), CUPS_SNMP_VERSION_1
,
985 packet
.community
, CUPS_ASN1_GET_REQUEST
,
986 DEVICE_LOCATION
, LocationOID
);
987 _cupsSNMPWrite(fd
, &(packet
.address
), CUPS_SNMP_VERSION_1
,
988 packet
.community
, CUPS_ASN1_GET_REQUEST
,
989 DEVICE_PRODUCT
, LexmarkProductOID
);
990 _cupsSNMPWrite(fd
, &(packet
.address
), CUPS_SNMP_VERSION_1
,
991 packet
.community
, CUPS_ASN1_GET_REQUEST
,
992 DEVICE_PRODUCT
, LexmarkProductOID2
);
993 _cupsSNMPWrite(fd
, &(packet
.address
), CUPS_SNMP_VERSION_1
,
994 packet
.community
, CUPS_ASN1_GET_REQUEST
,
995 DEVICE_ID
, LexmarkDeviceIdOID
);
996 _cupsSNMPWrite(fd
, &(packet
.address
), CUPS_SNMP_VERSION_1
,
997 packet
.community
, CUPS_ASN1_GET_REQUEST
,
998 DEVICE_PRODUCT
, XeroxProductOID
);
1001 case DEVICE_DESCRIPTION
:
1002 if (device
&& packet
.object_type
== CUPS_ASN1_OCTET_STRING
)
1005 * Update an existing cache entry...
1008 char make_model
[256]; /* Make and model */
1011 if (strchr((char *)packet
.object_value
.string
.bytes
, ':') &&
1012 strchr((char *)packet
.object_value
.string
.bytes
, ';'))
1015 * Description is the IEEE-1284 device ID...
1019 device
->id
= strdup((char *)packet
.object_value
.string
.bytes
);
1021 backendGetMakeModel((char *)packet
.object_value
.string
.bytes
,
1022 make_model
, sizeof(make_model
));
1027 device
->info
= strdup(make_model
);
1032 * Description is plain text...
1035 fix_make_model(make_model
, (char *)packet
.object_value
.string
.bytes
,
1036 sizeof(make_model
));
1041 device
->info
= strdup((char *)packet
.object_value
.string
.bytes
);
1044 if (!device
->make_and_model
)
1045 device
->make_and_model
= strdup(make_model
);
1050 if (device
&& packet
.object_type
== CUPS_ASN1_OCTET_STRING
&&
1052 strlen(device
->id
) < packet
.object_value
.string
.num_bytes
))
1055 * Update an existing cache entry...
1058 char make_model
[256]; /* Make and model */
1064 device
->id
= strdup((char *)packet
.object_value
.string
.bytes
);
1067 * Convert the ID to a make and model string...
1070 backendGetMakeModel((char *)packet
.object_value
.string
.bytes
,
1071 make_model
, sizeof(make_model
));
1072 if (device
->make_and_model
)
1073 free(device
->make_and_model
);
1075 device
->make_and_model
= strdup(make_model
);
1079 case DEVICE_LOCATION
:
1080 if (device
&& packet
.object_type
== CUPS_ASN1_OCTET_STRING
&&
1082 device
->location
= strdup((char *)packet
.object_value
.string
.bytes
);
1085 case DEVICE_PRODUCT
:
1086 if (device
&& packet
.object_type
== CUPS_ASN1_OCTET_STRING
&&
1090 * Update an existing cache entry...
1094 device
->info
= strdup((char *)packet
.object_value
.string
.bytes
);
1096 if (device
->make_and_model
)
1097 free(device
->make_and_model
);
1099 device
->make_and_model
= strdup((char *)packet
.object_value
.string
.bytes
);
1104 if (device
&& packet
.object_type
== CUPS_ASN1_OCTET_STRING
&&
1105 !device
->uri
&& packet
.object_value
.string
.num_bytes
> 0)
1108 * Update an existing cache entry...
1111 if (!strncmp((char *)packet
.object_value
.string
.bytes
, "lpr:", 4))
1114 * We want "lpd://..." for the URI...
1117 packet
.object_value
.string
.bytes
[2] = 'd';
1120 device
->uri
= strdup((char *)packet
.object_value
.string
.bytes
);
1128 * 'run_time()' - Return the total running time...
1131 static double /* O - Number of seconds */
1134 struct timeval curtime
; /* Current time */
1137 gettimeofday(&curtime
, NULL
);
1139 return (curtime
.tv_sec
- StartTime
.tv_sec
+
1140 0.000001 * (curtime
.tv_usec
- StartTime
.tv_usec
));
1145 * 'scan_devices()' - Scan for devices using SNMP.
1149 scan_devices(int fd
) /* I - SNMP socket */
1151 char *address
, /* Current address */
1152 *community
; /* Current community */
1153 fd_set input
; /* Input set for select() */
1154 struct timeval timeout
; /* Timeout for select() */
1155 time_t endtime
; /* End time for scan */
1156 http_addrlist_t
*addrs
, /* List of addresses */
1157 *addr
; /* Current address */
1158 snmp_cache_t
*device
; /* Current device */
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 */
1178 strlcpy(ifname
, address
+ 4, sizeof(ifname
));
1180 ifname
[strlen(ifname
) - 1] = '\0';
1182 addrs
= get_interface_addresses(ifname
);
1185 addrs
= httpAddrGetList(address
, AF_INET
, NULL
);
1189 fprintf(stderr
, "ERROR: Unable to scan \"%s\"!\n", address
);
1193 for (community
= (char *)cupsArrayFirst(Communities
);
1195 community
= (char *)cupsArrayNext(Communities
))
1197 debug_printf("DEBUG: Scanning for devices in \"%s\" via \"%s\"...\n",
1198 community
, address
);
1200 for (addr
= addrs
; addr
; addr
= addr
->next
)
1201 _cupsSNMPWrite(fd
, &(addr
->addr
), CUPS_SNMP_VERSION_1
, community
,
1202 CUPS_ASN1_GET_REQUEST
, DEVICE_TYPE
, DeviceTypeOID
);
1205 httpAddrFreeList(addrs
);
1209 * Then read any responses that come in over the next 3 seconds...
1212 endtime
= time(NULL
) + MaxRunTime
;
1216 while (time(NULL
) < endtime
)
1219 timeout
.tv_usec
= 0;
1222 if (select(fd
+ 1, &input
, NULL
, NULL
, &timeout
) < 0)
1224 fprintf(stderr
, "ERROR: %.3f select() for %d failed: %s\n", run_time(),
1225 fd
, strerror(errno
));
1229 if (FD_ISSET(fd
, &input
))
1230 read_snmp_response(fd
);
1234 * List devices with complete information...
1237 int sent_something
= 0;
1239 for (device
= (snmp_cache_t
*)cupsArrayFirst(Devices
);
1241 device
= (snmp_cache_t
*)cupsArrayNext(Devices
))
1242 if (!device
->sent
&& device
->info
&& device
->make_and_model
)
1245 list_device(device
);
1247 probe_device(device
);
1249 device
->sent
= sent_something
= 1;
1252 if (!sent_something
)
1257 debug_printf("DEBUG: %.3f Scan complete!\n", run_time());
1262 * 'try_connect()' - Try connecting on a port...
1265 static int /* O - 0 on success or -1 on error */
1266 try_connect(http_addr_t
*addr
, /* I - Socket address */
1267 const char *addrname
, /* I - Hostname or IP address */
1268 int port
) /* I - Port number */
1270 int fd
; /* Socket */
1271 int status
; /* Connection status */
1274 debug_printf("DEBUG: %.3f Trying %s://%s:%d...\n", run_time(),
1275 port
== 515 ? "lpd" : "socket", addrname
, port
);
1277 if ((fd
= socket(AF_INET
, SOCK_STREAM
, 0)) < 0)
1279 fprintf(stderr
, "ERROR: Unable to create socket: %s\n",
1284 addr
->ipv4
.sin_port
= htons(port
);
1288 status
= connect(fd
, (void *)addr
, httpAddrLength(addr
));
1298 * 'update_cache()' - Update a cached device...
1302 update_cache(snmp_cache_t
*device
, /* I - Device */
1303 const char *uri
, /* I - Device URI */
1304 const char *id
, /* I - Device ID */
1305 const char *make_model
) /* I - Device make and model */
1310 device
->uri
= strdup(uri
);
1317 device
->id
= strdup(id
);
1322 if (device
->make_and_model
)
1323 free(device
->make_and_model
);
1325 device
->make_and_model
= strdup(make_model
);
1328 list_device(device
);
1333 * End of "$Id: snmp.c 7810 2008-07-29 01:11:15Z mike $".