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");
96 lprint("IP address of your DNS(s): ");
97 while (fgets(buff, 512, f) != NULL) {
98 if (sscanf(buff, "nameserver%*[ \t]%256s", buff2) == 1) {
99 if (!inet_aton(buff2, &tmp_addr))
101 if (tmp_addr.s_addr == loopback_addr.s_addr)
103 /* If it's the first one, set it to dns_addr */
105 *pdns_addr = tmp_addr;
112 lprint("%s", inet_ntoa(tmp_addr));
124 void slirp_cleanup(void)
130 void slirp_init(void)
132 // debug_init("/tmp/slirp.log", DEBUG_DEFAULT);
137 WSAStartup(MAKEWORD(2,0), &Data);
138 atexit(slirp_cleanup);
147 /* Initialise mbufs *after* setting the MTU */
150 /* set default addresses */
151 inet_aton("127.0.0.1", &loopback_addr);
153 if (get_dns_addr(&dns_addr) < 0) {
154 dns_addr = loopback_addr;
155 fprintf (stderr, "Warning: No DNS servers found\n");
158 inet_aton(CTL_SPECIAL, &special_addr);
159 alias_addr.s_addr = special_addr.s_addr | htonl(CTL_ALIAS);
163 #define CONN_CANFSEND(so) (((so)->so_state & (SS_FCANTSENDMORE|SS_ISFCONNECTED)) == SS_ISFCONNECTED)
164 #define CONN_CANFRCV(so) (((so)->so_state & (SS_FCANTRCVMORE|SS_ISFCONNECTED)) == SS_ISFCONNECTED)
165 #define UPD_NFDS(x) if (nfds < (x)) nfds = (x)
168 * curtime kept to an accuracy of 1ms
171 static void updtime(void)
176 curtime = (u_int)tb.time * (u_int)1000;
177 curtime += (u_int)tb.millitm;
180 static void updtime(void)
182 gettimeofday(&tt, 0);
184 curtime = (u_int)tt.tv_sec * (u_int)1000;
185 curtime += (u_int)tt.tv_usec / (u_int)1000;
187 if ((tt.tv_usec % 1000) >= 500)
192 void slirp_select_fill(int *pnfds,
193 fd_set *readfds, fd_set *writefds, fd_set *xfds)
195 struct socket *so, *so_next;
196 struct timeval timeout;
201 global_readfds = NULL;
202 global_writefds = NULL;
212 * *_slowtimo needs calling if there are IP fragments
213 * in the fragment queue, or there are TCP connections active
215 do_slowtimo = ((tcb.so_next != &tcb) ||
216 ((struct ipasfrag *)&ipq != (struct ipasfrag *)ipq.next));
218 for (so = tcb.so_next; so != &tcb; so = so_next) {
219 so_next = so->so_next;
222 * See if we need a tcp_fasttimo
224 if (time_fasttimo == 0 && so->so_tcpcb->t_flags & TF_DELACK)
225 time_fasttimo = curtime; /* Flag when we want a fasttimo */
228 * NOFDREF can include still connecting to local-host,
229 * newly socreated() sockets etc. Don't want to select these.
231 if (so->so_state & SS_NOFDREF || so->s == -1)
235 * Set for reading sockets which are accepting
237 if (so->so_state & SS_FACCEPTCONN) {
238 FD_SET(so->s, readfds);
244 * Set for writing sockets which are connecting
246 if (so->so_state & SS_ISFCONNECTING) {
247 FD_SET(so->s, writefds);
253 * Set for writing if we are connected, can send more, and
254 * we have something to send
256 if (CONN_CANFSEND(so) && so->so_rcv.sb_cc) {
257 FD_SET(so->s, writefds);
262 * Set for reading (and urgent data) if we are connected, can
263 * receive more, and we have room for it XXX /2 ?
265 if (CONN_CANFRCV(so) && (so->so_snd.sb_cc < (so->so_snd.sb_datalen/2))) {
266 FD_SET(so->s, readfds);
275 for (so = udb.so_next; so != &udb; so = so_next) {
276 so_next = so->so_next;
279 * See if it's timed out
282 if (so->so_expire <= curtime) {
286 do_slowtimo = 1; /* Let socket expire */
290 * When UDP packets are received from over the
291 * link, they're sendto()'d straight away, so
292 * no need for setting for writing
293 * Limit the number of packets queued by this session
294 * to 4. Note that even though we try and limit this
295 * to 4 packets, the session could have more queued
296 * if the packets needed to be fragmented
299 if ((so->so_state & SS_ISFCONNECTED) && so->so_queued <= 4) {
300 FD_SET(so->s, readfds);
307 * Setup timeout to use minimum CPU usage, especially when idle
311 * First, see the timeout needed by *timo
314 timeout.tv_usec = -1;
316 * If a slowtimo is needed, set timeout to 500ms from the last
317 * slow timeout. If a fast timeout is needed, set timeout within
318 * 200ms of when it was requested.
321 /* XXX + 10000 because some select()'s aren't that accurate */
322 timeout.tv_usec = ((500 - (curtime - last_slowtimo)) * 1000) + 10000;
323 if (timeout.tv_usec < 0)
325 else if (timeout.tv_usec > 510000)
326 timeout.tv_usec = 510000;
328 /* Can only fasttimo if we also slowtimo */
330 tmp_time = (200 - (curtime - time_fasttimo)) * 1000;
334 /* Choose the smallest of the 2 */
335 if (tmp_time < timeout.tv_usec)
336 timeout.tv_usec = (u_int)tmp_time;
342 void slirp_select_poll(fd_set *readfds, fd_set *writefds, fd_set *xfds)
344 struct socket *so, *so_next;
347 global_readfds = readfds;
348 global_writefds = writefds;
355 * See if anything has timed out
358 if (time_fasttimo && ((curtime - time_fasttimo) >= 2)) {
362 if (do_slowtimo && ((curtime - last_slowtimo) >= 499)) {
365 last_slowtimo = curtime;
376 for (so = tcb.so_next; so != &tcb; so = so_next) {
377 so_next = so->so_next;
380 * FD_ISSET is meaningless on these sockets
381 * (and they can crash the program)
383 if (so->so_state & SS_NOFDREF || so->s == -1)
388 * This will soread as well, so no need to
389 * test for readfds below if this succeeds
391 if (FD_ISSET(so->s, xfds))
394 * Check sockets for reading
396 else if (FD_ISSET(so->s, readfds)) {
398 * Check for incoming connections
400 if (so->so_state & SS_FACCEPTCONN) {
406 /* Output it if we read something */
408 tcp_output(sototcpcb(so));
412 * Check sockets for writing
414 if (FD_ISSET(so->s, writefds)) {
416 * Check for non-blocking, still-connecting sockets
418 if (so->so_state & SS_ISFCONNECTING) {
420 so->so_state &= ~SS_ISFCONNECTING;
422 ret = send(so->s, &ret, 0, 0);
424 /* XXXXX Must fix, zero bytes is a NOP */
425 if (errno == EAGAIN || errno == EWOULDBLOCK ||
426 errno == EINPROGRESS || errno == ENOTCONN)
430 so->so_state = SS_NOFDREF;
432 /* else so->so_state &= ~SS_ISFCONNECTING; */
437 tcp_input((struct mbuf *)NULL, sizeof(struct ip), so);
442 * XXXXX If we wrote something (a lot), there
443 * could be a need for a window update.
444 * In the worst case, the remote will send
445 * a window probe to get things going again
450 * Probe a still-connecting, non-blocking socket
451 * to check if it's still alive
454 if (so->so_state & SS_ISFCONNECTING) {
455 ret = recv(so->s, (char *)&ret, 0,0);
459 if (errno == EAGAIN || errno == EWOULDBLOCK ||
460 errno == EINPROGRESS || errno == ENOTCONN)
461 continue; /* Still connecting, continue */
464 so->so_state = SS_NOFDREF;
466 /* tcp_input will take care of it */
468 ret = send(so->s, &ret, 0,0);
471 if (errno == EAGAIN || errno == EWOULDBLOCK ||
472 errno == EINPROGRESS || errno == ENOTCONN)
475 so->so_state = SS_NOFDREF;
477 so->so_state &= ~SS_ISFCONNECTING;
480 tcp_input((struct mbuf *)NULL, sizeof(struct ip),so);
481 } /* SS_ISFCONNECTING */
487 * Incoming packets are sent straight away, they're not buffered.
488 * Incoming UDP data isn't buffered either.
490 for (so = udb.so_next; so != &udb; so = so_next) {
491 so_next = so->so_next;
493 if (so->s != -1 && FD_ISSET(so->s, readfds)) {
500 * See if we can start outputting
502 if (if_queued && link_up)
505 /* clear global file descriptor sets.
506 * these reside on the stack in vl.c
507 * so they're unusable if we're not in
508 * slirp_select_fill or slirp_select_poll.
510 global_readfds = NULL;
511 global_writefds = NULL;
518 #define ETH_P_IP 0x0800 /* Internet Protocol packet */
519 #define ETH_P_ARP 0x0806 /* Address Resolution packet */
521 #define ARPOP_REQUEST 1 /* ARP request */
522 #define ARPOP_REPLY 2 /* ARP reply */
526 unsigned char h_dest[ETH_ALEN]; /* destination eth addr */
527 unsigned char h_source[ETH_ALEN]; /* source ether addr */
528 unsigned short h_proto; /* packet type ID field */
533 unsigned short ar_hrd; /* format of hardware address */
534 unsigned short ar_pro; /* format of protocol address */
535 unsigned char ar_hln; /* length of hardware address */
536 unsigned char ar_pln; /* length of protocol address */
537 unsigned short ar_op; /* ARP opcode (command) */
540 * Ethernet looks like this : This bit is variable sized however...
542 unsigned char ar_sha[ETH_ALEN]; /* sender hardware address */
543 unsigned char ar_sip[4]; /* sender IP address */
544 unsigned char ar_tha[ETH_ALEN]; /* target hardware address */
545 unsigned char ar_tip[4]; /* target IP address */
548 void arp_input(const uint8_t *pkt, int pkt_len)
550 struct ethhdr *eh = (struct ethhdr *)pkt;
551 struct arphdr *ah = (struct arphdr *)(pkt + ETH_HLEN);
552 uint8_t arp_reply[ETH_HLEN + sizeof(struct arphdr)];
553 struct ethhdr *reh = (struct ethhdr *)arp_reply;
554 struct arphdr *rah = (struct arphdr *)(arp_reply + ETH_HLEN);
556 struct ex_list *ex_ptr;
558 ar_op = ntohs(ah->ar_op);
561 if (!memcmp(ah->ar_tip, &special_addr, 3)) {
562 if (ah->ar_tip[3] == CTL_DNS || ah->ar_tip[3] == CTL_ALIAS)
564 for (ex_ptr = exec_list; ex_ptr; ex_ptr = ex_ptr->ex_next) {
565 if (ex_ptr->ex_addr == ah->ar_tip[3])
570 /* XXX: make an ARP request to have the client address */
571 memcpy(client_ethaddr, eh->h_source, ETH_ALEN);
573 /* ARP request for alias/dns mac address */
574 memcpy(reh->h_dest, pkt + ETH_ALEN, ETH_ALEN);
575 memcpy(reh->h_source, special_ethaddr, ETH_ALEN - 1);
576 reh->h_source[5] = ah->ar_tip[3];
577 reh->h_proto = htons(ETH_P_ARP);
579 rah->ar_hrd = htons(1);
580 rah->ar_pro = htons(ETH_P_IP);
581 rah->ar_hln = ETH_ALEN;
583 rah->ar_op = htons(ARPOP_REPLY);
584 memcpy(rah->ar_sha, reh->h_source, ETH_ALEN);
585 memcpy(rah->ar_sip, ah->ar_tip, 4);
586 memcpy(rah->ar_tha, ah->ar_sha, ETH_ALEN);
587 memcpy(rah->ar_tip, ah->ar_sip, 4);
588 slirp_output(arp_reply, sizeof(arp_reply));
596 void slirp_input(const uint8_t *pkt, int pkt_len)
601 if (pkt_len < ETH_HLEN)
604 proto = ntohs(*(uint16_t *)(pkt + 12));
607 arp_input(pkt, pkt_len);
613 /* Note: we add to align the IP header */
614 m->m_len = pkt_len + 2;
615 memcpy(m->m_data + 2, pkt, pkt_len);
617 m->m_data += 2 + ETH_HLEN;
618 m->m_len -= 2 + ETH_HLEN;
627 /* output the IP packet to the ethernet device */
628 void if_encap(const uint8_t *ip_data, int ip_data_len)
631 struct ethhdr *eh = (struct ethhdr *)buf;
633 if (ip_data_len + ETH_HLEN > sizeof(buf))
636 memcpy(eh->h_dest, client_ethaddr, ETH_ALEN);
637 memcpy(eh->h_source, special_ethaddr, ETH_ALEN - 1);
638 /* XXX: not correct */
639 eh->h_source[5] = CTL_ALIAS;
640 eh->h_proto = htons(ETH_P_IP);
641 memcpy(buf + sizeof(struct ethhdr), ip_data, ip_data_len);
642 slirp_output(buf, ip_data_len + ETH_HLEN);
645 int slirp_redir(int is_udp, int host_port,
646 struct in_addr guest_addr, int guest_port)
649 if (!udp_listen(htons(host_port), guest_addr.s_addr,
650 htons(guest_port), 0))
653 if (!solisten(htons(host_port), guest_addr.s_addr,
654 htons(guest_port), 0))
660 int slirp_add_exec(int do_pty, const char *args, int addr_low_byte,
663 return add_exec(&exec_list, do_pty, (char *)args,
664 addr_low_byte, htons(guest_port));