From 9fa0fd268273d90e765d9ebfe0be87ea4d64e138 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dag-Erling=20Sm=C3=B8rgrav?= Date: Tue, 6 Jul 2004 12:13:28 +0000 Subject: [PATCH] Introduce inline {ip,udp,tcp}_next() functions which take a pointer to an {ip,udp,tcp} header and return a void * pointing to the payload (i.e. the first byte past the end of the header and any required padding). Use them consistently throughout libalias to a) reduce code duplication, b) improve code legibility, c) get rid of a bunch of alignment warnings. --- lib/libalias/alias.c | 44 ++++++++++++++-------------- lib/libalias/alias_cuseeme.c | 9 +++--- lib/libalias/alias_db.c | 6 ++-- lib/libalias/alias_ftp.c | 4 +-- lib/libalias/alias_irc.c | 2 +- lib/libalias/alias_local.h | 26 ++++++++++++++++ lib/libalias/alias_nbt.c | 8 ++--- lib/libalias/alias_pptp.c | 12 ++++---- lib/libalias/alias_proxy.c | 6 ++-- lib/libalias/alias_skinny.c | 4 +-- lib/libalias/alias_smedia.c | 6 ++-- lib/libalias/alias_util.c | 2 +- sys/netinet/libalias/alias.c | 44 ++++++++++++++-------------- sys/netinet/libalias/alias_cuseeme.c | 9 +++--- sys/netinet/libalias/alias_db.c | 6 ++-- sys/netinet/libalias/alias_ftp.c | 4 +-- sys/netinet/libalias/alias_irc.c | 2 +- sys/netinet/libalias/alias_local.h | 26 ++++++++++++++++ sys/netinet/libalias/alias_nbt.c | 8 ++--- sys/netinet/libalias/alias_pptp.c | 12 ++++---- sys/netinet/libalias/alias_proxy.c | 6 ++-- sys/netinet/libalias/alias_skinny.c | 4 +-- sys/netinet/libalias/alias_smedia.c | 6 ++-- sys/netinet/libalias/alias_util.c | 2 +- 24 files changed, 154 insertions(+), 104 deletions(-) diff --git a/lib/libalias/alias.c b/lib/libalias/alias.c index 6acf03b01d44..a2c29118caf2 100644 --- a/lib/libalias/alias.c +++ b/lib/libalias/alias.c @@ -173,7 +173,7 @@ TcpMonitorIn(struct ip *pip, struct alias_link *lnk) { 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 @@ TcpMonitorOut(struct ip *pip, struct alias_link *lnk) { 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 @@ IcmpAliasIn1(struct libalias *la, struct ip *pip) 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 @@ IcmpAliasIn2(struct libalias *la, struct ip *pip) 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 @@ IcmpAliasIn(struct libalias *la, struct ip *pip) 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 @@ IcmpAliasOut1(struct libalias *la, struct ip *pip) 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 @@ IcmpAliasOut2(struct libalias *la, struct ip *pip) 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 @@ IcmpAliasOut(struct libalias *la, struct ip *pip, int create) 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 @@ UdpAliasIn(struct libalias *la, struct ip *pip) 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 @@ UdpAliasOut(struct libalias *la, struct ip *pip, int create) 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 @@ TcpAliasIn(struct libalias *la, struct ip *pip) 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 @@ TcpAliasOut(struct libalias *la, struct ip *pip, int maxpacketsize, int create) 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 @@ TcpAliasOut(struct libalias *la, struct ip *pip, int maxpacketsize, int create) 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 @@ LibAliasUnaliasOut(struct libalias *la, char *ptr, /* valid IP packet */ || (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) diff --git a/lib/libalias/alias_cuseeme.c b/lib/libalias/alias_cuseeme.c index bca6b3b28154..cbacc55cad8d 100644 --- a/lib/libalias/alias_cuseeme.c +++ b/lib/libalias/alias_cuseeme.c @@ -70,14 +70,13 @@ struct client_info { 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 @@ AliasHandleCUSeeMeIn(struct libalias *la, struct ip *pip, struct in_addr origina (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); diff --git a/lib/libalias/alias_db.c b/lib/libalias/alias_db.c index e41106600356..f9c953b27b1f 100644 --- a/lib/libalias/alias_db.c +++ b/lib/libalias/alias_db.c @@ -1908,7 +1908,7 @@ packet size was altered is searched. 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 @@ packet size was altered is searched. 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 @@ been altered, then this list will begin to overwrite itself. 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); diff --git a/lib/libalias/alias_ftp.c b/lib/libalias/alias_ftp.c index 79938db218ed..b628528a0c1d 100644 --- a/lib/libalias/alias_ftp.c +++ b/lib/libalias/alias_ftp.c @@ -114,7 +114,7 @@ AliasHandleFtpOut( 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 @@ NewFtpMessage(struct libalias *la, struct ip *pip, #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; diff --git a/lib/libalias/alias_irc.c b/lib/libalias/alias_irc.c index 06abab0501eb..66106fa35112 100644 --- a/lib/libalias/alias_irc.c +++ b/lib/libalias/alias_irc.c @@ -80,7 +80,7 @@ AliasHandleIrcOut(struct libalias *la, 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; diff --git a/lib/libalias/alias_local.h b/lib/libalias/alias_local.h index bf923a29aad2..67947da81853 100644 --- a/lib/libalias/alias_local.h +++ b/lib/libalias/alias_local.h @@ -324,6 +324,32 @@ enum alias_tcp_state { 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_ */ diff --git a/lib/libalias/alias_nbt.c b/lib/libalias/alias_nbt.c index 151bc9d934d1..b51abd1c5da5 100644 --- a/lib/libalias/alias_nbt.c +++ b/lib/libalias/alias_nbt.c @@ -218,10 +218,10 @@ AliasHandleUdpNbt( (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 @@ AliasHandleUdpNbtNS( 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); diff --git a/lib/libalias/alias_pptp.c b/lib/libalias/alias_pptp.c index 499b5fe2fea6..174780fc29fe 100644 --- a/lib/libalias/alias_pptp.c +++ b/lib/libalias/alias_pptp.c @@ -193,7 +193,7 @@ AliasHandlePptpOut(struct libalias *la, 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 @@ AliasHandlePptpIn(struct libalias *la, *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 @@ AliasVerifyPptp(struct ip *pip, u_int16_t * ptype) 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 @@ AliasVerifyPptp(struct ip *pip, u_int16_t * ptype) 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 @@ AliasHandlePptpGreOut(struct libalias *la, struct ip *pip) 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 @@ AliasHandlePptpGreIn(struct libalias *la, struct ip *pip) 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) diff --git a/lib/libalias/alias_proxy.c b/lib/libalias/alias_proxy.c index 24d8bfee73b0..e0e1c059ed68 100644 --- a/lib/libalias/alias_proxy.c +++ b/lib/libalias/alias_proxy.c @@ -283,7 +283,7 @@ ProxyEncodeTcpStream(struct alias_link *lnk, 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 @@ ProxyEncodeIpHeader(struct ip *pip, 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 @@ ProxyCheck(struct libalias *la, struct ip *pip, 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; diff --git a/lib/libalias/alias_skinny.c b/lib/libalias/alias_skinny.c index 6a64063cdd99..fccecc6764bb 100644 --- a/lib/libalias/alias_skinny.c +++ b/lib/libalias/alias_skinny.c @@ -223,12 +223,12 @@ AliasHandleSkinny(struct libalias *la, struct ip *pip, struct alias_link *lnk) 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 diff --git a/lib/libalias/alias_smedia.c b/lib/libalias/alias_smedia.c index ef19bea95203..888937c9f241 100644 --- a/lib/libalias/alias_smedia.c +++ b/lib/libalias/alias_smedia.c @@ -155,7 +155,7 @@ alias_rtsp_out(struct libalias *la, struct ip *pip, 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 @@ alias_pna_out(struct libalias *la, struct ip *pip, /* 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 @@ AliasHandleRtspOut(struct libalias *la, struct ip *pip, struct alias_link *lnk, (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; diff --git a/lib/libalias/alias_util.c b/lib/libalias/alias_util.c index 14df53d97768..687168e830b7 100644 --- a/lib/libalias/alias_util.c +++ b/lib/libalias/alias_util.c @@ -102,7 +102,7 @@ TcpChecksum(struct ip *pip) 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 */ diff --git a/sys/netinet/libalias/alias.c b/sys/netinet/libalias/alias.c index 6acf03b01d44..a2c29118caf2 100644 --- a/sys/netinet/libalias/alias.c +++ b/sys/netinet/libalias/alias.c @@ -173,7 +173,7 @@ TcpMonitorIn(struct ip *pip, struct alias_link *lnk) { 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 @@ TcpMonitorOut(struct ip *pip, struct alias_link *lnk) { 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 @@ IcmpAliasIn1(struct libalias *la, struct ip *pip) 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 @@ IcmpAliasIn2(struct libalias *la, struct ip *pip) 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 @@ IcmpAliasIn(struct libalias *la, struct ip *pip) 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 @@ IcmpAliasOut1(struct libalias *la, struct ip *pip) 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 @@ IcmpAliasOut2(struct libalias *la, struct ip *pip) 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 @@ IcmpAliasOut(struct libalias *la, struct ip *pip, int create) 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 @@ UdpAliasIn(struct libalias *la, struct ip *pip) 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 @@ UdpAliasOut(struct libalias *la, struct ip *pip, int create) 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 @@ TcpAliasIn(struct libalias *la, struct ip *pip) 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 @@ TcpAliasOut(struct libalias *la, struct ip *pip, int maxpacketsize, int create) 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 @@ TcpAliasOut(struct libalias *la, struct ip *pip, int maxpacketsize, int create) 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 @@ LibAliasUnaliasOut(struct libalias *la, char *ptr, /* valid IP packet */ || (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) diff --git a/sys/netinet/libalias/alias_cuseeme.c b/sys/netinet/libalias/alias_cuseeme.c index bca6b3b28154..cbacc55cad8d 100644 --- a/sys/netinet/libalias/alias_cuseeme.c +++ b/sys/netinet/libalias/alias_cuseeme.c @@ -70,14 +70,13 @@ struct client_info { 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 @@ AliasHandleCUSeeMeIn(struct libalias *la, struct ip *pip, struct in_addr origina (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); diff --git a/sys/netinet/libalias/alias_db.c b/sys/netinet/libalias/alias_db.c index e41106600356..f9c953b27b1f 100644 --- a/sys/netinet/libalias/alias_db.c +++ b/sys/netinet/libalias/alias_db.c @@ -1908,7 +1908,7 @@ packet size was altered is searched. 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 @@ packet size was altered is searched. 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 @@ been altered, then this list will begin to overwrite itself. 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); diff --git a/sys/netinet/libalias/alias_ftp.c b/sys/netinet/libalias/alias_ftp.c index 79938db218ed..b628528a0c1d 100644 --- a/sys/netinet/libalias/alias_ftp.c +++ b/sys/netinet/libalias/alias_ftp.c @@ -114,7 +114,7 @@ AliasHandleFtpOut( 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 @@ NewFtpMessage(struct libalias *la, struct ip *pip, #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; diff --git a/sys/netinet/libalias/alias_irc.c b/sys/netinet/libalias/alias_irc.c index 06abab0501eb..66106fa35112 100644 --- a/sys/netinet/libalias/alias_irc.c +++ b/sys/netinet/libalias/alias_irc.c @@ -80,7 +80,7 @@ AliasHandleIrcOut(struct libalias *la, 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; diff --git a/sys/netinet/libalias/alias_local.h b/sys/netinet/libalias/alias_local.h index bf923a29aad2..67947da81853 100644 --- a/sys/netinet/libalias/alias_local.h +++ b/sys/netinet/libalias/alias_local.h @@ -324,6 +324,32 @@ enum alias_tcp_state { 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_ */ diff --git a/sys/netinet/libalias/alias_nbt.c b/sys/netinet/libalias/alias_nbt.c index 151bc9d934d1..b51abd1c5da5 100644 --- a/sys/netinet/libalias/alias_nbt.c +++ b/sys/netinet/libalias/alias_nbt.c @@ -218,10 +218,10 @@ AliasHandleUdpNbt( (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 @@ AliasHandleUdpNbtNS( 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); diff --git a/sys/netinet/libalias/alias_pptp.c b/sys/netinet/libalias/alias_pptp.c index 499b5fe2fea6..174780fc29fe 100644 --- a/sys/netinet/libalias/alias_pptp.c +++ b/sys/netinet/libalias/alias_pptp.c @@ -193,7 +193,7 @@ AliasHandlePptpOut(struct libalias *la, 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 @@ AliasHandlePptpIn(struct libalias *la, *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 @@ AliasVerifyPptp(struct ip *pip, u_int16_t * ptype) 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 @@ AliasVerifyPptp(struct ip *pip, u_int16_t * ptype) 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 @@ AliasHandlePptpGreOut(struct libalias *la, struct ip *pip) 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 @@ AliasHandlePptpGreIn(struct libalias *la, struct ip *pip) 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) diff --git a/sys/netinet/libalias/alias_proxy.c b/sys/netinet/libalias/alias_proxy.c index 24d8bfee73b0..e0e1c059ed68 100644 --- a/sys/netinet/libalias/alias_proxy.c +++ b/sys/netinet/libalias/alias_proxy.c @@ -283,7 +283,7 @@ ProxyEncodeTcpStream(struct alias_link *lnk, 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 @@ ProxyEncodeIpHeader(struct ip *pip, 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 @@ ProxyCheck(struct libalias *la, struct ip *pip, 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; diff --git a/sys/netinet/libalias/alias_skinny.c b/sys/netinet/libalias/alias_skinny.c index 6a64063cdd99..fccecc6764bb 100644 --- a/sys/netinet/libalias/alias_skinny.c +++ b/sys/netinet/libalias/alias_skinny.c @@ -223,12 +223,12 @@ AliasHandleSkinny(struct libalias *la, struct ip *pip, struct alias_link *lnk) 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 diff --git a/sys/netinet/libalias/alias_smedia.c b/sys/netinet/libalias/alias_smedia.c index ef19bea95203..888937c9f241 100644 --- a/sys/netinet/libalias/alias_smedia.c +++ b/sys/netinet/libalias/alias_smedia.c @@ -155,7 +155,7 @@ alias_rtsp_out(struct libalias *la, struct ip *pip, 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 @@ alias_pna_out(struct libalias *la, struct ip *pip, /* 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 @@ AliasHandleRtspOut(struct libalias *la, struct ip *pip, struct alias_link *lnk, (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; diff --git a/sys/netinet/libalias/alias_util.c b/sys/netinet/libalias/alias_util.c index 14df53d97768..687168e830b7 100644 --- a/sys/netinet/libalias/alias_util.c +++ b/sys/netinet/libalias/alias_util.c @@ -102,7 +102,7 @@ TcpChecksum(struct ip *pip) 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 */