/*
- * Copyright (c) 1999 Kungliga Tekniska Högskolan
+ * Copyright (c) 1999 Kungliga Tekniska Högskolan
* (Royal Institute of Technology, Stockholm, Sweden).
* All rights reserved.
*
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the Kungliga Tekniska
- * Högskolan and its contributors.
+ * Högskolan and its contributors.
*
* 4. Neither the name of the Institute nor the names of its contributors
* may be used to endorse or promote products derived from this software
#endif
#include <netdissect-stdinc.h>
+#include "addrtostr.h"
#include <stdio.h>
+#include <string.h>
/*
*
#define INT16SZ 2 /* word size */
#endif
-static const char *
-inet_ntop_v4 (const void *src, char *dst, size_t size)
+const char *
+addrtostr (const void *src, char *dst, size_t size)
{
+ const u_char *srcaddr = (const u_char *)src;
const char digits[] = "0123456789";
int i;
- struct in_addr *addr = (struct in_addr *)src;
- u_long a = ntohl(addr->s_addr);
const char *orig_dst = dst;
if (size < INET_ADDRSTRLEN) {
return NULL;
}
for (i = 0; i < 4; ++i) {
- int n = (a >> (24 - i * 8)) & 0xFF;
+ int n = *srcaddr++;
int non_zerop = 0;
if (non_zerop || n / 100 > 0) {
return orig_dst;
}
-#ifdef INET6
/*
* Convert IPv6 binary address into presentation (printable) format.
*/
-static const char *
-inet_ntop_v6 (const u_char *src, char *dst, size_t size)
+const char *
+addrtostr6 (const void *src, char *dst, size_t size)
{
/*
* Note that int32_t and int16_t need only be "at least" large enough
* Keep this in mind if you think this function should have been coded
* to use pointer overlays. All the world's not a VAX.
*/
+ const u_char *srcaddr = (const u_char *)src;
char tmp [INET6_ADDRSTRLEN+1];
char *tp;
struct {
*/
memset (words, 0, sizeof(words));
for (i = 0; i < IN6ADDRSZ; i++)
- words[i/2] |= (src[i] << ((1 - (i % 2)) << 3));
+ words[i/2] |= (srcaddr[i] << ((1 - (i % 2)) << 3));
best.len = 0;
best.base = -1;
if (i == 6 && best.base == 0 &&
(best.len == 6 || (best.len == 5 && words[5] == 0xffff)))
{
- if (!inet_ntop_v4(src+12, tp, sizeof(tmp) - (tp - tmp)))
+ if (!addrtostr(srcaddr+12, tp, sizeof(tmp) - (tp - tmp)))
{
errno = ENOSPC;
+fprintf(stderr, "Bleah 1\n");
return (NULL);
}
tp += strlen(tp);
if ((size_t)(tp - tmp) > size)
{
errno = ENOSPC;
+fprintf(stderr, "Bleah 2\n");
return (NULL);
}
return strcpy (dst, tmp);
}
-#endif /* INET6 */
-
-
-const char *
-inet_ntop(int af, const void *src, char *dst, size_t size)
-{
- switch (af) {
- case AF_INET :
- return inet_ntop_v4 (src, dst, size);
-#ifdef INET6
- case AF_INET6:
- return inet_ntop_v6 ((const u_char*)src, dst, size);
-#endif
- default :
- errno = EAFNOSUPPORT;
- return NULL;
- }
-}