Revision e1c5a2b3 slirp/socket.c

b/slirp/socket.c
5 5
 * terms and conditions of the copyright.
6 6
 */
7 7

  
8
#include "qemu-common.h"
8 9
#define WANT_SYS_IOCTL_H
9 10
#include <slirp.h>
10 11
#include "ip_icmp.h"
11 12
#ifdef __sun__
12 13
#include <sys/filio.h>
13 14
#endif
14
#include "qemu-common.h"
15 15

  
16 16
static void sofcantrcvmore(struct socket *so);
17 17
static void sofcantsendmore(struct socket *so);
......
91 91
  free(so);
92 92
}
93 93

  
94
/*
95
 * Read from so's socket into sb_snd, updating all relevant sbuf fields
96
 * NOTE: This will only be called if it is select()ed for reading, so
97
 * a read() of 0 (or less) means it's disconnected
98
 */
99
int
100
soread(so)
101
	struct socket *so;
94
size_t sopreprbuf(struct socket *so, struct iovec *iov, int *np)
102 95
{
103
	int n, nn, lss, total;
96
	int n, lss, total;
104 97
	struct sbuf *sb = &so->so_snd;
105 98
	int len = sb->sb_datalen - sb->sb_cc;
106
	struct iovec iov[2];
107 99
	int mss = so->so_tcpcb->t_maxseg;
108 100

  
109
	DEBUG_CALL("soread");
101
	DEBUG_CALL("sopreprbuf");
110 102
	DEBUG_ARG("so = %lx", (long )so);
111 103

  
112
	/*
113
	 * No need to check if there's enough room to read.
114
	 * soread wouldn't have been called if there weren't
115
	 */
116

  
117 104
	len = sb->sb_datalen - sb->sb_cc;
118 105

  
106
	if (len <= 0)
107
		return 0;
108

  
119 109
	iov[0].iov_base = sb->sb_wptr;
120 110
        iov[1].iov_base = NULL;
121 111
        iov[1].iov_len = 0;
......
156 146
			n = 1;
157 147
		}
158 148
	}
149
	if (np)
150
		*np = n;
151

  
152
	return iov[0].iov_len + (n - 1) * iov[1].iov_len;
153
}
154

  
155
/*
156
 * Read from so's socket into sb_snd, updating all relevant sbuf fields
157
 * NOTE: This will only be called if it is select()ed for reading, so
158
 * a read() of 0 (or less) means it's disconnected
159
 */
160
int
161
soread(so)
162
	struct socket *so;
163
{
164
	int n, nn;
165
	struct sbuf *sb = &so->so_snd;
166
	struct iovec iov[2];
167

  
168
	DEBUG_CALL("soread");
169
	DEBUG_ARG("so = %lx", (long )so);
170

  
171
	/*
172
	 * No need to check if there's enough room to read.
173
	 * soread wouldn't have been called if there weren't
174
	 */
175
	sopreprbuf(so, iov, &n);
159 176

  
160 177
#ifdef HAVE_READV
161 178
	nn = readv(so->s, (struct iovec *)iov, n);
......
202 219
	return nn;
203 220
}
204 221

  
222
int soreadbuf(struct socket *so, const char *buf, int size)
223
{
224
    int n, nn, copy = size;
225
	struct sbuf *sb = &so->so_snd;
226
	struct iovec iov[2];
227

  
228
	DEBUG_CALL("soreadbuf");
229
	DEBUG_ARG("so = %lx", (long )so);
230

  
231
	/*
232
	 * No need to check if there's enough room to read.
233
	 * soread wouldn't have been called if there weren't
234
	 */
235
	if (sopreprbuf(so, iov, &n) < size)
236
        goto err;
237

  
238
    nn = MIN(iov[0].iov_len, copy);
239
    memcpy(iov[0].iov_base, buf, nn);
240

  
241
    copy -= nn;
242
    buf += nn;
243

  
244
    if (copy == 0)
245
        goto done;
246

  
247
    memcpy(iov[1].iov_base, buf, copy);
248

  
249
done:
250
    /* Update fields */
251
	sb->sb_cc += size;
252
	sb->sb_wptr += size;
253
	if (sb->sb_wptr >= (sb->sb_data + sb->sb_datalen))
254
		sb->sb_wptr -= sb->sb_datalen;
255
    return size;
256
err:
257

  
258
    sofcantrcvmore(so);
259
    tcp_sockclosed(sototcpcb(so));
260
    fprintf(stderr, "soreadbuf buffer to small");
261
    return -1;
262
}
263

  
205 264
/*
206 265
 * Get urgent data
207 266
 *
......
255 314

  
256 315
	if (sb->sb_rptr < sb->sb_wptr) {
257 316
		/* We can send it directly */
258
		n = send(so->s, sb->sb_rptr, so->so_urgc, (MSG_OOB)); /* |MSG_DONTWAIT)); */
317
		n = slirp_send(so, sb->sb_rptr, so->so_urgc, (MSG_OOB)); /* |MSG_DONTWAIT)); */
259 318
		so->so_urgc -= n;
260 319

  
261 320
		DEBUG_MISC((dfd, " --- sent %d bytes urgent data, %d urgent bytes left\n", n, so->so_urgc));
......
276 335
			so->so_urgc -= n;
277 336
			len += n;
278 337
		}
279
		n = send(so->s, buff, len, (MSG_OOB)); /* |MSG_DONTWAIT)); */
338
		n = slirp_send(so, buff, len, (MSG_OOB)); /* |MSG_DONTWAIT)); */
280 339
#ifdef DEBUG
281 340
		if (n != len)
282 341
		   DEBUG_ERROR((dfd, "Didn't send all data urgently XXXXX\n"));
......
348 407

  
349 408
	DEBUG_MISC((dfd, "  ... wrote nn = %d bytes\n", nn));
350 409
#else
351
	nn = send(so->s, iov[0].iov_base, iov[0].iov_len,0);
410
	nn = slirp_send(so, iov[0].iov_base, iov[0].iov_len,0);
352 411
#endif
353 412
	/* This should never happen, but people tell me it does *shrug* */
354 413
	if (nn < 0 && (errno == EAGAIN || errno == EINTR))
......
365 424
#ifndef HAVE_READV
366 425
	if (n == 2 && nn == iov[0].iov_len) {
367 426
            int ret;
368
            ret = send(so->s, iov[1].iov_base, iov[1].iov_len,0);
427
            ret = slirp_send(so, iov[1].iov_base, iov[1].iov_len,0);
369 428
            if (ret > 0)
370 429
                nn += ret;
371 430
        }

Also available in: Unified diff