switch to new MPL based Fraunhofer FOKUS Public License
[senf.git] / senf / Socket / Protocols / BSDSocketProtocol.cc
1 // $Id$
2 //
3 // Copyright (C) 2006
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 //
6 // The contents of this file are subject to the Fraunhofer FOKUS Public License
7 // Version 1.0 (the "License"); you may not use this file except in compliance
8 // with the License. You may obtain a copy of the License at 
9 // http://senf.berlios.de/license.html
10 //
11 // The Fraunhofer FOKUS Public License Version 1.0 is based on, 
12 // but modifies the Mozilla Public License Version 1.1.
13 // See the full license text for the amendments.
14 //
15 // Software distributed under the License is distributed on an "AS IS" basis, 
16 // WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 
17 // for the specific language governing rights and limitations under the License.
18 //
19 // The Original Code is Fraunhofer FOKUS code.
20 //
21 // The Initial Developer of the Original Code is Fraunhofer-Gesellschaft e.V. 
22 // (registered association), Hansastraße 27 c, 80686 Munich, Germany.
23 // All Rights Reserved.
24 //
25 // Contributor(s):
26 //   Stefan Bund <g0dil@berlios.de>
27
28 /** \file
29     \brief BSDSocketProtocol non-inline non-template implementation */
30
31 #include "BSDSocketProtocol.hh"
32 //#include "BSDSocketProtocol.ih"
33
34 // Custom includes
35 #include <sys/types.h>
36 #include <sys/socket.h>
37 #include <sys/ioctl.h>
38
39 //#include "BSDSocketProtocol.mpp"
40 #define prefix_
41 //-/////////////////////////////////////////////////////////////////////////////////////////////////
42
43 prefix_ std::pair<bool,unsigned> senf::BSDSocketProtocol::linger()
44     const
45 {
46     struct linger ling;
47     socklen_t len = sizeof(ling);
48     ::memset(&ling, 0, sizeof(ling));
49     if (::getsockopt(fd(),SOL_SOCKET,SO_LINGER,&ling,&len) < 0)
50         SENF_THROW_SYSTEM_EXCEPTION("");
51     return std::make_pair(ling.l_onoff, ling.l_linger);
52 }
53
54 prefix_ void senf::BSDSocketProtocol::linger(bool enable, unsigned timeout)
55     const
56 {
57     struct linger ling;
58     ling.l_onoff = enable;
59     ling.l_linger = timeout;
60     if (::setsockopt(fd(),SOL_SOCKET,SO_LINGER,&ling,sizeof(ling)) < 0)
61         SENF_THROW_SYSTEM_EXCEPTION("");
62 }
63
64 prefix_ boost::uint8_t senf::BSDSocketProtocol::priority()
65     const
66 {
67     int value;
68     socklen_t len (sizeof(value));
69     if (::getsockopt(fd(),SOL_SOCKET,SO_PRIORITY,&value,&len) < 0)
70         SENF_THROW_SYSTEM_EXCEPTION("");
71     return value;
72 }
73
74 prefix_ void senf::BSDSocketProtocol::priority(boost::uint8_t value)
75     const
76 {
77     int ivalue (value);
78     if (::setsockopt(fd(),SOL_SOCKET,SO_PRIORITY,&ivalue,sizeof(ivalue)) < 0)
79         SENF_THROW_SYSTEM_EXCEPTION("");
80 }
81
82 prefix_ int senf::BSDSocketProtocol::error()
83     const
84 {
85     int err;
86     socklen_t len (sizeof(err));
87     if (::getsockopt(fd(),SOL_SOCKET,SO_ERROR,&err,&len) < 0)
88         SENF_THROW_SYSTEM_EXCEPTION("");
89     return err;
90 }
91
92 prefix_ unsigned senf::BSDSocketProtocol::rcvbuf()
93     const
94 {
95     unsigned size;
96     socklen_t len (sizeof(size));
97     if (::getsockopt(fd(),SOL_SOCKET,SO_RCVBUF,&size,&len) < 0)
98         SENF_THROW_SYSTEM_EXCEPTION("");
99     // Linux doubles the bufer size on setting the RCVBUF to cater for internal
100     // headers. We fix this up here .. (see lkml FAQ)
101     return size/2;
102 }
103
104 prefix_ void senf::BSDSocketProtocol::rcvbuf(unsigned size)
105     const
106 {
107     if (::setsockopt(fd(),SOL_SOCKET,SO_RCVBUF,&size,sizeof(size)) < 0)
108         SENF_THROW_SYSTEM_EXCEPTION("");
109 }
110
111 prefix_ unsigned senf::BSDSocketProtocol::sndbuf()
112     const
113 {
114     unsigned size;
115     socklen_t len (sizeof(size));
116     if (::getsockopt(fd(),SOL_SOCKET,SO_SNDBUF,&size,&len) < 0)
117         SENF_THROW_SYSTEM_EXCEPTION("");
118     // Linux doubles the bufer size on setting the SNDBUF to cater for internal
119     // headers. We fix this up here .. (see lkml FAQ)
120     return size/2;
121 }
122
123 prefix_ void senf::BSDSocketProtocol::sndbuf(unsigned size)
124     const
125 {
126     if (::setsockopt(fd(),SOL_SOCKET,SO_SNDBUF,&size,sizeof(size)) < 0)
127         SENF_THROW_SYSTEM_EXCEPTION("");
128 }
129
130 //-/////////////////////////////////////////////////////////////////////////////////////////////////
131
132 prefix_ bool senf::AddressableBSDSocketProtocol::reuseaddr()
133     const
134 {
135     int value;
136     socklen_t len (sizeof(value));
137     if (::getsockopt(fd(),SOL_SOCKET,SO_REUSEADDR,&value,&len) < 0)
138         SENF_THROW_SYSTEM_EXCEPTION("");
139     return value;
140 }
141
142 prefix_ void senf::AddressableBSDSocketProtocol::reuseaddr(bool value)
143     const
144 {
145     int ivalue (value);
146     if (::setsockopt(fd(),SOL_SOCKET,SO_REUSEADDR,&ivalue,sizeof(ivalue)) < 0)
147         SENF_THROW_SYSTEM_EXCEPTION("");
148 }
149
150 //-/////////////////////////////////////////////////////////////////////////////////////////////////
151 #undef prefix_
152 //#include "BSDSocketProtocol.mpp"
153
154 \f
155 // Local Variables:
156 // mode: c++
157 // fill-column: 100
158 // c-file-style: "senf"
159 // indent-tabs-mode: nil
160 // ispell-local-dictionary: "american"
161 // compile-command: "scons -u test"
162 // comment-column: 40
163 // End: