c0bf37496f2b20e5cefde7cd64af628db69102e0
[senf.git] / senf / Socket / Protocols / BSDSocketProtocol.cc
1 // $Id$
2 //
3 // Copyright (C) 2006
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
6 //     Stefan Bund <g0dil@berlios.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
34 //#include "BSDSocketProtocol.mpp"
35 #define prefix_
36 //-/////////////////////////////////////////////////////////////////////////////////////////////////
37
38 prefix_ std::pair<bool,unsigned> senf::BSDSocketProtocol::linger()
39     const
40 {
41     struct linger ling;
42     socklen_t len = sizeof(ling);
43     ::memset(&ling, 0, sizeof(ling));
44     if (::getsockopt(fd(),SOL_SOCKET,SO_LINGER,&ling,&len) < 0)
45         SENF_THROW_SYSTEM_EXCEPTION("");
46     return std::make_pair(ling.l_onoff, ling.l_linger);
47 }
48
49 prefix_ void senf::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(fd(),SOL_SOCKET,SO_LINGER,&ling,sizeof(ling)) < 0)
56         SENF_THROW_SYSTEM_EXCEPTION("");
57 }
58
59 prefix_ boost::uint8_t senf::BSDSocketProtocol::priority()
60     const
61 {
62     int value;
63     socklen_t len (sizeof(value));
64     if (::getsockopt(fd(),SOL_SOCKET,SO_PRIORITY,&value,&len) < 0)
65         SENF_THROW_SYSTEM_EXCEPTION("");
66     return value;
67 }
68
69 prefix_ void senf::BSDSocketProtocol::priority(boost::uint8_t value)
70     const
71 {
72     int ivalue (value);
73     if (::setsockopt(fd(),SOL_SOCKET,SO_PRIORITY,&ivalue,sizeof(ivalue)) < 0)
74         SENF_THROW_SYSTEM_EXCEPTION("");
75 }
76
77 prefix_ int senf::BSDSocketProtocol::error()
78     const
79 {
80     int err;
81     socklen_t len (sizeof(err));
82     if (::getsockopt(fd(),SOL_SOCKET,SO_ERROR,&err,&len) < 0)
83         SENF_THROW_SYSTEM_EXCEPTION("");
84     return err;
85 }
86
87 prefix_ unsigned senf::BSDSocketProtocol::rcvbuf()
88     const
89 {
90     unsigned size;
91     socklen_t len (sizeof(size));
92     if (::getsockopt(fd(),SOL_SOCKET,SO_RCVBUF,&size,&len) < 0)
93         SENF_THROW_SYSTEM_EXCEPTION("");
94     // Linux doubles the bufer size on setting the RCVBUF to cater for internal
95     // headers. We fix this up here .. (see lkml FAQ)
96     return size/2;
97 }
98
99 prefix_ void senf::BSDSocketProtocol::rcvbuf(unsigned size)
100     const
101 {
102     if (::setsockopt(fd(),SOL_SOCKET,SO_RCVBUF,&size,sizeof(size)) < 0)
103         SENF_THROW_SYSTEM_EXCEPTION("");
104 }
105
106 prefix_ unsigned senf::BSDSocketProtocol::sndbuf()
107     const
108 {
109     unsigned size;
110     socklen_t len (sizeof(size));
111     if (::getsockopt(fd(),SOL_SOCKET,SO_SNDBUF,&size,&len) < 0)
112         SENF_THROW_SYSTEM_EXCEPTION("");
113     // Linux doubles the bufer size on setting the SNDBUF to cater for internal
114     // headers. We fix this up here .. (see lkml FAQ)
115     return size/2;
116 }
117
118 prefix_ void senf::BSDSocketProtocol::sndbuf(unsigned size)
119     const
120 {
121     if (::setsockopt(fd(),SOL_SOCKET,SO_SNDBUF,&size,sizeof(size)) < 0)
122         SENF_THROW_SYSTEM_EXCEPTION("");
123 }
124
125 //-/////////////////////////////////////////////////////////////////////////////////////////////////
126
127 prefix_ bool senf::AddressableBSDSocketProtocol::reuseaddr()
128     const
129 {
130     int value;
131     socklen_t len (sizeof(value));
132     if (::getsockopt(fd(),SOL_SOCKET,SO_REUSEADDR,&value,&len) < 0)
133         SENF_THROW_SYSTEM_EXCEPTION("");
134     return value;
135 }
136
137 prefix_ void senf::AddressableBSDSocketProtocol::reuseaddr(bool value)
138     const
139 {
140     int ivalue (value);
141     if (::setsockopt(fd(),SOL_SOCKET,SO_REUSEADDR,&ivalue,sizeof(ivalue)) < 0)
142         SENF_THROW_SYSTEM_EXCEPTION("");
143 }
144
145 //-/////////////////////////////////////////////////////////////////////////////////////////////////
146 #undef prefix_
147 //#include "BSDSocketProtocol.mpp"
148
149 \f
150 // Local Variables:
151 // mode: c++
152 // fill-column: 100
153 // c-file-style: "senf"
154 // indent-tabs-mode: nil
155 // ispell-local-dictionary: "american"
156 // compile-command: "scons -u test"
157 // comment-column: 40
158 // End: