|  | 
|  | 1 | +/** | 
|  | 2 | + * Copyright (c) 2022 Paul-Louis Ageneau | 
|  | 3 | + * | 
|  | 4 | + * This Source Code Form is subject to the terms of the Mozilla Public | 
|  | 5 | + * License, v. 2.0. If a copy of the MPL was not distributed with this | 
|  | 6 | + * file, You can obtain one at https://mozilla.org/MPL/2.0/. | 
|  | 7 | + */ | 
|  | 8 | + | 
|  | 9 | +#include "juice/juice.h" | 
|  | 10 | + | 
|  | 11 | +#include <stdbool.h> | 
|  | 12 | +#include <stdint.h> | 
|  | 13 | +#include <stdio.h> | 
|  | 14 | +#include <string.h> | 
|  | 15 | + | 
|  | 16 | +#ifdef _WIN32 | 
|  | 17 | +#include <windows.h> | 
|  | 18 | +static void sleep(unsigned int secs) { Sleep(secs * 1000); } | 
|  | 19 | +#else | 
|  | 20 | +#include <unistd.h> // for sleep | 
|  | 21 | +#endif | 
|  | 22 | + | 
|  | 23 | +#define BUFFER_SIZE 4096 | 
|  | 24 | + | 
|  | 25 | +static juice_agent_t *localAgent; | 
|  | 26 | +static juice_agent_t *remoteAgent; | 
|  | 27 | +static bool success; | 
|  | 28 | + | 
|  | 29 | +void unhandled_stun_callback (const juice_mux_binding_request_t *info, void *user_ptr) { | 
|  | 30 | +	success = true; | 
|  | 31 | +} | 
|  | 32 | + | 
|  | 33 | +int test_stun_unhandled() { | 
|  | 34 | +	juice_set_log_level(JUICE_LOG_LEVEL_DEBUG); | 
|  | 35 | + | 
|  | 36 | +	uint16_t port = 60001; | 
|  | 37 | + | 
|  | 38 | +	// Set up callback | 
|  | 39 | +	juice_mux_listen("127.0.0.1", port, &unhandled_stun_callback, NULL); | 
|  | 40 | + | 
|  | 41 | +	// Create local agent | 
|  | 42 | +	juice_config_t localConfig; | 
|  | 43 | +	memset(&localConfig, 0, sizeof(localConfig)); | 
|  | 44 | +	localConfig.concurrency_mode = JUICE_CONCURRENCY_MODE_MUX; | 
|  | 45 | +	localConfig.local_port_range_begin = port; | 
|  | 46 | +	localConfig.local_port_range_end = port; | 
|  | 47 | +	localAgent = juice_create(&localConfig); | 
|  | 48 | + | 
|  | 49 | +	// Gather local candidates | 
|  | 50 | +	juice_gather_candidates(localAgent); | 
|  | 51 | + | 
|  | 52 | +	// Generate local description | 
|  | 53 | +	char localSdp[JUICE_MAX_SDP_STRING_LEN]; | 
|  | 54 | +	juice_get_local_description(localAgent, localSdp, JUICE_MAX_SDP_STRING_LEN); | 
|  | 55 | +	printf("Local description 1:\n%s\n", localSdp); | 
|  | 56 | + | 
|  | 57 | +	// Destroy local agent | 
|  | 58 | +	juice_destroy(localAgent); | 
|  | 59 | + | 
|  | 60 | +	// Create remote agent | 
|  | 61 | +	juice_config_t remoteConfig; | 
|  | 62 | +	memset(&remoteConfig, 0, sizeof(remoteConfig)); | 
|  | 63 | +	localConfig.concurrency_mode = JUICE_CONCURRENCY_MODE_MUX; | 
|  | 64 | +	remoteAgent = juice_create(&remoteConfig); | 
|  | 65 | + | 
|  | 66 | +	// Remote agent: Receive description from local agent | 
|  | 67 | +	juice_set_remote_description(remoteAgent, localSdp); | 
|  | 68 | + | 
|  | 69 | +	// Remote agent: Gather candidates (and send them to local agent) | 
|  | 70 | +	juice_gather_candidates(remoteAgent); | 
|  | 71 | +	sleep(2); | 
|  | 72 | + | 
|  | 73 | +	// -- Should have received unhandled STUN packet(s) -- | 
|  | 74 | + | 
|  | 75 | +	// Destroy remote agent | 
|  | 76 | +	juice_destroy(remoteAgent); | 
|  | 77 | + | 
|  | 78 | +	if (success) { | 
|  | 79 | +		printf("Success\n"); | 
|  | 80 | +		return 0; | 
|  | 81 | +	} else { | 
|  | 82 | +		printf("Failure\n"); | 
|  | 83 | +		return -1; | 
|  | 84 | +	} | 
|  | 85 | +} | 
0 commit comments