• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2018 The Android Open Source Project
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 #include "netdutils/SocketOption.h"
18 
19 #include <netinet/in.h>
20 #include <netinet/tcp.h>
21 #include <sys/socket.h>
22 #include <utility>
23 
24 #include "netdutils/Syscalls.h"
25 
26 namespace android {
27 namespace netdutils {
28 
enableSockopt(Fd sock,int level,int optname)29 Status enableSockopt(Fd sock, int level, int optname) {
30     auto& sys = sSyscalls.get();
31     const int on = 1;
32     return sys.setsockopt(sock, level, optname, &on, sizeof(on));
33 }
34 
enableTcpKeepAlives(Fd sock,unsigned idleTime,unsigned numProbes,unsigned probeInterval)35 Status enableTcpKeepAlives(Fd sock, unsigned idleTime, unsigned numProbes, unsigned probeInterval) {
36     RETURN_IF_NOT_OK(enableSockopt(sock, SOL_SOCKET, SO_KEEPALIVE));
37 
38     auto& sys = sSyscalls.get();
39     if (idleTime != 0) {
40         RETURN_IF_NOT_OK(sys.setsockopt(sock, SOL_TCP, TCP_KEEPIDLE, &idleTime, sizeof(idleTime)));
41     }
42     if (numProbes != 0) {
43         RETURN_IF_NOT_OK(sys.setsockopt(sock, SOL_TCP, TCP_KEEPCNT, &numProbes, sizeof(numProbes)));
44     }
45     if (probeInterval != 0) {
46         RETURN_IF_NOT_OK(sys.setsockopt(sock, SOL_TCP, TCP_KEEPINTVL, &probeInterval,
47                 sizeof(probeInterval)));
48     }
49 
50     return status::ok;
51 }
52 
53 }  // namespace netdutils
54 }  // namespace android
55