Statistics
| Branch: | Revision:

root / slirp / socket.c @ 7b88e48b

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 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 f0cbd3ec bellard
        len = sb->sb_datalen - sb->sb_cc;
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 02d2c54c bellard
        nn = 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 17444c9c bellard
            ret = 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
        len = sb->sb_cc;
367 5fafdf24 ths
368 f0cbd3ec bellard
        iov[0].iov_base = sb->sb_rptr;
369 66029f6a blueswir1
        iov[1].iov_base = NULL;
370 66029f6a blueswir1
        iov[1].iov_len = 0;
371 f0cbd3ec bellard
        if (sb->sb_rptr < sb->sb_wptr) {
372 f0cbd3ec bellard
                iov[0].iov_len = sb->sb_wptr - sb->sb_rptr;
373 f0cbd3ec bellard
                /* Should never succeed, but... */
374 f0cbd3ec bellard
                if (iov[0].iov_len > len) iov[0].iov_len = len;
375 f0cbd3ec bellard
                n = 1;
376 f0cbd3ec bellard
        } else {
377 f0cbd3ec bellard
                iov[0].iov_len = (sb->sb_data + sb->sb_datalen) - sb->sb_rptr;
378 f0cbd3ec bellard
                if (iov[0].iov_len > len) iov[0].iov_len = len;
379 f0cbd3ec bellard
                len -= iov[0].iov_len;
380 f0cbd3ec bellard
                if (len) {
381 f0cbd3ec bellard
                        iov[1].iov_base = sb->sb_data;
382 f0cbd3ec bellard
                        iov[1].iov_len = sb->sb_wptr - sb->sb_data;
383 f0cbd3ec bellard
                        if (iov[1].iov_len > len) iov[1].iov_len = len;
384 f0cbd3ec bellard
                        n = 2;
385 f0cbd3ec bellard
                } else
386 f0cbd3ec bellard
                        n = 1;
387 f0cbd3ec bellard
        }
388 f0cbd3ec bellard
        /* Check if there's urgent data to send, and if so, send it */
389 f0cbd3ec bellard
390 f0cbd3ec bellard
#ifdef HAVE_READV
391 f0cbd3ec bellard
        nn = writev(so->s, (const struct iovec *)iov, n);
392 5fafdf24 ths
393 f0cbd3ec bellard
        DEBUG_MISC((dfd, "  ... wrote nn = %d bytes\n", nn));
394 f0cbd3ec bellard
#else
395 e1c5a2b3 aliguori
        nn = slirp_send(so, iov[0].iov_base, iov[0].iov_len,0);
396 f0cbd3ec bellard
#endif
397 f0cbd3ec bellard
        /* This should never happen, but people tell me it does *shrug* */
398 f0cbd3ec bellard
        if (nn < 0 && (errno == EAGAIN || errno == EINTR))
399 f0cbd3ec bellard
                return 0;
400 5fafdf24 ths
401 f0cbd3ec bellard
        if (nn <= 0) {
402 f0cbd3ec bellard
                DEBUG_MISC((dfd, " --- sowrite disconnected, so->so_state = %x, errno = %d\n",
403 f0cbd3ec bellard
                        so->so_state, errno));
404 f0cbd3ec bellard
                sofcantsendmore(so);
405 f0cbd3ec bellard
                tcp_sockclosed(sototcpcb(so));
406 f0cbd3ec bellard
                return -1;
407 f0cbd3ec bellard
        }
408 5fafdf24 ths
409 f0cbd3ec bellard
#ifndef HAVE_READV
410 3bc2175d bellard
        if (n == 2 && nn == iov[0].iov_len) {
411 3bc2175d bellard
            int ret;
412 e1c5a2b3 aliguori
            ret = slirp_send(so, iov[1].iov_base, iov[1].iov_len,0);
413 3bc2175d bellard
            if (ret > 0)
414 3bc2175d bellard
                nn += ret;
415 3bc2175d bellard
        }
416 f0cbd3ec bellard
        DEBUG_MISC((dfd, "  ... wrote nn = %d bytes\n", nn));
417 f0cbd3ec bellard
#endif
418 5fafdf24 ths
419 f0cbd3ec bellard
        /* Update sbuf */
420 f0cbd3ec bellard
        sb->sb_cc -= nn;
421 f0cbd3ec bellard
        sb->sb_rptr += nn;
422 f0cbd3ec bellard
        if (sb->sb_rptr >= (sb->sb_data + sb->sb_datalen))
423 f0cbd3ec bellard
                sb->sb_rptr -= sb->sb_datalen;
424 5fafdf24 ths
425 f0cbd3ec bellard
        /*
426 f0cbd3ec bellard
         * If in DRAIN mode, and there's no more data, set
427 f0cbd3ec bellard
         * it CANTSENDMORE
428 f0cbd3ec bellard
         */
429 f0cbd3ec bellard
        if ((so->so_state & SS_FWDRAIN) && sb->sb_cc == 0)
430 f0cbd3ec bellard
                sofcantsendmore(so);
431 5fafdf24 ths
432 f0cbd3ec bellard
        return nn;
433 f0cbd3ec bellard
}
434 f0cbd3ec bellard
435 f0cbd3ec bellard
/*
436 f0cbd3ec bellard
 * recvfrom() a UDP socket
437 f0cbd3ec bellard
 */
438 f0cbd3ec bellard
void
439 511d2b14 blueswir1
sorecvfrom(struct socket *so)
440 f0cbd3ec bellard
{
441 f0cbd3ec bellard
        struct sockaddr_in addr;
442 242acf3a balrog
        socklen_t addrlen = sizeof(struct sockaddr_in);
443 5fafdf24 ths
444 f0cbd3ec bellard
        DEBUG_CALL("sorecvfrom");
445 f0cbd3ec bellard
        DEBUG_ARG("so = %lx", (long)so);
446 5fafdf24 ths
447 f0cbd3ec bellard
        if (so->so_type == IPPROTO_ICMP) {   /* This is a "ping" reply */
448 f0cbd3ec bellard
          char buff[256];
449 f0cbd3ec bellard
          int len;
450 3b46e624 ths
451 5fafdf24 ths
          len = recvfrom(so->s, buff, 256, 0,
452 f0cbd3ec bellard
                         (struct sockaddr *)&addr, &addrlen);
453 f0cbd3ec bellard
          /* XXX Check if reply is "correct"? */
454 3b46e624 ths
455 f0cbd3ec bellard
          if(len == -1 || len == 0) {
456 f0cbd3ec bellard
            u_char code=ICMP_UNREACH_PORT;
457 f0cbd3ec bellard
458 f0cbd3ec bellard
            if(errno == EHOSTUNREACH) code=ICMP_UNREACH_HOST;
459 f0cbd3ec bellard
            else if(errno == ENETUNREACH) code=ICMP_UNREACH_NET;
460 3b46e624 ths
461 f0cbd3ec bellard
            DEBUG_MISC((dfd," udp icmp rx errno = %d-%s\n",
462 f0cbd3ec bellard
                        errno,strerror(errno)));
463 f0cbd3ec bellard
            icmp_error(so->so_m, ICMP_UNREACH,code, 0,strerror(errno));
464 f0cbd3ec bellard
          } else {
465 f0cbd3ec bellard
            icmp_reflect(so->so_m);
466 511d2b14 blueswir1
            so->so_m = NULL; /* Don't m_free() it again! */
467 f0cbd3ec bellard
          }
468 f0cbd3ec bellard
          /* No need for this socket anymore, udp_detach it */
469 f0cbd3ec bellard
          udp_detach(so);
470 f0cbd3ec bellard
        } else {                                    /* A "normal" UDP packet */
471 f0cbd3ec bellard
          struct mbuf *m;
472 c5b76b38 Blue Swirl
          int len;
473 c5b76b38 Blue Swirl
#ifdef _WIN32
474 c5b76b38 Blue Swirl
          unsigned long n;
475 c5b76b38 Blue Swirl
#else
476 c5b76b38 Blue Swirl
          int n;
477 c5b76b38 Blue Swirl
#endif
478 f0cbd3ec bellard
479 460fec67 Jan Kiszka
          m = m_get(so->slirp);
480 460fec67 Jan Kiszka
          if (!m) {
481 460fec67 Jan Kiszka
              return;
482 460fec67 Jan Kiszka
          }
483 9634d903 blueswir1
          m->m_data += IF_MAXLINKHDR;
484 3b46e624 ths
485 5fafdf24 ths
          /*
486 f0cbd3ec bellard
           * XXX Shouldn't FIONREAD packets destined for port 53,
487 f0cbd3ec bellard
           * but I don't know the max packet size for DNS lookups
488 f0cbd3ec bellard
           */
489 f0cbd3ec bellard
          len = M_FREEROOM(m);
490 f0cbd3ec bellard
          /* if (so->so_fport != htons(53)) { */
491 379ff53d bellard
          ioctlsocket(so->s, FIONREAD, &n);
492 3b46e624 ths
493 f0cbd3ec bellard
          if (n > len) {
494 f0cbd3ec bellard
            n = (m->m_data - m->m_dat) + m->m_len + n + 1;
495 f0cbd3ec bellard
            m_inc(m, n);
496 f0cbd3ec bellard
            len = M_FREEROOM(m);
497 f0cbd3ec bellard
          }
498 f0cbd3ec bellard
          /* } */
499 3b46e624 ths
500 f0cbd3ec bellard
          m->m_len = recvfrom(so->s, m->m_data, len, 0,
501 f0cbd3ec bellard
                              (struct sockaddr *)&addr, &addrlen);
502 5fafdf24 ths
          DEBUG_MISC((dfd, " did recvfrom %d, errno = %d-%s\n",
503 f0cbd3ec bellard
                      m->m_len, errno,strerror(errno)));
504 f0cbd3ec bellard
          if(m->m_len<0) {
505 f0cbd3ec bellard
            u_char code=ICMP_UNREACH_PORT;
506 f0cbd3ec bellard
507 f0cbd3ec bellard
            if(errno == EHOSTUNREACH) code=ICMP_UNREACH_HOST;
508 f0cbd3ec bellard
            else if(errno == ENETUNREACH) code=ICMP_UNREACH_NET;
509 3b46e624 ths
510 f0cbd3ec bellard
            DEBUG_MISC((dfd," rx error, tx icmp ICMP_UNREACH:%i\n", code));
511 f0cbd3ec bellard
            icmp_error(so->so_m, ICMP_UNREACH,code, 0,strerror(errno));
512 f0cbd3ec bellard
            m_free(m);
513 f0cbd3ec bellard
          } else {
514 f0cbd3ec bellard
          /*
515 f0cbd3ec bellard
           * Hack: domain name lookup will be used the most for UDP,
516 f0cbd3ec bellard
           * and since they'll only be used once there's no need
517 f0cbd3ec bellard
           * for the 4 minute (or whatever) timeout... So we time them
518 f0cbd3ec bellard
           * out much quicker (10 seconds  for now...)
519 f0cbd3ec bellard
           */
520 f0cbd3ec bellard
            if (so->so_expire) {
521 f0cbd3ec bellard
              if (so->so_fport == htons(53))
522 f0cbd3ec bellard
                so->so_expire = curtime + SO_EXPIREFAST;
523 f0cbd3ec bellard
              else
524 f0cbd3ec bellard
                so->so_expire = curtime + SO_EXPIRE;
525 f0cbd3ec bellard
            }
526 f0cbd3ec bellard
527 5fafdf24 ths
            /*
528 f0cbd3ec bellard
             * If this packet was destined for CTL_ADDR,
529 f0cbd3ec bellard
             * make it look like that's where it came from, done by udp_output
530 f0cbd3ec bellard
             */
531 f0cbd3ec bellard
            udp_output(so, m, &addr);
532 f0cbd3ec bellard
          } /* rx error */
533 f0cbd3ec bellard
        } /* if ping packet */
534 f0cbd3ec bellard
}
535 f0cbd3ec bellard
536 f0cbd3ec bellard
/*
537 f0cbd3ec bellard
 * sendto() a socket
538 f0cbd3ec bellard
 */
539 f0cbd3ec bellard
int
540 511d2b14 blueswir1
sosendto(struct socket *so, struct mbuf *m)
541 f0cbd3ec bellard
{
542 460fec67 Jan Kiszka
        Slirp *slirp = so->slirp;
543 f0cbd3ec bellard
        int ret;
544 f0cbd3ec bellard
        struct sockaddr_in addr;
545 f0cbd3ec bellard
546 f0cbd3ec bellard
        DEBUG_CALL("sosendto");
547 f0cbd3ec bellard
        DEBUG_ARG("so = %lx", (long)so);
548 f0cbd3ec bellard
        DEBUG_ARG("m = %lx", (long)m);
549 5fafdf24 ths
550 f0cbd3ec bellard
        addr.sin_family = AF_INET;
551 460fec67 Jan Kiszka
        if ((so->so_faddr.s_addr & slirp->vnetwork_mask.s_addr) ==
552 460fec67 Jan Kiszka
            slirp->vnetwork_addr.s_addr) {
553 f0cbd3ec bellard
          /* It's an alias */
554 460fec67 Jan Kiszka
          if (so->so_faddr.s_addr == slirp->vnameserver_addr.s_addr) {
555 df7a86ed Ed Swierk
            if (get_dns_addr(&addr.sin_addr) < 0)
556 df7a86ed Ed Swierk
              addr.sin_addr = loopback_addr;
557 a13a4126 Jan Kiszka
          } else {
558 f0cbd3ec bellard
            addr.sin_addr = loopback_addr;
559 f0cbd3ec bellard
          }
560 f0cbd3ec bellard
        } else
561 f0cbd3ec bellard
          addr.sin_addr = so->so_faddr;
562 f0cbd3ec bellard
        addr.sin_port = so->so_fport;
563 f0cbd3ec bellard
564 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)));
565 5fafdf24 ths
566 f0cbd3ec bellard
        /* Don't care what port we get */
567 f0cbd3ec bellard
        ret = sendto(so->s, m->m_data, m->m_len, 0,
568 f0cbd3ec bellard
                     (struct sockaddr *)&addr, sizeof (struct sockaddr));
569 f0cbd3ec bellard
        if (ret < 0)
570 f0cbd3ec bellard
                return -1;
571 5fafdf24 ths
572 f0cbd3ec bellard
        /*
573 f0cbd3ec bellard
         * Kill the socket if there's no reply in 4 minutes,
574 f0cbd3ec bellard
         * but only if it's an expirable socket
575 f0cbd3ec bellard
         */
576 f0cbd3ec bellard
        if (so->so_expire)
577 f0cbd3ec bellard
                so->so_expire = curtime + SO_EXPIRE;
578 f932b6ce Jan Kiszka
        so->so_state &= SS_PERSISTENT_MASK;
579 f932b6ce Jan Kiszka
        so->so_state |= SS_ISFCONNECTED; /* So that it gets select()ed */
580 f0cbd3ec bellard
        return 0;
581 f0cbd3ec bellard
}
582 f0cbd3ec bellard
583 f0cbd3ec bellard
/*
584 3c6a0580 Jan Kiszka
 * Listen for incoming TCP connections
585 f0cbd3ec bellard
 */
586 f0cbd3ec bellard
struct socket *
587 460fec67 Jan Kiszka
tcp_listen(Slirp *slirp, u_int32_t haddr, u_int hport, u_int32_t laddr,
588 460fec67 Jan Kiszka
           u_int lport, int flags)
589 f0cbd3ec bellard
{
590 f0cbd3ec bellard
        struct sockaddr_in addr;
591 f0cbd3ec bellard
        struct socket *so;
592 242acf3a balrog
        int s, opt = 1;
593 242acf3a balrog
        socklen_t addrlen = sizeof(addr);
594 f0cbd3ec bellard
595 3c6a0580 Jan Kiszka
        DEBUG_CALL("tcp_listen");
596 9f349498 Jan Kiszka
        DEBUG_ARG("haddr = %x", haddr);
597 9f349498 Jan Kiszka
        DEBUG_ARG("hport = %d", hport);
598 f0cbd3ec bellard
        DEBUG_ARG("laddr = %x", laddr);
599 f0cbd3ec bellard
        DEBUG_ARG("lport = %d", lport);
600 f0cbd3ec bellard
        DEBUG_ARG("flags = %x", flags);
601 5fafdf24 ths
602 460fec67 Jan Kiszka
        so = socreate(slirp);
603 460fec67 Jan Kiszka
        if (!so) {
604 f0cbd3ec bellard
          return NULL;
605 f0cbd3ec bellard
        }
606 5fafdf24 ths
607 f0cbd3ec bellard
        /* Don't tcp_attach... we don't need so_snd nor so_rcv */
608 f0cbd3ec bellard
        if ((so->so_tcpcb = tcp_newtcpcb(so)) == NULL) {
609 f0cbd3ec bellard
                free(so);
610 f0cbd3ec bellard
                return NULL;
611 f0cbd3ec bellard
        }
612 460fec67 Jan Kiszka
        insque(so, &slirp->tcb);
613 5fafdf24 ths
614 5fafdf24 ths
        /*
615 f0cbd3ec bellard
         * SS_FACCEPTONCE sockets must time out.
616 f0cbd3ec bellard
         */
617 f0cbd3ec bellard
        if (flags & SS_FACCEPTONCE)
618 f0cbd3ec bellard
           so->so_tcpcb->t_timer[TCPT_KEEP] = TCPTV_KEEP_INIT*2;
619 5fafdf24 ths
620 f932b6ce Jan Kiszka
        so->so_state &= SS_PERSISTENT_MASK;
621 f932b6ce Jan Kiszka
        so->so_state |= (SS_FACCEPTCONN | flags);
622 f0cbd3ec bellard
        so->so_lport = lport; /* Kept in network format */
623 f0cbd3ec bellard
        so->so_laddr.s_addr = laddr; /* Ditto */
624 5fafdf24 ths
625 f0cbd3ec bellard
        addr.sin_family = AF_INET;
626 3c6a0580 Jan Kiszka
        addr.sin_addr.s_addr = haddr;
627 3c6a0580 Jan Kiszka
        addr.sin_port = hport;
628 5fafdf24 ths
629 40ff6d7e Kevin Wolf
        if (((s = qemu_socket(AF_INET,SOCK_STREAM,0)) < 0) ||
630 b55669bf pbrook
            (setsockopt(s,SOL_SOCKET,SO_REUSEADDR,(char *)&opt,sizeof(int)) < 0) ||
631 f0cbd3ec bellard
            (bind(s,(struct sockaddr *)&addr, sizeof(addr)) < 0) ||
632 f0cbd3ec bellard
            (listen(s,1) < 0)) {
633 f0cbd3ec bellard
                int tmperrno = errno; /* Don't clobber the real reason we failed */
634 3b46e624 ths
635 f0cbd3ec bellard
                close(s);
636 f0cbd3ec bellard
                sofree(so);
637 f0cbd3ec bellard
                /* Restore the real errno */
638 02d2c54c bellard
#ifdef _WIN32
639 02d2c54c bellard
                WSASetLastError(tmperrno);
640 02d2c54c bellard
#else
641 f0cbd3ec bellard
                errno = tmperrno;
642 02d2c54c bellard
#endif
643 f0cbd3ec bellard
                return NULL;
644 f0cbd3ec bellard
        }
645 f0cbd3ec bellard
        setsockopt(s,SOL_SOCKET,SO_OOBINLINE,(char *)&opt,sizeof(int));
646 5fafdf24 ths
647 f0cbd3ec bellard
        getsockname(s,(struct sockaddr *)&addr,&addrlen);
648 f0cbd3ec bellard
        so->so_fport = addr.sin_port;
649 f0cbd3ec bellard
        if (addr.sin_addr.s_addr == 0 || addr.sin_addr.s_addr == loopback_addr.s_addr)
650 460fec67 Jan Kiszka
           so->so_faddr = slirp->vhost_addr;
651 f0cbd3ec bellard
        else
652 f0cbd3ec bellard
           so->so_faddr = addr.sin_addr;
653 f0cbd3ec bellard
654 f0cbd3ec bellard
        so->s = s;
655 f0cbd3ec bellard
        return so;
656 f0cbd3ec bellard
}
657 f0cbd3ec bellard
658 f0cbd3ec bellard
/*
659 f0cbd3ec bellard
 * Various session state calls
660 f0cbd3ec bellard
 * XXX Should be #define's
661 f0cbd3ec bellard
 * The socket state stuff needs work, these often get call 2 or 3
662 f0cbd3ec bellard
 * times each when only 1 was needed
663 f0cbd3ec bellard
 */
664 f0cbd3ec bellard
void
665 511d2b14 blueswir1
soisfconnecting(struct socket *so)
666 f0cbd3ec bellard
{
667 f0cbd3ec bellard
        so->so_state &= ~(SS_NOFDREF|SS_ISFCONNECTED|SS_FCANTRCVMORE|
668 f0cbd3ec bellard
                          SS_FCANTSENDMORE|SS_FWDRAIN);
669 f0cbd3ec bellard
        so->so_state |= SS_ISFCONNECTING; /* Clobber other states */
670 f0cbd3ec bellard
}
671 f0cbd3ec bellard
672 f0cbd3ec bellard
void
673 511d2b14 blueswir1
soisfconnected(struct socket *so)
674 f0cbd3ec bellard
{
675 f0cbd3ec bellard
        so->so_state &= ~(SS_ISFCONNECTING|SS_FWDRAIN|SS_NOFDREF);
676 f0cbd3ec bellard
        so->so_state |= SS_ISFCONNECTED; /* Clobber other states */
677 f0cbd3ec bellard
}
678 f0cbd3ec bellard
679 9634d903 blueswir1
static void
680 9634d903 blueswir1
sofcantrcvmore(struct socket *so)
681 f0cbd3ec bellard
{
682 f0cbd3ec bellard
        if ((so->so_state & SS_NOFDREF) == 0) {
683 f0cbd3ec bellard
                shutdown(so->s,0);
684 02d2c54c bellard
                if(global_writefds) {
685 02d2c54c bellard
                  FD_CLR(so->s,global_writefds);
686 02d2c54c bellard
                }
687 f0cbd3ec bellard
        }
688 f0cbd3ec bellard
        so->so_state &= ~(SS_ISFCONNECTING);
689 f932b6ce Jan Kiszka
        if (so->so_state & SS_FCANTSENDMORE) {
690 f932b6ce Jan Kiszka
           so->so_state &= SS_PERSISTENT_MASK;
691 f932b6ce Jan Kiszka
           so->so_state |= SS_NOFDREF; /* Don't select it */
692 f932b6ce Jan Kiszka
        } else {
693 f0cbd3ec bellard
           so->so_state |= SS_FCANTRCVMORE;
694 f932b6ce Jan Kiszka
        }
695 f0cbd3ec bellard
}
696 f0cbd3ec bellard
697 9634d903 blueswir1
static void
698 9634d903 blueswir1
sofcantsendmore(struct socket *so)
699 f0cbd3ec bellard
{
700 f0cbd3ec bellard
        if ((so->so_state & SS_NOFDREF) == 0) {
701 02d2c54c bellard
            shutdown(so->s,1);           /* send FIN to fhost */
702 02d2c54c bellard
            if (global_readfds) {
703 02d2c54c bellard
                FD_CLR(so->s,global_readfds);
704 02d2c54c bellard
            }
705 02d2c54c bellard
            if (global_xfds) {
706 02d2c54c bellard
                FD_CLR(so->s,global_xfds);
707 02d2c54c bellard
            }
708 f0cbd3ec bellard
        }
709 f0cbd3ec bellard
        so->so_state &= ~(SS_ISFCONNECTING);
710 f932b6ce Jan Kiszka
        if (so->so_state & SS_FCANTRCVMORE) {
711 f932b6ce Jan Kiszka
           so->so_state &= SS_PERSISTENT_MASK;
712 f932b6ce Jan Kiszka
           so->so_state |= SS_NOFDREF; /* as above */
713 f932b6ce Jan Kiszka
        } else {
714 f0cbd3ec bellard
           so->so_state |= SS_FCANTSENDMORE;
715 f932b6ce Jan Kiszka
        }
716 f0cbd3ec bellard
}
717 f0cbd3ec bellard
718 f0cbd3ec bellard
/*
719 f0cbd3ec bellard
 * Set write drain mode
720 f0cbd3ec bellard
 * Set CANTSENDMORE once all data has been write()n
721 f0cbd3ec bellard
 */
722 f0cbd3ec bellard
void
723 511d2b14 blueswir1
sofwdrain(struct socket *so)
724 f0cbd3ec bellard
{
725 f0cbd3ec bellard
        if (so->so_rcv.sb_cc)
726 f0cbd3ec bellard
                so->so_state |= SS_FWDRAIN;
727 f0cbd3ec bellard
        else
728 f0cbd3ec bellard
                sofcantsendmore(so);
729 f0cbd3ec bellard
}