3 * Wireshark - Network traffic analyzer
4 * By Gerald Combs <gerald@wireshark.org>
5 * Copyright 1998 Gerald Combs
7 * SPDX-License-Identifier: GPL-2.0-or-later
16 #include <wsutil/file_util.h>
17 #include <wsutil/ws_assert.h>
19 #include "sync_pipe.h"
21 /****************************************************************************************************************/
22 /* sync_pipe handling */
25 /* write a single message header to the recipient pipe */
27 sync_pipe_write_header(int pipe_fd
, char indicator
, unsigned int length
)
29 unsigned char header
[1+3]; /* indicator + 3-byte len */
31 ws_assert(length
<= SP_MAX_MSG_LEN
);
33 /* write header (indicator + 3-byte len) */
34 header
[0] = indicator
;
35 header
[1] = (length
>> 16) & 0xFF;
36 header
[2] = (length
>> 8) & 0xFF;
37 header
[3] = (length
>> 0) & 0xFF;
40 return ws_write(pipe_fd
, header
, sizeof header
);
44 /* Write a message, with a string body, to the recipient pipe in the
45 standard format (1-byte message indicator, 3-byte message length
46 (excluding length and indicator field), and the string.
47 If msg is NULL, the message has only a length and indicator. */
49 sync_pipe_write_string_msg(int pipe_fd
, char indicator
, const char *msg
)
54 /*ws_warning("write %d enter", pipe_fd);*/
57 len
= (int) strlen(msg
) + 1; /* including the terminating '\0'! */
62 /* write header (indicator + 3-byte len) */
63 ret
= sync_pipe_write_header(pipe_fd
, indicator
, len
);
68 /* write value (if we have one) */
70 /*ws_warning("write %d indicator: %c value len: %u msg: %s", pipe_fd, indicator, len, msg);*/
71 ret
= ws_write(pipe_fd
, msg
, len
);
76 /*ws_warning("write %d indicator: %c no value", pipe_fd, indicator);*/
79 /*ws_warning("write %d leave", pipe_fd);*/
83 /* Size of buffer to hold decimal representation of
84 signed/unsigned 64-bit int */
85 #define SP_DECISIZE 20
87 /* Write a message, with an unsigned integer body, to the recipient
88 pipe in the standard format (1-byte message indicator, 3-byte
89 message length (excluding length and indicator field), and the
90 unsigned integer, as a string. */
92 sync_pipe_write_uint_msg(int pipe_fd
, char indicator
, unsigned int num
)
94 char count_str
[SP_DECISIZE
+1+1];
96 snprintf(count_str
, sizeof(count_str
), "%u", num
);
97 sync_pipe_write_string_msg(pipe_fd
, indicator
, count_str
);
100 /* Write a message, with an integer body, to the recipient pipe in the
101 standard format (1-byte message indicator, 3-byte message length
102 (excluding length and indicator field), and the unsigned integer,
105 sync_pipe_write_int_msg(int pipe_fd
, char indicator
, int num
)
107 char count_str
[SP_DECISIZE
+1+1];
109 snprintf(count_str
, sizeof(count_str
), "%d", num
);
110 sync_pipe_write_string_msg(pipe_fd
, indicator
, count_str
);
113 /* Write a message, with a primary and secondary error message as the body,
114 to the recipient pipe. The header is an SP_ERROR_MSG header, with the
115 length being the length of two string submessages; the submessages
116 are the body of the message, with each submessage being a message
117 with an indicator of SP_ERROR_MSG, the first message having the
118 primary error message string and the second message having the secondary
119 error message string. */
121 sync_pipe_write_errmsgs_to_parent(int pipe_fd
, const char *error_msg
,
122 const char *secondary_error_msg
)
124 sync_pipe_write_header(pipe_fd
, SP_ERROR_MSG
,
125 (unsigned int) (strlen(error_msg
) + 1 + 4 + strlen(secondary_error_msg
) + 1 + 4));
126 sync_pipe_write_string_msg(pipe_fd
, SP_ERROR_MSG
, error_msg
);
127 sync_pipe_write_string_msg(pipe_fd
, SP_ERROR_MSG
, secondary_error_msg
);
131 * Editor modelines - https://www.wireshark.org/tools/modelines.html
136 * indent-tabs-mode: nil
139 * vi: set shiftwidth=4 tabstop=8 expandtab:
140 * :indentSize=4:tabSize=8:noTabs=true: