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 #ifndef __MUX_H__ 31 #define __MUX_H__ 32 33 #define MUX_MAXCHANNELS 2 34 #define MUX_IN 0 35 #define MUX_OUT 1 36 37 #define MUX_MIN_MSS 1024 /* 1KB */ 38 #define MUX_DEFAULT_MSS 2048 /* 2KB */ 39 #define MUX_MAX_MSS 4096 /* 4KB */ 40 #define MUX_MAX_MSS_ZLIB (MUX_MAX_MSS * 2) 41 42 #define MUX_MIN_BUFSIZE 8192 /* 8KB */ 43 #define MUX_DEFAULT_BUFSIZE 16384 /* 16KB */ 44 #define MUX_MAX_BUFSIZE 32768 /* 32KB */ 45 46 #define MUX_DIRCMP 0 /* DirScan -> DirCmp */ 47 #define MUX_FILESCAN_IN 0 /* FileScan <- DirCmp */ 48 #define MUX_FILECMP 1 /* FileScan -> FileCmp */ 49 #define MUX_UPDATER_IN 1 /* Updater <- FileCmp */ 50 51 #define MUX_DIRCMP_IN 0 /* DirCmp <- DirScan */ 52 #define MUX_FILESCAN 0 /* DirCmp -> FileScan */ 53 #define MUX_FILECMP_IN 1 /* FileCmp <- FileScan */ 54 #define MUX_UPDATER 1 /* FileCmp -> Updater */ 55 56 #define MUX_CMD_DATA 0x00 57 #define MUX_CMD_RESET 0x01 58 #define MUX_CMD_CLOSE 0x02 59 60 #define MUX_CMDLEN_DATA 4 61 #define MUX_CMDLEN_RESET 6 62 #define MUX_CMDLEN_CLOSE 2 63 #define MUX_MAXCMDLEN 6 /* max(MUX_CMDLEN_{DATA,RESET,CLOSE}) */ 64 65 enum mux_state { 66 MUX_STATE_INIT, 67 MUX_STATE_RUNNING, 68 MUX_STATE_CLOSED, 69 MUX_STATE_ERROR 70 }; 71 72 struct muxbuf { 73 uint8_t *mxb_buffer; 74 uint32_t mxb_bufsize, mxb_length, mxb_head, mxb_rlength; 75 uint16_t mxb_mss, mxb_size; 76 enum mux_state mxb_state; 77 78 pthread_mutex_t mxb_lock; 79 pthread_cond_t mxb_wait_in, mxb_wait_out; 80 }; 81 82 struct mux { 83 int mx_socket; 84 struct muxbuf mx_buffer[2][MUX_MAXCHANNELS]; 85 uint8_t mx_recvcmd[MUX_MAXCMDLEN]; 86 bool mx_state[2][MUX_MAXCHANNELS]; 87 88 pthread_t mx_receiver; 89 pthread_mutex_t mx_lock; 90 pthread_cond_t mx_wait; 91 bool mx_isconnected; 92 93 uint64_t mx_xfer_in, mx_xfer_out; 94 int mx_compress; 95 void *mx_stream; 96 }; 97 98 struct mux *mux_init(int, uint16_t, int, int); 99 void mux_destroy(struct mux *); 100 bool muxbuf_init(struct muxbuf *, uint16_t, uint32_t, int); 101 void muxbuf_destroy(struct muxbuf *); 102 103 bool mux_send(struct mux *, uint8_t, const void *, size_t); 104 bool mux_recv(struct mux *, uint8_t, void *, size_t); 105 bool mux_flush(struct mux *, uint8_t); 106 bool mux_close_in(struct mux *, uint8_t); 107 bool mux_close_out(struct mux *, uint8_t); 108 void mux_abort(struct mux *); 109 110 bool mux_send_raw(struct mux *, uint8_t, const void *, size_t); 111 bool mux_flush_raw(struct mux *, uint8_t); 112 113 bool mux_init_zlib(struct mux *, int); 114 void mux_destroy_zlib(struct mux *); 115 bool mux_send_zlib(struct mux *, uint8_t, const void *, size_t); 116 bool mux_flush_zlib(struct mux *, uint8_t); 117 118 #endif /* __MUX_H__ */ 119