summaryrefslogtreecommitdiff
path: root/src/upnp.rs
blob: 20c798952f9351945708f71fe9fb58ffb48da229 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
extern crate libc;

use self::libc::{uint32_t, uint16_t, uint8_t, c_char, c_uchar, c_int, c_void};
use std::ptr;

#[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 = "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 err = UpnpInit(ptr::null(), 0);
        let target = CString::new("ssdp:all");
        UpnpSearchAsync(self.handle.client, 1, target.as_ptr(), cookie);
    }
}