1 /*-
2  * Copyright (c) 2000-2005 MAEKAWA Masahide <maekawa@cvsync.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the author nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 
30 #include <sys/types.h>
31 #include <sys/socket.h>
32 
33 #include <pthread.h>
34 
35 #include "compat_stdbool.h"
36 #include "compat_stdint.h"
37 #include "compat_inttypes.h"
38 #include "basedef.h"
39 
40 #include "logmsg.h"
41 #include "mux.h"
42 #include "network.h"
43 
44 bool
mux_send_raw(struct mux * mx,uint8_t chnum,const void * buffer,size_t bufsize)45 mux_send_raw(struct mux *mx, uint8_t chnum, const void *buffer, size_t bufsize)
46 {
47 	struct muxbuf *mxb = &mx->mx_buffer[MUX_OUT][chnum];
48 	uint8_t cmd[MUX_CMDLEN_DATA];
49 
50 	cmd[0] = MUX_CMD_DATA;
51 	cmd[1] = chnum;
52 	SetWord(&cmd[2], mxb->mxb_length + bufsize);
53 
54 	if (!sock_send(mx->mx_socket, cmd, MUX_CMDLEN_DATA)) {
55 		logmsg_err("Mux(SEND) Error: send");
56 		return (false);
57 	}
58 	if (!sock_send(mx->mx_socket, mxb->mxb_buffer, mxb->mxb_length)) {
59 		logmsg_err("Mux(SEND) Error: send");
60 		return (false);
61 	}
62 	mx->mx_xfer_out += mxb->mxb_length;
63 	if (!sock_send(mx->mx_socket, buffer, bufsize)) {
64 		logmsg_err("Mux(SEND) Error: send");
65 		return (false);
66 	}
67 	mx->mx_xfer_out += bufsize;
68 
69 	return (true);
70 }
71 
72 bool
mux_flush_raw(struct mux * mx,uint8_t chnum)73 mux_flush_raw(struct mux *mx, uint8_t chnum)
74 {
75 	struct muxbuf *mxb = &mx->mx_buffer[MUX_OUT][chnum];
76 	uint8_t cmd[MUX_CMDLEN_DATA];
77 
78 	cmd[0] = MUX_CMD_DATA;
79 	cmd[1] = chnum;
80 	SetWord(&cmd[2], mxb->mxb_length);
81 
82 	if (!sock_send(mx->mx_socket, cmd, MUX_CMDLEN_DATA)) {
83 		logmsg_err("Mux(FLUSH) Error: send");
84 		return (false);
85 	}
86 	if (!sock_send(mx->mx_socket, mxb->mxb_buffer, mxb->mxb_length)) {
87 		logmsg_err("Mux(FLUSH) Error: send");
88 		return (false);
89 	}
90 	mx->mx_xfer_out += mxb->mxb_length;
91 
92 	return (true);
93 }
94