Greenbone Vulnerability Management Libraries  10.0.0
xmlutils.h File Reference

Headers for simple XML reader. More...

#include "serverutils.h"
#include <glib.h>
#include <gnutls/gnutls.h>
#include <stdio.h>
Include dependency graph for xmlutils.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  context_data_t
 XML context. More...
 
struct  entity_s
 XML element. More...
 
struct  xml_search_data_t
 Data for xml search functions. More...
 

Typedefs

typedef GSList * entities_t
 Entities. More...
 
typedef struct entity_sentity_t
 

Functions

void xml_handle_start_element (context_data_t *, const gchar *, const gchar **, const gchar **)
 Handle the start of an OMP XML element. More...
 
void xml_handle_end_element (context_data_t *, const gchar *)
 Handle the end of an XML element. More...
 
void xml_handle_text (context_data_t *, const gchar *, gsize)
 Handle additional text of an XML element. More...
 
entities_t next_entities (entities_t)
 Return all the entities from an entities_t after the first. More...
 
entity_t first_entity (entities_t)
 Return the first entity from an entities_t. More...
 
entity_t add_entity (entities_t *, const char *, const char *)
 Add an XML entity to a tree of entities. More...
 
int compare_entities (entity_t, entity_t)
 Compare two XML entity. More...
 
entity_t entity_child (entity_t, const char *)
 Get a child of an entity. More...
 
const char * entity_attribute (entity_t, const char *)
 Get an attribute of an entity. More...
 
char * entity_name (entity_t entity)
 Get the name an entity. More...
 
char * entity_text (entity_t entity)
 Get the text an entity. More...
 
void free_entity (entity_t)
 Free an entity, recursively. More...
 
void print_entity (FILE *, entity_t)
 Print an XML entity. More...
 
void print_entity_format (entity_t, gpointer indentation)
 Print an XML entity to stdout, recursively printing its children. More...
 
int try_read_entity_and_string (gnutls_session_t *, int, entity_t *, GString **)
 Try read an XML entity tree from the manager. More...
 
int read_entity_and_string (gnutls_session_t *, entity_t *, GString **)
 Try read an XML entity tree from the manager. More...
 
int read_entity_and_string_c (gvm_connection_t *, entity_t *, GString **)
 Try read an XML entity tree from the manager. More...
 
int read_entity_and_text (gnutls_session_t *, entity_t *, char **)
 Read an XML entity tree from the manager. More...
 
int read_entity_and_text_c (gvm_connection_t *, entity_t *, char **)
 Read an XML entity tree from the manager. More...
 
int try_read_entity (gnutls_session_t *, int, entity_t *)
 Try read an XML entity tree from the manager. More...
 
int try_read_entity_c (gvm_connection_t *, int, entity_t *)
 Try read an XML entity tree from the manager. More...
 
int read_entity (gnutls_session_t *, entity_t *)
 Read an XML entity tree from the manager. More...
 
int read_entity_s (int, entity_t *)
 Read an XML entity tree from the socket. More...
 
int read_entity_c (gvm_connection_t *, entity_t *)
 Read an XML entity tree from the manager. More...
 
int read_string (gnutls_session_t *, GString **)
 Read entity and text. Free the entity immediately. More...
 
int read_string_c (gvm_connection_t *, GString **)
 Read entity and text. Free the entity immediately. More...
 
int parse_entity (const char *, entity_t *)
 Read an XML entity tree from a string. More...
 
void print_entity_to_string (entity_t entity, GString *string)
 Print an XML entity tree to a GString, appending it if string is not. More...
 
int xml_count_entities (entities_t)
 Count the number of entities. More...
 
void xml_string_append (GString *, const char *,...)
 Append formatted escaped XML to a string. More...
 
int find_element_in_xml_file (gchar *, gchar *, GHashTable *)
 Tests if an XML file contains an element with given attributes. More...
 

Detailed Description

Headers for simple XML reader.

Definition in file xmlutils.h.

Typedef Documentation

◆ entities_t

typedef GSList* entities_t

Entities.

Definition at line 60 of file xmlutils.h.

◆ entity_t

typedef struct entity_s* entity_t

Definition at line 72 of file xmlutils.h.

Function Documentation

◆ add_entity()

entity_t add_entity ( entities_t entities,
const char *  name,
const char *  text 
)

Add an XML entity to a tree of entities.

Parameters
[in]entitiesThe tree of entities
[in]nameName of the entity. Copied, copy is freed by free_entity.
[in]textText of the entity. Copied, copy is freed by free_entity.
Returns
The new entity.

Definition at line 113 of file xmlutils.c.

114 {
115  entity_t entity = make_entity (name, text);
116  if (entities)
117  *entities = g_slist_append (*entities, entity);
118  return entity;
119 }
XML element.
Definition: xmlutils.h:65
entity_t make_entity(const char *name, const char *text)
Create an entity.
Definition: xmlutils.c:60

References make_entity().

Referenced by handle_start_element().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ compare_entities()

int compare_entities ( entity_t  entity1,
entity_t  entity2 
)

Compare two XML entity.

Parameters
[in]entity1First entity.
[in]entity2First entity.
Returns
0 if equal, 1 otherwise.

Definition at line 1346 of file xmlutils.c.

1347 {
1348  if (entity1 == NULL)
1349  return entity2 == NULL ? 0 : 1;
1350  if (entity2 == NULL)
1351  return 1;
1352 
1353  if (strcmp (entity1->name, entity2->name))
1354  {
1355  g_debug (" compare failed name: %s vs %s\n", entity1->name,
1356  entity2->name);
1357  return 1;
1358  }
1359  if (strcmp (entity1->text, entity2->text))
1360  {
1361  g_debug (" compare failed text %s vs %s (%s)\n", entity1->text,
1362  entity2->text, entity1->name);
1363  return 1;
1364  }
1365 
1366  if (entity1->attributes == NULL)
1367  {
1368  if (entity2->attributes)
1369  return 1;
1370  }
1371  else
1372  {
1373  if (entity2->attributes == NULL)
1374  return 1;
1375  if (g_hash_table_find (entity1->attributes, compare_find_attribute,
1376  (gpointer) entity2->attributes))
1377  {
1378  g_debug (" compare failed attributes\n");
1379  return 1;
1380  }
1381  }
1382 
1383  // FIX entities can be in any order
1384  GSList *list1 = entity1->entities;
1385  GSList *list2 = entity2->entities;
1386  while (list1 && list2)
1387  {
1388  if (compare_entities (list1->data, list2->data))
1389  {
1390  g_debug (" compare failed subentity\n");
1391  return 1;
1392  }
1393  list1 = g_slist_next (list1);
1394  list2 = g_slist_next (list2);
1395  }
1396  if (list1 == list2)
1397  return 0;
1398  /* More entities in one of the two. */
1399  g_debug (" compare failed number of entities (%s)\n", entity1->name);
1400  return 1;
1401 }
GHashTable * attributes
Attributes.
Definition: xmlutils.h:69
entities_t entities
Children.
Definition: xmlutils.h:70
char * name
Name.
Definition: xmlutils.h:67
int compare_entities(entity_t entity1, entity_t entity2)
Compare two XML entity.
Definition: xmlutils.c:1346
gboolean compare_find_attribute(gpointer key, gpointer value, gpointer attributes2)
Look for a key-value pair in a hash table.
Definition: xmlutils.c:1328
char * text
Text.
Definition: xmlutils.h:68

References entity_s::attributes, compare_entities(), compare_find_attribute(), entity_s::entities, entity_s::name, and entity_s::text.

Referenced by compare_entities().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ entity_attribute()

const char* entity_attribute ( entity_t  entity,
const char *  name 
)

Get an attribute of an entity.

Parameters
[in]entityEntity.
[in]nameName of the attribute.
Returns
Attribute if found, else NULL.

Definition at line 228 of file xmlutils.c.

229 {
230  if (!entity)
231  return NULL;
232 
233  if (entity->attributes)
234  return (const char *) g_hash_table_lookup (entity->attributes, name);
235  return NULL;
236 }
GHashTable * attributes
Attributes.
Definition: xmlutils.h:69

References entity_s::attributes.

Referenced by gmp_authenticate_info_ext(), gmp_authenticate_info_ext_c(), gmp_check_response(), gmp_check_response_c(), gmp_get_report_ext(), gmp_get_tasks_ext(), gmp_ping(), gmp_ping_c(), gmp_read_create_response(), gmp_resume_task_report_c(), gmp_start_task_report_c(), osp_delete_scan(), osp_get_scan(), osp_get_scanner_details(), osp_start_scan(), and osp_stop_scan().

Here is the caller graph for this function:

◆ entity_child()

entity_t entity_child ( entity_t  entity,
const char *  name 
)

Get a child of an entity.

Parameters
[in]entityEntity.
[in]nameName of the child.
Returns
Entity if found, else NULL.

Definition at line 205 of file xmlutils.c.

206 {
207  if (!entity)
208  return NULL;
209 
210  if (entity->entities)
211  {
212  entities_t match =
213  g_slist_find_custom (entity->entities, name, compare_entity_with_name);
214  return match ? (entity_t) match->data : NULL;
215  }
216  return NULL;
217 }
struct entity_s * entity_t
Definition: xmlutils.h:72
entities_t entities
Children.
Definition: xmlutils.h:70
int compare_entity_with_name(gconstpointer entity, gconstpointer name)
Compare a given name with the name of a given entity.
Definition: xmlutils.c:191
GSList * entities_t
Entities.
Definition: xmlutils.h:60

References compare_entity_with_name(), and entity_s::entities.

Referenced by gmp_authenticate_info_ext(), gmp_authenticate_info_ext_c(), gmp_ping_c(), gmp_resume_task_report(), gmp_resume_task_report_c(), gmp_start_task_report(), gmp_start_task_report_c(), gmp_task_status(), osp_get_scan(), osp_get_scanner_details(), and osp_get_version().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ entity_name()

char* entity_name ( entity_t  entity)

Get the name an entity.

Parameters
[in]entityEntity.
Returns
Entity name, which is freed by free_entity.

Definition at line 173 of file xmlutils.c.

174 {
175  if (!entity)
176  return NULL;
177 
178  return entity->name;
179 }
char * name
Name.
Definition: xmlutils.h:67

References entity_s::name.

Referenced by compare_entity_with_name().

Here is the caller graph for this function:

◆ entity_text()

char* entity_text ( entity_t  entity)

Get the text an entity.

Parameters
[in]entityEntity.
Returns
Entity text, which is freed by free_entity.

Definition at line 157 of file xmlutils.c.

158 {
159  if (!entity)
160  return NULL;
161 
162  return entity->text;
163 }
char * text
Text.
Definition: xmlutils.h:68

References entity_s::text.

Referenced by gmp_authenticate_info_ext(), gmp_authenticate_info_ext_c(), gmp_ping_c(), gmp_resume_task_report(), gmp_resume_task_report_c(), gmp_start_task_report(), gmp_start_task_report_c(), gmp_task_status(), osp_get_scanner_details(), and osp_get_version().

Here is the caller graph for this function:

◆ find_element_in_xml_file()

int find_element_in_xml_file ( gchar *  file_path,
gchar *  find_element,
GHashTable *  find_attributes 
)

Tests if an XML file contains an element with given attributes.

Parameters
[in]file_pathPath of the XML file.
[in]find_elementName of the element to find.
[in]find_attributesGHashTable of attributes to find.
Returns
1 if element was found, 0 if not.

Definition at line 1525 of file xmlutils.c.

1527 {
1528  gchar buffer[XML_FILE_BUFFER_SIZE];
1529  FILE *file;
1530  int read_len;
1531  GMarkupParser xml_parser;
1532  GMarkupParseContext *xml_context;
1533  xml_search_data_t search_data;
1534  GError *error = NULL;
1535 
1536  search_data.find_element = find_element;
1537  search_data.find_attributes = find_attributes;
1538  search_data.found = 0;
1539 
1540  /* Create the XML parser. */
1541  xml_parser.start_element = xml_search_handle_start_element;
1542  xml_parser.end_element = NULL;
1543  xml_parser.text = NULL;
1544  xml_parser.passthrough = NULL;
1545  xml_parser.error = NULL;
1546  xml_context = g_markup_parse_context_new (&xml_parser, 0, &search_data, NULL);
1547 
1548  file = fopen (file_path, "r");
1549  if (file == NULL)
1550  {
1551  g_markup_parse_context_free (xml_context);
1552  g_warning ("%s: Failed to open '%s':", __FUNCTION__, strerror (errno));
1553  return 0;
1554  }
1555 
1556  while ((read_len = fread (&buffer, sizeof (char), XML_FILE_BUFFER_SIZE, file))
1557  && g_markup_parse_context_parse (xml_context, buffer, read_len, &error)
1558  && error == NULL)
1559  {
1560  }
1561  g_markup_parse_context_end_parse (xml_context, &error);
1562 
1563  fclose (file);
1564 
1565  g_markup_parse_context_free (xml_context);
1566  return search_data.found;
1567 }
Data for xml search functions.
Definition: xmlutils.h:77
GHashTable * find_attributes
Definition: xmlutils.h:82
static void xml_search_handle_start_element(GMarkupParseContext *ctx, const gchar *element_name, const gchar **attribute_names, const gchar **attribute_values, gpointer data, GError **error)
Handle the opening tag of an element in an XML search.
Definition: xmlutils.c:1457
#define XML_FILE_BUFFER_SIZE
Definition: xmlutils.c:1514
gchar * find_element
Definition: xmlutils.h:81

References xml_search_data_t::find_attributes, xml_search_data_t::find_element, xml_search_data_t::found, XML_FILE_BUFFER_SIZE, and xml_search_handle_start_element().

Here is the call graph for this function:

◆ first_entity()

entity_t first_entity ( entities_t  entities)

Return the first entity from an entities_t.

Parameters
[in]entitiesThe list of entities.
Returns
The first entity.

Definition at line 94 of file xmlutils.c.

95 {
96  if (entities)
97  return (entity_t) entities->data;
98  return NULL;
99 }
XML element.
Definition: xmlutils.h:65

Referenced by xml_count_entities().

Here is the caller graph for this function:

◆ free_entity()

void free_entity ( entity_t  entity)

Free an entity, recursively.

Parameters
[in]entityThe entity, can be NULL.

Definition at line 127 of file xmlutils.c.

128 {
129  if (entity)
130  {
131  g_free (entity->name);
132  g_free (entity->text);
133  if (entity->attributes)
134  g_hash_table_destroy (entity->attributes);
135  if (entity->entities)
136  {
137  GSList *list = entity->entities;
138  while (list)
139  {
140  free_entity (list->data);
141  list = list->next;
142  }
143  g_slist_free (entity->entities);
144  }
145  g_free (entity);
146  }
147 }
GHashTable * attributes
Attributes.
Definition: xmlutils.h:69
entities_t entities
Children.
Definition: xmlutils.h:70
char * name
Name.
Definition: xmlutils.h:67
void free_entity(entity_t entity)
Free an entity, recursively.
Definition: xmlutils.c:127
char * text
Text.
Definition: xmlutils.h:68

References entity_s::attributes, entity_s::entities, free_entity(), entity_s::name, and entity_s::text.

Referenced by free_entity(), gmp_authenticate(), gmp_authenticate_info_ext(), gmp_authenticate_info_ext_c(), gmp_check_response(), gmp_check_response_c(), gmp_delete_config_ext(), gmp_delete_lsc_credential_ext(), gmp_delete_port_list_ext(), gmp_delete_report(), gmp_delete_target_ext(), gmp_delete_task(), gmp_delete_task_ext(), gmp_get_report_ext(), gmp_get_tasks_ext(), gmp_modify_task_file(), gmp_ping(), gmp_ping_c(), gmp_read_create_response(), gmp_resume_task_report(), gmp_resume_task_report_c(), gmp_start_task_report(), gmp_start_task_report_c(), gmp_stop_task(), osp_delete_scan(), osp_get_scan(), osp_get_scanner_details(), osp_get_version(), osp_start_scan(), osp_stop_scan(), parse_entity(), read_string(), read_string_c(), try_read_entity_and_string(), and try_read_entity_and_string_s().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ next_entities()

entities_t next_entities ( entities_t  entities)

Return all the entities from an entities_t after the first.

Parameters
[in]entitiesThe list of entities.
Returns
All the entities that follow the first.

Definition at line 79 of file xmlutils.c.

80 {
81  if (entities)
82  return (entities_t) entities->next;
83  return NULL;
84 }
GSList * entities_t
Entities.
Definition: xmlutils.h:60

Referenced by osp_get_scanner_details(), and xml_count_entities().

Here is the caller graph for this function:

◆ parse_entity()

int parse_entity ( const char *  string,
entity_t entity 
)

Read an XML entity tree from a string.

Parameters
[in]stringInput string.
[out]entityPointer to an entity tree.
Returns
0 success, -1 read error, -2 parse error, -3 XML ended prematurely.

Definition at line 1099 of file xmlutils.c.

1100 {
1101  GMarkupParser xml_parser;
1102  GError *error = NULL;
1103  GMarkupParseContext *xml_context;
1104  context_data_t context_data;
1105 
1106  /* Create the XML parser. */
1107 
1108  xml_parser.start_element = handle_start_element;
1109  xml_parser.end_element = handle_end_element;
1110  xml_parser.text = handle_text;
1111  xml_parser.passthrough = NULL;
1112  xml_parser.error = handle_error;
1113 
1114  context_data.done = FALSE;
1115  context_data.first = NULL;
1116  context_data.current = NULL;
1117 
1118  /* Setup the XML context. */
1119 
1120  xml_context =
1121  g_markup_parse_context_new (&xml_parser, 0, &context_data, NULL);
1122 
1123  /* Parse the string. */
1124 
1125  g_markup_parse_context_parse (xml_context, string, strlen (string), &error);
1126  if (error)
1127  {
1128  g_error_free (error);
1129  if (context_data.first && context_data.first->data)
1130  {
1131  free_entity (context_data.first->data);
1132  g_slist_free_1 (context_data.first);
1133  }
1134  return -2;
1135  }
1136  if (context_data.done)
1137  {
1138  g_markup_parse_context_end_parse (xml_context, &error);
1139  if (error)
1140  {
1141  g_warning (" End error: %s\n", error->message);
1142  g_error_free (error);
1143  if (context_data.first && context_data.first->data)
1144  {
1145  free_entity (context_data.first->data);
1146  g_slist_free_1 (context_data.first);
1147  }
1148  return -2;
1149  }
1150  *entity = (entity_t) context_data.first->data;
1151  g_slist_free_1 (context_data.first);
1152  return 0;
1153  }
1154  if (context_data.first && context_data.first->data)
1155  {
1156  free_entity (context_data.first->data);
1157  g_slist_free_1 (context_data.first);
1158  }
1159  return -3;
1160 }
struct entity_s * entity_t
Definition: xmlutils.h:72
static void handle_start_element(GMarkupParseContext *context, const gchar *element_name, const gchar **attribute_names, const gchar **attribute_values, gpointer user_data, GError **error)
Handle the start of an OMP XML element.
Definition: xmlutils.c:275
GSList * first
The very first entity.
Definition: xmlutils.h:42
static void handle_text(GMarkupParseContext *context, const gchar *text, gsize text_len, gpointer user_data, GError **error)
Handle additional text of an XML element.
Definition: xmlutils.c:379
GSList * current
The element currently being parsed.
Definition: xmlutils.h:43
XML context.
Definition: xmlutils.h:40
gboolean done
Flag which is true when the first element is closed.
Definition: xmlutils.h:44
void free_entity(entity_t entity)
Free an entity, recursively.
Definition: xmlutils.c:127
static void handle_end_element(GMarkupParseContext *context, const gchar *element_name, gpointer user_data, GError **error)
Handle the end of an XML element.
Definition: xmlutils.c:328
void handle_error(GMarkupParseContext *context, GError *error, gpointer user_data)
Handle an OMP XML parsing error.
Definition: xmlutils.c:419

References context_data_t::current, context_data_t::done, context_data_t::first, free_entity(), handle_end_element(), handle_error(), handle_start_element(), and handle_text().

Here is the call graph for this function:

◆ print_entity()

void print_entity ( FILE *  stream,
entity_t  entity 
)

Print an XML entity.

Parameters
[in]entityThe entity.
[in]streamThe stream to which to print.

Definition at line 1244 of file xmlutils.c.

1245 {
1246  gchar *text_escaped = NULL;
1247  fprintf (stream, "<%s", entity->name);
1248  if (entity->attributes && g_hash_table_size (entity->attributes))
1249  g_hash_table_foreach (entity->attributes, foreach_print_attribute, stream);
1250  fprintf (stream, ">");
1251  text_escaped = g_markup_escape_text (entity->text, -1);
1252  fprintf (stream, "%s", text_escaped);
1253  g_free (text_escaped);
1254  g_slist_foreach (entity->entities, foreach_print_entity, stream);
1255  fprintf (stream, "</%s>", entity->name);
1256  fflush (stream);
1257 }
GHashTable * attributes
Attributes.
Definition: xmlutils.h:69
entities_t entities
Children.
Definition: xmlutils.h:70
char * name
Name.
Definition: xmlutils.h:67
static void foreach_print_entity(gpointer entity, gpointer stream)
Print an XML entity for g_slist_foreach.
Definition: xmlutils.c:1219
char * text
Text.
Definition: xmlutils.h:68
static void foreach_print_attribute(gpointer name, gpointer value, gpointer stream)
Print an XML attribute for g_hash_table_foreach.
Definition: xmlutils.c:1232

References entity_s::attributes, entity_s::entities, foreach_print_attribute(), foreach_print_entity(), entity_s::name, and entity_s::text.

Referenced by foreach_print_entity().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ print_entity_format()

void print_entity_format ( entity_t  entity,
gpointer  indent 
)

Print an XML entity to stdout, recursively printing its children.

Does very basic indentation for pretty printing.

This function is used as the (callback) GFunc in g_slist_foreach.

Parameters
[in]entityThe entity.
[in]indentIndentation level, indentation width is 2 spaces. Use GINT_TO_POINTER to convert a integer value when passing this parameter.

Definition at line 1287 of file xmlutils.c.

1288 {
1289  int i = 0;
1290  int indentation = GPOINTER_TO_INT (indent);
1291  gchar *text_escaped = NULL;
1292 
1293  for (i = 0; i < indentation; i++)
1294  printf (" ");
1295 
1296  printf ("<%s", entity->name);
1297  if (entity->attributes && g_hash_table_size (entity->attributes))
1298  g_hash_table_foreach (entity->attributes, foreach_print_attribute_format,
1299  indent);
1300  printf (">");
1301 
1302  text_escaped = g_markup_escape_text (entity->text, -1);
1303  printf ("%s", text_escaped);
1304  g_free (text_escaped);
1305 
1306  if (entity->entities)
1307  {
1308  printf ("\n");
1309  g_slist_foreach (entity->entities, (GFunc) print_entity_format,
1310  GINT_TO_POINTER (indentation + 1));
1311  for (i = 0; i < indentation; i++)
1312  printf (" ");
1313  }
1314 
1315  printf ("</%s>\n", entity->name);
1316 }
GHashTable * attributes
Attributes.
Definition: xmlutils.h:69
entities_t entities
Children.
Definition: xmlutils.h:70
void print_entity_format(entity_t entity, gpointer indent)
Print an XML entity to stdout, recursively printing its children.
Definition: xmlutils.c:1287
char * name
Name.
Definition: xmlutils.h:67
static void foreach_print_attribute_format(gpointer name, gpointer value, gpointer none)
Print an XML attribute for g_hash_table_foreach to stdout.
Definition: xmlutils.c:1269
char * text
Text.
Definition: xmlutils.h:68

References entity_s::attributes, entity_s::entities, foreach_print_attribute_format(), entity_s::name, print_entity_format(), and entity_s::text.

Referenced by print_entity_format().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ print_entity_to_string()

void print_entity_to_string ( entity_t  entity,
GString *  string 
)

Print an XML entity tree to a GString, appending it if string is not.

empty.

Parameters
[in]entityEntity tree to print to string.
[in,out]stringString to write to (will be created if NULL).

Definition at line 1197 of file xmlutils.c.

1198 {
1199  gchar *text_escaped = NULL;
1200  g_string_append_printf (string, "<%s", entity->name);
1201  if (entity->attributes && g_hash_table_size (entity->attributes))
1202  g_hash_table_foreach (entity->attributes, foreach_print_attribute_to_string,
1203  string);
1204  g_string_append_printf (string, ">");
1205  text_escaped = g_markup_escape_text (entity->text, -1);
1206  g_string_append_printf (string, "%s", text_escaped);
1207  g_free (text_escaped);
1208  g_slist_foreach (entity->entities, foreach_print_entity_to_string, string);
1209  g_string_append_printf (string, "</%s>", entity->name);
1210 }
static void foreach_print_entity_to_string(gpointer entity, gpointer string)
Print an XML entity for g_slist_foreach to a GString.
Definition: xmlutils.c:1169
GHashTable * attributes
Attributes.
Definition: xmlutils.h:69
entities_t entities
Children.
Definition: xmlutils.h:70
char * name
Name.
Definition: xmlutils.h:67
static void foreach_print_attribute_to_string(gpointer name, gpointer value, gpointer string)
Print an XML attribute for g_hash_table_foreach to a GString.
Definition: xmlutils.c:1182
char * text
Text.
Definition: xmlutils.h:68

References entity_s::attributes, entity_s::entities, foreach_print_attribute_to_string(), foreach_print_entity_to_string(), entity_s::name, and entity_s::text.

Referenced by foreach_print_entity_to_string(), and osp_get_scan().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ read_entity()

int read_entity ( gnutls_session_t *  session,
entity_t entity 
)

Read an XML entity tree from the manager.

Parameters
[in]sessionPointer to GNUTLS session.
[out]entityPointer to an entity tree.
Returns
0 success, -1 read error, -2 parse error, -3 end of file.

Definition at line 1057 of file xmlutils.c.

1058 {
1059  return try_read_entity (session, 0, entity);
1060 }
int try_read_entity(gnutls_session_t *session, int timeout, entity_t *entity)
Try read an XML entity tree from the manager.
Definition: xmlutils.c:1024

References try_read_entity().

Referenced by gmp_check_response(), and gmp_read_create_response().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ read_entity_and_string()

int read_entity_and_string ( gnutls_session_t *  session,
entity_t entity,
GString **  string_return 
)

Try read an XML entity tree from the manager.

Parameters
[in]sessionPointer to GNUTLS session.
[out]entityPointer to an entity tree.
[out]string_returnAn optional return location for the text read from the session. If NULL then it simply remains NULL. If a pointer to NULL then it points to a freshly allocated GString on successful return. Otherwise it points to an existing GString onto which the text is appended.
Returns
0 success, -1 read error, -2 parse error, -3 end of file.

Definition at line 880 of file xmlutils.c.

882 {
883  return try_read_entity_and_string (session, 0, entity, string_return);
884 }
int try_read_entity_and_string(gnutls_session_t *session, int timeout, entity_t *entity, GString **string_return)
Try read an XML entity tree from the manager.
Definition: xmlutils.c:442

References try_read_entity_and_string().

Referenced by read_entity_and_text(), and read_string().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ read_entity_and_string_c()

int read_entity_and_string_c ( gvm_connection_t connection,
entity_t entity,
GString **  string_return 
)

Try read an XML entity tree from the manager.

Parameters
[in]connectionConnection.
[out]entityPointer to an entity tree.
[out]string_returnAn optional return location for the text read from the session. If NULL then it simply remains NULL. If a pointer to NULL then it points to a freshly allocated GString on successful return. Otherwise it points to an existing GString onto which the text is appended.
Returns
0 success, -1 read error, -2 parse error, -3 end of file.

Definition at line 900 of file xmlutils.c.

902 {
903  if (connection->tls)
904  return try_read_entity_and_string (&connection->session, 0, entity,
905  string_return);
906  return try_read_entity_and_string_s (connection->socket, 0, entity,
907  string_return);
908 }
gnutls_session_t session
Session.
Definition: serverutils.h:47
int socket
Socket.
Definition: serverutils.h:46
int try_read_entity_and_string(gnutls_session_t *session, int timeout, entity_t *entity, GString **string_return)
Try read an XML entity tree from the manager.
Definition: xmlutils.c:442
int tls
Whether uses TCP-TLS (vs UNIX socket).
Definition: serverutils.h:45
int try_read_entity_and_string_s(int socket, int timeout, entity_t *entity, GString **string_return)
Try read an XML entity tree from the socket.
Definition: xmlutils.c:666

References gvm_connection_t::session, gvm_connection_t::socket, gvm_connection_t::tls, try_read_entity_and_string(), and try_read_entity_and_string_s().

Referenced by read_entity_and_text_c(), and read_string_c().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ read_entity_and_text()

int read_entity_and_text ( gnutls_session_t *  session,
entity_t entity,
char **  text 
)

Read an XML entity tree from the manager.

Parameters
[in]sessionPointer to GNUTLS session.
[out]entityPointer to an entity tree.
[out]textA pointer to a pointer, at which to store the address of a newly allocated string holding the text read from the session, if the text is required, else NULL.
Returns
0 success, -1 read error, -2 parse error, -3 end of file.

Definition at line 923 of file xmlutils.c.

924 {
925  if (text)
926  {
927  GString *string = NULL;
928  int ret = read_entity_and_string (session, entity, &string);
929  if (ret)
930  {
931  if (string)
932  g_string_free (string, TRUE);
933  return ret;
934  }
935  *text = g_string_free (string, FALSE);
936  return 0;
937  }
938  return read_entity_and_string (session, entity, NULL);
939 }
int read_entity_and_string(gnutls_session_t *session, entity_t *entity, GString **string_return)
Try read an XML entity tree from the manager.
Definition: xmlutils.c:880

References read_entity_and_string().

Here is the call graph for this function:

◆ read_entity_and_text_c()

int read_entity_and_text_c ( gvm_connection_t connection,
entity_t entity,
char **  text 
)

Read an XML entity tree from the manager.

Parameters
[in]connectionConnection.
[out]entityEntity tree.
[out]textA pointer to a pointer, at which to store the address of a newly allocated string holding the text read from the session, if the text is required, else NULL.
Returns
0 success, -1 read error, -2 parse error, -3 end of file.

Definition at line 954 of file xmlutils.c.

956 {
957  if (text)
958  {
959  GString *string = NULL;
960  int ret = read_entity_and_string_c (connection, entity, &string);
961  if (ret)
962  {
963  if (string)
964  g_string_free (string, TRUE);
965  return ret;
966  }
967  *text = g_string_free (string, FALSE);
968  return 0;
969  }
970  return read_entity_and_string_c (connection, entity, NULL);
971 }
int read_entity_and_string_c(gvm_connection_t *connection, entity_t *entity, GString **string_return)
Try read an XML entity tree from the manager.
Definition: xmlutils.c:900

References read_entity_and_string_c().

Here is the call graph for this function:

◆ read_entity_c()

int read_entity_c ( gvm_connection_t connection,
entity_t entity 
)

Read an XML entity tree from the manager.

Parameters
[in]connectionConnection.
[out]entityPointer to an entity tree.
Returns
0 success, -1 read error, -2 parse error, -3 end of file.

Definition at line 1085 of file xmlutils.c.

1086 {
1087  return try_read_entity_c (connection, 0, entity);
1088 }
int try_read_entity_c(gvm_connection_t *connection, int timeout, entity_t *entity)
Try read an XML entity tree from the manager.
Definition: xmlutils.c:1040

References try_read_entity_c().

Referenced by gmp_check_response_c(), gmp_resume_task_report_c(), and gmp_start_task_report_c().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ read_entity_s()

int read_entity_s ( int  socket,
entity_t entity 
)

Read an XML entity tree from the socket.

Parameters
[in]socketSocket to read from.
[out]entityPointer to an entity tree.
Returns
0 success, -1 read error, -2 parse error, -3 end of file.

Definition at line 1071 of file xmlutils.c.

1072 {
1073  return try_read_entity_and_string_s (socket, 0, entity, NULL);
1074 }
int try_read_entity_and_string_s(int socket, int timeout, entity_t *entity, GString **string_return)
Try read an XML entity tree from the socket.
Definition: xmlutils.c:666

References try_read_entity_and_string_s().

Here is the call graph for this function:

◆ read_string()

int read_string ( gnutls_session_t *  session,
GString **  string 
)

Read entity and text. Free the entity immediately.

Parameters
[in]sessionPointer to GNUTLS session to read from.
[out]stringReturn location for the string.
Returns
0 success, -1 read error, -2 parse error, -3 end of file.

Definition at line 982 of file xmlutils.c.

983 {
984  int ret = 0;
985  entity_t entity;
986 
987  if (!(ret = read_entity_and_string (session, &entity, string)))
988  free_entity (entity);
989 
990  return ret;
991 }
int read_entity_and_string(gnutls_session_t *session, entity_t *entity, GString **string_return)
Try read an XML entity tree from the manager.
Definition: xmlutils.c:880
XML element.
Definition: xmlutils.h:65
void free_entity(entity_t entity)
Free an entity, recursively.
Definition: xmlutils.c:127

References free_entity(), and read_entity_and_string().

Here is the call graph for this function:

◆ read_string_c()

int read_string_c ( gvm_connection_t connection,
GString **  string 
)

Read entity and text. Free the entity immediately.

Parameters
[in]connectionConnection.
[out]stringReturn location for the string.
Returns
0 success, -1 read error, -2 parse error, -3 end of file.

Definition at line 1002 of file xmlutils.c.

1003 {
1004  int ret = 0;
1005  entity_t entity;
1006 
1007  if (!(ret = read_entity_and_string_c (connection, &entity, string)))
1008  free_entity (entity);
1009 
1010  return ret;
1011 }
XML element.
Definition: xmlutils.h:65
int read_entity_and_string_c(gvm_connection_t *connection, entity_t *entity, GString **string_return)
Try read an XML entity tree from the manager.
Definition: xmlutils.c:900
void free_entity(entity_t entity)
Free an entity, recursively.
Definition: xmlutils.c:127

References free_entity(), and read_entity_and_string_c().

Here is the call graph for this function:

◆ try_read_entity()

int try_read_entity ( gnutls_session_t *  session,
int  timeout,
entity_t entity 
)

Try read an XML entity tree from the manager.

Parameters
[in]sessionPointer to GNUTLS session.
[in]timeoutServer idle time before giving up, in seconds. 0 to wait forever.
[out]entityPointer to an entity tree.
Returns
0 success, -1 read error, -2 parse error, -3 end of file, -4 timeout.

Definition at line 1024 of file xmlutils.c.

1025 {
1026  return try_read_entity_and_string (session, timeout, entity, NULL);
1027 }
int try_read_entity_and_string(gnutls_session_t *session, int timeout, entity_t *entity, GString **string_return)
Try read an XML entity tree from the manager.
Definition: xmlutils.c:442

References try_read_entity_and_string().

Referenced by gmp_authenticate_info_ext(), gmp_get_report_ext(), gmp_get_tasks_ext(), gmp_ping(), and read_entity().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ try_read_entity_and_string()

int try_read_entity_and_string ( gnutls_session_t *  session,
int  timeout,
entity_t entity,
GString **  string_return 
)

Try read an XML entity tree from the manager.

Parameters
[in]sessionPointer to GNUTLS session.
[in]timeoutServer idle time before giving up, in seconds. 0 to wait forever.
[out]entityPointer to an entity tree.
[out]string_returnAn optional return location for the text read from the session. If NULL then it simply remains NULL. If a pointer to NULL then it points to a freshly allocated GString on successful return. Otherwise it points to an existing GString onto which the text is appended.
Returns
0 success, -1 read error, -2 parse error, -3 end of file, -4 timeout.

Definition at line 442 of file xmlutils.c.

444 {
445  GMarkupParser xml_parser;
446  GError *error = NULL;
447  GMarkupParseContext *xml_context;
448  GString *string;
449  int socket;
450  time_t last_time;
451 
452  // Buffer for reading from the manager.
453  char *buffer;
454 
455  /* Record the start time. */
456 
457  if (time (&last_time) == -1)
458  {
459  g_warning (" failed to get current time: %s\n", strerror (errno));
460  return -1;
461  }
462 
463  if (timeout > 0)
464  {
465  /* Turn off blocking. */
466 
467  socket = GPOINTER_TO_INT (gnutls_transport_get_ptr (*session));
468  if (fcntl (socket, F_SETFL, O_NONBLOCK) == -1)
469  return -1;
470  }
471  else
472  /* Quiet compiler. */
473  socket = 0;
474 
475  buffer = g_malloc0 (BUFFER_SIZE);
476 
477  /* Setup return arg. */
478 
479  if (string_return == NULL)
480  string = NULL;
481  else if (*string_return == NULL)
482  string = g_string_new ("");
483  else
484  string = *string_return;
485 
486  /* Create the XML parser. */
487 
488  xml_parser.start_element = handle_start_element;
489  xml_parser.end_element = handle_end_element;
490  xml_parser.text = handle_text;
491  xml_parser.passthrough = NULL;
492  xml_parser.error = handle_error;
493 
494  context_data_t context_data;
495  context_data.done = FALSE;
496  context_data.first = NULL;
497  context_data.current = NULL;
498 
499  /* Setup the XML context. */
500 
501  xml_context =
502  g_markup_parse_context_new (&xml_parser, 0, &context_data, NULL);
503 
504  /* Read and parse, until encountering end of file or error. */
505 
506  while (1)
507  {
508  ssize_t count;
509  while (1)
510  {
511  g_debug (" asking for %i\n", BUFFER_SIZE);
512  count = gnutls_record_recv (*session, buffer, BUFFER_SIZE);
513  if (count < 0)
514  {
515  if (count == GNUTLS_E_INTERRUPTED)
516  /* Interrupted, try read again. */
517  continue;
518  if ((timeout > 0) && (count == GNUTLS_E_AGAIN))
519  {
520  /* Server still busy, either timeout or try read again. */
521  if ((timeout - (time (NULL) - last_time)) <= 0)
522  {
523  g_warning (" timeout\n");
524  if (fcntl (socket, F_SETFL, 0L) < 0)
525  g_warning ("%s :failed to set socket flag: %s",
526  __FUNCTION__, strerror (errno));
527  g_markup_parse_context_free (xml_context);
528  g_free (buffer);
529  return -4;
530  }
531  continue;
532  }
533  if (count == GNUTLS_E_REHANDSHAKE)
534  /* Try again. TODO Rehandshake. */
535  continue;
536  if (context_data.first && context_data.first->data)
537  {
538  free_entity (context_data.first->data);
539  g_slist_free_1 (context_data.first);
540  }
541  if (string && *string_return == NULL)
542  g_string_free (string, TRUE);
543  if (timeout > 0)
544  {
545  if (fcntl (socket, F_SETFL, 0L) < 0)
546  g_warning ("%s :failed to set socket flag: %s",
547  __FUNCTION__, strerror (errno));
548  }
549  g_markup_parse_context_free (xml_context);
550  g_free (buffer);
551  return -1;
552  }
553  if (count == 0)
554  {
555  /* End of file. */
556  g_markup_parse_context_end_parse (xml_context, &error);
557  if (error)
558  {
559  g_warning (" End error: %s\n", error->message);
560  g_error_free (error);
561  }
562  if (context_data.first && context_data.first->data)
563  {
564  free_entity (context_data.first->data);
565  g_slist_free_1 (context_data.first);
566  }
567  if (string && *string_return == NULL)
568  g_string_free (string, TRUE);
569  if (timeout > 0)
570  {
571  if (fcntl (socket, F_SETFL, 0L) < 0)
572  g_warning ("%s :failed to set socket flag: %s",
573  __FUNCTION__, strerror (errno));
574  }
575  g_markup_parse_context_free (xml_context);
576  g_free (buffer);
577  return -3;
578  }
579  break;
580  }
581 
582  g_debug ("<= %.*s\n", (int) count, buffer);
583 
584  if (string)
585  g_string_append_len (string, buffer, count);
586 
587  g_markup_parse_context_parse (xml_context, buffer, count, &error);
588  if (error)
589  {
590  g_error_free (error);
591  if (context_data.first && context_data.first->data)
592  {
593  free_entity (context_data.first->data);
594  g_slist_free_1 (context_data.first);
595  }
596  if (string && *string_return == NULL)
597  g_string_free (string, TRUE);
598  if (timeout > 0)
599  {
600  if (fcntl (socket, F_SETFL, 0L) < 0)
601  g_warning ("%s :failed to set socket flag: %s", __FUNCTION__,
602  strerror (errno));
603  }
604  g_markup_parse_context_free (xml_context);
605  g_free (buffer);
606  return -2;
607  }
608  if (context_data.done)
609  {
610  g_markup_parse_context_end_parse (xml_context, &error);
611  if (error)
612  {
613  g_warning (" End error: %s\n", error->message);
614  g_error_free (error);
615  if (context_data.first && context_data.first->data)
616  {
617  free_entity (context_data.first->data);
618  g_slist_free_1 (context_data.first);
619  }
620  if (timeout > 0)
621  fcntl (socket, F_SETFL, 0L);
622  g_markup_parse_context_free (xml_context);
623  g_free (buffer);
624  return -2;
625  }
626  *entity = (entity_t) context_data.first->data;
627  if (string)
628  *string_return = string;
629  if (timeout > 0)
630  fcntl (socket, F_SETFL, 0L);
631  g_markup_parse_context_free (xml_context);
632  g_free (buffer);
633  return 0;
634  }
635 
636  if ((timeout > 0) && (time (&last_time) == -1))
637  {
638  g_warning (" failed to get current time (1): %s\n",
639  strerror (errno));
640  if (fcntl (socket, F_SETFL, 0L) < 0)
641  g_warning ("%s :failed to set socket flag: %s", __FUNCTION__,
642  strerror (errno));
643  g_markup_parse_context_free (xml_context);
644  g_free (buffer);
645  return -1;
646  }
647  }
648 }
struct entity_s * entity_t
Definition: xmlutils.h:72
static void handle_start_element(GMarkupParseContext *context, const gchar *element_name, const gchar **attribute_names, const gchar **attribute_values, gpointer user_data, GError **error)
Handle the start of an OMP XML element.
Definition: xmlutils.c:275
GSList * first
The very first entity.
Definition: xmlutils.h:42
#define BUFFER_SIZE
Size of the buffer for reading from the manager.
Definition: xmlutils.c:49
static void handle_text(GMarkupParseContext *context, const gchar *text, gsize text_len, gpointer user_data, GError **error)
Handle additional text of an XML element.
Definition: xmlutils.c:379
GSList * current
The element currently being parsed.
Definition: xmlutils.h:43
XML context.
Definition: xmlutils.h:40
gboolean done
Flag which is true when the first element is closed.
Definition: xmlutils.h:44
void free_entity(entity_t entity)
Free an entity, recursively.
Definition: xmlutils.c:127
static void handle_end_element(GMarkupParseContext *context, const gchar *element_name, gpointer user_data, GError **error)
Handle the end of an XML element.
Definition: xmlutils.c:328
void handle_error(GMarkupParseContext *context, GError *error, gpointer user_data)
Handle an OMP XML parsing error.
Definition: xmlutils.c:419

References BUFFER_SIZE, context_data_t::current, context_data_t::done, context_data_t::first, free_entity(), handle_end_element(), handle_error(), handle_start_element(), and handle_text().

Referenced by read_entity_and_string(), read_entity_and_string_c(), try_read_entity(), and try_read_entity_c().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ try_read_entity_c()

int try_read_entity_c ( gvm_connection_t connection,
int  timeout,
entity_t entity 
)

Try read an XML entity tree from the manager.

Parameters
[in]connectionConnection.
[in]timeoutServer idle time before giving up, in seconds. 0 to wait forever.
[out]entityPointer to an entity tree.
Returns
0 success, -1 read error, -2 parse error, -3 end of file, -4 timeout.

Definition at line 1040 of file xmlutils.c.

1041 {
1042  if (connection->tls)
1043  return try_read_entity_and_string (&connection->session, 0, entity, NULL);
1044  return try_read_entity_and_string_s (connection->socket, timeout, entity,
1045  NULL);
1046 }
gnutls_session_t session
Session.
Definition: serverutils.h:47
int socket
Socket.
Definition: serverutils.h:46
int try_read_entity_and_string(gnutls_session_t *session, int timeout, entity_t *entity, GString **string_return)
Try read an XML entity tree from the manager.
Definition: xmlutils.c:442
int tls
Whether uses TCP-TLS (vs UNIX socket).
Definition: serverutils.h:45
int try_read_entity_and_string_s(int socket, int timeout, entity_t *entity, GString **string_return)
Try read an XML entity tree from the socket.
Definition: xmlutils.c:666

References gvm_connection_t::session, gvm_connection_t::socket, gvm_connection_t::tls, try_read_entity_and_string(), and try_read_entity_and_string_s().

Referenced by gmp_authenticate_info_ext_c(), gmp_ping_c(), and read_entity_c().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ xml_count_entities()

int xml_count_entities ( entities_t  entities)

Count the number of entities.

Parameters
[in]entitiesEntities.
Returns
Number of entities.

Definition at line 1411 of file xmlutils.c.

1412 {
1413  int count = 0;
1414  while (first_entity (entities))
1415  {
1416  entities = next_entities (entities);
1417  count++;
1418  }
1419  return count;
1420 }
entity_t first_entity(entities_t entities)
Return the first entity from an entities_t.
Definition: xmlutils.c:94
entities_t next_entities(entities_t entities)
Return all the entities from an entities_t after the first.
Definition: xmlutils.c:79

References first_entity(), and next_entities().

Here is the call graph for this function:

◆ xml_handle_end_element()

void xml_handle_end_element ( context_data_t context,
const gchar *  element_name 
)

Handle the end of an XML element.

Parameters
[in]contextParser context.
[in]element_nameXML element name.

Definition at line 364 of file xmlutils.c.

365 {
366  handle_end_element (NULL, element_name, context, NULL);
367 }
static void handle_end_element(GMarkupParseContext *context, const gchar *element_name, gpointer user_data, GError **error)
Handle the end of an XML element.
Definition: xmlutils.c:328

References handle_end_element().

Here is the call graph for this function:

◆ xml_handle_start_element()

void xml_handle_start_element ( context_data_t context,
const gchar *  element_name,
const gchar **  attribute_names,
const gchar **  attribute_values 
)

Handle the start of an OMP XML element.

Parameters
[in]contextParser context.
[in]element_nameXML element name.
[in]attribute_namesXML attribute name.
[in]attribute_valuesXML attribute values.

Definition at line 311 of file xmlutils.c.

314 {
315  return handle_start_element (NULL, element_name, attribute_names,
316  attribute_values, context, NULL);
317 }
static void handle_start_element(GMarkupParseContext *context, const gchar *element_name, const gchar **attribute_names, const gchar **attribute_values, gpointer user_data, GError **error)
Handle the start of an OMP XML element.
Definition: xmlutils.c:275

References handle_start_element().

Here is the call graph for this function:

◆ xml_handle_text()

void xml_handle_text ( context_data_t context,
const gchar *  text,
gsize  text_len 
)

Handle additional text of an XML element.

Parameters
[in]contextParser context.
[in]textThe text.
[in]text_lenLength of the text.

Definition at line 406 of file xmlutils.c.

407 {
408  handle_text (NULL, text, text_len, context, NULL);
409 }
static void handle_text(GMarkupParseContext *context, const gchar *text, gsize text_len, gpointer user_data, GError **error)
Handle additional text of an XML element.
Definition: xmlutils.c:379

References handle_text().

Here is the call graph for this function:

◆ xml_string_append()

void xml_string_append ( GString *  xml,
const char *  format,
  ... 
)

Append formatted escaped XML to a string.

Parameters
[in]xmlXML string.
[in]formatFormat string.
[in]...Arguments for format string.
Returns
Result of XSL transformation.

Definition at line 1432 of file xmlutils.c.

1433 {
1434  gchar *piece;
1435  va_list args;
1436 
1437  va_start (args, format);
1438  piece = g_markup_vprintf_escaped (format, args);
1439  va_end (args);
1440  g_string_append (xml, piece);
1441  g_free (piece);
1442 }

Referenced by gmp_get_system_reports_ext().

Here is the caller graph for this function: