]>
Commit | Line | Data |
---|---|---|
89d46774 | 1 | /* |
b19ccc9e | 2 | * "$Id: snmp.c 7810 2008-07-29 01:11:15Z mike $" |
89d46774 | 3 | * |
22c9029b | 4 | * SNMP discovery backend for CUPS. |
89d46774 | 5 | * |
12f89d24 | 6 | * Copyright 2007-2012 by Apple Inc. |
c0e1af83 | 7 | * Copyright 2006-2007 by Easy Software Products, all rights reserved. |
89d46774 | 8 | * |
9 | * These coded instructions, statements, and computer programs are the | |
bc44d920 | 10 | * property of Apple Inc. and are protected by Federal copyright |
11 | * law. Distribution and use rights are outlined in the file "LICENSE.txt" | |
89d46774 | 12 | * "LICENSE" which should have been included with this file. If this |
bc44d920 | 13 | * file is missing or damaged, see the license at "http://www.cups.org/". |
89d46774 | 14 | * |
15 | * This file is subject to the Apple OS-Developed Software exception. | |
16 | * | |
17 | * Contents: | |
18 | * | |
19 | * main() - Discover printers via SNMP. | |
20 | * add_array() - Add a string to an array. | |
21 | * add_cache() - Add a cached device... | |
b94498cf | 22 | * add_device_uri() - Add a device URI to the cache. |
89d46774 | 23 | * alarm_handler() - Handle alarm signals... |
89d46774 | 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 | |
27 | * string. | |
28 | * free_array() - Free an array of strings. | |
29 | * free_cache() - Free the array of cached devices. | |
91c84a35 MS |
30 | * get_interface_addresses() - Get the broadcast address(es) associated with |
31 | * an interface. | |
d09495fa | 32 | * list_device() - List a device we found... |
89d46774 | 33 | * password_cb() - Handle authentication requests. |
34 | * probe_device() - Probe a device to discover whether it is a | |
35 | * printer. | |
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. | |
89d46774 | 40 | * try_connect() - Try connecting on a port... |
41 | * update_cache() - Update a cached device... | |
42 | */ | |
43 | ||
44 | /* | |
45 | * Include necessary headers. | |
46 | */ | |
47 | ||
ed486911 | 48 | #include "backend-private.h" |
89d46774 | 49 | #include <cups/array.h> |
50 | #include <cups/file.h> | |
91c84a35 | 51 | #include <cups/http-private.h> |
b94498cf | 52 | #include <regex.h> |
89d46774 | 53 | |
54 | ||
55 | /* | |
56 | * This backend implements SNMP printer discovery. It uses a broadcast- | |
b94498cf | 57 | * based approach to get SNMP response packets from potential printers, |
7a14d768 MS |
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). | |
89d46774 | 61 | * |
62 | * The current focus is on printers with internal network cards, although | |
7a14d768 | 63 | * the code also works with many external print servers as well. |
89d46774 | 64 | * |
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 | |
67 | * directives: | |
68 | * | |
69 | * Address ip-address | |
70 | * Address @LOCAL | |
71 | * Address @IF(name) | |
72 | * Community name | |
73 | * DebugLevel N | |
b94498cf | 74 | * DeviceURI "regex pattern" uri |
89d46774 | 75 | * HostNameLookups on |
76 | * HostNameLookups off | |
b94498cf | 77 | * MaxRunTime N |
89d46774 | 78 | * |
79 | * The default is to use: | |
80 | * | |
81 | * Address @LOCAL | |
82 | * Community public | |
83 | * DebugLevel 0 | |
84 | * HostNameLookups off | |
ae71f5de | 85 | * MaxRunTime 120 |
89d46774 | 86 | * |
87 | * This backend is known to work with the following network printers and | |
88 | * print servers: | |
89 | * | |
90 | * Axis OfficeBasic, 5400, 5600 | |
7a14d768 | 91 | * Brother |
89d46774 | 92 | * EPSON |
93 | * Genicom | |
94 | * HP JetDirect | |
95 | * Lexmark | |
96 | * Sharp | |
97 | * Tektronix | |
98 | * Xerox | |
99 | * | |
100 | * It does not currently work with: | |
101 | * | |
102 | * DLink | |
103 | * Linksys | |
104 | * Netgear | |
105 | * Okidata | |
106 | * | |
107 | * (for all of these, they do not support the Host MIB) | |
108 | */ | |
109 | ||
89d46774 | 110 | /* |
111 | * Types... | |
112 | */ | |
113 | ||
749b1e90 MS |
114 | enum /**** Request IDs for each field ****/ |
115 | { | |
116 | DEVICE_TYPE = 1, | |
117 | DEVICE_DESCRIPTION, | |
118 | DEVICE_LOCATION, | |
119 | DEVICE_ID, | |
120 | DEVICE_URI, | |
121 | DEVICE_PRODUCT | |
122 | }; | |
123 | ||
b94498cf | 124 | typedef struct device_uri_s /**** DeviceURI values ****/ |
125 | { | |
126 | regex_t re; /* Regular expression to match */ | |
127 | cups_array_t *uris; /* URIs */ | |
128 | } device_uri_t; | |
129 | ||
89d46774 | 130 | typedef struct snmp_cache_s /**** SNMP scan cache ****/ |
131 | { | |
132 | http_addr_t address; /* Address of device */ | |
133 | char *addrname, /* Name of device */ | |
134 | *uri, /* device-uri */ | |
135 | *id, /* device-id */ | |
7a14d768 | 136 | *info, /* device-info */ |
749b1e90 | 137 | *location, /* device-location */ |
89d46774 | 138 | *make_and_model; /* device-make-and-model */ |
749b1e90 | 139 | int sent; /* Has this device been listed? */ |
89d46774 | 140 | } snmp_cache_t; |
141 | ||
89d46774 | 142 | |
143 | /* | |
144 | * Local functions... | |
145 | */ | |
146 | ||
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); | |
b94498cf | 151 | static device_uri_t *add_device_uri(char *value); |
89d46774 | 152 | static void alarm_handler(int sig); |
89d46774 | 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); | |
d09495fa | 161 | static void list_device(snmp_cache_t *cache); |
89d46774 | 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); | |
22c9029b | 167 | static void scan_devices(int ipv4, int ipv6); |
89d46774 | 168 | static int try_connect(http_addr_t *addr, const char *addrname, |
169 | int port); | |
170 | static void update_cache(snmp_cache_t *device, const char *uri, | |
171 | const char *id, const char *make_model); | |
172 | ||
173 | ||
174 | /* | |
175 | * Local globals... | |
176 | */ | |
177 | ||
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; | |
749b1e90 MS |
182 | static const int DescriptionOID[] = { CUPS_OID_hrDeviceDescr, 1, -1 }; |
183 | static const int LocationOID[] = { CUPS_OID_sysLocation, 0, -1 }; | |
7a14d768 | 184 | static const int DeviceTypeOID[] = { CUPS_OID_hrDeviceType, 1, -1 }; |
7a14d768 | 185 | static const int DeviceIdOID[] = { CUPS_OID_ppmPrinterIEEE1284DeviceId, 1, -1 }; |
749b1e90 MS |
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 }; | |
b94498cf | 191 | static cups_array_t *DeviceURIs = NULL; |
89d46774 | 192 | static int HostNameLookups = 0; |
ae71f5de | 193 | static int MaxRunTime = 120; |
89d46774 | 194 | static struct timeval StartTime; |
195 | ||
196 | ||
197 | /* | |
198 | * 'main()' - Discover printers via SNMP. | |
199 | */ | |
200 | ||
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 */ | |
204 | { | |
22c9029b MS |
205 | int ipv4, /* SNMP IPv4 socket */ |
206 | ipv6; /* SNMP IPv6 socket */ | |
d09495fa | 207 | #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET) |
208 | struct sigaction action; /* Actions for POSIX signals */ | |
209 | #endif /* HAVE_SIGACTION && !HAVE_SIGSET */ | |
89d46774 | 210 | |
211 | ||
212 | /* | |
213 | * Check command-line options... | |
214 | */ | |
215 | ||
216 | if (argc > 2) | |
217 | { | |
0837b7e8 | 218 | _cupsLangPuts(stderr, _("Usage: snmp [host-or-ip-address]")); |
89d46774 | 219 | return (1); |
220 | } | |
221 | ||
222 | /* | |
223 | * Set the password callback for IPP operations... | |
224 | */ | |
225 | ||
226 | cupsSetPasswordCB(password_cb); | |
227 | ||
d09495fa | 228 | /* |
229 | * Catch SIGALRM signals... | |
230 | */ | |
231 | ||
232 | #ifdef HAVE_SIGSET | |
233 | sigset(SIGALRM, alarm_handler); | |
234 | #elif defined(HAVE_SIGACTION) | |
235 | memset(&action, 0, sizeof(action)); | |
236 | ||
237 | sigemptyset(&action.sa_mask); | |
238 | sigaddset(&action.sa_mask, SIGALRM); | |
239 | action.sa_handler = alarm_handler; | |
240 | sigaction(SIGALRM, &action, NULL); | |
241 | #else | |
242 | signal(SIGALRM, alarm_handler); | |
243 | #endif /* HAVE_SIGSET */ | |
244 | ||
89d46774 | 245 | /* |
246 | * Open the SNMP socket... | |
247 | */ | |
248 | ||
22c9029b | 249 | if ((ipv4 = _cupsSNMPOpen(AF_INET)) < 0) |
89d46774 | 250 | return (1); |
251 | ||
22c9029b MS |
252 | #ifdef AF_INET6 |
253 | if ((ipv6 = _cupsSNMPOpen(AF_INET6)) < 0) | |
37e7e6e0 | 254 | perror("DEBUG: Unable to create IPv6 socket"); |
22c9029b MS |
255 | #else |
256 | ipv6 = -1; | |
257 | #endif /* AF_INET6 */ | |
258 | ||
89d46774 | 259 | /* |
260 | * Read the configuration file and any cache data... | |
261 | */ | |
262 | ||
263 | read_snmp_conf(argv[1]); | |
264 | ||
7a14d768 | 265 | _cupsSNMPSetDebug(DebugLevel); |
91c84a35 | 266 | |
89d46774 | 267 | Devices = cupsArrayNew((cups_array_func_t)compare_cache, NULL); |
268 | ||
269 | /* | |
270 | * Scan for devices... | |
271 | */ | |
272 | ||
22c9029b | 273 | scan_devices(ipv4, ipv6); |
89d46774 | 274 | |
89d46774 | 275 | /* |
276 | * Close, free, and return with no errors... | |
277 | */ | |
278 | ||
22c9029b MS |
279 | _cupsSNMPClose(ipv4); |
280 | if (ipv6 >= 0) | |
281 | _cupsSNMPClose(ipv6); | |
89d46774 | 282 | |
283 | free_array(Addresses); | |
284 | free_array(Communities); | |
285 | free_cache(); | |
286 | ||
287 | return (0); | |
288 | } | |
289 | ||
290 | ||
291 | /* | |
292 | * 'add_array()' - Add a string to an array. | |
293 | */ | |
294 | ||
295 | static char * /* O - New string */ | |
296 | add_array(cups_array_t *a, /* I - Array */ | |
297 | const char *s) /* I - String to add */ | |
298 | { | |
299 | char *dups; /* New string */ | |
300 | ||
301 | ||
302 | dups = strdup(s); | |
303 | ||
304 | cupsArrayAdd(a, dups); | |
305 | ||
306 | return (dups); | |
307 | } | |
308 | ||
309 | ||
310 | /* | |
311 | * 'add_cache()' - Add a cached device... | |
312 | */ | |
313 | ||
314 | static void | |
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 */ | |
320 | { | |
321 | snmp_cache_t *temp; /* New device entry */ | |
322 | ||
323 | ||
324 | debug_printf("DEBUG: add_cache(addr=%p, addrname=\"%s\", uri=\"%s\", " | |
325 | "id=\"%s\", make_and_model=\"%s\")\n", | |
d09495fa | 326 | addr, addrname, uri ? uri : "(null)", id ? id : "(null)", |
89d46774 | 327 | make_and_model ? make_and_model : "(null)"); |
328 | ||
329 | temp = calloc(1, sizeof(snmp_cache_t)); | |
330 | memcpy(&(temp->address), addr, sizeof(temp->address)); | |
331 | ||
332 | temp->addrname = strdup(addrname); | |
333 | ||
334 | if (uri) | |
335 | temp->uri = strdup(uri); | |
336 | ||
337 | if (id) | |
338 | temp->id = strdup(id); | |
339 | ||
340 | if (make_and_model) | |
341 | temp->make_and_model = strdup(make_and_model); | |
342 | ||
343 | cupsArrayAdd(Devices, temp); | |
d09495fa | 344 | |
345 | if (uri) | |
346 | list_device(temp); | |
89d46774 | 347 | } |
348 | ||
349 | ||
b94498cf | 350 | /* |
351 | * 'add_device_uri()' - Add a device URI to the cache. | |
352 | * | |
353 | * The value string is modified (chopped up) as needed. | |
354 | */ | |
355 | ||
356 | static device_uri_t * /* O - Device URI */ | |
357 | add_device_uri(char *value) /* I - Value from snmp.conf */ | |
358 | { | |
359 | device_uri_t *device_uri; /* Device URI */ | |
360 | char *start; /* Start of value */ | |
361 | ||
362 | ||
363 | /* | |
364 | * Allocate memory as needed... | |
365 | */ | |
366 | ||
367 | if (!DeviceURIs) | |
368 | DeviceURIs = cupsArrayNew(NULL, NULL); | |
369 | ||
370 | if (!DeviceURIs) | |
371 | return (NULL); | |
372 | ||
373 | if ((device_uri = calloc(1, sizeof(device_uri_t))) == NULL) | |
374 | return (NULL); | |
375 | ||
376 | if ((device_uri->uris = cupsArrayNew(NULL, NULL)) == NULL) | |
377 | { | |
378 | free(device_uri); | |
379 | return (NULL); | |
380 | } | |
381 | ||
382 | /* | |
383 | * Scan the value string for the regular expression and URI(s)... | |
384 | */ | |
385 | ||
386 | value ++; /* Skip leading " */ | |
387 | ||
388 | for (start = value; *value && *value != '\"'; value ++) | |
389 | if (*value == '\\' && value[1]) | |
390 | _cups_strcpy(value, value + 1); | |
391 | ||
392 | if (!*value) | |
393 | { | |
394 | fputs("ERROR: Missing end quote for DeviceURI!\n", stderr); | |
395 | ||
396 | cupsArrayDelete(device_uri->uris); | |
397 | free(device_uri); | |
398 | ||
399 | return (NULL); | |
400 | } | |
401 | ||
402 | *value++ = '\0'; | |
403 | ||
404 | if (regcomp(&(device_uri->re), start, REG_EXTENDED | REG_ICASE)) | |
405 | { | |
406 | fputs("ERROR: Bad regular expression for DeviceURI!\n", stderr); | |
407 | ||
408 | cupsArrayDelete(device_uri->uris); | |
409 | free(device_uri); | |
410 | ||
411 | return (NULL); | |
412 | } | |
413 | ||
414 | while (*value) | |
415 | { | |
416 | while (isspace(*value & 255)) | |
417 | value ++; | |
418 | ||
419 | if (!*value) | |
420 | break; | |
421 | ||
422 | for (start = value; *value && !isspace(*value & 255); value ++); | |
423 | ||
424 | if (*value) | |
425 | *value++ = '\0'; | |
426 | ||
427 | cupsArrayAdd(device_uri->uris, strdup(start)); | |
428 | } | |
429 | ||
430 | /* | |
431 | * Add the device URI to the list and return it... | |
432 | */ | |
433 | ||
434 | cupsArrayAdd(DeviceURIs, device_uri); | |
435 | ||
436 | return (device_uri); | |
437 | } | |
438 | ||
439 | ||
89d46774 | 440 | /* |
441 | * 'alarm_handler()' - Handle alarm signals... | |
442 | */ | |
443 | ||
444 | static void | |
445 | alarm_handler(int sig) /* I - Signal number */ | |
446 | { | |
447 | /* | |
448 | * Do nothing... | |
449 | */ | |
450 | ||
451 | (void)sig; | |
452 | ||
d09495fa | 453 | #if !defined(HAVE_SIGSET) && !defined(HAVE_SIGACTION) |
454 | signal(SIGALRM, alarm_handler); | |
455 | #endif /* !HAVE_SIGSET && !HAVE_SIGACTION */ | |
456 | ||
89d46774 | 457 | if (DebugLevel) |
458 | write(2, "DEBUG: ALARM!\n", 14); | |
459 | } | |
460 | ||
461 | ||
89d46774 | 462 | /* |
463 | * 'compare_cache()' - Compare two cache entries. | |
464 | */ | |
465 | ||
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 */ | |
469 | { | |
88f9aafc | 470 | return (_cups_strcasecmp(a->addrname, b->addrname)); |
89d46774 | 471 | } |
472 | ||
473 | ||
474 | /* | |
475 | * 'debug_printf()' - Display some debugging information. | |
476 | */ | |
477 | ||
478 | static void | |
479 | debug_printf(const char *format, /* I - Printf-style format string */ | |
480 | ...) /* I - Additional arguments as needed */ | |
481 | { | |
482 | va_list ap; /* Pointer to arguments */ | |
483 | ||
484 | ||
485 | if (!DebugLevel) | |
486 | return; | |
487 | ||
488 | va_start(ap, format); | |
489 | vfprintf(stderr, format, ap); | |
490 | va_end(ap); | |
491 | } | |
492 | ||
493 | ||
494 | /* | |
495 | * 'fix_make_model()' - Fix common problems in the make-and-model string. | |
496 | */ | |
497 | ||
498 | static void | |
499 | fix_make_model( | |
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 */ | |
503 | { | |
a74454a7 | 504 | char *mmptr; /* Pointer into make-and-model string */ |
89d46774 | 505 | |
506 | ||
507 | /* | |
508 | * Fix some common problems with the make-and-model string so | |
509 | * that printer driver detection works better... | |
510 | */ | |
511 | ||
88f9aafc | 512 | if (!_cups_strncasecmp(old_make_model, "Hewlett-Packard", 15)) |
89d46774 | 513 | { |
514 | /* | |
515 | * Strip leading Hewlett-Packard and hp prefixes and replace | |
516 | * with a single HP manufacturer prefix... | |
517 | */ | |
518 | ||
a74454a7 | 519 | mmptr = (char *)old_make_model + 15; |
89d46774 | 520 | |
521 | while (isspace(*mmptr & 255)) | |
522 | mmptr ++; | |
523 | ||
88f9aafc | 524 | if (!_cups_strncasecmp(mmptr, "hp", 2)) |
89d46774 | 525 | { |
526 | mmptr += 2; | |
527 | ||
528 | while (isspace(*mmptr & 255)) | |
529 | mmptr ++; | |
530 | } | |
531 | ||
532 | make_model[0] = 'H'; | |
533 | make_model[1] = 'P'; | |
534 | make_model[2] = ' '; | |
535 | strlcpy(make_model + 3, mmptr, make_model_size - 3); | |
536 | } | |
88f9aafc | 537 | else if (!_cups_strncasecmp(old_make_model, "deskjet", 7)) |
89d46774 | 538 | snprintf(make_model, make_model_size, "HP DeskJet%s", old_make_model + 7); |
88f9aafc | 539 | else if (!_cups_strncasecmp(old_make_model, "officejet", 9)) |
b86bc4cf | 540 | snprintf(make_model, make_model_size, "HP OfficeJet%s", old_make_model + 9); |
88f9aafc | 541 | else if (!_cups_strncasecmp(old_make_model, "stylus_pro_", 11)) |
89d46774 | 542 | snprintf(make_model, make_model_size, "EPSON Stylus Pro %s", |
543 | old_make_model + 11); | |
544 | else | |
545 | strlcpy(make_model, old_make_model, make_model_size); | |
546 | ||
547 | if ((mmptr = strstr(make_model, ", Inc.,")) != NULL) | |
548 | { | |
549 | /* | |
550 | * Strip inc. from name, e.g. "Tektronix, Inc., Phaser 560" | |
551 | * becomes "Tektronix Phaser 560"... | |
552 | */ | |
553 | ||
a74454a7 | 554 | _cups_strcpy(mmptr, mmptr + 7); |
555 | } | |
556 | ||
d09495fa | 557 | if ((mmptr = strstr(make_model, " Network")) != NULL) |
558 | { | |
559 | /* | |
560 | * Drop unnecessary informational text, e.g. "Xerox DocuPrint N2025 | |
561 | * Network LaserJet - 2.12" becomes "Xerox DocuPrint N2025"... | |
562 | */ | |
563 | ||
564 | *mmptr = '\0'; | |
565 | } | |
566 | ||
a74454a7 | 567 | if ((mmptr = strchr(make_model, ',')) != NULL) |
568 | { | |
569 | /* | |
570 | * Drop anything after a trailing comma... | |
571 | */ | |
572 | ||
573 | *mmptr = '\0'; | |
89d46774 | 574 | } |
575 | } | |
576 | ||
577 | ||
578 | /* | |
579 | * 'free_array()' - Free an array of strings. | |
580 | */ | |
581 | ||
582 | static void | |
583 | free_array(cups_array_t *a) /* I - Array */ | |
584 | { | |
585 | char *s; /* Current string */ | |
586 | ||
587 | ||
588 | for (s = (char *)cupsArrayFirst(a); s; s = (char *)cupsArrayNext(a)) | |
589 | free(s); | |
590 | ||
591 | cupsArrayDelete(a); | |
592 | } | |
593 | ||
594 | ||
595 | /* | |
596 | * 'free_cache()' - Free the array of cached devices. | |
597 | */ | |
598 | ||
599 | static void | |
600 | free_cache(void) | |
601 | { | |
602 | snmp_cache_t *cache; /* Cached device */ | |
603 | ||
604 | ||
605 | for (cache = (snmp_cache_t *)cupsArrayFirst(Devices); | |
606 | cache; | |
607 | cache = (snmp_cache_t *)cupsArrayNext(Devices)) | |
608 | { | |
609 | free(cache->addrname); | |
610 | ||
611 | if (cache->uri) | |
612 | free(cache->uri); | |
613 | ||
614 | if (cache->id) | |
615 | free(cache->id); | |
616 | ||
617 | if (cache->make_and_model) | |
618 | free(cache->make_and_model); | |
619 | ||
620 | free(cache); | |
621 | } | |
622 | ||
623 | cupsArrayDelete(Devices); | |
624 | Devices = NULL; | |
625 | } | |
626 | ||
627 | ||
628 | /* | |
629 | * 'get_interface_addresses()' - Get the broadcast address(es) associated | |
630 | * with an interface. | |
631 | */ | |
632 | ||
633 | static http_addrlist_t * /* O - List of addresses */ | |
634 | get_interface_addresses( | |
635 | const char *ifname) /* I - Interface name */ | |
636 | { | |
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 */ | |
642 | ||
643 | ||
644 | if (getifaddrs(&addrs) < 0) | |
645 | return (NULL); | |
646 | ||
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))) | |
651 | { | |
652 | current = calloc(1, sizeof(http_addrlist_t)); | |
653 | ||
654 | memcpy(&(current->addr), addr->ifa_broadaddr, | |
655 | sizeof(struct sockaddr_in)); | |
656 | ||
657 | if (!last) | |
658 | first = current; | |
659 | else | |
660 | last->next = current; | |
661 | ||
662 | last = current; | |
663 | } | |
664 | ||
665 | freeifaddrs(addrs); | |
666 | ||
667 | return (first); | |
668 | } | |
669 | ||
670 | ||
89d46774 | 671 | /* |
d09495fa | 672 | * 'list_device()' - List a device we found... |
89d46774 | 673 | */ |
674 | ||
675 | static void | |
d09495fa | 676 | list_device(snmp_cache_t *cache) /* I - Cached device */ |
89d46774 | 677 | { |
d09495fa | 678 | if (cache->uri) |
749b1e90 MS |
679 | cupsBackendReport("network", cache->uri, cache->make_and_model, |
680 | cache->info, cache->id, cache->location); | |
89d46774 | 681 | } |
682 | ||
683 | ||
89d46774 | 684 | /* |
685 | * 'password_cb()' - Handle authentication requests. | |
686 | * | |
687 | * All we do right now is return NULL, indicating that no authentication | |
688 | * is possible. | |
689 | */ | |
690 | ||
691 | static const char * /* O - Password (NULL) */ | |
692 | password_cb(const char *prompt) /* I - Prompt message */ | |
693 | { | |
694 | (void)prompt; /* Anti-compiler-warning-code */ | |
695 | ||
696 | return (NULL); | |
697 | } | |
698 | ||
699 | ||
700 | /* | |
701 | * 'probe_device()' - Probe a device to discover whether it is a printer. | |
702 | * | |
703 | * TODO: Try using the Port Monitor MIB to discover the correct protocol | |
704 | * to use - first need a commercially-available printer that supports | |
705 | * it, though... | |
706 | */ | |
707 | ||
708 | static void | |
709 | probe_device(snmp_cache_t *device) /* I - Device */ | |
710 | { | |
b94498cf | 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 */ | |
89d46774 | 715 | |
716 | ||
89d46774 | 717 | debug_printf("DEBUG: %.3f Probing %s...\n", run_time(), device->addrname); |
718 | ||
b94498cf | 719 | #ifdef __APPLE__ |
720 | /* | |
22c9029b | 721 | * If the printer supports Bonjour/mDNS, don't report it from the SNMP backend. |
b94498cf | 722 | */ |
d09495fa | 723 | |
b94498cf | 724 | if (!try_connect(&(device->address), device->addrname, 5353)) |
d09495fa | 725 | { |
b94498cf | 726 | debug_printf("DEBUG: %s supports mDNS, not reporting!\n", device->addrname); |
727 | return; | |
d09495fa | 728 | } |
b94498cf | 729 | #endif /* __APPLE__ */ |
d09495fa | 730 | |
b94498cf | 731 | /* |
732 | * Lookup the device in the match table... | |
733 | */ | |
89d46774 | 734 | |
b94498cf | 735 | for (device_uri = (device_uri_t *)cupsArrayFirst(DeviceURIs); |
736 | device_uri; | |
737 | device_uri = (device_uri_t *)cupsArrayNext(DeviceURIs)) | |
ac884b6a MS |
738 | if (device->make_and_model && |
739 | !regexec(&(device_uri->re), device->make_and_model, 0, NULL, 0)) | |
89d46774 | 740 | { |
d09495fa | 741 | /* |
b94498cf | 742 | * Found a match, add the URIs... |
89d46774 | 743 | */ |
744 | ||
b94498cf | 745 | for (format = (char *)cupsArrayFirst(device_uri->uris); |
746 | format; | |
747 | format = (char *)cupsArrayNext(device_uri->uris)) | |
89d46774 | 748 | { |
b94498cf | 749 | for (uriptr = uri; *format && uriptr < (uri + sizeof(uri) - 1);) |
750 | if (*format == '%' && format[1] == 's') | |
89d46774 | 751 | { |
b94498cf | 752 | /* |
753 | * Insert hostname/address... | |
754 | */ | |
89d46774 | 755 | |
b94498cf | 756 | strlcpy(uriptr, device->addrname, sizeof(uri) - (uriptr - uri)); |
757 | uriptr += strlen(uriptr); | |
758 | format += 2; | |
759 | } | |
760 | else | |
761 | *uriptr++ = *format++; | |
89d46774 | 762 | |
b94498cf | 763 | *uriptr = '\0'; |
89d46774 | 764 | |
b94498cf | 765 | update_cache(device, uri, NULL, NULL); |
89d46774 | 766 | } |
767 | ||
89d46774 | 768 | return; |
b94498cf | 769 | } |
89d46774 | 770 | |
771 | /* | |
b94498cf | 772 | * Then try the standard ports... |
89d46774 | 773 | */ |
774 | ||
775 | if (!try_connect(&(device->address), device->addrname, 9100)) | |
776 | { | |
777 | debug_printf("DEBUG: %s supports AppSocket!\n", device->addrname); | |
778 | ||
779 | snprintf(uri, sizeof(uri), "socket://%s", device->addrname); | |
780 | update_cache(device, uri, NULL, NULL); | |
781 | } | |
782 | else if (!try_connect(&(device->address), device->addrname, 515)) | |
783 | { | |
784 | debug_printf("DEBUG: %s supports LPD!\n", device->addrname); | |
785 | ||
786 | snprintf(uri, sizeof(uri), "lpd://%s/", device->addrname); | |
787 | update_cache(device, uri, NULL, NULL); | |
788 | } | |
789 | } | |
790 | ||
791 | ||
792 | /* | |
793 | * 'read_snmp_conf()' - Read the snmp.conf file. | |
794 | */ | |
795 | ||
796 | static void | |
797 | read_snmp_conf(const char *address) /* I - Single address to probe */ | |
798 | { | |
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 */ | |
d09495fa | 806 | const char *runtime; /* CUPS_MAX_RUN_TIME env var */ |
89d46774 | 807 | |
808 | ||
809 | /* | |
810 | * Initialize the global address and community lists... | |
811 | */ | |
812 | ||
813 | Addresses = cupsArrayNew(NULL, NULL); | |
814 | Communities = cupsArrayNew(NULL, NULL); | |
815 | ||
816 | if (address) | |
817 | add_array(Addresses, address); | |
818 | ||
819 | if ((debug = getenv("CUPS_DEBUG_LEVEL")) != NULL) | |
820 | DebugLevel = atoi(debug); | |
821 | ||
d09495fa | 822 | if ((runtime = getenv("CUPS_MAX_RUN_TIME")) != NULL) |
823 | MaxRunTime = atoi(runtime); | |
824 | ||
89d46774 | 825 | /* |
826 | * Find the snmp.conf file... | |
827 | */ | |
828 | ||
829 | if ((cups_serverroot = getenv("CUPS_SERVERROOT")) == NULL) | |
830 | cups_serverroot = CUPS_SERVERROOT; | |
831 | ||
832 | snprintf(filename, sizeof(filename), "%s/snmp.conf", cups_serverroot); | |
833 | ||
834 | if ((fp = cupsFileOpen(filename, "r")) != NULL) | |
835 | { | |
836 | /* | |
837 | * Read the snmp.conf file... | |
838 | */ | |
839 | ||
840 | linenum = 0; | |
841 | ||
842 | while (cupsFileGetConf(fp, line, sizeof(line), &value, &linenum)) | |
843 | { | |
844 | if (!value) | |
845 | fprintf(stderr, "ERROR: Missing value on line %d of %s!\n", linenum, | |
846 | filename); | |
88f9aafc | 847 | else if (!_cups_strcasecmp(line, "Address")) |
89d46774 | 848 | { |
849 | if (!address) | |
850 | add_array(Addresses, value); | |
851 | } | |
88f9aafc | 852 | else if (!_cups_strcasecmp(line, "Community")) |
89d46774 | 853 | add_array(Communities, value); |
88f9aafc | 854 | else if (!_cups_strcasecmp(line, "DebugLevel")) |
89d46774 | 855 | DebugLevel = atoi(value); |
88f9aafc | 856 | else if (!_cups_strcasecmp(line, "DeviceURI")) |
b94498cf | 857 | { |
858 | if (*value != '\"') | |
859 | fprintf(stderr, | |
860 | "ERROR: Missing double quote for regular expression on " | |
861 | "line %d of %s!\n", linenum, filename); | |
862 | else | |
863 | add_device_uri(value); | |
864 | } | |
88f9aafc MS |
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")) | |
d09495fa | 871 | MaxRunTime = atoi(value); |
89d46774 | 872 | else |
873 | fprintf(stderr, "ERROR: Unknown directive %s on line %d of %s!\n", | |
874 | line, linenum, filename); | |
875 | } | |
876 | ||
877 | cupsFileClose(fp); | |
878 | } | |
879 | ||
880 | /* | |
881 | * Use defaults if parameters are undefined... | |
882 | */ | |
883 | ||
884 | if (cupsArrayCount(Addresses) == 0) | |
885 | { | |
bc44d920 | 886 | /* |
887 | * If we have no addresses, exit immediately... | |
888 | */ | |
889 | ||
890 | fprintf(stderr, | |
891 | "DEBUG: No address specified and no Address line in %s...\n", | |
892 | filename); | |
893 | exit(0); | |
89d46774 | 894 | } |
895 | ||
896 | if (cupsArrayCount(Communities) == 0) | |
897 | { | |
898 | fputs("INFO: Using default SNMP Community public\n", stderr); | |
899 | add_array(Communities, "public"); | |
900 | } | |
901 | } | |
902 | ||
903 | ||
904 | /* | |
905 | * 'read_snmp_response()' - Read and parse a SNMP response... | |
906 | */ | |
907 | ||
908 | static void | |
909 | read_snmp_response(int fd) /* I - SNMP socket file descriptor */ | |
910 | { | |
89d46774 | 911 | char addrname[256]; /* Source address name */ |
91c84a35 | 912 | cups_snmp_t packet; /* Decoded packet */ |
89d46774 | 913 | snmp_cache_t key, /* Search key */ |
914 | *device; /* Matching device */ | |
915 | ||
916 | ||
917 | /* | |
918 | * Read the response data... | |
919 | */ | |
920 | ||
7a14d768 | 921 | if (!_cupsSNMPRead(fd, &packet, -1.0)) |
89d46774 | 922 | { |
923 | fprintf(stderr, "ERROR: Unable to read data from socket: %s\n", | |
924 | strerror(errno)); | |
925 | return; | |
926 | } | |
927 | ||
928 | if (HostNameLookups) | |
91c84a35 | 929 | httpAddrLookup(&(packet.address), addrname, sizeof(addrname)); |
89d46774 | 930 | else |
91c84a35 | 931 | httpAddrString(&(packet.address), addrname, sizeof(addrname)); |
89d46774 | 932 | |
91c84a35 | 933 | debug_printf("DEBUG: %.3f Received data from %s...\n", run_time(), addrname); |
89d46774 | 934 | |
935 | /* | |
936 | * Look for the response status code in the SNMP message header... | |
937 | */ | |
938 | ||
91c84a35 | 939 | if (packet.error) |
89d46774 | 940 | { |
91c84a35 MS |
941 | fprintf(stderr, "ERROR: Bad SNMP packet from %s: %s\n", addrname, |
942 | packet.error); | |
89d46774 | 943 | |
944 | return; | |
945 | } | |
946 | ||
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); | |
950 | ||
771bd8cb | 951 | if (packet.error_status && packet.request_id != DEVICE_TYPE) |
89d46774 | 952 | return; |
953 | ||
954 | /* | |
955 | * Find a matching device in the cache... | |
956 | */ | |
957 | ||
8ca02f3c | 958 | key.addrname = addrname; |
959 | device = (snmp_cache_t *)cupsArrayFind(Devices, &key); | |
89d46774 | 960 | |
961 | /* | |
962 | * Process the message... | |
963 | */ | |
964 | ||
749b1e90 | 965 | switch (packet.request_id) |
89d46774 | 966 | { |
749b1e90 MS |
967 | case DEVICE_TYPE : |
968 | /* | |
969 | * Got the device type response... | |
970 | */ | |
971 | ||
972 | if (device) | |
973 | { | |
974 | debug_printf("DEBUG: Discarding duplicate device type for \"%s\"...\n", | |
975 | addrname); | |
976 | return; | |
977 | } | |
978 | ||
979 | /* | |
980 | * Add the device and request the device data... | |
981 | */ | |
982 | ||
983 | add_cache(&(packet.address), addrname, NULL, NULL, NULL); | |
984 | ||
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, | |
993 | DEVICE_URI, UriOID); | |
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, | |
ef55b745 | 1005 | DEVICE_ID, LexmarkDeviceIdOID); |
749b1e90 MS |
1006 | _cupsSNMPWrite(fd, &(packet.address), CUPS_SNMP_VERSION_1, |
1007 | packet.community, CUPS_ASN1_GET_REQUEST, | |
1008 | DEVICE_PRODUCT, XeroxProductOID); | |
1009 | break; | |
1010 | ||
1011 | case DEVICE_DESCRIPTION : | |
1012 | if (device && packet.object_type == CUPS_ASN1_OCTET_STRING) | |
1013 | { | |
1014 | /* | |
1015 | * Update an existing cache entry... | |
1016 | */ | |
1017 | ||
1018 | char make_model[256]; /* Make and model */ | |
1019 | ||
1020 | ||
d1c13e16 MS |
1021 | if (strchr((char *)packet.object_value.string.bytes, ':') && |
1022 | strchr((char *)packet.object_value.string.bytes, ';')) | |
749b1e90 MS |
1023 | { |
1024 | /* | |
1025 | * Description is the IEEE-1284 device ID... | |
1026 | */ | |
89d46774 | 1027 | |
749b1e90 | 1028 | if (!device->id) |
d1c13e16 | 1029 | device->id = strdup((char *)packet.object_value.string.bytes); |
89d46774 | 1030 | |
d1c13e16 MS |
1031 | backendGetMakeModel((char *)packet.object_value.string.bytes, |
1032 | make_model, sizeof(make_model)); | |
89d46774 | 1033 | |
749b1e90 MS |
1034 | if (device->info) |
1035 | free(device->info); | |
89d46774 | 1036 | |
749b1e90 MS |
1037 | device->info = strdup(make_model); |
1038 | } | |
1039 | else | |
1040 | { | |
1041 | /* | |
1042 | * Description is plain text... | |
1043 | */ | |
89d46774 | 1044 | |
d1c13e16 | 1045 | fix_make_model(make_model, (char *)packet.object_value.string.bytes, |
749b1e90 | 1046 | sizeof(make_model)); |
89d46774 | 1047 | |
749b1e90 MS |
1048 | if (device->info) |
1049 | free(device->info); | |
89d46774 | 1050 | |
d1c13e16 | 1051 | device->info = strdup((char *)packet.object_value.string.bytes); |
749b1e90 | 1052 | } |
89d46774 | 1053 | |
749b1e90 MS |
1054 | if (!device->make_and_model) |
1055 | device->make_and_model = strdup(make_model); | |
1056 | } | |
1057 | break; | |
89d46774 | 1058 | |
749b1e90 | 1059 | case DEVICE_ID : |
ef55b745 MS |
1060 | if (device && packet.object_type == CUPS_ASN1_OCTET_STRING && |
1061 | (!device->id || | |
1062 | strlen(device->id) < packet.object_value.string.num_bytes)) | |
749b1e90 MS |
1063 | { |
1064 | /* | |
1065 | * Update an existing cache entry... | |
1066 | */ | |
7a14d768 | 1067 | |
749b1e90 | 1068 | char make_model[256]; /* Make and model */ |
89d46774 | 1069 | |
7a14d768 | 1070 | |
749b1e90 MS |
1071 | if (device->id) |
1072 | free(device->id); | |
89d46774 | 1073 | |
d1c13e16 | 1074 | device->id = strdup((char *)packet.object_value.string.bytes); |
7a14d768 | 1075 | |
749b1e90 MS |
1076 | /* |
1077 | * Convert the ID to a make and model string... | |
1078 | */ | |
7a14d768 | 1079 | |
d1c13e16 MS |
1080 | backendGetMakeModel((char *)packet.object_value.string.bytes, |
1081 | make_model, sizeof(make_model)); | |
749b1e90 MS |
1082 | if (device->make_and_model) |
1083 | free(device->make_and_model); | |
7a14d768 | 1084 | |
749b1e90 MS |
1085 | device->make_and_model = strdup(make_model); |
1086 | } | |
1087 | break; | |
7a14d768 | 1088 | |
749b1e90 MS |
1089 | case DEVICE_LOCATION : |
1090 | if (device && packet.object_type == CUPS_ASN1_OCTET_STRING && | |
1091 | !device->location) | |
d1c13e16 | 1092 | device->location = strdup((char *)packet.object_value.string.bytes); |
749b1e90 | 1093 | break; |
7a14d768 | 1094 | |
749b1e90 MS |
1095 | case DEVICE_PRODUCT : |
1096 | if (device && packet.object_type == CUPS_ASN1_OCTET_STRING && | |
1097 | !device->id) | |
1098 | { | |
1099 | /* | |
1100 | * Update an existing cache entry... | |
1101 | */ | |
7a14d768 | 1102 | |
749b1e90 | 1103 | if (!device->info) |
d1c13e16 | 1104 | device->info = strdup((char *)packet.object_value.string.bytes); |
7a14d768 | 1105 | |
749b1e90 MS |
1106 | if (device->make_and_model) |
1107 | free(device->make_and_model); | |
7a14d768 | 1108 | |
d1c13e16 | 1109 | device->make_and_model = strdup((char *)packet.object_value.string.bytes); |
749b1e90 MS |
1110 | } |
1111 | break; | |
7a14d768 | 1112 | |
749b1e90 MS |
1113 | case DEVICE_URI : |
1114 | if (device && packet.object_type == CUPS_ASN1_OCTET_STRING && | |
12f89d24 | 1115 | !device->uri && packet.object_value.string.num_bytes > 3) |
749b1e90 MS |
1116 | { |
1117 | /* | |
1118 | * Update an existing cache entry... | |
1119 | */ | |
7a14d768 | 1120 | |
12f89d24 MS |
1121 | char scheme[32], /* URI scheme */ |
1122 | userpass[256], /* Username:password in URI */ | |
1123 | hostname[256], /* Hostname in URI */ | |
1124 | resource[1024]; /* Resource path in URI */ | |
1125 | int port; /* Port number in URI */ | |
1126 | ||
d1c13e16 | 1127 | if (!strncmp((char *)packet.object_value.string.bytes, "lpr:", 4)) |
749b1e90 MS |
1128 | { |
1129 | /* | |
1130 | * We want "lpd://..." for the URI... | |
1131 | */ | |
7a14d768 | 1132 | |
d1c13e16 | 1133 | packet.object_value.string.bytes[2] = 'd'; |
749b1e90 | 1134 | } |
7a14d768 | 1135 | |
12f89d24 MS |
1136 | if (httpSeparateURI(HTTP_URI_CODING_ALL, |
1137 | (char *)packet.object_value.string.bytes, | |
1138 | scheme, sizeof(scheme), | |
1139 | userpass, sizeof(userpass), | |
1140 | hostname, sizeof(hostname), &port, | |
1141 | resource, sizeof(resource)) >= HTTP_URI_OK) | |
1142 | device->uri = strdup((char *)packet.object_value.string.bytes); | |
749b1e90 MS |
1143 | } |
1144 | break; | |
89d46774 | 1145 | } |
1146 | } | |
1147 | ||
1148 | ||
1149 | /* | |
1150 | * 'run_time()' - Return the total running time... | |
1151 | */ | |
1152 | ||
1153 | static double /* O - Number of seconds */ | |
1154 | run_time(void) | |
1155 | { | |
1156 | struct timeval curtime; /* Current time */ | |
1157 | ||
1158 | ||
1159 | gettimeofday(&curtime, NULL); | |
1160 | ||
1161 | return (curtime.tv_sec - StartTime.tv_sec + | |
1162 | 0.000001 * (curtime.tv_usec - StartTime.tv_usec)); | |
1163 | } | |
1164 | ||
1165 | ||
1166 | /* | |
1167 | * 'scan_devices()' - Scan for devices using SNMP. | |
1168 | */ | |
1169 | ||
1170 | static void | |
22c9029b MS |
1171 | scan_devices(int ipv4, /* I - SNMP IPv4 socket */ |
1172 | int ipv6) /* I - SNMP IPv6 socket */ | |
89d46774 | 1173 | { |
22c9029b MS |
1174 | int fd, /* File descriptor for this address */ |
1175 | busy; /* Are we busy processing something? */ | |
89d46774 | 1176 | char *address, /* Current address */ |
1177 | *community; /* Current community */ | |
1178 | fd_set input; /* Input set for select() */ | |
1179 | struct timeval timeout; /* Timeout for select() */ | |
1180 | time_t endtime; /* End time for scan */ | |
1181 | http_addrlist_t *addrs, /* List of addresses */ | |
1182 | *addr; /* Current address */ | |
1183 | snmp_cache_t *device; /* Current device */ | |
22c9029b | 1184 | char temp[1024]; /* Temporary address string */ |
89d46774 | 1185 | |
1186 | ||
89d46774 | 1187 | gettimeofday(&StartTime, NULL); |
1188 | ||
89d46774 | 1189 | /* |
1190 | * First send all of the broadcast queries... | |
1191 | */ | |
1192 | ||
1193 | for (address = (char *)cupsArrayFirst(Addresses); | |
1194 | address; | |
1195 | address = (char *)cupsArrayNext(Addresses)) | |
1196 | { | |
1197 | if (!strcmp(address, "@LOCAL")) | |
1198 | addrs = get_interface_addresses(NULL); | |
1199 | else if (!strncmp(address, "@IF(", 4)) | |
1200 | { | |
1201 | char ifname[255]; /* Interface name */ | |
1202 | ||
89d46774 | 1203 | strlcpy(ifname, address + 4, sizeof(ifname)); |
1204 | if (ifname[0]) | |
1205 | ifname[strlen(ifname) - 1] = '\0'; | |
1206 | ||
1207 | addrs = get_interface_addresses(ifname); | |
1208 | } | |
1209 | else | |
22c9029b | 1210 | addrs = httpAddrGetList(address, AF_UNSPEC, NULL); |
89d46774 | 1211 | |
1212 | if (!addrs) | |
1213 | { | |
1214 | fprintf(stderr, "ERROR: Unable to scan \"%s\"!\n", address); | |
1215 | continue; | |
1216 | } | |
1217 | ||
1218 | for (community = (char *)cupsArrayFirst(Communities); | |
1219 | community; | |
1220 | community = (char *)cupsArrayNext(Communities)) | |
1221 | { | |
1222 | debug_printf("DEBUG: Scanning for devices in \"%s\" via \"%s\"...\n", | |
1223 | community, address); | |
1224 | ||
1225 | for (addr = addrs; addr; addr = addr->next) | |
22c9029b MS |
1226 | { |
1227 | #ifdef AF_INET6 | |
1228 | if (_httpAddrFamily(&(addr->addr)) == AF_INET6) | |
1229 | fd = ipv6; | |
1230 | else | |
1231 | #endif /* AF_INET6 */ | |
1232 | fd = ipv4; | |
1233 | ||
1234 | debug_printf("DEBUG: Sending get request to %s...\n", | |
1235 | httpAddrString(&(addr->addr), temp, sizeof(temp))); | |
1236 | ||
7a14d768 | 1237 | _cupsSNMPWrite(fd, &(addr->addr), CUPS_SNMP_VERSION_1, community, |
749b1e90 | 1238 | CUPS_ASN1_GET_REQUEST, DEVICE_TYPE, DeviceTypeOID); |
22c9029b | 1239 | } |
89d46774 | 1240 | } |
1241 | ||
1242 | httpAddrFreeList(addrs); | |
1243 | } | |
1244 | ||
1245 | /* | |
1246 | * Then read any responses that come in over the next 3 seconds... | |
1247 | */ | |
1248 | ||
ae71f5de | 1249 | endtime = time(NULL) + MaxRunTime; |
89d46774 | 1250 | |
1251 | FD_ZERO(&input); | |
1252 | ||
1253 | while (time(NULL) < endtime) | |
1254 | { | |
ae71f5de | 1255 | timeout.tv_sec = 2; |
89d46774 | 1256 | timeout.tv_usec = 0; |
1257 | ||
22c9029b MS |
1258 | FD_SET(ipv4, &input); |
1259 | if (ipv6 >= 0) | |
1260 | FD_SET(ipv6, &input); | |
1261 | ||
1262 | fd = ipv4 > ipv6 ? ipv4 : ipv6; | |
89d46774 | 1263 | if (select(fd + 1, &input, NULL, NULL, &timeout) < 0) |
1264 | { | |
22c9029b MS |
1265 | fprintf(stderr, "ERROR: %.3f select() for %d/%d failed: %s\n", run_time(), |
1266 | ipv4, ipv6, strerror(errno)); | |
89d46774 | 1267 | break; |
1268 | } | |
1269 | ||
22c9029b MS |
1270 | busy = 0; |
1271 | ||
1272 | if (FD_ISSET(ipv4, &input)) | |
1273 | { | |
1274 | read_snmp_response(ipv4); | |
1275 | busy = 1; | |
1276 | } | |
1277 | ||
1278 | if (ipv6 >= 0 && FD_ISSET(ipv6, &input)) | |
1279 | { | |
1280 | read_snmp_response(ipv6); | |
1281 | busy = 1; | |
1282 | } | |
1283 | ||
1284 | if (!busy) | |
749b1e90 MS |
1285 | { |
1286 | /* | |
1287 | * List devices with complete information... | |
1288 | */ | |
89d46774 | 1289 | |
749b1e90 | 1290 | int sent_something = 0; |
89d46774 | 1291 | |
749b1e90 MS |
1292 | for (device = (snmp_cache_t *)cupsArrayFirst(Devices); |
1293 | device; | |
1294 | device = (snmp_cache_t *)cupsArrayNext(Devices)) | |
1295 | if (!device->sent && device->info && device->make_and_model) | |
1296 | { | |
1297 | if (device->uri) | |
1298 | list_device(device); | |
1299 | else | |
1300 | probe_device(device); | |
1301 | ||
1302 | device->sent = sent_something = 1; | |
1303 | } | |
1304 | ||
1305 | if (!sent_something) | |
1306 | break; | |
1307 | } | |
1308 | } | |
89d46774 | 1309 | |
1310 | debug_printf("DEBUG: %.3f Scan complete!\n", run_time()); | |
1311 | } | |
1312 | ||
1313 | ||
89d46774 | 1314 | /* |
1315 | * 'try_connect()' - Try connecting on a port... | |
1316 | */ | |
1317 | ||
1318 | static int /* O - 0 on success or -1 on error */ | |
1319 | try_connect(http_addr_t *addr, /* I - Socket address */ | |
1320 | const char *addrname, /* I - Hostname or IP address */ | |
1321 | int port) /* I - Port number */ | |
1322 | { | |
1323 | int fd; /* Socket */ | |
1324 | int status; /* Connection status */ | |
1325 | ||
1326 | ||
1327 | debug_printf("DEBUG: %.3f Trying %s://%s:%d...\n", run_time(), | |
1328 | port == 515 ? "lpd" : "socket", addrname, port); | |
1329 | ||
22c9029b | 1330 | if ((fd = socket(_httpAddrFamily(addr), SOCK_STREAM, 0)) < 0) |
89d46774 | 1331 | { |
c0e1af83 | 1332 | fprintf(stderr, "ERROR: Unable to create socket: %s\n", |
1333 | strerror(errno)); | |
89d46774 | 1334 | return (-1); |
1335 | } | |
1336 | ||
22c9029b | 1337 | _httpAddrSetPort(addr, port); |
89d46774 | 1338 | |
89d46774 | 1339 | alarm(1); |
1340 | ||
1341 | status = connect(fd, (void *)addr, httpAddrLength(addr)); | |
1342 | ||
1343 | close(fd); | |
1344 | alarm(0); | |
1345 | ||
1346 | return (status); | |
1347 | } | |
1348 | ||
1349 | ||
1350 | /* | |
1351 | * 'update_cache()' - Update a cached device... | |
1352 | */ | |
1353 | ||
1354 | static void | |
1355 | update_cache(snmp_cache_t *device, /* I - Device */ | |
1356 | const char *uri, /* I - Device URI */ | |
1357 | const char *id, /* I - Device ID */ | |
1358 | const char *make_model) /* I - Device make and model */ | |
1359 | { | |
1360 | if (device->uri) | |
1361 | free(device->uri); | |
1362 | ||
1363 | device->uri = strdup(uri); | |
1364 | ||
1365 | if (id) | |
1366 | { | |
1367 | if (device->id) | |
1368 | free(device->id); | |
1369 | ||
1370 | device->id = strdup(id); | |
1371 | } | |
1372 | ||
1373 | if (make_model) | |
1374 | { | |
1375 | if (device->make_and_model) | |
1376 | free(device->make_and_model); | |
1377 | ||
1378 | device->make_and_model = strdup(make_model); | |
1379 | } | |
d09495fa | 1380 | |
1381 | list_device(device); | |
89d46774 | 1382 | } |
1383 | ||
1384 | ||
1385 | /* | |
b19ccc9e | 1386 | * End of "$Id: snmp.c 7810 2008-07-29 01:11:15Z mike $". |
89d46774 | 1387 | */ |