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; |
Neels Hofmeyr | 85f9401 | 2018-10-08 03:36:45 +0200 | [diff] [blame] | 30 | int rx_payload(struct msgb *msg, void *node_priv) |
Neels Hofmeyr | 3243c7c | 2018-09-30 05:01:20 +0200 | [diff] [blame] | 31 | { |
| 32 | printf("rx_payload() invoked by iuup_cn!\n"); |
| 33 | printf(" [IuUP] -RTP->\n"); |
| 34 | printf("%s\n", dump(msg)); |
Neels Hofmeyr | 85f9401 | 2018-10-08 03:36:45 +0200 | [diff] [blame] | 35 | printf("node_priv=%p\n", node_priv); |
Neels Hofmeyr | 3243c7c | 2018-09-30 05:01:20 +0200 | [diff] [blame] | 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"); |
Neels Hofmeyr | 3243c7c | 2018-09-30 05:01:20 +0200 | [diff] [blame] | 44 | expect_rx_payload = NULL; |
| 45 | return 0; |
| 46 | } |
| 47 | |
| 48 | const char *expect_tx_msg = NULL; |
Neels Hofmeyr | 85f9401 | 2018-10-08 03:36:45 +0200 | [diff] [blame] | 49 | int tx_msg(struct msgb *msg, void *node_priv) |
Neels Hofmeyr | 3243c7c | 2018-09-30 05:01:20 +0200 | [diff] [blame] | 50 | { |
| 51 | printf("tx_msg() invoked by iuup_cn!\n"); |
| 52 | printf(" <-PDU- [IuUP]\n"); |
| 53 | printf("%s\n", dump(msg)); |
Neels Hofmeyr | 85f9401 | 2018-10-08 03:36:45 +0200 | [diff] [blame] | 54 | printf("node_priv=%p\n", node_priv); |
Neels Hofmeyr | 3243c7c | 2018-09-30 05:01:20 +0200 | [diff] [blame] | 55 | if (!expect_tx_msg) { |
| 56 | printf("ERROR: did not expect tx_msg()\n"); |
| 57 | exit(-1); |
| 58 | } else if (strcmp(expect_tx_msg, dump(msg))) { |
| 59 | printf("ERROR: mismatches expected msg %s\n", expect_tx_msg); |
| 60 | exit(-1); |
| 61 | } else |
| 62 | printf("ok: matches expected msg\n"); |
Neels Hofmeyr | 3243c7c | 2018-09-30 05:01:20 +0200 | [diff] [blame] | 63 | expect_tx_msg = NULL; |
| 64 | return 0; |
| 65 | } |
| 66 | |
Neels Hofmeyr | 85f9401 | 2018-10-08 03:36:45 +0200 | [diff] [blame] | 67 | static int rx_pdu(struct osmo_iuup_cn *cn, struct msgb *msg) |
Neels Hofmeyr | 3243c7c | 2018-09-30 05:01:20 +0200 | [diff] [blame] | 68 | { |
| 69 | int rc; |
| 70 | printf(" -PDU-> [IuUP]\n"); |
| 71 | printf("%s\n", dump(msg)); |
Neels Hofmeyr | 85f9401 | 2018-10-08 03:36:45 +0200 | [diff] [blame] | 72 | rc = osmo_iuup_cn_rx_pdu(cn, msg); |
Neels Hofmeyr | 3243c7c | 2018-09-30 05:01:20 +0200 | [diff] [blame] | 73 | printf("rc=%d\n", rc); |
| 74 | return rc; |
| 75 | } |
| 76 | |
Neels Hofmeyr | 85f9401 | 2018-10-08 03:36:45 +0200 | [diff] [blame] | 77 | static int tx_payload(struct osmo_iuup_cn *cn, struct msgb *msg) |
Neels Hofmeyr | 3243c7c | 2018-09-30 05:01:20 +0200 | [diff] [blame] | 78 | { |
| 79 | int rc; |
| 80 | printf(" [IuUP] <-RTP-\n"); |
| 81 | printf("%s\n", dump(msg)); |
Neels Hofmeyr | 85f9401 | 2018-10-08 03:36:45 +0200 | [diff] [blame] | 82 | rc = osmo_iuup_cn_tx_payload(cn, msg); |
Neels Hofmeyr | 3243c7c | 2018-09-30 05:01:20 +0200 | [diff] [blame] | 83 | printf("rc=%d\n", rc); |
| 84 | return rc; |
| 85 | } |
| 86 | |
| 87 | void test_cn_session() |
| 88 | { |
| 89 | void *node_priv = (void*)0x2342; |
Neels Hofmeyr | 3243c7c | 2018-09-30 05:01:20 +0200 | [diff] [blame] | 90 | |
| 91 | struct osmo_iuup_cn_cfg cfg = { |
| 92 | .node_priv = node_priv, |
| 93 | .rx_payload = rx_payload, |
| 94 | .tx_msg = tx_msg, |
| 95 | }; |
| 96 | |
| 97 | struct osmo_iuup_cn *cn = osmo_iuup_cn_init(ctx, &cfg, __func__); |
| 98 | OSMO_ASSERT(cn); |
| 99 | |
| 100 | printf("\nSend IuUP Initialization. Expecting direct tx_msg() of the Initialization Ack\n"); |
Neels Hofmeyr | 85f9401 | 2018-10-08 03:36:45 +0200 | [diff] [blame] | 101 | expect_tx_msg = "8060dc5219495e3f00010111" /* RTP header */ |
| 102 | "e4002400"; /* IuUP Init Ack */ |
Neels Hofmeyr | 3243c7c | 2018-09-30 05:01:20 +0200 | [diff] [blame] | 103 | rx_pdu(cn, |
| 104 | msgb_from_hex("IuUP-Init", |
| 105 | "8060dc5219495e3f00010111" /* <- RTP header */ |
| 106 | "e000df99" /* <- IuUP header */ |
Neels Hofmeyr | 85f9401 | 2018-10-08 03:36:45 +0200 | [diff] [blame] | 107 | "160051673c01270000820000001710000100" /* IuUP params */)); |
Neels Hofmeyr | 3243c7c | 2018-09-30 05:01:20 +0200 | [diff] [blame] | 108 | |
| 109 | #define RTP_HEADER "8060944c6256042c00010102" |
| 110 | #define IUUP_HEADER "0100e2b3" |
| 111 | #define RTP_PAYLOAD "6cfb23bc46d18180c3e5ffe040045600005a7d35b625b80005fff03214ced0" |
| 112 | printf("\nReceive payload encapsulated in IuUP. Expecting rx_payload() of just RTP packet\n"); |
| 113 | printf("i.e. should strip away " IUUP_HEADER "\n"); |
efistokl | fdfa184 | 2018-12-21 20:25:43 +0200 | [diff] [blame^] | 114 | expect_rx_payload = RTP_HEADER "f03c" RTP_PAYLOAD; |
Neels Hofmeyr | 3243c7c | 2018-09-30 05:01:20 +0200 | [diff] [blame] | 115 | rx_pdu(cn, |
| 116 | msgb_from_hex("IuUP-Data", |
Neels Hofmeyr | 85f9401 | 2018-10-08 03:36:45 +0200 | [diff] [blame] | 117 | RTP_HEADER IUUP_HEADER RTP_PAYLOAD)); |
Neels Hofmeyr | 3243c7c | 2018-09-30 05:01:20 +0200 | [diff] [blame] | 118 | |
| 119 | printf("\nTransmit RTP. Expecting tx_msg() with inserted IuUP header\n"); |
| 120 | expect_tx_msg = RTP_HEADER "000002b3" RTP_PAYLOAD; |
| 121 | tx_payload(cn, |
efistokl | fdfa184 | 2018-12-21 20:25:43 +0200 | [diff] [blame^] | 122 | msgb_from_hex("RTP data", RTP_HEADER "f03c" RTP_PAYLOAD)); |
Neels Hofmeyr | 3243c7c | 2018-09-30 05:01:20 +0200 | [diff] [blame] | 123 | |
| 124 | printf("\nMore RTP, each time the Frame Nr advances, causing a new header CRC.\n"); |
| 125 | expect_tx_msg = RTP_HEADER "0100e2b3" RTP_PAYLOAD; |
| 126 | tx_payload(cn, |
efistokl | fdfa184 | 2018-12-21 20:25:43 +0200 | [diff] [blame^] | 127 | msgb_from_hex("RTP data", RTP_HEADER "f03c" RTP_PAYLOAD)); |
Neels Hofmeyr | 3243c7c | 2018-09-30 05:01:20 +0200 | [diff] [blame] | 128 | expect_tx_msg = RTP_HEADER "02007eb3" RTP_PAYLOAD; |
| 129 | tx_payload(cn, |
efistokl | fdfa184 | 2018-12-21 20:25:43 +0200 | [diff] [blame^] | 130 | msgb_from_hex("RTP data", RTP_HEADER "f03c" RTP_PAYLOAD)); |
Neels Hofmeyr | 3243c7c | 2018-09-30 05:01:20 +0200 | [diff] [blame] | 131 | expect_tx_msg = RTP_HEADER "03009eb3" RTP_PAYLOAD; |
| 132 | tx_payload(cn, |
efistokl | fdfa184 | 2018-12-21 20:25:43 +0200 | [diff] [blame^] | 133 | msgb_from_hex("RTP data", RTP_HEADER "f03c" RTP_PAYLOAD)); |
Neels Hofmeyr | 3243c7c | 2018-09-30 05:01:20 +0200 | [diff] [blame] | 134 | |
| 135 | printf("All done.\n"); |
| 136 | } |
| 137 | |
| 138 | static const struct log_info_cat log_categories[] = { |
| 139 | }; |
| 140 | |
| 141 | const struct log_info log_info = { |
| 142 | .cat = log_categories, |
| 143 | .num_cat = ARRAY_SIZE(log_categories), |
| 144 | }; |
| 145 | |
| 146 | int main(void) |
| 147 | { |
| 148 | ctx = talloc_named_const(NULL, 0, __FILE__); |
| 149 | void *msgb_ctx = msgb_talloc_ctx_init(ctx, 0); |
| 150 | osmo_init_logging2(ctx, &log_info); |
| 151 | |
| 152 | test_cn_session(); |
| 153 | |
| 154 | talloc_free(msgb_ctx); |
| 155 | return 0; |
| 156 | } |