Neels Hofmeyr | 3243c7c | 2018-09-30 05:01:20 +0200 | [diff] [blame^] | 1 | #include <stdint.h> |
| 2 | #include <string.h> |
| 3 | |
| 4 | #include <osmocom/core/msgb.h> |
| 5 | #include <osmocom/core/application.h> |
| 6 | #include <osmocom/core/logging.h> |
| 7 | |
| 8 | #include <osmocom/mgcp/iuup_cn_node.h> |
| 9 | #include <osmocom/mgcp/iuup_protocol.h> |
| 10 | |
| 11 | void *ctx = NULL; |
| 12 | |
| 13 | static const char *dump(struct msgb *msg) |
| 14 | { |
| 15 | return osmo_hexdump_nospc(msg->data, msg->len); |
| 16 | } |
| 17 | |
| 18 | struct msgb *msgb_from_hex(const char *label, const char *hex) |
| 19 | { |
| 20 | struct msgb *msg = msgb_alloc_headroom(4096 + OSMO_IUUP_HEADROOM, |
| 21 | OSMO_IUUP_HEADROOM, label); |
| 22 | unsigned char *rc; |
| 23 | msg->l2h = msg->data; |
| 24 | rc = msgb_put(msg, osmo_hexparse(hex, msg->data, msgb_tailroom(msg))); |
| 25 | OSMO_ASSERT(rc == msg->l2h); |
| 26 | return msg; |
| 27 | } |
| 28 | |
| 29 | const char *expect_rx_payload = NULL; |
| 30 | int rx_payload(struct msgb *msg, void *node_priv, void *pdu_priv) |
| 31 | { |
| 32 | printf("rx_payload() invoked by iuup_cn!\n"); |
| 33 | printf(" [IuUP] -RTP->\n"); |
| 34 | printf("%s\n", dump(msg)); |
| 35 | printf("node_priv=%p pdu_priv=%p\n", node_priv, pdu_priv); |
| 36 | if (!expect_rx_payload) { |
| 37 | printf("ERROR: did not expect rx_payload()\n"); |
| 38 | exit(-1); |
| 39 | } else if (strcmp(expect_rx_payload, dump(msg))) { |
| 40 | printf("ERROR: mismatches expected msg %s\n", expect_rx_payload); |
| 41 | exit(-1); |
| 42 | } else |
| 43 | printf("ok: matches expected msg\n"); |
| 44 | msgb_free(msg); |
| 45 | expect_rx_payload = NULL; |
| 46 | return 0; |
| 47 | } |
| 48 | |
| 49 | const char *expect_tx_msg = NULL; |
| 50 | int tx_msg(struct msgb *msg, void *node_priv, void *pdu_priv) |
| 51 | { |
| 52 | printf("tx_msg() invoked by iuup_cn!\n"); |
| 53 | printf(" <-PDU- [IuUP]\n"); |
| 54 | printf("%s\n", dump(msg)); |
| 55 | printf("node_priv=%p pdu_priv=%p\n", node_priv, pdu_priv); |
| 56 | if (!expect_tx_msg) { |
| 57 | printf("ERROR: did not expect tx_msg()\n"); |
| 58 | exit(-1); |
| 59 | } else if (strcmp(expect_tx_msg, dump(msg))) { |
| 60 | printf("ERROR: mismatches expected msg %s\n", expect_tx_msg); |
| 61 | exit(-1); |
| 62 | } else |
| 63 | printf("ok: matches expected msg\n"); |
| 64 | msgb_free(msg); |
| 65 | expect_tx_msg = NULL; |
| 66 | return 0; |
| 67 | } |
| 68 | |
| 69 | static int rx_pdu(struct osmo_iuup_cn *cn, struct msgb *msg, void *pdu_priv) |
| 70 | { |
| 71 | int rc; |
| 72 | printf(" -PDU-> [IuUP]\n"); |
| 73 | printf("%s\n", dump(msg)); |
| 74 | printf("pdu_priv=%p\n", pdu_priv); |
| 75 | rc = osmo_iuup_cn_rx_pdu(cn, msg, pdu_priv); |
| 76 | printf("rc=%d\n", rc); |
| 77 | return rc; |
| 78 | } |
| 79 | |
| 80 | static int tx_payload(struct osmo_iuup_cn *cn, struct msgb *msg, void *pdu_priv) |
| 81 | { |
| 82 | int rc; |
| 83 | printf(" [IuUP] <-RTP-\n"); |
| 84 | printf("%s\n", dump(msg)); |
| 85 | printf("pdu_priv=%p\n", pdu_priv); |
| 86 | rc = osmo_iuup_cn_tx_payload(cn, msg, pdu_priv); |
| 87 | printf("rc=%d\n", rc); |
| 88 | return rc; |
| 89 | } |
| 90 | |
| 91 | void test_cn_session() |
| 92 | { |
| 93 | void *node_priv = (void*)0x2342; |
| 94 | void *pdu_priv = (void*)0xfeed; |
| 95 | |
| 96 | struct osmo_iuup_cn_cfg cfg = { |
| 97 | .node_priv = node_priv, |
| 98 | .rx_payload = rx_payload, |
| 99 | .tx_msg = tx_msg, |
| 100 | }; |
| 101 | |
| 102 | struct osmo_iuup_cn *cn = osmo_iuup_cn_init(ctx, &cfg, __func__); |
| 103 | OSMO_ASSERT(cn); |
| 104 | |
| 105 | printf("\nSend IuUP Initialization. Expecting direct tx_msg() of the Initialization Ack\n"); |
| 106 | expect_tx_msg = "e400240000"; |
| 107 | rx_pdu(cn, |
| 108 | msgb_from_hex("IuUP-Init", |
| 109 | "8060dc5219495e3f00010111" /* <- RTP header */ |
| 110 | "e000df99" /* <- IuUP header */ |
| 111 | "160051673c01270000820000001710000100" /* IuUP params */), |
| 112 | pdu_priv); |
| 113 | |
| 114 | #define RTP_HEADER "8060944c6256042c00010102" |
| 115 | #define IUUP_HEADER "0100e2b3" |
| 116 | #define RTP_PAYLOAD "6cfb23bc46d18180c3e5ffe040045600005a7d35b625b80005fff03214ced0" |
| 117 | printf("\nReceive payload encapsulated in IuUP. Expecting rx_payload() of just RTP packet\n"); |
| 118 | printf("i.e. should strip away " IUUP_HEADER "\n"); |
| 119 | expect_rx_payload = RTP_HEADER RTP_PAYLOAD; |
| 120 | rx_pdu(cn, |
| 121 | msgb_from_hex("IuUP-Data", |
| 122 | RTP_HEADER IUUP_HEADER RTP_PAYLOAD), |
| 123 | pdu_priv); |
| 124 | |
| 125 | printf("\nTransmit RTP. Expecting tx_msg() with inserted IuUP header\n"); |
| 126 | expect_tx_msg = RTP_HEADER "000002b3" RTP_PAYLOAD; |
| 127 | tx_payload(cn, |
| 128 | msgb_from_hex("RTP data", RTP_HEADER RTP_PAYLOAD), |
| 129 | pdu_priv); |
| 130 | |
| 131 | printf("\nMore RTP, each time the Frame Nr advances, causing a new header CRC.\n"); |
| 132 | expect_tx_msg = RTP_HEADER "0100e2b3" RTP_PAYLOAD; |
| 133 | tx_payload(cn, |
| 134 | msgb_from_hex("RTP data", RTP_HEADER RTP_PAYLOAD), |
| 135 | pdu_priv); |
| 136 | expect_tx_msg = RTP_HEADER "02007eb3" RTP_PAYLOAD; |
| 137 | tx_payload(cn, |
| 138 | msgb_from_hex("RTP data", RTP_HEADER RTP_PAYLOAD), |
| 139 | pdu_priv); |
| 140 | expect_tx_msg = RTP_HEADER "03009eb3" RTP_PAYLOAD; |
| 141 | tx_payload(cn, |
| 142 | msgb_from_hex("RTP data", RTP_HEADER RTP_PAYLOAD), |
| 143 | pdu_priv); |
| 144 | |
| 145 | printf("All done.\n"); |
| 146 | } |
| 147 | |
| 148 | static const struct log_info_cat log_categories[] = { |
| 149 | }; |
| 150 | |
| 151 | const struct log_info log_info = { |
| 152 | .cat = log_categories, |
| 153 | .num_cat = ARRAY_SIZE(log_categories), |
| 154 | }; |
| 155 | |
| 156 | int main(void) |
| 157 | { |
| 158 | ctx = talloc_named_const(NULL, 0, __FILE__); |
| 159 | void *msgb_ctx = msgb_talloc_ctx_init(ctx, 0); |
| 160 | osmo_init_logging2(ctx, &log_info); |
| 161 | |
| 162 | test_cn_session(); |
| 163 | |
| 164 | talloc_free(msgb_ctx); |
| 165 | return 0; |
| 166 | } |