blob: fb10ea3a1c904dffc4eb8db642979163f03dba27 [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
32#include <osmocore/talloc.h>
33
34#include <osmocom/vty/vty.h>
35#include <osmocom/vty/telnet_interface.h>
36
37#include <sys/stat.h>
38#include <sys/types.h>
39
40#include <fcntl.h>
41#include <signal.h>
42#include <stdio.h>
43#include <stdlib.h>
44#include <string.h>
45#include <assert.h>
46#include <unistd.h>
47
48#ifndef _GNU_SOURCE
49#define _GNU_SOURCE
50#endif
51#include <getopt.h>
52
53#undef PACKAGE_NAME
54#undef PACKAGE_VERSION
55#undef PACKAGE_BUGREPORT
56#undef PACKAGE_TARNAME
57#undef PACKAGE_STRING
58#include <cellmgr_config.h>
59
60static struct log_target *stderr_target;
61
62static char *config = "osmo_stp.cfg";
63
64struct bsc_data bsc;
65extern void cell_vty_init(void);
66
67/*
68 * methods called from the MTP Level3 part
69 */
70void mtp_link_set_forward_sccp(struct mtp_link_set *link, struct msgb *_msg, int sls)
71{
Holger Hans Peter Freyther9cf11bc2011-01-17 15:53:06 +010072 struct mtp_link_set *target;
73
74 target = bsc.m2ua_set == link ? bsc.link_set : bsc.m2ua_set;
75 mtp_link_set_submit_sccp_data(target, sls, _msg->l2h, msgb_l2len(_msg));
Holger Hans Peter Freythera93b83b2011-01-17 15:21:06 +010076}
77
Holger Hans Peter Freyther1b6901e2011-01-17 16:13:28 +010078void mtp_link_set_forward_isup(struct mtp_link_set *set, struct msgb *msg, int sls)
79{
80 struct mtp_link_set *target;
81
82 target = bsc.m2ua_set == set ? bsc.link_set : bsc.m2ua_set;
83 mtp_link_set_submit_isup_data(target, sls, msg->l3h, msgb_l3len(msg));
84}
85
Holger Hans Peter Freythera93b83b2011-01-17 15:21:06 +010086void mtp_linkset_down(struct mtp_link_set *set)
87{
88 set->available = 0;
89 mtp_link_set_stop(set);
90}
91
92void mtp_linkset_up(struct mtp_link_set *set)
93{
94 set->available = 1;
95 mtp_link_set_reset(set);
96}
97
98static void print_usage()
99{
100 printf("Usage: osmo-stp\n");
101}
102
103static void sigint()
104{
105 static pthread_mutex_t exit_mutex = PTHREAD_MUTEX_INITIALIZER;
106 static int handled = 0;
107
108 /* failed to lock */
109 if (pthread_mutex_trylock(&exit_mutex) != 0)
110 return;
111 if (handled)
112 goto out;
113
114 printf("Terminating.\n");
115 handled = 1;
116 if (bsc.setup)
117 link_shutdown_all(bsc.link_set);
118 exit(0);
119
120out:
121 pthread_mutex_unlock(&exit_mutex);
122}
123
124static void print_help()
125{
126 printf(" Some useful help...\n");
127 printf(" -h --help this text\n");
128 printf(" -c --config=CFG The config file to use.\n");
129 printf(" -p --pcap=FILE. Write MSUs to the PCAP file.\n");
130 printf(" -c --once. Send the SLTM msg only once.\n");
131 printf(" -v --version. Print the version number\n");
132}
133
134static void handle_options(int argc, char **argv)
135{
136 while (1) {
137 int option_index = 0, c;
138 static struct option long_options[] = {
139 {"help", 0, 0, 'h'},
140 {"config", 1, 0, 'c'},
141 {"pcap", 1, 0, 'p'},
142 {"version", 0, 0, 0},
143 {0, 0, 0, 0},
144 };
145
146 c = getopt_long(argc, argv, "hc:p:v",
147 long_options, &option_index);
148 if (c == -1)
149 break;
150
151 switch (c) {
152 case 'h':
153 print_usage();
154 print_help();
155 exit(0);
156 case 'p':
157 if (bsc.pcap_fd >= 0)
158 close(bsc.pcap_fd);
159 bsc.pcap_fd = open(optarg, O_WRONLY | O_TRUNC | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP| S_IROTH);
160 if (bsc.pcap_fd < 0) {
161 fprintf(stderr, "Failed to open PCAP file.\n");
162 exit(0);
163 }
164 mtp_pcap_write_header(bsc.pcap_fd);
165 break;
166 case 'c':
167 config = optarg;
168 break;
169 case 'v':
170 printf("This is %s version %s.\n", PACKAGE, VERSION);
171 exit(0);
172 break;
173 default:
174 fprintf(stderr, "Unknown option.\n");
175 break;
176 }
177 }
178}
179
180int main(int argc, char **argv)
181{
182 int rc;
Holger Hans Peter Freyther9cf11bc2011-01-17 15:53:06 +0100183 struct mtp_link *data;
184 struct mtp_m2ua_link *lnk;
Holger Hans Peter Freythera93b83b2011-01-17 15:21:06 +0100185 INIT_LLIST_HEAD(&bsc.sccp_connections);
186
187 bsc.dpc = 1;
188 bsc.opc = 0;
189 bsc.sccp_opc = -1;
190 bsc.udp_port = 3456;
191 bsc.udp_ip = NULL;
192 bsc.src_port = 1313;
193 bsc.ni_ni = MTP_NI_NATION_NET;
194 bsc.ni_spare = 0;
195
196 mtp_link_set_init();
197 thread_init();
198
199 log_init(&log_info);
200 stderr_target = log_target_create_stderr();
201 log_add_target(stderr_target);
202
203 /* enable filters */
204 log_set_all_filter(stderr_target, 1);
205 log_set_category_filter(stderr_target, DINP, 1, LOGL_INFO);
206 log_set_category_filter(stderr_target, DSCCP, 1, LOGL_INFO);
207 log_set_category_filter(stderr_target, DMSC, 1, LOGL_INFO);
208 log_set_category_filter(stderr_target, DMGCP, 1, LOGL_INFO);
209 log_set_print_timestamp(stderr_target, 1);
210 log_set_use_color(stderr_target, 0);
211
212 sccp_set_log_area(DSCCP);
213
214 bsc.setup = 0;
215 bsc.msc_address = "127.0.0.1";
216 bsc.pcap_fd = -1;
217 bsc.udp_reset_timeout = 180;
218 bsc.ping_time = 20;
219 bsc.pong_time = 5;
220 bsc.msc_time = 20;
221 bsc.forward_only = 1;
222
223 handle_options(argc, argv);
224
225 signal(SIGPIPE, SIG_IGN);
226 signal(SIGINT, sigint);
227 srand(time(NULL));
228
229 cell_vty_init();
230 if (vty_read_config_file(config, NULL) < 0) {
231 fprintf(stderr, "Failed to read the VTY config.\n");
232 return -1;
233 }
234
235 rc = telnet_init(NULL, NULL, 4242);
236 if (rc < 0)
237 return rc;
238
239 if (link_init(&bsc) != 0)
240 return -1;
241
Holger Hans Peter Freyther9cf11bc2011-01-17 15:53:06 +0100242 bsc.m2ua_set = mtp_link_set_alloc();
243 bsc.m2ua_set->dpc = 92;
244 bsc.m2ua_set->opc = 9;
245 bsc.m2ua_set->sccp_opc = 9;
246 bsc.m2ua_set->ni = 3;
247 bsc.m2ua_set->bsc = &bsc;
248
249 lnk = sctp_m2ua_transp_create("0.0.0.0", 2904);
250 lnk->base.pcap_fd = -1;
251 lnk->base.the_link = bsc.m2ua_set;
252 mtp_link_set_add_link(bsc.m2ua_set, (struct mtp_link *) lnk);
253
254 llist_for_each_entry(data, &bsc.m2ua_set->links, entry)
255 data->start(data);
256
Holger Hans Peter Freythera93b83b2011-01-17 15:21:06 +0100257 while (1) {
258 bsc_select_main(0);
259 }
260
261 return 0;
262}
263
264/* dummy for links */
265int msc_init(struct bsc_data *data, int dummy)
266{
267 return 0;
268}
269