xref: /webrtc/stun/examples/stun_decode.rs (revision 5d8fe953)
1 use clap::{App, Arg};
2 
3 use stun::message::Message;
4 
main()5 fn main() {
6     let mut app = App::new("STUN decode")
7         .version("0.1.0")
8         .author("Jtplouffe <[email protected]>")
9         .about("An example of STUN decode")
10         .arg(
11             Arg::with_name("FULLHELP")
12                 .help("Prints more detailed help information")
13                 .long("fullhelp"),
14         )
15         .arg(
16             Arg::with_name("data")
17                 .required_unless("FULLHELP")
18                 .takes_value(true)
19                 .index(1)
20                 .help("base64 encoded message, e.g. 'AAEAHCESpEJML0JTQWsyVXkwcmGALwAWaHR0cDovL2xvY2FsaG9zdDozMDAwLwAA'"),
21         );
22 
23     let matches = app.clone().get_matches();
24 
25     if matches.is_present("FULLHELP") {
26         app.print_long_help().unwrap();
27         std::process::exit(0);
28     }
29 
30     let encoded_data = matches.value_of("data").unwrap();
31     let decoded_data = match base64::decode(encoded_data) {
32         Ok(d) => d,
33         Err(e) => panic!("Unable to decode base64 value: {e}"),
34     };
35 
36     let mut message = Message::new();
37     message.raw = decoded_data;
38 
39     match message.decode() {
40         Ok(_) => println!("{message}"),
41         Err(e) => panic!("Unable to decode message: {e}"),
42     }
43 }
44