1 /*
2 belle-sip - SIP (RFC3261) library.
3 Copyright (C) 2014 Belledonne Communications SARL
4
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 #include "linphone/core.h"
20 #include "private.h"
21 #include "liblinphone_tester.h"
22 #include "mediastreamer2/stun.h"
23 #include "ortp/port.h"
24
25
26 static const char *stun_address = "stun.linphone.org";
27
28
test_stun_encode(char ** buffer)29 static size_t test_stun_encode(char **buffer)
30 {
31 MSStunMessage *req = ms_stun_binding_request_create();
32 UInt96 tr_id = ms_stun_message_get_tr_id(req);
33 tr_id.octet[0] = 11;
34 ms_stun_message_set_tr_id(req, tr_id);
35 return ms_stun_message_encode(req, buffer);
36 }
37
linphone_stun_test_encode(void)38 static void linphone_stun_test_encode(void)
39 {
40 char *buffer = NULL;
41 size_t len = test_stun_encode(&buffer);
42 BC_ASSERT(len > 0);
43 BC_ASSERT_PTR_NOT_NULL(buffer);
44 if (buffer != NULL) ms_free(buffer);
45 ms_message("STUN message encoded in %i bytes", (int)len);
46 }
47
linphone_stun_test_grab_ip(void)48 static void linphone_stun_test_grab_ip(void)
49 {
50
51 LinphoneCoreManager* lc_stun = linphone_core_manager_new2("stun_rc", FALSE);
52 LinphoneCall dummy_call;
53 int ping_time;
54 int tmp = 0;
55
56 /*this test verifies the very basic STUN support of liblinphone, which is deprecated.
57 * It works only in IPv4 mode and there is no plan to make it work over ipv6.*/
58 if (liblinphone_tester_ipv4_available()){
59 goto end;
60 }
61 linphone_core_enable_ipv6(lc_stun->lc, FALSE);
62
63 memset(&dummy_call, 0, sizeof(LinphoneCall));
64 dummy_call.main_audio_stream_index = 0;
65 dummy_call.main_video_stream_index = 1;
66 dummy_call.main_text_stream_index = 2;
67 dummy_call.media_ports[dummy_call.main_audio_stream_index].rtp_port = 7078;
68 dummy_call.media_ports[dummy_call.main_video_stream_index].rtp_port = 9078;
69 dummy_call.media_ports[dummy_call.main_text_stream_index].rtp_port = 11078;
70
71 linphone_core_set_stun_server(lc_stun->lc, stun_address);
72 BC_ASSERT_STRING_EQUAL(stun_address, linphone_core_get_stun_server(lc_stun->lc));
73
74 wait_for(lc_stun->lc, lc_stun->lc, &tmp, 1);
75
76 ping_time = linphone_core_run_stun_tests(lc_stun->lc, &dummy_call);
77 BC_ASSERT(ping_time != -1);
78
79 ms_message("Round trip to STUN: %d ms", ping_time);
80
81 BC_ASSERT(dummy_call.ac.addr[0] != '\0');
82 BC_ASSERT(dummy_call.ac.port != 0);
83 #ifdef VIDEO_ENABLED
84 BC_ASSERT(dummy_call.vc.addr[0] != '\0');
85 BC_ASSERT(dummy_call.vc.port != 0);
86 #endif
87 BC_ASSERT(dummy_call.tc.addr[0] != '\0');
88 BC_ASSERT(dummy_call.tc.port != 0);
89
90 ms_message("STUN test result: local audio port maps to %s:%i", dummy_call.ac.addr, dummy_call.ac.port);
91 #ifdef VIDEO_ENABLED
92 ms_message("STUN test result: local video port maps to %s:%i", dummy_call.vc.addr, dummy_call.vc.port);
93 #endif
94 ms_message("STUN test result: local text port maps to %s:%i", dummy_call.tc.addr, dummy_call.tc.port);
95
96 end:
97 linphone_core_manager_destroy(lc_stun);
98 }
99
configure_nat_policy(LinphoneCore * lc,bool_t turn_enabled)100 static void configure_nat_policy(LinphoneCore *lc, bool_t turn_enabled) {
101 const char *username = "liblinphone-tester";
102 const char *password = "retset-enohpnilbil";
103 LinphoneAuthInfo *auth_info = linphone_core_create_auth_info(lc, username, NULL, password, NULL, "sip.linphone.org", NULL);
104 LinphoneNatPolicy *nat_policy = linphone_core_create_nat_policy(lc);
105 linphone_nat_policy_enable_ice(nat_policy, TRUE);
106 if (turn_enabled) {
107 linphone_nat_policy_enable_turn(nat_policy, TRUE);
108 linphone_nat_policy_set_stun_server(nat_policy, "sip1.linphone.org:3479");
109 linphone_nat_policy_set_stun_server_username(nat_policy, username);
110 } else {
111 linphone_nat_policy_enable_stun(nat_policy, TRUE);
112 linphone_nat_policy_set_stun_server(nat_policy, "stun.linphone.org");
113 }
114 linphone_core_set_nat_policy(lc, nat_policy);
115 linphone_core_add_auth_info(lc, auth_info);
116 linphone_nat_policy_unref(nat_policy);
117 linphone_auth_info_unref(auth_info);
118 }
119
check_turn_context_statistics(MSTurnContext * turn_context,bool_t forced_relay)120 static void check_turn_context_statistics(MSTurnContext *turn_context, bool_t forced_relay) {
121 BC_ASSERT_TRUE(turn_context->stats.nb_successful_allocate > 1);
122 if (forced_relay == TRUE) {
123 BC_ASSERT_TRUE(turn_context->stats.nb_send_indication > 0);
124 BC_ASSERT_TRUE(turn_context->stats.nb_data_indication > 0);
125 BC_ASSERT_TRUE(turn_context->stats.nb_received_channel_msg > 0);
126 BC_ASSERT_TRUE(turn_context->stats.nb_sent_channel_msg > 0);
127 BC_ASSERT_TRUE(turn_context->stats.nb_successful_refresh > 0);
128 BC_ASSERT_TRUE(turn_context->stats.nb_successful_create_permission > 1);
129 BC_ASSERT_TRUE(turn_context->stats.nb_successful_channel_bind > 1);
130 }
131 }
132
ice_turn_call_base(bool_t video_enabled,bool_t forced_relay,bool_t caller_turn_enabled,bool_t callee_turn_enabled,bool_t rtcp_mux_enabled,bool_t ipv6)133 static void ice_turn_call_base(bool_t video_enabled, bool_t forced_relay, bool_t caller_turn_enabled, bool_t callee_turn_enabled, bool_t rtcp_mux_enabled, bool_t ipv6) {
134 LinphoneCoreManager *marie;
135 LinphoneCoreManager *pauline;
136 LinphoneCall *lcall;
137 LinphoneIceState expected_ice_state = LinphoneIceStateHostConnection;
138 LinphoneMediaDirection expected_video_dir = LinphoneMediaDirectionInactive;
139 bctbx_list_t *lcs = NULL;
140
141 marie = linphone_core_manager_new2("marie_rc", FALSE);
142 lcs = bctbx_list_append(lcs, marie->lc);
143 pauline = linphone_core_manager_new2(transport_supported(LinphoneTransportTls) ? "pauline_rc" : "pauline_tcp_rc", FALSE);
144 lcs = bctbx_list_append(lcs, pauline->lc);
145
146 if (ipv6) {
147 linphone_core_enable_ipv6(marie->lc, TRUE);
148 linphone_core_enable_ipv6(pauline->lc, TRUE);
149 } else {
150 linphone_core_enable_ipv6(marie->lc, FALSE);
151 linphone_core_enable_ipv6(pauline->lc, FALSE);
152 }
153
154 configure_nat_policy(marie->lc, caller_turn_enabled);
155 configure_nat_policy(pauline->lc, callee_turn_enabled);
156 if (forced_relay == TRUE) {
157 linphone_core_enable_forced_ice_relay(marie->lc, TRUE);
158 linphone_core_enable_forced_ice_relay(pauline->lc, TRUE);
159 linphone_core_enable_short_turn_refresh(marie->lc, TRUE);
160 linphone_core_enable_short_turn_refresh(pauline->lc, TRUE);
161 expected_ice_state = LinphoneIceStateRelayConnection;
162 }
163 if (rtcp_mux_enabled == TRUE) {
164 lp_config_set_int(linphone_core_get_config(marie->lc), "rtp", "rtcp_mux", 1);
165 lp_config_set_int(linphone_core_get_config(pauline->lc), "rtp", "rtcp_mux", 1);
166 }
167
168 linphone_core_manager_start(marie, TRUE);
169 linphone_core_manager_start(pauline, TRUE);
170
171 if (video_enabled) {
172 #ifdef VIDEO_ENABLED
173 linphone_core_set_video_device(pauline->lc,liblinphone_tester_mire_id);
174 linphone_core_set_video_device(marie->lc,liblinphone_tester_mire_id);
175 video_call_base_2(marie, pauline, FALSE, LinphoneMediaEncryptionNone, TRUE, TRUE);
176 expected_video_dir = LinphoneMediaDirectionSendRecv;
177 #endif
178 } else {
179 BC_ASSERT_TRUE(call(marie, pauline));
180 }
181
182 /* Wait for the ICE reINVITE to complete */
183 BC_ASSERT_TRUE(wait_for(pauline->lc, marie->lc, &pauline->stat.number_of_LinphoneCallStreamsRunning, 2));
184 BC_ASSERT_TRUE(wait_for(pauline->lc, marie->lc, &marie->stat.number_of_LinphoneCallStreamsRunning, 2));
185 BC_ASSERT_TRUE(check_ice(pauline, marie, expected_ice_state));
186 check_nb_media_starts(pauline, marie, 1, 1);
187 check_media_direction(marie, linphone_core_get_current_call(marie->lc), lcs, LinphoneMediaDirectionSendRecv, expected_video_dir);
188 check_media_direction(pauline, linphone_core_get_current_call(pauline->lc), lcs, LinphoneMediaDirectionSendRecv, expected_video_dir);
189 liblinphone_tester_check_rtcp(marie, pauline);
190 lcall = linphone_core_get_current_call(marie->lc);
191 BC_ASSERT_PTR_NOT_NULL(lcall);
192 if (lcall != NULL) {
193 BC_ASSERT_PTR_NOT_NULL(lcall->ice_session);
194 if (lcall->ice_session != NULL) {
195 IceCheckList *cl = ice_session_check_list(lcall->ice_session, 0);
196 BC_ASSERT_PTR_NOT_NULL(cl);
197 if (cl != NULL) {
198 check_turn_context_statistics(cl->rtp_turn_context, forced_relay);
199 if (!rtcp_mux_enabled) check_turn_context_statistics(cl->rtcp_turn_context, forced_relay);
200 }
201 }
202 }
203
204 end_call(marie, pauline);
205
206 linphone_core_manager_destroy(pauline);
207 linphone_core_manager_destroy(marie);
208 bctbx_list_free(lcs);
209 }
210
basic_ice_turn_call(void)211 static void basic_ice_turn_call(void) {
212 ice_turn_call_base(FALSE, FALSE, TRUE, TRUE, FALSE, FALSE);
213 }
214
basic_ipv6_ice_turn_call(void)215 static void basic_ipv6_ice_turn_call(void) {
216 if (liblinphone_tester_ipv6_available()) {
217 ice_turn_call_base(FALSE, FALSE, TRUE, TRUE, FALSE, TRUE);
218 } else {
219 ms_warning("Test skipped, no ipv6 available");
220 }
221 }
222
223 #ifdef VIDEO_ENABLED
video_ice_turn_call(void)224 static void video_ice_turn_call(void) {
225 ice_turn_call_base(TRUE, FALSE, TRUE, TRUE, FALSE, FALSE);
226 }
227 #endif
228
relayed_ice_turn_call(void)229 static void relayed_ice_turn_call(void) {
230 ice_turn_call_base(FALSE, TRUE, TRUE, TRUE, FALSE, FALSE);
231 }
232
233 #ifdef VIDEO_ENABLED
relayed_video_ice_turn_call(void)234 static void relayed_video_ice_turn_call(void) {
235 ice_turn_call_base(TRUE, TRUE, TRUE, TRUE, FALSE, FALSE);
236 }
237 #endif
238
relayed_ice_turn_call_with_rtcp_mux(void)239 static void relayed_ice_turn_call_with_rtcp_mux(void) {
240 ice_turn_call_base(FALSE, TRUE, TRUE, TRUE, TRUE, FALSE);
241 }
242
relayed_ice_turn_to_ice_stun_call(void)243 static void relayed_ice_turn_to_ice_stun_call(void) {
244 ice_turn_call_base(FALSE, TRUE, TRUE, FALSE, FALSE, FALSE);
245 }
246
247
248 test_t stun_tests[] = {
249 TEST_ONE_TAG("Basic Stun test (Ping/public IP)", linphone_stun_test_grab_ip, "STUN"),
250 TEST_ONE_TAG("STUN encode", linphone_stun_test_encode, "STUN"),
251 TEST_TWO_TAGS("Basic ICE+TURN call", basic_ice_turn_call, "ICE", "TURN"),
252 TEST_TWO_TAGS("Basic IPv6 ICE+TURN call", basic_ipv6_ice_turn_call, "ICE", "TURN"),
253 #ifdef VIDEO_ENABLED
254 TEST_TWO_TAGS("Video ICE+TURN call", video_ice_turn_call, "ICE", "TURN"),
255 TEST_TWO_TAGS("Relayed video ICE+TURN call", relayed_video_ice_turn_call, "ICE", "TURN"),
256 #endif
257 TEST_TWO_TAGS("Relayed ICE+TURN call", relayed_ice_turn_call, "ICE", "TURN"),
258 TEST_TWO_TAGS("Relayed ICE+TURN call with rtcp-mux", relayed_ice_turn_call_with_rtcp_mux, "ICE", "TURN"),
259 TEST_TWO_TAGS("Relayed ICE+TURN to ICE+STUN call", relayed_ice_turn_to_ice_stun_call, "ICE", "TURN")
260 };
261
262 test_suite_t stun_test_suite = {"Stun", NULL, NULL, liblinphone_tester_before_each, liblinphone_tester_after_each,
263 sizeof(stun_tests) / sizeof(stun_tests[0]), stun_tests};
264