]>
git.proxmox.com Git - mirror_qemu.git/blob - slirp/slirp.c
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
));
78 // debug_init("/tmp/slirp.log", DEBUG_DEFAULT);
85 /* Initialise mbufs *after* setting the MTU */
88 /* set default addresses */
90 inet_aton("127.0.0.1", &loopback_addr
);
92 if (get_dns_addr(&dns_addr
) < 0) {
93 fprintf(stderr
, "Could not get DNS address\n");
97 inet_aton(CTL_SPECIAL
, &special_addr
);
100 #define CONN_CANFSEND(so) (((so)->so_state & (SS_FCANTSENDMORE|SS_ISFCONNECTED)) == SS_ISFCONNECTED)
101 #define CONN_CANFRCV(so) (((so)->so_state & (SS_FCANTRCVMORE|SS_ISFCONNECTED)) == SS_ISFCONNECTED)
102 #define UPD_NFDS(x) if (nfds < (x)) nfds = (x)
105 * curtime kept to an accuracy of 1ms
107 static void updtime(void)
109 gettimeofday(&tt
, 0);
111 curtime
= (u_int
)tt
.tv_sec
* (u_int
)1000;
112 curtime
+= (u_int
)tt
.tv_usec
/ (u_int
)1000;
114 if ((tt
.tv_usec
% 1000) >= 500)
118 void slirp_select_fill(int *pnfds
,
119 fd_set
*readfds
, fd_set
*writefds
, fd_set
*xfds
)
121 struct socket
*so
, *so_next
;
122 struct timeval timeout
;
127 global_readfds
= NULL
;
128 global_writefds
= NULL
;
138 * *_slowtimo needs calling if there are IP fragments
139 * in the fragment queue, or there are TCP connections active
141 do_slowtimo
= ((tcb
.so_next
!= &tcb
) ||
142 ((struct ipasfrag
*)&ipq
!= (struct ipasfrag
*)ipq
.next
));
144 for (so
= tcb
.so_next
; so
!= &tcb
; so
= so_next
) {
145 so_next
= so
->so_next
;
148 * See if we need a tcp_fasttimo
150 if (time_fasttimo
== 0 && so
->so_tcpcb
->t_flags
& TF_DELACK
)
151 time_fasttimo
= curtime
; /* Flag when we want a fasttimo */
154 * NOFDREF can include still connecting to local-host,
155 * newly socreated() sockets etc. Don't want to select these.
157 if (so
->so_state
& SS_NOFDREF
|| so
->s
== -1)
161 * Set for reading sockets which are accepting
163 if (so
->so_state
& SS_FACCEPTCONN
) {
164 FD_SET(so
->s
, readfds
);
170 * Set for writing sockets which are connecting
172 if (so
->so_state
& SS_ISFCONNECTING
) {
173 FD_SET(so
->s
, writefds
);
179 * Set for writing if we are connected, can send more, and
180 * we have something to send
182 if (CONN_CANFSEND(so
) && so
->so_rcv
.sb_cc
) {
183 FD_SET(so
->s
, writefds
);
188 * Set for reading (and urgent data) if we are connected, can
189 * receive more, and we have room for it XXX /2 ?
191 if (CONN_CANFRCV(so
) && (so
->so_snd
.sb_cc
< (so
->so_snd
.sb_datalen
/2))) {
192 FD_SET(so
->s
, readfds
);
201 for (so
= udb
.so_next
; so
!= &udb
; so
= so_next
) {
202 so_next
= so
->so_next
;
205 * See if it's timed out
208 if (so
->so_expire
<= curtime
) {
212 do_slowtimo
= 1; /* Let socket expire */
216 * When UDP packets are received from over the
217 * link, they're sendto()'d straight away, so
218 * no need for setting for writing
219 * Limit the number of packets queued by this session
220 * to 4. Note that even though we try and limit this
221 * to 4 packets, the session could have more queued
222 * if the packets needed to be fragmented
225 if ((so
->so_state
& SS_ISFCONNECTED
) && so
->so_queued
<= 4) {
226 FD_SET(so
->s
, readfds
);
233 * Setup timeout to use minimum CPU usage, especially when idle
237 * First, see the timeout needed by *timo
240 timeout
.tv_usec
= -1;
242 * If a slowtimo is needed, set timeout to 500ms from the last
243 * slow timeout. If a fast timeout is needed, set timeout within
244 * 200ms of when it was requested.
247 /* XXX + 10000 because some select()'s aren't that accurate */
248 timeout
.tv_usec
= ((500 - (curtime
- last_slowtimo
)) * 1000) + 10000;
249 if (timeout
.tv_usec
< 0)
251 else if (timeout
.tv_usec
> 510000)
252 timeout
.tv_usec
= 510000;
254 /* Can only fasttimo if we also slowtimo */
256 tmp_time
= (200 - (curtime
- time_fasttimo
)) * 1000;
260 /* Choose the smallest of the 2 */
261 if (tmp_time
< timeout
.tv_usec
)
262 timeout
.tv_usec
= (u_int
)tmp_time
;
268 void slirp_select_poll(fd_set
*readfds
, fd_set
*writefds
, fd_set
*xfds
)
270 struct socket
*so
, *so_next
;
273 global_readfds
= readfds
;
274 global_writefds
= writefds
;
281 * See if anything has timed out
284 if (time_fasttimo
&& ((curtime
- time_fasttimo
) >= 199)) {
288 if (do_slowtimo
&& ((curtime
- last_slowtimo
) >= 499)) {
291 last_slowtimo
= curtime
;
302 for (so
= tcb
.so_next
; so
!= &tcb
; so
= so_next
) {
303 so_next
= so
->so_next
;
306 * FD_ISSET is meaningless on these sockets
307 * (and they can crash the program)
309 if (so
->so_state
& SS_NOFDREF
|| so
->s
== -1)
314 * This will soread as well, so no need to
315 * test for readfds below if this succeeds
317 if (FD_ISSET(so
->s
, xfds
))
320 * Check sockets for reading
322 else if (FD_ISSET(so
->s
, readfds
)) {
324 * Check for incoming connections
326 if (so
->so_state
& SS_FACCEPTCONN
) {
332 /* Output it if we read something */
334 tcp_output(sototcpcb(so
));
338 * Check sockets for writing
340 if (FD_ISSET(so
->s
, writefds
)) {
342 * Check for non-blocking, still-connecting sockets
344 if (so
->so_state
& SS_ISFCONNECTING
) {
346 so
->so_state
&= ~SS_ISFCONNECTING
;
348 ret
= write(so
->s
, &ret
, 0);
350 /* XXXXX Must fix, zero bytes is a NOP */
351 if (errno
== EAGAIN
|| errno
== EWOULDBLOCK
||
352 errno
== EINPROGRESS
|| errno
== ENOTCONN
)
356 so
->so_state
= SS_NOFDREF
;
358 /* else so->so_state &= ~SS_ISFCONNECTING; */
363 tcp_input((struct mbuf
*)NULL
, sizeof(struct ip
), so
);
368 * XXXXX If we wrote something (a lot), there
369 * could be a need for a window update.
370 * In the worst case, the remote will send
371 * a window probe to get things going again
376 * Probe a still-connecting, non-blocking socket
377 * to check if it's still alive
380 if (so
->so_state
& SS_ISFCONNECTING
) {
381 ret
= read(so
->s
, (char *)&ret
, 0);
385 if (errno
== EAGAIN
|| errno
== EWOULDBLOCK
||
386 errno
== EINPROGRESS
|| errno
== ENOTCONN
)
387 continue; /* Still connecting, continue */
390 so
->so_state
= SS_NOFDREF
;
392 /* tcp_input will take care of it */
394 ret
= write(so
->s
, &ret
, 0);
397 if (errno
== EAGAIN
|| errno
== EWOULDBLOCK
||
398 errno
== EINPROGRESS
|| errno
== ENOTCONN
)
401 so
->so_state
= SS_NOFDREF
;
403 so
->so_state
&= ~SS_ISFCONNECTING
;
406 tcp_input((struct mbuf
*)NULL
, sizeof(struct ip
),so
);
407 } /* SS_ISFCONNECTING */
413 * Incoming packets are sent straight away, they're not buffered.
414 * Incoming UDP data isn't buffered either.
416 for (so
= udb
.so_next
; so
!= &udb
; so
= so_next
) {
417 so_next
= so
->so_next
;
419 if (so
->s
!= -1 && FD_ISSET(so
->s
, readfds
)) {
426 * See if we can start outputting
428 if (if_queued
&& link_up
)
435 #define ETH_P_IP 0x0800 /* Internet Protocol packet */
436 #define ETH_P_ARP 0x0806 /* Address Resolution packet */
438 #define ARPOP_REQUEST 1 /* ARP request */
439 #define ARPOP_REPLY 2 /* ARP reply */
443 unsigned char h_dest
[ETH_ALEN
]; /* destination eth addr */
444 unsigned char h_source
[ETH_ALEN
]; /* source ether addr */
445 unsigned short h_proto
; /* packet type ID field */
450 unsigned short ar_hrd
; /* format of hardware address */
451 unsigned short ar_pro
; /* format of protocol address */
452 unsigned char ar_hln
; /* length of hardware address */
453 unsigned char ar_pln
; /* length of protocol address */
454 unsigned short ar_op
; /* ARP opcode (command) */
457 * Ethernet looks like this : This bit is variable sized however...
459 unsigned char ar_sha
[ETH_ALEN
]; /* sender hardware address */
460 unsigned char ar_sip
[4]; /* sender IP address */
461 unsigned char ar_tha
[ETH_ALEN
]; /* target hardware address */
462 unsigned char ar_tip
[4]; /* target IP address */
465 void arp_input(const uint8_t *pkt
, int pkt_len
)
467 struct ethhdr
*eh
= (struct ethhdr
*)pkt
;
468 struct arphdr
*ah
= (struct arphdr
*)(pkt
+ ETH_HLEN
);
469 uint8_t arp_reply
[ETH_HLEN
+ sizeof(struct arphdr
)];
470 struct ethhdr
*reh
= (struct ethhdr
*)arp_reply
;
471 struct arphdr
*rah
= (struct arphdr
*)(arp_reply
+ ETH_HLEN
);
474 ar_op
= ntohs(ah
->ar_op
);
477 if (!memcmp(ah
->ar_tip
, &special_addr
, 3) &&
478 (ah
->ar_tip
[3] == CTL_DNS
|| ah
->ar_tip
[3] == CTL_ALIAS
)) {
480 /* XXX: make an ARP request to have the client address */
481 memcpy(client_ethaddr
, eh
->h_source
, ETH_ALEN
);
483 /* ARP request for alias/dns mac address */
484 memcpy(reh
->h_dest
, pkt
+ ETH_ALEN
, ETH_ALEN
);
485 memcpy(reh
->h_source
, special_ethaddr
, ETH_ALEN
- 1);
486 reh
->h_source
[5] = ah
->ar_tip
[3];
487 reh
->h_proto
= htons(ETH_P_ARP
);
489 rah
->ar_hrd
= htons(1);
490 rah
->ar_pro
= htons(ETH_P_IP
);
491 rah
->ar_hln
= ETH_ALEN
;
493 rah
->ar_op
= htons(ARPOP_REPLY
);
494 memcpy(rah
->ar_sha
, reh
->h_source
, ETH_ALEN
);
495 memcpy(rah
->ar_sip
, ah
->ar_tip
, 4);
496 memcpy(rah
->ar_tha
, ah
->ar_sha
, ETH_ALEN
);
497 memcpy(rah
->ar_tip
, ah
->ar_sip
, 4);
498 slirp_output(arp_reply
, sizeof(arp_reply
));
506 void slirp_input(const uint8_t *pkt
, int pkt_len
)
511 if (pkt_len
< ETH_HLEN
)
514 proto
= ntohs(*(uint16_t *)(pkt
+ 12));
517 arp_input(pkt
, pkt_len
);
524 memcpy(m
->m_data
, pkt
, pkt_len
);
526 m
->m_data
+= ETH_HLEN
;
527 m
->m_len
-= ETH_HLEN
;
536 /* output the IP packet to the ethernet device */
537 void if_encap(const uint8_t *ip_data
, int ip_data_len
)
540 struct ethhdr
*eh
= (struct ethhdr
*)buf
;
542 if (ip_data_len
+ ETH_HLEN
> sizeof(buf
))
545 memcpy(eh
->h_dest
, client_ethaddr
, ETH_ALEN
);
546 memcpy(eh
->h_source
, special_ethaddr
, ETH_ALEN
- 1);
547 eh
->h_source
[5] = CTL_ALIAS
;
548 eh
->h_proto
= htons(ETH_P_IP
);
549 memcpy(buf
+ sizeof(struct ethhdr
), ip_data
, ip_data_len
);
550 slirp_output(buf
, ip_data_len
+ ETH_HLEN
);