summaryrefslogtreecommitdiffstats
path: root/dom/media/webrtc/sdp/rsdparsa_capi/src/network.rs
blob: 970f7c8decb62ba91dea11959bf342a598fa692e (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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

extern crate nserror;

use std::ffi::{CStr, CString};
use std::net::IpAddr;
use std::os::raw::c_char;

use rsdparsa::address::{Address, AddressType, AddressTyped, ExplicitlyTypedAddress};
use rsdparsa::{SdpBandwidth, SdpConnection, SdpOrigin};
use std::convert::TryFrom;
use types::{StringView, NULL_STRING};

#[repr(C)]
#[derive(Clone, Copy, PartialEq)]
pub enum RustAddressType {
    IP4,
    IP6,
}

impl TryFrom<u32> for RustAddressType {
    type Error = nserror::nsresult;
    fn try_from(address_type: u32) -> Result<Self, Self::Error> {
        match address_type {
            1 => Ok(RustAddressType::IP4),
            2 => Ok(RustAddressType::IP6),
            _ => Err(nserror::NS_ERROR_INVALID_ARG),
        }
    }
}

impl From<AddressType> for RustAddressType {
    fn from(address_type: AddressType) -> Self {
        match address_type {
            AddressType::IpV4 => RustAddressType::IP4,
            AddressType::IpV6 => RustAddressType::IP6,
        }
    }
}

impl Into<AddressType> for RustAddressType {
    fn into(self) -> AddressType {
        match self {
            RustAddressType::IP4 => AddressType::IpV4,
            RustAddressType::IP6 => AddressType::IpV6,
        }
    }
}

impl<'a> From<&'a IpAddr> for RustAddressType {
    fn from(addr: &IpAddr) -> RustAddressType {
        addr.address_type().into()
    }
}

pub fn get_octets(addr: &IpAddr) -> [u8; 16] {
    let mut octets = [0; 16];
    match *addr {
        IpAddr::V4(v4_addr) => {
            let v4_octets = v4_addr.octets();
            (&mut octets[0..4]).copy_from_slice(&v4_octets);
        }
        IpAddr::V6(v6_addr) => {
            let v6_octets = v6_addr.octets();
            octets.copy_from_slice(&v6_octets);
        }
    }
    octets
}

#[repr(C)]
pub struct RustAddress {
    ip_address: [u8; 50],
    fqdn: StringView,
    is_fqdn: bool,
}

impl<'a> From<&'a Address> for RustAddress {
    fn from(address: &Address) -> Self {
        match address {
            Address::Ip(ip) => Self::from(ip),
            Address::Fqdn(fqdn) => Self {
                ip_address: [0; 50],
                fqdn: fqdn.as_str().into(),
                is_fqdn: true,
            },
        }
    }
}

impl<'a> From<&'a IpAddr> for RustAddress {
    fn from(addr: &IpAddr) -> Self {
        let mut c_addr = [0; 50];
        let str_addr = format!("{}", addr);
        let str_bytes = str_addr.as_bytes();
        if str_bytes.len() < 50 {
            c_addr[..str_bytes.len()].copy_from_slice(&str_bytes);
        }
        Self {
            ip_address: c_addr,
            fqdn: NULL_STRING,
            is_fqdn: false,
        }
    }
}

#[repr(C)]
pub struct RustExplicitlyTypedAddress {
    address_type: RustAddressType,
    address: RustAddress,
}

impl Default for RustExplicitlyTypedAddress {
    fn default() -> Self {
        Self {
            address_type: RustAddressType::IP4,
            address: RustAddress {
                ip_address: [0; 50],
                fqdn: NULL_STRING,
                is_fqdn: false,
            },
        }
    }
}

impl<'a> From<&'a ExplicitlyTypedAddress> for RustExplicitlyTypedAddress {
    fn from(address: &ExplicitlyTypedAddress) -> Self {
        match address {
            ExplicitlyTypedAddress::Fqdn { domain, .. } => Self {
                address_type: address.address_type().into(),
                address: RustAddress {
                    ip_address: [0; 50],
                    fqdn: StringView::from(domain.as_str()),
                    is_fqdn: true,
                },
            },
            ExplicitlyTypedAddress::Ip(ip_address) => Self {
                address_type: ip_address.address_type().into(),
                address: ip_address.into(),
            },
        }
    }
}

// TODO @@NG remove
impl<'a> From<&'a Option<IpAddr>> for RustExplicitlyTypedAddress {
    fn from(addr: &Option<IpAddr>) -> Self {
        match *addr {
            Some(ref x) => Self {
                address_type: RustAddressType::from(x.address_type()),
                address: RustAddress::from(x),
            },
            None => Self::default(),
        }
    }
}

#[repr(C)]
pub struct RustSdpConnection {
    pub addr: RustExplicitlyTypedAddress,
    pub ttl: u8,
    pub amount: u64,
}

impl<'a> From<&'a SdpConnection> for RustSdpConnection {
    fn from(sdp_connection: &SdpConnection) -> Self {
        let ttl = match sdp_connection.ttl {
            Some(x) => x as u8,
            None => 0,
        };
        let amount = match sdp_connection.amount {
            Some(x) => x as u64,
            None => 0,
        };
        RustSdpConnection {
            addr: RustExplicitlyTypedAddress::from(&sdp_connection.address),
            ttl: ttl,
            amount: amount,
        }
    }
}

#[repr(C)]
pub struct RustSdpOrigin {
    username: StringView,
    session_id: u64,
    session_version: u64,
    addr: RustExplicitlyTypedAddress,
}

fn bandwidth_match(str_bw: &str, enum_bw: &SdpBandwidth) -> bool {
    match *enum_bw {
        SdpBandwidth::As(_) => str_bw == "AS",
        SdpBandwidth::Ct(_) => str_bw == "CT",
        SdpBandwidth::Tias(_) => str_bw == "TIAS",
        SdpBandwidth::Unknown(ref type_name, _) => str_bw == type_name,
    }
}

fn bandwidth_value(bandwidth: &SdpBandwidth) -> u32 {
    match *bandwidth {
        SdpBandwidth::As(x) | SdpBandwidth::Ct(x) | SdpBandwidth::Tias(x) => x,
        SdpBandwidth::Unknown(_, _) => 0,
    }
}

pub unsafe fn get_bandwidth(bandwidths: &Vec<SdpBandwidth>, bandwidth_type: *const c_char) -> u32 {
    let bw_type = match CStr::from_ptr(bandwidth_type).to_str() {
        Ok(string) => string,
        Err(_) => return 0,
    };
    for bandwidth in bandwidths.iter() {
        if bandwidth_match(bw_type, bandwidth) {
            return bandwidth_value(bandwidth);
        }
    }
    0
}

#[no_mangle]
pub unsafe extern "C" fn sdp_serialize_bandwidth(bw: *const Vec<SdpBandwidth>) -> *mut c_char {
    let mut builder = String::new();
    for bandwidth in (*bw).iter() {
        match *bandwidth {
            SdpBandwidth::As(val) => {
                builder.push_str("b=AS:");
                builder.push_str(&val.to_string());
                builder.push_str("\r\n");
            }
            SdpBandwidth::Ct(val) => {
                builder.push_str("b=CT:");
                builder.push_str(&val.to_string());
                builder.push_str("\r\n");
            }
            SdpBandwidth::Tias(val) => {
                builder.push_str("b=TIAS:");
                builder.push_str(&val.to_string());
                builder.push_str("\r\n");
            }
            SdpBandwidth::Unknown(ref name, val) => {
                builder.push_str("b=");
                builder.push_str(name.as_str());
                builder.push(':');
                builder.push_str(&val.to_string());
                builder.push_str("\r\n");
            }
        }
    }
    CString::from_vec_unchecked(builder.into_bytes()).into_raw()
}

#[no_mangle]
pub unsafe extern "C" fn sdp_free_string(s: *mut c_char) {
    drop(CString::from_raw(s));
}

pub unsafe fn origin_view_helper(origin: &SdpOrigin) -> RustSdpOrigin {
    RustSdpOrigin {
        username: StringView::from(origin.username.as_str()),
        session_id: origin.session_id,
        session_version: origin.session_version,
        addr: RustExplicitlyTypedAddress::from(&origin.unicast_addr),
    }
}