From Albert Chin
[obnox/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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22  */
23
24 #ifdef HAVE_CONFIG_H
25 # include "config.h"
26 #endif
27
28 #include <string.h>
29
30 #ifdef HAVE_UNISTD_H
31 #include <unistd.h>
32 #endif
33
34 #include <glib.h>
35
36 #include "sync_pipe.h"
37 #include "log.h"
38
39 /****************************************************************************************************************/
40 /* sync_pipe handling */
41
42
43 /* write a single message header to the recipient pipe */
44 int
45 pipe_write_header(int pipe, char indicator, int length)
46 {
47     guchar header[1+3]; /* indicator + 3-byte len */
48
49
50     g_assert(length <= SP_MAX_MSG_LEN);
51
52     /* write header (indicator + 3-byte len) */
53     header[0] = indicator;
54     header[1] = (length >> 16) & 0xFF;
55     header[2] = (length >> 8) & 0xFF;
56     header[3] = (length >> 0) & 0xFF;
57
58     /* write header */
59     return write(pipe, header, sizeof header);
60 }
61
62
63 /* write a message to the recipient pipe in the standard format 
64    (3 digit message length (excluding length and indicator field), 
65    1 byte message indicator and the rest is the message).
66    If msg is NULL, the message has only a length and indicator. */
67 void
68 pipe_write_block(int pipe, char indicator, const char *msg)
69 {
70     int ret;
71     size_t len;
72
73     /*g_warning("write %d enter", pipe);*/
74
75     if(msg != NULL) {
76         len = strlen(msg) + 1;    /* including the terminating '\0'! */
77     } else {
78         len = 0;
79     }
80
81     /* write header (indicator + 3-byte len) */
82     ret = pipe_write_header(pipe, indicator, len);
83     if(ret == -1) {
84         return;
85     }
86
87     /* write value (if we have one) */
88     if(len) {
89         /*g_warning("write %d indicator: %c value len: %u msg: %s", pipe, indicator, len, msg);*/
90         ret = write(pipe, msg, len);
91         if(ret == -1) {
92             return;
93         }
94     } else {
95         /*g_warning("write %d indicator: %c no value", pipe, indicator);*/
96     }
97
98     /*g_warning("write %d leave", pipe);*/
99 }
100
101
102 void
103 sync_pipe_errmsg_to_parent(const char *error_msg, 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(1, SP_ERROR_MSG, strlen(error_msg) + 1 + 4 + strlen(secondary_error_msg) + 1 + 4);
108     pipe_write_block(1, SP_ERROR_MSG, error_msg);
109     pipe_write_block(1, SP_ERROR_MSG, secondary_error_msg);
110 }