]>
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
)
31 FIXED_INFO
*FixedInfo
=NULL
;
34 IP_ADDR_STRING
*pIPAddr
;
35 struct in_addr tmp_addr
;
37 FixedInfo
= (FIXED_INFO
*)GlobalAlloc(GPTR
, sizeof(FIXED_INFO
));
38 BufLen
= sizeof(FIXED_INFO
);
40 if (ERROR_BUFFER_OVERFLOW
== GetNetworkParams(FixedInfo
, &BufLen
)) {
42 GlobalFree(FixedInfo
);
45 FixedInfo
= GlobalAlloc(GPTR
, BufLen
);
48 if ((ret
= GetNetworkParams(FixedInfo
, &BufLen
)) != ERROR_SUCCESS
) {
49 printf("GetNetworkParams failed. ret = %08x\n", (u_int
)ret
);
51 GlobalFree(FixedInfo
);
57 pIPAddr
= &(FixedInfo
->DnsServerList
);
58 inet_aton(pIPAddr
->IpAddress
.String
, &tmp_addr
);
59 *pdns_addr
= tmp_addr
;
61 printf( "DNS Servers:\n" );
62 printf( "DNS Addr:%s\n", pIPAddr
->IpAddress
.String
);
64 pIPAddr
= FixedInfo
-> DnsServerList
.Next
;
66 printf( "DNS Addr:%s\n", pIPAddr
->IpAddress
.String
);
67 pIPAddr
= pIPAddr
->Next
;
71 GlobalFree(FixedInfo
);
79 static int get_dns_addr(struct in_addr
*pdns_addr
)
85 struct in_addr tmp_addr
;
87 f
= fopen("/etc/resolv.conf", "r");
91 lprint("IP address of your DNS(s): ");
92 while (fgets(buff
, 512, f
) != NULL
) {
93 if (sscanf(buff
, "nameserver%*[ \t]%256s", buff2
) == 1) {
94 if (!inet_aton(buff2
, &tmp_addr
))
96 if (tmp_addr
.s_addr
== loopback_addr
.s_addr
)
98 /* If it's the first one, set it to dns_addr */
100 *pdns_addr
= tmp_addr
;
107 lprint("%s", inet_ntoa(tmp_addr
));
119 void slirp_cleanup(void)
125 void slirp_init(void)
127 // debug_init("/tmp/slirp.log", DEBUG_DEFAULT);
132 WSAStartup(MAKEWORD(2,0), &Data
);
133 atexit(slirp_cleanup
);
142 /* Initialise mbufs *after* setting the MTU */
145 /* set default addresses */
147 inet_aton("127.0.0.1", &loopback_addr
);
149 if (get_dns_addr(&dns_addr
) < 0) {
150 fprintf(stderr
, "Could not get DNS address\n");
154 inet_aton(CTL_SPECIAL
, &special_addr
);
157 #define CONN_CANFSEND(so) (((so)->so_state & (SS_FCANTSENDMORE|SS_ISFCONNECTED)) == SS_ISFCONNECTED)
158 #define CONN_CANFRCV(so) (((so)->so_state & (SS_FCANTRCVMORE|SS_ISFCONNECTED)) == SS_ISFCONNECTED)
159 #define UPD_NFDS(x) if (nfds < (x)) nfds = (x)
162 * curtime kept to an accuracy of 1ms
165 static void updtime(void)
170 curtime
= (u_int
)tb
.time
* (u_int
)1000;
171 curtime
+= (u_int
)tb
.millitm
;
174 static void updtime(void)
176 gettimeofday(&tt
, 0);
178 curtime
= (u_int
)tt
.tv_sec
* (u_int
)1000;
179 curtime
+= (u_int
)tt
.tv_usec
/ (u_int
)1000;
181 if ((tt
.tv_usec
% 1000) >= 500)
186 void slirp_select_fill(int *pnfds
,
187 fd_set
*readfds
, fd_set
*writefds
, fd_set
*xfds
)
189 struct socket
*so
, *so_next
;
190 struct timeval timeout
;
195 global_readfds
= NULL
;
196 global_writefds
= NULL
;
206 * *_slowtimo needs calling if there are IP fragments
207 * in the fragment queue, or there are TCP connections active
209 do_slowtimo
= ((tcb
.so_next
!= &tcb
) ||
210 ((struct ipasfrag
*)&ipq
!= (struct ipasfrag
*)ipq
.next
));
212 for (so
= tcb
.so_next
; so
!= &tcb
; so
= so_next
) {
213 so_next
= so
->so_next
;
216 * See if we need a tcp_fasttimo
218 if (time_fasttimo
== 0 && so
->so_tcpcb
->t_flags
& TF_DELACK
)
219 time_fasttimo
= curtime
; /* Flag when we want a fasttimo */
222 * NOFDREF can include still connecting to local-host,
223 * newly socreated() sockets etc. Don't want to select these.
225 if (so
->so_state
& SS_NOFDREF
|| so
->s
== -1)
229 * Set for reading sockets which are accepting
231 if (so
->so_state
& SS_FACCEPTCONN
) {
232 FD_SET(so
->s
, readfds
);
238 * Set for writing sockets which are connecting
240 if (so
->so_state
& SS_ISFCONNECTING
) {
241 FD_SET(so
->s
, writefds
);
247 * Set for writing if we are connected, can send more, and
248 * we have something to send
250 if (CONN_CANFSEND(so
) && so
->so_rcv
.sb_cc
) {
251 FD_SET(so
->s
, writefds
);
256 * Set for reading (and urgent data) if we are connected, can
257 * receive more, and we have room for it XXX /2 ?
259 if (CONN_CANFRCV(so
) && (so
->so_snd
.sb_cc
< (so
->so_snd
.sb_datalen
/2))) {
260 FD_SET(so
->s
, readfds
);
269 for (so
= udb
.so_next
; so
!= &udb
; so
= so_next
) {
270 so_next
= so
->so_next
;
273 * See if it's timed out
276 if (so
->so_expire
<= curtime
) {
280 do_slowtimo
= 1; /* Let socket expire */
284 * When UDP packets are received from over the
285 * link, they're sendto()'d straight away, so
286 * no need for setting for writing
287 * Limit the number of packets queued by this session
288 * to 4. Note that even though we try and limit this
289 * to 4 packets, the session could have more queued
290 * if the packets needed to be fragmented
293 if ((so
->so_state
& SS_ISFCONNECTED
) && so
->so_queued
<= 4) {
294 FD_SET(so
->s
, readfds
);
301 * Setup timeout to use minimum CPU usage, especially when idle
305 * First, see the timeout needed by *timo
308 timeout
.tv_usec
= -1;
310 * If a slowtimo is needed, set timeout to 500ms from the last
311 * slow timeout. If a fast timeout is needed, set timeout within
312 * 200ms of when it was requested.
315 /* XXX + 10000 because some select()'s aren't that accurate */
316 timeout
.tv_usec
= ((500 - (curtime
- last_slowtimo
)) * 1000) + 10000;
317 if (timeout
.tv_usec
< 0)
319 else if (timeout
.tv_usec
> 510000)
320 timeout
.tv_usec
= 510000;
322 /* Can only fasttimo if we also slowtimo */
324 tmp_time
= (200 - (curtime
- time_fasttimo
)) * 1000;
328 /* Choose the smallest of the 2 */
329 if (tmp_time
< timeout
.tv_usec
)
330 timeout
.tv_usec
= (u_int
)tmp_time
;
336 void slirp_select_poll(fd_set
*readfds
, fd_set
*writefds
, fd_set
*xfds
)
338 struct socket
*so
, *so_next
;
341 global_readfds
= readfds
;
342 global_writefds
= writefds
;
349 * See if anything has timed out
352 if (time_fasttimo
&& ((curtime
- time_fasttimo
) >= 199)) {
356 if (do_slowtimo
&& ((curtime
- last_slowtimo
) >= 499)) {
359 last_slowtimo
= curtime
;
370 for (so
= tcb
.so_next
; so
!= &tcb
; so
= so_next
) {
371 so_next
= so
->so_next
;
374 * FD_ISSET is meaningless on these sockets
375 * (and they can crash the program)
377 if (so
->so_state
& SS_NOFDREF
|| so
->s
== -1)
382 * This will soread as well, so no need to
383 * test for readfds below if this succeeds
385 if (FD_ISSET(so
->s
, xfds
))
388 * Check sockets for reading
390 else if (FD_ISSET(so
->s
, readfds
)) {
392 * Check for incoming connections
394 if (so
->so_state
& SS_FACCEPTCONN
) {
400 /* Output it if we read something */
402 tcp_output(sototcpcb(so
));
406 * Check sockets for writing
408 if (FD_ISSET(so
->s
, writefds
)) {
410 * Check for non-blocking, still-connecting sockets
412 if (so
->so_state
& SS_ISFCONNECTING
) {
414 so
->so_state
&= ~SS_ISFCONNECTING
;
416 ret
= write(so
->s
, &ret
, 0);
418 /* XXXXX Must fix, zero bytes is a NOP */
419 if (errno
== EAGAIN
|| errno
== EWOULDBLOCK
||
420 errno
== EINPROGRESS
|| errno
== ENOTCONN
)
424 so
->so_state
= SS_NOFDREF
;
426 /* else so->so_state &= ~SS_ISFCONNECTING; */
431 tcp_input((struct mbuf
*)NULL
, sizeof(struct ip
), so
);
436 * XXXXX If we wrote something (a lot), there
437 * could be a need for a window update.
438 * In the worst case, the remote will send
439 * a window probe to get things going again
444 * Probe a still-connecting, non-blocking socket
445 * to check if it's still alive
448 if (so
->so_state
& SS_ISFCONNECTING
) {
449 ret
= read(so
->s
, (char *)&ret
, 0);
453 if (errno
== EAGAIN
|| errno
== EWOULDBLOCK
||
454 errno
== EINPROGRESS
|| errno
== ENOTCONN
)
455 continue; /* Still connecting, continue */
458 so
->so_state
= SS_NOFDREF
;
460 /* tcp_input will take care of it */
462 ret
= write(so
->s
, &ret
, 0);
465 if (errno
== EAGAIN
|| errno
== EWOULDBLOCK
||
466 errno
== EINPROGRESS
|| errno
== ENOTCONN
)
469 so
->so_state
= SS_NOFDREF
;
471 so
->so_state
&= ~SS_ISFCONNECTING
;
474 tcp_input((struct mbuf
*)NULL
, sizeof(struct ip
),so
);
475 } /* SS_ISFCONNECTING */
481 * Incoming packets are sent straight away, they're not buffered.
482 * Incoming UDP data isn't buffered either.
484 for (so
= udb
.so_next
; so
!= &udb
; so
= so_next
) {
485 so_next
= so
->so_next
;
487 if (so
->s
!= -1 && FD_ISSET(so
->s
, readfds
)) {
494 * See if we can start outputting
496 if (if_queued
&& link_up
)
503 #define ETH_P_IP 0x0800 /* Internet Protocol packet */
504 #define ETH_P_ARP 0x0806 /* Address Resolution packet */
506 #define ARPOP_REQUEST 1 /* ARP request */
507 #define ARPOP_REPLY 2 /* ARP reply */
511 unsigned char h_dest
[ETH_ALEN
]; /* destination eth addr */
512 unsigned char h_source
[ETH_ALEN
]; /* source ether addr */
513 unsigned short h_proto
; /* packet type ID field */
518 unsigned short ar_hrd
; /* format of hardware address */
519 unsigned short ar_pro
; /* format of protocol address */
520 unsigned char ar_hln
; /* length of hardware address */
521 unsigned char ar_pln
; /* length of protocol address */
522 unsigned short ar_op
; /* ARP opcode (command) */
525 * Ethernet looks like this : This bit is variable sized however...
527 unsigned char ar_sha
[ETH_ALEN
]; /* sender hardware address */
528 unsigned char ar_sip
[4]; /* sender IP address */
529 unsigned char ar_tha
[ETH_ALEN
]; /* target hardware address */
530 unsigned char ar_tip
[4]; /* target IP address */
533 void arp_input(const uint8_t *pkt
, int pkt_len
)
535 struct ethhdr
*eh
= (struct ethhdr
*)pkt
;
536 struct arphdr
*ah
= (struct arphdr
*)(pkt
+ ETH_HLEN
);
537 uint8_t arp_reply
[ETH_HLEN
+ sizeof(struct arphdr
)];
538 struct ethhdr
*reh
= (struct ethhdr
*)arp_reply
;
539 struct arphdr
*rah
= (struct arphdr
*)(arp_reply
+ ETH_HLEN
);
542 ar_op
= ntohs(ah
->ar_op
);
545 if (!memcmp(ah
->ar_tip
, &special_addr
, 3) &&
546 (ah
->ar_tip
[3] == CTL_DNS
|| ah
->ar_tip
[3] == CTL_ALIAS
)) {
548 /* XXX: make an ARP request to have the client address */
549 memcpy(client_ethaddr
, eh
->h_source
, ETH_ALEN
);
551 /* ARP request for alias/dns mac address */
552 memcpy(reh
->h_dest
, pkt
+ ETH_ALEN
, ETH_ALEN
);
553 memcpy(reh
->h_source
, special_ethaddr
, ETH_ALEN
- 1);
554 reh
->h_source
[5] = ah
->ar_tip
[3];
555 reh
->h_proto
= htons(ETH_P_ARP
);
557 rah
->ar_hrd
= htons(1);
558 rah
->ar_pro
= htons(ETH_P_IP
);
559 rah
->ar_hln
= ETH_ALEN
;
561 rah
->ar_op
= htons(ARPOP_REPLY
);
562 memcpy(rah
->ar_sha
, reh
->h_source
, ETH_ALEN
);
563 memcpy(rah
->ar_sip
, ah
->ar_tip
, 4);
564 memcpy(rah
->ar_tha
, ah
->ar_sha
, ETH_ALEN
);
565 memcpy(rah
->ar_tip
, ah
->ar_sip
, 4);
566 slirp_output(arp_reply
, sizeof(arp_reply
));
574 void slirp_input(const uint8_t *pkt
, int pkt_len
)
579 if (pkt_len
< ETH_HLEN
)
582 proto
= ntohs(*(uint16_t *)(pkt
+ 12));
585 arp_input(pkt
, pkt_len
);
592 memcpy(m
->m_data
, pkt
, pkt_len
);
594 m
->m_data
+= ETH_HLEN
;
595 m
->m_len
-= ETH_HLEN
;
604 /* output the IP packet to the ethernet device */
605 void if_encap(const uint8_t *ip_data
, int ip_data_len
)
608 struct ethhdr
*eh
= (struct ethhdr
*)buf
;
610 if (ip_data_len
+ ETH_HLEN
> sizeof(buf
))
613 memcpy(eh
->h_dest
, client_ethaddr
, ETH_ALEN
);
614 memcpy(eh
->h_source
, special_ethaddr
, ETH_ALEN
- 1);
615 eh
->h_source
[5] = CTL_ALIAS
;
616 eh
->h_proto
= htons(ETH_P_IP
);
617 memcpy(buf
+ sizeof(struct ethhdr
), ip_data
, ip_data_len
);
618 slirp_output(buf
, ip_data_len
+ ETH_HLEN
);