upgraded to vde 2.1.0
[vde.git] / vde-2 / slirpvde / slirp.c
blob0408ae2a8d8adb787aefd8be4ff390225a3e0728
1 #include <config.h>
2 #include <slirp.h>
4 /* host address */
5 struct in_addr our_addr;
6 /* host dns address */
7 struct in_addr dns_addr;
8 /* host loopback address */
9 struct in_addr loopback_addr;
11 /* address for slirp virtual addresses */
12 struct in_addr special_addr;
14 const uint8_t special_ethaddr[6] = {
15 0x52, 0x54, 0x00, 0x12, 0x35, 0x00
18 static uint8_t client_ethaddr[256][6];
20 int do_slowtimo;
21 int link_up;
22 struct timeval tt;
23 FILE *lfd;
25 /* XXX: suppress those select globals */
26 fd_set *global_readfds, *global_writefds, *global_xfds;
28 #ifdef _WIN32
30 static int get_dns_addr(struct in_addr *pdns_addr)
32 /* XXX: add it */
33 return -1;
36 #else
38 static int get_dns_addr(struct in_addr *pdns_addr)
40 char buff[512];
41 char buff2[256];
42 FILE *f;
43 int found = 0;
44 struct in_addr tmp_addr;
46 f = fopen("/etc/resolv.conf", "r");
47 if (!f)
48 return -1;
50 lprint("IP address of your DNS(s): ");
51 while (fgets(buff, 512, f) != NULL) {
52 if (sscanf(buff, "nameserver%*[ \t]%256s", buff2) == 1) {
53 if (!inet_aton(buff2, &tmp_addr))
54 continue;
55 if (tmp_addr.s_addr == loopback_addr.s_addr)
56 tmp_addr = our_addr;
57 /* If it's the first one, set it to dns_addr */
58 if (!found)
59 *pdns_addr = tmp_addr;
60 else
61 lprint(", ");
62 if (++found > 3) {
63 lprint("(more)");
64 break;
65 } else
66 lprint("%s", inet_ntoa(tmp_addr));
69 lprint("\n");
70 if (!found)
71 return -1;
72 return 0;
75 #endif
77 void slirp_init(char *network)
79 debug_init("/tmp/slirp.log", DEBUG_DEFAULT);
81 link_up = 1;
83 memset(client_ethaddr,0xff,sizeof(client_ethaddr));
85 if_init();
86 ip_init();
88 /* Initialise mbufs *after* setting the MTU */
89 m_init();
91 /* set default addresses */
92 getouraddr();
93 inet_aton("127.0.0.1", &loopback_addr);
95 if (get_dns_addr(&dns_addr) < 0) {
96 fprintf(stderr, "Could not get DNS address\n");
97 exit(1);
100 if (network==NULL)
101 inet_aton(CTL_SPECIAL, &special_addr);
102 else
103 inet_aton(network, &special_addr);
107 #define CONN_CANFSEND(so) (((so)->so_state & (SS_FCANTSENDMORE|SS_ISFCONNECTED)) == SS_ISFCONNECTED)
108 #define CONN_CANFRCV(so) (((so)->so_state & (SS_FCANTRCVMORE|SS_ISFCONNECTED)) == SS_ISFCONNECTED)
109 #define UPD_NFDS(x) if (nfds < (x)) nfds = (x)
112 * curtime kept to an accuracy of 1ms
114 static void updtime(void)
116 gettimeofday(&tt, 0);
118 curtime = (u_int)tt.tv_sec * (u_int)1000;
119 curtime += (u_int)tt.tv_usec / (u_int)1000;
121 if ((tt.tv_usec % 1000) >= 500)
122 curtime++;
125 void slirp_select_fill(int *pnfds,
126 fd_set *readfds, fd_set *writefds, fd_set *xfds)
128 struct socket *so, *so_next;
129 struct timeval timeout;
130 int nfds;
131 int tmp_time;
133 /* fail safe */
134 global_readfds = NULL;
135 global_writefds = NULL;
136 global_xfds = NULL;
138 nfds = *pnfds;
140 * First, TCP sockets
142 do_slowtimo = 0;
143 if (link_up) {
145 * *_slowtimo needs calling if there are IP fragments
146 * in the fragment queue, or there are TCP connections active
148 do_slowtimo = ((tcb.so_next != &tcb) ||
149 ((struct ipasfrag *)&ipq != (struct ipasfrag *)ipq.next));
151 for (so = tcb.so_next; so != &tcb; so = so_next) {
152 so_next = so->so_next;
155 * See if we need a tcp_fasttimo
157 if (time_fasttimo == 0 && so->so_tcpcb->t_flags & TF_DELACK)
158 time_fasttimo = curtime; /* Flag when we want a fasttimo */
161 * NOFDREF can include still connecting to local-host,
162 * newly socreated() sockets etc. Don't want to select these.
164 if (so->so_state & SS_NOFDREF || so->s == -1)
165 continue;
168 * Set for reading sockets which are accepting
170 if (so->so_state & SS_FACCEPTCONN) {
171 FD_SET(so->s, readfds);
172 UPD_NFDS(so->s);
173 continue;
177 * Set for writing sockets which are connecting
179 if (so->so_state & SS_ISFCONNECTING) {
180 FD_SET(so->s, writefds);
181 UPD_NFDS(so->s);
182 continue;
186 * Set for writing if we are connected, can send more, and
187 * we have something to send
189 if (CONN_CANFSEND(so) && so->so_rcv.sb_cc) {
190 FD_SET(so->s, writefds);
191 UPD_NFDS(so->s);
195 * Set for reading (and urgent data) if we are connected, can
196 * receive more, and we have room for it XXX /2 ?
198 if (CONN_CANFRCV(so) && (so->so_snd.sb_cc < (so->so_snd.sb_datalen/2))) {
199 FD_SET(so->s, readfds);
200 FD_SET(so->s, xfds);
201 UPD_NFDS(so->s);
206 * UDP sockets
208 for (so = udb.so_next; so != &udb; so = so_next) {
209 so_next = so->so_next;
212 * See if it's timed out
214 if (so->so_expire) {
215 if (so->so_expire <= curtime) {
216 udp_detach(so);
217 continue;
218 } else
219 do_slowtimo = 1; /* Let socket expire */
223 * When UDP packets are received from over the
224 * link, they're sendto()'d straight away, so
225 * no need for setting for writing
226 * Limit the number of packets queued by this session
227 * to 4. Note that even though we try and limit this
228 * to 4 packets, the session could have more queued
229 * if the packets needed to be fragmented
230 * (XXX <= 4 ?)
232 if ((so->so_state & SS_ISFCONNECTED) && so->so_queued <= 4) {
233 FD_SET(so->s, readfds);
234 UPD_NFDS(so->s);
240 * Setup timeout to use minimum CPU usage, especially when idle
244 * First, see the timeout needed by *timo
246 timeout.tv_sec = 0;
247 timeout.tv_usec = -1;
249 * If a slowtimo is needed, set timeout to 500ms from the last
250 * slow timeout. If a fast timeout is needed, set timeout within
251 * 200ms of when it was requested.
253 if (do_slowtimo) {
254 /* XXX + 10000 because some select()'s aren't that accurate */
255 timeout.tv_usec = ((500 - (curtime - last_slowtimo)) * 1000) + 10000;
256 if (timeout.tv_usec < 0)
257 timeout.tv_usec = 0;
258 else if (timeout.tv_usec > 510000)
259 timeout.tv_usec = 510000;
261 /* Can only fasttimo if we also slowtimo */
262 if (time_fasttimo) {
263 tmp_time = (200 - (curtime - time_fasttimo)) * 1000;
264 if (tmp_time < 0)
265 tmp_time = 0;
267 /* Choose the smallest of the 2 */
268 if (tmp_time < timeout.tv_usec)
269 timeout.tv_usec = (u_int)tmp_time;
272 *pnfds = nfds;
275 void slirp_select_poll(fd_set *readfds, fd_set *writefds, fd_set *xfds)
277 struct socket *so, *so_next;
278 int ret;
280 global_readfds = readfds;
281 global_writefds = writefds;
282 global_xfds = xfds;
284 /* Update time */
285 updtime();
288 * See if anything has timed out
290 if (link_up) {
291 if (time_fasttimo && ((curtime - time_fasttimo) >= 199)) {
292 tcp_fasttimo();
293 time_fasttimo = 0;
295 if (do_slowtimo && ((curtime - last_slowtimo) >= 499)) {
296 ip_slowtimo();
297 tcp_slowtimo();
298 last_slowtimo = curtime;
303 * Check sockets
305 if (link_up) {
307 * Check TCP sockets
309 for (so = tcb.so_next; so != &tcb; so = so_next) {
310 so_next = so->so_next;
313 * FD_ISSET is meaningless on these sockets
314 * (and they can crash the program)
316 if (so->so_state & SS_NOFDREF || so->s == -1)
317 continue;
320 * Check for URG data
321 * This will soread as well, so no need to
322 * test for readfds below if this succeeds
324 if (FD_ISSET(so->s, xfds))
325 sorecvoob(so);
327 * Check sockets for reading
329 else if (FD_ISSET(so->s, readfds)) {
331 * Check for incoming connections
333 if (so->so_state & SS_FACCEPTCONN) {
334 tcp_connect(so);
335 continue;
336 } /* else */
337 ret = soread(so);
339 /* Output it if we read something */
340 if (ret > 0)
341 tcp_output(sototcpcb(so));
345 * Check sockets for writing
347 if (FD_ISSET(so->s, writefds)) {
349 * Check for non-blocking, still-connecting sockets
351 if (so->so_state & SS_ISFCONNECTING) {
352 /* Connected */
353 so->so_state &= ~SS_ISFCONNECTING;
355 ret = write(so->s, &ret, 0);
356 if (ret < 0) {
357 /* XXXXX Must fix, zero bytes is a NOP */
358 if (errno == EAGAIN || errno == EWOULDBLOCK ||
359 errno == EINPROGRESS || errno == ENOTCONN)
360 continue;
362 /* else failed */
363 so->so_state = SS_NOFDREF;
365 /* else so->so_state &= ~SS_ISFCONNECTING; */
368 * Continue tcp_input
370 tcp_input((struct mbuf *)NULL, sizeof(struct ip), so);
371 /* continue; */
372 } else
373 ret = sowrite(so);
375 * XXXXX If we wrote something (a lot), there
376 * could be a need for a window update.
377 * In the worst case, the remote will send
378 * a window probe to get things going again
383 * Probe a still-connecting, non-blocking socket
384 * to check if it's still alive
386 #ifdef PROBE_CONN
387 if (so->so_state & SS_ISFCONNECTING) {
388 ret = read(so->s, (char *)&ret, 0);
390 if (ret < 0) {
391 /* XXX */
392 if (errno == EAGAIN || errno == EWOULDBLOCK ||
393 errno == EINPROGRESS || errno == ENOTCONN)
394 continue; /* Still connecting, continue */
396 /* else failed */
397 so->so_state = SS_NOFDREF;
399 /* tcp_input will take care of it */
400 } else {
401 ret = write(so->s, &ret, 0);
402 if (ret < 0) {
403 /* XXX */
404 if (errno == EAGAIN || errno == EWOULDBLOCK ||
405 errno == EINPROGRESS || errno == ENOTCONN)
406 continue;
407 /* else failed */
408 so->so_state = SS_NOFDREF;
409 } else
410 so->so_state &= ~SS_ISFCONNECTING;
413 tcp_input((struct mbuf *)NULL, sizeof(struct ip),so);
414 } /* SS_ISFCONNECTING */
415 #endif
419 * Now UDP sockets.
420 * Incoming packets are sent straight away, they're not buffered.
421 * Incoming UDP data isn't buffered either.
423 for (so = udb.so_next; so != &udb; so = so_next) {
424 so_next = so->so_next;
426 if (so->s != -1 && FD_ISSET(so->s, readfds)) {
427 sorecvfrom(so);
433 * See if we can start outputting
435 if (if_queued && link_up)
436 if_start();
439 #define ETH_ALEN 6
440 #define ETH_HLEN 14
442 #define ETH_P_IP 0x0800 /* Internet Protocol packet */
443 #define ETH_P_ARP 0x0806 /* Address Resolution packet */
445 #define ARPOP_REQUEST 1 /* ARP request */
446 #define ARPOP_REPLY 2 /* ARP reply */
448 struct ethhdr
450 unsigned char h_dest[ETH_ALEN]; /* destination eth addr */
451 unsigned char h_source[ETH_ALEN]; /* source ether addr */
452 unsigned short h_proto; /* packet type ID field */
455 struct arphdr
457 unsigned short ar_hrd; /* format of hardware address */
458 unsigned short ar_pro; /* format of protocol address */
459 unsigned char ar_hln; /* length of hardware address */
460 unsigned char ar_pln; /* length of protocol address */
461 unsigned short ar_op; /* ARP opcode (command) */
464 * Ethernet looks like this : This bit is variable sized however...
466 unsigned char ar_sha[ETH_ALEN]; /* sender hardware address */
467 unsigned char ar_sip[4]; /* sender IP address */
468 unsigned char ar_tha[ETH_ALEN]; /* target hardware address */
469 unsigned char ar_tip[4]; /* target IP address */
472 struct ip_part_header
474 unsigned char filler[12];
475 unsigned char ip_sip[4];
476 unsigned char ip_tip[4];
479 void client_eth_register(const unsigned char *eth_addr, const unsigned char *ip_addr)
481 int host=ip_addr[3];
482 if (memcmp(ip_addr, &special_addr, 3) == 0 && host != 0 && host != 0xff)
484 memcpy(client_ethaddr[host],eth_addr,ETH_ALEN);
485 /*printf("register %02x:%02x:%02x:%02x:%02x:%02x %d.%d.%d.%d\n",
486 eth_addr[0], eth_addr[1], eth_addr[2],
487 eth_addr[3], eth_addr[4], eth_addr[5],
488 ip_addr[0], ip_addr[1], ip_addr[2], ip_addr[3]);*/
492 static void client_eth_get(unsigned char *eth_addr, const unsigned char *ip_addr)
494 int host=ip_addr[3];
495 if (memcmp(ip_addr, &special_addr, 3) == 0 && host != 0 && host != 0xff)
497 memcpy(eth_addr,client_ethaddr[host],ETH_ALEN);
498 /*printf("get %02x:%02x:%02x:%02x:%02x:%02x %d.%d.%d.%d\n",
499 eth_addr[0], eth_addr[1], eth_addr[2],
500 eth_addr[3], eth_addr[4], eth_addr[5],
501 ip_addr[0], ip_addr[1], ip_addr[2], ip_addr[3]);*/
505 static void client_eth_register_ip(const uint8_t *pkt, int pkt_len)
507 struct ethhdr *eh = (struct ethhdr *)pkt;
508 struct ip_part_header *ih=(struct ip_part_header *) (pkt+ETH_HLEN);
509 if (pkt_len >= 20) {
510 client_eth_register(eh->h_source,ih->ip_sip);
514 static void client_eth_get_ip(unsigned char *eth_addr, const uint8_t *pkt, int pkt_len)
516 struct ip_part_header *ih=(struct ip_part_header *) pkt;
517 if (pkt_len >= 20)
518 client_eth_get(eth_addr,ih->ip_tip);
521 void arp_input(const uint8_t *pkt, int pkt_len)
523 struct ethhdr *eh = (struct ethhdr *)pkt;
524 struct arphdr *ah = (struct arphdr *)(pkt + ETH_HLEN);
525 uint8_t arp_reply[ETH_HLEN + sizeof(struct arphdr)];
526 struct ethhdr *reh = (struct ethhdr *)arp_reply;
527 struct arphdr *rah = (struct arphdr *)(arp_reply + ETH_HLEN);
528 int ar_op;
530 ar_op = ntohs(ah->ar_op);
531 switch(ar_op) {
532 case ARPOP_REQUEST:
533 if (!memcmp(ah->ar_tip, &special_addr, 3) &&
534 (ah->ar_tip[3] == CTL_DNS || ah->ar_tip[3] == CTL_ALIAS)) {
536 /* make an ARP request to have the client address */
537 client_eth_register(ah->ar_sha, ah->ar_sip);
539 /* ARP request for alias/dns mac address */
540 memcpy(reh->h_dest, pkt + ETH_ALEN, ETH_ALEN);
541 memcpy(reh->h_source, special_ethaddr, ETH_ALEN - 1);
542 reh->h_source[5] = ah->ar_tip[3];
543 reh->h_proto = htons(ETH_P_ARP);
545 rah->ar_hrd = htons(1);
546 rah->ar_pro = htons(ETH_P_IP);
547 rah->ar_hln = ETH_ALEN;
548 rah->ar_pln = 4;
549 rah->ar_op = htons(ARPOP_REPLY);
550 memcpy(rah->ar_sha, reh->h_source, ETH_ALEN);
551 memcpy(rah->ar_sip, ah->ar_tip, 4);
552 memcpy(rah->ar_tha, ah->ar_sha, ETH_ALEN);
553 memcpy(rah->ar_tip, ah->ar_sip, 4);
555 slirp_output(arp_reply, sizeof(arp_reply));
557 break;
558 default:
559 break;
563 void slirp_input(const uint8_t *pkt, int pkt_len)
565 struct mbuf *m;
566 int proto;
568 if (pkt_len < ETH_HLEN)
569 return;
571 proto = ntohs(*(uint16_t *)(pkt + 12));
572 switch(proto) {
573 case ETH_P_ARP:
574 arp_input(pkt, pkt_len);
575 break;
576 case ETH_P_IP:
577 m = m_get();
578 if (!m)
579 return;
580 m->m_len = pkt_len;
581 memcpy(m->m_data, pkt, pkt_len);
583 client_eth_register_ip(m->m_data, m->m_len);
584 m->m_data += ETH_HLEN;
585 m->m_len -= ETH_HLEN;
587 ip_input(m);
588 break;
589 default:
590 break;
594 /* output the IP packet to the ethernet device */
595 void if_encap(const uint8_t *ip_data, int ip_data_len)
597 uint8_t buf[1600];
598 struct ethhdr *eh = (struct ethhdr *)buf;
600 if (ip_data_len + ETH_HLEN > sizeof(buf))
601 return;
603 client_eth_get_ip(eh->h_dest, ip_data, ip_data_len);
604 memcpy(eh->h_source, special_ethaddr, ETH_ALEN - 1);
605 eh->h_source[5] = CTL_ALIAS;
606 eh->h_proto = htons(ETH_P_IP);
607 memcpy(buf + sizeof(struct ethhdr), ip_data, ip_data_len);
608 slirp_output(buf, ip_data_len + ETH_HLEN);