1 /** @file
2 
3   A brief file description
4 
5   @section license License
6 
7   Licensed to the Apache Software Foundation (ASF) under one
8   or more contributor license agreements.  See the NOTICE file
9   distributed with this work for additional information
10   regarding copyright ownership.  The ASF licenses this file
11   to you under the Apache License, Version 2.0 (the
12   "License"); you may not use this file except in compliance
13   with the License.  You may obtain a copy of the License at
14 
15       http://www.apache.org/licenses/LICENSE-2.0
16 
17   Unless required by applicable law or agreed to in writing, software
18   distributed under the License is distributed on an "AS IS" BASIS,
19   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20   See the License for the specific language governing permissions and
21   limitations under the License.
22  */
23 
24 /****************************************************************************
25 
26   SocketManager.cc
27  ****************************************************************************/
28 #include "tscore/ink_platform.h"
29 #include "P_EventSystem.h"
30 
31 #include "tscore/TextBuffer.h"
32 #include "tscore/TestBox.h"
33 
34 SocketManager socketManager;
35 
36 #if !HAVE_ACCEPT4
37 static int
38 accept4(int sockfd, struct sockaddr *addr, socklen_t *addrlen, int flags)
39 {
40   int fd, err;
41 
42   do {
43     fd = accept(sockfd, addr, addrlen);
44     if (likely(fd >= 0))
45       break;
46   } while (transient_error());
47 
48   if ((fd >= 0) && (flags & SOCK_CLOEXEC) && (safe_fcntl(fd, F_SETFD, FD_CLOEXEC) < 0)) {
49     err = errno;
50     close(fd);
51     errno = err;
52     return -1;
53   }
54 
55   if ((fd >= 0) && (flags & SOCK_NONBLOCK) && (safe_nonblocking(fd) < 0)) {
56     err = errno;
57     close(fd);
58     errno = err;
59     return -1;
60   }
61 
62   return fd;
63 }
64 #endif
65 
66 int
67 SocketManager::accept4(int s, struct sockaddr *addr, socklen_t *addrlen, int flags)
68 {
69   do {
70     int fd = ::accept4(s, addr, addrlen, flags);
71     if (likely(fd >= 0)) {
72       return fd;
73     }
74   } while (transient_error());
75 
76   return -errno;
77 }
78 
79 SocketManager::SocketManager() : pagesize(ats_pagesize()) {}
80 
81 SocketManager::~SocketManager()
82 {
83   // free the hash table and values
84 }
85 
86 int
87 SocketManager::ink_bind(int s, struct sockaddr const *name, int namelen, short Proto)
88 {
89   (void)Proto;
90   return safe_bind(s, name, namelen);
91 }
92 
93 int
94 SocketManager::close(int s)
95 {
96   int res;
97 
98   if (s == 0) {
99     return -EACCES;
100   } else if (s < 0) {
101     return -EINVAL;
102   }
103 
104   do {
105     res = ::close(s);
106     if (res == -1) {
107       res = -errno;
108     }
109   } while (res == -EINTR);
110   return res;
111 }
112 
113 bool
114 SocketManager::fastopen_supported()
115 {
116   static const unsigned TFO_CLIENT_ENABLE = 1;
117 
118   ats_scoped_fd fd(::open("/proc/sys/net/ipv4/tcp_fastopen", O_RDONLY));
119   int value = 0;
120 
121   if (fd) {
122     TextBuffer buffer(16);
123 
124     buffer.slurp(fd.get());
125     value = atoi(buffer.bufPtr());
126   }
127 
128   return value & TFO_CLIENT_ENABLE;
129 }
130 
131 REGRESSION_TEST(socket_fastopen)(RegressionTest *test, int level, int *pstatus)
132 {
133   TestBox box(test, pstatus);
134 
135   box = REGRESSION_TEST_PASSED;
136 
137   if (SocketManager::fastopen_supported()) {
138     box.check(MSG_FASTOPEN != 0, "TCP Fast Open is supported, MSG_FASTOPEN must not be 0");
139   }
140 
141   if (::access("/proc/sys/net/ipv4/tcp_fastopen", F_OK) == 0) {
142     box.check(MSG_FASTOPEN != 0, "TCP Fast Open is available, MSG_FASTOPEN must not be 0");
143   }
144 }
145