Coverage Report

Created: 2023-12-12 06:10

/src/core/libcfnet/protocol_version.h
Line
Count
Source (jump to first uncovered line)
1
/*
2
   Copyright 2023 Northern.tech AS
3
4
   This file is part of CFEngine 3 - written and maintained by Northern.tech AS.
5
6
   This program is free software; you can redistribute it and/or modify it
7
   under the terms of the GNU General Public License as published by the
8
   Free Software Foundation; version 3.
9
10
   This program is distributed in the hope that it will be useful,
11
   but WITHOUT ANY WARRANTY; without even the implied warranty of
12
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
   GNU General Public License for more details.
14
15
  You should have received a copy of the GNU General Public License
16
  along with this program; if not, write to the Free Software
17
  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA
18
19
  To the extent this program is licensed as part of the Enterprise
20
  versions of CFEngine, the applicable Commercial Open Source License
21
  (COSL) may apply to this file if you as a licensee so wish it. See
22
  included file COSL.txt.
23
*/
24
25
26
#ifndef CFENGINE_PROTOCOL_VERSION_H
27
#define CFENGINE_PROTOCOL_VERSION_H
28
29
/**
30
  Available protocol versions. When connection is initialised ProtocolVersion
31
  is 0, i.e. undefined. It is after the call to ServerConnection() that
32
  protocol version is decided, according to body copy_from and body common
33
  control. All protocol numbers are numbered incrementally starting from 1.
34
 */
35
typedef enum
36
{
37
    CF_PROTOCOL_UNDEFINED = 0,
38
    CF_PROTOCOL_CLASSIC = 1,
39
    /* --- Greater versions use TLS as secure communications layer --- */
40
    CF_PROTOCOL_TLS = 2,
41
    CF_PROTOCOL_COOKIE = 3,
42
} ProtocolVersion;
43
44
/* We use CF_PROTOCOL_LATEST as the default for new connections. */
45
#define CF_PROTOCOL_LATEST CF_PROTOCOL_COOKIE
46
47
static inline const char *ProtocolVersionString(const ProtocolVersion p)
48
0
{
49
0
    switch (p)
50
0
    {
51
0
    case CF_PROTOCOL_COOKIE:
52
0
        return "cookie";
53
0
    case CF_PROTOCOL_TLS:
54
0
        return "tls";
55
0
    case CF_PROTOCOL_CLASSIC:
56
0
        return "classic";
57
0
    default:
58
0
        return "undefined";
59
0
    }
60
0
}
61
62
static inline bool ProtocolIsKnown(const ProtocolVersion p)
63
0
{
64
0
    return ((p > CF_PROTOCOL_UNDEFINED) && (p <= CF_PROTOCOL_LATEST));
65
0
}
66
67
static inline bool ProtocolIsTLS(const ProtocolVersion p)
68
0
{
69
0
    return ((p >= CF_PROTOCOL_TLS) && (p <= CF_PROTOCOL_LATEST));
70
0
}
71
72
static inline bool ProtocolIsTooNew(const ProtocolVersion p)
73
0
{
74
0
    return (p > CF_PROTOCOL_LATEST);
75
0
}
76
77
static inline bool ProtocolIsUndefined(const ProtocolVersion p)
78
0
{
79
0
    return (p <= CF_PROTOCOL_UNDEFINED);
80
0
}
81
82
static inline bool ProtocolIsClassic(const ProtocolVersion p)
83
0
{
84
0
    return (p == CF_PROTOCOL_CLASSIC);
85
0
}
86
87
static inline bool ProtocolTerminateCSV(const ProtocolVersion p)
88
0
{
89
0
    return (p < CF_PROTOCOL_COOKIE);
90
0
}
91
92
/**
93
 * Returns CF_PROTOCOL_TLS or CF_PROTOCOL_CLASSIC (or CF_PROTOCOL_UNDEFINED)
94
 * Maps all versions using TLS to CF_PROTOCOL_TLS for convenience
95
 * in switch statements.
96
 */
97
static inline ProtocolVersion ProtocolClassicOrTLS(const ProtocolVersion p)
98
0
{
99
0
    if (ProtocolIsTLS(p))
100
0
    {
101
0
        return CF_PROTOCOL_TLS;
102
0
    }
103
0
    if (ProtocolIsClassic(p))
104
0
    {
105
0
        return CF_PROTOCOL_CLASSIC;
106
0
    }
107
0
    return CF_PROTOCOL_UNDEFINED;
108
0
}
109
110
/**
111
 * Parses the version string sent over network to enum, e.g. "CFE_v1" -> 1
112
 */
113
ProtocolVersion ParseProtocolVersionNetwork(const char *s);
114
115
/**
116
 * Parses the version string set in policy to enum, e.g. "classic" -> 1
117
 */
118
ProtocolVersion ParseProtocolVersionPolicy(const char *s);
119
120
// Old name for compatibility (enterprise), TODO remove:
121
#define ProtocolVersionParse ParseProtocolVersionPolicy
122
123
#endif // CFENGINE_PROTOCOL_VERSION_H