git-svn-id: https://svn.berlios.de/svnroot/repos/senf/trunk@129 270642c3-0616-0410...
[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 // Definition of non-inline non-template functions
24
25 #include "BSDSocketProtocol.hh"
26 //#include "BSDSocketProtocol.ih"
27
28 // Custom includes
29 #include <sys/types.h>
30 #include <sys/socket.h>
31 #include <sys/ioctl.h>
32 #include "SocketHandle.hh"
33
34 //#include "BSDSocketProtocol.mpp"
35 #define prefix_
36 ///////////////////////////////cc.p////////////////////////////////////////
37
38 prefix_ std::pair<bool,unsigned> satcom::lib::BSDSocketProtocol::linger()
39     const
40 {
41     struct linger ling;
42     socklen_t len = sizeof(ling);
43     ::memset(&ling,sizeof(ling),0);
44     if (::getsockopt(body().fd(),SOL_SOCKET,SO_LINGER,&ling,&len) < 0)
45         throw SystemException(errno);
46     return std::make_pair(ling.l_onoff, ling.l_linger);
47 }
48
49 prefix_ void satcom::lib::BSDSocketProtocol::linger(bool enable, unsigned timeout)
50     const
51 {
52     struct linger ling;
53     ling.l_onoff = enable;
54     ling.l_linger = timeout;
55     if (::setsockopt(body().fd(),SOL_SOCKET,SO_LINGER,&ling,sizeof(ling)) < 0)
56         throw SystemException(errno);
57 }
58
59 prefix_ struct timeval satcom::lib::BSDSocketProtocol::timestamp()
60     const
61 {
62     // TODO: Check, why this fails with ENOFILE (!!!!) at least when
63     // called from a tcp socket. Further investigation necessary ...
64     struct timeval tv;
65     if (::ioctl(body().fd(), SIOCGSTAMP, &tv) < 0)
66         throw SystemException(errno);
67     return tv;
68 }
69
70 ///////////////////////////////////////////////////////////////////////////
71
72 prefix_ bool satcom::lib::AddressableBSDSocketProtocol::reuseaddr()
73     const
74 {
75     int value;
76     socklen_t len (sizeof(value));
77     if (::getsockopt(body().fd(),SOL_SOCKET,SO_REUSEADDR,&value,&len) < 0)
78         throw SystemException(errno);
79     return value;
80 }
81
82 prefix_ void satcom::lib::AddressableBSDSocketProtocol::reuseaddr(bool value)
83     const
84 {
85     int ivalue (value);
86     if (::setsockopt(body().fd(),SOL_SOCKET,SO_REUSEADDR,&ivalue,sizeof(ivalue)) < 0)
87         throw SystemException(errno);
88 }
89
90 ///////////////////////////////cc.e////////////////////////////////////////
91 #undef prefix_
92 //#include "BSDSocketProtocol.mpp"
93
94 \f
95 // Local Variables:
96 // mode: c++
97 // c-file-style: "satcom"
98 // End: