#automotive #ecu #client-server #doip #do-ip

doip-tokio

Client and server implementation for DoIP (diagnostic communication over IP), used in the automotive domain

1 unstable release

0.1.0 Jul 26, 2024

#6 in #ecu

MIT license

32KB
608 lines

DoIP - ISO13400-2 is a protocol for diagnostic communication over IP, used in the automotive domain.

DoIP only handles the transmission of diagnostic packets. The actual diagnostic messages is encoded using the UDS (Unified Diagnostic Services) protocol, specified in ISO14229.

Client example

use std::net::Ipv4Addr;
use doip::{ActivationType, RoutingActivationResponseCode};
use doip_tokio::{DoIpClient, DoIpTokioError};
use futures::StreamExt;
use openssl::{
    pkey::PKey,
    ssl::{SslConnector, SslMethod},
    x509::X509,
};
use tls_api::TlsConnectorBuilder;
use tls_api_openssl::TlsConnector;

#[tokio::main]
async fn main() -> anyhow::Result<()> {
    // Configure TLS setup
    const CERT_PEM: &[u8] =  b"-----BEGIN CERTIFICATE-----...";
    const CERT_PRIVATE_KEY: &[u8] = b"-----BEGIN RSA PRIVATE KEY-----...";

    let x509 = X509::from_pem(CERT_PEM)?;
    let pkey = PKey::private_key_from_pem(CERT_PRIVATE_KEY)?;

    let mut builder = SslConnector::builder(SslMethod::tls())?;

    builder.set_cipher_list(doip::TLS_V1_2_CIPHER_SUITES_OPENSSL)?;
    // TLSv1.3
    builder.set_ciphersuites(doip::TLS_V1_3_CIPHER_SUITES)?;
    builder.set_private_key(&pkey)?;
    builder.cert_store_mut().add_cert(x509)?;
    let tls_connector = tls_api_openssl::TlsConnectorBuilder {
        builder,
        verify_hostname: false,
    }
    .build()?;

    let addr = Ipv4Addr::new(127, 0, 0, 1);
    let mut client = DoIpClient::connect(addr, 0x0E80, tls_connector, "localhost").await?;

    let response = client
        .routing_activation([0x00, 0x00], ActivationType::Default)
        .await?;
    Ok(())
}

Dependencies

~5–15MB
~193K SLoC