aboutsummaryrefslogtreecommitdiffstats
path: root/examples/sccp_test_server.c
blob: 83b42fa120fb0ad9ee7004efc23e363601454d6c (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
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>

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

#include <osmocom/sigtran/sccp_sap.h>
#include <osmocom/sigtran/sccp_helpers.h>

#include "internal.h"

unsigned int conn_id =1;

/* a simple SCCP User which refuses all connections and discards all
 * unitdata */
static int refuser_prim_cb(struct osmo_prim_hdr *oph, void *_scu)
{
	struct osmo_sccp_user *scu = _scu;
	struct osmo_scu_prim *scu_prim = (struct osmo_scu_prim *) oph;

	switch (OSMO_PRIM_HDR(&scu_prim->oph)) {
	case OSMO_PRIM(OSMO_SCU_PRIM_N_CONNECT, PRIM_OP_INDICATION):
		LOGP(DMAIN, LOGL_INFO, "refusing N-CONNECT.ind (local_ref=%u)\n",
		     scu_prim->u.connect.conn_id);
		osmo_sccp_tx_disconn(scu, scu_prim->u.connect.conn_id,
				     &scu_prim->u.connect.called_addr,
				     23);
		break;
	default:
		LOGP(DMAIN, LOGL_NOTICE, "Unknown primitive %u:%u\n",
		     oph->primitive, oph->operation);
		break;
	}
	msgb_free(oph->msg);
	return 0;
}

/* a simple SCCP User which accepts all connections and echos back all
 * DATA + UNITDATA */
static int echo_prim_cb(struct osmo_prim_hdr *oph, void *_scu)
{
	struct osmo_sccp_user *scu = _scu;
	struct osmo_scu_prim *scu_prim = (struct osmo_scu_prim *) oph;
	const uint8_t *data = msgb_l2(oph->msg);
	unsigned int data_len = msgb_l2len(oph->msg);

	switch (OSMO_PRIM_HDR(&scu_prim->oph)) {
	case OSMO_PRIM(OSMO_SCU_PRIM_N_CONNECT, PRIM_OP_INDICATION):
		LOGP(DMAIN, LOGL_INFO, "Accepting N-CONNECT.ind (local_ref=%u)\n",
		     scu_prim->u.connect.conn_id);
		osmo_sccp_tx_conn_resp(scu, scu_prim->u.connect.conn_id,
				     &scu_prim->u.connect.called_addr,
				     data, data_len);
		break;
	case OSMO_PRIM(OSMO_SCU_PRIM_N_DATA, PRIM_OP_INDICATION):
		LOGP(DMAIN, LOGL_INFO, "Echoing N-DATA.ind (local_ref=%u)\n",
		    scu_prim->u.data.conn_id);
		osmo_sccp_tx_data(scu, scu_prim->u.data.conn_id,
				  data, data_len);
		break;
	case OSMO_PRIM(OSMO_SCU_PRIM_N_UNITDATA, PRIM_OP_INDICATION):
		LOGP(DMAIN, LOGL_INFO, "Echoing N-UNITDATA.ind\n");
		osmo_sccp_tx_unitdata(scu, &scu_prim->u.unitdata.called_addr,
				      &scu_prim->u.unitdata.calling_addr,
				      data, data_len);
		break;
	default:
		LOGP(DMAIN, LOGL_NOTICE, "Unknown primitive %u:%u\n",
		     oph->primitive, oph->operation);
		break;
	}
	msgb_free(oph->msg);
	return 0;
}

/* a simple SCCP User which receives UNITDATA messages and connects back
 * to whoever sents UNITDATA and then echo's back all DATA */
static int callback_prim_cb(struct osmo_prim_hdr *oph, void *_scu)
{
	struct osmo_sccp_user *scu = _scu;
	struct osmo_scu_prim *scu_prim = (struct osmo_scu_prim *) oph;
	const uint8_t *data = msgb_l2(oph->msg);
	unsigned int data_len = msgb_l2len(oph->msg);

	switch (OSMO_PRIM_HDR(&scu_prim->oph)) {
	case OSMO_PRIM(OSMO_SCU_PRIM_N_UNITDATA, PRIM_OP_INDICATION):
		LOGP(DMAIN, LOGL_INFO, "N-UNITDATA.ind: Connecting back to sender\n");
		osmo_sccp_tx_conn_req(scu, conn_id++,
				     &scu_prim->u.unitdata.called_addr,
				     &scu_prim->u.unitdata.calling_addr,
				     data, data_len);
		break;
	case OSMO_PRIM(OSMO_SCU_PRIM_N_DATA, PRIM_OP_INDICATION):
		LOGP(DMAIN, LOGL_INFO, "Echoing N-DATA.ind (local_ref=%u)\n",
		     scu_prim->u.data.conn_id);
		osmo_sccp_tx_data(scu, scu_prim->u.data.conn_id,
				  data, data_len);
		break;
	default:
		LOGP(DMAIN, LOGL_NOTICE, "Unknown primitive %u:%u\n",
		     oph->primitive, oph->operation);
		break;
	}
	msgb_free(oph->msg);
	return 0;
}

int sccp_test_server_init(struct osmo_sccp_instance *sccp)
{
	osmo_sccp_user_bind(sccp, "refuser", &refuser_prim_cb, SSN_TEST_REFUSE);
	osmo_sccp_user_bind(sccp, "echo", &echo_prim_cb, SSN_TEST_ECHO);
	osmo_sccp_user_bind(sccp, "callback", &callback_prim_cb, SSN_TEST_CALLBACK);

	return 0;
}