blob: 6e080b58401a223f13a6d9480b2b80c22c5a33ff [file] [log] [blame]
Holger Hans Peter Freythera93b83b2011-01-17 15:21:06 +01001/* Relay UDT/all SCCP messages */
2/*
3 * (C) 2010-2011 by Holger Hans Peter Freyther <zecke@selfish.org>
4 * (C) 2010-2011 by On-Waves
5 * All Rights Reserved
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 */
22
23#include <mtp_data.h>
24#include <mtp_level3.h>
25#include <mtp_pcap.h>
26#include <thread.h>
27#include <bsc_data.h>
28#include <snmp_mtp.h>
29#include <cellmgr_debug.h>
Holger Hans Peter Freyther9cf11bc2011-01-17 15:53:06 +010030#include <sctp_m2ua.h>
Holger Hans Peter Freythera93b83b2011-01-17 15:21:06 +010031
Holger Hans Peter Freyther02921272011-01-22 23:05:03 +010032#include <osmocom/m2ua/m2ua_msg.h>
33
Holger Hans Peter Freythera93b83b2011-01-17 15:21:06 +010034#include <osmocore/talloc.h>
35
36#include <osmocom/vty/vty.h>
37#include <osmocom/vty/telnet_interface.h>
38
39#include <sys/stat.h>
40#include <sys/types.h>
41
42#include <fcntl.h>
43#include <signal.h>
44#include <stdio.h>
45#include <stdlib.h>
46#include <string.h>
47#include <assert.h>
48#include <unistd.h>
49
50#ifndef _GNU_SOURCE
51#define _GNU_SOURCE
52#endif
53#include <getopt.h>
54
55#undef PACKAGE_NAME
56#undef PACKAGE_VERSION
57#undef PACKAGE_BUGREPORT
58#undef PACKAGE_TARNAME
59#undef PACKAGE_STRING
60#include <cellmgr_config.h>
61
62static struct log_target *stderr_target;
63
64static char *config = "osmo_stp.cfg";
65
66struct bsc_data bsc;
67extern void cell_vty_init(void);
68
69/*
70 * methods called from the MTP Level3 part
71 */
72void mtp_link_set_forward_sccp(struct mtp_link_set *link, struct msgb *_msg, int sls)
73{
Holger Hans Peter Freyther9cf11bc2011-01-17 15:53:06 +010074 struct mtp_link_set *target;
75
76 target = bsc.m2ua_set == link ? bsc.link_set : bsc.m2ua_set;
77 mtp_link_set_submit_sccp_data(target, sls, _msg->l2h, msgb_l2len(_msg));
Holger Hans Peter Freythera93b83b2011-01-17 15:21:06 +010078}
79
Holger Hans Peter Freyther1b6901e2011-01-17 16:13:28 +010080void mtp_link_set_forward_isup(struct mtp_link_set *set, struct msgb *msg, int sls)
81{
82 struct mtp_link_set *target;
83
84 target = bsc.m2ua_set == set ? bsc.link_set : bsc.m2ua_set;
85 mtp_link_set_submit_isup_data(target, sls, msg->l3h, msgb_l3len(msg));
86}
87
Holger Hans Peter Freythera93b83b2011-01-17 15:21:06 +010088void mtp_linkset_down(struct mtp_link_set *set)
89{
90 set->available = 0;
91 mtp_link_set_stop(set);
92}
93
94void mtp_linkset_up(struct mtp_link_set *set)
95{
96 set->available = 1;
97 mtp_link_set_reset(set);
98}
99
100static void print_usage()
101{
102 printf("Usage: osmo-stp\n");
103}
104
105static void sigint()
106{
107 static pthread_mutex_t exit_mutex = PTHREAD_MUTEX_INITIALIZER;
108 static int handled = 0;
109
110 /* failed to lock */
111 if (pthread_mutex_trylock(&exit_mutex) != 0)
112 return;
113 if (handled)
114 goto out;
115
116 printf("Terminating.\n");
117 handled = 1;
118 if (bsc.setup)
119 link_shutdown_all(bsc.link_set);
120 exit(0);
121
122out:
123 pthread_mutex_unlock(&exit_mutex);
124}
125
126static void print_help()
127{
128 printf(" Some useful help...\n");
129 printf(" -h --help this text\n");
130 printf(" -c --config=CFG The config file to use.\n");
131 printf(" -p --pcap=FILE. Write MSUs to the PCAP file.\n");
132 printf(" -c --once. Send the SLTM msg only once.\n");
133 printf(" -v --version. Print the version number\n");
134}
135
136static void handle_options(int argc, char **argv)
137{
138 while (1) {
139 int option_index = 0, c;
140 static struct option long_options[] = {
141 {"help", 0, 0, 'h'},
142 {"config", 1, 0, 'c'},
143 {"pcap", 1, 0, 'p'},
144 {"version", 0, 0, 0},
145 {0, 0, 0, 0},
146 };
147
148 c = getopt_long(argc, argv, "hc:p:v",
149 long_options, &option_index);
150 if (c == -1)
151 break;
152
153 switch (c) {
154 case 'h':
155 print_usage();
156 print_help();
157 exit(0);
158 case 'p':
159 if (bsc.pcap_fd >= 0)
160 close(bsc.pcap_fd);
161 bsc.pcap_fd = open(optarg, O_WRONLY | O_TRUNC | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP| S_IROTH);
162 if (bsc.pcap_fd < 0) {
163 fprintf(stderr, "Failed to open PCAP file.\n");
164 exit(0);
165 }
166 mtp_pcap_write_header(bsc.pcap_fd);
167 break;
168 case 'c':
169 config = optarg;
170 break;
171 case 'v':
172 printf("This is %s version %s.\n", PACKAGE, VERSION);
173 exit(0);
174 break;
175 default:
176 fprintf(stderr, "Unknown option.\n");
177 break;
178 }
179 }
180}
181
182int main(int argc, char **argv)
183{
184 int rc;
Holger Hans Peter Freyther9cf11bc2011-01-17 15:53:06 +0100185 struct mtp_link *data;
186 struct mtp_m2ua_link *lnk;
Holger Hans Peter Freythera93b83b2011-01-17 15:21:06 +0100187 INIT_LLIST_HEAD(&bsc.sccp_connections);
188
Holger Hans Peter Freythera310e532011-01-22 16:34:16 +0100189 bsc.app = APP_STP;
Holger Hans Peter Freythera93b83b2011-01-17 15:21:06 +0100190 bsc.dpc = 1;
191 bsc.opc = 0;
192 bsc.sccp_opc = -1;
Holger Hans Peter Freytherd8a73e22011-01-17 22:37:11 +0100193 bsc.isup_opc = -1;
Holger Hans Peter Freythera93b83b2011-01-17 15:21:06 +0100194 bsc.udp_port = 3456;
195 bsc.udp_ip = NULL;
196 bsc.src_port = 1313;
197 bsc.ni_ni = MTP_NI_NATION_NET;
198 bsc.ni_spare = 0;
Holger Hans Peter Freytherc6bfa272011-01-22 17:06:34 +0100199 bsc.udp_nr_links = 1;
Holger Hans Peter Freythera93b83b2011-01-17 15:21:06 +0100200
201 mtp_link_set_init();
202 thread_init();
203
204 log_init(&log_info);
205 stderr_target = log_target_create_stderr();
206 log_add_target(stderr_target);
207
208 /* enable filters */
209 log_set_all_filter(stderr_target, 1);
210 log_set_category_filter(stderr_target, DINP, 1, LOGL_INFO);
211 log_set_category_filter(stderr_target, DSCCP, 1, LOGL_INFO);
212 log_set_category_filter(stderr_target, DMSC, 1, LOGL_INFO);
213 log_set_category_filter(stderr_target, DMGCP, 1, LOGL_INFO);
214 log_set_print_timestamp(stderr_target, 1);
215 log_set_use_color(stderr_target, 0);
216
217 sccp_set_log_area(DSCCP);
Holger Hans Peter Freyther02921272011-01-22 23:05:03 +0100218 m2ua_set_log_area(DM2UA);
Holger Hans Peter Freythera93b83b2011-01-17 15:21:06 +0100219
220 bsc.setup = 0;
221 bsc.msc_address = "127.0.0.1";
222 bsc.pcap_fd = -1;
223 bsc.udp_reset_timeout = 180;
224 bsc.ping_time = 20;
225 bsc.pong_time = 5;
226 bsc.msc_time = 20;
227 bsc.forward_only = 1;
228
229 handle_options(argc, argv);
230
231 signal(SIGPIPE, SIG_IGN);
232 signal(SIGINT, sigint);
233 srand(time(NULL));
234
235 cell_vty_init();
236 if (vty_read_config_file(config, NULL) < 0) {
237 fprintf(stderr, "Failed to read the VTY config.\n");
238 return -1;
239 }
240
241 rc = telnet_init(NULL, NULL, 4242);
242 if (rc < 0)
243 return rc;
244
245 if (link_init(&bsc) != 0)
246 return -1;
247
Holger Hans Peter Freyther9cf11bc2011-01-17 15:53:06 +0100248 bsc.m2ua_set = mtp_link_set_alloc();
249 bsc.m2ua_set->dpc = 92;
250 bsc.m2ua_set->opc = 9;
251 bsc.m2ua_set->sccp_opc = 9;
Holger Hans Peter Freytherd8a73e22011-01-17 22:37:11 +0100252 bsc.m2ua_set->isup_opc = 9;
Holger Hans Peter Freyther9cf11bc2011-01-17 15:53:06 +0100253 bsc.m2ua_set->ni = 3;
254 bsc.m2ua_set->bsc = &bsc;
Holger Hans Peter Freytherf6375b42011-01-22 21:01:23 +0100255 bsc.m2ua_set->pcap_fd = bsc.pcap_fd;
Holger Hans Peter Freyther9cf11bc2011-01-17 15:53:06 +0100256
Holger Hans Peter Freytherefbd8c22011-01-17 20:21:45 +0100257 /* for both links we want to have all isup messages */
258 bsc.m2ua_set->pass_all_isup = 1;
259 bsc.link_set->pass_all_isup = 1;
260
Holger Hans Peter Freyther9cf11bc2011-01-17 15:53:06 +0100261 lnk = sctp_m2ua_transp_create("0.0.0.0", 2904);
Holger Hans Peter Freytherf6375b42011-01-22 21:01:23 +0100262 lnk->base.pcap_fd = -1;
Holger Hans Peter Freyther9cf11bc2011-01-17 15:53:06 +0100263 mtp_link_set_add_link(bsc.m2ua_set, (struct mtp_link *) lnk);
264
265 llist_for_each_entry(data, &bsc.m2ua_set->links, entry)
266 data->start(data);
267
Holger Hans Peter Freythera93b83b2011-01-17 15:21:06 +0100268 while (1) {
269 bsc_select_main(0);
270 }
271
272 return 0;
273}
274
275/* dummy for links */
276int msc_init(struct bsc_data *data, int dummy)
277{
278 return 0;
279}
280