1/*
2 *
3 * Copyright 2018 gRPC authors.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 *     http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 */
18
19package channelz
20
21import (
22	"syscall"
23
24	"golang.org/x/sys/unix"
25)
26
27// SocketOptionData defines the struct to hold socket option data, and related
28// getter function to obtain info from fd.
29type SocketOptionData struct {
30	Linger      *unix.Linger
31	RecvTimeout *unix.Timeval
32	SendTimeout *unix.Timeval
33	TCPInfo     *unix.TCPInfo
34}
35
36// Getsockopt defines the function to get socket options requested by channelz.
37// It is to be passed to syscall.RawConn.Control().
38func (s *SocketOptionData) Getsockopt(fd uintptr) {
39	if v, err := unix.GetsockoptLinger(int(fd), syscall.SOL_SOCKET, syscall.SO_LINGER); err == nil {
40		s.Linger = v
41	}
42	if v, err := unix.GetsockoptTimeval(int(fd), syscall.SOL_SOCKET, syscall.SO_RCVTIMEO); err == nil {
43		s.RecvTimeout = v
44	}
45	if v, err := unix.GetsockoptTimeval(int(fd), syscall.SOL_SOCKET, syscall.SO_SNDTIMEO); err == nil {
46		s.SendTimeout = v
47	}
48	if v, err := unix.GetsockoptTCPInfo(int(fd), syscall.SOL_TCP, syscall.TCP_INFO); err == nil {
49		s.TCPInfo = v
50	}
51}
52