]> git.ipfire.org Git - thirdparty/cups.git/blobdiff - cups/snmp.c
Remove all of the Subversion keywords from various source files.
[thirdparty/cups.git] / cups / snmp.c
index 69a91ac51e57fba351d1c8c1f6a7489575f92a41..fffa2182ba333da1b03ed489c3f304dcb9d4a83f 100644 (file)
@@ -1,69 +1,26 @@
 /*
- * "$Id$"
+ * SNMP functions for CUPS.
  *
- *   SNMP functions for the Common UNIX Printing System (CUPS).
+ * Copyright 2007-2014 by Apple Inc.
+ * Copyright 2006-2007 by Easy Software Products, all rights reserved.
  *
- *   Copyright 2007-2008 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 */
 
 
@@ -79,32 +36,32 @@ static int          asn1_encode_snmp(unsigned char *buffer, size_t len,
                                         cups_snmp_t *packet);
 static int             asn1_get_integer(unsigned char **buffer,
                                         unsigned char *bufend,
-                                        int length);
+                                        unsigned length);
 static int             asn1_get_oid(unsigned char **buffer,
                                     unsigned char *bufend,
-                                    int length, int *oid, int oidsize);
+                                    unsigned length, int *oid, int oidsize);
 static int             asn1_get_packed(unsigned char **buffer,
                                        unsigned char *bufend);
 static char            *asn1_get_string(unsigned char **buffer,
                                         unsigned char *bufend,
-                                        int length, char *string,
-                                        int strsize);
-static int             asn1_get_length(unsigned char **buffer,
+                                        unsigned length, char *string,
+                                        size_t strsize);
+static unsigned                asn1_get_length(unsigned char **buffer,
                                        unsigned char *bufend);
 static int             asn1_get_type(unsigned char **buffer,
                                      unsigned char *bufend);
 static void            asn1_set_integer(unsigned char **buffer,
                                         int integer);
 static void            asn1_set_length(unsigned char **buffer,
-                                       int length);
+                                       unsigned length);
 static void            asn1_set_oid(unsigned char **buffer,
                                     const int *oid);
 static void            asn1_set_packed(unsigned char **buffer,
                                        int integer);
-static int             asn1_size_integer(int integer);
-static int             asn1_size_length(int length);
-static int             asn1_size_oid(const int *oid);
-static int             asn1_size_packed(int integer);
+static unsigned                asn1_size_integer(int integer);
+static unsigned                asn1_size_length(unsigned length);
+static unsigned                asn1_size_oid(const int *oid);
+static unsigned                asn1_size_packed(int integer);
 static void            snmp_set_error(cups_snmp_t *packet,
                                       const char *message);
 
@@ -116,13 +73,9 @@ static void         snmp_set_error(cups_snmp_t *packet,
 void
 _cupsSNMPClose(int fd)                 /* I - SNMP socket file descriptor */
 {
-  DEBUG_printf(("_cupsSNMPClose(fd=%d)\n", fd));
+  DEBUG_printf(("4_cupsSNMPClose(fd=%d)", fd));
 
-#ifdef WIN32
-  closesocket(fd);
-#else
-  close(fd);
-#endif /* WIN32 */
+  httpAddrClose(NULL, fd);
 }
 
 
@@ -140,7 +93,7 @@ _cupsSNMPCopyOID(int       *dst,     /* I - Destination OID */
   int  i;                              /* Looping var */
 
 
-  DEBUG_printf(("_cupsSNMPCopyOID(dst=%p, src=%p, dstsize=%d)\n", dst, src,
+  DEBUG_printf(("4_cupsSNMPCopyOID(dst=%p, src=%p, dstsize=%d)", dst, src,
                 dstsize));
 
   for (i = 0, dstsize --; src[i] >= 0 && i < dstsize; i ++)
@@ -169,7 +122,7 @@ _cupsSNMPDefaultCommunity(void)
   _cups_globals_t *cg = _cupsGlobals();        /* Global data */
 
 
-  DEBUG_puts("_cupsSNMPDefaultCommunity()");
+  DEBUG_puts("4_cupsSNMPDefaultCommunity()");
 
   if (!cg->snmp_community[0])
   {
@@ -180,7 +133,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;
@@ -190,7 +143,7 @@ _cupsSNMPDefaultCommunity(void)
     }
   }
 
-  DEBUG_printf(("_cupsSNMPDefaultCommunity: Returning \"%s\"\n",
+  DEBUG_printf(("5_cupsSNMPDefaultCommunity: Returning \"%s\"",
                 cg->snmp_community));
 
   return (cg->snmp_community);
@@ -214,11 +167,11 @@ _cupsSNMPIsOID(cups_snmp_t *packet,       /* I - Response packet */
   * Range check input...
   */
 
-  DEBUG_printf(("_cupsSNMPIsOID(packet=%p, oid=%p)\n", packet, oid));
+  DEBUG_printf(("4_cupsSNMPIsOID(packet=%p, oid=%p)", packet, oid));
 
   if (!packet || !oid)
   {
-    DEBUG_puts("_cupsSNMPIsOID: Returning 0");
+    DEBUG_puts("5_cupsSNMPIsOID: Returning 0");
 
     return (0);
   }
@@ -232,12 +185,12 @@ _cupsSNMPIsOID(cups_snmp_t *packet,       /* I - Response packet */
        i ++)
     if (oid[i] != packet->object_name[i])
     {
-      DEBUG_puts("_cupsSNMPIsOID: Returning 0");
+      DEBUG_puts("5_cupsSNMPIsOID: Returning 0");
 
       return (0);
     }
 
-  DEBUG_printf(("_cupsSNMPIsOID: Returning %d\n",
+  DEBUG_printf(("5_cupsSNMPIsOID: Returning %d",
                 i < CUPS_SNMP_MAX_OID && oid[i] == packet->object_name[i]));
 
   return (i < CUPS_SNMP_MAX_OID && oid[i] == packet->object_name[i]);
@@ -263,12 +216,12 @@ _cupsSNMPIsOIDPrefixed(
   * Range check input...
   */
 
-  DEBUG_printf(("_cupsSNMPIsOIDPrefixed(packet=%p, prefix=%p)\n", packet,
+  DEBUG_printf(("4_cupsSNMPIsOIDPrefixed(packet=%p, prefix=%p)", packet,
                 prefix));
 
   if (!packet || !prefix)
   {
-    DEBUG_puts("_cupsSNMPIsOIDPrefixed: Returning 0");
+    DEBUG_puts("5_cupsSNMPIsOIDPrefixed: Returning 0");
 
     return (0);
   }
@@ -282,12 +235,12 @@ _cupsSNMPIsOIDPrefixed(
        i ++)
     if (prefix[i] != packet->object_name[i])
     {
-      DEBUG_puts("_cupsSNMPIsOIDPrefixed: Returning 0");
+      DEBUG_puts("5_cupsSNMPIsOIDPrefixed: Returning 0");
 
       return (0);
     }
 
-  DEBUG_printf(("_cupsSNMPIsOIDPrefixed: Returning %d\n",
+  DEBUG_printf(("5_cupsSNMPIsOIDPrefixed: Returning %d",
                 i < CUPS_SNMP_MAX_OID));
 
   return (i < CUPS_SNMP_MAX_OID);
@@ -308,7 +261,7 @@ _cupsSNMPOIDToString(const int *src,        /* I - OID */
        *dstend;                        /* End of string buffer */
 
 
-  DEBUG_printf(("_cupsSNMPOIDToString(src=%p, dst=%p, dstsize=" CUPS_LLFMT ")\n",
+  DEBUG_printf(("4_cupsSNMPOIDToString(src=%p, dst=%p, dstsize=" CUPS_LLFMT ")",
                 src, dst, CUPS_LLCAST dstsize));
 
  /*
@@ -325,7 +278,7 @@ _cupsSNMPOIDToString(const int *src,        /* I - OID */
   for (dstptr = dst, dstend = dstptr + dstsize - 1;
        *src >= 0 && dstptr < dstend;
        src ++, dstptr += strlen(dstptr))
-    snprintf(dstptr, dstend - dstptr + 1, ".%d", *src);
+    snprintf(dstptr, (size_t)(dstend - dstptr + 1), ".%d", *src);
 
   if (*src >= 0)
     return (NULL);
@@ -349,11 +302,11 @@ _cupsSNMPOpen(int family)         /* I - Address family - @code AF_INET@ or @code AF_IN
   * Create the SNMP socket...
   */
 
-  DEBUG_printf(("_cupsSNMPOpen(family=%d)\n", family));
+  DEBUG_printf(("4_cupsSNMPOpen(family=%d)", family));
 
   if ((fd = socket(family, SOCK_DGRAM, 0)) < 0)
   {
-    DEBUG_printf(("_cupsSNMPOpen: Returning -1 (%s)\n", strerror(errno)));
+    DEBUG_printf(("5_cupsSNMPOpen: Returning -1 (%s)", strerror(errno)));
 
     return (-1);
   }
@@ -364,16 +317,16 @@ _cupsSNMPOpen(int family)         /* I - Address family - @code AF_INET@ or @code AF_IN
 
   val = 1;
 
-  if (setsockopt(fd, SOL_SOCKET, SO_BROADCAST, &val, sizeof(val)))
+  if (setsockopt(fd, SOL_SOCKET, SO_BROADCAST, CUPS_SOCAST &val, sizeof(val)))
   {
-    DEBUG_printf(("_cupsSNMPOpen: Returning -1 (%s)\n", strerror(errno)));
+    DEBUG_printf(("5_cupsSNMPOpen: Returning -1 (%s)", strerror(errno)));
 
     close(fd);
 
     return (-1);
   }
 
-  DEBUG_printf(("_cupsSNMPOpen: Returning %d\n", fd));
+  DEBUG_printf(("5_cupsSNMPOpen: Returning %d", fd));
 
   return (fd);
 }
@@ -393,7 +346,7 @@ _cupsSNMPRead(int         fd,               /* I - SNMP socket file descriptor */
 {
   unsigned char        buffer[CUPS_SNMP_MAX_PACKET];
                                        /* Data packet */
-  int          bytes;                  /* Number of bytes received */
+  ssize_t      bytes;                  /* Number of bytes received */
   socklen_t    addrlen;                /* Source address length */
   http_addr_t  address;                /* Source address */
 
@@ -402,12 +355,12 @@ _cupsSNMPRead(int         fd,             /* I - SNMP socket file descriptor */
   * Range check input...
   */
 
-  DEBUG_printf(("_cupsSNMPRead(fd=%d, packet=%p, timeout=%.1f)\n", fd, packet,
+  DEBUG_printf(("4_cupsSNMPRead(fd=%d, packet=%p, timeout=%.1f)", fd, packet,
                 timeout));
 
   if (fd < 0 || !packet)
   {
-    DEBUG_puts("_cupsSNMPRead: Returning NULL");
+    DEBUG_puts("5_cupsSNMPRead: Returning NULL");
 
     return (NULL);
   }
@@ -426,7 +379,7 @@ _cupsSNMPRead(int         fd,               /* I - SNMP socket file descriptor */
     pfd.events = POLLIN;
 
     while ((ready = poll(&pfd, 1, (int)(timeout * 1000.0))) < 0 &&
-           errno == EINTR);
+           (errno == EINTR || errno == EAGAIN));
 
 #else
     fd_set             input_set;      /* select() input set */
@@ -438,14 +391,14 @@ _cupsSNMPRead(int         fd,             /* I - SNMP socket file descriptor */
       FD_SET(fd, &input_set);
 
       stimeout.tv_sec  = (int)timeout;
-      stimeout.tv_usec = (int)((timeout - stimeout.tv_usec) * 1000000);
+      stimeout.tv_usec = (int)((timeout - stimeout.tv_sec) * 1000000);
 
       ready = select(fd + 1, &input_set, NULL, NULL, &stimeout);
     }
 #  ifdef WIN32
     while (ready < 0 && WSAGetLastError() == WSAEINTR);
 #  else
-    while (ready < 0 && errno == EINTR);
+    while (ready < 0 && (errno == EINTR || errno == EAGAIN));
 #  endif /* WIN32 */
 #endif /* HAVE_POLL */
 
@@ -455,7 +408,7 @@ _cupsSNMPRead(int         fd,               /* I - SNMP socket file descriptor */
 
     if (ready <= 0)
     {
-      DEBUG_puts("_cupsSNMPRead: Returning NULL (timeout)");
+      DEBUG_puts("5_cupsSNMPRead: Returning NULL (timeout)");
 
       return (NULL);
     }
@@ -470,7 +423,7 @@ _cupsSNMPRead(int         fd,               /* I - SNMP socket file descriptor */
   if ((bytes = recvfrom(fd, buffer, sizeof(buffer), 0, (void *)&address,
                         &addrlen)) < 0)
   {
-    DEBUG_printf(("_cupsSNMPRead: Returning NULL (%s)\n", strerror(errno)));
+    DEBUG_printf(("5_cupsSNMPRead: Returning NULL (%s)", strerror(errno)));
 
     return (NULL);
   }
@@ -479,9 +432,9 @@ _cupsSNMPRead(int         fd,               /* I - SNMP socket file descriptor */
   * Look for the response status code in the SNMP message header...
   */
 
-  asn1_debug("DEBUG: IN ", buffer, bytes, 0);
+  asn1_debug("DEBUG: IN ", buffer, (size_t)bytes, 0);
 
-  asn1_decode_snmp(buffer, bytes, packet);
+  asn1_decode_snmp(buffer, (size_t)bytes, packet);
 
   memcpy(&(packet->address), &address, sizeof(packet->address));
 
@@ -489,7 +442,7 @@ _cupsSNMPRead(int         fd,               /* I - SNMP socket file descriptor */
   * Return decoded data packet...
   */
 
-  DEBUG_puts("_cupsSNMPRead: Returning packet");
+  DEBUG_puts("5_cupsSNMPRead: Returning packet");
 
   return (packet);
 }
@@ -505,7 +458,7 @@ _cupsSNMPSetDebug(int level)                /* I - 1 to enable debug output, 0 otherwise */
   _cups_globals_t *cg = _cupsGlobals();        /* Global data */
 
 
-  DEBUG_printf(("_cupsSNMPSetDebug(level=%d)\n", level));
+  DEBUG_printf(("4_cupsSNMPSetDebug(level=%d)", level));
 
   cg->snmp_debug = level;
 }
@@ -530,7 +483,7 @@ _cupsSNMPStringToOID(const char *src,       /* I - OID string */
        *dstend;                        /* End of OID array */
 
 
-  DEBUG_printf(("_cupsSNMPStringToOID(src=\"%s\", dst=%p, dstsize=%d)\n",
+  DEBUG_printf(("4_cupsSNMPStringToOID(src=\"%s\", dst=%p, dstsize=%d)",
                 src, dst, dstsize));
 
  /*
@@ -602,23 +555,24 @@ _cupsSNMPWalk(int            fd,  /* I - SNMP socket */
              void           *data)     /* I - User data pointer that is passed to the callback function */
 {
   int          count = 0;              /* Number of OIDs found */
-  int          request_id = 0;         /* Current request ID */
+  unsigned     request_id = 0;         /* Current request ID */
   cups_snmp_t  packet;                 /* Current response packet */
+  int          lastoid[CUPS_SNMP_MAX_OID];
+                                       /* Last OID we got */
 
 
  /*
   * Range check input...
   */
 
-  DEBUG_printf(("_cupsSNMPWalk(fd=%d, address=%p, version=%d, "
-                "community=\"%s\", prefix=%p, timeout=%.1f, cb=%p, data=%p)\n",
-               fd, address, version, community ? community : "(null)",
-               prefix, timeout, cb, data));
+  DEBUG_printf(("4_cupsSNMPWalk(fd=%d, address=%p, version=%d, "
+                "community=\"%s\", prefix=%p, timeout=%.1f, cb=%p, data=%p)",
+               fd, address, version, community, prefix, timeout, cb, data));
 
   if (fd < 0 || !address || version != CUPS_SNMP_VERSION_1 || !community ||
       !prefix || !cb)
   {
-    DEBUG_puts("_cupsSNMPWalk: Returning -1");
+    DEBUG_puts("5_cupsSNMPWalk: Returning -1");
 
     return (-1);
   }
@@ -628,41 +582,45 @@ _cupsSNMPWalk(int            fd,  /* I - SNMP socket */
   */
 
   _cupsSNMPCopyOID(packet.object_name, prefix, CUPS_SNMP_MAX_OID);
+  lastoid[0] = -1;
 
   for (;;)
   {
     request_id ++;
 
     if (!_cupsSNMPWrite(fd, address, version, community,
-                       CUPS_ASN1_GET_NEXT_REQUEST, request_id,
-                      packet.object_name))
+                        CUPS_ASN1_GET_NEXT_REQUEST, request_id,
+                       packet.object_name))
     {
-      DEBUG_puts("_cupsSNMPWalk: Returning -1");
+      DEBUG_puts("5_cupsSNMPWalk: Returning -1");
 
       return (-1);
     }
 
     if (!_cupsSNMPRead(fd, &packet, timeout))
     {
-      DEBUG_puts("_cupsSNMPWalk: Returning -1");
+      DEBUG_puts("5_cupsSNMPWalk: Returning -1");
 
       return (-1);
     }
 
-    if (!_cupsSNMPIsOIDPrefixed(&packet, prefix))
+    if (!_cupsSNMPIsOIDPrefixed(&packet, prefix) ||
+        _cupsSNMPIsOID(&packet, lastoid))
     {
-      DEBUG_printf(("_cupsSNMPWalk: Returning %d\n", count));
+      DEBUG_printf(("5_cupsSNMPWalk: Returning %d", count));
 
       return (count);
     }
 
     if (packet.error || packet.error_status)
     {
-      DEBUG_printf(("_cupsSNMPWalk: Returning %d\n", count > 0 ? count : -1));
+      DEBUG_printf(("5_cupsSNMPWalk: Returning %d", count > 0 ? count : -1));
 
       return (count > 0 ? count : -1);
     }
 
+    _cupsSNMPCopyOID(lastoid, packet.object_name, CUPS_SNMP_MAX_OID);
+
     count ++;
 
     (*cb)(&packet, data);
@@ -690,7 +648,7 @@ _cupsSNMPWrite(
   cups_snmp_t  packet;                 /* SNMP message packet */
   unsigned char        buffer[CUPS_SNMP_MAX_PACKET];
                                        /* SNMP message buffer */
-  int          bytes;                  /* Size of message */
+  ssize_t      bytes;                  /* Size of message */
   http_addr_t  temp;                   /* Copy of address */
 
 
@@ -698,16 +656,15 @@ _cupsSNMPWrite(
   * Range check input...
   */
 
-  DEBUG_printf(("_cupsSNMPWrite(fd=%d, address=%p, version=%d, "
-                "community=\"%s\", request_type=%d, request_id=%u, oid=%p)\n",
-               fd, address, version, community ? community : "(null)",
-               request_type, request_id, oid));
+  DEBUG_printf(("4_cupsSNMPWrite(fd=%d, address=%p, version=%d, "
+                "community=\"%s\", request_type=%d, request_id=%u, oid=%p)",
+               fd, address, version, community, request_type, request_id, oid));
 
   if (fd < 0 || !address || version != CUPS_SNMP_VERSION_1 || !community ||
       (request_type != CUPS_ASN1_GET_REQUEST &&
        request_type != CUPS_ASN1_GET_NEXT_REQUEST) || request_id < 1 || !oid)
   {
-    DEBUG_puts("_cupsSNMPWrite: Returning 0 (bad arguments)");
+    DEBUG_puts("5_cupsSNMPWrite: Returning 0 (bad arguments)");
 
     return (0);
   }
@@ -722,7 +679,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 ++)
@@ -731,7 +688,7 @@ _cupsSNMPWrite(
 
   if (oid[i] >= 0)
   {
-    DEBUG_puts("_cupsSNMPWrite: Returning 0 (OID too big)");
+    DEBUG_puts("5_cupsSNMPWrite: Returning 0 (OID too big)");
 
     errno = E2BIG;
     return (0);
@@ -741,13 +698,13 @@ _cupsSNMPWrite(
 
   if (bytes < 0)
   {
-    DEBUG_puts("_cupsSNMPWrite: Returning 0 (request too big)");
+    DEBUG_puts("5_cupsSNMPWrite: Returning 0 (request too big)");
 
     errno = E2BIG;
     return (0);
   }
 
-  asn1_debug("DEBUG: OUT ", buffer, bytes, 0);
+  asn1_debug("DEBUG: OUT ", buffer, (size_t)bytes, 0);
 
  /*
   * Send the message...
@@ -755,15 +712,9 @@ _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);
+  return (sendto(fd, buffer, (size_t)bytes, 0, (void *)&temp, (socklen_t)httpAddrLength(&temp)) == bytes);
 }
 
 
@@ -777,14 +728,14 @@ asn1_debug(const char    *prefix, /* I - Prefix string */
            size_t        len,          /* I - Length of buffer */
            int           indent)       /* I - Indentation */
 {
-  int          i;                      /* Looping var */
+  size_t       i;                      /* Looping var */
   unsigned char        *bufend;                /* End of buffer */
   int          integer;                /* Number value */
   int          oid[CUPS_SNMP_MAX_OID]; /* OID value */
   char         string[CUPS_SNMP_MAX_STRING];
                                        /* String value */
   unsigned char        value_type;             /* Type of value */
-  int          value_length;           /* Length of value */
+  unsigned     value_length;           /* Length of value */
   _cups_globals_t *cg = _cupsGlobals();        /* Global data */
 
 
@@ -797,13 +748,13 @@ asn1_debug(const char    *prefix, /* I - Prefix string */
     * Do a hex dump of the packet...
     */
 
-    int j;
+    size_t j;
 
     fprintf(stderr, "%sHex Dump (%d bytes):\n", prefix, (int)len);
 
     for (i = 0; i < len; i += 16)
     {
-      fprintf(stderr, "%s%04x:", prefix, i);
+      fprintf(stderr, "%s%04x:", prefix, (unsigned)i);
 
       for (j = 0; j < 16 && (i + j) < len; j ++)
       {
@@ -846,7 +797,7 @@ asn1_debug(const char    *prefix,   /* I - Prefix string */
     * Get value type...
     */
 
-    value_type   = asn1_get_type(&buffer, bufend);
+    value_type   = (unsigned char)asn1_get_type(&buffer, bufend);
     value_length = asn1_get_length(&buffer, bufend);
 
     switch (value_type)
@@ -916,7 +867,7 @@ asn1_debug(const char    *prefix,   /* I - Prefix string */
 
           fprintf(stderr, "%s%*sOID %d bytes ", prefix, indent, "",
                  value_length);
-         for (i = 0; i < integer; i ++)
+         for (i = 0; i < (unsigned)integer; i ++)
            fprintf(stderr, ".%d", oid[i]);
          putc('\n', stderr);
           break;
@@ -962,7 +913,7 @@ asn1_debug(const char    *prefix,   /* I - Prefix string */
     }
   }
 }
-          
+
 
 /*
  * 'asn1_decode_snmp()' - Decode a SNMP packet.
@@ -975,7 +926,7 @@ asn1_decode_snmp(unsigned char *buffer,     /* I - Buffer */
 {
   unsigned char        *bufptr,                /* Pointer into the data */
                *bufend;                /* End of data */
-  int          length;                 /* Length of value */
+  unsigned     length;                 /* Length of value */
 
 
  /*
@@ -1008,7 +959,7 @@ asn1_decode_snmp(unsigned char *buffer,    /* I - Buffer */
     asn1_get_string(&bufptr, bufend, length, packet->community,
                     sizeof(packet->community));
 
-    if ((packet->request_type = asn1_get_type(&bufptr, bufend))
+    if ((packet->request_type = (cups_asn1_t)asn1_get_type(&bufptr, bufend))
             != CUPS_ASN1_GET_RESPONSE)
       snmp_set_error(packet, _("Packet does not contain a Get-Response-PDU"));
     else if (asn1_get_length(&bufptr, bufend) == 0)
@@ -1019,7 +970,7 @@ asn1_decode_snmp(unsigned char *buffer,    /* I - Buffer */
       snmp_set_error(packet, _("request-id uses indefinite length"));
     else
     {
-      packet->request_id = asn1_get_integer(&bufptr, bufend, length);
+      packet->request_id = (unsigned)asn1_get_integer(&bufptr, bufend, length);
 
       if (asn1_get_type(&bufptr, bufend) != CUPS_ASN1_INTEGER)
        snmp_set_error(packet, _("No error-status"));
@@ -1055,7 +1006,7 @@ asn1_decode_snmp(unsigned char *buffer,   /* I - Buffer */
            asn1_get_oid(&bufptr, bufend, length, packet->object_name,
                         CUPS_SNMP_MAX_OID);
 
-            packet->object_type = asn1_get_type(&bufptr, bufend);
+            packet->object_type = (cups_asn1_t)asn1_get_type(&bufptr, bufend);
 
            if ((length = asn1_get_length(&bufptr, bufend)) == 0 &&
                packet->object_type != CUPS_ASN1_NULL_VALUE &&
@@ -1079,9 +1030,12 @@ asn1_decode_snmp(unsigned char *buffer,  /* I - Buffer */
                    break;
 
                case CUPS_ASN1_OCTET_STRING :
+               case CUPS_ASN1_BIT_STRING :
+               case CUPS_ASN1_HEX_STRING :
+                   packet->object_value.string.num_bytes = length;
                    asn1_get_string(&bufptr, bufend, length,
-                                   packet->object_value.string,
-                                   CUPS_SNMP_MAX_STRING);
+                                   (char *)packet->object_value.string.bytes,
+                                   sizeof(packet->object_value.string.bytes));
                    break;
 
                case CUPS_ASN1_OID :
@@ -1089,14 +1043,6 @@ asn1_decode_snmp(unsigned char *buffer,  /* I - Buffer */
                                 packet->object_value.oid, CUPS_SNMP_MAX_OID);
                    break;
 
-               case CUPS_ASN1_HEX_STRING :
-                   packet->object_value.hex_string.num_bytes = length;
-
-                   asn1_get_string(&bufptr, bufend, length,
-                                   (char *)packet->object_value.hex_string.bytes,
-                                   CUPS_SNMP_MAX_STRING);
-                   break;
-
                case CUPS_ASN1_COUNTER :
                    packet->object_value.counter =
                        asn1_get_integer(&bufptr, bufend, length);
@@ -1104,12 +1050,12 @@ asn1_decode_snmp(unsigned char *buffer, /* I - Buffer */
 
                case CUPS_ASN1_GAUGE :
                    packet->object_value.gauge =
-                       asn1_get_integer(&bufptr, bufend, length);
+                       (unsigned)asn1_get_integer(&bufptr, bufend, length);
                    break;
 
                case CUPS_ASN1_TIMETICKS :
                    packet->object_value.timeticks =
-                       asn1_get_integer(&bufptr, bufend, length);
+                       (unsigned)asn1_get_integer(&bufptr, bufend, length);
                    break;
 
                 default :
@@ -1137,7 +1083,7 @@ asn1_encode_snmp(unsigned char *buffer,   /* I - Buffer */
                  cups_snmp_t   *packet)        /* I - SNMP packet */
 {
   unsigned char        *bufptr;                /* Pointer into buffer */
-  int          total,                  /* Total length */
+  unsigned     total,                  /* Total length */
                msglen,                 /* Length of entire message */
                commlen,                /* Length of community string */
                reqlen,                 /* Length of request */
@@ -1169,7 +1115,7 @@ asn1_encode_snmp(unsigned char *buffer,   /* I - Buffer */
        break;
 
     case CUPS_ASN1_OCTET_STRING :
-        valuelen = strlen(packet->object_value.string);
+        valuelen = packet->object_value.string.num_bytes;
        break;
 
     case CUPS_ASN1_OID :
@@ -1184,11 +1130,11 @@ asn1_encode_snmp(unsigned char *buffer, /* I - Buffer */
   varlen  = 1 + asn1_size_length(namelen) + namelen +
             1 + asn1_size_length(valuelen) + valuelen;
   listlen = 1 + asn1_size_length(varlen) + varlen;
-  reqlen  = 2 + asn1_size_integer(packet->request_id) +
+  reqlen  = 2 + asn1_size_integer((int)packet->request_id) +
             2 + asn1_size_integer(packet->error_status) +
             2 + asn1_size_integer(packet->error_index) +
             1 + asn1_size_length(listlen) + listlen;
-  commlen = strlen(packet->community);
+  commlen = (unsigned)strlen(packet->community);
   msglen  = 2 + asn1_size_integer(packet->version) +
             1 + asn1_size_length(commlen) + commlen +
            1 + asn1_size_length(reqlen) + reqlen;
@@ -1220,7 +1166,7 @@ asn1_encode_snmp(unsigned char *buffer,   /* I - Buffer */
   *bufptr++ = packet->request_type;    /* Get-Request-PDU/Get-Next-Request-PDU */
   asn1_set_length(&bufptr, reqlen);
 
-  asn1_set_integer(&bufptr, packet->request_id);
+  asn1_set_integer(&bufptr, (int)packet->request_id);
 
   asn1_set_integer(&bufptr, packet->error_status);
 
@@ -1254,7 +1200,7 @@ asn1_encode_snmp(unsigned char *buffer,   /* I - Buffer */
     case CUPS_ASN1_OCTET_STRING :
         *bufptr++ = CUPS_ASN1_OCTET_STRING;
        asn1_set_length(&bufptr, valuelen);
-       memcpy(bufptr, packet->object_value.string, valuelen);
+       memcpy(bufptr, packet->object_value.string.bytes, valuelen);
        bufptr += valuelen;
        break;
 
@@ -1266,7 +1212,7 @@ asn1_encode_snmp(unsigned char *buffer,   /* I - Buffer */
         break;
   }
 
-  return (bufptr - buffer);
+  return ((int)(bufptr - buffer));
 }
 
 
@@ -1278,12 +1224,18 @@ static int                              /* O  - Integer value */
 asn1_get_integer(
     unsigned char **buffer,            /* IO - Pointer in buffer */
     unsigned char *bufend,             /* I  - End of buffer */
-    int           length)              /* I  - Length of value */
+    unsigned      length)              /* I  - Length of value */
 {
   int  value;                          /* Integer value */
 
 
-  for (value = 0;
+  if (length > sizeof(int))
+  {
+    (*buffer) += length;
+    return (0);
+  }
+
+  for (value = (**buffer & 0x80) ? -1 : 0;
        length > 0 && *buffer < bufend;
        length --, (*buffer) ++)
     value = (value << 8) | **buffer;
@@ -1296,18 +1248,32 @@ asn1_get_integer(
  * 'asn1_get_length()' - Get a value length.
  */
 
-static int                             /* O  - Length */
+static unsigned                                /* O  - Length */
 asn1_get_length(unsigned char **buffer,        /* IO - Pointer in buffer */
                unsigned char *bufend)  /* I  - End of buffer */
 {
-  int  length;                         /* Length */
+  unsigned     length;                 /* Length */
 
 
   length = **buffer;
   (*buffer) ++;
 
   if (length & 128)
-    length = asn1_get_integer(buffer, bufend, length & 127);
+  {
+    int        count;                          /* Number of bytes for length */
+
+
+    if ((count = length & 127) > sizeof(unsigned))
+    {
+      (*buffer) += count;
+      return (0);
+    }
+
+    for (length = 0;
+        count > 0 && *buffer < bufend;
+        count --, (*buffer) ++)
+      length = (length << 8) | **buffer;
+  }
 
   return (length);
 }
@@ -1321,7 +1287,7 @@ static int                                /* O  - Number of OIDs */
 asn1_get_oid(
     unsigned char **buffer,            /* IO - Pointer in buffer */
     unsigned char *bufend,             /* I  - End of buffer */
-    int           length,              /* I  - Length of value */
+    unsigned      length,              /* I  - Length of value */
     int           *oid,                        /* I  - OID buffer */
     int           oidsize)             /* I  - Size of OID buffer */
 {
@@ -1363,7 +1329,7 @@ asn1_get_oid(
 
   *oidptr = -1;
 
-  return (oidptr - oid);
+  return ((int)(oidptr - oid));
 }
 
 
@@ -1405,19 +1371,14 @@ static char *                           /* O  - String */
 asn1_get_string(
     unsigned char **buffer,            /* IO - Pointer in buffer */
     unsigned char *bufend,             /* I  - End of buffer */
-    int           length,              /* I  - Value length */
+    unsigned      length,              /* I  - Value length */
     char          *string,             /* I  - String buffer */
-    int           strsize)             /* I  - String buffer size */
+    size_t        strsize)             /* I  - String buffer size */
 {
-  if (length < 0)
-  {
-   /*
-    * Disallow negative lengths!
-    */
+  if (length > (unsigned)(bufend - *buffer))
+    length = (unsigned)(bufend - *buffer);
 
-    *string = '\0';
-  }
-  else if (length < strsize)
+  if (length < strsize)
   {
    /*
     * String is smaller than the buffer...
@@ -1441,7 +1402,7 @@ asn1_get_string(
   if (length > 0)
     (*buffer) += length;
 
-  return (length < 0 ? NULL : string);
+  return (string);
 }
 
 
@@ -1481,40 +1442,40 @@ asn1_set_integer(unsigned char **buffer,/* IO - Pointer in buffer */
   {
     **buffer = 4;
     (*buffer) ++;
-    **buffer = integer >> 24;
+    **buffer = (unsigned char)(integer >> 24);
     (*buffer) ++;
-    **buffer = integer >> 16;
+    **buffer = (unsigned char)(integer >> 16);
     (*buffer) ++;
-    **buffer = integer >> 8;
+    **buffer = (unsigned char)(integer >> 8);
     (*buffer) ++;
-    **buffer = integer;
+    **buffer = (unsigned char)integer;
     (*buffer) ++;
   }
   else if (integer > 0x7fff || integer < -0x8000)
   {
     **buffer = 3;
     (*buffer) ++;
-    **buffer = integer >> 16;
+    **buffer = (unsigned char)(integer >> 16);
     (*buffer) ++;
-    **buffer = integer >> 8;
+    **buffer = (unsigned char)(integer >> 8);
     (*buffer) ++;
-    **buffer = integer;
+    **buffer = (unsigned char)integer;
     (*buffer) ++;
   }
   else if (integer > 0x7f || integer < -0x80)
   {
     **buffer = 2;
     (*buffer) ++;
-    **buffer = integer >> 8;
+    **buffer = (unsigned char)(integer >> 8);
     (*buffer) ++;
-    **buffer = integer;
+    **buffer = (unsigned char)integer;
     (*buffer) ++;
   }
   else
   {
     **buffer = 1;
     (*buffer) ++;
-    **buffer = integer;
+    **buffer = (unsigned char)integer;
     (*buffer) ++;
   }
 }
@@ -1526,27 +1487,27 @@ asn1_set_integer(unsigned char **buffer,/* IO - Pointer in buffer */
 
 static void
 asn1_set_length(unsigned char **buffer,        /* IO - Pointer in buffer */
-               int           length)   /* I  - Length value */
+               unsigned      length)   /* I  - Length value */
 {
   if (length > 255)
   {
     **buffer = 0x82;                   /* 2-byte length */
     (*buffer) ++;
-    **buffer = length >> 8;
+    **buffer = (unsigned char)(length >> 8);
     (*buffer) ++;
-    **buffer = length;
+    **buffer = (unsigned char)length;
     (*buffer) ++;
   }
   else if (length > 127)
   {
     **buffer = 0x81;                   /* 1-byte length */
     (*buffer) ++;
-    **buffer = length;
+    **buffer = (unsigned char)length;
     (*buffer) ++;
   }
   else
   {
-    **buffer = length;                 /* Length */
+    **buffer = (unsigned char)length;  /* Length */
     (*buffer) ++;
   }
 }
@@ -1620,7 +1581,7 @@ asn1_set_packed(unsigned char **buffer,   /* IO - Pointer in buffer */
  *                         integer value.
  */
 
-static int                             /* O - Size in bytes */
+static unsigned                                /* O - Size in bytes */
 asn1_size_integer(int integer)         /* I - Integer value */
 {
   if (integer > 0x7fffff || integer < -0x800000)
@@ -1639,8 +1600,8 @@ asn1_size_integer(int integer)            /* I - Integer value */
  *                        length value.
  */
 
-static int                             /* O - Size in bytes */
-asn1_size_length(int length)           /* I - Length value */
+static unsigned                                /* O - Size in bytes */
+asn1_size_length(unsigned length)      /* I - Length value */
 {
   if (length > 0xff)
     return (3);
@@ -1656,10 +1617,10 @@ asn1_size_length(int length)            /* I - Length value */
  *                     OID value.
  */
 
-static int                             /* O - Size in bytes */
+static unsigned                                /* O - Size in bytes */
 asn1_size_oid(const int *oid)          /* I - OID value */
 {
-  int  length;                         /* Length of value */
+  unsigned     length;                 /* Length of value */
 
 
   if (oid[1] < 0)
@@ -1679,7 +1640,7 @@ asn1_size_oid(const int *oid)             /* I - OID value */
  *                        packed integer value.
  */
 
-static int                             /* O - Size in bytes */
+static unsigned                                /* O - Size in bytes */
 asn1_size_packed(int integer)          /* I - Integer value */
 {
   if (integer > 0xfffffff)
@@ -1711,8 +1672,3 @@ snmp_set_error(cups_snmp_t *packet,       /* I - Packet */
 
   packet->error = _cupsLangString(cg->lang_default, message);
 }
-
-
-/*
- * End of "$Id$".
- */