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 |
Harald Welte | 0e3e88e | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 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 Freyther | 9e16ba5 | 2010-06-15 18:45:06 +0800 | [diff] [blame] | 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 |
Harald Welte | 0e3e88e | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 15 | * GNU Affero General Public License for more details. |
Holger Hans Peter Freyther | 9e16ba5 | 2010-06-15 18:45:06 +0800 | [diff] [blame] | 16 | * |
Harald Welte | 0e3e88e | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 17 | * 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 Freyther | 9e16ba5 | 2010-06-15 18:45:06 +0800 | [diff] [blame] | 19 | * |
| 20 | */ |
| 21 | |
| 22 | #include <openbsc/bsc_msc.h> |
Holger Hans Peter Freyther | 8b901fa | 2010-03-26 10:41:20 +0100 | [diff] [blame] | 23 | #include <openbsc/debug.h> |
Pablo Neira Ayuso | 42e41df | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 24 | #include <osmocom/abis/ipaccess.h> |
Holger Hans Peter Freyther | 8b901fa | 2010-03-26 10:41:20 +0100 | [diff] [blame] | 25 | |
Pablo Neira Ayuso | dd5fff4 | 2011-03-22 16:47:59 +0100 | [diff] [blame] | 26 | #include <osmocom/core/write_queue.h> |
| 27 | #include <osmocom/core/talloc.h> |
Holger Hans Peter Freyther | 9e16ba5 | 2010-06-15 18:45:06 +0800 | [diff] [blame] | 28 | |
Pablo Neira Ayuso | 42e41df | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 29 | #include <osmocom/gsm/tlv.h> |
| 30 | |
Holger Hans Peter Freyther | 9e16ba5 | 2010-06-15 18:45:06 +0800 | [diff] [blame] | 31 | #include <arpa/inet.h> |
| 32 | #include <sys/socket.h> |
Holger Hans Peter Freyther | 8b901fa | 2010-03-26 10:41:20 +0100 | [diff] [blame] | 33 | #include <errno.h> |
| 34 | #include <fcntl.h> |
Holger Hans Peter Freyther | 9e16ba5 | 2010-06-15 18:45:06 +0800 | [diff] [blame] | 35 | #include <stdio.h> |
| 36 | #include <string.h> |
| 37 | #include <unistd.h> |
| 38 | |
Holger Hans Peter Freyther | 4a860a0 | 2010-06-15 18:47:29 +0800 | [diff] [blame] | 39 | static void connection_loss(struct bsc_msc_connection *con) |
| 40 | { |
Pablo Neira Ayuso | 04d24cd | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 41 | struct osmo_fd *fd; |
Holger Hans Peter Freyther | 4a860a0 | 2010-06-15 18:47:29 +0800 | [diff] [blame] | 42 | |
| 43 | fd = &con->write_queue.bfd; |
| 44 | |
Jacob Erlbeck | 1a5c246 | 2014-03-31 13:42:11 +0200 | [diff] [blame] | 45 | if (con->pending_msg) { |
| 46 | LOGP(DMSC, LOGL_ERROR, |
| 47 | "MSC(%s) dropping incomplete message.\n", con->name); |
| 48 | msgb_free(con->pending_msg); |
| 49 | con->pending_msg = NULL; |
| 50 | } |
| 51 | |
Holger Hans Peter Freyther | 4a860a0 | 2010-06-15 18:47:29 +0800 | [diff] [blame] | 52 | close(fd->fd); |
| 53 | fd->fd = -1; |
Pablo Neira Ayuso | da08850 | 2011-05-06 12:09:47 +0200 | [diff] [blame] | 54 | fd->cb = osmo_wqueue_bfd_cb; |
Holger Hans Peter Freyther | 4a860a0 | 2010-06-15 18:47:29 +0800 | [diff] [blame] | 55 | fd->when = 0; |
| 56 | |
| 57 | con->is_connected = 0; |
Holger Hans Peter Freyther | 8fd9775 | 2010-07-05 14:14:18 +0800 | [diff] [blame] | 58 | con->first_contact = 0; |
Holger Hans Peter Freyther | 4a860a0 | 2010-06-15 18:47:29 +0800 | [diff] [blame] | 59 | con->connection_loss(con); |
| 60 | } |
| 61 | |
Holger Hans Peter Freyther | 9b6f999 | 2010-05-05 22:48:56 +0800 | [diff] [blame] | 62 | static void msc_con_timeout(void *_con) |
| 63 | { |
| 64 | struct bsc_msc_connection *con = _con; |
| 65 | |
Holger Hans Peter Freyther | 7be0e60 | 2013-09-02 13:07:45 +0200 | [diff] [blame] | 66 | LOGP(DMSC, LOGL_ERROR, |
| 67 | "MSC(%s) Connection timeout.\n", con->name); |
Holger Hans Peter Freyther | 9b6f999 | 2010-05-05 22:48:56 +0800 | [diff] [blame] | 68 | bsc_msc_lost(con); |
| 69 | } |
| 70 | |
Holger Hans Peter Freyther | 8b901fa | 2010-03-26 10:41:20 +0100 | [diff] [blame] | 71 | /* called in the case of a non blocking connect */ |
Pablo Neira Ayuso | 04d24cd | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 72 | static int msc_connection_connect(struct osmo_fd *fd, unsigned int what) |
Holger Hans Peter Freyther | 8b901fa | 2010-03-26 10:41:20 +0100 | [diff] [blame] | 73 | { |
| 74 | int rc; |
| 75 | int val; |
Holger Hans Peter Freyther | 4a860a0 | 2010-06-15 18:47:29 +0800 | [diff] [blame] | 76 | struct bsc_msc_connection *con; |
Pablo Neira Ayuso | da08850 | 2011-05-06 12:09:47 +0200 | [diff] [blame] | 77 | struct osmo_wqueue *queue; |
Holger Hans Peter Freyther | 8b901fa | 2010-03-26 10:41:20 +0100 | [diff] [blame] | 78 | |
| 79 | socklen_t len = sizeof(val); |
| 80 | |
Pablo Neira Ayuso | da08850 | 2011-05-06 12:09:47 +0200 | [diff] [blame] | 81 | queue = container_of(fd, struct osmo_wqueue, bfd); |
Holger Hans Peter Freyther | 4a860a0 | 2010-06-15 18:47:29 +0800 | [diff] [blame] | 82 | con = container_of(queue, struct bsc_msc_connection, write_queue); |
| 83 | |
Holger Hans Peter Freyther | 7be0e60 | 2013-09-02 13:07:45 +0200 | [diff] [blame] | 84 | if ((what & BSC_FD_WRITE) == 0) { |
| 85 | LOGP(DMSC, LOGL_ERROR, |
| 86 | "MSC(%s) Callback but not writable.\n", con->name); |
| 87 | return -1; |
| 88 | } |
| 89 | |
Holger Hans Peter Freyther | 58aba05 | 2010-10-07 04:42:03 +0800 | [diff] [blame] | 90 | /* From here on we will either be connected or reconnect */ |
Pablo Neira Ayuso | 840ccf6 | 2011-05-06 12:11:06 +0200 | [diff] [blame] | 91 | osmo_timer_del(&con->timeout_timer); |
Holger Hans Peter Freyther | 58aba05 | 2010-10-07 04:42:03 +0800 | [diff] [blame] | 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) { |
Holger Hans Peter Freyther | 7be0e60 | 2013-09-02 13:07:45 +0200 | [diff] [blame] | 96 | LOGP(DMSC, LOGL_ERROR, |
| 97 | "getsockopt for the MSC(%s) socket failed.\n", con->name); |
Holger Hans Peter Freyther | 8b901fa | 2010-03-26 10:41:20 +0100 | [diff] [blame] | 98 | goto error; |
| 99 | } |
| 100 | if (val != 0) { |
Holger Hans Peter Freyther | 7be0e60 | 2013-09-02 13:07:45 +0200 | [diff] [blame] | 101 | LOGP(DMSC, LOGL_ERROR, |
| 102 | "Not connected to the MSC(%s): %d\n", |
| 103 | con->name, val); |
Holger Hans Peter Freyther | 8b901fa | 2010-03-26 10:41:20 +0100 | [diff] [blame] | 104 | goto error; |
| 105 | } |
| 106 | |
| 107 | |
| 108 | /* go to full operation */ |
Pablo Neira Ayuso | da08850 | 2011-05-06 12:09:47 +0200 | [diff] [blame] | 109 | fd->cb = osmo_wqueue_bfd_cb; |
Holger Hans Peter Freyther | 1f9d511 | 2010-04-08 11:12:32 +0200 | [diff] [blame] | 110 | fd->when = BSC_FD_READ | BSC_FD_EXCEPT; |
Holger Hans Peter Freyther | 4a860a0 | 2010-06-15 18:47:29 +0800 | [diff] [blame] | 111 | |
| 112 | con->is_connected = 1; |
Holger Hans Peter Freyther | 7be0e60 | 2013-09-02 13:07:45 +0200 | [diff] [blame] | 113 | LOGP(DMSC, LOGL_NOTICE, |
| 114 | "(Re)Connected to the MSC(%s).\n", con->name); |
Holger Hans Peter Freyther | 4a860a0 | 2010-06-15 18:47:29 +0800 | [diff] [blame] | 115 | if (con->connected) |
| 116 | con->connected(con); |
Holger Hans Peter Freyther | 8b901fa | 2010-03-26 10:41:20 +0100 | [diff] [blame] | 117 | return 0; |
| 118 | |
| 119 | error: |
Pablo Neira Ayuso | 04d24cd | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 120 | osmo_fd_unregister(fd); |
Holger Hans Peter Freyther | 4a860a0 | 2010-06-15 18:47:29 +0800 | [diff] [blame] | 121 | connection_loss(con); |
Holger Hans Peter Freyther | 8b901fa | 2010-03-26 10:41:20 +0100 | [diff] [blame] | 122 | return -1; |
| 123 | } |
Pablo Neira Ayuso | 04d24cd | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 124 | static void setnonblocking(struct osmo_fd *fd) |
Holger Hans Peter Freyther | 8b901fa | 2010-03-26 10:41:20 +0100 | [diff] [blame] | 125 | { |
| 126 | int flags; |
| 127 | |
| 128 | flags = fcntl(fd->fd, F_GETFL); |
| 129 | if (flags < 0) { |
| 130 | perror("fcntl get failed"); |
| 131 | close(fd->fd); |
| 132 | fd->fd = -1; |
| 133 | return; |
| 134 | } |
| 135 | |
| 136 | flags |= O_NONBLOCK; |
| 137 | flags = fcntl(fd->fd, F_SETFL, flags); |
| 138 | if (flags < 0) { |
| 139 | perror("fcntl get failed"); |
| 140 | close(fd->fd); |
| 141 | fd->fd = -1; |
| 142 | return; |
| 143 | } |
| 144 | } |
| 145 | |
Holger Hans Peter Freyther | 4a860a0 | 2010-06-15 18:47:29 +0800 | [diff] [blame] | 146 | int bsc_msc_connect(struct bsc_msc_connection *con) |
Holger Hans Peter Freyther | 9e16ba5 | 2010-06-15 18:45:06 +0800 | [diff] [blame] | 147 | { |
Holger Hans Peter Freyther | 47a9f4b | 2011-04-23 23:31:31 +0200 | [diff] [blame] | 148 | struct bsc_msc_dest *dest; |
Pablo Neira Ayuso | 04d24cd | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 149 | struct osmo_fd *fd; |
Holger Hans Peter Freyther | 9e16ba5 | 2010-06-15 18:45:06 +0800 | [diff] [blame] | 150 | struct sockaddr_in sin; |
| 151 | int on = 1, ret; |
| 152 | |
Holger Hans Peter Freyther | 47a9f4b | 2011-04-23 23:31:31 +0200 | [diff] [blame] | 153 | if (llist_empty(con->dests)) { |
Holger Hans Peter Freyther | 7be0e60 | 2013-09-02 13:07:45 +0200 | [diff] [blame] | 154 | LOGP(DMSC, LOGL_ERROR, |
| 155 | "No MSC(%s) connections configured.\n", |
| 156 | con->name); |
Holger Hans Peter Freyther | 47a9f4b | 2011-04-23 23:31:31 +0200 | [diff] [blame] | 157 | connection_loss(con); |
| 158 | return -1; |
| 159 | } |
| 160 | |
Harald Welte | bf216ec | 2016-11-26 14:13:33 +0100 | [diff] [blame] | 161 | /* TODO: Why are we not using the libosmocore soecket |
| 162 | * abstraction, or libosmo-netif? */ |
| 163 | |
Holger Hans Peter Freyther | 47a9f4b | 2011-04-23 23:31:31 +0200 | [diff] [blame] | 164 | /* move to the next connection */ |
| 165 | dest = (struct bsc_msc_dest *) con->dests->next; |
| 166 | llist_del(&dest->list); |
| 167 | llist_add_tail(&dest->list, con->dests); |
| 168 | |
Holger Hans Peter Freyther | 7be0e60 | 2013-09-02 13:07:45 +0200 | [diff] [blame] | 169 | LOGP(DMSC, LOGL_NOTICE, |
| 170 | "Attempting to connect MSC(%s) at %s:%d\n", |
| 171 | con->name, dest->ip, dest->port); |
Holger Hans Peter Freyther | 9e16ba5 | 2010-06-15 18:45:06 +0800 | [diff] [blame] | 172 | |
Holger Hans Peter Freyther | 4a860a0 | 2010-06-15 18:47:29 +0800 | [diff] [blame] | 173 | con->is_connected = 0; |
| 174 | |
Jacob Erlbeck | 1a5c246 | 2014-03-31 13:42:11 +0200 | [diff] [blame] | 175 | msgb_free(con->pending_msg); |
| 176 | con->pending_msg = NULL; |
| 177 | |
Holger Hans Peter Freyther | 4a860a0 | 2010-06-15 18:47:29 +0800 | [diff] [blame] | 178 | fd = &con->write_queue.bfd; |
Holger Hans Peter Freyther | 9e16ba5 | 2010-06-15 18:45:06 +0800 | [diff] [blame] | 179 | fd->fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); |
Holger Hans Peter Freyther | 9e16ba5 | 2010-06-15 18:45:06 +0800 | [diff] [blame] | 180 | fd->priv_nr = 1; |
| 181 | |
| 182 | if (fd->fd < 0) { |
| 183 | perror("Creating TCP socket failed"); |
| 184 | return fd->fd; |
| 185 | } |
| 186 | |
Holger Hans Peter Freyther | 8b901fa | 2010-03-26 10:41:20 +0100 | [diff] [blame] | 187 | /* make it non blocking */ |
| 188 | setnonblocking(fd); |
Holger Hans Peter Freyther | 9e16ba5 | 2010-06-15 18:45:06 +0800 | [diff] [blame] | 189 | |
Holger Hans Peter Freyther | 7ba0c54 | 2010-06-15 18:52:38 +0800 | [diff] [blame] | 190 | /* set the socket priority */ |
| 191 | ret = setsockopt(fd->fd, IPPROTO_IP, IP_TOS, |
Holger Hans Peter Freyther | 47a9f4b | 2011-04-23 23:31:31 +0200 | [diff] [blame] | 192 | &dest->dscp, sizeof(dest->dscp)); |
Holger Hans Peter Freyther | 7ba0c54 | 2010-06-15 18:52:38 +0800 | [diff] [blame] | 193 | if (ret != 0) |
Holger Hans Peter Freyther | 7be0e60 | 2013-09-02 13:07:45 +0200 | [diff] [blame] | 194 | LOGP(DMSC, LOGL_ERROR, |
| 195 | "Failed to set DSCP to %d on MSC(%s). %s\n", |
| 196 | dest->dscp, con->name, strerror(errno)); |
Holger Hans Peter Freyther | 7ba0c54 | 2010-06-15 18:52:38 +0800 | [diff] [blame] | 197 | |
Holger Hans Peter Freyther | 9e16ba5 | 2010-06-15 18:45:06 +0800 | [diff] [blame] | 198 | memset(&sin, 0, sizeof(sin)); |
| 199 | sin.sin_family = AF_INET; |
Holger Hans Peter Freyther | 47a9f4b | 2011-04-23 23:31:31 +0200 | [diff] [blame] | 200 | sin.sin_port = htons(dest->port); |
| 201 | inet_aton(dest->ip, &sin.sin_addr); |
Holger Hans Peter Freyther | 9e16ba5 | 2010-06-15 18:45:06 +0800 | [diff] [blame] | 202 | |
Harald Welte | bf216ec | 2016-11-26 14:13:33 +0100 | [diff] [blame] | 203 | ret = setsockopt(fd->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)); |
| 204 | if (ret != 0) |
| 205 | LOGP(DMSC, LOGL_ERROR, |
| 206 | "Failed to set SO_REUSEADDR socket option\n"); |
Holger Hans Peter Freyther | 9e16ba5 | 2010-06-15 18:45:06 +0800 | [diff] [blame] | 207 | ret = connect(fd->fd, (struct sockaddr *) &sin, sizeof(sin)); |
| 208 | |
Holger Hans Peter Freyther | 8b901fa | 2010-03-26 10:41:20 +0100 | [diff] [blame] | 209 | if (ret == -1 && errno == EINPROGRESS) { |
Holger Hans Peter Freyther | 7be0e60 | 2013-09-02 13:07:45 +0200 | [diff] [blame] | 210 | LOGP(DMSC, LOGL_ERROR, |
| 211 | "MSC(%s) Connection in progress\n", con->name); |
Holger Hans Peter Freyther | 8b901fa | 2010-03-26 10:41:20 +0100 | [diff] [blame] | 212 | fd->when = BSC_FD_WRITE; |
| 213 | fd->cb = msc_connection_connect; |
Pablo Neira Ayuso | 274b384 | 2017-05-08 20:57:52 +0200 | [diff] [blame^] | 214 | osmo_timer_setup(&con->timeout_timer, msc_con_timeout, con); |
Pablo Neira Ayuso | 840ccf6 | 2011-05-06 12:11:06 +0200 | [diff] [blame] | 215 | osmo_timer_schedule(&con->timeout_timer, 20, 0); |
Holger Hans Peter Freyther | 8b901fa | 2010-03-26 10:41:20 +0100 | [diff] [blame] | 216 | } else if (ret < 0) { |
Holger Hans Peter Freyther | 9e16ba5 | 2010-06-15 18:45:06 +0800 | [diff] [blame] | 217 | perror("Connection failed"); |
Holger Hans Peter Freyther | 4a860a0 | 2010-06-15 18:47:29 +0800 | [diff] [blame] | 218 | connection_loss(con); |
Holger Hans Peter Freyther | 9e16ba5 | 2010-06-15 18:45:06 +0800 | [diff] [blame] | 219 | return ret; |
Holger Hans Peter Freyther | 8b901fa | 2010-03-26 10:41:20 +0100 | [diff] [blame] | 220 | } else { |
Holger Hans Peter Freyther | 1f9d511 | 2010-04-08 11:12:32 +0200 | [diff] [blame] | 221 | fd->when = BSC_FD_READ | BSC_FD_EXCEPT; |
Pablo Neira Ayuso | da08850 | 2011-05-06 12:09:47 +0200 | [diff] [blame] | 222 | fd->cb = osmo_wqueue_bfd_cb; |
Holger Hans Peter Freyther | 4a860a0 | 2010-06-15 18:47:29 +0800 | [diff] [blame] | 223 | con->is_connected = 1; |
| 224 | if (con->connected) |
| 225 | con->connected(con); |
Holger Hans Peter Freyther | 9e16ba5 | 2010-06-15 18:45:06 +0800 | [diff] [blame] | 226 | } |
| 227 | |
Pablo Neira Ayuso | 04d24cd | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 228 | ret = osmo_fd_register(fd); |
Holger Hans Peter Freyther | 9e16ba5 | 2010-06-15 18:45:06 +0800 | [diff] [blame] | 229 | if (ret < 0) { |
| 230 | perror("Registering the fd failed"); |
| 231 | close(fd->fd); |
| 232 | return ret; |
| 233 | } |
| 234 | |
| 235 | return ret; |
| 236 | } |
Holger Hans Peter Freyther | 4a860a0 | 2010-06-15 18:47:29 +0800 | [diff] [blame] | 237 | |
Holger Hans Peter Freyther | 47a9f4b | 2011-04-23 23:31:31 +0200 | [diff] [blame] | 238 | struct bsc_msc_connection *bsc_msc_create(void *ctx, struct llist_head *dests) |
Holger Hans Peter Freyther | 4a860a0 | 2010-06-15 18:47:29 +0800 | [diff] [blame] | 239 | { |
| 240 | struct bsc_msc_connection *con; |
| 241 | |
| 242 | con = talloc_zero(NULL, struct bsc_msc_connection); |
| 243 | if (!con) { |
Holger Hans Peter Freyther | 323d2f5 | 2013-09-04 08:45:48 +0200 | [diff] [blame] | 244 | LOGP(DMSC, LOGL_FATAL, "Failed to create the MSC connection.\n"); |
Holger Hans Peter Freyther | 4a860a0 | 2010-06-15 18:47:29 +0800 | [diff] [blame] | 245 | return NULL; |
| 246 | } |
| 247 | |
Holger Hans Peter Freyther | 47a9f4b | 2011-04-23 23:31:31 +0200 | [diff] [blame] | 248 | con->dests = dests; |
| 249 | con->write_queue.bfd.fd = -1; |
Holger Hans Peter Freyther | 7be0e60 | 2013-09-02 13:07:45 +0200 | [diff] [blame] | 250 | con->name = ""; |
Pablo Neira Ayuso | da08850 | 2011-05-06 12:09:47 +0200 | [diff] [blame] | 251 | osmo_wqueue_init(&con->write_queue, 100); |
Holger Hans Peter Freyther | 4a860a0 | 2010-06-15 18:47:29 +0800 | [diff] [blame] | 252 | return con; |
| 253 | } |
| 254 | |
| 255 | void bsc_msc_lost(struct bsc_msc_connection *con) |
| 256 | { |
Pablo Neira Ayuso | da08850 | 2011-05-06 12:09:47 +0200 | [diff] [blame] | 257 | osmo_wqueue_clear(&con->write_queue); |
Pablo Neira Ayuso | 840ccf6 | 2011-05-06 12:11:06 +0200 | [diff] [blame] | 258 | osmo_timer_del(&con->timeout_timer); |
Holger Hans Peter Freyther | bac47a2 | 2013-09-03 14:21:48 +0200 | [diff] [blame] | 259 | osmo_timer_del(&con->reconnect_timer); |
Holger Hans Peter Freyther | 88b3012 | 2010-10-07 06:07:57 +0800 | [diff] [blame] | 260 | |
| 261 | if (con->write_queue.bfd.fd >= 0) |
Pablo Neira Ayuso | 04d24cd | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 262 | osmo_fd_unregister(&con->write_queue.bfd); |
Holger Hans Peter Freyther | 4a860a0 | 2010-06-15 18:47:29 +0800 | [diff] [blame] | 263 | connection_loss(con); |
| 264 | } |
| 265 | |
| 266 | static void reconnect_msc(void *_msc) |
| 267 | { |
| 268 | struct bsc_msc_connection *con = _msc; |
| 269 | |
Holger Hans Peter Freyther | 7be0e60 | 2013-09-02 13:07:45 +0200 | [diff] [blame] | 270 | LOGP(DMSC, LOGL_NOTICE, |
| 271 | "Attempting to reconnect to the MSC(%s).\n", con->name); |
Holger Hans Peter Freyther | 4a860a0 | 2010-06-15 18:47:29 +0800 | [diff] [blame] | 272 | bsc_msc_connect(con); |
| 273 | } |
| 274 | |
| 275 | void bsc_msc_schedule_connect(struct bsc_msc_connection *con) |
| 276 | { |
Holger Hans Peter Freyther | 7be0e60 | 2013-09-02 13:07:45 +0200 | [diff] [blame] | 277 | LOGP(DMSC, LOGL_NOTICE, |
| 278 | "Attempting to reconnect to the MSC(%s)\n", con->name); |
Pablo Neira Ayuso | 274b384 | 2017-05-08 20:57:52 +0200 | [diff] [blame^] | 279 | osmo_timer_setup(&con->reconnect_timer, reconnect_msc, con); |
Pablo Neira Ayuso | 840ccf6 | 2011-05-06 12:11:06 +0200 | [diff] [blame] | 280 | osmo_timer_schedule(&con->reconnect_timer, 5, 0); |
Holger Hans Peter Freyther | afe60c0 | 2010-06-15 18:52:05 +0800 | [diff] [blame] | 281 | } |
| 282 | |
Holger Hans Peter Freyther | 1f93da7 | 2015-06-08 18:02:10 +0200 | [diff] [blame] | 283 | struct msgb *bsc_msc_id_get_resp(int fixed, const char *token, const uint8_t *res, int len) |
Holger Hans Peter Freyther | afe60c0 | 2010-06-15 18:52:05 +0800 | [diff] [blame] | 284 | { |
| 285 | struct msgb *msg; |
| 286 | |
| 287 | if (!token) { |
| 288 | LOGP(DMSC, LOGL_ERROR, "No token specified.\n"); |
| 289 | return NULL; |
| 290 | } |
| 291 | |
| 292 | msg = msgb_alloc_headroom(4096, 128, "id resp"); |
| 293 | if (!msg) { |
| 294 | LOGP(DMSC, LOGL_ERROR, "Failed to create the message.\n"); |
| 295 | return NULL; |
| 296 | } |
| 297 | |
Holger Hans Peter Freyther | b4b23c7 | 2015-06-10 11:51:16 +0200 | [diff] [blame] | 298 | /* |
| 299 | * The situation is bizarre. The encoding doesn't follow the |
| 300 | * TLV structure. It is more like a LV and old versions had |
| 301 | * it wrong but we want new versions to old servers so we |
| 302 | * introduce the quirk here. |
| 303 | */ |
Holger Hans Peter Freyther | afe60c0 | 2010-06-15 18:52:05 +0800 | [diff] [blame] | 304 | msg->l2h = msgb_v_put(msg, IPAC_MSGT_ID_RESP); |
Holger Hans Peter Freyther | b4b23c7 | 2015-06-10 11:51:16 +0200 | [diff] [blame] | 305 | if (fixed) { |
| 306 | msgb_put_u8(msg, 0); |
| 307 | msgb_put_u8(msg, strlen(token) + 2); |
| 308 | msgb_tv_fixed_put(msg, IPAC_IDTAG_UNITNAME, strlen(token) + 1, (uint8_t *) token); |
Holger Hans Peter Freyther | 1f93da7 | 2015-06-08 18:02:10 +0200 | [diff] [blame] | 309 | if (len > 0) { |
| 310 | msgb_put_u8(msg, 0); |
| 311 | msgb_put_u8(msg, len + 1); |
| 312 | msgb_tv_fixed_put(msg, 0x24, len, res); |
| 313 | } |
Holger Hans Peter Freyther | b4b23c7 | 2015-06-10 11:51:16 +0200 | [diff] [blame] | 314 | } else { |
| 315 | msgb_l16tv_put(msg, strlen(token) + 1, |
Holger Hans Peter Freyther | 7eb8a9a | 2011-04-18 17:04:00 +0200 | [diff] [blame] | 316 | IPAC_IDTAG_UNITNAME, (uint8_t *) token); |
Holger Hans Peter Freyther | b4b23c7 | 2015-06-10 11:51:16 +0200 | [diff] [blame] | 317 | } |
| 318 | |
Holger Hans Peter Freyther | afe60c0 | 2010-06-15 18:52:05 +0800 | [diff] [blame] | 319 | return msg; |
Holger Hans Peter Freyther | 8fd9775 | 2010-07-05 14:14:18 +0800 | [diff] [blame] | 320 | } |