blob: d07cc3deeacad9d82b7470ace8551925ceb8c322 [file] [log] [blame]
Jacob Erlbeckdcce1962013-10-08 12:04:43 +02001/* test routines for NS connection handling
2 * (C) 2013 by sysmocom s.f.m.c. GmbH
3 * Author: Jacob Erlbeck <jerlbeck@sysmocom.de>
4 */
5
6#undef _GNU_SOURCE
7#define _GNU_SOURCE
8
9#include <stdio.h>
10#include <stdlib.h>
11#include <stdint.h>
12#include <string.h>
13#include <getopt.h>
14#include <dlfcn.h>
15#include <sys/types.h>
16#include <sys/socket.h>
17
18#include <osmocom/core/msgb.h>
19#include <osmocom/core/application.h>
20#include <osmocom/core/utils.h>
21#include <osmocom/core/logging.h>
22#include <osmocom/core/talloc.h>
23#include <osmocom/gprs/gprs_msgb.h>
24#include <osmocom/gprs/gprs_ns.h>
25#include <osmocom/gprs/gprs_bssgp.h>
26
27
28/* GPRS Network Service, PDU type: NS_RESET,
29 * Cause: O&M intervention, NS VCI: 0x1122, NSEI 0x1122
30 */
31static const unsigned char gprs_ns_reset[12] = {
32 0x02, 0x00, 0x81, 0x01, 0x01, 0x82, 0x11, 0x22,
33 0x04, 0x82, 0x11, 0x22
34};
35
36/* GPRS Network Service, PDU type: NS_RESET,
37 * Cause: O&M intervention, NS VCI: 0x3344, NSEI 0x1122
38 */
39static const unsigned char gprs_ns_reset_vci2[12] = {
40 0x02, 0x00, 0x81, 0x01, 0x01, 0x82, 0x33, 0x44,
41 0x04, 0x82, 0x11, 0x22
42};
43
44/* GPRS Network Service, PDU type: NS_RESET,
45 * Cause: O&M intervention, NS VCI: 0x1122, NSEI 0x3344
46 */
47static const unsigned char gprs_ns_reset_nsei2[12] = {
48 0x02, 0x00, 0x81, 0x01, 0x01, 0x82, 0x11, 0x22,
49 0x04, 0x82, 0x33, 0x44
50};
51
52/* GPRS Network Service, PDU type: NS_ALIVE */
53static const unsigned char gprs_ns_alive[1] = {
54 0x0a
55};
56
57/* GPRS Network Service, PDU type: NS_STATUS,
58 * Cause: PDU not compatible with the protocol state
59 * PDU: NS_ALIVE
60 */
61static const unsigned char gprs_ns_status_invalid_alive[7] = {
62 0x08, 0x00, 0x81, 0x0a, 0x02, 0x81, 0x0a
63};
64
65/* GPRS Network Service, PDU type: NS_ALIVE_ACK */
66static const unsigned char gprs_ns_alive_ack[1] = {
67 0x0b
68};
69
70/* GPRS Network Service, PDU type: NS_UNBLOCK */
71static const unsigned char gprs_ns_unblock[1] = {
72 0x06
73};
74
75
76/* GPRS Network Service, PDU type: NS_STATUS,
77 * Cause: PDU not compatible with the protocol state
78 * PDU: NS_RESET_ACK, NS VCI: 0x1122, NSEI 0x1122
79 */
80static const unsigned char gprs_ns_status_invalid_reset_ack[15] = {
81 0x08, 0x00, 0x81, 0x0a, 0x02, 0x89, 0x03, 0x01,
82 0x82, 0x11, 0x22, 0x04, 0x82, 0x11, 0x22
83};
84
85/* GPRS Network Service, PDU type: NS_UNITDATA, BVCI 0 */
86static const unsigned char gprs_bssgp_reset[22] = {
87 0x00, 0x00, 0x00, 0x00, 0x22, 0x04, 0x82, 0x4a,
88 0x2e, 0x07, 0x81, 0x08, 0x08, 0x88, 0x10, 0x20,
89 0x30, 0x40, 0x50, 0x60, 0x10, 0x00
90};
91
92int gprs_ns_rcvmsg(struct gprs_ns_inst *nsi, struct msgb *msg,
93 struct sockaddr_in *saddr, enum gprs_ns_ll ll);
94
95/* override */
96int gprs_ns_callback(enum gprs_ns_evt event, struct gprs_nsvc *nsvc,
97 struct msgb *msg, uint16_t bvci)
98{
99 printf("CALLBACK, event %d, msg length %d, bvci 0x%04x\n%s\n\n",
100 event, msgb_bssgp_len(msg), bvci,
101 osmo_hexdump(msgb_bssgph(msg), msgb_bssgp_len(msg)));
102 return 0;
103}
104
105/* override */
106ssize_t sendto(int sockfd, const void *buf, size_t len, int flags,
107 const struct sockaddr *dest_addr, socklen_t addrlen)
108{
109 typedef ssize_t (*sendto_t)(int, const void *, size_t, int,
110 const struct sockaddr *, socklen_t);
111 static sendto_t real_sendto = NULL;
112
113 if (!real_sendto)
114 real_sendto = dlsym(RTLD_NEXT, "sendto");
115
116 if (sockfd != 0xdead && ((struct sockaddr_in *)dest_addr)->sin_addr.s_addr != htonl(0x01020304))
117 return real_sendto(sockfd, buf, len, flags, dest_addr, addrlen);
118
119 printf("RESPONSE, msg length %d\n%s\n\n", len, osmo_hexdump(buf, len));
120
121 return len;
122}
123
124static int gprs_process_message(struct gprs_ns_inst *nsi, const char *text, struct sockaddr_in *peer, const unsigned char* data, size_t data_len)
125{
126 struct msgb *msg;
127 int ret;
128 if (data_len > NS_ALLOC_SIZE - NS_ALLOC_HEADROOM) {
129 fprintf(stderr, "message too long: %d\n", data_len);
130 return -1;
131 }
132
133 msg = gprs_ns_msgb_alloc();
134 memmove(msg->data, data, data_len);
135 msg->l2h = msg->data;
136 msgb_put(msg, data_len);
137
138 printf("PROCESSING %s from 0x%08x:%d\n%s\n\n",
139 text, ntohl(peer->sin_addr.s_addr), ntohs(peer->sin_port),
140 osmo_hexdump(data, data_len));
141
142 ret = gprs_ns_rcvmsg(nsi, msg, peer, GPRS_NS_LL_UDP);
143
144 printf("result (%s) = %d\n\n", text, ret);
145
146 msgb_free(msg);
147
148 return ret;
149}
150
151static void gprs_dump_nsi(struct gprs_ns_inst *nsi)
152{
153 struct gprs_nsvc *nsvc;
154
155 printf("Current NS-VCIs:\n");
156 llist_for_each_entry(nsvc, &nsi->gprs_nsvcs, list) {
157 struct sockaddr_in *peer = &(nsvc->ip.bts_addr);
158 printf(" VCI 0x%04x, NSEI 0x%04x, peer 0x%08x:%d\n",
159 nsvc->nsvci, nsvc->nsei,
160 ntohl(peer->sin_addr.s_addr), ntohs(peer->sin_port)
161 );
162 }
163 printf("\n");
164}
165
166static void test_ns()
167{
168 struct gprs_ns_inst *nsi = gprs_ns_instantiate(gprs_ns_callback, NULL);
169 struct sockaddr_in peer[4] = {{0},};
170
171 peer[0].sin_family = AF_INET;
172 peer[0].sin_port = htons(1111);
173 peer[0].sin_addr.s_addr = htonl(0x01020304);
174 peer[1].sin_family = AF_INET;
175 peer[1].sin_port = htons(2222);
176 peer[1].sin_addr.s_addr = htonl(0x01020304);
177 peer[2].sin_family = AF_INET;
178 peer[2].sin_port = htons(3333);
179 peer[2].sin_addr.s_addr = htonl(0x01020304);
180 peer[3].sin_family = AF_INET;
181 peer[3].sin_port = htons(4444);
182 peer[3].sin_addr.s_addr = htonl(0x01020304);
183
184 gprs_process_message(nsi, "RESET", &peer[0],
185 gprs_ns_reset, sizeof(gprs_ns_reset));
186 gprs_dump_nsi(nsi);
187 gprs_process_message(nsi, "ALIVE", &peer[0],
188 gprs_ns_alive, sizeof(gprs_ns_alive));
189 gprs_process_message(nsi, "UNBLOCK", &peer[0],
190 gprs_ns_unblock, sizeof(gprs_ns_unblock));
191 gprs_process_message(nsi, "BSSGP RESET", &peer[0],
192 gprs_bssgp_reset, sizeof(gprs_bssgp_reset));
193
194 printf("--- Peer port changes, RESET, message remains unchanged ---\n\n");
195
196 gprs_process_message(nsi, "RESET", &peer[1],
197 gprs_ns_reset, sizeof(gprs_ns_reset));
198 gprs_dump_nsi(nsi);
199
200 printf("--- Peer port changes, RESET, VCI changes ---\n\n");
201
202 gprs_process_message(nsi, "RESET", &peer[2],
203 gprs_ns_reset_vci2, sizeof(gprs_ns_reset_vci2));
204 gprs_dump_nsi(nsi);
205
206 printf("--- Peer port changes, RESET, NSEI changes ---\n\n");
207
208 gprs_process_message(nsi, "RESET", &peer[3],
209 gprs_ns_reset_nsei2, sizeof(gprs_ns_reset_nsei2));
210 gprs_dump_nsi(nsi);
211
212 printf("--- Peer port 3333, RESET, VCI is changed back ---\n\n");
213
214 gprs_process_message(nsi, "RESET", &peer[2],
215 gprs_ns_reset, sizeof(gprs_ns_reset));
216 gprs_dump_nsi(nsi);
217
218 printf("--- Peer port 4444, RESET, NSEI is changed back ---\n\n");
219
220 gprs_process_message(nsi, "RESET", &peer[3],
221 gprs_ns_reset, sizeof(gprs_ns_reset));
222 gprs_dump_nsi(nsi);
223
224 gprs_ns_destroy(nsi);
225 nsi = NULL;
226}
227
228
229int bssgp_prim_cb(struct osmo_prim_hdr *oph, void *ctx)
230{
231 return -1;
232}
233
234static struct log_info info = {};
235
236int main(int argc, char **argv)
237{
238 osmo_init_logging(&info);
239 log_set_use_color(osmo_stderr_target, 0);
240 log_set_print_filename(osmo_stderr_target, 0);
241
242 printf("===== NS protocol test START\n");
243 test_ns();
244 printf("===== NS protocol test END\n\n");
245
246 exit(EXIT_SUCCESS);
247}