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];
24 /* XXX: suppress those select globals */
25 fd_set
*global_readfds
, *global_writefds
, *global_xfds
;
29 static int get_dns_addr(struct in_addr
*pdns_addr
)
37 static int get_dns_addr(struct in_addr
*pdns_addr
)
43 struct in_addr tmp_addr
;
45 f
= fopen("/etc/resolv.conf", "r");
49 lprint("IP address of your DNS(s): ");
50 while (fgets(buff
, 512, f
) != NULL
) {
51 if (sscanf(buff
, "nameserver%*[ \t]%256s", buff2
) == 1) {
52 if (!inet_aton(buff2
, &tmp_addr
))
54 if (tmp_addr
.s_addr
== loopback_addr
.s_addr
)
56 /* If it's the first one, set it to dns_addr */
58 *pdns_addr
= tmp_addr
;
65 lprint("%s", inet_ntoa(tmp_addr
));
75 void slirp_init(char *network
)
77 debug_init("/tmp/slirp.log", DEBUG_DEFAULT
);
84 /* Initialise mbufs *after* setting the MTU */
87 /* set default addresses */
89 inet_aton("127.0.0.1", &loopback_addr
);
91 if (get_dns_addr(&dns_addr
) < 0) {
92 fprintf(stderr
, "Could not get DNS address\n");
97 inet_aton(CTL_SPECIAL
, &special_addr
);
99 inet_aton(network
, &special_addr
);
103 #define CONN_CANFSEND(so) (((so)->so_state & (SS_FCANTSENDMORE|SS_ISFCONNECTED)) == SS_ISFCONNECTED)
104 #define CONN_CANFRCV(so) (((so)->so_state & (SS_FCANTRCVMORE|SS_ISFCONNECTED)) == SS_ISFCONNECTED)
105 #define UPD_NFDS(x) if (nfds < (x)) nfds = (x)
108 * curtime kept to an accuracy of 1ms
110 static void updtime(void)
112 gettimeofday(&tt
, 0);
114 curtime
= (u_int
)tt
.tv_sec
* (u_int
)1000;
115 curtime
+= (u_int
)tt
.tv_usec
/ (u_int
)1000;
117 if ((tt
.tv_usec
% 1000) >= 500)
121 void slirp_select_fill(int *pnfds
,
122 fd_set
*readfds
, fd_set
*writefds
, fd_set
*xfds
)
124 struct socket
*so
, *so_next
;
125 struct timeval timeout
;
130 global_readfds
= NULL
;
131 global_writefds
= NULL
;
141 * *_slowtimo needs calling if there are IP fragments
142 * in the fragment queue, or there are TCP connections active
144 do_slowtimo
= ((tcb
.so_next
!= &tcb
) ||
145 ((struct ipasfrag
*)&ipq
!= (struct ipasfrag
*)ipq
.next
));
147 for (so
= tcb
.so_next
; so
!= &tcb
; so
= so_next
) {
148 so_next
= so
->so_next
;
151 * See if we need a tcp_fasttimo
153 if (time_fasttimo
== 0 && so
->so_tcpcb
->t_flags
& TF_DELACK
)
154 time_fasttimo
= curtime
; /* Flag when we want a fasttimo */
157 * NOFDREF can include still connecting to local-host,
158 * newly socreated() sockets etc. Don't want to select these.
160 if (so
->so_state
& SS_NOFDREF
|| so
->s
== -1)
164 * Set for reading sockets which are accepting
166 if (so
->so_state
& SS_FACCEPTCONN
) {
167 FD_SET(so
->s
, readfds
);
173 * Set for writing sockets which are connecting
175 if (so
->so_state
& SS_ISFCONNECTING
) {
176 FD_SET(so
->s
, writefds
);
182 * Set for writing if we are connected, can send more, and
183 * we have something to send
185 if (CONN_CANFSEND(so
) && so
->so_rcv
.sb_cc
) {
186 FD_SET(so
->s
, writefds
);
191 * Set for reading (and urgent data) if we are connected, can
192 * receive more, and we have room for it XXX /2 ?
194 if (CONN_CANFRCV(so
) && (so
->so_snd
.sb_cc
< (so
->so_snd
.sb_datalen
/2))) {
195 FD_SET(so
->s
, readfds
);
204 for (so
= udb
.so_next
; so
!= &udb
; so
= so_next
) {
205 so_next
= so
->so_next
;
208 * See if it's timed out
211 if (so
->so_expire
<= curtime
) {
215 do_slowtimo
= 1; /* Let socket expire */
219 * When UDP packets are received from over the
220 * link, they're sendto()'d straight away, so
221 * no need for setting for writing
222 * Limit the number of packets queued by this session
223 * to 4. Note that even though we try and limit this
224 * to 4 packets, the session could have more queued
225 * if the packets needed to be fragmented
228 if ((so
->so_state
& SS_ISFCONNECTED
) && so
->so_queued
<= 4) {
229 FD_SET(so
->s
, readfds
);
236 * Setup timeout to use minimum CPU usage, especially when idle
240 * First, see the timeout needed by *timo
243 timeout
.tv_usec
= -1;
245 * If a slowtimo is needed, set timeout to 500ms from the last
246 * slow timeout. If a fast timeout is needed, set timeout within
247 * 200ms of when it was requested.
250 /* XXX + 10000 because some select()'s aren't that accurate */
251 timeout
.tv_usec
= ((500 - (curtime
- last_slowtimo
)) * 1000) + 10000;
252 if (timeout
.tv_usec
< 0)
254 else if (timeout
.tv_usec
> 510000)
255 timeout
.tv_usec
= 510000;
257 /* Can only fasttimo if we also slowtimo */
259 tmp_time
= (200 - (curtime
- time_fasttimo
)) * 1000;
263 /* Choose the smallest of the 2 */
264 if (tmp_time
< timeout
.tv_usec
)
265 timeout
.tv_usec
= (u_int
)tmp_time
;
271 void slirp_select_poll(fd_set
*readfds
, fd_set
*writefds
, fd_set
*xfds
)
273 struct socket
*so
, *so_next
;
276 global_readfds
= readfds
;
277 global_writefds
= writefds
;
284 * See if anything has timed out
287 if (time_fasttimo
&& ((curtime
- time_fasttimo
) >= 199)) {
291 if (do_slowtimo
&& ((curtime
- last_slowtimo
) >= 499)) {
294 last_slowtimo
= curtime
;
305 for (so
= tcb
.so_next
; so
!= &tcb
; so
= so_next
) {
306 so_next
= so
->so_next
;
309 * FD_ISSET is meaningless on these sockets
310 * (and they can crash the program)
312 if (so
->so_state
& SS_NOFDREF
|| so
->s
== -1)
317 * This will soread as well, so no need to
318 * test for readfds below if this succeeds
320 if (FD_ISSET(so
->s
, xfds
))
323 * Check sockets for reading
325 else if (FD_ISSET(so
->s
, readfds
)) {
327 * Check for incoming connections
329 if (so
->so_state
& SS_FACCEPTCONN
) {
335 /* Output it if we read something */
337 tcp_output(sototcpcb(so
));
341 * Check sockets for writing
343 if (FD_ISSET(so
->s
, writefds
)) {
345 * Check for non-blocking, still-connecting sockets
347 if (so
->so_state
& SS_ISFCONNECTING
) {
349 so
->so_state
&= ~SS_ISFCONNECTING
;
351 ret
= write(so
->s
, &ret
, 0);
353 /* XXXXX Must fix, zero bytes is a NOP */
354 if (errno
== EAGAIN
|| errno
== EWOULDBLOCK
||
355 errno
== EINPROGRESS
|| errno
== ENOTCONN
)
359 so
->so_state
= SS_NOFDREF
;
361 /* else so->so_state &= ~SS_ISFCONNECTING; */
366 tcp_input((struct mbuf
*)NULL
, sizeof(struct ip
), so
);
371 * XXXXX If we wrote something (a lot), there
372 * could be a need for a window update.
373 * In the worst case, the remote will send
374 * a window probe to get things going again
379 * Probe a still-connecting, non-blocking socket
380 * to check if it's still alive
383 if (so
->so_state
& SS_ISFCONNECTING
) {
384 ret
= read(so
->s
, (char *)&ret
, 0);
388 if (errno
== EAGAIN
|| errno
== EWOULDBLOCK
||
389 errno
== EINPROGRESS
|| errno
== ENOTCONN
)
390 continue; /* Still connecting, continue */
393 so
->so_state
= SS_NOFDREF
;
395 /* tcp_input will take care of it */
397 ret
= write(so
->s
, &ret
, 0);
400 if (errno
== EAGAIN
|| errno
== EWOULDBLOCK
||
401 errno
== EINPROGRESS
|| errno
== ENOTCONN
)
404 so
->so_state
= SS_NOFDREF
;
406 so
->so_state
&= ~SS_ISFCONNECTING
;
409 tcp_input((struct mbuf
*)NULL
, sizeof(struct ip
),so
);
410 } /* SS_ISFCONNECTING */
416 * Incoming packets are sent straight away, they're not buffered.
417 * Incoming UDP data isn't buffered either.
419 for (so
= udb
.so_next
; so
!= &udb
; so
= so_next
) {
420 so_next
= so
->so_next
;
422 if (so
->s
!= -1 && FD_ISSET(so
->s
, readfds
)) {
429 * See if we can start outputting
431 if (if_queued
&& link_up
)
438 #define ETH_P_IP 0x0800 /* Internet Protocol packet */
439 #define ETH_P_ARP 0x0806 /* Address Resolution packet */
441 #define ARPOP_REQUEST 1 /* ARP request */
442 #define ARPOP_REPLY 2 /* ARP reply */
446 unsigned char h_dest
[ETH_ALEN
]; /* destination eth addr */
447 unsigned char h_source
[ETH_ALEN
]; /* source ether addr */
448 unsigned short h_proto
; /* packet type ID field */
453 unsigned short ar_hrd
; /* format of hardware address */
454 unsigned short ar_pro
; /* format of protocol address */
455 unsigned char ar_hln
; /* length of hardware address */
456 unsigned char ar_pln
; /* length of protocol address */
457 unsigned short ar_op
; /* ARP opcode (command) */
460 * Ethernet looks like this : This bit is variable sized however...
462 unsigned char ar_sha
[ETH_ALEN
]; /* sender hardware address */
463 unsigned char ar_sip
[4]; /* sender IP address */
464 unsigned char ar_tha
[ETH_ALEN
]; /* target hardware address */
465 unsigned char ar_tip
[4]; /* target IP address */
468 void arp_input(const uint8_t *pkt
, int pkt_len
)
470 struct ethhdr
*eh
= (struct ethhdr
*)pkt
;
471 struct arphdr
*ah
= (struct arphdr
*)(pkt
+ ETH_HLEN
);
472 uint8_t arp_reply
[ETH_HLEN
+ sizeof(struct arphdr
)];
473 struct ethhdr
*reh
= (struct ethhdr
*)arp_reply
;
474 struct arphdr
*rah
= (struct arphdr
*)(arp_reply
+ ETH_HLEN
);
477 ar_op
= ntohs(ah
->ar_op
);
480 if (!memcmp(ah
->ar_tip
, &special_addr
, 3) &&
481 (ah
->ar_tip
[3] == CTL_DNS
|| ah
->ar_tip
[3] == CTL_ALIAS
)) {
483 /* XXX: make an ARP request to have the client address */
484 memcpy(client_ethaddr
, eh
->h_source
, ETH_ALEN
);
486 /* ARP request for alias/dns mac address */
487 memcpy(reh
->h_dest
, pkt
+ ETH_ALEN
, ETH_ALEN
);
488 memcpy(reh
->h_source
, special_ethaddr
, ETH_ALEN
- 1);
489 reh
->h_source
[5] = ah
->ar_tip
[3];
490 reh
->h_proto
= htons(ETH_P_ARP
);
492 rah
->ar_hrd
= htons(1);
493 rah
->ar_pro
= htons(ETH_P_IP
);
494 rah
->ar_hln
= ETH_ALEN
;
496 rah
->ar_op
= htons(ARPOP_REPLY
);
497 memcpy(rah
->ar_sha
, reh
->h_source
, ETH_ALEN
);
498 memcpy(rah
->ar_sip
, ah
->ar_tip
, 4);
499 memcpy(rah
->ar_tha
, ah
->ar_sha
, ETH_ALEN
);
500 memcpy(rah
->ar_tip
, ah
->ar_sip
, 4);
502 slirp_output(arp_reply
, sizeof(arp_reply
));
510 void slirp_input(const uint8_t *pkt
, int pkt_len
)
515 if (pkt_len
< ETH_HLEN
)
518 proto
= ntohs(*(uint16_t *)(pkt
+ 12));
521 arp_input(pkt
, pkt_len
);
528 memcpy(m
->m_data
, pkt
, pkt_len
);
530 m
->m_data
+= ETH_HLEN
;
531 m
->m_len
-= ETH_HLEN
;
540 /* output the IP packet to the ethernet device */
541 void if_encap(const uint8_t *ip_data
, int ip_data_len
)
544 struct ethhdr
*eh
= (struct ethhdr
*)buf
;
546 if (ip_data_len
+ ETH_HLEN
> sizeof(buf
))
549 memcpy(eh
->h_dest
, client_ethaddr
, ETH_ALEN
);
550 memcpy(eh
->h_source
, special_ethaddr
, ETH_ALEN
- 1);
551 eh
->h_source
[5] = CTL_ALIAS
;
552 eh
->h_proto
= htons(ETH_P_IP
);
553 memcpy(buf
+ sizeof(struct ethhdr
), ip_data
, ip_data_len
);
554 slirp_output(buf
, ip_data_len
+ ETH_HLEN
);