Apache Mesos
network.hpp
Go to the documentation of this file.
1 // Licensed under the Apache License, Version 2.0 (the "License");
2 // you may not use this file except in compliance with the License.
3 // You may obtain a copy of the License at
4 //
5 // http://www.apache.org/licenses/LICENSE-2.0
6 //
7 // Unless required by applicable law or agreed to in writing, software
8 // distributed under the License is distributed on an "AS IS" BASIS,
9 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10 // See the License for the specific language governing permissions and
11 // limitations under the License
12 
13 #ifndef __PROCESS_NETWORK_HPP__
14 #define __PROCESS_NETWORK_HPP__
15 
16 #include <process/address.hpp>
17 
18 #include <stout/net.hpp>
19 #include <stout/try.hpp>
20 
21 #include <stout/os/socket.hpp>
22 
23 
24 namespace process {
25 namespace network {
26 
27 using net::socket;
28 
29 
30 // TODO(benh): Remove and defer to Socket::accept.
32 {
33  sockaddr_storage storage;
34  socklen_t length = sizeof(storage);
35 
36  int_fd accepted = net::accept(s, (sockaddr*) &storage, &length);
37  if (accepted < 0) {
38  return ErrnoError("Failed to accept");
39  }
40 
41  return accepted;
42 }
43 
44 
45 // TODO(benh): Remove and defer to Socket::bind.
47 {
48  sockaddr_storage storage = address;
49 
50  const socklen_t address_size = static_cast<socklen_t>(address.size());
51  if (net::bind(s, (sockaddr*) &storage, address_size) < 0) {
52  return ErrnoError("Failed to bind on " + stringify(address));
53  }
54 
55  return Nothing();
56 }
57 
58 
59 // TODO(benh): Remove and defer to Socket::connect.
61 {
62  sockaddr_storage storage = address;
63 
64  const socklen_t address_size = static_cast<socklen_t>(address.size());
65  if (net::connect(s, (sockaddr*) &storage, address_size) < 0) {
66  return SocketError("Failed to connect to " + stringify(address));
67  }
68 
69  return Nothing();
70 }
71 
72 
80 {
81  sockaddr_storage storage;
82  socklen_t length = sizeof(storage);
83 
84  if (::getsockname(s, (sockaddr*)&storage, &length) < 0) {
85  return ErrnoError("Failed to getsockname");
86  }
87 
88  return Address::create(storage, length);
89 }
90 
91 
99 {
100  sockaddr_storage storage;
101  socklen_t length = sizeof(storage);
102 
103  if (::getpeername(s, (sockaddr*)&storage, &length) < 0) {
104  return ErrnoError("Failed to getpeername");
105  }
106 
107  return Address::create(storage, length);
108 }
109 
110 } // namespace network {
111 } // namespace process {
112 
113 #endif // __PROCESS_NETWORK_HPP__
Try< int_fd > socket(int family, int type, int protocol)
Definition: socket.hpp:43
Definition: nothing.hpp:16
Definition: check.hpp:33
Try< Address > address(int_fd s)
Returns the Address with the assigned ip and assigned port.
Definition: network.hpp:79
Definition: address.hpp:324
Definition: errorbase.hpp:50
int bind(const int_fd &fd, const sockaddr *addr, socklen_t addrlen)
Definition: socket.hpp:143
size_t size() const
Definition: address.hpp:455
static Try< Address > create(const sockaddr_storage &storage, Option< socklen_t > length=None())
Definition: address.hpp:344
ErrnoError SocketError
Definition: error.hpp:33
int_fd accept(const int_fd &fd, sockaddr *addr, socklen_t *addrlen)
Definition: socket.hpp:132
Try< int_fd > accept(int_fd s)
Definition: network.hpp:31
Try< Nothing, SocketError > connect(int_fd s, const Address &address)
Definition: network.hpp:60
Definition: executor.hpp:48
Try< Address > peer(int_fd s)
Returns the peer&#39;s Address for the accepted or connected socket.
Definition: network.hpp:98
int connect(const int_fd &fd, const sockaddr *address, socklen_t addrlen)
Definition: socket.hpp:151
Try< Nothing > bind(int_fd s, const Address &address)
Definition: network.hpp:46
int int_fd
Definition: int_fd.hpp:35
std::string stringify(int flags)