2 * "$Id: snmp.c 181 2006-06-22 20:01:18Z jlovell $"
4 * SNMP discovery backend for the Common UNIX Printing System (CUPS).
6 * Copyright 2006 by Easy Software Products, all rights reserved.
8 * These coded instructions, statements, and computer programs are the
9 * property of Easy Software Products and are protected by Federal
10 * copyright law. Distribution and use rights are outlined in the file
11 * "LICENSE" which should have been included with this file. If this
12 * file is missing or damaged please contact Easy Software Products
15 * Attn: CUPS Licensing Information
16 * Easy Software Products
17 * 44141 Airport View Drive, Suite 204
18 * Hollywood, Maryland 20636 USA
20 * Voice: (301) 373-9600
21 * EMail: cups-info@cups.org
22 * WWW: http://www.cups.org
24 * This file is subject to the Apple OS-Developed Software exception.
28 * main() - Discover printers via SNMP.
29 * add_array() - Add a string to an array.
30 * add_cache() - Add a cached device...
31 * alarm_handler() - Handle alarm signals...
32 * asn1_decode_snmp() - Decode a SNMP packet.
33 * asn1_debug() - Decode an ASN1-encoded message.
34 * asn1_encode_snmp() - Encode a SNMP packet.
35 * asn1_get_integer() - Get an integer value.
36 * asn1_get_length() - Get a value length.
37 * asn1_get_oid() - Get an OID value.
38 * asn1_get_packed() - Get a packed integer value.
39 * asn1_get_string() - Get a string value.
40 * asn1_get_type() - Get a value type.
41 * asn1_set_integer() - Set an integer value.
42 * asn1_set_length() - Set a value length.
43 * asn1_set_oid() - Set an OID value.
44 * asn1_set_packed() - Set a packed integer value.
45 * asn1_size_integer() - Figure out the number of bytes needed for an
47 * asn1_size_length() - Figure out the number of bytes needed for a
49 * asn1_size_oid() - Figure out the numebr of bytes needed for an
51 * asn1_size_packed() - Figure out the number of bytes needed for a
52 * packed integer value.
53 * compare_cache() - Compare two cache entries.
54 * debug_printf() - Display some debugging information.
55 * fix_make_model() - Fix common problems in the make-and-model
57 * free_array() - Free an array of strings.
58 * free_cache() - Free the array of cached devices.
59 * get_interface_addresses() - Get the broadcast address(es) associated
61 * hex_debug() - Output hex debugging data...
62 * list_devices() - List all of the devices we found...
63 * open_snmp_socket() - Open the SNMP broadcast socket.
64 * password_cb() - Handle authentication requests.
65 * probe_device() - Probe a device to discover whether it is a
67 * read_snmp_conf() - Read the snmp.conf file.
68 * read_snmp_response() - Read and parse a SNMP response...
69 * run_time() - Return the total running time...
70 * scan_devices() - Scan for devices using SNMP.
71 * send_snmp_query() - Send an SNMP query packet.
72 * try_connect() - Try connecting on a port...
73 * update_cache() - Update a cached device...
77 * Include necessary headers.
80 #include <cups/http-private.h>
81 #include "backend-private.h"
82 #include <cups/array.h>
83 #include <cups/file.h>
87 * This backend implements SNMP printer discovery. It uses a broadcast-
88 * based approach to get SNMP response packets from potential printers
89 * and then interrogates each responder by trying to connect on port
92 * The current focus is on printers with internal network cards, although
93 * the code also works with many external print servers as well. Future
94 * versions will support scanning for vendor-specific SNMP OIDs and the
95 * new PWG Port Monitor MIB and not just the Host MIB OIDs.
97 * The backend reads the snmp.conf file from the CUPS_SERVERROOT directory
98 * which can contain comments, blank lines, or any number of the following
107 * HostNameLookups off
109 * The default is to use:
114 * HostNameLookups off
116 * This backend is known to work with the following network printers and
119 * Axis OfficeBasic, 5400, 5600
128 * It does not currently work with:
135 * (for all of these, they do not support the Host MIB)
142 #define SNMP_PORT 161 /* SNMP well-known port */
143 #define SNMP_MAX_OID 64 /* Maximum number of OID numbers */
144 #define SNMP_MAX_PACKET 1472 /* Maximum size of SNMP packet */
145 #define SNMP_MAX_STRING 512 /* Maximum size of string */
146 #define SNMP_VERSION_1 0 /* SNMPv1 */
148 #define ASN1_END_OF_CONTENTS 0x00 /* End-of-contents */
149 #define ASN1_BOOLEAN 0x01 /* BOOLEAN */
150 #define ASN1_INTEGER 0x02 /* INTEGER or ENUMERATION */
151 #define ASN1_BIT_STRING 0x03 /* BIT STRING */
152 #define ASN1_OCTET_STRING 0x04 /* OCTET STRING */
153 #define ASN1_NULL_VALUE 0x05 /* NULL VALUE */
154 #define ASN1_OID 0x06 /* OBJECT IDENTIFIER */
155 #define ASN1_SEQUENCE 0x30 /* SEQUENCE */
156 #define ASN1_GET_REQUEST 0xa0 /* Get-Request-PDU */
157 #define ASN1_GET_RESPONSE 0xa2 /* Get-Response-PDU */
164 typedef struct snmp_cache_s
/**** SNMP scan cache ****/
166 http_addr_t address
; /* Address of device */
167 char *addrname
, /* Name of device */
168 *uri
, /* device-uri */
170 *make_and_model
; /* device-make-and-model */
173 typedef struct snmp_packet_s
/**** SNMP packet ****/
175 const char *error
; /* Encode/decode error */
176 int version
; /* Version number */
177 char community
[SNMP_MAX_STRING
];
179 int request_type
; /* Request type */
180 int request_id
; /* request-id value */
181 int error_status
; /* error-status value */
182 int error_index
; /* error-index value */
183 int object_name
[SNMP_MAX_OID
];
184 /* object-name value */
185 int object_type
; /* object-value type */
188 int boolean
; /* Boolean value */
189 int integer
; /* Integer value */
190 int oid
[SNMP_MAX_OID
]; /* OID value */
191 char string
[SNMP_MAX_STRING
];/* String value */
192 } object_value
; /* object-value value */
200 static char *add_array(cups_array_t
*a
, const char *s
);
201 static void add_cache(http_addr_t
*addr
, const char *addrname
,
202 const char *uri
, const char *id
,
203 const char *make_and_model
);
204 static void alarm_handler(int sig
);
205 static int asn1_decode_snmp(unsigned char *buffer
, size_t len
,
206 snmp_packet_t
*packet
);
207 static void asn1_debug(unsigned char *buffer
, size_t len
,
209 static int asn1_encode_snmp(unsigned char *buffer
, size_t len
,
210 snmp_packet_t
*packet
);
211 static int asn1_get_integer(unsigned char **buffer
,
212 unsigned char *bufend
,
214 static int asn1_get_oid(unsigned char **buffer
,
215 unsigned char *bufend
,
216 int length
, int *oid
, int oidsize
);
217 static int asn1_get_packed(unsigned char **buffer
,
218 unsigned char *bufend
);
219 static char *asn1_get_string(unsigned char **buffer
,
220 unsigned char *bufend
,
221 int length
, char *string
,
223 static int asn1_get_length(unsigned char **buffer
,
224 unsigned char *bufend
);
225 static int asn1_get_type(unsigned char **buffer
,
226 unsigned char *bufend
);
227 static void asn1_set_integer(unsigned char **buffer
,
229 static void asn1_set_length(unsigned char **buffer
,
231 static void asn1_set_oid(unsigned char **buffer
,
233 static void asn1_set_packed(unsigned char **buffer
,
235 static int asn1_size_integer(int integer
);
236 static int asn1_size_length(int length
);
237 static int asn1_size_oid(const int *oid
);
238 static int asn1_size_packed(int integer
);
239 static int compare_cache(snmp_cache_t
*a
, snmp_cache_t
*b
);
240 static void debug_printf(const char *format
, ...);
241 static void fix_make_model(char *make_model
,
242 const char *old_make_model
,
243 int make_model_size
);
244 static void free_array(cups_array_t
*a
);
245 static void free_cache(void);
246 static http_addrlist_t
*get_interface_addresses(const char *ifname
);
247 static void hex_debug(unsigned char *buffer
, size_t len
);
248 static void list_devices(void);
249 static int open_snmp_socket(void);
250 static const char *password_cb(const char *prompt
);
251 static void probe_device(snmp_cache_t
*device
);
252 static void read_snmp_conf(const char *address
);
253 static void read_snmp_response(int fd
);
254 static double run_time(void);
255 static void scan_devices(int fd
);
256 static void send_snmp_query(int fd
, http_addr_t
*addr
, int version
,
257 const char *community
,
258 const unsigned request_id
,
260 static int try_connect(http_addr_t
*addr
, const char *addrname
,
262 static void update_cache(snmp_cache_t
*device
, const char *uri
,
263 const char *id
, const char *make_model
);
270 static cups_array_t
*Addresses
= NULL
;
271 static cups_array_t
*Communities
= NULL
;
272 static cups_array_t
*Devices
= NULL
;
273 static int DebugLevel
= 0;
274 static int DeviceTypeOID
[] = { 1, 3, 6, 1, 2, 1, 25, 3,
276 static int DeviceDescOID
[] = { 1, 3, 6, 1, 2, 1, 25, 3,
278 static unsigned DeviceTypeRequest
;
279 static unsigned DeviceDescRequest
;
280 static int HostNameLookups
= 0;
281 static struct timeval StartTime
;
285 * 'main()' - Discover printers via SNMP.
288 int /* O - Exit status */
289 main(int argc
, /* I - Number of command-line arguments (6 or 7) */
290 char *argv
[]) /* I - Command-line arguments */
292 int fd
; /* SNMP socket */
296 * Check command-line options...
301 fputs("Usage: snmp [host-or-ip-address]\n", stderr
);
306 * Set the password callback for IPP operations...
309 cupsSetPasswordCB(password_cb
);
312 * Open the SNMP socket...
315 if ((fd
= open_snmp_socket()) < 0)
319 * Read the configuration file and any cache data...
322 read_snmp_conf(argv
[1]);
324 Devices
= cupsArrayNew((cups_array_func_t
)compare_cache
, NULL
);
327 * Scan for devices...
333 * Display the results...
339 * Close, free, and return with no errors...
344 free_array(Addresses
);
345 free_array(Communities
);
353 * 'add_array()' - Add a string to an array.
356 static char * /* O - New string */
357 add_array(cups_array_t
*a
, /* I - Array */
358 const char *s
) /* I - String to add */
360 char *dups
; /* New string */
365 cupsArrayAdd(a
, dups
);
372 * 'add_cache()' - Add a cached device...
376 add_cache(http_addr_t
*addr
, /* I - Device IP address */
377 const char *addrname
, /* I - IP address or name string */
378 const char *uri
, /* I - Device URI */
379 const char *id
, /* I - 1284 device ID */
380 const char *make_and_model
) /* I - Make and model */
382 snmp_cache_t
*temp
; /* New device entry */
385 debug_printf("DEBUG: add_cache(addr=%p, addrname=\"%s\", uri=\"%s\", "
386 "id=\"%s\", make_and_model=\"%s\")\n",
387 addr
, addrname
, uri
, id
? id
: "(null)",
388 make_and_model
? make_and_model
: "(null)");
390 temp
= calloc(1, sizeof(snmp_cache_t
));
391 memcpy(&(temp
->address
), addr
, sizeof(temp
->address
));
393 temp
->addrname
= strdup(addrname
);
396 temp
->uri
= strdup(uri
);
399 temp
->id
= strdup(id
);
402 temp
->make_and_model
= strdup(make_and_model
);
404 cupsArrayAdd(Devices
, temp
);
409 * 'alarm_handler()' - Handle alarm signals...
413 alarm_handler(int sig
) /* I - Signal number */
422 write(2, "DEBUG: ALARM!\n", 14);
427 * 'asn1_decode_snmp()' - Decode a SNMP packet.
430 static int /* O - 0 on success, -1 on error */
431 asn1_decode_snmp(unsigned char *buffer
, /* I - Buffer */
432 size_t len
, /* I - Size of buffer */
433 snmp_packet_t
*packet
) /* I - SNMP packet */
435 unsigned char *bufptr
, /* Pointer into the data */
436 *bufend
; /* End of data */
437 int length
; /* Length of value */
441 * Initialize the decoding...
444 memset(packet
, 0, sizeof(snmp_packet_t
));
447 bufend
= buffer
+ len
;
449 if (asn1_get_type(&bufptr
, bufend
) != ASN1_SEQUENCE
)
450 packet
->error
= "Packet does not start with SEQUENCE";
451 else if (asn1_get_length(&bufptr
, bufend
) == 0)
452 packet
->error
= "SEQUENCE uses indefinite length";
453 else if (asn1_get_type(&bufptr
, bufend
) != ASN1_INTEGER
)
454 packet
->error
= "No version number";
455 else if ((length
= asn1_get_length(&bufptr
, bufend
)) == 0)
456 packet
->error
= "Version uses indefinite length";
457 else if ((packet
->version
= asn1_get_integer(&bufptr
, bufend
, length
))
459 packet
->error
= "Bad SNMP version number";
460 else if (asn1_get_type(&bufptr
, bufend
) != ASN1_OCTET_STRING
)
461 packet
->error
= "No community name";
462 else if ((length
= asn1_get_length(&bufptr
, bufend
)) == 0)
463 packet
->error
= "Community name uses indefinite length";
466 asn1_get_string(&bufptr
, bufend
, length
, packet
->community
,
467 sizeof(packet
->community
));
469 if ((packet
->request_type
= asn1_get_type(&bufptr
, bufend
))
470 != ASN1_GET_RESPONSE
)
471 packet
->error
= "Packet does not contain a Get-Response-PDU";
472 else if (asn1_get_length(&bufptr
, bufend
) == 0)
473 packet
->error
= "Get-Response-PDU uses indefinite length";
474 else if (asn1_get_type(&bufptr
, bufend
) != ASN1_INTEGER
)
475 packet
->error
= "No request-id";
476 else if ((length
= asn1_get_length(&bufptr
, bufend
)) == 0)
477 packet
->error
= "request-id uses indefinite length";
480 packet
->request_id
= asn1_get_integer(&bufptr
, bufend
, length
);
482 if (asn1_get_type(&bufptr
, bufend
) != ASN1_INTEGER
)
483 packet
->error
= "No error-status";
484 else if ((length
= asn1_get_length(&bufptr
, bufend
)) == 0)
485 packet
->error
= "error-status uses indefinite length";
488 packet
->error_status
= asn1_get_integer(&bufptr
, bufend
, length
);
490 if (asn1_get_type(&bufptr
, bufend
) != ASN1_INTEGER
)
491 packet
->error
= "No error-index";
492 else if ((length
= asn1_get_length(&bufptr
, bufend
)) == 0)
493 packet
->error
= "error-index uses indefinite length";
496 packet
->error_index
= asn1_get_integer(&bufptr
, bufend
, length
);
498 if (asn1_get_type(&bufptr
, bufend
) != ASN1_SEQUENCE
)
499 packet
->error
= "No variable-bindings SEQUENCE";
500 else if (asn1_get_length(&bufptr
, bufend
) == 0)
501 packet
->error
= "variable-bindings uses indefinite length";
502 else if (asn1_get_type(&bufptr
, bufend
) != ASN1_SEQUENCE
)
503 packet
->error
= "No VarBind SEQUENCE";
504 else if (asn1_get_length(&bufptr
, bufend
) == 0)
505 packet
->error
= "VarBind uses indefinite length";
506 else if (asn1_get_type(&bufptr
, bufend
) != ASN1_OID
)
507 packet
->error
= "No name OID";
508 else if ((length
= asn1_get_length(&bufptr
, bufend
)) == 0)
509 packet
->error
= "Name OID uses indefinite length";
512 asn1_get_oid(&bufptr
, bufend
, length
, packet
->object_name
,
515 packet
->object_type
= asn1_get_type(&bufptr
, bufend
);
517 if ((length
= asn1_get_length(&bufptr
, bufend
)) == 0 &&
518 packet
->object_type
!= ASN1_NULL_VALUE
&&
519 packet
->object_type
!= ASN1_OCTET_STRING
)
520 packet
->error
= "Value uses indefinite length";
523 switch (packet
->object_type
)
526 packet
->object_value
.boolean
=
527 asn1_get_integer(&bufptr
, bufend
, length
);
531 packet
->object_value
.integer
=
532 asn1_get_integer(&bufptr
, bufend
, length
);
535 case ASN1_NULL_VALUE
:
538 case ASN1_OCTET_STRING
:
539 asn1_get_string(&bufptr
, bufend
, length
,
540 packet
->object_value
.string
,
545 asn1_get_oid(&bufptr
, bufend
, length
,
546 packet
->object_value
.oid
, SNMP_MAX_OID
);
550 packet
->error
= "Unsupported value type";
560 return (packet
->error
? -1 : 0);
565 * 'asn1_debug()' - Decode an ASN1-encoded message.
569 asn1_debug(unsigned char *buffer
, /* I - Buffer */
570 size_t len
, /* I - Length of buffer */
571 int indent
) /* I - Indentation */
573 int i
; /* Looping var */
574 unsigned char *bufend
; /* End of buffer */
575 int integer
; /* Number value */
576 int oid
[SNMP_MAX_OID
]; /* OID value */
577 char string
[SNMP_MAX_STRING
];/* String value */
578 unsigned char value_type
; /* Type of value */
579 int value_length
; /* Length of value */
582 bufend
= buffer
+ len
;
584 while (buffer
< bufend
)
590 value_type
= asn1_get_type(&buffer
, bufend
);
591 value_length
= asn1_get_length(&buffer
, bufend
);
596 integer
= asn1_get_integer(&buffer
, bufend
, value_length
);
598 fprintf(stderr
, "DEBUG: %*sBOOLEAN %d bytes %d\n", indent
, "",
599 value_length
, integer
);
603 integer
= asn1_get_integer(&buffer
, bufend
, value_length
);
605 fprintf(stderr
, "DEBUG: %*sINTEGER %d bytes %d\n", indent
, "",
606 value_length
, integer
);
609 case ASN1_OCTET_STRING
:
610 fprintf(stderr
, "DEBUG: %*sOCTET STRING %d bytes \"%s\"\n", indent
, "",
611 value_length
, asn1_get_string(&buffer
, bufend
,
612 value_length
, string
,
616 case ASN1_NULL_VALUE
:
617 fprintf(stderr
, "DEBUG: %*sNULL VALUE %d bytes\n", indent
, "",
620 buffer
+= value_length
;
624 asn1_get_oid(&buffer
, bufend
, value_length
, oid
, SNMP_MAX_OID
);
626 fprintf(stderr
, "DEBUG: %*sOID %d bytes ", indent
, "",
628 for (i
= 0; oid
[i
]; i
++)
629 fprintf(stderr
, ".%d", oid
[i
]);
634 fprintf(stderr
, "DEBUG: %*sSEQUENCE %d bytes\n", indent
, "",
636 asn1_debug(buffer
, value_length
, indent
+ 4);
638 buffer
+= value_length
;
641 case ASN1_GET_REQUEST
:
642 fprintf(stderr
, "DEBUG: %*sGet-Request-PDU %d bytes\n", indent
, "",
644 asn1_debug(buffer
, value_length
, indent
+ 4);
646 buffer
+= value_length
;
649 case ASN1_GET_RESPONSE
:
650 fprintf(stderr
, "DEBUG: %*sGet-Response-PDU %d bytes\n", indent
, "",
652 asn1_debug(buffer
, value_length
, indent
+ 4);
654 buffer
+= value_length
;
658 fprintf(stderr
, "DEBUG: %*sUNKNOWN(%x) %d bytes\n", indent
, "",
659 value_type
, value_length
);
661 buffer
+= value_length
;
669 * 'asn1_encode_snmp()' - Encode a SNMP packet.
672 static int /* O - Length on success, -1 on error */
673 asn1_encode_snmp(unsigned char *buffer
, /* I - Buffer */
674 size_t bufsize
, /* I - Size of buffer */
675 snmp_packet_t
*packet
) /* I - SNMP packet */
677 unsigned char *bufptr
; /* Pointer into buffer */
678 int total
, /* Total length */
679 msglen
, /* Length of entire message */
680 commlen
, /* Length of community string */
681 reqlen
, /* Length of request */
682 listlen
, /* Length of variable list */
683 varlen
, /* Length of variable */
684 namelen
, /* Length of object name OID */
685 valuelen
; /* Length of object value */
689 * Get the lengths of the community string, OID, and message...
692 namelen
= asn1_size_oid(packet
->object_name
);
694 switch (packet
->object_type
)
696 case ASN1_NULL_VALUE
:
701 valuelen
= asn1_size_integer(packet
->object_value
.boolean
);
705 valuelen
= asn1_size_integer(packet
->object_value
.integer
);
708 case ASN1_OCTET_STRING
:
709 valuelen
= strlen(packet
->object_value
.string
);
713 valuelen
= asn1_size_oid(packet
->object_value
.oid
);
717 packet
->error
= "Unknown object type";
721 varlen
= 1 + asn1_size_length(namelen
) + namelen
+
722 1 + asn1_size_length(valuelen
) + valuelen
;
723 listlen
= 1 + asn1_size_length(varlen
) + varlen
;
724 reqlen
= 2 + asn1_size_integer(packet
->request_id
) +
725 2 + asn1_size_integer(packet
->error_status
) +
726 2 + asn1_size_integer(packet
->error_index
) +
727 1 + asn1_size_length(listlen
) + listlen
;
728 commlen
= strlen(packet
->community
);
729 msglen
= 2 + asn1_size_integer(packet
->version
) +
730 1 + asn1_size_length(commlen
) + commlen
+
731 1 + asn1_size_length(reqlen
) + reqlen
;
732 total
= 1 + asn1_size_length(msglen
) + msglen
;
736 packet
->error
= "Message too large for buffer";
741 * Then format the message...
746 *bufptr
++ = ASN1_SEQUENCE
; /* SNMPv1 message header */
747 asn1_set_length(&bufptr
, msglen
);
749 asn1_set_integer(&bufptr
, packet
->version
);
752 *bufptr
++ = ASN1_OCTET_STRING
; /* community */
753 asn1_set_length(&bufptr
, commlen
);
754 memcpy(bufptr
, packet
->community
, commlen
);
757 *bufptr
++ = packet
->request_type
; /* Get-Request-PDU */
758 asn1_set_length(&bufptr
, reqlen
);
760 asn1_set_integer(&bufptr
, packet
->request_id
);
762 asn1_set_integer(&bufptr
, packet
->error_status
);
764 asn1_set_integer(&bufptr
, packet
->error_index
);
766 *bufptr
++ = ASN1_SEQUENCE
; /* variable-bindings */
767 asn1_set_length(&bufptr
, listlen
);
769 *bufptr
++ = ASN1_SEQUENCE
; /* variable */
770 asn1_set_length(&bufptr
, varlen
);
772 asn1_set_oid(&bufptr
, packet
->object_name
);
775 switch (packet
->object_type
)
777 case ASN1_NULL_VALUE
:
778 *bufptr
++ = ASN1_NULL_VALUE
; /* ObjectValue */
779 *bufptr
++ = 0; /* Length */
783 asn1_set_integer(&bufptr
, packet
->object_value
.boolean
);
787 asn1_set_integer(&bufptr
, packet
->object_value
.integer
);
790 case ASN1_OCTET_STRING
:
791 *bufptr
++ = ASN1_OCTET_STRING
;
792 asn1_set_length(&bufptr
, valuelen
);
793 memcpy(bufptr
, packet
->object_value
.string
, valuelen
);
798 asn1_set_oid(&bufptr
, packet
->object_value
.oid
);
802 return (bufptr
- buffer
);
807 * 'asn1_get_integer()' - Get an integer value.
810 static int /* O - Integer value */
812 unsigned char **buffer
, /* IO - Pointer in buffer */
813 unsigned char *bufend
, /* I - End of buffer */
814 int length
) /* I - Length of value */
816 int value
; /* Integer value */
820 length
> 0 && *buffer
< bufend
;
821 length
--, (*buffer
) ++)
822 value
= (value
<< 8) | **buffer
;
829 * 'asn1_get_length()' - Get a value length.
832 static int /* O - Length */
833 asn1_get_length(unsigned char **buffer
, /* IO - Pointer in buffer */
834 unsigned char *bufend
) /* I - End of buffer */
836 int length
; /* Length */
843 length
= asn1_get_integer(buffer
, bufend
, length
& 127);
850 * 'asn1_get_oid()' - Get an OID value.
853 static int /* O - Last OID number */
855 unsigned char **buffer
, /* IO - Pointer in buffer */
856 unsigned char *bufend
, /* I - End of buffer */
857 int length
, /* I - Length of value */
858 int *oid
, /* I - OID buffer */
859 int oidsize
) /* I - Size of OID buffer */
861 unsigned char *valend
; /* End of value */
862 int *oidend
; /* End of OID buffer */
863 int number
; /* OID number */
866 valend
= *buffer
+ length
;
867 oidend
= oid
+ oidsize
- 1;
872 number
= asn1_get_packed(buffer
, bufend
);
876 *oid
++ = number
/ 40;
877 number
= number
% 40;
887 while (*buffer
< valend
)
889 number
= asn1_get_packed(buffer
, bufend
);
902 * 'asn1_get_packed()' - Get a packed integer value.
905 static int /* O - Value */
907 unsigned char **buffer
, /* IO - Pointer in buffer */
908 unsigned char *bufend
) /* I - End of buffer */
910 int value
; /* Value */
915 while ((**buffer
& 128) && *buffer
< bufend
)
917 value
= (value
<< 7) | (**buffer
& 127);
921 if (*buffer
< bufend
)
923 value
= (value
<< 7) | **buffer
;
932 * 'asn1_get_string()' - Get a string value.
935 static char * /* O - String */
937 unsigned char **buffer
, /* IO - Pointer in buffer */
938 unsigned char *bufend
, /* I - End of buffer */
939 int length
, /* I - Value length */
940 char *string
, /* I - String buffer */
941 int strsize
) /* I - String buffer size */
943 if (length
< strsize
)
945 memcpy(string
, *buffer
, length
);
946 string
[length
] = '\0';
950 memcpy(string
, buffer
, strsize
- 1);
951 string
[strsize
- 1] = '\0';
961 * 'asn1_get_type()' - Get a value type.
964 static int /* O - Type */
965 asn1_get_type(unsigned char **buffer
, /* IO - Pointer in buffer */
966 unsigned char *bufend
) /* I - End of buffer */
974 if ((type
& 31) == 31)
975 type
= asn1_get_packed(buffer
, bufend
);
982 * 'asn1_set_integer()' - Set an integer value.
986 asn1_set_integer(unsigned char **buffer
,/* IO - Pointer in buffer */
987 int integer
) /* I - Integer value */
989 **buffer
= ASN1_INTEGER
;
992 if (integer
> 0x7fffff || integer
< -0x800000)
996 **buffer
= integer
>> 24;
998 **buffer
= integer
>> 16;
1000 **buffer
= integer
>> 8;
1005 else if (integer
> 0x7fff || integer
< -0x8000)
1009 **buffer
= integer
>> 16;
1011 **buffer
= integer
>> 8;
1016 else if (integer
> 0x7f || integer
< -0x80)
1020 **buffer
= integer
>> 8;
1036 * 'asn1_set_length()' - Set a value length.
1040 asn1_set_length(unsigned char **buffer
, /* IO - Pointer in buffer */
1041 int length
) /* I - Length value */
1045 **buffer
= 0x82; /* 2-byte length */
1047 **buffer
= length
>> 8;
1052 else if (length
> 127)
1054 **buffer
= 0x81; /* 1-byte length */
1061 **buffer
= length
; /* Length */
1068 * 'asn1_set_oid()' - Set an OID value.
1072 asn1_set_oid(unsigned char **buffer
, /* IO - Pointer in buffer */
1073 const int *oid
) /* I - OID value */
1075 **buffer
= ASN1_OID
;
1078 asn1_set_length(buffer
, asn1_size_oid(oid
));
1080 asn1_set_packed(buffer
, oid
[0] * 40 + oid
[1]);
1082 for (oid
+= 2; *oid
; oid
++)
1083 asn1_set_packed(buffer
, *oid
);
1088 * 'asn1_set_packed()' - Set a packed integer value.
1092 asn1_set_packed(unsigned char **buffer
, /* IO - Pointer in buffer */
1093 int integer
) /* I - Integer value */
1095 if (integer
> 0xfffffff)
1097 **buffer
= (integer
>> 14) & 0x7f;
1101 if (integer
> 0x1fffff)
1103 **buffer
= (integer
>> 21) & 0x7f;
1107 if (integer
> 0x3fff)
1109 **buffer
= (integer
>> 14) & 0x7f;
1115 **buffer
= (integer
>> 7) & 0x7f;
1119 **buffer
= integer
& 0x7f;
1124 * 'asn1_size_integer()' - Figure out the number of bytes needed for an
1128 static int /* O - Size in bytes */
1129 asn1_size_integer(int integer
) /* I - Integer value */
1131 if (integer
> 0x7fffff || integer
< -0x800000)
1133 else if (integer
> 0x7fff || integer
< -0x8000)
1135 else if (integer
> 0x7f || integer
< -0x80)
1143 * 'asn1_size_length()' - Figure out the number of bytes needed for a
1147 static int /* O - Size in bytes */
1148 asn1_size_length(int length
) /* I - Length value */
1152 else if (length
> 0x7f)
1160 * 'asn1_size_oid()' - Figure out the numebr of bytes needed for an
1164 static int /* O - Size in bytes */
1165 asn1_size_oid(const int *oid
) /* I - OID value */
1167 int length
; /* Length of value */
1170 for (length
= asn1_size_packed(oid
[0] * 40 + oid
[1]), oid
+= 2; *oid
; oid
++)
1171 length
+= asn1_size_packed(*oid
);
1178 * 'asn1_size_packed()' - Figure out the number of bytes needed for a
1179 * packed integer value.
1182 static int /* O - Size in bytes */
1183 asn1_size_packed(int integer
) /* I - Integer value */
1185 if (integer
> 0xfffffff)
1187 else if (integer
> 0x1fffff)
1189 else if (integer
> 0x3fff)
1191 else if (integer
> 0x7f)
1199 * 'compare_cache()' - Compare two cache entries.
1202 static int /* O - Result of comparison */
1203 compare_cache(snmp_cache_t
*a
, /* I - First cache entry */
1204 snmp_cache_t
*b
) /* I - Second cache entry */
1206 return (a
->address
.ipv4
.sin_addr
.s_addr
- b
->address
.ipv4
.sin_addr
.s_addr
);
1211 * 'debug_printf()' - Display some debugging information.
1215 debug_printf(const char *format
, /* I - Printf-style format string */
1216 ...) /* I - Additional arguments as needed */
1218 va_list ap
; /* Pointer to arguments */
1224 va_start(ap
, format
);
1225 vfprintf(stderr
, format
, ap
);
1231 * 'fix_make_model()' - Fix common problems in the make-and-model string.
1236 char *make_model
, /* I - New make-and-model string */
1237 const char *old_make_model
, /* I - Old make-and-model string */
1238 int make_model_size
) /* I - Size of new string buffer */
1240 char *mmptr
; /* Pointer into make-and-model string */
1244 * Fix some common problems with the make-and-model string so
1245 * that printer driver detection works better...
1248 if (!strncasecmp(old_make_model
, "Hewlett-Packard", 15))
1251 * Strip leading Hewlett-Packard and hp prefixes and replace
1252 * with a single HP manufacturer prefix...
1255 mmptr
= (char *)old_make_model
+ 15;
1257 while (isspace(*mmptr
& 255))
1260 if (!strncasecmp(mmptr
, "hp", 2))
1264 while (isspace(*mmptr
& 255))
1268 make_model
[0] = 'H';
1269 make_model
[1] = 'P';
1270 make_model
[2] = ' ';
1271 strlcpy(make_model
+ 3, mmptr
, make_model_size
- 3);
1273 else if (!strncasecmp(old_make_model
, "deskjet", 7))
1274 snprintf(make_model
, make_model_size
, "HP DeskJet%s", old_make_model
+ 7);
1275 else if (!strncasecmp(old_make_model
, "stylus_pro_", 11))
1276 snprintf(make_model
, make_model_size
, "EPSON Stylus Pro %s",
1277 old_make_model
+ 11);
1279 strlcpy(make_model
, old_make_model
, make_model_size
);
1281 if ((mmptr
= strstr(make_model
, ", Inc.,")) != NULL
)
1284 * Strip inc. from name, e.g. "Tektronix, Inc., Phaser 560"
1285 * becomes "Tektronix Phaser 560"...
1288 _cups_strcpy(mmptr
, mmptr
+ 7);
1291 if ((mmptr
= strchr(make_model
, ',')) != NULL
)
1294 * Drop anything after a trailing comma...
1303 * 'free_array()' - Free an array of strings.
1307 free_array(cups_array_t
*a
) /* I - Array */
1309 char *s
; /* Current string */
1312 for (s
= (char *)cupsArrayFirst(a
); s
; s
= (char *)cupsArrayNext(a
))
1320 * 'free_cache()' - Free the array of cached devices.
1326 snmp_cache_t
*cache
; /* Cached device */
1329 for (cache
= (snmp_cache_t
*)cupsArrayFirst(Devices
);
1331 cache
= (snmp_cache_t
*)cupsArrayNext(Devices
))
1333 free(cache
->addrname
);
1341 if (cache
->make_and_model
)
1342 free(cache
->make_and_model
);
1347 cupsArrayDelete(Devices
);
1353 * 'get_interface_addresses()' - Get the broadcast address(es) associated
1354 * with an interface.
1357 static http_addrlist_t
* /* O - List of addresses */
1358 get_interface_addresses(
1359 const char *ifname
) /* I - Interface name */
1361 struct ifaddrs
*addrs
, /* Interface address list */
1362 *addr
; /* Current interface address */
1363 http_addrlist_t
*first
, /* First address in list */
1364 *last
, /* Last address in list */
1365 *current
; /* Current address */
1368 if (getifaddrs(&addrs
) < 0)
1371 for (addr
= addrs
, first
= NULL
, last
= NULL
; addr
; addr
= addr
->ifa_next
)
1372 if ((addr
->ifa_flags
& IFF_BROADCAST
) && addr
->ifa_broadaddr
&&
1373 addr
->ifa_broadaddr
->sa_family
== AF_INET
&&
1374 (!ifname
|| !strcmp(ifname
, addr
->ifa_name
)))
1376 current
= calloc(1, sizeof(http_addrlist_t
));
1378 memcpy(&(current
->addr
), addr
->ifa_broadaddr
,
1379 sizeof(struct sockaddr_in
));
1384 last
->next
= current
;
1396 * 'hex_debug()' - Output hex debugging data...
1400 hex_debug(unsigned char *buffer
, /* I - Buffer */
1401 size_t len
) /* I - Number of bytes */
1403 int col
; /* Current column */
1406 fputs("DEBUG: Hex dump of packet:\n", stderr
);
1408 for (col
= 0; len
> 0; col
++, buffer
++, len
--)
1410 if ((col
& 15) == 0)
1411 fprintf(stderr
, "DEBUG: %04X ", col
);
1413 fprintf(stderr
, " %02X", *buffer
);
1415 if ((col
& 15) == 15)
1425 * 'list_devices()' - List all of the devices we found...
1431 snmp_cache_t
*cache
; /* Cached device */
1434 for (cache
= (snmp_cache_t
*)cupsArrayFirst(Devices
);
1436 cache
= (snmp_cache_t
*)cupsArrayNext(Devices
))
1438 printf("network %s \"%s\" \"%s %s\" \"%s\"\n",
1440 cache
->make_and_model
? cache
->make_and_model
: "Unknown",
1441 cache
->make_and_model
? cache
->make_and_model
: "Unknown",
1442 cache
->addrname
, cache
->id
? cache
->id
: "");
1447 * 'open_snmp_socket()' - Open the SNMP broadcast socket.
1450 static int /* O - SNMP socket file descriptor */
1451 open_snmp_socket(void)
1453 int fd
; /* SNMP socket file descriptor */
1454 int val
; /* Socket option value */
1458 * Create the SNMP socket...
1461 if ((fd
= socket(AF_INET
, SOCK_DGRAM
, 0)) < 0)
1463 fprintf(stderr
, "ERROR: Unable to create SNMP socket - %s\n",
1470 * Set the "broadcast" flag...
1475 if (setsockopt(fd
, SOL_SOCKET
, SO_BROADCAST
, &val
, sizeof(val
)))
1477 fprintf(stderr
, "ERROR: Unable to set broadcast mode - %s\n",
1490 * 'password_cb()' - Handle authentication requests.
1492 * All we do right now is return NULL, indicating that no authentication
1496 static const char * /* O - Password (NULL) */
1497 password_cb(const char *prompt
) /* I - Prompt message */
1499 (void)prompt
; /* Anti-compiler-warning-code */
1506 * 'probe_device()' - Probe a device to discover whether it is a printer.
1508 * TODO: Try using the Port Monitor MIB to discover the correct protocol
1509 * to use - first need a commercially-available printer that supports
1514 probe_device(snmp_cache_t
*device
) /* I - Device */
1516 int i
, j
; /* Looping vars */
1517 http_t
*http
; /* HTTP connection for IPP */
1518 char uri
[1024]; /* Full device URI */
1522 * Try connecting via IPP first...
1525 debug_printf("DEBUG: %.3f Probing %s...\n", run_time(), device
->addrname
);
1527 if ((http
= httpConnect(device
->addrname
, 631)) != NULL
)
1530 * IPP is supported...
1533 ipp_t
*request
, /* IPP request */
1534 *response
; /* IPP response */
1535 ipp_attribute_t
*model
, /* printer-make-and-model attribute */
1536 *info
, /* printer-info attribute */
1537 *supported
; /* printer-uri-supported attribute */
1538 char make_model
[256],/* Make and model string to use */
1539 temp
[256]; /* Temporary make/model string */
1540 int num_uris
; /* Number of good URIs */
1541 static const char * const resources
[] =
1542 { /* Common resource paths for IPP */
1546 "/EPSON_IPP_Printer",
1554 debug_printf("DEBUG: %s supports IPP!\n", device
->addrname
);
1557 * Use non-blocking IO...
1560 httpBlocking(http
, 0);
1563 * Loop through a list of common resources that covers 99% of the
1564 * IPP-capable printers on the market today...
1567 for (i
= 0, num_uris
= 0;
1568 i
< (int)(sizeof(resources
) / sizeof(resources
[0]));
1572 * Don't look past /ipp if we have found a working URI...
1575 if (num_uris
> 0 && strncmp(resources
[i
], "/ipp", 4))
1578 httpAssembleURI(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
1579 device
->addrname
, 631, resources
[i
]);
1581 debug_printf("DEBUG: Trying %s (num_uris=%d)\n", uri
, num_uris
);
1583 request
= ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES
);
1585 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
1588 response
= cupsDoRequest(http
, request
, resources
[i
]);
1590 debug_printf("DEBUG: %s %s (%s)\n", uri
,
1591 ippErrorString(cupsLastError()), cupsLastErrorString());
1593 if (response
&& response
->request
.status
.status_code
== IPP_OK
)
1595 model
= ippFindAttribute(response
, "printer-make-and-model",
1597 info
= ippFindAttribute(response
, "printer-info", IPP_TAG_TEXT
);
1598 supported
= ippFindAttribute(response
, "printer-uri-supported",
1603 fprintf(stderr
, "ERROR: Missing printer-uri-supported from %s!\n",
1610 debug_printf("DEBUG: printer-info=\"%s\"\n",
1611 info
? info
->values
[0].string
.text
: "(null)");
1612 debug_printf("DEBUG: printer-make-and-model=\"%s\"\n",
1613 model
? model
->values
[0].string
.text
: "(null)");
1616 * Don't advertise this port if the printer actually only supports
1617 * a more generic version...
1620 if (!strncmp(resources
[i
], "/ipp/", 5))
1622 for (j
= 0; j
< supported
->num_values
; j
++)
1623 if (strstr(supported
->values
[j
].string
.text
, "/ipp/"))
1626 if (j
>= supported
->num_values
)
1628 ippDelete(response
);
1634 * Don't use the printer-info attribute if it does not contain the
1635 * IEEE-1284 device ID data...
1639 (!strchr(info
->values
[0].string
.text
, ':') ||
1640 !strchr(info
->values
[0].string
.text
, ';')))
1644 * Don't use the printer-make-and-model if it contains a generic
1645 * string like "Ricoh IPP Printer"...
1648 if (model
&& strstr(model
->values
[0].string
.text
, "IPP Printer"))
1652 * If we don't have a printer-make-and-model string from the printer
1653 * but do have the 1284 device ID string, generate a make-and-model
1654 * string from the device ID info...
1658 strlcpy(temp
, model
->values
[0].string
.text
, sizeof(temp
));
1660 backendGetMakeModel(info
->values
[0].string
.text
, temp
, sizeof(temp
));
1664 fix_make_model(make_model
, temp
, sizeof(make_model
));
1667 * Update the current device or add a new printer to the cache...
1671 update_cache(device
, uri
,
1672 info
? info
->values
[0].string
.text
: NULL
,
1673 make_model
[0] ? make_model
: NULL
);
1675 add_cache(&(device
->address
), device
->addrname
, uri
,
1676 info
? info
->values
[0].string
.text
: NULL
,
1677 make_model
[0] ? make_model
: NULL
);
1682 ippDelete(response
);
1684 if (num_uris
> 0 && cupsLastError() != IPP_OK
)
1695 * OK, now try the standard ports...
1698 if (!try_connect(&(device
->address
), device
->addrname
, 9100))
1700 debug_printf("DEBUG: %s supports AppSocket!\n", device
->addrname
);
1702 snprintf(uri
, sizeof(uri
), "socket://%s", device
->addrname
);
1703 update_cache(device
, uri
, NULL
, NULL
);
1705 else if (!try_connect(&(device
->address
), device
->addrname
, 515))
1707 debug_printf("DEBUG: %s supports LPD!\n", device
->addrname
);
1709 snprintf(uri
, sizeof(uri
), "lpd://%s/", device
->addrname
);
1710 update_cache(device
, uri
, NULL
, NULL
);
1716 * 'read_snmp_conf()' - Read the snmp.conf file.
1720 read_snmp_conf(const char *address
) /* I - Single address to probe */
1722 cups_file_t
*fp
; /* File pointer */
1723 char filename
[1024], /* Filename */
1724 line
[1024], /* Line from file */
1725 *value
; /* Value on line */
1726 int linenum
; /* Line number */
1727 const char *cups_serverroot
; /* CUPS_SERVERROOT env var */
1728 const char *debug
; /* CUPS_DEBUG_LEVEL env var */
1732 * Initialize the global address and community lists...
1735 Addresses
= cupsArrayNew(NULL
, NULL
);
1736 Communities
= cupsArrayNew(NULL
, NULL
);
1739 add_array(Addresses
, address
);
1741 if ((debug
= getenv("CUPS_DEBUG_LEVEL")) != NULL
)
1742 DebugLevel
= atoi(debug
);
1745 * Find the snmp.conf file...
1748 if ((cups_serverroot
= getenv("CUPS_SERVERROOT")) == NULL
)
1749 cups_serverroot
= CUPS_SERVERROOT
;
1751 snprintf(filename
, sizeof(filename
), "%s/snmp.conf", cups_serverroot
);
1753 if ((fp
= cupsFileOpen(filename
, "r")) != NULL
)
1756 * Read the snmp.conf file...
1761 while (cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
))
1764 fprintf(stderr
, "ERROR: Missing value on line %d of %s!\n", linenum
,
1766 else if (!strcasecmp(line
, "Address"))
1769 add_array(Addresses
, value
);
1771 else if (!strcasecmp(line
, "Community"))
1772 add_array(Communities
, value
);
1773 else if (!strcasecmp(line
, "DebugLevel"))
1774 DebugLevel
= atoi(value
);
1775 else if (!strcasecmp(line
, "HostNameLookups"))
1776 HostNameLookups
= !strcasecmp(value
, "on") ||
1777 !strcasecmp(value
, "yes") ||
1778 !strcasecmp(value
, "true") ||
1779 !strcasecmp(value
, "double");
1781 fprintf(stderr
, "ERROR: Unknown directive %s on line %d of %s!\n",
1782 line
, linenum
, filename
);
1789 * Use defaults if parameters are undefined...
1792 if (cupsArrayCount(Addresses
) == 0)
1794 fputs("INFO: Using default SNMP Address @LOCAL\n", stderr
);
1795 add_array(Addresses
, "@LOCAL");
1798 if (cupsArrayCount(Communities
) == 0)
1800 fputs("INFO: Using default SNMP Community public\n", stderr
);
1801 add_array(Communities
, "public");
1807 * 'read_snmp_response()' - Read and parse a SNMP response...
1811 read_snmp_response(int fd
) /* I - SNMP socket file descriptor */
1813 unsigned char buffer
[SNMP_MAX_PACKET
];/* Data packet */
1814 int bytes
; /* Number of bytes received */
1815 http_addr_t addr
; /* Source address */
1816 socklen_t addrlen
; /* Source address length */
1817 char addrname
[256]; /* Source address name */
1818 snmp_packet_t packet
; /* Decoded packet */
1819 snmp_cache_t key
, /* Search key */
1820 *device
; /* Matching device */
1824 * Read the response data...
1827 addrlen
= sizeof(addr
);
1829 if ((bytes
= recvfrom(fd
, buffer
, sizeof(buffer
), 0, (void *)&addr
,
1832 fprintf(stderr
, "ERROR: Unable to read data from socket: %s\n",
1837 if (HostNameLookups
)
1838 httpAddrLookup(&addr
, addrname
, sizeof(addrname
));
1840 httpAddrString(&addr
, addrname
, sizeof(addrname
));
1842 debug_printf("DEBUG: %.3f Received %d bytes from %s...\n", run_time(),
1846 * Look for the response status code in the SNMP message header...
1849 if (asn1_decode_snmp(buffer
, bytes
, &packet
))
1851 fprintf(stderr
, "ERROR: Bad SNMP packet from %s: %s\n", addrname
,
1854 asn1_debug(buffer
, bytes
, 0);
1855 hex_debug(buffer
, bytes
);
1860 debug_printf("DEBUG: community=\"%s\"\n", packet
.community
);
1861 debug_printf("DEBUG: request-id=%d\n", packet
.request_id
);
1862 debug_printf("DEBUG: error-status=%d\n", packet
.error_status
);
1865 asn1_debug(buffer
, bytes
, 0);
1868 hex_debug(buffer
, bytes
);
1870 if (packet
.error_status
)
1874 * Find a matching device in the cache...
1878 device
= (snmp_cache_t
*)cupsArrayFind(Devices
, &key
);
1881 * Process the message...
1884 if (packet
.request_id
== DeviceTypeRequest
)
1887 * Got the device type response...
1892 debug_printf("DEBUG: Discarding duplicate device type for \"%s\"...\n",
1898 * Add the device and request the device description...
1901 add_cache(&addr
, addrname
, NULL
, NULL
, NULL
);
1903 send_snmp_query(fd
, &addr
, SNMP_VERSION_1
, packet
.community
,
1904 DeviceDescRequest
, DeviceDescOID
);
1906 else if (packet
.request_id
== DeviceDescRequest
&&
1907 packet
.object_type
== ASN1_OCTET_STRING
)
1910 * Update an existing cache entry...
1913 char make_model
[256]; /* Make and model */
1918 debug_printf("DEBUG: Discarding device description for \"%s\"...\n",
1924 * Convert the description to a make and model string...
1927 if (strchr(packet
.object_value
.string
, ':') &&
1928 strchr(packet
.object_value
.string
, ';'))
1931 * Description is the IEEE-1284 device ID...
1934 backendGetMakeModel(packet
.object_value
.string
, make_model
,
1935 sizeof(make_model
));
1940 * Description is plain text...
1943 fix_make_model(make_model
, packet
.object_value
.string
,
1944 sizeof(make_model
));
1947 if (device
->make_and_model
)
1948 free(device
->make_and_model
);
1950 device
->make_and_model
= strdup(make_model
);
1956 * 'run_time()' - Return the total running time...
1959 static double /* O - Number of seconds */
1962 struct timeval curtime
; /* Current time */
1965 gettimeofday(&curtime
, NULL
);
1967 return (curtime
.tv_sec
- StartTime
.tv_sec
+
1968 0.000001 * (curtime
.tv_usec
- StartTime
.tv_usec
));
1973 * 'scan_devices()' - Scan for devices using SNMP.
1977 scan_devices(int fd
) /* I - SNMP socket */
1979 char *address
, /* Current address */
1980 *community
; /* Current community */
1981 fd_set input
; /* Input set for select() */
1982 struct timeval timeout
; /* Timeout for select() */
1983 time_t endtime
; /* End time for scan */
1984 http_addrlist_t
*addrs
, /* List of addresses */
1985 *addr
; /* Current address */
1986 snmp_cache_t
*device
; /* Current device */
1990 * Setup the request IDs...
1993 gettimeofday(&StartTime
, NULL
);
1995 DeviceTypeRequest
= StartTime
.tv_sec
;
1996 DeviceDescRequest
= StartTime
.tv_sec
+ 1;
1999 * First send all of the broadcast queries...
2002 for (address
= (char *)cupsArrayFirst(Addresses
);
2004 address
= (char *)cupsArrayNext(Addresses
))
2006 if (!strcmp(address
, "@LOCAL"))
2007 addrs
= get_interface_addresses(NULL
);
2008 else if (!strncmp(address
, "@IF(", 4))
2010 char ifname
[255]; /* Interface name */
2013 strlcpy(ifname
, address
+ 4, sizeof(ifname
));
2015 ifname
[strlen(ifname
) - 1] = '\0';
2017 addrs
= get_interface_addresses(ifname
);
2020 addrs
= httpAddrGetList(address
, AF_INET
, NULL
);
2024 fprintf(stderr
, "ERROR: Unable to scan \"%s\"!\n", address
);
2028 for (community
= (char *)cupsArrayFirst(Communities
);
2030 community
= (char *)cupsArrayNext(Communities
))
2032 debug_printf("DEBUG: Scanning for devices in \"%s\" via \"%s\"...\n",
2033 community
, address
);
2035 for (addr
= addrs
; addr
; addr
= addr
->next
)
2036 send_snmp_query(fd
, &(addr
->addr
), SNMP_VERSION_1
, community
,
2037 DeviceTypeRequest
, DeviceTypeOID
);
2040 httpAddrFreeList(addrs
);
2044 * Then read any responses that come in over the next 3 seconds...
2047 endtime
= time(NULL
) + 3;
2051 while (time(NULL
) < endtime
)
2054 timeout
.tv_usec
= 0;
2057 if (select(fd
+ 1, &input
, NULL
, NULL
, &timeout
) < 0)
2059 fprintf(stderr
, "ERROR: %.3f select() for %d failed: %s\n", run_time(),
2060 fd
, strerror(errno
));
2064 if (FD_ISSET(fd
, &input
))
2065 read_snmp_response(fd
);
2071 * Finally, probe all of the printers we discovered to see how they are
2075 for (device
= (snmp_cache_t
*)cupsArrayFirst(Devices
);
2077 device
= (snmp_cache_t
*)cupsArrayNext(Devices
))
2079 probe_device(device
);
2081 debug_printf("DEBUG: %.3f Scan complete!\n", run_time());
2086 * 'send_snmp_query()' - Send an SNMP query packet.
2091 int fd
, /* I - SNMP socket */
2092 http_addr_t
*addr
, /* I - Address to send to */
2093 int version
, /* I - SNMP version */
2094 const char *community
, /* I - Community name */
2095 const unsigned request_id
, /* I - Request ID */
2096 const int *oid
) /* I - OID */
2098 int i
; /* Looping var */
2099 snmp_packet_t packet
; /* SNMP message packet */
2100 unsigned char buffer
[SNMP_MAX_PACKET
];/* SNMP message buffer */
2101 int bytes
; /* Size of message */
2102 char addrname
[32]; /* Address name */
2106 * Create the SNMP message...
2109 memset(&packet
, 0, sizeof(packet
));
2111 packet
.version
= version
;
2112 packet
.request_type
= ASN1_GET_REQUEST
;
2113 packet
.request_id
= request_id
;
2114 packet
.object_type
= ASN1_NULL_VALUE
;
2116 strlcpy(packet
.community
, community
, sizeof(packet
.community
));
2118 for (i
= 0; oid
[i
]; i
++)
2119 packet
.object_name
[i
] = oid
[i
];
2121 bytes
= asn1_encode_snmp(buffer
, sizeof(buffer
), &packet
);
2125 fprintf(stderr
, "ERROR: Unable to send SNMP query: %s\n", packet
.error
);
2130 * Send the message...
2133 debug_printf("DEBUG: %.3f Sending %d bytes to %s...\n", run_time(),
2134 bytes
, httpAddrString(addr
, addrname
, sizeof(addrname
)));
2136 asn1_debug(buffer
, bytes
, 0);
2138 hex_debug(buffer
, bytes
);
2140 addr
->ipv4
.sin_port
= htons(SNMP_PORT
);
2142 if (sendto(fd
, buffer
, bytes
, 0, (void *)addr
, sizeof(addr
->ipv4
)) < bytes
)
2143 fprintf(stderr
, "ERROR: Unable to send %d bytes to %s: %s\n",
2144 bytes
, addrname
, strerror(errno
));
2149 * 'try_connect()' - Try connecting on a port...
2152 static int /* O - 0 on success or -1 on error */
2153 try_connect(http_addr_t
*addr
, /* I - Socket address */
2154 const char *addrname
, /* I - Hostname or IP address */
2155 int port
) /* I - Port number */
2157 int fd
; /* Socket */
2158 int status
; /* Connection status */
2161 debug_printf("DEBUG: %.3f Trying %s://%s:%d...\n", run_time(),
2162 port
== 515 ? "lpd" : "socket", addrname
, port
);
2164 if ((fd
= socket(AF_INET
, SOCK_STREAM
, 0)) < 0)
2166 fprintf(stderr
, "ERROR: Unable to create socket: %s\n", strerror(errno
));
2170 addr
->ipv4
.sin_port
= htons(port
);
2172 signal(SIGALRM
, alarm_handler
);
2175 status
= connect(fd
, (void *)addr
, httpAddrLength(addr
));
2185 * 'update_cache()' - Update a cached device...
2189 update_cache(snmp_cache_t
*device
, /* I - Device */
2190 const char *uri
, /* I - Device URI */
2191 const char *id
, /* I - Device ID */
2192 const char *make_model
) /* I - Device make and model */
2197 device
->uri
= strdup(uri
);
2204 device
->id
= strdup(id
);
2209 if (device
->make_and_model
)
2210 free(device
->make_and_model
);
2212 device
->make_and_model
= strdup(make_model
);
2218 * End of "$Id: snmp.c 181 2006-06-22 20:01:18Z jlovell $".