summaryrefslogtreecommitdiffstats
path: root/gisi/server.c
blob: 8be109dab6541b2d16f9a7232251b06e741e61bb (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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
/*
 * This file is part of oFono - Open Source Telephony
 *
 * Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * version 2 as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA
 *
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdint.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/uio.h>
#include <sys/ioctl.h>
#include <errno.h>
#include "phonet.h"
#include <glib.h>

#include "socket.h"
#include "server.h"

#define PN_NAMESERVICE		0xDB
#define PNS_NAME_ADD_REQ	0x05

struct _GIsiIncoming {
	struct sockaddr_pn spn;
	uint8_t trans_id;
};

struct _GIsiServer {
	GIsiModem *modem;
	uint8_t resource;
	struct {
		int major;
		int minor;
	} version;

	/* Callbacks */
	int fd;
	guint source;
	GIsiRequestFunc func[256];
	void *data[256];

	/* Debugging */
	GIsiDebugFunc debug_func;
	void *debug_data;
};

static gboolean g_isi_server_callback(GIOChannel *channel, GIOCondition cond,
					gpointer data);

/**
 * Create an ISI server.
 * @param resource PhoNet resource ID for the server
 * @return NULL on error (see errno), a GIsiServer pointer on success,
 */
GIsiServer *g_isi_server_create(GIsiModem *modem, uint8_t resource,
				uint8_t major, uint8_t minor)
{
	void *ptr;
	GIsiServer *self;
	GIOChannel *channel;

	if (G_UNLIKELY(posix_memalign(&ptr, 256, sizeof(*self))))
		abort();

	self = ptr;
	memset(self, 0, sizeof(*self));
	self->resource = resource;
	self->version.major = major;
	self->version.minor = minor;
	self->modem = modem;
	self->debug_func = NULL;

	channel = phonet_new(modem, resource);
	if (channel == NULL) {
		free(self);
		return NULL;
	}

	self->fd = g_io_channel_unix_get_fd(channel);
	self->source = g_io_add_watch(channel,
					G_IO_IN|G_IO_ERR|G_IO_HUP|G_IO_NVAL,
					g_isi_server_callback, self);
	g_io_channel_unref(channel);
	return self;
}

/**
 * Returns the resource associated with @a server
 * @param server server for the resource
 * @return PhoNet resource ID for the server
 */
uint8_t g_isi_server_resource(GIsiServer *server)
{
	return server->resource;
}

/**
 * Set a debugging function for @a server. This function will be
 * called whenever an ISI protocol message is sent or received.
 * @param server server to debug
 * @param func debug function
 * @param opaque user data
 */
void g_isi_server_set_debug(GIsiServer *server, GIsiDebugFunc func,
				void *opaque)
{
	if (!server)
		return;

	server->debug_func = func;
	server->debug_data = opaque;
}

/**
 * Destroys an ISI server, cancels all pending transactions and subscriptions.
 * @param server server to destroy
 */
void g_isi_server_destroy(GIsiServer *server)
{
	if (!server)
		return;

	g_source_remove(server->source);
	free(server);
}

/**
 * Request the server name from the name server.
 */
void
g_isi_server_add_name(GIsiServer *self)
{
	uint16_t object = 0;

	if (!self)
		return;

	if (ioctl(self->fd, SIOCPNGETOBJECT, &object) < 0) {
		g_warning("%s: %s", "ioctl(SIOCPNGETOBJECT)", strerror(errno));
	} else {
		struct sockaddr_pn spn = {
			.spn_family = PF_PHONET,
			.spn_dev = 0,	/* PN_DEV_HOST */
			.spn_resource = PN_NAMESERVICE,
		};
		uint8_t req[] = {
			0, PNS_NAME_ADD_REQ, 0, 0,
			0, 0, 0, self->resource,	/* name */
			object >> 8, object & 0xff,	/* device/object */
			0, 0,
		};

		if (sendto(self->fd, req, sizeof(req), 0,
				(void *)&spn, sizeof(spn)) != sizeof(req)) {
			g_warning("%s: %s", "sendto(PN_NAMESERVICE)",
				  strerror(errno));
		}
	}
}

/**
 * Make an ISI request and register a callback to process the response(s) to
 * the resulting transaction.
 * @param self ISI server (from g_isi_server_create())
 * @param buf pointer to request payload
 * @param len request payload byte length
 * @param irq information from incoming request
 */
int g_isi_respond(GIsiServer *self, const void *data, size_t len,
			GIsiIncoming *irq)
{
	const struct iovec iov = {
		.iov_base = (void *)data,
		.iov_len = len,
	};

	if (self->debug_func)
		self->debug_func(data, len, self->debug_data);

	return g_isi_vrespond(self, &iov, 1, irq);
}

/**
 * Make an ISI request and register a callback to process the response(s) to
 * the resulting transaction.
 * @param self ISI server (from g_isi_server_create())
 * @param iov scatter-gather array to the request payload
 * @param iovlen number of vectors in the scatter-gather array
 * @param irq information from incoming request
 */
int g_isi_vrespond(GIsiServer *self, const struct iovec *iov, size_t iovlen,
			GIsiIncoming *irq)
{
	struct iovec _iov[1 + iovlen];
	const struct msghdr msg = {
		.msg_name = (void *)&irq->spn,
		.msg_namelen = sizeof(irq->spn),
		.msg_iov = (struct iovec *)_iov,
		.msg_iovlen = 1 + iovlen,
		.msg_control = NULL,
		.msg_controllen = 0,
		.msg_flags = 0,
	};
	ssize_t ret;
	size_t i, len;

	if (self == NULL) {
		errno = EINVAL;
		return -1;
	}

	if (irq == NULL) {
		errno = EINVAL;
		return -1;
	}

	_iov[0].iov_base = &irq->trans_id;
	_iov[0].iov_len = 1;
	for (i = 0, len = 1; i < iovlen; i++) {
		_iov[1 + i] = iov[i];
		len += iov[i].iov_len;
	}

	ret = sendmsg(self->fd, &msg, MSG_NOSIGNAL);

	g_free(irq);

	return ret;
}

/**
 * Prepare to handle given request type for the resource that an ISI server
 * is associated with. If the same type was already handled, the old
 * handler is overriden.
 * @param self ISI server (fomr g_isi_server_create())
 * @param type request message type
 * @param cb callback to process received requests
 * @param data data for the callback
 * @return 0 on success, -1 upon an error.
 */
int g_isi_server_handle(GIsiServer *self, uint8_t type,
			GIsiRequestFunc cb, void *data)
{
	if (self == NULL || cb == NULL) {
		errno = EINVAL;
		return -1;
	}

	self->func[type] = cb;
	self->data[type] = data;
	return 0;
}

/**
 * Remove handler from a given request type.
 * @param server ISI server (from g_isi_server_create())
 * @param type indication type.
 */
void g_isi_server_unhandle(GIsiServer *self, uint8_t type)
{
	if (self)
		self->func[type] = NULL;
}


static void generic_error_response(GIsiServer *self,
			uint8_t trans_id, uint8_t error, uint8_t message_id,
			void *addr, socklen_t addrlen)
{
	uint8_t common[] = { trans_id, 0xF0, error, message_id };

	sendto(self->fd, common, sizeof(common), MSG_NOSIGNAL, addr, addrlen);
}

static void process_message(GIsiServer *self, int len)
{
	uint8_t msg[len + 1];
	struct sockaddr_pn addr;
	socklen_t addrlen = sizeof(addr);
	uint8_t message_id;
	GIsiRequestFunc func;
	void *data;

	len = recvfrom(self->fd, msg, sizeof(msg), MSG_DONTWAIT,
			(void *)&addr, &addrlen);

	if (len < 2 || addr.spn_resource != self->resource)
		return;

	if (self->debug_func)
		self->debug_func(msg + 1, len - 1, self->debug_data);

	message_id = msg[1];
	func = self->func[message_id];
	data = self->data[message_id];

	if (func) {
		GIsiIncoming *irq = g_new0(GIsiIncoming, 1);

		if (irq) {
			irq->spn = addr;
			irq->trans_id = msg[0];
			func(self, msg + 1, len - 1, irq, data);
			return;
		}
	}

	/* Respond with COMMON MESSAGE COMM_SERVICE_NOT_AUTHENTICATED_RESP */
	generic_error_response(self, msg[0], 0x17, msg[1], &addr, addrlen);
}

/* Data callback */
static gboolean g_isi_server_callback(GIOChannel *channel, GIOCondition cond,
					gpointer opaque)
{
	if (cond & (G_IO_NVAL|G_IO_HUP)) {
		g_warning("Unexpected event on Phonet channel %p", channel);
		return FALSE;
	}

	process_message(opaque, phonet_peek_length(channel));

	return TRUE;
}