blob: 94dc0ed5bbf75b288908c39d3e6a28361d4dab62 [file] [log] [blame]
Holger Hans Peter Freythereef86b52010-06-15 18:45:06 +08001/* Routines to talk to the MSC using the IPA Protocol */
2/*
3 * (C) 2010 by Holger Hans Peter Freyther <zecke@selfish.org>
Holger Hans Peter Freytherdf6143a2010-06-15 18:46:56 +08004 * (C) 2010 by On-Waves
Holger Hans Peter Freythereef86b52010-06-15 18:45:06 +08005 * 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 <openbsc/bsc_msc.h>
Holger Hans Peter Freyther6c0a04e2010-03-26 10:41:20 +010024#include <openbsc/debug.h>
25
26#include <osmocore/write_queue.h>
Holger Hans Peter Freytherbaf2abe2010-06-15 18:47:29 +080027#include <osmocore/talloc.h>
Holger Hans Peter Freythereef86b52010-06-15 18:45:06 +080028
29#include <arpa/inet.h>
30#include <sys/socket.h>
Holger Hans Peter Freyther6c0a04e2010-03-26 10:41:20 +010031#include <errno.h>
32#include <fcntl.h>
Holger Hans Peter Freythereef86b52010-06-15 18:45:06 +080033#include <stdio.h>
34#include <string.h>
35#include <unistd.h>
36
Holger Hans Peter Freytherbaf2abe2010-06-15 18:47:29 +080037static void connection_loss(struct bsc_msc_connection *con)
38{
39 struct bsc_fd *fd;
40
41 fd = &con->write_queue.bfd;
42
43 close(fd->fd);
44 fd->fd = -1;
45 fd->cb = write_queue_bfd_cb;
46 fd->when = 0;
47
48 con->is_connected = 0;
49 con->connection_loss(con);
50}
51
Holger Hans Peter Freyther6c0a04e2010-03-26 10:41:20 +010052/* called in the case of a non blocking connect */
53static int msc_connection_connect(struct bsc_fd *fd, unsigned int what)
54{
55 int rc;
56 int val;
Holger Hans Peter Freytherbaf2abe2010-06-15 18:47:29 +080057 struct bsc_msc_connection *con;
Holger Hans Peter Freyther6c0a04e2010-03-26 10:41:20 +010058 struct write_queue *queue;
59
60 socklen_t len = sizeof(val);
61
62 if ((what & BSC_FD_WRITE) == 0) {
63 LOGP(DMSC, LOGL_ERROR, "Callback but not readable.\n");
64 return -1;
65 }
66
Holger Hans Peter Freytherbaf2abe2010-06-15 18:47:29 +080067 queue = container_of(fd, struct write_queue, bfd);
68 con = container_of(queue, struct bsc_msc_connection, write_queue);
69
Holger Hans Peter Freyther6c0a04e2010-03-26 10:41:20 +010070 /* check the socket state */
71 rc = getsockopt(fd->fd, SOL_SOCKET, SO_ERROR, &val, &len);
72 if (rc != 0) {
73 LOGP(DMSC, LOGL_ERROR, "getsockopt for the MSC socket failed.\n");
74 goto error;
75 }
76 if (val != 0) {
Holger Hans Peter Freyther9d90da92010-04-06 10:25:00 +020077 LOGP(DMSC, LOGL_ERROR, "Not connected to the MSC: %d\n", val);
Holger Hans Peter Freyther6c0a04e2010-03-26 10:41:20 +010078 goto error;
79 }
80
81
82 /* go to full operation */
Holger Hans Peter Freyther6c0a04e2010-03-26 10:41:20 +010083 fd->cb = write_queue_bfd_cb;
84 fd->when = BSC_FD_READ;
Holger Hans Peter Freytherbaf2abe2010-06-15 18:47:29 +080085
86 con->is_connected = 1;
Holger Hans Peter Freytherb9ac37d2010-04-05 17:58:52 +020087 LOGP(DMSC, LOGL_NOTICE, "(Re)Connected to the MSC.\n");
Holger Hans Peter Freytherbaf2abe2010-06-15 18:47:29 +080088 if (con->connected)
89 con->connected(con);
Holger Hans Peter Freyther6c0a04e2010-03-26 10:41:20 +010090 return 0;
91
92error:
93 bsc_unregister_fd(fd);
Holger Hans Peter Freytherbaf2abe2010-06-15 18:47:29 +080094 connection_loss(con);
Holger Hans Peter Freyther6c0a04e2010-03-26 10:41:20 +010095 return -1;
96}
97static void setnonblocking(struct bsc_fd *fd)
98{
99 int flags;
100
101 flags = fcntl(fd->fd, F_GETFL);
102 if (flags < 0) {
103 perror("fcntl get failed");
104 close(fd->fd);
105 fd->fd = -1;
106 return;
107 }
108
109 flags |= O_NONBLOCK;
110 flags = fcntl(fd->fd, F_SETFL, flags);
111 if (flags < 0) {
112 perror("fcntl get failed");
113 close(fd->fd);
114 fd->fd = -1;
115 return;
116 }
117}
118
Holger Hans Peter Freytherbaf2abe2010-06-15 18:47:29 +0800119int bsc_msc_connect(struct bsc_msc_connection *con)
Holger Hans Peter Freythereef86b52010-06-15 18:45:06 +0800120{
Holger Hans Peter Freytherbaf2abe2010-06-15 18:47:29 +0800121 struct bsc_fd *fd;
Holger Hans Peter Freythereef86b52010-06-15 18:45:06 +0800122 struct sockaddr_in sin;
123 int on = 1, ret;
124
Holger Hans Peter Freytherbaf2abe2010-06-15 18:47:29 +0800125 LOGP(DMSC, LOGL_NOTICE, "Attempting to connect MSC at %s:%d\n", con->ip, con->port);
Holger Hans Peter Freythereef86b52010-06-15 18:45:06 +0800126
Holger Hans Peter Freytherbaf2abe2010-06-15 18:47:29 +0800127 con->is_connected = 0;
128
129 fd = &con->write_queue.bfd;
Holger Hans Peter Freythereef86b52010-06-15 18:45:06 +0800130 fd->fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
Holger Hans Peter Freythereef86b52010-06-15 18:45:06 +0800131 fd->data = NULL;
132 fd->priv_nr = 1;
133
134 if (fd->fd < 0) {
135 perror("Creating TCP socket failed");
136 return fd->fd;
137 }
138
Holger Hans Peter Freyther6c0a04e2010-03-26 10:41:20 +0100139 /* make it non blocking */
140 setnonblocking(fd);
Holger Hans Peter Freythereef86b52010-06-15 18:45:06 +0800141
142 memset(&sin, 0, sizeof(sin));
143 sin.sin_family = AF_INET;
Holger Hans Peter Freytherbaf2abe2010-06-15 18:47:29 +0800144 sin.sin_port = htons(con->port);
145 inet_aton(con->ip, &sin.sin_addr);
Holger Hans Peter Freythereef86b52010-06-15 18:45:06 +0800146
147 setsockopt(fd->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
148 ret = connect(fd->fd, (struct sockaddr *) &sin, sizeof(sin));
149
Holger Hans Peter Freyther6c0a04e2010-03-26 10:41:20 +0100150 if (ret == -1 && errno == EINPROGRESS) {
151 LOGP(DMSC, LOGL_ERROR, "MSC Connection in progress\n");
152 fd->when = BSC_FD_WRITE;
153 fd->cb = msc_connection_connect;
154 } else if (ret < 0) {
Holger Hans Peter Freythereef86b52010-06-15 18:45:06 +0800155 perror("Connection failed");
Holger Hans Peter Freytherbaf2abe2010-06-15 18:47:29 +0800156 connection_loss(con);
Holger Hans Peter Freythereef86b52010-06-15 18:45:06 +0800157 return ret;
Holger Hans Peter Freyther6c0a04e2010-03-26 10:41:20 +0100158 } else {
159 fd->when = BSC_FD_READ;
160 fd->cb = write_queue_bfd_cb;
Holger Hans Peter Freytherbaf2abe2010-06-15 18:47:29 +0800161 con->is_connected = 1;
162 if (con->connected)
163 con->connected(con);
Holger Hans Peter Freythereef86b52010-06-15 18:45:06 +0800164 }
165
166 ret = bsc_register_fd(fd);
167 if (ret < 0) {
168 perror("Registering the fd failed");
169 close(fd->fd);
170 return ret;
171 }
172
173 return ret;
174}
Holger Hans Peter Freytherbaf2abe2010-06-15 18:47:29 +0800175
176struct bsc_msc_connection *bsc_msc_create(const char *ip, int port)
177{
178 struct bsc_msc_connection *con;
179
180 con = talloc_zero(NULL, struct bsc_msc_connection);
181 if (!con) {
182 LOGP(DMSC, LOGL_FATAL, "Failed to create the MSC connection.\n");
183 return NULL;
184 }
185
186 con->ip = ip;
187 con->port = port;
188 write_queue_init(&con->write_queue, 100);
189 return con;
190}
191
192void bsc_msc_lost(struct bsc_msc_connection *con)
193{
Holger Hans Peter Freyther03ca97e2010-03-31 13:15:26 +0200194 write_queue_clear(&con->write_queue);
Holger Hans Peter Freytherbaf2abe2010-06-15 18:47:29 +0800195 bsc_unregister_fd(&con->write_queue.bfd);
196 connection_loss(con);
197}
198
199static void reconnect_msc(void *_msc)
200{
201 struct bsc_msc_connection *con = _msc;
202
203 LOGP(DMSC, LOGL_NOTICE, "Attempting to reconnect to the MSC.\n");
204 bsc_msc_connect(con);
205}
206
207void bsc_msc_schedule_connect(struct bsc_msc_connection *con)
208{
209 LOGP(DMSC, LOGL_NOTICE, "Attempting to reconnect to the MSC.\n");
210 con->reconnect_timer.cb = reconnect_msc;
211 con->reconnect_timer.data = con;
212 bsc_schedule_timer(&con->reconnect_timer, 5, 0);
213}