Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 1 | /* |
| 2 | * (C) 2016 by sysmocom s.f.m.c. GmbH <info@sysmocom.de> |
| 3 | * All Rights Reserved |
| 4 | * |
| 5 | * Author: Neels Hofmeyr |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 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 |
| 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 Affero General Public License for more details. |
| 16 | * |
| 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/>. |
| 19 | */ |
| 20 | |
| 21 | #include <string.h> |
| 22 | |
| 23 | #include <osmocom/core/msgb.h> |
| 24 | #include <osmocom/core/application.h> |
Neels Hofmeyr | 3a8e723 | 2017-09-04 01:02:56 +0200 | [diff] [blame] | 25 | #include <osmocom/mgcp_client/mgcp_client.h> |
| 26 | #include <osmocom/mgcp_client/mgcp_client_internal.h> |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 27 | |
| 28 | void *ctx; |
| 29 | |
| 30 | #define buf_len 4096 |
| 31 | |
| 32 | #if 0 |
| 33 | static struct msgb *from_hex(const char *hex) |
| 34 | { |
| 35 | struct msgb *msg = msgb_alloc(buf_len, "mgcpgw_test_from_hex"); |
| 36 | unsigned int l = osmo_hexparse(hex, msg->data, buf_len); |
| 37 | msg->l2h = msgb_put(msg, l); |
| 38 | return msg; |
| 39 | } |
| 40 | |
| 41 | static struct msgb *mgcp_from_str(const char *head, const char *params) |
| 42 | { |
| 43 | struct msgb *msg = msgb_alloc(buf_len, "mgcp_from_str"); |
| 44 | unsigned int l; |
| 45 | char *data; |
| 46 | l = strlen(head); |
| 47 | msg->l2h = msgb_put(msg, l); |
| 48 | data = (char*)msgb_l2(msg); |
| 49 | strncpy(data, head, l); |
| 50 | |
| 51 | data = (char*)msgb_put(msg, 1); |
| 52 | *data = '\n'; |
| 53 | |
| 54 | l = strlen(params); |
| 55 | data = (char*)msgb_put(msg, l); |
| 56 | strncpy(data, params, l); |
| 57 | |
| 58 | return msg; |
| 59 | } |
| 60 | #endif |
| 61 | |
| 62 | static struct msgb *from_str(const char *str) |
| 63 | { |
| 64 | struct msgb *msg = msgb_alloc(buf_len, "from_str"); |
| 65 | unsigned int l = strlen(str); |
| 66 | char *data; |
| 67 | msg->l2h = msgb_put(msg, l); |
| 68 | data = (char*)msgb_l2(msg); |
| 69 | strncpy(data, str, l); |
| 70 | return msg; |
| 71 | } |
| 72 | |
Neels Hofmeyr | 3a8e723 | 2017-09-04 01:02:56 +0200 | [diff] [blame] | 73 | static struct mgcp_client_conf conf; |
| 74 | struct mgcp_client *mgcp = NULL; |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 75 | |
| 76 | static void reply_to(mgcp_trans_id_t trans_id, int code, const char *comment, |
| 77 | int conn_id, const char *params) |
| 78 | { |
| 79 | static char compose[4096 - 128]; |
| 80 | int len; |
| 81 | |
| 82 | len = snprintf(compose, sizeof(compose), |
| 83 | "%d %u %s\r\nI: %d\n\n%s", |
| 84 | code, trans_id, comment, conn_id, params); |
| 85 | OSMO_ASSERT(len < sizeof(compose)); |
| 86 | OSMO_ASSERT(len > 0); |
| 87 | |
| 88 | printf("composed response:\n-----\n%s\n-----\n", |
| 89 | compose); |
Neels Hofmeyr | 3a8e723 | 2017-09-04 01:02:56 +0200 | [diff] [blame] | 90 | mgcp_client_rx(mgcp, from_str(compose)); |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | void test_response_cb(struct mgcp_response *response, void *priv) |
| 94 | { |
| 95 | OSMO_ASSERT(priv == mgcp); |
| 96 | mgcp_response_parse_params(response); |
| 97 | |
| 98 | printf("response cb received:\n" |
| 99 | " head.response_code = %d\n" |
| 100 | " head.trans_id = %u\n" |
| 101 | " head.comment = %s\n" |
Philipp Maier | 06da85e | 2017-10-05 18:49:24 +0200 | [diff] [blame] | 102 | " audio_port = %u\n" |
| 103 | " audio_ip = %s\n", |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 104 | response->head.response_code, |
| 105 | response->head.trans_id, |
| 106 | response->head.comment, |
Philipp Maier | 06da85e | 2017-10-05 18:49:24 +0200 | [diff] [blame] | 107 | response->audio_port, |
| 108 | response->audio_ip |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 109 | ); |
| 110 | } |
| 111 | |
| 112 | mgcp_trans_id_t dummy_mgcp_send(struct msgb *msg) |
| 113 | { |
| 114 | mgcp_trans_id_t trans_id; |
| 115 | trans_id = msg->cb[MSGB_CB_MGCP_TRANS_ID]; |
| 116 | char *end; |
| 117 | |
Neels Hofmeyr | 3a8e723 | 2017-09-04 01:02:56 +0200 | [diff] [blame] | 118 | OSMO_ASSERT(mgcp_client_pending_add(mgcp, trans_id, test_response_cb, mgcp)); |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 119 | |
| 120 | end = (char*)msgb_put(msg, 1); |
| 121 | *end = '\0'; |
| 122 | printf("composed:\n-----\n%s\n-----\n", |
| 123 | (char*)msgb_l2(msg)); |
| 124 | |
| 125 | talloc_free(msg); |
| 126 | return trans_id; |
| 127 | } |
| 128 | |
| 129 | void test_crcx(void) |
| 130 | { |
| 131 | struct msgb *msg; |
| 132 | mgcp_trans_id_t trans_id; |
| 133 | |
| 134 | printf("\n===== %s =====\n", __func__); |
| 135 | |
| 136 | if (mgcp) |
| 137 | talloc_free(mgcp); |
Neels Hofmeyr | 3a8e723 | 2017-09-04 01:02:56 +0200 | [diff] [blame] | 138 | mgcp = mgcp_client_init(ctx, &conf); |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 139 | |
| 140 | msg = mgcp_msg_crcx(mgcp, 23, 42, MGCP_CONN_LOOPBACK); |
| 141 | trans_id = dummy_mgcp_send(msg); |
| 142 | |
| 143 | reply_to(trans_id, 200, "OK", 1, |
| 144 | "v=0\r\n" |
| 145 | "o=- 1 23 IN IP4 10.9.1.120\r\n" |
| 146 | "s=-\r\n" |
| 147 | "c=IN IP4 10.9.1.120\r\n" |
| 148 | "t=0 0\r\n" |
| 149 | "m=audio 16002 RTP/AVP 98\r\n" |
| 150 | "a=rtpmap:98 AMR/8000\r\n" |
| 151 | "a=ptime:20\r\n"); |
| 152 | } |
| 153 | |
Philipp Maier | 1dc6be6 | 2017-10-05 18:25:37 +0200 | [diff] [blame] | 154 | void test_mgcp_msg(void) |
| 155 | { |
| 156 | struct msgb *msg; |
| 157 | char audio_ip_overflow[5000]; |
| 158 | |
| 159 | /* A message struct prefilled with some arbitary values */ |
| 160 | struct mgcp_msg mgcp_msg = { |
| 161 | .audio_ip = "192.168.100.23", |
| 162 | .endpoint = "23@mgw", |
| 163 | .audio_port = 1234, |
| 164 | .call_id = 47, |
Philipp Maier | 01d24a3 | 2017-11-21 17:26:09 +0100 | [diff] [blame^] | 165 | .conn_id = "11", |
Philipp Maier | 1dc6be6 | 2017-10-05 18:25:37 +0200 | [diff] [blame] | 166 | .conn_mode = MGCP_CONN_RECV_SEND |
| 167 | }; |
| 168 | |
| 169 | if (mgcp) |
| 170 | talloc_free(mgcp); |
| 171 | mgcp = mgcp_client_init(ctx, &conf); |
| 172 | |
| 173 | printf("\n"); |
| 174 | |
| 175 | printf("Generated CRCX message:\n"); |
| 176 | mgcp_msg.verb = MGCP_VERB_CRCX; |
| 177 | mgcp_msg.presence = |
| 178 | (MGCP_MSG_PRESENCE_ENDPOINT | MGCP_MSG_PRESENCE_CALL_ID | |
| 179 | MGCP_MSG_PRESENCE_CONN_ID | MGCP_MSG_PRESENCE_CONN_MODE); |
| 180 | msg = mgcp_msg_gen(mgcp, &mgcp_msg); |
| 181 | printf("%s\n", (char *)msg->data); |
| 182 | |
| 183 | printf("Generated MDCX message:\n"); |
| 184 | mgcp_msg.verb = MGCP_VERB_MDCX; |
| 185 | mgcp_msg.presence = |
| 186 | (MGCP_MSG_PRESENCE_ENDPOINT | MGCP_MSG_PRESENCE_CALL_ID | |
| 187 | MGCP_MSG_PRESENCE_CONN_ID | MGCP_MSG_PRESENCE_CONN_MODE | |
| 188 | MGCP_MSG_PRESENCE_AUDIO_IP | MGCP_MSG_PRESENCE_AUDIO_PORT); |
| 189 | msg = mgcp_msg_gen(mgcp, &mgcp_msg); |
| 190 | printf("%s\n", (char *)msg->data); |
| 191 | |
| 192 | printf("Generated DLCX message:\n"); |
| 193 | mgcp_msg.verb = MGCP_VERB_DLCX; |
| 194 | mgcp_msg.presence = |
| 195 | (MGCP_MSG_PRESENCE_ENDPOINT | MGCP_MSG_PRESENCE_CALL_ID | |
| 196 | MGCP_MSG_PRESENCE_CONN_ID); |
| 197 | msg = mgcp_msg_gen(mgcp, &mgcp_msg); |
| 198 | printf("%s\n", (char *)msg->data); |
| 199 | |
| 200 | printf("Generated AUEP message:\n"); |
| 201 | mgcp_msg.verb = MGCP_VERB_AUEP; |
| 202 | mgcp_msg.presence = (MGCP_MSG_PRESENCE_ENDPOINT); |
| 203 | msg = mgcp_msg_gen(mgcp, &mgcp_msg); |
| 204 | printf("%s\n", msg->data); |
| 205 | |
| 206 | printf("Generated RSIP message:\n"); |
| 207 | mgcp_msg.verb = MGCP_VERB_RSIP; |
| 208 | mgcp_msg.presence = (MGCP_MSG_PRESENCE_ENDPOINT); |
| 209 | msg = mgcp_msg_gen(mgcp, &mgcp_msg); |
| 210 | printf("%s\n", (char *)msg->data); |
| 211 | |
| 212 | printf("Overfolow test:\n"); |
| 213 | mgcp_msg.verb = MGCP_VERB_MDCX; |
| 214 | mgcp_msg.presence = |
| 215 | (MGCP_MSG_PRESENCE_ENDPOINT | MGCP_MSG_PRESENCE_CALL_ID | |
| 216 | MGCP_MSG_PRESENCE_CONN_ID | MGCP_MSG_PRESENCE_CONN_MODE | |
| 217 | MGCP_MSG_PRESENCE_AUDIO_IP | MGCP_MSG_PRESENCE_AUDIO_PORT); |
| 218 | memset(audio_ip_overflow, 'X', sizeof(audio_ip_overflow)); |
| 219 | audio_ip_overflow[sizeof(audio_ip_overflow) - 1] = '\0'; |
| 220 | mgcp_msg.audio_ip = audio_ip_overflow; |
| 221 | msg = mgcp_msg_gen(mgcp, &mgcp_msg); |
| 222 | OSMO_ASSERT(msg == NULL); |
| 223 | |
| 224 | printf("\n"); |
| 225 | msgb_free(msg); |
| 226 | } |
| 227 | |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 228 | static const struct log_info_cat log_categories[] = { |
| 229 | }; |
| 230 | |
| 231 | const struct log_info log_info = { |
| 232 | .cat = log_categories, |
| 233 | .num_cat = ARRAY_SIZE(log_categories), |
| 234 | }; |
| 235 | |
| 236 | |
| 237 | int main(int argc, char **argv) |
| 238 | { |
Neels Hofmeyr | 3a8e723 | 2017-09-04 01:02:56 +0200 | [diff] [blame] | 239 | ctx = talloc_named_const(NULL, 1, "mgcp_client_test"); |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 240 | msgb_talloc_ctx_init(ctx, 0); |
| 241 | osmo_init_logging(&log_info); |
Philipp Maier | 8348abb | 2017-10-25 12:14:13 +0200 | [diff] [blame] | 242 | log_set_print_filename(osmo_stderr_target, 0); |
| 243 | log_set_print_timestamp(osmo_stderr_target, 0); |
| 244 | log_set_use_color(osmo_stderr_target, 0); |
| 245 | log_set_print_category(osmo_stderr_target, 1); |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 246 | |
Neels Hofmeyr | 3a8e723 | 2017-09-04 01:02:56 +0200 | [diff] [blame] | 247 | mgcp_client_conf_init(&conf); |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 248 | |
| 249 | test_crcx(); |
Philipp Maier | 1dc6be6 | 2017-10-05 18:25:37 +0200 | [diff] [blame] | 250 | test_mgcp_msg(); |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 251 | |
| 252 | printf("Done\n"); |
| 253 | fprintf(stderr, "Done\n"); |
| 254 | return EXIT_SUCCESS; |
| 255 | } |