shithub: riscv

Download patch

ref: 4ae79545470c374834487e5d5b2559ae7d430c43
parent: cfadfd97b894aa5086c94d45f67d1557968eb8ae
author: cinap_lenrek <[email protected]>
date: Fri Apr 5 10:09:41 EDT 2013

ape: inet_ntop() print v4 mapped addresses in v6 as dotted quad

--- a/sys/src/ape/lib/bsd/inet_ntop.c
+++ b/sys/src/ape/lib/bsd/inet_ntop.c
@@ -15,16 +15,21 @@
 char*
 inet_ntop(int af, void *src, char *dst, int size)
 {
+	static unsigned char v4prefix[12] = {
+		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+		0x00, 0x00, 0xff, 0xff, };
+
 	unsigned char *p;
-	char *t, *e;
+	char *t;
 	int i;
 
 	if(af == AF_INET){
+		p = (unsigned char*)&(((struct in_addr*)src)->s_addr);
+Dot4:
 		if(size < INET_ADDRSTRLEN){
 			errno = ENOSPC;
 			return 0;
 		}
-		p = (unsigned char*)&(((struct in_addr*)src)->s_addr);
 		snprintf(dst, size, "%d.%d.%d.%d", p[0], p[1], p[2], p[3]);
 		return dst;
 	}
@@ -33,14 +38,19 @@
 		errno = EAFNOSUPPORT;
 		return 0;
 	}
+
+	p = (unsigned char*)((struct in6_addr*)src)->s6_addr;
+	if(memcmp(p, v4prefix, 12) == 0){
+		p += 12;
+		goto Dot4;
+	}
+
 	if(size < INET6_ADDRSTRLEN){
 		errno = ENOSPC;
 		return 0;
 	}
 
-	p = (unsigned char*)((struct in6_addr*)src)->s6_addr;
 	t = dst;
-	e = t + size;
 	for(i=0; i<16; i += 2){
 		unsigned int w;
 
@@ -47,7 +57,7 @@
 		if(i > 0)
 			*t++ = ':';
 		w = p[i]<<8 | p[i+1];
-		snprintf(t, e - t, "%x", w);
+		sprintf(t, "%x", w);
 		t += strlen(t);
 	}
 	return dst;