OpenVAS Libraries
9.0.3
|
Simple XML reader. More...
#include <assert.h>
#include <errno.h>
#include <fcntl.h>
#include <glib.h>
#include <string.h>
#include <unistd.h>
#include "xml.h"
Go to the source code of this file.
Data Structures | |
struct | context_data_t |
XML context. More... | |
Macros | |
#define | G_LOG_DOMAIN "lib xml" |
GLib log domain. More... | |
#define | BUFFER_SIZE 1048576 |
Size of the buffer for reading from the manager. More... | |
#define | XML_FILE_BUFFER_SIZE 1048576 |
Functions | |
entity_t | make_entity (const char *name, const char *text) |
Create an entity. More... | |
entities_t | next_entities (entities_t entities) |
Return all the entities from an entities_t after the first. More... | |
entity_t | first_entity (entities_t entities) |
Return the first entity from an entities_t. More... | |
entity_t | add_entity (entities_t *entities, const char *name, const char *text) |
Add an XML entity to a tree of entities. More... | |
void | free_entity (entity_t entity) |
Free an entity, recursively. More... | |
char * | entity_text (entity_t entity) |
Get the text an entity. More... | |
char * | entity_name (entity_t entity) |
Get the name an entity. More... | |
int | compare_entity_with_name (gconstpointer entity, gconstpointer name) |
Compare a given name with the name of a given entity. More... | |
entity_t | entity_child (entity_t entity, const char *name) |
Get a child of an entity. More... | |
const char * | entity_attribute (entity_t entity, const char *name) |
Get an attribute of an entity. More... | |
void | add_attributes (entity_t entity, const gchar **names, const gchar **values) |
Add attributes from an XML callback to an entity. More... | |
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. More... | |
void | handle_end_element (GMarkupParseContext *context, const gchar *element_name, gpointer user_data, GError **error) |
Handle the end of an XML element. More... | |
void | handle_text (GMarkupParseContext *context, const gchar *text, gsize text_len, gpointer user_data, GError **error) |
Handle additional text of an XML element. More... | |
void | handle_error (GMarkupParseContext *context, GError *error, gpointer user_data) |
Handle an OMP XML parsing error. More... | |
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. More... | |
int | try_read_entity_and_string_c (openvas_connection_t *connection, int timeout, entity_t *entity, GString **string_return) |
Try read an XML entity tree from the manager. More... | |
int | read_entity_and_string (gnutls_session_t *session, entity_t *entity, GString **string_return) |
Try read an XML entity tree from the manager. More... | |
int | read_entity_and_string_c (openvas_connection_t *connection, entity_t *entity, GString **string_return) |
Try read an XML entity tree from the manager. More... | |
int | read_entity_and_text (gnutls_session_t *session, entity_t *entity, char **text) |
Read an XML entity tree from the manager. More... | |
int | read_entity_and_text_c (openvas_connection_t *connection, entity_t *entity, char **text) |
Read an XML entity tree from the manager. More... | |
int | read_string (gnutls_session_t *session, GString **string) |
Read entity and text. Free the entity immediately. More... | |
int | read_string_c (openvas_connection_t *connection, GString **string) |
Read entity and text. Free the entity immediately. More... | |
int | try_read_entity (gnutls_session_t *session, int timeout, entity_t *entity) |
Try read an XML entity tree from the manager. More... | |
int | try_read_entity_c (openvas_connection_t *connection, int timeout, entity_t *entity) |
Try read an XML entity tree from the manager. More... | |
int | read_entity (gnutls_session_t *session, entity_t *entity) |
Read an XML entity tree from the manager. More... | |
int | read_entity_c (openvas_connection_t *connection, entity_t *entity) |
Read an XML entity tree from the manager. More... | |
int | parse_entity (const char *string, entity_t *entity) |
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... | |
void | print_entity (FILE *stream, entity_t entity) |
Print an XML entity. More... | |
void | print_entity_format (entity_t entity, gpointer indent) |
Print an XML entity to stdout, recusively printing its children. More... | |
gboolean | compare_find_attribute (gpointer key, gpointer value, gpointer attributes2) |
Look for a key-value pair in a hash table. More... | |
int | compare_entities (entity_t entity1, entity_t entity2) |
Compare two XML entity. More... | |
int | xml_count_entities (entities_t entities) |
Count the number of entities. More... | |
void | xml_string_append (GString *xml, const char *format,...) |
Append formatted escaped XML to a string. More... | |
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. More... | |
Simple XML reader.
This is a generic XML interface. The key function is read_entity.
The openvas-manager tests use this interface to read and handle the XML returned by the manager. The OMP part of openvas-client does the same.
There are examples of using this interface in omp.c and in the openvas-manager tests.
Definition in file xml.c.
#define BUFFER_SIZE 1048576 |
void add_attributes | ( | entity_t | entity, |
const gchar ** | names, | ||
const gchar ** | values | ||
) |
Add attributes from an XML callback to an entity.
[in] | entity | The entity. |
[in] | names | List of attribute names. |
[in] | values | List of attribute values. |
Definition at line 256 of file xml.c.
References entity_s::attributes.
Referenced by handle_start_element().
entity_t add_entity | ( | entities_t * | entities, |
const char * | name, | ||
const char * | text | ||
) |
Add an XML entity to a tree of entities.
[in] | entities | The tree of entities |
[in] | name | Name of the entity. Copied, copy is freed by free_entity. |
[in] | text | Text of the entity. Copied, copy is freed by free_entity. |
Definition at line 134 of file xml.c.
References make_entity(), and name.
Referenced by handle_start_element().
Compare two XML entity.
[in] | entity1 | First entity. |
[in] | entity2 | First entity. |
Definition at line 1279 of file xml.c.
References entity_s::attributes, compare_entities(), compare_find_attribute(), entity_s::entities, entity_s::name, and entity_s::text.
Referenced by compare_entities().
int compare_entity_with_name | ( | gconstpointer | entity, |
gconstpointer | name | ||
) |
Compare a given name with the name of a given entity.
[in] | entity | Entity. |
[in] | name | Name. |
Definition at line 206 of file xml.c.
References entity_name(), and name.
Referenced by entity_child().
gboolean compare_find_attribute | ( | gpointer | key, |
gpointer | value, | ||
gpointer | attributes2 | ||
) |
Look for a key-value pair in a hash table.
[in] | key | Key. |
[in] | value | Value. |
[in] | attributes2 | The hash table. |
Definition at line 1261 of file xml.c.
Referenced by compare_entities().
const char* entity_attribute | ( | entity_t | entity, |
const char * | name | ||
) |
Get an attribute of an entity.
[in] | entity | Entity. |
[in] | name | Name of the attribute. |
Definition at line 241 of file xml.c.
References entity_s::attributes, and name.
Referenced by check_response(), check_response_c(), omp_authenticate(), omp_authenticate_info_ext(), omp_authenticate_info_ext_c(), omp_get_report_ext(), omp_get_system_reports(), omp_get_system_reports_ext(), omp_get_targets(), omp_get_task_ext(), omp_get_tasks(), omp_get_tasks_ext(), omp_ping(), omp_ping_c(), omp_read_create_response(), omp_resume_task_report(), omp_resume_task_report_c(), omp_start_task_report(), and omp_start_task_report_c().
Get a child of an entity.
[in] | entity | Entity. |
[in] | name | Name of the child. |
Definition at line 220 of file xml.c.
References compare_entity_with_name(), entity_s::entities, and name.
Referenced by omp_authenticate_info_ext(), omp_authenticate_info_ext_c(), omp_ping_c(), omp_resume_task_report(), omp_resume_task_report_c(), omp_start_task_report(), omp_start_task_report_c(), and omp_task_status().
char* entity_name | ( | entity_t | entity | ) |
Get the name an entity.
[in] | entity | Entity. |
Definition at line 191 of file xml.c.
References entity_s::name.
Referenced by compare_entity_with_name().
char* entity_text | ( | entity_t | entity | ) |
Get the text an entity.
[in] | entity | Entity. |
Definition at line 178 of file xml.c.
References entity_s::text.
Referenced by omp_authenticate_info_ext(), omp_authenticate_info_ext_c(), omp_ping_c(), omp_resume_task_report(), omp_resume_task_report_c(), omp_start_task_report(), omp_start_task_report_c(), and omp_task_status().
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.
[in] | file_path | Path of the XML file. |
[in] | find_element | Name of the element to find. |
[in] | find_attributes | GHashTable of attributes to find. |
Definition at line 1463 of file xml.c.
References xml_search_data_t::find_attributes, xml_search_data_t::find_element, xml_search_data_t::found, and XML_FILE_BUFFER_SIZE.
entity_t first_entity | ( | entities_t | entities | ) |
Return the first entity from an entities_t.
[in] | entities | The list of entities. |
Definition at line 115 of file xml.c.
Referenced by xml_count_entities().
void free_entity | ( | entity_t | entity | ) |
Free an entity, recursively.
[in] | entity | The entity, can be NULL. |
Definition at line 148 of file xml.c.
References entity_s::attributes, entity_s::entities, free_entity(), entity_s::name, list::next, and entity_s::text.
Referenced by check_response(), check_response_c(), free_entity(), omp_authenticate(), omp_authenticate_info_ext(), omp_authenticate_info_ext_c(), omp_get_report_ext(), omp_get_system_reports(), omp_get_system_reports_ext(), omp_get_targets(), omp_get_task_ext(), omp_get_tasks(), omp_get_tasks_ext(), omp_ping(), omp_ping_c(), omp_read_create_response(), omp_resume_task_report(), omp_resume_task_report_c(), omp_start_task_report(), omp_start_task_report_c(), parse_entity(), read_string(), read_string_c(), try_read_entity_and_string(), and try_read_entity_and_string_c().
void handle_end_element | ( | GMarkupParseContext * | context, |
const gchar * | element_name, | ||
gpointer | user_data, | ||
GError ** | error | ||
) |
Handle the end of an XML element.
[in] | context | Parser context. |
[in] | element_name | XML element name. |
[in] | user_data | Dummy parameter. |
[in] | error | Error parameter. |
Definition at line 321 of file xml.c.
References context_data_t::current, context_data_t::done, and context_data_t::first.
Referenced by parse_entity(), try_read_entity_and_string(), and try_read_entity_and_string_c().
void handle_error | ( | GMarkupParseContext * | context, |
GError * | error, | ||
gpointer | user_data | ||
) |
Handle an OMP XML parsing error.
[in] | context | Parser context. |
[in] | error | The error. |
[in] | user_data | Dummy parameter. |
Definition at line 386 of file xml.c.
Referenced by parse_entity(), try_read_entity_and_string(), and try_read_entity_and_string_c().
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.
[in] | context | Parser context. |
[in] | element_name | XML element name. |
[in] | attribute_names | XML attribute name. |
[in] | attribute_values | XML attribute values. |
[in] | user_data | Dummy parameter. |
[in] | error | Error parameter. |
Definition at line 285 of file xml.c.
References add_attributes(), add_entity(), context_data_t::current, entity_s::entities, and context_data_t::first.
Referenced by parse_entity(), try_read_entity_and_string(), and try_read_entity_and_string_c().
void handle_text | ( | GMarkupParseContext * | context, |
const gchar * | text, | ||
gsize | text_len, | ||
gpointer | user_data, | ||
GError ** | error | ||
) |
Handle additional text of an XML element.
[in] | context | Parser context. |
[in] | text | The text. |
[in] | text_len | Length of the text. |
[in] | user_data | Dummy parameter. |
[in] | error | Error parameter. |
Definition at line 359 of file xml.c.
References context_data_t::current, and entity_s::text.
Referenced by parse_entity(), try_read_entity_and_string(), and try_read_entity_and_string_c().
entity_t make_entity | ( | const char * | name, |
const char * | text | ||
) |
Create an entity.
[in] | name | Name of the entity. Copied, freed by free_entity. |
[in] | text | Text of the entity. Copied, freed by free_entity. |
Definition at line 81 of file xml.c.
References entity_s::attributes, entity_s::entities, entity_s::name, name, and entity_s::text.
Referenced by add_entity().
entities_t next_entities | ( | entities_t | entities | ) |
Return all the entities from an entities_t after the first.
[in] | entities | The list of entities. |
Definition at line 100 of file xml.c.
Referenced by xml_count_entities().
int parse_entity | ( | const char * | string, |
entity_t * | entity | ||
) |
Read an XML entity tree from a string.
[in] | string | Input string. |
[out] | entity | Pointer to an entity tree. |
Definition at line 1032 of file xml.c.
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().
void print_entity | ( | FILE * | stream, |
entity_t | entity | ||
) |
Print an XML entity.
[in] | entity | The entity. |
[in] | stream | The stream to which to print. |
Definition at line 1177 of file xml.c.
References entity_s::attributes, and entity_s::name.
void print_entity_format | ( | entity_t | entity, |
gpointer | indent | ||
) |
Print an XML entity to stdout, recusively printing its children.
Does very basic indentation for pretty printing.
This function is used as the (callback) GFunc in g_slist_foreach.
[in] | entity | The entity. |
[in] | indent | Indentation level, indentation width is 2 spaces. Use GINT_TO_POINTER to convert a integer value when passing this parameter. |
Definition at line 1220 of file xml.c.
References entity_s::attributes, and entity_s::name.
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.
[in] | entity | Entity tree to print to string. |
[in,out] | string | String to write to (will be created if NULL). |
Definition at line 1130 of file xml.c.
References entity_s::attributes, and entity_s::name.
int read_entity | ( | gnutls_session_t * | session, |
entity_t * | entity | ||
) |
Read an XML entity tree from the manager.
[in] | session | Pointer to GNUTLS session. |
[out] | entity | Pointer to an entity tree. |
Definition at line 1004 of file xml.c.
References try_read_entity().
Referenced by check_response(), omp_authenticate(), omp_get_system_reports(), omp_get_system_reports_ext(), omp_get_targets(), omp_get_task_ext(), omp_get_tasks(), omp_read_create_response(), omp_resume_task_report(), and omp_start_task_report().
int read_entity_and_string | ( | gnutls_session_t * | session, |
entity_t * | entity, | ||
GString ** | string_return | ||
) |
Try read an XML entity tree from the manager.
[in] | session | Pointer to GNUTLS session. |
[out] | entity | Pointer to an entity tree. |
[out] | string_return | An 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. |
Definition at line 831 of file xml.c.
References try_read_entity_and_string().
Referenced by read_entity_and_text(), and read_string().
int read_entity_and_string_c | ( | openvas_connection_t * | connection, |
entity_t * | entity, | ||
GString ** | string_return | ||
) |
Try read an XML entity tree from the manager.
[in] | connection | Connection. |
[out] | entity | Pointer to an entity tree. |
[out] | string_return | An 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. |
Definition at line 852 of file xml.c.
References try_read_entity_and_string_c().
Referenced by read_entity_and_text_c(), and read_string_c().
int read_entity_and_text | ( | gnutls_session_t * | session, |
entity_t * | entity, | ||
char ** | text | ||
) |
Read an XML entity tree from the manager.
[in] | session | Pointer to GNUTLS session. |
[out] | entity | Pointer to an entity tree. |
[out] | text | A 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. |
Definition at line 871 of file xml.c.
References read_entity_and_string().
int read_entity_and_text_c | ( | openvas_connection_t * | connection, |
entity_t * | entity, | ||
char ** | text | ||
) |
Read an XML entity tree from the manager.
[in] | connection | Connection. |
[out] | entity | Entity tree. |
[out] | text | A 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. |
Definition at line 903 of file xml.c.
References read_entity_and_string_c().
int read_entity_c | ( | openvas_connection_t * | connection, |
entity_t * | entity | ||
) |
Read an XML entity tree from the manager.
[in] | session | Pointer to GNUTLS session. |
[out] | entity | Pointer to an entity tree. |
Definition at line 1018 of file xml.c.
References try_read_entity_c().
Referenced by check_response_c(), omp_resume_task_report_c(), and omp_start_task_report_c().
int read_string | ( | gnutls_session_t * | session, |
GString ** | string | ||
) |
Read entity and text. Free the entity immediately.
[in] | session | Pointer to GNUTLS session to read from. |
[out] | string | Return location for the string. |
Definition at line 931 of file xml.c.
References free_entity(), and read_entity_and_string().
int read_string_c | ( | openvas_connection_t * | connection, |
GString ** | string | ||
) |
Read entity and text. Free the entity immediately.
[in] | connection | Connection. |
[out] | string | Return location for the string. |
Definition at line 951 of file xml.c.
References free_entity(), and read_entity_and_string_c().
int try_read_entity | ( | gnutls_session_t * | session, |
int | timeout, | ||
entity_t * | entity | ||
) |
Try read an XML entity tree from the manager.
[in] | session | Pointer to GNUTLS session. |
[in] | timeout | Server idle time before giving up, in seconds. 0 to wait forever. |
[out] | entity | Pointer to an entity tree. |
Definition at line 973 of file xml.c.
References try_read_entity_and_string().
Referenced by omp_authenticate_info_ext(), omp_get_report_ext(), omp_get_tasks_ext(), omp_ping(), and read_entity().
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.
[in] | session | Pointer to GNUTLS session. |
[in] | timeout | Server idle time before giving up, in seconds. 0 to wait forever. |
[out] | entity | Pointer to an entity tree. |
[out] | string | An 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. |
Definition at line 410 of file xml.c.
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(), try_read_entity(), and try_read_entity_and_string_c().
int try_read_entity_and_string_c | ( | openvas_connection_t * | connection, |
int | timeout, | ||
entity_t * | entity, | ||
GString ** | string_return | ||
) |
Try read an XML entity tree from the manager.
[in] | connection | Connection. |
[in] | timeout | Server idle time before giving up, in seconds. 0 to wait forever. |
[out] | entity | Pointer to an entity tree. |
[out] | string | An 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. |
Definition at line 625 of file xml.c.
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(), handle_text(), openvas_connection_t::session, openvas_connection_t::socket, openvas_connection_t::tls, and try_read_entity_and_string().
Referenced by read_entity_and_string_c(), and try_read_entity_c().
int try_read_entity_c | ( | openvas_connection_t * | connection, |
int | timeout, | ||
entity_t * | entity | ||
) |
Try read an XML entity tree from the manager.
[in] | connection | Connection. |
[in] | timeout | Server idle time before giving up, in seconds. 0 to wait forever. |
[out] | entity | Pointer to an entity tree. |
Definition at line 989 of file xml.c.
References try_read_entity_and_string_c().
Referenced by omp_authenticate_info_ext_c(), omp_ping_c(), and read_entity_c().
int xml_count_entities | ( | entities_t | entities | ) |
Count the number of entities.
[in] | entities | Entities. |
Definition at line 1345 of file xml.c.
References first_entity(), and next_entities().
void xml_string_append | ( | GString * | xml, |
const char * | format, | ||
... | |||
) |
Append formatted escaped XML to a string.
[in] | xml | XML string. |
[in] | format | Format string. |
[in] | ... | Arguments for format string. |
Definition at line 1366 of file xml.c.
Referenced by omp_get_system_reports_ext().