2 * "$Id: snmp.c 5479 2006-05-02 15:03:17Z mike $"
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/backend.h>
81 #include <cups/http-private.h>
82 #include <cups/cups.h>
83 #include <cups/string.h>
84 #include <cups/array.h>
85 #include <cups/file.h>
94 * This backend implements SNMP printer discovery. It uses a broadcast-
95 * based approach to get SNMP response packets from potential printers
96 * and then interrogates each responder by trying to connect on port
99 * The current focus is on printers with internal network cards, although
100 * the code also works with many external print servers as well. Future
101 * versions will support scanning for vendor-specific SNMP OIDs and the
102 * new PWG Port Monitor MIB and not just the Host MIB OIDs.
104 * The backend reads the snmp.conf file from the CUPS_SERVERROOT directory
105 * which can contain comments, blank lines, or any number of the following
114 * HostNameLookups off
116 * The default is to use:
121 * HostNameLookups off
123 * This backend is known to work with the following network printers and
126 * Axis OfficeBasic, 5400, 5600
135 * It does not currently work with:
142 * (for all of these, they do not support the Host MIB)
149 #define SNMP_PORT 161 /* SNMP well-known port */
150 #define SNMP_MAX_OID 64 /* Maximum number of OID numbers */
151 #define SNMP_MAX_PACKET 1472 /* Maximum size of SNMP packet */
152 #define SNMP_MAX_STRING 512 /* Maximum size of string */
153 #define SNMP_VERSION_1 0 /* SNMPv1 */
155 #define ASN1_END_OF_CONTENTS 0x00 /* End-of-contents */
156 #define ASN1_BOOLEAN 0x01 /* BOOLEAN */
157 #define ASN1_INTEGER 0x02 /* INTEGER or ENUMERATION */
158 #define ASN1_BIT_STRING 0x03 /* BIT STRING */
159 #define ASN1_OCTET_STRING 0x04 /* OCTET STRING */
160 #define ASN1_NULL_VALUE 0x05 /* NULL VALUE */
161 #define ASN1_OID 0x06 /* OBJECT IDENTIFIER */
162 #define ASN1_SEQUENCE 0x30 /* SEQUENCE */
163 #define ASN1_GET_REQUEST 0xa0 /* Get-Request-PDU */
164 #define ASN1_GET_RESPONSE 0xa2 /* Get-Response-PDU */
171 typedef struct snmp_cache_s
/**** SNMP scan cache ****/
173 http_addr_t address
; /* Address of device */
174 char *addrname
, /* Name of device */
175 *uri
, /* device-uri */
177 *make_and_model
; /* device-make-and-model */
180 typedef struct snmp_packet_s
/**** SNMP packet ****/
182 const char *error
; /* Encode/decode error */
183 int version
; /* Version number */
184 char community
[SNMP_MAX_STRING
];
186 int request_type
; /* Request type */
187 int request_id
; /* request-id value */
188 int error_status
; /* error-status value */
189 int error_index
; /* error-index value */
190 int object_name
[SNMP_MAX_OID
];
191 /* object-name value */
192 int object_type
; /* object-value type */
195 int boolean
; /* Boolean value */
196 int integer
; /* Integer value */
197 int oid
[SNMP_MAX_OID
]; /* OID value */
198 char string
[SNMP_MAX_STRING
];/* String value */
199 } object_value
; /* object-value value */
207 static char *add_array(cups_array_t
*a
, const char *s
);
208 static void add_cache(http_addr_t
*addr
, const char *addrname
,
209 const char *uri
, const char *id
,
210 const char *make_and_model
);
211 static void alarm_handler(int sig
);
212 static int asn1_decode_snmp(unsigned char *buffer
, size_t len
,
213 snmp_packet_t
*packet
);
214 static void asn1_debug(unsigned char *buffer
, size_t len
,
216 static int asn1_encode_snmp(unsigned char *buffer
, size_t len
,
217 snmp_packet_t
*packet
);
218 static int asn1_get_integer(unsigned char **buffer
,
219 unsigned char *bufend
,
221 static int asn1_get_oid(unsigned char **buffer
,
222 unsigned char *bufend
,
223 int length
, int *oid
, int oidsize
);
224 static int asn1_get_packed(unsigned char **buffer
,
225 unsigned char *bufend
);
226 static char *asn1_get_string(unsigned char **buffer
,
227 unsigned char *bufend
,
228 int length
, char *string
,
230 static int asn1_get_length(unsigned char **buffer
,
231 unsigned char *bufend
);
232 static int asn1_get_type(unsigned char **buffer
,
233 unsigned char *bufend
);
234 static void asn1_set_integer(unsigned char **buffer
,
236 static void asn1_set_length(unsigned char **buffer
,
238 static void asn1_set_oid(unsigned char **buffer
,
240 static void asn1_set_packed(unsigned char **buffer
,
242 static int asn1_size_integer(int integer
);
243 static int asn1_size_length(int length
);
244 static int asn1_size_oid(const int *oid
);
245 static int asn1_size_packed(int integer
);
246 static int compare_cache(snmp_cache_t
*a
, snmp_cache_t
*b
);
247 static void debug_printf(const char *format
, ...);
248 static void fix_make_model(char *make_model
,
249 const char *old_make_model
,
250 int make_model_size
);
251 static void free_array(cups_array_t
*a
);
252 static void free_cache(void);
253 static http_addrlist_t
*get_interface_addresses(const char *ifname
);
254 static void hex_debug(unsigned char *buffer
, size_t len
);
255 static void list_devices(void);
256 static int open_snmp_socket(void);
257 static const char *password_cb(const char *prompt
);
258 static void probe_device(snmp_cache_t
*device
);
259 static void read_snmp_conf(const char *address
);
260 static void read_snmp_response(int fd
);
261 static double run_time(void);
262 static void scan_devices(int fd
);
263 static void send_snmp_query(int fd
, http_addr_t
*addr
, int version
,
264 const char *community
,
265 const unsigned request_id
,
267 static int try_connect(http_addr_t
*addr
, const char *addrname
,
269 static void update_cache(snmp_cache_t
*device
, const char *uri
,
270 const char *id
, const char *make_model
);
277 static cups_array_t
*Addresses
= NULL
;
278 static cups_array_t
*Communities
= NULL
;
279 static cups_array_t
*Devices
= NULL
;
280 static int DebugLevel
= 0;
281 static int DeviceTypeOID
[] = { 1, 3, 6, 1, 2, 1, 25, 3,
283 static int DeviceDescOID
[] = { 1, 3, 6, 1, 2, 1, 25, 3,
285 static unsigned DeviceTypeRequest
;
286 static unsigned DeviceDescRequest
;
287 static int HostNameLookups
= 0;
288 static struct timeval StartTime
;
292 * 'main()' - Discover printers via SNMP.
295 int /* O - Exit status */
296 main(int argc
, /* I - Number of command-line arguments (6 or 7) */
297 char *argv
[]) /* I - Command-line arguments */
299 int fd
; /* SNMP socket */
303 * Check command-line options...
308 fputs("Usage: snmp [host-or-ip-address]\n", stderr
);
313 * Set the password callback for IPP operations...
316 cupsSetPasswordCB(password_cb
);
319 * Open the SNMP socket...
322 if ((fd
= open_snmp_socket()) < 0)
326 * Read the configuration file and any cache data...
329 read_snmp_conf(argv
[1]);
331 Devices
= cupsArrayNew((cups_array_func_t
)compare_cache
, NULL
);
334 * Scan for devices...
340 * Display the results...
346 * Close, free, and return with no errors...
351 free_array(Addresses
);
352 free_array(Communities
);
360 * 'add_array()' - Add a string to an array.
363 static char * /* O - New string */
364 add_array(cups_array_t
*a
, /* I - Array */
365 const char *s
) /* I - String to add */
367 char *dups
; /* New string */
372 cupsArrayAdd(a
, dups
);
379 * 'add_cache()' - Add a cached device...
383 add_cache(http_addr_t
*addr
, /* I - Device IP address */
384 const char *addrname
, /* I - IP address or name string */
385 const char *uri
, /* I - Device URI */
386 const char *id
, /* I - 1284 device ID */
387 const char *make_and_model
) /* I - Make and model */
389 snmp_cache_t
*temp
; /* New device entry */
392 debug_printf("DEBUG: add_cache(addr=%p, addrname=\"%s\", uri=\"%s\", "
393 "id=\"%s\", make_and_model=\"%s\")\n",
394 addr
, addrname
, uri
, id
? id
: "(null)",
395 make_and_model
? make_and_model
: "(null)");
397 temp
= calloc(1, sizeof(snmp_cache_t
));
398 memcpy(&(temp
->address
), addr
, sizeof(temp
->address
));
400 temp
->addrname
= strdup(addrname
);
403 temp
->uri
= strdup(uri
);
406 temp
->id
= strdup(id
);
409 temp
->make_and_model
= strdup(make_and_model
);
411 cupsArrayAdd(Devices
, temp
);
416 * 'alarm_handler()' - Handle alarm signals...
420 alarm_handler(int sig
) /* I - Signal number */
429 write(2, "DEBUG: ALARM!\n", 14);
434 * 'asn1_decode_snmp()' - Decode a SNMP packet.
437 static int /* O - 0 on success, -1 on error */
438 asn1_decode_snmp(unsigned char *buffer
, /* I - Buffer */
439 size_t len
, /* I - Size of buffer */
440 snmp_packet_t
*packet
) /* I - SNMP packet */
442 unsigned char *bufptr
, /* Pointer into the data */
443 *bufend
; /* End of data */
444 int length
; /* Length of value */
448 * Initialize the decoding...
451 memset(packet
, 0, sizeof(snmp_packet_t
));
454 bufend
= buffer
+ len
;
456 if (asn1_get_type(&bufptr
, bufend
) != ASN1_SEQUENCE
)
457 packet
->error
= "Packet does not start with SEQUENCE";
458 else if (asn1_get_length(&bufptr
, bufend
) == 0)
459 packet
->error
= "SEQUENCE uses indefinite length";
460 else if (asn1_get_type(&bufptr
, bufend
) != ASN1_INTEGER
)
461 packet
->error
= "No version number";
462 else if ((length
= asn1_get_length(&bufptr
, bufend
)) == 0)
463 packet
->error
= "Version uses indefinite length";
464 else if ((packet
->version
= asn1_get_integer(&bufptr
, bufend
, length
))
466 packet
->error
= "Bad SNMP version number";
467 else if (asn1_get_type(&bufptr
, bufend
) != ASN1_OCTET_STRING
)
468 packet
->error
= "No community name";
469 else if ((length
= asn1_get_length(&bufptr
, bufend
)) == 0)
470 packet
->error
= "Community name uses indefinite length";
473 asn1_get_string(&bufptr
, bufend
, length
, packet
->community
,
474 sizeof(packet
->community
));
476 if ((packet
->request_type
= asn1_get_type(&bufptr
, bufend
))
477 != ASN1_GET_RESPONSE
)
478 packet
->error
= "Packet does not contain a Get-Response-PDU";
479 else if (asn1_get_length(&bufptr
, bufend
) == 0)
480 packet
->error
= "Get-Response-PDU uses indefinite length";
481 else if (asn1_get_type(&bufptr
, bufend
) != ASN1_INTEGER
)
482 packet
->error
= "No request-id";
483 else if ((length
= asn1_get_length(&bufptr
, bufend
)) == 0)
484 packet
->error
= "request-id uses indefinite length";
487 packet
->request_id
= asn1_get_integer(&bufptr
, bufend
, length
);
489 if (asn1_get_type(&bufptr
, bufend
) != ASN1_INTEGER
)
490 packet
->error
= "No error-status";
491 else if ((length
= asn1_get_length(&bufptr
, bufend
)) == 0)
492 packet
->error
= "error-status uses indefinite length";
495 packet
->error_status
= asn1_get_integer(&bufptr
, bufend
, length
);
497 if (asn1_get_type(&bufptr
, bufend
) != ASN1_INTEGER
)
498 packet
->error
= "No error-index";
499 else if ((length
= asn1_get_length(&bufptr
, bufend
)) == 0)
500 packet
->error
= "error-index uses indefinite length";
503 packet
->error_index
= asn1_get_integer(&bufptr
, bufend
, length
);
505 if (asn1_get_type(&bufptr
, bufend
) != ASN1_SEQUENCE
)
506 packet
->error
= "No variable-bindings SEQUENCE";
507 else if (asn1_get_length(&bufptr
, bufend
) == 0)
508 packet
->error
= "variable-bindings uses indefinite length";
509 else if (asn1_get_type(&bufptr
, bufend
) != ASN1_SEQUENCE
)
510 packet
->error
= "No VarBind SEQUENCE";
511 else if (asn1_get_length(&bufptr
, bufend
) == 0)
512 packet
->error
= "VarBind uses indefinite length";
513 else if (asn1_get_type(&bufptr
, bufend
) != ASN1_OID
)
514 packet
->error
= "No name OID";
515 else if ((length
= asn1_get_length(&bufptr
, bufend
)) == 0)
516 packet
->error
= "Name OID uses indefinite length";
519 asn1_get_oid(&bufptr
, bufend
, length
, packet
->object_name
,
522 packet
->object_type
= asn1_get_type(&bufptr
, bufend
);
524 if ((length
= asn1_get_length(&bufptr
, bufend
)) == 0 &&
525 packet
->object_type
!= ASN1_NULL_VALUE
&&
526 packet
->object_type
!= ASN1_OCTET_STRING
)
527 packet
->error
= "Value uses indefinite length";
530 switch (packet
->object_type
)
533 packet
->object_value
.boolean
=
534 asn1_get_integer(&bufptr
, bufend
, length
);
538 packet
->object_value
.integer
=
539 asn1_get_integer(&bufptr
, bufend
, length
);
542 case ASN1_NULL_VALUE
:
545 case ASN1_OCTET_STRING
:
546 asn1_get_string(&bufptr
, bufend
, length
,
547 packet
->object_value
.string
,
552 asn1_get_oid(&bufptr
, bufend
, length
,
553 packet
->object_value
.oid
, SNMP_MAX_OID
);
557 packet
->error
= "Unsupported value type";
567 return (packet
->error
? -1 : 0);
572 * 'asn1_debug()' - Decode an ASN1-encoded message.
576 asn1_debug(unsigned char *buffer
, /* I - Buffer */
577 size_t len
, /* I - Length of buffer */
578 int indent
) /* I - Indentation */
580 int i
; /* Looping var */
581 unsigned char *bufend
; /* End of buffer */
582 int integer
; /* Number value */
583 int oid
[SNMP_MAX_OID
]; /* OID value */
584 char string
[SNMP_MAX_STRING
];/* String value */
585 unsigned char value_type
; /* Type of value */
586 int value_length
; /* Length of value */
589 bufend
= buffer
+ len
;
591 while (buffer
< bufend
)
597 value_type
= asn1_get_type(&buffer
, bufend
);
598 value_length
= asn1_get_length(&buffer
, bufend
);
603 integer
= asn1_get_integer(&buffer
, bufend
, value_length
);
605 fprintf(stderr
, "DEBUG: %*sBOOLEAN %d bytes %d\n", indent
, "",
606 value_length
, integer
);
610 integer
= asn1_get_integer(&buffer
, bufend
, value_length
);
612 fprintf(stderr
, "DEBUG: %*sINTEGER %d bytes %d\n", indent
, "",
613 value_length
, integer
);
616 case ASN1_OCTET_STRING
:
617 fprintf(stderr
, "DEBUG: %*sOCTET STRING %d bytes \"%s\"\n", indent
, "",
618 value_length
, asn1_get_string(&buffer
, bufend
,
619 value_length
, string
,
623 case ASN1_NULL_VALUE
:
624 fprintf(stderr
, "DEBUG: %*sNULL VALUE %d bytes\n", indent
, "",
627 buffer
+= value_length
;
631 asn1_get_oid(&buffer
, bufend
, value_length
, oid
, SNMP_MAX_OID
);
633 fprintf(stderr
, "DEBUG: %*sOID %d bytes ", indent
, "",
635 for (i
= 0; oid
[i
]; i
++)
636 fprintf(stderr
, ".%d", oid
[i
]);
641 fprintf(stderr
, "DEBUG: %*sSEQUENCE %d bytes\n", indent
, "",
643 asn1_debug(buffer
, value_length
, indent
+ 4);
645 buffer
+= value_length
;
648 case ASN1_GET_REQUEST
:
649 fprintf(stderr
, "DEBUG: %*sGet-Request-PDU %d bytes\n", indent
, "",
651 asn1_debug(buffer
, value_length
, indent
+ 4);
653 buffer
+= value_length
;
656 case ASN1_GET_RESPONSE
:
657 fprintf(stderr
, "DEBUG: %*sGet-Response-PDU %d bytes\n", indent
, "",
659 asn1_debug(buffer
, value_length
, indent
+ 4);
661 buffer
+= value_length
;
665 fprintf(stderr
, "DEBUG: %*sUNKNOWN(%x) %d bytes\n", indent
, "",
666 value_type
, value_length
);
668 buffer
+= value_length
;
676 * 'asn1_encode_snmp()' - Encode a SNMP packet.
679 static int /* O - Length on success, -1 on error */
680 asn1_encode_snmp(unsigned char *buffer
, /* I - Buffer */
681 size_t bufsize
, /* I - Size of buffer */
682 snmp_packet_t
*packet
) /* I - SNMP packet */
684 unsigned char *bufptr
; /* Pointer into buffer */
685 int total
, /* Total length */
686 msglen
, /* Length of entire message */
687 commlen
, /* Length of community string */
688 reqlen
, /* Length of request */
689 listlen
, /* Length of variable list */
690 varlen
, /* Length of variable */
691 namelen
, /* Length of object name OID */
692 valuelen
; /* Length of object value */
696 * Get the lengths of the community string, OID, and message...
699 namelen
= asn1_size_oid(packet
->object_name
);
701 switch (packet
->object_type
)
703 case ASN1_NULL_VALUE
:
708 valuelen
= asn1_size_integer(packet
->object_value
.boolean
);
712 valuelen
= asn1_size_integer(packet
->object_value
.integer
);
715 case ASN1_OCTET_STRING
:
716 valuelen
= strlen(packet
->object_value
.string
);
720 valuelen
= asn1_size_oid(packet
->object_value
.oid
);
724 packet
->error
= "Unknown object type";
728 varlen
= 1 + asn1_size_length(namelen
) + namelen
+
729 1 + asn1_size_length(valuelen
) + valuelen
;
730 listlen
= 1 + asn1_size_length(varlen
) + varlen
;
731 reqlen
= 2 + asn1_size_integer(packet
->request_id
) +
732 2 + asn1_size_integer(packet
->error_status
) +
733 2 + asn1_size_integer(packet
->error_index
) +
734 1 + asn1_size_length(listlen
) + listlen
;
735 commlen
= strlen(packet
->community
);
736 msglen
= 2 + asn1_size_integer(packet
->version
) +
737 1 + asn1_size_length(commlen
) + commlen
+
738 1 + asn1_size_length(reqlen
) + reqlen
;
739 total
= 1 + asn1_size_length(msglen
) + msglen
;
743 packet
->error
= "Message too large for buffer";
748 * Then format the message...
753 *bufptr
++ = ASN1_SEQUENCE
; /* SNMPv1 message header */
754 asn1_set_length(&bufptr
, msglen
);
756 asn1_set_integer(&bufptr
, packet
->version
);
759 *bufptr
++ = ASN1_OCTET_STRING
; /* community */
760 asn1_set_length(&bufptr
, commlen
);
761 memcpy(bufptr
, packet
->community
, commlen
);
764 *bufptr
++ = packet
->request_type
; /* Get-Request-PDU */
765 asn1_set_length(&bufptr
, reqlen
);
767 asn1_set_integer(&bufptr
, packet
->request_id
);
769 asn1_set_integer(&bufptr
, packet
->error_status
);
771 asn1_set_integer(&bufptr
, packet
->error_index
);
773 *bufptr
++ = ASN1_SEQUENCE
; /* variable-bindings */
774 asn1_set_length(&bufptr
, listlen
);
776 *bufptr
++ = ASN1_SEQUENCE
; /* variable */
777 asn1_set_length(&bufptr
, varlen
);
779 asn1_set_oid(&bufptr
, packet
->object_name
);
782 switch (packet
->object_type
)
784 case ASN1_NULL_VALUE
:
785 *bufptr
++ = ASN1_NULL_VALUE
; /* ObjectValue */
786 *bufptr
++ = 0; /* Length */
790 asn1_set_integer(&bufptr
, packet
->object_value
.boolean
);
794 asn1_set_integer(&bufptr
, packet
->object_value
.integer
);
797 case ASN1_OCTET_STRING
:
798 *bufptr
++ = ASN1_OCTET_STRING
;
799 asn1_set_length(&bufptr
, valuelen
);
800 memcpy(bufptr
, packet
->object_value
.string
, valuelen
);
805 asn1_set_oid(&bufptr
, packet
->object_value
.oid
);
809 return (bufptr
- buffer
);
814 * 'asn1_get_integer()' - Get an integer value.
817 static int /* O - Integer value */
819 unsigned char **buffer
, /* IO - Pointer in buffer */
820 unsigned char *bufend
, /* I - End of buffer */
821 int length
) /* I - Length of value */
823 int value
; /* Integer value */
827 length
> 0 && *buffer
< bufend
;
828 length
--, (*buffer
) ++)
829 value
= (value
<< 8) | **buffer
;
836 * 'asn1_get_length()' - Get a value length.
839 static int /* O - Length */
840 asn1_get_length(unsigned char **buffer
, /* IO - Pointer in buffer */
841 unsigned char *bufend
) /* I - End of buffer */
843 int length
; /* Length */
850 length
= asn1_get_integer(buffer
, bufend
, length
& 127);
857 * 'asn1_get_oid()' - Get an OID value.
860 static int /* O - Last OID number */
862 unsigned char **buffer
, /* IO - Pointer in buffer */
863 unsigned char *bufend
, /* I - End of buffer */
864 int length
, /* I - Length of value */
865 int *oid
, /* I - OID buffer */
866 int oidsize
) /* I - Size of OID buffer */
868 unsigned char *valend
; /* End of value */
869 int *oidend
; /* End of OID buffer */
870 int number
; /* OID number */
873 valend
= *buffer
+ length
;
874 oidend
= oid
+ oidsize
- 1;
879 number
= asn1_get_packed(buffer
, bufend
);
883 *oid
++ = number
/ 40;
884 number
= number
% 40;
894 while (*buffer
< valend
)
896 number
= asn1_get_packed(buffer
, bufend
);
909 * 'asn1_get_packed()' - Get a packed integer value.
912 static int /* O - Value */
914 unsigned char **buffer
, /* IO - Pointer in buffer */
915 unsigned char *bufend
) /* I - End of buffer */
917 int value
; /* Value */
922 while ((**buffer
& 128) && *buffer
< bufend
)
924 value
= (value
<< 7) | (**buffer
& 127);
928 if (*buffer
< bufend
)
930 value
= (value
<< 7) | **buffer
;
939 * 'asn1_get_string()' - Get a string value.
942 static char * /* O - String */
944 unsigned char **buffer
, /* IO - Pointer in buffer */
945 unsigned char *bufend
, /* I - End of buffer */
946 int length
, /* I - Value length */
947 char *string
, /* I - String buffer */
948 int strsize
) /* I - String buffer size */
950 if (length
< strsize
)
952 memcpy(string
, *buffer
, length
);
953 string
[length
] = '\0';
957 memcpy(string
, buffer
, strsize
- 1);
958 string
[strsize
- 1] = '\0';
968 * 'asn1_get_type()' - Get a value type.
971 static int /* O - Type */
972 asn1_get_type(unsigned char **buffer
, /* IO - Pointer in buffer */
973 unsigned char *bufend
) /* I - End of buffer */
981 if ((type
& 31) == 31)
982 type
= asn1_get_packed(buffer
, bufend
);
989 * 'asn1_set_integer()' - Set an integer value.
993 asn1_set_integer(unsigned char **buffer
,/* IO - Pointer in buffer */
994 int integer
) /* I - Integer value */
996 **buffer
= ASN1_INTEGER
;
999 if (integer
> 0x7fffff || integer
< -0x800000)
1003 **buffer
= integer
>> 24;
1005 **buffer
= integer
>> 16;
1007 **buffer
= integer
>> 8;
1012 else if (integer
> 0x7fff || integer
< -0x8000)
1016 **buffer
= integer
>> 16;
1018 **buffer
= integer
>> 8;
1023 else if (integer
> 0x7f || integer
< -0x80)
1027 **buffer
= integer
>> 8;
1043 * 'asn1_set_length()' - Set a value length.
1047 asn1_set_length(unsigned char **buffer
, /* IO - Pointer in buffer */
1048 int length
) /* I - Length value */
1052 **buffer
= 0x82; /* 2-byte length */
1054 **buffer
= length
>> 8;
1059 else if (length
> 127)
1061 **buffer
= 0x81; /* 1-byte length */
1068 **buffer
= length
; /* Length */
1075 * 'asn1_set_oid()' - Set an OID value.
1079 asn1_set_oid(unsigned char **buffer
, /* IO - Pointer in buffer */
1080 const int *oid
) /* I - OID value */
1082 **buffer
= ASN1_OID
;
1085 asn1_set_length(buffer
, asn1_size_oid(oid
));
1087 asn1_set_packed(buffer
, oid
[0] * 40 + oid
[1]);
1089 for (oid
+= 2; *oid
; oid
++)
1090 asn1_set_packed(buffer
, *oid
);
1095 * 'asn1_set_packed()' - Set a packed integer value.
1099 asn1_set_packed(unsigned char **buffer
, /* IO - Pointer in buffer */
1100 int integer
) /* I - Integer value */
1102 if (integer
> 0xfffffff)
1104 **buffer
= (integer
>> 14) & 0x7f;
1108 if (integer
> 0x1fffff)
1110 **buffer
= (integer
>> 21) & 0x7f;
1114 if (integer
> 0x3fff)
1116 **buffer
= (integer
>> 14) & 0x7f;
1122 **buffer
= (integer
>> 7) & 0x7f;
1126 **buffer
= integer
& 0x7f;
1131 * 'asn1_size_integer()' - Figure out the number of bytes needed for an
1135 static int /* O - Size in bytes */
1136 asn1_size_integer(int integer
) /* I - Integer value */
1138 if (integer
> 0x7fffff || integer
< -0x800000)
1140 else if (integer
> 0x7fff || integer
< -0x8000)
1142 else if (integer
> 0x7f || integer
< -0x80)
1150 * 'asn1_size_length()' - Figure out the number of bytes needed for a
1154 static int /* O - Size in bytes */
1155 asn1_size_length(int length
) /* I - Length value */
1159 else if (length
> 0x7f)
1167 * 'asn1_size_oid()' - Figure out the numebr of bytes needed for an
1171 static int /* O - Size in bytes */
1172 asn1_size_oid(const int *oid
) /* I - OID value */
1174 int length
; /* Length of value */
1177 for (length
= asn1_size_packed(oid
[0] * 40 + oid
[1]), oid
+= 2; *oid
; oid
++)
1178 length
+= asn1_size_packed(*oid
);
1185 * 'asn1_size_packed()' - Figure out the number of bytes needed for a
1186 * packed integer value.
1189 static int /* O - Size in bytes */
1190 asn1_size_packed(int integer
) /* I - Integer value */
1192 if (integer
> 0xfffffff)
1194 else if (integer
> 0x1fffff)
1196 else if (integer
> 0x3fff)
1198 else if (integer
> 0x7f)
1206 * 'compare_cache()' - Compare two cache entries.
1209 static int /* O - Result of comparison */
1210 compare_cache(snmp_cache_t
*a
, /* I - First cache entry */
1211 snmp_cache_t
*b
) /* I - Second cache entry */
1213 return (a
->address
.ipv4
.sin_addr
.s_addr
- b
->address
.ipv4
.sin_addr
.s_addr
);
1218 * 'debug_printf()' - Display some debugging information.
1222 debug_printf(const char *format
, /* I - Printf-style format string */
1223 ...) /* I - Additional arguments as needed */
1225 va_list ap
; /* Pointer to arguments */
1231 va_start(ap
, format
);
1232 vfprintf(stderr
, format
, ap
);
1238 * 'fix_make_model()' - Fix common problems in the make-and-model string.
1243 char *make_model
, /* I - New make-and-model string */
1244 const char *old_make_model
, /* I - Old make-and-model string */
1245 int make_model_size
) /* I - Size of new string buffer */
1247 char *mmptr
; /* Pointer into make-and-model string */
1251 * Fix some common problems with the make-and-model string so
1252 * that printer driver detection works better...
1255 if (!strncasecmp(old_make_model
, "Hewlett-Packard", 15))
1258 * Strip leading Hewlett-Packard and hp prefixes and replace
1259 * with a single HP manufacturer prefix...
1262 mmptr
= (char *)old_make_model
+ 15;
1264 while (isspace(*mmptr
& 255))
1267 if (!strncasecmp(mmptr
, "hp", 2))
1271 while (isspace(*mmptr
& 255))
1275 make_model
[0] = 'H';
1276 make_model
[1] = 'P';
1277 make_model
[2] = ' ';
1278 strlcpy(make_model
+ 3, mmptr
, make_model_size
- 3);
1280 else if (!strncasecmp(old_make_model
, "deskjet", 7))
1281 snprintf(make_model
, make_model_size
, "HP DeskJet%s", old_make_model
+ 7);
1282 else if (!strncasecmp(old_make_model
, "stylus_pro_", 11))
1283 snprintf(make_model
, make_model_size
, "EPSON Stylus Pro %s",
1284 old_make_model
+ 11);
1286 strlcpy(make_model
, old_make_model
, make_model_size
);
1288 if ((mmptr
= strstr(make_model
, ", Inc.,")) != NULL
)
1291 * Strip inc. from name, e.g. "Tektronix, Inc., Phaser 560"
1292 * becomes "Tektronix Phaser 560"...
1295 _cups_strcpy(mmptr
, mmptr
+ 7);
1298 if ((mmptr
= strchr(make_model
, ',')) != NULL
)
1301 * Drop anything after a trailing comma...
1310 * 'free_array()' - Free an array of strings.
1314 free_array(cups_array_t
*a
) /* I - Array */
1316 char *s
; /* Current string */
1319 for (s
= (char *)cupsArrayFirst(a
); s
; s
= (char *)cupsArrayNext(a
))
1327 * 'free_cache()' - Free the array of cached devices.
1333 snmp_cache_t
*cache
; /* Cached device */
1336 for (cache
= (snmp_cache_t
*)cupsArrayFirst(Devices
);
1338 cache
= (snmp_cache_t
*)cupsArrayNext(Devices
))
1340 free(cache
->addrname
);
1348 if (cache
->make_and_model
)
1349 free(cache
->make_and_model
);
1354 cupsArrayDelete(Devices
);
1360 * 'get_interface_addresses()' - Get the broadcast address(es) associated
1361 * with an interface.
1364 static http_addrlist_t
* /* O - List of addresses */
1365 get_interface_addresses(
1366 const char *ifname
) /* I - Interface name */
1368 struct ifaddrs
*addrs
, /* Interface address list */
1369 *addr
; /* Current interface address */
1370 http_addrlist_t
*first
, /* First address in list */
1371 *last
, /* Last address in list */
1372 *current
; /* Current address */
1375 if (getifaddrs(&addrs
) < 0)
1378 for (addr
= addrs
, first
= NULL
, last
= NULL
; addr
; addr
= addr
->ifa_next
)
1379 if ((addr
->ifa_flags
& IFF_BROADCAST
) && addr
->ifa_broadaddr
&&
1380 addr
->ifa_broadaddr
->sa_family
== AF_INET
&&
1381 (!ifname
|| !strcmp(ifname
, addr
->ifa_name
)))
1383 current
= calloc(1, sizeof(http_addrlist_t
));
1385 memcpy(&(current
->addr
), addr
->ifa_broadaddr
,
1386 sizeof(struct sockaddr_in
));
1391 last
->next
= current
;
1403 * 'hex_debug()' - Output hex debugging data...
1407 hex_debug(unsigned char *buffer
, /* I - Buffer */
1408 size_t len
) /* I - Number of bytes */
1410 int col
; /* Current column */
1413 fputs("DEBUG: Hex dump of packet:\n", stderr
);
1415 for (col
= 0; len
> 0; col
++, buffer
++, len
--)
1417 if ((col
& 15) == 0)
1418 fprintf(stderr
, "DEBUG: %04X ", col
);
1420 fprintf(stderr
, " %02X", *buffer
);
1422 if ((col
& 15) == 15)
1432 * 'list_devices()' - List all of the devices we found...
1438 snmp_cache_t
*cache
; /* Cached device */
1441 for (cache
= (snmp_cache_t
*)cupsArrayFirst(Devices
);
1443 cache
= (snmp_cache_t
*)cupsArrayNext(Devices
))
1445 printf("network %s \"%s\" \"%s %s\" \"%s\"\n",
1447 cache
->make_and_model
? cache
->make_and_model
: "Unknown",
1448 cache
->make_and_model
? cache
->make_and_model
: "Unknown",
1449 cache
->addrname
, cache
->id
? cache
->id
: "");
1454 * 'open_snmp_socket()' - Open the SNMP broadcast socket.
1457 static int /* O - SNMP socket file descriptor */
1458 open_snmp_socket(void)
1460 int fd
; /* SNMP socket file descriptor */
1461 int val
; /* Socket option value */
1465 * Create the SNMP socket...
1468 if ((fd
= socket(AF_INET
, SOCK_DGRAM
, 0)) < 0)
1470 fprintf(stderr
, "ERROR: Unable to create SNMP socket - %s\n",
1477 * Set the "broadcast" flag...
1482 if (setsockopt(fd
, SOL_SOCKET
, SO_BROADCAST
, &val
, sizeof(val
)))
1484 fprintf(stderr
, "ERROR: Unable to set broadcast mode - %s\n",
1497 * 'password_cb()' - Handle authentication requests.
1499 * All we do right now is return NULL, indicating that no authentication
1503 static const char * /* O - Password (NULL) */
1504 password_cb(const char *prompt
) /* I - Prompt message */
1506 (void)prompt
; /* Anti-compiler-warning-code */
1513 * 'probe_device()' - Probe a device to discover whether it is a printer.
1515 * TODO: Try using the Port Monitor MIB to discover the correct protocol
1516 * to use - first need a commercially-available printer that supports
1521 probe_device(snmp_cache_t
*device
) /* I - Device */
1523 int i
, j
; /* Looping vars */
1524 http_t
*http
; /* HTTP connection for IPP */
1525 char uri
[1024]; /* Full device URI */
1529 * Try connecting via IPP first...
1532 debug_printf("DEBUG: %.3f Probing %s...\n", run_time(), device
->addrname
);
1534 if ((http
= httpConnect(device
->addrname
, 631)) != NULL
)
1537 * IPP is supported...
1540 ipp_t
*request
, /* IPP request */
1541 *response
; /* IPP response */
1542 ipp_attribute_t
*model
, /* printer-make-and-model attribute */
1543 *info
, /* printer-info attribute */
1544 *supported
; /* printer-uri-supported attribute */
1545 char make_model
[256],/* Make and model string to use */
1546 temp
[256]; /* Temporary make/model string */
1547 int num_uris
; /* Number of good URIs */
1548 static const char * const resources
[] =
1549 { /* Common resource paths for IPP */
1553 "/EPSON_IPP_Printer",
1561 debug_printf("DEBUG: %s supports IPP!\n", device
->addrname
);
1564 * Use non-blocking IO...
1567 httpBlocking(http
, 0);
1570 * Loop through a list of common resources that covers 99% of the
1571 * IPP-capable printers on the market today...
1574 for (i
= 0, num_uris
= 0;
1575 i
< (int)(sizeof(resources
) / sizeof(resources
[0]));
1579 * Don't look past /ipp if we have found a working URI...
1582 if (num_uris
> 0 && strncmp(resources
[i
], "/ipp", 4))
1585 httpAssembleURI(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
1586 device
->addrname
, 631, resources
[i
]);
1588 debug_printf("DEBUG: Trying %s (num_uris=%d)\n", uri
, num_uris
);
1590 request
= ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES
);
1592 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
1595 response
= cupsDoRequest(http
, request
, resources
[i
]);
1597 debug_printf("DEBUG: %s %s (%s)\n", uri
,
1598 ippErrorString(cupsLastError()), cupsLastErrorString());
1600 if (response
&& response
->request
.status
.status_code
== IPP_OK
)
1602 model
= ippFindAttribute(response
, "printer-make-and-model",
1604 info
= ippFindAttribute(response
, "printer-info", IPP_TAG_TEXT
);
1605 supported
= ippFindAttribute(response
, "printer-uri-supported",
1610 fprintf(stderr
, "ERROR: Missing printer-uri-supported from %s!\n",
1617 debug_printf("DEBUG: printer-info=\"%s\"\n",
1618 info
? info
->values
[0].string
.text
: "(null)");
1619 debug_printf("DEBUG: printer-make-and-model=\"%s\"\n",
1620 model
? model
->values
[0].string
.text
: "(null)");
1623 * Don't advertise this port if the printer actually only supports
1624 * a more generic version...
1627 if (!strncmp(resources
[i
], "/ipp/", 5))
1629 for (j
= 0; j
< supported
->num_values
; j
++)
1630 if (strstr(supported
->values
[j
].string
.text
, "/ipp/"))
1633 if (j
>= supported
->num_values
)
1635 ippDelete(response
);
1641 * Don't use the printer-info attribute if it does not contain the
1642 * IEEE-1284 device ID data...
1646 (!strchr(info
->values
[0].string
.text
, ':') ||
1647 !strchr(info
->values
[0].string
.text
, ';')))
1651 * If we don't have a printer-make-and-model string from the printer
1652 * but do have the 1284 device ID string, generate a make-and-model
1653 * string from the device ID info...
1657 strlcpy(temp
, model
->values
[0].string
.text
, sizeof(temp
));
1659 get_make_model(info
->values
[0].string
.text
, temp
, sizeof(temp
));
1661 fix_make_model(make_model
, temp
, sizeof(make_model
));
1664 * Update the current device or add a new printer to the cache...
1668 update_cache(device
, uri
,
1669 info
? info
->values
[0].string
.text
: NULL
,
1670 make_model
[0] ? make_model
: NULL
);
1672 add_cache(&(device
->address
), device
->addrname
, uri
,
1673 info
? info
->values
[0].string
.text
: NULL
,
1674 make_model
[0] ? make_model
: NULL
);
1679 ippDelete(response
);
1681 if (num_uris
> 0 && cupsLastError() != IPP_OK
)
1692 * OK, now try the standard ports...
1695 if (!try_connect(&(device
->address
), device
->addrname
, 9100))
1697 debug_printf("DEBUG: %s supports AppSocket!\n", device
->addrname
);
1699 snprintf(uri
, sizeof(uri
), "socket://%s", device
->addrname
);
1700 update_cache(device
, uri
, NULL
, NULL
);
1702 else if (!try_connect(&(device
->address
), device
->addrname
, 515))
1704 debug_printf("DEBUG: %s supports LPD!\n", device
->addrname
);
1706 snprintf(uri
, sizeof(uri
), "lpd://%s/", device
->addrname
);
1707 update_cache(device
, uri
, NULL
, NULL
);
1713 * 'read_snmp_conf()' - Read the snmp.conf file.
1717 read_snmp_conf(const char *address
) /* I - Single address to probe */
1719 cups_file_t
*fp
; /* File pointer */
1720 char filename
[1024], /* Filename */
1721 line
[1024], /* Line from file */
1722 *value
; /* Value on line */
1723 int linenum
; /* Line number */
1724 const char *cups_serverroot
; /* CUPS_SERVERROOT env var */
1725 const char *debug
; /* CUPS_DEBUG_LEVEL env var */
1729 * Initialize the global address and community lists...
1732 Addresses
= cupsArrayNew(NULL
, NULL
);
1733 Communities
= cupsArrayNew(NULL
, NULL
);
1736 add_array(Addresses
, address
);
1738 if ((debug
= getenv("CUPS_DEBUG_LEVEL")) != NULL
)
1739 DebugLevel
= atoi(debug
);
1742 * Find the snmp.conf file...
1745 if ((cups_serverroot
= getenv("CUPS_SERVERROOT")) == NULL
)
1746 cups_serverroot
= CUPS_SERVERROOT
;
1748 snprintf(filename
, sizeof(filename
), "%s/snmp.conf", cups_serverroot
);
1750 if ((fp
= cupsFileOpen(filename
, "r")) != NULL
)
1753 * Read the snmp.conf file...
1758 while (cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
))
1761 fprintf(stderr
, "ERROR: Missing value on line %d of %s!\n", linenum
,
1763 else if (!strcasecmp(line
, "Address"))
1766 add_array(Addresses
, value
);
1768 else if (!strcasecmp(line
, "Community"))
1769 add_array(Communities
, value
);
1770 else if (!strcasecmp(line
, "DebugLevel"))
1771 DebugLevel
= atoi(value
);
1772 else if (!strcasecmp(line
, "HostNameLookups"))
1773 HostNameLookups
= !strcasecmp(value
, "on") ||
1774 !strcasecmp(value
, "yes") ||
1775 !strcasecmp(value
, "true") ||
1776 !strcasecmp(value
, "double");
1778 fprintf(stderr
, "ERROR: Unknown directive %s on line %d of %s!\n",
1779 line
, linenum
, filename
);
1786 * Use defaults if parameters are undefined...
1789 if (cupsArrayCount(Addresses
) == 0)
1791 fputs("INFO: Using default SNMP Address @LOCAL\n", stderr
);
1792 add_array(Addresses
, "@LOCAL");
1795 if (cupsArrayCount(Communities
) == 0)
1797 fputs("INFO: Using default SNMP Community public\n", stderr
);
1798 add_array(Communities
, "public");
1804 * 'read_snmp_response()' - Read and parse a SNMP response...
1808 read_snmp_response(int fd
) /* I - SNMP socket file descriptor */
1810 unsigned char buffer
[SNMP_MAX_PACKET
];/* Data packet */
1811 int bytes
; /* Number of bytes received */
1812 http_addr_t addr
; /* Source address */
1813 socklen_t addrlen
; /* Source address length */
1814 char addrname
[256]; /* Source address name */
1815 snmp_packet_t packet
; /* Decoded packet */
1816 snmp_cache_t key
, /* Search key */
1817 *device
; /* Matching device */
1821 * Read the response data...
1824 addrlen
= sizeof(addr
);
1826 if ((bytes
= recvfrom(fd
, buffer
, sizeof(buffer
), 0, (void *)&addr
,
1829 fprintf(stderr
, "ERROR: Unable to read data from socket: %s\n",
1834 if (HostNameLookups
)
1835 httpAddrLookup(&addr
, addrname
, sizeof(addrname
));
1837 httpAddrString(&addr
, addrname
, sizeof(addrname
));
1839 debug_printf("DEBUG: %.3f Received %d bytes from %s...\n", run_time(),
1843 * Look for the response status code in the SNMP message header...
1846 if (asn1_decode_snmp(buffer
, bytes
, &packet
))
1848 fprintf(stderr
, "ERROR: Bad SNMP packet from %s: %s\n", addrname
,
1851 asn1_debug(buffer
, bytes
, 0);
1852 hex_debug(buffer
, bytes
);
1857 debug_printf("DEBUG: community=\"%s\"\n", packet
.community
);
1858 debug_printf("DEBUG: request-id=%d\n", packet
.request_id
);
1859 debug_printf("DEBUG: error-status=%d\n", packet
.error_status
);
1862 asn1_debug(buffer
, bytes
, 0);
1865 hex_debug(buffer
, bytes
);
1867 if (packet
.error_status
)
1871 * Find a matching device in the cache...
1875 device
= (snmp_cache_t
*)cupsArrayFind(Devices
, &key
);
1878 * Process the message...
1881 if (packet
.request_id
== DeviceTypeRequest
)
1884 * Got the device type response...
1889 debug_printf("DEBUG: Discarding duplicate device type for \"%s\"...\n",
1895 * Add the device and request the device description...
1898 add_cache(&addr
, addrname
, NULL
, NULL
, NULL
);
1900 send_snmp_query(fd
, &addr
, SNMP_VERSION_1
, packet
.community
,
1901 DeviceDescRequest
, DeviceDescOID
);
1903 else if (packet
.request_id
== DeviceDescRequest
&&
1904 packet
.object_type
== ASN1_OCTET_STRING
)
1907 * Update an existing cache entry...
1910 char make_model
[256]; /* Make and model */
1915 debug_printf("DEBUG: Discarding device description for \"%s\"...\n",
1921 * Convert the description to a make and model string...
1924 if (strchr(packet
.object_value
.string
, ':') &&
1925 strchr(packet
.object_value
.string
, ';'))
1928 * Description is the IEEE-1284 device ID...
1931 get_make_model(packet
.object_value
.string
, make_model
,
1932 sizeof(make_model
));
1937 * Description is plain text...
1940 fix_make_model(make_model
, packet
.object_value
.string
,
1941 sizeof(make_model
));
1944 if (device
->make_and_model
)
1945 free(device
->make_and_model
);
1947 device
->make_and_model
= strdup(make_model
);
1953 * 'run_time()' - Return the total running time...
1956 static double /* O - Number of seconds */
1959 struct timeval curtime
; /* Current time */
1962 gettimeofday(&curtime
, NULL
);
1964 return (curtime
.tv_sec
- StartTime
.tv_sec
+
1965 0.000001 * (curtime
.tv_usec
- StartTime
.tv_usec
));
1970 * 'scan_devices()' - Scan for devices using SNMP.
1974 scan_devices(int fd
) /* I - SNMP socket */
1976 char *address
, /* Current address */
1977 *community
; /* Current community */
1978 fd_set input
; /* Input set for select() */
1979 struct timeval timeout
; /* Timeout for select() */
1980 time_t endtime
; /* End time for scan */
1981 http_addrlist_t
*addrs
, /* List of addresses */
1982 *addr
; /* Current address */
1983 snmp_cache_t
*device
; /* Current device */
1987 * Setup the request IDs...
1990 gettimeofday(&StartTime
, NULL
);
1992 DeviceTypeRequest
= StartTime
.tv_sec
;
1993 DeviceDescRequest
= StartTime
.tv_sec
+ 1;
1996 * First send all of the broadcast queries...
1999 for (address
= (char *)cupsArrayFirst(Addresses
);
2001 address
= (char *)cupsArrayNext(Addresses
))
2003 if (!strcmp(address
, "@LOCAL"))
2004 addrs
= get_interface_addresses(NULL
);
2005 else if (!strncmp(address
, "@IF(", 4))
2007 char ifname
[255]; /* Interface name */
2010 strlcpy(ifname
, address
+ 4, sizeof(ifname
));
2012 ifname
[strlen(ifname
) - 1] = '\0';
2014 addrs
= get_interface_addresses(ifname
);
2017 addrs
= httpAddrGetList(address
, AF_INET
, NULL
);
2021 fprintf(stderr
, "ERROR: Unable to scan \"%s\"!\n", address
);
2025 for (community
= (char *)cupsArrayFirst(Communities
);
2027 community
= (char *)cupsArrayNext(Communities
))
2029 debug_printf("DEBUG: Scanning for devices in \"%s\" via \"%s\"...\n",
2030 community
, address
);
2032 for (addr
= addrs
; addr
; addr
= addr
->next
)
2033 send_snmp_query(fd
, &(addr
->addr
), SNMP_VERSION_1
, community
,
2034 DeviceTypeRequest
, DeviceTypeOID
);
2037 httpAddrFreeList(addrs
);
2041 * Then read any responses that come in over the next 3 seconds...
2044 endtime
= time(NULL
) + 3;
2048 while (time(NULL
) < endtime
)
2051 timeout
.tv_usec
= 0;
2054 if (select(fd
+ 1, &input
, NULL
, NULL
, &timeout
) < 0)
2056 fprintf(stderr
, "ERROR: %.3f select() for %d failed: %s\n", run_time(),
2057 fd
, strerror(errno
));
2061 if (FD_ISSET(fd
, &input
))
2062 read_snmp_response(fd
);
2068 * Finally, probe all of the printers we discovered to see how they are
2072 for (device
= (snmp_cache_t
*)cupsArrayFirst(Devices
);
2074 device
= (snmp_cache_t
*)cupsArrayNext(Devices
))
2076 probe_device(device
);
2078 debug_printf("DEBUG: %.3f Scan complete!\n", run_time());
2083 * 'send_snmp_query()' - Send an SNMP query packet.
2088 int fd
, /* I - SNMP socket */
2089 http_addr_t
*addr
, /* I - Address to send to */
2090 int version
, /* I - SNMP version */
2091 const char *community
, /* I - Community name */
2092 const unsigned request_id
, /* I - Request ID */
2093 const int *oid
) /* I - OID */
2095 int i
; /* Looping var */
2096 snmp_packet_t packet
; /* SNMP message packet */
2097 unsigned char buffer
[SNMP_MAX_PACKET
];/* SNMP message buffer */
2098 int bytes
; /* Size of message */
2099 char addrname
[32]; /* Address name */
2103 * Create the SNMP message...
2106 memset(&packet
, 0, sizeof(packet
));
2108 packet
.version
= version
;
2109 packet
.request_type
= ASN1_GET_REQUEST
;
2110 packet
.request_id
= request_id
;
2111 packet
.object_type
= ASN1_NULL_VALUE
;
2113 strlcpy(packet
.community
, community
, sizeof(packet
.community
));
2115 for (i
= 0; oid
[i
]; i
++)
2116 packet
.object_name
[i
] = oid
[i
];
2118 bytes
= asn1_encode_snmp(buffer
, sizeof(buffer
), &packet
);
2122 fprintf(stderr
, "ERROR: Unable to send SNMP query: %s\n", packet
.error
);
2127 * Send the message...
2130 debug_printf("DEBUG: %.3f Sending %d bytes to %s...\n", run_time(),
2131 bytes
, httpAddrString(addr
, addrname
, sizeof(addrname
)));
2133 asn1_debug(buffer
, bytes
, 0);
2135 hex_debug(buffer
, bytes
);
2137 addr
->ipv4
.sin_port
= htons(SNMP_PORT
);
2139 if (sendto(fd
, buffer
, bytes
, 0, (void *)addr
, sizeof(addr
->ipv4
)) < bytes
)
2140 fprintf(stderr
, "ERROR: Unable to send %d bytes to %s: %s\n",
2141 bytes
, addrname
, strerror(errno
));
2146 * 'try_connect()' - Try connecting on a port...
2149 static int /* O - 0 on success or -1 on error */
2150 try_connect(http_addr_t
*addr
, /* I - Socket address */
2151 const char *addrname
, /* I - Hostname or IP address */
2152 int port
) /* I - Port number */
2154 int fd
; /* Socket */
2155 int status
; /* Connection status */
2158 debug_printf("DEBUG: %.3f Trying %s://%s:%d...\n", run_time(),
2159 port
== 515 ? "lpd" : "socket", addrname
, port
);
2161 if ((fd
= socket(AF_INET
, SOCK_STREAM
, 0)) < 0)
2163 fprintf(stderr
, "ERROR: Unable to create socket: %s\n", strerror(errno
));
2167 addr
->ipv4
.sin_port
= htons(port
);
2169 signal(SIGALRM
, alarm_handler
);
2172 status
= connect(fd
, (void *)addr
, httpAddrLength(addr
));
2182 * 'update_cache()' - Update a cached device...
2186 update_cache(snmp_cache_t
*device
, /* I - Device */
2187 const char *uri
, /* I - Device URI */
2188 const char *id
, /* I - Device ID */
2189 const char *make_model
) /* I - Device make and model */
2194 device
->uri
= strdup(uri
);
2201 device
->id
= strdup(id
);
2206 if (device
->make_and_model
)
2207 free(device
->make_and_model
);
2209 device
->make_and_model
= strdup(make_model
);
2215 * End of "$Id: snmp.c 5479 2006-05-02 15:03:17Z mike $".