Statistics
| Branch: | Revision:

root / slirp / slirp.c @ dc5d0b3d

History | View | Annotate | Download (13.4 kB)

1 f0cbd3ec bellard
#include "slirp.h"
2 f0cbd3ec bellard
3 f0cbd3ec bellard
/* host address */
4 f0cbd3ec bellard
struct in_addr our_addr;
5 f0cbd3ec bellard
/* host dns address */
6 f0cbd3ec bellard
struct in_addr dns_addr;
7 f0cbd3ec bellard
/* host loopback address */
8 f0cbd3ec bellard
struct in_addr loopback_addr;
9 f0cbd3ec bellard
10 f0cbd3ec bellard
/* address for slirp virtual addresses */
11 f0cbd3ec bellard
struct in_addr special_addr;
12 f0cbd3ec bellard
13 f0cbd3ec bellard
const uint8_t special_ethaddr[6] = { 
14 f0cbd3ec bellard
    0x52, 0x54, 0x00, 0x12, 0x35, 0x00
15 f0cbd3ec bellard
};
16 f0cbd3ec bellard
17 f0cbd3ec bellard
uint8_t client_ethaddr[6];
18 f0cbd3ec bellard
19 f0cbd3ec bellard
int do_slowtimo;
20 f0cbd3ec bellard
int link_up;
21 f0cbd3ec bellard
struct timeval tt;
22 f0cbd3ec bellard
FILE *lfd;
23 f0cbd3ec bellard
24 f0cbd3ec bellard
/* XXX: suppress those select globals */
25 f0cbd3ec bellard
fd_set *global_readfds, *global_writefds, *global_xfds;
26 f0cbd3ec bellard
27 f0cbd3ec bellard
#ifdef _WIN32
28 f0cbd3ec bellard
29 f0cbd3ec bellard
static int get_dns_addr(struct in_addr *pdns_addr)
30 f0cbd3ec bellard
{
31 f0cbd3ec bellard
    /* XXX: add it */
32 f0cbd3ec bellard
    return -1;
33 f0cbd3ec bellard
}
34 f0cbd3ec bellard
35 f0cbd3ec bellard
#else
36 f0cbd3ec bellard
37 f0cbd3ec bellard
static int get_dns_addr(struct in_addr *pdns_addr)
38 f0cbd3ec bellard
{
39 f0cbd3ec bellard
    char buff[512];
40 f0cbd3ec bellard
    char buff2[256];
41 f0cbd3ec bellard
    FILE *f;
42 f0cbd3ec bellard
    int found = 0;
43 f0cbd3ec bellard
    struct in_addr tmp_addr;
44 f0cbd3ec bellard
    
45 f0cbd3ec bellard
    f = fopen("/etc/resolv.conf", "r");
46 f0cbd3ec bellard
    if (!f)
47 f0cbd3ec bellard
        return -1;
48 f0cbd3ec bellard
49 f0cbd3ec bellard
    lprint("IP address of your DNS(s): ");
50 f0cbd3ec bellard
    while (fgets(buff, 512, f) != NULL) {
51 f0cbd3ec bellard
        if (sscanf(buff, "nameserver%*[ \t]%256s", buff2) == 1) {
52 f0cbd3ec bellard
            if (!inet_aton(buff2, &tmp_addr))
53 f0cbd3ec bellard
                continue;
54 f0cbd3ec bellard
            if (tmp_addr.s_addr == loopback_addr.s_addr)
55 f0cbd3ec bellard
                tmp_addr = our_addr;
56 f0cbd3ec bellard
            /* If it's the first one, set it to dns_addr */
57 f0cbd3ec bellard
            if (!found)
58 f0cbd3ec bellard
                *pdns_addr = tmp_addr;
59 f0cbd3ec bellard
            else
60 f0cbd3ec bellard
                lprint(", ");
61 f0cbd3ec bellard
            if (++found > 3) {
62 f0cbd3ec bellard
                lprint("(more)");
63 f0cbd3ec bellard
                break;
64 f0cbd3ec bellard
            } else
65 f0cbd3ec bellard
                lprint("%s", inet_ntoa(tmp_addr));
66 f0cbd3ec bellard
        }
67 f0cbd3ec bellard
    }
68 f0cbd3ec bellard
    if (!found)
69 f0cbd3ec bellard
        return -1;
70 f0cbd3ec bellard
    return 0;
71 f0cbd3ec bellard
}
72 f0cbd3ec bellard
73 f0cbd3ec bellard
#endif
74 f0cbd3ec bellard
75 f0cbd3ec bellard
void slirp_init(void)
76 f0cbd3ec bellard
{
77 512176db bellard
    //    debug_init("/tmp/slirp.log", DEBUG_DEFAULT);
78 f0cbd3ec bellard
79 f0cbd3ec bellard
    link_up = 1;
80 f0cbd3ec bellard
81 f0cbd3ec bellard
    if_init();
82 f0cbd3ec bellard
    ip_init();
83 f0cbd3ec bellard
84 f0cbd3ec bellard
    /* Initialise mbufs *after* setting the MTU */
85 f0cbd3ec bellard
    m_init();
86 f0cbd3ec bellard
87 f0cbd3ec bellard
    /* set default addresses */
88 f0cbd3ec bellard
    getouraddr();
89 f0cbd3ec bellard
    inet_aton("127.0.0.1", &loopback_addr);
90 f0cbd3ec bellard
91 f0cbd3ec bellard
    if (get_dns_addr(&dns_addr) < 0) {
92 f0cbd3ec bellard
        fprintf(stderr, "Could not get DNS address\n");
93 f0cbd3ec bellard
        exit(1);
94 f0cbd3ec bellard
    }
95 f0cbd3ec bellard
96 f0cbd3ec bellard
    inet_aton(CTL_SPECIAL, &special_addr);
97 f0cbd3ec bellard
}
98 f0cbd3ec bellard
99 f0cbd3ec bellard
#define CONN_CANFSEND(so) (((so)->so_state & (SS_FCANTSENDMORE|SS_ISFCONNECTED)) == SS_ISFCONNECTED)
100 f0cbd3ec bellard
#define CONN_CANFRCV(so) (((so)->so_state & (SS_FCANTRCVMORE|SS_ISFCONNECTED)) == SS_ISFCONNECTED)
101 f0cbd3ec bellard
#define UPD_NFDS(x) if (nfds < (x)) nfds = (x)
102 f0cbd3ec bellard
103 f0cbd3ec bellard
/*
104 f0cbd3ec bellard
 * curtime kept to an accuracy of 1ms
105 f0cbd3ec bellard
 */
106 f0cbd3ec bellard
static void updtime(void)
107 f0cbd3ec bellard
{
108 f0cbd3ec bellard
        gettimeofday(&tt, 0);
109 f0cbd3ec bellard
        
110 f0cbd3ec bellard
        curtime = (u_int)tt.tv_sec * (u_int)1000;
111 f0cbd3ec bellard
        curtime += (u_int)tt.tv_usec / (u_int)1000;
112 f0cbd3ec bellard
        
113 f0cbd3ec bellard
        if ((tt.tv_usec % 1000) >= 500)
114 f0cbd3ec bellard
           curtime++;
115 f0cbd3ec bellard
}
116 f0cbd3ec bellard
117 f0cbd3ec bellard
void slirp_select_fill(int *pnfds, 
118 f0cbd3ec bellard
                       fd_set *readfds, fd_set *writefds, fd_set *xfds)
119 f0cbd3ec bellard
{
120 f0cbd3ec bellard
    struct socket *so, *so_next;
121 f0cbd3ec bellard
    struct timeval timeout;
122 f0cbd3ec bellard
    int nfds;
123 f0cbd3ec bellard
    int tmp_time;
124 f0cbd3ec bellard
125 f0cbd3ec bellard
    /* fail safe */
126 f0cbd3ec bellard
    global_readfds = NULL;
127 f0cbd3ec bellard
    global_writefds = NULL;
128 f0cbd3ec bellard
    global_xfds = NULL;
129 f0cbd3ec bellard
    
130 f0cbd3ec bellard
    nfds = *pnfds;
131 f0cbd3ec bellard
        /*
132 f0cbd3ec bellard
         * First, TCP sockets
133 f0cbd3ec bellard
         */
134 f0cbd3ec bellard
        do_slowtimo = 0;
135 f0cbd3ec bellard
        if (link_up) {
136 f0cbd3ec bellard
                /* 
137 f0cbd3ec bellard
                 * *_slowtimo needs calling if there are IP fragments
138 f0cbd3ec bellard
                 * in the fragment queue, or there are TCP connections active
139 f0cbd3ec bellard
                 */
140 f0cbd3ec bellard
                do_slowtimo = ((tcb.so_next != &tcb) ||
141 f0cbd3ec bellard
                               ((struct ipasfrag *)&ipq != (struct ipasfrag *)ipq.next));
142 f0cbd3ec bellard
                
143 f0cbd3ec bellard
                for (so = tcb.so_next; so != &tcb; so = so_next) {
144 f0cbd3ec bellard
                        so_next = so->so_next;
145 f0cbd3ec bellard
                        
146 f0cbd3ec bellard
                        /*
147 f0cbd3ec bellard
                         * See if we need a tcp_fasttimo
148 f0cbd3ec bellard
                         */
149 f0cbd3ec bellard
                        if (time_fasttimo == 0 && so->so_tcpcb->t_flags & TF_DELACK)
150 f0cbd3ec bellard
                           time_fasttimo = curtime; /* Flag when we want a fasttimo */
151 f0cbd3ec bellard
                        
152 f0cbd3ec bellard
                        /*
153 f0cbd3ec bellard
                         * NOFDREF can include still connecting to local-host,
154 f0cbd3ec bellard
                         * newly socreated() sockets etc. Don't want to select these.
155 f0cbd3ec bellard
                          */
156 f0cbd3ec bellard
                        if (so->so_state & SS_NOFDREF || so->s == -1)
157 f0cbd3ec bellard
                           continue;
158 f0cbd3ec bellard
                        
159 f0cbd3ec bellard
                        /*
160 f0cbd3ec bellard
                         * Set for reading sockets which are accepting
161 f0cbd3ec bellard
                         */
162 f0cbd3ec bellard
                        if (so->so_state & SS_FACCEPTCONN) {
163 f0cbd3ec bellard
                                FD_SET(so->s, readfds);
164 f0cbd3ec bellard
                                UPD_NFDS(so->s);
165 f0cbd3ec bellard
                                continue;
166 f0cbd3ec bellard
                        }
167 f0cbd3ec bellard
                        
168 f0cbd3ec bellard
                        /*
169 f0cbd3ec bellard
                         * Set for writing sockets which are connecting
170 f0cbd3ec bellard
                         */
171 f0cbd3ec bellard
                        if (so->so_state & SS_ISFCONNECTING) {
172 f0cbd3ec bellard
                                FD_SET(so->s, writefds);
173 f0cbd3ec bellard
                                UPD_NFDS(so->s);
174 f0cbd3ec bellard
                                continue;
175 f0cbd3ec bellard
                        }
176 f0cbd3ec bellard
                        
177 f0cbd3ec bellard
                        /*
178 f0cbd3ec bellard
                         * Set for writing if we are connected, can send more, and
179 f0cbd3ec bellard
                         * we have something to send
180 f0cbd3ec bellard
                         */
181 f0cbd3ec bellard
                        if (CONN_CANFSEND(so) && so->so_rcv.sb_cc) {
182 f0cbd3ec bellard
                                FD_SET(so->s, writefds);
183 f0cbd3ec bellard
                                UPD_NFDS(so->s);
184 f0cbd3ec bellard
                        }
185 f0cbd3ec bellard
                        
186 f0cbd3ec bellard
                        /*
187 f0cbd3ec bellard
                         * Set for reading (and urgent data) if we are connected, can
188 f0cbd3ec bellard
                         * receive more, and we have room for it XXX /2 ?
189 f0cbd3ec bellard
                         */
190 f0cbd3ec bellard
                        if (CONN_CANFRCV(so) && (so->so_snd.sb_cc < (so->so_snd.sb_datalen/2))) {
191 f0cbd3ec bellard
                                FD_SET(so->s, readfds);
192 f0cbd3ec bellard
                                FD_SET(so->s, xfds);
193 f0cbd3ec bellard
                                UPD_NFDS(so->s);
194 f0cbd3ec bellard
                        }
195 f0cbd3ec bellard
                }
196 f0cbd3ec bellard
                
197 f0cbd3ec bellard
                /*
198 f0cbd3ec bellard
                 * UDP sockets
199 f0cbd3ec bellard
                 */
200 f0cbd3ec bellard
                for (so = udb.so_next; so != &udb; so = so_next) {
201 f0cbd3ec bellard
                        so_next = so->so_next;
202 f0cbd3ec bellard
                        
203 f0cbd3ec bellard
                        /*
204 f0cbd3ec bellard
                         * See if it's timed out
205 f0cbd3ec bellard
                         */
206 f0cbd3ec bellard
                        if (so->so_expire) {
207 f0cbd3ec bellard
                                if (so->so_expire <= curtime) {
208 f0cbd3ec bellard
                                        udp_detach(so);
209 f0cbd3ec bellard
                                        continue;
210 f0cbd3ec bellard
                                } else
211 f0cbd3ec bellard
                                        do_slowtimo = 1; /* Let socket expire */
212 f0cbd3ec bellard
                        }
213 f0cbd3ec bellard
                        
214 f0cbd3ec bellard
                        /*
215 f0cbd3ec bellard
                         * When UDP packets are received from over the
216 f0cbd3ec bellard
                         * link, they're sendto()'d straight away, so
217 f0cbd3ec bellard
                         * no need for setting for writing
218 f0cbd3ec bellard
                         * Limit the number of packets queued by this session
219 f0cbd3ec bellard
                         * to 4.  Note that even though we try and limit this
220 f0cbd3ec bellard
                         * to 4 packets, the session could have more queued
221 f0cbd3ec bellard
                         * if the packets needed to be fragmented
222 f0cbd3ec bellard
                         * (XXX <= 4 ?)
223 f0cbd3ec bellard
                         */
224 f0cbd3ec bellard
                        if ((so->so_state & SS_ISFCONNECTED) && so->so_queued <= 4) {
225 f0cbd3ec bellard
                                FD_SET(so->s, readfds);
226 f0cbd3ec bellard
                                UPD_NFDS(so->s);
227 f0cbd3ec bellard
                        }
228 f0cbd3ec bellard
                }
229 f0cbd3ec bellard
        }
230 f0cbd3ec bellard
        
231 f0cbd3ec bellard
        /*
232 f0cbd3ec bellard
         * Setup timeout to use minimum CPU usage, especially when idle
233 f0cbd3ec bellard
         */
234 f0cbd3ec bellard
        
235 f0cbd3ec bellard
        /* 
236 f0cbd3ec bellard
         * First, see the timeout needed by *timo
237 f0cbd3ec bellard
         */
238 f0cbd3ec bellard
        timeout.tv_sec = 0;
239 f0cbd3ec bellard
        timeout.tv_usec = -1;
240 f0cbd3ec bellard
        /*
241 f0cbd3ec bellard
         * If a slowtimo is needed, set timeout to 500ms from the last
242 f0cbd3ec bellard
         * slow timeout. If a fast timeout is needed, set timeout within
243 f0cbd3ec bellard
         * 200ms of when it was requested.
244 f0cbd3ec bellard
         */
245 f0cbd3ec bellard
        if (do_slowtimo) {
246 f0cbd3ec bellard
                /* XXX + 10000 because some select()'s aren't that accurate */
247 f0cbd3ec bellard
                timeout.tv_usec = ((500 - (curtime - last_slowtimo)) * 1000) + 10000;
248 f0cbd3ec bellard
                if (timeout.tv_usec < 0)
249 f0cbd3ec bellard
                   timeout.tv_usec = 0;
250 f0cbd3ec bellard
                else if (timeout.tv_usec > 510000)
251 f0cbd3ec bellard
                   timeout.tv_usec = 510000;
252 f0cbd3ec bellard
                
253 f0cbd3ec bellard
                /* Can only fasttimo if we also slowtimo */
254 f0cbd3ec bellard
                if (time_fasttimo) {
255 f0cbd3ec bellard
                        tmp_time = (200 - (curtime - time_fasttimo)) * 1000;
256 f0cbd3ec bellard
                        if (tmp_time < 0)
257 f0cbd3ec bellard
                           tmp_time = 0;
258 f0cbd3ec bellard
                        
259 f0cbd3ec bellard
                        /* Choose the smallest of the 2 */
260 f0cbd3ec bellard
                        if (tmp_time < timeout.tv_usec)
261 f0cbd3ec bellard
                           timeout.tv_usec = (u_int)tmp_time;
262 f0cbd3ec bellard
                }
263 f0cbd3ec bellard
        }
264 f0cbd3ec bellard
        *pnfds = nfds;
265 f0cbd3ec bellard
}        
266 f0cbd3ec bellard
267 f0cbd3ec bellard
void slirp_select_poll(fd_set *readfds, fd_set *writefds, fd_set *xfds)
268 f0cbd3ec bellard
{
269 f0cbd3ec bellard
    struct socket *so, *so_next;
270 f0cbd3ec bellard
    int ret;
271 f0cbd3ec bellard
272 f0cbd3ec bellard
    global_readfds = readfds;
273 f0cbd3ec bellard
    global_writefds = writefds;
274 f0cbd3ec bellard
    global_xfds = xfds;
275 f0cbd3ec bellard
276 f0cbd3ec bellard
        /* Update time */
277 f0cbd3ec bellard
        updtime();
278 f0cbd3ec bellard
        
279 f0cbd3ec bellard
        /*
280 f0cbd3ec bellard
         * See if anything has timed out 
281 f0cbd3ec bellard
         */
282 f0cbd3ec bellard
        if (link_up) {
283 f0cbd3ec bellard
                if (time_fasttimo && ((curtime - time_fasttimo) >= 199)) {
284 f0cbd3ec bellard
                        tcp_fasttimo();
285 f0cbd3ec bellard
                        time_fasttimo = 0;
286 f0cbd3ec bellard
                }
287 f0cbd3ec bellard
                if (do_slowtimo && ((curtime - last_slowtimo) >= 499)) {
288 f0cbd3ec bellard
                        ip_slowtimo();
289 f0cbd3ec bellard
                        tcp_slowtimo();
290 f0cbd3ec bellard
                        last_slowtimo = curtime;
291 f0cbd3ec bellard
                }
292 f0cbd3ec bellard
        }
293 f0cbd3ec bellard
        
294 f0cbd3ec bellard
        /*
295 f0cbd3ec bellard
         * Check sockets
296 f0cbd3ec bellard
         */
297 f0cbd3ec bellard
        if (link_up) {
298 f0cbd3ec bellard
                /*
299 f0cbd3ec bellard
                 * Check TCP sockets
300 f0cbd3ec bellard
                 */
301 f0cbd3ec bellard
                for (so = tcb.so_next; so != &tcb; so = so_next) {
302 f0cbd3ec bellard
                        so_next = so->so_next;
303 f0cbd3ec bellard
                        
304 f0cbd3ec bellard
                        /*
305 f0cbd3ec bellard
                         * FD_ISSET is meaningless on these sockets
306 f0cbd3ec bellard
                         * (and they can crash the program)
307 f0cbd3ec bellard
                         */
308 f0cbd3ec bellard
                        if (so->so_state & SS_NOFDREF || so->s == -1)
309 f0cbd3ec bellard
                           continue;
310 f0cbd3ec bellard
                        
311 f0cbd3ec bellard
                        /*
312 f0cbd3ec bellard
                         * Check for URG data
313 f0cbd3ec bellard
                         * This will soread as well, so no need to
314 f0cbd3ec bellard
                         * test for readfds below if this succeeds
315 f0cbd3ec bellard
                         */
316 f0cbd3ec bellard
                        if (FD_ISSET(so->s, xfds))
317 f0cbd3ec bellard
                           sorecvoob(so);
318 f0cbd3ec bellard
                        /*
319 f0cbd3ec bellard
                         * Check sockets for reading
320 f0cbd3ec bellard
                         */
321 f0cbd3ec bellard
                        else if (FD_ISSET(so->s, readfds)) {
322 f0cbd3ec bellard
                                /*
323 f0cbd3ec bellard
                                 * Check for incoming connections
324 f0cbd3ec bellard
                                 */
325 f0cbd3ec bellard
                                if (so->so_state & SS_FACCEPTCONN) {
326 f0cbd3ec bellard
                                        tcp_connect(so);
327 f0cbd3ec bellard
                                        continue;
328 f0cbd3ec bellard
                                } /* else */
329 f0cbd3ec bellard
                                ret = soread(so);
330 f0cbd3ec bellard
                                
331 f0cbd3ec bellard
                                /* Output it if we read something */
332 f0cbd3ec bellard
                                if (ret > 0)
333 f0cbd3ec bellard
                                   tcp_output(sototcpcb(so));
334 f0cbd3ec bellard
                        }
335 f0cbd3ec bellard
                        
336 f0cbd3ec bellard
                        /*
337 f0cbd3ec bellard
                         * Check sockets for writing
338 f0cbd3ec bellard
                         */
339 f0cbd3ec bellard
                        if (FD_ISSET(so->s, writefds)) {
340 f0cbd3ec bellard
                          /*
341 f0cbd3ec bellard
                           * Check for non-blocking, still-connecting sockets
342 f0cbd3ec bellard
                           */
343 f0cbd3ec bellard
                          if (so->so_state & SS_ISFCONNECTING) {
344 f0cbd3ec bellard
                            /* Connected */
345 f0cbd3ec bellard
                            so->so_state &= ~SS_ISFCONNECTING;
346 f0cbd3ec bellard
                            
347 f0cbd3ec bellard
                            ret = write(so->s, &ret, 0);
348 f0cbd3ec bellard
                            if (ret < 0) {
349 f0cbd3ec bellard
                              /* XXXXX Must fix, zero bytes is a NOP */
350 f0cbd3ec bellard
                              if (errno == EAGAIN || errno == EWOULDBLOCK ||
351 f0cbd3ec bellard
                                  errno == EINPROGRESS || errno == ENOTCONN)
352 f0cbd3ec bellard
                                continue;
353 f0cbd3ec bellard
                              
354 f0cbd3ec bellard
                              /* else failed */
355 f0cbd3ec bellard
                              so->so_state = SS_NOFDREF;
356 f0cbd3ec bellard
                            }
357 f0cbd3ec bellard
                            /* else so->so_state &= ~SS_ISFCONNECTING; */
358 f0cbd3ec bellard
                            
359 f0cbd3ec bellard
                            /*
360 f0cbd3ec bellard
                             * Continue tcp_input
361 f0cbd3ec bellard
                             */
362 f0cbd3ec bellard
                            tcp_input((struct mbuf *)NULL, sizeof(struct ip), so);
363 f0cbd3ec bellard
                            /* continue; */
364 f0cbd3ec bellard
                          } else
365 f0cbd3ec bellard
                            ret = sowrite(so);
366 f0cbd3ec bellard
                          /*
367 f0cbd3ec bellard
                           * XXXXX If we wrote something (a lot), there 
368 f0cbd3ec bellard
                           * could be a need for a window update.
369 f0cbd3ec bellard
                           * In the worst case, the remote will send
370 f0cbd3ec bellard
                           * a window probe to get things going again
371 f0cbd3ec bellard
                           */
372 f0cbd3ec bellard
                        }
373 f0cbd3ec bellard
                        
374 f0cbd3ec bellard
                        /*
375 f0cbd3ec bellard
                         * Probe a still-connecting, non-blocking socket
376 f0cbd3ec bellard
                         * to check if it's still alive
377 f0cbd3ec bellard
                           */
378 f0cbd3ec bellard
#ifdef PROBE_CONN
379 f0cbd3ec bellard
                        if (so->so_state & SS_ISFCONNECTING) {
380 f0cbd3ec bellard
                          ret = read(so->s, (char *)&ret, 0);
381 f0cbd3ec bellard
                          
382 f0cbd3ec bellard
                          if (ret < 0) {
383 f0cbd3ec bellard
                            /* XXX */
384 f0cbd3ec bellard
                            if (errno == EAGAIN || errno == EWOULDBLOCK ||
385 f0cbd3ec bellard
                                errno == EINPROGRESS || errno == ENOTCONN)
386 f0cbd3ec bellard
                              continue; /* Still connecting, continue */
387 f0cbd3ec bellard
                            
388 f0cbd3ec bellard
                            /* else failed */
389 f0cbd3ec bellard
                            so->so_state = SS_NOFDREF;
390 f0cbd3ec bellard
                            
391 f0cbd3ec bellard
                            /* tcp_input will take care of it */
392 f0cbd3ec bellard
                          } else {
393 f0cbd3ec bellard
                            ret = write(so->s, &ret, 0);
394 f0cbd3ec bellard
                            if (ret < 0) {
395 f0cbd3ec bellard
                              /* XXX */
396 f0cbd3ec bellard
                              if (errno == EAGAIN || errno == EWOULDBLOCK ||
397 f0cbd3ec bellard
                                  errno == EINPROGRESS || errno == ENOTCONN)
398 f0cbd3ec bellard
                                continue;
399 f0cbd3ec bellard
                              /* else failed */
400 f0cbd3ec bellard
                              so->so_state = SS_NOFDREF;
401 f0cbd3ec bellard
                            } else
402 f0cbd3ec bellard
                              so->so_state &= ~SS_ISFCONNECTING;
403 f0cbd3ec bellard
                            
404 f0cbd3ec bellard
                          }
405 f0cbd3ec bellard
                          tcp_input((struct mbuf *)NULL, sizeof(struct ip),so);
406 f0cbd3ec bellard
                        } /* SS_ISFCONNECTING */
407 f0cbd3ec bellard
#endif
408 f0cbd3ec bellard
                }
409 f0cbd3ec bellard
                
410 f0cbd3ec bellard
                /*
411 f0cbd3ec bellard
                 * Now UDP sockets.
412 f0cbd3ec bellard
                 * Incoming packets are sent straight away, they're not buffered.
413 f0cbd3ec bellard
                 * Incoming UDP data isn't buffered either.
414 f0cbd3ec bellard
                 */
415 f0cbd3ec bellard
                for (so = udb.so_next; so != &udb; so = so_next) {
416 f0cbd3ec bellard
                        so_next = so->so_next;
417 f0cbd3ec bellard
                        
418 f0cbd3ec bellard
                        if (so->s != -1 && FD_ISSET(so->s, readfds)) {
419 f0cbd3ec bellard
                            sorecvfrom(so);
420 f0cbd3ec bellard
                        }
421 f0cbd3ec bellard
                }
422 f0cbd3ec bellard
        }
423 f0cbd3ec bellard
        
424 f0cbd3ec bellard
        /*
425 f0cbd3ec bellard
         * See if we can start outputting
426 f0cbd3ec bellard
         */
427 f0cbd3ec bellard
        if (if_queued && link_up)
428 f0cbd3ec bellard
           if_start();
429 f0cbd3ec bellard
}
430 f0cbd3ec bellard
431 f0cbd3ec bellard
#define ETH_ALEN 6
432 f0cbd3ec bellard
#define ETH_HLEN 14
433 f0cbd3ec bellard
434 f0cbd3ec bellard
#define ETH_P_IP        0x0800                /* Internet Protocol packet        */
435 f0cbd3ec bellard
#define ETH_P_ARP        0x0806                /* Address Resolution packet        */
436 f0cbd3ec bellard
437 f0cbd3ec bellard
#define        ARPOP_REQUEST        1                /* ARP request                        */
438 f0cbd3ec bellard
#define        ARPOP_REPLY        2                /* ARP reply                        */
439 f0cbd3ec bellard
440 f0cbd3ec bellard
struct ethhdr 
441 f0cbd3ec bellard
{
442 f0cbd3ec bellard
        unsigned char        h_dest[ETH_ALEN];        /* destination eth addr        */
443 f0cbd3ec bellard
        unsigned char        h_source[ETH_ALEN];        /* source ether addr        */
444 f0cbd3ec bellard
        unsigned short        h_proto;                /* packet type ID field        */
445 f0cbd3ec bellard
};
446 f0cbd3ec bellard
447 f0cbd3ec bellard
struct arphdr
448 f0cbd3ec bellard
{
449 f0cbd3ec bellard
        unsigned short        ar_hrd;                /* format of hardware address        */
450 f0cbd3ec bellard
        unsigned short        ar_pro;                /* format of protocol address        */
451 f0cbd3ec bellard
        unsigned char        ar_hln;                /* length of hardware address        */
452 f0cbd3ec bellard
        unsigned char        ar_pln;                /* length of protocol address        */
453 f0cbd3ec bellard
        unsigned short        ar_op;                /* ARP opcode (command)                */
454 f0cbd3ec bellard
455 f0cbd3ec bellard
         /*
456 f0cbd3ec bellard
          *         Ethernet looks like this : This bit is variable sized however...
457 f0cbd3ec bellard
          */
458 f0cbd3ec bellard
        unsigned char                ar_sha[ETH_ALEN];        /* sender hardware address        */
459 f0cbd3ec bellard
        unsigned char                ar_sip[4];                /* sender IP address                */
460 f0cbd3ec bellard
        unsigned char                ar_tha[ETH_ALEN];        /* target hardware address        */
461 f0cbd3ec bellard
        unsigned char                ar_tip[4];                /* target IP address                */
462 f0cbd3ec bellard
};
463 f0cbd3ec bellard
464 f0cbd3ec bellard
void arp_input(const uint8_t *pkt, int pkt_len)
465 f0cbd3ec bellard
{
466 f0cbd3ec bellard
    struct ethhdr *eh = (struct ethhdr *)pkt;
467 f0cbd3ec bellard
    struct arphdr *ah = (struct arphdr *)(pkt + ETH_HLEN);
468 f0cbd3ec bellard
    uint8_t arp_reply[ETH_HLEN + sizeof(struct arphdr)];
469 f0cbd3ec bellard
    struct ethhdr *reh = (struct ethhdr *)arp_reply;
470 f0cbd3ec bellard
    struct arphdr *rah = (struct arphdr *)(arp_reply + ETH_HLEN);
471 f0cbd3ec bellard
    int ar_op;
472 f0cbd3ec bellard
473 f0cbd3ec bellard
    ar_op = ntohs(ah->ar_op);
474 f0cbd3ec bellard
    switch(ar_op) {
475 f0cbd3ec bellard
    case ARPOP_REQUEST:
476 f0cbd3ec bellard
        if (!memcmp(ah->ar_tip, &special_addr, 3) &&
477 f0cbd3ec bellard
            (ah->ar_tip[3] == CTL_DNS || ah->ar_tip[3] == CTL_ALIAS)) {
478 f0cbd3ec bellard
479 f0cbd3ec bellard
            /* XXX: make an ARP request to have the client address */
480 f0cbd3ec bellard
            memcpy(client_ethaddr, eh->h_source, ETH_ALEN);
481 f0cbd3ec bellard
482 f0cbd3ec bellard
            /* ARP request for alias/dns mac address */
483 f0cbd3ec bellard
            memcpy(reh->h_dest, pkt + ETH_ALEN, ETH_ALEN);
484 f0cbd3ec bellard
            memcpy(reh->h_source, special_ethaddr, ETH_ALEN - 1);
485 f0cbd3ec bellard
            reh->h_source[5] = ah->ar_tip[3];
486 f0cbd3ec bellard
            reh->h_proto = htons(ETH_P_ARP);
487 f0cbd3ec bellard
488 f0cbd3ec bellard
            rah->ar_hrd = htons(1);
489 f0cbd3ec bellard
            rah->ar_pro = htons(ETH_P_IP);
490 f0cbd3ec bellard
            rah->ar_hln = ETH_ALEN;
491 f0cbd3ec bellard
            rah->ar_pln = 4;
492 f0cbd3ec bellard
            rah->ar_op = htons(ARPOP_REPLY);
493 f0cbd3ec bellard
            memcpy(rah->ar_sha, reh->h_source, ETH_ALEN);
494 f0cbd3ec bellard
            memcpy(rah->ar_sip, ah->ar_tip, 4);
495 f0cbd3ec bellard
            memcpy(rah->ar_tha, ah->ar_sha, ETH_ALEN);
496 f0cbd3ec bellard
            memcpy(rah->ar_tip, ah->ar_sip, 4);
497 f0cbd3ec bellard
            slirp_output(arp_reply, sizeof(arp_reply));
498 f0cbd3ec bellard
        }
499 f0cbd3ec bellard
        break;
500 f0cbd3ec bellard
    default:
501 f0cbd3ec bellard
        break;
502 f0cbd3ec bellard
    }
503 f0cbd3ec bellard
}
504 f0cbd3ec bellard
505 f0cbd3ec bellard
void slirp_input(const uint8_t *pkt, int pkt_len)
506 f0cbd3ec bellard
{
507 f0cbd3ec bellard
    struct mbuf *m;
508 f0cbd3ec bellard
    int proto;
509 f0cbd3ec bellard
510 f0cbd3ec bellard
    if (pkt_len < ETH_HLEN)
511 f0cbd3ec bellard
        return;
512 f0cbd3ec bellard
    
513 f0cbd3ec bellard
    proto = ntohs(*(uint16_t *)(pkt + 12));
514 f0cbd3ec bellard
    switch(proto) {
515 f0cbd3ec bellard
    case ETH_P_ARP:
516 f0cbd3ec bellard
        arp_input(pkt, pkt_len);
517 f0cbd3ec bellard
        break;
518 f0cbd3ec bellard
    case ETH_P_IP:
519 f0cbd3ec bellard
        m = m_get();
520 f0cbd3ec bellard
        if (!m)
521 f0cbd3ec bellard
            return;
522 f0cbd3ec bellard
        m->m_len = pkt_len;
523 f0cbd3ec bellard
        memcpy(m->m_data, pkt, pkt_len);
524 f0cbd3ec bellard
525 f0cbd3ec bellard
        m->m_data += ETH_HLEN;
526 f0cbd3ec bellard
        m->m_len -= ETH_HLEN;
527 f0cbd3ec bellard
528 f0cbd3ec bellard
        ip_input(m);
529 f0cbd3ec bellard
        break;
530 f0cbd3ec bellard
    default:
531 f0cbd3ec bellard
        break;
532 f0cbd3ec bellard
    }
533 f0cbd3ec bellard
}
534 f0cbd3ec bellard
535 f0cbd3ec bellard
/* output the IP packet to the ethernet device */
536 f0cbd3ec bellard
void if_encap(const uint8_t *ip_data, int ip_data_len)
537 f0cbd3ec bellard
{
538 f0cbd3ec bellard
    uint8_t buf[1600];
539 f0cbd3ec bellard
    struct ethhdr *eh = (struct ethhdr *)buf;
540 f0cbd3ec bellard
541 f0cbd3ec bellard
    if (ip_data_len + ETH_HLEN > sizeof(buf))
542 f0cbd3ec bellard
        return;
543 f0cbd3ec bellard
544 f0cbd3ec bellard
    memcpy(eh->h_dest, client_ethaddr, ETH_ALEN);
545 f0cbd3ec bellard
    memcpy(eh->h_source, special_ethaddr, ETH_ALEN - 1);
546 f0cbd3ec bellard
    eh->h_source[5] = CTL_ALIAS;
547 f0cbd3ec bellard
    eh->h_proto = htons(ETH_P_IP);
548 f0cbd3ec bellard
    memcpy(buf + sizeof(struct ethhdr), ip_data, ip_data_len);
549 f0cbd3ec bellard
    slirp_output(buf, ip_data_len + ETH_HLEN);
550 f0cbd3ec bellard
}