#include "common.h"
#include "sysinfo.h"
#include "comms.h"
#include "log.h"
#include "cfg.h"
#include "net.h"
#include "zbxalgo.h"
int tcp_expect(const char *host, unsigned short port, int timeout, const char *request,
int (*validate_func)(const char *), const char *sendtoclose, int *value_int)
{
zbx_socket_t s;
const char *buf;
int net, val = ZBX_TCP_EXPECT_OK;
*value_int = 0;
if (SUCCEED != (net = zbx_tcp_connect(&s, CONFIG_SOURCE_IP, host, port, timeout, ZBX_TCP_SEC_UNENCRYPTED, NULL,
NULL)))
{
goto out;
}
if (NULL != request)
net = zbx_tcp_send_raw(&s, request);
if (NULL != validate_func && SUCCEED == net)
{
val = ZBX_TCP_EXPECT_FAIL;
while (NULL != (buf = zbx_tcp_recv_line(&s)))
{
val = validate_func(buf);
if (ZBX_TCP_EXPECT_OK == val)
break;
if (ZBX_TCP_EXPECT_FAIL == val)
{
zabbix_log(LOG_LEVEL_DEBUG, "TCP expect content error, received [%s]", buf);
break;
}
}
}
if (NULL != sendtoclose && SUCCEED == net && ZBX_TCP_EXPECT_OK == val)
(void)zbx_tcp_send_raw(&s, sendtoclose);
if (SUCCEED == net && ZBX_TCP_EXPECT_OK == val)
*value_int = 1;
zbx_tcp_close(&s);
out:
if (SUCCEED != net)
zabbix_log(LOG_LEVEL_DEBUG, "TCP expect network error: %s", zbx_socket_strerror());
return SYSINFO_RET_OK;
}
int NET_TCP_PORT(AGENT_REQUEST *request, AGENT_RESULT *result)
{
unsigned short port;
int value_int, ret;
char *ip_str, ip[MAX_ZBX_DNSNAME_LEN + 1], *port_str;
if (2 < request->nparam)
{
SET_MSG_RESULT(result, zbx_strdup(NULL, "Too many parameters."));
return SYSINFO_RET_FAIL;
}