summaryrefslogtreecommitdiff
path: root/lib/python2.7/site-packages/setools/policyrep/netcontext.py
diff options
context:
space:
mode:
Diffstat (limited to 'lib/python2.7/site-packages/setools/policyrep/netcontext.py')
-rwxr-xr-x[-rw-r--r--]lib/python2.7/site-packages/setools/policyrep/netcontext.py25
1 files changed, 22 insertions, 3 deletions
diff --git a/lib/python2.7/site-packages/setools/policyrep/netcontext.py b/lib/python2.7/site-packages/setools/policyrep/netcontext.py
index 4c9b6ec..6a70a5a 100644..100755
--- a/lib/python2.7/site-packages/setools/policyrep/netcontext.py
+++ b/lib/python2.7/site-packages/setools/policyrep/netcontext.py
@@ -16,7 +16,7 @@
# License along with SETools. If not, see
# <http://www.gnu.org/licenses/>.
#
-import socket
+from socket import IPPROTO_TCP, IPPROTO_UDP, getprotobyname
from collections import namedtuple
from . import qpol
@@ -25,6 +25,10 @@ from . import context
port_range = namedtuple("port_range", ["low", "high"])
+# Python does not have a constant
+# for the DCCP protocol.
+IPPROTO_DCCP = getprotobyname("dccp")
+
def netifcon_factory(policy, name):
"""Factory function for creating netifcon objects."""
@@ -146,8 +150,23 @@ class PortconProtocol(int):
corresponding protocol string (udp, tcp).
"""
- _proto_to_text = {socket.IPPROTO_TCP: 'tcp',
- socket.IPPROTO_UDP: 'udp'}
+ _proto_to_text = {IPPROTO_DCCP: 'dccp',
+ IPPROTO_TCP: 'tcp',
+ IPPROTO_UDP: 'udp'}
+
+ def __new__(cls, value):
+ try:
+ # convert string representation
+ num = getprotobyname(value)
+ except TypeError:
+ num = value
+
+ if num not in cls._proto_to_text:
+ raise ValueError("{0} is not a supported IP protocol. "
+ "Values such as {1} (TCP) or {2} (UDP) should be used.".
+ format(value, IPPROTO_TCP, IPPROTO_UDP))
+
+ return super(PortconProtocol, cls).__new__(cls, num)
def __str__(self):
return self._proto_to_text[self]