The FreeRADIUS server  $Id: 15bac2a4c627c01d1aa2047687b3418955ac7f00 $
section.h
Go to the documentation of this file.
1 #pragma once
2 /*
3  * This program is free software; you can redistribute it and/or modify
4  * it under the terms of the GNU General Public License as published by
5  * the Free Software Foundation; either version 2 of the License, or
6  * (at your option) any later version.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
16  */
17 
18 /**
19  * $Id: 7c8c1ae778cd16c3a88080d07a96f37d197ea5d1 $
20  *
21  * @file lib/server/section.h
22  * @brief Structures which identify sections
23  *
24  * @copyright 2024 Arran Cudbard-Bell (a.cudbardb@freeradius.org)
25  */
26 RCSIDH(section_h, "$Id: 7c8c1ae778cd16c3a88080d07a96f37d197ea5d1 $")
27 
28 #include <stdbool.h>
29 #include <freeradius-devel/server/cf_util.h>
30 
31 #ifdef __cplusplus
32 extern "C" {
33 #endif
34 
35 /** Define a section name consisting of a verb and a noun
36  *
37  * @param[in] _name1 verb name.
38  * @param[in] _name2 noun name.
39  */
40 #define SECTION_NAME(_name1, _name2) &(section_name_t){ .name1 = _name1, .name2 = _name2 }
41 
42 /** Section name identifier
43  */
44 typedef struct {
45  char const *name1; //!< First section name. Usually a verb like 'recv', 'send', etc...
46  char const *name2; //!< Second section name. Usually a packet type like 'access-request', 'access-accept', etc...
48 
49 /* Compare two sections based on name2
50  *
51  * Respects CF_IDENT_ANY values
52  *
53  * @param[in] a First section name.
54  * @param[in] b Second section name.
55  *
56  * @return
57  * - 1 if name2 values match.
58  * - 0 if name2 values don't match.
59  */
60 static inline int section_name2_match(section_name_t const *a, section_name_t const *b)
61 {
62  if ((a->name2 == CF_IDENT_ANY) || (b->name2 == CF_IDENT_ANY)) return 1;
63  if (!a->name2 || !b->name2) {
64  if (a->name2 == b->name2) return 1;
65  return 0;
66  }
67 
68  return (strcmp(a->name2, b->name2) == 0) ? 1 : 0;
69 }
70 
71 /* Compare two section names
72  *
73  * Respects CF_IDENT_ANY values
74  *
75  * @param[in] a First section name.
76  * @param[in] b Second section name.
77  *
78  * @return
79  * - 1 if the section names match.
80  * - 0 if the section names don't match.
81  * - -1 if name1 doesn't match.
82  *
83  */
84 static inline int section_name_match(section_name_t const *a, section_name_t const *b)
85 {
86  if ((a->name1 == CF_IDENT_ANY) || (b->name2 == CF_IDENT_ANY)) goto name2;
87 
88  if (strcmp(a->name1, b->name1) != 0) return -1;
89 
90 name2:
91  return section_name2_match(a, b);
92 }
93 
94 /** Return a printable string for the section name
95  *
96  * @param[in] name Section name.
97  */
98 static inline char const *section_name_str(char const *name)
99 {
100  if (name == NULL) return "NULL";
101  if (name == CF_IDENT_ANY) return "*";
102  return name;
103 }
104 
105 static inline void section_name_dup(TALLOC_CTX *ctx, section_name_t *dst, section_name_t const *src)
106 {
107  dst->name1 = src->name1;
108  dst->name2 = src->name2;
109 
110  if (dst->name1 && (dst->name1 != CF_IDENT_ANY)) dst->name1 = talloc_typed_strdup(ctx, src->name1);
111  if (dst->name2 && (dst->name2 != CF_IDENT_ANY)) dst->name2 = talloc_typed_strdup(ctx, src->name2);
112 }
113 
114 int8_t section_name_cmp(void const *one, void const *two);
115 
116 #ifdef __cplusplus
117 }
118 #endif
#define RCSIDH(h, id)
Definition: build.h:482
#define CF_IDENT_ANY
Definition: cf_util.h:78
static char const * name
static int section_name2_match(section_name_t const *a, section_name_t const *b)
Definition: section.h:60
static int section_name_match(section_name_t const *a, section_name_t const *b)
Definition: section.h:84
static char const * section_name_str(char const *name)
Return a printable string for the section name.
Definition: section.h:98
int8_t section_name_cmp(void const *one, void const *two)
Compare two sections.
Definition: section.c:48
static void section_name_dup(TALLOC_CTX *ctx, section_name_t *dst, section_name_t const *src)
Definition: section.h:105
char const * name2
Second section name. Usually a packet type like 'access-request', 'access-accept',...
Definition: section.h:46
char const * name1
First section name. Usually a verb like 'recv', 'send', etc...
Definition: section.h:45
Section name identifier.
Definition: section.h:44
char * talloc_typed_strdup(TALLOC_CTX *ctx, char const *p)
Call talloc_strdup, setting the type on the new chunk correctly.
Definition: talloc.c:445