Skip to content

feat: raw ffi bindings to Stream module #153

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

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
33 changes: 30 additions & 3 deletions nginx-sys/build/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const NGX_CONF_FEATURES: &[&str] = &[
"have_file_aio",
"have_kqueue",
"have_variadic_macros",
"http",
"http_cache",
"http_dav",
"http_gzip",
Expand All @@ -40,6 +41,7 @@ const NGX_CONF_FEATURES: &[&str] = &[
"pcre2",
"quic",
"ssl",
"stream",
"stream_ssl",
"stream_upstream_zone",
"threads",
Expand Down Expand Up @@ -328,17 +330,26 @@ pub fn print_cargo_metadata<T: AsRef<Path>>(includes: &[T]) -> Result<(), Box<dy
);

// A quoted list of all recognized features to be passed to rustc-check-cfg.
let values = NGX_CONF_FEATURES.join("\",\"");
println!("cargo::metadata=features_check=\"{}\"", values);
println!(
"cargo::metadata=features_check=\"{}\"",
NGX_CONF_FEATURES.join("\",\"")
"cargo::rustc-check-cfg=cfg(ngx_feature, values(\"{}\"))",
values
);

// A list of features enabled in the nginx build we're using
println!("cargo::metadata=features={}", ngx_features.join(","));
for feature in ngx_features {
println!("cargo::rustc-cfg=ngx_feature=\"{}\"", feature);
}

// A quoted list of all recognized operating systems to be passed to rustc-check-cfg.
println!("cargo::metadata=os_check=\"{}\"", NGX_CONF_OS.join("\",\""));
let values = NGX_CONF_OS.join("\",\"");
println!("cargo::metadata=os_check=\"{}\"", values);
println!("cargo::rustc-check-cfg=cfg(ngx_os, values(\"{}\"))", values);
// Current detected operating system
println!("cargo::metadata=os={ngx_os}");
println!("cargo::rustc-cfg=ngx_os=\"{ngx_os}\"");

Ok(())
}
Expand All @@ -353,6 +364,22 @@ fn expand_definitions<T: AsRef<Path>>(includes: &[T]) -> Result<Vec<u8>, Box<dyn
#include <ngx_config.h>
#include <ngx_core.h>
/* C23 or Clang/GCC/MSVC >= 15.3 extension */
#if defined(__has_include)
#if __has_include(<ngx_http.h>)
RUST_CONF_HTTP=1
#endif
#if __has_include(<ngx_stream.h>)
RUST_CONF_STREAM=1
#endif
#else
/* fallback */
RUST_CONF_HTTP=1
#endif
RUST_CONF_NGINX_BUILD=NGINX_VER_BUILD
RUST_CONF_NGINX_VERSION=NGINX_VER
RUST_CONF_NGINX_VERSION_NUMBER=nginx_version
Expand Down
19 changes: 17 additions & 2 deletions nginx-sys/build/wrapper.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,23 @@
#include <ngx_http.h>
#include <ngx_conf_file.h>
#include <ngx_config.h>
#include <ngx_core.h>

/* __has_include was a compiler-specific extension until C23,
* but it's safe to assume that bindgen supports it via libclang.
*/
#if defined(__has_include)

#if __has_include(<ngx_http.h>)
#include <ngx_http.h>
#endif

#if __has_include(<ngx_stream.h>)
#include <ngx_stream.h>
#endif

#else
#include <ngx_http.h>
#endif

const char *NGX_RS_MODULE_SIGNATURE = NGX_MODULE_SIGNATURE;

// `--prefix=` results in not emitting the declaration
Expand Down
18 changes: 18 additions & 0 deletions nginx-sys/src/http.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
use core::mem::offset_of;

use crate::bindings::ngx_http_conf_ctx_t;

/// The offset of the `main_conf` field in the `ngx_http_conf_ctx_t` struct.
///
/// This is used to access the main configuration context for an HTTP module.
pub const NGX_HTTP_MAIN_CONF_OFFSET: usize = offset_of!(ngx_http_conf_ctx_t, main_conf);

/// The offset of the `srv_conf` field in the `ngx_http_conf_ctx_t` struct.
///
/// This is used to access the server configuration context for an HTTP module.
pub const NGX_HTTP_SRV_CONF_OFFSET: usize = offset_of!(ngx_http_conf_ctx_t, srv_conf);

/// The offset of the `loc_conf` field in the `ngx_http_conf_ctx_t` struct.
///
/// This is used to access the location configuration context for an HTTP module.
pub const NGX_HTTP_LOC_CONF_OFFSET: usize = offset_of!(ngx_http_conf_ctx_t, loc_conf);
24 changes: 8 additions & 16 deletions nginx-sys/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@
#![no_std]

mod event;
#[cfg(ngx_feature = "http")]
mod http;
mod queue;
#[cfg(ngx_feature = "stream")]
mod stream;

use core::fmt;
use core::mem::offset_of;
use core::ptr::{self, copy_nonoverlapping};
use core::slice;

Expand All @@ -25,22 +28,11 @@ mod bindings {
#[doc(no_inline)]
pub use bindings::*;
pub use event::*;
#[cfg(ngx_feature = "http")]
pub use http::*;
pub use queue::*;

/// The offset of the `main_conf` field in the `ngx_http_conf_ctx_t` struct.
///
/// This is used to access the main configuration context for an HTTP module.
pub const NGX_HTTP_MAIN_CONF_OFFSET: usize = offset_of!(ngx_http_conf_ctx_t, main_conf);

/// The offset of the `srv_conf` field in the `ngx_http_conf_ctx_t` struct.
///
/// This is used to access the server configuration context for an HTTP module.
pub const NGX_HTTP_SRV_CONF_OFFSET: usize = offset_of!(ngx_http_conf_ctx_t, srv_conf);

/// The offset of the `loc_conf` field in the `ngx_http_conf_ctx_t` struct.
///
/// This is used to access the location configuration context for an HTTP module.
pub const NGX_HTTP_LOC_CONF_OFFSET: usize = offset_of!(ngx_http_conf_ctx_t, loc_conf);
#[cfg(ngx_feature = "stream")]
pub use stream::*;

/// Convert a byte slice to a raw pointer (`*mut u_char`) allocated in the given nginx memory pool.
///
Expand Down
13 changes: 13 additions & 0 deletions nginx-sys/src/stream.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
use core::mem::offset_of;

use crate::bindings::ngx_stream_conf_ctx_t;

/// The offset of the `main_conf` field in the `ngx_stream_conf_ctx_t` struct.
///
/// This is used to access the main configuration context for a STREAM module.
pub const NGX_STREAM_MAIN_CONF_OFFSET: usize = offset_of!(ngx_stream_conf_ctx_t, main_conf);

/// The offset of the `srv_conf` field in the `ngx_stream_conf_ctx_t` struct.
///
/// This is used to access the server configuration context for a STREAM module.
pub const NGX_STREAM_SRV_CONF_OFFSET: usize = offset_of!(ngx_stream_conf_ctx_t, srv_conf);
Loading