Statistics
| Branch: | Revision:

root / slirp / socket.c @ c94c8d64

History | View | Annotate | Download (16.1 kB)

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