]>
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 */
1070 * Disallow negative lengths!
1073 fprintf(stderr
, "ERROR: Bad ASN1 string length %d!\n", length
);
1076 else if (length
< strsize
)
1079 * String is smaller than the buffer...
1083 memcpy(string
, *buffer
, length
);
1085 string
[length
] = '\0';
1090 * String is larger than the buffer...
1093 memcpy(string
, buffer
, strsize
- 1);
1094 string
[strsize
- 1] = '\0';
1098 (*buffer
) += length
;
1105 * 'asn1_get_type()' - Get a value type.
1108 static int /* O - Type */
1109 asn1_get_type(unsigned char **buffer
, /* IO - Pointer in buffer */
1110 unsigned char *bufend
) /* I - End of buffer */
1112 int type
; /* Type */
1118 if ((type
& 31) == 31)
1119 type
= asn1_get_packed(buffer
, bufend
);
1126 * 'asn1_set_integer()' - Set an integer value.
1130 asn1_set_integer(unsigned char **buffer
,/* IO - Pointer in buffer */
1131 int integer
) /* I - Integer value */
1133 **buffer
= ASN1_INTEGER
;
1136 if (integer
> 0x7fffff || integer
< -0x800000)
1140 **buffer
= integer
>> 24;
1142 **buffer
= integer
>> 16;
1144 **buffer
= integer
>> 8;
1149 else if (integer
> 0x7fff || integer
< -0x8000)
1153 **buffer
= integer
>> 16;
1155 **buffer
= integer
>> 8;
1160 else if (integer
> 0x7f || integer
< -0x80)
1164 **buffer
= integer
>> 8;
1180 * 'asn1_set_length()' - Set a value length.
1184 asn1_set_length(unsigned char **buffer
, /* IO - Pointer in buffer */
1185 int length
) /* I - Length value */
1189 **buffer
= 0x82; /* 2-byte length */
1191 **buffer
= length
>> 8;
1196 else if (length
> 127)
1198 **buffer
= 0x81; /* 1-byte length */
1205 **buffer
= length
; /* Length */
1212 * 'asn1_set_oid()' - Set an OID value.
1216 asn1_set_oid(unsigned char **buffer
, /* IO - Pointer in buffer */
1217 const int *oid
) /* I - OID value */
1219 **buffer
= ASN1_OID
;
1222 asn1_set_length(buffer
, asn1_size_oid(oid
));
1224 asn1_set_packed(buffer
, oid
[0] * 40 + oid
[1]);
1226 for (oid
+= 2; *oid
; oid
++)
1227 asn1_set_packed(buffer
, *oid
);
1232 * 'asn1_set_packed()' - Set a packed integer value.
1236 asn1_set_packed(unsigned char **buffer
, /* IO - Pointer in buffer */
1237 int integer
) /* I - Integer value */
1239 if (integer
> 0xfffffff)
1241 **buffer
= (integer
>> 28) & 0x7f;
1245 if (integer
> 0x1fffff)
1247 **buffer
= (integer
>> 21) & 0x7f;
1251 if (integer
> 0x3fff)
1253 **buffer
= (integer
>> 14) & 0x7f;
1259 **buffer
= (integer
>> 7) & 0x7f;
1263 **buffer
= integer
& 0x7f;
1268 * 'asn1_size_integer()' - Figure out the number of bytes needed for an
1272 static int /* O - Size in bytes */
1273 asn1_size_integer(int integer
) /* I - Integer value */
1275 if (integer
> 0x7fffff || integer
< -0x800000)
1277 else if (integer
> 0x7fff || integer
< -0x8000)
1279 else if (integer
> 0x7f || integer
< -0x80)
1287 * 'asn1_size_length()' - Figure out the number of bytes needed for a
1291 static int /* O - Size in bytes */
1292 asn1_size_length(int length
) /* I - Length value */
1296 else if (length
> 0x7f)
1304 * 'asn1_size_oid()' - Figure out the numebr of bytes needed for an
1308 static int /* O - Size in bytes */
1309 asn1_size_oid(const int *oid
) /* I - OID value */
1311 int length
; /* Length of value */
1314 for (length
= asn1_size_packed(oid
[0] * 40 + oid
[1]), oid
+= 2; *oid
; oid
++)
1315 length
+= asn1_size_packed(*oid
);
1322 * 'asn1_size_packed()' - Figure out the number of bytes needed for a
1323 * packed integer value.
1326 static int /* O - Size in bytes */
1327 asn1_size_packed(int integer
) /* I - Integer value */
1329 if (integer
> 0xfffffff)
1331 else if (integer
> 0x1fffff)
1333 else if (integer
> 0x3fff)
1335 else if (integer
> 0x7f)
1343 * 'compare_cache()' - Compare two cache entries.
1346 static int /* O - Result of comparison */
1347 compare_cache(snmp_cache_t
*a
, /* I - First cache entry */
1348 snmp_cache_t
*b
) /* I - Second cache entry */
1350 return (strcasecmp(a
->addrname
, b
->addrname
));
1355 * 'debug_printf()' - Display some debugging information.
1359 debug_printf(const char *format
, /* I - Printf-style format string */
1360 ...) /* I - Additional arguments as needed */
1362 va_list ap
; /* Pointer to arguments */
1368 va_start(ap
, format
);
1369 vfprintf(stderr
, format
, ap
);
1375 * 'fix_make_model()' - Fix common problems in the make-and-model string.
1380 char *make_model
, /* I - New make-and-model string */
1381 const char *old_make_model
, /* I - Old make-and-model string */
1382 int make_model_size
) /* I - Size of new string buffer */
1384 char *mmptr
; /* Pointer into make-and-model string */
1388 * Fix some common problems with the make-and-model string so
1389 * that printer driver detection works better...
1392 if (!strncasecmp(old_make_model
, "Hewlett-Packard", 15))
1395 * Strip leading Hewlett-Packard and hp prefixes and replace
1396 * with a single HP manufacturer prefix...
1399 mmptr
= (char *)old_make_model
+ 15;
1401 while (isspace(*mmptr
& 255))
1404 if (!strncasecmp(mmptr
, "hp", 2))
1408 while (isspace(*mmptr
& 255))
1412 make_model
[0] = 'H';
1413 make_model
[1] = 'P';
1414 make_model
[2] = ' ';
1415 strlcpy(make_model
+ 3, mmptr
, make_model_size
- 3);
1417 else if (!strncasecmp(old_make_model
, "deskjet", 7))
1418 snprintf(make_model
, make_model_size
, "HP DeskJet%s", old_make_model
+ 7);
1419 else if (!strncasecmp(old_make_model
, "officejet", 9))
1420 snprintf(make_model
, make_model_size
, "HP OfficeJet%s", old_make_model
+ 9);
1421 else if (!strncasecmp(old_make_model
, "stylus_pro_", 11))
1422 snprintf(make_model
, make_model_size
, "EPSON Stylus Pro %s",
1423 old_make_model
+ 11);
1425 strlcpy(make_model
, old_make_model
, make_model_size
);
1427 if ((mmptr
= strstr(make_model
, ", Inc.,")) != NULL
)
1430 * Strip inc. from name, e.g. "Tektronix, Inc., Phaser 560"
1431 * becomes "Tektronix Phaser 560"...
1434 _cups_strcpy(mmptr
, mmptr
+ 7);
1437 if ((mmptr
= strstr(make_model
, " Network")) != NULL
)
1440 * Drop unnecessary informational text, e.g. "Xerox DocuPrint N2025
1441 * Network LaserJet - 2.12" becomes "Xerox DocuPrint N2025"...
1447 if ((mmptr
= strchr(make_model
, ',')) != NULL
)
1450 * Drop anything after a trailing comma...
1459 * 'free_array()' - Free an array of strings.
1463 free_array(cups_array_t
*a
) /* I - Array */
1465 char *s
; /* Current string */
1468 for (s
= (char *)cupsArrayFirst(a
); s
; s
= (char *)cupsArrayNext(a
))
1476 * 'free_cache()' - Free the array of cached devices.
1482 snmp_cache_t
*cache
; /* Cached device */
1485 for (cache
= (snmp_cache_t
*)cupsArrayFirst(Devices
);
1487 cache
= (snmp_cache_t
*)cupsArrayNext(Devices
))
1489 free(cache
->addrname
);
1497 if (cache
->make_and_model
)
1498 free(cache
->make_and_model
);
1503 cupsArrayDelete(Devices
);
1509 * 'get_interface_addresses()' - Get the broadcast address(es) associated
1510 * with an interface.
1513 static http_addrlist_t
* /* O - List of addresses */
1514 get_interface_addresses(
1515 const char *ifname
) /* I - Interface name */
1517 struct ifaddrs
*addrs
, /* Interface address list */
1518 *addr
; /* Current interface address */
1519 http_addrlist_t
*first
, /* First address in list */
1520 *last
, /* Last address in list */
1521 *current
; /* Current address */
1524 if (getifaddrs(&addrs
) < 0)
1527 for (addr
= addrs
, first
= NULL
, last
= NULL
; addr
; addr
= addr
->ifa_next
)
1528 if ((addr
->ifa_flags
& IFF_BROADCAST
) && addr
->ifa_broadaddr
&&
1529 addr
->ifa_broadaddr
->sa_family
== AF_INET
&&
1530 (!ifname
|| !strcmp(ifname
, addr
->ifa_name
)))
1532 current
= calloc(1, sizeof(http_addrlist_t
));
1534 memcpy(&(current
->addr
), addr
->ifa_broadaddr
,
1535 sizeof(struct sockaddr_in
));
1540 last
->next
= current
;
1552 * 'hex_debug()' - Output hex debugging data...
1556 hex_debug(unsigned char *buffer
, /* I - Buffer */
1557 size_t len
) /* I - Number of bytes */
1559 int col
; /* Current column */
1562 fputs("DEBUG: Hex dump of packet:\n", stderr
);
1564 for (col
= 0; len
> 0; col
++, buffer
++, len
--)
1566 if ((col
& 15) == 0)
1567 fprintf(stderr
, "DEBUG: %04X ", col
);
1569 fprintf(stderr
, " %02X", *buffer
);
1571 if ((col
& 15) == 15)
1581 * 'list_device()' - List a device we found...
1585 list_device(snmp_cache_t
*cache
) /* I - Cached device */
1588 printf("network %s \"%s\" \"%s %s\" \"%s\"\n",
1590 cache
->make_and_model
? cache
->make_and_model
: "Unknown",
1591 cache
->make_and_model
? cache
->make_and_model
: "Unknown",
1592 cache
->addrname
, cache
->id
? cache
->id
: "");
1597 * 'open_snmp_socket()' - Open the SNMP broadcast socket.
1600 static int /* O - SNMP socket file descriptor */
1601 open_snmp_socket(void)
1603 int fd
; /* SNMP socket file descriptor */
1604 int val
; /* Socket option value */
1608 * Create the SNMP socket...
1611 if ((fd
= socket(AF_INET
, SOCK_DGRAM
, 0)) < 0)
1613 fprintf(stderr
, "ERROR: Unable to create SNMP socket - %s\n",
1620 * Set the "broadcast" flag...
1625 if (setsockopt(fd
, SOL_SOCKET
, SO_BROADCAST
, &val
, sizeof(val
)))
1627 fprintf(stderr
, "ERROR: Unable to set broadcast mode - %s\n",
1640 * 'password_cb()' - Handle authentication requests.
1642 * All we do right now is return NULL, indicating that no authentication
1646 static const char * /* O - Password (NULL) */
1647 password_cb(const char *prompt
) /* I - Prompt message */
1649 (void)prompt
; /* Anti-compiler-warning-code */
1656 * 'probe_device()' - Probe a device to discover whether it is a printer.
1658 * TODO: Try using the Port Monitor MIB to discover the correct protocol
1659 * to use - first need a commercially-available printer that supports
1664 probe_device(snmp_cache_t
*device
) /* I - Device */
1666 char uri
[1024], /* Full device URI */
1667 *uriptr
, /* Pointer into URI */
1668 *format
; /* Format string for device */
1669 device_uri_t
*device_uri
; /* Current DeviceURI match */
1672 debug_printf("DEBUG: %.3f Probing %s...\n", run_time(), device
->addrname
);
1676 * TODO: Try an mDNS query first, and then fallback on direct probes...
1679 if (!try_connect(&(device
->address
), device
->addrname
, 5353))
1681 debug_printf("DEBUG: %s supports mDNS, not reporting!\n", device
->addrname
);
1684 #endif /* __APPLE__ */
1687 * Lookup the device in the match table...
1690 for (device_uri
= (device_uri_t
*)cupsArrayFirst(DeviceURIs
);
1692 device_uri
= (device_uri_t
*)cupsArrayNext(DeviceURIs
))
1693 if (!regexec(&(device_uri
->re
), device
->make_and_model
, 0, NULL
, 0))
1696 * Found a match, add the URIs...
1699 for (format
= (char *)cupsArrayFirst(device_uri
->uris
);
1701 format
= (char *)cupsArrayNext(device_uri
->uris
))
1703 for (uriptr
= uri
; *format
&& uriptr
< (uri
+ sizeof(uri
) - 1);)
1704 if (*format
== '%' && format
[1] == 's')
1707 * Insert hostname/address...
1710 strlcpy(uriptr
, device
->addrname
, sizeof(uri
) - (uriptr
- uri
));
1711 uriptr
+= strlen(uriptr
);
1715 *uriptr
++ = *format
++;
1719 update_cache(device
, uri
, NULL
, NULL
);
1726 * Then try the standard ports...
1729 if (!try_connect(&(device
->address
), device
->addrname
, 9100))
1731 debug_printf("DEBUG: %s supports AppSocket!\n", device
->addrname
);
1733 snprintf(uri
, sizeof(uri
), "socket://%s", device
->addrname
);
1734 update_cache(device
, uri
, NULL
, NULL
);
1736 else if (!try_connect(&(device
->address
), device
->addrname
, 515))
1738 debug_printf("DEBUG: %s supports LPD!\n", device
->addrname
);
1740 snprintf(uri
, sizeof(uri
), "lpd://%s/", device
->addrname
);
1741 update_cache(device
, uri
, NULL
, NULL
);
1747 * 'read_snmp_conf()' - Read the snmp.conf file.
1751 read_snmp_conf(const char *address
) /* I - Single address to probe */
1753 cups_file_t
*fp
; /* File pointer */
1754 char filename
[1024], /* Filename */
1755 line
[1024], /* Line from file */
1756 *value
; /* Value on line */
1757 int linenum
; /* Line number */
1758 const char *cups_serverroot
; /* CUPS_SERVERROOT env var */
1759 const char *debug
; /* CUPS_DEBUG_LEVEL env var */
1760 const char *runtime
; /* CUPS_MAX_RUN_TIME env var */
1764 * Initialize the global address and community lists...
1767 Addresses
= cupsArrayNew(NULL
, NULL
);
1768 Communities
= cupsArrayNew(NULL
, NULL
);
1771 add_array(Addresses
, address
);
1773 if ((debug
= getenv("CUPS_DEBUG_LEVEL")) != NULL
)
1774 DebugLevel
= atoi(debug
);
1776 if ((runtime
= getenv("CUPS_MAX_RUN_TIME")) != NULL
)
1777 MaxRunTime
= atoi(runtime
);
1780 * Find the snmp.conf file...
1783 if ((cups_serverroot
= getenv("CUPS_SERVERROOT")) == NULL
)
1784 cups_serverroot
= CUPS_SERVERROOT
;
1786 snprintf(filename
, sizeof(filename
), "%s/snmp.conf", cups_serverroot
);
1788 if ((fp
= cupsFileOpen(filename
, "r")) != NULL
)
1791 * Read the snmp.conf file...
1796 while (cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
))
1799 fprintf(stderr
, "ERROR: Missing value on line %d of %s!\n", linenum
,
1801 else if (!strcasecmp(line
, "Address"))
1804 add_array(Addresses
, value
);
1806 else if (!strcasecmp(line
, "Community"))
1807 add_array(Communities
, value
);
1808 else if (!strcasecmp(line
, "DebugLevel"))
1809 DebugLevel
= atoi(value
);
1810 else if (!strcasecmp(line
, "DeviceURI"))
1814 "ERROR: Missing double quote for regular expression on "
1815 "line %d of %s!\n", linenum
, filename
);
1817 add_device_uri(value
);
1819 else if (!strcasecmp(line
, "HostNameLookups"))
1820 HostNameLookups
= !strcasecmp(value
, "on") ||
1821 !strcasecmp(value
, "yes") ||
1822 !strcasecmp(value
, "true") ||
1823 !strcasecmp(value
, "double");
1824 else if (!strcasecmp(line
, "MaxRunTime"))
1825 MaxRunTime
= atoi(value
);
1827 fprintf(stderr
, "ERROR: Unknown directive %s on line %d of %s!\n",
1828 line
, linenum
, filename
);
1835 * Use defaults if parameters are undefined...
1838 if (cupsArrayCount(Addresses
) == 0)
1841 * If we have no addresses, exit immediately...
1845 "DEBUG: No address specified and no Address line in %s...\n",
1850 if (cupsArrayCount(Communities
) == 0)
1852 fputs("INFO: Using default SNMP Community public\n", stderr
);
1853 add_array(Communities
, "public");
1859 * 'read_snmp_response()' - Read and parse a SNMP response...
1863 read_snmp_response(int fd
) /* I - SNMP socket file descriptor */
1865 unsigned char buffer
[SNMP_MAX_PACKET
];/* Data packet */
1866 int bytes
; /* Number of bytes received */
1867 http_addr_t addr
; /* Source address */
1868 socklen_t addrlen
; /* Source address length */
1869 char addrname
[256]; /* Source address name */
1870 snmp_packet_t packet
; /* Decoded packet */
1871 snmp_cache_t key
, /* Search key */
1872 *device
; /* Matching device */
1876 * Read the response data...
1879 addrlen
= sizeof(addr
);
1881 if ((bytes
= recvfrom(fd
, buffer
, sizeof(buffer
), 0, (void *)&addr
,
1884 fprintf(stderr
, "ERROR: Unable to read data from socket: %s\n",
1889 if (HostNameLookups
)
1890 httpAddrLookup(&addr
, addrname
, sizeof(addrname
));
1892 httpAddrString(&addr
, addrname
, sizeof(addrname
));
1894 debug_printf("DEBUG: %.3f Received %d bytes from %s...\n", run_time(),
1898 * Look for the response status code in the SNMP message header...
1901 if (asn1_decode_snmp(buffer
, bytes
, &packet
))
1903 fprintf(stderr
, "ERROR: Bad SNMP packet from %s: %s\n",
1904 addrname
, packet
.error
);
1906 asn1_debug(buffer
, bytes
, 0);
1907 hex_debug(buffer
, bytes
);
1912 debug_printf("DEBUG: community=\"%s\"\n", packet
.community
);
1913 debug_printf("DEBUG: request-id=%d\n", packet
.request_id
);
1914 debug_printf("DEBUG: error-status=%d\n", packet
.error_status
);
1917 asn1_debug(buffer
, bytes
, 0);
1920 hex_debug(buffer
, bytes
);
1922 if (packet
.error_status
)
1926 * Find a matching device in the cache...
1929 key
.addrname
= addrname
;
1930 device
= (snmp_cache_t
*)cupsArrayFind(Devices
, &key
);
1933 * Process the message...
1936 if (packet
.request_id
== DeviceTypeRequest
)
1939 * Got the device type response...
1944 debug_printf("DEBUG: Discarding duplicate device type for \"%s\"...\n",
1950 * Add the device and request the device description...
1953 add_cache(&addr
, addrname
, NULL
, NULL
, NULL
);
1955 send_snmp_query(fd
, &addr
, SNMP_VERSION_1
, packet
.community
,
1956 DeviceDescRequest
, DeviceDescOID
);
1958 else if (packet
.request_id
== DeviceDescRequest
&&
1959 packet
.object_type
== ASN1_OCTET_STRING
)
1962 * Update an existing cache entry...
1965 char make_model
[256]; /* Make and model */
1970 debug_printf("DEBUG: Discarding device description for \"%s\"...\n",
1976 * Convert the description to a make and model string...
1979 if (strchr(packet
.object_value
.string
, ':') &&
1980 strchr(packet
.object_value
.string
, ';'))
1983 * Description is the IEEE-1284 device ID...
1986 backendGetMakeModel(packet
.object_value
.string
, make_model
,
1987 sizeof(make_model
));
1992 * Description is plain text...
1995 fix_make_model(make_model
, packet
.object_value
.string
,
1996 sizeof(make_model
));
1999 if (device
->make_and_model
)
2000 free(device
->make_and_model
);
2002 device
->make_and_model
= strdup(make_model
);
2008 * 'run_time()' - Return the total running time...
2011 static double /* O - Number of seconds */
2014 struct timeval curtime
; /* Current time */
2017 gettimeofday(&curtime
, NULL
);
2019 return (curtime
.tv_sec
- StartTime
.tv_sec
+
2020 0.000001 * (curtime
.tv_usec
- StartTime
.tv_usec
));
2025 * 'scan_devices()' - Scan for devices using SNMP.
2029 scan_devices(int fd
) /* I - SNMP socket */
2031 char *address
, /* Current address */
2032 *community
; /* Current community */
2033 fd_set input
; /* Input set for select() */
2034 struct timeval timeout
; /* Timeout for select() */
2035 time_t endtime
; /* End time for scan */
2036 http_addrlist_t
*addrs
, /* List of addresses */
2037 *addr
; /* Current address */
2038 snmp_cache_t
*device
; /* Current device */
2042 * Setup the request IDs...
2045 gettimeofday(&StartTime
, NULL
);
2047 DeviceTypeRequest
= StartTime
.tv_sec
;
2048 DeviceDescRequest
= StartTime
.tv_sec
+ 1;
2051 * First send all of the broadcast queries...
2054 for (address
= (char *)cupsArrayFirst(Addresses
);
2056 address
= (char *)cupsArrayNext(Addresses
))
2058 if (!strcmp(address
, "@LOCAL"))
2059 addrs
= get_interface_addresses(NULL
);
2060 else if (!strncmp(address
, "@IF(", 4))
2062 char ifname
[255]; /* Interface name */
2065 strlcpy(ifname
, address
+ 4, sizeof(ifname
));
2067 ifname
[strlen(ifname
) - 1] = '\0';
2069 addrs
= get_interface_addresses(ifname
);
2072 addrs
= httpAddrGetList(address
, AF_INET
, NULL
);
2076 fprintf(stderr
, "ERROR: Unable to scan \"%s\"!\n", address
);
2080 for (community
= (char *)cupsArrayFirst(Communities
);
2082 community
= (char *)cupsArrayNext(Communities
))
2084 debug_printf("DEBUG: Scanning for devices in \"%s\" via \"%s\"...\n",
2085 community
, address
);
2087 for (addr
= addrs
; addr
; addr
= addr
->next
)
2088 send_snmp_query(fd
, &(addr
->addr
), SNMP_VERSION_1
, community
,
2089 DeviceTypeRequest
, DeviceTypeOID
);
2092 httpAddrFreeList(addrs
);
2096 * Then read any responses that come in over the next 3 seconds...
2099 endtime
= time(NULL
) + 3;
2103 while (time(NULL
) < endtime
)
2106 timeout
.tv_usec
= 0;
2109 if (select(fd
+ 1, &input
, NULL
, NULL
, &timeout
) < 0)
2111 fprintf(stderr
, "ERROR: %.3f select() for %d failed: %s\n", run_time(),
2112 fd
, strerror(errno
));
2116 if (FD_ISSET(fd
, &input
))
2117 read_snmp_response(fd
);
2123 * Finally, probe all of the printers we discovered to see how they are
2127 for (device
= (snmp_cache_t
*)cupsArrayFirst(Devices
);
2129 device
= (snmp_cache_t
*)cupsArrayNext(Devices
))
2130 if (MaxRunTime
> 0 && run_time() >= MaxRunTime
)
2132 else if (!device
->uri
)
2133 probe_device(device
);
2135 debug_printf("DEBUG: %.3f Scan complete!\n", run_time());
2140 * 'send_snmp_query()' - Send an SNMP query packet.
2145 int fd
, /* I - SNMP socket */
2146 http_addr_t
*addr
, /* I - Address to send to */
2147 int version
, /* I - SNMP version */
2148 const char *community
, /* I - Community name */
2149 const unsigned request_id
, /* I - Request ID */
2150 const int *oid
) /* I - OID */
2152 int i
; /* Looping var */
2153 snmp_packet_t packet
; /* SNMP message packet */
2154 unsigned char buffer
[SNMP_MAX_PACKET
];/* SNMP message buffer */
2155 int bytes
; /* Size of message */
2156 char addrname
[32]; /* Address name */
2160 * Create the SNMP message...
2163 memset(&packet
, 0, sizeof(packet
));
2165 packet
.version
= version
;
2166 packet
.request_type
= ASN1_GET_REQUEST
;
2167 packet
.request_id
= request_id
;
2168 packet
.object_type
= ASN1_NULL_VALUE
;
2170 strlcpy(packet
.community
, community
, sizeof(packet
.community
));
2172 for (i
= 0; oid
[i
]; i
++)
2173 packet
.object_name
[i
] = oid
[i
];
2175 bytes
= asn1_encode_snmp(buffer
, sizeof(buffer
), &packet
);
2179 fprintf(stderr
, "ERROR: Unable to send SNMP query: %s\n",
2185 * Send the message...
2188 debug_printf("DEBUG: %.3f Sending %d bytes to %s...\n", run_time(),
2189 bytes
, httpAddrString(addr
, addrname
, sizeof(addrname
)));
2191 asn1_debug(buffer
, bytes
, 0);
2193 hex_debug(buffer
, bytes
);
2195 addr
->ipv4
.sin_port
= htons(SNMP_PORT
);
2197 if (sendto(fd
, buffer
, bytes
, 0, (void *)addr
, sizeof(addr
->ipv4
)) < bytes
)
2198 fprintf(stderr
, "ERROR: Unable to send %d bytes to %s: %s\n",
2199 bytes
, addrname
, strerror(errno
));
2204 * 'try_connect()' - Try connecting on a port...
2207 static int /* O - 0 on success or -1 on error */
2208 try_connect(http_addr_t
*addr
, /* I - Socket address */
2209 const char *addrname
, /* I - Hostname or IP address */
2210 int port
) /* I - Port number */
2212 int fd
; /* Socket */
2213 int status
; /* Connection status */
2216 debug_printf("DEBUG: %.3f Trying %s://%s:%d...\n", run_time(),
2217 port
== 515 ? "lpd" : "socket", addrname
, port
);
2219 if ((fd
= socket(AF_INET
, SOCK_STREAM
, 0)) < 0)
2221 fprintf(stderr
, "ERROR: Unable to create socket: %s\n",
2226 addr
->ipv4
.sin_port
= htons(port
);
2230 status
= connect(fd
, (void *)addr
, httpAddrLength(addr
));
2240 * 'update_cache()' - Update a cached device...
2244 update_cache(snmp_cache_t
*device
, /* I - Device */
2245 const char *uri
, /* I - Device URI */
2246 const char *id
, /* I - Device ID */
2247 const char *make_model
) /* I - Device make and model */
2252 device
->uri
= strdup(uri
);
2259 device
->id
= strdup(id
);
2264 if (device
->make_and_model
)
2265 free(device
->make_and_model
);
2267 device
->make_and_model
= strdup(make_model
);
2270 list_device(device
);
2275 * End of "$Id: snmp.c 6649 2007-07-11 21:46:42Z mike $".