blob: d24250db6aa0ea69c21382fcb99cd48548b9ea82 [file] [log] [blame]
Jacob Erlbeckcdebf742014-10-14 14:49:37 +02001/* Test routines for the BSSGP implementation in libosmogb
2 *
3 * (C) 2014 by sysmocom s.f.m.c. GmbH
4 * Author: Jacob Erlbeck <jerlbeck@sysmocom.de>
5 *
6 * Skeleton based on bssgp_fc_test.c
7 * (C) 2012 by Harald Welte <laforge@gnumonks.org>
8 */
9
10#undef _GNU_SOURCE
11#define _GNU_SOURCE
12
13#include <osmocom/core/application.h>
14#include <osmocom/core/utils.h>
15#include <osmocom/core/logging.h>
16#include <osmocom/core/talloc.h>
17#include <osmocom/core/prim.h>
18#include <osmocom/gprs/gprs_bssgp.h>
19#include <osmocom/gprs/gprs_ns.h>
20
21#include <stdio.h>
22#include <stdlib.h>
23#include <stdint.h>
24#include <string.h>
25#include <getopt.h>
26#include <unistd.h>
27#include <netinet/ip.h>
28#include <dlfcn.h>
29
30#define BSS_NSEI 0x0b55
31
32static struct osmo_prim_hdr last_oph = {0};
33
34/* override */
35ssize_t sendto(int sockfd, const void *buf, size_t len, int flags,
36 const struct sockaddr *dest_addr, socklen_t addrlen)
37{
38 typedef ssize_t (*sendto_t)(int, const void *, size_t, int,
39 const struct sockaddr *, socklen_t);
40 static sendto_t real_sendto = NULL;
41 uint32_t dest_host = htonl(((struct sockaddr_in *)dest_addr)->sin_addr.s_addr);
42
43 if (!real_sendto)
44 real_sendto = dlsym(RTLD_NEXT, "sendto");
45
46 fprintf(stderr, "MESSAGE to 0x%08x, msg length %d\n%s\n",
47 dest_host, len, osmo_hexdump(buf, len));
48
49 return len;
50}
51
52/* override */
53int gprs_ns_callback(enum gprs_ns_evt event, struct gprs_nsvc *nsvc,
54 struct msgb *msg, uint16_t bvci)
55{
56 fprintf(stderr, "CALLBACK, event %d, msg length %d, bvci 0x%04x\n%s\n\n",
57 event, msgb_bssgp_len(msg), bvci,
58 osmo_hexdump(msgb_bssgph(msg), msgb_bssgp_len(msg)));
59 return 0;
60}
61
62int bssgp_prim_cb(struct osmo_prim_hdr *oph, void *ctx)
63{
64 printf("BSSGP primitive, SAP %d, prim = %d, op = %d, msg = %s\n",
65 oph->sap, oph->primitive, oph->operation, msgb_hexdump(oph->msg));
66
67 last_oph.sap = oph->sap;
68 last_oph.primitive = oph->primitive;
69 last_oph.operation = oph->operation;
70 last_oph.msg = NULL;
71 return -1;
72}
73
74static void msgb_bssgp_send_and_free(struct msgb *msg)
75{
76 msgb_nsei(msg) = BSS_NSEI;
77
78 bssgp_rcvmsg(msg);
79
80 msgb_free(msg);
81}
82
83static void send_bssgp_supend(enum bssgp_pdu_type pdu_type, uint32_t tlli)
84{
85 struct msgb *msg = bssgp_msgb_alloc();
86 uint32_t tlli_be = htonl(tlli);
87 uint8_t rai[] = {0x0f, 0xf1, 0x80, 0x20, 0x37, 0x00};
88
89 msgb_v_put(msg, pdu_type);
90 msgb_tvlv_put(msg, BSSGP_IE_TLLI, sizeof(tlli_be), (uint8_t *)&tlli_be);
91 msgb_tvlv_put(msg, BSSGP_IE_ROUTEING_AREA, sizeof(rai), &rai[0]);
92
93 msgb_bssgp_send_and_free(msg);
94}
95
96static void send_bssgp_resume(enum bssgp_pdu_type pdu_type, uint32_t tlli)
97{
98 struct msgb *msg = bssgp_msgb_alloc();
99 uint32_t tlli_be = htonl(tlli);
100 uint8_t rai[] = {0x0f, 0xf1, 0x80, 0x20, 0x37, 0x00};
101 uint8_t suspend_ref = 1;
102
103 msgb_v_put(msg, pdu_type);
104 msgb_tvlv_put(msg, BSSGP_IE_TLLI, sizeof(tlli_be), (uint8_t *)&tlli_be);
105 msgb_tvlv_put(msg, BSSGP_IE_ROUTEING_AREA, sizeof(rai), &rai[0]);
106 msgb_tvlv_put(msg, BSSGP_IE_SUSPEND_REF_NR, 1, &suspend_ref);
107
108 msgb_bssgp_send_and_free(msg);
109}
110
111static void test_bssgp_suspend_resume(void)
112{
113 const uint32_t tlli = 0xf0123456;
114
115 printf("----- %s START\n", __func__);
116 memset(&last_oph, 0, sizeof(last_oph));
117
118 send_bssgp_supend(BSSGP_PDUT_SUSPEND, tlli);
Jacob Erlbeckb43baf22014-09-10 12:43:28 +0200119 OSMO_ASSERT(last_oph.primitive == PRIM_BSSGP_GMM_SUSPEND);
Jacob Erlbeckcdebf742014-10-14 14:49:37 +0200120
121 send_bssgp_resume(BSSGP_PDUT_RESUME, tlli);
Jacob Erlbeckb43baf22014-09-10 12:43:28 +0200122 OSMO_ASSERT(last_oph.primitive == PRIM_BSSGP_GMM_RESUME);
Jacob Erlbeckcdebf742014-10-14 14:49:37 +0200123
124 printf("----- %s END\n", __func__);
125}
126
127static struct log_info info = {};
128
129int main(int argc, char **argv)
130{
131 struct sockaddr_in bss_peer= {0};
132
133 osmo_init_logging(&info);
134 log_set_use_color(osmo_stderr_target, 0);
135 log_set_print_filename(osmo_stderr_target, 0);
136
137 bssgp_nsi = gprs_ns_instantiate(gprs_ns_callback, NULL);
138
139 bss_peer.sin_family = AF_INET;
140 bss_peer.sin_port = htons(32000);
141 bss_peer.sin_addr.s_addr = htonl(0x7f0000ff);
142
143 gprs_ns_nsip_connect(bssgp_nsi, &bss_peer, BSS_NSEI, BSS_NSEI+1);
144
145
146 printf("===== BSSGP test START\n");
147 test_bssgp_suspend_resume();
148 printf("===== BSSGP test END\n\n");
149
150 exit(EXIT_SUCCESS);
151}