]>
git.ipfire.org Git - thirdparty/cups.git/blob - backend/snmp.c
2 * "$Id: snmp.c 6649 2007-07-11 21:46:42Z mike $"
4 * SNMP discovery backend for the Common UNIX Printing System (CUPS).
6 * Copyright 2007-2008 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/snmp.h>
52 #include <cups/http-private.h>
57 * This backend implements SNMP printer discovery. It uses a broadcast-
58 * based approach to get SNMP response packets from potential printers,
59 * tries a mDNS lookup (Mac OS X only at present), a URI lookup based on
60 * the device description string, and finally a probe of port 9100
61 * (AppSocket) and 515 (LPD).
63 * The current focus is on printers with internal network cards, although
64 * the code also works with many external print servers as well. Future
65 * versions will support scanning for vendor-specific SNMP OIDs and the
66 * new PWG Port Monitor MIB and not just the Host MIB OIDs.
68 * The backend reads the snmp.conf file from the CUPS_SERVERROOT directory
69 * which can contain comments, blank lines, or any number of the following
77 * DeviceURI "regex pattern" uri
82 * The default is to use:
90 * This backend is known to work with the following network printers and
93 * Axis OfficeBasic, 5400, 5600
102 * It does not currently work with:
109 * (for all of these, they do not support the Host MIB)
116 typedef struct device_uri_s
/**** DeviceURI values ****/
118 regex_t re
; /* Regular expression to match */
119 cups_array_t
*uris
; /* URIs */
122 typedef struct snmp_cache_s
/**** SNMP scan cache ****/
124 http_addr_t address
; /* Address of device */
125 char *addrname
, /* Name of device */
126 *uri
, /* device-uri */
128 *make_and_model
; /* device-make-and-model */
133 * Private CUPS API to set the last error...
136 extern void _cupsSetError(ipp_status_t status
, const char *message
);
143 static char *add_array(cups_array_t
*a
, const char *s
);
144 static void add_cache(http_addr_t
*addr
, const char *addrname
,
145 const char *uri
, const char *id
,
146 const char *make_and_model
);
147 static device_uri_t
*add_device_uri(char *value
);
148 static void alarm_handler(int sig
);
149 static int compare_cache(snmp_cache_t
*a
, snmp_cache_t
*b
);
150 static void debug_printf(const char *format
, ...);
151 static void fix_make_model(char *make_model
,
152 const char *old_make_model
,
153 int make_model_size
);
154 static void free_array(cups_array_t
*a
);
155 static void free_cache(void);
156 static http_addrlist_t
*get_interface_addresses(const char *ifname
);
157 static void list_device(snmp_cache_t
*cache
);
158 static const char *password_cb(const char *prompt
);
159 static void probe_device(snmp_cache_t
*device
);
160 static void read_snmp_conf(const char *address
);
161 static void read_snmp_response(int fd
);
162 static double run_time(void);
163 static void scan_devices(int fd
);
164 static int try_connect(http_addr_t
*addr
, const char *addrname
,
166 static void update_cache(snmp_cache_t
*device
, const char *uri
,
167 const char *id
, const char *make_model
);
174 static cups_array_t
*Addresses
= NULL
;
175 static cups_array_t
*Communities
= NULL
;
176 static cups_array_t
*Devices
= NULL
;
177 static int DebugLevel
= 0;
178 static int DeviceDescOID
[] = { CUPS_OID_hrDeviceDescr
, 1, 0 };
179 static unsigned DeviceDescRequest
;
180 static int DeviceTypeOID
[] = { CUPS_OID_hrDeviceType
, 1, 0 };
181 static unsigned DeviceTypeRequest
;
182 static cups_array_t
*DeviceURIs
= NULL
;
183 static int HostNameLookups
= 0;
184 static int MaxRunTime
= 10;
185 static struct timeval StartTime
;
189 * 'main()' - Discover printers via SNMP.
192 int /* O - Exit status */
193 main(int argc
, /* I - Number of command-line arguments (6 or 7) */
194 char *argv
[]) /* I - Command-line arguments */
196 int fd
; /* SNMP socket */
197 #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
198 struct sigaction action
; /* Actions for POSIX signals */
199 #endif /* HAVE_SIGACTION && !HAVE_SIGSET */
203 * Check command-line options...
208 fputs(_("Usage: snmp [host-or-ip-address]\n"), stderr
);
213 * Set the password callback for IPP operations...
216 cupsSetPasswordCB(password_cb
);
219 * Catch SIGALRM signals...
223 sigset(SIGALRM
, alarm_handler
);
224 #elif defined(HAVE_SIGACTION)
225 memset(&action
, 0, sizeof(action
));
227 sigemptyset(&action
.sa_mask
);
228 sigaddset(&action
.sa_mask
, SIGALRM
);
229 action
.sa_handler
= alarm_handler
;
230 sigaction(SIGALRM
, &action
, NULL
);
232 signal(SIGALRM
, alarm_handler
);
233 #endif /* HAVE_SIGSET */
236 * Open the SNMP socket...
239 if ((fd
= cupsSNMPOpen()) < 0)
243 * Read the configuration file and any cache data...
246 read_snmp_conf(argv
[1]);
248 cupsSNMPSetDebug(DebugLevel
);
250 Devices
= cupsArrayNew((cups_array_func_t
)compare_cache
, NULL
);
253 * Scan for devices...
259 * Close, free, and return with no errors...
264 free_array(Addresses
);
265 free_array(Communities
);
273 * 'add_array()' - Add a string to an array.
276 static char * /* O - New string */
277 add_array(cups_array_t
*a
, /* I - Array */
278 const char *s
) /* I - String to add */
280 char *dups
; /* New string */
285 cupsArrayAdd(a
, dups
);
292 * 'add_cache()' - Add a cached device...
296 add_cache(http_addr_t
*addr
, /* I - Device IP address */
297 const char *addrname
, /* I - IP address or name string */
298 const char *uri
, /* I - Device URI */
299 const char *id
, /* I - 1284 device ID */
300 const char *make_and_model
) /* I - Make and model */
302 snmp_cache_t
*temp
; /* New device entry */
305 debug_printf("DEBUG: add_cache(addr=%p, addrname=\"%s\", uri=\"%s\", "
306 "id=\"%s\", make_and_model=\"%s\")\n",
307 addr
, addrname
, uri
? uri
: "(null)", id
? id
: "(null)",
308 make_and_model
? make_and_model
: "(null)");
310 temp
= calloc(1, sizeof(snmp_cache_t
));
311 memcpy(&(temp
->address
), addr
, sizeof(temp
->address
));
313 temp
->addrname
= strdup(addrname
);
316 temp
->uri
= strdup(uri
);
319 temp
->id
= strdup(id
);
322 temp
->make_and_model
= strdup(make_and_model
);
324 cupsArrayAdd(Devices
, temp
);
332 * 'add_device_uri()' - Add a device URI to the cache.
334 * The value string is modified (chopped up) as needed.
337 static device_uri_t
* /* O - Device URI */
338 add_device_uri(char *value
) /* I - Value from snmp.conf */
340 device_uri_t
*device_uri
; /* Device URI */
341 char *start
; /* Start of value */
345 * Allocate memory as needed...
349 DeviceURIs
= cupsArrayNew(NULL
, NULL
);
354 if ((device_uri
= calloc(1, sizeof(device_uri_t
))) == NULL
)
357 if ((device_uri
->uris
= cupsArrayNew(NULL
, NULL
)) == NULL
)
364 * Scan the value string for the regular expression and URI(s)...
367 value
++; /* Skip leading " */
369 for (start
= value
; *value
&& *value
!= '\"'; value
++)
370 if (*value
== '\\' && value
[1])
371 _cups_strcpy(value
, value
+ 1);
375 fputs("ERROR: Missing end quote for DeviceURI!\n", stderr
);
377 cupsArrayDelete(device_uri
->uris
);
385 if (regcomp(&(device_uri
->re
), start
, REG_EXTENDED
| REG_ICASE
))
387 fputs("ERROR: Bad regular expression for DeviceURI!\n", stderr
);
389 cupsArrayDelete(device_uri
->uris
);
397 while (isspace(*value
& 255))
403 for (start
= value
; *value
&& !isspace(*value
& 255); value
++);
408 cupsArrayAdd(device_uri
->uris
, strdup(start
));
412 * Add the device URI to the list and return it...
415 cupsArrayAdd(DeviceURIs
, device_uri
);
422 * 'alarm_handler()' - Handle alarm signals...
426 alarm_handler(int sig
) /* I - Signal number */
434 #if !defined(HAVE_SIGSET) && !defined(HAVE_SIGACTION)
435 signal(SIGALRM
, alarm_handler
);
436 #endif /* !HAVE_SIGSET && !HAVE_SIGACTION */
439 write(2, "DEBUG: ALARM!\n", 14);
444 * 'compare_cache()' - Compare two cache entries.
447 static int /* O - Result of comparison */
448 compare_cache(snmp_cache_t
*a
, /* I - First cache entry */
449 snmp_cache_t
*b
) /* I - Second cache entry */
451 return (strcasecmp(a
->addrname
, b
->addrname
));
456 * 'debug_printf()' - Display some debugging information.
460 debug_printf(const char *format
, /* I - Printf-style format string */
461 ...) /* I - Additional arguments as needed */
463 va_list ap
; /* Pointer to arguments */
469 va_start(ap
, format
);
470 vfprintf(stderr
, format
, ap
);
476 * 'fix_make_model()' - Fix common problems in the make-and-model string.
481 char *make_model
, /* I - New make-and-model string */
482 const char *old_make_model
, /* I - Old make-and-model string */
483 int make_model_size
) /* I - Size of new string buffer */
485 char *mmptr
; /* Pointer into make-and-model string */
489 * Fix some common problems with the make-and-model string so
490 * that printer driver detection works better...
493 if (!strncasecmp(old_make_model
, "Hewlett-Packard", 15))
496 * Strip leading Hewlett-Packard and hp prefixes and replace
497 * with a single HP manufacturer prefix...
500 mmptr
= (char *)old_make_model
+ 15;
502 while (isspace(*mmptr
& 255))
505 if (!strncasecmp(mmptr
, "hp", 2))
509 while (isspace(*mmptr
& 255))
516 strlcpy(make_model
+ 3, mmptr
, make_model_size
- 3);
518 else if (!strncasecmp(old_make_model
, "deskjet", 7))
519 snprintf(make_model
, make_model_size
, "HP DeskJet%s", old_make_model
+ 7);
520 else if (!strncasecmp(old_make_model
, "officejet", 9))
521 snprintf(make_model
, make_model_size
, "HP OfficeJet%s", old_make_model
+ 9);
522 else if (!strncasecmp(old_make_model
, "stylus_pro_", 11))
523 snprintf(make_model
, make_model_size
, "EPSON Stylus Pro %s",
524 old_make_model
+ 11);
526 strlcpy(make_model
, old_make_model
, make_model_size
);
528 if ((mmptr
= strstr(make_model
, ", Inc.,")) != NULL
)
531 * Strip inc. from name, e.g. "Tektronix, Inc., Phaser 560"
532 * becomes "Tektronix Phaser 560"...
535 _cups_strcpy(mmptr
, mmptr
+ 7);
538 if ((mmptr
= strstr(make_model
, " Network")) != NULL
)
541 * Drop unnecessary informational text, e.g. "Xerox DocuPrint N2025
542 * Network LaserJet - 2.12" becomes "Xerox DocuPrint N2025"...
548 if ((mmptr
= strchr(make_model
, ',')) != NULL
)
551 * Drop anything after a trailing comma...
560 * 'free_array()' - Free an array of strings.
564 free_array(cups_array_t
*a
) /* I - Array */
566 char *s
; /* Current string */
569 for (s
= (char *)cupsArrayFirst(a
); s
; s
= (char *)cupsArrayNext(a
))
577 * 'free_cache()' - Free the array of cached devices.
583 snmp_cache_t
*cache
; /* Cached device */
586 for (cache
= (snmp_cache_t
*)cupsArrayFirst(Devices
);
588 cache
= (snmp_cache_t
*)cupsArrayNext(Devices
))
590 free(cache
->addrname
);
598 if (cache
->make_and_model
)
599 free(cache
->make_and_model
);
604 cupsArrayDelete(Devices
);
610 * 'get_interface_addresses()' - Get the broadcast address(es) associated
614 static http_addrlist_t
* /* O - List of addresses */
615 get_interface_addresses(
616 const char *ifname
) /* I - Interface name */
618 struct ifaddrs
*addrs
, /* Interface address list */
619 *addr
; /* Current interface address */
620 http_addrlist_t
*first
, /* First address in list */
621 *last
, /* Last address in list */
622 *current
; /* Current address */
625 if (getifaddrs(&addrs
) < 0)
628 for (addr
= addrs
, first
= NULL
, last
= NULL
; addr
; addr
= addr
->ifa_next
)
629 if ((addr
->ifa_flags
& IFF_BROADCAST
) && addr
->ifa_broadaddr
&&
630 addr
->ifa_broadaddr
->sa_family
== AF_INET
&&
631 (!ifname
|| !strcmp(ifname
, addr
->ifa_name
)))
633 current
= calloc(1, sizeof(http_addrlist_t
));
635 memcpy(&(current
->addr
), addr
->ifa_broadaddr
,
636 sizeof(struct sockaddr_in
));
641 last
->next
= current
;
653 * 'list_device()' - List a device we found...
657 list_device(snmp_cache_t
*cache
) /* I - Cached device */
660 printf("network %s \"%s\" \"%s %s\" \"%s\"\n",
662 cache
->make_and_model
? cache
->make_and_model
: "Unknown",
663 cache
->make_and_model
? cache
->make_and_model
: "Unknown",
664 cache
->addrname
, cache
->id
? cache
->id
: "");
669 * 'password_cb()' - Handle authentication requests.
671 * All we do right now is return NULL, indicating that no authentication
675 static const char * /* O - Password (NULL) */
676 password_cb(const char *prompt
) /* I - Prompt message */
678 (void)prompt
; /* Anti-compiler-warning-code */
685 * 'probe_device()' - Probe a device to discover whether it is a printer.
687 * TODO: Try using the Port Monitor MIB to discover the correct protocol
688 * to use - first need a commercially-available printer that supports
693 probe_device(snmp_cache_t
*device
) /* I - Device */
695 char uri
[1024], /* Full device URI */
696 *uriptr
, /* Pointer into URI */
697 *format
; /* Format string for device */
698 device_uri_t
*device_uri
; /* Current DeviceURI match */
701 debug_printf("DEBUG: %.3f Probing %s...\n", run_time(), device
->addrname
);
705 * TODO: Try an mDNS query first, and then fallback on direct probes...
708 if (!try_connect(&(device
->address
), device
->addrname
, 5353))
710 debug_printf("DEBUG: %s supports mDNS, not reporting!\n", device
->addrname
);
713 #endif /* __APPLE__ */
716 * Lookup the device in the match table...
719 for (device_uri
= (device_uri_t
*)cupsArrayFirst(DeviceURIs
);
721 device_uri
= (device_uri_t
*)cupsArrayNext(DeviceURIs
))
722 if (!regexec(&(device_uri
->re
), device
->make_and_model
, 0, NULL
, 0))
725 * Found a match, add the URIs...
728 for (format
= (char *)cupsArrayFirst(device_uri
->uris
);
730 format
= (char *)cupsArrayNext(device_uri
->uris
))
732 for (uriptr
= uri
; *format
&& uriptr
< (uri
+ sizeof(uri
) - 1);)
733 if (*format
== '%' && format
[1] == 's')
736 * Insert hostname/address...
739 strlcpy(uriptr
, device
->addrname
, sizeof(uri
) - (uriptr
- uri
));
740 uriptr
+= strlen(uriptr
);
744 *uriptr
++ = *format
++;
748 update_cache(device
, uri
, NULL
, NULL
);
755 * Then try the standard ports...
758 if (!try_connect(&(device
->address
), device
->addrname
, 9100))
760 debug_printf("DEBUG: %s supports AppSocket!\n", device
->addrname
);
762 snprintf(uri
, sizeof(uri
), "socket://%s", device
->addrname
);
763 update_cache(device
, uri
, NULL
, NULL
);
765 else if (!try_connect(&(device
->address
), device
->addrname
, 515))
767 debug_printf("DEBUG: %s supports LPD!\n", device
->addrname
);
769 snprintf(uri
, sizeof(uri
), "lpd://%s/", device
->addrname
);
770 update_cache(device
, uri
, NULL
, NULL
);
776 * 'read_snmp_conf()' - Read the snmp.conf file.
780 read_snmp_conf(const char *address
) /* I - Single address to probe */
782 cups_file_t
*fp
; /* File pointer */
783 char filename
[1024], /* Filename */
784 line
[1024], /* Line from file */
785 *value
; /* Value on line */
786 int linenum
; /* Line number */
787 const char *cups_serverroot
; /* CUPS_SERVERROOT env var */
788 const char *debug
; /* CUPS_DEBUG_LEVEL env var */
789 const char *runtime
; /* CUPS_MAX_RUN_TIME env var */
793 * Initialize the global address and community lists...
796 Addresses
= cupsArrayNew(NULL
, NULL
);
797 Communities
= cupsArrayNew(NULL
, NULL
);
800 add_array(Addresses
, address
);
802 if ((debug
= getenv("CUPS_DEBUG_LEVEL")) != NULL
)
803 DebugLevel
= atoi(debug
);
805 if ((runtime
= getenv("CUPS_MAX_RUN_TIME")) != NULL
)
806 MaxRunTime
= atoi(runtime
);
809 * Find the snmp.conf file...
812 if ((cups_serverroot
= getenv("CUPS_SERVERROOT")) == NULL
)
813 cups_serverroot
= CUPS_SERVERROOT
;
815 snprintf(filename
, sizeof(filename
), "%s/snmp.conf", cups_serverroot
);
817 if ((fp
= cupsFileOpen(filename
, "r")) != NULL
)
820 * Read the snmp.conf file...
825 while (cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
))
828 fprintf(stderr
, "ERROR: Missing value on line %d of %s!\n", linenum
,
830 else if (!strcasecmp(line
, "Address"))
833 add_array(Addresses
, value
);
835 else if (!strcasecmp(line
, "Community"))
836 add_array(Communities
, value
);
837 else if (!strcasecmp(line
, "DebugLevel"))
838 DebugLevel
= atoi(value
);
839 else if (!strcasecmp(line
, "DeviceURI"))
843 "ERROR: Missing double quote for regular expression on "
844 "line %d of %s!\n", linenum
, filename
);
846 add_device_uri(value
);
848 else if (!strcasecmp(line
, "HostNameLookups"))
849 HostNameLookups
= !strcasecmp(value
, "on") ||
850 !strcasecmp(value
, "yes") ||
851 !strcasecmp(value
, "true") ||
852 !strcasecmp(value
, "double");
853 else if (!strcasecmp(line
, "MaxRunTime"))
854 MaxRunTime
= atoi(value
);
856 fprintf(stderr
, "ERROR: Unknown directive %s on line %d of %s!\n",
857 line
, linenum
, filename
);
864 * Use defaults if parameters are undefined...
867 if (cupsArrayCount(Addresses
) == 0)
870 * If we have no addresses, exit immediately...
874 "DEBUG: No address specified and no Address line in %s...\n",
879 if (cupsArrayCount(Communities
) == 0)
881 fputs("INFO: Using default SNMP Community public\n", stderr
);
882 add_array(Communities
, "public");
888 * 'read_snmp_response()' - Read and parse a SNMP response...
892 read_snmp_response(int fd
) /* I - SNMP socket file descriptor */
894 char addrname
[256]; /* Source address name */
895 cups_snmp_t packet
; /* Decoded packet */
896 snmp_cache_t key
, /* Search key */
897 *device
; /* Matching device */
901 * Read the response data...
904 if (!cupsSNMPRead(fd
, &packet
, -1))
906 fprintf(stderr
, "ERROR: Unable to read data from socket: %s\n",
912 httpAddrLookup(&(packet
.address
), addrname
, sizeof(addrname
));
914 httpAddrString(&(packet
.address
), addrname
, sizeof(addrname
));
916 debug_printf("DEBUG: %.3f Received data from %s...\n", run_time(), addrname
);
919 * Look for the response status code in the SNMP message header...
924 fprintf(stderr
, "ERROR: Bad SNMP packet from %s: %s\n", addrname
,
930 debug_printf("DEBUG: community=\"%s\"\n", packet
.community
);
931 debug_printf("DEBUG: request-id=%d\n", packet
.request_id
);
932 debug_printf("DEBUG: error-status=%d\n", packet
.error_status
);
934 if (packet
.error_status
)
938 * Find a matching device in the cache...
941 key
.addrname
= addrname
;
942 device
= (snmp_cache_t
*)cupsArrayFind(Devices
, &key
);
945 * Process the message...
948 if (packet
.request_id
== DeviceTypeRequest
)
951 * Got the device type response...
956 debug_printf("DEBUG: Discarding duplicate device type for \"%s\"...\n",
962 * Add the device and request the device description...
965 add_cache(&(packet
.address
), addrname
, NULL
, NULL
, NULL
);
967 cupsSNMPWrite(fd
, &(packet
.address
), CUPS_SNMP_VERSION_1
, packet
.community
,
968 CUPS_ASN1_GET_REQUEST
, DeviceDescRequest
, DeviceDescOID
);
970 else if (packet
.request_id
== DeviceDescRequest
&&
971 packet
.object_type
== CUPS_ASN1_OCTET_STRING
)
974 * Update an existing cache entry...
977 char make_model
[256]; /* Make and model */
982 debug_printf("DEBUG: Discarding device description for \"%s\"...\n",
988 * Convert the description to a make and model string...
991 if (strchr(packet
.object_value
.string
, ':') &&
992 strchr(packet
.object_value
.string
, ';'))
995 * Description is the IEEE-1284 device ID...
998 backendGetMakeModel(packet
.object_value
.string
, make_model
,
1004 * Description is plain text...
1007 fix_make_model(make_model
, packet
.object_value
.string
,
1008 sizeof(make_model
));
1011 if (device
->make_and_model
)
1012 free(device
->make_and_model
);
1014 device
->make_and_model
= strdup(make_model
);
1020 * 'run_time()' - Return the total running time...
1023 static double /* O - Number of seconds */
1026 struct timeval curtime
; /* Current time */
1029 gettimeofday(&curtime
, NULL
);
1031 return (curtime
.tv_sec
- StartTime
.tv_sec
+
1032 0.000001 * (curtime
.tv_usec
- StartTime
.tv_usec
));
1037 * 'scan_devices()' - Scan for devices using SNMP.
1041 scan_devices(int fd
) /* I - SNMP socket */
1043 char *address
, /* Current address */
1044 *community
; /* Current community */
1045 fd_set input
; /* Input set for select() */
1046 struct timeval timeout
; /* Timeout for select() */
1047 time_t endtime
; /* End time for scan */
1048 http_addrlist_t
*addrs
, /* List of addresses */
1049 *addr
; /* Current address */
1050 snmp_cache_t
*device
; /* Current device */
1054 * Setup the request IDs...
1057 gettimeofday(&StartTime
, NULL
);
1059 DeviceTypeRequest
= StartTime
.tv_sec
;
1060 DeviceDescRequest
= StartTime
.tv_sec
+ 1;
1063 * First send all of the broadcast queries...
1066 for (address
= (char *)cupsArrayFirst(Addresses
);
1068 address
= (char *)cupsArrayNext(Addresses
))
1070 if (!strcmp(address
, "@LOCAL"))
1071 addrs
= get_interface_addresses(NULL
);
1072 else if (!strncmp(address
, "@IF(", 4))
1074 char ifname
[255]; /* Interface name */
1077 strlcpy(ifname
, address
+ 4, sizeof(ifname
));
1079 ifname
[strlen(ifname
) - 1] = '\0';
1081 addrs
= get_interface_addresses(ifname
);
1084 addrs
= httpAddrGetList(address
, AF_INET
, NULL
);
1088 fprintf(stderr
, "ERROR: Unable to scan \"%s\"!\n", address
);
1092 for (community
= (char *)cupsArrayFirst(Communities
);
1094 community
= (char *)cupsArrayNext(Communities
))
1096 debug_printf("DEBUG: Scanning for devices in \"%s\" via \"%s\"...\n",
1097 community
, address
);
1099 for (addr
= addrs
; addr
; addr
= addr
->next
)
1100 cupsSNMPWrite(fd
, &(addr
->addr
), CUPS_SNMP_VERSION_1
, community
,
1101 CUPS_ASN1_GET_REQUEST
, DeviceTypeRequest
, DeviceTypeOID
);
1104 httpAddrFreeList(addrs
);
1108 * Then read any responses that come in over the next 3 seconds...
1111 endtime
= time(NULL
) + 3;
1115 while (time(NULL
) < endtime
)
1118 timeout
.tv_usec
= 0;
1121 if (select(fd
+ 1, &input
, NULL
, NULL
, &timeout
) < 0)
1123 fprintf(stderr
, "ERROR: %.3f select() for %d failed: %s\n", run_time(),
1124 fd
, strerror(errno
));
1128 if (FD_ISSET(fd
, &input
))
1129 read_snmp_response(fd
);
1135 * Finally, probe all of the printers we discovered to see how they are
1139 for (device
= (snmp_cache_t
*)cupsArrayFirst(Devices
);
1141 device
= (snmp_cache_t
*)cupsArrayNext(Devices
))
1142 if (MaxRunTime
> 0 && run_time() >= MaxRunTime
)
1144 else if (!device
->uri
)
1145 probe_device(device
);
1147 debug_printf("DEBUG: %.3f Scan complete!\n", run_time());
1152 * 'try_connect()' - Try connecting on a port...
1155 static int /* O - 0 on success or -1 on error */
1156 try_connect(http_addr_t
*addr
, /* I - Socket address */
1157 const char *addrname
, /* I - Hostname or IP address */
1158 int port
) /* I - Port number */
1160 int fd
; /* Socket */
1161 int status
; /* Connection status */
1164 debug_printf("DEBUG: %.3f Trying %s://%s:%d...\n", run_time(),
1165 port
== 515 ? "lpd" : "socket", addrname
, port
);
1167 if ((fd
= socket(AF_INET
, SOCK_STREAM
, 0)) < 0)
1169 fprintf(stderr
, "ERROR: Unable to create socket: %s\n",
1174 addr
->ipv4
.sin_port
= htons(port
);
1178 status
= connect(fd
, (void *)addr
, httpAddrLength(addr
));
1188 * 'update_cache()' - Update a cached device...
1192 update_cache(snmp_cache_t
*device
, /* I - Device */
1193 const char *uri
, /* I - Device URI */
1194 const char *id
, /* I - Device ID */
1195 const char *make_model
) /* I - Device make and model */
1200 device
->uri
= strdup(uri
);
1207 device
->id
= strdup(id
);
1212 if (device
->make_and_model
)
1213 free(device
->make_and_model
);
1215 device
->make_and_model
= strdup(make_model
);
1218 list_device(device
);
1223 * End of "$Id: snmp.c 6649 2007-07-11 21:46:42Z mike $".