From 39ba9d4e7fb52ca8017bd4e934d6e6f8acba8d4c Mon Sep 17 00:00:00 2001 From: Guy Harris Date: Sun, 29 Oct 2017 19:10:46 -0700 Subject: Add wsutil/inet_ipv4.h. It defines a ws_in4_addr type, which is intended to hold IPv4 addresses in network byte order, and some macros to test for IPv4 addresses in the local network control block and multicast IPv4 addresses. Use those macros in places where dissectors had their own code for that purpose. Change-Id: I4252b410e37207157be85119a332e2a6913b332f Reviewed-on: https://code.wireshark.org/review/24178 Reviewed-by: Guy Harris --- epan/dissectors/packet-cipsafety.c | 2 +- epan/dissectors/packet-icmp.c | 3 +-- epan/dissectors/packet-ip.c | 12 ++--------- epan/ipv4.h | 2 +- wsutil/Makefile.am | 1 + wsutil/inet_ipv4.h | 44 ++++++++++++++++++++++++++++++++++++++ 6 files changed, 50 insertions(+), 14 deletions(-) create mode 100644 wsutil/inet_ipv4.h diff --git a/epan/dissectors/packet-cipsafety.c b/epan/dissectors/packet-cipsafety.c index eb40a6b7a1..233e6b5161 100644 --- a/epan/dissectors/packet-cipsafety.c +++ b/epan/dissectors/packet-cipsafety.c @@ -1484,7 +1484,7 @@ static void dissect_cip_safety_data( proto_tree *tree, proto_item *item, tvbuff_t *tvb, int item_length, packet_info *pinfo, cip_safety_info_t* safety_info) { int base_length, io_data_size; - gboolean multicast = (((pntoh32(pinfo->dst.data)) & 0xf0000000) == 0xe0000000); + gboolean multicast = in4_addr_is_multicast(pntoh32(pinfo->dst.data)); gboolean server_dir = FALSE; enum enip_connid_type conn_type = ECIDT_UNKNOWN; enum cip_safety_format_type format = CIP_SAFETY_BASE_FORMAT; diff --git a/epan/dissectors/packet-icmp.c b/epan/dissectors/packet-icmp.c index e9b9767c1f..0dd9d565a9 100644 --- a/epan/dissectors/packet-icmp.c +++ b/epan/dissectors/packet-icmp.c @@ -357,8 +357,7 @@ static const value_string interface_role_str[] = { #define MPLS_EXTENDED_PAYLOAD_C_TYPE 1 /* Return true if the address is in the 224.0.0.0/4 network block */ -#define is_a_multicast_addr(a) \ - ((g_ntohl(a) & 0xf0000000) == 0xe0000000) +#define is_a_multicast_addr(a) in4_addr_is_multicast(g_ntohl(a)) /* Return true if the address is the 255.255.255.255 broadcast address */ #define is_a_broadcast_addr(a) \ diff --git a/epan/dissectors/packet-ip.c b/epan/dissectors/packet-ip.c index 998ab0a372..28be99e498 100644 --- a/epan/dissectors/packet-ip.c +++ b/epan/dissectors/packet-ip.c @@ -498,14 +498,6 @@ const value_string ip_version_vals[] = { #define IPLOCAL_NETWRK_CTRL_BLK_ANY_TTL 0x1000 /* larger than max ttl */ #define IPLOCAL_NETWRK_CTRL_BLK_DEFAULT_TTL 0X01 -/* Return true if the address is in the 224.0.0.0/24 network block */ -#define is_a_local_network_control_block_addr(addr) \ - ((addr & 0xffffff00) == 0xe0000000) - -/* Return true if the address is in the 224.0.0.0/4 network block */ -#define is_a_multicast_addr(addr) \ - ((addr & 0xf0000000) == 0xe0000000) - static void ip_prompt(packet_info *pinfo, gchar* result) { g_snprintf(result, MAX_DECODE_AS_PROMPT_LEN, "IP protocol %u as", @@ -2174,14 +2166,14 @@ dissect_ip_v4(tvbuff_t *tvb, packet_info *pinfo, proto_tree *parent_tree, void* * (e.g. 224.0.0.0/4) ... and the payload isn't protocol 103 (PIM). * (see http://tools.ietf.org/html/rfc3973#section-4.7). */ - if (is_a_local_network_control_block_addr(dst32)) { + if (in4_addr_is_local_network_control_block(dst32)) { ttl = local_network_control_block_addr_valid_ttl(dst32); if (ttl != iph->ip_ttl && ttl != IPLOCAL_NETWRK_CTRL_BLK_ANY_TTL) { expert_add_info_format(pinfo, ttl_item, &ei_ip_ttl_lncb, "\"Time To Live\" != %d for a packet sent to the " "Local Network Control Block (see RFC 3171)", ttl); } - } else if (!is_a_multicast_addr(dst32) && + } else if (!in4_addr_is_multicast(dst32) && /* At least BGP should appear here as well */ iph->ip_ttl < 5 && iph->ip_proto != IP_PROTO_PIM && diff --git a/epan/ipv4.h b/epan/ipv4.h index 979f5f52d6..af43414509 100644 --- a/epan/ipv4.h +++ b/epan/ipv4.h @@ -28,7 +28,7 @@ #ifndef __IPV4_H__ #define __IPV4_H__ -#include +#include typedef struct { guint32 addr; /* stored in host order */ diff --git a/wsutil/Makefile.am b/wsutil/Makefile.am index bea58f3dde..6cb212bbf9 100644 --- a/wsutil/Makefile.am +++ b/wsutil/Makefile.am @@ -59,6 +59,7 @@ WSUTIL_PUBLIC_INCLUDES = \ g711.h \ glib-compat.h \ inet_addr.h \ + inet_ipv4.h \ inet_ipv6.h \ interface.h \ jsmn.h \ diff --git a/wsutil/inet_ipv4.h b/wsutil/inet_ipv4.h new file mode 100644 index 0000000000..9c1094f5af --- /dev/null +++ b/wsutil/inet_ipv4.h @@ -0,0 +1,44 @@ +/* inet_ipv4.h + * + * Wireshark - Network traffic analyzer + * By Gerald Combs + * Copyright 1998 Gerald Combs + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifndef __INET_IPV4_H__ +#define __INET_IPV4_H__ + +#include + +typedef guint32 ws_in4_addr; /* 32 bit IPv4 address, in network byte order */ + +/** + * Unicast Local + * Returns true if the address is in the 224.0.0.0/24 local network + * control block + */ +#define in4_addr_is_local_network_control_block(addr) \ + ((addr & 0xffffff00) == 0xe0000000) + +/** + * Multicast + * Returns true if the address is in the 224.0.0.0/4 network block + */ +#define in4_addr_is_multicast(addr) \ + ((addr & 0xf0000000) == 0xe0000000) + +#endif -- cgit v1.2.3