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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
//! This crate provides a type that can act as a platform-native socket address
//! (i.e. `libc::sockaddr`)
//!
//! #Motivation
//! 
//! The std crate provides `std::net::SocketAddr` for managing socket addresses. Its `V4` variant
//! encapsulates a `libc::sockaddr_in` and its `V6` variant encapsulates a `libc::sockaddr_in6`.
//! However there is no easy way to convert `SocketAddr` from/into a `libc::sockaddr`, because
//! `SocketAddr` is a rust enum.
//! 
//! This crate provides `OsSocketAddr` which holds a `libc::sockaddr` (containing an IPv4 or IPv6
//! address) and the conversion functions from/into `std::net::SocketAddr`.
//! 
//!
//! #Example
//! 
//! ```
//! # mod foo {
//! extern crate libc;
//! extern crate os_socketaddr;
//! 
//! use std::net::SocketAddr;
//! use self::libc::{c_int, c_void, size_t, ssize_t};
//! use self::os_socketaddr::OsSocketAddr;
//! 
//! fn send(socket: c_int, buf: &[u8], dst: SocketAddr) -> ssize_t {
//!     let addr : OsSocketAddr = dst.into();
//!     unsafe {
//!         libc::sendto(socket, buf.as_ptr() as *const c_void, buf.len() as size_t, 0,
//!                      addr.as_ptr(), addr.len())
//!     }
//! }
//! 
//! fn receive(socket: c_int, buf: &mut[u8]) -> (ssize_t, Option<SocketAddr>)
//! {
//!     let mut addr = OsSocketAddr::new();
//!     let mut addrlen = addr.capacity();
//!     let nb = unsafe {
//!         libc::recvfrom(socket, buf.as_mut_ptr() as *mut c_void, buf.len(), 0,
//!                        addr.as_mut_ptr(), &mut addrlen as *mut _)
//!     };
//!     (nb, addr.into())
//! }
//! # }
//! ```
//!

extern crate libc;

use std::net::SocketAddr;


/// A type for handling platform-native socket addresses (`struct sockaddr`)
/// 
/// This type holds a buffer enough big to have a `libc::sockaddr_in` or `libc::sockaddr_in6`
/// struct. Its content can be arbitrary written using `.as_mut()` or `.as_mut_ptr()`.
/// 
/// It also provides the conversion functions from/into `std::net::SocketAddr`.
/// 
/// See [module level][mod] documentation for more details.
/// 
/// [mod]: index.html
/// 
#[derive(Copy,Clone)]
pub struct OsSocketAddr
{
    sa6: libc::sockaddr_in6
}

#[allow(dead_code)]
impl OsSocketAddr {

    /// Create a new empty socket address
    pub fn new() -> Self
    {
        OsSocketAddr{sa6: unsafe { std::mem::zeroed() }}
    }

    /// Create a new socket address from a raw slice
    /// 
    /// # Panics
    /// 
    /// Panics if the slice is bigger that the size of `libc::sockaddr_in6`
    /// 
    pub fn from_slice(raw: &[u8]) -> Self
    {
        assert!(raw.len() <= std::mem::size_of::<libc::sockaddr_in6>());
        let mut addr = OsSocketAddr::new();
        addr.as_mut()[..raw.len()].copy_from_slice(raw);
        addr
    }


    /// Create a new socket address from a raw memory buffer
    /// 
    /// # Panics
    /// 
    /// Panics if `len` is bigger that the size of `libc::sockaddr_in6`
    /// 
    pub unsafe fn from_raw_parts(ptr: *const u8, len: usize) -> Self
    {
        Self::from_slice(std::slice::from_raw_parts(ptr, len))
    }

    /// Create a new socket address from a `std::net::SocketAddr` object
    pub fn from(addr: SocketAddr) -> Self
    {
        addr.into()
    }

    /// Attempt to convert the internal buffer into a `std::net::SocketAddr` object
    /// 
    /// The internal buffer is assumed to be a `libc::sockaddr`.
    /// 
    /// If the value of `.sa_family` resolves to `AF_INET` or `AF_INET6` then the buffer is
    /// converted into `SocketAddr`, otherwise the function returns None.
    /// 
    pub fn into_addr(self) -> Option<SocketAddr>
    {
        self.into()
    }

    /// Return the length of the address
    /// 
    /// The result depends on the value of `.sa_family` in the internal buffer:
    /// * `AF_INET`  -> the size of `sockaddr_in`
    /// * `AF_INET6` -> the size of `sockaddr_in6`
    /// * *other* -> 0
    pub fn len(&self) -> libc::socklen_t
    {
        (match self.sa6.sin6_family as i32 {
            libc::AF_INET  => std::mem::size_of::<libc::sockaddr_in >(),
            libc::AF_INET6 => std::mem::size_of::<libc::sockaddr_in6>(),
            _ => 0
        }) as libc::socklen_t
    }

    /// Return the size of the internal buffer
    pub fn capacity(&self) -> libc::socklen_t
    {
        std::mem::size_of::<libc::sockaddr_in6>() as libc::socklen_t
    }

    /// Get a pointer to the internal buffer
    pub fn as_ptr(&self) -> *const libc::sockaddr {
        &self.sa6 as *const _ as *const _
    }

    /// Get a mutable pointer to the internal buffer
    pub fn as_mut_ptr(&mut self) -> *mut libc::sockaddr {
        &mut self.sa6 as *mut _ as *mut _
    }

}

impl AsRef<[u8]> for OsSocketAddr
{
    /// Get the internal buffer as a byte slice
    /// 
    /// Note: the actual length of slice depends on the value of `.sa_family` (see `.len()`)
    /// 
    fn as_ref(&self) -> &[u8] {
        unsafe {
            std::slice::from_raw_parts(&self.sa6 as *const _ as *const u8, self.len() as usize)
        }
    }
}

impl AsMut<[u8]> for OsSocketAddr
{
    /// Get the internal buffer as a mutable slice
    fn as_mut(&mut self) -> &mut[u8] {
        unsafe {
            std::slice::from_raw_parts_mut(&mut self.sa6 as *mut _ as *mut u8,
                                           self.capacity() as usize)
        }
    }
}

impl Into<Option<SocketAddr>> for OsSocketAddr
{
    /// Attempt to convert the internal buffer into a `std::net::SocketAddr` object
    /// 
    /// The internal buffer is assumed to be a `libc::sockaddr`.
    /// 
    /// If the value of `.sa_family` resolves to `AF_INET` or `AF_INET6` then the buffer is
    /// converted into `SocketAddr`, otherwise the function returns None.
    /// 
    fn into(self) -> Option<SocketAddr>
    {
        unsafe { match self.sa6.sin6_family as i32 {
                libc::AF_INET   => Some(SocketAddr::V4(*(self.as_ptr() as *const _))),
                libc::AF_INET6  => Some(SocketAddr::V6(*(self.as_ptr() as *const _))),
                _ => None
        }}
    }
}

impl From<SocketAddr> for OsSocketAddr
{
    fn from(addr: SocketAddr) -> Self
    {
        OsSocketAddr{sa6: unsafe {
            match addr {
                SocketAddr::V4(addr) => {
                    let mut sa6 = std::mem::uninitialized();
                    *(&mut sa6 as *mut _ as *mut _) = addr;
                    sa6
                },
                SocketAddr::V6(addr) =>
                    *(&addr as *const _ as *const _),
            }
        }}
    }
}

impl From<Option<SocketAddr>> for OsSocketAddr
{
    fn from(addr: Option<SocketAddr>) -> Self
    {
        match addr {
            None => Self::new(),
            Some(addr) => addr.into(),
        }
    }
}

impl std::fmt::Debug for OsSocketAddr
{
    fn fmt(&self, fmt: &mut std::fmt::Formatter) -> std::fmt::Result
    {
        self.into_addr().fmt(fmt)
    }
}

#[cfg(test)]
mod tests {
    use std::net::SocketAddrV6;
    use super::*;

    fn check_as_mut(osa: &mut OsSocketAddr)
    {
        let ptr = osa as *mut _ as usize;
        let buf = osa.as_mut();
        assert_eq!(buf.as_mut_ptr(), ptr as *mut _);
        assert_eq!(buf.len(), std::mem::size_of::<libc::sockaddr_in6>());
    }

    #[test]
    fn ptr_and_capacity() {
        let mut osa = OsSocketAddr::new();
        assert_eq!(osa.as_ptr(), &osa as *const _ as *const _);
        assert_eq!(osa.as_mut_ptr(), &mut osa as *mut _ as *mut _);
        assert_eq!(osa.capacity() as usize, std::mem::size_of::<libc::sockaddr_in6>());
    }

    #[test]
    fn as_slice() {
        let mut osa = OsSocketAddr::new();
        {
            let sl = osa.as_ref();
            assert_eq!(sl.as_ptr(), &osa as *const _ as *const _);
            assert_eq!(sl.len(), 0);
        }
        {
            let ptr = &mut osa as *mut _ as *mut _;
            let sl = osa.as_mut();
            assert_eq!(sl.as_mut_ptr(), ptr);
            assert_eq!(sl.len(), std::mem::size_of::<libc::sockaddr_in6>());
        }
    }

    #[test]
    fn os_socketaddr_ipv4()
    {
        let addr : SocketAddr = "12.34.56.78:4242".parse().unwrap();
        unsafe {
            let sa = libc::sockaddr_in {
                sin_family: libc::AF_INET as u16,
                sin_addr: *(&[12u8,34,56,78] as *const _ as *const libc::in_addr),
                sin_port: 4242u16.to_be(),
                sin_zero: std::mem::zeroed(),
            };
            let mut osa = OsSocketAddr::from_slice(
                std::slice::from_raw_parts(&sa as *const _ as *const u8,
                                           std::mem::size_of_val(&sa)));
            assert_eq!(osa.len()      as usize, std::mem::size_of::<libc::sockaddr_in>());
            assert_eq!(osa.capacity() as usize, std::mem::size_of::<libc::sockaddr_in6>());
            assert_eq!(osa.into_addr(), Some(addr));
            assert_eq!(OsSocketAddr::from(addr).into_addr(), Some(addr));
            {
                let buf = osa.as_ref();
                assert_eq!(buf.as_ptr(), &osa as *const _ as *const _);
                assert_eq!(buf.len(), std::mem::size_of_val(&sa));
            } 
            check_as_mut(&mut osa);
        }
    }

    #[test]
    fn os_socketaddr_ipv6()
    {
        let ip = [7u8,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22];
        let addr = SocketAddr::V6(SocketAddrV6::new(ip.into(), 4242,
                        0x11223344, 0x55667788));
        unsafe {
            let sa = libc::sockaddr_in6 {
                sin6_family: libc::AF_INET6 as u16,
                sin6_addr: *(&ip as *const _ as *const libc::in6_addr),
                sin6_port: 4242u16.to_be(),
                sin6_flowinfo: 0x11223344,
                sin6_scope_id: 0x55667788,
            };
            let mut osa = OsSocketAddr::from_raw_parts(&sa as *const _ as *const u8,
                                                    std::mem::size_of_val(&sa));
            assert_eq!(osa.len()      as usize, std::mem::size_of::<libc::sockaddr_in6>());
            assert_eq!(osa.capacity() as usize, std::mem::size_of::<libc::sockaddr_in6>());
            assert_eq!(osa.into_addr(), Some(addr));
            assert_eq!(OsSocketAddr::from(addr).into_addr(), Some(addr));
            {
                let buf = osa.as_ref();
                assert_eq!(buf.as_ptr(), &osa as *const _ as *const _);
                assert_eq!(buf.len(), std::mem::size_of_val(&sa));
            }
            check_as_mut(&mut osa);
        }
    }

    #[test]
    fn os_socketaddr_other()
    {
        fn check(osa: &mut OsSocketAddr) {
            assert_eq!(osa.into_addr(), None);
            {
                let buf = osa.as_ref();
                assert_eq!(buf.len(), 0);
                assert_eq!(osa.len(), 0);
                assert_eq!(osa.capacity() as usize, std::mem::size_of::<libc::sockaddr_in6>());
            }
            check_as_mut(osa);
        };

        check(&mut OsSocketAddr::new());
        check(&mut None.into());

        unsafe {
            check(&mut OsSocketAddr::from_raw_parts([0xde,0xad,0xbe,0xef].as_ptr(), 4));
        }
    }
}