** Copyright (C) 2001-2023 Zabbix SIA
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 of the License, or
** (at your option) any later version.
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
"git.zabbix.com/ap/plugin-support/log"
const headerSize = 4 + 1 + 4 + 4
const tcpProtocol = byte(0x01)
const zlibCompress = byte(0x02)
connStateAccept = iota + 1
func open(address string, localAddr *net.Addr, timeout time.Duration, connect_timeout time.Duration, timeoutMode int,
args ...interface{}) (c *Connection, err error) {
c = &Connection{state: connStateConnect, compress: true, timeout: timeout, timeoutMode: timeoutMode}
d := net.Dialer{Timeout: connect_timeout, LocalAddr: *localAddr}
c.conn, err = d.Dial("tcp", address)
if err = c.conn.SetDeadline(time.Now().Add(timeout)); err != nil {
var tlsconfig *tls.Config
if tlsconfig, ok = args[0].(*tls.Config); !ok {
return nil, fmt.Errorf("invalid TLS configuration parameter of type %T", args[0])
c.conn, err = tls.NewClient(c.conn, tlsconfig, timeout, timeoutMode == TimeoutModeShift, address)
func (c *Connection) write(w io.Writer, data []byte) (err error) {