Harald Welte | 4029e8c | 2017-11-23 22:00:42 +0100 | [diff] [blame] | 1 | module MGCP_Templates { |
| 2 | |
| 3 | import from MGCP_Types all; |
| 4 | import from SDP_Types all; |
| 5 | |
| 6 | function f_mgcp_par_append(inout template MgcpParameterList list, template MgcpParameter par) { |
| 7 | var integer len := lengthof(list); |
| 8 | list[len] := par; |
| 9 | } |
| 10 | |
| 11 | /* 3.2.2.6 Connection Mode (sendonly, recvonly, sendrecv, confrnce, inactive, loopback, |
| 12 | * conttest, netwloop, netwtest) */ |
| 13 | template MgcpParameter t_MgcpParConnMode(template MgcpConnectionMode mode) := { "M", mode }; |
| 14 | |
| 15 | /* 3.2.2.2 CallId: maximum 32 hex chars */ |
| 16 | template MgcpParameter ts_MgcpParCallId(MgcpCallId cid) := { |
| 17 | code := "C", |
| 18 | val := hex2str(cid) |
| 19 | }; |
| 20 | |
| 21 | /* 3.2.2.18 RequestIdentifier: Maximum 32 hex chars */ |
| 22 | template MgcpParameter ts_MgcpParReqId(MgcpRequestId rid) := { |
| 23 | code := "X", |
| 24 | val := hex2str(rid) |
| 25 | }; |
| 26 | |
| 27 | /* 3.2.2.10: LocalConnectionOptions (codec, packetization, bandwidth, ToS, eco, gain, silence, ...) */ |
| 28 | template MgcpParameter t_MgcpParLocConnOpt(template charstring lco) := { "L", lco }; |
| 29 | |
| 30 | /* 3.2.2.5: ConnectionId: maximum 32 hex chars */ |
| 31 | template MgcpParameter ts_MgcpParConnectionId(MgcpConnectionId cid) := { |
| 32 | code := "I", |
| 33 | val := hex2str(cid) |
| 34 | }; |
| 35 | |
| 36 | /* osmo-bsc_mgcp implements L/C/M/X only, osmo-mgw adds 'I' */ |
| 37 | /* SDP: osmo-bsc_mgcp implements Tx of v,o,s,c,t,m,a */ |
| 38 | |
| 39 | template MgcpResponse tr_MgcpResp_Err(template MgcpResponseCode code) := { |
| 40 | line := { |
| 41 | code := code, |
| 42 | trans_id := ?, |
| 43 | string := ? |
| 44 | }, |
| 45 | params := {}, |
| 46 | sdp := omit |
| 47 | } |
| 48 | |
| 49 | template MgcpCommandLine t_MgcpCmdLine(template charstring verb, template MgcpTransId trans_id, template charstring ep) := { |
| 50 | verb := verb, |
| 51 | trans_id := trans_id, |
| 52 | ep := ep, |
| 53 | ver := "1.0" |
| 54 | }; |
| 55 | |
| 56 | template MgcpCommand ts_CRCX(MgcpTransId trans_id, charstring ep, MgcpConnectionMode mode, MgcpCallId call_id, template SDP_Message sdp := omit) := { |
| 57 | line := t_MgcpCmdLine("CRCX", trans_id, ep), |
| 58 | params := { |
| 59 | t_MgcpParConnMode(mode), |
| 60 | ts_MgcpParCallId(call_id), |
| 61 | //t_MgcpParReqId(omit), |
Daniel Willmann | fbef714 | 2017-11-30 13:16:14 +0100 | [diff] [blame] | 62 | t_MgcpParLocConnOpt("p:20, a:AMR") |
Harald Welte | 4029e8c | 2017-11-23 22:00:42 +0100 | [diff] [blame] | 63 | }, |
| 64 | sdp := sdp |
| 65 | } |
| 66 | |
Harald Welte | 9002957 | 2017-11-24 23:39:50 +0100 | [diff] [blame] | 67 | template MgcpCommand tr_CRCX := { |
| 68 | line := t_MgcpCmdLine("CRCX", ?, ?), |
| 69 | params := *, |
| 70 | sdp := * |
| 71 | } |
| 72 | |
Harald Welte | 4029e8c | 2017-11-23 22:00:42 +0100 | [diff] [blame] | 73 | template MgcpResponse tr_CRCX_ACK := { |
| 74 | line := { |
| 75 | code := "200", |
Harald Welte | 51f34ad | 2017-11-24 20:40:43 +0100 | [diff] [blame] | 76 | trans_id := ?, |
Harald Welte | 4029e8c | 2017-11-23 22:00:42 +0100 | [diff] [blame] | 77 | string := "OK" |
| 78 | }, |
| 79 | params:= { { "I", ? }, *}, |
| 80 | sdp := ? |
| 81 | } |
| 82 | |
Harald Welte | 9002957 | 2017-11-24 23:39:50 +0100 | [diff] [blame] | 83 | template MgcpResponse ts_CRCX_ACK(MgcpTransId trans_id, MgcpConnectionId conn_id, template SDP_Message sdp := omit) := { |
| 84 | line := { |
| 85 | code := "200", |
| 86 | trans_id := trans_id, |
| 87 | string := "OK" |
| 88 | }, |
| 89 | params:= { ts_MgcpParConnectionId(conn_id) }, |
| 90 | sdp := sdp |
| 91 | } |
| 92 | |
Harald Welte | 4029e8c | 2017-11-23 22:00:42 +0100 | [diff] [blame] | 93 | template MgcpCommand ts_MDCX(MgcpTransId trans_id, charstring ep, MgcpConnectionMode mode, MgcpCallId call_id, MgcpConnectionId conn_id, template SDP_Message sdp := omit) := { |
| 94 | line := t_MgcpCmdLine("MDCX", trans_id, ep), |
| 95 | params := { |
| 96 | t_MgcpParConnMode(mode), |
| 97 | ts_MgcpParCallId(call_id), |
| 98 | ts_MgcpParConnectionId(conn_id), |
| 99 | //t_MgcpParReqId(omit), |
Daniel Willmann | fbef714 | 2017-11-30 13:16:14 +0100 | [diff] [blame] | 100 | t_MgcpParLocConnOpt("p:20, a:AMR") |
Harald Welte | 4029e8c | 2017-11-23 22:00:42 +0100 | [diff] [blame] | 101 | }, |
| 102 | sdp := sdp |
| 103 | } |
| 104 | |
Harald Welte | 9002957 | 2017-11-24 23:39:50 +0100 | [diff] [blame] | 105 | template MgcpCommand tr_MDCX := { |
| 106 | line := t_MgcpCmdLine("MDCX", ?, ?), |
| 107 | params := *, |
| 108 | sdp := * |
| 109 | } |
| 110 | |
| 111 | template MgcpResponse ts_MDCX_ACK(MgcpTransId trans_id, MgcpConnectionId conn_id, template SDP_Message sdp := omit) := ts_CRCX_ACK(trans_id, conn_id, sdp); |
| 112 | |
Harald Welte | 4029e8c | 2017-11-23 22:00:42 +0100 | [diff] [blame] | 113 | /* have a function that generates a template, rather than a template in order to handle |
| 114 | * optional parameters */ |
| 115 | function ts_DLCX(MgcpTransId trans_id, charstring ep, template MgcpCallId call_id := omit, |
| 116 | template MgcpConnectionId conn_id := omit) return template MgcpCommand { |
| 117 | var template MgcpCommand cmd; |
| 118 | cmd.line := t_MgcpCmdLine("DLCX", trans_id, ep); |
| 119 | cmd.params := {}; |
| 120 | cmd.sdp := omit; |
| 121 | if (isvalue(call_id)) { |
| 122 | f_mgcp_par_append(cmd.params, ts_MgcpParCallId(valueof(call_id))); |
| 123 | if (isvalue(conn_id)) { |
| 124 | f_mgcp_par_append(cmd.params, ts_MgcpParConnectionId(valueof(conn_id))); |
| 125 | } |
| 126 | } |
| 127 | return cmd; |
| 128 | } |
| 129 | |
Harald Welte | 9002957 | 2017-11-24 23:39:50 +0100 | [diff] [blame] | 130 | template MgcpCommand tr_DLCX := { |
| 131 | line := t_MgcpCmdLine("DLCX", ?, ?), |
| 132 | params := *, |
| 133 | sdp := * |
| 134 | } |
| 135 | |
Harald Welte | c82eef4 | 2017-11-24 20:40:12 +0100 | [diff] [blame] | 136 | template MgcpResponse tr_DLCX_ACK := { |
| 137 | line := { |
Daniel Willmann | 961e5c9 | 2017-11-30 16:37:40 +0100 | [diff] [blame] | 138 | code := ("200", "250"), |
Harald Welte | c82eef4 | 2017-11-24 20:40:12 +0100 | [diff] [blame] | 139 | trans_id := ?, |
| 140 | string := "OK" |
| 141 | }, |
| 142 | params:= *, |
| 143 | sdp := * |
| 144 | } |
| 145 | |
Harald Welte | 9002957 | 2017-11-24 23:39:50 +0100 | [diff] [blame] | 146 | template MgcpResponse ts_DLCX_ACK(MgcpTransId trans_id, MgcpConnectionId conn_id, template SDP_Message sdp := omit) := ts_CRCX_ACK(trans_id, conn_id, sdp); |
| 147 | |
Harald Welte | e98bb2e | 2017-11-29 12:09:48 +0100 | [diff] [blame] | 148 | template MgcpCommand tr_RSIP := { |
| 149 | line := t_MgcpCmdLine("RSIP", ?, ?), |
| 150 | params := *, |
| 151 | sdp := * |
| 152 | } |
| 153 | |
Harald Welte | 4029e8c | 2017-11-23 22:00:42 +0100 | [diff] [blame] | 154 | /* SDP Templates */ |
| 155 | template SDP_Origin ts_SDP_origin(charstring addr, charstring session_id, |
| 156 | charstring session_version := "1", |
| 157 | charstring addr_type := "IP4", |
| 158 | charstring user_name := "-") := { |
| 159 | user_name := user_name, |
| 160 | session_id := session_id, |
| 161 | session_version := session_version, |
| 162 | net_type := "IN", |
| 163 | addr_type := addr_type, |
| 164 | addr := addr |
| 165 | } |
| 166 | |
| 167 | template SDP_connection ts_SDP_connection_IP(charstring addr, charstring addr_type := "IP4", |
| 168 | template integer ttl := omit, |
| 169 | template integer num_of_addr := omit) :={ |
| 170 | net_type := "IN", |
| 171 | addr_type := addr_type, |
| 172 | conn_addr := { |
| 173 | addr := addr, |
| 174 | ttl := ttl, |
| 175 | num_of_addr := num_of_addr |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | template SDP_time ts_SDP_time(charstring beg, charstring end) := { |
| 180 | time_field := { |
| 181 | start_time := beg, |
| 182 | stop_time := end |
| 183 | }, |
| 184 | time_repeat := omit |
| 185 | } |
| 186 | |
| 187 | template SDP_media_desc ts_SDP_media_desc(integer port_number, SDP_fmt_list fmts, |
| 188 | SDP_attribute_list attributes) := { |
| 189 | media_field := { |
| 190 | media := "audio", |
| 191 | ports := { |
| 192 | port_number := port_number, |
| 193 | num_of_ports := omit |
| 194 | }, |
| 195 | transport := "RTP/AVP", |
| 196 | fmts := fmts |
| 197 | }, |
| 198 | information := omit, |
| 199 | connections := omit, |
| 200 | bandwidth := omit, |
| 201 | key := omit, |
| 202 | attributes := attributes |
| 203 | } |
| 204 | |
| 205 | /* master template for generating SDP based in template arguments */ |
| 206 | template SDP_Message ts_SDP(charstring local_addr, charstring remote_addr, |
| 207 | charstring session_id, charstring session_version, |
| 208 | integer rtp_port, SDP_fmt_list fmts, |
| 209 | SDP_attribute_list attributes) := { |
| 210 | protocol_version := 0, |
| 211 | origin := ts_SDP_origin(local_addr, session_id, session_version), |
| 212 | session_name := "-", |
| 213 | information := omit, |
| 214 | uri := omit, |
| 215 | emails := omit, |
| 216 | phone_numbers := omit, |
| 217 | connection := ts_SDP_connection_IP(remote_addr), |
| 218 | bandwidth := omit, |
| 219 | times := { ts_SDP_time("0","0") }, |
| 220 | timezone_adjustments := omit, |
| 221 | key := omit, |
| 222 | attributes := omit, |
| 223 | media_list := { ts_SDP_media_desc(rtp_port, fmts, attributes) } |
| 224 | } |
| 225 | |
| 226 | template SDP_attribute ts_SDP_rtpmap(integer fmt, charstring val) := { |
| 227 | rtpmap := { |
| 228 | attr_value := int2str(fmt) & " " & val |
| 229 | } |
| 230 | } |
| 231 | template SDP_attribute ts_SDP_ptime(integer p) := { |
| 232 | ptime := { |
| 233 | attr_value := int2str(p) |
| 234 | } |
| 235 | } |
| 236 | |
Harald Welte | 4c11d56 | 2017-11-24 23:39:00 +0100 | [diff] [blame] | 237 | function f_MgcpResp_extract_conn_id(MgcpResponse resp) return MgcpConnectionId { |
| 238 | var integer i; |
| 239 | for (i := 0; i < lengthof(resp.params); i := i + 1) { |
| 240 | var MgcpParameter par := resp.params[i]; |
| 241 | if (par.code == "I") { |
| 242 | return str2hex(par.val); |
| 243 | } |
| 244 | } |
| 245 | setverdict(fail); |
| 246 | return '00000000'H; |
| 247 | } |
| 248 | |
| 249 | function f_mgcp_alloc_tid() return MgcpTransId { |
| 250 | return int2str(float2int(rnd()*2147483647.0)); |
| 251 | } |
| 252 | |
| 253 | function f_mgcp_alloc_call_id() return MgcpCallId { |
| 254 | return int2hex(float2int(rnd()*2147483647.0), 8); |
| 255 | } |
| 256 | |
| 257 | function f_mgcp_alloc_conn_id() return MgcpConnectionId { |
| 258 | return int2hex(float2int(rnd()*2147483647.0), 8); |
| 259 | } |
| 260 | |
Harald Welte | 4029e8c | 2017-11-23 22:00:42 +0100 | [diff] [blame] | 261 | } |