-
Notifications
You must be signed in to change notification settings - Fork 0
/
protocol_wrapper.h
162 lines (139 loc) · 5.2 KB
/
protocol_wrapper.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#ifndef CP2P_PROTOCOL_TYPE
#define CP2P_PROTOCOL_TYPE TRUE
#ifdef _cplusplus
extern "C" {
#endif
#include <Python.h>
#include <bytesobject.h>
#include "structmember.h"
#include "SubnetStruct.h"
#include "base.h"
#include "py_utils.h"
typedef struct {
PyObject_HEAD
SubnetStruct *sub;
} protocol_wrapper;
static void protocol_wrapper_dealloc(protocol_wrapper* self) {
Py_BEGIN_ALLOW_THREADS
destroySubnet(self->sub);
Py_END_ALLOW_THREADS
Py_TYPE(self)->tp_free((PyObject*)self);
}
static PyObject *protocol_wrapper_new(PyTypeObject *type, PyObject *args, PyObject *kwds) {
protocol_wrapper *self;
self = (protocol_wrapper *)type->tp_alloc(type, 0);
return (PyObject *)self;
}
static int protocol_wrapper_init(protocol_wrapper *self, PyObject *args, PyObject *kwds) {
const char *sub=NULL, *enc=NULL;
int sub_size = 0, enc_size = 0;
static char *kwlist[] = {(char*)"subnet", (char*)"encryption", NULL};
if (! PyArg_ParseTupleAndKeywords(args, kwds, "s#s#", kwlist,
&sub, &sub_size, &enc, &enc_size))
return -1;
Py_BEGIN_ALLOW_THREADS
CP2P_DEBUG("Building protocol\n");
self->sub = getSubnet((char *)sub, sub_size, (char *)enc, enc_size);
Py_END_ALLOW_THREADS
return 0;
}
static PyObject *protocol_id(protocol_wrapper *self) {
char * id;
PyObject *ret;
Py_BEGIN_ALLOW_THREADS
CP2P_DEBUG("Entering id getter\n");
id = subnetID(self->sub);
Py_END_ALLOW_THREADS
ret = PyUnicode_FromStringAndSize(id, self->sub->idSize);
if (PyErr_Occurred())
return NULL;
return ret;
}
static PyObject *protocol_subnet(protocol_wrapper *self) {
PyObject *ret = Py_BuildValue("s#", self->sub->subnet, self->sub->subnetSize);
if (PyErr_Occurred())
return NULL;
return ret;
}
static PyObject *protocol_encryption(protocol_wrapper *self) {
PyObject *ret = Py_BuildValue("s#", self->sub->encryption, self->sub->encryptionSize);
if (PyErr_Occurred())
return NULL;
return ret;
}
static PyGetSetDef protocol_wrapper_getsets[] = {
{(char*)"subnet", (getter)protocol_subnet, NULL,
(char*)"Return the protocol subnet name"
},
{(char*)"encryption", (getter)protocol_encryption, NULL,
(char*)"Return the protocol encryption method"
},
{(char*)"id", (getter)protocol_id, NULL,
(char*)"Return the protocol ID"
},
{NULL} /* Sentinel */
};
static PyObject *protocol_getitem(protocol_wrapper *self, Py_ssize_t index) {
if (index == 0 || index == -2)
return Py_BuildValue("s#", self->sub->subnet, self->sub->subnetSize);
else if (index == 1 || index == -1)
return Py_BuildValue("s#", self->sub->encryption, self->sub->encryptionSize);
PyErr_SetString(PyExc_IndexError, "tuple index out of range");
return NULL;
}
static unsigned short protocol__len__(protocol_wrapper *self) {
return 2;
}
static PySequenceMethods protocol_wrapper_sequence = {
(lenfunc)protocol__len__, /* __len__ */
0, /* __add__ */
0, /* __mul__ */
(ssizeargfunc)protocol_getitem, /* __getitem__ */
0, /* __getslice__ */
0, /* __setitem__ */
0 /* __setslice__ */
};
static PyTypeObject protocol_wrapper_type = {
PyVarObject_HEAD_INIT(NULL, 0)
"protocol", /* tp_name */
sizeof(protocol_wrapper), /* tp_basicsize */
0, /* tp_itemsize */
(destructor)protocol_wrapper_dealloc,/* tp_dealloc */
0, /* tp_print */
0, /* tp_getattr */
0, /* tp_setattr */
0, /* tp_reserved */
0, /* tp_repr */
0, /* tp_as_number */
&protocol_wrapper_sequence,/* tp_as_sequence */
0, /* tp_as_mapping */
0, /* tp_hash */
0, /* tp_call */
0, /* tp_str */
0, /* tp_getattro */
0, /* tp_setattro */
0, /* tp_as_buffer */
Py_TPFLAGS_DEFAULT, /* tp_flags */
"C implementation of the protocol object",/* tp_doc */
0, /* tp_traverse */
0, /* tp_clear */
0, /* tp_richcompare */
0, /* tp_weaklistoffset */
0, /* tp_iter */
0, /* tp_iternext */
0, /* tp_methods */
0, /* tp_members */
protocol_wrapper_getsets, /* tp_getset */
0, /* tp_base */
0, /* tp_dict */
0, /* tp_descr_get */
0, /* tp_descr_set */
0, /* tp_dictoffset */
(initproc)protocol_wrapper_init,/* tp_init */
0, /* tp_alloc */
protocol_wrapper_new, /* tp_new */
};
#ifdef _cplusplus
}
#endif
#endif