fn match_result<'a>(res:IResult<&[u8],TlsPlaintext<'a>>) -> Vec<Value> {

	let mut messages = Vec::new();

	match res {
		// rem is the remaining data (not parsed)
		// record is an object of type TlsRecord
		IResult::Done(rem,record) => {

			for msg in record.msg {
				match msg {
					TlsMessage::ChangeCipherSpec => {
						let change_cipher_json = json!({
								"type": "ChangeCipherSpec"
						});
						messages.push(change_cipher_json);
					}
					TlsMessage::Alert(_) => {
                        let json = json!({"type": "Alert"});
                        messages.push(json);
                    }
					TlsMessage::Handshake(handshake) => {
						match handshake {
							TlsMessageHandshake::HelloRequest => {
								let json = json!({"type": "HelloRequest"});
								messages.push(json);
							}
							TlsMessageHandshake::EndOfEarlyData => {
								let json = json!({"type": "EndOfEarlyData"});
								messages.push(json);
							}
							TlsMessageHandshake::ClientHello(client_hello) => {
								let extensions = match client_hello.ext {
									Some(x) => match_extensions(parse_tls_extensions(x)),
								    None    => json!({}) //Ignore
								};

								let hello_json = json!({
									"type" : "ClientHello",
									"version": client_hello.version,
									"ciphers": client_hello.ciphers,
                                    "compressions": client_hello.comp,
									"ext": extensions,
								});

								messages.push(hello_json);
							},
							TlsMessageHandshake::ServerHello(server_hello) => {
								let extensions = match server_hello.ext {
									Some(x) => match_extensions(parse_tls_extensions(x)),
								    None    => json!({}) //Ignore
								};

								let hello_json = json!({
									"type": "ServerHello",
									"version": server_hello.version,
									"cipher": server_hello.cipher,
                                    "compression": server_hello.compression,
									"ext": extensions
								});

								messages.push(hello_json);
							}
							TlsMessageHandshake::ServerHelloV13(_) => {
								let json = json!({"type": "ServerHelloV13"});
								messages.push(json);
							}
							TlsMessageHandshake::NewSessionTicket(_) => {
								let json = json!({"type": "NewSessionTicket"});
								messages.push(json);
							}
							TlsMessageHandshake::HelloRetry(_) => {
								let json = json!({"type": "HelloRetry"});
								messages.push(json);
							}
							TlsMessageHandshake::Certificate(cert) => {
								let cert_json = json!({
										"type": "Certificate",
										"cert_chain": cert.cert_chain.iter().map(|x| encode(x.data)).collect::<Vec<String>>()
								});
								messages.push(cert_json);
							}
							TlsMessageHandshake::ServerKeyExchange(_) => {
								let json = json!({"type": "ServerKeyExchange"});
								messages.push(json);
							}
							TlsMessageHandshake::CertificateRequest(_) =>  {
								let json = json!({"type": "CertificateRequest"});
								messages.push(json);
							}
							TlsMessageHandshake::ServerDone(_) => {
								let done_json = json!({
										"type": "ServerDone"
								});
								messages.push(done_json);
							}
							TlsMessageHandshake::CertificateVerify(_) => {
								let json = json!({"type": "CertificateVerify"});
								messages.push(json);
							}
							TlsMessageHandshake::ClientKeyExchange(_) => {
								let json = json!({"type": "ClientKeyExchange"});
								messages.push(json);
							}
							TlsMessageHandshake::Finished(_) => {
								let json = json!({"type": "Finished"});
								messages.push(json);
							}
							TlsMessageHandshake::CertificateStatus(_) => {
								let json = json!({"type": "CertificateStatus"});
								messages.push(json);
							}
							TlsMessageHandshake::NextProtocol(_) => {
								let json = json!({"type": "NextProtocol"});
								messages.push(json);
							}
							TlsMessageHandshake::KeyUpdate(_) => {
								let json = json!({"type": "KeyUpdate"});
								messages.push(json);
							}

						}

					},
					TlsMessage::ApplicationData(_) => {
                        let json = json!({"type": "ApplicationData"});
                        messages.push(json);
                    }
					TlsMessage::Heartbeat(_) => {
                        let json = json!({"type": "Heartbeat"});
                        messages.push(json);
                    }
				};

				// match remaining bytes
				let mut res2 = match_result(parse_tls_plaintext(&rem));
				messages.append(&mut res2);

			}
		},
		IResult::Incomplete(_) => {
            let mut stderr = std::io::stderr();
            writeln!(&mut stderr, "Defragmentation required (TLS record)").unwrap();
		},
		IResult::Error(e) => {
            let mut stderr = std::io::stderr();
            writeln!(&mut stderr, "parse_tls_record_with_header failed: {:?}",e).unwrap();
		}

	}

	messages
}
//recursive approach to parse all bytes
recursive approach to parse all bytes
recursive approach to parse all bytes
fn match_result<'a>(res:IResult<&[u8],TlsPlaintext<'a>>) -> Vec<Value> {
	let mut messages = Vec::new();
	match res {
		// rem is the remaining data (not parsed)
		// record is an object of type TlsRecord
		IResult::Done(rem,record) => {
			for msg in record.msg {
				match msg {
					TlsMessage::ChangeCipherSpec => {
						let change_cipher_json = json!({
								"type": "ChangeCipherSpec"
						});
						messages.push(change_cipher_json);
					}
					TlsMessage::Alert(_) => {
                        let json = json!({"type": "Alert"});
                        messages.push(json);
                    }
					TlsMessage::Handshake(handshake) => {
						match handshake {
							TlsMessageHandshake::HelloRequest => {
								let json = json!({"type": "HelloRequest"});
								messages.push(json);
							}
							TlsMessageHandshake::EndOfEarlyData => {
								let json = json!({"type": "EndOfEarlyData"});
								messages.push(json);
							}
							TlsMessageHandshake::ClientHello(client_hello) => {
								let extensions = match client_hello.ext {
									Some(x) => match_extensions(parse_tls_extensions(x)),
								    None    => json!({}) //Ignore
								};
								let hello_json = json!({
									"type" : "ClientHello",
									"version": client_hello.version,
									"ciphers": client_hello.ciphers,
                                    "compressions": client_hello.comp,
									"ext": extensions,
								});
								messages.push(hello_json);
							},
							TlsMessageHandshake::ServerHello(server_hello) => {
								let extensions = match server_hello.ext {
									Some(x) => match_extensions(parse_tls_extensions(x)),
								    None    => json!({}) //Ignore
								};
								let hello_json = json!({
									"type": "ServerHello",
									"version": server_hello.version,
									"cipher": server_hello.cipher,
                                    "compression": server_hello.compression,
									"ext": extensions
								});
								messages.push(hello_json);
							}
							TlsMessageHandshake::ServerHelloV13(_) => {
								let json = json!({"type": "ServerHelloV13"});
								messages.push(json);
							}
							TlsMessageHandshake::NewSessionTicket(_) => {
								let json = json!({"type": "NewSessionTicket"});
								messages.push(json);
							}
							TlsMessageHandshake::HelloRetry(_) => {
								let json = json!({"type": "HelloRetry"});
								messages.push(json);
							}
							TlsMessageHandshake::Certificate(cert) => {
								let cert_json = json!({
										"type": "Certificate",
										"cert_chain": cert.cert_chain.iter().map(|x| encode(x.data)).collect::<Vec<String>>()
								});
								messages.push(cert_json);
							}
							TlsMessageHandshake::ServerKeyExchange(_) => {
								let json = json!({"type": "ServerKeyExchange"});
								messages.push(json);
							}
							TlsMessageHandshake::CertificateRequest(_) =>  {
								let json = json!({"type": "CertificateRequest"});
								messages.push(json);
							}
							TlsMessageHandshake::ServerDone(_) => {
								let done_json = json!({
										"type": "ServerDone"
								});
								messages.push(done_json);
							}
							TlsMessageHandshake::CertificateVerify(_) => {
								let json = json!({"type": "CertificateVerify"});
								messages.push(json);
							}
							TlsMessageHandshake::ClientKeyExchange(_) => {
								let json = json!({"type": "ClientKeyExchange"});
								messages.push(json);
							}
							TlsMessageHandshake::Finished(_) => {
								let json = json!({"type": "Finished"});
								messages.push(json);
							}
							TlsMessageHandshake::CertificateStatus(_) => {
								let json = json!({"type": "CertificateStatus"});
								messages.push(json);
							}
							TlsMessageHandshake::NextProtocol(_) => {
								let json = json!({"type": "NextProtocol"});
								messages.push(json);
							}
							TlsMessageHandshake::KeyUpdate(_) => {
								let json = json!({"type": "KeyUpdate"});
								messages.push(json);
							}
						}
					},
					TlsMessage::ApplicationData(_) => {
                        let json = json!({"type": "ApplicationData"});
                        messages.push(json);
                    }
					TlsMessage::Heartbeat(_) => {
                        let json = json!({"type": "Heartbeat"});
                        messages.push(json);
                    }
				};
				// match remaining bytes
				let mut res2 = match_result(parse_tls_plaintext(&rem));
				messages.append(&mut res2);
			}
		},
		IResult::Incomplete(_) => {
            let mut stderr = std::io::stderr();
            writeln!(&mut stderr, "Defragmentation required (TLS record)").unwrap();
		},
		IResult::Error(e) => {
            let mut stderr = std::io::stderr();
            writeln!(&mut stderr, "parse_tls_record_with_header failed: {:?}",e).unwrap();
		}
	}
	messages
}
fn match_result < ' a > ( res : IResult < & [ u8 ] , TlsPlaintext < ' a > > ) -> Vec < Value > { let mut messages = Vec :: new ( ) ; match res { // rem is the remaining data (not parsed) // record is an object of type TlsRecord IResult :: Done ( rem , record ) => { for msg in record . msg { match msg { TlsMessage :: ChangeCipherSpec => { let change_cipher_json = json ! ( { " " " " } ) ; messages . push ( change_cipher_json ) ; } TlsMessage :: Alert ( _ ) => { let json = json ! ( { " " " " } ) ; messages . push ( json ) ; } TlsMessage :: Handshake ( handshake ) => { match handshake { TlsMessageHandshake :: HelloRequest => { let json = json ! ( { " " " " } ) ; messages . push ( json ) ; } TlsMessageHandshake :: EndOfEarlyData => { let json = json ! ( { " " " " } ) ; messages . push ( json ) ; } TlsMessageHandshake :: ClientHello ( client_hello ) => { let extensions = match client_hello . ext { Some ( x ) => match_extensions ( parse_tls_extensions ( x ) ) , None => json ! ( { } ) //Ignore } ; let hello_json = json ! ( { " " " " " " client_hello version " " client_hello ciphers " " client_hello comp " " extensions } ) ; messages . push ( hello_json ) ; } , TlsMessageHandshake :: ServerHello ( server_hello ) => { let extensions = match server_hello . ext { Some ( x ) => match_extensions ( parse_tls_extensions ( x ) ) , None => json ! ( { } ) //Ignore } ; let hello_json = json ! ( { " " " " " " server_hello version " " server_hello cipher " " server_hello compression " " extensions } ) ; messages . push ( hello_json ) ; } TlsMessageHandshake :: ServerHelloV13 ( _ ) => { let json = json ! ( { " " " " } ) ; messages . push ( json ) ; } TlsMessageHandshake :: NewSessionTicket ( _ ) => { let json = json ! ( { " " " " } ) ; messages . push ( json ) ; } TlsMessageHandshake :: HelloRetry ( _ ) => { let json = json ! ( { " " " " } ) ; messages . push ( json ) ; } TlsMessageHandshake :: Certificate ( cert ) => { let cert_json = json ! ( { " " " " " " cert cert_chain iter ( ) map ( x encode ( x data ) ) collect Vec String ( ) } ) ; messages . push ( cert_json ) ; } TlsMessageHandshake :: ServerKeyExchange ( _ ) => { let json = json ! ( { " " " " } ) ; messages . push ( json ) ; } TlsMessageHandshake :: CertificateRequest ( _ ) => { let json = json ! ( { " " " " } ) ; messages . push ( json ) ; } TlsMessageHandshake :: ServerDone ( _ ) => { let done_json = json ! ( { " " " " } ) ; messages . push ( done_json ) ; } TlsMessageHandshake :: CertificateVerify ( _ ) => { let json = json ! ( { " " " " } ) ; messages . push ( json ) ; } TlsMessageHandshake :: ClientKeyExchange ( _ ) => { let json = json ! ( { " " " " } ) ; messages . push ( json ) ; } TlsMessageHandshake :: Finished ( _ ) => { let json = json ! ( { " " " " } ) ; messages . push ( json ) ; } TlsMessageHandshake :: CertificateStatus ( _ ) => { let json = json ! ( { " " " " } ) ; messages . push ( json ) ; } TlsMessageHandshake :: NextProtocol ( _ ) => { let json = json ! ( { " " " " } ) ; messages . push ( json ) ; } TlsMessageHandshake :: KeyUpdate ( _ ) => { let json = json ! ( { " " " " } ) ; messages . push ( json ) ; } } } , TlsMessage :: ApplicationData ( _ ) => { let json = json ! ( { " " " " } ) ; messages . push ( json ) ; } TlsMessage :: Heartbeat ( _ ) => { let json = json ! ( { " " " " } ) ; messages . push ( json ) ; } } ; // match remaining bytes let mut res2 = match_result ( parse_tls_plaintext ( & rem ) ) ; messages . append ( & mut res2 ) ; } } , IResult :: Incomplete ( _ ) => { let mut stderr = std :: io :: stderr ( ) ; writeln ! ( mut stderr " " ) . unwrap ( ) ; } , IResult :: Error ( e ) => { let mut stderr = std :: io :: stderr ( ) ; writeln ! ( mut stderr " " e ) . unwrap ( ) ; } } messages }
recursive approach to parse all bytes
recursive approach to parse all bytes
res
IResult<&[u8],TlsPlaintext<'a>>
res
IResult<&[u8],TlsPlaintext<'a>>