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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
// Copyright 2016 Nathan Sizemore <nathanrsizemore@gmail.com>
//
// 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/.


#[macro_use]
extern crate bitflags;
extern crate libc;
extern crate errno;


use std::mem;
use std::ffi::CString;
use std::os::unix::io::{RawFd, AsRawFd};
use std::io::{Read, Write, Error, ErrorKind};

use libc::{c_int, c_void};
use errno::errno;


bitflags! {
    pub flags SocketType: libc::c_int {
        const SOCK_STREAM   = libc::SOCK_STREAM,
        const SOCK_DGRAM    = libc::SOCK_DGRAM,
        const SOCK_RAW      = libc::SOCK_RAW,
        const SOCK_CLOEXEC  = libc::SOCK_CLOEXEC,
        const SOCK_NONBLOCK = libc::SOCK_NONBLOCK
    }
}

bitflags! {
    pub flags IoFlags: libc::c_int {
        const MSG_CMSG_DONTWAIT = 0x0040,
        const MSG_ERRQUEUE      = 0x2000,
        const MSG_OOB           = 0x0001,
        const MSG_PEEK          = 0x0002,
        const MSG_TRUNC         = 0x0020,
        const MSG_WAITALL       = 0x0100,
        const MSG_CONFIRM       = 0x0800,
        const MSG_DONTROUTE     = 0x0004,
        const MSG_EOR           = 0x0080,
        const MSG_MORE          = 0x8000,
        const MSG_NOSIGNAL      = 0x4000
    }
}

pub enum Domain {
    Unix,
    Inet,
    Inet6,
    Netlink,
    Packet
}

pub enum Shutdown {
    Read,
    Write,
    Both
}

pub struct Socket {
    fd: c_int
}

impl Domain {
    fn to_cint(&self) -> c_int {
        match *self {
            Domain::Unix => libc::AF_UNIX,
            Domain::Inet => libc::AF_INET,
            Domain::Inet6 => libc::AF_INET6,
            Domain::Netlink => libc::AF_NETLINK,
            Domain::Packet => libc::AF_PACKET,
        }
    }
}

impl Shutdown {
    fn to_cint(&self) -> c_int {
        match *self {
            Shutdown::Read => libc::SHUT_RD,
            Shutdown::Write => libc::SHUT_WR,
            Shutdown::Both => libc::SHUT_RDWR
        }
    }
}

impl Socket {
    /// Creates a new socket with the passed options.
    pub fn new(domain: Domain,
               sock_type: c_int,
               protocol: c_int)
               -> Result<Socket, Error>
    {
        let result = unsafe {
            libc::socket(domain.to_cint(), sock_type, protocol)
        };

        if result < 0 {
            return Err(Error::from_raw_os_error(errno().0 as i32));
        }

        return Ok(Socket {
            fd: result
        });
    }

    /// Creates a socket from an already established socket fd.
    pub fn from_fd(fd: c_int) -> Socket {
        return Socket {
            fd: fd
        };
    }

    /// Assigns the address specified by `address` to the socket
    pub fn bind(&self,
                address: *const libc::sockaddr,
                address_len: libc::socklen_t)
                -> Result<(), Error>
    {
        let result = unsafe {
            libc::bind(self.fd, address, address_len)
        };

        if result < 0 {
            return Err(Error::from_raw_os_error(errno().0 as i32));
        }

        return Ok(());
    }

    /// Connects the socket to `address`.
    pub fn connect(&self,
                   address: *const libc::sockaddr,
                   address_len: libc::socklen_t)
                   -> Result<(), Error>
    {
        let result = unsafe {
            libc::connect(self.fd, address, address_len)
        };

        if result < 0 {
            return Err(Error::from_raw_os_error(errno().0 as i32));
        }

        return Ok(());
    }

    /// Marks the socket as a socket that will be used to accept incoming connections.
    pub fn listen(&self, backlog: usize) -> Result<(), Error> {
        let result = unsafe {
            libc::listen(self.fd, backlog as c_int)
        };

        if result < 0 {
            return Err(Error::from_raw_os_error(errno().0 as i32));
        }

        return Ok(());
    }

    /// Extracts the first connection from the backlog queue of pending connections and creates
    /// and returns a new `Socket`.
    pub fn accept(&self,
                  address: *mut libc::sockaddr,
                  address_len: *mut libc::socklen_t)
                  -> Result<Socket, Error>
    {
        let result = unsafe {
            libc::accept(self.fd, address, address_len)
        };

        if result < 0 {
            return Err(Error::from_raw_os_error(errno().0 as i32));
        }

        return Ok(Socket{
            fd: result
        });
    }

    /// If no messages are available at the socket, the call waits for a message to arrive, unless
    /// the socket is nonblocking, in which case `ErrorKind::WouldBlock` is returned. The call
    /// normally returns any data available, up to the requested amount, rather than waiting for
    /// receipt of the full amount requested.
    pub fn recv(&self, buf: &mut [u8], flags: libc::c_int) -> Result<usize, Error> {
        let len = buf.len() as libc::size_t;
        let buf_ptr = buf.as_mut_ptr() as *mut _ as *mut libc::c_void;
        
        let result = unsafe {
            libc::recv(self.fd, buf_ptr, len, flags)
        };

        if result < 0 {
            return Err(Error::from_raw_os_error(errno().0 as i32));
        }

        if result == 0 {
            return Err(Error::new(ErrorKind::UnexpectedEof, "Unexpected end of file"))
        }

        let num_read = result as usize;
        return Ok(num_read);
    }

    /// If the message is too long to pass atomically through the underlying protocol, the error
    /// `EMSGSIZE` is returned, and the message is not transmitted. No indication of failure
    /// to deliver is implicit in a send(). Locally detected errors are indicated by an Error
    /// returned. When the message does not fit into the send buffer of the socket, send()
    /// normally blocks, unless the socket has been placed in nonblocking I/O mode.
    /// In nonblocking mode it would fail with the error `ErrorKind::WouldBlock` in this case.
    pub fn send(&self, buf: &[u8], flags: libc::c_int) -> Result<usize, Error> {
        let len = buf.len() as libc::size_t;
        let buf_ptr = buf.as_ptr() as *const _ as *const libc::c_void;

        let result = unsafe {
            libc::send(self.fd, buf_ptr, len, flags)
        };

        if result < 0 {
            return Err(Error::from_raw_os_error(errno().0 as i32));
        }

        if result == 0 && len > 0 {
            return Err(Error::new(ErrorKind::Other, "Send returned zero (0)"));
        }

        let num_sent = result as usize;
        return Ok(num_sent);
    }

    /// Disables subsequent send and/or receive operations on a socket, depending on the value
    /// of the how argument.
    pub fn shutdown(&self, how: Shutdown) -> Result<(), Error> {
        let result = unsafe {
            libc::shutdown(self.fd, how.to_cint())
        };

        if result < 0 {
            return Err(Error::from_raw_os_error(errno().0 as i32));
        }

        return Ok(());
    }

    /// Closes the underlying file descriptor of the socket.
    pub fn close(&self) -> Result<(), Error> {
        let result = unsafe {
            libc::close(self.fd)
        };

        if result < 0 {
            return Err(Error::from_raw_os_error(errno().0 as i32));
        }

        return Ok(());
    }

    /// If set, disable the Nagle algorithm. This means that segments are always sent as soon as
    /// possible, even if there is only a small amount of data. When not set, data is buffered
    /// until there is a sufficient amount to send out, thereby avoiding the frequent sending of
    /// small packets, which results in poor utilization of the network. This option is
    /// overridden by TCP_CORK; however, setting this option forces an explicit flush of pending
    /// output, even if TCP_CORK is currently set.
    pub fn set_tcp_nodelay(&mut self, nodelay: bool) -> Result<(), Error> {
        let optval: c_int = match nodelay {
            true => 1,
            false => 0
        };
        let opt_result = unsafe {
            libc::setsockopt(self.fd,
                             libc::IPPROTO_TCP,
                             libc::TCP_NODELAY,
                             &optval as *const _ as *const c_void,
                             mem::size_of::<c_int>() as u32)
        };
        if opt_result < 0 {
            return Err(Error::from_raw_os_error(errno().0 as i32));
        }

        Ok(())
    }

    /// Sets the `O_NONBLOCK` flag on the underlying fd
    pub fn set_nonblocking(&mut self) -> Result<(), Error> {
        let result = unsafe {
            libc::fcntl(self.as_raw_fd(), libc::F_GETFL, 0)
        };
        if result < 0 {
            return Err(Error::from_raw_os_error(errno().0 as i32));
        }

        let flags = result | libc::O_NONBLOCK;
        let result = unsafe {
            libc::fcntl(self.as_raw_fd(), libc::F_SETFL, flags)
        };
        if result < 0 {
            return Err(Error::from_raw_os_error(errno().0 as i32));
        }

        Ok(())
    }

    /// Bind this socket to a particular device like "eth0", as specified in the passed
    /// interface name. If the name is an empty string or the option length is zero, the socket
    /// device binding is removed. The passed option is a variable-length null-terminated
    /// interface name string with the maximum size of IFNAMSIZ. If a socket is bound to an
    /// interface, only packets received from that particular interface are processed by the
    /// socket. Note that this only works for some socket types, particularly AF_INET sockets.
    /// It is not supported for packet sockets (use normal bind(2) there).
    ///
    /// Before Linux 3.8, this socket option could be set, but could not retrieved with
    /// getsockopt(2). Since Linux 3.8, it is readable. The optlen argument should contain the
    /// buffer size available to receive the device name and is recommended to be IFNAMSZ bytes.
    /// The real device name length is reported back in the optlen argument.
    pub fn set_bindtodevice(&mut self, interface: String) -> Result<(), Error> {
        const SO_BINDTODEVICE: i32 = 25;
        let cstr_result = CString::new(interface);
        if cstr_result.is_err() {
            return Err(Error::new(ErrorKind::Other, "Null Byte"));
        }

        let cstr = cstr_result.unwrap();
        unsafe {
            if libc::strlen(cstr.as_ptr()) > libc::IF_NAMESIZE {
                return Err(Error::new(ErrorKind::Other, "strlen(interface) > IFNAMSIZ"));
            }
        }

        let opt_result = unsafe {
            libc::setsockopt(self.fd,
                             libc::SOL_SOCKET,
                             SO_BINDTODEVICE,
                             cstr.as_ptr() as *const c_void,
                             libc::strlen(cstr.as_ptr()) as u32)
        };
        if opt_result < 0 {
            return Err(Error::from_raw_os_error(errno().0 as i32));
        }

        Ok(())
    }

    /// When enabled, datagram sockets are allowed to send packets to a broadcast address.
    /// This option has no effect on stream-oriented sockets.
    pub fn set_broadcast(&mut self, option: bool) -> Result<(), Error> {
        let optval: c_int = match option {
            true => 1,
            false => 0
        };
        let opt_result = unsafe {
            libc::setsockopt(self.fd,
                             libc::SOL_SOCKET,
                             libc::SO_BROADCAST,
                             &optval as *const _ as *const c_void,
                             mem::size_of::<c_int>() as u32)
        };
        if opt_result < 0 {
            return Err(Error::from_raw_os_error(errno().0 as i32));
        }

        Ok(())
    }

    /// Enable BSD bug-to-bug compatibility. This is used by the UDP protocol module in
    /// Linux 2.0 and 2.2. If enabled ICMP errors received for a UDP socket will not be passed
    /// to the user program. In later kernel versions, support for this option has been phased
    /// out: Linux 2.4 silently ignores it, and Linux 2.6 generates a kernel warning
    /// (printk()) if a program uses this option. Linux 2.0 also enabled BSD bug-to-bug
    /// compatibility options (random header changing, skipping of the broadcast flag) for raw
    /// sockets with this option, but that was removed in Linux 2.2.
    pub fn set_bsdcompat(&mut self, option: bool) -> Result<(), Error> {
        const SO_BSDCOMPAT: i32 = 14;
        let optval: c_int = match option {
            true => 1,
            false => 0
        };
        let opt_result = unsafe {
            libc::setsockopt(self.fd,
                             libc::SOL_SOCKET,
                             SO_BSDCOMPAT,
                             &optval as *const _ as *const c_void,
                             mem::size_of::<c_int>() as u32)
        };
        if opt_result < 0 {
            return Err(Error::from_raw_os_error(errno().0 as i32));
        }

        Ok(())
    }

    /// Enable socket debugging. Only allowed for processes with the CAP_NET_ADMIN capability
    /// or an effective user ID of 0.
    pub fn set_debug(&mut self, option: bool) -> Result<(), Error> {
        let optval: c_int = match option {
            true => 1,
            false => 0
        };
        let opt_result = unsafe {
            libc::setsockopt(self.fd,
                             libc::SOL_SOCKET,
                             libc::SO_DEBUG,
                             &optval as *const _ as *const c_void,
                             mem::size_of::<c_int>() as u32)
        };
        if opt_result < 0 {
            return Err(Error::from_raw_os_error(errno().0 as i32));
        }

        Ok(())
    }

    /// Don't send via a gateway, only send to directly connected hosts. The same effect can be
    /// achieved by setting the MSG_DONTROUTE flag on a socket send(2) operation. Expects an
    /// integer boolean flag.
    pub fn set_dontroute(&mut self, option: bool) -> Result<(), Error> {
        let optval: c_int = match option {
            true => 1,
            false => 0
        };
        let opt_result = unsafe {
            libc::setsockopt(self.fd,
                             libc::SOL_SOCKET,
                             libc::SO_DONTROUTE,
                             &optval as *const _ as *const c_void,
                             mem::size_of::<c_int>() as u32)
        };
        if opt_result < 0 {
            return Err(Error::from_raw_os_error(errno().0 as i32));
        }

        Ok(())
    }

    /// Enable sending of keep-alive messages on connection-oriented sockets. Expects an integer
    /// boolean flag.
    pub fn set_keepalive(&mut self, option: bool) -> Result<(), Error> {
        let optval: c_int = match option {
            true => 1,
            false => 0
        };
        let opt_result = unsafe {
            libc::setsockopt(self.fd,
                             libc::SOL_SOCKET,
                             libc::SO_KEEPALIVE,
                             &optval as *const _ as *const c_void,
                             mem::size_of::<c_int>() as u32)
        };
        if opt_result < 0 {
            return Err(Error::from_raw_os_error(errno().0 as i32));
        }

        Ok(())
    }

    /// Sets or gets the SO_LINGER option. When enabled, a close(2) or shutdown(2) will not return
    /// until all queued messages for the socket have been successfully sent or the linger timeout
    /// has been reached. Otherwise, the call returns immediately and the closing is done in the
    /// background. When the socket is closed as part of exit(2), it always lingers in the
    /// background.
    pub fn set_linger(&mut self, option: bool, sec: u32) -> Result<(), Error> {
        #[repr(C, packed)]
        struct Linger {
            l_onoff: c_int,
            l_linger: c_int
        };

        let optval: c_int = match option {
            true => 1,
            false => 0
        };
        let data = Linger {
            l_onoff: optval,
            l_linger: sec as i32
        };

        let opt_result = unsafe {
            libc::setsockopt(self.fd,
                             libc::SOL_SOCKET,
                             libc::SO_LINGER,
                             &data as *const _ as *const c_void,
                             mem::size_of::<Linger>() as u32)
        };
        if opt_result < 0 {
            return Err(Error::from_raw_os_error(errno().0 as i32));
        }

        Ok(())
    }

    /// Set the mark for each packet sent through this socket (similar to the netfilter MARK
    /// target but socket-based). Changing the mark can be used for mark-based routing without
    /// netfilter or for packet filtering. Setting this option requires the CAP_NET_ADMIN
    /// capability.
    pub fn set_mark(&mut self, option: bool) -> Result<(), Error> {
        const SO_MARK: i32 = 36;
        let optval: c_int = match option {
            true => 1,
            false => 0
        };
        let opt_result = unsafe {
            libc::setsockopt(self.fd,
                             libc::SOL_SOCKET,
                             SO_MARK,
                             &optval as *const _ as *const c_void,
                             mem::size_of::<c_int>() as u32)
        };
        if opt_result < 0 {
            return Err(Error::from_raw_os_error(errno().0 as i32));
        }

        Ok(())
    }

    /// If this option is enabled, out-of-band data is directly placed into the receive data
    /// stream. Otherwise out-of-band data is only passed when the MSG_OOB flag is set during
    /// receiving.
    pub fn set_oobinline(&mut self, option: bool) -> Result<(), Error> {
        let optval: c_int = match option {
            true => 1,
            false => 0
        };
        let opt_result = unsafe {
            libc::setsockopt(self.fd,
                             libc::SOL_SOCKET,
                             libc::SO_OOBINLINE,
                             &optval as *const _ as *const c_void,
                             mem::size_of::<c_int>() as u32)
        };
        if opt_result < 0 {
            return Err(Error::from_raw_os_error(errno().0 as i32));
        }

        Ok(())
    }

    /// Enable or disable the receiving of the SCM_CREDENTIALS control message. For more
    /// information see unix(7).
    pub fn set_passcred(&mut self, option: bool) -> Result<(), Error> {
        const SO_PASSCRED: i32 = 16;
        let optval: c_int = match option {
            true => 1,
            false => 0
        };
        let opt_result = unsafe {
            libc::setsockopt(self.fd,
                             libc::SOL_SOCKET,
                             SO_PASSCRED,
                             &optval as *const _ as *const c_void,
                             mem::size_of::<c_int>() as u32)
        };
        if opt_result < 0 {
            return Err(Error::from_raw_os_error(errno().0 as i32));
        }

        Ok(())
    }

    /// Set the protocol-defined priority for all packets to be sent on this socket. Linux uses
    /// this value to order the networking queues: packets with a higher priority may be processed
    /// first depending on the selected device queueing discipline. For ip(7), this also sets the
    /// IP type-of-service (TOS) field for outgoing packets. Setting a priority outside the
    /// range 0 to 6 requires the CAP_NET_ADMIN capability.
    pub fn set_priority(&mut self, priority: u32) -> Result<(), Error> {
        const SO_PRIORITY: i32 = 12;
        let opt_result = unsafe {
            libc::setsockopt(self.fd,
                             libc::SOL_SOCKET,
                             SO_PRIORITY,
                             &priority as *const _ as *const c_void,
                             mem::size_of::<u32>() as u32)
        };
        if opt_result < 0 {
            return Err(Error::from_raw_os_error(errno().0 as i32));
        }

        Ok(())
    }

    /// Sets or gets the maximum socket receive buffer in bytes. The kernel doubles this value
    /// (to allow space for bookkeeping overhead) when it is set using setsockopt(2), and this
    /// doubled value is returned by getsockopt(2). The default value is set by
    /// the /proc/sys/net/core/rmem_default file, and the maximum allowed value is set by
    /// the /proc/sys/net/core/rmem_max file. The minimum (doubled) value for this option is 256.
    pub fn set_rcvbuf(&mut self, size: usize) -> Result<(), Error> {
        let opt_result = unsafe {
            libc::setsockopt(self.fd,
                             libc::SOL_SOCKET,
                             libc::SO_RCVBUF,
                             &size as *const _ as *const c_void,
                             mem::size_of::<usize>() as u32)
        };
        if opt_result < 0 {
            return Err(Error::from_raw_os_error(errno().0 as i32));
        }

        Ok(())
    }

    /// Using this socket option, a privileged (CAP_NET_ADMIN) process can perform the same task
    /// as SO_RCVBUF, but the rmem_max limit can be overridden.
    pub fn set_rcvbufforce(&mut self, size: usize) -> Result<(), Error> {
        self.set_rcvbuf(size)
    }

    /// Specify the minimum number of bytes in the buffer until the socket layer will pass the
    /// data to the protocol (SO_SNDLOWAT) or the user on receiving (SO_RCVLOWAT). These two
    /// values are initialized to 1. SO_SNDLOWAT is not changeable on Linux (setsockopt(2) fails
    /// with the error ENOPROTOOPT). SO_RCVLOWAT is changeable only since Linux 2.4. The select(2)
    /// and poll(2) system calls currently do not respect the SO_RCVLOWAT setting on Linux, and
    /// mark a socket readable when even a single byte of data is available. A subsequent read
    /// from the socket will block until SO_RCVLOWAT bytes are available.
    pub fn set_rcvlowat(&mut self, bytes: usize) -> Result<(), Error> {
        let opt_result = unsafe {
            libc::setsockopt(self.fd,
                             libc::SOL_SOCKET,
                             libc::SO_RCVLOWAT,
                             &bytes as *const _ as *const c_void,
                             mem::size_of::<usize>() as u32)
        };
        if opt_result < 0 {
            return Err(Error::from_raw_os_error(errno().0 as i32));
        }

        Ok(())
    }

    /// Specify the minimum number of bytes in the buffer until the socket layer will pass the
    /// data to the protocol (SO_SNDLOWAT) or the user on receiving (SO_RCVLOWAT). These two
    /// values are initialized to 1. SO_SNDLOWAT is not changeable on Linux (setsockopt(2) fails
    /// with the error ENOPROTOOPT). SO_RCVLOWAT is changeable only since Linux 2.4. The select(2)
    /// and poll(2) system calls currently do not respect the SO_RCVLOWAT setting on Linux, and
    /// mark a socket readable when even a single byte of data is available. A subsequent read
    /// from the socket will block until SO_RCVLOWAT bytes are available.
    pub fn set_sndlowat(&mut self, bytes: usize) -> Result<(), Error> {
        let opt_result = unsafe {
            libc::setsockopt(self.fd,
                             libc::SOL_SOCKET,
                             libc::SO_SNDLOWAT,
                             &bytes as *const _ as *const c_void,
                             mem::size_of::<usize>() as u32)
        };
        if opt_result < 0 {
            return Err(Error::from_raw_os_error(errno().0 as i32));
        }

        Ok(())
    }

    /// Specify the receiving or sending timeouts until reporting an error. The argument is a
    /// struct timeval. If an input or output function blocks for this period of time, and data
    /// has been sent or received, the return value of that function will be the amount of data
    /// transferred; if no data has been transferred and the timeout has been reached then -1 is
    /// returned with errno set to EAGAIN or EWOULDBLOCK, or EINPROGRESS (for connect(2)) just as
    /// if the socket was specified to be nonblocking. If the timeout is set to zero (the default)
    /// then the operation will never timeout. Timeouts only have effect for system calls that
    /// perform socket I/O (e.g., read(2), recvmsg(2), send(2), sendmsg(2)); timeouts have no
    /// effect for select(2), poll(2), epoll_wait(2), and so on.
    pub fn set_rcvtimeo(&mut self,
                        sec: libc::time_t,
                        micro_sec: libc::suseconds_t)
                        -> Result<(), Error>
    {
        #[repr(C, packed)]
        struct Timeval {
            tv_sec: libc::time_t,
            tv_usec: libc::suseconds_t
        };
        let data = Timeval {
            tv_sec: sec,
            tv_usec: micro_sec
        };

        let opt_result = unsafe {
            libc::setsockopt(self.fd,
                             libc::SOL_SOCKET,
                             libc::SO_RCVTIMEO,
                             &data as *const _ as *const c_void,
                             mem::size_of::<Timeval>() as u32)
        };
        if opt_result < 0 {
            return Err(Error::from_raw_os_error(errno().0 as i32));
        }

        Ok(())
    }

    /// Specify the receiving or sending timeouts until reporting an error. The argument is a
    /// struct timeval. If an input or output function blocks for this period of time, and data
    /// has been sent or received, the return value of that function will be the amount of data
    /// transferred; if no data has been transferred and the timeout has been reached then -1 is
    /// returned with errno set to EAGAIN or EWOULDBLOCK, or EINPROGRESS (for connect(2)) just as
    /// if the socket was specified to be nonblocking. If the timeout is set to zero (the default)
    /// then the operation will never timeout. Timeouts only have effect for system calls that
    /// perform socket I/O (e.g., read(2), recvmsg(2), send(2), sendmsg(2)); timeouts have no
    /// effect for select(2), poll(2), epoll_wait(2), and so on.
    pub fn set_sndtimeo(&mut self,
                        sec: libc::time_t,
                        micro_sec: libc::suseconds_t)
                        -> Result<(), Error>
    {
        #[repr(C, packed)]
        struct Timeval {
            tv_sec: libc::time_t,
            tv_usec: libc::suseconds_t
        };
        let data = Timeval {
            tv_sec: sec,
            tv_usec: micro_sec
        };

        let opt_result = unsafe {
            libc::setsockopt(self.fd,
                             libc::SOL_SOCKET,
                             libc::SO_SNDTIMEO,
                             &data as *const _ as *const c_void,
                             mem::size_of::<Timeval>() as u32)
        };
        if opt_result < 0 {
            return Err(Error::from_raw_os_error(errno().0 as i32));
        }

        Ok(())
    }

    /// Indicates that the rules used in validating addresses supplied in a bind(2) call should
    /// allow reuse of local addresses. For AF_INET sockets this means that a socket may bind,
    /// except when there is an active listening socket bound to the address. When the listening
    /// socket is bound to INADDR_ANY with a specific port then it is not possible to bind to this
    /// port for any local address. Argument is an integer boolean flag.
    pub fn set_reuseaddr(&mut self, option: bool) -> Result<(), Error> {
        let optval: c_int = match option {
            true => 1,
            false => 0
        };
        let opt_result = unsafe {
            libc::setsockopt(self.fd,
                             libc::SOL_SOCKET,
                             libc::SO_REUSEADDR,
                             &optval as *const _ as *const c_void,
                             mem::size_of::<c_int>() as u32)
        };
        if opt_result < 0 {
            return Err(Error::from_raw_os_error(errno().0 as i32));
        }

        Ok(())
    }

    /// Sets or gets the maximum socket send buffer in bytes. The kernel doubles this value
    /// (to allow space for bookkeeping overhead) when it is set using setsockopt(2), and this
    /// doubled value is returned by getsockopt(2). The default value is set by
    /// the /proc/sys/net/core/wmem_default file and the maximum allowed value is set by
    /// the /proc/sys/net/core/wmem_max file. The minimum (doubled) value for this option is 2048.
    pub fn set_sndbuf(&mut self, size: usize) -> Result<(), Error> {
        let opt_result = unsafe {
            libc::setsockopt(self.fd,
                             libc::SOL_SOCKET,
                             libc::SO_SNDBUF,
                             &size as *const _ as *const c_void,
                             mem::size_of::<usize>() as u32)
        };
        if opt_result < 0 {
            return Err(Error::from_raw_os_error(errno().0 as i32));
        }

        Ok(())
    }

    /// Using this socket option, a privileged (CAP_NET_ADMIN) process can perform the same task
    /// as SO_SNDBUF, but the wmem_max limit can be overridden.
    pub fn set_sndbufforce(&mut self, size: usize) -> Result<(), Error> {
        self.set_sndbuf(size)
    }

    /// Enable or disable the receiving of the SO_TIMESTAMP control message. The timestamp control
    /// message is sent with level SOL_SOCKET and the cmsg_data field is a struct timeval
    /// indicating the reception time of the last packet passed to the user in this call.
    /// See cmsg(3) for details on control messages.
    pub fn set_timestamp(&mut self, option: bool) -> Result<(), Error> {
        const SO_TIMESTAMP: i32 = 29;
        let optval: c_int = match option {
            true => 1,
            false => 0
        };
        let opt_result = unsafe {
            libc::setsockopt(self.fd,
                             libc::SOL_SOCKET,
                             SO_TIMESTAMP,
                             &optval as *const _ as *const c_void,
                             mem::size_of::<c_int>() as u32)
        };
        if opt_result < 0 {
            return Err(Error::from_raw_os_error(errno().0 as i32));
        }

        Ok(())
    }
}

impl Read for Socket {
    fn read(&mut self, buf: &mut [u8]) -> Result<usize, Error> {
        return self.recv(buf, 0);
    }
}

impl Write for Socket {
    fn write(&mut self, buf: &[u8]) -> Result<usize, Error> {
        return self.send(buf, 0);
    }
    fn flush(&mut self) -> Result<(), Error> {
        return Ok(());
    }
}

impl AsRawFd for Socket {
    fn as_raw_fd(&self) -> RawFd {
        self.fd
    }
}

unsafe impl Send for Socket {}
unsafe impl Sync for Socket {}