Statistics
| Branch: | Revision:

root / slirp / socket.c @ 5dba48a8

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