fe9fd1d766dcacda118cb813710d0bcf5b2c159e
[senf.git] / Socket / BSDSocketProtocol.cc
1 // $Id$
2 //
3 // Copyright (C) 2006 
4 // Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS)
5 // Kompetenzzentrum fuer Satelitenkommunikation (SatCom)
6 //     Stefan Bund <stefan.bund@fokus.fraunhofer.de>
7 //
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 2 of the License, or
11 // (at your option) any later version.
12 //
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 // GNU General Public License for more details.
17 //
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the
20 // Free Software Foundation, Inc.,
21 // 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22
23 /** \file
24     \brief BSDSocketProtocol non-inline non-template implementation */
25
26 #include "BSDSocketProtocol.hh"
27 //#include "BSDSocketProtocol.ih"
28
29 // Custom includes
30 #include <sys/types.h>
31 #include <sys/socket.h>
32 #include <sys/ioctl.h>
33 #include "SocketHandle.hh"
34
35 //#include "BSDSocketProtocol.mpp"
36 #define prefix_
37 ///////////////////////////////cc.p////////////////////////////////////////
38
39 prefix_ std::pair<bool,unsigned> senf::BSDSocketProtocol::linger()
40     const
41 {
42     struct linger ling;
43     socklen_t len = sizeof(ling);
44     ::memset(&ling,sizeof(ling),0);
45     if (::getsockopt(body().fd(),SOL_SOCKET,SO_LINGER,&ling,&len) < 0)
46         throw SystemException(errno);
47     return std::make_pair(ling.l_onoff, ling.l_linger);
48 }
49
50 prefix_ void senf::BSDSocketProtocol::linger(bool enable, unsigned timeout)
51     const
52 {
53     struct linger ling;
54     ling.l_onoff = enable;
55     ling.l_linger = timeout;
56     if (::setsockopt(body().fd(),SOL_SOCKET,SO_LINGER,&ling,sizeof(ling)) < 0)
57         throw SystemException(errno);
58 }
59
60 prefix_ struct timeval senf::BSDSocketProtocol::timestamp()
61     const
62 {
63     /** \bug Check, why this fails with ENOFILE (!!!!) at least when
64         called from a tcp socket.Maybe this is only available for
65         datagram sockets ? That could make sense from the description
66         (what is the last packet passed to the user on a stream
67         socket?)  Further investigation necessary ... */
68     struct timeval tv;
69     if (::ioctl(body().fd(), SIOCGSTAMP, &tv) < 0)
70         throw SystemException(errno);
71     return tv;
72 }
73
74 ///////////////////////////////////////////////////////////////////////////
75
76 prefix_ bool senf::AddressableBSDSocketProtocol::reuseaddr()
77     const
78 {
79     int value;
80     socklen_t len (sizeof(value));
81     if (::getsockopt(body().fd(),SOL_SOCKET,SO_REUSEADDR,&value,&len) < 0)
82         throw SystemException(errno);
83     return value;
84 }
85
86 prefix_ void senf::AddressableBSDSocketProtocol::reuseaddr(bool value)
87     const
88 {
89     int ivalue (value);
90     if (::setsockopt(body().fd(),SOL_SOCKET,SO_REUSEADDR,&ivalue,sizeof(ivalue)) < 0)
91         throw SystemException(errno);
92 }
93
94 ///////////////////////////////cc.e////////////////////////////////////////
95 #undef prefix_
96 //#include "BSDSocketProtocol.mpp"
97
98 \f
99 // Local Variables:
100 // mode: c++
101 // c-file-style: "senf"
102 // End: