From c4b1c00c419c14e641e46f9f40cbf6d301c998b4 Mon Sep 17 00:00:00 2001 From: Yuval Adam <_@yuv.al> Date: Fri, 6 Jul 2018 17:11:13 +0300 Subject: Might as well work but going another way --- build.rs | 2 +- src/upnp.rs | 67 +++++++++++++++++++++++++++++++++++++++++++++++++++++-------- 2 files changed, 60 insertions(+), 9 deletions(-) diff --git a/build.rs b/build.rs index 39dfd25..aa0e144 100644 --- a/build.rs +++ b/build.rs @@ -1,5 +1,5 @@ extern crate pkg_config; fn main() { - pkg_config::Config::new().atleast_version("1.0.0").probe("gssdp-1.0").unwrap(); + pkg_config::Config::new().atleast_version("1.6.25").probe("libupnp").unwrap(); } diff --git a/src/upnp.rs b/src/upnp.rs index b142258..20c7989 100644 --- a/src/upnp.rs +++ b/src/upnp.rs @@ -3,19 +3,70 @@ extern crate libc; use self::libc::{uint32_t, uint16_t, uint8_t, c_char, c_uchar, c_int, c_void}; use std::ptr; -pub type GSSDPClient = *mut c_void; +#[allow(dead_code)] +#[derive(Debug)] +#[repr(C)] +enum EventType { + ControlActionRequest, + ControlActionComplete, + ControlGetVarRequest, + ControlGetVarComplete, + DiscoveryAdvertisementAlive, + DiscoveryAdvertisementByebye, + DiscoverySearchResult, + DiscoverySearchTimeout, + SubscriptionRequest, + Received, + RenewalComplete, + SubscribeComplete, + UnsubscribeComplete, + AutorenewalFailed, + SubscriptionExpired, +} + +const LINE_SIZE: usize = 180; + +#[repr(C)] +struct Discovery { + err_code: libc::c_int, + expires: libc::c_int, + device_id: [libc::c_char; LINE_SIZE], + device_type: [libc::c_char; LINE_SIZE], + service_type: [libc::c_char; LINE_SIZE], + service_ver: [libc::c_char; LINE_SIZE], + location: [libc::c_char; LINE_SIZE], + os: [libc::c_char; LINE_SIZE], + date: [libc::c_char; LINE_SIZE], + ext: [libc::c_char; LINE_SIZE], + dest_addr: *mut libc::sockaddr_in, +} + +type ClientHandle = libc::c_int; + +type ClientCallbackPtr = extern "C" fn(event_type: EventType, + event: *const libc::c_void, + cookie: *mut libc::c_void); -#[link(name = "gssdp-1.0")] -extern { - pub fn gssdp_client_new(main_context: *mut c_void, iface: *const c_char, error: *mut c_void) -> GSSDPClient; - pub fn gssdp_client_get_interface(client: *mut GSSDPClient) -> *const c_char; +#[link(name = "upnp")] +extern "C" { + fn UpnpInit(hostIp: *const libc::c_char, destPort: libc::c_ushort) -> libc::c_int; + fn UpnpRegisterClient(callback: ClientCallbackPtr, + cookie: *mut libc::c_void, + handle: *mut ClientHandle) + -> libc::c_int; + fn UpnpUnRegisterClient(handle: ClientHandle) -> libc::c_int; + fn UpnpSearchAsync(handle: ClientHandle, + maxAttempts: libc::c_int, + target: *const libc::c_char, + cookie: *const libc::c_void) + -> libc::c_int; } pub fn discover() { println!("Discovering..."); unsafe { - let mut client = gssdp_client_new(ptr::null_mut(), ptr::null(), ptr::null_mut()); - let iface = gssdp_client_get_interface(&client); + let err = UpnpInit(ptr::null(), 0); + let target = CString::new("ssdp:all"); + UpnpSearchAsync(self.handle.client, 1, target.as_ptr(), cookie); } - println!("Using interface: {}", iface); } -- cgit v1.3.1