Fix Wai dissector
[metze/wireshark/wip.git] / sync_pipe_write.c
1 /* sync_pipe_write.c
2  *
3  * Wireshark - Network traffic analyzer
4  * By Gerald Combs <gerald@wireshark.org>
5  * Copyright 1998 Gerald Combs
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20  */
21
22 #include <config.h>
23
24 #include <string.h>
25
26 #ifdef HAVE_UNISTD_H
27 #include <unistd.h>
28 #endif
29
30 #ifdef _WIN32
31 #include <io.h>
32 #endif
33
34 #include <glib.h>
35
36 #include "sync_pipe.h"
37
38 /****************************************************************************************************************/
39 /* sync_pipe handling */
40
41
42 /* write a single message header to the recipient pipe */
43 ssize_t
44 pipe_write_header(int pipe_fd, char indicator, int length)
45 {
46     guchar header[1+3]; /* indicator + 3-byte len */
47
48
49     g_assert(length <= SP_MAX_MSG_LEN);
50
51     /* write header (indicator + 3-byte len) */
52     header[0] = indicator;
53     header[1] = (length >> 16) & 0xFF;
54     header[2] = (length >> 8) & 0xFF;
55     header[3] = (length >> 0) & 0xFF;
56
57     /* write header */
58     return write(pipe_fd, header, sizeof header);
59 }
60
61
62 /* write a message to the recipient pipe in the standard format
63    (3 digit message length (excluding length and indicator field),
64    1 byte message indicator and the rest is the message).
65    If msg is NULL, the message has only a length and indicator. */
66 void
67 pipe_write_block(int pipe_fd, char indicator, const char *msg)
68 {
69     ssize_t ret;
70     int len;
71
72     /*g_warning("write %d enter", pipe_fd);*/
73
74     if(msg != NULL) {
75         len = (int) strlen(msg) + 1;    /* including the terminating '\0'! */
76     } else {
77         len = 0;
78     }
79
80     /* write header (indicator + 3-byte len) */
81     ret = pipe_write_header(pipe_fd, indicator, len);
82     if(ret == -1) {
83         return;
84     }
85
86     /* write value (if we have one) */
87     if(len) {
88         /*g_warning("write %d indicator: %c value len: %u msg: %s", pipe_fd, indicator, len, msg);*/
89         ret = write(pipe_fd, msg, len);
90         if(ret == -1) {
91             return;
92         }
93     } else {
94         /*g_warning("write %d indicator: %c no value", pipe_fd, indicator);*/
95     }
96
97     /*g_warning("write %d leave", pipe_fd);*/
98 }
99
100
101 void
102 sync_pipe_errmsg_to_parent(int pipe_fd, const char *error_msg,
103                            const char *secondary_error_msg)
104 {
105
106     /* first write a "master header" with the length of the two messages plus their "slave headers" */
107     pipe_write_header(pipe_fd, SP_ERROR_MSG, (int) (strlen(error_msg) + 1 + 4 + strlen(secondary_error_msg) + 1 + 4));
108     pipe_write_block(pipe_fd, SP_ERROR_MSG, error_msg);
109     pipe_write_block(pipe_fd, SP_ERROR_MSG, secondary_error_msg);
110 }
111
112 /*
113  * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
114  *
115  * Local variables:
116  * c-basic-offset: 4
117  * tab-width: 8
118  * indent-tabs-mode: nil
119  * End:
120  *
121  * vi: set shiftwidth=4 tabstop=8 expandtab:
122  * :indentSize=4:tabSize=8:noTabs=true:
123  */