blob: 153630123592c7d108ea03087ca8daa836904e0c [file] [log] [blame]
Holger Hans Peter Freyther594ee9a2010-11-16 11:03:19 +01001/* Relay UDT/all SCCP messages */
2/*
3 * (C) 2010 by Holger Hans Peter Freyther <zecke@selfish.org>
4 * (C) 2010 by On-Waves
5 * All Rights Reserved
6 *
Holger Hans Peter Freytherde56c222011-01-16 17:45:14 +01007 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
Holger Hans Peter Freyther594ee9a2010-11-16 11:03:19 +010010 * (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
Holger Hans Peter Freytherde56c222011-01-16 17:45:14 +010015 * GNU Affero General Public License for more details.
Holger Hans Peter Freyther594ee9a2010-11-16 11:03:19 +010016 *
Holger Hans Peter Freytherde56c222011-01-16 17:45:14 +010017 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
Holger Hans Peter Freyther594ee9a2010-11-16 11:03:19 +010019 *
20 */
21
22#include <mtp_data.h>
Holger Hans Peter Freyther396282e2010-12-08 10:08:14 +010023#include <mtp_level3.h>
Holger Hans Peter Freyther594ee9a2010-11-16 11:03:19 +010024#include <mtp_pcap.h>
25#include <thread.h>
26#include <bsc_data.h>
27#include <snmp_mtp.h>
28#include <cellmgr_debug.h>
29
30#include <osmocore/talloc.h>
31
32#include <osmocom/vty/vty.h>
33#include <osmocom/vty/telnet_interface.h>
34
35#include <sys/stat.h>
36#include <sys/types.h>
37
38#include <fcntl.h>
39#include <signal.h>
40#include <stdio.h>
41#include <stdlib.h>
42#include <string.h>
43#include <assert.h>
44#include <unistd.h>
45
46#ifndef _GNU_SOURCE
47#define _GNU_SOURCE
48#endif
49#include <getopt.h>
50
51#undef PACKAGE_NAME
52#undef PACKAGE_VERSION
53#undef PACKAGE_BUGREPORT
54#undef PACKAGE_TARNAME
55#undef PACKAGE_STRING
56#include <cellmgr_config.h>
57
58static struct log_target *stderr_target;
59
60static char *config = "udt_relay.cfg";
61
62struct bsc_data bsc;
63extern void cell_vty_init(void);
64
Holger Hans Peter Freyther594ee9a2010-11-16 11:03:19 +010065/*
66 * methods called from the MTP Level3 part
67 */
Holger Hans Peter Freyther569f1e12011-01-02 18:47:49 +010068void mtp_link_set_forward_sccp(struct mtp_link_set *link, struct msgb *_msg, int sls)
Holger Hans Peter Freyther594ee9a2010-11-16 11:03:19 +010069{
70 msc_send_direct(&bsc, _msg);
71}
72
73void bsc_link_down(struct link_data *data)
74{
75 int was_up;
Holger Hans Peter Freyther569f1e12011-01-02 18:47:49 +010076 struct mtp_link_set *link = data->the_link;
Holger Hans Peter Freyther594ee9a2010-11-16 11:03:19 +010077
78 link->available = 0;
79 was_up = link->sccp_up;
Holger Hans Peter Freyther569f1e12011-01-02 18:47:49 +010080 mtp_link_set_stop(link);
Holger Hans Peter Freyther594ee9a2010-11-16 11:03:19 +010081
82 data->clear_queue(data);
83
Holger Hans Peter Freyther594ee9a2010-11-16 11:03:19 +010084 /* If we have an A link send a reset to the MSC */
85 msc_send_reset(data->bsc);
86}
87
88void bsc_link_up(struct link_data *data)
89{
90 data->the_link->available = 1;
Holger Hans Peter Freyther569f1e12011-01-02 18:47:49 +010091 mtp_link_set_reset(data->the_link);
Holger Hans Peter Freyther594ee9a2010-11-16 11:03:19 +010092}
93
94static void print_usage()
95{
96 printf("Usage: cellmgr_ng\n");
97}
98
99static void sigint()
100{
101 static pthread_mutex_t exit_mutex = PTHREAD_MUTEX_INITIALIZER;
102 static int handled = 0;
103
104 /* failed to lock */
105 if (pthread_mutex_trylock(&exit_mutex) != 0)
106 return;
107 if (handled)
108 goto out;
109
110 printf("Terminating.\n");
111 handled = 1;
112 if (bsc.setup)
113 bsc.link.shutdown(&bsc.link);
114 exit(0);
115
116out:
117 pthread_mutex_unlock(&exit_mutex);
118}
119
120static void sigusr2()
121{
122 printf("Closing the MSC connection on demand.\n");
123 msc_close_connection(&bsc);
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.link.pcap_fd >= 0)
160 close(bsc.link.pcap_fd);
161 bsc.link.pcap_fd = open(optarg, O_WRONLY | O_TRUNC | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP| S_IROTH);
162 if (bsc.link.pcap_fd < 0) {
163 fprintf(stderr, "Failed to open PCAP file.\n");
164 exit(0);
165 }
166 mtp_pcap_write_header(bsc.link.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
Holger Hans Peter Freyther594ee9a2010-11-16 11:03:19 +0100182int main(int argc, char **argv)
183{
184 int rc;
185 INIT_LLIST_HEAD(&bsc.sccp_connections);
186
187 bsc.dpc = 1;
188 bsc.opc = 0;
Holger Hans Peter Freyther7a725562011-01-01 13:34:58 +0100189 bsc.sccp_opc = -1;
Holger Hans Peter Freyther594ee9a2010-11-16 11:03:19 +0100190 bsc.udp_port = 3456;
191 bsc.udp_ip = NULL;
192 bsc.src_port = 1313;
Holger Hans Peter Freyther396282e2010-12-08 10:08:14 +0100193 bsc.ni_ni = MTP_NI_NATION_NET;
194 bsc.ni_spare = 0;
Holger Hans Peter Freyther594ee9a2010-11-16 11:03:19 +0100195
Holger Hans Peter Freyther569f1e12011-01-02 18:47:49 +0100196 mtp_link_set_init();
Holger Hans Peter Freyther594ee9a2010-11-16 11:03:19 +0100197 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.link.pcap_fd = -1;
217 bsc.link.udp.reset_timeout = 180;
218 bsc.ping_time = 20;
219 bsc.pong_time = 5;
220 bsc.msc_time = 20;
Holger Hans Peter Freyther76943812010-11-16 11:14:34 +0100221 bsc.forward_only = 1;
Holger Hans Peter Freyther594ee9a2010-11-16 11:03:19 +0100222
223 handle_options(argc, argv);
224
225 signal(SIGPIPE, SIG_IGN);
226 signal(SIGINT, sigint);
227 signal(SIGUSR2, sigusr2);
228 srand(time(NULL));
229
230 cell_vty_init();
231 if (vty_read_config_file(config, NULL) < 0) {
232 fprintf(stderr, "Failed to read the VTY config.\n");
233 return -1;
234 }
235
236 rc = telnet_init(NULL, NULL, 4242);
237 if (rc < 0)
238 return rc;
239
Holger Hans Peter Freythera99b04b2011-01-02 11:23:54 +0100240 if (link_init(&bsc) != 0)
241 return -1;
Holger Hans Peter Freyther594ee9a2010-11-16 11:03:19 +0100242
243 while (1) {
244 bsc_select_main(0);
245 }
246
247 return 0;
248}
249
250void release_bsc_resources(struct bsc_data *bsc)
251{
Holger Hans Peter Freyther594ee9a2010-11-16 11:03:19 +0100252}
253
254struct msgb *create_sccp_rlc(struct sccp_source_reference *src_ref,
255 struct sccp_source_reference *dst)
256{
Holger Hans Peter Freyther8220b942010-12-08 10:08:40 +0100257 LOGP(DMSC, LOGL_NOTICE, "Refusing to create connection handling.\n");
Holger Hans Peter Freyther594ee9a2010-11-16 11:03:19 +0100258 return NULL;
259}
260
261struct msgb *create_reset()
262{
Holger Hans Peter Freyther8220b942010-12-08 10:08:40 +0100263 LOGP(DMSC, LOGL_NOTICE, "Refusing to create a GSM0808 reset message.\n");
Holger Hans Peter Freyther594ee9a2010-11-16 11:03:19 +0100264 return NULL;
265}
266
Holger Hans Peter Freyther569f1e12011-01-02 18:47:49 +0100267void update_con_state(struct mtp_link_set *link, int rc, struct sccp_parse_result *res, struct msgb *msg, int from_msc, int sls)
Holger Hans Peter Freyther594ee9a2010-11-16 11:03:19 +0100268{
269 LOGP(DMSC, LOGL_ERROR, "Should not be called.\n");
270 return;
271}
272
273unsigned int sls_for_src_ref(struct sccp_source_reference *ref)
274{
275 return 13;
276}
277
278int bsc_ussd_handle_in_msg(struct bsc_data *bsc, struct sccp_parse_result *res, struct msgb *msg)
279{
280 return 0;
281}
282
283int bsc_ussd_handle_out_msg(struct bsc_data *bsc, struct sccp_parse_result *res, struct msgb *msg)
284{
285 return 0;
286}