]> git.proxmox.com Git - mirror_qemu.git/blame - slirp/socket.c
slirp: Adding ICMPv6 error sending
[mirror_qemu.git] / slirp / socket.c
CommitLineData
f0cbd3ec
FB
1/*
2 * Copyright (c) 1995 Danny Gasparovski.
5fafdf24
TS
3 *
4 * Please read the file COPYRIGHT for the
f0cbd3ec
FB
5 * terms and conditions of the copyright.
6 */
7
7df7482b 8#include "qemu/osdep.h"
e1c5a2b3 9#include "qemu-common.h"
f0cbd3ec
FB
10#include <slirp.h>
11#include "ip_icmp.h"
ec530c81
FB
12#ifdef __sun__
13#include <sys/filio.h>
14#endif
f0cbd3ec 15
9634d903
BS
16static void sofcantrcvmore(struct socket *so);
17static void sofcantsendmore(struct socket *so);
18
8a87f121
GS
19struct socket *solookup(struct socket **last, struct socket *head,
20 struct sockaddr_storage *lhost, struct sockaddr_storage *fhost)
f0cbd3ec 21{
a5fd24aa 22 struct socket *so = *last;
5fafdf24 23
a5fd24aa 24 /* Optimisation */
8a87f121
GS
25 if (so != head && sockaddr_equal(&(so->lhost.ss), lhost)
26 && (!fhost || sockaddr_equal(&so->fhost.ss, fhost))) {
a5fd24aa
GS
27 return so;
28 }
5fafdf24 29
a5fd24aa 30 for (so = head->so_next; so != head; so = so->so_next) {
8a87f121
GS
31 if (sockaddr_equal(&(so->lhost.ss), lhost)
32 && (!fhost || sockaddr_equal(&so->fhost.ss, fhost))) {
a5fd24aa
GS
33 *last = so;
34 return so;
35 }
36 }
5fafdf24 37
a5fd24aa 38 return (struct socket *)NULL;
f0cbd3ec
FB
39}
40
41/*
42 * Create a new socket, initialise the fields
43 * It is the responsibility of the caller to
44 * insque() it into the correct linked-list
45 */
46struct socket *
460fec67 47socreate(Slirp *slirp)
f0cbd3ec
FB
48{
49 struct socket *so;
5fafdf24 50
f0cbd3ec
FB
51 so = (struct socket *)malloc(sizeof(struct socket));
52 if(so) {
53 memset(so, 0, sizeof(struct socket));
54 so->so_state = SS_NOFDREF;
55 so->s = -1;
460fec67 56 so->slirp = slirp;
7bd43ec2 57 so->pollfds_idx = -1;
f0cbd3ec
FB
58 }
59 return(so);
60}
61
62/*
63 * remque and free a socket, clobber cache
64 */
65void
511d2b14 66sofree(struct socket *so)
f0cbd3ec 67{
460fec67
JK
68 Slirp *slirp = so->slirp;
69
f0cbd3ec
FB
70 if (so->so_emu==EMU_RSH && so->extra) {
71 sofree(so->extra);
72 so->extra=NULL;
73 }
460fec67
JK
74 if (so == slirp->tcp_last_so) {
75 slirp->tcp_last_so = &slirp->tcb;
76 } else if (so == slirp->udp_last_so) {
77 slirp->udp_last_so = &slirp->udb;
e6d43cfb
JK
78 } else if (so == slirp->icmp_last_so) {
79 slirp->icmp_last_so = &slirp->icmp;
460fec67 80 }
f0cbd3ec 81 m_free(so->so_m);
5fafdf24
TS
82
83 if(so->so_next && so->so_prev)
f0cbd3ec
FB
84 remque(so); /* crashes if so is not in a queue */
85
86 free(so);
87}
88
e1c5a2b3 89size_t sopreprbuf(struct socket *so, struct iovec *iov, int *np)
f0cbd3ec 90{
e1c5a2b3 91 int n, lss, total;
f0cbd3ec
FB
92 struct sbuf *sb = &so->so_snd;
93 int len = sb->sb_datalen - sb->sb_cc;
f0cbd3ec 94 int mss = so->so_tcpcb->t_maxseg;
5fafdf24 95
e1c5a2b3 96 DEBUG_CALL("sopreprbuf");
ecc804ca 97 DEBUG_ARG("so = %p", so);
5fafdf24 98
e1c5a2b3
AL
99 if (len <= 0)
100 return 0;
101
f0cbd3ec 102 iov[0].iov_base = sb->sb_wptr;
66029f6a
BS
103 iov[1].iov_base = NULL;
104 iov[1].iov_len = 0;
f0cbd3ec
FB
105 if (sb->sb_wptr < sb->sb_rptr) {
106 iov[0].iov_len = sb->sb_rptr - sb->sb_wptr;
107 /* Should never succeed, but... */
108 if (iov[0].iov_len > len)
109 iov[0].iov_len = len;
110 if (iov[0].iov_len > mss)
111 iov[0].iov_len -= iov[0].iov_len%mss;
112 n = 1;
113 } else {
114 iov[0].iov_len = (sb->sb_data + sb->sb_datalen) - sb->sb_wptr;
115 /* Should never succeed, but... */
116 if (iov[0].iov_len > len) iov[0].iov_len = len;
117 len -= iov[0].iov_len;
118 if (len) {
119 iov[1].iov_base = sb->sb_data;
120 iov[1].iov_len = sb->sb_rptr - sb->sb_data;
121 if(iov[1].iov_len > len)
122 iov[1].iov_len = len;
123 total = iov[0].iov_len + iov[1].iov_len;
124 if (total > mss) {
125 lss = total%mss;
126 if (iov[1].iov_len > lss) {
127 iov[1].iov_len -= lss;
128 n = 2;
129 } else {
130 lss -= iov[1].iov_len;
131 iov[0].iov_len -= lss;
132 n = 1;
133 }
134 } else
135 n = 2;
136 } else {
137 if (iov[0].iov_len > mss)
138 iov[0].iov_len -= iov[0].iov_len%mss;
139 n = 1;
140 }
141 }
e1c5a2b3
AL
142 if (np)
143 *np = n;
144
145 return iov[0].iov_len + (n - 1) * iov[1].iov_len;
146}
147
148/*
149 * Read from so's socket into sb_snd, updating all relevant sbuf fields
150 * NOTE: This will only be called if it is select()ed for reading, so
151 * a read() of 0 (or less) means it's disconnected
152 */
153int
511d2b14 154soread(struct socket *so)
e1c5a2b3
AL
155{
156 int n, nn;
157 struct sbuf *sb = &so->so_snd;
158 struct iovec iov[2];
159
160 DEBUG_CALL("soread");
ecc804ca 161 DEBUG_ARG("so = %p", so);
e1c5a2b3
AL
162
163 /*
164 * No need to check if there's enough room to read.
165 * soread wouldn't have been called if there weren't
166 */
167 sopreprbuf(so, iov, &n);
5fafdf24 168
f0cbd3ec
FB
169#ifdef HAVE_READV
170 nn = readv(so->s, (struct iovec *)iov, n);
171 DEBUG_MISC((dfd, " ... read nn = %d bytes\n", nn));
172#else
00aa0040 173 nn = qemu_recv(so->s, iov[0].iov_base, iov[0].iov_len,0);
5fafdf24 174#endif
f0cbd3ec
FB
175 if (nn <= 0) {
176 if (nn < 0 && (errno == EINTR || errno == EAGAIN))
177 return 0;
178 else {
179 DEBUG_MISC((dfd, " --- soread() disconnected, nn = %d, errno = %d-%s\n", nn, errno,strerror(errno)));
180 sofcantrcvmore(so);
181 tcp_sockclosed(sototcpcb(so));
182 return -1;
183 }
184 }
5fafdf24 185
f0cbd3ec
FB
186#ifndef HAVE_READV
187 /*
188 * If there was no error, try and read the second time round
189 * We read again if n = 2 (ie, there's another part of the buffer)
190 * and we read as much as we could in the first read
191 * We don't test for <= 0 this time, because there legitimately
192 * might not be any more data (since the socket is non-blocking),
193 * a close will be detected on next iteration.
194 * A return of -1 wont (shouldn't) happen, since it didn't happen above
195 */
17444c9c
FB
196 if (n == 2 && nn == iov[0].iov_len) {
197 int ret;
00aa0040 198 ret = qemu_recv(so->s, iov[1].iov_base, iov[1].iov_len,0);
17444c9c
FB
199 if (ret > 0)
200 nn += ret;
201 }
5fafdf24 202
f0cbd3ec
FB
203 DEBUG_MISC((dfd, " ... read nn = %d bytes\n", nn));
204#endif
5fafdf24 205
f0cbd3ec
FB
206 /* Update fields */
207 sb->sb_cc += nn;
208 sb->sb_wptr += nn;
209 if (sb->sb_wptr >= (sb->sb_data + sb->sb_datalen))
210 sb->sb_wptr -= sb->sb_datalen;
211 return nn;
212}
5fafdf24 213
e1c5a2b3
AL
214int soreadbuf(struct socket *so, const char *buf, int size)
215{
216 int n, nn, copy = size;
217 struct sbuf *sb = &so->so_snd;
218 struct iovec iov[2];
219
220 DEBUG_CALL("soreadbuf");
ecc804ca 221 DEBUG_ARG("so = %p", so);
e1c5a2b3
AL
222
223 /*
224 * No need to check if there's enough room to read.
225 * soread wouldn't have been called if there weren't
226 */
227 if (sopreprbuf(so, iov, &n) < size)
228 goto err;
229
230 nn = MIN(iov[0].iov_len, copy);
231 memcpy(iov[0].iov_base, buf, nn);
232
233 copy -= nn;
234 buf += nn;
235
236 if (copy == 0)
237 goto done;
238
239 memcpy(iov[1].iov_base, buf, copy);
240
241done:
242 /* Update fields */
243 sb->sb_cc += size;
244 sb->sb_wptr += size;
245 if (sb->sb_wptr >= (sb->sb_data + sb->sb_datalen))
246 sb->sb_wptr -= sb->sb_datalen;
247 return size;
248err:
249
250 sofcantrcvmore(so);
251 tcp_sockclosed(sototcpcb(so));
252 fprintf(stderr, "soreadbuf buffer to small");
253 return -1;
254}
255
f0cbd3ec
FB
256/*
257 * Get urgent data
5fafdf24 258 *
f0cbd3ec
FB
259 * When the socket is created, we set it SO_OOBINLINE,
260 * so when OOB data arrives, we soread() it and everything
261 * in the send buffer is sent as urgent data
262 */
263void
511d2b14 264sorecvoob(struct socket *so)
f0cbd3ec
FB
265{
266 struct tcpcb *tp = sototcpcb(so);
267
268 DEBUG_CALL("sorecvoob");
ecc804ca 269 DEBUG_ARG("so = %p", so);
5fafdf24 270
f0cbd3ec
FB
271 /*
272 * We take a guess at how much urgent data has arrived.
273 * In most situations, when urgent data arrives, the next
274 * read() should get all the urgent data. This guess will
275 * be wrong however if more data arrives just after the
5fafdf24 276 * urgent data, or the read() doesn't return all the
f0cbd3ec
FB
277 * urgent data.
278 */
279 soread(so);
280 tp->snd_up = tp->snd_una + so->so_snd.sb_cc;
281 tp->t_force = 1;
282 tcp_output(tp);
283 tp->t_force = 0;
284}
285
286/*
287 * Send urgent data
288 * There's a lot duplicated code here, but...
289 */
290int
511d2b14 291sosendoob(struct socket *so)
f0cbd3ec
FB
292{
293 struct sbuf *sb = &so->so_rcv;
294 char buff[2048]; /* XXX Shouldn't be sending more oob data than this */
5fafdf24 295
f0cbd3ec 296 int n, len;
5fafdf24 297
f0cbd3ec 298 DEBUG_CALL("sosendoob");
ecc804ca 299 DEBUG_ARG("so = %p", so);
f0cbd3ec 300 DEBUG_ARG("sb->sb_cc = %d", sb->sb_cc);
5fafdf24 301
f0cbd3ec
FB
302 if (so->so_urgc > 2048)
303 so->so_urgc = 2048; /* XXXX */
5fafdf24 304
f0cbd3ec
FB
305 if (sb->sb_rptr < sb->sb_wptr) {
306 /* We can send it directly */
e1c5a2b3 307 n = slirp_send(so, sb->sb_rptr, so->so_urgc, (MSG_OOB)); /* |MSG_DONTWAIT)); */
f0cbd3ec 308 so->so_urgc -= n;
3b46e624 309
f0cbd3ec
FB
310 DEBUG_MISC((dfd, " --- sent %d bytes urgent data, %d urgent bytes left\n", n, so->so_urgc));
311 } else {
5fafdf24 312 /*
f0cbd3ec
FB
313 * Since there's no sendv or sendtov like writev,
314 * we must copy all data to a linear buffer then
315 * send it all
316 */
317 len = (sb->sb_data + sb->sb_datalen) - sb->sb_rptr;
318 if (len > so->so_urgc) len = so->so_urgc;
319 memcpy(buff, sb->sb_rptr, len);
320 so->so_urgc -= len;
321 if (so->so_urgc) {
322 n = sb->sb_wptr - sb->sb_data;
323 if (n > so->so_urgc) n = so->so_urgc;
324 memcpy((buff + len), sb->sb_data, n);
325 so->so_urgc -= n;
326 len += n;
327 }
e1c5a2b3 328 n = slirp_send(so, buff, len, (MSG_OOB)); /* |MSG_DONTWAIT)); */
f0cbd3ec
FB
329#ifdef DEBUG
330 if (n != len)
331 DEBUG_ERROR((dfd, "Didn't send all data urgently XXXXX\n"));
3b46e624 332#endif
f0cbd3ec
FB
333 DEBUG_MISC((dfd, " ---2 sent %d bytes urgent data, %d urgent bytes left\n", n, so->so_urgc));
334 }
5fafdf24 335
f0cbd3ec
FB
336 sb->sb_cc -= n;
337 sb->sb_rptr += n;
338 if (sb->sb_rptr >= (sb->sb_data + sb->sb_datalen))
339 sb->sb_rptr -= sb->sb_datalen;
5fafdf24 340
f0cbd3ec
FB
341 return n;
342}
343
344/*
5fafdf24 345 * Write data from so_rcv to so's socket,
f0cbd3ec
FB
346 * updating all sbuf field as necessary
347 */
348int
511d2b14 349sowrite(struct socket *so)
f0cbd3ec
FB
350{
351 int n,nn;
352 struct sbuf *sb = &so->so_rcv;
353 int len = sb->sb_cc;
354 struct iovec iov[2];
5fafdf24 355
f0cbd3ec 356 DEBUG_CALL("sowrite");
ecc804ca 357 DEBUG_ARG("so = %p", so);
5fafdf24 358
f0cbd3ec
FB
359 if (so->so_urgc) {
360 sosendoob(so);
361 if (sb->sb_cc == 0)
362 return 0;
363 }
364
365 /*
366 * No need to check if there's something to write,
367 * sowrite wouldn't have been called otherwise
368 */
5fafdf24 369
f0cbd3ec 370 iov[0].iov_base = sb->sb_rptr;
66029f6a
BS
371 iov[1].iov_base = NULL;
372 iov[1].iov_len = 0;
f0cbd3ec
FB
373 if (sb->sb_rptr < sb->sb_wptr) {
374 iov[0].iov_len = sb->sb_wptr - sb->sb_rptr;
375 /* Should never succeed, but... */
376 if (iov[0].iov_len > len) iov[0].iov_len = len;
377 n = 1;
378 } else {
379 iov[0].iov_len = (sb->sb_data + sb->sb_datalen) - sb->sb_rptr;
380 if (iov[0].iov_len > len) iov[0].iov_len = len;
381 len -= iov[0].iov_len;
382 if (len) {
383 iov[1].iov_base = sb->sb_data;
384 iov[1].iov_len = sb->sb_wptr - sb->sb_data;
385 if (iov[1].iov_len > len) iov[1].iov_len = len;
386 n = 2;
387 } else
388 n = 1;
389 }
390 /* Check if there's urgent data to send, and if so, send it */
391
392#ifdef HAVE_READV
393 nn = writev(so->s, (const struct iovec *)iov, n);
5fafdf24 394
f0cbd3ec
FB
395 DEBUG_MISC((dfd, " ... wrote nn = %d bytes\n", nn));
396#else
e1c5a2b3 397 nn = slirp_send(so, iov[0].iov_base, iov[0].iov_len,0);
f0cbd3ec
FB
398#endif
399 /* This should never happen, but people tell me it does *shrug* */
400 if (nn < 0 && (errno == EAGAIN || errno == EINTR))
401 return 0;
5fafdf24 402
f0cbd3ec
FB
403 if (nn <= 0) {
404 DEBUG_MISC((dfd, " --- sowrite disconnected, so->so_state = %x, errno = %d\n",
405 so->so_state, errno));
406 sofcantsendmore(so);
407 tcp_sockclosed(sototcpcb(so));
408 return -1;
409 }
5fafdf24 410
f0cbd3ec 411#ifndef HAVE_READV
3bc2175d
FB
412 if (n == 2 && nn == iov[0].iov_len) {
413 int ret;
e1c5a2b3 414 ret = slirp_send(so, iov[1].iov_base, iov[1].iov_len,0);
3bc2175d
FB
415 if (ret > 0)
416 nn += ret;
417 }
f0cbd3ec
FB
418 DEBUG_MISC((dfd, " ... wrote nn = %d bytes\n", nn));
419#endif
5fafdf24 420
f0cbd3ec
FB
421 /* Update sbuf */
422 sb->sb_cc -= nn;
423 sb->sb_rptr += nn;
424 if (sb->sb_rptr >= (sb->sb_data + sb->sb_datalen))
425 sb->sb_rptr -= sb->sb_datalen;
5fafdf24 426
f0cbd3ec
FB
427 /*
428 * If in DRAIN mode, and there's no more data, set
429 * it CANTSENDMORE
430 */
431 if ((so->so_state & SS_FWDRAIN) && sb->sb_cc == 0)
432 sofcantsendmore(so);
5fafdf24 433
f0cbd3ec
FB
434 return nn;
435}
436
437/*
438 * recvfrom() a UDP socket
439 */
440void
511d2b14 441sorecvfrom(struct socket *so)
f0cbd3ec 442{
eae303ff 443 struct sockaddr_storage addr;
5379229a 444 struct sockaddr_storage saddr, daddr;
eae303ff 445 socklen_t addrlen = sizeof(struct sockaddr_storage);
5fafdf24 446
f0cbd3ec 447 DEBUG_CALL("sorecvfrom");
ecc804ca 448 DEBUG_ARG("so = %p", so);
5fafdf24 449
f0cbd3ec
FB
450 if (so->so_type == IPPROTO_ICMP) { /* This is a "ping" reply */
451 char buff[256];
452 int len;
3b46e624 453
5fafdf24 454 len = recvfrom(so->s, buff, 256, 0,
f0cbd3ec
FB
455 (struct sockaddr *)&addr, &addrlen);
456 /* XXX Check if reply is "correct"? */
3b46e624 457
f0cbd3ec
FB
458 if(len == -1 || len == 0) {
459 u_char code=ICMP_UNREACH_PORT;
460
461 if(errno == EHOSTUNREACH) code=ICMP_UNREACH_HOST;
462 else if(errno == ENETUNREACH) code=ICMP_UNREACH_NET;
3b46e624 463
f0cbd3ec
FB
464 DEBUG_MISC((dfd," udp icmp rx errno = %d-%s\n",
465 errno,strerror(errno)));
de40abfe 466 icmp_send_error(so->so_m, ICMP_UNREACH, code, 0, strerror(errno));
f0cbd3ec
FB
467 } else {
468 icmp_reflect(so->so_m);
511d2b14 469 so->so_m = NULL; /* Don't m_free() it again! */
f0cbd3ec
FB
470 }
471 /* No need for this socket anymore, udp_detach it */
472 udp_detach(so);
473 } else { /* A "normal" UDP packet */
474 struct mbuf *m;
c5b76b38
BS
475 int len;
476#ifdef _WIN32
477 unsigned long n;
478#else
479 int n;
480#endif
f0cbd3ec 481
460fec67
JK
482 m = m_get(so->slirp);
483 if (!m) {
484 return;
485 }
9634d903 486 m->m_data += IF_MAXLINKHDR;
3b46e624 487
5fafdf24 488 /*
f0cbd3ec
FB
489 * XXX Shouldn't FIONREAD packets destined for port 53,
490 * but I don't know the max packet size for DNS lookups
491 */
492 len = M_FREEROOM(m);
493 /* if (so->so_fport != htons(53)) { */
379ff53d 494 ioctlsocket(so->s, FIONREAD, &n);
3b46e624 495
f0cbd3ec
FB
496 if (n > len) {
497 n = (m->m_data - m->m_dat) + m->m_len + n + 1;
498 m_inc(m, n);
499 len = M_FREEROOM(m);
500 }
501 /* } */
3b46e624 502
f0cbd3ec
FB
503 m->m_len = recvfrom(so->s, m->m_data, len, 0,
504 (struct sockaddr *)&addr, &addrlen);
5fafdf24 505 DEBUG_MISC((dfd, " did recvfrom %d, errno = %d-%s\n",
f0cbd3ec
FB
506 m->m_len, errno,strerror(errno)));
507 if(m->m_len<0) {
508 u_char code=ICMP_UNREACH_PORT;
509
510 if(errno == EHOSTUNREACH) code=ICMP_UNREACH_HOST;
511 else if(errno == ENETUNREACH) code=ICMP_UNREACH_NET;
3b46e624 512
f0cbd3ec 513 DEBUG_MISC((dfd," rx error, tx icmp ICMP_UNREACH:%i\n", code));
de40abfe 514 icmp_send_error(so->so_m, ICMP_UNREACH, code, 0, strerror(errno));
f0cbd3ec
FB
515 m_free(m);
516 } else {
517 /*
518 * Hack: domain name lookup will be used the most for UDP,
519 * and since they'll only be used once there's no need
520 * for the 4 minute (or whatever) timeout... So we time them
521 * out much quicker (10 seconds for now...)
522 */
523 if (so->so_expire) {
524 if (so->so_fport == htons(53))
525 so->so_expire = curtime + SO_EXPIREFAST;
526 else
527 so->so_expire = curtime + SO_EXPIRE;
528 }
529
5fafdf24 530 /*
f0cbd3ec 531 * If this packet was destined for CTL_ADDR,
5379229a 532 * make it look like that's where it came from
f0cbd3ec 533 */
5379229a
GS
534 saddr = addr;
535 sotranslate_in(so, &saddr);
536 daddr = so->lhost.ss;
537
eae303ff
GS
538 switch (so->so_ffamily) {
539 case AF_INET:
5379229a
GS
540 udp_output(so, m, (struct sockaddr_in *) &saddr,
541 (struct sockaddr_in *) &daddr,
542 so->so_iptos);
eae303ff
GS
543 break;
544 default:
545 break;
546 }
f0cbd3ec
FB
547 } /* rx error */
548 } /* if ping packet */
549}
550
551/*
552 * sendto() a socket
553 */
554int
511d2b14 555sosendto(struct socket *so, struct mbuf *m)
f0cbd3ec
FB
556{
557 int ret;
5379229a 558 struct sockaddr_storage addr;
f0cbd3ec
FB
559
560 DEBUG_CALL("sosendto");
ecc804ca
SW
561 DEBUG_ARG("so = %p", so);
562 DEBUG_ARG("m = %p", m);
5fafdf24 563
5379229a
GS
564 addr = so->fhost.ss;
565 DEBUG_CALL(" sendto()ing)");
566 sotranslate_out(so, &addr);
5fafdf24 567
f0cbd3ec
FB
568 /* Don't care what port we get */
569 ret = sendto(so->s, m->m_data, m->m_len, 0,
5379229a 570 (struct sockaddr *)&addr, sizeof(addr));
f0cbd3ec
FB
571 if (ret < 0)
572 return -1;
5fafdf24 573
f0cbd3ec
FB
574 /*
575 * Kill the socket if there's no reply in 4 minutes,
576 * but only if it's an expirable socket
577 */
578 if (so->so_expire)
579 so->so_expire = curtime + SO_EXPIRE;
f932b6ce
JK
580 so->so_state &= SS_PERSISTENT_MASK;
581 so->so_state |= SS_ISFCONNECTED; /* So that it gets select()ed */
f0cbd3ec
FB
582 return 0;
583}
584
585/*
3c6a0580 586 * Listen for incoming TCP connections
f0cbd3ec
FB
587 */
588struct socket *
b6dce92e 589tcp_listen(Slirp *slirp, uint32_t haddr, u_int hport, uint32_t laddr,
460fec67 590 u_int lport, int flags)
f0cbd3ec
FB
591{
592 struct sockaddr_in addr;
593 struct socket *so;
242acf3a
AZ
594 int s, opt = 1;
595 socklen_t addrlen = sizeof(addr);
ab07b980 596 memset(&addr, 0, addrlen);
f0cbd3ec 597
3c6a0580 598 DEBUG_CALL("tcp_listen");
9f349498
JK
599 DEBUG_ARG("haddr = %x", haddr);
600 DEBUG_ARG("hport = %d", hport);
f0cbd3ec
FB
601 DEBUG_ARG("laddr = %x", laddr);
602 DEBUG_ARG("lport = %d", lport);
603 DEBUG_ARG("flags = %x", flags);
5fafdf24 604
460fec67
JK
605 so = socreate(slirp);
606 if (!so) {
f0cbd3ec
FB
607 return NULL;
608 }
5fafdf24 609
f0cbd3ec
FB
610 /* Don't tcp_attach... we don't need so_snd nor so_rcv */
611 if ((so->so_tcpcb = tcp_newtcpcb(so)) == NULL) {
612 free(so);
613 return NULL;
614 }
460fec67 615 insque(so, &slirp->tcb);
5fafdf24
TS
616
617 /*
f0cbd3ec
FB
618 * SS_FACCEPTONCE sockets must time out.
619 */
620 if (flags & SS_FACCEPTONCE)
621 so->so_tcpcb->t_timer[TCPT_KEEP] = TCPTV_KEEP_INIT*2;
5fafdf24 622
f932b6ce
JK
623 so->so_state &= SS_PERSISTENT_MASK;
624 so->so_state |= (SS_FACCEPTCONN | flags);
eae303ff 625 so->so_lfamily = AF_INET;
f0cbd3ec
FB
626 so->so_lport = lport; /* Kept in network format */
627 so->so_laddr.s_addr = laddr; /* Ditto */
5fafdf24 628
f0cbd3ec 629 addr.sin_family = AF_INET;
3c6a0580
JK
630 addr.sin_addr.s_addr = haddr;
631 addr.sin_port = hport;
5fafdf24 632
40ff6d7e 633 if (((s = qemu_socket(AF_INET,SOCK_STREAM,0)) < 0) ||
aad1239a 634 (socket_set_fast_reuse(s) < 0) ||
f0cbd3ec
FB
635 (bind(s,(struct sockaddr *)&addr, sizeof(addr)) < 0) ||
636 (listen(s,1) < 0)) {
637 int tmperrno = errno; /* Don't clobber the real reason we failed */
3b46e624 638
f0cbd3ec
FB
639 close(s);
640 sofree(so);
641 /* Restore the real errno */
02d2c54c
FB
642#ifdef _WIN32
643 WSASetLastError(tmperrno);
644#else
f0cbd3ec 645 errno = tmperrno;
02d2c54c 646#endif
f0cbd3ec
FB
647 return NULL;
648 }
9957fc7f 649 qemu_setsockopt(s, SOL_SOCKET, SO_OOBINLINE, &opt, sizeof(int));
5fafdf24 650
f0cbd3ec 651 getsockname(s,(struct sockaddr *)&addr,&addrlen);
eae303ff 652 so->so_ffamily = AF_INET;
f0cbd3ec
FB
653 so->so_fport = addr.sin_port;
654 if (addr.sin_addr.s_addr == 0 || addr.sin_addr.s_addr == loopback_addr.s_addr)
460fec67 655 so->so_faddr = slirp->vhost_addr;
f0cbd3ec
FB
656 else
657 so->so_faddr = addr.sin_addr;
658
659 so->s = s;
660 return so;
661}
662
f0cbd3ec
FB
663/*
664 * Various session state calls
665 * XXX Should be #define's
666 * The socket state stuff needs work, these often get call 2 or 3
667 * times each when only 1 was needed
668 */
669void
511d2b14 670soisfconnecting(struct socket *so)
f0cbd3ec
FB
671{
672 so->so_state &= ~(SS_NOFDREF|SS_ISFCONNECTED|SS_FCANTRCVMORE|
673 SS_FCANTSENDMORE|SS_FWDRAIN);
674 so->so_state |= SS_ISFCONNECTING; /* Clobber other states */
675}
676
677void
511d2b14 678soisfconnected(struct socket *so)
f0cbd3ec
FB
679{
680 so->so_state &= ~(SS_ISFCONNECTING|SS_FWDRAIN|SS_NOFDREF);
681 so->so_state |= SS_ISFCONNECTED; /* Clobber other states */
682}
683
9634d903
BS
684static void
685sofcantrcvmore(struct socket *so)
f0cbd3ec
FB
686{
687 if ((so->so_state & SS_NOFDREF) == 0) {
688 shutdown(so->s,0);
f0cbd3ec
FB
689 }
690 so->so_state &= ~(SS_ISFCONNECTING);
f932b6ce
JK
691 if (so->so_state & SS_FCANTSENDMORE) {
692 so->so_state &= SS_PERSISTENT_MASK;
693 so->so_state |= SS_NOFDREF; /* Don't select it */
694 } else {
f0cbd3ec 695 so->so_state |= SS_FCANTRCVMORE;
f932b6ce 696 }
f0cbd3ec
FB
697}
698
9634d903
BS
699static void
700sofcantsendmore(struct socket *so)
f0cbd3ec
FB
701{
702 if ((so->so_state & SS_NOFDREF) == 0) {
02d2c54c 703 shutdown(so->s,1); /* send FIN to fhost */
f0cbd3ec
FB
704 }
705 so->so_state &= ~(SS_ISFCONNECTING);
f932b6ce
JK
706 if (so->so_state & SS_FCANTRCVMORE) {
707 so->so_state &= SS_PERSISTENT_MASK;
708 so->so_state |= SS_NOFDREF; /* as above */
709 } else {
f0cbd3ec 710 so->so_state |= SS_FCANTSENDMORE;
f932b6ce 711 }
f0cbd3ec
FB
712}
713
f0cbd3ec
FB
714/*
715 * Set write drain mode
716 * Set CANTSENDMORE once all data has been write()n
717 */
718void
511d2b14 719sofwdrain(struct socket *so)
f0cbd3ec
FB
720{
721 if (so->so_rcv.sb_cc)
722 so->so_state |= SS_FWDRAIN;
723 else
724 sofcantsendmore(so);
725}
5379229a
GS
726
727/*
728 * Translate addr in host addr when it is a virtual address
729 */
730void sotranslate_out(struct socket *so, struct sockaddr_storage *addr)
731{
732 Slirp *slirp = so->slirp;
733 struct sockaddr_in *sin = (struct sockaddr_in *)addr;
734
735 switch (addr->ss_family) {
736 case AF_INET:
737 if ((so->so_faddr.s_addr & slirp->vnetwork_mask.s_addr) ==
738 slirp->vnetwork_addr.s_addr) {
739 /* It's an alias */
740 if (so->so_faddr.s_addr == slirp->vnameserver_addr.s_addr) {
741 if (get_dns_addr(&sin->sin_addr) < 0) {
742 sin->sin_addr = loopback_addr;
743 }
744 } else {
745 sin->sin_addr = loopback_addr;
746 }
747 }
748
749 DEBUG_MISC((dfd, " addr.sin_port=%d, "
750 "addr.sin_addr.s_addr=%.16s\n",
751 ntohs(sin->sin_port), inet_ntoa(sin->sin_addr)));
752 break;
753
754 default:
755 break;
756 }
757}
758
759void sotranslate_in(struct socket *so, struct sockaddr_storage *addr)
760{
761 Slirp *slirp = so->slirp;
762 struct sockaddr_in *sin = (struct sockaddr_in *)addr;
763
764 switch (addr->ss_family) {
765 case AF_INET:
766 if ((so->so_faddr.s_addr & slirp->vnetwork_mask.s_addr) ==
767 slirp->vnetwork_addr.s_addr) {
768 uint32_t inv_mask = ~slirp->vnetwork_mask.s_addr;
769
770 if ((so->so_faddr.s_addr & inv_mask) == inv_mask) {
771 sin->sin_addr = slirp->vhost_addr;
772 } else if (sin->sin_addr.s_addr == loopback_addr.s_addr ||
773 so->so_faddr.s_addr != slirp->vhost_addr.s_addr) {
774 sin->sin_addr = so->so_faddr;
775 }
776 }
777 break;
778
779 default:
780 break;
781 }
782}
783
784/*
785 * Translate connections from localhost to the real hostname
786 */
787void sotranslate_accept(struct socket *so)
788{
789 Slirp *slirp = so->slirp;
790
791 switch (so->so_ffamily) {
792 case AF_INET:
793 if (so->so_faddr.s_addr == INADDR_ANY ||
794 (so->so_faddr.s_addr & loopback_mask) ==
795 (loopback_addr.s_addr & loopback_mask)) {
796 so->so_faddr = slirp->vhost_addr;
797 }
798 break;
799
800 default:
801 break;
802 }
803}