BSDAddressingPolicy.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 "BSDAddressingPolicy.hh"
19 //#include "BSDAddressingPolicy.ih"
20 
21 // Custom includes
22 #include <sys/socket.h>
23 #include <sys/types.h>
24 #include <senf/Utils/Exception.hh>
25 
26 //#include "BSDAddressingPolicy.mpp"
27 #define prefix_
28 //-/////////////////////////////////////////////////////////////////////////////////////////////////
29 
31  struct sockaddr * addr,
32  socklen_t * len)
33 {
34  if (::getsockname(handle.fd(),addr,len) < 0)
35  SENF_THROW_SYSTEM_EXCEPTION("could not get sockname");
36 }
37 
39  struct sockaddr * addr,
40  socklen_t * len)
41 {
42  if (::getpeername(handle.fd(),addr,len) < 0)
43  SENF_THROW_SYSTEM_EXCEPTION("could not get peername");
44 }
45 
47  struct sockaddr const * addr,
48  socklen_t len)
49 {
50  if (::bind(handle.fd(),addr,len) < 0)
51  SENF_THROW_SYSTEM_EXCEPTION("could not bind");
52 }
53 
55  struct sockaddr const * addr,
56  socklen_t len)
57 {
58  while(1) {
59  if (::connect(handle.fd(),addr,len) < 0)
60  switch (errno) {
61  case EINPROGRESS: {
62  handle.waitWriteable();
63  int err = 0;
64  socklen_t len = sizeof(err);
65  if (::getsockopt(handle.fd(),SOL_SOCKET,SO_ERROR,&err,&len) < 0)
66  SENF_THROW_SYSTEM_EXCEPTION("::getsockopt(SO_ERROR)");
67  if (err != 0)
69  return;
70  }
71  case EINTR:
72  break;
73  default:
74  SENF_THROW_SYSTEM_EXCEPTION("could not ::connect");
75  }
76  else
77  return;
78  }
79 }
80 
81 //-/////////////////////////////////////////////////////////////////////////////////////////////////
82 #undef prefix_
83 //#include "BSDAddressingPolicy.mpp"
84 
85 
86 // Local Variables:
87 // mode: c++
88 // fill-column: 100
89 // c-file-style: "senf"
90 // indent-tabs-mode: nil
91 // ispell-local-dictionary: "american"
92 // compile-command: "scons -u test"
93 // comment-column: 40
94 // End:
#define SENF_THROW_SYSTEM_EXCEPTION(desc)
static void do_local(FileHandle const &handle, struct sockaddr *addr, socklen_t *len)
static void do_peer(FileHandle const &handle, struct sockaddr *addr, socklen_t *len)
static void do_connect(FileHandle const &handle, struct sockaddr const *addr, socklen_t len)
bool waitWriteable(senf::ClockService::clock_type timeout=senf::ClockService::clock_type(-1)) const
Wait, until a write on the handle would not block (ignoring blocking state)
Basic file handle wrapper.
Definition: FileHandle.hh:102
#define prefix_
void connect(connector::FastActiveOutput< PacketType > &source, connector::FastPassiveInput< PacketType > &target)
int fd() const
Return the raw FileHandle.
#define SENF_EXC_DEBUGINFO
BSDAddressingPolicyMixin public header.
static void do_bind(FileHandle const &handle, struct sockaddr const *addr, socklen_t len)