aboutsummaryrefslogtreecommitdiffstats
path: root/tests/gb/gprs_ns_test.c
blob: d07cc3deeacad9d82b7470ace8551925ceb8c322 (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
/* test routines for NS connection handling
 * (C) 2013 by sysmocom s.f.m.c. GmbH
 * Author: Jacob Erlbeck <jerlbeck@sysmocom.de>
 */

#undef _GNU_SOURCE
#define _GNU_SOURCE

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <getopt.h>
#include <dlfcn.h>
#include <sys/types.h>
#include <sys/socket.h>

#include <osmocom/core/msgb.h>
#include <osmocom/core/application.h>
#include <osmocom/core/utils.h>
#include <osmocom/core/logging.h>
#include <osmocom/core/talloc.h>
#include <osmocom/gprs/gprs_msgb.h>
#include <osmocom/gprs/gprs_ns.h>
#include <osmocom/gprs/gprs_bssgp.h>


/* GPRS Network Service, PDU type: NS_RESET,
 * Cause: O&M intervention, NS VCI: 0x1122, NSEI 0x1122
 */
static const unsigned char gprs_ns_reset[12] = {
	0x02, 0x00, 0x81, 0x01, 0x01, 0x82, 0x11, 0x22,
	0x04, 0x82, 0x11, 0x22
};

/* GPRS Network Service, PDU type: NS_RESET,
 * Cause: O&M intervention, NS VCI: 0x3344, NSEI 0x1122
 */
static const unsigned char gprs_ns_reset_vci2[12] = {
	0x02, 0x00, 0x81, 0x01, 0x01, 0x82, 0x33, 0x44,
	0x04, 0x82, 0x11, 0x22
};

/* GPRS Network Service, PDU type: NS_RESET,
 * Cause: O&M intervention, NS VCI: 0x1122, NSEI 0x3344
 */
static const unsigned char gprs_ns_reset_nsei2[12] = {
	0x02, 0x00, 0x81, 0x01, 0x01, 0x82, 0x11, 0x22,
	0x04, 0x82, 0x33, 0x44
};

/* GPRS Network Service, PDU type: NS_ALIVE */
static const unsigned char gprs_ns_alive[1] = {
	0x0a
};

/* GPRS Network Service, PDU type: NS_STATUS,
 * Cause: PDU not compatible with the protocol state
 * PDU: NS_ALIVE
 */
static const unsigned char gprs_ns_status_invalid_alive[7] = {
	0x08, 0x00, 0x81, 0x0a, 0x02, 0x81, 0x0a
};

/* GPRS Network Service, PDU type: NS_ALIVE_ACK */
static const unsigned char gprs_ns_alive_ack[1] = {
	0x0b
};

/* GPRS Network Service, PDU type: NS_UNBLOCK */
static const unsigned char gprs_ns_unblock[1] = {
	0x06
};


/* GPRS Network Service, PDU type: NS_STATUS,
 * Cause: PDU not compatible with the protocol state
 * PDU: NS_RESET_ACK, NS VCI: 0x1122, NSEI 0x1122
 */
static const unsigned char gprs_ns_status_invalid_reset_ack[15] = {
	0x08, 0x00, 0x81, 0x0a, 0x02, 0x89, 0x03, 0x01,
	0x82, 0x11, 0x22, 0x04, 0x82, 0x11, 0x22
};

/* GPRS Network Service, PDU type: NS_UNITDATA, BVCI 0 */
static const unsigned char gprs_bssgp_reset[22] = {
	0x00, 0x00, 0x00, 0x00, 0x22, 0x04, 0x82, 0x4a,
	0x2e, 0x07, 0x81, 0x08, 0x08, 0x88, 0x10, 0x20,
	0x30, 0x40, 0x50, 0x60, 0x10, 0x00
};

int gprs_ns_rcvmsg(struct gprs_ns_inst *nsi, struct msgb *msg,
		   struct sockaddr_in *saddr, enum gprs_ns_ll ll);

/* override */
int gprs_ns_callback(enum gprs_ns_evt event, struct gprs_nsvc *nsvc,
			 struct msgb *msg, uint16_t bvci)
{
	printf("CALLBACK, event %d, msg length %d, bvci 0x%04x\n%s\n\n",
			event, msgb_bssgp_len(msg), bvci,
			osmo_hexdump(msgb_bssgph(msg), msgb_bssgp_len(msg)));
	return 0;
}

/* override */
ssize_t sendto(int sockfd, const void *buf, size_t len, int flags,
		const struct sockaddr *dest_addr, socklen_t addrlen)
{
	typedef ssize_t (*sendto_t)(int, const void *, size_t, int,
			const struct sockaddr *, socklen_t);
	static sendto_t real_sendto = NULL;

	if (!real_sendto)
		real_sendto = dlsym(RTLD_NEXT, "sendto");

	if (sockfd != 0xdead && ((struct sockaddr_in *)dest_addr)->sin_addr.s_addr != htonl(0x01020304))
		return real_sendto(sockfd, buf, len, flags, dest_addr, addrlen);

	printf("RESPONSE, msg length %d\n%s\n\n", len, osmo_hexdump(buf, len));

	return len;
}

static int gprs_process_message(struct gprs_ns_inst *nsi, const char *text, struct sockaddr_in *peer, const unsigned char* data, size_t data_len)
{
	struct msgb *msg;
	int ret;
	if (data_len > NS_ALLOC_SIZE - NS_ALLOC_HEADROOM) {
		fprintf(stderr, "message too long: %d\n", data_len);
		return -1;
	}

	msg = gprs_ns_msgb_alloc();
	memmove(msg->data, data, data_len);
	msg->l2h = msg->data;
	msgb_put(msg, data_len);

	printf("PROCESSING %s from 0x%08x:%d\n%s\n\n",
	       text, ntohl(peer->sin_addr.s_addr), ntohs(peer->sin_port),
	       osmo_hexdump(data, data_len));

	ret = gprs_ns_rcvmsg(nsi, msg, peer, GPRS_NS_LL_UDP);

	printf("result (%s) = %d\n\n", text, ret);

	msgb_free(msg);

	return ret;
}

static void gprs_dump_nsi(struct gprs_ns_inst *nsi)
{
	struct gprs_nsvc *nsvc;

	printf("Current NS-VCIs:\n");
	llist_for_each_entry(nsvc, &nsi->gprs_nsvcs, list) {
		struct sockaddr_in *peer = &(nsvc->ip.bts_addr);
		printf("    VCI 0x%04x, NSEI 0x%04x, peer 0x%08x:%d\n",
		       nsvc->nsvci, nsvc->nsei,
		       ntohl(peer->sin_addr.s_addr), ntohs(peer->sin_port)
		      );
	}
	printf("\n");
}

static void test_ns()
{
	struct gprs_ns_inst *nsi = gprs_ns_instantiate(gprs_ns_callback, NULL);
	struct sockaddr_in peer[4] = {{0},};

	peer[0].sin_family = AF_INET;
	peer[0].sin_port = htons(1111);
	peer[0].sin_addr.s_addr = htonl(0x01020304);
	peer[1].sin_family = AF_INET;
	peer[1].sin_port = htons(2222);
	peer[1].sin_addr.s_addr = htonl(0x01020304);
	peer[2].sin_family = AF_INET;
	peer[2].sin_port = htons(3333);
	peer[2].sin_addr.s_addr = htonl(0x01020304);
	peer[3].sin_family = AF_INET;
	peer[3].sin_port = htons(4444);
	peer[3].sin_addr.s_addr = htonl(0x01020304);

	gprs_process_message(nsi, "RESET", &peer[0],
			     gprs_ns_reset, sizeof(gprs_ns_reset));
	gprs_dump_nsi(nsi);
	gprs_process_message(nsi, "ALIVE", &peer[0],
			     gprs_ns_alive, sizeof(gprs_ns_alive));
	gprs_process_message(nsi, "UNBLOCK", &peer[0],
			     gprs_ns_unblock, sizeof(gprs_ns_unblock));
	gprs_process_message(nsi, "BSSGP RESET", &peer[0],
			     gprs_bssgp_reset, sizeof(gprs_bssgp_reset));

	printf("--- Peer port changes, RESET, message remains unchanged ---\n\n");

	gprs_process_message(nsi, "RESET", &peer[1],
			     gprs_ns_reset, sizeof(gprs_ns_reset));
	gprs_dump_nsi(nsi);

	printf("--- Peer port changes, RESET, VCI changes ---\n\n");

	gprs_process_message(nsi, "RESET", &peer[2],
			     gprs_ns_reset_vci2, sizeof(gprs_ns_reset_vci2));
	gprs_dump_nsi(nsi);

	printf("--- Peer port changes, RESET, NSEI changes ---\n\n");

	gprs_process_message(nsi, "RESET", &peer[3],
			     gprs_ns_reset_nsei2, sizeof(gprs_ns_reset_nsei2));
	gprs_dump_nsi(nsi);

	printf("--- Peer port 3333, RESET, VCI is changed back ---\n\n");

	gprs_process_message(nsi, "RESET", &peer[2],
			     gprs_ns_reset, sizeof(gprs_ns_reset));
	gprs_dump_nsi(nsi);

	printf("--- Peer port 4444, RESET, NSEI is changed back ---\n\n");

	gprs_process_message(nsi, "RESET", &peer[3],
			     gprs_ns_reset, sizeof(gprs_ns_reset));
	gprs_dump_nsi(nsi);

	gprs_ns_destroy(nsi);
	nsi = NULL;
}


int bssgp_prim_cb(struct osmo_prim_hdr *oph, void *ctx)
{
	return -1;
}

static struct log_info info = {};

int main(int argc, char **argv)
{
	osmo_init_logging(&info);
	log_set_use_color(osmo_stderr_target, 0);
	log_set_print_filename(osmo_stderr_target, 0);

	printf("===== NS protocol test START\n");
	test_ns();
	printf("===== NS protocol test END\n\n");

	exit(EXIT_SUCCESS);
}