4 struct in_addr our_addr;
6 struct in_addr dns_addr;
7 /* host loopback address */
8 struct in_addr loopback_addr;
10 /* address for slirp virtual addresses */
11 struct in_addr special_addr;
12 /* virtual address alias for host */
13 struct in_addr alias_addr;
15 const uint8_t special_ethaddr[6] = {
16 0x52, 0x54, 0x00, 0x12, 0x35, 0x00
19 uint8_t client_ethaddr[6];
25 struct ex_list *exec_list;
27 /* XXX: suppress those select globals */
28 fd_set *global_readfds, *global_writefds, *global_xfds;
30 char slirp_hostname[33];
34 static int get_dns_addr(struct in_addr *pdns_addr)
36 FIXED_INFO *FixedInfo=NULL;
39 IP_ADDR_STRING *pIPAddr;
40 struct in_addr tmp_addr;
42 FixedInfo = (FIXED_INFO *)GlobalAlloc(GPTR, sizeof(FIXED_INFO));
43 BufLen = sizeof(FIXED_INFO);
45 if (ERROR_BUFFER_OVERFLOW == GetNetworkParams(FixedInfo, &BufLen)) {
47 GlobalFree(FixedInfo);
50 FixedInfo = GlobalAlloc(GPTR, BufLen);
53 if ((ret = GetNetworkParams(FixedInfo, &BufLen)) != ERROR_SUCCESS) {
54 printf("GetNetworkParams failed. ret = %08x\n", (u_int)ret );
56 GlobalFree(FixedInfo);
62 pIPAddr = &(FixedInfo->DnsServerList);
63 inet_aton(pIPAddr->IpAddress.String, &tmp_addr);
64 *pdns_addr = tmp_addr;
66 printf( "DNS Servers:\n" );
67 printf( "DNS Addr:%s\n", pIPAddr->IpAddress.String );
69 pIPAddr = FixedInfo -> DnsServerList.Next;
71 printf( "DNS Addr:%s\n", pIPAddr ->IpAddress.String );
72 pIPAddr = pIPAddr ->Next;
76 GlobalFree(FixedInfo);
84 static int get_dns_addr(struct in_addr *pdns_addr)
90 struct in_addr tmp_addr;
92 f = fopen("/etc/resolv.conf", "r");
97 lprint("IP address of your DNS(s): ");
99 while (fgets(buff, 512, f) != NULL) {
100 if (sscanf(buff, "nameserver%*[ \t]%256s", buff2) == 1) {
101 if (!inet_aton(buff2, &tmp_addr))
103 if (tmp_addr.s_addr == loopback_addr.s_addr)
105 /* If it's the first one, set it to dns_addr */
107 *pdns_addr = tmp_addr;
120 lprint("%s", inet_ntoa(tmp_addr));
133 void slirp_cleanup(void)
139 void slirp_init(void)
141 // debug_init("/tmp/slirp.log", DEBUG_DEFAULT);
146 WSAStartup(MAKEWORD(2,0), &Data);
147 atexit(slirp_cleanup);
156 /* Initialise mbufs *after* setting the MTU */
159 /* set default addresses */
160 inet_aton("127.0.0.1", &loopback_addr);
162 if (get_dns_addr(&dns_addr) < 0) {
163 dns_addr = loopback_addr;
164 fprintf (stderr, "Warning: No DNS servers found\n");
167 inet_aton(CTL_SPECIAL, &special_addr);
168 alias_addr.s_addr = special_addr.s_addr | htonl(CTL_ALIAS);
172 #define CONN_CANFSEND(so) (((so)->so_state & (SS_FCANTSENDMORE|SS_ISFCONNECTED)) == SS_ISFCONNECTED)
173 #define CONN_CANFRCV(so) (((so)->so_state & (SS_FCANTRCVMORE|SS_ISFCONNECTED)) == SS_ISFCONNECTED)
174 #define UPD_NFDS(x) if (nfds < (x)) nfds = (x)
177 * curtime kept to an accuracy of 1ms
180 static void updtime(void)
185 curtime = (u_int)tb.time * (u_int)1000;
186 curtime += (u_int)tb.millitm;
189 static void updtime(void)
191 gettimeofday(&tt, 0);
193 curtime = (u_int)tt.tv_sec * (u_int)1000;
194 curtime += (u_int)tt.tv_usec / (u_int)1000;
196 if ((tt.tv_usec % 1000) >= 500)
201 void slirp_select_fill(int *pnfds,
202 fd_set *readfds, fd_set *writefds, fd_set *xfds)
204 struct socket *so, *so_next;
205 struct timeval timeout;
210 global_readfds = NULL;
211 global_writefds = NULL;
221 * *_slowtimo needs calling if there are IP fragments
222 * in the fragment queue, or there are TCP connections active
224 do_slowtimo = ((tcb.so_next != &tcb) ||
225 ((struct ipasfrag *)&ipq != (struct ipasfrag *)ipq.next));
227 for (so = tcb.so_next; so != &tcb; so = so_next) {
228 so_next = so->so_next;
231 * See if we need a tcp_fasttimo
233 if (time_fasttimo == 0 && so->so_tcpcb->t_flags & TF_DELACK)
234 time_fasttimo = curtime; /* Flag when we want a fasttimo */
237 * NOFDREF can include still connecting to local-host,
238 * newly socreated() sockets etc. Don't want to select these.
240 if (so->so_state & SS_NOFDREF || so->s == -1)
244 * Set for reading sockets which are accepting
246 if (so->so_state & SS_FACCEPTCONN) {
247 FD_SET(so->s, readfds);
253 * Set for writing sockets which are connecting
255 if (so->so_state & SS_ISFCONNECTING) {
256 FD_SET(so->s, writefds);
262 * Set for writing if we are connected, can send more, and
263 * we have something to send
265 if (CONN_CANFSEND(so) && so->so_rcv.sb_cc) {
266 FD_SET(so->s, writefds);
271 * Set for reading (and urgent data) if we are connected, can
272 * receive more, and we have room for it XXX /2 ?
274 if (CONN_CANFRCV(so) && (so->so_snd.sb_cc < (so->so_snd.sb_datalen/2))) {
275 FD_SET(so->s, readfds);
284 for (so = udb.so_next; so != &udb; so = so_next) {
285 so_next = so->so_next;
288 * See if it's timed out
291 if (so->so_expire <= curtime) {
295 do_slowtimo = 1; /* Let socket expire */
299 * When UDP packets are received from over the
300 * link, they're sendto()'d straight away, so
301 * no need for setting for writing
302 * Limit the number of packets queued by this session
303 * to 4. Note that even though we try and limit this
304 * to 4 packets, the session could have more queued
305 * if the packets needed to be fragmented
308 if ((so->so_state & SS_ISFCONNECTED) && so->so_queued <= 4) {
309 FD_SET(so->s, readfds);
316 * Setup timeout to use minimum CPU usage, especially when idle
320 * First, see the timeout needed by *timo
323 timeout.tv_usec = -1;
325 * If a slowtimo is needed, set timeout to 500ms from the last
326 * slow timeout. If a fast timeout is needed, set timeout within
327 * 200ms of when it was requested.
330 /* XXX + 10000 because some select()'s aren't that accurate */
331 timeout.tv_usec = ((500 - (curtime - last_slowtimo)) * 1000) + 10000;
332 if (timeout.tv_usec < 0)
334 else if (timeout.tv_usec > 510000)
335 timeout.tv_usec = 510000;
337 /* Can only fasttimo if we also slowtimo */
339 tmp_time = (200 - (curtime - time_fasttimo)) * 1000;
343 /* Choose the smallest of the 2 */
344 if (tmp_time < timeout.tv_usec)
345 timeout.tv_usec = (u_int)tmp_time;
351 void slirp_select_poll(fd_set *readfds, fd_set *writefds, fd_set *xfds)
353 struct socket *so, *so_next;
356 global_readfds = readfds;
357 global_writefds = writefds;
364 * See if anything has timed out
367 if (time_fasttimo && ((curtime - time_fasttimo) >= 2)) {
371 if (do_slowtimo && ((curtime - last_slowtimo) >= 499)) {
374 last_slowtimo = curtime;
385 for (so = tcb.so_next; so != &tcb; so = so_next) {
386 so_next = so->so_next;
389 * FD_ISSET is meaningless on these sockets
390 * (and they can crash the program)
392 if (so->so_state & SS_NOFDREF || so->s == -1)
397 * This will soread as well, so no need to
398 * test for readfds below if this succeeds
400 if (FD_ISSET(so->s, xfds))
403 * Check sockets for reading
405 else if (FD_ISSET(so->s, readfds)) {
407 * Check for incoming connections
409 if (so->so_state & SS_FACCEPTCONN) {
415 /* Output it if we read something */
417 tcp_output(sototcpcb(so));
421 * Check sockets for writing
423 if (FD_ISSET(so->s, writefds)) {
425 * Check for non-blocking, still-connecting sockets
427 if (so->so_state & SS_ISFCONNECTING) {
429 so->so_state &= ~SS_ISFCONNECTING;
431 ret = send(so->s, &ret, 0, 0);
433 /* XXXXX Must fix, zero bytes is a NOP */
434 if (errno == EAGAIN || errno == EWOULDBLOCK ||
435 errno == EINPROGRESS || errno == ENOTCONN)
439 so->so_state = SS_NOFDREF;
441 /* else so->so_state &= ~SS_ISFCONNECTING; */
446 tcp_input((struct mbuf *)NULL, sizeof(struct ip), so);
451 * XXXXX If we wrote something (a lot), there
452 * could be a need for a window update.
453 * In the worst case, the remote will send
454 * a window probe to get things going again
459 * Probe a still-connecting, non-blocking socket
460 * to check if it's still alive
463 if (so->so_state & SS_ISFCONNECTING) {
464 ret = recv(so->s, (char *)&ret, 0,0);
468 if (errno == EAGAIN || errno == EWOULDBLOCK ||
469 errno == EINPROGRESS || errno == ENOTCONN)
470 continue; /* Still connecting, continue */
473 so->so_state = SS_NOFDREF;
475 /* tcp_input will take care of it */
477 ret = send(so->s, &ret, 0,0);
480 if (errno == EAGAIN || errno == EWOULDBLOCK ||
481 errno == EINPROGRESS || errno == ENOTCONN)
484 so->so_state = SS_NOFDREF;
486 so->so_state &= ~SS_ISFCONNECTING;
489 tcp_input((struct mbuf *)NULL, sizeof(struct ip),so);
490 } /* SS_ISFCONNECTING */
496 * Incoming packets are sent straight away, they're not buffered.
497 * Incoming UDP data isn't buffered either.
499 for (so = udb.so_next; so != &udb; so = so_next) {
500 so_next = so->so_next;
502 if (so->s != -1 && FD_ISSET(so->s, readfds)) {
509 * See if we can start outputting
511 if (if_queued && link_up)
514 /* clear global file descriptor sets.
515 * these reside on the stack in vl.c
516 * so they're unusable if we're not in
517 * slirp_select_fill or slirp_select_poll.
519 global_readfds = NULL;
520 global_writefds = NULL;
527 #define ETH_P_IP 0x0800 /* Internet Protocol packet */
528 #define ETH_P_ARP 0x0806 /* Address Resolution packet */
530 #define ARPOP_REQUEST 1 /* ARP request */
531 #define ARPOP_REPLY 2 /* ARP reply */
535 unsigned char h_dest[ETH_ALEN]; /* destination eth addr */
536 unsigned char h_source[ETH_ALEN]; /* source ether addr */
537 unsigned short h_proto; /* packet type ID field */
542 unsigned short ar_hrd; /* format of hardware address */
543 unsigned short ar_pro; /* format of protocol address */
544 unsigned char ar_hln; /* length of hardware address */
545 unsigned char ar_pln; /* length of protocol address */
546 unsigned short ar_op; /* ARP opcode (command) */
549 * Ethernet looks like this : This bit is variable sized however...
551 unsigned char ar_sha[ETH_ALEN]; /* sender hardware address */
552 unsigned char ar_sip[4]; /* sender IP address */
553 unsigned char ar_tha[ETH_ALEN]; /* target hardware address */
554 unsigned char ar_tip[4]; /* target IP address */
557 void arp_input(const uint8_t *pkt, int pkt_len)
559 struct ethhdr *eh = (struct ethhdr *)pkt;
560 struct arphdr *ah = (struct arphdr *)(pkt + ETH_HLEN);
561 uint8_t arp_reply[ETH_HLEN + sizeof(struct arphdr)];
562 struct ethhdr *reh = (struct ethhdr *)arp_reply;
563 struct arphdr *rah = (struct arphdr *)(arp_reply + ETH_HLEN);
565 struct ex_list *ex_ptr;
567 ar_op = ntohs(ah->ar_op);
570 if (!memcmp(ah->ar_tip, &special_addr, 3)) {
571 if (ah->ar_tip[3] == CTL_DNS || ah->ar_tip[3] == CTL_ALIAS)
573 for (ex_ptr = exec_list; ex_ptr; ex_ptr = ex_ptr->ex_next) {
574 if (ex_ptr->ex_addr == ah->ar_tip[3])
579 /* XXX: make an ARP request to have the client address */
580 memcpy(client_ethaddr, eh->h_source, ETH_ALEN);
582 /* ARP request for alias/dns mac address */
583 memcpy(reh->h_dest, pkt + ETH_ALEN, ETH_ALEN);
584 memcpy(reh->h_source, special_ethaddr, ETH_ALEN - 1);
585 reh->h_source[5] = ah->ar_tip[3];
586 reh->h_proto = htons(ETH_P_ARP);
588 rah->ar_hrd = htons(1);
589 rah->ar_pro = htons(ETH_P_IP);
590 rah->ar_hln = ETH_ALEN;
592 rah->ar_op = htons(ARPOP_REPLY);
593 memcpy(rah->ar_sha, reh->h_source, ETH_ALEN);
594 memcpy(rah->ar_sip, ah->ar_tip, 4);
595 memcpy(rah->ar_tha, ah->ar_sha, ETH_ALEN);
596 memcpy(rah->ar_tip, ah->ar_sip, 4);
597 slirp_output(arp_reply, sizeof(arp_reply));
605 void slirp_input(const uint8_t *pkt, int pkt_len)
610 if (pkt_len < ETH_HLEN)
613 proto = ntohs(*(uint16_t *)(pkt + 12));
616 arp_input(pkt, pkt_len);
622 /* Note: we add to align the IP header */
623 m->m_len = pkt_len + 2;
624 memcpy(m->m_data + 2, pkt, pkt_len);
626 m->m_data += 2 + ETH_HLEN;
627 m->m_len -= 2 + ETH_HLEN;
636 /* output the IP packet to the ethernet device */
637 void if_encap(const uint8_t *ip_data, int ip_data_len)
640 struct ethhdr *eh = (struct ethhdr *)buf;
642 if (ip_data_len + ETH_HLEN > sizeof(buf))
645 memcpy(eh->h_dest, client_ethaddr, ETH_ALEN);
646 memcpy(eh->h_source, special_ethaddr, ETH_ALEN - 1);
647 /* XXX: not correct */
648 eh->h_source[5] = CTL_ALIAS;
649 eh->h_proto = htons(ETH_P_IP);
650 memcpy(buf + sizeof(struct ethhdr), ip_data, ip_data_len);
651 slirp_output(buf, ip_data_len + ETH_HLEN);
654 int slirp_redir(int is_udp, int host_port,
655 struct in_addr guest_addr, int guest_port)
658 if (!udp_listen(htons(host_port), guest_addr.s_addr,
659 htons(guest_port), 0))
662 if (!solisten(htons(host_port), guest_addr.s_addr,
663 htons(guest_port), 0))
669 int slirp_add_exec(int do_pty, const char *args, int addr_low_byte,
672 return add_exec(&exec_list, do_pty, (char *)args,
673 addr_low_byte, htons(guest_port));