Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1 | /* Message connection list handling */ |
| 2 | |
| 3 | /* |
| 4 | * (C) 2017 by sysmocom s.f.m.c. GmbH <info@sysmocom.de> |
| 5 | * All Rights Reserved |
| 6 | * |
| 7 | * Author: Philipp Maier |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU Affero General Public License as published by |
| 11 | * the Free Software Foundation; either version 3 of the License, or |
| 12 | * (at your option) any later version. |
| 13 | * |
| 14 | * This program is distributed in the hope that it will be useful, |
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | * GNU Affero General Public License for more details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU Affero General Public License |
| 20 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 21 | * |
| 22 | */ |
| 23 | |
| 24 | #include <osmocom/mgcp/mgcp_conn.h> |
| 25 | #include <osmocom/mgcp/mgcp_internal.h> |
Neels Hofmeyr | 6779354 | 2017-09-08 04:25:16 +0200 | [diff] [blame^] | 26 | #include <osmocom/mgcp/mgcp_common.h> |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 27 | |
| 28 | /* Reset codec state and free memory */ |
| 29 | static void mgcp_rtp_codec_reset(struct mgcp_rtp_codec *codec) |
| 30 | { |
| 31 | codec->payload_type = -1; |
| 32 | codec->subtype_name = NULL; |
| 33 | codec->audio_name = NULL; |
| 34 | codec->frame_duration_num = DEFAULT_RTP_AUDIO_FRAME_DUR_NUM; |
| 35 | codec->frame_duration_den = DEFAULT_RTP_AUDIO_FRAME_DUR_DEN; |
| 36 | codec->rate = DEFAULT_RTP_AUDIO_DEFAULT_RATE; |
| 37 | codec->channels = DEFAULT_RTP_AUDIO_DEFAULT_CHANNELS; |
| 38 | |
| 39 | /* see also mgcp_sdp.c, mgcp_set_audio_info() */ |
| 40 | talloc_free(codec->subtype_name); |
| 41 | talloc_free(codec->audio_name); |
| 42 | } |
| 43 | |
| 44 | /* Reset states, free memory, set defaults and reset codec state */ |
| 45 | static void mgcp_rtp_conn_reset(struct mgcp_conn_rtp *conn) |
| 46 | { |
| 47 | struct mgcp_rtp_end *end = &conn->end; |
| 48 | |
| 49 | conn->type = MGCP_RTP_DEFAULT; |
| 50 | conn->osmux.allocated_cid = -1; |
| 51 | |
| 52 | end->rtp.fd = -1; |
| 53 | end->rtcp.fd = -1; |
| 54 | end->local_port = 0; |
| 55 | end->packets_rx = 0; |
| 56 | end->octets_rx = 0; |
| 57 | end->packets_tx = 0; |
| 58 | end->octets_tx = 0; |
| 59 | end->dropped_packets = 0; |
| 60 | end->rtp_port = end->rtcp_port = 0; |
| 61 | talloc_free(end->fmtp_extra); |
| 62 | end->fmtp_extra = NULL; |
| 63 | |
| 64 | /* Set default values */ |
| 65 | end->frames_per_packet = 0; /* unknown */ |
| 66 | end->packet_duration_ms = DEFAULT_RTP_AUDIO_PACKET_DURATION_MS; |
| 67 | end->output_enabled = 0; |
| 68 | |
| 69 | mgcp_rtp_codec_reset(&end->codec); |
| 70 | mgcp_rtp_codec_reset(&end->alt_codec); |
| 71 | } |
| 72 | |
| 73 | /*! allocate a new connection list entry. |
| 74 | * \param[in] ctx talloc context |
| 75 | * \param[in] endp associated endpoint |
| 76 | * \param[in] id identification number of the connection |
| 77 | * \param[in] type connection type (e.g. MGCP_CONN_TYPE_RTP) |
| 78 | * \returns pointer to allocated connection, NULL on error */ |
| 79 | struct mgcp_conn *mgcp_conn_alloc(void *ctx, struct mgcp_endpoint *endp, |
| 80 | uint32_t id, enum mgcp_conn_type type, |
| 81 | char *name) |
| 82 | { |
| 83 | struct mgcp_conn *conn; |
| 84 | OSMO_ASSERT(endp); |
| 85 | OSMO_ASSERT(endp->conns.next != NULL && endp->conns.prev != NULL); |
| 86 | OSMO_ASSERT(strlen(name) < sizeof(conn->name)); |
| 87 | |
| 88 | /* Do not allow more then two connections */ |
| 89 | if (llist_count(&endp->conns) >= endp->type->max_conns) |
| 90 | return NULL; |
| 91 | |
| 92 | /* Prevent duplicate connection IDs */ |
| 93 | if (mgcp_conn_get(endp, id)) |
| 94 | return NULL; |
| 95 | |
| 96 | /* Create new connection and add it to the list */ |
| 97 | conn = talloc_zero(ctx, struct mgcp_conn); |
| 98 | if (!conn) |
| 99 | return NULL; |
| 100 | conn->endp = endp; |
| 101 | conn->type = type; |
| 102 | conn->mode = MGCP_CONN_NONE; |
| 103 | conn->mode_orig = MGCP_CONN_NONE; |
| 104 | conn->id = id; |
| 105 | conn->u.rtp.conn = conn; |
| 106 | strcpy(conn->name, name); |
| 107 | |
| 108 | switch (type) { |
| 109 | case MGCP_CONN_TYPE_RTP: |
| 110 | mgcp_rtp_conn_reset(&conn->u.rtp); |
| 111 | break; |
| 112 | default: |
| 113 | /* NOTE: This should never be called with an |
| 114 | * invalid type, its up to the programmer |
| 115 | * to ensure propery types */ |
| 116 | OSMO_ASSERT(false); |
| 117 | } |
| 118 | |
| 119 | llist_add(&conn->entry, &endp->conns); |
| 120 | |
| 121 | return conn; |
| 122 | } |
| 123 | |
| 124 | /*! find a connection by its ID. |
| 125 | * \param[in] endp associated endpoint |
| 126 | * \param[in] id identification number of the connection |
| 127 | * \returns pointer to allocated connection, NULL if not found */ |
| 128 | struct mgcp_conn *mgcp_conn_get(struct mgcp_endpoint *endp, uint32_t id) |
| 129 | { |
| 130 | OSMO_ASSERT(endp); |
| 131 | OSMO_ASSERT(endp->conns.next != NULL && endp->conns.prev != NULL); |
| 132 | |
| 133 | struct mgcp_conn *conn; |
| 134 | |
| 135 | llist_for_each_entry(conn, &endp->conns, entry) { |
| 136 | if (conn->id == id) |
| 137 | return conn; |
| 138 | } |
| 139 | |
| 140 | return NULL; |
| 141 | } |
| 142 | |
| 143 | /*! find an RTP connection by its ID. |
| 144 | * \param[in] endp associated endpoint |
| 145 | * \param[in] id identification number of the connection |
| 146 | * \returns pointer to allocated connection, NULL if not found */ |
| 147 | struct mgcp_conn_rtp *mgcp_conn_get_rtp(struct mgcp_endpoint *endp, uint32_t id) |
| 148 | { |
| 149 | OSMO_ASSERT(endp); |
| 150 | OSMO_ASSERT(endp->conns.next != NULL && endp->conns.prev != NULL); |
| 151 | |
| 152 | struct mgcp_conn *conn; |
| 153 | |
| 154 | conn = mgcp_conn_get(endp, id); |
| 155 | if (!conn) |
| 156 | return NULL; |
| 157 | |
| 158 | if (conn->type == MGCP_CONN_TYPE_RTP) |
| 159 | return &conn->u.rtp; |
| 160 | |
| 161 | return NULL; |
| 162 | } |
| 163 | |
| 164 | /*! free a connection by its ID. |
| 165 | * \param[in] endp associated endpoint |
| 166 | * \param[in] id identification number of the connection */ |
| 167 | void mgcp_conn_free(struct mgcp_endpoint *endp, uint32_t id) |
| 168 | { |
| 169 | OSMO_ASSERT(endp); |
| 170 | OSMO_ASSERT(endp->conns.next != NULL && endp->conns.prev != NULL); |
| 171 | |
| 172 | struct mgcp_conn *conn; |
| 173 | |
| 174 | conn = mgcp_conn_get(endp, id); |
| 175 | if (!conn) |
| 176 | return; |
| 177 | |
| 178 | switch (conn->type) { |
| 179 | case MGCP_CONN_TYPE_RTP: |
| 180 | osmux_disable_conn(&conn->u.rtp); |
| 181 | osmux_release_cid(&conn->u.rtp); |
| 182 | mgcp_free_rtp_port(&conn->u.rtp.end); |
| 183 | break; |
| 184 | default: |
| 185 | /* NOTE: This should never be called with an |
| 186 | * invalid type, its up to the programmer |
| 187 | * to ensure propery types */ |
| 188 | OSMO_ASSERT(false); |
| 189 | } |
| 190 | |
| 191 | llist_del(&conn->entry); |
| 192 | talloc_free(conn); |
| 193 | } |
| 194 | |
| 195 | /*! free oldest connection in the list. |
| 196 | * \param[in] endp associated endpoint */ |
| 197 | void mgcp_conn_free_oldest(struct mgcp_endpoint *endp) |
| 198 | { |
| 199 | OSMO_ASSERT(endp); |
| 200 | OSMO_ASSERT(endp->conns.next != NULL && endp->conns.prev != NULL); |
| 201 | |
| 202 | struct mgcp_conn *conn; |
| 203 | |
| 204 | if (llist_empty(&endp->conns)) |
| 205 | return; |
| 206 | |
| 207 | conn = llist_last_entry(&endp->conns, struct mgcp_conn, entry); |
| 208 | if (!conn) |
| 209 | return; |
| 210 | |
| 211 | mgcp_conn_free(endp, conn->id); |
| 212 | } |
| 213 | |
| 214 | /*! free all connections at once. |
| 215 | * \param[in] endp associated endpoint */ |
| 216 | void mgcp_conn_free_all(struct mgcp_endpoint *endp) |
| 217 | { |
| 218 | OSMO_ASSERT(endp); |
| 219 | OSMO_ASSERT(endp->conns.next != NULL && endp->conns.prev != NULL); |
| 220 | |
| 221 | struct mgcp_conn *conn; |
| 222 | struct mgcp_conn *conn_tmp; |
| 223 | |
| 224 | /* Drop all items in the list */ |
| 225 | llist_for_each_entry_safe(conn, conn_tmp, &endp->conns, entry) { |
| 226 | mgcp_conn_free(endp, conn->id); |
| 227 | } |
| 228 | |
| 229 | return; |
| 230 | } |
| 231 | |
| 232 | /*! dump basic connection information to human readble string. |
| 233 | * \param[in] conn to dump |
| 234 | * \returns human readble string */ |
| 235 | char *mgcp_conn_dump(struct mgcp_conn *conn) |
| 236 | { |
| 237 | static char str[256]; |
| 238 | |
| 239 | if (!conn) { |
| 240 | snprintf(str, sizeof(str), "(null connection)"); |
| 241 | return str; |
| 242 | } |
| 243 | |
| 244 | switch (conn->type) { |
| 245 | case MGCP_CONN_TYPE_RTP: |
| 246 | /* Dump RTP connection */ |
| 247 | snprintf(str, sizeof(str), "(%s/rtp, id:%u, ip:%s, " |
| 248 | "rtp:%u rtcp:%u)", |
| 249 | conn->name, |
| 250 | conn->id, |
| 251 | inet_ntoa(conn->u.rtp.end.addr), |
| 252 | ntohs(conn->u.rtp.end.rtp_port), |
| 253 | ntohs(conn->u.rtp.end.rtcp_port)); |
| 254 | break; |
| 255 | |
| 256 | default: |
| 257 | /* Should not happen, we should be able to dump |
| 258 | * every possible connection type. */ |
| 259 | snprintf(str, sizeof(str), "(unknown connection type)"); |
| 260 | break; |
| 261 | } |
| 262 | |
| 263 | return str; |
| 264 | } |
| 265 | |
| 266 | /*! find destination connection on a specific endpoint. |
| 267 | * \param[in] conn to search a destination for |
| 268 | * \returns destination connection, NULL on failure */ |
| 269 | struct mgcp_conn *mgcp_find_dst_conn(struct mgcp_conn *conn) |
| 270 | { |
| 271 | struct mgcp_endpoint *endp; |
| 272 | struct mgcp_conn *partner_conn; |
| 273 | endp = conn->endp; |
| 274 | |
| 275 | /*! NOTE: This simply works by grabbing the first connection that is |
| 276 | * not the supplied connection, which is suitable for endpoints that |
| 277 | * do not serve more than two connections. */ |
| 278 | |
| 279 | llist_for_each_entry(partner_conn, &endp->conns, entry) { |
| 280 | if (conn != partner_conn) { |
| 281 | return partner_conn; |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | return NULL; |
| 286 | } |