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
;
28 const char slirp_hostname
[33];
32 static int get_dns_addr(struct in_addr
*pdns_addr
)
34 FIXED_INFO
*FixedInfo
=NULL
;
37 IP_ADDR_STRING
*pIPAddr
;
38 struct in_addr tmp_addr
;
40 FixedInfo
= (FIXED_INFO
*)GlobalAlloc(GPTR
, sizeof(FIXED_INFO
));
41 BufLen
= sizeof(FIXED_INFO
);
43 if (ERROR_BUFFER_OVERFLOW
== GetNetworkParams(FixedInfo
, &BufLen
)) {
45 GlobalFree(FixedInfo
);
48 FixedInfo
= GlobalAlloc(GPTR
, BufLen
);
51 if ((ret
= GetNetworkParams(FixedInfo
, &BufLen
)) != ERROR_SUCCESS
) {
52 printf("GetNetworkParams failed. ret = %08x\n", (u_int
)ret
);
54 GlobalFree(FixedInfo
);
60 pIPAddr
= &(FixedInfo
->DnsServerList
);
61 inet_aton(pIPAddr
->IpAddress
.String
, &tmp_addr
);
62 *pdns_addr
= tmp_addr
;
64 printf( "DNS Servers:\n" );
65 printf( "DNS Addr:%s\n", pIPAddr
->IpAddress
.String
);
67 pIPAddr
= FixedInfo
-> DnsServerList
.Next
;
69 printf( "DNS Addr:%s\n", pIPAddr
->IpAddress
.String
);
70 pIPAddr
= pIPAddr
->Next
;
74 GlobalFree(FixedInfo
);
82 static int get_dns_addr(struct in_addr
*pdns_addr
)
88 struct in_addr tmp_addr
;
90 f
= fopen("/etc/resolv.conf", "r");
94 lprint("IP address of your DNS(s): ");
95 while (fgets(buff
, 512, f
) != NULL
) {
96 if (sscanf(buff
, "nameserver%*[ \t]%256s", buff2
) == 1) {
97 if (!inet_aton(buff2
, &tmp_addr
))
99 if (tmp_addr
.s_addr
== loopback_addr
.s_addr
)
101 /* If it's the first one, set it to dns_addr */
103 *pdns_addr
= tmp_addr
;
110 lprint("%s", inet_ntoa(tmp_addr
));
122 void slirp_cleanup(void)
128 void slirp_init(void)
130 // debug_init("/tmp/slirp.log", DEBUG_DEFAULT);
135 WSAStartup(MAKEWORD(2,0), &Data
);
136 atexit(slirp_cleanup
);
145 /* Initialise mbufs *after* setting the MTU */
148 /* set default addresses */
150 inet_aton("127.0.0.1", &loopback_addr
);
152 if (get_dns_addr(&dns_addr
) < 0) {
153 dns_addr
= loopback_addr
;
154 fprintf (stderr
, "Warning: No DNS servers found\n");
157 inet_aton(CTL_SPECIAL
, &special_addr
);
160 #define CONN_CANFSEND(so) (((so)->so_state & (SS_FCANTSENDMORE|SS_ISFCONNECTED)) == SS_ISFCONNECTED)
161 #define CONN_CANFRCV(so) (((so)->so_state & (SS_FCANTRCVMORE|SS_ISFCONNECTED)) == SS_ISFCONNECTED)
162 #define UPD_NFDS(x) if (nfds < (x)) nfds = (x)
165 * curtime kept to an accuracy of 1ms
168 static void updtime(void)
173 curtime
= (u_int
)tb
.time
* (u_int
)1000;
174 curtime
+= (u_int
)tb
.millitm
;
177 static void updtime(void)
179 gettimeofday(&tt
, 0);
181 curtime
= (u_int
)tt
.tv_sec
* (u_int
)1000;
182 curtime
+= (u_int
)tt
.tv_usec
/ (u_int
)1000;
184 if ((tt
.tv_usec
% 1000) >= 500)
189 void slirp_select_fill(int *pnfds
,
190 fd_set
*readfds
, fd_set
*writefds
, fd_set
*xfds
)
192 struct socket
*so
, *so_next
;
193 struct timeval timeout
;
198 global_readfds
= NULL
;
199 global_writefds
= NULL
;
209 * *_slowtimo needs calling if there are IP fragments
210 * in the fragment queue, or there are TCP connections active
212 do_slowtimo
= ((tcb
.so_next
!= &tcb
) ||
213 ((struct ipasfrag
*)&ipq
!= (struct ipasfrag
*)ipq
.next
));
215 for (so
= tcb
.so_next
; so
!= &tcb
; so
= so_next
) {
216 so_next
= so
->so_next
;
219 * See if we need a tcp_fasttimo
221 if (time_fasttimo
== 0 && so
->so_tcpcb
->t_flags
& TF_DELACK
)
222 time_fasttimo
= curtime
; /* Flag when we want a fasttimo */
225 * NOFDREF can include still connecting to local-host,
226 * newly socreated() sockets etc. Don't want to select these.
228 if (so
->so_state
& SS_NOFDREF
|| so
->s
== -1)
232 * Set for reading sockets which are accepting
234 if (so
->so_state
& SS_FACCEPTCONN
) {
235 FD_SET(so
->s
, readfds
);
241 * Set for writing sockets which are connecting
243 if (so
->so_state
& SS_ISFCONNECTING
) {
244 FD_SET(so
->s
, writefds
);
250 * Set for writing if we are connected, can send more, and
251 * we have something to send
253 if (CONN_CANFSEND(so
) && so
->so_rcv
.sb_cc
) {
254 FD_SET(so
->s
, writefds
);
259 * Set for reading (and urgent data) if we are connected, can
260 * receive more, and we have room for it XXX /2 ?
262 if (CONN_CANFRCV(so
) && (so
->so_snd
.sb_cc
< (so
->so_snd
.sb_datalen
/2))) {
263 FD_SET(so
->s
, readfds
);
272 for (so
= udb
.so_next
; so
!= &udb
; so
= so_next
) {
273 so_next
= so
->so_next
;
276 * See if it's timed out
279 if (so
->so_expire
<= curtime
) {
283 do_slowtimo
= 1; /* Let socket expire */
287 * When UDP packets are received from over the
288 * link, they're sendto()'d straight away, so
289 * no need for setting for writing
290 * Limit the number of packets queued by this session
291 * to 4. Note that even though we try and limit this
292 * to 4 packets, the session could have more queued
293 * if the packets needed to be fragmented
296 if ((so
->so_state
& SS_ISFCONNECTED
) && so
->so_queued
<= 4) {
297 FD_SET(so
->s
, readfds
);
304 * Setup timeout to use minimum CPU usage, especially when idle
308 * First, see the timeout needed by *timo
311 timeout
.tv_usec
= -1;
313 * If a slowtimo is needed, set timeout to 500ms from the last
314 * slow timeout. If a fast timeout is needed, set timeout within
315 * 200ms of when it was requested.
318 /* XXX + 10000 because some select()'s aren't that accurate */
319 timeout
.tv_usec
= ((500 - (curtime
- last_slowtimo
)) * 1000) + 10000;
320 if (timeout
.tv_usec
< 0)
322 else if (timeout
.tv_usec
> 510000)
323 timeout
.tv_usec
= 510000;
325 /* Can only fasttimo if we also slowtimo */
327 tmp_time
= (200 - (curtime
- time_fasttimo
)) * 1000;
331 /* Choose the smallest of the 2 */
332 if (tmp_time
< timeout
.tv_usec
)
333 timeout
.tv_usec
= (u_int
)tmp_time
;
339 void slirp_select_poll(fd_set
*readfds
, fd_set
*writefds
, fd_set
*xfds
)
341 struct socket
*so
, *so_next
;
344 global_readfds
= readfds
;
345 global_writefds
= writefds
;
352 * See if anything has timed out
355 if (time_fasttimo
&& ((curtime
- time_fasttimo
) >= 2)) {
359 if (do_slowtimo
&& ((curtime
- last_slowtimo
) >= 499)) {
362 last_slowtimo
= curtime
;
373 for (so
= tcb
.so_next
; so
!= &tcb
; so
= so_next
) {
374 so_next
= so
->so_next
;
377 * FD_ISSET is meaningless on these sockets
378 * (and they can crash the program)
380 if (so
->so_state
& SS_NOFDREF
|| so
->s
== -1)
385 * This will soread as well, so no need to
386 * test for readfds below if this succeeds
388 if (FD_ISSET(so
->s
, xfds
))
391 * Check sockets for reading
393 else if (FD_ISSET(so
->s
, readfds
)) {
395 * Check for incoming connections
397 if (so
->so_state
& SS_FACCEPTCONN
) {
403 /* Output it if we read something */
405 tcp_output(sototcpcb(so
));
409 * Check sockets for writing
411 if (FD_ISSET(so
->s
, writefds
)) {
413 * Check for non-blocking, still-connecting sockets
415 if (so
->so_state
& SS_ISFCONNECTING
) {
417 so
->so_state
&= ~SS_ISFCONNECTING
;
419 ret
= send(so
->s
, &ret
, 0, 0);
421 /* XXXXX Must fix, zero bytes is a NOP */
422 if (errno
== EAGAIN
|| errno
== EWOULDBLOCK
||
423 errno
== EINPROGRESS
|| errno
== ENOTCONN
)
427 so
->so_state
= SS_NOFDREF
;
429 /* else so->so_state &= ~SS_ISFCONNECTING; */
434 tcp_input((struct mbuf
*)NULL
, sizeof(struct ip
), so
);
439 * XXXXX If we wrote something (a lot), there
440 * could be a need for a window update.
441 * In the worst case, the remote will send
442 * a window probe to get things going again
447 * Probe a still-connecting, non-blocking socket
448 * to check if it's still alive
451 if (so
->so_state
& SS_ISFCONNECTING
) {
452 ret
= recv(so
->s
, (char *)&ret
, 0,0);
456 if (errno
== EAGAIN
|| errno
== EWOULDBLOCK
||
457 errno
== EINPROGRESS
|| errno
== ENOTCONN
)
458 continue; /* Still connecting, continue */
461 so
->so_state
= SS_NOFDREF
;
463 /* tcp_input will take care of it */
465 ret
= send(so
->s
, &ret
, 0,0);
468 if (errno
== EAGAIN
|| errno
== EWOULDBLOCK
||
469 errno
== EINPROGRESS
|| errno
== ENOTCONN
)
472 so
->so_state
= SS_NOFDREF
;
474 so
->so_state
&= ~SS_ISFCONNECTING
;
477 tcp_input((struct mbuf
*)NULL
, sizeof(struct ip
),so
);
478 } /* SS_ISFCONNECTING */
484 * Incoming packets are sent straight away, they're not buffered.
485 * Incoming UDP data isn't buffered either.
487 for (so
= udb
.so_next
; so
!= &udb
; so
= so_next
) {
488 so_next
= so
->so_next
;
490 if (so
->s
!= -1 && FD_ISSET(so
->s
, readfds
)) {
497 * See if we can start outputting
499 if (if_queued
&& link_up
)
502 /* clear global file descriptor sets.
503 * these reside on the stack in vl.c
504 * so they're unusable if we're not in
505 * slirp_select_fill or slirp_select_poll.
507 global_readfds
= NULL
;
508 global_writefds
= NULL
;
515 #define ETH_P_IP 0x0800 /* Internet Protocol packet */
516 #define ETH_P_ARP 0x0806 /* Address Resolution packet */
518 #define ARPOP_REQUEST 1 /* ARP request */
519 #define ARPOP_REPLY 2 /* ARP reply */
523 unsigned char h_dest
[ETH_ALEN
]; /* destination eth addr */
524 unsigned char h_source
[ETH_ALEN
]; /* source ether addr */
525 unsigned short h_proto
; /* packet type ID field */
530 unsigned short ar_hrd
; /* format of hardware address */
531 unsigned short ar_pro
; /* format of protocol address */
532 unsigned char ar_hln
; /* length of hardware address */
533 unsigned char ar_pln
; /* length of protocol address */
534 unsigned short ar_op
; /* ARP opcode (command) */
537 * Ethernet looks like this : This bit is variable sized however...
539 unsigned char ar_sha
[ETH_ALEN
]; /* sender hardware address */
540 unsigned char ar_sip
[4]; /* sender IP address */
541 unsigned char ar_tha
[ETH_ALEN
]; /* target hardware address */
542 unsigned char ar_tip
[4]; /* target IP address */
545 void arp_input(const uint8_t *pkt
, int pkt_len
)
547 struct ethhdr
*eh
= (struct ethhdr
*)pkt
;
548 struct arphdr
*ah
= (struct arphdr
*)(pkt
+ ETH_HLEN
);
549 uint8_t arp_reply
[ETH_HLEN
+ sizeof(struct arphdr
)];
550 struct ethhdr
*reh
= (struct ethhdr
*)arp_reply
;
551 struct arphdr
*rah
= (struct arphdr
*)(arp_reply
+ ETH_HLEN
);
553 struct ex_list
*ex_ptr
;
555 ar_op
= ntohs(ah
->ar_op
);
558 if (!memcmp(ah
->ar_tip
, &special_addr
, 3)) {
559 if (ah
->ar_tip
[3] == CTL_DNS
|| ah
->ar_tip
[3] == CTL_ALIAS
)
561 for (ex_ptr
= exec_list
; ex_ptr
; ex_ptr
= ex_ptr
->ex_next
) {
562 if (ex_ptr
->ex_addr
== ah
->ar_tip
[3])
567 /* XXX: make an ARP request to have the client address */
568 memcpy(client_ethaddr
, eh
->h_source
, ETH_ALEN
);
570 /* ARP request for alias/dns mac address */
571 memcpy(reh
->h_dest
, pkt
+ ETH_ALEN
, ETH_ALEN
);
572 memcpy(reh
->h_source
, special_ethaddr
, ETH_ALEN
- 1);
573 reh
->h_source
[5] = ah
->ar_tip
[3];
574 reh
->h_proto
= htons(ETH_P_ARP
);
576 rah
->ar_hrd
= htons(1);
577 rah
->ar_pro
= htons(ETH_P_IP
);
578 rah
->ar_hln
= ETH_ALEN
;
580 rah
->ar_op
= htons(ARPOP_REPLY
);
581 memcpy(rah
->ar_sha
, reh
->h_source
, ETH_ALEN
);
582 memcpy(rah
->ar_sip
, ah
->ar_tip
, 4);
583 memcpy(rah
->ar_tha
, ah
->ar_sha
, ETH_ALEN
);
584 memcpy(rah
->ar_tip
, ah
->ar_sip
, 4);
585 slirp_output(arp_reply
, sizeof(arp_reply
));
593 void slirp_input(const uint8_t *pkt
, int pkt_len
)
598 if (pkt_len
< ETH_HLEN
)
601 proto
= ntohs(*(uint16_t *)(pkt
+ 12));
604 arp_input(pkt
, pkt_len
);
611 memcpy(m
->m_data
, pkt
, pkt_len
);
613 m
->m_data
+= ETH_HLEN
;
614 m
->m_len
-= ETH_HLEN
;
623 /* output the IP packet to the ethernet device */
624 void if_encap(const uint8_t *ip_data
, int ip_data_len
)
627 struct ethhdr
*eh
= (struct ethhdr
*)buf
;
629 if (ip_data_len
+ ETH_HLEN
> sizeof(buf
))
632 memcpy(eh
->h_dest
, client_ethaddr
, ETH_ALEN
);
633 memcpy(eh
->h_source
, special_ethaddr
, ETH_ALEN
- 1);
634 /* XXX: not correct */
635 eh
->h_source
[5] = CTL_ALIAS
;
636 eh
->h_proto
= htons(ETH_P_IP
);
637 memcpy(buf
+ sizeof(struct ethhdr
), ip_data
, ip_data_len
);
638 slirp_output(buf
, ip_data_len
+ ETH_HLEN
);
641 int slirp_redir(int is_udp
, int host_port
,
642 struct in_addr guest_addr
, int guest_port
)
645 if (!udp_listen(htons(host_port
), guest_addr
.s_addr
,
646 htons(guest_port
), 0))
649 if (!solisten(htons(host_port
), guest_addr
.s_addr
,
650 htons(guest_port
), 0))
656 int slirp_add_exec(int do_pty
, const char *args
, int addr_low_byte
,
659 return add_exec(&exec_list
, do_pty
, (char *)args
,
660 addr_low_byte
, htons(guest_port
));