4 * Copyright (c) 2004-2008 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 #include "qemu-common.h"
25 #include "qemu-char.h"
30 struct in_addr our_addr
;
31 /* host dns address */
32 struct in_addr dns_addr
;
33 /* host loopback address */
34 struct in_addr loopback_addr
;
36 /* virtual network configuration */
37 struct in_addr vnetwork_addr
;
38 struct in_addr vnetwork_mask
;
39 struct in_addr vhost_addr
;
40 struct in_addr vdhcp_startaddr
;
41 struct in_addr vnameserver_addr
;
43 /* emulated hosts use the MAC addr 52:55:IP:IP:IP:IP */
44 static const uint8_t special_ethaddr
[6] = {
45 0x52, 0x55, 0x00, 0x00, 0x00, 0x00
48 /* ARP cache for the guest IP addresses (XXX: allow many entries) */
49 uint8_t client_ethaddr
[6];
50 static struct in_addr client_ipaddr
;
52 static const uint8_t zero_ethaddr
[6] = { 0, 0, 0, 0, 0, 0 };
55 static int do_slowtimo
;
59 struct ex_list
*exec_list
;
61 /* XXX: suppress those select globals */
62 fd_set
*global_readfds
, *global_writefds
, *global_xfds
;
64 char slirp_hostname
[33];
68 static int get_dns_addr(struct in_addr
*pdns_addr
)
70 FIXED_INFO
*FixedInfo
=NULL
;
73 IP_ADDR_STRING
*pIPAddr
;
74 struct in_addr tmp_addr
;
76 FixedInfo
= (FIXED_INFO
*)GlobalAlloc(GPTR
, sizeof(FIXED_INFO
));
77 BufLen
= sizeof(FIXED_INFO
);
79 if (ERROR_BUFFER_OVERFLOW
== GetNetworkParams(FixedInfo
, &BufLen
)) {
81 GlobalFree(FixedInfo
);
84 FixedInfo
= GlobalAlloc(GPTR
, BufLen
);
87 if ((ret
= GetNetworkParams(FixedInfo
, &BufLen
)) != ERROR_SUCCESS
) {
88 printf("GetNetworkParams failed. ret = %08x\n", (u_int
)ret
);
90 GlobalFree(FixedInfo
);
96 pIPAddr
= &(FixedInfo
->DnsServerList
);
97 inet_aton(pIPAddr
->IpAddress
.String
, &tmp_addr
);
98 *pdns_addr
= tmp_addr
;
100 printf( "DNS Servers:\n" );
101 printf( "DNS Addr:%s\n", pIPAddr
->IpAddress
.String
);
103 pIPAddr
= FixedInfo
-> DnsServerList
.Next
;
105 printf( "DNS Addr:%s\n", pIPAddr
->IpAddress
.String
);
106 pIPAddr
= pIPAddr
->Next
;
110 GlobalFree(FixedInfo
);
118 static int get_dns_addr(struct in_addr
*pdns_addr
)
124 struct in_addr tmp_addr
;
126 f
= fopen("/etc/resolv.conf", "r");
131 lprint("IP address of your DNS(s): ");
133 while (fgets(buff
, 512, f
) != NULL
) {
134 if (sscanf(buff
, "nameserver%*[ \t]%256s", buff2
) == 1) {
135 if (!inet_aton(buff2
, &tmp_addr
))
137 if (tmp_addr
.s_addr
== loopback_addr
.s_addr
)
139 /* If it's the first one, set it to dns_addr */
141 *pdns_addr
= tmp_addr
;
154 lprint("%s", inet_ntoa(tmp_addr
));
167 static void slirp_cleanup(void)
173 static void slirp_state_save(QEMUFile
*f
, void *opaque
);
174 static int slirp_state_load(QEMUFile
*f
, void *opaque
, int version_id
);
176 void slirp_init(int restricted
, const char *special_ip
, const char *tftp_path
,
177 const char *bootfile
)
179 // debug_init("/tmp/slirp.log", DEBUG_DEFAULT);
181 struct in_addr special_addr
= { .s_addr
= htonl(0x0a000200) };
185 WSAStartup(MAKEWORD(2,0), &Data
);
186 atexit(slirp_cleanup
);
190 slirp_restrict
= restricted
;
195 /* Initialise mbufs *after* setting the MTU */
198 /* set default addresses */
199 inet_aton("127.0.0.1", &loopback_addr
);
201 if (get_dns_addr(&dns_addr
) < 0) {
202 dns_addr
= loopback_addr
;
203 fprintf (stderr
, "Warning: No DNS servers found\n");
207 inet_aton(special_ip
, &special_addr
);
209 qemu_free(tftp_prefix
);
212 tftp_prefix
= qemu_strdup(tftp_path
);
214 qemu_free(bootp_filename
);
215 bootp_filename
= NULL
;
217 bootp_filename
= qemu_strdup(bootfile
);
220 vnetwork_addr
= special_addr
;
221 vnetwork_mask
.s_addr
= htonl(0xffffff00);
222 vhost_addr
.s_addr
= special_addr
.s_addr
| htonl(2);
223 vdhcp_startaddr
.s_addr
= special_addr
.s_addr
| htonl(15);
224 vnameserver_addr
.s_addr
= special_addr
.s_addr
| htonl(3);
226 register_savevm("slirp", 0, 1, slirp_state_save
, slirp_state_load
, NULL
);
229 #define CONN_CANFSEND(so) (((so)->so_state & (SS_FCANTSENDMORE|SS_ISFCONNECTED)) == SS_ISFCONNECTED)
230 #define CONN_CANFRCV(so) (((so)->so_state & (SS_FCANTRCVMORE|SS_ISFCONNECTED)) == SS_ISFCONNECTED)
231 #define UPD_NFDS(x) if (nfds < (x)) nfds = (x)
234 * curtime kept to an accuracy of 1ms
237 static void updtime(void)
242 curtime
= (u_int
)tb
.time
* (u_int
)1000;
243 curtime
+= (u_int
)tb
.millitm
;
246 static void updtime(void)
248 gettimeofday(&tt
, NULL
);
250 curtime
= (u_int
)tt
.tv_sec
* (u_int
)1000;
251 curtime
+= (u_int
)tt
.tv_usec
/ (u_int
)1000;
253 if ((tt
.tv_usec
% 1000) >= 500)
258 void slirp_select_fill(int *pnfds
,
259 fd_set
*readfds
, fd_set
*writefds
, fd_set
*xfds
)
261 struct socket
*so
, *so_next
;
262 struct timeval timeout
;
267 global_readfds
= NULL
;
268 global_writefds
= NULL
;
278 * *_slowtimo needs calling if there are IP fragments
279 * in the fragment queue, or there are TCP connections active
281 do_slowtimo
= ((tcb
.so_next
!= &tcb
) ||
282 (&ipq
.ip_link
!= ipq
.ip_link
.next
));
284 for (so
= tcb
.so_next
; so
!= &tcb
; so
= so_next
) {
285 so_next
= so
->so_next
;
288 * See if we need a tcp_fasttimo
290 if (time_fasttimo
== 0 && so
->so_tcpcb
->t_flags
& TF_DELACK
)
291 time_fasttimo
= curtime
; /* Flag when we want a fasttimo */
294 * NOFDREF can include still connecting to local-host,
295 * newly socreated() sockets etc. Don't want to select these.
297 if (so
->so_state
& SS_NOFDREF
|| so
->s
== -1)
301 * Set for reading sockets which are accepting
303 if (so
->so_state
& SS_FACCEPTCONN
) {
304 FD_SET(so
->s
, readfds
);
310 * Set for writing sockets which are connecting
312 if (so
->so_state
& SS_ISFCONNECTING
) {
313 FD_SET(so
->s
, writefds
);
319 * Set for writing if we are connected, can send more, and
320 * we have something to send
322 if (CONN_CANFSEND(so
) && so
->so_rcv
.sb_cc
) {
323 FD_SET(so
->s
, writefds
);
328 * Set for reading (and urgent data) if we are connected, can
329 * receive more, and we have room for it XXX /2 ?
331 if (CONN_CANFRCV(so
) && (so
->so_snd
.sb_cc
< (so
->so_snd
.sb_datalen
/2))) {
332 FD_SET(so
->s
, readfds
);
341 for (so
= udb
.so_next
; so
!= &udb
; so
= so_next
) {
342 so_next
= so
->so_next
;
345 * See if it's timed out
348 if (so
->so_expire
<= curtime
) {
352 do_slowtimo
= 1; /* Let socket expire */
356 * When UDP packets are received from over the
357 * link, they're sendto()'d straight away, so
358 * no need for setting for writing
359 * Limit the number of packets queued by this session
360 * to 4. Note that even though we try and limit this
361 * to 4 packets, the session could have more queued
362 * if the packets needed to be fragmented
365 if ((so
->so_state
& SS_ISFCONNECTED
) && so
->so_queued
<= 4) {
366 FD_SET(so
->s
, readfds
);
373 * Setup timeout to use minimum CPU usage, especially when idle
377 * First, see the timeout needed by *timo
380 timeout
.tv_usec
= -1;
382 * If a slowtimo is needed, set timeout to 500ms from the last
383 * slow timeout. If a fast timeout is needed, set timeout within
384 * 200ms of when it was requested.
387 /* XXX + 10000 because some select()'s aren't that accurate */
388 timeout
.tv_usec
= ((500 - (curtime
- last_slowtimo
)) * 1000) + 10000;
389 if (timeout
.tv_usec
< 0)
391 else if (timeout
.tv_usec
> 510000)
392 timeout
.tv_usec
= 510000;
394 /* Can only fasttimo if we also slowtimo */
396 tmp_time
= (200 - (curtime
- time_fasttimo
)) * 1000;
400 /* Choose the smallest of the 2 */
401 if (tmp_time
< timeout
.tv_usec
)
402 timeout
.tv_usec
= (u_int
)tmp_time
;
408 void slirp_select_poll(fd_set
*readfds
, fd_set
*writefds
, fd_set
*xfds
)
410 struct socket
*so
, *so_next
;
413 global_readfds
= readfds
;
414 global_writefds
= writefds
;
421 * See if anything has timed out
424 if (time_fasttimo
&& ((curtime
- time_fasttimo
) >= 2)) {
428 if (do_slowtimo
&& ((curtime
- last_slowtimo
) >= 499)) {
431 last_slowtimo
= curtime
;
442 for (so
= tcb
.so_next
; so
!= &tcb
; so
= so_next
) {
443 so_next
= so
->so_next
;
446 * FD_ISSET is meaningless on these sockets
447 * (and they can crash the program)
449 if (so
->so_state
& SS_NOFDREF
|| so
->s
== -1)
454 * This will soread as well, so no need to
455 * test for readfds below if this succeeds
457 if (FD_ISSET(so
->s
, xfds
))
460 * Check sockets for reading
462 else if (FD_ISSET(so
->s
, readfds
)) {
464 * Check for incoming connections
466 if (so
->so_state
& SS_FACCEPTCONN
) {
472 /* Output it if we read something */
474 tcp_output(sototcpcb(so
));
478 * Check sockets for writing
480 if (FD_ISSET(so
->s
, writefds
)) {
482 * Check for non-blocking, still-connecting sockets
484 if (so
->so_state
& SS_ISFCONNECTING
) {
486 so
->so_state
&= ~SS_ISFCONNECTING
;
488 ret
= send(so
->s
, (const void *) &ret
, 0, 0);
490 /* XXXXX Must fix, zero bytes is a NOP */
491 if (errno
== EAGAIN
|| errno
== EWOULDBLOCK
||
492 errno
== EINPROGRESS
|| errno
== ENOTCONN
)
496 so
->so_state
= SS_NOFDREF
;
498 /* else so->so_state &= ~SS_ISFCONNECTING; */
503 tcp_input((struct mbuf
*)NULL
, sizeof(struct ip
), so
);
508 * XXXXX If we wrote something (a lot), there
509 * could be a need for a window update.
510 * In the worst case, the remote will send
511 * a window probe to get things going again
516 * Probe a still-connecting, non-blocking socket
517 * to check if it's still alive
520 if (so
->so_state
& SS_ISFCONNECTING
) {
521 ret
= recv(so
->s
, (char *)&ret
, 0,0);
525 if (errno
== EAGAIN
|| errno
== EWOULDBLOCK
||
526 errno
== EINPROGRESS
|| errno
== ENOTCONN
)
527 continue; /* Still connecting, continue */
530 so
->so_state
= SS_NOFDREF
;
532 /* tcp_input will take care of it */
534 ret
= send(so
->s
, &ret
, 0,0);
537 if (errno
== EAGAIN
|| errno
== EWOULDBLOCK
||
538 errno
== EINPROGRESS
|| errno
== ENOTCONN
)
541 so
->so_state
= SS_NOFDREF
;
543 so
->so_state
&= ~SS_ISFCONNECTING
;
546 tcp_input((struct mbuf
*)NULL
, sizeof(struct ip
),so
);
547 } /* SS_ISFCONNECTING */
553 * Incoming packets are sent straight away, they're not buffered.
554 * Incoming UDP data isn't buffered either.
556 for (so
= udb
.so_next
; so
!= &udb
; so
= so_next
) {
557 so_next
= so
->so_next
;
559 if (so
->s
!= -1 && FD_ISSET(so
->s
, readfds
)) {
566 * See if we can start outputting
568 if (if_queued
&& link_up
)
571 /* clear global file descriptor sets.
572 * these reside on the stack in vl.c
573 * so they're unusable if we're not in
574 * slirp_select_fill or slirp_select_poll.
576 global_readfds
= NULL
;
577 global_writefds
= NULL
;
584 #define ETH_P_IP 0x0800 /* Internet Protocol packet */
585 #define ETH_P_ARP 0x0806 /* Address Resolution packet */
587 #define ARPOP_REQUEST 1 /* ARP request */
588 #define ARPOP_REPLY 2 /* ARP reply */
592 unsigned char h_dest
[ETH_ALEN
]; /* destination eth addr */
593 unsigned char h_source
[ETH_ALEN
]; /* source ether addr */
594 unsigned short h_proto
; /* packet type ID field */
599 unsigned short ar_hrd
; /* format of hardware address */
600 unsigned short ar_pro
; /* format of protocol address */
601 unsigned char ar_hln
; /* length of hardware address */
602 unsigned char ar_pln
; /* length of protocol address */
603 unsigned short ar_op
; /* ARP opcode (command) */
606 * Ethernet looks like this : This bit is variable sized however...
608 unsigned char ar_sha
[ETH_ALEN
]; /* sender hardware address */
609 uint32_t ar_sip
; /* sender IP address */
610 unsigned char ar_tha
[ETH_ALEN
]; /* target hardware address */
611 uint32_t ar_tip
; /* target IP address */
612 } __attribute__((packed
));
614 static void arp_input(const uint8_t *pkt
, int pkt_len
)
616 struct ethhdr
*eh
= (struct ethhdr
*)pkt
;
617 struct arphdr
*ah
= (struct arphdr
*)(pkt
+ ETH_HLEN
);
618 uint8_t arp_reply
[ETH_HLEN
+ sizeof(struct arphdr
)];
619 struct ethhdr
*reh
= (struct ethhdr
*)arp_reply
;
620 struct arphdr
*rah
= (struct arphdr
*)(arp_reply
+ ETH_HLEN
);
622 struct ex_list
*ex_ptr
;
624 ar_op
= ntohs(ah
->ar_op
);
627 if ((ah
->ar_tip
& vnetwork_mask
.s_addr
) == vnetwork_addr
.s_addr
) {
628 if (ah
->ar_tip
== vnameserver_addr
.s_addr
||
629 ah
->ar_tip
== vhost_addr
.s_addr
)
631 for (ex_ptr
= exec_list
; ex_ptr
; ex_ptr
= ex_ptr
->ex_next
) {
632 if (ex_ptr
->ex_addr
.s_addr
== ah
->ar_tip
)
637 /* XXX: make an ARP request to have the client address */
638 memcpy(client_ethaddr
, eh
->h_source
, ETH_ALEN
);
640 /* ARP request for alias/dns mac address */
641 memcpy(reh
->h_dest
, pkt
+ ETH_ALEN
, ETH_ALEN
);
642 memcpy(reh
->h_source
, special_ethaddr
, ETH_ALEN
- 4);
643 memcpy(&reh
->h_source
[2], &ah
->ar_tip
, 4);
644 reh
->h_proto
= htons(ETH_P_ARP
);
646 rah
->ar_hrd
= htons(1);
647 rah
->ar_pro
= htons(ETH_P_IP
);
648 rah
->ar_hln
= ETH_ALEN
;
650 rah
->ar_op
= htons(ARPOP_REPLY
);
651 memcpy(rah
->ar_sha
, reh
->h_source
, ETH_ALEN
);
652 rah
->ar_sip
= ah
->ar_tip
;
653 memcpy(rah
->ar_tha
, ah
->ar_sha
, ETH_ALEN
);
654 rah
->ar_tip
= ah
->ar_sip
;
655 slirp_output(arp_reply
, sizeof(arp_reply
));
659 /* reply to request of client mac address ? */
660 if (!memcmp(client_ethaddr
, zero_ethaddr
, ETH_ALEN
) &&
661 ah
->ar_sip
== client_ipaddr
.s_addr
) {
662 memcpy(client_ethaddr
, ah
->ar_sha
, ETH_ALEN
);
670 void slirp_input(const uint8_t *pkt
, int pkt_len
)
675 if (pkt_len
< ETH_HLEN
)
678 proto
= ntohs(*(uint16_t *)(pkt
+ 12));
681 arp_input(pkt
, pkt_len
);
687 /* Note: we add to align the IP header */
688 if (M_FREEROOM(m
) < pkt_len
+ 2) {
689 m_inc(m
, pkt_len
+ 2);
691 m
->m_len
= pkt_len
+ 2;
692 memcpy(m
->m_data
+ 2, pkt
, pkt_len
);
694 m
->m_data
+= 2 + ETH_HLEN
;
695 m
->m_len
-= 2 + ETH_HLEN
;
704 /* output the IP packet to the ethernet device */
705 void if_encap(const uint8_t *ip_data
, int ip_data_len
)
708 struct ethhdr
*eh
= (struct ethhdr
*)buf
;
710 if (ip_data_len
+ ETH_HLEN
> sizeof(buf
))
713 if (!memcmp(client_ethaddr
, zero_ethaddr
, ETH_ALEN
)) {
714 uint8_t arp_req
[ETH_HLEN
+ sizeof(struct arphdr
)];
715 struct ethhdr
*reh
= (struct ethhdr
*)arp_req
;
716 struct arphdr
*rah
= (struct arphdr
*)(arp_req
+ ETH_HLEN
);
717 const struct ip
*iph
= (const struct ip
*)ip_data
;
719 /* If the client addr is not known, there is no point in
720 sending the packet to it. Normally the sender should have
721 done an ARP request to get its MAC address. Here we do it
722 in place of sending the packet and we hope that the sender
723 will retry sending its packet. */
724 memset(reh
->h_dest
, 0xff, ETH_ALEN
);
725 memcpy(reh
->h_source
, special_ethaddr
, ETH_ALEN
- 4);
726 memcpy(&reh
->h_source
[2], &vhost_addr
, 4);
727 reh
->h_proto
= htons(ETH_P_ARP
);
728 rah
->ar_hrd
= htons(1);
729 rah
->ar_pro
= htons(ETH_P_IP
);
730 rah
->ar_hln
= ETH_ALEN
;
732 rah
->ar_op
= htons(ARPOP_REQUEST
);
734 memcpy(rah
->ar_sha
, special_ethaddr
, ETH_ALEN
- 4);
735 memcpy(&rah
->ar_sha
[2], &vhost_addr
, 4);
737 rah
->ar_sip
= vhost_addr
.s_addr
;
738 /* target hw addr (none) */
739 memset(rah
->ar_tha
, 0, ETH_ALEN
);
741 rah
->ar_tip
= iph
->ip_dst
.s_addr
;
742 client_ipaddr
= iph
->ip_dst
;
743 slirp_output(arp_req
, sizeof(arp_req
));
745 memcpy(eh
->h_dest
, client_ethaddr
, ETH_ALEN
);
746 memcpy(eh
->h_source
, special_ethaddr
, ETH_ALEN
- 4);
747 /* XXX: not correct */
748 memcpy(&eh
->h_source
[2], &vhost_addr
, 4);
749 eh
->h_proto
= htons(ETH_P_IP
);
750 memcpy(buf
+ sizeof(struct ethhdr
), ip_data
, ip_data_len
);
751 slirp_output(buf
, ip_data_len
+ ETH_HLEN
);
755 /* Unlistens a redirection
757 * Return value: number of redirs removed */
758 int slirp_redir_rm(int is_udp
, int host_port
)
761 struct socket
*head
= (is_udp
? &udb
: &tcb
);
762 int fport
= htons(host_port
);
766 for (so
= head
->so_next
; so
!= head
; so
= so
->so_next
) {
767 if (so
->so_fport
== fport
) {
778 int slirp_redir(int is_udp
, int host_port
,
779 struct in_addr guest_addr
, int guest_port
)
781 if (!guest_addr
.s_addr
) {
782 guest_addr
= vdhcp_startaddr
;
785 if (!udp_listen(htons(host_port
), guest_addr
.s_addr
,
786 htons(guest_port
), 0))
789 if (!solisten(htons(host_port
), guest_addr
.s_addr
,
790 htons(guest_port
), 0))
796 int slirp_add_exec(int do_pty
, const void *args
, int addr_low_byte
,
799 struct in_addr guest_addr
= {
800 .s_addr
= vnetwork_addr
.s_addr
| htonl(addr_low_byte
)
803 if ((guest_addr
.s_addr
& vnetwork_mask
.s_addr
) != vnetwork_addr
.s_addr
||
804 guest_addr
.s_addr
== vhost_addr
.s_addr
||
805 guest_addr
.s_addr
== vnameserver_addr
.s_addr
) {
808 return add_exec(&exec_list
, do_pty
, (char *)args
, guest_addr
,
812 ssize_t
slirp_send(struct socket
*so
, const void *buf
, size_t len
, int flags
)
814 if (so
->s
== -1 && so
->extra
) {
815 qemu_chr_write(so
->extra
, buf
, len
);
819 return send(so
->s
, buf
, len
, flags
);
822 static struct socket
*
823 slirp_find_ctl_socket(struct in_addr guest_addr
, int guest_port
)
827 for (so
= tcb
.so_next
; so
!= &tcb
; so
= so
->so_next
) {
828 if (so
->so_faddr
.s_addr
== guest_addr
.s_addr
&&
829 htons(so
->so_fport
) == guest_port
) {
836 size_t slirp_socket_can_recv(int addr_low_byte
, int guest_port
)
838 struct in_addr guest_addr
= {
839 .s_addr
= vnetwork_addr
.s_addr
| htonl(addr_low_byte
)
847 so
= slirp_find_ctl_socket(guest_addr
, guest_port
);
849 if (!so
|| so
->so_state
& SS_NOFDREF
)
852 if (!CONN_CANFRCV(so
) || so
->so_snd
.sb_cc
>= (so
->so_snd
.sb_datalen
/2))
855 return sopreprbuf(so
, iov
, NULL
);
858 void slirp_socket_recv(int addr_low_byte
, int guest_port
, const uint8_t *buf
,
862 struct in_addr guest_addr
= {
863 .s_addr
= vnetwork_addr
.s_addr
| htonl(addr_low_byte
)
865 struct socket
*so
= slirp_find_ctl_socket(guest_addr
, guest_port
);
870 ret
= soreadbuf(so
, (const char *)buf
, size
);
873 tcp_output(sototcpcb(so
));
876 static void slirp_tcp_save(QEMUFile
*f
, struct tcpcb
*tp
)
880 qemu_put_sbe16(f
, tp
->t_state
);
881 for (i
= 0; i
< TCPT_NTIMERS
; i
++)
882 qemu_put_sbe16(f
, tp
->t_timer
[i
]);
883 qemu_put_sbe16(f
, tp
->t_rxtshift
);
884 qemu_put_sbe16(f
, tp
->t_rxtcur
);
885 qemu_put_sbe16(f
, tp
->t_dupacks
);
886 qemu_put_be16(f
, tp
->t_maxseg
);
887 qemu_put_sbyte(f
, tp
->t_force
);
888 qemu_put_be16(f
, tp
->t_flags
);
889 qemu_put_be32(f
, tp
->snd_una
);
890 qemu_put_be32(f
, tp
->snd_nxt
);
891 qemu_put_be32(f
, tp
->snd_up
);
892 qemu_put_be32(f
, tp
->snd_wl1
);
893 qemu_put_be32(f
, tp
->snd_wl2
);
894 qemu_put_be32(f
, tp
->iss
);
895 qemu_put_be32(f
, tp
->snd_wnd
);
896 qemu_put_be32(f
, tp
->rcv_wnd
);
897 qemu_put_be32(f
, tp
->rcv_nxt
);
898 qemu_put_be32(f
, tp
->rcv_up
);
899 qemu_put_be32(f
, tp
->irs
);
900 qemu_put_be32(f
, tp
->rcv_adv
);
901 qemu_put_be32(f
, tp
->snd_max
);
902 qemu_put_be32(f
, tp
->snd_cwnd
);
903 qemu_put_be32(f
, tp
->snd_ssthresh
);
904 qemu_put_sbe16(f
, tp
->t_idle
);
905 qemu_put_sbe16(f
, tp
->t_rtt
);
906 qemu_put_be32(f
, tp
->t_rtseq
);
907 qemu_put_sbe16(f
, tp
->t_srtt
);
908 qemu_put_sbe16(f
, tp
->t_rttvar
);
909 qemu_put_be16(f
, tp
->t_rttmin
);
910 qemu_put_be32(f
, tp
->max_sndwnd
);
911 qemu_put_byte(f
, tp
->t_oobflags
);
912 qemu_put_byte(f
, tp
->t_iobc
);
913 qemu_put_sbe16(f
, tp
->t_softerror
);
914 qemu_put_byte(f
, tp
->snd_scale
);
915 qemu_put_byte(f
, tp
->rcv_scale
);
916 qemu_put_byte(f
, tp
->request_r_scale
);
917 qemu_put_byte(f
, tp
->requested_s_scale
);
918 qemu_put_be32(f
, tp
->ts_recent
);
919 qemu_put_be32(f
, tp
->ts_recent_age
);
920 qemu_put_be32(f
, tp
->last_ack_sent
);
923 static void slirp_sbuf_save(QEMUFile
*f
, struct sbuf
*sbuf
)
927 qemu_put_be32(f
, sbuf
->sb_cc
);
928 qemu_put_be32(f
, sbuf
->sb_datalen
);
929 off
= (uint32_t)(sbuf
->sb_wptr
- sbuf
->sb_data
);
930 qemu_put_sbe32(f
, off
);
931 off
= (uint32_t)(sbuf
->sb_rptr
- sbuf
->sb_data
);
932 qemu_put_sbe32(f
, off
);
933 qemu_put_buffer(f
, (unsigned char*)sbuf
->sb_data
, sbuf
->sb_datalen
);
936 static void slirp_socket_save(QEMUFile
*f
, struct socket
*so
)
938 qemu_put_be32(f
, so
->so_urgc
);
939 qemu_put_be32(f
, so
->so_faddr
.s_addr
);
940 qemu_put_be32(f
, so
->so_laddr
.s_addr
);
941 qemu_put_be16(f
, so
->so_fport
);
942 qemu_put_be16(f
, so
->so_lport
);
943 qemu_put_byte(f
, so
->so_iptos
);
944 qemu_put_byte(f
, so
->so_emu
);
945 qemu_put_byte(f
, so
->so_type
);
946 qemu_put_be32(f
, so
->so_state
);
947 slirp_sbuf_save(f
, &so
->so_rcv
);
948 slirp_sbuf_save(f
, &so
->so_snd
);
949 slirp_tcp_save(f
, so
->so_tcpcb
);
952 static void slirp_state_save(QEMUFile
*f
, void *opaque
)
954 struct ex_list
*ex_ptr
;
956 for (ex_ptr
= exec_list
; ex_ptr
; ex_ptr
= ex_ptr
->ex_next
)
957 if (ex_ptr
->ex_pty
== 3) {
959 so
= slirp_find_ctl_socket(ex_ptr
->ex_addr
, ntohs(ex_ptr
->ex_fport
));
963 qemu_put_byte(f
, 42);
964 slirp_socket_save(f
, so
);
969 static void slirp_tcp_load(QEMUFile
*f
, struct tcpcb
*tp
)
973 tp
->t_state
= qemu_get_sbe16(f
);
974 for (i
= 0; i
< TCPT_NTIMERS
; i
++)
975 tp
->t_timer
[i
] = qemu_get_sbe16(f
);
976 tp
->t_rxtshift
= qemu_get_sbe16(f
);
977 tp
->t_rxtcur
= qemu_get_sbe16(f
);
978 tp
->t_dupacks
= qemu_get_sbe16(f
);
979 tp
->t_maxseg
= qemu_get_be16(f
);
980 tp
->t_force
= qemu_get_sbyte(f
);
981 tp
->t_flags
= qemu_get_be16(f
);
982 tp
->snd_una
= qemu_get_be32(f
);
983 tp
->snd_nxt
= qemu_get_be32(f
);
984 tp
->snd_up
= qemu_get_be32(f
);
985 tp
->snd_wl1
= qemu_get_be32(f
);
986 tp
->snd_wl2
= qemu_get_be32(f
);
987 tp
->iss
= qemu_get_be32(f
);
988 tp
->snd_wnd
= qemu_get_be32(f
);
989 tp
->rcv_wnd
= qemu_get_be32(f
);
990 tp
->rcv_nxt
= qemu_get_be32(f
);
991 tp
->rcv_up
= qemu_get_be32(f
);
992 tp
->irs
= qemu_get_be32(f
);
993 tp
->rcv_adv
= qemu_get_be32(f
);
994 tp
->snd_max
= qemu_get_be32(f
);
995 tp
->snd_cwnd
= qemu_get_be32(f
);
996 tp
->snd_ssthresh
= qemu_get_be32(f
);
997 tp
->t_idle
= qemu_get_sbe16(f
);
998 tp
->t_rtt
= qemu_get_sbe16(f
);
999 tp
->t_rtseq
= qemu_get_be32(f
);
1000 tp
->t_srtt
= qemu_get_sbe16(f
);
1001 tp
->t_rttvar
= qemu_get_sbe16(f
);
1002 tp
->t_rttmin
= qemu_get_be16(f
);
1003 tp
->max_sndwnd
= qemu_get_be32(f
);
1004 tp
->t_oobflags
= qemu_get_byte(f
);
1005 tp
->t_iobc
= qemu_get_byte(f
);
1006 tp
->t_softerror
= qemu_get_sbe16(f
);
1007 tp
->snd_scale
= qemu_get_byte(f
);
1008 tp
->rcv_scale
= qemu_get_byte(f
);
1009 tp
->request_r_scale
= qemu_get_byte(f
);
1010 tp
->requested_s_scale
= qemu_get_byte(f
);
1011 tp
->ts_recent
= qemu_get_be32(f
);
1012 tp
->ts_recent_age
= qemu_get_be32(f
);
1013 tp
->last_ack_sent
= qemu_get_be32(f
);
1017 static int slirp_sbuf_load(QEMUFile
*f
, struct sbuf
*sbuf
)
1019 uint32_t off
, sb_cc
, sb_datalen
;
1021 sb_cc
= qemu_get_be32(f
);
1022 sb_datalen
= qemu_get_be32(f
);
1024 sbreserve(sbuf
, sb_datalen
);
1026 if (sbuf
->sb_datalen
!= sb_datalen
)
1029 sbuf
->sb_cc
= sb_cc
;
1031 off
= qemu_get_sbe32(f
);
1032 sbuf
->sb_wptr
= sbuf
->sb_data
+ off
;
1033 off
= qemu_get_sbe32(f
);
1034 sbuf
->sb_rptr
= sbuf
->sb_data
+ off
;
1035 qemu_get_buffer(f
, (unsigned char*)sbuf
->sb_data
, sbuf
->sb_datalen
);
1040 static int slirp_socket_load(QEMUFile
*f
, struct socket
*so
)
1042 if (tcp_attach(so
) < 0)
1045 so
->so_urgc
= qemu_get_be32(f
);
1046 so
->so_faddr
.s_addr
= qemu_get_be32(f
);
1047 so
->so_laddr
.s_addr
= qemu_get_be32(f
);
1048 so
->so_fport
= qemu_get_be16(f
);
1049 so
->so_lport
= qemu_get_be16(f
);
1050 so
->so_iptos
= qemu_get_byte(f
);
1051 so
->so_emu
= qemu_get_byte(f
);
1052 so
->so_type
= qemu_get_byte(f
);
1053 so
->so_state
= qemu_get_be32(f
);
1054 if (slirp_sbuf_load(f
, &so
->so_rcv
) < 0)
1056 if (slirp_sbuf_load(f
, &so
->so_snd
) < 0)
1058 slirp_tcp_load(f
, so
->so_tcpcb
);
1063 static int slirp_state_load(QEMUFile
*f
, void *opaque
, int version_id
)
1065 struct ex_list
*ex_ptr
;
1068 while ((r
= qemu_get_byte(f
))) {
1070 struct socket
*so
= socreate();
1075 ret
= slirp_socket_load(f
, so
);
1080 if ((so
->so_faddr
.s_addr
& vnetwork_mask
.s_addr
) !=
1081 vnetwork_addr
.s_addr
) {
1084 for (ex_ptr
= exec_list
; ex_ptr
; ex_ptr
= ex_ptr
->ex_next
) {
1085 if (ex_ptr
->ex_pty
== 3 &&
1086 so
->so_faddr
.s_addr
== ex_ptr
->ex_addr
.s_addr
&&
1087 so
->so_fport
== ex_ptr
->ex_fport
) {
1094 so
->extra
= (void *)ex_ptr
->ex_exec
;