Statistics
| Branch: | Revision:

root / slirp / socket.c @ 39bffca2

History | View | Annotate | Download (17.5 kB)

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