Skip site navigation (1)Skip section navigation (2)
Date:      Tue, 06 Jul 2004 13:48:06 +0200
From:      des@des.no (=?iso-8859-1?q?Dag-Erling_Sm=F8rgrav?=)
To:        Divacky Roman <xdivac02@stud.fit.vutbr.cz>
Cc:        current@freebsd.org
Subject:   Re: recent libalias changes
Message-ID:  <xzpr7rp9x1l.fsf@dwp.des.no>
In-Reply-To: <20040706113518.GA56265@stud.fit.vutbr.cz> (Divacky Roman's message of "Tue, 6 Jul 2004 13:35:18 %2B0200")
References:  <20040705133820.GA9159@stud.fit.vutbr.cz> <xzp1xjqbhee.fsf@dwp.des.no> <20040705161059.GA52584@walton.maths.tcd.ie> <xzpsmc6a02c.fsf@dwp.des.no> <xzpoemu9zfe.fsf@dwp.des.no> <20040706082906.GA50212@stud.fit.vutbr.cz> <xzpeknpbg0t.fsf@dwp.des.no> <20040706113518.GA56265@stud.fit.vutbr.cz>

next in thread | previous in thread | raw e-mail | index | archive | help
--=-=-=
Content-Type: text/plain; charset=iso-8859-1
Content-Transfer-Encoding: quoted-printable

Divacky Roman <xdivac02@stud.fit.vutbr.cz> writes:
> On Tue, Jul 06, 2004 at 12:12:50PM +0200, Dag-Erling Sm?rgrav wrote:
> > Done.  Could you please also test this one?
> with this patch applied it DOESNT work...

OK, thanks.  How about this one?  It clears up a lot of the alignment
warnings on alpha; it also improves code legibility a little and
reduces code duplication.

DES
--=20
Dag-Erling Sm=F8rgrav - des@des.no


--=-=-=
Content-Type: text/x-patch
Content-Disposition: attachment; filename=libalias.diff

Index: alias.c
===================================================================
RCS file: /home/ncvs/src/lib/libalias/alias.c,v
retrieving revision 1.47
diff -u -r1.47 alias.c
--- alias.c	6 Jul 2004 09:22:18 -0000	1.47
+++ alias.c	6 Jul 2004 09:50:15 -0000
@@ -173,7 +173,7 @@
 {
 	struct tcphdr *tc;
 
-	tc = (struct tcphdr *)((char *)pip + (pip->ip_hl << 2));
+	tc = (struct tcphdr *)ip_next(pip);
 
 	switch (GetStateIn(lnk)) {
 	case ALIAS_TCP_STATE_NOT_CONNECTED:
@@ -194,7 +194,7 @@
 {
 	struct tcphdr *tc;
 
-	tc = (struct tcphdr *)((char *)pip + (pip->ip_hl << 2));
+	tc = (struct tcphdr *)ip_next(pip);
 
 	switch (GetStateOut(lnk)) {
 	case ALIAS_TCP_STATE_NOT_CONNECTED:
@@ -283,7 +283,7 @@
 	struct alias_link *lnk;
 	struct icmp *ic;
 
-	ic = (struct icmp *)((char *)pip + (pip->ip_hl << 2));
+	ic = (struct icmp *)ip_next(pip);
 
 /* Get source address from ICMP data field and restore original data */
 	lnk = FindIcmpIn(la, pip->ip_src, pip->ip_dst, ic->icmp_id, 1);
@@ -329,12 +329,12 @@
 	struct tcphdr *tc;
 	struct alias_link *lnk;
 
-	ic = (struct icmp *)((char *)pip + (pip->ip_hl << 2));
+	ic = (struct icmp *)ip_next(pip);
 	ip = &ic->icmp_ip;
 
-	ud = (struct udphdr *)((char *)ip + (ip->ip_hl << 2));
-	tc = (struct tcphdr *)ud;
-	ic2 = (struct icmp *)ud;
+	ud = (struct udphdr *)ip_next(ip);
+	tc = (struct tcphdr *)ip_next(ip);
+	ic2 = (struct icmp *)ip_next(ip);
 
 	if (ip->ip_p == IPPROTO_UDP)
 		lnk = FindUdpTcpIn(la, ip->ip_dst, ip->ip_src,
@@ -426,7 +426,7 @@
 	if (la->packetAliasMode & PKT_ALIAS_PROXY_ONLY)
 		return (PKT_ALIAS_OK);
 
-	ic = (struct icmp *)((char *)pip + (pip->ip_hl << 2));
+	ic = (struct icmp *)ip_next(pip);
 
 	iresult = PKT_ALIAS_IGNORED;
 	switch (ic->icmp_type) {
@@ -461,7 +461,7 @@
 	struct alias_link *lnk;
 	struct icmp *ic;
 
-	ic = (struct icmp *)((char *)pip + (pip->ip_hl << 2));
+	ic = (struct icmp *)ip_next(pip);
 
 /* Save overwritten data for when echo packet returns */
 	lnk = FindIcmpOut(la, pip->ip_src, pip->ip_dst, ic->icmp_id, 1);
@@ -508,12 +508,12 @@
 	struct tcphdr *tc;
 	struct alias_link *lnk;
 
-	ic = (struct icmp *)((char *)pip + (pip->ip_hl << 2));
+	ic = (struct icmp *)ip_next(pip);
 	ip = &ic->icmp_ip;
 
-	ud = (struct udphdr *)((char *)ip + (ip->ip_hl << 2));
-	tc = (struct tcphdr *)ud;
-	ic2 = (struct icmp *)ud;
+	ud = (struct udphdr *)ip_next(ip);
+	tc = (struct tcphdr *)ip_next(ip);
+	ic2 = (struct icmp *)ip_next(ip);
 
 	if (ip->ip_p == IPPROTO_UDP)
 		lnk = FindUdpTcpOut(la, ip->ip_dst, ip->ip_src,
@@ -607,7 +607,7 @@
 	if (la->packetAliasMode & PKT_ALIAS_PROXY_ONLY)
 		return (PKT_ALIAS_OK);
 
-	ic = (struct icmp *)((char *)pip + (pip->ip_hl << 2));
+	ic = (struct icmp *)ip_next(pip);
 
 	iresult = PKT_ALIAS_IGNORED;
 	switch (ic->icmp_type) {
@@ -707,7 +707,7 @@
 	if (la->packetAliasMode & PKT_ALIAS_PROXY_ONLY)
 		return (PKT_ALIAS_OK);
 
-	ud = (struct udphdr *)((char *)pip + (pip->ip_hl << 2));
+	ud = (struct udphdr *)ip_next(pip);
 
 	lnk = FindUdpTcpIn(la, pip->ip_src, pip->ip_dst,
 	    ud->uh_sport, ud->uh_dport,
@@ -771,7 +771,7 @@
 	if (la->packetAliasMode & PKT_ALIAS_PROXY_ONLY)
 		return (PKT_ALIAS_OK);
 
-	ud = (struct udphdr *)((char *)pip + (pip->ip_hl << 2));
+	ud = (struct udphdr *)ip_next(pip);
 
 	lnk = FindUdpTcpOut(la, pip->ip_src, pip->ip_dst,
 	    ud->uh_sport, ud->uh_dport,
@@ -835,7 +835,7 @@
 	struct tcphdr *tc;
 	struct alias_link *lnk;
 
-	tc = (struct tcphdr *)((char *)pip + (pip->ip_hl << 2));
+	tc = (struct tcphdr *)ip_next(pip);
 
 	lnk = FindUdpTcpIn(la, pip->ip_src, pip->ip_dst,
 	    tc->th_sport, tc->th_dport,
@@ -926,7 +926,7 @@
 	struct tcphdr *tc;
 	struct alias_link *lnk;
 
-	tc = (struct tcphdr *)((char *)pip + (pip->ip_hl << 2));
+	tc = (struct tcphdr *)ip_next(pip);
 
 	proxy_type = ProxyCheck(la, pip, &proxy_server_address, &proxy_server_port);
 
@@ -969,7 +969,7 @@
 			SetProxyPort(lnk, dest_port);
 			SetProxyAddress(lnk, dest_address);
 			ProxyModify(la, lnk, pip, maxpacketsize, proxy_type);
-			tc = (struct tcphdr *)((char *)pip + (pip->ip_hl << 2));
+			tc = (struct tcphdr *)ip_next(pip);
 		}
 /* Get alias address and port */
 		alias_port = GetAliasPort(lnk);
@@ -1346,9 +1346,9 @@
 	    || (pip->ip_hl << 2) > maxpacketsize)
 		return (iresult);
 
-	ud = (struct udphdr *)((char *)pip + (pip->ip_hl << 2));
-	tc = (struct tcphdr *)ud;
-	ic = (struct icmp *)ud;
+	ud = (struct udphdr *)ip_next(pip);
+	tc = (struct tcphdr *)ip_next(pip);
+	ic = (struct icmp *)ip_next(pip);
 
 	/* Find a link */
 	if (pip->ip_p == IPPROTO_UDP)
Index: alias_cuseeme.c
===================================================================
RCS file: /home/ncvs/src/lib/libalias/alias_cuseeme.c,v
retrieving revision 1.9
diff -u -r1.9 alias_cuseeme.c
--- alias_cuseeme.c	5 Jul 2004 11:10:57 -0000	1.9
+++ alias_cuseeme.c	6 Jul 2004 09:50:15 -0000
@@ -70,14 +70,13 @@
 void
 AliasHandleCUSeeMeOut(struct libalias *la, struct ip *pip, struct alias_link *lnk)
 {
-	struct udphdr *ud;
+	struct udphdr *ud = ip_next(pip);
 
-	ud = (struct udphdr *)((char *)pip + (pip->ip_hl << 2));
 	if (ntohs(ud->uh_ulen) - sizeof(struct udphdr) >= sizeof(struct cu_header)) {
 		struct cu_header *cu;
 		struct alias_link *cu_lnk;
 
-		cu = (struct cu_header *)(ud + 1);
+		cu = udp_next(ud);
 		if (cu->addr)
 			cu->addr = (u_int32_t) GetAliasAddress(lnk).s_addr;
 
@@ -104,8 +103,8 @@
 
 	(void)la;
 	alias_addr.s_addr = pip->ip_dst.s_addr;
-	ud = (struct udphdr *)((char *)pip + (pip->ip_hl << 2));
-	cu = (struct cu_header *)(ud + 1);
+	ud = ip_next(pip);
+	cu = udp_next(ud);
 	oc = (struct oc_header *)(cu + 1);
 	ci = (struct client_info *)(oc + 1);
 	end = (char *)ud + ntohs(ud->uh_ulen);
Index: alias_db.c
===================================================================
RCS file: /home/ncvs/src/lib/libalias/alias_db.c,v
retrieving revision 1.60
diff -u -r1.60 alias_db.c
--- alias_db.c	5 Jul 2004 11:10:57 -0000	1.60
+++ alias_db.c	6 Jul 2004 09:50:15 -0000
@@ -1908,7 +1908,7 @@
 	int delta, ack_diff_min;
 	u_long ack;
 
-	tc = (struct tcphdr *)((char *)pip + (pip->ip_hl << 2));
+	tc = ip_next(pip);
 	ack = tc->th_ack;
 
 	delta = 0;
@@ -1952,7 +1952,7 @@
 	int delta, seq_diff_min;
 	u_long seq;
 
-	tc = (struct tcphdr *)((char *)pip + (pip->ip_hl << 2));
+	tc = ip_next(pip);
 	seq = tc->th_seq;
 
 	delta = 0;
@@ -1996,7 +1996,7 @@
 	int hlen, tlen, dlen;
 	int i;
 
-	tc = (struct tcphdr *)((char *)pip + (pip->ip_hl << 2));
+	tc = ip_next(pip);
 
 	hlen = (pip->ip_hl + tc->th_off) << 2;
 	tlen = ntohs(pip->ip_len);
Index: alias_ftp.c
===================================================================
RCS file: /home/ncvs/src/lib/libalias/alias_ftp.c,v
retrieving revision 1.22
diff -u -r1.22 alias_ftp.c
--- alias_ftp.c	5 Jul 2004 11:10:57 -0000	1.22
+++ alias_ftp.c	6 Jul 2004 09:50:15 -0000
@@ -114,7 +114,7 @@
 	int ftp_message_type;
 
 /* Calculate data length of TCP packet */
-	tc = (struct tcphdr *)((char *)pip + (pip->ip_hl << 2));
+	tc = (struct tcphdr *)ip_next(pip);
 	hlen = (pip->ip_hl + tc->th_off) << 2;
 	tlen = ntohs(pip->ip_len);
 	dlen = tlen - hlen;
@@ -578,7 +578,7 @@
 #endif
 
 /* Calculate data length of TCP packet */
-		tc = (struct tcphdr *)((char *)pip + (pip->ip_hl << 2));
+		tc = (struct tcphdr *)ip_next(pip);
 		hlen = (pip->ip_hl + tc->th_off) << 2;
 		tlen = ntohs(pip->ip_len);
 		dlen = tlen - hlen;
Index: alias_irc.c
===================================================================
RCS file: /home/ncvs/src/lib/libalias/alias_irc.c,v
retrieving revision 1.17
diff -u -r1.17 alias_irc.c
--- alias_irc.c	5 Jul 2004 11:10:57 -0000	1.17
+++ alias_irc.c	6 Jul 2004 09:50:15 -0000
@@ -80,7 +80,7 @@
 	int i;			/* Iterator through the source */
 
 /* Calculate data length of TCP packet */
-	tc = (struct tcphdr *)((char *)pip + (pip->ip_hl << 2));
+	tc = (struct tcphdr *)ip_next(pip);
 	hlen = (pip->ip_hl + tc->th_off) << 2;
 	tlen = ntohs(pip->ip_len);
 	dlen = tlen - hlen;
Index: alias_local.h
===================================================================
RCS file: /home/ncvs/src/lib/libalias/alias_local.h,v
retrieving revision 1.28
diff -u -r1.28 alias_local.h
--- alias_local.h	5 Jul 2004 11:10:57 -0000	1.28
+++ alias_local.h	6 Jul 2004 09:44:42 -0000
@@ -324,6 +324,32 @@
 	ALIAS_TCP_STATE_DISCONNECTED
 };
 
+#if defined(_NETINET_IP_H_)
+static __inline void *
+ip_next(struct ip *iphdr)
+{
+	char *p = (char *)iphdr;
+	return (&p[iphdr->ip_hl * 4]);
+}
+#endif
+
+#if defined(_NETINET_TCP_H_)
+static __inline void *
+tcp_next(struct tcphdr *tcphdr)
+{
+	char *p = (char *)tcphdr;
+	return (&p[tcphdr->th_off * 4]);
+}
+#endif
+
+#if defined(_NETINET_UDP_H_)
+static __inline void *
+udp_next(struct udphdr *udphdr)
+{
+	return ((void *)(udphdr + 1));
+}
+#endif
+
 /*lint -restore */
 
 #endif				/* !_ALIAS_LOCAL_H_ */
Index: alias_nbt.c
===================================================================
RCS file: /home/ncvs/src/lib/libalias/alias_nbt.c,v
retrieving revision 1.14
diff -u -r1.14 alias_nbt.c
--- alias_nbt.c	5 Jul 2004 11:10:57 -0000	1.14
+++ alias_nbt.c	6 Jul 2004 09:56:31 -0000
@@ -218,10 +218,10 @@
 	(void)lnk;
 
 	/* Calculate data length of UDP packet */
-	uh = (struct udphdr *)((char *)pip + (pip->ip_hl << 2));
+	uh = (struct udphdr *)ip_next(pip);
 	pmax = (char *)uh + ntohs(uh->uh_ulen);
 
-	ndh = (NbtDataHeader *) ((char *)uh + (sizeof(struct udphdr)));
+	ndh = (NbtDataHeader *)udp_next(uh);
 	if ((char *)(ndh + 1) > pmax)
 		return (-1);
 #ifdef DEBUG
@@ -654,9 +654,9 @@
 	nbtarg.newport = *original_port;
 
 	/* Calculate data length of UDP packet */
-	uh = (struct udphdr *)((char *)pip + (pip->ip_hl << 2));
+	uh = (struct udphdr *)ip_next(pip);
 	nbtarg.uh_sum = &(uh->uh_sum);
-	nsh = (NbtNSHeader *) ((char *)uh + (sizeof(struct udphdr)));
+	nsh = (NbtNSHeader *)udp_next(uh);
 	p = (u_char *) (nsh + 1);
 	pmax = (char *)uh + ntohs(uh->uh_ulen);
 
Index: alias_pptp.c
===================================================================
RCS file: /home/ncvs/src/lib/libalias/alias_pptp.c,v
retrieving revision 1.10
diff -u -r1.10 alias_pptp.c
--- alias_pptp.c	5 Jul 2004 11:10:57 -0000	1.10
+++ alias_pptp.c	6 Jul 2004 09:53:40 -0000
@@ -193,7 +193,7 @@
 		cptr->cid1 = GetAliasPort(pptp_lnk);
 
 		/* Compute TCP checksum for revised packet */
-		tc = (struct tcphdr *)((char *)pip + (pip->ip_hl << 2));
+		tc = (struct tcphdr *)ip_next(pip);
 		accumulate -= cptr->cid1;
 		ADJUST_CHECKSUM(accumulate, tc->th_sum);
 
@@ -265,7 +265,7 @@
 		*pcall_id = GetOriginalPort(pptp_lnk);
 
 		/* Compute TCP checksum for modified packet */
-		tc = (struct tcphdr *)((char *)pip + (pip->ip_hl << 2));
+		tc = (struct tcphdr *)ip_next(pip);
 		accumulate -= *pcall_id;
 		ADJUST_CHECKSUM(accumulate, tc->th_sum);
 
@@ -290,7 +290,7 @@
 	struct tcphdr *tc;
 
 	/* Calculate some lengths */
-	tc = (struct tcphdr *)((char *)pip + (pip->ip_hl << 2));
+	tc = (struct tcphdr *)ip_next(pip);
 	hlen = (pip->ip_hl + tc->th_off) << 2;
 	tlen = ntohs(pip->ip_len);
 	dlen = tlen - hlen;
@@ -300,7 +300,7 @@
 		return (NULL);
 
 	/* Move up to PPTP message header */
-	hptr = (PptpMsgHead) (((char *)pip) + hlen);
+	hptr = (PptpMsgHead) ip_next(pip);
 
 	/* Return the control message type */
 	*ptype = ntohs(hptr->type);
@@ -326,7 +326,7 @@
 	GreHdr *gr;
 	struct alias_link *lnk;
 
-	gr = (GreHdr *) ((char *)pip + (pip->ip_hl << 2));
+	gr = (GreHdr *) ip_next(pip);
 
 	/* Check GRE header bits. */
 	if ((ntohl(*((u_int32_t *) gr)) & PPTP_INIT_MASK) != PPTP_INIT_VALUE)
@@ -351,7 +351,7 @@
 	GreHdr *gr;
 	struct alias_link *lnk;
 
-	gr = (GreHdr *) ((char *)pip + (pip->ip_hl << 2));
+	gr = (GreHdr *) ip_next(pip);
 
 	/* Check GRE header bits. */
 	if ((ntohl(*((u_int32_t *) gr)) & PPTP_INIT_MASK) != PPTP_INIT_VALUE)
Index: alias_proxy.c
===================================================================
RCS file: /home/ncvs/src/lib/libalias/alias_proxy.c,v
retrieving revision 1.17
diff -u -r1.17 alias_proxy.c
--- alias_proxy.c	5 Jul 2004 11:10:57 -0000	1.17
+++ alias_proxy.c	6 Jul 2004 09:54:56 -0000
@@ -283,7 +283,7 @@
 	struct tcphdr *tc;
 
 /* Compute pointer to tcp header */
-	tc = (struct tcphdr *)((char *)pip + (pip->ip_hl << 2));
+	tc = (struct tcphdr *)ip_next(pip);
 
 /* Don't modify if once already modified */
 
@@ -392,7 +392,7 @@
 
 		memcpy(&option[2], (u_char *) & pip->ip_dst, 4);
 
-		tc = (struct tcphdr *)((char *)pip + (pip->ip_hl << 2));
+		tc = (struct tcphdr *)ip_next(pip);
 		memcpy(&option[6], (u_char *) & tc->th_sport, 2);
 
 		memcpy(ptr, option, 8);
@@ -451,7 +451,7 @@
 
 	src_addr = pip->ip_src;
 	dst_addr = pip->ip_dst;
-	dst_port = ((struct tcphdr *)((char *)pip + (pip->ip_hl << 2)))
+	dst_port = ((struct tcphdr *)ip_next(pip))
 	    ->th_dport;
 
 	ptr = la->proxyList;
Index: alias_skinny.c
===================================================================
RCS file: /home/ncvs/src/lib/libalias/alias_skinny.c,v
retrieving revision 1.5
diff -u -r1.5 alias_skinny.c
--- alias_skinny.c	5 Jul 2004 11:10:57 -0000	1.5
+++ alias_skinny.c	6 Jul 2004 09:56:49 -0000
@@ -223,12 +223,12 @@
 	int orig_len, skinny_hdr_len = sizeof(struct skinny_header);
 	ConvDirection direction;
 
-	tc = (struct tcphdr *)((char *)pip + (pip->ip_hl << 2));
+	tc = (struct tcphdr *)ip_next(pip);
 	hlen = (pip->ip_hl + tc->th_off) << 2;
 	tlen = ntohs(pip->ip_len);
 	dlen = tlen - hlen;
 
-	sd = (struct skinny_header *)((char *)pip + hlen);
+	sd = (struct skinny_header *)ip_next(pip);
 
 	/*
 	 * XXX This direction is reserved for future use.  I still need to
Index: alias_smedia.c
===================================================================
RCS file: /home/ncvs/src/lib/libalias/alias_smedia.c,v
retrieving revision 1.10
diff -u -r1.10 alias_smedia.c
--- alias_smedia.c	5 Jul 2004 11:10:57 -0000	1.10
+++ alias_smedia.c	6 Jul 2004 09:50:15 -0000
@@ -155,7 +155,7 @@
 	struct in_addr null_addr;
 
 	/* Calculate data length of TCP packet */
-	tc = (struct tcphdr *)((char *)pip + (pip->ip_hl << 2));
+	tc = (struct tcphdr *)ip_next(pip);
 	hlen = (pip->ip_hl + tc->th_off) << 2;
 	tlen = ntohs(pip->ip_len);
 	dlen = tlen - hlen;
@@ -359,7 +359,7 @@
 				/* Punch hole in firewall */
 				PunchFWHole(pna_links);
 #endif
-				tc = (struct tcphdr *)((char *)pip + (pip->ip_hl << 2));
+				tc = (struct tcphdr *)ip_next(pip);
 				alias_port = GetAliasPort(pna_links);
 				memcpy(work, &alias_port, 2);
 
@@ -387,7 +387,7 @@
 
 	(void)maxpacketsize;
 
-	tc = (struct tcphdr *)((char *)pip + (pip->ip_hl << 2));
+	tc = (struct tcphdr *)ip_next(pip);
 	hlen = (pip->ip_hl + tc->th_off) << 2;
 	tlen = ntohs(pip->ip_len);
 	dlen = tlen - hlen;
Index: alias_util.c
===================================================================
RCS file: /home/ncvs/src/lib/libalias/alias_util.c,v
retrieving revision 1.14
diff -u -r1.14 alias_util.c
--- alias_util.c	5 Jul 2004 11:10:57 -0000	1.14
+++ alias_util.c	6 Jul 2004 11:44:46 -0000
@@ -102,7 +102,7 @@
 	nhdr = pip->ip_hl << 2;
 	ntcp = ntohs(pip->ip_len) - nhdr;
 
-	tc = (struct tcphdr *)((char *)pip + nhdr);
+	tc = (struct tcphdr *)ip_next(pip);
 	ptr = (u_short *) tc;
 
 /* Add up TCP header and data */

--=-=-=--



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?xzpr7rp9x1l.fsf>