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 #include <sys/uio.h>
33 
34 #include <errno.h>
35 #include <pthread.h>
36 #include <string.h>
37 
38 #include "compat_stdbool.h"
39 #include "compat_stdint.h"
40 #include "compat_inttypes.h"
41 #include "basedef.h"
42 
43 #include "logmsg.h"
44 #include "mux.h"
45 #include "network.h"
46 
47 #include "receiver.h"
48 
49 bool
receiver_data_raw(struct mux * mx,uint8_t chnum)50 receiver_data_raw(struct mux *mx, uint8_t chnum)
51 {
52 	struct muxbuf *mxb = &mx->mx_buffer[MUX_IN][chnum];
53 	uint16_t mss;
54 	uint8_t *cmd = mx->mx_recvcmd;
55 	size_t len1, len2, tail;
56 	int err;
57 
58 	if (!sock_recv(mx->mx_socket, cmd, MUX_CMDLEN_DATA - 2)) {
59 		logmsg_err("Receiver(DATA) Error: recv");
60 		return (false);
61 	}
62 
63 	mss = GetWord(cmd);
64 	if ((mss == 0) || (mss > mxb->mxb_mss)) {
65 		logmsg_err("Receiver(DATA) Error: invalid length: %u", mss);
66 		return (false);
67 	}
68 
69 	if ((err = pthread_mutex_lock(&mxb->mxb_lock)) != 0) {
70 		logmsg_err("Receiver(DATA) Error: mutex lock: %s",
71 			   strerror(err));
72 		return (false);
73 	}
74 	if (mxb->mxb_state != MUX_STATE_RUNNING) {
75 		logmsg_err("Receiver(DATA) Error: not running: %u", chnum);
76 		mxb->mxb_state = MUX_STATE_ERROR;
77 		pthread_mutex_unlock(&mxb->mxb_lock);
78 		return (false);
79 	}
80 
81 	while (mxb->mxb_length + mss > mxb->mxb_bufsize) {
82 		logmsg_debug(DEBUG_BASE, "Receiver: Sleep(%u): %u + %u > %u",
83 			     chnum, mxb->mxb_length, mss, mxb->mxb_bufsize);
84 		if ((err = pthread_cond_wait(&mxb->mxb_wait_in,
85 					     &mxb->mxb_lock)) != 0) {
86 			logmsg_err("Receiver(DATA) Error: cond wait: %s",
87 				   strerror(err));
88 			mxb->mxb_state = MUX_STATE_ERROR;
89 			pthread_mutex_unlock(&mxb->mxb_lock);
90 			return (false);
91 		}
92 		logmsg_debug(DEBUG_BASE, "Receiver: Wakeup(%u): %u, %u, %u",
93 			     chnum, mxb->mxb_length, mss, mxb->mxb_bufsize);
94 		if (mxb->mxb_state != MUX_STATE_RUNNING) {
95 			logmsg_err("Receiver(DATA) Error: not running: %u",
96 				   chnum);
97 			mxb->mxb_state = MUX_STATE_ERROR;
98 			pthread_mutex_unlock(&mxb->mxb_lock);
99 			return (false);
100 		}
101 	}
102 
103 	if ((tail = mxb->mxb_head + mxb->mxb_length) >= mxb->mxb_bufsize)
104 		tail -= mxb->mxb_bufsize;
105 	if ((len1 = tail + mss) > mxb->mxb_bufsize)
106 		len2 = len1 - mxb->mxb_bufsize;
107 	else
108 		len2 = 0;
109 	len1 = mss - len2;
110 
111 	if (!sock_recv(mx->mx_socket, &mxb->mxb_buffer[tail], len1)) {
112 		logmsg_err("Receiver(DATA) Error: recv data");
113 		mxb->mxb_state = MUX_STATE_ERROR;
114 		pthread_mutex_unlock(&mxb->mxb_lock);
115 		return (false);
116 	}
117 	if (len2 > 0) {
118 		if (!sock_recv(mx->mx_socket, mxb->mxb_buffer, len2)) {
119 			logmsg_err("Receiver(DATA) Error: recv data");
120 			mxb->mxb_state = MUX_STATE_ERROR;
121 			pthread_mutex_unlock(&mxb->mxb_lock);
122 			return (false);
123 		}
124 	}
125 
126 	mx->mx_xfer_in += mss;
127 	mxb->mxb_length += mss;
128 
129 	if ((err = pthread_cond_signal(&mxb->mxb_wait_out)) != 0) {
130 		logmsg_err("Receiver(DATA) Error: cond signal: %s",
131 			   strerror(err));
132 		mxb->mxb_state = MUX_STATE_ERROR;
133 		pthread_mutex_unlock(&mxb->mxb_lock);
134 		return (false);
135 	}
136 
137 	if ((err = pthread_mutex_unlock(&mxb->mxb_lock)) != 0) {
138 		logmsg_err("Receiver(DATA) Error: mutex unlock: %s",
139 			   strerror(err));
140 		return(false);
141 	}
142 
143 	return (true);
144 }
145