tech-net archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

small code duplication reduction patch



hi,

the attached patch splits out a new function in6_in_2_v4mapin6() for
creating IPv4-Maped IPv6 addresses from IPv4 addresses from the
existing in6_sin_2_v4mapsin6()

the remainder of the patch replaces redundant code which uses either
the new in6_in_2_v4mapin6() (for dealing with in{,6}_addr) or
existing in6_sin_2_v4mapsin6() (for dealing with sockaddr_in{,6})

no functional change intended, unless there are problems i'll
commit in the next day or two.

thanks
Index: netinet/in_pcb.c
===================================================================
RCS file: /cvsroot/src/sys/netinet/in_pcb.c,v
retrieving revision 1.162
diff -p -u -r1.162 in_pcb.c
--- netinet/in_pcb.c	24 Aug 2015 22:21:26 -0000	1.162
+++ netinet/in_pcb.c	14 Feb 2016 22:37:31 -0000
@@ -347,10 +347,7 @@ in_pcbbind_port(struct inpcb *inp, struc
 			return (EACCES);
 
 #ifdef INET6
-		memset(&mapped, 0, sizeof(mapped));
-		mapped.s6_addr16[5] = 0xffff;
-		memcpy(&mapped.s6_addr32[3], &sin->sin_addr,
-		    sizeof(mapped.s6_addr32[3]));
+		in6_in_2_v4mapin6(&sin->sin_addr, &mapped);
 		t6 = in6_pcblookup_port(table, &mapped, sin->sin_port, wild, &vestige);
 		if (t6 && (reuseport & t6->in6p_socket->so_options) == 0)
 			return (EADDRINUSE);
Index: netinet/tcp_input.c
===================================================================
RCS file: /cvsroot/src/sys/netinet/tcp_input.c,v
retrieving revision 1.344
diff -p -u -r1.344 tcp_input.c
--- netinet/tcp_input.c	24 Aug 2015 22:21:26 -0000	1.344
+++ netinet/tcp_input.c	14 Feb 2016 22:37:31 -0000
@@ -1424,12 +1424,8 @@ findpcb:
 			struct in6_addr s, d;
 
 			/* mapped addr case */
-			memset(&s, 0, sizeof(s));
-			s.s6_addr16[5] = htons(0xffff);
-			bcopy(&ip->ip_src, &s.s6_addr32[3], sizeof(ip->ip_src));
-			memset(&d, 0, sizeof(d));
-			d.s6_addr16[5] = htons(0xffff);
-			bcopy(&ip->ip_dst, &d.s6_addr32[3], sizeof(ip->ip_dst));
+			in6_in_2_v4mapin6(&ip->ip_src, &s);
+			in6_in_2_v4mapin6(&ip->ip_dst, &d);
 			in6p = in6_pcblookup_connect(&tcbtable, &s,
 						     th->th_sport, &d, th->th_dport,
 						     0, &vestige);
@@ -4086,14 +4082,7 @@ syn_cache_get(struct sockaddr *src, stru
 		memcpy(&sin6, src, src->sa_len);
 		if (src->sa_family == AF_INET) {
 			/* IPv4 packet to AF_INET6 socket */
-			memset(&sin6, 0, sizeof(sin6));
-			sin6.sin6_family = AF_INET6;
-			sin6.sin6_len = sizeof(sin6);
-			sin6.sin6_port = ((struct sockaddr_in *)src)->sin_port;
-			sin6.sin6_addr.s6_addr16[5] = htons(0xffff);
-			bcopy(&((struct sockaddr_in *)src)->sin_addr,
-				&sin6.sin6_addr.s6_addr32[3],
-				sizeof(sin6.sin6_addr.s6_addr32[3]));
+			in6_sin_2_v4mapsin6((struct sockaddr_in *)src, &sin6);
 		}
 		if (in6_pcbconnect(in6p, &sin6, NULL)) {
 			goto resetandabort;
Index: netinet/tcp_subr.c
===================================================================
RCS file: /cvsroot/src/sys/netinet/tcp_subr.c,v
retrieving revision 1.264
diff -p -u -r1.264 tcp_subr.c
--- netinet/tcp_subr.c	7 Sep 2015 01:56:50 -0000	1.264
+++ netinet/tcp_subr.c	14 Feb 2016 22:37:31 -0000
@@ -1603,11 +1603,8 @@ tcp_ctlinput(int cmd, const struct socka
 		 */
 		th = (struct tcphdr *)((char *)ip + (ip->ip_hl << 2));
 #ifdef INET6
-		memset(&src6, 0, sizeof(src6));
-		memset(&dst6, 0, sizeof(dst6));
-		src6.s6_addr16[5] = dst6.s6_addr16[5] = 0xffff;
-		memcpy(&src6.s6_addr32[3], &ip->ip_src, sizeof(struct in_addr));
-		memcpy(&dst6.s6_addr32[3], &ip->ip_dst, sizeof(struct in_addr));
+		in6_in_2_v4mapin6(&ip->ip_src, &src6);
+		in6_in_2_v4mapin6(&ip->ip_dst, &dst6);
 #endif
 		if ((inp = in_pcblookup_connect(&tcbtable, ip->ip_dst,
 						th->th_dport, ip->ip_src, th->th_sport, 0)) != NULL)
@@ -1752,9 +1749,7 @@ tcp_mtudisc_callback(struct in_addr fadd
 
 	in_pcbnotifyall(&tcbtable, faddr, EMSGSIZE, tcp_mtudisc);
 #ifdef INET6
-	memset(&in6, 0, sizeof(in6));
-	in6.s6_addr16[5] = 0xffff;
-	memcpy(&in6.s6_addr32[3], &faddr, sizeof(struct in_addr));
+	in6_in_2_v4mapin6(&faddr, &in6);
 	tcp6_mtudisc_callback(&in6);
 #endif
 }
Index: netinet/udp_usrreq.c
===================================================================
RCS file: /cvsroot/src/sys/netinet/udp_usrreq.c,v
retrieving revision 1.223
diff -p -u -r1.223 udp_usrreq.c
--- netinet/udp_usrreq.c	20 Jan 2016 22:01:18 -0000	1.223
+++ netinet/udp_usrreq.c	14 Feb 2016 22:37:31 -0000
@@ -405,16 +405,12 @@ udp_input(struct mbuf *m, ...)
 		memset(&src6, 0, sizeof(src6));
 		src6.sin6_family = AF_INET6;
 		src6.sin6_len = sizeof(struct sockaddr_in6);
-		src6.sin6_addr.s6_addr[10] = src6.sin6_addr.s6_addr[11] = 0xff;
-		memcpy(&src6.sin6_addr.s6_addr[12], &ip->ip_src,
-			sizeof(ip->ip_src));
+		in6_in_2_v4mapin6(&ip->ip_src, &src6.sin6_addr);
 		src6.sin6_port = uh->uh_sport;
 		memset(&dst6, 0, sizeof(dst6));
 		dst6.sin6_family = AF_INET6;
 		dst6.sin6_len = sizeof(struct sockaddr_in6);
-		dst6.sin6_addr.s6_addr[10] = dst6.sin6_addr.s6_addr[11] = 0xff;
-		memcpy(&dst6.sin6_addr.s6_addr[12], &ip->ip_dst,
-			sizeof(ip->ip_dst));
+		in6_in_2_v4mapin6(&ip->ip_dst, &dst6.sin6_addr);
 		dst6.sin6_port = uh->uh_dport;
 
 		n += udp6_realinput(AF_INET, &src6, &dst6, m, iphlen);
Index: netinet6/in6.c
===================================================================
RCS file: /cvsroot/src/sys/netinet6/in6.c,v
retrieving revision 1.194
diff -p -u -r1.194 in6.c
--- netinet6/in6.c	12 Dec 2015 23:34:25 -0000	1.194
+++ netinet6/in6.c	14 Feb 2016 22:37:31 -0000
@@ -2553,6 +2553,19 @@ in6_domifdetach(struct ifnet *ifp, void 
 }
 
 /*
+ * Convert IPv4 address stored in struct in_addr to IPv4-Mapped IPv6 address
+ * stored in struct in6_addr as defined in RFC 4921 section 2.5.5.2.
+ */
+void
+in6_in_2_v4mapin6(const struct in_addr *in, struct in6_addr *in6)
+{
+	in6->s6_addr32[0] = 0;
+	in6->s6_addr32[1] = 0;
+	in6->s6_addr32[2] = IPV6_ADDR_INT32_SMP;
+	in6->s6_addr32[3] = in->s_addr;
+}
+
+/*
  * Convert sockaddr_in6 to sockaddr_in.  Original sockaddr_in6 must be
  * v4 mapped addr or v4 compat addr
  */
@@ -2574,10 +2587,7 @@ in6_sin_2_v4mapsin6(const struct sockadd
 	sin6->sin6_len = sizeof(struct sockaddr_in6);
 	sin6->sin6_family = AF_INET6;
 	sin6->sin6_port = sin->sin_port;
-	sin6->sin6_addr.s6_addr32[0] = 0;
-	sin6->sin6_addr.s6_addr32[1] = 0;
-	sin6->sin6_addr.s6_addr32[2] = IPV6_ADDR_INT32_SMP;
-	sin6->sin6_addr.s6_addr32[3] = sin->sin_addr.s_addr;
+	in6_in_2_v4mapin6(&sin->sin_addr, &sin6->sin6_addr);
 }
 
 /* Convert sockaddr_in6 into sockaddr_in. */
Index: netinet6/in6.h
===================================================================
RCS file: /cvsroot/src/sys/netinet6/in6.h,v
retrieving revision 1.86
diff -p -u -r1.86 in6.h
--- netinet6/in6.h	12 Dec 2015 23:34:25 -0000	1.86
+++ netinet6/in6.h	14 Feb 2016 22:37:31 -0000
@@ -768,6 +768,7 @@ typedef	_BSD_SIZE_T_		size_t;
 __BEGIN_DECLS
 struct cmsghdr;
 
+void	in6_in_2_v4mapin6(const struct in_addr *, struct in6_addr *);
 void	in6_sin6_2_sin(struct sockaddr_in *, struct sockaddr_in6 *);
 void	in6_sin_2_v4mapsin6(const struct sockaddr_in *, struct sockaddr_in6 *);
 void	in6_sin6_2_sin_in_sock(struct sockaddr *);


Home | Main Index | Thread Index | Old Index