]> git.ipfire.org Git - thirdparty/cups.git/blobdiff - cups/snmp.c
Use httpAddrListen and new httpAddrClose in cupsd and other places that need it.
[thirdparty/cups.git] / cups / snmp.c
index 7079878a63454a57aa12217ee432bbf31c99ef55..143563ef4a11e837fcba0988dbafdc9357571a26 100644 (file)
@@ -1,69 +1,28 @@
 /*
  * "$Id$"
  *
- *   SNMP functions for the Common UNIX Printing System (CUPS).
+ * SNMP functions for CUPS.
  *
- *   Copyright 2007-2010 by Apple Inc.
- *   Copyright 2006-2007 by Easy Software Products, all rights reserved.
+ * Copyright 2007-2013 by Apple Inc.
+ * Copyright 2006-2007 by Easy Software Products, all rights reserved.
  *
- *   These coded instructions, statements, and computer programs are the
- *   property of Apple Inc. and are protected by Federal copyright
- *   law.  Distribution and use rights are outlined in the file "LICENSE.txt"
- *   "LICENSE" which should have been included with this file.  If this
- *   file is missing or damaged, see the license at "http://www.cups.org/".
+ * These coded instructions, statements, and computer programs are the
+ * property of Apple Inc. and are protected by Federal copyright
+ * law.  Distribution and use rights are outlined in the file "LICENSE.txt"
+ * "LICENSE" which should have been included with this file.  If this
+ * file is missing or damaged, see the license at "http://www.cups.org/".
  *
- *   This file is subject to the Apple OS-Developed Software exception.
- *
- * Contents:
- *
- *   _cupsSNMPClose()            - Close a SNMP socket.
- *   _cupsSNMPCopyOID()          - Copy an OID.
- *   _cupsSNMPDefaultCommunity() - Get the default SNMP community name.
- *   _cupsSNMPIsOID()            - Test whether a SNMP response contains the
- *                                 specified OID.
- *   _cupsSNMPIsOIDPrefixed()    - Test whether a SNMP response uses the
- *                                 specified OID prefix.
- *   _cupsSNMPOIDToString()      - Convert an OID to a string.
- *   _cupsSNMPOpen()             - Open a SNMP socket.
- *   _cupsSNMPRead()             - Read and parse a SNMP response.
- *   _cupsSNMPSetDebug()         - Enable/disable debug logging to stderr.
- *   _cupsSNMPStringToOID()      - Convert a numeric OID string to an OID array.
- *   _cupsSNMPWalk()             - Enumerate a group of OIDs.
- *   _cupsSNMPWrite()            - Send an SNMP query packet.
- *   asn1_debug()                - Decode an ASN1-encoded message.
- *   asn1_decode_snmp()          - Decode a SNMP packet.
- *   asn1_encode_snmp()          - Encode a SNMP packet.
- *   asn1_get_integer()          - Get an integer value.
- *   asn1_get_length()           - Get a value length.
- *   asn1_get_oid()              - Get an OID value.
- *   asn1_get_packed()           - Get a packed integer value.
- *   asn1_get_string()           - Get a string value.
- *   asn1_get_type()             - Get a value type.
- *   asn1_set_integer()          - Set an integer value.
- *   asn1_set_length()           - Set a value length.
- *   asn1_set_oid()              - Set an OID value.
- *   asn1_set_packed()           - Set a packed integer value.
- *   asn1_size_integer()         - Figure out the number of bytes needed for an
- *                                 integer value.
- *   asn1_size_length()          - Figure out the number of bytes needed for a
- *                                 length value.
- *   asn1_size_oid()             - Figure out the numebr of bytes needed for an
- *                                 OID value.
- *   asn1_size_packed()          - Figure out the number of bytes needed for a
- *                                 packed integer value.
- *   snmp_set_error()            - Set the localized error for a packet.
+ * This file is subject to the Apple OS-Developed Software exception.
  */
 
 /*
  * Include necessary headers.
  */
 
-#include "globals.h"
-#include "debug.h"
+#include "cups-private.h"
 #include "snmp-private.h"
-#include <errno.h>
 #ifdef HAVE_POLL
-#  include <sys/poll.h>
+#  include <poll.h>
 #endif /* HAVE_POLL */
 
 
@@ -118,11 +77,7 @@ _cupsSNMPClose(int fd)                      /* I - SNMP socket file descriptor */
 {
   DEBUG_printf(("4_cupsSNMPClose(fd=%d)", fd));
 
-#ifdef WIN32
-  closesocket(fd);
-#else
-  close(fd);
-#endif /* WIN32 */
+  httpAddrClose(NULL, fd);
 }
 
 
@@ -180,7 +135,7 @@ _cupsSNMPDefaultCommunity(void)
     {
       linenum = 0;
       while (cupsFileGetConf(fp, line, sizeof(line), &value, &linenum))
-       if (!strcasecmp(line, "Community") && value)
+       if (!_cups_strcasecmp(line, "Community") && value)
        {
          strlcpy(cg->snmp_community, value, sizeof(cg->snmp_community));
          break;
@@ -364,11 +319,7 @@ _cupsSNMPOpen(int family)          /* I - Address family - @code AF_INET@ or @code AF_IN
 
   val = 1;
 
-#ifdef WIN32
-  if (setsockopt(fd, SOL_SOCKET, SO_BROADCAST, (char *)&val, sizeof(val)))
-#else
-  if (setsockopt(fd, SOL_SOCKET, SO_BROADCAST, &val, sizeof(val)))
-#endif /* WIN32 */
+  if (setsockopt(fd, SOL_SOCKET, SO_BROADCAST, CUPS_SOCAST &val, sizeof(val)))
   {
     DEBUG_printf(("5_cupsSNMPOpen: Returning -1 (%s)", strerror(errno)));
 
@@ -730,7 +681,7 @@ _cupsSNMPWrite(
   packet.request_type = request_type;
   packet.request_id   = request_id;
   packet.object_type  = CUPS_ASN1_NULL_VALUE;
-  
+
   strlcpy(packet.community, community, sizeof(packet.community));
 
   for (i = 0; oid[i] >= 0 && i < (CUPS_SNMP_MAX_OID - 1); i ++)
@@ -763,12 +714,7 @@ _cupsSNMPWrite(
 
   temp = *address;
 
-#ifdef AF_INET6
-  if (temp.addr.sa_family == AF_INET6)
-    temp.ipv6.sin6_port = htons(CUPS_SNMP_PORT);
-  else
-#endif /* AF_INET6 */
-  temp.ipv4.sin_port = htons(CUPS_SNMP_PORT);
+  _httpAddrSetPort(&temp, CUPS_SNMP_PORT);
 
   return (sendto(fd, buffer, bytes, 0, (void *)&temp,
                  httpAddrLength(&temp)) == bytes);
@@ -970,7 +916,7 @@ asn1_debug(const char    *prefix,   /* I - Prefix string */
     }
   }
 }
-          
+
 
 /*
  * 'asn1_decode_snmp()' - Decode a SNMP packet.
@@ -1092,7 +1038,7 @@ asn1_decode_snmp(unsigned char *buffer,   /* I - Buffer */
                    packet->object_value.string.num_bytes = length;
                    asn1_get_string(&bufptr, bufend, length,
                                    (char *)packet->object_value.string.bytes,
-                                   CUPS_SNMP_MAX_STRING);
+                                   sizeof(packet->object_value.string.bytes));
                    break;
 
                case CUPS_ASN1_OID :
@@ -1432,6 +1378,9 @@ asn1_get_string(
     char          *string,             /* I  - String buffer */
     int           strsize)             /* I  - String buffer size */
 {
+  if (length > (bufend - *buffer))
+    length = bufend - *buffer;
+
   if (length < 0)
   {
    /*