diff --git a/ipcalc.c b/ipcalc.c
index f2bc47a..04c9372 100644
--- a/ipcalc.c
+++ b/ipcalc.c
@@ -146,7 +146,12 @@ static struct in_addr calc_broadcast(struct in_addr addr, int prefix)
mask.s_addr = prefix2mask(prefix);
memset(&broadcast, 0, sizeof(broadcast));
- broadcast.s_addr = (addr.s_addr & mask.s_addr) | ~mask.s_addr;
+
+ /* if prefix is set to 31 return 255.255.255.255 (RFC3021) */
+ if (mask.s_addr == htonl(0xFFFFFFFE))
+ broadcast.s_addr = htonl(0xFFFFFFFF);
+ else
+ broadcast.s_addr = (addr.s_addr & mask.s_addr) | ~mask.s_addr;
return broadcast;
}
diff --git a/tests/192.168.1.5-31 b/tests/192.168.1.5-31
index fee8227..b720b56 100644
--- a/tests/192.168.1.5-31
+++ b/tests/192.168.1.5-31
@@ -1,4 +1,4 @@
NETMASK=255.255.255.254
PREFIX=31
-BROADCAST=192.168.1.5
+BROADCAST=255.255.255.255
NETWORK=192.168.1.4