Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 1 | // +build !appengine |
| 2 | |
| 3 | /* |
| 4 | * |
| 5 | * Copyright 2018 gRPC authors. |
| 6 | * |
| 7 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | * you may not use this file except in compliance with the License. |
| 9 | * You may obtain a copy of the License at |
| 10 | * |
| 11 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | * |
| 13 | * Unless required by applicable law or agreed to in writing, software |
| 14 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | * See the License for the specific language governing permissions and |
| 17 | * limitations under the License. |
| 18 | * |
| 19 | */ |
| 20 | |
| 21 | // Package internal contains credentials-internal code. |
| 22 | package internal |
| 23 | |
| 24 | import ( |
| 25 | "net" |
| 26 | "syscall" |
| 27 | ) |
| 28 | |
| 29 | type sysConn = syscall.Conn |
| 30 | |
| 31 | // syscallConn keeps reference of rawConn to support syscall.Conn for channelz. |
| 32 | // SyscallConn() (the method in interface syscall.Conn) is explicitly |
| 33 | // implemented on this type, |
| 34 | // |
| 35 | // Interface syscall.Conn is implemented by most net.Conn implementations (e.g. |
| 36 | // TCPConn, UnixConn), but is not part of net.Conn interface. So wrapper conns |
| 37 | // that embed net.Conn don't implement syscall.Conn. (Side note: tls.Conn |
| 38 | // doesn't embed net.Conn, so even if syscall.Conn is part of net.Conn, it won't |
| 39 | // help here). |
| 40 | type syscallConn struct { |
| 41 | net.Conn |
| 42 | // sysConn is a type alias of syscall.Conn. It's necessary because the name |
| 43 | // `Conn` collides with `net.Conn`. |
| 44 | sysConn |
| 45 | } |
| 46 | |
| 47 | // WrapSyscallConn tries to wrap rawConn and newConn into a net.Conn that |
| 48 | // implements syscall.Conn. rawConn will be used to support syscall, and newConn |
| 49 | // will be used for read/write. |
| 50 | // |
| 51 | // This function returns newConn if rawConn doesn't implement syscall.Conn. |
| 52 | func WrapSyscallConn(rawConn, newConn net.Conn) net.Conn { |
| 53 | sysConn, ok := rawConn.(syscall.Conn) |
| 54 | if !ok { |
| 55 | return newConn |
| 56 | } |
| 57 | return &syscallConn{ |
| 58 | Conn: newConn, |
| 59 | sysConn: sysConn, |
| 60 | } |
| 61 | } |