aboutsummaryrefslogtreecommitdiffstats
path: root/wpaspy/wpaspy.c
blob: 278089b48adaf24559759f981fdce84ac9ec39c4 (plain)
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/*
 * Python bindings for wpa_ctrl (wpa_supplicant/hostapd control interface)
 * Copyright (c) 2013, Jouni Malinen <j@w1.fi>
 *
 * This software may be distributed under the terms of the BSD license.
 * See README for more details.
 */

#include <Python.h>
#include <structmember.h>

#include "wpa_ctrl.h"


struct wpaspy_obj {
	PyObject_HEAD
	struct wpa_ctrl *ctrl;
	int attached;
};

static PyObject *wpaspy_error;


static int wpaspy_open(struct wpaspy_obj *self, PyObject *args)
{
	const char *path;

	if (!PyArg_ParseTuple(args, "s", &path))
		return -1;
	self->ctrl = wpa_ctrl_open(path);
	if (self->ctrl == NULL)
		return -1;
	self->attached = 0;
	return 0;
}


static void wpaspy_close(struct wpaspy_obj *self)
{
	if (self->ctrl) {
		if (self->attached)
			wpa_ctrl_detach(self->ctrl);
		wpa_ctrl_close(self->ctrl);
		self->ctrl = NULL;
	}

	if (self->ob_type)
		self->ob_type->tp_free((PyObject *) self);
}


static PyObject * wpaspy_request(struct wpaspy_obj *self, PyObject *args)
{
	const char *cmd;
	char buf[4096];
	size_t buflen;
	int ret;

	if (!PyArg_ParseTuple(args, "s", &cmd))
		return NULL;

	buflen = sizeof(buf) - 1;
	ret = wpa_ctrl_request(self->ctrl, cmd, strlen(cmd), buf, &buflen,
			       NULL);
	if (ret == -2) {
		PyErr_SetString(wpaspy_error, "Request timed out");
		return NULL;
	}
	if (ret) {
		PyErr_SetString(wpaspy_error, "Request failed");
		return NULL;
	}

	buf[buflen] = '\0';
	return Py_BuildValue("s", buf);
}


static PyObject * wpaspy_attach(struct wpaspy_obj *self)
{
	int ret;

	if (self->attached)
		Py_RETURN_NONE;

	ret = wpa_ctrl_attach(self->ctrl);
	if (ret) {
		PyErr_SetString(wpaspy_error, "Attach failed");
		return NULL;
	}
	Py_RETURN_NONE;
}


static PyObject * wpaspy_detach(struct wpaspy_obj *self)
{
	int ret;

	if (!self->attached)
		Py_RETURN_NONE;

	ret = wpa_ctrl_detach(self->ctrl);
	if (ret) {
		PyErr_SetString(wpaspy_error, "Detach failed");
		return NULL;
	}
	Py_RETURN_NONE;
}


static PyObject * wpaspy_pending(struct wpaspy_obj *self)
{
	switch (wpa_ctrl_pending(self->ctrl)) {
	case 1:
		Py_RETURN_TRUE;
	case 0:
		Py_RETURN_FALSE;
	default:
		PyErr_SetString(wpaspy_error, "wpa_ctrl_pending failed");
		break;
	}

	return NULL;
}


static PyObject * wpaspy_recv(struct wpaspy_obj *self)
{
	int ret;
	char buf[4096];
	size_t buflen;

	buflen = sizeof(buf) - 1;
	Py_BEGIN_ALLOW_THREADS
	ret = wpa_ctrl_recv(self->ctrl, buf, &buflen);
	Py_END_ALLOW_THREADS

	if (ret) {
		PyErr_SetString(wpaspy_error, "wpa_ctrl_recv failed");
		return NULL;
	}

	buf[buflen] = '\0';
	return Py_BuildValue("s", buf);
}


static PyMethodDef wpaspy_methods[] = {
	{
		"request", (PyCFunction) wpaspy_request, METH_VARARGS,
		"Send a control interface command and return response"
	},
	{
		"attach", (PyCFunction) wpaspy_attach, METH_NOARGS,
		"Attach as an event monitor"
	},
	{
		"detach", (PyCFunction) wpaspy_detach, METH_NOARGS,
		"Detach an event monitor"
	},
	{
		"pending", (PyCFunction) wpaspy_pending, METH_NOARGS,
		"Check whether any events are pending"
	},
	{
		"recv", (PyCFunction) wpaspy_recv, METH_NOARGS,
		"Received pending event"
	},
	{ NULL, NULL, 0, NULL }
};

static PyMemberDef wpaspy_members[] = {
	{
		"attached", T_INT, offsetof(struct wpaspy_obj, attached),
		READONLY,
		"Whether instance is attached as event monitor"
	},
	{ NULL }
};

static PyTypeObject wpaspy_ctrl = {
	PyObject_HEAD_INIT(NULL)
	.tp_name = "wpaspy.Ctrl",
	.tp_basicsize = sizeof(struct wpaspy_obj),
	.tp_getattro = PyObject_GenericGetAttr,
	.tp_setattro = PyObject_GenericSetAttr,
	.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
	.tp_methods = wpaspy_methods,
	.tp_members = wpaspy_members,
	.tp_init = (initproc) wpaspy_open,
	.tp_dealloc = (destructor) wpaspy_close,
	.tp_new = PyType_GenericNew,
};


static PyMethodDef module_methods[] = {
	{ NULL, NULL, 0, NULL }
};


PyMODINIT_FUNC initwpaspy(void)
{
	PyObject *mod;

	PyType_Ready(&wpaspy_ctrl);
	mod = Py_InitModule("wpaspy", module_methods);
	wpaspy_error = PyErr_NewException("wpaspy.error", NULL, NULL);

	Py_INCREF(&wpaspy_ctrl);
	Py_INCREF(wpaspy_error);

	PyModule_AddObject(mod, "Ctrl", (PyObject *) &wpaspy_ctrl);
	PyModule_AddObject(mod, "error", wpaspy_error);
}