From 660de68b7fa767bebedeec91cb62dd1c5fb9e14c Mon Sep 17 00:00:00 2001 From: Alberto Leiva Popper Date: Mon, 6 Nov 2023 17:24:17 -0600 Subject: [PATCH] Privatize struct publish and struct withdraw --- src/rrdp/rrdp_objects.c | 27 ---------------- src/rrdp/rrdp_objects.h | 18 ----------- src/rrdp/rrdp_parser.c | 69 +++++++++++++++++++++-------------------- 3 files changed, 35 insertions(+), 79 deletions(-) diff --git a/src/rrdp/rrdp_objects.c b/src/rrdp/rrdp_objects.c index 6989ef86..91022a20 100644 --- a/src/rrdp/rrdp_objects.c +++ b/src/rrdp/rrdp_objects.c @@ -136,30 +136,3 @@ update_notification_cleanup(struct update_notification *file) deltas_head_cleanup(&file->deltas_list, delta_head_destroy); uri_refput(file->uri); } - -void -publish_init(struct publish *tag) -{ - metadata_init(&tag->meta); - tag->content = NULL; - tag->content_len = 0; -} - -void -publish_cleanup(struct publish *tag) -{ - metadata_cleanup(&tag->meta); - free(tag->content); -} - -void -withdraw_init(struct withdraw *tag) -{ - metadata_init(&tag->meta); -} - -void -withdraw_cleanup(struct withdraw *tag) -{ - metadata_cleanup(&tag->meta); -} diff --git a/src/rrdp/rrdp_objects.h b/src/rrdp/rrdp_objects.h index 3a15f2c8..7aa1775f 100644 --- a/src/rrdp/rrdp_objects.h +++ b/src/rrdp/rrdp_objects.h @@ -16,18 +16,6 @@ struct file_metadata { size_t hash_len; }; -/* Represents a element */ -struct publish { - struct file_metadata meta; - unsigned char *content; - size_t content_len; -}; - -/* Represents a element */ -struct withdraw { - struct file_metadata meta; -}; - /* Delta element located at an update notification file */ struct delta_head { /* @@ -64,10 +52,4 @@ int deltas_head_for_each(struct deltas_head *, unsigned long, unsigned long, delta_head_cb, void *); int deltas_head_sort(struct deltas_head *, unsigned long); -void publish_init(struct publish *); -void publish_cleanup(struct publish *); - -void withdraw_init(struct withdraw *); -void withdraw_cleanup(struct withdraw *); - #endif /* SRC_RRDP_RRDP_OBJECTS_H_ */ diff --git a/src/rrdp/rrdp_parser.c b/src/rrdp/rrdp_parser.c index cbadb943..54489ed0 100644 --- a/src/rrdp/rrdp_parser.c +++ b/src/rrdp/rrdp_parser.c @@ -30,6 +30,18 @@ #define RRDP_ATTR_URI "uri" #define RRDP_ATTR_HASH "hash" +/* Represents a element */ +struct publish { + struct file_metadata meta; + unsigned char *content; + size_t content_len; +}; + +/* Represents a element */ +struct withdraw { + struct file_metadata meta; +}; + /* Context while reading a snapshot */ struct rdr_snapshot_ctx { /* Parent data to validate session ID and serial */ @@ -392,26 +404,23 @@ parse_publish(xmlTextReaderPtr reader, struct rpki_uri *notif, char *base64_str; int error; - publish_init(tag); - error = parse_doc_data(reader, notif, hr, &tag->meta); if (error) - goto release_tmp; + return error; /* Read the text */ if (xmlTextReaderRead(reader) != 1) { - error = pr_val_err("Couldn't read publish content of element '%s'", + return pr_val_err("Couldn't read publish content of element '%s'", uri_get_global(tag->meta.uri)); - goto release_tmp; } error = parse_string(reader, NULL, &base64_str); if (error) - goto release_tmp; - + return error; error = base64_read(base64_str, &tag->content, &tag->content_len); + free(base64_str); if (error) - goto release_base64; + return error; /* rfc8181#section-2.2 but considering optional hash */ if (tag->meta.hash_len > 0) { @@ -421,18 +430,11 @@ parse_publish(xmlTextReaderPtr reader, struct rpki_uri *notif, if (error) { pr_val_info("Hash of base64 decoded element from URI '%s' doesn't match element hash", uri_get_global(tag->meta.uri)); - error = EINVAL; - goto release_base64; + return EINVAL; } } - free(base64_str); return 0; -release_base64: - free(base64_str); -release_tmp: - publish_cleanup(tag); - return error; } static int @@ -441,22 +443,12 @@ parse_withdraw(xmlTextReaderPtr reader, struct rpki_uri *notif, { int error; - withdraw_init(tag); - error = parse_doc_data(reader, notif, HR_MANDATORY, &tag->meta); if (error) - goto fail; + return error; - error = hash_validate_file(tag->meta.uri, tag->meta.hash, + return hash_validate_file(tag->meta.uri, tag->meta.hash, tag->meta.hash_len); - if (error) - goto fail; - - return 0; - -fail: - withdraw_cleanup(tag); - return error; } static int @@ -475,15 +467,15 @@ write_from_uri(struct rpki_uri *uri, unsigned char *content, size_t content_len) return error; written = fwrite(content, sizeof(unsigned char), content_len, out); + file_close(out); + if (written != content_len) { - file_close(out); return pr_val_err( "Couldn't write file '%s' (error code not available)", uri_get_local(uri) ); } - file_close(out); return 0; } @@ -506,13 +498,19 @@ parse_publish_elem(xmlTextReaderPtr reader, struct rpki_uri *notif, struct publish tag; int error; + metadata_init(&tag.meta); + tag.content = NULL; + tag.content_len = 0; + error = parse_publish(reader, notif, hr, &tag); if (error) - return error; + goto end; error = write_from_uri(tag.meta.uri, tag.content, tag.content_len); - publish_cleanup(&tag); +end: + metadata_cleanup(&tag.meta); + free(tag.content); return error; } @@ -526,13 +524,16 @@ parse_withdraw_elem(xmlTextReaderPtr reader, struct rpki_uri *notif) struct withdraw tag; int error; + metadata_init(&tag.meta); + error = parse_withdraw(reader, notif, &tag); if (error) - return error; + goto end; error = delete_from_uri(tag.meta.uri); - withdraw_cleanup(&tag); +end: + metadata_cleanup(&tag.meta); return error; } -- 2.47.2