]> git.proxmox.com Git - mirror_ovs.git/blame - lib/ovsdb-types.h
Don't go beyond buffer length when printing descriptions
[mirror_ovs.git] / lib / ovsdb-types.h
CommitLineData
c7239c2f 1/* Copyright (c) 2009, 2010 Nicira Networks
f85f8ebb
BP
2 *
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16#ifndef OVSDB_TYPES_H
17#define OVSDB_TYPES_H 1
18
bd76d25d 19#include <float.h>
f85f8ebb
BP
20#include <stdbool.h>
21#include <stdint.h>
22#include "compiler.h"
23#include "uuid.h"
24
bfe8e67a
BP
25#ifdef HAVE_PCRE
26#include <pcre.h>
27#endif
28
f85f8ebb
BP
29struct json;
30
31/* An atomic type: one that OVSDB regards as a single unit of data. */
32enum ovsdb_atomic_type {
33 OVSDB_TYPE_VOID, /* No value. */
34 OVSDB_TYPE_INTEGER, /* Signed 64-bit integer. */
35 OVSDB_TYPE_REAL, /* IEEE 754 double-precision floating point. */
36 OVSDB_TYPE_BOOLEAN, /* True or false. */
37 OVSDB_TYPE_STRING, /* UTF-8 string. */
38 OVSDB_TYPE_UUID, /* RFC 4122 UUID referencing a table row. */
39 OVSDB_N_TYPES
40};
41
42static inline bool ovsdb_atomic_type_is_valid(enum ovsdb_atomic_type);
f85f8ebb
BP
43bool ovsdb_atomic_type_from_string(const char *, enum ovsdb_atomic_type *);
44struct ovsdb_error *ovsdb_atomic_type_from_json(enum ovsdb_atomic_type *,
45 const struct json *);
46const char *ovsdb_atomic_type_to_string(enum ovsdb_atomic_type);
47struct json *ovsdb_atomic_type_to_json(enum ovsdb_atomic_type);
48\f
bd76d25d
BP
49/* An atomic type plus optional constraints. */
50
51struct ovsdb_base_type {
52 enum ovsdb_atomic_type type;
53 union {
54 struct ovsdb_integer_constraints {
55 int64_t min; /* minInteger or INT64_MIN. */
56 int64_t max; /* maxInteger or INT64_MAX. */
57 } integer;
58
59 struct ovsdb_real_constraints {
60 double min; /* minReal or -DBL_MAX. */
61 double max; /* minReal or DBL_MAX. */
62 } real;
63
64 /* No constraints for Boolean types. */
65
66 struct ovsdb_string_constraints {
bfe8e67a 67#ifdef HAVE_PCRE
bd76d25d 68 pcre *re; /* Compiled regular expression. */
bfe8e67a 69#endif
bd76d25d
BP
70 char *reMatch; /* reMatch or NULL. */
71 char *reComment; /* reComment or NULL. */
72 unsigned int minLen; /* minLength or 0. */
73 unsigned int maxLen; /* maxLength or UINT_MAX. */
74 } string;
0d0f05b9
BP
75
76 struct ovsdb_uuid_constraints {
77 char *refTableName; /* Name of referenced table, or NULL. */
78 struct ovsdb_table *refTable; /* Referenced table, if available. */
79 } uuid;
bd76d25d
BP
80 } u;
81};
82
83#define OVSDB_BASE_VOID_INIT { .type = OVSDB_TYPE_VOID }
84#define OVSDB_BASE_INTEGER_INIT { .type = OVSDB_TYPE_INTEGER, \
85 .u.integer = { INT64_MIN, INT64_MAX } }
86#define OVSDB_BASE_REAL_INIT { .type = OVSDB_TYPE_REAL, \
87 .u.real = { -DBL_MAX, DBL_MAX } }
88#define OVSDB_BASE_BOOLEAN_INIT { .type = OVSDB_TYPE_BOOLEAN }
bfe8e67a 89#ifdef HAVE_PCRE
bd76d25d
BP
90#define OVSDB_BASE_STRING_INIT { .type = OVSDB_TYPE_STRING, \
91 .u.string = { NULL, NULL, NULL, \
92 0, UINT_MAX } }
bfe8e67a
BP
93#else
94#define OVSDB_BASE_STRING_INIT { .type = OVSDB_TYPE_STRING, \
95 .u.string = { NULL, NULL, \
96 0, UINT_MAX } }
97#endif
0d0f05b9
BP
98#define OVSDB_BASE_UUID_INIT { .type = OVSDB_TYPE_UUID, \
99 .u.uuid = { NULL, NULL } }
bd76d25d
BP
100
101void ovsdb_base_type_init(struct ovsdb_base_type *, enum ovsdb_atomic_type);
102void ovsdb_base_type_clone(struct ovsdb_base_type *,
103 const struct ovsdb_base_type *);
104void ovsdb_base_type_destroy(struct ovsdb_base_type *);
105
106bool ovsdb_base_type_is_valid(const struct ovsdb_base_type *);
107bool ovsdb_base_type_has_constraints(const struct ovsdb_base_type *);
108void ovsdb_base_type_clear_constraints(struct ovsdb_base_type *);
109struct ovsdb_error *ovsdb_base_type_set_regex(struct ovsdb_base_type *,
110 const char *reMatch,
111 const char *reComment)
112 WARN_UNUSED_RESULT;
113
114struct ovsdb_error *ovsdb_base_type_from_json(struct ovsdb_base_type *,
115 const struct json *)
116 WARN_UNUSED_RESULT;
117struct json *ovsdb_base_type_to_json(const struct ovsdb_base_type *);
118\f
c7239c2f 119/* An OVSDB type.
f85f8ebb 120 *
c7239c2f
BP
121 * Several rules constrain the valid types. See ovsdb_type_is_valid() (in
122 * ovsdb-types.c) for details.
f85f8ebb 123 *
c7239c2f
BP
124 * If 'value_type' is OVSDB_TYPE_VOID, 'n_min' is 1, and 'n_max' is 1, then the
125 * type is a single atomic 'key_type'.
f85f8ebb 126 *
c7239c2f
BP
127 * If 'value_type' is OVSDB_TYPE_VOID and 'n_min' or 'n_max' (or both) has a
128 * value other than 1, then the type is a set of 'key_type'. If 'n_min' is 0
129 * and 'n_max' is 1, then the type can also be considered an optional
130 * 'key_type'.
131 *
132 * If 'value_type' is not OVSDB_TYPE_VOID, then the type is a map from
133 * 'key_type' to 'value_type'. If 'n_min' is 0 and 'n_max' is 1, then the type
134 * can also be considered an optional pair of 'key_type' and 'value_type'.
f85f8ebb
BP
135 */
136struct ovsdb_type {
bd76d25d
BP
137 struct ovsdb_base_type key;
138 struct ovsdb_base_type value;
f85f8ebb
BP
139 unsigned int n_min;
140 unsigned int n_max; /* UINT_MAX stands in for "unlimited". */
141};
142
bd76d25d 143#define OVSDB_TYPE_SCALAR_INITIALIZER(KEY) { KEY, OVSDB_BASE_VOID_INIT, 1, 1 }
f85f8ebb
BP
144
145extern const struct ovsdb_type ovsdb_type_integer;
146extern const struct ovsdb_type ovsdb_type_real;
147extern const struct ovsdb_type ovsdb_type_boolean;
148extern const struct ovsdb_type ovsdb_type_string;
149extern const struct ovsdb_type ovsdb_type_uuid;
150
bd76d25d
BP
151void ovsdb_type_clone(struct ovsdb_type *, const struct ovsdb_type *);
152void ovsdb_type_destroy(struct ovsdb_type *);
153
f85f8ebb
BP
154bool ovsdb_type_is_valid(const struct ovsdb_type *);
155
156static inline bool ovsdb_type_is_scalar(const struct ovsdb_type *);
157static inline bool ovsdb_type_is_optional(const struct ovsdb_type *);
158static inline bool ovsdb_type_is_composite(const struct ovsdb_type *);
159static inline bool ovsdb_type_is_set(const struct ovsdb_type *);
160static inline bool ovsdb_type_is_map(const struct ovsdb_type *);
161
162char *ovsdb_type_to_english(const struct ovsdb_type *);
163
164struct ovsdb_error *ovsdb_type_from_json(struct ovsdb_type *,
165 const struct json *)
166 WARN_UNUSED_RESULT;
167struct json *ovsdb_type_to_json(const struct ovsdb_type *);
168\f
169/* Inline function implementations. */
170
171static inline bool
172ovsdb_atomic_type_is_valid(enum ovsdb_atomic_type atomic_type)
173{
174 return atomic_type >= 0 && atomic_type < OVSDB_N_TYPES;
175}
176
f85f8ebb
BP
177static inline bool ovsdb_type_is_scalar(const struct ovsdb_type *type)
178{
bd76d25d 179 return (type->value.type == OVSDB_TYPE_VOID
f85f8ebb
BP
180 && type->n_min == 1 && type->n_max == 1);
181}
182
183static inline bool ovsdb_type_is_optional(const struct ovsdb_type *type)
184{
185 return type->n_min == 0;
186}
187
188static inline bool ovsdb_type_is_composite(const struct ovsdb_type *type)
189{
190 return type->n_max > 1;
191}
192
193static inline bool ovsdb_type_is_set(const struct ovsdb_type *type)
194{
bd76d25d 195 return (type->value.type == OVSDB_TYPE_VOID
f85f8ebb
BP
196 && (type->n_min != 1 || type->n_max != 1));
197}
198
199static inline bool ovsdb_type_is_map(const struct ovsdb_type *type)
200{
bd76d25d 201 return type->value.type != OVSDB_TYPE_VOID;
f85f8ebb
BP
202}
203
204#endif /* ovsdb-types.h */