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;
13 const uint8_t special_ethaddr[6] = {
14 0x52, 0x54, 0x00, 0x12, 0x35, 0x00
17 uint8_t client_ethaddr[6];
23 struct ex_list *exec_list;
25 /* XXX: suppress those select globals */
26 fd_set *global_readfds, *global_writefds, *global_xfds;
30 static int get_dns_addr(struct in_addr *pdns_addr)
32 FIXED_INFO *FixedInfo=NULL;
35 IP_ADDR_STRING *pIPAddr;
36 struct in_addr tmp_addr;
38 FixedInfo = (FIXED_INFO *)GlobalAlloc(GPTR, sizeof(FIXED_INFO));
39 BufLen = sizeof(FIXED_INFO);
41 if (ERROR_BUFFER_OVERFLOW == GetNetworkParams(FixedInfo, &BufLen)) {
43 GlobalFree(FixedInfo);
46 FixedInfo = GlobalAlloc(GPTR, BufLen);
49 if ((ret = GetNetworkParams(FixedInfo, &BufLen)) != ERROR_SUCCESS) {
50 printf("GetNetworkParams failed. ret = %08x\n", (u_int)ret );
52 GlobalFree(FixedInfo);
58 pIPAddr = &(FixedInfo->DnsServerList);
59 inet_aton(pIPAddr->IpAddress.String, &tmp_addr);
60 *pdns_addr = tmp_addr;
62 printf( "DNS Servers:\n" );
63 printf( "DNS Addr:%s\n", pIPAddr->IpAddress.String );
65 pIPAddr = FixedInfo -> DnsServerList.Next;
67 printf( "DNS Addr:%s\n", pIPAddr ->IpAddress.String );
68 pIPAddr = pIPAddr ->Next;
72 GlobalFree(FixedInfo);
80 static int get_dns_addr(struct in_addr *pdns_addr)
86 struct in_addr tmp_addr;
88 f = fopen("/etc/resolv.conf", "r");
92 lprint("IP address of your DNS(s): ");
93 while (fgets(buff, 512, f) != NULL) {
94 if (sscanf(buff, "nameserver%*[ \t]%256s", buff2) == 1) {
95 if (!inet_aton(buff2, &tmp_addr))
97 if (tmp_addr.s_addr == loopback_addr.s_addr)
99 /* If it's the first one, set it to dns_addr */
101 *pdns_addr = tmp_addr;
108 lprint("%s", inet_ntoa(tmp_addr));
120 void slirp_cleanup(void)
126 void slirp_init(void)
128 // debug_init("/tmp/slirp.log", DEBUG_DEFAULT);
133 WSAStartup(MAKEWORD(2,0), &Data);
134 atexit(slirp_cleanup);
143 /* Initialise mbufs *after* setting the MTU */
146 /* set default addresses */
148 inet_aton("127.0.0.1", &loopback_addr);
150 if (get_dns_addr(&dns_addr) < 0) {
151 fprintf(stderr, "Could not get DNS address\n");
155 inet_aton(CTL_SPECIAL, &special_addr);
158 #define CONN_CANFSEND(so) (((so)->so_state & (SS_FCANTSENDMORE|SS_ISFCONNECTED)) == SS_ISFCONNECTED)
159 #define CONN_CANFRCV(so) (((so)->so_state & (SS_FCANTRCVMORE|SS_ISFCONNECTED)) == SS_ISFCONNECTED)
160 #define UPD_NFDS(x) if (nfds < (x)) nfds = (x)
163 * curtime kept to an accuracy of 1ms
166 static void updtime(void)
171 curtime = (u_int)tb.time * (u_int)1000;
172 curtime += (u_int)tb.millitm;
175 static void updtime(void)
177 gettimeofday(&tt, 0);
179 curtime = (u_int)tt.tv_sec * (u_int)1000;
180 curtime += (u_int)tt.tv_usec / (u_int)1000;
182 if ((tt.tv_usec % 1000) >= 500)
187 void slirp_select_fill(int *pnfds,
188 fd_set *readfds, fd_set *writefds, fd_set *xfds)
190 struct socket *so, *so_next;
191 struct timeval timeout;
196 global_readfds = NULL;
197 global_writefds = NULL;
207 * *_slowtimo needs calling if there are IP fragments
208 * in the fragment queue, or there are TCP connections active
210 do_slowtimo = ((tcb.so_next != &tcb) ||
211 ((struct ipasfrag *)&ipq != (struct ipasfrag *)ipq.next));
213 for (so = tcb.so_next; so != &tcb; so = so_next) {
214 so_next = so->so_next;
217 * See if we need a tcp_fasttimo
219 if (time_fasttimo == 0 && so->so_tcpcb->t_flags & TF_DELACK)
220 time_fasttimo = curtime; /* Flag when we want a fasttimo */
223 * NOFDREF can include still connecting to local-host,
224 * newly socreated() sockets etc. Don't want to select these.
226 if (so->so_state & SS_NOFDREF || so->s == -1)
230 * Set for reading sockets which are accepting
232 if (so->so_state & SS_FACCEPTCONN) {
233 FD_SET(so->s, readfds);
239 * Set for writing sockets which are connecting
241 if (so->so_state & SS_ISFCONNECTING) {
242 FD_SET(so->s, writefds);
248 * Set for writing if we are connected, can send more, and
249 * we have something to send
251 if (CONN_CANFSEND(so) && so->so_rcv.sb_cc) {
252 FD_SET(so->s, writefds);
257 * Set for reading (and urgent data) if we are connected, can
258 * receive more, and we have room for it XXX /2 ?
260 if (CONN_CANFRCV(so) && (so->so_snd.sb_cc < (so->so_snd.sb_datalen/2))) {
261 FD_SET(so->s, readfds);
270 for (so = udb.so_next; so != &udb; so = so_next) {
271 so_next = so->so_next;
274 * See if it's timed out
277 if (so->so_expire <= curtime) {
281 do_slowtimo = 1; /* Let socket expire */
285 * When UDP packets are received from over the
286 * link, they're sendto()'d straight away, so
287 * no need for setting for writing
288 * Limit the number of packets queued by this session
289 * to 4. Note that even though we try and limit this
290 * to 4 packets, the session could have more queued
291 * if the packets needed to be fragmented
294 if ((so->so_state & SS_ISFCONNECTED) && so->so_queued <= 4) {
295 FD_SET(so->s, readfds);
302 * Setup timeout to use minimum CPU usage, especially when idle
306 * First, see the timeout needed by *timo
309 timeout.tv_usec = -1;
311 * If a slowtimo is needed, set timeout to 500ms from the last
312 * slow timeout. If a fast timeout is needed, set timeout within
313 * 200ms of when it was requested.
316 /* XXX + 10000 because some select()'s aren't that accurate */
317 timeout.tv_usec = ((500 - (curtime - last_slowtimo)) * 1000) + 10000;
318 if (timeout.tv_usec < 0)
320 else if (timeout.tv_usec > 510000)
321 timeout.tv_usec = 510000;
323 /* Can only fasttimo if we also slowtimo */
325 tmp_time = (200 - (curtime - time_fasttimo)) * 1000;
329 /* Choose the smallest of the 2 */
330 if (tmp_time < timeout.tv_usec)
331 timeout.tv_usec = (u_int)tmp_time;
337 void slirp_select_poll(fd_set *readfds, fd_set *writefds, fd_set *xfds)
339 struct socket *so, *so_next;
342 global_readfds = readfds;
343 global_writefds = writefds;
350 * See if anything has timed out
353 if (time_fasttimo && ((curtime - time_fasttimo) >= 199)) {
357 if (do_slowtimo && ((curtime - last_slowtimo) >= 499)) {
360 last_slowtimo = curtime;
371 for (so = tcb.so_next; so != &tcb; so = so_next) {
372 so_next = so->so_next;
375 * FD_ISSET is meaningless on these sockets
376 * (and they can crash the program)
378 if (so->so_state & SS_NOFDREF || so->s == -1)
383 * This will soread as well, so no need to
384 * test for readfds below if this succeeds
386 if (FD_ISSET(so->s, xfds))
389 * Check sockets for reading
391 else if (FD_ISSET(so->s, readfds)) {
393 * Check for incoming connections
395 if (so->so_state & SS_FACCEPTCONN) {
401 /* Output it if we read something */
403 tcp_output(sototcpcb(so));
407 * Check sockets for writing
409 if (FD_ISSET(so->s, writefds)) {
411 * Check for non-blocking, still-connecting sockets
413 if (so->so_state & SS_ISFCONNECTING) {
415 so->so_state &= ~SS_ISFCONNECTING;
417 ret = send(so->s, &ret, 0, 0);
419 /* XXXXX Must fix, zero bytes is a NOP */
420 if (errno == EAGAIN || errno == EWOULDBLOCK ||
421 errno == EINPROGRESS || errno == ENOTCONN)
425 so->so_state = SS_NOFDREF;
427 /* else so->so_state &= ~SS_ISFCONNECTING; */
432 tcp_input((struct mbuf *)NULL, sizeof(struct ip), so);
437 * XXXXX If we wrote something (a lot), there
438 * could be a need for a window update.
439 * In the worst case, the remote will send
440 * a window probe to get things going again
445 * Probe a still-connecting, non-blocking socket
446 * to check if it's still alive
449 if (so->so_state & SS_ISFCONNECTING) {
450 ret = recv(so->s, (char *)&ret, 0,0);
454 if (errno == EAGAIN || errno == EWOULDBLOCK ||
455 errno == EINPROGRESS || errno == ENOTCONN)
456 continue; /* Still connecting, continue */
459 so->so_state = SS_NOFDREF;
461 /* tcp_input will take care of it */
463 ret = send(so->s, &ret, 0,0);
466 if (errno == EAGAIN || errno == EWOULDBLOCK ||
467 errno == EINPROGRESS || errno == ENOTCONN)
470 so->so_state = SS_NOFDREF;
472 so->so_state &= ~SS_ISFCONNECTING;
475 tcp_input((struct mbuf *)NULL, sizeof(struct ip),so);
476 } /* SS_ISFCONNECTING */
482 * Incoming packets are sent straight away, they're not buffered.
483 * Incoming UDP data isn't buffered either.
485 for (so = udb.so_next; so != &udb; so = so_next) {
486 so_next = so->so_next;
488 if (so->s != -1 && FD_ISSET(so->s, readfds)) {
495 * See if we can start outputting
497 if (if_queued && link_up)
500 /* clear global file descriptor sets.
501 * these reside on the stack in vl.c
502 * so they're unusable if we're not in
503 * slirp_select_fill or slirp_select_poll.
505 global_readfds = NULL;
506 global_writefds = NULL;
513 #define ETH_P_IP 0x0800 /* Internet Protocol packet */
514 #define ETH_P_ARP 0x0806 /* Address Resolution packet */
516 #define ARPOP_REQUEST 1 /* ARP request */
517 #define ARPOP_REPLY 2 /* ARP reply */
521 unsigned char h_dest[ETH_ALEN]; /* destination eth addr */
522 unsigned char h_source[ETH_ALEN]; /* source ether addr */
523 unsigned short h_proto; /* packet type ID field */
528 unsigned short ar_hrd; /* format of hardware address */
529 unsigned short ar_pro; /* format of protocol address */
530 unsigned char ar_hln; /* length of hardware address */
531 unsigned char ar_pln; /* length of protocol address */
532 unsigned short ar_op; /* ARP opcode (command) */
535 * Ethernet looks like this : This bit is variable sized however...
537 unsigned char ar_sha[ETH_ALEN]; /* sender hardware address */
538 unsigned char ar_sip[4]; /* sender IP address */
539 unsigned char ar_tha[ETH_ALEN]; /* target hardware address */
540 unsigned char ar_tip[4]; /* target IP address */
543 void arp_input(const uint8_t *pkt, int pkt_len)
545 struct ethhdr *eh = (struct ethhdr *)pkt;
546 struct arphdr *ah = (struct arphdr *)(pkt + ETH_HLEN);
547 uint8_t arp_reply[ETH_HLEN + sizeof(struct arphdr)];
548 struct ethhdr *reh = (struct ethhdr *)arp_reply;
549 struct arphdr *rah = (struct arphdr *)(arp_reply + ETH_HLEN);
551 struct ex_list *ex_ptr;
553 ar_op = ntohs(ah->ar_op);
556 if (!memcmp(ah->ar_tip, &special_addr, 3)) {
557 if (ah->ar_tip[3] == CTL_DNS || ah->ar_tip[3] == CTL_ALIAS)
559 for (ex_ptr = exec_list; ex_ptr; ex_ptr = ex_ptr->ex_next) {
560 if (ex_ptr->ex_addr == ah->ar_tip[3])
565 /* XXX: make an ARP request to have the client address */
566 memcpy(client_ethaddr, eh->h_source, ETH_ALEN);
568 /* ARP request for alias/dns mac address */
569 memcpy(reh->h_dest, pkt + ETH_ALEN, ETH_ALEN);
570 memcpy(reh->h_source, special_ethaddr, ETH_ALEN - 1);
571 reh->h_source[5] = ah->ar_tip[3];
572 reh->h_proto = htons(ETH_P_ARP);
574 rah->ar_hrd = htons(1);
575 rah->ar_pro = htons(ETH_P_IP);
576 rah->ar_hln = ETH_ALEN;
578 rah->ar_op = htons(ARPOP_REPLY);
579 memcpy(rah->ar_sha, reh->h_source, ETH_ALEN);
580 memcpy(rah->ar_sip, ah->ar_tip, 4);
581 memcpy(rah->ar_tha, ah->ar_sha, ETH_ALEN);
582 memcpy(rah->ar_tip, ah->ar_sip, 4);
583 slirp_output(arp_reply, sizeof(arp_reply));
591 void slirp_input(const uint8_t *pkt, int pkt_len)
596 if (pkt_len < ETH_HLEN)
599 proto = ntohs(*(uint16_t *)(pkt + 12));
602 arp_input(pkt, pkt_len);
609 memcpy(m->m_data, pkt, pkt_len);
611 m->m_data += ETH_HLEN;
612 m->m_len -= ETH_HLEN;
621 /* output the IP packet to the ethernet device */
622 void if_encap(const uint8_t *ip_data, int ip_data_len)
625 struct ethhdr *eh = (struct ethhdr *)buf;
627 if (ip_data_len + ETH_HLEN > sizeof(buf))
630 memcpy(eh->h_dest, client_ethaddr, ETH_ALEN);
631 memcpy(eh->h_source, special_ethaddr, ETH_ALEN - 1);
632 /* XXX: not correct */
633 eh->h_source[5] = CTL_ALIAS;
634 eh->h_proto = htons(ETH_P_IP);
635 memcpy(buf + sizeof(struct ethhdr), ip_data, ip_data_len);
636 slirp_output(buf, ip_data_len + ETH_HLEN);
639 int slirp_redir(int is_udp, int host_port,
640 struct in_addr guest_addr, int guest_port)
643 if (!udp_listen(htons(host_port), guest_addr.s_addr,
644 htons(guest_port), 0))
647 if (!solisten(htons(host_port), guest_addr.s_addr,
648 htons(guest_port), 0))
654 int slirp_add_exec(int do_pty, const char *args, int addr_low_byte,
657 return add_exec(&exec_list, do_pty, (char *)args,
658 addr_low_byte, htons(guest_port));