TCPSocketHandle.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 
18 #include "TCPSocketHandle.hh"
19 //#include "TCPSocketHandle.ih"
20 
21 // Custom includes
22 #include <sys/types.h>
23 #include <sys/socket.h>
24 #include <sys/ioctl.h>
25 
26 #include <senf/Utils/Exception.hh>
27 
28 //#include "TCPSocketHandle.mpp"
29 #define prefix_
30 //-/////////////////////////////////////////////////////////////////////////////////////////////////
31 
32 //-/////////////////////////////////////////////////////////////////////////////////////////////////
33 // senf::TCPv4SocketProtocol
34 
36  const
37 {
38  int sock = ::socket(PF_INET,SOCK_STREAM | SOCK_CLOEXEC, 0);
39  if (sock < 0)
40  SENF_THROW_SYSTEM_EXCEPTION("could not open TCPv4Socket");
41  fd(sock);
42 }
43 
44 prefix_ void
46  const
47 {
48  init_client();
49  clientHandle().connect(address);
50 }
51 
53  const
54 {
55  int sock = ::socket(PF_INET,SOCK_STREAM | SOCK_CLOEXEC, 0);
56  if (sock < 0)
57  SENF_THROW_SYSTEM_EXCEPTION("could not open TCPv4Socket");
58  fd(sock);
59 }
60 
62  unsigned backlog)
63  const
64 {
65  init_server();
66  reuseaddr(true);
67  serverHandle().bind(address);
68  if (::listen(fd(),backlog) < 0)
69  SENF_THROW_SYSTEM_EXCEPTION("could not listen on TCPv4Socket");
70 }
71 
72 //-/////////////////////////////////////////////////////////////////////////////////////////////////
73 // senf::TCPv6SocketProtocol::
74 
76  const
77 {
78  int sock = ::socket(PF_INET6,SOCK_STREAM | SOCK_CLOEXEC, 0);
79  if (sock < 0)
80  SENF_THROW_SYSTEM_EXCEPTION("could not open TCPv6Socket");
81  fd(sock);
82 }
83 
84 prefix_ void
86  const
87 {
88  init_client();
89  clientHandle().connect(address);
90 }
91 
93  const
94 {
95  int sock = ::socket(PF_INET6,SOCK_STREAM | SOCK_CLOEXEC, 0);
96  if (sock < 0)
97  SENF_THROW_SYSTEM_EXCEPTION("could not open TCPv6Socket");
98  fd(sock);
99 }
100 
102  unsigned backlog)
103  const
104 {
105  init_server();
106  serverHandle().bind(address);
107  reuseaddr(true);
108  if (::listen(fd(),backlog) < 0)
109  SENF_THROW_SYSTEM_EXCEPTION("could not listen on TCPv6Socket");
110 }
111 
112 //-/////////////////////////////////////////////////////////////////////////////////////////////////
113 #undef prefix_
114 //#include "TCPSocketHandle.mpp"
115 
116 
117 // Local Variables:
118 // mode: c++
119 // fill-column: 100
120 // c-file-style: "senf"
121 // indent-tabs-mode: nil
122 // ispell-local-dictionary: "american"
123 // compile-command: "scons -u test"
124 // comment-column: 40
125 // End:
void init_client() const
Create unconnected client socket.
ClientSocketHandle< Policy > clientHandle() const
Get client handle for associated socket.
#define SENF_THROW_SYSTEM_EXCEPTION(desc)
IPv6 socket address.
bool reuseaddr() const
Return current reuseaddr state.
int fd() const
Get file descriptor.
IPv4 socket address.
ServerSocketHandle< Policy > serverHandle() const
Get server handle for associated socket.
TCPv4SocketHandle and TCPv6SocketHandle public header.
void init_client() const
Create unconnected client socket.
void init_server() const
Create server socket.
#define prefix_
void init_server() const
Create server socket.