blob: 6c189713688cdf10ce25b01221d67a15e864ca0b [file] [log] [blame]
bellardf0cbd3e2004-04-22 00:10:48 +00001/*
2 * Copyright (c) 1995 Danny Gasparovski.
ths5fafdf22007-09-16 21:08:06 +00003 *
4 * Please read the file COPYRIGHT for the
bellardf0cbd3e2004-04-22 00:10:48 +00005 * terms and conditions of the copyright.
6 */
7
Peter Maydell7df74822016-01-29 17:49:59 +00008#include "qemu/osdep.h"
aliguorie1c5a2b2009-01-08 19:18:21 +00009#include "qemu-common.h"
Markus Armbrustera9c94272016-06-22 19:11:19 +020010#include "slirp.h"
bellardf0cbd3e2004-04-22 00:10:48 +000011#include "ip_icmp.h"
bellardec530c82006-04-25 22:36:06 +000012#ifdef __sun__
13#include <sys/filio.h>
14#endif
bellardf0cbd3e2004-04-22 00:10:48 +000015
blueswir19634d902007-10-26 19:01:16 +000016static void sofcantrcvmore(struct socket *so);
17static void sofcantsendmore(struct socket *so);
18
Guillaume Subiron8a87f122015-12-19 22:25:01 +010019struct socket *solookup(struct socket **last, struct socket *head,
20 struct sockaddr_storage *lhost, struct sockaddr_storage *fhost)
bellardf0cbd3e2004-04-22 00:10:48 +000021{
Guillaume Subirona5fd24a2015-12-19 22:25:00 +010022 struct socket *so = *last;
ths5fafdf22007-09-16 21:08:06 +000023
Guillaume Subirona5fd24a2015-12-19 22:25:00 +010024 /* Optimisation */
Guillaume Subiron8a87f122015-12-19 22:25:01 +010025 if (so != head && sockaddr_equal(&(so->lhost.ss), lhost)
26 && (!fhost || sockaddr_equal(&so->fhost.ss, fhost))) {
Guillaume Subirona5fd24a2015-12-19 22:25:00 +010027 return so;
28 }
ths5fafdf22007-09-16 21:08:06 +000029
Guillaume Subirona5fd24a2015-12-19 22:25:00 +010030 for (so = head->so_next; so != head; so = so->so_next) {
Guillaume Subiron8a87f122015-12-19 22:25:01 +010031 if (sockaddr_equal(&(so->lhost.ss), lhost)
32 && (!fhost || sockaddr_equal(&so->fhost.ss, fhost))) {
Guillaume Subirona5fd24a2015-12-19 22:25:00 +010033 *last = so;
34 return so;
35 }
36 }
ths5fafdf22007-09-16 21:08:06 +000037
Guillaume Subirona5fd24a2015-12-19 22:25:00 +010038 return (struct socket *)NULL;
bellardf0cbd3e2004-04-22 00:10:48 +000039}
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 *
Jan Kiszka460fec62009-06-24 14:42:31 +020047socreate(Slirp *slirp)
bellardf0cbd3e2004-04-22 00:10:48 +000048{
49 struct socket *so;
ths5fafdf22007-09-16 21:08:06 +000050
bellardf0cbd3e2004-04-22 00:10:48 +000051 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;
Jan Kiszka460fec62009-06-24 14:42:31 +020056 so->slirp = slirp;
Jan Kiszka7bd43ec2013-02-22 20:47:10 +010057 so->pollfds_idx = -1;
bellardf0cbd3e2004-04-22 00:10:48 +000058 }
59 return(so);
60}
61
62/*
63 * remque and free a socket, clobber cache
64 */
65void
blueswir1511d2b12009-03-07 15:32:56 +000066sofree(struct socket *so)
bellardf0cbd3e2004-04-22 00:10:48 +000067{
Jan Kiszka460fec62009-06-24 14:42:31 +020068 Slirp *slirp = so->slirp;
Samuel Thibaultea64d5f2016-11-13 23:54:27 +010069 struct mbuf *ifm;
70
71 for (ifm = (struct mbuf *) slirp->if_fastq.qh_link;
72 (struct quehead *) ifm != &slirp->if_fastq;
73 ifm = ifm->ifq_next) {
74 if (ifm->ifq_so == so) {
75 ifm->ifq_so = NULL;
76 }
77 }
78
79 for (ifm = (struct mbuf *) slirp->if_batchq.qh_link;
80 (struct quehead *) ifm != &slirp->if_batchq;
81 ifm = ifm->ifq_next) {
82 if (ifm->ifq_so == so) {
83 ifm->ifq_so = NULL;
84 }
85 }
Jan Kiszka460fec62009-06-24 14:42:31 +020086
bellardf0cbd3e2004-04-22 00:10:48 +000087 if (so->so_emu==EMU_RSH && so->extra) {
88 sofree(so->extra);
89 so->extra=NULL;
90 }
Jan Kiszka460fec62009-06-24 14:42:31 +020091 if (so == slirp->tcp_last_so) {
92 slirp->tcp_last_so = &slirp->tcb;
93 } else if (so == slirp->udp_last_so) {
94 slirp->udp_last_so = &slirp->udb;
Jan Kiszkae6d43cf2011-07-20 12:20:18 +020095 } else if (so == slirp->icmp_last_so) {
96 slirp->icmp_last_so = &slirp->icmp;
Jan Kiszka460fec62009-06-24 14:42:31 +020097 }
bellardf0cbd3e2004-04-22 00:10:48 +000098 m_free(so->so_m);
ths5fafdf22007-09-16 21:08:06 +000099
100 if(so->so_next && so->so_prev)
bellardf0cbd3e2004-04-22 00:10:48 +0000101 remque(so); /* crashes if so is not in a queue */
102
103 free(so);
104}
105
aliguorie1c5a2b2009-01-08 19:18:21 +0000106size_t sopreprbuf(struct socket *so, struct iovec *iov, int *np)
bellardf0cbd3e2004-04-22 00:10:48 +0000107{
aliguorie1c5a2b2009-01-08 19:18:21 +0000108 int n, lss, total;
bellardf0cbd3e2004-04-22 00:10:48 +0000109 struct sbuf *sb = &so->so_snd;
110 int len = sb->sb_datalen - sb->sb_cc;
bellardf0cbd3e2004-04-22 00:10:48 +0000111 int mss = so->so_tcpcb->t_maxseg;
ths5fafdf22007-09-16 21:08:06 +0000112
aliguorie1c5a2b2009-01-08 19:18:21 +0000113 DEBUG_CALL("sopreprbuf");
Stefan Weilecc804c2015-08-29 09:12:35 +0200114 DEBUG_ARG("so = %p", so);
ths5fafdf22007-09-16 21:08:06 +0000115
aliguorie1c5a2b2009-01-08 19:18:21 +0000116 if (len <= 0)
117 return 0;
118
bellardf0cbd3e2004-04-22 00:10:48 +0000119 iov[0].iov_base = sb->sb_wptr;
blueswir166029f62008-10-01 19:39:40 +0000120 iov[1].iov_base = NULL;
121 iov[1].iov_len = 0;
bellardf0cbd3e2004-04-22 00:10:48 +0000122 if (sb->sb_wptr < sb->sb_rptr) {
123 iov[0].iov_len = sb->sb_rptr - sb->sb_wptr;
124 /* Should never succeed, but... */
125 if (iov[0].iov_len > len)
126 iov[0].iov_len = len;
127 if (iov[0].iov_len > mss)
128 iov[0].iov_len -= iov[0].iov_len%mss;
129 n = 1;
130 } else {
131 iov[0].iov_len = (sb->sb_data + sb->sb_datalen) - sb->sb_wptr;
132 /* Should never succeed, but... */
133 if (iov[0].iov_len > len) iov[0].iov_len = len;
134 len -= iov[0].iov_len;
135 if (len) {
136 iov[1].iov_base = sb->sb_data;
137 iov[1].iov_len = sb->sb_rptr - sb->sb_data;
138 if(iov[1].iov_len > len)
139 iov[1].iov_len = len;
140 total = iov[0].iov_len + iov[1].iov_len;
141 if (total > mss) {
142 lss = total%mss;
143 if (iov[1].iov_len > lss) {
144 iov[1].iov_len -= lss;
145 n = 2;
146 } else {
147 lss -= iov[1].iov_len;
148 iov[0].iov_len -= lss;
149 n = 1;
150 }
151 } else
152 n = 2;
153 } else {
154 if (iov[0].iov_len > mss)
155 iov[0].iov_len -= iov[0].iov_len%mss;
156 n = 1;
157 }
158 }
aliguorie1c5a2b2009-01-08 19:18:21 +0000159 if (np)
160 *np = n;
161
162 return iov[0].iov_len + (n - 1) * iov[1].iov_len;
163}
164
165/*
166 * Read from so's socket into sb_snd, updating all relevant sbuf fields
167 * NOTE: This will only be called if it is select()ed for reading, so
168 * a read() of 0 (or less) means it's disconnected
169 */
170int
blueswir1511d2b12009-03-07 15:32:56 +0000171soread(struct socket *so)
aliguorie1c5a2b2009-01-08 19:18:21 +0000172{
173 int n, nn;
174 struct sbuf *sb = &so->so_snd;
175 struct iovec iov[2];
176
177 DEBUG_CALL("soread");
Stefan Weilecc804c2015-08-29 09:12:35 +0200178 DEBUG_ARG("so = %p", so);
aliguorie1c5a2b2009-01-08 19:18:21 +0000179
180 /*
181 * No need to check if there's enough room to read.
182 * soread wouldn't have been called if there weren't
183 */
184 sopreprbuf(so, iov, &n);
ths5fafdf22007-09-16 21:08:06 +0000185
bellardf0cbd3e2004-04-22 00:10:48 +0000186#ifdef HAVE_READV
187 nn = readv(so->s, (struct iovec *)iov, n);
188 DEBUG_MISC((dfd, " ... read nn = %d bytes\n", nn));
189#else
Blue Swirl00aa0042011-07-23 20:04:29 +0000190 nn = qemu_recv(so->s, iov[0].iov_base, iov[0].iov_len,0);
ths5fafdf22007-09-16 21:08:06 +0000191#endif
bellardf0cbd3e2004-04-22 00:10:48 +0000192 if (nn <= 0) {
193 if (nn < 0 && (errno == EINTR || errno == EAGAIN))
194 return 0;
195 else {
Edgar E. Iglesias27d92eb2016-04-06 22:04:43 -0700196 int err;
197 socklen_t slen = sizeof err;
198
199 err = errno;
200 if (nn == 0) {
201 getsockopt(so->s, SOL_SOCKET, SO_ERROR,
202 &err, &slen);
203 }
204
bellardf0cbd3e2004-04-22 00:10:48 +0000205 DEBUG_MISC((dfd, " --- soread() disconnected, nn = %d, errno = %d-%s\n", nn, errno,strerror(errno)));
206 sofcantrcvmore(so);
Edgar E. Iglesias27d92eb2016-04-06 22:04:43 -0700207
Steven Luo6625d832016-04-06 22:04:55 -0700208 if (err == ECONNRESET || err == ECONNREFUSED
Edgar E. Iglesias27d92eb2016-04-06 22:04:43 -0700209 || err == ENOTCONN || err == EPIPE) {
210 tcp_drop(sototcpcb(so), err);
211 } else {
212 tcp_sockclosed(sototcpcb(so));
213 }
bellardf0cbd3e2004-04-22 00:10:48 +0000214 return -1;
215 }
216 }
ths5fafdf22007-09-16 21:08:06 +0000217
bellardf0cbd3e2004-04-22 00:10:48 +0000218#ifndef HAVE_READV
219 /*
220 * If there was no error, try and read the second time round
221 * We read again if n = 2 (ie, there's another part of the buffer)
222 * and we read as much as we could in the first read
223 * We don't test for <= 0 this time, because there legitimately
224 * might not be any more data (since the socket is non-blocking),
225 * a close will be detected on next iteration.
Stefan Weilcb8d4c82016-03-23 15:59:57 +0100226 * A return of -1 won't (shouldn't) happen, since it didn't happen above
bellardf0cbd3e2004-04-22 00:10:48 +0000227 */
bellard17444c92004-11-21 20:02:08 +0000228 if (n == 2 && nn == iov[0].iov_len) {
229 int ret;
Blue Swirl00aa0042011-07-23 20:04:29 +0000230 ret = qemu_recv(so->s, iov[1].iov_base, iov[1].iov_len,0);
bellard17444c92004-11-21 20:02:08 +0000231 if (ret > 0)
232 nn += ret;
233 }
ths5fafdf22007-09-16 21:08:06 +0000234
bellardf0cbd3e2004-04-22 00:10:48 +0000235 DEBUG_MISC((dfd, " ... read nn = %d bytes\n", nn));
236#endif
ths5fafdf22007-09-16 21:08:06 +0000237
bellardf0cbd3e2004-04-22 00:10:48 +0000238 /* Update fields */
239 sb->sb_cc += nn;
240 sb->sb_wptr += nn;
241 if (sb->sb_wptr >= (sb->sb_data + sb->sb_datalen))
242 sb->sb_wptr -= sb->sb_datalen;
243 return nn;
244}
ths5fafdf22007-09-16 21:08:06 +0000245
aliguorie1c5a2b2009-01-08 19:18:21 +0000246int soreadbuf(struct socket *so, const char *buf, int size)
247{
248 int n, nn, copy = size;
249 struct sbuf *sb = &so->so_snd;
250 struct iovec iov[2];
251
252 DEBUG_CALL("soreadbuf");
Stefan Weilecc804c2015-08-29 09:12:35 +0200253 DEBUG_ARG("so = %p", so);
aliguorie1c5a2b2009-01-08 19:18:21 +0000254
255 /*
256 * No need to check if there's enough room to read.
257 * soread wouldn't have been called if there weren't
258 */
259 if (sopreprbuf(so, iov, &n) < size)
260 goto err;
261
262 nn = MIN(iov[0].iov_len, copy);
263 memcpy(iov[0].iov_base, buf, nn);
264
265 copy -= nn;
266 buf += nn;
267
268 if (copy == 0)
269 goto done;
270
271 memcpy(iov[1].iov_base, buf, copy);
272
273done:
274 /* Update fields */
275 sb->sb_cc += size;
276 sb->sb_wptr += size;
277 if (sb->sb_wptr >= (sb->sb_data + sb->sb_datalen))
278 sb->sb_wptr -= sb->sb_datalen;
279 return size;
280err:
281
282 sofcantrcvmore(so);
283 tcp_sockclosed(sototcpcb(so));
284 fprintf(stderr, "soreadbuf buffer to small");
285 return -1;
286}
287
bellardf0cbd3e2004-04-22 00:10:48 +0000288/*
289 * Get urgent data
ths5fafdf22007-09-16 21:08:06 +0000290 *
bellardf0cbd3e2004-04-22 00:10:48 +0000291 * When the socket is created, we set it SO_OOBINLINE,
292 * so when OOB data arrives, we soread() it and everything
293 * in the send buffer is sent as urgent data
294 */
Steven Luobfb1ac12016-04-06 22:04:32 -0700295int
blueswir1511d2b12009-03-07 15:32:56 +0000296sorecvoob(struct socket *so)
bellardf0cbd3e2004-04-22 00:10:48 +0000297{
298 struct tcpcb *tp = sototcpcb(so);
Steven Luobfb1ac12016-04-06 22:04:32 -0700299 int ret;
bellardf0cbd3e2004-04-22 00:10:48 +0000300
301 DEBUG_CALL("sorecvoob");
Stefan Weilecc804c2015-08-29 09:12:35 +0200302 DEBUG_ARG("so = %p", so);
ths5fafdf22007-09-16 21:08:06 +0000303
bellardf0cbd3e2004-04-22 00:10:48 +0000304 /*
305 * We take a guess at how much urgent data has arrived.
306 * In most situations, when urgent data arrives, the next
307 * read() should get all the urgent data. This guess will
308 * be wrong however if more data arrives just after the
ths5fafdf22007-09-16 21:08:06 +0000309 * urgent data, or the read() doesn't return all the
bellardf0cbd3e2004-04-22 00:10:48 +0000310 * urgent data.
311 */
Steven Luobfb1ac12016-04-06 22:04:32 -0700312 ret = soread(so);
313 if (ret > 0) {
314 tp->snd_up = tp->snd_una + so->so_snd.sb_cc;
315 tp->t_force = 1;
316 tcp_output(tp);
317 tp->t_force = 0;
318 }
319
320 return ret;
bellardf0cbd3e2004-04-22 00:10:48 +0000321}
322
323/*
324 * Send urgent data
325 * There's a lot duplicated code here, but...
326 */
327int
blueswir1511d2b12009-03-07 15:32:56 +0000328sosendoob(struct socket *so)
bellardf0cbd3e2004-04-22 00:10:48 +0000329{
330 struct sbuf *sb = &so->so_rcv;
331 char buff[2048]; /* XXX Shouldn't be sending more oob data than this */
ths5fafdf22007-09-16 21:08:06 +0000332
bellardf0cbd3e2004-04-22 00:10:48 +0000333 int n, len;
ths5fafdf22007-09-16 21:08:06 +0000334
bellardf0cbd3e2004-04-22 00:10:48 +0000335 DEBUG_CALL("sosendoob");
Stefan Weilecc804c2015-08-29 09:12:35 +0200336 DEBUG_ARG("so = %p", so);
bellardf0cbd3e2004-04-22 00:10:48 +0000337 DEBUG_ARG("sb->sb_cc = %d", sb->sb_cc);
ths5fafdf22007-09-16 21:08:06 +0000338
bellardf0cbd3e2004-04-22 00:10:48 +0000339 if (so->so_urgc > 2048)
340 so->so_urgc = 2048; /* XXXX */
ths5fafdf22007-09-16 21:08:06 +0000341
bellardf0cbd3e2004-04-22 00:10:48 +0000342 if (sb->sb_rptr < sb->sb_wptr) {
343 /* We can send it directly */
aliguorie1c5a2b2009-01-08 19:18:21 +0000344 n = slirp_send(so, sb->sb_rptr, so->so_urgc, (MSG_OOB)); /* |MSG_DONTWAIT)); */
bellardf0cbd3e2004-04-22 00:10:48 +0000345 so->so_urgc -= n;
ths3b46e622007-09-17 08:09:54 +0000346
bellardf0cbd3e2004-04-22 00:10:48 +0000347 DEBUG_MISC((dfd, " --- sent %d bytes urgent data, %d urgent bytes left\n", n, so->so_urgc));
348 } else {
ths5fafdf22007-09-16 21:08:06 +0000349 /*
bellardf0cbd3e2004-04-22 00:10:48 +0000350 * Since there's no sendv or sendtov like writev,
351 * we must copy all data to a linear buffer then
352 * send it all
353 */
354 len = (sb->sb_data + sb->sb_datalen) - sb->sb_rptr;
355 if (len > so->so_urgc) len = so->so_urgc;
356 memcpy(buff, sb->sb_rptr, len);
357 so->so_urgc -= len;
358 if (so->so_urgc) {
359 n = sb->sb_wptr - sb->sb_data;
360 if (n > so->so_urgc) n = so->so_urgc;
361 memcpy((buff + len), sb->sb_data, n);
362 so->so_urgc -= n;
363 len += n;
364 }
aliguorie1c5a2b2009-01-08 19:18:21 +0000365 n = slirp_send(so, buff, len, (MSG_OOB)); /* |MSG_DONTWAIT)); */
bellardf0cbd3e2004-04-22 00:10:48 +0000366#ifdef DEBUG
367 if (n != len)
368 DEBUG_ERROR((dfd, "Didn't send all data urgently XXXXX\n"));
ths3b46e622007-09-17 08:09:54 +0000369#endif
bellardf0cbd3e2004-04-22 00:10:48 +0000370 DEBUG_MISC((dfd, " ---2 sent %d bytes urgent data, %d urgent bytes left\n", n, so->so_urgc));
371 }
ths5fafdf22007-09-16 21:08:06 +0000372
bellardf0cbd3e2004-04-22 00:10:48 +0000373 sb->sb_cc -= n;
374 sb->sb_rptr += n;
375 if (sb->sb_rptr >= (sb->sb_data + sb->sb_datalen))
376 sb->sb_rptr -= sb->sb_datalen;
ths5fafdf22007-09-16 21:08:06 +0000377
bellardf0cbd3e2004-04-22 00:10:48 +0000378 return n;
379}
380
381/*
ths5fafdf22007-09-16 21:08:06 +0000382 * Write data from so_rcv to so's socket,
bellardf0cbd3e2004-04-22 00:10:48 +0000383 * updating all sbuf field as necessary
384 */
385int
blueswir1511d2b12009-03-07 15:32:56 +0000386sowrite(struct socket *so)
bellardf0cbd3e2004-04-22 00:10:48 +0000387{
388 int n,nn;
389 struct sbuf *sb = &so->so_rcv;
390 int len = sb->sb_cc;
391 struct iovec iov[2];
ths5fafdf22007-09-16 21:08:06 +0000392
bellardf0cbd3e2004-04-22 00:10:48 +0000393 DEBUG_CALL("sowrite");
Stefan Weilecc804c2015-08-29 09:12:35 +0200394 DEBUG_ARG("so = %p", so);
ths5fafdf22007-09-16 21:08:06 +0000395
bellardf0cbd3e2004-04-22 00:10:48 +0000396 if (so->so_urgc) {
397 sosendoob(so);
398 if (sb->sb_cc == 0)
399 return 0;
400 }
401
402 /*
403 * No need to check if there's something to write,
404 * sowrite wouldn't have been called otherwise
405 */
ths5fafdf22007-09-16 21:08:06 +0000406
bellardf0cbd3e2004-04-22 00:10:48 +0000407 iov[0].iov_base = sb->sb_rptr;
blueswir166029f62008-10-01 19:39:40 +0000408 iov[1].iov_base = NULL;
409 iov[1].iov_len = 0;
bellardf0cbd3e2004-04-22 00:10:48 +0000410 if (sb->sb_rptr < sb->sb_wptr) {
411 iov[0].iov_len = sb->sb_wptr - sb->sb_rptr;
412 /* Should never succeed, but... */
413 if (iov[0].iov_len > len) iov[0].iov_len = len;
414 n = 1;
415 } else {
416 iov[0].iov_len = (sb->sb_data + sb->sb_datalen) - sb->sb_rptr;
417 if (iov[0].iov_len > len) iov[0].iov_len = len;
418 len -= iov[0].iov_len;
419 if (len) {
420 iov[1].iov_base = sb->sb_data;
421 iov[1].iov_len = sb->sb_wptr - sb->sb_data;
422 if (iov[1].iov_len > len) iov[1].iov_len = len;
423 n = 2;
424 } else
425 n = 1;
426 }
427 /* Check if there's urgent data to send, and if so, send it */
428
429#ifdef HAVE_READV
430 nn = writev(so->s, (const struct iovec *)iov, n);
ths5fafdf22007-09-16 21:08:06 +0000431
bellardf0cbd3e2004-04-22 00:10:48 +0000432 DEBUG_MISC((dfd, " ... wrote nn = %d bytes\n", nn));
433#else
aliguorie1c5a2b2009-01-08 19:18:21 +0000434 nn = slirp_send(so, iov[0].iov_base, iov[0].iov_len,0);
bellardf0cbd3e2004-04-22 00:10:48 +0000435#endif
436 /* This should never happen, but people tell me it does *shrug* */
437 if (nn < 0 && (errno == EAGAIN || errno == EINTR))
438 return 0;
ths5fafdf22007-09-16 21:08:06 +0000439
bellardf0cbd3e2004-04-22 00:10:48 +0000440 if (nn <= 0) {
441 DEBUG_MISC((dfd, " --- sowrite disconnected, so->so_state = %x, errno = %d\n",
442 so->so_state, errno));
443 sofcantsendmore(so);
444 tcp_sockclosed(sototcpcb(so));
445 return -1;
446 }
ths5fafdf22007-09-16 21:08:06 +0000447
bellardf0cbd3e2004-04-22 00:10:48 +0000448#ifndef HAVE_READV
bellard3bc21752004-11-24 20:39:26 +0000449 if (n == 2 && nn == iov[0].iov_len) {
450 int ret;
aliguorie1c5a2b2009-01-08 19:18:21 +0000451 ret = slirp_send(so, iov[1].iov_base, iov[1].iov_len,0);
bellard3bc21752004-11-24 20:39:26 +0000452 if (ret > 0)
453 nn += ret;
454 }
bellardf0cbd3e2004-04-22 00:10:48 +0000455 DEBUG_MISC((dfd, " ... wrote nn = %d bytes\n", nn));
456#endif
ths5fafdf22007-09-16 21:08:06 +0000457
bellardf0cbd3e2004-04-22 00:10:48 +0000458 /* Update sbuf */
459 sb->sb_cc -= nn;
460 sb->sb_rptr += nn;
461 if (sb->sb_rptr >= (sb->sb_data + sb->sb_datalen))
462 sb->sb_rptr -= sb->sb_datalen;
ths5fafdf22007-09-16 21:08:06 +0000463
bellardf0cbd3e2004-04-22 00:10:48 +0000464 /*
465 * If in DRAIN mode, and there's no more data, set
466 * it CANTSENDMORE
467 */
468 if ((so->so_state & SS_FWDRAIN) && sb->sb_cc == 0)
469 sofcantsendmore(so);
ths5fafdf22007-09-16 21:08:06 +0000470
bellardf0cbd3e2004-04-22 00:10:48 +0000471 return nn;
472}
473
474/*
475 * recvfrom() a UDP socket
476 */
477void
blueswir1511d2b12009-03-07 15:32:56 +0000478sorecvfrom(struct socket *so)
bellardf0cbd3e2004-04-22 00:10:48 +0000479{
Guillaume Subironeae303f2015-12-19 22:24:58 +0100480 struct sockaddr_storage addr;
Guillaume Subiron53792292015-12-19 22:24:59 +0100481 struct sockaddr_storage saddr, daddr;
Guillaume Subironeae303f2015-12-19 22:24:58 +0100482 socklen_t addrlen = sizeof(struct sockaddr_storage);
ths5fafdf22007-09-16 21:08:06 +0000483
bellardf0cbd3e2004-04-22 00:10:48 +0000484 DEBUG_CALL("sorecvfrom");
Stefan Weilecc804c2015-08-29 09:12:35 +0200485 DEBUG_ARG("so = %p", so);
ths5fafdf22007-09-16 21:08:06 +0000486
bellardf0cbd3e2004-04-22 00:10:48 +0000487 if (so->so_type == IPPROTO_ICMP) { /* This is a "ping" reply */
488 char buff[256];
489 int len;
ths3b46e622007-09-17 08:09:54 +0000490
ths5fafdf22007-09-16 21:08:06 +0000491 len = recvfrom(so->s, buff, 256, 0,
bellardf0cbd3e2004-04-22 00:10:48 +0000492 (struct sockaddr *)&addr, &addrlen);
493 /* XXX Check if reply is "correct"? */
ths3b46e622007-09-17 08:09:54 +0000494
bellardf0cbd3e2004-04-22 00:10:48 +0000495 if(len == -1 || len == 0) {
496 u_char code=ICMP_UNREACH_PORT;
497
498 if(errno == EHOSTUNREACH) code=ICMP_UNREACH_HOST;
499 else if(errno == ENETUNREACH) code=ICMP_UNREACH_NET;
ths3b46e622007-09-17 08:09:54 +0000500
bellardf0cbd3e2004-04-22 00:10:48 +0000501 DEBUG_MISC((dfd," udp icmp rx errno = %d-%s\n",
502 errno,strerror(errno)));
Yann Bordenavede40abf2016-03-15 10:31:19 +0100503 icmp_send_error(so->so_m, ICMP_UNREACH, code, 0, strerror(errno));
bellardf0cbd3e2004-04-22 00:10:48 +0000504 } else {
505 icmp_reflect(so->so_m);
blueswir1511d2b12009-03-07 15:32:56 +0000506 so->so_m = NULL; /* Don't m_free() it again! */
bellardf0cbd3e2004-04-22 00:10:48 +0000507 }
508 /* No need for this socket anymore, udp_detach it */
509 udp_detach(so);
510 } else { /* A "normal" UDP packet */
511 struct mbuf *m;
Blue Swirlc5b76b32009-06-13 08:44:31 +0000512 int len;
513#ifdef _WIN32
514 unsigned long n;
515#else
516 int n;
517#endif
bellardf0cbd3e2004-04-22 00:10:48 +0000518
Jan Kiszka460fec62009-06-24 14:42:31 +0200519 m = m_get(so->slirp);
520 if (!m) {
521 return;
522 }
Guillaume Subiron98c63052016-03-15 10:31:20 +0100523 switch (so->so_ffamily) {
524 case AF_INET:
525 m->m_data += IF_MAXLINKHDR + sizeof(struct udpiphdr);
526 break;
527 case AF_INET6:
528 m->m_data += IF_MAXLINKHDR + sizeof(struct ip6)
529 + sizeof(struct udphdr);
530 break;
531 default:
532 g_assert_not_reached();
533 break;
534 }
ths3b46e622007-09-17 08:09:54 +0000535
ths5fafdf22007-09-16 21:08:06 +0000536 /*
bellardf0cbd3e2004-04-22 00:10:48 +0000537 * XXX Shouldn't FIONREAD packets destined for port 53,
538 * but I don't know the max packet size for DNS lookups
539 */
540 len = M_FREEROOM(m);
541 /* if (so->so_fport != htons(53)) { */
bellard379ff532004-07-12 22:33:07 +0000542 ioctlsocket(so->s, FIONREAD, &n);
ths3b46e622007-09-17 08:09:54 +0000543
bellardf0cbd3e2004-04-22 00:10:48 +0000544 if (n > len) {
545 n = (m->m_data - m->m_dat) + m->m_len + n + 1;
546 m_inc(m, n);
547 len = M_FREEROOM(m);
548 }
549 /* } */
ths3b46e622007-09-17 08:09:54 +0000550
bellardf0cbd3e2004-04-22 00:10:48 +0000551 m->m_len = recvfrom(so->s, m->m_data, len, 0,
552 (struct sockaddr *)&addr, &addrlen);
ths5fafdf22007-09-16 21:08:06 +0000553 DEBUG_MISC((dfd, " did recvfrom %d, errno = %d-%s\n",
bellardf0cbd3e2004-04-22 00:10:48 +0000554 m->m_len, errno,strerror(errno)));
555 if(m->m_len<0) {
Guillaume Subiron15d62af2016-03-15 10:31:20 +0100556 /* Report error as ICMP */
557 switch (so->so_lfamily) {
558 uint8_t code;
559 case AF_INET:
560 code = ICMP_UNREACH_PORT;
bellardf0cbd3e2004-04-22 00:10:48 +0000561
Guillaume Subiron15d62af2016-03-15 10:31:20 +0100562 if (errno == EHOSTUNREACH) {
563 code = ICMP_UNREACH_HOST;
564 } else if (errno == ENETUNREACH) {
565 code = ICMP_UNREACH_NET;
566 }
ths3b46e622007-09-17 08:09:54 +0000567
Guillaume Subiron15d62af2016-03-15 10:31:20 +0100568 DEBUG_MISC((dfd, " rx error, tx icmp ICMP_UNREACH:%i\n", code));
569 icmp_send_error(so->so_m, ICMP_UNREACH, code, 0, strerror(errno));
570 break;
571 case AF_INET6:
572 code = ICMP6_UNREACH_PORT;
573
574 if (errno == EHOSTUNREACH) {
575 code = ICMP6_UNREACH_ADDRESS;
576 } else if (errno == ENETUNREACH) {
577 code = ICMP6_UNREACH_NO_ROUTE;
578 }
579
580 DEBUG_MISC((dfd, " rx error, tx icmp6 ICMP_UNREACH:%i\n", code));
581 icmp6_send_error(so->so_m, ICMP6_UNREACH, code);
582 break;
583 default:
584 g_assert_not_reached();
585 break;
586 }
bellardf0cbd3e2004-04-22 00:10:48 +0000587 m_free(m);
588 } else {
589 /*
590 * Hack: domain name lookup will be used the most for UDP,
591 * and since they'll only be used once there's no need
592 * for the 4 minute (or whatever) timeout... So we time them
593 * out much quicker (10 seconds for now...)
594 */
595 if (so->so_expire) {
596 if (so->so_fport == htons(53))
597 so->so_expire = curtime + SO_EXPIREFAST;
598 else
599 so->so_expire = curtime + SO_EXPIRE;
600 }
601
ths5fafdf22007-09-16 21:08:06 +0000602 /*
bellardf0cbd3e2004-04-22 00:10:48 +0000603 * If this packet was destined for CTL_ADDR,
Guillaume Subiron53792292015-12-19 22:24:59 +0100604 * make it look like that's where it came from
bellardf0cbd3e2004-04-22 00:10:48 +0000605 */
Guillaume Subiron53792292015-12-19 22:24:59 +0100606 saddr = addr;
607 sotranslate_in(so, &saddr);
608 daddr = so->lhost.ss;
609
Guillaume Subironeae303f2015-12-19 22:24:58 +0100610 switch (so->so_ffamily) {
611 case AF_INET:
Guillaume Subiron53792292015-12-19 22:24:59 +0100612 udp_output(so, m, (struct sockaddr_in *) &saddr,
613 (struct sockaddr_in *) &daddr,
614 so->so_iptos);
Guillaume Subironeae303f2015-12-19 22:24:58 +0100615 break;
Guillaume Subiron15d62af2016-03-15 10:31:20 +0100616 case AF_INET6:
617 udp6_output(so, m, (struct sockaddr_in6 *) &saddr,
618 (struct sockaddr_in6 *) &daddr);
619 break;
Guillaume Subironeae303f2015-12-19 22:24:58 +0100620 default:
Guillaume Subiron15d62af2016-03-15 10:31:20 +0100621 g_assert_not_reached();
Guillaume Subironeae303f2015-12-19 22:24:58 +0100622 break;
623 }
bellardf0cbd3e2004-04-22 00:10:48 +0000624 } /* rx error */
625 } /* if ping packet */
626}
627
628/*
629 * sendto() a socket
630 */
631int
blueswir1511d2b12009-03-07 15:32:56 +0000632sosendto(struct socket *so, struct mbuf *m)
bellardf0cbd3e2004-04-22 00:10:48 +0000633{
634 int ret;
Guillaume Subiron53792292015-12-19 22:24:59 +0100635 struct sockaddr_storage addr;
bellardf0cbd3e2004-04-22 00:10:48 +0000636
637 DEBUG_CALL("sosendto");
Stefan Weilecc804c2015-08-29 09:12:35 +0200638 DEBUG_ARG("so = %p", so);
639 DEBUG_ARG("m = %p", m);
ths5fafdf22007-09-16 21:08:06 +0000640
Guillaume Subiron53792292015-12-19 22:24:59 +0100641 addr = so->fhost.ss;
642 DEBUG_CALL(" sendto()ing)");
643 sotranslate_out(so, &addr);
ths5fafdf22007-09-16 21:08:06 +0000644
bellardf0cbd3e2004-04-22 00:10:48 +0000645 /* Don't care what port we get */
646 ret = sendto(so->s, m->m_data, m->m_len, 0,
Samuel Thibault0d48dfe2016-04-28 18:53:08 +0200647 (struct sockaddr *)&addr, sockaddr_size(&addr));
bellardf0cbd3e2004-04-22 00:10:48 +0000648 if (ret < 0)
649 return -1;
ths5fafdf22007-09-16 21:08:06 +0000650
bellardf0cbd3e2004-04-22 00:10:48 +0000651 /*
652 * Kill the socket if there's no reply in 4 minutes,
653 * but only if it's an expirable socket
654 */
655 if (so->so_expire)
656 so->so_expire = curtime + SO_EXPIRE;
Jan Kiszkaf932b6c2009-06-24 14:42:29 +0200657 so->so_state &= SS_PERSISTENT_MASK;
658 so->so_state |= SS_ISFCONNECTED; /* So that it gets select()ed */
bellardf0cbd3e2004-04-22 00:10:48 +0000659 return 0;
660}
661
662/*
Jan Kiszka3c6a0582009-06-24 14:42:28 +0200663 * Listen for incoming TCP connections
bellardf0cbd3e2004-04-22 00:10:48 +0000664 */
665struct socket *
Stefan Weilb6dce922010-07-22 22:15:23 +0200666tcp_listen(Slirp *slirp, uint32_t haddr, u_int hport, uint32_t laddr,
Jan Kiszka460fec62009-06-24 14:42:31 +0200667 u_int lport, int flags)
bellardf0cbd3e2004-04-22 00:10:48 +0000668{
669 struct sockaddr_in addr;
670 struct socket *so;
balrog242acf32008-05-10 01:49:53 +0000671 int s, opt = 1;
672 socklen_t addrlen = sizeof(addr);
Juha Riihimäkiab07b982010-04-13 09:16:55 +0300673 memset(&addr, 0, addrlen);
bellardf0cbd3e2004-04-22 00:10:48 +0000674
Jan Kiszka3c6a0582009-06-24 14:42:28 +0200675 DEBUG_CALL("tcp_listen");
Jan Kiszka9f349492009-06-24 14:42:29 +0200676 DEBUG_ARG("haddr = %x", haddr);
677 DEBUG_ARG("hport = %d", hport);
bellardf0cbd3e2004-04-22 00:10:48 +0000678 DEBUG_ARG("laddr = %x", laddr);
679 DEBUG_ARG("lport = %d", lport);
680 DEBUG_ARG("flags = %x", flags);
ths5fafdf22007-09-16 21:08:06 +0000681
Jan Kiszka460fec62009-06-24 14:42:31 +0200682 so = socreate(slirp);
683 if (!so) {
bellardf0cbd3e2004-04-22 00:10:48 +0000684 return NULL;
685 }
ths5fafdf22007-09-16 21:08:06 +0000686
bellardf0cbd3e2004-04-22 00:10:48 +0000687 /* Don't tcp_attach... we don't need so_snd nor so_rcv */
688 if ((so->so_tcpcb = tcp_newtcpcb(so)) == NULL) {
689 free(so);
690 return NULL;
691 }
Jan Kiszka460fec62009-06-24 14:42:31 +0200692 insque(so, &slirp->tcb);
ths5fafdf22007-09-16 21:08:06 +0000693
694 /*
bellardf0cbd3e2004-04-22 00:10:48 +0000695 * SS_FACCEPTONCE sockets must time out.
696 */
697 if (flags & SS_FACCEPTONCE)
698 so->so_tcpcb->t_timer[TCPT_KEEP] = TCPTV_KEEP_INIT*2;
ths5fafdf22007-09-16 21:08:06 +0000699
Jan Kiszkaf932b6c2009-06-24 14:42:29 +0200700 so->so_state &= SS_PERSISTENT_MASK;
701 so->so_state |= (SS_FACCEPTCONN | flags);
Guillaume Subironeae303f2015-12-19 22:24:58 +0100702 so->so_lfamily = AF_INET;
bellardf0cbd3e2004-04-22 00:10:48 +0000703 so->so_lport = lport; /* Kept in network format */
704 so->so_laddr.s_addr = laddr; /* Ditto */
ths5fafdf22007-09-16 21:08:06 +0000705
bellardf0cbd3e2004-04-22 00:10:48 +0000706 addr.sin_family = AF_INET;
Jan Kiszka3c6a0582009-06-24 14:42:28 +0200707 addr.sin_addr.s_addr = haddr;
708 addr.sin_port = hport;
ths5fafdf22007-09-16 21:08:06 +0000709
Kevin Wolf40ff6d72009-12-02 12:24:42 +0100710 if (((s = qemu_socket(AF_INET,SOCK_STREAM,0)) < 0) ||
Sebastian Ottlikaad12392013-10-02 12:23:15 +0200711 (socket_set_fast_reuse(s) < 0) ||
bellardf0cbd3e2004-04-22 00:10:48 +0000712 (bind(s,(struct sockaddr *)&addr, sizeof(addr)) < 0) ||
713 (listen(s,1) < 0)) {
714 int tmperrno = errno; /* Don't clobber the real reason we failed */
ths3b46e622007-09-17 08:09:54 +0000715
bellardf0cbd3e2004-04-22 00:10:48 +0000716 close(s);
717 sofree(so);
718 /* Restore the real errno */
bellard02d2c542004-10-07 23:27:35 +0000719#ifdef _WIN32
720 WSASetLastError(tmperrno);
721#else
bellardf0cbd3e2004-04-22 00:10:48 +0000722 errno = tmperrno;
bellard02d2c542004-10-07 23:27:35 +0000723#endif
bellardf0cbd3e2004-04-22 00:10:48 +0000724 return NULL;
725 }
Stefan Weil9957fc72013-03-08 19:58:32 +0100726 qemu_setsockopt(s, SOL_SOCKET, SO_OOBINLINE, &opt, sizeof(int));
ths5fafdf22007-09-16 21:08:06 +0000727
bellardf0cbd3e2004-04-22 00:10:48 +0000728 getsockname(s,(struct sockaddr *)&addr,&addrlen);
Guillaume Subironeae303f2015-12-19 22:24:58 +0100729 so->so_ffamily = AF_INET;
bellardf0cbd3e2004-04-22 00:10:48 +0000730 so->so_fport = addr.sin_port;
731 if (addr.sin_addr.s_addr == 0 || addr.sin_addr.s_addr == loopback_addr.s_addr)
Jan Kiszka460fec62009-06-24 14:42:31 +0200732 so->so_faddr = slirp->vhost_addr;
bellardf0cbd3e2004-04-22 00:10:48 +0000733 else
734 so->so_faddr = addr.sin_addr;
735
736 so->s = s;
737 return so;
738}
739
bellardf0cbd3e2004-04-22 00:10:48 +0000740/*
741 * Various session state calls
742 * XXX Should be #define's
743 * The socket state stuff needs work, these often get call 2 or 3
744 * times each when only 1 was needed
745 */
746void
blueswir1511d2b12009-03-07 15:32:56 +0000747soisfconnecting(struct socket *so)
bellardf0cbd3e2004-04-22 00:10:48 +0000748{
749 so->so_state &= ~(SS_NOFDREF|SS_ISFCONNECTED|SS_FCANTRCVMORE|
750 SS_FCANTSENDMORE|SS_FWDRAIN);
751 so->so_state |= SS_ISFCONNECTING; /* Clobber other states */
752}
753
754void
blueswir1511d2b12009-03-07 15:32:56 +0000755soisfconnected(struct socket *so)
bellardf0cbd3e2004-04-22 00:10:48 +0000756{
757 so->so_state &= ~(SS_ISFCONNECTING|SS_FWDRAIN|SS_NOFDREF);
758 so->so_state |= SS_ISFCONNECTED; /* Clobber other states */
759}
760
blueswir19634d902007-10-26 19:01:16 +0000761static void
762sofcantrcvmore(struct socket *so)
bellardf0cbd3e2004-04-22 00:10:48 +0000763{
764 if ((so->so_state & SS_NOFDREF) == 0) {
765 shutdown(so->s,0);
bellardf0cbd3e2004-04-22 00:10:48 +0000766 }
767 so->so_state &= ~(SS_ISFCONNECTING);
Jan Kiszkaf932b6c2009-06-24 14:42:29 +0200768 if (so->so_state & SS_FCANTSENDMORE) {
769 so->so_state &= SS_PERSISTENT_MASK;
770 so->so_state |= SS_NOFDREF; /* Don't select it */
771 } else {
bellardf0cbd3e2004-04-22 00:10:48 +0000772 so->so_state |= SS_FCANTRCVMORE;
Jan Kiszkaf932b6c2009-06-24 14:42:29 +0200773 }
bellardf0cbd3e2004-04-22 00:10:48 +0000774}
775
blueswir19634d902007-10-26 19:01:16 +0000776static void
777sofcantsendmore(struct socket *so)
bellardf0cbd3e2004-04-22 00:10:48 +0000778{
779 if ((so->so_state & SS_NOFDREF) == 0) {
bellard02d2c542004-10-07 23:27:35 +0000780 shutdown(so->s,1); /* send FIN to fhost */
bellardf0cbd3e2004-04-22 00:10:48 +0000781 }
782 so->so_state &= ~(SS_ISFCONNECTING);
Jan Kiszkaf932b6c2009-06-24 14:42:29 +0200783 if (so->so_state & SS_FCANTRCVMORE) {
784 so->so_state &= SS_PERSISTENT_MASK;
785 so->so_state |= SS_NOFDREF; /* as above */
786 } else {
bellardf0cbd3e2004-04-22 00:10:48 +0000787 so->so_state |= SS_FCANTSENDMORE;
Jan Kiszkaf932b6c2009-06-24 14:42:29 +0200788 }
bellardf0cbd3e2004-04-22 00:10:48 +0000789}
790
bellardf0cbd3e2004-04-22 00:10:48 +0000791/*
792 * Set write drain mode
793 * Set CANTSENDMORE once all data has been write()n
794 */
795void
blueswir1511d2b12009-03-07 15:32:56 +0000796sofwdrain(struct socket *so)
bellardf0cbd3e2004-04-22 00:10:48 +0000797{
798 if (so->so_rcv.sb_cc)
799 so->so_state |= SS_FWDRAIN;
800 else
801 sofcantsendmore(so);
802}
Guillaume Subiron53792292015-12-19 22:24:59 +0100803
804/*
805 * Translate addr in host addr when it is a virtual address
806 */
807void sotranslate_out(struct socket *so, struct sockaddr_storage *addr)
808{
809 Slirp *slirp = so->slirp;
810 struct sockaddr_in *sin = (struct sockaddr_in *)addr;
Guillaume Subiron05061d82016-03-15 10:31:22 +0100811 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)addr;
Guillaume Subiron53792292015-12-19 22:24:59 +0100812
813 switch (addr->ss_family) {
814 case AF_INET:
815 if ((so->so_faddr.s_addr & slirp->vnetwork_mask.s_addr) ==
816 slirp->vnetwork_addr.s_addr) {
817 /* It's an alias */
818 if (so->so_faddr.s_addr == slirp->vnameserver_addr.s_addr) {
819 if (get_dns_addr(&sin->sin_addr) < 0) {
820 sin->sin_addr = loopback_addr;
821 }
822 } else {
823 sin->sin_addr = loopback_addr;
824 }
825 }
826
827 DEBUG_MISC((dfd, " addr.sin_port=%d, "
828 "addr.sin_addr.s_addr=%.16s\n",
829 ntohs(sin->sin_port), inet_ntoa(sin->sin_addr)));
830 break;
831
Guillaume Subiron05061d82016-03-15 10:31:22 +0100832 case AF_INET6:
833 if (in6_equal_net(&so->so_faddr6, &slirp->vprefix_addr6,
834 slirp->vprefix_len)) {
835 if (in6_equal(&so->so_faddr6, &slirp->vnameserver_addr6)) {
Samuel Thibaultef763fa2016-03-20 15:16:21 +0100836 uint32_t scope_id;
837 if (get_dns6_addr(&sin6->sin6_addr, &scope_id) >= 0) {
838 sin6->sin6_scope_id = scope_id;
839 } else {
Guillaume Subiron05061d82016-03-15 10:31:22 +0100840 sin6->sin6_addr = in6addr_loopback;
Samuel Thibault1d176542016-03-20 15:09:09 +0100841 }
Guillaume Subiron05061d82016-03-15 10:31:22 +0100842 } else {
843 sin6->sin6_addr = in6addr_loopback;
844 }
845 }
846 break;
847
Guillaume Subiron53792292015-12-19 22:24:59 +0100848 default:
849 break;
850 }
851}
852
853void sotranslate_in(struct socket *so, struct sockaddr_storage *addr)
854{
855 Slirp *slirp = so->slirp;
856 struct sockaddr_in *sin = (struct sockaddr_in *)addr;
Guillaume Subiron05061d82016-03-15 10:31:22 +0100857 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)addr;
Guillaume Subiron53792292015-12-19 22:24:59 +0100858
859 switch (addr->ss_family) {
860 case AF_INET:
861 if ((so->so_faddr.s_addr & slirp->vnetwork_mask.s_addr) ==
862 slirp->vnetwork_addr.s_addr) {
863 uint32_t inv_mask = ~slirp->vnetwork_mask.s_addr;
864
865 if ((so->so_faddr.s_addr & inv_mask) == inv_mask) {
866 sin->sin_addr = slirp->vhost_addr;
867 } else if (sin->sin_addr.s_addr == loopback_addr.s_addr ||
868 so->so_faddr.s_addr != slirp->vhost_addr.s_addr) {
869 sin->sin_addr = so->so_faddr;
870 }
871 }
872 break;
873
Guillaume Subiron05061d82016-03-15 10:31:22 +0100874 case AF_INET6:
875 if (in6_equal_net(&so->so_faddr6, &slirp->vprefix_addr6,
876 slirp->vprefix_len)) {
877 if (in6_equal(&sin6->sin6_addr, &in6addr_loopback)
878 || !in6_equal(&so->so_faddr6, &slirp->vhost_addr6)) {
879 sin6->sin6_addr = so->so_faddr6;
880 }
881 }
882 break;
883
Guillaume Subiron53792292015-12-19 22:24:59 +0100884 default:
885 break;
886 }
887}
888
889/*
890 * Translate connections from localhost to the real hostname
891 */
892void sotranslate_accept(struct socket *so)
893{
894 Slirp *slirp = so->slirp;
895
896 switch (so->so_ffamily) {
897 case AF_INET:
898 if (so->so_faddr.s_addr == INADDR_ANY ||
899 (so->so_faddr.s_addr & loopback_mask) ==
900 (loopback_addr.s_addr & loopback_mask)) {
901 so->so_faddr = slirp->vhost_addr;
902 }
903 break;
904
Guillaume Subiron05061d82016-03-15 10:31:22 +0100905 case AF_INET6:
906 if (in6_equal(&so->so_faddr6, &in6addr_any) ||
907 in6_equal(&so->so_faddr6, &in6addr_loopback)) {
908 so->so_faddr6 = slirp->vhost_addr6;
909 }
910 break;
911
Guillaume Subiron53792292015-12-19 22:24:59 +0100912 default:
913 break;
914 }
915}