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