Holger Hans Peter Freyther | 9e16ba5 | 2010-06-15 18:45:06 +0800 | [diff] [blame] | 1 | /* 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 Freyther | 98e49d4 | 2010-06-15 18:46:56 +0800 | [diff] [blame] | 4 | * (C) 2010 by On-Waves |
Holger Hans Peter Freyther | 9e16ba5 | 2010-06-15 18:45:06 +0800 | [diff] [blame] | 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 <openbsc/bsc_msc.h> |
Holger Hans Peter Freyther | 8b901fa | 2010-03-26 10:41:20 +0100 | [diff] [blame] | 24 | #include <openbsc/debug.h> |
Holger Hans Peter Freyther | afe60c0 | 2010-06-15 18:52:05 +0800 | [diff] [blame] | 25 | #include <openbsc/ipaccess.h> |
Holger Hans Peter Freyther | 8b901fa | 2010-03-26 10:41:20 +0100 | [diff] [blame] | 26 | |
| 27 | #include <osmocore/write_queue.h> |
Holger Hans Peter Freyther | 4a860a0 | 2010-06-15 18:47:29 +0800 | [diff] [blame] | 28 | #include <osmocore/talloc.h> |
Holger Hans Peter Freyther | 9e16ba5 | 2010-06-15 18:45:06 +0800 | [diff] [blame] | 29 | |
| 30 | #include <arpa/inet.h> |
| 31 | #include <sys/socket.h> |
Holger Hans Peter Freyther | 8b901fa | 2010-03-26 10:41:20 +0100 | [diff] [blame] | 32 | #include <errno.h> |
| 33 | #include <fcntl.h> |
Holger Hans Peter Freyther | 9e16ba5 | 2010-06-15 18:45:06 +0800 | [diff] [blame] | 34 | #include <stdio.h> |
| 35 | #include <string.h> |
| 36 | #include <unistd.h> |
| 37 | |
Holger Hans Peter Freyther | 4a860a0 | 2010-06-15 18:47:29 +0800 | [diff] [blame] | 38 | static void connection_loss(struct bsc_msc_connection *con) |
| 39 | { |
| 40 | struct bsc_fd *fd; |
| 41 | |
| 42 | fd = &con->write_queue.bfd; |
| 43 | |
| 44 | close(fd->fd); |
| 45 | fd->fd = -1; |
| 46 | fd->cb = write_queue_bfd_cb; |
| 47 | fd->when = 0; |
| 48 | |
| 49 | con->is_connected = 0; |
| 50 | con->connection_loss(con); |
| 51 | } |
| 52 | |
Holger Hans Peter Freyther | 9b6f999 | 2010-05-05 22:48:56 +0800 | [diff] [blame] | 53 | static void msc_con_timeout(void *_con) |
| 54 | { |
| 55 | struct bsc_msc_connection *con = _con; |
| 56 | |
| 57 | LOGP(DMSC, LOGL_ERROR, "MSC Connection timeout.\n"); |
| 58 | bsc_msc_lost(con); |
| 59 | } |
| 60 | |
Holger Hans Peter Freyther | 1f9d511 | 2010-04-08 11:12:32 +0200 | [diff] [blame] | 61 | static int bsc_msc_except(struct bsc_fd *bfd) |
| 62 | { |
| 63 | struct write_queue *wrt; |
| 64 | struct bsc_msc_connection *con; |
| 65 | |
| 66 | LOGP(DMSC, LOGL_ERROR, "Exception on the BFD. Closing down.\n"); |
| 67 | |
| 68 | wrt = container_of(bfd, struct write_queue, bfd); |
| 69 | con = container_of(wrt, struct bsc_msc_connection, write_queue); |
| 70 | |
| 71 | connection_loss(con); |
| 72 | return 0; |
| 73 | } |
| 74 | |
Holger Hans Peter Freyther | 8b901fa | 2010-03-26 10:41:20 +0100 | [diff] [blame] | 75 | /* called in the case of a non blocking connect */ |
| 76 | static int msc_connection_connect(struct bsc_fd *fd, unsigned int what) |
| 77 | { |
| 78 | int rc; |
| 79 | int val; |
Holger Hans Peter Freyther | 4a860a0 | 2010-06-15 18:47:29 +0800 | [diff] [blame] | 80 | struct bsc_msc_connection *con; |
Holger Hans Peter Freyther | 8b901fa | 2010-03-26 10:41:20 +0100 | [diff] [blame] | 81 | struct write_queue *queue; |
| 82 | |
| 83 | socklen_t len = sizeof(val); |
| 84 | |
| 85 | if ((what & BSC_FD_WRITE) == 0) { |
| 86 | LOGP(DMSC, LOGL_ERROR, "Callback but not readable.\n"); |
| 87 | return -1; |
| 88 | } |
| 89 | |
Holger Hans Peter Freyther | 4a860a0 | 2010-06-15 18:47:29 +0800 | [diff] [blame] | 90 | queue = container_of(fd, struct write_queue, bfd); |
| 91 | con = container_of(queue, struct bsc_msc_connection, write_queue); |
| 92 | |
Holger Hans Peter Freyther | 8b901fa | 2010-03-26 10:41:20 +0100 | [diff] [blame] | 93 | /* check the socket state */ |
| 94 | rc = getsockopt(fd->fd, SOL_SOCKET, SO_ERROR, &val, &len); |
| 95 | if (rc != 0) { |
| 96 | LOGP(DMSC, LOGL_ERROR, "getsockopt for the MSC socket failed.\n"); |
| 97 | goto error; |
| 98 | } |
| 99 | if (val != 0) { |
Holger Hans Peter Freyther | 1764368 | 2010-04-06 10:25:00 +0200 | [diff] [blame] | 100 | LOGP(DMSC, LOGL_ERROR, "Not connected to the MSC: %d\n", val); |
Holger Hans Peter Freyther | 8b901fa | 2010-03-26 10:41:20 +0100 | [diff] [blame] | 101 | goto error; |
| 102 | } |
| 103 | |
| 104 | |
| 105 | /* go to full operation */ |
Holger Hans Peter Freyther | 8b901fa | 2010-03-26 10:41:20 +0100 | [diff] [blame] | 106 | fd->cb = write_queue_bfd_cb; |
Holger Hans Peter Freyther | 1f9d511 | 2010-04-08 11:12:32 +0200 | [diff] [blame] | 107 | fd->when = BSC_FD_READ | BSC_FD_EXCEPT; |
Holger Hans Peter Freyther | 4a860a0 | 2010-06-15 18:47:29 +0800 | [diff] [blame] | 108 | |
| 109 | con->is_connected = 1; |
Holger Hans Peter Freyther | 9b6f999 | 2010-05-05 22:48:56 +0800 | [diff] [blame] | 110 | bsc_del_timer(&con->timeout_timer); |
Holger Hans Peter Freyther | c12e193 | 2010-04-05 17:58:52 +0200 | [diff] [blame] | 111 | LOGP(DMSC, LOGL_NOTICE, "(Re)Connected to the MSC.\n"); |
Holger Hans Peter Freyther | 4a860a0 | 2010-06-15 18:47:29 +0800 | [diff] [blame] | 112 | if (con->connected) |
| 113 | con->connected(con); |
Holger Hans Peter Freyther | 8b901fa | 2010-03-26 10:41:20 +0100 | [diff] [blame] | 114 | return 0; |
| 115 | |
| 116 | error: |
| 117 | bsc_unregister_fd(fd); |
Holger Hans Peter Freyther | 4a860a0 | 2010-06-15 18:47:29 +0800 | [diff] [blame] | 118 | connection_loss(con); |
Holger Hans Peter Freyther | 8b901fa | 2010-03-26 10:41:20 +0100 | [diff] [blame] | 119 | return -1; |
| 120 | } |
| 121 | static void setnonblocking(struct bsc_fd *fd) |
| 122 | { |
| 123 | int flags; |
| 124 | |
| 125 | flags = fcntl(fd->fd, F_GETFL); |
| 126 | if (flags < 0) { |
| 127 | perror("fcntl get failed"); |
| 128 | close(fd->fd); |
| 129 | fd->fd = -1; |
| 130 | return; |
| 131 | } |
| 132 | |
| 133 | flags |= O_NONBLOCK; |
| 134 | flags = fcntl(fd->fd, F_SETFL, flags); |
| 135 | if (flags < 0) { |
| 136 | perror("fcntl get failed"); |
| 137 | close(fd->fd); |
| 138 | fd->fd = -1; |
| 139 | return; |
| 140 | } |
| 141 | } |
| 142 | |
Holger Hans Peter Freyther | 4a860a0 | 2010-06-15 18:47:29 +0800 | [diff] [blame] | 143 | int bsc_msc_connect(struct bsc_msc_connection *con) |
Holger Hans Peter Freyther | 9e16ba5 | 2010-06-15 18:45:06 +0800 | [diff] [blame] | 144 | { |
Holger Hans Peter Freyther | 4a860a0 | 2010-06-15 18:47:29 +0800 | [diff] [blame] | 145 | struct bsc_fd *fd; |
Holger Hans Peter Freyther | 9e16ba5 | 2010-06-15 18:45:06 +0800 | [diff] [blame] | 146 | struct sockaddr_in sin; |
| 147 | int on = 1, ret; |
| 148 | |
Holger Hans Peter Freyther | 4a860a0 | 2010-06-15 18:47:29 +0800 | [diff] [blame] | 149 | LOGP(DMSC, LOGL_NOTICE, "Attempting to connect MSC at %s:%d\n", con->ip, con->port); |
Holger Hans Peter Freyther | 9e16ba5 | 2010-06-15 18:45:06 +0800 | [diff] [blame] | 150 | |
Holger Hans Peter Freyther | 4a860a0 | 2010-06-15 18:47:29 +0800 | [diff] [blame] | 151 | con->is_connected = 0; |
| 152 | |
| 153 | fd = &con->write_queue.bfd; |
Holger Hans Peter Freyther | 9e16ba5 | 2010-06-15 18:45:06 +0800 | [diff] [blame] | 154 | fd->fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); |
Holger Hans Peter Freyther | 9e16ba5 | 2010-06-15 18:45:06 +0800 | [diff] [blame] | 155 | fd->data = NULL; |
| 156 | fd->priv_nr = 1; |
| 157 | |
| 158 | if (fd->fd < 0) { |
| 159 | perror("Creating TCP socket failed"); |
| 160 | return fd->fd; |
| 161 | } |
| 162 | |
Holger Hans Peter Freyther | 8b901fa | 2010-03-26 10:41:20 +0100 | [diff] [blame] | 163 | /* make it non blocking */ |
| 164 | setnonblocking(fd); |
Holger Hans Peter Freyther | 9e16ba5 | 2010-06-15 18:45:06 +0800 | [diff] [blame] | 165 | |
| 166 | memset(&sin, 0, sizeof(sin)); |
| 167 | sin.sin_family = AF_INET; |
Holger Hans Peter Freyther | 4a860a0 | 2010-06-15 18:47:29 +0800 | [diff] [blame] | 168 | sin.sin_port = htons(con->port); |
| 169 | inet_aton(con->ip, &sin.sin_addr); |
Holger Hans Peter Freyther | 9e16ba5 | 2010-06-15 18:45:06 +0800 | [diff] [blame] | 170 | |
| 171 | setsockopt(fd->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)); |
| 172 | ret = connect(fd->fd, (struct sockaddr *) &sin, sizeof(sin)); |
| 173 | |
Holger Hans Peter Freyther | 8b901fa | 2010-03-26 10:41:20 +0100 | [diff] [blame] | 174 | if (ret == -1 && errno == EINPROGRESS) { |
| 175 | LOGP(DMSC, LOGL_ERROR, "MSC Connection in progress\n"); |
| 176 | fd->when = BSC_FD_WRITE; |
| 177 | fd->cb = msc_connection_connect; |
Holger Hans Peter Freyther | 9b6f999 | 2010-05-05 22:48:56 +0800 | [diff] [blame] | 178 | con->timeout_timer.cb = msc_con_timeout; |
| 179 | con->timeout_timer.data = con; |
| 180 | bsc_schedule_timer(&con->timeout_timer, 20, 0); |
Holger Hans Peter Freyther | 8b901fa | 2010-03-26 10:41:20 +0100 | [diff] [blame] | 181 | } else if (ret < 0) { |
Holger Hans Peter Freyther | 9e16ba5 | 2010-06-15 18:45:06 +0800 | [diff] [blame] | 182 | perror("Connection failed"); |
Holger Hans Peter Freyther | 4a860a0 | 2010-06-15 18:47:29 +0800 | [diff] [blame] | 183 | connection_loss(con); |
Holger Hans Peter Freyther | 9e16ba5 | 2010-06-15 18:45:06 +0800 | [diff] [blame] | 184 | return ret; |
Holger Hans Peter Freyther | 8b901fa | 2010-03-26 10:41:20 +0100 | [diff] [blame] | 185 | } else { |
Holger Hans Peter Freyther | 1f9d511 | 2010-04-08 11:12:32 +0200 | [diff] [blame] | 186 | fd->when = BSC_FD_READ | BSC_FD_EXCEPT; |
Holger Hans Peter Freyther | 8b901fa | 2010-03-26 10:41:20 +0100 | [diff] [blame] | 187 | fd->cb = write_queue_bfd_cb; |
Holger Hans Peter Freyther | 4a860a0 | 2010-06-15 18:47:29 +0800 | [diff] [blame] | 188 | con->is_connected = 1; |
| 189 | if (con->connected) |
| 190 | con->connected(con); |
Holger Hans Peter Freyther | 9e16ba5 | 2010-06-15 18:45:06 +0800 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | ret = bsc_register_fd(fd); |
| 194 | if (ret < 0) { |
| 195 | perror("Registering the fd failed"); |
| 196 | close(fd->fd); |
| 197 | return ret; |
| 198 | } |
| 199 | |
| 200 | return ret; |
| 201 | } |
Holger Hans Peter Freyther | 4a860a0 | 2010-06-15 18:47:29 +0800 | [diff] [blame] | 202 | |
| 203 | struct bsc_msc_connection *bsc_msc_create(const char *ip, int port) |
| 204 | { |
| 205 | struct bsc_msc_connection *con; |
| 206 | |
| 207 | con = talloc_zero(NULL, struct bsc_msc_connection); |
| 208 | if (!con) { |
| 209 | LOGP(DMSC, LOGL_FATAL, "Failed to create the MSC connection.\n"); |
| 210 | return NULL; |
| 211 | } |
| 212 | |
| 213 | con->ip = ip; |
| 214 | con->port = port; |
| 215 | write_queue_init(&con->write_queue, 100); |
Holger Hans Peter Freyther | 1f9d511 | 2010-04-08 11:12:32 +0200 | [diff] [blame] | 216 | con->write_queue.except_cb = bsc_msc_except; |
Holger Hans Peter Freyther | 4a860a0 | 2010-06-15 18:47:29 +0800 | [diff] [blame] | 217 | return con; |
| 218 | } |
| 219 | |
| 220 | void bsc_msc_lost(struct bsc_msc_connection *con) |
| 221 | { |
Holger Hans Peter Freyther | b47bf47 | 2010-03-31 13:15:26 +0200 | [diff] [blame] | 222 | write_queue_clear(&con->write_queue); |
Holger Hans Peter Freyther | 4a860a0 | 2010-06-15 18:47:29 +0800 | [diff] [blame] | 223 | bsc_unregister_fd(&con->write_queue.bfd); |
| 224 | connection_loss(con); |
| 225 | } |
| 226 | |
| 227 | static void reconnect_msc(void *_msc) |
| 228 | { |
| 229 | struct bsc_msc_connection *con = _msc; |
| 230 | |
| 231 | LOGP(DMSC, LOGL_NOTICE, "Attempting to reconnect to the MSC.\n"); |
| 232 | bsc_msc_connect(con); |
| 233 | } |
| 234 | |
| 235 | void bsc_msc_schedule_connect(struct bsc_msc_connection *con) |
| 236 | { |
| 237 | LOGP(DMSC, LOGL_NOTICE, "Attempting to reconnect to the MSC.\n"); |
| 238 | con->reconnect_timer.cb = reconnect_msc; |
| 239 | con->reconnect_timer.data = con; |
| 240 | bsc_schedule_timer(&con->reconnect_timer, 5, 0); |
Holger Hans Peter Freyther | afe60c0 | 2010-06-15 18:52:05 +0800 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | struct msgb *bsc_msc_id_get_resp(const char *token) |
| 244 | { |
| 245 | struct msgb *msg; |
| 246 | |
| 247 | if (!token) { |
| 248 | LOGP(DMSC, LOGL_ERROR, "No token specified.\n"); |
| 249 | return NULL; |
| 250 | } |
| 251 | |
| 252 | msg = msgb_alloc_headroom(4096, 128, "id resp"); |
| 253 | if (!msg) { |
| 254 | LOGP(DMSC, LOGL_ERROR, "Failed to create the message.\n"); |
| 255 | return NULL; |
| 256 | } |
| 257 | |
| 258 | msg->l2h = msgb_v_put(msg, IPAC_MSGT_ID_RESP); |
| 259 | msgb_l16tv_put(msg, strlen(token) + 1, |
| 260 | IPAC_IDTAG_UNITNAME, (u_int8_t *) token); |
| 261 | return msg; |
Holger Hans Peter Freyther | 4a860a0 | 2010-06-15 18:47:29 +0800 | [diff] [blame] | 262 | } |