@@ -3039,6 +3039,39 @@ static FnCallResult FnCallReadTcp(ARG_UNUSED EvalContext *ctx,
3039
3039
3040
3040
/*********************************************************************/
3041
3041
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
+
3042
3075
/**
3043
3076
* Look for the indices of a variable in #finalargs if it is an array.
3044
3077
*
@@ -9778,6 +9811,14 @@ static const FnCallArg IRANGE_ARGS[] =
9778
9811
{NULL , CF_DATA_TYPE_NONE , NULL }
9779
9812
};
9780
9813
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
+
9781
9822
static const FnCallArg ISGREATERTHAN_ARGS [] =
9782
9823
{
9783
9824
{CF_ANYSTRING , CF_DATA_TYPE_STRING , "Larger string or value" },
@@ -10622,6 +10663,8 @@ const FnCallType CF_FNCALL_TYPES[] =
10622
10663
FNCALL_OPTION_VARARG , FNCALL_CATEGORY_COMM , SYNTAX_STATUS_NORMAL ),
10623
10664
FnCallTypeNew ("irange" , CF_DATA_TYPE_INT_RANGE , IRANGE_ARGS , & FnCallIRange , "Define a range of integer values for cfengine internal use" ,
10624
10665
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 ),
10625
10668
FnCallTypeNew ("isdir" , CF_DATA_TYPE_CONTEXT , FILESTAT_ARGS , & FnCallFileStat , "True if the named object is a directory" ,
10626
10669
FNCALL_OPTION_NONE , FNCALL_CATEGORY_FILES , SYNTAX_STATUS_NORMAL ),
10627
10670
FnCallTypeNew ("isexecutable" , CF_DATA_TYPE_CONTEXT , FILESTAT_ARGS , & FnCallFileStat , "True if the named object has execution rights for the current user" ,
0 commit comments