Skip to content

Commit 3e56f52

Browse files
committed
merge: 'raw_bindings' into 'main'
Raw bindings to TinyDTLS See merge request namib-master/libraries/tinydtls-rs!1
2 parents 7336542 + 4802fe6 commit 3e56f52

File tree

13 files changed

+356
-2
lines changed

13 files changed

+356
-2
lines changed

.gitignore

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
# Generated by Cargo
2+
# will have compiled files and executables
3+
debug/
4+
target/
5+
6+
# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
7+
# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html
8+
Cargo.lock
9+
10+
# These are backup files generated by rustfmt
11+
**/*.rs.bk
12+
13+
# MSVC Windows builds of rustc generate these, which store debugging information
14+
*.pdb
15+
16+
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio, WebStorm and Rider
17+
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
18+
19+
# User-specific stuff
20+
.idea/**/workspace.xml
21+
.idea/**/tasks.xml
22+
.idea/**/usage.statistics.xml
23+
.idea/**/dictionaries
24+
.idea/**/shelf
25+
26+
# AWS User-specific
27+
.idea/**/aws.xml
28+
29+
# Generated files
30+
.idea/**/contentModel.xml
31+
32+
# Sensitive or high-churn files
33+
.idea/**/dataSources/
34+
.idea/**/dataSources.ids
35+
.idea/**/dataSources.local.xml
36+
.idea/**/sqlDataSources.xml
37+
.idea/**/dynamic.xml
38+
.idea/**/uiDesigner.xml
39+
.idea/**/dbnavigator.xml
40+
41+
# Gradle
42+
.idea/**/gradle.xml
43+
.idea/**/libraries
44+
45+
# Gradle and Maven with auto-import
46+
# When using Gradle or Maven with auto-import, you should exclude module files,
47+
# since they will be recreated, and may cause churn. Uncomment if using
48+
# auto-import.
49+
# .idea/artifacts
50+
# .idea/compiler.xml
51+
# .idea/jarRepositories.xml
52+
# .idea/modules.xml
53+
# .idea/*.iml
54+
# .idea/modules
55+
# *.iml
56+
# *.ipr
57+
58+
# CMake
59+
cmake-build-*/
60+
61+
# Mongo Explorer plugin
62+
.idea/**/mongoSettings.xml
63+
64+
# File-based project format
65+
*.iws
66+
67+
# IntelliJ
68+
out/
69+
70+
# mpeltonen/sbt-idea plugin
71+
.idea_modules/
72+
73+
# JIRA plugin
74+
atlassian-ide-plugin.xml
75+
76+
# Cursive Clojure plugin
77+
.idea/replstate.xml
78+
79+
# Crashlytics plugin (for Android Studio and IntelliJ)
80+
com_crashlytics_export_strings.xml
81+
crashlytics.properties
82+
crashlytics-build.properties
83+
fabric.properties
84+
85+
# Editor-based Rest Client
86+
.idea/httpRequests
87+
88+
# Android studio 3.1+ serialized cache file
89+
.idea/caches/build_file_checksums.ser

.gitmodules

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[submodule "tinydtls-sys/src/tinydtls"]
2+
path = tinydtls-sys/src/tinydtls
3+
url = https://github.com/eclipse/tinydtls.git
4+
branch = develop

Cargo.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# SPDX-License-Identifier: EPL-1.0 OR BSD-3-CLAUSE
2+
3+
[workspace]
4+
members = [
5+
"tinydtls-sys",
6+
]

LICENSE

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ which accompany this distribution.
66
The Eclipse Public License is also available at http://www.eclipse.org/legal/epl-v10.html
77
and the 3-Clause BSD License is also available at https://opensource.org/licenses/BSD-3-Clause.
88

9+
The underlying TinyDTLS library also includes third-party code for which different licensing terms might apply.
10+
See tinydtls-sys/src/tinydtls/ABOUT.md and tinydtls-sys/src/tinydtls/LICENSE for more information.
11+
912
3-Clause BSD License
1013

1114
Copyright 2021 The NAMIB project contributors

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ Matching the license of the tinydtls C library, this library is made available b
88
the terms of the Eclipse Public License v1.0 and 3-Clause BSD License (which the
99
Eclipse Distribution License v1.0 that is used for tinydtls is based on).
1010

11-
You may choose at your own option which of the two licenses you wish to use. This also
12-
applies to the tinydtls C library, see https://www.eclipse.org/legal/eplfaq.php#DUALLIC
11+
You may choose at your own option which of the two licenses you wish to use for the Rust
12+
crate code and the TinyDTLS C library itself, see https://www.eclipse.org/legal/eplfaq.php#DUALLIC.
1313

1414
Additionally, the tinydtls C library contains third party code that might be included
1515
in compiled binaries that link to tinydtls.

rustfmt.toml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
unstable_features = true
2+
3+
edition = "2018"
4+
5+
# Maximum width of each line
6+
max_width = 120
7+
8+
# Use field initialize shorthand if possible.
9+
use_field_init_shorthand = true
10+
11+
# Put a trailing comma after a block based match arm (non-block arms are not affected)
12+
match_block_trailing_comma = true
13+
14+
# Error if unable to get all lines within max_width
15+
error_on_line_overflow = false
16+
17+
# Merge multiple imports into a single nested import.
18+
imports_granularity = "Crate"
19+
20+
reorder_imports = true
21+
22+
group_imports = "StdExternalCrate"
23+
24+
# Reorder impl items. type and const are put first, then macros and methods.
25+
reorder_impl_items = true
26+
27+
# Determines if + or = are wrapped in spaces in the punctuation of types
28+
type_punctuation_density = "Compressed"

tinydtls-sys/Cargo.toml

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Copyright (c) 2021 The NAMIB Project Developers, all rights reserved.
2+
# See the README as well as the LICENSE file for more information.
3+
# SPDX-License-Identifier: EPL-1.0 OR BSD-3-CLAUSE
4+
[package]
5+
name = "tinydtls-sys"
6+
description = "Raw bindings to the TinyDTLS library."
7+
version = "0.1.0-alpha.1+tinydtls-c8d2def"
8+
edition = "2021"
9+
links = "tinydtls"
10+
# For tinydtls, both licenses can be applied, see https://www.eclipse.org/legal/eplfaq.php#DUALLIC
11+
# BSD-3-CLAUSE is equivalent to the EDL v1.0, see https://www.eclipse.org/org/documents/edl-v10.php
12+
# First bracket is the license for TinyDTLS, the remainder is for code included with tinydtls.
13+
license = "(EPL-1.0 OR BSD-3-CLAUSE) AND BSD-1-Clause AND BSD-3-CLAUSE AND MIT"
14+
readme = "README.md"
15+
homepage = "https://namib.me/"
16+
authors = ["Hugo Hakim Damer <[email protected]>"]
17+
categories = ["external-ffi-bindings", "network-programming", "cryptography", "embedded"]
18+
keywords = ["tinydtls", "sys", "dtls", "crypto"]
19+
exclude = ['/src/tinydtls/share/', '/src/tinydtls/include/', '/src/tinydtls/configure.prev']
20+
21+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
22+
[features]
23+
default = ["vendored"]
24+
# Use a vendored version of tinydtls
25+
vendored = ["static"]
26+
# Attempt static linking to tinydtls
27+
static = []
28+
29+
# Enable ECC functionality in vendored library
30+
ecc = []
31+
# Enable PSK functionality in vendored library
32+
psk = []
33+
34+
[dependencies]
35+
libc = "^0.2"
36+
37+
[build-dependencies]
38+
bindgen = "0.59.2"
39+
autotools = "0.2.3"
40+
fs_extra = "1.2.0"

tinydtls-sys/LICENSE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../LICENSE

tinydtls-sys/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../README.md

tinydtls-sys/build.rs

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
// SPDX-License-Identifier: EPL-1.0 OR BSD-3-CLAUSE
2+
/*
3+
* build.rs - build script for TinyDTLS Rust bindings.
4+
* Copyright (c) 2021 The NAMIB Project Developers, all rights reserved.
5+
* See the README as well as the LICENSE file for more information.
6+
*/
7+
use std::{
8+
env,
9+
path::{Path, PathBuf},
10+
process::Command,
11+
};
12+
13+
use bindgen::EnumVariation;
14+
15+
fn main() {
16+
println!("cargo:rerun-if-changed=src/tinydtls/");
17+
println!("cargo:rerun-if-changed=tinydtls_wrapper.h");
18+
println!("cargo:rerun-if-changed=build.rs");
19+
let mut bindgen_builder = bindgen::Builder::default();
20+
21+
// Build vendored library if feature was set.
22+
if cfg!(feature = "vendored") {
23+
// Read required environment variables.
24+
let out_dir = std::env::var_os("OUT_DIR").unwrap();
25+
26+
// TinyDTLS does not like being built out of source, but we get verification errors if files
27+
// in the source package are modified.
28+
// Therefore, we copy tinydtls over to the output directory and build from there.
29+
let mut copy_options = fs_extra::dir::CopyOptions::default();
30+
copy_options.overwrite = true;
31+
fs_extra::dir::copy(
32+
Path::new(env!("CARGO_MANIFEST_DIR")).join("src").join("tinydtls"),
33+
&out_dir,
34+
&copy_options,
35+
)
36+
.unwrap();
37+
let tinydtls_src_dir = Path::new(&out_dir).join("tinydtls");
38+
39+
// Read Makeflags into vector of strings
40+
let make_flags = std::env::var_os("CARGO_MAKEFLAGS")
41+
.unwrap()
42+
.into_string()
43+
.unwrap()
44+
.split(" ")
45+
.map(String::from)
46+
.collect();
47+
48+
// Run autogen to generate necessary build files.
49+
Command::new(tinydtls_src_dir.join("autogen.sh"))
50+
.current_dir(&tinydtls_src_dir)
51+
.status()
52+
.unwrap();
53+
54+
// Run make clean
55+
autotools::Config::new(&tinydtls_src_dir)
56+
.insource(true)
57+
.out_dir(&out_dir)
58+
.make_target("clean")
59+
.build();
60+
61+
// Create build configuration instance and enable in-source builds.
62+
let mut build_config = autotools::Config::new(&tinydtls_src_dir);
63+
build_config.insource(true).out_dir(&out_dir);
64+
65+
// Set Makeflags
66+
build_config.make_args(make_flags);
67+
68+
// Enable debug symbols if enabled in Rust.
69+
match std::env::var_os("DEBUG").unwrap().to_str().unwrap() {
70+
"0" | "false" => {},
71+
_ => {
72+
build_config.with("debug", None);
73+
},
74+
}
75+
76+
// Enable dependency features based on selected cargo features.
77+
build_config
78+
.enable("ecc", Some(if cfg!(feature = "ecc") { "yes" } else { "no" }))
79+
.enable("psk", Some(if cfg!(feature = "psk") { "yes" } else { "no" }));
80+
81+
// Run build
82+
let dst = build_config.build();
83+
84+
// Add the built library to the search path
85+
println!("cargo:rustc-link-search=native={}", dst.join("lib").to_str().unwrap());
86+
// Set some values that can be used by other crates that have to interact with the C library
87+
// directly, see https://doc.rust-lang.org/cargo/reference/build-scripts.html#the-links-manifest-key
88+
// for more info.
89+
println!("cargo:include={}", dst.join("include").to_str().unwrap());
90+
println!("cargo:libs={}", dst.to_str().unwrap());
91+
92+
// Tell bindgen to look for the right header files.
93+
bindgen_builder = bindgen_builder
94+
.clang_arg(format!("-I{}", dst.join("include").join("tinydtls").to_str().unwrap()))
95+
.clang_arg(format!("-I{}", dst.join("include").to_str().unwrap()));
96+
}
97+
98+
// Instruct cargo to link to the TinyDTLS C library, either statically or dynamically.
99+
println!(
100+
"cargo:rustc-link-lib={}tinydtls",
101+
cfg!(feature = "static").then(|| "static=").unwrap_or("")
102+
);
103+
104+
// Customize and configure generated bindings.
105+
bindgen_builder = bindgen_builder
106+
.header("src/wrapper.h")
107+
.parse_callbacks(Box::new(bindgen::CargoCallbacks))
108+
.default_enum_style(EnumVariation::Rust { non_exhaustive: true })
109+
.rustfmt_bindings(false)
110+
// Declarations that should be part of the bindings.
111+
.allowlist_function("dtls_.*")
112+
.allowlist_type("dtls_.*")
113+
.allowlist_var("dtls_.*")
114+
.allowlist_function("DTLS_.*")
115+
.allowlist_type("DTLS_.*")
116+
.allowlist_var("DTLS_.*")
117+
.allowlist_type("seqnum_t")
118+
.allowlist_type("__attribute__")
119+
.allowlist_type("clock_time_t")
120+
.allowlist_var("CLOCK_SECOND")
121+
.allowlist_var("TLS_.*")
122+
.allowlist_var("DTLSv12")
123+
.allowlist_function("memxor")
124+
.allowlist_function("equals")
125+
.allowlist_var("WITH_.*")
126+
.allowlist_type("WITH_.*")
127+
.allowlist_function("WITH_.*")
128+
.allowlist_var("PACKAGE_.*")
129+
.allowlist_type("PACKAGE_.*")
130+
.allowlist_function("PACKAGE_.*")
131+
.allowlist_function("netq_.*")
132+
.allowlist_type("netq_.*")
133+
.allowlist_var("netq_.*")
134+
.allowlist_function("NETQ_.*")
135+
.allowlist_type("NETQ_.*")
136+
.allowlist_var("NETQ_.*")
137+
.allowlist_type("session_t")
138+
// We use the definitions made by the libc crate instead
139+
.blocklist_type("sockaddr(_in|_in6|_storage)?")
140+
.blocklist_type("in6?_(addr|port)(_t)?")
141+
.blocklist_type("in6_addr__bindgen_ty_1")
142+
.blocklist_type("(__)?socklen_t")
143+
.blocklist_type("sa_family_t")
144+
.blocklist_type("__fd_mask")
145+
// size_t matches usize in our case here.
146+
.size_t_is_usize(true);
147+
148+
// Run binding generation and write the output to a file.
149+
let bindings = bindgen_builder.generate().expect("Could not generate bindings!");
150+
let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());
151+
bindings.write_to_file(out_path.join("bindings.rs")).unwrap();
152+
}

0 commit comments

Comments
 (0)