aboutsummaryrefslogtreecommitdiffstats
path: root/sync_pipe_write.c
diff options
context:
space:
mode:
Diffstat (limited to 'sync_pipe_write.c')
-rw-r--r--sync_pipe_write.c76
1 files changed, 56 insertions, 20 deletions
diff --git a/sync_pipe_write.c b/sync_pipe_write.c
index ea1a7927d0..c279e5765f 100644
--- a/sync_pipe_write.c
+++ b/sync_pipe_write.c
@@ -14,6 +14,7 @@
#include <glib.h>
#include <wsutil/file_util.h>
+#include <wsutil/ws_assert.h>
#include "sync_pipe.h"
@@ -22,13 +23,12 @@
/* write a single message header to the recipient pipe */
-ssize_t
-pipe_write_header(int pipe_fd, char indicator, int length)
+static ssize_t
+sync_pipe_write_header(int pipe_fd, char indicator, unsigned int length)
{
guchar header[1+3]; /* indicator + 3-byte len */
-
- g_assert(length <= SP_MAX_MSG_LEN);
+ ws_assert(length <= SP_MAX_MSG_LEN);
/* write header (indicator + 3-byte len) */
header[0] = indicator;
@@ -41,17 +41,17 @@ pipe_write_header(int pipe_fd, char indicator, int length)
}
-/* write a message to the recipient pipe in the standard format
- (3 digit message length (excluding length and indicator field),
- 1 byte message indicator and the rest is the message).
+/* Write a message, with a string body, to the recipient pipe in the
+ standard format (1-byte message indicator, 3-byte message length
+ (excluding length and indicator field), and the string.
If msg is NULL, the message has only a length and indicator. */
void
-pipe_write_block(int pipe_fd, char indicator, const char *msg)
+sync_pipe_write_string_msg(int pipe_fd, char indicator, const char *msg)
{
ssize_t ret;
int len;
- /*g_warning("write %d enter", pipe_fd);*/
+ /*ws_warning("write %d enter", pipe_fd);*/
if(msg != NULL) {
len = (int) strlen(msg) + 1; /* including the terminating '\0'! */
@@ -60,39 +60,75 @@ pipe_write_block(int pipe_fd, char indicator, const char *msg)
}
/* write header (indicator + 3-byte len) */
- ret = pipe_write_header(pipe_fd, indicator, len);
+ ret = sync_pipe_write_header(pipe_fd, indicator, len);
if(ret == -1) {
return;
}
/* write value (if we have one) */
if(len) {
- /*g_warning("write %d indicator: %c value len: %u msg: %s", pipe_fd, indicator, len, msg);*/
+ /*ws_warning("write %d indicator: %c value len: %u msg: %s", pipe_fd, indicator, len, msg);*/
ret = ws_write(pipe_fd, msg, len);
if(ret == -1) {
return;
}
} else {
- /*g_warning("write %d indicator: %c no value", pipe_fd, indicator);*/
+ /*ws_warning("write %d indicator: %c no value", pipe_fd, indicator);*/
}
- /*g_warning("write %d leave", pipe_fd);*/
+ /*ws_warning("write %d leave", pipe_fd);*/
}
+/* Size of buffer to hold decimal representation of
+ signed/unsigned 64-bit int */
+#define SP_DECISIZE 20
+
+/* Write a message, with an unsigned integer body, to the recipient
+ pipe in the standard format (1-byte message indicator, 3-byte
+ message length (excluding length and indicator field), and the
+ unsigned integer, as a string. */
void
-sync_pipe_errmsg_to_parent(int pipe_fd, const char *error_msg,
- const char *secondary_error_msg)
+sync_pipe_write_uint_msg(int pipe_fd, char indicator, unsigned int num)
{
+ char count_str[SP_DECISIZE+1+1];
+
+ snprintf(count_str, sizeof(count_str), "%u", num);
+ sync_pipe_write_string_msg(pipe_fd, indicator, count_str);
+}
- /* first write a "master header" with the length of the two messages plus their "slave headers" */
- pipe_write_header(pipe_fd, SP_ERROR_MSG, (int) (strlen(error_msg) + 1 + 4 + strlen(secondary_error_msg) + 1 + 4));
- pipe_write_block(pipe_fd, SP_ERROR_MSG, error_msg);
- pipe_write_block(pipe_fd, SP_ERROR_MSG, secondary_error_msg);
+/* Write a message, with an integer body, to the recipient pipe in the
+ standard format (1-byte message indicator, 3-byte message length
+ (excluding length and indicator field), and the unsigned integer,
+ as a string. */
+void
+sync_pipe_write_int_msg(int pipe_fd, char indicator, int num)
+{
+ char count_str[SP_DECISIZE+1+1];
+
+ snprintf(count_str, sizeof(count_str), "%d", num);
+ sync_pipe_write_string_msg(pipe_fd, indicator, count_str);
+}
+
+/* Write a message, with a primary and secondary error message as the body,
+ to the recipient pipe. The header is an SP_ERROR_MSG header, with the
+ length being the length of two string submessages; the submessages
+ are the body of the message, with each submessage being a message
+ with an indicator of SP_ERROR_MSG, the first message having the
+ primary error message string and the second message having the secondary
+ error message string. */
+void
+sync_pipe_write_errmsgs_to_parent(int pipe_fd, const char *error_msg,
+ const char *secondary_error_msg)
+{
+ sync_pipe_write_header(pipe_fd, SP_ERROR_MSG,
+ (unsigned int) (strlen(error_msg) + 1 + 4 + strlen(secondary_error_msg) + 1 + 4));
+ sync_pipe_write_string_msg(pipe_fd, SP_ERROR_MSG, error_msg);
+ sync_pipe_write_string_msg(pipe_fd, SP_ERROR_MSG, secondary_error_msg);
}
/*
- * Editor modelines - http://www.wireshark.org/tools/modelines.html
+ * Editor modelines - https://www.wireshark.org/tools/modelines.html
*
* Local variables:
* c-basic-offset: 4