aboutsummaryrefslogtreecommitdiffstats
path: root/src/mncc.c
blob: 5e2a3a12c469bba3418cbd4406eab8829a967ab1 (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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
/*
 * (C) 2016 by Holger Hans Peter Freyther
 *
 * All Rights Reserved
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * 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 Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#include "mncc.h"
#include "mncc_protocol.h"
#include "app.h"
#include "logging.h"
#include "call.h"

#include <osmocom/gsm/protocol/gsm_03_40.h>

#include <osmocom/core/socket.h>
#include <osmocom/core/utils.h>

#include <sys/socket.h>
#include <sys/un.h>

#include <errno.h>
#include <unistd.h>

extern void *tall_mncc_ctx;

static void close_connection(struct mncc_connection *conn);

static void mncc_leg_release(struct mncc_call_leg *leg)
{
	osmo_timer_del(&leg->cmd_timeout);
	call_leg_release(&leg->base);
}

static void cmd_timeout(void *data)
{
	struct mncc_call_leg *leg = data;
	struct call_leg *other_leg;

	LOGP(DMNCC, LOGL_ERROR, "cmd(0x%x) never arrived for leg(%u)\n",
		leg->rsp_wanted, leg->callref);

	other_leg = call_leg_other(&leg->base);
	if (other_leg)
		other_leg->release_call(other_leg);
	mncc_leg_release(leg);
}

static void start_cmd_timer(struct mncc_call_leg *leg, uint32_t expected_next)
{
	leg->rsp_wanted = expected_next;

	leg->cmd_timeout.cb = cmd_timeout;
	leg->cmd_timeout.data = leg;
	osmo_timer_schedule(&leg->cmd_timeout, 5, 0);
}

static void stop_cmd_timer(struct mncc_call_leg *leg, uint32_t got_res)
{
	if (leg->rsp_wanted != got_res) {
		LOGP(DMNCC, LOGL_ERROR, "Wanted rsp(%u) but got(%u) for leg(%u)\n",
			leg->rsp_wanted, got_res, leg->callref);
		return;
	}

	LOGP(DMNCC, LOGL_DEBUG,
		"Got response(0x%x), stopping timer on leg(%u)\n",
		got_res, leg->callref);
	osmo_timer_del(&leg->cmd_timeout);
}

static struct mncc_call_leg *mncc_find_leg(uint32_t callref)
{
	struct call *call;

	llist_for_each_entry(call, &g_call_list, entry) {
		if (call->initial && call->initial->type == CALL_TYPE_MNCC) {
			struct mncc_call_leg *leg = (struct mncc_call_leg *) call->initial;
			if (leg->callref == callref)
				return leg;
		}
		if (call->remote && call->remote->type == CALL_TYPE_MNCC) {
			struct mncc_call_leg *leg = (struct mncc_call_leg *) call->remote;
			if (leg->callref == callref)
				return leg;
		}
	}

	return NULL;
}

static void mncc_fill_header(struct gsm_mncc *mncc, uint32_t msg_type, uint32_t callref)
{
	mncc->msg_type = msg_type;
	mncc->callref = callref;
}

static void mncc_write(struct mncc_connection *conn, struct gsm_mncc *mncc, uint32_t callref)
{
	int rc;

	/*
	 * TODO: we need to put cause in here for release or such? shall we return a
	 * static struct?
	 */
	rc = write(conn->fd.fd, mncc, sizeof(*mncc));
	if (rc != sizeof(*mncc)) {
		LOGP(DMNCC, LOGL_ERROR, "Failed to send message call(%u)\n", callref);
		close_connection(conn);
	}
}

static void mncc_send(struct mncc_connection *conn, uint32_t msg_type, uint32_t callref)
{
	struct gsm_mncc mncc = { 0, };

	mncc_fill_header(&mncc, msg_type, callref);
	mncc_write(conn, &mncc, callref);
}

static void mncc_rtp_send(struct mncc_connection *conn, uint32_t msg_type, uint32_t callref)
{
	int rc;
	struct gsm_mncc_rtp mncc = { 0, };

	mncc.msg_type = msg_type;
	mncc.callref = callref;

	rc = write(conn->fd.fd, &mncc, sizeof(mncc));
	if (rc != sizeof(mncc)) {
		LOGP(DMNCC, LOGL_ERROR, "Failed to send message call(%u)\n", callref);
		close_connection(conn);
	}
}

static bool send_rtp_connect(struct mncc_call_leg *leg, struct call_leg *other)
{
	struct gsm_mncc_rtp mncc = { 0, };
	int rc;

	/*
	 * Send RTP CONNECT and we handle the general failure of it by
	 * tearing down the call.
	 */
	mncc.msg_type = MNCC_RTP_CONNECT;
	mncc.callref = leg->callref;
	mncc.ip = htonl(other->ip);
	mncc.port = other->port;
	mncc.payload_type = other->payload_type;
	/*
	 * FIXME: mncc.payload_msg_type should already be compatible.. but
	 * payload_type should be different..
	 */
	rc = write(leg->conn->fd.fd, &mncc, sizeof(mncc));
	if (rc != sizeof(mncc)) {
		LOGP(DMNCC, LOGL_ERROR, "Failed to send message leg(%u)\n",
			leg->callref);
		close_connection(leg->conn);
		return false;
	}
	return true;
}

static void mncc_call_leg_connect(struct call_leg *_leg)
{
	struct mncc_call_leg *leg;
	struct call_leg *other;

	OSMO_ASSERT(_leg->type == CALL_TYPE_MNCC);
	leg = (struct mncc_call_leg *) _leg;

	other = call_leg_other(_leg);
	OSMO_ASSERT(other);

	if (!send_rtp_connect(leg, other))
		return;

	start_cmd_timer(leg, MNCC_SETUP_COMPL_IND);
	mncc_send(leg->conn, MNCC_SETUP_RSP, leg->callref);
}

static void mncc_call_leg_ring(struct call_leg *_leg)
{
	struct gsm_mncc out_mncc = { 0, };
	struct mncc_call_leg *leg;

	OSMO_ASSERT(_leg->type == CALL_TYPE_MNCC);
	leg = (struct mncc_call_leg *) _leg;

	mncc_fill_header(&out_mncc, MNCC_ALERT_REQ, leg->callref);
	/* GSM 04.08 10.5.4.21 */
	out_mncc.fields |= MNCC_F_PROGRESS;
	out_mncc.progress.coding = 3; /* Standard defined for the GSMßPLMNS */
	out_mncc.progress.location = 1; /* Private network serving the local user */
	out_mncc.progress.descr = 8; /* In-band information or appropriate pattern now available */

	mncc_write(leg->conn, &out_mncc, leg->callref);
}

static void mncc_call_leg_release(struct call_leg *_leg)
{
	struct mncc_call_leg *leg;

	OSMO_ASSERT(_leg->type == CALL_TYPE_MNCC);
	leg = (struct mncc_call_leg *) _leg;

	/* drop it directly, if not connected */
	if (leg->conn->state != MNCC_READY) {
		LOGP(DMNCC, LOGL_DEBUG,
			"MNCC not connected releasing leg leg(%u)\n", leg->callref);
		return mncc_leg_release(leg);
	}

	switch (leg->state) {
	case MNCC_CC_INITIAL:
		LOGP(DMNCC, LOGL_DEBUG,
			"Releasing call in initial-state leg(%u)\n", leg->callref);
		if (leg->dir == MNCC_DIR_MO) {
			mncc_send(leg->conn, MNCC_REJ_REQ, leg->callref);
			osmo_timer_del(&leg->cmd_timeout);
			mncc_leg_release(leg);
		} else {
			leg->base.in_release = true;
			start_cmd_timer(leg, MNCC_REL_CNF);
			mncc_send(leg->conn, MNCC_REL_REQ, leg->callref);
		}
		break;
	case MNCC_CC_PROCEEDING:
	case MNCC_CC_CONNECTED:
		LOGP(DMNCC, LOGL_DEBUG,
			"Releasing call in non-initial leg(%u)\n", leg->callref);
		leg->base.in_release = true;
		start_cmd_timer(leg, MNCC_REL_IND);
		mncc_send(leg->conn, MNCC_DISC_REQ, leg->callref);
		break;
	default:
		LOGP(DMNCC, LOGL_ERROR, "Unknown state leg(%u) state(%d)\n",
			leg->callref, leg->state);
		break;
	}
}

static void close_connection(struct mncc_connection *conn)
{
	osmo_fd_unregister(&conn->fd);
	close(conn->fd.fd);
	osmo_timer_schedule(&conn->reconnect, 5, 0);
	conn->state = MNCC_DISCONNECTED;
	if (conn->on_disconnect)
		conn->on_disconnect(conn);
}

static void continue_mo_call(struct mncc_call_leg *leg)
{
	char *dest, *source;

	/* TODO.. continue call obviously only for MO call right now */
	mncc_send(leg->conn, MNCC_CALL_PROC_REQ, leg->callref);
	leg->state = MNCC_CC_PROCEEDING;

	if (leg->called.type == GSM340_TYPE_INTERNATIONAL)
		dest = talloc_asprintf(leg, "+%.32s", leg->called.number);
	else
		dest = talloc_asprintf(leg, "%.32s", leg->called.number);

	if (leg->conn->app->use_imsi_as_id)
		source = talloc_asprintf(leg, "%.16s", leg->imsi);
	else
		source = talloc_asprintf(leg, "%.32s", leg->calling.number);

	app_route_call(leg->base.call, source, dest);
}

static void continue_mt_call(struct mncc_call_leg *leg)
{
	struct call_leg *other_leg;

	/* TODO.. check codec selection */
	other_leg = call_leg_other(&leg->base);
	if (!other_leg)
		return;

	/* assume the type is compatible */
	other_leg->payload_type = leg->base.payload_type;
}

static void continue_call(struct mncc_call_leg *leg)
{
	if (leg->dir == MNCC_DIR_MO)
		return continue_mo_call(leg);
	return continue_mt_call(leg);
}

static void check_rtp_connect(struct mncc_connection *conn, char *buf, int rc)
{
	struct gsm_mncc_rtp *rtp;
	struct mncc_call_leg *leg;
	struct call_leg *other_leg;

	if (rc < sizeof(*rtp)) {
		LOGP(DMNCC, LOGL_ERROR, "gsm_mncc_rtp of wrong size %d < %zu\n",
			rc, sizeof(*rtp));
		return close_connection(conn);
	}

	rtp = (struct gsm_mncc_rtp *) buf;
	leg = mncc_find_leg(rtp->callref);
	if (!leg) {
		LOGP(DMNCC, LOGL_ERROR, "leg(%u) can not be found\n", rtp->callref);
		return mncc_send(conn, MNCC_REJ_REQ, rtp->callref);
	}

	/* extract information about where the RTP is */
	if (rtp->ip != 0 || rtp->port != 0 || rtp->payload_type != 0)
		return;

	LOGP(DMNCC, LOGL_ERROR, "leg(%u) rtp connect failed\n", rtp->callref);

	other_leg = call_leg_other(&leg->base);
	if (other_leg)
		other_leg->release_call(other_leg);
	leg->base.release_call(&leg->base);
}

static void check_rtp_create(struct mncc_connection *conn, char *buf, int rc)
{
	struct gsm_mncc_rtp *rtp;
	struct mncc_call_leg *leg;

	if (rc < sizeof(*rtp)) {
		LOGP(DMNCC, LOGL_ERROR, "gsm_mncc_rtp of wrong size %d < %zu\n",
			rc, sizeof(*rtp));
		return close_connection(conn);
	}

	rtp = (struct gsm_mncc_rtp *) buf;
	leg = mncc_find_leg(rtp->callref);
	if (!leg) {
		LOGP(DMNCC, LOGL_ERROR, "call(%u) can not be found\n", rtp->callref);
		return mncc_send(conn, MNCC_REJ_REQ, rtp->callref);
	}

	/* extract information about where the RTP is */
	leg->base.ip = rtp->ip;
	leg->base.port = rtp->port;
	leg->base.payload_type = rtp->payload_type;
	leg->base.payload_msg_type = rtp->payload_msg_type;

	/* TODO.. now we can continue with the call */
	LOGP(DMNCC, LOGL_DEBUG,
		"RTP cnt leg(%u) ip(%u), port(%u) pt(%u) ptm(%u)\n",
		leg->callref, leg->base.ip, leg->base.port,
		leg->base.payload_type, leg->base.payload_msg_type);
	stop_cmd_timer(leg, MNCC_RTP_CREATE);
	continue_call(leg);
}

static int continue_setup(struct mncc_connection *conn, struct gsm_mncc *mncc)
{
	if (mncc->called.plan != GSM340_PLAN_ISDN) {
		LOGP(DMNCC, LOGL_ERROR,
			"leg(%u) has non(%d) ISDN dial plan. not supported.\n",
			mncc->callref, mncc->called.plan);
		return 0;
	}

	return 1;
}

static void check_setup(struct mncc_connection *conn, char *buf, int rc)
{
	struct gsm_mncc *data;
	struct call *call;
	struct mncc_call_leg *leg;

	if (rc != sizeof(*data)) {
		LOGP(DMNCC, LOGL_ERROR, "gsm_mncc of wrong size %d vs. %zu\n",
			rc, sizeof(*data));
		return close_connection(conn);
	}

	data = (struct gsm_mncc *) buf;

	/* screen arguments */
	if ((data->fields & MNCC_F_CALLED) == 0) {
		LOGP(DMNCC, LOGL_ERROR,
			"MNCC leg(%u) without called addr fields(%u)\n",
			data->callref, data->fields);
		return mncc_send(conn, MNCC_REJ_REQ, data->callref);
	}
	if ((data->fields & MNCC_F_CALLING) == 0) {
		LOGP(DMNCC, LOGL_ERROR,
			"MNCC leg(%u) without calling addr fields(%u)\n",
			data->callref, data->fields);
		return mncc_send(conn, MNCC_REJ_REQ, data->callref);
	}

	/* TODO.. bearer caps and better audio handling */
	if (!continue_setup(conn, data)) {
		LOGP(DMNCC, LOGL_ERROR,
			"MNCC screening parameters failed leg(%u)\n", data->callref);
		return mncc_send(conn, MNCC_REJ_REQ, data->callref);
	}

	/* Create an RTP port and then allocate a call */
	call = call_mncc_create();
	if (!call) {
		LOGP(DMNCC, LOGL_ERROR,
			"MNCC leg(%u) failed to allocate call\n", data->callref);
		return mncc_send(conn, MNCC_REJ_REQ, data->callref);
	}

	leg = (struct mncc_call_leg *) call->initial;
	leg->base.connect_call = mncc_call_leg_connect;
	leg->base.ring_call = mncc_call_leg_ring;
	leg->base.release_call = mncc_call_leg_release;
	leg->callref = data->callref;
	leg->conn = conn;
	leg->state = MNCC_CC_INITIAL;
	leg->dir = MNCC_DIR_MO;
	memcpy(&leg->called, &data->called, sizeof(leg->called));
	memcpy(&leg->calling, &data->calling, sizeof(leg->calling));
	memcpy(&leg->imsi, data->imsi, sizeof(leg->imsi));

	LOGP(DMNCC, LOGL_DEBUG,
		"Created call(%u) with MNCC leg(%u) IMSI(%.16s)\n",
		call->id, leg->callref, data->imsi);

	start_cmd_timer(leg, MNCC_RTP_CREATE);
	mncc_rtp_send(conn, MNCC_RTP_CREATE, data->callref);
}

static struct mncc_call_leg *find_leg(struct mncc_connection *conn,
					char *buf, int rc, struct gsm_mncc **mncc)
{
	struct mncc_call_leg *leg;

	if (rc != sizeof(**mncc)) {
		LOGP(DMNCC, LOGL_ERROR, "gsm_mncc of wrong size %d vs. %zu\n",
			rc, sizeof(**mncc));
		close_connection(conn);
		return NULL;
	}

	*mncc = (struct gsm_mncc *) buf;
	leg = mncc_find_leg((*mncc)->callref);
	if (!leg) {
		LOGP(DMNCC, LOGL_ERROR, "call(%u) can not be found\n", (*mncc)->callref);
		return NULL;
	}

	return leg;
}

static void check_disc_ind(struct mncc_connection *conn, char *buf, int rc)
{
	struct gsm_mncc *data;
	struct mncc_call_leg *leg;
	struct call_leg *other_leg;

	leg = find_leg(conn, buf, rc, &data);
	if (!leg)
		return;

	LOGP(DMNCC,
		LOGL_DEBUG, "leg(%u) was disconnected. Releasing\n", data->callref);
	leg->base.in_release = true;
	start_cmd_timer(leg, MNCC_REL_CNF);
	mncc_send(leg->conn, MNCC_REL_REQ, leg->callref);

	other_leg = call_leg_other(&leg->base);
	if (other_leg)
		other_leg->release_call(other_leg);
}

static void check_rel_ind(struct mncc_connection *conn, char *buf, int rc)
{
	struct gsm_mncc *data;
	struct mncc_call_leg *leg;

	leg = find_leg(conn, buf, rc, &data);
	if (!leg)
		return;

	if (leg->base.in_release)
		stop_cmd_timer(leg, MNCC_REL_IND);
	else {
		struct call_leg *other_leg;
		other_leg = call_leg_other(&leg->base);
		if (other_leg)
			other_leg->release_call(other_leg);
	}
	LOGP(DMNCC, LOGL_DEBUG, "leg(%u) was released.\n", data->callref);
	mncc_leg_release(leg);
}

static void check_rel_cnf(struct mncc_connection *conn, char *buf, int rc)
{
	struct gsm_mncc *data;
	struct mncc_call_leg *leg;

	leg = find_leg(conn, buf, rc, &data);
	if (!leg)
		return;

	stop_cmd_timer(leg, MNCC_REL_CNF);
	LOGP(DMNCC, LOGL_DEBUG, "leg(%u) was cnf released.\n", data->callref);
	mncc_leg_release(leg);
}

static void check_stp_cmpl_ind(struct mncc_connection *conn, char *buf, int rc)
{
	struct gsm_mncc *data;
	struct mncc_call_leg *leg;

	leg = find_leg(conn, buf, rc, &data);
	if (!leg)
		return;

	LOGP(DMNCC, LOGL_NOTICE, "leg(%u) is now connected.\n", leg->callref);
	stop_cmd_timer(leg, MNCC_SETUP_COMPL_IND);
	leg->state = MNCC_CC_CONNECTED;
}

static void check_rej_ind(struct mncc_connection *conn, char *buf, int rc)
{
	struct gsm_mncc *data;
	struct mncc_call_leg *leg;
	struct call_leg *other_leg;

	leg = find_leg(conn, buf, rc, &data);
	if (!leg)
		return;

	other_leg = call_leg_other(&leg->base);
	if (other_leg)
		other_leg->release_call(other_leg);
	LOGP(DMNCC, LOGL_DEBUG, "leg(%u) was rejected.\n", data->callref);
	mncc_leg_release(leg);
}

static void check_cnf_ind(struct mncc_connection *conn, char *buf, int rc)
{
	struct gsm_mncc *data;
	struct mncc_call_leg *leg;

	leg = find_leg(conn, buf, rc, &data);
	if (!leg)
		return;

	LOGP(DMNCC, LOGL_DEBUG,
		"leg(%u) confirmend. creating RTP socket.\n",
		leg->callref);

	start_cmd_timer(leg, MNCC_RTP_CREATE);
	mncc_rtp_send(conn, MNCC_RTP_CREATE, data->callref);
}

static void check_alrt_ind(struct mncc_connection *conn, char *buf, int rc)
{
	struct gsm_mncc *data;
	struct mncc_call_leg *leg;
	struct call_leg *other_leg;

	leg = find_leg(conn, buf, rc, &data);
	if (!leg)
		return;

	LOGP(DMNCC, LOGL_DEBUG,
		"leg(%u) is alerting.\n", leg->callref);

	other_leg = call_leg_other(&leg->base);
	if (!other_leg) {
		LOGP(DMNCC, LOGL_ERROR, "leg(%u) other leg gone!\n",
			leg->callref);
		mncc_call_leg_release(&leg->base);
		return;
	}

	other_leg->ring_call(other_leg);
}

static void check_hold_ind(struct mncc_connection *conn, char *buf, int rc)
{
	struct gsm_mncc *data;
	struct mncc_call_leg *leg;

	leg = find_leg(conn, buf, rc, &data);
	if (!leg)
		return;

	LOGP(DMNCC, LOGL_DEBUG,
		"leg(%u) is req hold. rejecting.\n", leg->callref);
	mncc_send(leg->conn, MNCC_HOLD_REJ, leg->callref);
}

static void check_stp_cnf(struct mncc_connection *conn, char *buf, int rc)
{
	struct gsm_mncc *data;
	struct mncc_call_leg *leg;
	struct call_leg *other_leg;

	leg = find_leg(conn, buf, rc, &data);
	if (!leg)
		return;

	LOGP(DMNCC, LOGL_DEBUG, "leg(%u) setup completed\n", leg->callref);

	other_leg = call_leg_other(&leg->base);
	if (!other_leg) {
		LOGP(DMNCC, LOGL_ERROR, "leg(%u) other leg gone!\n",
			leg->callref);
		mncc_call_leg_release(&leg->base);
		return;
	}

	if (!send_rtp_connect(leg, other_leg))
		return;
	leg->state = MNCC_CC_CONNECTED;
	mncc_send(leg->conn, MNCC_SETUP_COMPL_REQ, leg->callref);

	other_leg->connect_call(other_leg);
}

static void check_dtmf_start(struct mncc_connection *conn, char *buf, int rc)
{
	struct gsm_mncc out_mncc = { 0, };
	struct gsm_mncc *data;
	struct mncc_call_leg *leg;
	struct call_leg *other_leg;

	leg = find_leg(conn, buf, rc, &data);
	if (!leg)
		return;

	LOGP(DMNCC, LOGL_DEBUG, "leg(%u) DTMF key=%c\n", leg->callref, data->keypad);

	other_leg = call_leg_other(&leg->base);
	if (other_leg && other_leg->dtmf)
		other_leg->dtmf(other_leg, data->keypad);

	mncc_fill_header(&out_mncc, MNCC_START_DTMF_RSP, leg->callref);
	out_mncc.fields |= MNCC_F_KEYPAD;
	out_mncc.keypad = data->keypad;
	mncc_write(conn, &out_mncc, leg->callref);
}

static void check_dtmf_stop(struct mncc_connection *conn, char *buf, int rc)
{
	struct gsm_mncc out_mncc = { 0, };
	struct gsm_mncc *data;
	struct mncc_call_leg *leg;

	leg = find_leg(conn, buf, rc, &data);
	if (!leg)
		return;

	LOGP(DMNCC, LOGL_DEBUG, "leg(%u) DTMF key=%c\n", leg->callref, data->keypad);

	mncc_fill_header(&out_mncc, MNCC_STOP_DTMF_RSP, leg->callref);
	out_mncc.fields |= MNCC_F_KEYPAD;
	out_mncc.keypad = data->keypad;
	mncc_write(conn, &out_mncc, leg->callref);
}

static void check_hello(struct mncc_connection *conn, char *buf, int rc)
{
	struct gsm_mncc_hello *hello;

	if (rc != sizeof(*hello)) {
		LOGP(DMNCC, LOGL_ERROR, "Hello shorter than expected %d vs. %zu\n",
			rc, sizeof(*hello));
		return close_connection(conn);
	}

	hello = (struct gsm_mncc_hello *) buf;
	LOGP(DMNCC, LOGL_NOTICE, "Got hello message version %d\n", hello->version);

	if (hello->version != MNCC_SOCK_VERSION) {
		LOGP(DMNCC, LOGL_NOTICE, "Incompatible version(%d) expected %d\n",
			hello->version, MNCC_SOCK_VERSION);
		return close_connection(conn);
	}

	conn->state = MNCC_READY;
}

int mncc_create_remote_leg(struct mncc_connection *conn, struct call *call)
{
	struct mncc_call_leg *leg;
	struct gsm_mncc mncc = { 0, };
	int rc;

	leg = talloc_zero(call, struct mncc_call_leg);
	if (!leg) {
		LOGP(DMNCC, LOGL_ERROR, "Failed to allocate leg call(%u)\n",
			call->id);
		return -1;
	}

	leg->base.type = CALL_TYPE_MNCC;
	leg->base.connect_call = mncc_call_leg_connect;
	leg->base.ring_call = mncc_call_leg_ring;
	leg->base.release_call = mncc_call_leg_release;
	leg->base.call = call;

	leg->callref = call->id;

	leg->conn = conn;
	leg->state = MNCC_CC_INITIAL;
	leg->dir = MNCC_DIR_MT;

	mncc.msg_type = MNCC_SETUP_REQ;
	mncc.callref = leg->callref;

	mncc.fields |= MNCC_F_CALLING;
	mncc.calling.plan = 1;
	mncc.calling.type = 0x0;
	strncpy(mncc.calling.number, call->source, sizeof(mncc.calling.number));

	if (conn->app->use_imsi_as_id) {
		snprintf(mncc.imsi, 15, "%s", call->dest);
	} else {
		mncc.fields |= MNCC_F_CALLED;
		mncc.called.plan = 1;
		mncc.called.type = 0x0;
		strncpy(mncc.called.number, call->dest, sizeof(mncc.called.number));
	}

	/*
	 * TODO/FIXME:
	 *  - Determine/request channel based on offered audio codecs
	 *  - Screening, redirect?
	 *  - Synth. the bearer caps based on codecs?
	 */
	rc = write(conn->fd.fd, &mncc, sizeof(mncc));
	if (rc != sizeof(mncc)) {
		LOGP(DMNCC, LOGL_ERROR, "Failed to send message leg(%u)\n",
			leg->callref);
		close_connection(conn);
		talloc_free(leg);
		return -1;
	}

	call->remote = &leg->base;
	return 0;
}

static void mncc_reconnect(void *data)
{
	int rc;
	struct mncc_connection *conn = data;

	rc = osmo_sock_unix_init_ofd(&conn->fd, SOCK_SEQPACKET, 0,
					conn->app->mncc.path, OSMO_SOCK_F_CONNECT);
	if (rc < 0) {
		LOGP(DMNCC, LOGL_ERROR, "Failed to connect(%s). Retrying\n",
			conn->app->mncc.path);
		conn->state = MNCC_DISCONNECTED;
		osmo_timer_schedule(&conn->reconnect, 5, 0);
		return;
	}

	LOGP(DMNCC, LOGL_NOTICE, "Reconnected to %s\n", conn->app->mncc.path);
	conn->state = MNCC_WAIT_VERSION;
}

static int mncc_data(struct osmo_fd *fd, unsigned int what)
{
	char buf[4096];
	uint32_t msg_type;
	int rc;
	struct mncc_connection *conn = fd->data;

	rc = read(fd->fd, buf, sizeof(buf));
	if (rc <= 0) {
		LOGP(DMNCC, LOGL_ERROR, "Failed to read %d/%s. Re-connecting.\n",
			rc, strerror(errno));
		goto bad_data;
	}
	if (rc <= 4) {
		LOGP(DMNCC, LOGL_ERROR, "Data too short with: %d\n", rc);
		goto bad_data;
	}

	memcpy(&msg_type, buf, 4);
	switch (msg_type) {
	case MNCC_SOCKET_HELLO:
		check_hello(conn, buf, rc);
		break;
	case MNCC_SETUP_IND:
		check_setup(conn, buf, rc);
		break;
	case MNCC_RTP_CREATE:
		check_rtp_create(conn, buf, rc);
		break;
	case MNCC_RTP_CONNECT:
		check_rtp_connect(conn, buf, rc);
		break;
	case MNCC_DISC_IND:
		check_disc_ind(conn, buf, rc);
		break;
	case MNCC_REL_IND:
		check_rel_ind(conn, buf, rc);
		break;
	case MNCC_REJ_IND:
		check_rej_ind(conn, buf, rc);
		break;
	case MNCC_REL_CNF:
		check_rel_cnf(conn, buf, rc);
		break;
	case MNCC_SETUP_COMPL_IND:
		check_stp_cmpl_ind(conn, buf, rc);
		break;
	case MNCC_SETUP_CNF:
		check_stp_cnf(conn, buf, rc);
		break;
	case MNCC_CALL_CONF_IND:
		check_cnf_ind(conn, buf, rc);
		break;
	case MNCC_ALERT_IND:
		check_alrt_ind(conn, buf, rc);
		break;
	case MNCC_HOLD_IND:
		check_hold_ind(conn, buf, rc);
		break;
	case MNCC_START_DTMF_IND:
		check_dtmf_start(conn, buf, rc);
		break;
	case MNCC_STOP_DTMF_IND:
		check_dtmf_stop(conn, buf, rc);
		break;
	default:
		LOGP(DMNCC, LOGL_ERROR, "Unhandled message type %d/0x%x\n",
			msg_type, msg_type);
		break;
	}
	return 0;

bad_data:
	close_connection(conn);
	return 0;
}

void mncc_connection_init(struct mncc_connection *conn, struct app_config *cfg)
{
	conn->reconnect.cb = mncc_reconnect;
	conn->reconnect.data = conn;
	conn->fd.cb = mncc_data;
	conn->fd.data = conn;
	conn->app = cfg;
	conn->state = MNCC_DISCONNECTED;
}

void mncc_connection_start(struct mncc_connection *conn)
{
	LOGP(DMNCC, LOGL_NOTICE, "Scheduling MNCC connect\n");
	osmo_timer_schedule(&conn->reconnect, 0, 0);
}

const struct value_string mncc_conn_state_vals[] = {
	{ MNCC_DISCONNECTED,	"DISCONNECTED"	},
	{ MNCC_WAIT_VERSION,	"WAITING"	},
	{ MNCC_READY,		"READY"		},
	{ 0, NULL },
};