From 0dafa2815000e24e9d0e6b9f03837aaa2be3839f Mon Sep 17 00:00:00 2001
From: Jan Dittberner <jan@dittberner.info>
Date: Thu, 5 May 2016 11:13:53 +0200
Subject: [PATCH] Make sure that values are IP addresses/ranges

This commit adds safeguarding for invalid IP address/range values.
---
 README.rst            |  5 +++++
 jandd/sphinxext/ip.py | 11 +++++++++++
 2 files changed, 16 insertions(+)

diff --git a/README.rst b/README.rst
index 97ccd17..563d9f7 100644
--- a/README.rst
+++ b/README.rst
@@ -28,6 +28,11 @@ Contributors
 Changes
 =======
 
+0.2.1 - to be released
+----------------------
+
+* fix handling of invalid IP address/range values
+
 0.2.0 - 2016-05-04
 ------------------
 
diff --git a/jandd/sphinxext/ip.py b/jandd/sphinxext/ip.py
index a236356..7582159 100644
--- a/jandd/sphinxext/ip.py
+++ b/jandd/sphinxext/ip.py
@@ -52,6 +52,17 @@ class IPXRefRole(XRefRole):
         super(IPXRefRole, self).__init__(
             innernodeclass=innernodeclass, **kwargs)
 
+    def __call__(self, typ, rawtext, text, lineno, inliner,
+                 options={}, content=[]):
+        try:
+            Network(text)
+        except ValueError as e:
+            env = inliner.document.settings.env
+            env.warn(env.docname, "invalid ip address/range %s" % text, lineno)
+            return [nodes.literal(text, text), []]
+        return super(IPXRefRole, self).__call__(
+            typ, rawtext, text, lineno, inliner, options, content)
+
     def process_link(self, env, refnode, has_explicit_title, title, target):
         domaindata = env.domaindata['ip']
         domaindata[self.method][target] = (target, refnode)