Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Auto generate rust bindings #4696

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@ include = [
"/THIRD-PARTY-NOTICES",
]

[workspace]
members = [
".",
"crates/tools/api",
"crates/libs/msquic-rs"
]

[build-dependencies]
cmake = "0.1"

Expand Down
1 change: 1 addition & 0 deletions crates/.metadata/MsQuic.Win32.c
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#include "MsQuic.Win32.h"
42 changes: 42 additions & 0 deletions crates/.metadata/MsQuic.Win32.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#include "winerror.h"
#define QUIC_TLS_ALERT_HRESULT_PREFIX _HRESULT_TYPEDEF_(0x80410100L)

typedef enum {
QUIC_STATUS_SUCCESS = 0x0, // S_OK
QUIC_STATUS_PENDING = 0x703e5, // SUCCESS_HRESULT_FROM_WIN32(ERROR_IO_PENDING)
QUIC_STATUS_CONTINUE = 0x704de, // SUCCESS_HRESULT_FROM_WIN32(ERROR_CONTINUE)
QUIC_STATUS_OUT_OF_MEMORY = 0x8007000e, // E_OUTOFMEMORY
QUIC_STATUS_INVALID_PARAMETER = 0x80070057, // E_INVALIDARG
QUIC_STATUS_INVALID_STATE = 0x8007139f, // E_NOT_VALID_STATE
QUIC_STATUS_NOT_SUPPORTED = 0x80004002, // E_NOINTERFACE
QUIC_STATUS_NOT_FOUND = 0x80070490, // HRESULT_FROM_WIN32(ERROR_NOT_FOUND)
QUIC_STATUS_BUFFER_TOO_SMALL = 0x8007007a, // E_NOT_SUFFICIENT_BUFFER
QUIC_STATUS_HANDSHAKE_FAILURE = 0x80410000, // ERROR_QUIC_HANDSHAKE_FAILURE
QUIC_STATUS_ABORTED = 0x80004004, // E_ABORT
QUIC_STATUS_ADDRESS_IN_USE = 0x80072740, // HRESULT_FROM_WIN32(WSAEADDRINUSE)
QUIC_STATUS_INVALID_ADDRESS = 0x80072741, // HRESULT_FROM_WIN32(WSAEADDRNOTAVAIL)
QUIC_STATUS_CONNECTION_TIMEOUT = 0x80410006, // ERROR_QUIC_CONNECTION_TIMEOUT
QUIC_STATUS_CONNECTION_IDLE = 0x80410005, // ERROR_QUIC_CONNECTION_IDLE
QUIC_STATUS_UNREACHABLE = 0x800704d0, // HRESULT_FROM_WIN32(ERROR_HOST_UNREACHABLE)
QUIC_STATUS_INTERNAL_ERROR = 0x80410003, // ERROR_QUIC_INTERNAL_ERROR
QUIC_STATUS_CONNECTION_REFUSED = 0x800704c9, // HRESULT_FROM_WIN32(ERROR_CONNECTION_REFUSED)
QUIC_STATUS_PROTOCOL_ERROR = 0x80410004, // ERROR_QUIC_PROTOCOL_VIOLATION
QUIC_STATUS_VER_NEG_ERROR = 0x80410001, // ERROR_QUIC_VER_NEG_FAILURE
QUIC_STATUS_TLS_ERROR = 0x80072b18, // HRESULT_FROM_WIN32(WSA_SECURE_HOST_NOT_FOUND)
QUIC_STATUS_USER_CANCELED = 0x80410002, // ERROR_QUIC_USER_CANCELED
QUIC_STATUS_ALPN_NEG_FAILURE = 0x80410007, // ERROR_QUIC_ALPN_NEG_FAILURE
QUIC_STATUS_STREAM_LIMIT_REACHED = 0x80410008, // ERROR_QUIC_STREAM_LIMIT_REACHED
QUIC_STATUS_ALPN_IN_USE = 0x80410009, // ERROR_QUIC_ALPN_IN_USE

QUIC_STATUS_CLOSE_NOTIFY = QUIC_TLS_ALERT_HRESULT_PREFIX | (0xff & 0), // QUIC_STATUS_TLS_ALERT(0) - Close notify
QUIC_STATUS_BAD_CERTIFICATE = QUIC_TLS_ALERT_HRESULT_PREFIX | (0xff & 42), // QUIC_STATUS_TLS_ALERT(42) - Bad Certificate
QUIC_STATUS_UNSUPPORTED_CERTIFICATE = QUIC_TLS_ALERT_HRESULT_PREFIX | (0xff & 43), // QUIC_STATUS_TLS_ALERT(43) - Unsupported Certificate
QUIC_STATUS_REVOKED_CERTIFICATE = QUIC_TLS_ALERT_HRESULT_PREFIX | (0xff & 44), // QUIC_STATUS_TLS_ALERT(44) - Revoked Certificate
QUIC_STATUS_EXPIRED_CERTIFICATE = QUIC_TLS_ALERT_HRESULT_PREFIX | (0xff & 45), // QUIC_STATUS_TLS_ALERT(45) - Expired Certificate
QUIC_STATUS_UNKNOWN_CERTIFICATE = QUIC_TLS_ALERT_HRESULT_PREFIX | (0xff & 46), // QUIC_STATUS_TLS_ALERT(46) - Unknown Certificate
QUIC_STATUS_REQUIRED_CERTIFICATE = QUIC_TLS_ALERT_HRESULT_PREFIX | (0xff & 116), // QUIC_STATUS_TLS_ALERT(116) - Required Certificate

QUIC_STATUS_CERT_EXPIRED = 0xBEBC401, // CERT_E_EXPIRED
QUIC_STATUS_CERT_UNTRUSTED_ROOT = 0xBEBC402, // CERT_E_UNTRUSTEDROOT
QUIC_STATUS_CERT_NO_CERT = 0xBEBC403 // SEC_E_NO_CREDENTIALS
} QUIC_STATUS;
7 changes: 7 additions & 0 deletions crates/.metadata/MsQuic.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// force the return type to be long (for linux to work as well)
#define _HRESULT_DEFINED
#define HRESULT long
// TODO: typedef does not work. maybe scraper has special case for hresult
// typedef long QSTATUS;
// typedef QSTATUS HRESULT;
#include "msquic.h"
15 changes: 15 additions & 0 deletions crates/.metadata/autoTypes.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[
{
"Namespace": "Microsoft.MsQuic",
"Name": "QUIC_HANDLE",
"ValueType": "typedef struct QUIC_HANDLE",
"NativeTypedef": true
},
{
"Namespace": "Microsoft.MsQuic",
"Name": "HQUIC",
"ValueType": "typedef struct QUIC_HANDLE *",
"InvalidHandleValues": [ 0 ],
"NativeTypedef": true
}
]
37 changes: 37 additions & 0 deletions crates/.metadata/generate.proj
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?xml version="1.0" encoding="utf-8"?>
<Project Sdk="Microsoft.Windows.WinmdGenerator/0.56.13-preview">
<PropertyGroup Label="Globals">
<OutputWinmd>../.windows/winmd/Microsoft.MsQuic.winmd</OutputWinmd>
<WinmdVersion>255.255.255.255</WinmdVersion>
<ExcludeFromCrossarch>true</ExcludeFromCrossarch>
<ScanArch>x64</ScanArch>
</PropertyGroup>
<ItemGroup>
<ScraperRsp Include="scraper.settings.rsp"/>
<ConstantsScraperRsp Include="ConstantsScraper.settings.rsp"/>
</ItemGroup>
<ItemGroup>
<ImportLibs Include="..\..\artifacts\bin\windows\x64_Debug_schannel\msquic.lib" />
<Partition Include="MsQuic.c">
<TraverseFiles>..\..\src\inc\msquic.h;</TraverseFiles>
<Namespace>Microsoft.MsQuic</Namespace>
</Partition>
<Partition Include="MsQuic.Win32.c">
<TraverseFiles>MsQuic.Win32.h</TraverseFiles>
<Namespace>Microsoft.MsQuic.Win32</Namespace>
</Partition>
<Partition Include="./linux/MsQuic.Linux.c">
<TraverseFiles>./linux/MsQuic.Linux.h</TraverseFiles>
<Namespace>Microsoft.MsQuic.Linux</Namespace>
</Partition>
<Partition Include="./macos/MsQuic.MacOs.c">
<TraverseFiles>./macos/MsQuic.MacOs.h</TraverseFiles>
<Namespace>Microsoft.MsQuic.MacOs</Namespace>
</Partition>
</ItemGroup>
<Target Name="CopyWin32Metadata" AfterTargets="Build">
<Copy
SourceFiles="$(PkgMicrosoft_Windows_SDK_Win32Metadata)\Windows.Win32.winmd"
DestinationFiles="..\.windows\winmd\Windows.Win32.winmd" />
</Target>
</Project>
1 change: 1 addition & 0 deletions crates/.metadata/linux/MsQuic.Linux.c
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#include "MsQuic.Linux.h"
44 changes: 44 additions & 0 deletions crates/.metadata/linux/MsQuic.Linux.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#define ERROR_BASE 200000000 // 0xBEBC200
#define TLS_ERROR_BASE 256 + ERROR_BASE // 0xBEBC300
#define CERT_ERROR_BASE 512 + ERROR_BASE // 0xBEBC400

typedef enum {
QUIC_STATUS_SUCCESS = 0, // 0
QUIC_STATUS_PENDING = -2, // -2
QUIC_STATUS_CONTINUE = -1, // -1
QUIC_STATUS_OUT_OF_MEMORY = 12, // 12
QUIC_STATUS_INVALID_PARAMETER = 22, // 22
QUIC_STATUS_INVALID_STATE = 1, // 1
QUIC_STATUS_NOT_SUPPORTED = 95, // 95 (102 on macOS)
QUIC_STATUS_NOT_FOUND = 2, // 2
QUIC_STATUS_BUFFER_TOO_SMALL = 75, // 75 (84 on macOS)
QUIC_STATUS_HANDSHAKE_FAILURE = 103, // 103 (53 on macOS)
QUIC_STATUS_ABORTED = 125, // 125 (89 on macOS)
QUIC_STATUS_ADDRESS_IN_USE = 98, // 98 (48 on macOS)
QUIC_STATUS_INVALID_ADDRESS = 97, // 97 (47 on macOS)
QUIC_STATUS_CONNECTION_TIMEOUT = 110, // 110 (60 on macOS)
QUIC_STATUS_CONNECTION_IDLE = 62, // 62 (101 on macOS)
QUIC_STATUS_INTERNAL_ERROR = 5, // 5
QUIC_STATUS_CONNECTION_REFUSED = 111, // 111 (61 on macOS)
QUIC_STATUS_PROTOCOL_ERROR = 71, // 71 (100 on macOS)
QUIC_STATUS_VER_NEG_ERROR = 93, // 93 (43 on macOS)
QUIC_STATUS_UNREACHABLE = 113, // 113 (65 on macOS)
QUIC_STATUS_TLS_ERROR = 126, // 126
QUIC_STATUS_USER_CANCELED = 130, // 130 (105 on macOS)
QUIC_STATUS_ALPN_NEG_FAILURE = 92, // 92 (42 on macOS)
QUIC_STATUS_STREAM_LIMIT_REACHED = 86, // 86
QUIC_STATUS_ALPN_IN_USE = 91, // 91 (41 on macOS)
QUIC_STATUS_ADDRESS_NOT_AVAILABLE = 99, // 99 (47 on macOS)

QUIC_STATUS_CLOSE_NOTIFY = (0xff & 0) + TLS_ERROR_BASE, // 0xBEBC300 - Close notify
QUIC_STATUS_BAD_CERTIFICATE = (0xff & 42) + TLS_ERROR_BASE, // 0xBEBC32A - Bad Certificate
QUIC_STATUS_UNSUPPORTED_CERTIFICATE = (0xff & 43) + TLS_ERROR_BASE, // 0xBEBC32B - Unsupported Certificate
QUIC_STATUS_REVOKED_CERTIFICATE = (0xff & 44) + TLS_ERROR_BASE, // 0xBEBC32C - Revoked Certificate
QUIC_STATUS_EXPIRED_CERTIFICATE = (0xff & 45) + TLS_ERROR_BASE, // 0xBEBC32D - Expired Certificate
QUIC_STATUS_UNKNOWN_CERTIFICATE = (0xff & 46) + TLS_ERROR_BASE, // 0xBEBC32E - Unknown Certificate
QUIC_STATUS_REQUIRED_CERTIFICATE = (0xff & 116) + TLS_ERROR_BASE, // 0xBEBC374 - Required Certificate

QUIC_STATUS_CERT_EXPIRED = 1 + CERT_ERROR_BASE, // 0xBEBC401
QUIC_STATUS_CERT_UNTRUSTED_ROOT = 2 + CERT_ERROR_BASE, // 0xBEBC402
QUIC_STATUS_CERT_NO_CERT = 3 + CERT_ERROR_BASE, // 0xBEBC403
} QUIC_STATUS;
1 change: 1 addition & 0 deletions crates/.metadata/macos/MsQuic.MacOs.c
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#include "MsQuic.MacOs.h"
44 changes: 44 additions & 0 deletions crates/.metadata/macos/MsQuic.MacOs.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#define ERROR_BASE 200000000 // 0xBEBC200
#define TLS_ERROR_BASE 256 + ERROR_BASE // 0xBEBC300
#define CERT_ERROR_BASE 512 + ERROR_BASE // 0xBEBC400

typedef enum {
QUIC_STATUS_SUCCESS = 0, // 0
QUIC_STATUS_PENDING = -2, // -2
QUIC_STATUS_CONTINUE = -1, // -1
QUIC_STATUS_OUT_OF_MEMORY = 12, // 12
QUIC_STATUS_INVALID_PARAMETER = 22, // 22
QUIC_STATUS_INVALID_STATE = 1, // 1
QUIC_STATUS_NOT_SUPPORTED = 102, // 102 (95 on Linux)
QUIC_STATUS_NOT_FOUND = 2, // 2
QUIC_STATUS_BUFFER_TOO_SMALL = 84, // 84 (75 on Linux)
QUIC_STATUS_HANDSHAKE_FAILURE = 53, // 53 (103 on Linux)
QUIC_STATUS_ABORTED = 89, // 89 (125 on Linux)
QUIC_STATUS_ADDRESS_IN_USE = 48, // 48 (98 on Linux)
QUIC_STATUS_INVALID_ADDRESS = 47, // 47 (97 on Linux)
QUIC_STATUS_CONNECTION_TIMEOUT = 60, // 60 (110 on Linux)
QUIC_STATUS_CONNECTION_IDLE = 101, // 101 (62 on Linux)
QUIC_STATUS_INTERNAL_ERROR = 5, // 5
QUIC_STATUS_CONNECTION_REFUSED = 61, // 61 (111 on Linux)
QUIC_STATUS_PROTOCOL_ERROR = 100, // 100 (71 on Linux)
QUIC_STATUS_VER_NEG_ERROR = 43, // 43 (93 on Linux)
QUIC_STATUS_UNREACHABLE = 65, // 65 (113 on Linux)
QUIC_STATUS_TLS_ERROR = 126, // 126
QUIC_STATUS_USER_CANCELED = 105, // 105 (130 on Linux)
QUIC_STATUS_ALPN_NEG_FAILURE = 42, // 42 (92 on Linux)
QUIC_STATUS_STREAM_LIMIT_REACHED = 86, // 86
QUIC_STATUS_ALPN_IN_USE = 41, // 41 (91 on Linux)
QUIC_STATUS_ADDRESS_NOT_AVAILABLE = 47, // 47 (99 on Linux)

QUIC_STATUS_CLOSE_NOTIFY = (0xff & 0) + TLS_ERROR_BASE, // 0xBEBC300 - Close notify
QUIC_STATUS_BAD_CERTIFICATE = (0xff & 42) + TLS_ERROR_BASE, // 0xBEBC32A - Bad Certificate
QUIC_STATUS_UNSUPPORTED_CERTIFICATE = (0xff & 43) + TLS_ERROR_BASE, // 0xBEBC32B - Unsupported Certificate
QUIC_STATUS_REVOKED_CERTIFICATE = (0xff & 44) + TLS_ERROR_BASE, // 0xBEBC32C - Revoked Certificate
QUIC_STATUS_EXPIRED_CERTIFICATE = (0xff & 45) + TLS_ERROR_BASE, // 0xBEBC32D - Expired Certificate
QUIC_STATUS_UNKNOWN_CERTIFICATE = (0xff & 46) + TLS_ERROR_BASE, // 0xBEBC32E - Unknown Certificate
QUIC_STATUS_REQUIRED_CERTIFICATE = (0xff & 116) + TLS_ERROR_BASE, // 0xBEBC374 - Required Certificate

QUIC_STATUS_CERT_EXPIRED = 1 + CERT_ERROR_BASE, // 0xBEBC401
QUIC_STATUS_CERT_UNTRUSTED_ROOT = 2 + CERT_ERROR_BASE, // 0xBEBC402
QUIC_STATUS_CERT_NO_CERT = 3 + CERT_ERROR_BASE, // 0xBEBC403
} QUIC_STATUS;
Binary file added crates/.windows/winmd/Microsoft.MsQuic.winmd
Binary file not shown.
Binary file added crates/.windows/winmd/Windows.Win32.winmd
Binary file not shown.
15 changes: 15 additions & 0 deletions crates/Rust.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# msquic Rust

Rust binding for msquic is auto generated from c headers using win32metadata and windows-bindgen toolchain.
Generated bindings is cross platform with use use of [mssf-pal](https://github.com/Azure/service-fabric-rs/tree/main/crates/libs/pal).
The c functions that require linking are not included in the generated code for now, and the crate can be built without installing msquic shared libraries. Most likely these functions will be dynamically loaded in future.(This is subjected to change.)

## Regenerate code
```ps1
# generate winmd
dotnet build .\crates\.metadata\
# generate rust code
cargo run -p tools_api
# build generated code
cargo build -p msquic-rs
```
18 changes: 18 additions & 0 deletions crates/libs/msquic-rs/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[package]
name = "msquic-rs"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

# need win foundataion because of BOOLEAN.
# need winsock due to addr def.
[dependencies]
windows-core = "0.57.0"
windows = { version = "0.57.0", features = [ "Win32_Foundation", "Win32_Networking_WinSock"]}
mssf-pal = "0" # for linux to work with windows crates

[features]
Foundation = []
# generated features
MsQuic = ["Foundation"]
3 changes: 3 additions & 0 deletions crates/libs/msquic-rs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# msquic-rs

Auto generated rust bindings for msquic.
Loading
Loading