Skip to content

Commit 725bde1

Browse files
authored
Merge pull request cfengine#5721 from victormlg/connectable-ENT-10666
ENT-10666: Added new policy function isconnectable()
2 parents f58f7b8 + 776d9a8 commit 725bde1

File tree

2 files changed

+88
-0
lines changed

2 files changed

+88
-0
lines changed

examples/isconnectable.cf

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# Copyright 2025 Northern.tech AS
2+
3+
# This file is part of CFEngine 3 - written and maintained by Northern.tech AS.
4+
5+
# This program is free software; you can redistribute it and/or modify it
6+
# under the terms of the GNU General Public License as published by the
7+
# Free Software Foundation; version 3.
8+
9+
# This program is distributed in the hope that it will be useful,
10+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
# GNU General Public License for more details.
13+
14+
# You should have received a copy of the GNU General Public License
15+
# along with this program; if not, write to the Free Software
16+
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
17+
18+
# To the extent this program is licensed as part of the Enterprise
19+
# versions of Cfengine, the applicable Commercial Open Source License
20+
# (COSL) may apply to this file if you as a licensee so wish it. See
21+
# included file COSL.txt.
22+
23+
#+begin_src cfengine3
24+
body common control
25+
{
26+
bundlesequence => { "example" };
27+
}
28+
bundle agent example
29+
{
30+
classes:
31+
"isconnectable" expression => isconnectable("localhost", 22, "30");
32+
33+
reports:
34+
isconnectable::
35+
"Port is connectable";
36+
!isconnectable::
37+
"Port is not connectable";
38+
}
39+
#+end_src
40+
###############################################################################
41+
#+begin_src example_output
42+
#@ ```
43+
#@ R: Port is connectable
44+
#@ ```
45+
#+end_src

libpromises/evalfunction.c

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3039,6 +3039,39 @@ static FnCallResult FnCallReadTcp(ARG_UNUSED EvalContext *ctx,
30393039

30403040
/*********************************************************************/
30413041

3042+
static FnCallResult FnCallIsConnectable(ARG_UNUSED EvalContext *ctx,
3043+
ARG_UNUSED const Policy *policy,
3044+
const FnCall *fp,
3045+
const Rlist *finalargs)
3046+
{
3047+
assert(fp != NULL);
3048+
if (finalargs == NULL)
3049+
{
3050+
Log(LOG_LEVEL_ERR, "Function %s requires a host name, or IP address as first argument",
3051+
fp->name);
3052+
return FnFailure();
3053+
}
3054+
char *hostnameip = RlistScalarValue(finalargs);
3055+
if (finalargs->next == NULL)
3056+
{
3057+
Log(LOG_LEVEL_ERR, "Function %s requires a port number as second argument",
3058+
fp->name);
3059+
return FnFailure();
3060+
}
3061+
char *port = RlistScalarValue(finalargs->next);
3062+
long connect_timeout = (finalargs->next->next != NULL) ? IntFromString(RlistScalarValue(finalargs->next->next)) : CONNTIMEOUT;
3063+
3064+
char txtaddr[CF_MAX_IP_LEN] = "";
3065+
int sd = SocketConnect(hostnameip, port, connect_timeout, false, txtaddr, sizeof(txtaddr));
3066+
if (sd > -1) {
3067+
cf_closesocket(sd);
3068+
}
3069+
3070+
return FnReturnContext(sd > -1);
3071+
}
3072+
3073+
/*********************************************************************/
3074+
30423075
/**
30433076
* Look for the indices of a variable in #finalargs if it is an array.
30443077
*
@@ -9778,6 +9811,14 @@ static const FnCallArg IRANGE_ARGS[] =
97789811
{NULL, CF_DATA_TYPE_NONE, NULL}
97799812
};
97809813

9814+
static const FnCallArg ISCONNECTABLE_ARGS[] =
9815+
{
9816+
{CF_ANYSTRING, CF_DATA_TYPE_STRING, "Host name, domain name or IP address"},
9817+
{CF_ANYSTRING, CF_DATA_TYPE_STRING, "Port number"},
9818+
{CF_VALRANGE, CF_DATA_TYPE_INT, "Connection timeout (in seconds)"},
9819+
{NULL, CF_DATA_TYPE_NONE, NULL}
9820+
};
9821+
97819822
static const FnCallArg ISGREATERTHAN_ARGS[] =
97829823
{
97839824
{CF_ANYSTRING, CF_DATA_TYPE_STRING, "Larger string or value"},
@@ -10622,6 +10663,8 @@ const FnCallType CF_FNCALL_TYPES[] =
1062210663
FNCALL_OPTION_VARARG, FNCALL_CATEGORY_COMM, SYNTAX_STATUS_NORMAL),
1062310664
FnCallTypeNew("irange", CF_DATA_TYPE_INT_RANGE, IRANGE_ARGS, &FnCallIRange, "Define a range of integer values for cfengine internal use",
1062410665
FNCALL_OPTION_NONE, FNCALL_CATEGORY_DATA, SYNTAX_STATUS_NORMAL),
10666+
FnCallTypeNew("isconnectable", CF_DATA_TYPE_CONTEXT, ISCONNECTABLE_ARGS, &FnCallIsConnectable, "Check if a port is connectable",
10667+
FNCALL_OPTION_VARARG, FNCALL_CATEGORY_COMM, SYNTAX_STATUS_NORMAL),
1062510668
FnCallTypeNew("isdir", CF_DATA_TYPE_CONTEXT, FILESTAT_ARGS, &FnCallFileStat, "True if the named object is a directory",
1062610669
FNCALL_OPTION_NONE, FNCALL_CATEGORY_FILES, SYNTAX_STATUS_NORMAL),
1062710670
FnCallTypeNew("isexecutable", CF_DATA_TYPE_CONTEXT, FILESTAT_ARGS, &FnCallFileStat, "True if the named object has execution rights for the current user",

0 commit comments

Comments
 (0)