Skip to content

Commit 6c0752b

Browse files
author
ldx
committed
Move code finding native libraries to util.py.
Now we use util.find_library() to find both system libraries and our libxtwrapper.so.
1 parent 72f59bb commit 6c0752b

4 files changed

Lines changed: 48 additions & 46 deletions

File tree

iptc/ip4tc.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
import socket
77
import struct
88
import weakref
9-
import ctypes.util
109

10+
from util import find_library
1111
from xtables import (XT_INV_PROTO, NFPROTO_IPV4, XTablesError, xtables,
1212
xt_align, xt_counters, xt_entry_target, xt_entry_match)
1313

@@ -83,12 +83,7 @@ class IPTCError(Exception):
8383
"""
8484

8585

86-
_libiptc_file = ctypes.util.find_library("ip4tc")
87-
if not _libiptc_file:
88-
_libiptc_file = ctypes.util.find_library("iptc")
89-
if not _libiptc_file:
90-
raise IPTCError("error: libiptc/libip4tc not found")
91-
_libiptc = ct.CDLL(_libiptc_file, use_errno=True)
86+
_libiptc = find_library("ip4tc", "iptc") # old iptables versions use iptc
9287

9388

9489
class iptc(object):

iptc/ip6tc.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,10 @@
33
import ctypes as ct
44
import socket
55
import weakref
6-
import ctypes.util
76

87
from ip4tc import Rule, Table, IPTCError
8+
from util import find_library, load_kernel
99
from xtables import (XT_INV_PROTO, NFPROTO_IPV6, xt_align, xt_counters)
10-
from util import load_kernel
1110

1211
__all__ = ["Table6", "Rule6"]
1312

@@ -82,10 +81,7 @@ class ip6t_entry(ct.Structure):
8281
("elems", ct.c_ubyte * 0)] # the matches then the target
8382

8483

85-
_libiptc_file = ctypes.util.find_library("ip6tc")
86-
if not _libiptc_file:
87-
raise IPTCError("error: libip6tc not found")
88-
_libiptc = ct.CDLL(_libiptc_file, use_errno=True)
84+
_libiptc = find_library("ip6tc", "iptc") # old iptables versions use iptc
8985

9086

9187
class ip6tc(object):

iptc/util.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import ctypes
2+
import ctypes.util
13
from subprocess import Popen, PIPE
24

35

@@ -26,3 +28,39 @@ def load_kernel(name, exc_if_failed=False):
2628
err = err[:-1]
2729
if exc_if_failed:
2830
raise Exception(err)
31+
32+
33+
def _find_library(name):
34+
p = ctypes.util.find_library(name)
35+
if p:
36+
lib = ctypes.CDLL(p, mode=ctypes.RTLD_GLOBAL)
37+
return lib
38+
39+
# probably we have been installed in a virtualenv
40+
import os
41+
from distutils.sysconfig import get_python_lib
42+
try:
43+
lib = ctypes.CDLL(os.path.join(get_python_lib(), 'lib%s.so' % (name)),
44+
mode=ctypes.RTLD_GLOBAL)
45+
return lib
46+
except:
47+
pass
48+
49+
import sys
50+
for p in sys.path:
51+
try:
52+
lib = ctypes.CDLL(os.path.join(p, 'lib%s.so' % (name)),
53+
mode=ctypes.RTLD_GLOBAL)
54+
return lib
55+
except:
56+
pass
57+
return None
58+
59+
60+
def find_library(*names):
61+
lib = None
62+
for name in names:
63+
lib = _find_library(name)
64+
if lib is not None:
65+
break
66+
return lib

iptc/xtables.py

Lines changed: 6 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
# -*- coding: utf-8 -*-
22

33
import ctypes as ct
4-
import ctypes.util
54
import weakref
65
import version
76

7+
from util import find_library
8+
89
XT_INV_PROTO = 0x40 # invert the sense of PROTO
910

1011
NFPROTO_UNSPEC = 0
@@ -274,42 +275,14 @@ class XTablesError(Exception):
274275
"""Raised when an xtables call fails for some reason."""
275276

276277

277-
_libc = ct.CDLL(ctypes.util.find_library("c"))
278+
_libc = find_library("c")
278279
_optind = ct.c_long.in_dll(_libc, "optind")
279280
_optarg = ct.c_char_p.in_dll(_libc, "optarg")
280281

282+
_lib_xtables = find_library("xtables")
283+
284+
_lib_xtwrapper = find_library("xtwrapper")
281285

282-
_lib_xtables = ct.CDLL(ctypes.util.find_library("xtables"),
283-
mode=ct.RTLD_GLOBAL)
284-
285-
286-
def _get_library(name):
287-
p = ctypes.util.find_library(name)
288-
if p:
289-
lib = ct.CDLL(p, mode=ct.RTLD_GLOBAL)
290-
return lib
291-
292-
# probably we have been installed in a virtualenv
293-
import os
294-
from distutils.sysconfig import get_python_lib
295-
try:
296-
lib = ct.CDLL(os.path.join(get_python_lib(), 'lib%s.so' % (name)),
297-
mode=ct.RTLD_GLOBAL)
298-
return lib
299-
except:
300-
pass
301-
302-
import sys
303-
for p in sys.path:
304-
try:
305-
lib = ct.CDLL(os.path.join(p, 'lib%s.so' % (name)),
306-
mode=ct.RTLD_GLOBAL)
307-
return lib
308-
except:
309-
pass
310-
return None
311-
312-
_lib_xtwrapper = _get_library("xtwrapper")
313286
_throw = _lib_xtwrapper.throw_exception
314287

315288
_wrap_parse = _lib_xtwrapper.wrap_parse

0 commit comments

Comments
 (0)