]>
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 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 * asn1_decode_snmp() - Decode a SNMP packet.
25 * asn1_debug() - Decode an ASN1-encoded message.
26 * asn1_encode_snmp() - Encode a SNMP packet.
27 * asn1_get_integer() - Get an integer value.
28 * asn1_get_length() - Get a value length.
29 * asn1_get_oid() - Get an OID value.
30 * asn1_get_packed() - Get a packed integer value.
31 * asn1_get_string() - Get a string value.
32 * asn1_get_type() - Get a value type.
33 * asn1_set_integer() - Set an integer value.
34 * asn1_set_length() - Set a value length.
35 * asn1_set_oid() - Set an OID value.
36 * asn1_set_packed() - Set a packed integer value.
37 * asn1_size_integer() - Figure out the number of bytes needed for an
39 * asn1_size_length() - Figure out the number of bytes needed for a
41 * asn1_size_oid() - Figure out the numebr of bytes needed for an
43 * asn1_size_packed() - Figure out the number of bytes needed for a
44 * packed integer value.
45 * compare_cache() - Compare two cache entries.
46 * debug_printf() - Display some debugging information.
47 * fix_make_model() - Fix common problems in the make-and-model
49 * free_array() - Free an array of strings.
50 * free_cache() - Free the array of cached devices.
51 * get_interface_addresses() - Get the broadcast address(es) associated
53 * hex_debug() - Output hex debugging data...
54 * list_device() - List a device we found...
55 * open_snmp_socket() - Open the SNMP broadcast socket.
56 * password_cb() - Handle authentication requests.
57 * probe_device() - Probe a device to discover whether it is a
59 * read_snmp_conf() - Read the snmp.conf file.
60 * read_snmp_response() - Read and parse a SNMP response...
61 * run_time() - Return the total running time...
62 * scan_devices() - Scan for devices using SNMP.
63 * send_snmp_query() - Send an SNMP query packet.
64 * try_connect() - Try connecting on a port...
65 * update_cache() - Update a cached device...
69 * Include necessary headers.
72 #include <cups/http-private.h>
73 #include "backend-private.h"
74 #include <cups/array.h>
75 #include <cups/file.h>
80 * This backend implements SNMP printer discovery. It uses a broadcast-
81 * based approach to get SNMP response packets from potential printers,
82 * tries a mDNS lookup (Mac OS X only at present), a URI lookup based on
83 * the device description string, and finally a probe of port 9100
84 * (AppSocket) and 515 (LPD).
86 * The current focus is on printers with internal network cards, although
87 * the code also works with many external print servers as well. Future
88 * versions will support scanning for vendor-specific SNMP OIDs and the
89 * new PWG Port Monitor MIB and not just the Host MIB OIDs.
91 * The backend reads the snmp.conf file from the CUPS_SERVERROOT directory
92 * which can contain comments, blank lines, or any number of the following
100 * DeviceURI "regex pattern" uri
102 * HostNameLookups off
105 * The default is to use:
110 * HostNameLookups off
113 * This backend is known to work with the following network printers and
116 * Axis OfficeBasic, 5400, 5600
125 * It does not currently work with:
132 * (for all of these, they do not support the Host MIB)
139 #define SNMP_PORT 161 /* SNMP well-known port */
140 #define SNMP_MAX_OID 64 /* Maximum number of OID numbers */
141 #define SNMP_MAX_PACKET 1472 /* Maximum size of SNMP packet */
142 #define SNMP_MAX_STRING 512 /* Maximum size of string */
143 #define SNMP_VERSION_1 0 /* SNMPv1 */
145 #define ASN1_END_OF_CONTENTS 0x00 /* End-of-contents */
146 #define ASN1_BOOLEAN 0x01 /* BOOLEAN */
147 #define ASN1_INTEGER 0x02 /* INTEGER or ENUMERATION */
148 #define ASN1_BIT_STRING 0x03 /* BIT STRING */
149 #define ASN1_OCTET_STRING 0x04 /* OCTET STRING */
150 #define ASN1_NULL_VALUE 0x05 /* NULL VALUE */
151 #define ASN1_OID 0x06 /* OBJECT IDENTIFIER */
152 #define ASN1_SEQUENCE 0x30 /* SEQUENCE */
153 #define ASN1_GET_REQUEST 0xa0 /* Get-Request-PDU */
154 #define ASN1_GET_RESPONSE 0xa2 /* Get-Response-PDU */
161 typedef struct device_uri_s
/**** DeviceURI values ****/
163 regex_t re
; /* Regular expression to match */
164 cups_array_t
*uris
; /* URIs */
167 typedef struct snmp_cache_s
/**** SNMP scan cache ****/
169 http_addr_t address
; /* Address of device */
170 char *addrname
, /* Name of device */
171 *uri
, /* device-uri */
173 *make_and_model
; /* device-make-and-model */
176 typedef struct snmp_packet_s
/**** SNMP packet ****/
178 const char *error
; /* Encode/decode error */
179 int version
; /* Version number */
180 char community
[SNMP_MAX_STRING
];
182 int request_type
; /* Request type */
183 int request_id
; /* request-id value */
184 int error_status
; /* error-status value */
185 int error_index
; /* error-index value */
186 int object_name
[SNMP_MAX_OID
];
187 /* object-name value */
188 int object_type
; /* object-value type */
191 int boolean
; /* Boolean value */
192 int integer
; /* Integer value */
193 int oid
[SNMP_MAX_OID
]; /* OID value */
194 char string
[SNMP_MAX_STRING
];/* String value */
195 } object_value
; /* object-value value */
200 * Private CUPS API to set the last error...
203 extern void _cupsSetError(ipp_status_t status
, const char *message
);
210 static char *add_array(cups_array_t
*a
, const char *s
);
211 static void add_cache(http_addr_t
*addr
, const char *addrname
,
212 const char *uri
, const char *id
,
213 const char *make_and_model
);
214 static device_uri_t
*add_device_uri(char *value
);
215 static void alarm_handler(int sig
);
216 static int asn1_decode_snmp(unsigned char *buffer
, size_t len
,
217 snmp_packet_t
*packet
);
218 static void asn1_debug(unsigned char *buffer
, size_t len
,
220 static int asn1_encode_snmp(unsigned char *buffer
, size_t len
,
221 snmp_packet_t
*packet
);
222 static int asn1_get_integer(unsigned char **buffer
,
223 unsigned char *bufend
,
225 static int asn1_get_oid(unsigned char **buffer
,
226 unsigned char *bufend
,
227 int length
, int *oid
, int oidsize
);
228 static int asn1_get_packed(unsigned char **buffer
,
229 unsigned char *bufend
);
230 static char *asn1_get_string(unsigned char **buffer
,
231 unsigned char *bufend
,
232 int length
, char *string
,
234 static int asn1_get_length(unsigned char **buffer
,
235 unsigned char *bufend
);
236 static int asn1_get_type(unsigned char **buffer
,
237 unsigned char *bufend
);
238 static void asn1_set_integer(unsigned char **buffer
,
240 static void asn1_set_length(unsigned char **buffer
,
242 static void asn1_set_oid(unsigned char **buffer
,
244 static void asn1_set_packed(unsigned char **buffer
,
246 static int asn1_size_integer(int integer
);
247 static int asn1_size_length(int length
);
248 static int asn1_size_oid(const int *oid
);
249 static int asn1_size_packed(int integer
);
250 static int compare_cache(snmp_cache_t
*a
, snmp_cache_t
*b
);
251 static void debug_printf(const char *format
, ...);
252 static void fix_make_model(char *make_model
,
253 const char *old_make_model
,
254 int make_model_size
);
255 static void free_array(cups_array_t
*a
);
256 static void free_cache(void);
257 static http_addrlist_t
*get_interface_addresses(const char *ifname
);
258 static void hex_debug(unsigned char *buffer
, size_t len
);
259 static void list_device(snmp_cache_t
*cache
);
260 static int open_snmp_socket(void);
261 static const char *password_cb(const char *prompt
);
262 static void probe_device(snmp_cache_t
*device
);
263 static void read_snmp_conf(const char *address
);
264 static void read_snmp_response(int fd
);
265 static double run_time(void);
266 static void scan_devices(int fd
);
267 static void send_snmp_query(int fd
, http_addr_t
*addr
, int version
,
268 const char *community
,
269 const unsigned request_id
,
271 static int try_connect(http_addr_t
*addr
, const char *addrname
,
273 static void update_cache(snmp_cache_t
*device
, const char *uri
,
274 const char *id
, const char *make_model
);
281 static cups_array_t
*Addresses
= NULL
;
282 static cups_array_t
*Communities
= NULL
;
283 static cups_array_t
*Devices
= NULL
;
284 static int DebugLevel
= 0;
285 static int DeviceDescOID
[] = { 1, 3, 6, 1, 2, 1, 25, 3,
287 static unsigned DeviceDescRequest
;
288 static int DeviceTypeOID
[] = { 1, 3, 6, 1, 2, 1, 25, 3,
290 static unsigned DeviceTypeRequest
;
291 static cups_array_t
*DeviceURIs
= NULL
;
292 static int HostNameLookups
= 0;
293 static int MaxRunTime
= 10;
294 static struct timeval StartTime
;
298 * 'main()' - Discover printers via SNMP.
301 int /* O - Exit status */
302 main(int argc
, /* I - Number of command-line arguments (6 or 7) */
303 char *argv
[]) /* I - Command-line arguments */
305 int fd
; /* SNMP socket */
306 #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
307 struct sigaction action
; /* Actions for POSIX signals */
308 #endif /* HAVE_SIGACTION && !HAVE_SIGSET */
312 * Check command-line options...
317 fputs(_("Usage: snmp [host-or-ip-address]\n"), stderr
);
322 * Set the password callback for IPP operations...
325 cupsSetPasswordCB(password_cb
);
328 * Catch SIGALRM signals...
332 sigset(SIGALRM
, alarm_handler
);
333 #elif defined(HAVE_SIGACTION)
334 memset(&action
, 0, sizeof(action
));
336 sigemptyset(&action
.sa_mask
);
337 sigaddset(&action
.sa_mask
, SIGALRM
);
338 action
.sa_handler
= alarm_handler
;
339 sigaction(SIGALRM
, &action
, NULL
);
341 signal(SIGALRM
, alarm_handler
);
342 #endif /* HAVE_SIGSET */
345 * Open the SNMP socket...
348 if ((fd
= open_snmp_socket()) < 0)
352 * Read the configuration file and any cache data...
355 read_snmp_conf(argv
[1]);
357 Devices
= cupsArrayNew((cups_array_func_t
)compare_cache
, NULL
);
360 * Scan for devices...
366 * Close, free, and return with no errors...
371 free_array(Addresses
);
372 free_array(Communities
);
380 * 'add_array()' - Add a string to an array.
383 static char * /* O - New string */
384 add_array(cups_array_t
*a
, /* I - Array */
385 const char *s
) /* I - String to add */
387 char *dups
; /* New string */
392 cupsArrayAdd(a
, dups
);
399 * 'add_cache()' - Add a cached device...
403 add_cache(http_addr_t
*addr
, /* I - Device IP address */
404 const char *addrname
, /* I - IP address or name string */
405 const char *uri
, /* I - Device URI */
406 const char *id
, /* I - 1284 device ID */
407 const char *make_and_model
) /* I - Make and model */
409 snmp_cache_t
*temp
; /* New device entry */
412 debug_printf("DEBUG: add_cache(addr=%p, addrname=\"%s\", uri=\"%s\", "
413 "id=\"%s\", make_and_model=\"%s\")\n",
414 addr
, addrname
, uri
? uri
: "(null)", id
? id
: "(null)",
415 make_and_model
? make_and_model
: "(null)");
417 temp
= calloc(1, sizeof(snmp_cache_t
));
418 memcpy(&(temp
->address
), addr
, sizeof(temp
->address
));
420 temp
->addrname
= strdup(addrname
);
423 temp
->uri
= strdup(uri
);
426 temp
->id
= strdup(id
);
429 temp
->make_and_model
= strdup(make_and_model
);
431 cupsArrayAdd(Devices
, temp
);
439 * 'add_device_uri()' - Add a device URI to the cache.
441 * The value string is modified (chopped up) as needed.
444 static device_uri_t
* /* O - Device URI */
445 add_device_uri(char *value
) /* I - Value from snmp.conf */
447 device_uri_t
*device_uri
; /* Device URI */
448 char *start
; /* Start of value */
452 * Allocate memory as needed...
456 DeviceURIs
= cupsArrayNew(NULL
, NULL
);
461 if ((device_uri
= calloc(1, sizeof(device_uri_t
))) == NULL
)
464 if ((device_uri
->uris
= cupsArrayNew(NULL
, NULL
)) == NULL
)
471 * Scan the value string for the regular expression and URI(s)...
474 value
++; /* Skip leading " */
476 for (start
= value
; *value
&& *value
!= '\"'; value
++)
477 if (*value
== '\\' && value
[1])
478 _cups_strcpy(value
, value
+ 1);
482 fputs("ERROR: Missing end quote for DeviceURI!\n", stderr
);
484 cupsArrayDelete(device_uri
->uris
);
492 if (regcomp(&(device_uri
->re
), start
, REG_EXTENDED
| REG_ICASE
))
494 fputs("ERROR: Bad regular expression for DeviceURI!\n", stderr
);
496 cupsArrayDelete(device_uri
->uris
);
504 while (isspace(*value
& 255))
510 for (start
= value
; *value
&& !isspace(*value
& 255); value
++);
515 cupsArrayAdd(device_uri
->uris
, strdup(start
));
519 * Add the device URI to the list and return it...
522 cupsArrayAdd(DeviceURIs
, device_uri
);
529 * 'alarm_handler()' - Handle alarm signals...
533 alarm_handler(int sig
) /* I - Signal number */
541 #if !defined(HAVE_SIGSET) && !defined(HAVE_SIGACTION)
542 signal(SIGALRM
, alarm_handler
);
543 #endif /* !HAVE_SIGSET && !HAVE_SIGACTION */
546 write(2, "DEBUG: ALARM!\n", 14);
551 * 'asn1_decode_snmp()' - Decode a SNMP packet.
554 static int /* O - 0 on success, -1 on error */
555 asn1_decode_snmp(unsigned char *buffer
, /* I - Buffer */
556 size_t len
, /* I - Size of buffer */
557 snmp_packet_t
*packet
) /* I - SNMP packet */
559 unsigned char *bufptr
, /* Pointer into the data */
560 *bufend
; /* End of data */
561 int length
; /* Length of value */
565 * Initialize the decoding...
568 memset(packet
, 0, sizeof(snmp_packet_t
));
571 bufend
= buffer
+ len
;
573 if (asn1_get_type(&bufptr
, bufend
) != ASN1_SEQUENCE
)
574 packet
->error
= "Packet does not start with SEQUENCE";
575 else if (asn1_get_length(&bufptr
, bufend
) == 0)
576 packet
->error
= "SEQUENCE uses indefinite length";
577 else if (asn1_get_type(&bufptr
, bufend
) != ASN1_INTEGER
)
578 packet
->error
= "No version number";
579 else if ((length
= asn1_get_length(&bufptr
, bufend
)) == 0)
580 packet
->error
= "Version uses indefinite length";
581 else if ((packet
->version
= asn1_get_integer(&bufptr
, bufend
, length
))
583 packet
->error
= "Bad SNMP version number";
584 else if (asn1_get_type(&bufptr
, bufend
) != ASN1_OCTET_STRING
)
585 packet
->error
= "No community name";
586 else if ((length
= asn1_get_length(&bufptr
, bufend
)) == 0)
587 packet
->error
= "Community name uses indefinite length";
590 asn1_get_string(&bufptr
, bufend
, length
, packet
->community
,
591 sizeof(packet
->community
));
593 if ((packet
->request_type
= asn1_get_type(&bufptr
, bufend
))
594 != ASN1_GET_RESPONSE
)
595 packet
->error
= "Packet does not contain a Get-Response-PDU";
596 else if (asn1_get_length(&bufptr
, bufend
) == 0)
597 packet
->error
= "Get-Response-PDU uses indefinite length";
598 else if (asn1_get_type(&bufptr
, bufend
) != ASN1_INTEGER
)
599 packet
->error
= "No request-id";
600 else if ((length
= asn1_get_length(&bufptr
, bufend
)) == 0)
601 packet
->error
= "request-id uses indefinite length";
604 packet
->request_id
= asn1_get_integer(&bufptr
, bufend
, length
);
606 if (asn1_get_type(&bufptr
, bufend
) != ASN1_INTEGER
)
607 packet
->error
= "No error-status";
608 else if ((length
= asn1_get_length(&bufptr
, bufend
)) == 0)
609 packet
->error
= "error-status uses indefinite length";
612 packet
->error_status
= asn1_get_integer(&bufptr
, bufend
, length
);
614 if (asn1_get_type(&bufptr
, bufend
) != ASN1_INTEGER
)
615 packet
->error
= "No error-index";
616 else if ((length
= asn1_get_length(&bufptr
, bufend
)) == 0)
617 packet
->error
= "error-index uses indefinite length";
620 packet
->error_index
= asn1_get_integer(&bufptr
, bufend
, length
);
622 if (asn1_get_type(&bufptr
, bufend
) != ASN1_SEQUENCE
)
623 packet
->error
= "No variable-bindings SEQUENCE";
624 else if (asn1_get_length(&bufptr
, bufend
) == 0)
625 packet
->error
= "variable-bindings uses indefinite length";
626 else if (asn1_get_type(&bufptr
, bufend
) != ASN1_SEQUENCE
)
627 packet
->error
= "No VarBind SEQUENCE";
628 else if (asn1_get_length(&bufptr
, bufend
) == 0)
629 packet
->error
= "VarBind uses indefinite length";
630 else if (asn1_get_type(&bufptr
, bufend
) != ASN1_OID
)
631 packet
->error
= "No name OID";
632 else if ((length
= asn1_get_length(&bufptr
, bufend
)) == 0)
633 packet
->error
= "Name OID uses indefinite length";
636 asn1_get_oid(&bufptr
, bufend
, length
, packet
->object_name
,
639 packet
->object_type
= asn1_get_type(&bufptr
, bufend
);
641 if ((length
= asn1_get_length(&bufptr
, bufend
)) == 0 &&
642 packet
->object_type
!= ASN1_NULL_VALUE
&&
643 packet
->object_type
!= ASN1_OCTET_STRING
)
644 packet
->error
= "Value uses indefinite length";
647 switch (packet
->object_type
)
650 packet
->object_value
.boolean
=
651 asn1_get_integer(&bufptr
, bufend
, length
);
655 packet
->object_value
.integer
=
656 asn1_get_integer(&bufptr
, bufend
, length
);
659 case ASN1_NULL_VALUE
:
662 case ASN1_OCTET_STRING
:
663 asn1_get_string(&bufptr
, bufend
, length
,
664 packet
->object_value
.string
,
669 asn1_get_oid(&bufptr
, bufend
, length
,
670 packet
->object_value
.oid
, SNMP_MAX_OID
);
674 packet
->error
= "Unsupported value type";
684 return (packet
->error
? -1 : 0);
689 * 'asn1_debug()' - Decode an ASN1-encoded message.
693 asn1_debug(unsigned char *buffer
, /* I - Buffer */
694 size_t len
, /* I - Length of buffer */
695 int indent
) /* I - Indentation */
697 int i
; /* Looping var */
698 unsigned char *bufend
; /* End of buffer */
699 int integer
; /* Number value */
700 int oid
[SNMP_MAX_OID
]; /* OID value */
701 char string
[SNMP_MAX_STRING
];/* String value */
702 unsigned char value_type
; /* Type of value */
703 int value_length
; /* Length of value */
706 bufend
= buffer
+ len
;
708 while (buffer
< bufend
)
714 value_type
= asn1_get_type(&buffer
, bufend
);
715 value_length
= asn1_get_length(&buffer
, bufend
);
720 integer
= asn1_get_integer(&buffer
, bufend
, value_length
);
722 fprintf(stderr
, "DEBUG: %*sBOOLEAN %d bytes %d\n", indent
, "",
723 value_length
, integer
);
727 integer
= asn1_get_integer(&buffer
, bufend
, value_length
);
729 fprintf(stderr
, "DEBUG: %*sINTEGER %d bytes %d\n", indent
, "",
730 value_length
, integer
);
733 case ASN1_OCTET_STRING
:
734 fprintf(stderr
, "DEBUG: %*sOCTET STRING %d bytes \"%s\"\n", indent
, "",
735 value_length
, asn1_get_string(&buffer
, bufend
,
736 value_length
, string
,
740 case ASN1_NULL_VALUE
:
741 fprintf(stderr
, "DEBUG: %*sNULL VALUE %d bytes\n", indent
, "",
744 buffer
+= value_length
;
748 asn1_get_oid(&buffer
, bufend
, value_length
, oid
, SNMP_MAX_OID
);
750 fprintf(stderr
, "DEBUG: %*sOID %d bytes ", indent
, "",
752 for (i
= 0; oid
[i
]; i
++)
753 fprintf(stderr
, ".%d", oid
[i
]);
758 fprintf(stderr
, "DEBUG: %*sSEQUENCE %d bytes\n", indent
, "",
760 asn1_debug(buffer
, value_length
, indent
+ 4);
762 buffer
+= value_length
;
765 case ASN1_GET_REQUEST
:
766 fprintf(stderr
, "DEBUG: %*sGet-Request-PDU %d bytes\n", indent
, "",
768 asn1_debug(buffer
, value_length
, indent
+ 4);
770 buffer
+= value_length
;
773 case ASN1_GET_RESPONSE
:
774 fprintf(stderr
, "DEBUG: %*sGet-Response-PDU %d bytes\n", indent
, "",
776 asn1_debug(buffer
, value_length
, indent
+ 4);
778 buffer
+= value_length
;
782 fprintf(stderr
, "DEBUG: %*sUNKNOWN(%x) %d bytes\n", indent
, "",
783 value_type
, value_length
);
785 buffer
+= value_length
;
793 * 'asn1_encode_snmp()' - Encode a SNMP packet.
796 static int /* O - Length on success, -1 on error */
797 asn1_encode_snmp(unsigned char *buffer
, /* I - Buffer */
798 size_t bufsize
, /* I - Size of buffer */
799 snmp_packet_t
*packet
) /* I - SNMP packet */
801 unsigned char *bufptr
; /* Pointer into buffer */
802 int total
, /* Total length */
803 msglen
, /* Length of entire message */
804 commlen
, /* Length of community string */
805 reqlen
, /* Length of request */
806 listlen
, /* Length of variable list */
807 varlen
, /* Length of variable */
808 namelen
, /* Length of object name OID */
809 valuelen
; /* Length of object value */
813 * Get the lengths of the community string, OID, and message...
816 namelen
= asn1_size_oid(packet
->object_name
);
818 switch (packet
->object_type
)
820 case ASN1_NULL_VALUE
:
825 valuelen
= asn1_size_integer(packet
->object_value
.boolean
);
829 valuelen
= asn1_size_integer(packet
->object_value
.integer
);
832 case ASN1_OCTET_STRING
:
833 valuelen
= strlen(packet
->object_value
.string
);
837 valuelen
= asn1_size_oid(packet
->object_value
.oid
);
841 packet
->error
= "Unknown object type";
845 varlen
= 1 + asn1_size_length(namelen
) + namelen
+
846 1 + asn1_size_length(valuelen
) + valuelen
;
847 listlen
= 1 + asn1_size_length(varlen
) + varlen
;
848 reqlen
= 2 + asn1_size_integer(packet
->request_id
) +
849 2 + asn1_size_integer(packet
->error_status
) +
850 2 + asn1_size_integer(packet
->error_index
) +
851 1 + asn1_size_length(listlen
) + listlen
;
852 commlen
= strlen(packet
->community
);
853 msglen
= 2 + asn1_size_integer(packet
->version
) +
854 1 + asn1_size_length(commlen
) + commlen
+
855 1 + asn1_size_length(reqlen
) + reqlen
;
856 total
= 1 + asn1_size_length(msglen
) + msglen
;
860 packet
->error
= "Message too large for buffer";
865 * Then format the message...
870 *bufptr
++ = ASN1_SEQUENCE
; /* SNMPv1 message header */
871 asn1_set_length(&bufptr
, msglen
);
873 asn1_set_integer(&bufptr
, packet
->version
);
876 *bufptr
++ = ASN1_OCTET_STRING
; /* community */
877 asn1_set_length(&bufptr
, commlen
);
878 memcpy(bufptr
, packet
->community
, commlen
);
881 *bufptr
++ = packet
->request_type
; /* Get-Request-PDU */
882 asn1_set_length(&bufptr
, reqlen
);
884 asn1_set_integer(&bufptr
, packet
->request_id
);
886 asn1_set_integer(&bufptr
, packet
->error_status
);
888 asn1_set_integer(&bufptr
, packet
->error_index
);
890 *bufptr
++ = ASN1_SEQUENCE
; /* variable-bindings */
891 asn1_set_length(&bufptr
, listlen
);
893 *bufptr
++ = ASN1_SEQUENCE
; /* variable */
894 asn1_set_length(&bufptr
, varlen
);
896 asn1_set_oid(&bufptr
, packet
->object_name
);
899 switch (packet
->object_type
)
901 case ASN1_NULL_VALUE
:
902 *bufptr
++ = ASN1_NULL_VALUE
; /* ObjectValue */
903 *bufptr
++ = 0; /* Length */
907 asn1_set_integer(&bufptr
, packet
->object_value
.boolean
);
911 asn1_set_integer(&bufptr
, packet
->object_value
.integer
);
914 case ASN1_OCTET_STRING
:
915 *bufptr
++ = ASN1_OCTET_STRING
;
916 asn1_set_length(&bufptr
, valuelen
);
917 memcpy(bufptr
, packet
->object_value
.string
, valuelen
);
922 asn1_set_oid(&bufptr
, packet
->object_value
.oid
);
926 return (bufptr
- buffer
);
931 * 'asn1_get_integer()' - Get an integer value.
934 static int /* O - Integer value */
936 unsigned char **buffer
, /* IO - Pointer in buffer */
937 unsigned char *bufend
, /* I - End of buffer */
938 int length
) /* I - Length of value */
940 int value
; /* Integer value */
944 length
> 0 && *buffer
< bufend
;
945 length
--, (*buffer
) ++)
946 value
= (value
<< 8) | **buffer
;
953 * 'asn1_get_length()' - Get a value length.
956 static int /* O - Length */
957 asn1_get_length(unsigned char **buffer
, /* IO - Pointer in buffer */
958 unsigned char *bufend
) /* I - End of buffer */
960 int length
; /* Length */
967 length
= asn1_get_integer(buffer
, bufend
, length
& 127);
974 * 'asn1_get_oid()' - Get an OID value.
977 static int /* O - Last OID number */
979 unsigned char **buffer
, /* IO - Pointer in buffer */
980 unsigned char *bufend
, /* I - End of buffer */
981 int length
, /* I - Length of value */
982 int *oid
, /* I - OID buffer */
983 int oidsize
) /* I - Size of OID buffer */
985 unsigned char *valend
; /* End of value */
986 int *oidend
; /* End of OID buffer */
987 int number
; /* OID number */
990 valend
= *buffer
+ length
;
991 oidend
= oid
+ oidsize
- 1;
996 number
= asn1_get_packed(buffer
, bufend
);
1000 *oid
++ = number
/ 40;
1001 number
= number
% 40;
1011 while (*buffer
< valend
)
1013 number
= asn1_get_packed(buffer
, bufend
);
1026 * 'asn1_get_packed()' - Get a packed integer value.
1029 static int /* O - Value */
1031 unsigned char **buffer
, /* IO - Pointer in buffer */
1032 unsigned char *bufend
) /* I - End of buffer */
1034 int value
; /* Value */
1039 while ((**buffer
& 128) && *buffer
< bufend
)
1041 value
= (value
<< 7) | (**buffer
& 127);
1045 if (*buffer
< bufend
)
1047 value
= (value
<< 7) | **buffer
;
1056 * 'asn1_get_string()' - Get a string value.
1059 static char * /* O - String */
1061 unsigned char **buffer
, /* IO - Pointer in buffer */
1062 unsigned char *bufend
, /* I - End of buffer */
1063 int length
, /* I - Value length */
1064 char *string
, /* I - String buffer */
1065 int strsize
) /* I - String buffer size */
1067 if (length
< strsize
)
1069 memcpy(string
, *buffer
, length
);
1070 string
[length
] = '\0';
1074 memcpy(string
, buffer
, strsize
- 1);
1075 string
[strsize
- 1] = '\0';
1078 (*buffer
) += length
;
1085 * 'asn1_get_type()' - Get a value type.
1088 static int /* O - Type */
1089 asn1_get_type(unsigned char **buffer
, /* IO - Pointer in buffer */
1090 unsigned char *bufend
) /* I - End of buffer */
1092 int type
; /* Type */
1098 if ((type
& 31) == 31)
1099 type
= asn1_get_packed(buffer
, bufend
);
1106 * 'asn1_set_integer()' - Set an integer value.
1110 asn1_set_integer(unsigned char **buffer
,/* IO - Pointer in buffer */
1111 int integer
) /* I - Integer value */
1113 **buffer
= ASN1_INTEGER
;
1116 if (integer
> 0x7fffff || integer
< -0x800000)
1120 **buffer
= integer
>> 24;
1122 **buffer
= integer
>> 16;
1124 **buffer
= integer
>> 8;
1129 else if (integer
> 0x7fff || integer
< -0x8000)
1133 **buffer
= integer
>> 16;
1135 **buffer
= integer
>> 8;
1140 else if (integer
> 0x7f || integer
< -0x80)
1144 **buffer
= integer
>> 8;
1160 * 'asn1_set_length()' - Set a value length.
1164 asn1_set_length(unsigned char **buffer
, /* IO - Pointer in buffer */
1165 int length
) /* I - Length value */
1169 **buffer
= 0x82; /* 2-byte length */
1171 **buffer
= length
>> 8;
1176 else if (length
> 127)
1178 **buffer
= 0x81; /* 1-byte length */
1185 **buffer
= length
; /* Length */
1192 * 'asn1_set_oid()' - Set an OID value.
1196 asn1_set_oid(unsigned char **buffer
, /* IO - Pointer in buffer */
1197 const int *oid
) /* I - OID value */
1199 **buffer
= ASN1_OID
;
1202 asn1_set_length(buffer
, asn1_size_oid(oid
));
1204 asn1_set_packed(buffer
, oid
[0] * 40 + oid
[1]);
1206 for (oid
+= 2; *oid
; oid
++)
1207 asn1_set_packed(buffer
, *oid
);
1212 * 'asn1_set_packed()' - Set a packed integer value.
1216 asn1_set_packed(unsigned char **buffer
, /* IO - Pointer in buffer */
1217 int integer
) /* I - Integer value */
1219 if (integer
> 0xfffffff)
1221 **buffer
= (integer
>> 28) & 0x7f;
1225 if (integer
> 0x1fffff)
1227 **buffer
= (integer
>> 21) & 0x7f;
1231 if (integer
> 0x3fff)
1233 **buffer
= (integer
>> 14) & 0x7f;
1239 **buffer
= (integer
>> 7) & 0x7f;
1243 **buffer
= integer
& 0x7f;
1248 * 'asn1_size_integer()' - Figure out the number of bytes needed for an
1252 static int /* O - Size in bytes */
1253 asn1_size_integer(int integer
) /* I - Integer value */
1255 if (integer
> 0x7fffff || integer
< -0x800000)
1257 else if (integer
> 0x7fff || integer
< -0x8000)
1259 else if (integer
> 0x7f || integer
< -0x80)
1267 * 'asn1_size_length()' - Figure out the number of bytes needed for a
1271 static int /* O - Size in bytes */
1272 asn1_size_length(int length
) /* I - Length value */
1276 else if (length
> 0x7f)
1284 * 'asn1_size_oid()' - Figure out the numebr of bytes needed for an
1288 static int /* O - Size in bytes */
1289 asn1_size_oid(const int *oid
) /* I - OID value */
1291 int length
; /* Length of value */
1294 for (length
= asn1_size_packed(oid
[0] * 40 + oid
[1]), oid
+= 2; *oid
; oid
++)
1295 length
+= asn1_size_packed(*oid
);
1302 * 'asn1_size_packed()' - Figure out the number of bytes needed for a
1303 * packed integer value.
1306 static int /* O - Size in bytes */
1307 asn1_size_packed(int integer
) /* I - Integer value */
1309 if (integer
> 0xfffffff)
1311 else if (integer
> 0x1fffff)
1313 else if (integer
> 0x3fff)
1315 else if (integer
> 0x7f)
1323 * 'compare_cache()' - Compare two cache entries.
1326 static int /* O - Result of comparison */
1327 compare_cache(snmp_cache_t
*a
, /* I - First cache entry */
1328 snmp_cache_t
*b
) /* I - Second cache entry */
1330 return (strcasecmp(a
->addrname
, b
->addrname
));
1335 * 'debug_printf()' - Display some debugging information.
1339 debug_printf(const char *format
, /* I - Printf-style format string */
1340 ...) /* I - Additional arguments as needed */
1342 va_list ap
; /* Pointer to arguments */
1348 va_start(ap
, format
);
1349 vfprintf(stderr
, format
, ap
);
1355 * 'fix_make_model()' - Fix common problems in the make-and-model string.
1360 char *make_model
, /* I - New make-and-model string */
1361 const char *old_make_model
, /* I - Old make-and-model string */
1362 int make_model_size
) /* I - Size of new string buffer */
1364 char *mmptr
; /* Pointer into make-and-model string */
1368 * Fix some common problems with the make-and-model string so
1369 * that printer driver detection works better...
1372 if (!strncasecmp(old_make_model
, "Hewlett-Packard", 15))
1375 * Strip leading Hewlett-Packard and hp prefixes and replace
1376 * with a single HP manufacturer prefix...
1379 mmptr
= (char *)old_make_model
+ 15;
1381 while (isspace(*mmptr
& 255))
1384 if (!strncasecmp(mmptr
, "hp", 2))
1388 while (isspace(*mmptr
& 255))
1392 make_model
[0] = 'H';
1393 make_model
[1] = 'P';
1394 make_model
[2] = ' ';
1395 strlcpy(make_model
+ 3, mmptr
, make_model_size
- 3);
1397 else if (!strncasecmp(old_make_model
, "deskjet", 7))
1398 snprintf(make_model
, make_model_size
, "HP DeskJet%s", old_make_model
+ 7);
1399 else if (!strncasecmp(old_make_model
, "officejet", 9))
1400 snprintf(make_model
, make_model_size
, "HP OfficeJet%s", old_make_model
+ 9);
1401 else if (!strncasecmp(old_make_model
, "stylus_pro_", 11))
1402 snprintf(make_model
, make_model_size
, "EPSON Stylus Pro %s",
1403 old_make_model
+ 11);
1405 strlcpy(make_model
, old_make_model
, make_model_size
);
1407 if ((mmptr
= strstr(make_model
, ", Inc.,")) != NULL
)
1410 * Strip inc. from name, e.g. "Tektronix, Inc., Phaser 560"
1411 * becomes "Tektronix Phaser 560"...
1414 _cups_strcpy(mmptr
, mmptr
+ 7);
1417 if ((mmptr
= strstr(make_model
, " Network")) != NULL
)
1420 * Drop unnecessary informational text, e.g. "Xerox DocuPrint N2025
1421 * Network LaserJet - 2.12" becomes "Xerox DocuPrint N2025"...
1427 if ((mmptr
= strchr(make_model
, ',')) != NULL
)
1430 * Drop anything after a trailing comma...
1439 * 'free_array()' - Free an array of strings.
1443 free_array(cups_array_t
*a
) /* I - Array */
1445 char *s
; /* Current string */
1448 for (s
= (char *)cupsArrayFirst(a
); s
; s
= (char *)cupsArrayNext(a
))
1456 * 'free_cache()' - Free the array of cached devices.
1462 snmp_cache_t
*cache
; /* Cached device */
1465 for (cache
= (snmp_cache_t
*)cupsArrayFirst(Devices
);
1467 cache
= (snmp_cache_t
*)cupsArrayNext(Devices
))
1469 free(cache
->addrname
);
1477 if (cache
->make_and_model
)
1478 free(cache
->make_and_model
);
1483 cupsArrayDelete(Devices
);
1489 * 'get_interface_addresses()' - Get the broadcast address(es) associated
1490 * with an interface.
1493 static http_addrlist_t
* /* O - List of addresses */
1494 get_interface_addresses(
1495 const char *ifname
) /* I - Interface name */
1497 struct ifaddrs
*addrs
, /* Interface address list */
1498 *addr
; /* Current interface address */
1499 http_addrlist_t
*first
, /* First address in list */
1500 *last
, /* Last address in list */
1501 *current
; /* Current address */
1504 if (getifaddrs(&addrs
) < 0)
1507 for (addr
= addrs
, first
= NULL
, last
= NULL
; addr
; addr
= addr
->ifa_next
)
1508 if ((addr
->ifa_flags
& IFF_BROADCAST
) && addr
->ifa_broadaddr
&&
1509 addr
->ifa_broadaddr
->sa_family
== AF_INET
&&
1510 (!ifname
|| !strcmp(ifname
, addr
->ifa_name
)))
1512 current
= calloc(1, sizeof(http_addrlist_t
));
1514 memcpy(&(current
->addr
), addr
->ifa_broadaddr
,
1515 sizeof(struct sockaddr_in
));
1520 last
->next
= current
;
1532 * 'hex_debug()' - Output hex debugging data...
1536 hex_debug(unsigned char *buffer
, /* I - Buffer */
1537 size_t len
) /* I - Number of bytes */
1539 int col
; /* Current column */
1542 fputs("DEBUG: Hex dump of packet:\n", stderr
);
1544 for (col
= 0; len
> 0; col
++, buffer
++, len
--)
1546 if ((col
& 15) == 0)
1547 fprintf(stderr
, "DEBUG: %04X ", col
);
1549 fprintf(stderr
, " %02X", *buffer
);
1551 if ((col
& 15) == 15)
1561 * 'list_device()' - List a device we found...
1565 list_device(snmp_cache_t
*cache
) /* I - Cached device */
1568 printf("network %s \"%s\" \"%s %s\" \"%s\"\n",
1570 cache
->make_and_model
? cache
->make_and_model
: "Unknown",
1571 cache
->make_and_model
? cache
->make_and_model
: "Unknown",
1572 cache
->addrname
, cache
->id
? cache
->id
: "");
1577 * 'open_snmp_socket()' - Open the SNMP broadcast socket.
1580 static int /* O - SNMP socket file descriptor */
1581 open_snmp_socket(void)
1583 int fd
; /* SNMP socket file descriptor */
1584 int val
; /* Socket option value */
1588 * Create the SNMP socket...
1591 if ((fd
= socket(AF_INET
, SOCK_DGRAM
, 0)) < 0)
1593 fprintf(stderr
, "ERROR: Unable to create SNMP socket - %s\n",
1600 * Set the "broadcast" flag...
1605 if (setsockopt(fd
, SOL_SOCKET
, SO_BROADCAST
, &val
, sizeof(val
)))
1607 fprintf(stderr
, "ERROR: Unable to set broadcast mode - %s\n",
1620 * 'password_cb()' - Handle authentication requests.
1622 * All we do right now is return NULL, indicating that no authentication
1626 static const char * /* O - Password (NULL) */
1627 password_cb(const char *prompt
) /* I - Prompt message */
1629 (void)prompt
; /* Anti-compiler-warning-code */
1636 * 'probe_device()' - Probe a device to discover whether it is a printer.
1638 * TODO: Try using the Port Monitor MIB to discover the correct protocol
1639 * to use - first need a commercially-available printer that supports
1644 probe_device(snmp_cache_t
*device
) /* I - Device */
1646 char uri
[1024], /* Full device URI */
1647 *uriptr
, /* Pointer into URI */
1648 *format
; /* Format string for device */
1649 device_uri_t
*device_uri
; /* Current DeviceURI match */
1652 debug_printf("DEBUG: %.3f Probing %s...\n", run_time(), device
->addrname
);
1656 * TODO: Try an mDNS query first, and then fallback on direct probes...
1659 if (!try_connect(&(device
->address
), device
->addrname
, 5353))
1661 debug_printf("DEBUG: %s supports mDNS, not reporting!\n", device
->addrname
);
1664 #endif /* __APPLE__ */
1667 * Lookup the device in the match table...
1670 for (device_uri
= (device_uri_t
*)cupsArrayFirst(DeviceURIs
);
1672 device_uri
= (device_uri_t
*)cupsArrayNext(DeviceURIs
))
1673 if (!regexec(&(device_uri
->re
), device
->make_and_model
, 0, NULL
, 0))
1676 * Found a match, add the URIs...
1679 for (format
= (char *)cupsArrayFirst(device_uri
->uris
);
1681 format
= (char *)cupsArrayNext(device_uri
->uris
))
1683 for (uriptr
= uri
; *format
&& uriptr
< (uri
+ sizeof(uri
) - 1);)
1684 if (*format
== '%' && format
[1] == 's')
1687 * Insert hostname/address...
1690 strlcpy(uriptr
, device
->addrname
, sizeof(uri
) - (uriptr
- uri
));
1691 uriptr
+= strlen(uriptr
);
1695 *uriptr
++ = *format
++;
1699 update_cache(device
, uri
, NULL
, NULL
);
1706 * Then try the standard ports...
1709 if (!try_connect(&(device
->address
), device
->addrname
, 9100))
1711 debug_printf("DEBUG: %s supports AppSocket!\n", device
->addrname
);
1713 snprintf(uri
, sizeof(uri
), "socket://%s", device
->addrname
);
1714 update_cache(device
, uri
, NULL
, NULL
);
1716 else if (!try_connect(&(device
->address
), device
->addrname
, 515))
1718 debug_printf("DEBUG: %s supports LPD!\n", device
->addrname
);
1720 snprintf(uri
, sizeof(uri
), "lpd://%s/", device
->addrname
);
1721 update_cache(device
, uri
, NULL
, NULL
);
1727 * 'read_snmp_conf()' - Read the snmp.conf file.
1731 read_snmp_conf(const char *address
) /* I - Single address to probe */
1733 cups_file_t
*fp
; /* File pointer */
1734 char filename
[1024], /* Filename */
1735 line
[1024], /* Line from file */
1736 *value
; /* Value on line */
1737 int linenum
; /* Line number */
1738 const char *cups_serverroot
; /* CUPS_SERVERROOT env var */
1739 const char *debug
; /* CUPS_DEBUG_LEVEL env var */
1740 const char *runtime
; /* CUPS_MAX_RUN_TIME env var */
1744 * Initialize the global address and community lists...
1747 Addresses
= cupsArrayNew(NULL
, NULL
);
1748 Communities
= cupsArrayNew(NULL
, NULL
);
1751 add_array(Addresses
, address
);
1753 if ((debug
= getenv("CUPS_DEBUG_LEVEL")) != NULL
)
1754 DebugLevel
= atoi(debug
);
1756 if ((runtime
= getenv("CUPS_MAX_RUN_TIME")) != NULL
)
1757 MaxRunTime
= atoi(runtime
);
1760 * Find the snmp.conf file...
1763 if ((cups_serverroot
= getenv("CUPS_SERVERROOT")) == NULL
)
1764 cups_serverroot
= CUPS_SERVERROOT
;
1766 snprintf(filename
, sizeof(filename
), "%s/snmp.conf", cups_serverroot
);
1768 if ((fp
= cupsFileOpen(filename
, "r")) != NULL
)
1771 * Read the snmp.conf file...
1776 while (cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
))
1779 fprintf(stderr
, "ERROR: Missing value on line %d of %s!\n", linenum
,
1781 else if (!strcasecmp(line
, "Address"))
1784 add_array(Addresses
, value
);
1786 else if (!strcasecmp(line
, "Community"))
1787 add_array(Communities
, value
);
1788 else if (!strcasecmp(line
, "DebugLevel"))
1789 DebugLevel
= atoi(value
);
1790 else if (!strcasecmp(line
, "DeviceURI"))
1794 "ERROR: Missing double quote for regular expression on "
1795 "line %d of %s!\n", linenum
, filename
);
1797 add_device_uri(value
);
1799 else if (!strcasecmp(line
, "HostNameLookups"))
1800 HostNameLookups
= !strcasecmp(value
, "on") ||
1801 !strcasecmp(value
, "yes") ||
1802 !strcasecmp(value
, "true") ||
1803 !strcasecmp(value
, "double");
1804 else if (!strcasecmp(line
, "MaxRunTime"))
1805 MaxRunTime
= atoi(value
);
1807 fprintf(stderr
, "ERROR: Unknown directive %s on line %d of %s!\n",
1808 line
, linenum
, filename
);
1815 * Use defaults if parameters are undefined...
1818 if (cupsArrayCount(Addresses
) == 0)
1821 * If we have no addresses, exit immediately...
1825 "DEBUG: No address specified and no Address line in %s...\n",
1830 if (cupsArrayCount(Communities
) == 0)
1832 fputs("INFO: Using default SNMP Community public\n", stderr
);
1833 add_array(Communities
, "public");
1839 * 'read_snmp_response()' - Read and parse a SNMP response...
1843 read_snmp_response(int fd
) /* I - SNMP socket file descriptor */
1845 unsigned char buffer
[SNMP_MAX_PACKET
];/* Data packet */
1846 int bytes
; /* Number of bytes received */
1847 http_addr_t addr
; /* Source address */
1848 socklen_t addrlen
; /* Source address length */
1849 char addrname
[256]; /* Source address name */
1850 snmp_packet_t packet
; /* Decoded packet */
1851 snmp_cache_t key
, /* Search key */
1852 *device
; /* Matching device */
1856 * Read the response data...
1859 addrlen
= sizeof(addr
);
1861 if ((bytes
= recvfrom(fd
, buffer
, sizeof(buffer
), 0, (void *)&addr
,
1864 fprintf(stderr
, "ERROR: Unable to read data from socket: %s\n",
1869 if (HostNameLookups
)
1870 httpAddrLookup(&addr
, addrname
, sizeof(addrname
));
1872 httpAddrString(&addr
, addrname
, sizeof(addrname
));
1874 debug_printf("DEBUG: %.3f Received %d bytes from %s...\n", run_time(),
1878 * Look for the response status code in the SNMP message header...
1881 if (asn1_decode_snmp(buffer
, bytes
, &packet
))
1883 fprintf(stderr
, "ERROR: Bad SNMP packet from %s: %s\n",
1884 addrname
, packet
.error
);
1886 asn1_debug(buffer
, bytes
, 0);
1887 hex_debug(buffer
, bytes
);
1892 debug_printf("DEBUG: community=\"%s\"\n", packet
.community
);
1893 debug_printf("DEBUG: request-id=%d\n", packet
.request_id
);
1894 debug_printf("DEBUG: error-status=%d\n", packet
.error_status
);
1897 asn1_debug(buffer
, bytes
, 0);
1900 hex_debug(buffer
, bytes
);
1902 if (packet
.error_status
)
1906 * Find a matching device in the cache...
1909 key
.addrname
= addrname
;
1910 device
= (snmp_cache_t
*)cupsArrayFind(Devices
, &key
);
1913 * Process the message...
1916 if (packet
.request_id
== DeviceTypeRequest
)
1919 * Got the device type response...
1924 debug_printf("DEBUG: Discarding duplicate device type for \"%s\"...\n",
1930 * Add the device and request the device description...
1933 add_cache(&addr
, addrname
, NULL
, NULL
, NULL
);
1935 send_snmp_query(fd
, &addr
, SNMP_VERSION_1
, packet
.community
,
1936 DeviceDescRequest
, DeviceDescOID
);
1938 else if (packet
.request_id
== DeviceDescRequest
&&
1939 packet
.object_type
== ASN1_OCTET_STRING
)
1942 * Update an existing cache entry...
1945 char make_model
[256]; /* Make and model */
1950 debug_printf("DEBUG: Discarding device description for \"%s\"...\n",
1956 * Convert the description to a make and model string...
1959 if (strchr(packet
.object_value
.string
, ':') &&
1960 strchr(packet
.object_value
.string
, ';'))
1963 * Description is the IEEE-1284 device ID...
1966 backendGetMakeModel(packet
.object_value
.string
, make_model
,
1967 sizeof(make_model
));
1972 * Description is plain text...
1975 fix_make_model(make_model
, packet
.object_value
.string
,
1976 sizeof(make_model
));
1979 if (device
->make_and_model
)
1980 free(device
->make_and_model
);
1982 device
->make_and_model
= strdup(make_model
);
1988 * 'run_time()' - Return the total running time...
1991 static double /* O - Number of seconds */
1994 struct timeval curtime
; /* Current time */
1997 gettimeofday(&curtime
, NULL
);
1999 return (curtime
.tv_sec
- StartTime
.tv_sec
+
2000 0.000001 * (curtime
.tv_usec
- StartTime
.tv_usec
));
2005 * 'scan_devices()' - Scan for devices using SNMP.
2009 scan_devices(int fd
) /* I - SNMP socket */
2011 char *address
, /* Current address */
2012 *community
; /* Current community */
2013 fd_set input
; /* Input set for select() */
2014 struct timeval timeout
; /* Timeout for select() */
2015 time_t endtime
; /* End time for scan */
2016 http_addrlist_t
*addrs
, /* List of addresses */
2017 *addr
; /* Current address */
2018 snmp_cache_t
*device
; /* Current device */
2022 * Setup the request IDs...
2025 gettimeofday(&StartTime
, NULL
);
2027 DeviceTypeRequest
= StartTime
.tv_sec
;
2028 DeviceDescRequest
= StartTime
.tv_sec
+ 1;
2031 * First send all of the broadcast queries...
2034 for (address
= (char *)cupsArrayFirst(Addresses
);
2036 address
= (char *)cupsArrayNext(Addresses
))
2038 if (!strcmp(address
, "@LOCAL"))
2039 addrs
= get_interface_addresses(NULL
);
2040 else if (!strncmp(address
, "@IF(", 4))
2042 char ifname
[255]; /* Interface name */
2045 strlcpy(ifname
, address
+ 4, sizeof(ifname
));
2047 ifname
[strlen(ifname
) - 1] = '\0';
2049 addrs
= get_interface_addresses(ifname
);
2052 addrs
= httpAddrGetList(address
, AF_INET
, NULL
);
2056 fprintf(stderr
, "ERROR: Unable to scan \"%s\"!\n", address
);
2060 for (community
= (char *)cupsArrayFirst(Communities
);
2062 community
= (char *)cupsArrayNext(Communities
))
2064 debug_printf("DEBUG: Scanning for devices in \"%s\" via \"%s\"...\n",
2065 community
, address
);
2067 for (addr
= addrs
; addr
; addr
= addr
->next
)
2068 send_snmp_query(fd
, &(addr
->addr
), SNMP_VERSION_1
, community
,
2069 DeviceTypeRequest
, DeviceTypeOID
);
2072 httpAddrFreeList(addrs
);
2076 * Then read any responses that come in over the next 3 seconds...
2079 endtime
= time(NULL
) + 3;
2083 while (time(NULL
) < endtime
)
2086 timeout
.tv_usec
= 0;
2089 if (select(fd
+ 1, &input
, NULL
, NULL
, &timeout
) < 0)
2091 fprintf(stderr
, "ERROR: %.3f select() for %d failed: %s\n", run_time(),
2092 fd
, strerror(errno
));
2096 if (FD_ISSET(fd
, &input
))
2097 read_snmp_response(fd
);
2103 * Finally, probe all of the printers we discovered to see how they are
2107 for (device
= (snmp_cache_t
*)cupsArrayFirst(Devices
);
2109 device
= (snmp_cache_t
*)cupsArrayNext(Devices
))
2110 if (MaxRunTime
> 0 && run_time() >= MaxRunTime
)
2112 else if (!device
->uri
)
2113 probe_device(device
);
2115 debug_printf("DEBUG: %.3f Scan complete!\n", run_time());
2120 * 'send_snmp_query()' - Send an SNMP query packet.
2125 int fd
, /* I - SNMP socket */
2126 http_addr_t
*addr
, /* I - Address to send to */
2127 int version
, /* I - SNMP version */
2128 const char *community
, /* I - Community name */
2129 const unsigned request_id
, /* I - Request ID */
2130 const int *oid
) /* I - OID */
2132 int i
; /* Looping var */
2133 snmp_packet_t packet
; /* SNMP message packet */
2134 unsigned char buffer
[SNMP_MAX_PACKET
];/* SNMP message buffer */
2135 int bytes
; /* Size of message */
2136 char addrname
[32]; /* Address name */
2140 * Create the SNMP message...
2143 memset(&packet
, 0, sizeof(packet
));
2145 packet
.version
= version
;
2146 packet
.request_type
= ASN1_GET_REQUEST
;
2147 packet
.request_id
= request_id
;
2148 packet
.object_type
= ASN1_NULL_VALUE
;
2150 strlcpy(packet
.community
, community
, sizeof(packet
.community
));
2152 for (i
= 0; oid
[i
]; i
++)
2153 packet
.object_name
[i
] = oid
[i
];
2155 bytes
= asn1_encode_snmp(buffer
, sizeof(buffer
), &packet
);
2159 fprintf(stderr
, "ERROR: Unable to send SNMP query: %s\n",
2165 * Send the message...
2168 debug_printf("DEBUG: %.3f Sending %d bytes to %s...\n", run_time(),
2169 bytes
, httpAddrString(addr
, addrname
, sizeof(addrname
)));
2171 asn1_debug(buffer
, bytes
, 0);
2173 hex_debug(buffer
, bytes
);
2175 addr
->ipv4
.sin_port
= htons(SNMP_PORT
);
2177 if (sendto(fd
, buffer
, bytes
, 0, (void *)addr
, sizeof(addr
->ipv4
)) < bytes
)
2178 fprintf(stderr
, "ERROR: Unable to send %d bytes to %s: %s\n",
2179 bytes
, addrname
, strerror(errno
));
2184 * 'try_connect()' - Try connecting on a port...
2187 static int /* O - 0 on success or -1 on error */
2188 try_connect(http_addr_t
*addr
, /* I - Socket address */
2189 const char *addrname
, /* I - Hostname or IP address */
2190 int port
) /* I - Port number */
2192 int fd
; /* Socket */
2193 int status
; /* Connection status */
2196 debug_printf("DEBUG: %.3f Trying %s://%s:%d...\n", run_time(),
2197 port
== 515 ? "lpd" : "socket", addrname
, port
);
2199 if ((fd
= socket(AF_INET
, SOCK_STREAM
, 0)) < 0)
2201 fprintf(stderr
, "ERROR: Unable to create socket: %s\n",
2206 addr
->ipv4
.sin_port
= htons(port
);
2210 status
= connect(fd
, (void *)addr
, httpAddrLength(addr
));
2220 * 'update_cache()' - Update a cached device...
2224 update_cache(snmp_cache_t
*device
, /* I - Device */
2225 const char *uri
, /* I - Device URI */
2226 const char *id
, /* I - Device ID */
2227 const char *make_model
) /* I - Device make and model */
2232 device
->uri
= strdup(uri
);
2239 device
->id
= strdup(id
);
2244 if (device
->make_and_model
)
2245 free(device
->make_and_model
);
2247 device
->make_and_model
= strdup(make_model
);
2250 list_device(device
);
2255 * End of "$Id: snmp.c 6649 2007-07-11 21:46:42Z mike $".