Add slirp_restrict option (Gleb Natapov)
[qemu] / slirp / slirp.c
1 /*
2  * libslirp glue
3  *
4  * Copyright (c) 2004-2008 Fabrice Bellard
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22  * THE SOFTWARE.
23  */
24 #include "qemu-common.h"
25 #include "slirp.h"
26
27 /* host address */
28 struct in_addr our_addr;
29 /* host dns address */
30 struct in_addr dns_addr;
31 /* host loopback address */
32 struct in_addr loopback_addr;
33
34 /* address for slirp virtual addresses */
35 struct in_addr special_addr;
36 /* virtual address alias for host */
37 struct in_addr alias_addr;
38
39 static const uint8_t special_ethaddr[6] = {
40     0x52, 0x54, 0x00, 0x12, 0x35, 0x00
41 };
42
43 /* ARP cache for the guest IP addresses (XXX: allow many entries) */
44 uint8_t client_ethaddr[6];
45 static struct in_addr client_ipaddr;
46
47 static const uint8_t zero_ethaddr[6] = { 0, 0, 0, 0, 0, 0 };
48
49 char *slirp_special_ip = CTL_SPECIAL;
50 int slirp_restrict;
51 int do_slowtimo;
52 int link_up;
53 struct timeval tt;
54 FILE *lfd;
55 struct ex_list *exec_list;
56
57 /* XXX: suppress those select globals */
58 fd_set *global_readfds, *global_writefds, *global_xfds;
59
60 char slirp_hostname[33];
61
62 #ifdef _WIN32
63
64 static int get_dns_addr(struct in_addr *pdns_addr)
65 {
66     FIXED_INFO *FixedInfo=NULL;
67     ULONG    BufLen;
68     DWORD    ret;
69     IP_ADDR_STRING *pIPAddr;
70     struct in_addr tmp_addr;
71
72     FixedInfo = (FIXED_INFO *)GlobalAlloc(GPTR, sizeof(FIXED_INFO));
73     BufLen = sizeof(FIXED_INFO);
74
75     if (ERROR_BUFFER_OVERFLOW == GetNetworkParams(FixedInfo, &BufLen)) {
76         if (FixedInfo) {
77             GlobalFree(FixedInfo);
78             FixedInfo = NULL;
79         }
80         FixedInfo = GlobalAlloc(GPTR, BufLen);
81     }
82
83     if ((ret = GetNetworkParams(FixedInfo, &BufLen)) != ERROR_SUCCESS) {
84         printf("GetNetworkParams failed. ret = %08x\n", (u_int)ret );
85         if (FixedInfo) {
86             GlobalFree(FixedInfo);
87             FixedInfo = NULL;
88         }
89         return -1;
90     }
91
92     pIPAddr = &(FixedInfo->DnsServerList);
93     inet_aton(pIPAddr->IpAddress.String, &tmp_addr);
94     *pdns_addr = tmp_addr;
95 #if 0
96     printf( "DNS Servers:\n" );
97     printf( "DNS Addr:%s\n", pIPAddr->IpAddress.String );
98
99     pIPAddr = FixedInfo -> DnsServerList.Next;
100     while ( pIPAddr ) {
101             printf( "DNS Addr:%s\n", pIPAddr ->IpAddress.String );
102             pIPAddr = pIPAddr ->Next;
103     }
104 #endif
105     if (FixedInfo) {
106         GlobalFree(FixedInfo);
107         FixedInfo = NULL;
108     }
109     return 0;
110 }
111
112 #else
113
114 static int get_dns_addr(struct in_addr *pdns_addr)
115 {
116     char buff[512];
117     char buff2[257];
118     FILE *f;
119     int found = 0;
120     struct in_addr tmp_addr;
121
122     f = fopen("/etc/resolv.conf", "r");
123     if (!f)
124         return -1;
125
126 #ifdef DEBUG
127     lprint("IP address of your DNS(s): ");
128 #endif
129     while (fgets(buff, 512, f) != NULL) {
130         if (sscanf(buff, "nameserver%*[ \t]%256s", buff2) == 1) {
131             if (!inet_aton(buff2, &tmp_addr))
132                 continue;
133             if (tmp_addr.s_addr == loopback_addr.s_addr)
134                 tmp_addr = our_addr;
135             /* If it's the first one, set it to dns_addr */
136             if (!found)
137                 *pdns_addr = tmp_addr;
138 #ifdef DEBUG
139             else
140                 lprint(", ");
141 #endif
142             if (++found > 3) {
143 #ifdef DEBUG
144                 lprint("(more)");
145 #endif
146                 break;
147             }
148 #ifdef DEBUG
149             else
150                 lprint("%s", inet_ntoa(tmp_addr));
151 #endif
152         }
153     }
154     fclose(f);
155     if (!found)
156         return -1;
157     return 0;
158 }
159
160 #endif
161
162 #ifdef _WIN32
163 static void slirp_cleanup(void)
164 {
165     WSACleanup();
166 }
167 #endif
168
169 void slirp_init(int restrict, char *special_ip)
170 {
171     //    debug_init("/tmp/slirp.log", DEBUG_DEFAULT);
172
173 #ifdef _WIN32
174     {
175         WSADATA Data;
176         WSAStartup(MAKEWORD(2,0), &Data);
177         atexit(slirp_cleanup);
178     }
179 #endif
180
181     link_up = 1;
182     slirp_restrict = restrict;
183
184     if_init();
185     ip_init();
186
187     /* Initialise mbufs *after* setting the MTU */
188     m_init();
189
190     /* set default addresses */
191     inet_aton("127.0.0.1", &loopback_addr);
192
193     if (get_dns_addr(&dns_addr) < 0) {
194         dns_addr = loopback_addr;
195         fprintf (stderr, "Warning: No DNS servers found\n");
196     }
197
198     if (special_ip)
199         slirp_special_ip = special_ip;
200
201     inet_aton(slirp_special_ip, &special_addr);
202     alias_addr.s_addr = special_addr.s_addr | htonl(CTL_ALIAS);
203     getouraddr();
204 }
205
206 #define CONN_CANFSEND(so) (((so)->so_state & (SS_FCANTSENDMORE|SS_ISFCONNECTED)) == SS_ISFCONNECTED)
207 #define CONN_CANFRCV(so) (((so)->so_state & (SS_FCANTRCVMORE|SS_ISFCONNECTED)) == SS_ISFCONNECTED)
208 #define UPD_NFDS(x) if (nfds < (x)) nfds = (x)
209
210 /*
211  * curtime kept to an accuracy of 1ms
212  */
213 #ifdef _WIN32
214 static void updtime(void)
215 {
216     struct _timeb tb;
217
218     _ftime(&tb);
219     curtime = (u_int)tb.time * (u_int)1000;
220     curtime += (u_int)tb.millitm;
221 }
222 #else
223 static void updtime(void)
224 {
225         gettimeofday(&tt, 0);
226
227         curtime = (u_int)tt.tv_sec * (u_int)1000;
228         curtime += (u_int)tt.tv_usec / (u_int)1000;
229
230         if ((tt.tv_usec % 1000) >= 500)
231            curtime++;
232 }
233 #endif
234
235 void slirp_select_fill(int *pnfds,
236                        fd_set *readfds, fd_set *writefds, fd_set *xfds)
237 {
238     struct socket *so, *so_next;
239     struct timeval timeout;
240     int nfds;
241     int tmp_time;
242
243     /* fail safe */
244     global_readfds = NULL;
245     global_writefds = NULL;
246     global_xfds = NULL;
247
248     nfds = *pnfds;
249         /*
250          * First, TCP sockets
251          */
252         do_slowtimo = 0;
253         if (link_up) {
254                 /*
255                  * *_slowtimo needs calling if there are IP fragments
256                  * in the fragment queue, or there are TCP connections active
257                  */
258                 do_slowtimo = ((tcb.so_next != &tcb) ||
259                                ((struct ipasfrag *)&ipq != (struct ipasfrag *)ipq.next));
260
261                 for (so = tcb.so_next; so != &tcb; so = so_next) {
262                         so_next = so->so_next;
263
264                         /*
265                          * See if we need a tcp_fasttimo
266                          */
267                         if (time_fasttimo == 0 && so->so_tcpcb->t_flags & TF_DELACK)
268                            time_fasttimo = curtime; /* Flag when we want a fasttimo */
269
270                         /*
271                          * NOFDREF can include still connecting to local-host,
272                          * newly socreated() sockets etc. Don't want to select these.
273                          */
274                         if (so->so_state & SS_NOFDREF || so->s == -1)
275                            continue;
276
277                         /*
278                          * Set for reading sockets which are accepting
279                          */
280                         if (so->so_state & SS_FACCEPTCONN) {
281                                 FD_SET(so->s, readfds);
282                                 UPD_NFDS(so->s);
283                                 continue;
284                         }
285
286                         /*
287                          * Set for writing sockets which are connecting
288                          */
289                         if (so->so_state & SS_ISFCONNECTING) {
290                                 FD_SET(so->s, writefds);
291                                 UPD_NFDS(so->s);
292                                 continue;
293                         }
294
295                         /*
296                          * Set for writing if we are connected, can send more, and
297                          * we have something to send
298                          */
299                         if (CONN_CANFSEND(so) && so->so_rcv.sb_cc) {
300                                 FD_SET(so->s, writefds);
301                                 UPD_NFDS(so->s);
302                         }
303
304                         /*
305                          * Set for reading (and urgent data) if we are connected, can
306                          * receive more, and we have room for it XXX /2 ?
307                          */
308                         if (CONN_CANFRCV(so) && (so->so_snd.sb_cc < (so->so_snd.sb_datalen/2))) {
309                                 FD_SET(so->s, readfds);
310                                 FD_SET(so->s, xfds);
311                                 UPD_NFDS(so->s);
312                         }
313                 }
314
315                 /*
316                  * UDP sockets
317                  */
318                 for (so = udb.so_next; so != &udb; so = so_next) {
319                         so_next = so->so_next;
320
321                         /*
322                          * See if it's timed out
323                          */
324                         if (so->so_expire) {
325                                 if (so->so_expire <= curtime) {
326                                         udp_detach(so);
327                                         continue;
328                                 } else
329                                         do_slowtimo = 1; /* Let socket expire */
330                         }
331
332                         /*
333                          * When UDP packets are received from over the
334                          * link, they're sendto()'d straight away, so
335                          * no need for setting for writing
336                          * Limit the number of packets queued by this session
337                          * to 4.  Note that even though we try and limit this
338                          * to 4 packets, the session could have more queued
339                          * if the packets needed to be fragmented
340                          * (XXX <= 4 ?)
341                          */
342                         if ((so->so_state & SS_ISFCONNECTED) && so->so_queued <= 4) {
343                                 FD_SET(so->s, readfds);
344                                 UPD_NFDS(so->s);
345                         }
346                 }
347         }
348
349         /*
350          * Setup timeout to use minimum CPU usage, especially when idle
351          */
352
353         /*
354          * First, see the timeout needed by *timo
355          */
356         timeout.tv_sec = 0;
357         timeout.tv_usec = -1;
358         /*
359          * If a slowtimo is needed, set timeout to 500ms from the last
360          * slow timeout. If a fast timeout is needed, set timeout within
361          * 200ms of when it was requested.
362          */
363         if (do_slowtimo) {
364                 /* XXX + 10000 because some select()'s aren't that accurate */
365                 timeout.tv_usec = ((500 - (curtime - last_slowtimo)) * 1000) + 10000;
366                 if (timeout.tv_usec < 0)
367                    timeout.tv_usec = 0;
368                 else if (timeout.tv_usec > 510000)
369                    timeout.tv_usec = 510000;
370
371                 /* Can only fasttimo if we also slowtimo */
372                 if (time_fasttimo) {
373                         tmp_time = (200 - (curtime - time_fasttimo)) * 1000;
374                         if (tmp_time < 0)
375                            tmp_time = 0;
376
377                         /* Choose the smallest of the 2 */
378                         if (tmp_time < timeout.tv_usec)
379                            timeout.tv_usec = (u_int)tmp_time;
380                 }
381         }
382         *pnfds = nfds;
383 }
384
385 void slirp_select_poll(fd_set *readfds, fd_set *writefds, fd_set *xfds)
386 {
387     struct socket *so, *so_next;
388     int ret;
389
390     global_readfds = readfds;
391     global_writefds = writefds;
392     global_xfds = xfds;
393
394         /* Update time */
395         updtime();
396
397         /*
398          * See if anything has timed out
399          */
400         if (link_up) {
401                 if (time_fasttimo && ((curtime - time_fasttimo) >= 2)) {
402                         tcp_fasttimo();
403                         time_fasttimo = 0;
404                 }
405                 if (do_slowtimo && ((curtime - last_slowtimo) >= 499)) {
406                         ip_slowtimo();
407                         tcp_slowtimo();
408                         last_slowtimo = curtime;
409                 }
410         }
411
412         /*
413          * Check sockets
414          */
415         if (link_up) {
416                 /*
417                  * Check TCP sockets
418                  */
419                 for (so = tcb.so_next; so != &tcb; so = so_next) {
420                         so_next = so->so_next;
421
422                         /*
423                          * FD_ISSET is meaningless on these sockets
424                          * (and they can crash the program)
425                          */
426                         if (so->so_state & SS_NOFDREF || so->s == -1)
427                            continue;
428
429                         /*
430                          * Check for URG data
431                          * This will soread as well, so no need to
432                          * test for readfds below if this succeeds
433                          */
434                         if (FD_ISSET(so->s, xfds))
435                            sorecvoob(so);
436                         /*
437                          * Check sockets for reading
438                          */
439                         else if (FD_ISSET(so->s, readfds)) {
440                                 /*
441                                  * Check for incoming connections
442                                  */
443                                 if (so->so_state & SS_FACCEPTCONN) {
444                                         tcp_connect(so);
445                                         continue;
446                                 } /* else */
447                                 ret = soread(so);
448
449                                 /* Output it if we read something */
450                                 if (ret > 0)
451                                    tcp_output(sototcpcb(so));
452                         }
453
454                         /*
455                          * Check sockets for writing
456                          */
457                         if (FD_ISSET(so->s, writefds)) {
458                           /*
459                            * Check for non-blocking, still-connecting sockets
460                            */
461                           if (so->so_state & SS_ISFCONNECTING) {
462                             /* Connected */
463                             so->so_state &= ~SS_ISFCONNECTING;
464
465                             ret = send(so->s, &ret, 0, 0);
466                             if (ret < 0) {
467                               /* XXXXX Must fix, zero bytes is a NOP */
468                               if (errno == EAGAIN || errno == EWOULDBLOCK ||
469                                   errno == EINPROGRESS || errno == ENOTCONN)
470                                 continue;
471
472                               /* else failed */
473                               so->so_state = SS_NOFDREF;
474                             }
475                             /* else so->so_state &= ~SS_ISFCONNECTING; */
476
477                             /*
478                              * Continue tcp_input
479                              */
480                             tcp_input((struct mbuf *)NULL, sizeof(struct ip), so);
481                             /* continue; */
482                           } else
483                             ret = sowrite(so);
484                           /*
485                            * XXXXX If we wrote something (a lot), there
486                            * could be a need for a window update.
487                            * In the worst case, the remote will send
488                            * a window probe to get things going again
489                            */
490                         }
491
492                         /*
493                          * Probe a still-connecting, non-blocking socket
494                          * to check if it's still alive
495                          */
496 #ifdef PROBE_CONN
497                         if (so->so_state & SS_ISFCONNECTING) {
498                           ret = recv(so->s, (char *)&ret, 0,0);
499
500                           if (ret < 0) {
501                             /* XXX */
502                             if (errno == EAGAIN || errno == EWOULDBLOCK ||
503                                 errno == EINPROGRESS || errno == ENOTCONN)
504                               continue; /* Still connecting, continue */
505
506                             /* else failed */
507                             so->so_state = SS_NOFDREF;
508
509                             /* tcp_input will take care of it */
510                           } else {
511                             ret = send(so->s, &ret, 0,0);
512                             if (ret < 0) {
513                               /* XXX */
514                               if (errno == EAGAIN || errno == EWOULDBLOCK ||
515                                   errno == EINPROGRESS || errno == ENOTCONN)
516                                 continue;
517                               /* else failed */
518                               so->so_state = SS_NOFDREF;
519                             } else
520                               so->so_state &= ~SS_ISFCONNECTING;
521
522                           }
523                           tcp_input((struct mbuf *)NULL, sizeof(struct ip),so);
524                         } /* SS_ISFCONNECTING */
525 #endif
526                 }
527
528                 /*
529                  * Now UDP sockets.
530                  * Incoming packets are sent straight away, they're not buffered.
531                  * Incoming UDP data isn't buffered either.
532                  */
533                 for (so = udb.so_next; so != &udb; so = so_next) {
534                         so_next = so->so_next;
535
536                         if (so->s != -1 && FD_ISSET(so->s, readfds)) {
537                             sorecvfrom(so);
538                         }
539                 }
540         }
541
542         /*
543          * See if we can start outputting
544          */
545         if (if_queued && link_up)
546            if_start();
547
548         /* clear global file descriptor sets.
549          * these reside on the stack in vl.c
550          * so they're unusable if we're not in
551          * slirp_select_fill or slirp_select_poll.
552          */
553          global_readfds = NULL;
554          global_writefds = NULL;
555          global_xfds = NULL;
556 }
557
558 #define ETH_ALEN 6
559 #define ETH_HLEN 14
560
561 #define ETH_P_IP        0x0800          /* Internet Protocol packet     */
562 #define ETH_P_ARP       0x0806          /* Address Resolution packet    */
563
564 #define ARPOP_REQUEST   1               /* ARP request                  */
565 #define ARPOP_REPLY     2               /* ARP reply                    */
566
567 struct ethhdr
568 {
569         unsigned char   h_dest[ETH_ALEN];       /* destination eth addr */
570         unsigned char   h_source[ETH_ALEN];     /* source ether addr    */
571         unsigned short  h_proto;                /* packet type ID field */
572 };
573
574 struct arphdr
575 {
576         unsigned short  ar_hrd;         /* format of hardware address   */
577         unsigned short  ar_pro;         /* format of protocol address   */
578         unsigned char   ar_hln;         /* length of hardware address   */
579         unsigned char   ar_pln;         /* length of protocol address   */
580         unsigned short  ar_op;          /* ARP opcode (command)         */
581
582          /*
583           *      Ethernet looks like this : This bit is variable sized however...
584           */
585         unsigned char           ar_sha[ETH_ALEN];       /* sender hardware address      */
586         unsigned char           ar_sip[4];              /* sender IP address            */
587         unsigned char           ar_tha[ETH_ALEN];       /* target hardware address      */
588         unsigned char           ar_tip[4];              /* target IP address            */
589 };
590
591 static void arp_input(const uint8_t *pkt, int pkt_len)
592 {
593     struct ethhdr *eh = (struct ethhdr *)pkt;
594     struct arphdr *ah = (struct arphdr *)(pkt + ETH_HLEN);
595     uint8_t arp_reply[ETH_HLEN + sizeof(struct arphdr)];
596     struct ethhdr *reh = (struct ethhdr *)arp_reply;
597     struct arphdr *rah = (struct arphdr *)(arp_reply + ETH_HLEN);
598     int ar_op;
599     struct ex_list *ex_ptr;
600
601     ar_op = ntohs(ah->ar_op);
602     switch(ar_op) {
603     case ARPOP_REQUEST:
604         if (!memcmp(ah->ar_tip, &special_addr, 3)) {
605             if (ah->ar_tip[3] == CTL_DNS || ah->ar_tip[3] == CTL_ALIAS)
606                 goto arp_ok;
607             for (ex_ptr = exec_list; ex_ptr; ex_ptr = ex_ptr->ex_next) {
608                 if (ex_ptr->ex_addr == ah->ar_tip[3])
609                     goto arp_ok;
610             }
611             return;
612         arp_ok:
613             /* XXX: make an ARP request to have the client address */
614             memcpy(client_ethaddr, eh->h_source, ETH_ALEN);
615
616             /* ARP request for alias/dns mac address */
617             memcpy(reh->h_dest, pkt + ETH_ALEN, ETH_ALEN);
618             memcpy(reh->h_source, special_ethaddr, ETH_ALEN - 1);
619             reh->h_source[5] = ah->ar_tip[3];
620             reh->h_proto = htons(ETH_P_ARP);
621
622             rah->ar_hrd = htons(1);
623             rah->ar_pro = htons(ETH_P_IP);
624             rah->ar_hln = ETH_ALEN;
625             rah->ar_pln = 4;
626             rah->ar_op = htons(ARPOP_REPLY);
627             memcpy(rah->ar_sha, reh->h_source, ETH_ALEN);
628             memcpy(rah->ar_sip, ah->ar_tip, 4);
629             memcpy(rah->ar_tha, ah->ar_sha, ETH_ALEN);
630             memcpy(rah->ar_tip, ah->ar_sip, 4);
631             slirp_output(arp_reply, sizeof(arp_reply));
632         }
633         break;
634     case ARPOP_REPLY:
635         /* reply to request of client mac address ? */
636         if (!memcmp(client_ethaddr, zero_ethaddr, ETH_ALEN) &&
637             !memcmp(ah->ar_sip, &client_ipaddr.s_addr, 4)) {
638             memcpy(client_ethaddr, ah->ar_sha, ETH_ALEN);
639         }
640         break;
641     default:
642         break;
643     }
644 }
645
646 void slirp_input(const uint8_t *pkt, int pkt_len)
647 {
648     struct mbuf *m;
649     int proto;
650
651     if (pkt_len < ETH_HLEN)
652         return;
653
654     proto = ntohs(*(uint16_t *)(pkt + 12));
655     switch(proto) {
656     case ETH_P_ARP:
657         arp_input(pkt, pkt_len);
658         break;
659     case ETH_P_IP:
660         m = m_get();
661         if (!m)
662             return;
663         /* Note: we add to align the IP header */
664         if (M_FREEROOM(m) < pkt_len + 2) {
665             m_inc(m, pkt_len + 2);
666         }
667         m->m_len = pkt_len + 2;
668         memcpy(m->m_data + 2, pkt, pkt_len);
669
670         m->m_data += 2 + ETH_HLEN;
671         m->m_len -= 2 + ETH_HLEN;
672
673         ip_input(m);
674         break;
675     default:
676         break;
677     }
678 }
679
680 /* output the IP packet to the ethernet device */
681 void if_encap(const uint8_t *ip_data, int ip_data_len)
682 {
683     uint8_t buf[1600];
684     struct ethhdr *eh = (struct ethhdr *)buf;
685
686     if (ip_data_len + ETH_HLEN > sizeof(buf))
687         return;
688     
689     if (!memcmp(client_ethaddr, zero_ethaddr, ETH_ALEN)) {
690         uint8_t arp_req[ETH_HLEN + sizeof(struct arphdr)];
691         struct ethhdr *reh = (struct ethhdr *)arp_req;
692         struct arphdr *rah = (struct arphdr *)(arp_req + ETH_HLEN);
693         const struct ip *iph = (const struct ip *)ip_data;
694
695         /* If the client addr is not known, there is no point in
696            sending the packet to it. Normally the sender should have
697            done an ARP request to get its MAC address. Here we do it
698            in place of sending the packet and we hope that the sender
699            will retry sending its packet. */
700         memset(reh->h_dest, 0xff, ETH_ALEN);
701         memcpy(reh->h_source, special_ethaddr, ETH_ALEN - 1);
702         reh->h_source[5] = CTL_ALIAS;
703         reh->h_proto = htons(ETH_P_ARP);
704         rah->ar_hrd = htons(1);
705         rah->ar_pro = htons(ETH_P_IP);
706         rah->ar_hln = ETH_ALEN;
707         rah->ar_pln = 4;
708         rah->ar_op = htons(ARPOP_REQUEST);
709         /* source hw addr */
710         memcpy(rah->ar_sha, special_ethaddr, ETH_ALEN - 1);
711         rah->ar_sha[5] = CTL_ALIAS;
712         /* source IP */
713         memcpy(rah->ar_sip, &alias_addr, 4);
714         /* target hw addr (none) */
715         memset(rah->ar_tha, 0, ETH_ALEN);
716         /* target IP */
717         memcpy(rah->ar_tip, &iph->ip_dst, 4);
718         client_ipaddr = iph->ip_dst;
719         slirp_output(arp_req, sizeof(arp_req));
720     } else {
721         memcpy(eh->h_dest, client_ethaddr, ETH_ALEN);
722         memcpy(eh->h_source, special_ethaddr, ETH_ALEN - 1);
723         /* XXX: not correct */
724         eh->h_source[5] = CTL_ALIAS;
725         eh->h_proto = htons(ETH_P_IP);
726         memcpy(buf + sizeof(struct ethhdr), ip_data, ip_data_len);
727         slirp_output(buf, ip_data_len + ETH_HLEN);
728     }
729 }
730
731 int slirp_redir(int is_udp, int host_port,
732                 struct in_addr guest_addr, int guest_port)
733 {
734     if (is_udp) {
735         if (!udp_listen(htons(host_port), guest_addr.s_addr,
736                         htons(guest_port), 0))
737             return -1;
738     } else {
739         if (!solisten(htons(host_port), guest_addr.s_addr,
740                       htons(guest_port), 0))
741             return -1;
742     }
743     return 0;
744 }
745
746 int slirp_add_exec(int do_pty, const void *args, int addr_low_byte,
747                   int guest_port)
748 {
749     return add_exec(&exec_list, do_pty, (char *)args,
750                     addr_low_byte, htons(guest_port));
751 }
752
753 ssize_t slirp_send(struct socket *so, const void *buf, size_t len, int flags)
754 {
755         if (so->s == -1 && so->extra) {
756                 qemu_chr_write(so->extra, buf, len);
757                 return len;
758         }
759
760         return send(so->s, buf, len, flags);
761 }
762
763 static struct socket *slirp_find_ctl_socket(int addr_low_byte, int guest_port)
764 {
765         struct socket *so;
766
767         for (so = tcb.so_next; so != &tcb; so = so->so_next) {
768                 if ((so->so_faddr.s_addr & htonl(0xffffff00)) ==
769                                 special_addr.s_addr
770                                 && (ntohl(so->so_faddr.s_addr) & 0xff) ==
771                                 addr_low_byte
772                                 && htons(so->so_fport) == guest_port)
773                         return so;
774         }
775
776         return NULL;
777 }
778
779 size_t slirp_socket_can_recv(int addr_low_byte, int guest_port)
780 {
781         struct iovec iov[2];
782         struct socket *so;
783
784     if (!link_up)
785         return 0;
786
787         so = slirp_find_ctl_socket(addr_low_byte, guest_port);
788
789         if (!so || so->so_state & SS_NOFDREF)
790                 return 0;
791
792         if (!CONN_CANFRCV(so) || so->so_snd.sb_cc >= (so->so_snd.sb_datalen/2))
793                 return 0;
794
795         return sopreprbuf(so, iov, NULL);
796 }
797
798 void slirp_socket_recv(int addr_low_byte, int guest_port, const uint8_t *buf,
799         int size)
800 {
801     int ret;
802     struct socket *so = slirp_find_ctl_socket(addr_low_byte, guest_port);
803    
804     if (!so)
805         return;
806
807     ret = soreadbuf(so, buf, size);
808
809     if (ret > 0)
810         tcp_output(sototcpcb(so));
811 }