socket.c 17.8 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 18 19
static void sofcantrcvmore(struct socket *so);
static void sofcantsendmore(struct socket *so);

#if 0
static void
B
bellard 已提交
20 21 22 23
so_init()
{
	/* Nothing yet */
}
24
#endif
B
bellard 已提交
25 26

struct socket *
27 28
solookup(struct socket *head, struct in_addr laddr, u_int lport,
         struct in_addr faddr, u_int fport)
B
bellard 已提交
29 30
{
	struct socket *so;
31

B
bellard 已提交
32
	for (so = head->so_next; so != head; so = so->so_next) {
33
		if (so->so_lport == lport &&
B
bellard 已提交
34 35 36 37 38
		    so->so_laddr.s_addr == laddr.s_addr &&
		    so->so_faddr.s_addr == faddr.s_addr &&
		    so->so_fport == fport)
		   break;
	}
39

B
bellard 已提交
40 41 42
	if (so == head)
	   return (struct socket *)NULL;
	return so;
43

B
bellard 已提交
44 45 46 47 48 49 50 51
}

/*
 * Create a new socket, initialise the fields
 * It is the responsibility of the caller to
 * insque() it into the correct linked-list
 */
struct socket *
52
socreate(void)
B
bellard 已提交
53 54
{
  struct socket *so;
55

B
bellard 已提交
56 57 58 59 60 61 62 63 64 65 66 67 68
  so = (struct socket *)malloc(sizeof(struct socket));
  if(so) {
    memset(so, 0, sizeof(struct socket));
    so->so_state = SS_NOFDREF;
    so->s = -1;
  }
  return(so);
}

/*
 * remque and free a socket, clobber cache
 */
void
69
sofree(struct socket *so)
B
bellard 已提交
70 71 72 73 74 75 76 77 78
{
  if (so->so_emu==EMU_RSH && so->extra) {
	sofree(so->extra);
	so->extra=NULL;
  }
  if (so == tcp_last_so)
    tcp_last_so = &tcb;
  else if (so == udp_last_so)
    udp_last_so = &udb;
79

B
bellard 已提交
80
  m_free(so->so_m);
81 82

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

  free(so);
}

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

95
	DEBUG_CALL("sopreprbuf");
B
bellard 已提交
96
	DEBUG_ARG("so = %lx", (long )so);
97

B
bellard 已提交
98
	len = sb->sb_datalen - sb->sb_cc;
99

100 101 102
	if (len <= 0)
		return 0;

B
bellard 已提交
103
	iov[0].iov_base = sb->sb_wptr;
104 105
        iov[1].iov_base = NULL;
        iov[1].iov_len = 0;
B
bellard 已提交
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 138 139 140 141 142
	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;
		}
	}
143 144 145 146 147 148 149 150 151 152 153 154
	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
155
soread(struct socket *so)
156 157 158 159 160 161 162 163 164 165 166 167 168
{
	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);
169

B
bellard 已提交
170 171 172 173
#ifdef HAVE_READV
	nn = readv(so->s, (struct iovec *)iov, n);
	DEBUG_MISC((dfd, " ... read nn = %d bytes\n", nn));
#else
B
bellard 已提交
174
	nn = recv(so->s, iov[0].iov_base, iov[0].iov_len,0);
175
#endif
B
bellard 已提交
176 177 178 179 180 181 182 183 184 185
	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;
		}
	}
186

B
bellard 已提交
187 188 189 190 191 192 193 194 195 196
#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 已提交
197 198 199 200 201 202
	if (n == 2 && nn == iov[0].iov_len) {
            int ret;
            ret = recv(so->s, iov[1].iov_base, iov[1].iov_len,0);
            if (ret > 0)
                nn += ret;
        }
203

B
bellard 已提交
204 205
	DEBUG_MISC((dfd, " ... read nn = %d bytes\n", nn));
#endif
206

B
bellard 已提交
207 208 209 210 211 212 213
	/* 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;
}
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 252 253 254 255 256
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 已提交
257 258
/*
 * Get urgent data
259
 *
B
bellard 已提交
260 261 262 263 264
 * 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
265
sorecvoob(struct socket *so)
B
bellard 已提交
266 267 268 269 270
{
	struct tcpcb *tp = sototcpcb(so);

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

B
bellard 已提交
272 273 274 275 276
	/*
	 * 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
277
	 * urgent data, or the read() doesn't return all the
B
bellard 已提交
278 279 280 281 282 283 284 285 286 287 288 289 290 291
	 * 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
292
sosendoob(struct socket *so)
B
bellard 已提交
293 294 295
{
	struct sbuf *sb = &so->so_rcv;
	char buff[2048]; /* XXX Shouldn't be sending more oob data than this */
296

B
bellard 已提交
297
	int n, len;
298

B
bellard 已提交
299 300 301
	DEBUG_CALL("sosendoob");
	DEBUG_ARG("so = %lx", (long)so);
	DEBUG_ARG("sb->sb_cc = %d", sb->sb_cc);
302

B
bellard 已提交
303 304
	if (so->so_urgc > 2048)
	   so->so_urgc = 2048; /* XXXX */
305

B
bellard 已提交
306 307
	if (sb->sb_rptr < sb->sb_wptr) {
		/* We can send it directly */
308
		n = slirp_send(so, sb->sb_rptr, so->so_urgc, (MSG_OOB)); /* |MSG_DONTWAIT)); */
B
bellard 已提交
309
		so->so_urgc -= n;
310

B
bellard 已提交
311 312
		DEBUG_MISC((dfd, " --- sent %d bytes urgent data, %d urgent bytes left\n", n, so->so_urgc));
	} else {
313
		/*
B
bellard 已提交
314 315 316 317 318 319 320 321 322 323 324 325 326 327 328
		 * 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;
		}
329
		n = slirp_send(so, buff, len, (MSG_OOB)); /* |MSG_DONTWAIT)); */
B
bellard 已提交
330 331 332
#ifdef DEBUG
		if (n != len)
		   DEBUG_ERROR((dfd, "Didn't send all data urgently XXXXX\n"));
333
#endif
B
bellard 已提交
334 335
		DEBUG_MISC((dfd, " ---2 sent %d bytes urgent data, %d urgent bytes left\n", n, so->so_urgc));
	}
336

B
bellard 已提交
337 338 339 340
	sb->sb_cc -= n;
	sb->sb_rptr += n;
	if (sb->sb_rptr >= (sb->sb_data + sb->sb_datalen))
		sb->sb_rptr -= sb->sb_datalen;
341

B
bellard 已提交
342 343 344 345
	return n;
}

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

B
bellard 已提交
357 358
	DEBUG_CALL("sowrite");
	DEBUG_ARG("so = %lx", (long)so);
359

B
bellard 已提交
360 361 362 363 364 365 366 367 368 369
	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
	 */
370

B
bellard 已提交
371
        len = sb->sb_cc;
372

B
bellard 已提交
373
	iov[0].iov_base = sb->sb_rptr;
374 375
        iov[1].iov_base = NULL;
        iov[1].iov_len = 0;
B
bellard 已提交
376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396
	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);
397

B
bellard 已提交
398 399
	DEBUG_MISC((dfd, "  ... wrote nn = %d bytes\n", nn));
#else
400
	nn = slirp_send(so, iov[0].iov_base, iov[0].iov_len,0);
B
bellard 已提交
401 402 403 404
#endif
	/* This should never happen, but people tell me it does *shrug* */
	if (nn < 0 && (errno == EAGAIN || errno == EINTR))
		return 0;
405

B
bellard 已提交
406 407 408 409 410 411 412
	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;
	}
413

B
bellard 已提交
414
#ifndef HAVE_READV
B
bellard 已提交
415 416
	if (n == 2 && nn == iov[0].iov_len) {
            int ret;
417
            ret = slirp_send(so, iov[1].iov_base, iov[1].iov_len,0);
B
bellard 已提交
418 419 420
            if (ret > 0)
                nn += ret;
        }
B
bellard 已提交
421 422
        DEBUG_MISC((dfd, "  ... wrote nn = %d bytes\n", nn));
#endif
423

B
bellard 已提交
424 425 426 427 428
	/* 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;
429

B
bellard 已提交
430 431 432 433 434 435
	/*
	 * 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);
436

B
bellard 已提交
437 438 439 440 441 442 443
	return nn;
}

/*
 * recvfrom() a UDP socket
 */
void
444
sorecvfrom(struct socket *so)
B
bellard 已提交
445 446
{
	struct sockaddr_in addr;
447
	socklen_t addrlen = sizeof(struct sockaddr_in);
448

B
bellard 已提交
449 450
	DEBUG_CALL("sorecvfrom");
	DEBUG_ARG("so = %lx", (long)so);
451

B
bellard 已提交
452 453 454
	if (so->so_type == IPPROTO_ICMP) {   /* This is a "ping" reply */
	  char buff[256];
	  int len;
455

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

B
bellard 已提交
460 461 462 463 464
	  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;
465

B
bellard 已提交
466 467 468 469 470
	    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);
471
            so->so_m = NULL; /* Don't m_free() it again! */
B
bellard 已提交
472 473 474 475 476 477 478 479
	  }
	  /* No need for this socket anymore, udp_detach it */
	  udp_detach(so);
	} else {                            	/* A "normal" UDP packet */
	  struct mbuf *m;
	  int len, n;

	  if (!(m = m_get())) return;
480
	  m->m_data += IF_MAXLINKHDR;
481

482
	  /*
B
bellard 已提交
483 484 485 486 487
	   * 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 已提交
488
	  ioctlsocket(so->s, FIONREAD, &n);
489

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

B
bellard 已提交
497 498
	  m->m_len = recvfrom(so->s, m->m_data, len, 0,
			      (struct sockaddr *)&addr, &addrlen);
499
	  DEBUG_MISC((dfd, " did recvfrom %d, errno = %d-%s\n",
B
bellard 已提交
500 501 502 503 504 505
		      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;
506

B
bellard 已提交
507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528
	    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;
	    }

	    /*		if (m->m_len == len) {
	     *			m_inc(m, MINCSIZE);
	     *			m->m_len = 0;
	     *		}
	     */
529

530
	    /*
B
bellard 已提交
531 532 533 534 535 536 537 538 539 540 541 542
	     * 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
543
sosendto(struct socket *so, struct mbuf *m)
B
bellard 已提交
544 545 546 547 548 549 550
{
	int ret;
	struct sockaddr_in addr;

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

B
bellard 已提交
552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568
        addr.sin_family = AF_INET;
	if ((so->so_faddr.s_addr & htonl(0xffffff00)) == special_addr.s_addr) {
	  /* It's an alias */
	  switch(ntohl(so->so_faddr.s_addr) & 0xff) {
	  case CTL_DNS:
	    addr.sin_addr = dns_addr;
	    break;
	  case CTL_ALIAS:
	  default:
	    addr.sin_addr = loopback_addr;
	    break;
	  }
	} 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)));
569

B
bellard 已提交
570 571 572 573 574
	/* 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;
575

B
bellard 已提交
576 577 578 579 580 581 582 583 584 585 586 587 588 589
	/*
	 * 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;
	so->so_state = SS_ISFCONNECTED; /* So that it gets select()ed */
	return 0;
}

/*
 * XXX This should really be tcp_listen
 */
struct socket *
590
solisten(u_int port, u_int32_t laddr, u_int lport, int flags)
B
bellard 已提交
591 592 593
{
	struct sockaddr_in addr;
	struct socket *so;
594 595
	int s, opt = 1;
	socklen_t addrlen = sizeof(addr);
B
bellard 已提交
596 597 598 599 600 601

	DEBUG_CALL("solisten");
	DEBUG_ARG("port = %d", port);
	DEBUG_ARG("laddr = %x", laddr);
	DEBUG_ARG("lport = %d", lport);
	DEBUG_ARG("flags = %x", flags);
602

B
bellard 已提交
603 604 605 606
	if ((so = socreate()) == NULL) {
	  /* free(so);      Not sofree() ??? free(NULL) == NOP */
	  return NULL;
	}
607

B
bellard 已提交
608 609 610 611 612 613
	/* 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;
	}
	insque(so,&tcb);
614 615

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

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

B
bellard 已提交
625 626 627
	addr.sin_family = AF_INET;
	addr.sin_addr.s_addr = INADDR_ANY;
	addr.sin_port = port;
628

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

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

B
bellard 已提交
647 648 649
	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)
650
	   so->so_faddr = alias_addr;
B
bellard 已提交
651 652 653 654 655 656 657
	else
	   so->so_faddr = addr.sin_addr;

	so->s = s;
	return so;
}

658
#if 0
659
/*
B
bellard 已提交
660 661 662 663
 * Data is available in so_rcv
 * Just write() the data to the socket
 * XXX not yet...
 */
664
static void
B
bellard 已提交
665 666 667 668 669 670
sorwakeup(so)
	struct socket *so;
{
/*	sowrite(so); */
/*	FD_CLR(so->s,&writefds); */
}
671

B
bellard 已提交
672 673 674 675 676
/*
 * Data has been freed in so_snd
 * We have room for a read() if we want to
 * For now, don't read, it'll be done in the main loop
 */
677
static void
B
bellard 已提交
678 679 680 681 682
sowwakeup(so)
	struct socket *so;
{
	/* Nothing, yet */
}
683
#endif
B
bellard 已提交
684 685 686 687 688 689 690 691

/*
 * 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
692
soisfconnecting(struct socket *so)
B
bellard 已提交
693 694 695 696 697 698 699
{
	so->so_state &= ~(SS_NOFDREF|SS_ISFCONNECTED|SS_FCANTRCVMORE|
			  SS_FCANTSENDMORE|SS_FWDRAIN);
	so->so_state |= SS_ISFCONNECTING; /* Clobber other states */
}

void
700
soisfconnected(struct socket *so)
B
bellard 已提交
701 702 703 704 705
{
	so->so_state &= ~(SS_ISFCONNECTING|SS_FWDRAIN|SS_NOFDREF);
	so->so_state |= SS_ISFCONNECTED; /* Clobber other states */
}

706 707
static void
sofcantrcvmore(struct socket *so)
B
bellard 已提交
708 709 710
{
	if ((so->so_state & SS_NOFDREF) == 0) {
		shutdown(so->s,0);
B
bellard 已提交
711 712 713
		if(global_writefds) {
		  FD_CLR(so->s,global_writefds);
		}
B
bellard 已提交
714 715 716 717 718 719 720 721
	}
	so->so_state &= ~(SS_ISFCONNECTING);
	if (so->so_state & SS_FCANTSENDMORE)
	   so->so_state = SS_NOFDREF; /* Don't select it */ /* XXX close() here as well? */
	else
	   so->so_state |= SS_FCANTRCVMORE;
}

722 723
static void
sofcantsendmore(struct socket *so)
B
bellard 已提交
724 725
{
	if ((so->so_state & SS_NOFDREF) == 0) {
B
bellard 已提交
726 727 728 729 730 731 732
            shutdown(so->s,1);           /* send FIN to fhost */
            if (global_readfds) {
                FD_CLR(so->s,global_readfds);
            }
            if (global_xfds) {
                FD_CLR(so->s,global_xfds);
            }
B
bellard 已提交
733 734 735 736 737 738 739 740 741
	}
	so->so_state &= ~(SS_ISFCONNECTING);
	if (so->so_state & SS_FCANTRCVMORE)
	   so->so_state = SS_NOFDREF; /* as above */
	else
	   so->so_state |= SS_FCANTSENDMORE;
}

void
742
soisfdisconnected(struct socket *so)
B
bellard 已提交
743 744 745 746 747 748 749 750 751 752 753 754 755 756
{
/*	so->so_state &= ~(SS_ISFCONNECTING|SS_ISFCONNECTED); */
/*	close(so->s); */
/*	so->so_state = SS_ISFDISCONNECTED; */
	/*
	 * XXX Do nothing ... ?
	 */
}

/*
 * Set write drain mode
 * Set CANTSENDMORE once all data has been write()n
 */
void
757
sofwdrain(struct socket *so)
B
bellard 已提交
758 759 760 761 762 763
{
	if (so->so_rcv.sb_cc)
		so->so_state |= SS_FWDRAIN;
	else
		sofcantsendmore(so);
}