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> |
| 25 | |
| 26 | #include <osmocore/write_queue.h> |
Holger Hans Peter Freyther | 4a860a0 | 2010-06-15 18:47:29 +0800 | [diff] [blame] | 27 | #include <osmocore/talloc.h> |
Holger Hans Peter Freyther | 9e16ba5 | 2010-06-15 18:45:06 +0800 | [diff] [blame] | 28 | |
| 29 | #include <arpa/inet.h> |
| 30 | #include <sys/socket.h> |
Holger Hans Peter Freyther | 8b901fa | 2010-03-26 10:41:20 +0100 | [diff] [blame] | 31 | #include <errno.h> |
| 32 | #include <fcntl.h> |
Holger Hans Peter Freyther | 9e16ba5 | 2010-06-15 18:45:06 +0800 | [diff] [blame] | 33 | #include <stdio.h> |
| 34 | #include <string.h> |
| 35 | #include <unistd.h> |
| 36 | |
Holger Hans Peter Freyther | 4a860a0 | 2010-06-15 18:47:29 +0800 | [diff] [blame] | 37 | static 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 Freyther | 8b901fa | 2010-03-26 10:41:20 +0100 | [diff] [blame] | 52 | /* called in the case of a non blocking connect */ |
| 53 | static int msc_connection_connect(struct bsc_fd *fd, unsigned int what) |
| 54 | { |
| 55 | int rc; |
| 56 | int val; |
Holger Hans Peter Freyther | 4a860a0 | 2010-06-15 18:47:29 +0800 | [diff] [blame] | 57 | struct bsc_msc_connection *con; |
Holger Hans Peter Freyther | 8b901fa | 2010-03-26 10:41:20 +0100 | [diff] [blame] | 58 | 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 Freyther | 4a860a0 | 2010-06-15 18:47:29 +0800 | [diff] [blame] | 67 | queue = container_of(fd, struct write_queue, bfd); |
| 68 | con = container_of(queue, struct bsc_msc_connection, write_queue); |
| 69 | |
Holger Hans Peter Freyther | 8b901fa | 2010-03-26 10:41:20 +0100 | [diff] [blame] | 70 | /* 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) { |
| 77 | LOGP(DMSC, LOGL_ERROR, "Not connected to the MSC.\n"); |
| 78 | goto error; |
| 79 | } |
| 80 | |
| 81 | |
| 82 | /* go to full operation */ |
Holger Hans Peter Freyther | 8b901fa | 2010-03-26 10:41:20 +0100 | [diff] [blame] | 83 | fd->cb = write_queue_bfd_cb; |
| 84 | fd->when = BSC_FD_READ; |
Holger Hans Peter Freyther | 4a860a0 | 2010-06-15 18:47:29 +0800 | [diff] [blame] | 85 | |
| 86 | con->is_connected = 1; |
| 87 | if (con->connected) |
| 88 | con->connected(con); |
Holger Hans Peter Freyther | 8b901fa | 2010-03-26 10:41:20 +0100 | [diff] [blame] | 89 | return 0; |
| 90 | |
| 91 | error: |
| 92 | bsc_unregister_fd(fd); |
Holger Hans Peter Freyther | 4a860a0 | 2010-06-15 18:47:29 +0800 | [diff] [blame] | 93 | connection_loss(con); |
Holger Hans Peter Freyther | 8b901fa | 2010-03-26 10:41:20 +0100 | [diff] [blame] | 94 | return -1; |
| 95 | } |
| 96 | static void setnonblocking(struct bsc_fd *fd) |
| 97 | { |
| 98 | int flags; |
| 99 | |
| 100 | flags = fcntl(fd->fd, F_GETFL); |
| 101 | if (flags < 0) { |
| 102 | perror("fcntl get failed"); |
| 103 | close(fd->fd); |
| 104 | fd->fd = -1; |
| 105 | return; |
| 106 | } |
| 107 | |
| 108 | flags |= O_NONBLOCK; |
| 109 | flags = fcntl(fd->fd, F_SETFL, flags); |
| 110 | if (flags < 0) { |
| 111 | perror("fcntl get failed"); |
| 112 | close(fd->fd); |
| 113 | fd->fd = -1; |
| 114 | return; |
| 115 | } |
| 116 | } |
| 117 | |
Holger Hans Peter Freyther | 4a860a0 | 2010-06-15 18:47:29 +0800 | [diff] [blame] | 118 | int bsc_msc_connect(struct bsc_msc_connection *con) |
Holger Hans Peter Freyther | 9e16ba5 | 2010-06-15 18:45:06 +0800 | [diff] [blame] | 119 | { |
Holger Hans Peter Freyther | 4a860a0 | 2010-06-15 18:47:29 +0800 | [diff] [blame] | 120 | struct bsc_fd *fd; |
Holger Hans Peter Freyther | 9e16ba5 | 2010-06-15 18:45:06 +0800 | [diff] [blame] | 121 | struct sockaddr_in sin; |
| 122 | int on = 1, ret; |
| 123 | |
Holger Hans Peter Freyther | 4a860a0 | 2010-06-15 18:47:29 +0800 | [diff] [blame] | 124 | 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] | 125 | |
Holger Hans Peter Freyther | 4a860a0 | 2010-06-15 18:47:29 +0800 | [diff] [blame] | 126 | con->is_connected = 0; |
| 127 | |
| 128 | fd = &con->write_queue.bfd; |
Holger Hans Peter Freyther | 9e16ba5 | 2010-06-15 18:45:06 +0800 | [diff] [blame] | 129 | fd->fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); |
Holger Hans Peter Freyther | 9e16ba5 | 2010-06-15 18:45:06 +0800 | [diff] [blame] | 130 | fd->data = NULL; |
| 131 | fd->priv_nr = 1; |
| 132 | |
| 133 | if (fd->fd < 0) { |
| 134 | perror("Creating TCP socket failed"); |
| 135 | return fd->fd; |
| 136 | } |
| 137 | |
Holger Hans Peter Freyther | 8b901fa | 2010-03-26 10:41:20 +0100 | [diff] [blame] | 138 | /* make it non blocking */ |
| 139 | setnonblocking(fd); |
Holger Hans Peter Freyther | 9e16ba5 | 2010-06-15 18:45:06 +0800 | [diff] [blame] | 140 | |
| 141 | memset(&sin, 0, sizeof(sin)); |
| 142 | sin.sin_family = AF_INET; |
Holger Hans Peter Freyther | 4a860a0 | 2010-06-15 18:47:29 +0800 | [diff] [blame] | 143 | sin.sin_port = htons(con->port); |
| 144 | inet_aton(con->ip, &sin.sin_addr); |
Holger Hans Peter Freyther | 9e16ba5 | 2010-06-15 18:45:06 +0800 | [diff] [blame] | 145 | |
| 146 | setsockopt(fd->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)); |
| 147 | ret = connect(fd->fd, (struct sockaddr *) &sin, sizeof(sin)); |
| 148 | |
Holger Hans Peter Freyther | 8b901fa | 2010-03-26 10:41:20 +0100 | [diff] [blame] | 149 | if (ret == -1 && errno == EINPROGRESS) { |
| 150 | LOGP(DMSC, LOGL_ERROR, "MSC Connection in progress\n"); |
| 151 | fd->when = BSC_FD_WRITE; |
| 152 | fd->cb = msc_connection_connect; |
| 153 | } else if (ret < 0) { |
Holger Hans Peter Freyther | 9e16ba5 | 2010-06-15 18:45:06 +0800 | [diff] [blame] | 154 | perror("Connection failed"); |
Holger Hans Peter Freyther | 4a860a0 | 2010-06-15 18:47:29 +0800 | [diff] [blame] | 155 | connection_loss(con); |
Holger Hans Peter Freyther | 9e16ba5 | 2010-06-15 18:45:06 +0800 | [diff] [blame] | 156 | return ret; |
Holger Hans Peter Freyther | 8b901fa | 2010-03-26 10:41:20 +0100 | [diff] [blame] | 157 | } else { |
| 158 | fd->when = BSC_FD_READ; |
| 159 | fd->cb = write_queue_bfd_cb; |
Holger Hans Peter Freyther | 4a860a0 | 2010-06-15 18:47:29 +0800 | [diff] [blame] | 160 | con->is_connected = 1; |
| 161 | if (con->connected) |
| 162 | con->connected(con); |
Holger Hans Peter Freyther | 9e16ba5 | 2010-06-15 18:45:06 +0800 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | ret = bsc_register_fd(fd); |
| 166 | if (ret < 0) { |
| 167 | perror("Registering the fd failed"); |
| 168 | close(fd->fd); |
| 169 | return ret; |
| 170 | } |
| 171 | |
| 172 | return ret; |
| 173 | } |
Holger Hans Peter Freyther | 4a860a0 | 2010-06-15 18:47:29 +0800 | [diff] [blame] | 174 | |
| 175 | struct bsc_msc_connection *bsc_msc_create(const char *ip, int port) |
| 176 | { |
| 177 | struct bsc_msc_connection *con; |
| 178 | |
| 179 | con = talloc_zero(NULL, struct bsc_msc_connection); |
| 180 | if (!con) { |
| 181 | LOGP(DMSC, LOGL_FATAL, "Failed to create the MSC connection.\n"); |
| 182 | return NULL; |
| 183 | } |
| 184 | |
| 185 | con->ip = ip; |
| 186 | con->port = port; |
| 187 | write_queue_init(&con->write_queue, 100); |
| 188 | return con; |
| 189 | } |
| 190 | |
| 191 | void bsc_msc_lost(struct bsc_msc_connection *con) |
| 192 | { |
| 193 | bsc_unregister_fd(&con->write_queue.bfd); |
| 194 | connection_loss(con); |
| 195 | } |
| 196 | |
| 197 | static void reconnect_msc(void *_msc) |
| 198 | { |
| 199 | struct bsc_msc_connection *con = _msc; |
| 200 | |
| 201 | LOGP(DMSC, LOGL_NOTICE, "Attempting to reconnect to the MSC.\n"); |
| 202 | bsc_msc_connect(con); |
| 203 | } |
| 204 | |
| 205 | void bsc_msc_schedule_connect(struct bsc_msc_connection *con) |
| 206 | { |
| 207 | LOGP(DMSC, LOGL_NOTICE, "Attempting to reconnect to the MSC.\n"); |
| 208 | con->reconnect_timer.cb = reconnect_msc; |
| 209 | con->reconnect_timer.data = con; |
| 210 | bsc_schedule_timer(&con->reconnect_timer, 5, 0); |
| 211 | } |