int NET_UDP_LISTEN(AGENT_REQUEST *request, AGENT_RESULT *result) { char pattern[64], *port_str, *buffer = NULL; unsigned short port; zbx_uint64_t listen = 0; int ret = SYSINFO_RET_FAIL, n, buffer_alloc = 64 * ZBX_KIBIBYTE; if (1 < request->nparam) { SET_MSG_RESULT(result, zbx_strdup(NULL, "Too many parameters.")); return SYSINFO_RET_FAIL; } port_str = get_rparam(request, 0); if (NULL == port_str || SUCCEED != is_ushort(port_str, &port)) { SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid first parameter.")); return SYSINFO_RET_FAIL; } buffer = zbx_malloc(NULL, buffer_alloc); if (0 < (n = proc_read_file("/proc/net/udp", &buffer, &buffer_alloc))) { ret = SYSINFO_RET_OK; zbx_snprintf(pattern, sizeof(pattern), "%04X 00000000:0000 07", (unsigned int)port); buffer[n] = '\0'; if (NULL != strstr(buffer, pattern)) { listen = 1; goto out; } } if (0 < (n = proc_read_file("/proc/net/udp6", &buffer, &buffer_alloc))) { ret = SYSINFO_RET_OK; zbx_snprintf(pattern, sizeof(pattern), "%04X 00000000000000000000000000000000:0000 07", (unsigned int)port); buffer[n] = '\0'; if (NULL != strstr(buffer, pattern)) listen = 1; } out: zbx_free(buffer); SET_UI64_RESULT(result, listen); return ret; }
int NET_UDP_LISTEN(const char *cmd, const char *param, unsigned flags, AGENT_RESULT *result) { char tmp[MAX_STRING_LEN], pattern[64], *buffer = NULL; unsigned short port; zbx_uint64_t listen = 0; int ret = SYSINFO_RET_FAIL, n, buffer_alloc = 64 * ZBX_KIBIBYTE; if (num_param(param) > 1) return ret; if (0 != get_param(param, 1, tmp, sizeof(tmp))) return ret; if (SUCCEED != is_ushort(tmp, &port)) return ret; buffer = zbx_malloc(NULL, buffer_alloc); if (0 < (n = proc_read_file("/proc/net/udp", &buffer, &buffer_alloc))) { ret = SYSINFO_RET_OK; zbx_snprintf(pattern, sizeof(pattern), "%04X 00000000:0000 07", (unsigned int)port); buffer[n] = '\0'; if (NULL != strstr(buffer, pattern)) { listen = 1; goto out; } } if (0 < (n = proc_read_file("/proc/net/udp6", &buffer, &buffer_alloc))) { ret = SYSINFO_RET_OK; zbx_snprintf(pattern, sizeof(pattern), "%04X 00000000000000000000000000000000:0000 07", (unsigned int)port); buffer[n] = '\0'; if (NULL != strstr(buffer, pattern)) listen = 1; } out: zbx_free(buffer); SET_UI64_RESULT(result, listen); return ret; }