blob: 2a5ec4e8184139a2874adffea00fd7d97b5f1a2a [file] [log] [blame]
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +02001/*
2 * SCCP management code
3 *
Holger Hans Peter Freytherefca5412010-01-27 12:12:46 +01004 * (C) 2009, 2010 by Holger Hans Peter Freyther <zecke@selfish.org>
5 * (C) 2009, 2010 by on-waves.com
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +02006 *
7 * All Rights Reserved
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 *
23 */
24
25#include <string.h>
26
27#include <sccp/sccp.h>
28
29#include <openbsc/debug.h>
30#include <openbsc/talloc.h>
31#include <openbsc/linuxlist.h>
32
33static void *tall_sccp_ctx;
34static LLIST_HEAD(sccp_connections);
35
36#define SCCP_MSG_SIZE 4096
37#define SCCP_MSG_HEADROOM 128
38
39/* global data */
40const struct sockaddr_sccp sccp_ssn_bssap = {
41 .sccp_family = 0,
42 .sccp_ssn = SCCP_SSN_BSSAP,
43};
44
45struct sccp_system {
46 /* layer3 -> layer2 */
47 int (*write_data)(struct msgb *data, void *context);
48 void *write_context;
49};
50
51
52static struct sccp_system sccp_system = {
53 .write_data = NULL,
54};
55
56struct sccp_data_callback {
57 /* connection based */
58 int (*accept_cb)(struct sccp_connection *, void *);
59 void *accept_context;
60
61 /* connection less */
62 int (*read_cb)(struct msgb *, unsigned int, void *);
63 void *read_context;
64
65 u_int8_t ssn;
66 struct llist_head callback;
67};
68
69static LLIST_HEAD(sccp_callbacks);
70
71static struct sccp_data_callback *_find_ssn(u_int8_t ssn)
72{
73 struct sccp_data_callback *cb;
74
75 llist_for_each_entry(cb, &sccp_callbacks, callback) {
76 if (cb->ssn == ssn)
77 return cb;
78 }
79
80 /* need to add one */
81 cb = talloc_zero(tall_sccp_ctx, struct sccp_data_callback);
82 if (!cb) {
83 DEBUGP(DSCCP, "Failed to allocate sccp callback.\n");
84 return NULL;
85 }
86
87 cb->ssn = ssn;
88 llist_add_tail(&cb->callback, &sccp_callbacks);
89 return cb;
90}
91
92
93static int _send_msg(struct msgb *msg)
94{
95 return sccp_system.write_data(msg, sccp_system.write_context);
96}
97
98/*
99 * parsing routines
100 */
101static int copy_address(struct sccp_address *addr, u_int8_t offset, struct msgb *msgb)
102{
103 struct sccp_called_party_address *party;
104
105 int room = msgb_l2len(msgb) - offset;
106 u_int8_t read = 0;
107 u_int8_t length;
108
109 if (room <= 0) {
110 DEBUGP(DSCCP, "Not enough room for an address: %u\n", room);
111 return -1;
112 }
113
114 length = msgb->l2h[offset];
115 if (room <= length) {
116 DEBUGP(DSCCP, "Not enough room for optional data %u %u\n", room, length);
117 return -1;
118 }
119
120
121 party = (struct sccp_called_party_address *)(msgb->l2h + offset + 1);
122 if (party->point_code_indicator) {
123 if (length <= read + 2) {
124 DEBUGP(DSCCP, "POI does not fit %u\n", length);
125 return -1;
126 }
127
128
129 memcpy(&addr->poi, &party->data[read], 2);
130 read += 2;
131 }
132
133 if (party->ssn_indicator) {
134 if (length <= read + 1) {
135 DEBUGP(DSCCP, "SSN does not fit %u\n", length);
136 return -1;
137 }
138
139 addr->ssn = party->data[read];
140 read += 1;
141 }
142
143 if (party->global_title_indicator) {
144 DEBUGP(DSCCP, "GTI not supported %u\n", *(u_int8_t *)party);
145 return -1;
146 }
147
148 addr->address = *party;
149 return 0;
150}
151
152static int check_address(struct sccp_address *addr)
153{
154 /* ignore point_code_indicator... it should be zero... but */
155 if (addr->address.ssn_indicator != 1
156 || addr->address.global_title_indicator == 1
157 || addr->address.routing_indicator != 1) {
158 DEBUGP(DSCCP, "Invalid called address according to 08.06: 0x%x 0x%x\n",
159 *(u_int8_t *)&addr->address, addr->ssn);
160 return -1;
161 }
162
163 return 0;
164}
165
166static int _sccp_parse_optional_data(const int offset,
167 struct msgb *msgb, struct sccp_optional_data *data)
168{
169 u_int16_t room = msgb_l2len(msgb) - offset;
170 u_int16_t read = 0;
171
172 while (room > read) {
173 u_int8_t type = msgb->l2h[offset + read];
174 if (type == SCCP_PNC_END_OF_OPTIONAL)
175 return 0;
176
177 if (read + 1 >= room) {
178 DEBUGP(DSCCP, "no place for length\n");
179 return 0;
180 }
181
182 u_int8_t length = msgb->l2h[offset + read + 1];
183 read += 2 + length;
184
185
186 if (room <= read) {
187 DEBUGP(DSCCP, "no space for the data: type: %d read: %d room: %d l2: %d\n",
188 type, read, room, msgb_l2len(msgb));
189 return 0;
190 }
191
192 if (type == SCCP_PNC_DATA) {
193 data->data_len = length;
194 data->data_start = offset + read - length;
195 }
196
197 }
198
199 return -1;
200}
201
Holger Hans Peter Freythera8cd2e62010-01-27 12:25:13 +0100202int _sccp_parse_connection_request(struct msgb *msgb, struct sccp_parse_result *result)
Holger Hans Peter Freytherefca5412010-01-27 12:12:46 +0100203{
Holger Hans Peter Freythera8cd2e62010-01-27 12:25:13 +0100204 static const u_int32_t header_size =
205 sizeof(struct sccp_connection_request);
206 static const u_int32_t optional_offset =
207 offsetof(struct sccp_connection_request, optional_start);
208 static const u_int32_t called_offset =
209 offsetof(struct sccp_connection_request, variable_called);
210
211 struct sccp_connection_request *req = (struct sccp_connection_request *)msgb->data;
212 struct sccp_optional_data optional_data;
213
214 /* header check */
215 if (msgb_l2len(msgb) < header_size) {
216 DEBUGP(DSCCP, "msgb < header_size %u %u\n",
217 msgb_l2len(msgb), header_size);
218 return -1;
219 }
220
221 /* copy out the calling and called address. Add the offset */
222 if (copy_address(&result->called, called_offset + req->variable_called, msgb) != 0)
223 return -1;
224
225 if (check_address(&result->called) != 0) {
226 DEBUGP(DSCCP, "Invalid called address according to 08.06: 0x%x 0x%x\n",
227 *(u_int8_t *)&result->called.address, result->called.ssn);
228 return -1;
229 }
230
231 result->source_local_reference = &req->source_local_reference;
232
233 /*
234 * parse optional data.
235 */
236 memset(&optional_data, 0, sizeof(optional_data));
237 if (_sccp_parse_optional_data(optional_offset + req->optional_start, msgb, &optional_data) != 0) {
238 DEBUGP(DSCCP, "parsing of optional data failed.\n");
239 return -1;
240 }
241
242 if (optional_data.data_len != 0) {
243 msgb->l3h = &msgb->l2h[optional_data.data_start];
244 result->data_len = optional_data.data_len;
245 } else {
246 result->data_len = 0;
247 }
248
249 return 0;
Holger Hans Peter Freytherefca5412010-01-27 12:12:46 +0100250}
251
Holger Hans Peter Freyther88fe6ee2010-01-29 03:49:32 +0100252int _sccp_parse_connection_released(struct msgb *msgb, struct sccp_parse_result *result)
Holger Hans Peter Freytherefca5412010-01-27 12:12:46 +0100253{
Holger Hans Peter Freyther88fe6ee2010-01-29 03:49:32 +0100254 static int header_size = sizeof(struct sccp_connection_released);
255 static int optional_offset = offsetof(struct sccp_connection_released, optional_start);
256
257 struct sccp_optional_data optional_data;
258 struct sccp_connection_released *rls = (struct sccp_connection_released *) msgb->l2h;
259
260 /* we don't have enough size for the struct */
261 if (msgb_l2len(msgb) < header_size) {
262 DEBUGP(DSCCP, "msgb > header_size %u %u\n",
263 msgb_l2len(msgb), header_size);
264 return -1;
265 }
266
267 memset(&optional_data, 0, sizeof(optional_data));
268 if (_sccp_parse_optional_data(optional_offset + rls->optional_start, msgb, &optional_data) != 0) {
269 DEBUGP(DSCCP, "parsing of optional data failed.\n");
270 return -1;
271 }
272
273 result->source_local_reference = &rls->source_local_reference;
274 result->destination_local_reference = &rls->destination_local_reference;
275
276 if (optional_data.data_len != 0) {
277 msgb->l3h = &msgb->l2h[optional_data.data_start];
278 result->data_len = optional_data.data_len;
279 } else {
280 result->data_len = 0;
281 }
282
283 return 0;
Holger Hans Peter Freytherefca5412010-01-27 12:12:46 +0100284}
285
Holger Hans Peter Freytherfe5de4e2010-01-29 03:58:12 +0100286int _sccp_parse_connection_refused(struct msgb *msgb, struct sccp_parse_result *result)
Holger Hans Peter Freytherefca5412010-01-27 12:12:46 +0100287{
Holger Hans Peter Freytherfe5de4e2010-01-29 03:58:12 +0100288 static const u_int32_t header_size =
289 sizeof(struct sccp_connection_refused);
290 static int optional_offset = offsetof(struct sccp_connection_refused, optional_start);
291
292 struct sccp_optional_data optional_data;
293 struct sccp_connection_refused *ref;
294
295 /* header check */
296 if (msgb_l2len(msgb) < header_size) {
297 DEBUGP(DSCCP, "msgb < header_size %u %u\n",
298 msgb_l2len(msgb), header_size);
299 return -1;
300 }
301
302 ref = (struct sccp_connection_refused *) msgb->l2h;
303
304 result->destination_local_reference = &ref->destination_local_reference;
305
306 memset(&optional_data, 0, sizeof(optional_data));
307 if (_sccp_parse_optional_data(optional_offset + ref->optional_start, msgb, &optional_data) != 0) {
308 DEBUGP(DSCCP, "parsing of optional data failed.\n");
309 return -1;
310 }
311
312 /* optional data */
313 if (optional_data.data_len != 0) {
314 msgb->l3h = &msgb->l2h[optional_data.data_start];
315 result->data_len = optional_data.data_len;
316 } else {
317 result->data_len = 0;
318 }
319
320 return 0;
Holger Hans Peter Freytherefca5412010-01-27 12:12:46 +0100321}
322
Holger Hans Peter Freytherca1d1d12010-01-29 04:03:00 +0100323int _sccp_parse_connection_confirm(struct msgb *msgb, struct sccp_parse_result *result)
Holger Hans Peter Freytherefca5412010-01-27 12:12:46 +0100324{
Holger Hans Peter Freytherca1d1d12010-01-29 04:03:00 +0100325 static u_int32_t header_size =
326 sizeof(struct sccp_connection_confirm);
327 static const u_int32_t optional_offset =
328 offsetof(struct sccp_connection_confirm, optional_start);
329
330 struct sccp_optional_data optional_data;
331 struct sccp_connection_confirm *con;
332
333 /* header check */
334 if (msgb_l2len(msgb) < header_size) {
335 DEBUGP(DSCCP, "msgb < header_size %u %u\n",
336 msgb_l2len(msgb), header_size);
337 return -1;
338 }
339
340 con = (struct sccp_connection_confirm *) msgb->l2h;
341 result->destination_local_reference = &con->destination_local_reference;
342 result->source_local_reference = &con->source_local_reference;
343
344 memset(&optional_data, 0, sizeof(optional_data));
345 if (_sccp_parse_optional_data(optional_offset + con->optional_start, msgb, &optional_data) != 0) {
346 DEBUGP(DSCCP, "parsing of optional data failed.\n");
347 return -1;
348 }
349
350 if (optional_data.data_len != 0) {
351 msgb->l3h = &msgb->l2h[optional_data.data_start];
352 result->data_len = optional_data.data_len;
353 } else {
354 result->data_len = 0;
355 }
356
357 return 0;
Holger Hans Peter Freytherefca5412010-01-27 12:12:46 +0100358}
359
360int _sccp_parse_connection_release_complete(struct msgb *msg, struct sccp_parse_result *result)
361{
362 return -1;
363}
364
365int _sccp_parse_connection_dt1(struct msgb *msg, struct sccp_parse_result *result)
366{
367 return -1;
368}
369
370int _sccp_parse_udt(struct msgb *msgb, struct sccp_parse_result *result)
371{
372 static const u_int32_t header_size = sizeof(struct sccp_data_unitdata);
373 static const u_int32_t called_offset = offsetof(struct sccp_data_unitdata, variable_called);
374 static const u_int32_t calling_offset = offsetof(struct sccp_data_unitdata, variable_calling);
375 static const u_int32_t data_offset = offsetof(struct sccp_data_unitdata, variable_data);
376
377 struct sccp_data_unitdata *udt = (struct sccp_data_unitdata *)msgb->l2h;
378
379 if (msgb_l2len(msgb) < header_size) {
380 DEBUGP(DSCCP, "msgb < header_size %u %u\n",
381 msgb_l2len(msgb), header_size);
382 return -1;
383 }
384
385 /* copy out the calling and called address. Add the off */
386 if (copy_address(&result->called, called_offset + udt->variable_called, msgb) != 0)
387 return -1;
388
389 if (check_address(&result->called) != 0) {
390 DEBUGP(DSCCP, "Invalid called address according to 08.06: 0x%x 0x%x\n",
391 *(u_int8_t *)&result->called.address, result->called.ssn);
392 return -1;
393 }
394
395 if (copy_address(&result->calling, calling_offset + udt->variable_calling, msgb) != 0)
396 return -1;
397
398 if (check_address(&result->calling) != 0) {
399 DEBUGP(DSCCP, "Invalid called address according to 08.06: 0x%x 0x%x\n",
400 *(u_int8_t *)&result->called.address, result->called.ssn);
401 }
402
403 /* we don't have enough size for the data */
404 if (msgb_l2len(msgb) < data_offset + udt->variable_data + 1) {
405 DEBUGP(DSCCP, "msgb < header + offset %u %u %u\n",
406 msgb_l2len(msgb), header_size, udt->variable_data);
407 return -1;
408 }
409
410
411 msgb->l3h = &udt->data[udt->variable_data];
412
413 if (msgb_l3len(msgb) != msgb->l3h[-1]) {
414 DEBUGP(DSCCP, "msgb is truncated %u %u\n",
415 msgb_l3len(msgb), msgb->l3h[-1]);
416 return -1;
417 }
418
419 return 0;
420}
421
422
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200423/*
424 * Send UDT. Currently we have a fixed address...
425 */
426static int _sccp_send_data(int class, const struct sockaddr_sccp *in,
427 const struct sockaddr_sccp *out, struct msgb *payload)
428{
429 struct sccp_data_unitdata *udt;
430 u_int8_t *data;
431 int ret;
432
433 if (msgb_l3len(payload) > 256) {
434 DEBUGP(DSCCP, "The payload is too big for one udt\n");
435 return -1;
436 }
437
438 struct msgb *msg = msgb_alloc_headroom(SCCP_MSG_SIZE,
439 SCCP_MSG_HEADROOM, "sccp: udt");
440 msg->l2h = &msg->data[0];
441 udt = (struct sccp_data_unitdata *)msgb_put(msg, sizeof(*udt));
442
443 udt->type = SCCP_MSG_TYPE_UDT;
444 udt->proto_class = class;
445 udt->variable_called = 3;
446 udt->variable_calling = 5;
447 udt->variable_data = 7;
448
449 /* for variable data we start with a size and the data */
450 data = msgb_put(msg, 1 + 2);
451 data[0] = 2;
452 data[1] = 0x42;
453 data[2] = out->sccp_ssn;
454
455 data = msgb_put(msg, 1 + 2);
456 data[0] = 2;
457 data[1] = 0x42;
458 data[2] = in->sccp_ssn;
459
460 /* copy the payload */
461 data = msgb_put(msg, 1 + msgb_l3len(payload));
462 data[0] = msgb_l3len(payload);
463 memcpy(&data[1], payload->l3h, msgb_l3len(payload));
464
465 ret = _send_msg(msg);
466 msgb_free(msg);
467
468 return ret;
469}
470
471static int _sccp_handle_read(struct msgb *msgb)
472{
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200473 struct sccp_data_callback *cb;
Holger Hans Peter Freytherefca5412010-01-27 12:12:46 +0100474 struct sccp_parse_result result;
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200475
Holger Hans Peter Freytherefca5412010-01-27 12:12:46 +0100476 if (_sccp_parse_udt(msgb, &result) != 0)
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200477 return -1;
478
Holger Hans Peter Freytherefca5412010-01-27 12:12:46 +0100479 cb = _find_ssn(result.called.ssn);
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200480 if (!cb || !cb->read_cb) {
Holger Hans Peter Freytherefca5412010-01-27 12:12:46 +0100481 DEBUGP(DSCCP, "No routing for UDT for called SSN: %u\n", result.called.ssn);
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200482 return -1;
483 }
484
485 /* sanity check */
486 return cb->read_cb(msgb, msgb_l3len(msgb), cb->read_context);
487}
488
489/*
490 * handle connection orientated methods
491 */
492static int source_local_reference_is_free(struct sccp_source_reference *reference)
493{
494 struct sccp_connection *connection;
495
496 llist_for_each_entry(connection, &sccp_connections, list) {
497 if (memcmp(reference, &connection->source_local_reference, sizeof(*reference)) == 0)
498 return -1;
499 }
500
501 return 0;
502}
503
504static int destination_local_reference_is_free(struct sccp_source_reference *reference)
505{
506 struct sccp_connection *connection;
507
508 llist_for_each_entry(connection, &sccp_connections, list) {
509 if (memcmp(reference, &connection->destination_local_reference, sizeof(*reference)) == 0)
510 return -1;
511 }
512
513 return 0;
514}
515
516static int assign_source_local_reference(struct sccp_connection *connection)
517{
518 static u_int32_t last_ref = 0x30000;
519 int wrapped = 0;
520
521 do {
522 struct sccp_source_reference reference;
523 reference.octet1 = (last_ref >> 0) & 0xff;
524 reference.octet2 = (last_ref >> 8) & 0xff;
525 reference.octet3 = (last_ref >> 16) & 0xff;
526
527 ++last_ref;
528 /* do not use the reversed word and wrap around */
529 if ((last_ref & 0x00FFFFFF) == 0x00FFFFFF) {
530 DEBUGP(DSCCP, "Wrapped searching for a free code\n");
531 last_ref = 0;
532 ++wrapped;
533 }
534
535 if (source_local_reference_is_free(&reference) == 0) {
536 connection->source_local_reference = reference;
537 return 0;
538 }
539 } while (wrapped != 2);
540
541 DEBUGP(DSCCP, "Finding a free reference failed\n");
542 return -1;
543}
544
545static void _sccp_set_connection_state(struct sccp_connection *connection, int new_state)
546{
547 int old_state = connection->connection_state;
548
549 connection->connection_state = new_state;
550 if (connection->state_cb)
551 connection->state_cb(connection, old_state);
552}
553
Holger Hans Peter Freythera8cd2e62010-01-27 12:25:13 +0100554static int _sccp_send_refuse(struct sccp_source_reference *src_ref, int cause)
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200555{
556 struct msgb *msgb;
557 struct sccp_connection_refused *ref;
558 u_int8_t *data;
559 int ret;
560
561 msgb = msgb_alloc_headroom(SCCP_MSG_SIZE,
562 SCCP_MSG_HEADROOM, "sccp ref");
563 msgb->l2h = &msgb->data[0];
564
565 ref = (struct sccp_connection_refused *) msgb_put(msgb, sizeof(*ref));
566 ref->type = SCCP_MSG_TYPE_CREF;
Holger Hans Peter Freythera8cd2e62010-01-27 12:25:13 +0100567 memcpy(&ref->destination_local_reference, src_ref,
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200568 sizeof(struct sccp_source_reference));
569 ref->cause = cause;
570 ref->optional_start = 1;
571
572 data = msgb_put(msgb, 1);
573 data[0] = SCCP_PNC_END_OF_OPTIONAL;
574
575 ret = _send_msg(msgb);
576 msgb_free(msgb);
577 return ret;
578}
579
580static int _sccp_send_connection_confirm(struct sccp_connection *connection)
581{
582 struct msgb *response;
583 struct sccp_connection_confirm *confirm;
584 u_int8_t *optional_data;
585 int ret;
586
587 if (assign_source_local_reference(connection) != 0)
588 return -1;
589
590 response = msgb_alloc_headroom(SCCP_MSG_SIZE,
591 SCCP_MSG_HEADROOM, "sccp confirm");
592 response->l2h = &response->data[0];
593
594 confirm = (struct sccp_connection_confirm *) msgb_put(response, sizeof(*confirm));
595
596 confirm->type = SCCP_MSG_TYPE_CC;
597 memcpy(&confirm->destination_local_reference,
598 &connection->destination_local_reference,
599 sizeof(connection->destination_local_reference));
600 memcpy(&confirm->source_local_reference,
601 &connection->source_local_reference,
602 sizeof(connection->source_local_reference));
603 confirm->proto_class = 2;
604 confirm->optional_start = 1;
605
606 optional_data = (u_int8_t *) msgb_put(response, 1);
607 optional_data[0] = SCCP_PNC_END_OF_OPTIONAL;
608
609 ret = _send_msg(response);
610 msgb_free(response);
611
612 _sccp_set_connection_state(connection, SCCP_CONNECTION_STATE_ESTABLISHED);
613 return ret;
614}
615
616static int _sccp_send_connection_request(struct sccp_connection *connection,
617 const struct sockaddr_sccp *called, struct msgb *msg)
618{
619 struct msgb *request;
620 struct sccp_connection_request *req;
621 u_int8_t *data;
622 u_int8_t extra_size = 3 + 1;
623 int ret;
624
625
626 if (msg && (msgb_l3len(msg) < 3 || msgb_l3len(msg) > 130)) {
627 DEBUGP(DSCCP, "Invalid amount of data... %d\n", msgb_l3len(msg));
628 return -1;
629 }
630
631 /* try to find a id */
632 if (assign_source_local_reference(connection) != 0) {
633 DEBUGP(DSCCP, "Assigning a local reference failed.\n");
634 _sccp_set_connection_state(connection, SCCP_CONNECTION_STATE_SETUP_ERROR);
635 return -1;
636 }
637
638
639 if (msg)
640 extra_size += 2 + msgb_l3len(msg);
641 request = msgb_alloc_headroom(SCCP_MSG_SIZE,
642 SCCP_MSG_HEADROOM, "sccp connection request");
643 request->l2h = &request->data[0];
644 req = (struct sccp_connection_request *) msgb_put(request, sizeof(*req));
645
646 req->type = SCCP_MSG_TYPE_CR;
647 memcpy(&req->source_local_reference, &connection->source_local_reference,
648 sizeof(connection->source_local_reference));
649 req->proto_class = 2;
650 req->variable_called = 2;
651 req->optional_start = 4;
652
653 /* write the called party address */
654 data = msgb_put(request, 1 + 2);
655 data[0] = 2;
656 data[1] = 0x42;
657 data[2] = called->sccp_ssn;
658
659 /* write the payload */
660 if (msg) {
661 data = msgb_put(request, 2 + msgb_l3len(msg));
662 data[0] = SCCP_PNC_DATA;
663 data[1] = msgb_l3len(msg);
664 memcpy(&data[2], msg->l3h, msgb_l3len(msg));
665 }
666
667 data = msgb_put(request, 1);
668 data[0] = SCCP_PNC_END_OF_OPTIONAL;
669
670 llist_add_tail(&connection->list, &sccp_connections);
671 _sccp_set_connection_state(connection, SCCP_CONNECTION_STATE_REQUEST);
672
673 ret = _send_msg(request);
674 msgb_free(request);
675
676 return ret;
677}
678
679static int _sccp_send_connection_data(struct sccp_connection *conn, struct msgb *_data)
680{
681 struct msgb *msgb;
682 struct sccp_data_form1 *dt1;
683 u_int8_t *data;
684 int extra_size;
685 int ret;
686
687 if (msgb_l3len(_data) < 2 || msgb_l3len(_data) > 256) {
688 DEBUGP(DSCCP, "data size too big, segmenting unimplemented.\n");
689 return -1;
690 }
691
692 extra_size = 1 + msgb_l3len(_data);
693 msgb = msgb_alloc_headroom(SCCP_MSG_SIZE,
694 SCCP_MSG_HEADROOM, "sccp dt1");
695 msgb->l2h = &msgb->data[0];
696
697 dt1 = (struct sccp_data_form1 *) msgb_put(msgb, sizeof(*dt1));
698 dt1->type = SCCP_MSG_TYPE_DT1;
699 memcpy(&dt1->destination_local_reference, &conn->destination_local_reference,
700 sizeof(struct sccp_source_reference));
701 dt1->segmenting = 0;
702
703 /* copy the data */
704 dt1->variable_start = 1;
705 data = msgb_put(msgb, extra_size);
706 data[0] = extra_size - 1;
707 memcpy(&data[1], _data->l3h, extra_size - 1);
708
709 ret = _send_msg(msgb);
710 msgb_free(msgb);
711
712 return ret;
713}
714
Holger Hans Peter Freyther3b9516e2009-11-18 22:11:28 +0100715static int _sccp_send_connection_it(struct sccp_connection *conn)
716{
717 struct msgb *msgb;
718 struct sccp_data_it *it;
719 int ret;
720
721 msgb = msgb_alloc_headroom(SCCP_MSG_SIZE,
722 SCCP_MSG_HEADROOM, "sccp it");
723 msgb->l2h = &msgb->data[0];
724 it = (struct sccp_data_it *) msgb_put(msgb, sizeof(*it));
725 it->type = SCCP_MSG_TYPE_IT;
726 memcpy(&it->destination_local_reference, &conn->destination_local_reference,
727 sizeof(struct sccp_source_reference));
728 memcpy(&it->source_local_reference, &conn->source_local_reference,
729 sizeof(struct sccp_source_reference));
730
731 it->proto_class = 0x2;
732 it->sequencing[0] = it->sequencing[1] = 0;
733 it->credit = 0;
734
735 ret = _send_msg(msgb);
736 msgb_free(msgb);
737 return ret;
738}
739
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200740static int _sccp_send_connection_released(struct sccp_connection *conn, int cause)
741{
742 struct msgb *msg;
743 struct sccp_connection_released *rel;
744 u_int8_t *data;
745 int ret;
746
747 msg = msgb_alloc_headroom(SCCP_MSG_SIZE, SCCP_MSG_HEADROOM,
748 "sccp: connection released");
749 msg->l2h = &msg->data[0];
750 rel = (struct sccp_connection_released *) msgb_put(msg, sizeof(*rel));
751 rel->type = SCCP_MSG_TYPE_RLSD;
752 rel->release_cause = cause;
753
754 /* copy the source references */
755 memcpy(&rel->destination_local_reference, &conn->destination_local_reference,
756 sizeof(struct sccp_source_reference));
757 memcpy(&rel->source_local_reference, &conn->source_local_reference,
758 sizeof(struct sccp_source_reference));
759
760 data = msgb_put(msg, 1);
761 data[0] = SCCP_PNC_END_OF_OPTIONAL;
762
763 _sccp_set_connection_state(conn, SCCP_CONNECTION_STATE_RELEASE);
764 ret = _send_msg(msg);
765 msgb_free(msg);
766
767 return ret;
768}
769
770/*
771 * Open a connection. The following is going to happen:
772 *
773 * - Verify the packet, e.g. that we have no other connection
774 * that id.
775 * - Ask the user if he wants to accept the connection
776 * - Try to open the connection by assigning a source local reference
777 * and sending the packet
778 */
779static int _sccp_handle_connection_request(struct msgb *msgb)
780{
Holger Hans Peter Freythera8cd2e62010-01-27 12:25:13 +0100781 struct sccp_parse_result result;
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200782
783 struct sccp_data_callback *cb;
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200784 struct sccp_connection *connection;
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200785
Holger Hans Peter Freythera8cd2e62010-01-27 12:25:13 +0100786 if (_sccp_parse_connection_request(msgb, &result) != 0)
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200787 return -1;
788
Holger Hans Peter Freythera8cd2e62010-01-27 12:25:13 +0100789 cb = _find_ssn(result.called.ssn);
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200790 if (!cb || !cb->accept_cb) {
Holger Hans Peter Freythera8cd2e62010-01-27 12:25:13 +0100791 DEBUGP(DSCCP, "No routing for CR for called SSN: %u\n", result.called.ssn);
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200792 return -1;
793 }
794
795 /* check if the system wants this connection */
796 connection = talloc_zero(tall_sccp_ctx, struct sccp_connection);
797 if (!connection) {
798 DEBUGP(DSCCP, "Allocation failed\n");
799 return -1;
800 }
801
802 /*
803 * sanity checks:
804 * - Is the source_local_reference in any other connection?
805 * then will call accept, assign a "destination" local reference
806 * and send a connection confirm, otherwise we will send a refuseed
807 * one....
808 */
Holger Hans Peter Freythera8cd2e62010-01-27 12:25:13 +0100809 if (destination_local_reference_is_free(result.source_local_reference) != 0) {
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200810 DEBUGP(DSCCP, "Need to reject connection with existing reference\n");
Holger Hans Peter Freythera8cd2e62010-01-27 12:25:13 +0100811 _sccp_send_refuse(result.source_local_reference, SCCP_REFUSAL_SCCP_FAILURE);
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200812 talloc_free(connection);
813 return -1;
814 }
815
816 connection->incoming = 1;
Holger Hans Peter Freythera8cd2e62010-01-27 12:25:13 +0100817 connection->destination_local_reference = *result.source_local_reference;
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200818
819 if (cb->accept_cb(connection, cb->accept_context) != 0) {
Holger Hans Peter Freythera8cd2e62010-01-27 12:25:13 +0100820 _sccp_send_refuse(result.source_local_reference, SCCP_REFUSAL_END_USER_ORIGINATED);
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200821 _sccp_set_connection_state(connection, SCCP_CONNECTION_STATE_REFUSED);
822 talloc_free(connection);
823 return 0;
824 }
825
826
827 llist_add_tail(&connection->list, &sccp_connections);
828
829 if (_sccp_send_connection_confirm(connection) != 0) {
830 DEBUGP(DSCCP, "Sending confirm failed... no available source reference?\n");
831
Holger Hans Peter Freythera8cd2e62010-01-27 12:25:13 +0100832 _sccp_send_refuse(result.source_local_reference, SCCP_REFUSAL_SCCP_FAILURE);
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200833 _sccp_set_connection_state(connection, SCCP_CONNECTION_STATE_REFUSED);
834 llist_del(&connection->list);
835 talloc_free(connection);
836
837 return -1;
838 }
839
840 /*
841 * If we have data let us forward things.
842 */
Holger Hans Peter Freythera8cd2e62010-01-27 12:25:13 +0100843 if (result.data_len != 0 && connection->data_cb) {
844 connection->data_cb(connection, msgb, result.data_len);
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200845 }
846
847 return 0;
848}
849
850/* Handle the release confirmed */
851static int _sccp_handle_connection_release_complete(struct msgb *data)
852{
853 static int header_size = sizeof(struct sccp_connection_release_complete);
854
855 struct sccp_connection_release_complete *cmpl;
856 struct sccp_connection *conn;
857
858 /* header check */
859 if (msgb_l2len(data) < header_size) {
860 DEBUGP(DSCCP, "msgb < header_size %u %u\n",
861 msgb_l2len(data), header_size);
862 return -1;
863 }
864
865 cmpl = (struct sccp_connection_release_complete *) data->l2h;
866
867 /* find the connection */
868 llist_for_each_entry(conn, &sccp_connections, list) {
869 if (conn->data_cb
870 && memcmp(&conn->source_local_reference,
871 &cmpl->destination_local_reference,
872 sizeof(conn->source_local_reference)) == 0
873 && memcmp(&conn->destination_local_reference,
874 &cmpl->source_local_reference,
875 sizeof(conn->destination_local_reference)) == 0) {
876 goto found;
877 }
878 }
879
880
881 DEBUGP(DSCCP, "Release complete of unknown connection\n");
882 return -1;
883
884found:
885 llist_del(&conn->list);
886 _sccp_set_connection_state(conn, SCCP_CONNECTION_STATE_RELEASE_COMPLETE);
887 return 0;
888}
889
890/* Handle the Data Form 1 message */
891static int _sccp_handle_connection_dt1(struct msgb *data)
892{
893 static int variable_offset = offsetof(struct sccp_data_form1, variable_start);
894 static int header_size = sizeof(struct sccp_data_form1);
895
896 struct sccp_data_form1 *dt1 = (struct sccp_data_form1 *)data->l2h;
897 struct sccp_connection *conn;
898 int size;
899
900 /* we don't have enough size for the struct */
901 if (msgb_l2len(data) < header_size) {
902 DEBUGP(DSCCP, "msgb > header_size %u %u\n",
903 msgb_l2len(data), header_size);
904 return -1;
905 }
906
907 if (dt1->segmenting != 0) {
908 DEBUGP(DSCCP, "This packet has segmenting, not supported: %d\n", dt1->segmenting);
909 return -1;
910 }
911
912 /* lookup if we have a connection with the given reference */
913 llist_for_each_entry(conn, &sccp_connections, list) {
914 if (conn->data_cb
915 && memcmp(&conn->source_local_reference,
916 &dt1->destination_local_reference,
917 sizeof(conn->source_local_reference)) == 0) {
918
919 /* some more size checks in here */
920 if (msgb_l2len(data) < variable_offset + dt1->variable_start + 1) {
921 DEBUGP(DSCCP, "Not enough space for variable start: %u %u\n",
922 msgb_l2len(data), dt1->variable_start);
923 return -1;
924 }
925
926 size = data->l2h[variable_offset + dt1->variable_start];
927 data->l3h = &data->l2h[dt1->variable_start + variable_offset + 1];
928
929 if (msgb_l3len(data) < size) {
930 DEBUGP(DSCCP, "Not enough room for the payload: %u %u\n",
931 msgb_l3len(data), size);
932 return -1;
933 }
934
935 conn->data_cb(conn, data, size);
936 return 0;
937 }
938 }
939
940 DEBUGP(DSCCP, "No connection found for dt1 data\n");
941 return -1;
942}
943
944/* confirm a connection release */
945static int _sccp_send_connection_release_complete(struct sccp_connection *connection)
946{
947 struct msgb *msgb;
948 struct sccp_connection_release_complete *rlc;
949 int ret;
950
951 msgb = msgb_alloc_headroom(SCCP_MSG_SIZE,
952 SCCP_MSG_HEADROOM, "sccp rlc");
953 msgb->l2h = &msgb->data[0];
954
955 rlc = (struct sccp_connection_release_complete *) msgb_put(msgb, sizeof(*rlc));
956 rlc->type = SCCP_MSG_TYPE_RLC;
957 memcpy(&rlc->destination_local_reference,
958 &connection->destination_local_reference, sizeof(struct sccp_source_reference));
959 memcpy(&rlc->source_local_reference,
960 &connection->source_local_reference, sizeof(struct sccp_source_reference));
961
962 ret = _send_msg(msgb);
963 msgb_free(msgb);
964
965 /*
966 * Remove from the list of active connections and set the state. User code
967 * should now free the entry.
968 */
969 llist_del(&connection->list);
970 _sccp_set_connection_state(connection, SCCP_CONNECTION_STATE_RELEASE_COMPLETE);
971
972 return ret;
973}
974
975/* connection released, send a released confirm */
Holger Hans Peter Freyther88fe6ee2010-01-29 03:49:32 +0100976static int _sccp_handle_connection_released(struct msgb *msgb)
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200977{
Holger Hans Peter Freyther88fe6ee2010-01-29 03:49:32 +0100978 struct sccp_parse_result result;
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200979 struct sccp_connection *conn;
980
Holger Hans Peter Freyther88fe6ee2010-01-29 03:49:32 +0100981 if (_sccp_parse_connection_released(msgb, &result) == -1)
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200982 return -1;
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200983
984 /* lookup if we have a connection with the given reference */
985 llist_for_each_entry(conn, &sccp_connections, list) {
986 if (conn->data_cb
987 && memcmp(&conn->source_local_reference,
Holger Hans Peter Freyther88fe6ee2010-01-29 03:49:32 +0100988 result.destination_local_reference,
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200989 sizeof(conn->source_local_reference)) == 0
990 && memcmp(&conn->destination_local_reference,
Holger Hans Peter Freyther88fe6ee2010-01-29 03:49:32 +0100991 result.source_local_reference,
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200992 sizeof(conn->destination_local_reference)) == 0) {
993 goto found;
994 }
995 }
996
997
998 DEBUGP(DSCCP, "Unknown connection was released.\n");
999 return -1;
1000
1001 /* we have found a connection */
1002found:
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +02001003 /* optional data */
Holger Hans Peter Freyther88fe6ee2010-01-29 03:49:32 +01001004 if (result.data_len != 0 && conn->data_cb) {
1005 conn->data_cb(conn, msgb, result.data_len);
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +02001006 }
1007
1008 /* generate a response */
1009 if (_sccp_send_connection_release_complete(conn) != 0) {
1010 DEBUGP(DSCCP, "Sending release confirmed failed\n");
1011 return -1;
1012 }
1013
1014 return 0;
1015}
1016
1017static int _sccp_handle_connection_refused(struct msgb *msgb)
1018{
Holger Hans Peter Freytherfe5de4e2010-01-29 03:58:12 +01001019 struct sccp_parse_result result;
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +02001020 struct sccp_connection *conn;
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +02001021
Holger Hans Peter Freytherfe5de4e2010-01-29 03:58:12 +01001022 if (_sccp_parse_connection_refused(msgb, &result) != 0)
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +02001023 return -1;
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +02001024
1025 /* lookup if we have a connection with the given reference */
1026 llist_for_each_entry(conn, &sccp_connections, list) {
1027 if (conn->incoming == 0 && conn->data_cb
1028 && memcmp(&conn->source_local_reference,
Holger Hans Peter Freytherfe5de4e2010-01-29 03:58:12 +01001029 result.destination_local_reference,
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +02001030 sizeof(conn->source_local_reference)) == 0) {
1031 goto found;
1032 }
1033 }
1034
1035 DEBUGP(DSCCP, "Refused but no connection found\n");
1036 return -1;
1037
1038found:
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +02001039 /* optional data */
Holger Hans Peter Freytherfe5de4e2010-01-29 03:58:12 +01001040 if (result.data_len != 0 && conn->data_cb) {
1041 conn->data_cb(conn, msgb, result.data_len);
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +02001042 }
1043
1044
1045 llist_del(&conn->list);
1046 _sccp_set_connection_state(conn, SCCP_CONNECTION_STATE_REFUSED);
1047 return 0;
1048}
1049
1050static int _sccp_handle_connection_confirm(struct msgb *msgb)
1051{
Holger Hans Peter Freytherca1d1d12010-01-29 04:03:00 +01001052 struct sccp_parse_result result;
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +02001053 struct sccp_connection *conn;
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +02001054
Holger Hans Peter Freytherca1d1d12010-01-29 04:03:00 +01001055 if (_sccp_parse_connection_confirm(msgb, &result) != 0)
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +02001056 return -1;
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +02001057
1058 /* lookup if we have a connection with the given reference */
1059 llist_for_each_entry(conn, &sccp_connections, list) {
1060 if (conn->incoming == 0 && conn->data_cb
1061 && memcmp(&conn->source_local_reference,
Holger Hans Peter Freytherca1d1d12010-01-29 04:03:00 +01001062 result.destination_local_reference,
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +02001063 sizeof(conn->source_local_reference)) == 0) {
1064 goto found;
1065 }
1066 }
1067
1068 DEBUGP(DSCCP, "Confirmed but no connection found\n");
1069 return -1;
1070
1071found:
1072 /* copy the addresses of the connection */
Holger Hans Peter Freytherca1d1d12010-01-29 04:03:00 +01001073 conn->destination_local_reference = *result.source_local_reference;
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +02001074 _sccp_set_connection_state(conn, SCCP_CONNECTION_STATE_ESTABLISHED);
1075
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +02001076 /* optional data */
Holger Hans Peter Freytherca1d1d12010-01-29 04:03:00 +01001077 if (result.data_len != 0 && conn->data_cb) {
1078 conn->data_cb(conn, msgb, result.data_len);
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +02001079 }
1080
1081 return 0;
1082}
1083
1084
1085int sccp_system_init(int (*outgoing)(struct msgb *data, void *ctx), void *ctx)
1086{
1087 sccp_system.write_data = outgoing;
1088 sccp_system.write_context = ctx;
1089
1090 return 0;
1091}
1092
1093/* oh my god a real SCCP packet. need to dispatch it now */
1094int sccp_system_incoming(struct msgb *msgb)
1095{
1096 if (msgb_l2len(msgb) < 1 ) {
1097 DEBUGP(DSCCP, "Too short packet\n");
1098 return -1;
1099 }
1100
1101 int type = msgb->l2h[0];
1102
1103 switch(type) {
1104 case SCCP_MSG_TYPE_CR:
1105 return _sccp_handle_connection_request(msgb);
1106 break;
1107 case SCCP_MSG_TYPE_RLSD:
1108 return _sccp_handle_connection_released(msgb);
1109 break;
1110 case SCCP_MSG_TYPE_CREF:
1111 return _sccp_handle_connection_refused(msgb);
1112 break;
1113 case SCCP_MSG_TYPE_CC:
1114 return _sccp_handle_connection_confirm(msgb);
1115 break;
1116 case SCCP_MSG_TYPE_RLC:
1117 return _sccp_handle_connection_release_complete(msgb);
1118 break;
1119 case SCCP_MSG_TYPE_DT1:
1120 return _sccp_handle_connection_dt1(msgb);
1121 break;
1122 case SCCP_MSG_TYPE_UDT:
1123 return _sccp_handle_read(msgb);
1124 break;
1125 default:
1126 DEBUGP(DSCCP, "unimplemented msg type: %d\n", type);
1127 };
1128
1129 return -1;
1130}
1131
1132/* create a packet from the data */
1133int sccp_connection_write(struct sccp_connection *connection, struct msgb *data)
1134{
1135 if (connection->connection_state < SCCP_CONNECTION_STATE_CONFIRM
1136 || connection->connection_state > SCCP_CONNECTION_STATE_ESTABLISHED) {
1137 DEBUGP(DSCCP, "sccp_connection_write: Wrong connection state: %p %d\n",
1138 connection, connection->connection_state);
1139 return -1;
1140 }
1141
1142 return _sccp_send_connection_data(connection, data);
1143}
1144
Holger Hans Peter Freyther3b9516e2009-11-18 22:11:28 +01001145/*
1146 * Send a Inactivity Test message. The owner of the connection
1147 * should start a timer and call this method regularily. Calling
1148 * this every 60 seconds should be good enough.
1149 */
1150int sccp_connection_send_it(struct sccp_connection *connection)
1151{
1152 if (connection->connection_state < SCCP_CONNECTION_STATE_CONFIRM
1153 || connection->connection_state > SCCP_CONNECTION_STATE_ESTABLISHED) {
1154 DEBUGP(DSCCP, "sccp_connection_write: Wrong connection state: %p %d\n",
1155 connection, connection->connection_state);
1156 return -1;
1157 }
1158
1159 return _sccp_send_connection_it(connection);
1160}
1161
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +02001162/* send a connection release and wait for the connection released */
1163int sccp_connection_close(struct sccp_connection *connection, int cause)
1164{
1165 if (connection->connection_state < SCCP_CONNECTION_STATE_CONFIRM
1166 || connection->connection_state > SCCP_CONNECTION_STATE_ESTABLISHED) {
1167 DEBUGPC(DSCCP, "Can not close the connection. It was never opened: %p %d\n",
1168 connection, connection->connection_state);
1169 return -1;
1170 }
1171
1172 return _sccp_send_connection_released(connection, cause);
1173}
1174
1175int sccp_connection_free(struct sccp_connection *connection)
1176{
1177 if (connection->connection_state > SCCP_CONNECTION_STATE_NONE
1178 && connection->connection_state < SCCP_CONNECTION_STATE_RELEASE_COMPLETE) {
1179 DEBUGP(DSCCP, "The connection needs to be released before it is freed");
1180 return -1;
1181 }
1182
1183 talloc_free(connection);
1184 return 0;
1185}
1186
1187struct sccp_connection *sccp_connection_socket(void)
1188{
1189 return talloc_zero(tall_sccp_ctx, struct sccp_connection);
1190}
1191
1192int sccp_connection_connect(struct sccp_connection *conn,
1193 const struct sockaddr_sccp *local,
1194 struct msgb *data)
1195{
1196 return _sccp_send_connection_request(conn, local, data);
1197}
1198
1199int sccp_connection_set_incoming(const struct sockaddr_sccp *sock,
1200 int (*accept_cb)(struct sccp_connection *, void *), void *context)
1201{
1202 struct sccp_data_callback *cb;
1203
1204 if (!sock)
1205 return -2;
1206
1207 cb = _find_ssn(sock->sccp_ssn);
1208 if (!cb)
1209 return -1;
1210
1211 cb->accept_cb = accept_cb;
1212 cb->accept_context = context;
1213 return 0;
1214}
1215
1216int sccp_write(struct msgb *data, const struct sockaddr_sccp *in,
1217 const struct sockaddr_sccp *out, int class)
1218{
1219 return _sccp_send_data(class, in, out, data);
1220}
1221
1222int sccp_set_read(const struct sockaddr_sccp *sock,
1223 int (*read_cb)(struct msgb *, unsigned int, void *), void *context)
1224{
1225 struct sccp_data_callback *cb;
1226
1227 if (!sock)
1228 return -2;
1229
1230 cb = _find_ssn(sock->sccp_ssn);
1231 if (!cb)
1232 return -1;
1233
1234 cb->read_cb = read_cb;
1235 cb->read_context = context;
1236 return 0;
1237}
1238
1239static_assert(sizeof(struct sccp_source_reference) <= sizeof(u_int32_t), enough_space);
1240
1241u_int32_t sccp_src_ref_to_int(struct sccp_source_reference *ref)
1242{
1243 u_int32_t src_ref = 0;
1244 memcpy(&src_ref, ref, sizeof(*ref));
1245 return src_ref;
1246}
1247
1248struct sccp_source_reference sccp_src_ref_from_int(u_int32_t int_ref)
1249{
1250 struct sccp_source_reference ref;
1251 memcpy(&ref, &int_ref, sizeof(ref));
1252 return ref;
1253}
1254
Holger Hans Peter Freythera692fbc2010-01-13 09:55:43 +01001255int sccp_determine_msg_type(struct msgb *msg)
1256{
1257 if (msgb_l2len(msg) < 1)
1258 return -1;
1259
1260 return msg->l2h[0];
1261}
1262
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +02001263static __attribute__((constructor)) void on_dso_load(void)
1264{
1265 tall_sccp_ctx = talloc_named_const(NULL, 1, "sccp");
1266}
1267
1268static __attribute__((destructor)) void on_dso_unload(void)
1269{
1270 talloc_report_full(tall_sccp_ctx, stderr);
1271}