proxygen
Sockets.h
Go to the documentation of this file.
1 /*
2  * Copyright 2016-present Facebook, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #pragma once
18 
19 #include <folly/net/NetOps.h>
20 
21 namespace folly {
22 namespace portability {
23 namespace sockets {
24 #ifndef _WIN32
31 using ::inet_ntop;
42 #else
43 // Some Windows specific helper functions.
44 bool is_fh_socket(int fh);
45 SOCKET fd_to_socket(int fd);
46 int socket_to_fd(SOCKET s);
47 int translate_wsa_error(int wsaErr);
48 
49 // These aren't additional overloads, but rather other functions that
50 // are referenced that we need to wrap, or, in the case of inet_aton,
51 // implement.
52 int accept(int s, struct sockaddr* addr, socklen_t* addrlen);
53 int inet_aton(const char* cp, struct in_addr* inp);
54 int socketpair(int domain, int type, int protocol, int sv[2]);
55 
56 // Unless you have a case where you would normally have
57 // to reference the function as being explicitly in the
58 // global scope, then you shouldn't be calling these directly.
59 int bind(int s, const struct sockaddr* name, socklen_t namelen);
60 int connect(int s, const struct sockaddr* name, socklen_t namelen);
61 int getpeername(int s, struct sockaddr* name, socklen_t* namelen);
62 int getsockname(int s, struct sockaddr* name, socklen_t* namelen);
63 int getsockopt(int s, int level, int optname, void* optval, socklen_t* optlen);
64 const char* inet_ntop(int af, const void* src, char* dst, socklen_t size);
65 int listen(int s, int backlog);
66 int poll(struct pollfd fds[], nfds_t nfds, int timeout);
67 ssize_t recv(int s, void* buf, size_t len, int flags);
68 ssize_t recvfrom(
69  int s,
70  void* buf,
71  size_t len,
72  int flags,
73  struct sockaddr* from,
74  socklen_t* fromlen);
75 ssize_t send(int s, const void* buf, size_t len, int flags);
76 ssize_t sendto(
77  int s,
78  const void* buf,
79  size_t len,
80  int flags,
81  const sockaddr* to,
82  socklen_t tolen);
83 ssize_t sendmsg(int socket, const struct msghdr* message, int flags);
84 int setsockopt(
85  int s,
86  int level,
87  int optname,
88  const void* optval,
89  socklen_t optlen);
90 int shutdown(int s, int how);
91 
92 // This is the only function that _must_ be referenced via the namespace
93 // because there is no difference in parameter types to overload
94 // on.
95 int socket(int af, int type, int protocol);
96 
97 // Windows needs a few extra overloads of some of the functions in order to
98 // resolve to our portability functions rather than the SOCKET accepting
99 // ones.
100 int getsockopt(int s, int level, int optname, char* optval, socklen_t* optlen);
101 ssize_t recv(int s, char* buf, int len, int flags);
102 ssize_t recv(int s, void* buf, int len, int flags);
103 ssize_t recvfrom(
104  int s,
105  char* buf,
106  int len,
107  int flags,
108  struct sockaddr* from,
109  socklen_t* fromlen);
110 ssize_t recvfrom(
111  int s,
112  void* buf,
113  int len,
114  int flags,
115  struct sockaddr* from,
116  socklen_t* fromlen);
117 ssize_t recvmsg(int s, struct msghdr* message, int fl);
118 ssize_t send(int s, const char* buf, int len, int flags);
119 ssize_t send(int s, const void* buf, int len, int flags);
120 ssize_t sendto(
121  int s,
122  const char* buf,
123  int len,
124  int flags,
125  const sockaddr* to,
126  socklen_t tolen);
127 ssize_t sendto(
128  int s,
129  const void* buf,
130  int len,
131  int flags,
132  const sockaddr* to,
133  socklen_t tolen);
134 int setsockopt(
135  int s,
136  int level,
137  int optname,
138  const char* optval,
139  socklen_t optlen);
140 #endif
141 } // namespace sockets
142 } // namespace portability
143 } // namespace folly
144 
145 #ifdef _WIN32
146 // Add our helpers to the overload set.
147 /* using override */
148 using namespace folly::portability::sockets;
149 #endif
ssize_t recvfrom(NetworkSocket s, void *buf, size_t len, int flags, sockaddr *from, socklen_t *fromlen)
Definition: NetOps.cpp:207
Definition: test.c:42
flags
Definition: http_parser.h:127
int connect(NetworkSocket s, const sockaddr *name, socklen_t namelen)
Definition: NetOps.cpp:94
ssize_t sendmsg(NetworkSocket socket, const msghdr *message, int flags)
Definition: NetOps.cpp:328
int inet_aton(const char *cp, in_addr *inp)
Definition: NetOps.cpp:132
int setsockopt(NetworkSocket s, int level, int optname, const void *optval, socklen_t optlen)
Definition: NetOps.cpp:384
PskType type
—— Concurrent Priority Queue Implementation ——
Definition: AtomicBitSet.h:29
const char * name
Definition: http_parser.c:437
int getsockname(NetworkSocket s, sockaddr *name, socklen_t *namelen)
Definition: NetOps.cpp:108
constexpr auto size(C const &c) -> decltype(c.size())
Definition: Access.h:45
ssize_t sendto(NetworkSocket s, const void *buf, size_t len, int flags, const sockaddr *to, socklen_t tolen)
Definition: NetOps.cpp:369
std::enable_if< detail::is_chrono_conversion< Tgt, Src >::value, Tgt >::type to(const Src &value)
Definition: Conv.h:677
int getsockopt(NetworkSocket s, int level, int optname, void *optval, socklen_t *optlen)
Definition: NetOps.cpp:112
void shutdown(Counter &)
ssize_t send(NetworkSocket s, const void *buf, size_t len, int flags)
Definition: NetOps.cpp:319
NetworkSocket socket(int af, int type, int protocol)
Definition: NetOps.cpp:412
int listen(NetworkSocket s, int backlog)
Definition: NetOps.cpp:137
PUSHMI_INLINE_VAR constexpr struct folly::pushmi::operators::from_fn from
ssize_t recvmsg(NetworkSocket s, msghdr *message, int flags)
Definition: NetOps.cpp:268
int poll(PollDescriptor fds[], nfds_t nfds, int timeout)
Definition: NetOps.cpp:141
int bind(NetworkSocket s, const sockaddr *name, socklen_t namelen)
Definition: NetOps.cpp:76
static set< string > s
int getpeername(NetworkSocket s, sockaddr *name, socklen_t *namelen)
Definition: NetOps.cpp:104
ssize_t recv(NetworkSocket s, void *buf, size_t len, int flags)
Definition: NetOps.cpp:180
ThreadPoolListHook * addr
int socketpair(int domain, int type, int protocol, NetworkSocket sv[2])
Definition: NetOps.cpp:416
NetworkSocket accept(NetworkSocket s, sockaddr *addr, socklen_t *addrlen)
Definition: NetOps.cpp:71