Apache Mesos
socket.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 __STOUT_OS_POSIX_SOCKET_HPP__
14 #define __STOUT_OS_POSIX_SOCKET_HPP__
15 
16 #include <errno.h>
17 #include <unistd.h>
18 
19 #include <sys/socket.h>
20 #include <sys/stat.h>
21 
22 namespace net {
23 
24 // Import `socket` functions into `net::` namespace.
30 
31 
32 // Returns a socket file descriptor for the specified options.
33 // NOTE: on OS X, the returned socket will have the SO_NOSIGPIPE option set.
34 inline Try<int_fd> socket(int family, int type, int protocol)
35 {
36  int_fd s;
37  if ((s = ::socket(family, type, protocol)) < 0) {
38  return ErrnoError();
39  }
40 
41 #ifdef __APPLE__
42  // Disable SIGPIPE via setsockopt because OS X does not support
43  // the MSG_NOSIGNAL flag on send(2).
44  const int enable = 1;
45  if (setsockopt(s, SOL_SOCKET, SO_NOSIGPIPE, &enable, sizeof(int)) == -1) {
46  return ErrnoError();
47  }
48 #endif // __APPLE__
49 
50  return s;
51 }
52 
53 
54 // The error indicates the last socket operation has been
55 // interupted, the operation can be restarted immediately.
56 inline bool is_restartable_error(int error)
57 {
58  return (error == EINTR);
59 }
60 
61 
62 // The error indicates the last socket function on a non-blocking socket
63 // cannot be completed. This is a temporary condition and the caller can
64 // retry the operation later.
65 inline bool is_retryable_error(int error)
66 {
67  return (error == EWOULDBLOCK || error == EAGAIN);
68 }
69 
70 
71 inline bool is_inprogress_error(int error)
72 {
73  return (error == EINPROGRESS);
74 }
75 
76 
77 inline bool is_socket(int fd)
78 {
79  struct stat statbuf;
80  if (::fstat(fd, &statbuf) < 0) {
81  return false;
82  }
83 
84  return S_ISSOCK(statbuf.st_mode) != 0;
85 }
86 
87 } // namespace net {
88 
89 #endif // __STOUT_OS_POSIX_SOCKET_HPP__
Try< int_fd > socket(int family, int type, int protocol)
Definition: socket.hpp:34
bool is_socket(int fd)
Definition: socket.hpp:77
Try< Nothing > enable(const std::string &hierarchy, const std::string &cgroup)
Definition: check.hpp:33
bool is_inprogress_error(int error)
Definition: socket.hpp:71
bool S_ISSOCK(const int mode)
Definition: windows.hpp:230
Definition: errorbase.hpp:50
int bind(const int_fd &fd, const sockaddr *addr, socklen_t addrlen)
Definition: socket.hpp:143
int_fd accept(const int_fd &fd, sockaddr *addr, socklen_t *addrlen)
Definition: socket.hpp:132
bool is_restartable_error(int error)
Definition: socket.hpp:56
ssize_t send(const int_fd &fd, const void *buf, size_t len, int flags)
Definition: socket.hpp:159
Definition: ip.hpp:70
ssize_t recv(const int_fd &fd, void *buf, size_t len, int flags)
Definition: socket.hpp:168
Try< hashmap< std::string, uint64_t > > stat(const std::string &hierarchy, const std::string &cgroup, const std::string &file)
std::string error(const std::string &msg, uint32_t code)
Try< uint32_t > type(const std::string &path)
int connect(const int_fd &fd, const sockaddr *address, socklen_t addrlen)
Definition: socket.hpp:151
int int_fd
Definition: int_fd.hpp:35
bool is_retryable_error(int error)
Definition: socket.hpp:65