Greenbone Vulnerability Management Libraries: base/hosts.h Source File

Greenbone Vulnerability Management Libraries 23.1.1

hosts.h
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2013-2023 Greenbone AG
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later
4 */
5
13
14#ifndef _GVM_BASE_HOSTS_H
15#define _GVM_BASE_HOSTS_H
16
20#define FEATURE_HOSTS_ALLOWED_ONLY 1
21
26#define FEATURE_REVERSE_LOOKUP_EXCLUDED 1
27
28#include <glib.h> /* for gchar, GList */
29#include <netinet/in.h> /* for in6_addr, in_addr */
30
31/* Static values */
32
33 enum host_type
34{
35 HOST_TYPE_NAME = 0, /* Hostname eg. foo */
36 HOST_TYPE_IPV4, /* eg. 192.168.1.1 */
37 HOST_TYPE_CIDR_BLOCK, /* eg. 192.168.15.0/24 */
38 HOST_TYPE_RANGE_SHORT, /* eg. 192.168.15.10-20 */
39 HOST_TYPE_RANGE_LONG, /* eg. 192.168.15.10-192.168.18.3 */
40 HOST_TYPE_IPV6, /* eg. ::1 */
41 HOST_TYPE_CIDR6_BLOCK, /* eg. ::ffee/120 */
42 HOST_TYPE_RANGE6_LONG, /* eg. ::1:200:7-::1:205:500 */
43 HOST_TYPE_RANGE6_SHORT, /* eg. ::1-fe10 */
44 HOST_TYPE_MAX /* Boundary checking. */
45};
46
47/* Typedefs */
48 typedef struct gvm_host gvm_host_t;
49 typedef struct gvm_vhost gvm_vhost_t;
50 typedef struct gvm_hosts gvm_hosts_t;
51
52/* Data structures. */
53
60 struct gvm_host
61{
62 union
63 {
64 gchar *name;
65 struct in_addr addr;
66 struct in6_addr addr6;
67 };
68 enum host_type type;
69 GSList *vhosts;
70};
71
75 struct gvm_vhost
76{
77 char *value;
78 char *source;
79};
80
87 struct gvm_hosts
88{
89 gchar *orig_str;
90 gvm_host_t **hosts;
91 size_t max_size;
92 size_t current;
93 size_t count;
94 size_t removed;
95 size_t duplicated;
96};
97
98/* Function prototypes. */
99
100/* gvm_hosts_t related */
101
102 gvm_hosts_t *
103 gvm_hosts_new (const gchar *);
104
105 gvm_hosts_t *
106 gvm_hosts_new_with_max (const gchar *, unsigned int);
107
108 gvm_host_t *
109 gvm_hosts_next (gvm_hosts_t *);
110
111void
112 gvm_hosts_move_current_host_to_end (gvm_hosts_t *);
113
114void
115 gvm_hosts_free (gvm_hosts_t *);
116
117void
118 gvm_hosts_shuffle (gvm_hosts_t *);
119
120void
121 gvm_hosts_reverse (gvm_hosts_t *);
122
123void
124 gvm_hosts_add (gvm_hosts_t *, gvm_host_t *);
125
126GSList *
127 gvm_hosts_resolve (gvm_hosts_t *);
128
129int
130 gvm_hosts_exclude (gvm_hosts_t *, const char *);
131
132int
133 gvm_vhosts_exclude (gvm_host_t *, const char *);
134
135int
136 gvm_hosts_exclude_with_max (gvm_hosts_t *, const char *, unsigned int);
137
138GSList *
139 gvm_hosts_allowed_only (gvm_hosts_t *, const char *, const char *);
140
141char *
142 gvm_host_reverse_lookup (gvm_host_t *);
143
144int
145 gvm_hosts_reverse_lookup_only (gvm_hosts_t *);
146
147int
148 gvm_hosts_reverse_lookup_unify (gvm_hosts_t *);
149
150 gvm_hosts_t *
151 gvm_hosts_reverse_lookup_only_excluded (gvm_hosts_t *);
152
153 gvm_hosts_t *
154 gvm_hosts_reverse_lookup_unify_excluded (gvm_hosts_t *);
155
156unsigned int
157 gvm_hosts_count (const gvm_hosts_t *);
158
159unsigned int
160 gvm_hosts_removed (const gvm_hosts_t *);
161
162unsigned int
163 gvm_hosts_duplicated (const gvm_hosts_t *);
164
165/* gvm_host_t related */
166
167 gvm_host_t *
168 gvm_host_from_str (const gchar *hosts_str);
169
170int
171 gvm_host_in_hosts (const gvm_host_t *, const struct in6_addr *,
172 const gvm_hosts_t *);
173
174 gvm_host_t *
175 gvm_host_find_in_hosts (const gvm_host_t *, const struct in6_addr *,
176 const gvm_hosts_t *);
177
178gchar *
179 gvm_host_type_str (const gvm_host_t *);
180
181enum host_type
182 gvm_host_type (const gvm_host_t *);
183
184gchar *
185 gvm_host_value_str (const gvm_host_t *);
186
187int
188 gvm_host_resolve (const gvm_host_t *, void *, int);
189
190int
191 gvm_host_get_addr6 (const gvm_host_t *, struct in6_addr *);
192
193void
194 gvm_host_add_reverse_lookup (gvm_host_t *);
195
196void gvm_host_free (gpointer);
197
198gpointer gvm_duplicate_vhost (gconstpointer, gpointer);
199
200 gvm_host_t *
201 gvm_duplicate_host (gvm_host_t *);
202
203/* Miscellaneous functions */
204
205 gvm_vhost_t *
206 gvm_vhost_new (char *, char *);
207
208int
209 gvm_get_host_type (const gchar *);
210
211int
212 gvm_is_cidr6_block (const char *);
213
214int
215 gvm_cidr6_get_block (const char *, unsigned int *);
216
217int
218 gvm_cidr6_get_ip (const char *, struct in6_addr *);
219
220int
221 gvm_cidr6_block_ips (const char *str, struct in6_addr *first,
222 struct in6_addr *last);
223
224#endif /* not _GVM_BASE_HOSTS_H */
gvm_hosts_shuffle
void gvm_hosts_shuffle(gvm_hosts_t *)
Randomizes the order of the hosts objects in the collection. Not to be used while iterating over the ...
Definition hosts.c:1399
gvm_hosts_add
void gvm_hosts_add(gvm_hosts_t *, gvm_host_t *)
Inserts a host object at the end of a hosts collection.
Definition hosts.c:966
gvm_is_cidr6_block
int gvm_is_cidr6_block(const char *)
Checks if a buffer points to an IPv6 CIDR-expressed block. "2620:0:2d0:200::7/120" is valid,...
Definition hosts.c:470
gvm_host_in_hosts
int gvm_host_in_hosts(const gvm_host_t *, const struct in6_addr *, const gvm_hosts_t *)
Returns whether a host has an equal host in a hosts collection. eg. 192.168.10.1 has an equal in list...
Definition hosts.c:2242
gvm_hosts_reverse
void gvm_hosts_reverse(gvm_hosts_t *)
Reverses the order of the hosts objects in the collection. Not to be used while iterating over the si...
Definition hosts.c:1431
gvm_hosts_exclude
int gvm_hosts_exclude(gvm_hosts_t *, const char *)
Excludes a set of hosts provided as a string from a hosts collection. Not to be used while iterating ...
Definition hosts.c:1771
host_type
host_type
Definition hosts.h:34
HOST_TYPE_RANGE_SHORT
@ HOST_TYPE_RANGE_SHORT
Definition hosts.h:38
HOST_TYPE_RANGE6_SHORT
@ HOST_TYPE_RANGE6_SHORT
Definition hosts.h:43
HOST_TYPE_RANGE_LONG
@ HOST_TYPE_RANGE_LONG
Definition hosts.h:39
HOST_TYPE_RANGE6_LONG
@ HOST_TYPE_RANGE6_LONG
Definition hosts.h:42
HOST_TYPE_NAME
@ HOST_TYPE_NAME
Definition hosts.h:35
HOST_TYPE_IPV6
@ HOST_TYPE_IPV6
Definition hosts.h:40
HOST_TYPE_CIDR6_BLOCK
@ HOST_TYPE_CIDR6_BLOCK
Definition hosts.h:41
HOST_TYPE_MAX
@ HOST_TYPE_MAX
Definition hosts.h:44
HOST_TYPE_IPV4
@ HOST_TYPE_IPV4
Definition hosts.h:36
HOST_TYPE_CIDR_BLOCK
@ HOST_TYPE_CIDR_BLOCK
Definition hosts.h:37
gvm_host_find_in_hosts
gvm_host_t * gvm_host_find_in_hosts(const gvm_host_t *, const struct in6_addr *, const gvm_hosts_t *)
Find the gvm_host_t from a gvm_hosts_t structure.
Definition hosts.c:2150
gvm_hosts_removed
unsigned int gvm_hosts_removed(const gvm_hosts_t *)
Gets the count of single values in hosts string that were removed (duplicates / excluded....
Definition hosts.c:2120
gvm_duplicate_host
gvm_host_t * gvm_duplicate_host(gvm_host_t *)
Creates a deep copy of a host. gvm_host_free has to be called on it.
Definition hosts.c:2199
gvm_cidr6_block_ips
int gvm_cidr6_block_ips(const char *str, struct in6_addr *first, struct in6_addr *last)
Gets the first and last usable IPv4 addresses from a CIDR-expressed block. eg. "192....
Definition hosts.c:572
gvm_hosts_resolve
GSList * gvm_hosts_resolve(gvm_hosts_t *)
Resolves host objects of type name in a hosts collection, replacing hostnames with IPv4 values....
Definition hosts.c:1457
gvm_vhosts_exclude
int gvm_vhosts_exclude(gvm_host_t *, const char *)
Exclude a list of vhosts from a host's vhosts list.
Definition hosts.c:1525
gvm_host_add_reverse_lookup
void gvm_host_add_reverse_lookup(gvm_host_t *)
Add a host's reverse-lookup name to the vhosts list.
Definition hosts.c:1921
gvm_hosts_move_current_host_to_end
void gvm_hosts_move_current_host_to_end(gvm_hosts_t *)
Move the current gvm_host_t from a gvm_hosts_t structure to the end of the hosts list.
Definition hosts.c:1333
gvm_hosts_new_with_max
gvm_hosts_t * gvm_hosts_new_with_max(const gchar *, unsigned int)
Creates a new gvm_hosts_t structure and the associated hosts objects from the provided hosts_str.
Definition hosts.c:1104
gvm_hosts_reverse_lookup_only
int gvm_hosts_reverse_lookup_only(gvm_hosts_t *)
Removes hosts that don't reverse-lookup from the hosts collection. Not to be used while iterating ove...
Definition hosts.c:2004
gvm_host_t
struct gvm_host gvm_host_t
Definition hosts.h:48
gvm_hosts_reverse_lookup_unify_excluded
gvm_hosts_t * gvm_hosts_reverse_lookup_unify_excluded(gvm_hosts_t *)
Removes hosts duplicates that reverse-lookup to the same value. Not to be used while iterating over t...
Definition hosts.c:2029
gvm_host_value_str
gchar * gvm_host_value_str(const gvm_host_t *)
Gets a host's value in printable format.
Definition hosts.c:2290
gvm_hosts_count
unsigned int gvm_hosts_count(const gvm_hosts_t *)
Gets the count of single hosts objects in a hosts collection.
Definition hosts.c:2106
gvm_vhost_t
struct gvm_vhost gvm_vhost_t
Definition hosts.h:49
gvm_hosts_next
gvm_host_t * gvm_hosts_next(gvm_hosts_t *)
Gets the next gvm_host_t from a gvm_hosts_t structure. The state of iteration is kept internally with...
Definition hosts.c:1315
gvm_hosts_free
void gvm_hosts_free(gvm_hosts_t *)
Frees memory occupied by an gvm_hosts_t structure.
Definition hosts.c:1376
gvm_host_reverse_lookup
char * gvm_host_reverse_lookup(gvm_host_t *)
Checks for a host object reverse dns lookup existence.
Definition hosts.c:1836
gvm_host_type_str
gchar * gvm_host_type_str(const gvm_host_t *)
Gets a host's type in printable format.
Definition hosts.c:2274
gvm_host_get_addr6
int gvm_host_get_addr6(const gvm_host_t *, struct in6_addr *)
Gives a host object's value as an IPv6 address. If the host type is hostname, it resolves the IPv4 ad...
Definition hosts.c:2368
gvm_get_host_type
int gvm_get_host_type(const gchar *)
Determines the host type in a buffer.
Definition hosts.c:814
gvm_hosts_exclude_with_max
int gvm_hosts_exclude_with_max(gvm_hosts_t *, const char *, unsigned int)
Excludes a set of hosts provided as a string from a hosts collection. Not to be used while iterating ...
Definition hosts.c:1580
gvm_hosts_t
struct gvm_hosts gvm_hosts_t
Definition hosts.h:50
gvm_hosts_new
gvm_hosts_t * gvm_hosts_new(const gchar *)
Creates a new gvm_hosts_t structure and the associated hosts objects from the provided hosts_str.
Definition hosts.c:1301
gvm_cidr6_get_block
int gvm_cidr6_get_block(const char *, unsigned int *)
Gets the network block value from a CIDR-expressed block string. For "192.168.1.1/24" it is 24.
Definition hosts.c:516
gvm_hosts_allowed_only
GSList * gvm_hosts_allowed_only(gvm_hosts_t *, const char *, const char *)
Returns a list of hosts after a host authorization check.
Definition hosts.c:1658
gvm_cidr6_get_ip
int gvm_cidr6_get_ip(const char *, struct in6_addr *)
Gets the IPv4 value from a CIDR-expressed block. eg. For "192.168.1.10/24" it is "192....
Definition hosts.c:537
gvm_host_free
void gvm_host_free(gpointer)
Frees the memory occupied by an gvm_host_t object.
Definition hosts.c:945
gvm_hosts_duplicated
unsigned int gvm_hosts_duplicated(const gvm_hosts_t *)
Gets the count of single values in hosts string that were duplicated and therefore removed from the l...
Definition hosts.c:2134
gvm_hosts_reverse_lookup_unify
int gvm_hosts_reverse_lookup_unify(gvm_hosts_t *)
Removes hosts duplicates that reverse-lookup to the same value. Not to be used while iterating over t...
Definition hosts.c:2085
gvm_host_type
enum host_type gvm_host_type(const gvm_host_t *)
Gets a host object's type.
Definition hosts.c:2259
gvm_duplicate_vhost
gpointer gvm_duplicate_vhost(gconstpointer, gpointer)
Creates a deep copy of a gvm_vhost_t object.
Definition hosts.c:910
gvm_vhost_new
gvm_vhost_t * gvm_vhost_new(char *, char *)
Creates a new gvm_vhost_t object.
Definition hosts.c:875
gvm_host_resolve
int gvm_host_resolve(const gvm_host_t *, void *, int)
Resolves a host object's name to an IPv4 or IPv6 address. Host object should be of type HOST_TYPE_NAM...
Definition hosts.c:2347
gvm_hosts_reverse_lookup_only_excluded
gvm_hosts_t * gvm_hosts_reverse_lookup_only_excluded(gvm_hosts_t *)
Removes hosts that don't reverse-lookup from the hosts collection. Not to be used while iterating ove...
Definition hosts.c:1963
gvm_host_from_str
gvm_host_t * gvm_host_from_str(const gchar *hosts_str)
Creates a new gvm_host_t from a host string.
Definition hosts.c:1787
gvm_host
The structure for a single host object.
Definition hosts.h:61
gvm_host::addr6
struct in6_addr addr6
Definition hosts.h:66
gvm_host::name
gchar * name
Definition hosts.h:64
gvm_host::vhosts
GSList * vhosts
Definition hosts.h:69
gvm_host::addr
struct in_addr addr
Definition hosts.h:65
gvm_host::type
enum host_type type
Definition hosts.h:68
gvm_hosts
The structure for Hosts collection.
Definition hosts.h:88
gvm_hosts::max_size
size_t max_size
Definition hosts.h:91
gvm_hosts::removed
size_t removed
Definition hosts.h:94
gvm_hosts::orig_str
gchar * orig_str
Definition hosts.h:89
gvm_hosts::duplicated
size_t duplicated
Definition hosts.h:95
gvm_hosts::current
size_t current
Definition hosts.h:92
gvm_hosts::count
size_t count
Definition hosts.h:93
gvm_hosts::hosts
gvm_host_t ** hosts
Definition hosts.h:90
gvm_vhost
The structure for a single vhost object.
Definition hosts.h:76
gvm_vhost::value
char * value
Definition hosts.h:77
gvm_vhost::source
char * source
Definition hosts.h:78