BSDSocketProtocol.cc
Go to the documentation of this file.
1 //
2 // Copyright (c) 2020 Fraunhofer Institute for Applied Information Technology (FIT)
3 // Network Research Group (NET)
4 // Schloss Birlinghoven, 53754 Sankt Augustin, GERMANY
5 // Contact: support@wiback.org
6 //
7 // This file is part of the SENF code tree.
8 // It is licensed under the 3-clause BSD License (aka New BSD License).
9 // See LICENSE.txt in the top level directory for details or visit
10 // https://opensource.org/licenses/BSD-3-Clause
11 //
12 
13 
17 #include <senf/Utils/String.hh>
18 #include "BSDSocketProtocol.hh"
19 //#include "BSDSocketProtocol.ih"
20 
21 // Custom includes
22 #include <linux/filter.h>
23 #include <sys/types.h>
24 #include <sys/socket.h>
25 #include <sys/ioctl.h>
26 #include <netinet/ip.h>
27 
28 
29 //#include "BSDSocketProtocol.mpp"
30 #define prefix_
31 //-/////////////////////////////////////////////////////////////////////////////////////////////////
32 
33 prefix_ std::pair<bool,unsigned> senf::BSDSocketProtocol::linger()
34  const
35 {
36  struct linger ling;
37  socklen_t len = sizeof(ling);
38  ::memset(&ling, 0, sizeof(ling));
39  if (::getsockopt(fd(),SOL_SOCKET,SO_LINGER,&ling,&len) < 0)
40  SENF_THROW_SYSTEM_EXCEPTION("could not get socketopt SO_LINGER");
41  return std::make_pair(ling.l_onoff, ling.l_linger);
42 }
43 
44 prefix_ void senf::BSDSocketProtocol::linger(bool enable, unsigned timeout)
45  const
46 {
47  struct linger ling;
48  ling.l_onoff = enable;
49  ling.l_linger = timeout;
50  if (::setsockopt(fd(),SOL_SOCKET,SO_LINGER,&ling,sizeof(ling)) < 0)
51  SENF_THROW_SYSTEM_EXCEPTION("could not set socketopt SO_LINGER");
52 }
53 
55  const
56 {
57  int value;
58  socklen_t len (sizeof(value));
59  if (::getsockopt(fd(),SOL_SOCKET,SO_PRIORITY,&value,&len) < 0)
60  SENF_THROW_SYSTEM_EXCEPTION("could not get socketopt SO_PRIORITY");
61  return value;
62 }
63 
64 prefix_ void senf::BSDSocketProtocol::priority(boost::uint8_t value)
65  const
66 {
67  int ivalue (value);
68  if (::setsockopt(fd(),SOL_SOCKET,SO_PRIORITY,&ivalue,sizeof(ivalue)) < 0)
69  SENF_THROW_SYSTEM_EXCEPTION("could not set socketopt SO_PRIORITY to " + senf::str(value));
70 }
71 
73  const
74 {
75  int err;
76  socklen_t len (sizeof(err));
77  if (::getsockopt(fd(),SOL_SOCKET,SO_ERROR,&err,&len) < 0)
78  SENF_THROW_SYSTEM_EXCEPTION("could not get socketopt SO_ERROR");
79  return err;
80 }
81 
83  const
84 {
85  unsigned size;
86  socklen_t len (sizeof(size));
87  if (::getsockopt(fd(),SOL_SOCKET,SO_RCVBUF,&size,&len) < 0)
88  SENF_THROW_SYSTEM_EXCEPTION("could not get socketopt SO_RCVBUF");
89  // Linux doubles the bufer size on setting the RCVBUF to cater for internal
90  // headers. We fix this up here .. (see lkml FAQ)
91  return size/2;
92 }
93 
95  const
96 {
97  if (::setsockopt(fd(),SOL_SOCKET,SO_RCVBUF,&size,sizeof(size)) < 0)
98  SENF_THROW_SYSTEM_EXCEPTION("could not set socketopt SO_RCVBUF to" + senf::str(size));
99 }
100 
102  const
103 {
104  unsigned size;
105  socklen_t len (sizeof(size));
106  if (::getsockopt(fd(),SOL_SOCKET,SO_SNDBUF,&size,&len) < 0)
107  SENF_THROW_SYSTEM_EXCEPTION("could not get socketopt SO_SNDBUF");
108  // Linux doubles the bufer size on setting the SNDBUF to cater for internal
109  // headers. We fix this up here .. (see lkml FAQ)
110  return size/2;
111 }
112 
114  const
115 {
116  if (::setsockopt(fd(),SOL_SOCKET,SO_SNDBUF,&size,sizeof(size)) < 0)
117  SENF_THROW_SYSTEM_EXCEPTION("could not set socketopt SO_SNDBUF to " + senf::str(size));
118 }
119 
121 {
122  int dummy;
123  if (::setsockopt(fd(), SOL_SOCKET, SO_DETACH_FILTER, &dummy, sizeof(dummy)) < 0)
124  SENF_THROW_SYSTEM_EXCEPTION("could not detach socket filter");
125 }
126 
127 prefix_ void senf::BSDSocketProtocol::do_attachSocketFilter(::sock_filter * filter, unsigned short len)
128 {
129  struct sock_fprog sockArg;
130  sockArg.filter = filter;
131  sockArg.len = len;
132  if (::setsockopt(fd(), SOL_SOCKET, SO_ATTACH_FILTER, &sockArg, sizeof(sockArg)) < 0)
133  SENF_THROW_SYSTEM_EXCEPTION("could not attach socket filter");
134 }
135 
137  const
138 {
139  if (::setsockopt(fd(), IPPROTO_IP, IP_MTU_DISCOVER, &mode, sizeof(mode)) < 0)
140  SENF_THROW_SYSTEM_EXCEPTION("could not set IP_MTU_DISCOVER mode to " + senf::str(mode));
141 }
142 
144  const
145 {
146  if (::setsockopt(fd(), SOL_SOCKET, SO_SNDLOWAT, &lowat, sizeof(lowat)) < 0)
147  SENF_THROW_SYSTEM_EXCEPTION("could not set SO_SNDLOWAT to " + senf::str(lowat));
148 }
149 
151  const
152 {
153  if (::setsockopt(fd(), SOL_SOCKET, SO_RCVLOWAT, &lowat, sizeof(lowat)) < 0)
154  SENF_THROW_SYSTEM_EXCEPTION("could not set SO_RCVLOWAT to " + senf::str(lowat));
155 }
156 
157 //-/////////////////////////////////////////////////////////////////////////////////////////////////
158 
160  const
161 {
162  int value;
163  socklen_t len (sizeof(value));
164  if (::getsockopt(fd(),SOL_SOCKET,SO_REUSEADDR,&value,&len) < 0)
165  SENF_THROW_SYSTEM_EXCEPTION("could not get socketopt SO_REUSEADDR");
166  return value;
167 }
168 
170  const
171 {
172  int ivalue (value);
173  if (::setsockopt(fd(),SOL_SOCKET,SO_REUSEADDR,&ivalue,sizeof(ivalue)) < 0)
174  SENF_THROW_SYSTEM_EXCEPTION("could not set socketopt SO_REUSEADDR");
175 }
176 
177 //-/////////////////////////////////////////////////////////////////////////////////////////////////
178 #undef prefix_
179 //#include "BSDSocketProtocol.mpp"
180 
181 
182 // Local Variables:
183 // mode: c++
184 // fill-column: 100
185 // c-file-style: "senf"
186 // indent-tabs-mode: nil
187 // ispell-local-dictionary: "american"
188 // compile-command: "scons -u test"
189 // comment-column: 40
190 // End:
void mtuDiscovery(int mode) const
#define SENF_THROW_SYSTEM_EXCEPTION(desc)
unsigned rcvbuf() const
Check receive buffer size.
bool reuseaddr() const
Return current reuseaddr state.
int fd() const
Get file descriptor.
void sndLowat(unsigned lowWat) const
#define prefix_
unsigned sndbuf() const
Check send buffer size.
boost::uint8_t priority() const
Get packet priority assigned to outgoing packets.
std::pair< bool, unsigned > linger() const
Return current linger status.
int error() const
Get and clear pending socket error.
void rcvLowat(unsigned lowWat) const
BSDSocketProtocol public header.