| 1 |
extern "C" { |
| 2 |
#include "Python.h" |
| 3 |
#include "patricia.h" |
| 4 |
} |
| 5 |
|
| 6 |
#ifdef SWIG |
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
%typemap(in) (const char* cidr, int size) (PyObject* ascii) |
| 11 |
%{ |
| 12 |
Py_ssize_t len; |
| 13 |
|
| 14 |
#if PY_MAJOR_VERSION >= 3 |
| 15 |
if ( PyUnicode_Check($input) ) |
| 16 |
{ |
| 17 |
ascii = PyUnicode_AsASCIIString($input); |
| 18 |
|
| 19 |
if ( ! ascii ) |
| 20 |
{ |
| 21 |
PyErr_SetString(PyExc_TypeError, "Expected a ASCII encodable string or bytes"); |
| 22 |
return NULL; |
| 23 |
} |
| 24 |
|
| 25 |
PyBytes_AsStringAndSize(ascii, &$1, &len); |
| 26 |
$2 = len; |
| 27 |
} |
| 28 |
else if ( PyBytes_Check($input) ) |
| 29 |
{ |
| 30 |
PyBytes_AsStringAndSize($input, &$1, &len); |
| 31 |
$2 = len; |
| 32 |
} |
| 33 |
else |
| 34 |
{ |
| 35 |
PyErr_SetString(PyExc_TypeError, "Expected a string or bytes"); |
| 36 |
return NULL; |
| 37 |
} |
| 38 |
#else |
| 39 |
if ( ! PyString_Check($input) ) |
| 40 |
{ |
| 41 |
PyErr_SetString(PyExc_TypeError, "Expected a string or bytes"); |
| 42 |
return NULL; |
| 43 |
} |
| 44 |
else |
| 45 |
{ |
| 46 |
PyString_AsStringAndSize($input, &$1, &len); |
| 47 |
$2 = len; |
| 48 |
} |
| 49 |
#endif |
| 50 |
%} |
| 51 |
|
| 52 |
%typemap(arginit) (const char* cidr, int size) |
| 53 |
{ |
| 54 |
ascii$argnum = NULL; |
| 55 |
} |
| 56 |
|
| 57 |
%typemap(freearg) (const char* cidr, int size) |
| 58 |
{ |
| 59 |
Py_XDECREF(ascii$argnum); |
| 60 |
} |
| 61 |
|
| 62 |
%typecheck(SWIG_TYPECHECK_STRING) (const char* cidr, int size) |
| 63 |
{ |
| 64 |
|
| 65 |
|
| 66 |
$1 = 1; |
| 67 |
} |
| 68 |
|
| 69 |
#endif |
| 70 |
|
| 71 |
typedef union _inx_addr { |
| 72 |
struct in_addr sin; |
| 73 |
struct in6_addr sin6; |
| 74 |
} inx_addr; |
| 75 |
|
| 76 |
class SubnetTree { |
| 77 |
public: |
| 78 |
SubnetTree(bool binary_lookup_mode = false); |
| 79 |
~SubnetTree(); |
| 80 |
|
| 81 |
PyObject* insert(const char* cidr, PyObject* data = 0); |
| 82 |
PyObject* insert(unsigned long subnet, unsigned short mask, PyObject* data = 0); |
| 83 |
|
| 84 |
PyObject* remove(const char* cidr); |
| 85 |
PyObject* remove(unsigned long subnet, unsigned short mask); |
| 86 |
|
| 87 |
PyObject* lookup(const char* cidr, int size) const; |
| 88 |
PyObject* lookup(unsigned long addr) const; |
| 89 |
|
| 90 |
PyObject* search_all(const char* cidr, int size) const; |
| 91 |
|
| 92 |
PyObject* prefixes(bool ipv4_native = false, bool with_len = true) const; |
| 93 |
|
| 94 |
bool get_binary_lookup_mode(); |
| 95 |
void set_binary_lookup_mode(bool binary_lookup_mode = true); |
| 96 |
|
| 97 |
#ifndef SWIG |
| 98 |
bool operator[](const char* addr) const { return lookup(addr, strlen(addr)); } |
| 99 |
bool operator[](unsigned long addr) const { return lookup(addr); } |
| 100 |
#else |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
%extend { |
| 105 |
|
| 106 |
PyObject* __contains__(const char* cidr, int size) { |
| 107 |
if ( ! cidr ) { |
| 108 |
PyErr_SetString(PyExc_TypeError, "index must be string"); |
| 109 |
return 0; |
| 110 |
} |
| 111 |
|
| 112 |
PyObject* obj = self->lookup(cidr, size); |
| 113 |
if ( obj ) { |
| 114 |
Py_DECREF(obj); |
| 115 |
} |
| 116 |
|
| 117 |
if ( PyErr_Occurred() ) |
| 118 |
return 0; |
| 119 |
|
| 120 |
if ( obj != 0 ) |
| 121 |
Py_RETURN_TRUE; |
| 122 |
else |
| 123 |
Py_RETURN_FALSE; |
| 124 |
} |
| 125 |
|
| 126 |
PyObject* __contains__(unsigned long addr) { |
| 127 |
PyObject* obj = self->lookup(addr); |
| 128 |
|
| 129 |
if ( obj ) { |
| 130 |
Py_DECREF(obj); |
| 131 |
} |
| 132 |
|
| 133 |
if ( PyErr_Occurred() ) |
| 134 |
return 0; |
| 135 |
|
| 136 |
if ( obj != 0 ) |
| 137 |
Py_RETURN_TRUE; |
| 138 |
else |
| 139 |
Py_RETURN_FALSE; |
| 140 |
} |
| 141 |
|
| 142 |
PyObject* __getitem__(const char* cidr, int size) { |
| 143 |
if ( ! cidr ) { |
| 144 |
PyErr_SetString(PyExc_TypeError, "index must be string"); |
| 145 |
return 0; |
| 146 |
} |
| 147 |
|
| 148 |
PyObject* data = self->lookup(cidr, size); |
| 149 |
if ( ! data ) { |
| 150 |
PyErr_SetObject(PyExc_KeyError, PyBytes_FromStringAndSize(cidr, size)); |
| 151 |
return 0; |
| 152 |
} |
| 153 |
|
| 154 |
return data; |
| 155 |
} |
| 156 |
|
| 157 |
PyObject* __setitem__(const char* cidr, PyObject* data) { |
| 158 |
if ( ! cidr ) { |
| 159 |
PyErr_SetString(PyExc_TypeError, "index must be string"); |
| 160 |
return 0; |
| 161 |
} |
| 162 |
|
| 163 |
if ( self->insert(cidr, data) ) |
| 164 |
Py_RETURN_TRUE; |
| 165 |
else |
| 166 |
return 0; |
| 167 |
} |
| 168 |
|
| 169 |
PyObject* __delitem__(const char* cidr) { |
| 170 |
if ( ! cidr ) { |
| 171 |
PyErr_SetString(PyExc_TypeError, "index must be string"); |
| 172 |
return 0; |
| 173 |
} |
| 174 |
|
| 175 |
if ( self->remove(cidr) ) |
| 176 |
Py_RETURN_TRUE; |
| 177 |
else |
| 178 |
return 0; |
| 179 |
} |
| 180 |
} |
| 181 |
#endif |
| 182 |
|
| 183 |
private: |
| 184 |
PyObject* insert(int family, inx_addr subnet, unsigned short mask, PyObject* data); |
| 185 |
PyObject* remove(int family, inx_addr subnet, unsigned short mask); |
| 186 |
PyObject* lookup(int family, inx_addr subnet) const; |
| 187 |
|
| 188 |
static void PatriciaDeleteFunction(void* data); |
| 189 |
|
| 190 |
patricia_tree_t* tree; |
| 191 |
bool binary_lookup_mode; |
| 192 |
}; |