socket.c 17.3 KB
Newer Older
B
bellard 已提交
1 2
/*
 * Copyright (c) 1995 Danny Gasparovski.
3 4
 *
 * Please read the file COPYRIGHT for the
B
bellard 已提交
5 6 7
 * terms and conditions of the copyright.
 */

8
#include "qemu-common.h"
B
bellard 已提交
9 10
#include <slirp.h>
#include "ip_icmp.h"
B
bellard 已提交
11 12 13
#ifdef __sun__
#include <sys/filio.h>
#endif
B
bellard 已提交
14

15 16 17
static void sofcantrcvmore(struct socket *so);
static void sofcantsendmore(struct socket *so);

B
bellard 已提交
18
struct socket *
19 20
solookup(struct socket *head, struct in_addr laddr, u_int lport,
         struct in_addr faddr, u_int fport)
B
bellard 已提交
21 22
{
	struct socket *so;
23

B
bellard 已提交
24
	for (so = head->so_next; so != head; so = so->so_next) {
25
		if (so->so_lport == lport &&
B
bellard 已提交
26 27 28 29 30
		    so->so_laddr.s_addr == laddr.s_addr &&
		    so->so_faddr.s_addr == faddr.s_addr &&
		    so->so_fport == fport)
		   break;
	}
31

B
bellard 已提交
32 33 34
	if (so == head)
	   return (struct socket *)NULL;
	return so;
35

B
bellard 已提交
36 37 38 39 40 41 42 43
}

/*
 * Create a new socket, initialise the fields
 * It is the responsibility of the caller to
 * insque() it into the correct linked-list
 */
struct socket *
44
socreate(Slirp *slirp)
B
bellard 已提交
45 46
{
  struct socket *so;
47

B
bellard 已提交
48 49 50 51 52
  so = (struct socket *)malloc(sizeof(struct socket));
  if(so) {
    memset(so, 0, sizeof(struct socket));
    so->so_state = SS_NOFDREF;
    so->s = -1;
53
    so->slirp = slirp;
B
bellard 已提交
54 55 56 57 58 59 60 61
  }
  return(so);
}

/*
 * remque and free a socket, clobber cache
 */
void
62
sofree(struct socket *so)
B
bellard 已提交
63
{
64 65
  Slirp *slirp = so->slirp;

B
bellard 已提交
66 67 68 69
  if (so->so_emu==EMU_RSH && so->extra) {
	sofree(so->extra);
	so->extra=NULL;
  }
70 71 72 73
  if (so == slirp->tcp_last_so) {
      slirp->tcp_last_so = &slirp->tcb;
  } else if (so == slirp->udp_last_so) {
      slirp->udp_last_so = &slirp->udb;
74 75
  } else if (so == slirp->icmp_last_so) {
      slirp->icmp_last_so = &slirp->icmp;
76
  }
B
bellard 已提交
77
  m_free(so->so_m);
78 79

  if(so->so_next && so->so_prev)
B
bellard 已提交
80 81 82 83 84
    remque(so);  /* crashes if so is not in a queue */

  free(so);
}

85
size_t sopreprbuf(struct socket *so, struct iovec *iov, int *np)
B
bellard 已提交
86
{
87
	int n, lss, total;
B
bellard 已提交
88 89 90
	struct sbuf *sb = &so->so_snd;
	int len = sb->sb_datalen - sb->sb_cc;
	int mss = so->so_tcpcb->t_maxseg;
91

92
	DEBUG_CALL("sopreprbuf");
B
bellard 已提交
93
	DEBUG_ARG("so = %lx", (long )so);
94

95 96 97
	if (len <= 0)
		return 0;

B
bellard 已提交
98
	iov[0].iov_base = sb->sb_wptr;
99 100
        iov[1].iov_base = NULL;
        iov[1].iov_len = 0;
B
bellard 已提交
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
	if (sb->sb_wptr < sb->sb_rptr) {
		iov[0].iov_len = sb->sb_rptr - sb->sb_wptr;
		/* Should never succeed, but... */
		if (iov[0].iov_len > len)
		   iov[0].iov_len = len;
		if (iov[0].iov_len > mss)
		   iov[0].iov_len -= iov[0].iov_len%mss;
		n = 1;
	} else {
		iov[0].iov_len = (sb->sb_data + sb->sb_datalen) - sb->sb_wptr;
		/* Should never succeed, but... */
		if (iov[0].iov_len > len) iov[0].iov_len = len;
		len -= iov[0].iov_len;
		if (len) {
			iov[1].iov_base = sb->sb_data;
			iov[1].iov_len = sb->sb_rptr - sb->sb_data;
			if(iov[1].iov_len > len)
			   iov[1].iov_len = len;
			total = iov[0].iov_len + iov[1].iov_len;
			if (total > mss) {
				lss = total%mss;
				if (iov[1].iov_len > lss) {
					iov[1].iov_len -= lss;
					n = 2;
				} else {
					lss -= iov[1].iov_len;
					iov[0].iov_len -= lss;
					n = 1;
				}
			} else
				n = 2;
		} else {
			if (iov[0].iov_len > mss)
			   iov[0].iov_len -= iov[0].iov_len%mss;
			n = 1;
		}
	}
138 139 140 141 142 143 144 145 146 147 148 149
	if (np)
		*np = n;

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

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

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

	/*
	 * No need to check if there's enough room to read.
	 * soread wouldn't have been called if there weren't
	 */
	sopreprbuf(so, iov, &n);
164

B
bellard 已提交
165 166 167 168
#ifdef HAVE_READV
	nn = readv(so->s, (struct iovec *)iov, n);
	DEBUG_MISC((dfd, " ... read nn = %d bytes\n", nn));
#else
B
Blue Swirl 已提交
169
	nn = qemu_recv(so->s, iov[0].iov_base, iov[0].iov_len,0);
170
#endif
B
bellard 已提交
171 172 173 174 175 176 177 178 179 180
	if (nn <= 0) {
		if (nn < 0 && (errno == EINTR || errno == EAGAIN))
			return 0;
		else {
			DEBUG_MISC((dfd, " --- soread() disconnected, nn = %d, errno = %d-%s\n", nn, errno,strerror(errno)));
			sofcantrcvmore(so);
			tcp_sockclosed(sototcpcb(so));
			return -1;
		}
	}
181

B
bellard 已提交
182 183 184 185 186 187 188 189 190 191
#ifndef HAVE_READV
	/*
	 * If there was no error, try and read the second time round
	 * We read again if n = 2 (ie, there's another part of the buffer)
	 * and we read as much as we could in the first read
	 * We don't test for <= 0 this time, because there legitimately
	 * might not be any more data (since the socket is non-blocking),
	 * a close will be detected on next iteration.
	 * A return of -1 wont (shouldn't) happen, since it didn't happen above
	 */
B
bellard 已提交
192 193
	if (n == 2 && nn == iov[0].iov_len) {
            int ret;
B
Blue Swirl 已提交
194
            ret = qemu_recv(so->s, iov[1].iov_base, iov[1].iov_len,0);
B
bellard 已提交
195 196 197
            if (ret > 0)
                nn += ret;
        }
198

B
bellard 已提交
199 200
	DEBUG_MISC((dfd, " ... read nn = %d bytes\n", nn));
#endif
201

B
bellard 已提交
202 203 204 205 206 207 208
	/* Update fields */
	sb->sb_cc += nn;
	sb->sb_wptr += nn;
	if (sb->sb_wptr >= (sb->sb_data + sb->sb_datalen))
		sb->sb_wptr -= sb->sb_datalen;
	return nn;
}
209

210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
int soreadbuf(struct socket *so, const char *buf, int size)
{
    int n, nn, copy = size;
	struct sbuf *sb = &so->so_snd;
	struct iovec iov[2];

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

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

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

    copy -= nn;
    buf += nn;

    if (copy == 0)
        goto done;

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

done:
    /* Update fields */
	sb->sb_cc += size;
	sb->sb_wptr += size;
	if (sb->sb_wptr >= (sb->sb_data + sb->sb_datalen))
		sb->sb_wptr -= sb->sb_datalen;
    return size;
err:

    sofcantrcvmore(so);
    tcp_sockclosed(sototcpcb(so));
    fprintf(stderr, "soreadbuf buffer to small");
    return -1;
}

B
bellard 已提交
252 253
/*
 * Get urgent data
254
 *
B
bellard 已提交
255 256 257 258 259
 * When the socket is created, we set it SO_OOBINLINE,
 * so when OOB data arrives, we soread() it and everything
 * in the send buffer is sent as urgent data
 */
void
260
sorecvoob(struct socket *so)
B
bellard 已提交
261 262 263 264 265
{
	struct tcpcb *tp = sototcpcb(so);

	DEBUG_CALL("sorecvoob");
	DEBUG_ARG("so = %lx", (long)so);
266

B
bellard 已提交
267 268 269 270 271
	/*
	 * We take a guess at how much urgent data has arrived.
	 * In most situations, when urgent data arrives, the next
	 * read() should get all the urgent data.  This guess will
	 * be wrong however if more data arrives just after the
272
	 * urgent data, or the read() doesn't return all the
B
bellard 已提交
273 274 275 276 277 278 279 280 281 282 283 284 285 286
	 * urgent data.
	 */
	soread(so);
	tp->snd_up = tp->snd_una + so->so_snd.sb_cc;
	tp->t_force = 1;
	tcp_output(tp);
	tp->t_force = 0;
}

/*
 * Send urgent data
 * There's a lot duplicated code here, but...
 */
int
287
sosendoob(struct socket *so)
B
bellard 已提交
288 289 290
{
	struct sbuf *sb = &so->so_rcv;
	char buff[2048]; /* XXX Shouldn't be sending more oob data than this */
291

B
bellard 已提交
292
	int n, len;
293

B
bellard 已提交
294 295 296
	DEBUG_CALL("sosendoob");
	DEBUG_ARG("so = %lx", (long)so);
	DEBUG_ARG("sb->sb_cc = %d", sb->sb_cc);
297

B
bellard 已提交
298 299
	if (so->so_urgc > 2048)
	   so->so_urgc = 2048; /* XXXX */
300

B
bellard 已提交
301 302
	if (sb->sb_rptr < sb->sb_wptr) {
		/* We can send it directly */
303
		n = slirp_send(so, sb->sb_rptr, so->so_urgc, (MSG_OOB)); /* |MSG_DONTWAIT)); */
B
bellard 已提交
304
		so->so_urgc -= n;
305

B
bellard 已提交
306 307
		DEBUG_MISC((dfd, " --- sent %d bytes urgent data, %d urgent bytes left\n", n, so->so_urgc));
	} else {
308
		/*
B
bellard 已提交
309 310 311 312 313 314 315 316 317 318 319 320 321 322 323
		 * Since there's no sendv or sendtov like writev,
		 * we must copy all data to a linear buffer then
		 * send it all
		 */
		len = (sb->sb_data + sb->sb_datalen) - sb->sb_rptr;
		if (len > so->so_urgc) len = so->so_urgc;
		memcpy(buff, sb->sb_rptr, len);
		so->so_urgc -= len;
		if (so->so_urgc) {
			n = sb->sb_wptr - sb->sb_data;
			if (n > so->so_urgc) n = so->so_urgc;
			memcpy((buff + len), sb->sb_data, n);
			so->so_urgc -= n;
			len += n;
		}
324
		n = slirp_send(so, buff, len, (MSG_OOB)); /* |MSG_DONTWAIT)); */
B
bellard 已提交
325 326 327
#ifdef DEBUG
		if (n != len)
		   DEBUG_ERROR((dfd, "Didn't send all data urgently XXXXX\n"));
328
#endif
B
bellard 已提交
329 330
		DEBUG_MISC((dfd, " ---2 sent %d bytes urgent data, %d urgent bytes left\n", n, so->so_urgc));
	}
331

B
bellard 已提交
332 333 334 335
	sb->sb_cc -= n;
	sb->sb_rptr += n;
	if (sb->sb_rptr >= (sb->sb_data + sb->sb_datalen))
		sb->sb_rptr -= sb->sb_datalen;
336

B
bellard 已提交
337 338 339 340
	return n;
}

/*
341
 * Write data from so_rcv to so's socket,
B
bellard 已提交
342 343 344
 * updating all sbuf field as necessary
 */
int
345
sowrite(struct socket *so)
B
bellard 已提交
346 347 348 349 350
{
	int  n,nn;
	struct sbuf *sb = &so->so_rcv;
	int len = sb->sb_cc;
	struct iovec iov[2];
351

B
bellard 已提交
352 353
	DEBUG_CALL("sowrite");
	DEBUG_ARG("so = %lx", (long)so);
354

B
bellard 已提交
355 356 357 358 359 360 361 362 363 364
	if (so->so_urgc) {
		sosendoob(so);
		if (sb->sb_cc == 0)
			return 0;
	}

	/*
	 * No need to check if there's something to write,
	 * sowrite wouldn't have been called otherwise
	 */
365

B
bellard 已提交
366
	iov[0].iov_base = sb->sb_rptr;
367 368
        iov[1].iov_base = NULL;
        iov[1].iov_len = 0;
B
bellard 已提交
369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389
	if (sb->sb_rptr < sb->sb_wptr) {
		iov[0].iov_len = sb->sb_wptr - sb->sb_rptr;
		/* Should never succeed, but... */
		if (iov[0].iov_len > len) iov[0].iov_len = len;
		n = 1;
	} else {
		iov[0].iov_len = (sb->sb_data + sb->sb_datalen) - sb->sb_rptr;
		if (iov[0].iov_len > len) iov[0].iov_len = len;
		len -= iov[0].iov_len;
		if (len) {
			iov[1].iov_base = sb->sb_data;
			iov[1].iov_len = sb->sb_wptr - sb->sb_data;
			if (iov[1].iov_len > len) iov[1].iov_len = len;
			n = 2;
		} else
			n = 1;
	}
	/* Check if there's urgent data to send, and if so, send it */

#ifdef HAVE_READV
	nn = writev(so->s, (const struct iovec *)iov, n);
390

B
bellard 已提交
391 392
	DEBUG_MISC((dfd, "  ... wrote nn = %d bytes\n", nn));
#else
393
	nn = slirp_send(so, iov[0].iov_base, iov[0].iov_len,0);
B
bellard 已提交
394 395 396 397
#endif
	/* This should never happen, but people tell me it does *shrug* */
	if (nn < 0 && (errno == EAGAIN || errno == EINTR))
		return 0;
398

B
bellard 已提交
399 400 401 402 403 404 405
	if (nn <= 0) {
		DEBUG_MISC((dfd, " --- sowrite disconnected, so->so_state = %x, errno = %d\n",
			so->so_state, errno));
		sofcantsendmore(so);
		tcp_sockclosed(sototcpcb(so));
		return -1;
	}
406

B
bellard 已提交
407
#ifndef HAVE_READV
B
bellard 已提交
408 409
	if (n == 2 && nn == iov[0].iov_len) {
            int ret;
410
            ret = slirp_send(so, iov[1].iov_base, iov[1].iov_len,0);
B
bellard 已提交
411 412 413
            if (ret > 0)
                nn += ret;
        }
B
bellard 已提交
414 415
        DEBUG_MISC((dfd, "  ... wrote nn = %d bytes\n", nn));
#endif
416

B
bellard 已提交
417 418 419 420 421
	/* Update sbuf */
	sb->sb_cc -= nn;
	sb->sb_rptr += nn;
	if (sb->sb_rptr >= (sb->sb_data + sb->sb_datalen))
		sb->sb_rptr -= sb->sb_datalen;
422

B
bellard 已提交
423 424 425 426 427 428
	/*
	 * If in DRAIN mode, and there's no more data, set
	 * it CANTSENDMORE
	 */
	if ((so->so_state & SS_FWDRAIN) && sb->sb_cc == 0)
		sofcantsendmore(so);
429

B
bellard 已提交
430 431 432 433 434 435 436
	return nn;
}

/*
 * recvfrom() a UDP socket
 */
void
437
sorecvfrom(struct socket *so)
B
bellard 已提交
438 439
{
	struct sockaddr_in addr;
440
	socklen_t addrlen = sizeof(struct sockaddr_in);
441

B
bellard 已提交
442 443
	DEBUG_CALL("sorecvfrom");
	DEBUG_ARG("so = %lx", (long)so);
444

B
bellard 已提交
445 446 447
	if (so->so_type == IPPROTO_ICMP) {   /* This is a "ping" reply */
	  char buff[256];
	  int len;
448

449
	  len = recvfrom(so->s, buff, 256, 0,
B
bellard 已提交
450 451
			 (struct sockaddr *)&addr, &addrlen);
	  /* XXX Check if reply is "correct"? */
452

B
bellard 已提交
453 454 455 456 457
	  if(len == -1 || len == 0) {
	    u_char code=ICMP_UNREACH_PORT;

	    if(errno == EHOSTUNREACH) code=ICMP_UNREACH_HOST;
	    else if(errno == ENETUNREACH) code=ICMP_UNREACH_NET;
458

B
bellard 已提交
459 460 461 462 463
	    DEBUG_MISC((dfd," udp icmp rx errno = %d-%s\n",
			errno,strerror(errno)));
	    icmp_error(so->so_m, ICMP_UNREACH,code, 0,strerror(errno));
	  } else {
	    icmp_reflect(so->so_m);
464
            so->so_m = NULL; /* Don't m_free() it again! */
B
bellard 已提交
465 466 467 468 469
	  }
	  /* No need for this socket anymore, udp_detach it */
	  udp_detach(so);
	} else {                            	/* A "normal" UDP packet */
	  struct mbuf *m;
B
Blue Swirl 已提交
470 471 472 473 474 475
          int len;
#ifdef _WIN32
          unsigned long n;
#else
          int n;
#endif
B
bellard 已提交
476

477 478 479 480
	  m = m_get(so->slirp);
	  if (!m) {
	      return;
	  }
481
	  m->m_data += IF_MAXLINKHDR;
482

483
	  /*
B
bellard 已提交
484 485 486 487 488
	   * XXX Shouldn't FIONREAD packets destined for port 53,
	   * but I don't know the max packet size for DNS lookups
	   */
	  len = M_FREEROOM(m);
	  /* if (so->so_fport != htons(53)) { */
B
bellard 已提交
489
	  ioctlsocket(so->s, FIONREAD, &n);
490

B
bellard 已提交
491 492 493 494 495 496
	  if (n > len) {
	    n = (m->m_data - m->m_dat) + m->m_len + n + 1;
	    m_inc(m, n);
	    len = M_FREEROOM(m);
	  }
	  /* } */
497

B
bellard 已提交
498 499
	  m->m_len = recvfrom(so->s, m->m_data, len, 0,
			      (struct sockaddr *)&addr, &addrlen);
500
	  DEBUG_MISC((dfd, " did recvfrom %d, errno = %d-%s\n",
B
bellard 已提交
501 502 503 504 505 506
		      m->m_len, errno,strerror(errno)));
	  if(m->m_len<0) {
	    u_char code=ICMP_UNREACH_PORT;

	    if(errno == EHOSTUNREACH) code=ICMP_UNREACH_HOST;
	    else if(errno == ENETUNREACH) code=ICMP_UNREACH_NET;
507

B
bellard 已提交
508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524
	    DEBUG_MISC((dfd," rx error, tx icmp ICMP_UNREACH:%i\n", code));
	    icmp_error(so->so_m, ICMP_UNREACH,code, 0,strerror(errno));
	    m_free(m);
	  } else {
	  /*
	   * Hack: domain name lookup will be used the most for UDP,
	   * and since they'll only be used once there's no need
	   * for the 4 minute (or whatever) timeout... So we time them
	   * out much quicker (10 seconds  for now...)
	   */
	    if (so->so_expire) {
	      if (so->so_fport == htons(53))
		so->so_expire = curtime + SO_EXPIREFAST;
	      else
		so->so_expire = curtime + SO_EXPIRE;
	    }

525
	    /*
B
bellard 已提交
526 527 528 529 530 531 532 533 534 535 536 537
	     * If this packet was destined for CTL_ADDR,
	     * make it look like that's where it came from, done by udp_output
	     */
	    udp_output(so, m, &addr);
	  } /* rx error */
	} /* if ping packet */
}

/*
 * sendto() a socket
 */
int
538
sosendto(struct socket *so, struct mbuf *m)
B
bellard 已提交
539
{
540
	Slirp *slirp = so->slirp;
B
bellard 已提交
541 542 543 544 545 546
	int ret;
	struct sockaddr_in addr;

	DEBUG_CALL("sosendto");
	DEBUG_ARG("so = %lx", (long)so);
	DEBUG_ARG("m = %lx", (long)m);
547

B
bellard 已提交
548
        addr.sin_family = AF_INET;
549 550
	if ((so->so_faddr.s_addr & slirp->vnetwork_mask.s_addr) ==
	    slirp->vnetwork_addr.s_addr) {
B
bellard 已提交
551
	  /* It's an alias */
552
	  if (so->so_faddr.s_addr == slirp->vnameserver_addr.s_addr) {
553 554
	    if (get_dns_addr(&addr.sin_addr) < 0)
	      addr.sin_addr = loopback_addr;
555
	  } else {
B
bellard 已提交
556 557 558 559 560 561 562
	    addr.sin_addr = loopback_addr;
	  }
	} else
	  addr.sin_addr = so->so_faddr;
	addr.sin_port = so->so_fport;

	DEBUG_MISC((dfd, " sendto()ing, addr.sin_port=%d, addr.sin_addr.s_addr=%.16s\n", ntohs(addr.sin_port), inet_ntoa(addr.sin_addr)));
563

B
bellard 已提交
564 565 566 567 568
	/* Don't care what port we get */
	ret = sendto(so->s, m->m_data, m->m_len, 0,
		     (struct sockaddr *)&addr, sizeof (struct sockaddr));
	if (ret < 0)
		return -1;
569

B
bellard 已提交
570 571 572 573 574 575
	/*
	 * Kill the socket if there's no reply in 4 minutes,
	 * but only if it's an expirable socket
	 */
	if (so->so_expire)
		so->so_expire = curtime + SO_EXPIRE;
576 577
	so->so_state &= SS_PERSISTENT_MASK;
	so->so_state |= SS_ISFCONNECTED; /* So that it gets select()ed */
B
bellard 已提交
578 579 580 581
	return 0;
}

/*
582
 * Listen for incoming TCP connections
B
bellard 已提交
583 584
 */
struct socket *
585
tcp_listen(Slirp *slirp, uint32_t haddr, u_int hport, uint32_t laddr,
586
           u_int lport, int flags)
B
bellard 已提交
587 588 589
{
	struct sockaddr_in addr;
	struct socket *so;
590 591
	int s, opt = 1;
	socklen_t addrlen = sizeof(addr);
592
	memset(&addr, 0, addrlen);
B
bellard 已提交
593

594
	DEBUG_CALL("tcp_listen");
595 596
	DEBUG_ARG("haddr = %x", haddr);
	DEBUG_ARG("hport = %d", hport);
B
bellard 已提交
597 598 599
	DEBUG_ARG("laddr = %x", laddr);
	DEBUG_ARG("lport = %d", lport);
	DEBUG_ARG("flags = %x", flags);
600

601 602
	so = socreate(slirp);
	if (!so) {
B
bellard 已提交
603 604
	  return NULL;
	}
605

B
bellard 已提交
606 607 608 609 610
	/* Don't tcp_attach... we don't need so_snd nor so_rcv */
	if ((so->so_tcpcb = tcp_newtcpcb(so)) == NULL) {
		free(so);
		return NULL;
	}
611
	insque(so, &slirp->tcb);
612 613

	/*
B
bellard 已提交
614 615 616 617
	 * SS_FACCEPTONCE sockets must time out.
	 */
	if (flags & SS_FACCEPTONCE)
	   so->so_tcpcb->t_timer[TCPT_KEEP] = TCPTV_KEEP_INIT*2;
618

619 620
	so->so_state &= SS_PERSISTENT_MASK;
	so->so_state |= (SS_FACCEPTCONN | flags);
B
bellard 已提交
621 622
	so->so_lport = lport; /* Kept in network format */
	so->so_laddr.s_addr = laddr; /* Ditto */
623

B
bellard 已提交
624
	addr.sin_family = AF_INET;
625 626
	addr.sin_addr.s_addr = haddr;
	addr.sin_port = hport;
627

K
Kevin Wolf 已提交
628
	if (((s = qemu_socket(AF_INET,SOCK_STREAM,0)) < 0) ||
P
pbrook 已提交
629
	    (setsockopt(s,SOL_SOCKET,SO_REUSEADDR,(char *)&opt,sizeof(int)) < 0) ||
B
bellard 已提交
630 631 632
	    (bind(s,(struct sockaddr *)&addr, sizeof(addr)) < 0) ||
	    (listen(s,1) < 0)) {
		int tmperrno = errno; /* Don't clobber the real reason we failed */
633

B
bellard 已提交
634 635 636
		close(s);
		sofree(so);
		/* Restore the real errno */
B
bellard 已提交
637 638 639
#ifdef _WIN32
		WSASetLastError(tmperrno);
#else
B
bellard 已提交
640
		errno = tmperrno;
B
bellard 已提交
641
#endif
B
bellard 已提交
642 643 644
		return NULL;
	}
	setsockopt(s,SOL_SOCKET,SO_OOBINLINE,(char *)&opt,sizeof(int));
645

B
bellard 已提交
646 647 648
	getsockname(s,(struct sockaddr *)&addr,&addrlen);
	so->so_fport = addr.sin_port;
	if (addr.sin_addr.s_addr == 0 || addr.sin_addr.s_addr == loopback_addr.s_addr)
649
	   so->so_faddr = slirp->vhost_addr;
B
bellard 已提交
650 651 652 653 654 655 656 657 658 659 660 661 662 663
	else
	   so->so_faddr = addr.sin_addr;

	so->s = s;
	return so;
}

/*
 * Various session state calls
 * XXX Should be #define's
 * The socket state stuff needs work, these often get call 2 or 3
 * times each when only 1 was needed
 */
void
664
soisfconnecting(struct socket *so)
B
bellard 已提交
665 666 667 668 669 670 671
{
	so->so_state &= ~(SS_NOFDREF|SS_ISFCONNECTED|SS_FCANTRCVMORE|
			  SS_FCANTSENDMORE|SS_FWDRAIN);
	so->so_state |= SS_ISFCONNECTING; /* Clobber other states */
}

void
672
soisfconnected(struct socket *so)
B
bellard 已提交
673 674 675 676 677
{
	so->so_state &= ~(SS_ISFCONNECTING|SS_FWDRAIN|SS_NOFDREF);
	so->so_state |= SS_ISFCONNECTED; /* Clobber other states */
}

678 679
static void
sofcantrcvmore(struct socket *so)
B
bellard 已提交
680 681 682 683 684
{
	if ((so->so_state & SS_NOFDREF) == 0) {
		shutdown(so->s,0);
	}
	so->so_state &= ~(SS_ISFCONNECTING);
685 686 687 688
	if (so->so_state & SS_FCANTSENDMORE) {
	   so->so_state &= SS_PERSISTENT_MASK;
	   so->so_state |= SS_NOFDREF; /* Don't select it */
	} else {
B
bellard 已提交
689
	   so->so_state |= SS_FCANTRCVMORE;
690
	}
B
bellard 已提交
691 692
}

693 694
static void
sofcantsendmore(struct socket *so)
B
bellard 已提交
695 696
{
	if ((so->so_state & SS_NOFDREF) == 0) {
B
bellard 已提交
697
            shutdown(so->s,1);           /* send FIN to fhost */
B
bellard 已提交
698 699
	}
	so->so_state &= ~(SS_ISFCONNECTING);
700 701 702 703
	if (so->so_state & SS_FCANTRCVMORE) {
	   so->so_state &= SS_PERSISTENT_MASK;
	   so->so_state |= SS_NOFDREF; /* as above */
	} else {
B
bellard 已提交
704
	   so->so_state |= SS_FCANTSENDMORE;
705
	}
B
bellard 已提交
706 707 708 709 710 711 712
}

/*
 * Set write drain mode
 * Set CANTSENDMORE once all data has been write()n
 */
void
713
sofwdrain(struct socket *so)
B
bellard 已提交
714 715 716 717 718 719
{
	if (so->so_rcv.sb_cc)
		so->so_state |= SS_FWDRAIN;
	else
		sofcantsendmore(so);
}