]> andersk Git - openssh.git/blobdiff - openbsd-compat/inet_ntop.c
- halex@cvs.openbsd.org 2009/11/22 13:18:00
[openssh.git] / openbsd-compat / inet_ntop.c
index 1c23579612d94770661655b12060d1223fa1a184..e7ca4b7f8bebe0f7f3be13de5917e085b62a5383 100644 (file)
@@ -1,4 +1,4 @@
-/*     $OpenBSD: inet_ntop.c,v 1.1 1997/03/13 19:07:32 downsj Exp $    */
+/*     $OpenBSD: inet_ntop.c,v 1.7 2005/08/06 20:30:03 espie Exp $     */
 
 /* Copyright (c) 1996 by Internet Software Consortium.
  *
  * SOFTWARE.
  */
 
-#include "config.h"
+/* OPENBSD ORIGINAL: lib/libc/net/inet_ntop.c */
 
-#ifndef HAVE_INET_NTOP
+#include "includes.h"
 
-#if defined(LIBC_SCCS) && !defined(lint)
-#if 0
-static char rcsid[] = "$From: inet_ntop.c,v 8.7 1996/08/05 08:41:18 vixie Exp $";
-#else
-static char rcsid[] = "$OpenBSD: inet_ntop.c,v 1.1 1997/03/13 19:07:32 downsj Exp $";
-#endif
-#endif /* LIBC_SCCS and not lint */
+#ifndef HAVE_INET_NTOP
 
 #include <sys/param.h>
 #include <sys/types.h>
 #include <sys/socket.h>
-#include "openbsd-compat/fake-socket.h"
 #include <netinet/in.h>
 #include <arpa/inet.h>
 #include <arpa/nameser.h>
@@ -52,8 +45,8 @@ static char rcsid[] = "$OpenBSD: inet_ntop.c,v 1.1 1997/03/13 19:07:32 downsj Ex
  * sizeof(int) < 4.  sizeof(int) > 4 is fine; all the world's not a VAX.
  */
 
-static const char *inet_ntop4 __P((const u_char *src, char *dst, size_t size));
-static const char *inet_ntop6 __P((const u_char *src, char *dst, size_t size));
+static const char *inet_ntop4(const u_char *src, char *dst, size_t size);
+static const char *inet_ntop6(const u_char *src, char *dst, size_t size);
 
 /* char *
  * inet_ntop(af, src, dst, size)
@@ -64,11 +57,7 @@ static const char *inet_ntop6 __P((const u_char *src, char *dst, size_t size));
  *     Paul Vixie, 1996.
  */
 const char *
-inet_ntop(af, src, dst, size)
-       int af;
-       const void *src;
-       char *dst;
-       size_t size;
+inet_ntop(int af, const void *src, char *dst, size_t size)
 {
        switch (af) {
        case AF_INET:
@@ -94,19 +83,18 @@ inet_ntop(af, src, dst, size)
  *     Paul Vixie, 1996.
  */
 static const char *
-inet_ntop4(src, dst, size)
-       const u_char *src;
-       char *dst;
-       size_t size;
+inet_ntop4(const u_char *src, char *dst, size_t size)
 {
        static const char fmt[] = "%u.%u.%u.%u";
        char tmp[sizeof "255.255.255.255"];
+       int l;
 
-       if (sprintf(tmp, fmt, src[0], src[1], src[2], src[3]) > size) {
+       l = snprintf(tmp, size, fmt, src[0], src[1], src[2], src[3]);
+       if (l <= 0 || l >= size) {
                errno = ENOSPC;
                return (NULL);
        }
-       strcpy(dst, tmp);
+       strlcpy(dst, tmp, size);
        return (dst);
 }
 
@@ -117,10 +105,7 @@ inet_ntop4(src, dst, size)
  *     Paul Vixie, 1996.
  */
 static const char *
-inet_ntop6(src, dst, size)
-       const u_char *src;
-       char *dst;
-       size_t size;
+inet_ntop6(const u_char *src, char *dst, size_t size)
 {
        /*
         * Note that int32_t and int16_t need only be "at least" large enough
@@ -129,10 +114,12 @@ inet_ntop6(src, dst, size)
         * Keep this in mind if you think this function should have been coded
         * to use pointer overlays.  All the world's not a VAX.
         */
-       char tmp[sizeof "ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255"], *tp;
+       char tmp[sizeof "ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255"];
+       char *tp, *ep;
        struct { int base, len; } best, cur;
        u_int words[IN6ADDRSZ / INT16SZ];
        int i;
+       int advance;
 
        /*
         * Preprocess:
@@ -169,30 +156,45 @@ inet_ntop6(src, dst, size)
         * Format the result.
         */
        tp = tmp;
-       for (i = 0; i < (IN6ADDRSZ / INT16SZ); i++) {
+       ep = tmp + sizeof(tmp);
+       for (i = 0; i < (IN6ADDRSZ / INT16SZ) && tp < ep; i++) {
                /* Are we inside the best run of 0x00's? */
                if (best.base != -1 && i >= best.base &&
                    i < (best.base + best.len)) {
-                       if (i == best.base)
+                       if (i == best.base) {
+                               if (tp + 1 >= ep)
+                                       return (NULL);
                                *tp++ = ':';
+                       }
                        continue;
                }
                /* Are we following an initial run of 0x00s or any real hex? */
-               if (i != 0)
+               if (i != 0) {
+                       if (tp + 1 >= ep)
+                               return (NULL);
                        *tp++ = ':';
+               }
                /* Is this address an encapsulated IPv4? */
                if (i == 6 && best.base == 0 &&
                    (best.len == 6 || (best.len == 5 && words[5] == 0xffff))) {
-                       if (!inet_ntop4(src+12, tp, sizeof tmp - (tp - tmp)))
+                       if (!inet_ntop4(src+12, tp, (size_t)(ep - tp)))
                                return (NULL);
                        tp += strlen(tp);
                        break;
                }
-               tp += sprintf(tp, "%x", words[i]);
+               advance = snprintf(tp, ep - tp, "%x", words[i]);
+               if (advance <= 0 || advance >= ep - tp)
+                       return (NULL);
+               tp += advance;
        }
        /* Was it a trailing run of 0x00's? */
-       if (best.base != -1 && (best.base + best.len) == (IN6ADDRSZ / INT16SZ))
+       if (best.base != -1 && (best.base + best.len) == (IN6ADDRSZ / INT16SZ)) {
+               if (tp + 1 >= ep)
+                       return (NULL);
                *tp++ = ':';
+       }
+       if (tp + 1 >= ep)
+               return (NULL);
        *tp++ = '\0';
 
        /*
@@ -202,7 +204,7 @@ inet_ntop6(src, dst, size)
                errno = ENOSPC;
                return (NULL);
        }
-       strcpy(dst, tmp);
+       strlcpy(dst, tmp, size);
        return (dst);
 }
 
This page took 0.071827 seconds and 4 git commands to generate.