]>
git.proxmox.com Git - qemu.git/blob - slirp/slirp.c
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 /* address for slirp virtual addresses */
37 struct in_addr special_addr
;
38 /* virtual address alias for host */
39 struct in_addr alias_addr
;
41 static const uint8_t special_ethaddr
[6] = {
42 0x52, 0x54, 0x00, 0x12, 0x35, 0x00
45 /* ARP cache for the guest IP addresses (XXX: allow many entries) */
46 uint8_t client_ethaddr
[6];
47 static struct in_addr client_ipaddr
;
49 static const uint8_t zero_ethaddr
[6] = { 0, 0, 0, 0, 0, 0 };
51 const char *slirp_special_ip
= CTL_SPECIAL
;
53 static int do_slowtimo
;
57 struct ex_list
*exec_list
;
59 /* XXX: suppress those select globals */
60 fd_set
*global_readfds
, *global_writefds
, *global_xfds
;
62 char slirp_hostname
[33];
66 static int get_dns_addr(struct in_addr
*pdns_addr
)
68 FIXED_INFO
*FixedInfo
=NULL
;
71 IP_ADDR_STRING
*pIPAddr
;
72 struct in_addr tmp_addr
;
74 FixedInfo
= (FIXED_INFO
*)GlobalAlloc(GPTR
, sizeof(FIXED_INFO
));
75 BufLen
= sizeof(FIXED_INFO
);
77 if (ERROR_BUFFER_OVERFLOW
== GetNetworkParams(FixedInfo
, &BufLen
)) {
79 GlobalFree(FixedInfo
);
82 FixedInfo
= GlobalAlloc(GPTR
, BufLen
);
85 if ((ret
= GetNetworkParams(FixedInfo
, &BufLen
)) != ERROR_SUCCESS
) {
86 printf("GetNetworkParams failed. ret = %08x\n", (u_int
)ret
);
88 GlobalFree(FixedInfo
);
94 pIPAddr
= &(FixedInfo
->DnsServerList
);
95 inet_aton(pIPAddr
->IpAddress
.String
, &tmp_addr
);
96 *pdns_addr
= tmp_addr
;
98 printf( "DNS Servers:\n" );
99 printf( "DNS Addr:%s\n", pIPAddr
->IpAddress
.String
);
101 pIPAddr
= FixedInfo
-> DnsServerList
.Next
;
103 printf( "DNS Addr:%s\n", pIPAddr
->IpAddress
.String
);
104 pIPAddr
= pIPAddr
->Next
;
108 GlobalFree(FixedInfo
);
116 static int get_dns_addr(struct in_addr
*pdns_addr
)
122 struct in_addr tmp_addr
;
124 f
= fopen("/etc/resolv.conf", "r");
129 lprint("IP address of your DNS(s): ");
131 while (fgets(buff
, 512, f
) != NULL
) {
132 if (sscanf(buff
, "nameserver%*[ \t]%256s", buff2
) == 1) {
133 if (!inet_aton(buff2
, &tmp_addr
))
135 if (tmp_addr
.s_addr
== loopback_addr
.s_addr
)
137 /* If it's the first one, set it to dns_addr */
139 *pdns_addr
= tmp_addr
;
152 lprint("%s", inet_ntoa(tmp_addr
));
165 static void slirp_cleanup(void)
171 static void slirp_state_save(QEMUFile
*f
, void *opaque
);
172 static int slirp_state_load(QEMUFile
*f
, void *opaque
, int version_id
);
174 void slirp_init(int restricted
, const char *special_ip
, const char *tftp_path
,
175 const char *bootfile
)
177 // debug_init("/tmp/slirp.log", DEBUG_DEFAULT);
182 WSAStartup(MAKEWORD(2,0), &Data
);
183 atexit(slirp_cleanup
);
188 slirp_restrict
= restricted
;
193 /* Initialise mbufs *after* setting the MTU */
196 /* set default addresses */
197 inet_aton("127.0.0.1", &loopback_addr
);
199 if (get_dns_addr(&dns_addr
) < 0) {
200 dns_addr
= loopback_addr
;
201 fprintf (stderr
, "Warning: No DNS servers found\n");
205 slirp_special_ip
= special_ip
;
207 qemu_free(tftp_prefix
);
210 tftp_prefix
= qemu_strdup(tftp_path
);
212 qemu_free(bootp_filename
);
213 bootp_filename
= NULL
;
215 bootp_filename
= qemu_strdup(bootfile
);
218 inet_aton(slirp_special_ip
, &special_addr
);
219 alias_addr
.s_addr
= special_addr
.s_addr
| htonl(CTL_ALIAS
);
221 register_savevm("slirp", 0, 1, slirp_state_save
, slirp_state_load
, NULL
);
224 #define CONN_CANFSEND(so) (((so)->so_state & (SS_FCANTSENDMORE|SS_ISFCONNECTED)) == SS_ISFCONNECTED)
225 #define CONN_CANFRCV(so) (((so)->so_state & (SS_FCANTRCVMORE|SS_ISFCONNECTED)) == SS_ISFCONNECTED)
226 #define UPD_NFDS(x) if (nfds < (x)) nfds = (x)
229 * curtime kept to an accuracy of 1ms
232 static void updtime(void)
237 curtime
= (u_int
)tb
.time
* (u_int
)1000;
238 curtime
+= (u_int
)tb
.millitm
;
241 static void updtime(void)
243 gettimeofday(&tt
, NULL
);
245 curtime
= (u_int
)tt
.tv_sec
* (u_int
)1000;
246 curtime
+= (u_int
)tt
.tv_usec
/ (u_int
)1000;
248 if ((tt
.tv_usec
% 1000) >= 500)
253 void slirp_select_fill(int *pnfds
,
254 fd_set
*readfds
, fd_set
*writefds
, fd_set
*xfds
)
256 struct socket
*so
, *so_next
;
257 struct timeval timeout
;
262 global_readfds
= NULL
;
263 global_writefds
= NULL
;
273 * *_slowtimo needs calling if there are IP fragments
274 * in the fragment queue, or there are TCP connections active
276 do_slowtimo
= ((tcb
.so_next
!= &tcb
) ||
277 (&ipq
.ip_link
!= ipq
.ip_link
.next
));
279 for (so
= tcb
.so_next
; so
!= &tcb
; so
= so_next
) {
280 so_next
= so
->so_next
;
283 * See if we need a tcp_fasttimo
285 if (time_fasttimo
== 0 && so
->so_tcpcb
->t_flags
& TF_DELACK
)
286 time_fasttimo
= curtime
; /* Flag when we want a fasttimo */
289 * NOFDREF can include still connecting to local-host,
290 * newly socreated() sockets etc. Don't want to select these.
292 if (so
->so_state
& SS_NOFDREF
|| so
->s
== -1)
296 * Set for reading sockets which are accepting
298 if (so
->so_state
& SS_FACCEPTCONN
) {
299 FD_SET(so
->s
, readfds
);
305 * Set for writing sockets which are connecting
307 if (so
->so_state
& SS_ISFCONNECTING
) {
308 FD_SET(so
->s
, writefds
);
314 * Set for writing if we are connected, can send more, and
315 * we have something to send
317 if (CONN_CANFSEND(so
) && so
->so_rcv
.sb_cc
) {
318 FD_SET(so
->s
, writefds
);
323 * Set for reading (and urgent data) if we are connected, can
324 * receive more, and we have room for it XXX /2 ?
326 if (CONN_CANFRCV(so
) && (so
->so_snd
.sb_cc
< (so
->so_snd
.sb_datalen
/2))) {
327 FD_SET(so
->s
, readfds
);
336 for (so
= udb
.so_next
; so
!= &udb
; so
= so_next
) {
337 so_next
= so
->so_next
;
340 * See if it's timed out
343 if (so
->so_expire
<= curtime
) {
347 do_slowtimo
= 1; /* Let socket expire */
351 * When UDP packets are received from over the
352 * link, they're sendto()'d straight away, so
353 * no need for setting for writing
354 * Limit the number of packets queued by this session
355 * to 4. Note that even though we try and limit this
356 * to 4 packets, the session could have more queued
357 * if the packets needed to be fragmented
360 if ((so
->so_state
& SS_ISFCONNECTED
) && so
->so_queued
<= 4) {
361 FD_SET(so
->s
, readfds
);
368 * Setup timeout to use minimum CPU usage, especially when idle
372 * First, see the timeout needed by *timo
375 timeout
.tv_usec
= -1;
377 * If a slowtimo is needed, set timeout to 500ms from the last
378 * slow timeout. If a fast timeout is needed, set timeout within
379 * 200ms of when it was requested.
382 /* XXX + 10000 because some select()'s aren't that accurate */
383 timeout
.tv_usec
= ((500 - (curtime
- last_slowtimo
)) * 1000) + 10000;
384 if (timeout
.tv_usec
< 0)
386 else if (timeout
.tv_usec
> 510000)
387 timeout
.tv_usec
= 510000;
389 /* Can only fasttimo if we also slowtimo */
391 tmp_time
= (200 - (curtime
- time_fasttimo
)) * 1000;
395 /* Choose the smallest of the 2 */
396 if (tmp_time
< timeout
.tv_usec
)
397 timeout
.tv_usec
= (u_int
)tmp_time
;
403 void slirp_select_poll(fd_set
*readfds
, fd_set
*writefds
, fd_set
*xfds
)
405 struct socket
*so
, *so_next
;
408 global_readfds
= readfds
;
409 global_writefds
= writefds
;
416 * See if anything has timed out
419 if (time_fasttimo
&& ((curtime
- time_fasttimo
) >= 2)) {
423 if (do_slowtimo
&& ((curtime
- last_slowtimo
) >= 499)) {
426 last_slowtimo
= curtime
;
437 for (so
= tcb
.so_next
; so
!= &tcb
; so
= so_next
) {
438 so_next
= so
->so_next
;
441 * FD_ISSET is meaningless on these sockets
442 * (and they can crash the program)
444 if (so
->so_state
& SS_NOFDREF
|| so
->s
== -1)
449 * This will soread as well, so no need to
450 * test for readfds below if this succeeds
452 if (FD_ISSET(so
->s
, xfds
))
455 * Check sockets for reading
457 else if (FD_ISSET(so
->s
, readfds
)) {
459 * Check for incoming connections
461 if (so
->so_state
& SS_FACCEPTCONN
) {
467 /* Output it if we read something */
469 tcp_output(sototcpcb(so
));
473 * Check sockets for writing
475 if (FD_ISSET(so
->s
, writefds
)) {
477 * Check for non-blocking, still-connecting sockets
479 if (so
->so_state
& SS_ISFCONNECTING
) {
481 so
->so_state
&= ~SS_ISFCONNECTING
;
483 ret
= send(so
->s
, (const void *) &ret
, 0, 0);
485 /* XXXXX Must fix, zero bytes is a NOP */
486 if (errno
== EAGAIN
|| errno
== EWOULDBLOCK
||
487 errno
== EINPROGRESS
|| errno
== ENOTCONN
)
491 so
->so_state
= SS_NOFDREF
;
493 /* else so->so_state &= ~SS_ISFCONNECTING; */
498 tcp_input((struct mbuf
*)NULL
, sizeof(struct ip
), so
);
503 * XXXXX If we wrote something (a lot), there
504 * could be a need for a window update.
505 * In the worst case, the remote will send
506 * a window probe to get things going again
511 * Probe a still-connecting, non-blocking socket
512 * to check if it's still alive
515 if (so
->so_state
& SS_ISFCONNECTING
) {
516 ret
= recv(so
->s
, (char *)&ret
, 0,0);
520 if (errno
== EAGAIN
|| errno
== EWOULDBLOCK
||
521 errno
== EINPROGRESS
|| errno
== ENOTCONN
)
522 continue; /* Still connecting, continue */
525 so
->so_state
= SS_NOFDREF
;
527 /* tcp_input will take care of it */
529 ret
= send(so
->s
, &ret
, 0,0);
532 if (errno
== EAGAIN
|| errno
== EWOULDBLOCK
||
533 errno
== EINPROGRESS
|| errno
== ENOTCONN
)
536 so
->so_state
= SS_NOFDREF
;
538 so
->so_state
&= ~SS_ISFCONNECTING
;
541 tcp_input((struct mbuf
*)NULL
, sizeof(struct ip
),so
);
542 } /* SS_ISFCONNECTING */
548 * Incoming packets are sent straight away, they're not buffered.
549 * Incoming UDP data isn't buffered either.
551 for (so
= udb
.so_next
; so
!= &udb
; so
= so_next
) {
552 so_next
= so
->so_next
;
554 if (so
->s
!= -1 && FD_ISSET(so
->s
, readfds
)) {
561 * See if we can start outputting
563 if (if_queued
&& link_up
)
566 /* clear global file descriptor sets.
567 * these reside on the stack in vl.c
568 * so they're unusable if we're not in
569 * slirp_select_fill or slirp_select_poll.
571 global_readfds
= NULL
;
572 global_writefds
= NULL
;
579 #define ETH_P_IP 0x0800 /* Internet Protocol packet */
580 #define ETH_P_ARP 0x0806 /* Address Resolution packet */
582 #define ARPOP_REQUEST 1 /* ARP request */
583 #define ARPOP_REPLY 2 /* ARP reply */
587 unsigned char h_dest
[ETH_ALEN
]; /* destination eth addr */
588 unsigned char h_source
[ETH_ALEN
]; /* source ether addr */
589 unsigned short h_proto
; /* packet type ID field */
594 unsigned short ar_hrd
; /* format of hardware address */
595 unsigned short ar_pro
; /* format of protocol address */
596 unsigned char ar_hln
; /* length of hardware address */
597 unsigned char ar_pln
; /* length of protocol address */
598 unsigned short ar_op
; /* ARP opcode (command) */
601 * Ethernet looks like this : This bit is variable sized however...
603 unsigned char ar_sha
[ETH_ALEN
]; /* sender hardware address */
604 unsigned char ar_sip
[4]; /* sender IP address */
605 unsigned char ar_tha
[ETH_ALEN
]; /* target hardware address */
606 unsigned char ar_tip
[4]; /* target IP address */
609 static void arp_input(const uint8_t *pkt
, int pkt_len
)
611 struct ethhdr
*eh
= (struct ethhdr
*)pkt
;
612 struct arphdr
*ah
= (struct arphdr
*)(pkt
+ ETH_HLEN
);
613 uint8_t arp_reply
[ETH_HLEN
+ sizeof(struct arphdr
)];
614 struct ethhdr
*reh
= (struct ethhdr
*)arp_reply
;
615 struct arphdr
*rah
= (struct arphdr
*)(arp_reply
+ ETH_HLEN
);
617 struct ex_list
*ex_ptr
;
619 ar_op
= ntohs(ah
->ar_op
);
622 if (!memcmp(ah
->ar_tip
, &special_addr
, 3)) {
623 if (ah
->ar_tip
[3] == CTL_DNS
|| ah
->ar_tip
[3] == CTL_ALIAS
)
625 for (ex_ptr
= exec_list
; ex_ptr
; ex_ptr
= ex_ptr
->ex_next
) {
626 if (ex_ptr
->ex_addr
== ah
->ar_tip
[3])
631 /* XXX: make an ARP request to have the client address */
632 memcpy(client_ethaddr
, eh
->h_source
, ETH_ALEN
);
634 /* ARP request for alias/dns mac address */
635 memcpy(reh
->h_dest
, pkt
+ ETH_ALEN
, ETH_ALEN
);
636 memcpy(reh
->h_source
, special_ethaddr
, ETH_ALEN
- 1);
637 reh
->h_source
[5] = ah
->ar_tip
[3];
638 reh
->h_proto
= htons(ETH_P_ARP
);
640 rah
->ar_hrd
= htons(1);
641 rah
->ar_pro
= htons(ETH_P_IP
);
642 rah
->ar_hln
= ETH_ALEN
;
644 rah
->ar_op
= htons(ARPOP_REPLY
);
645 memcpy(rah
->ar_sha
, reh
->h_source
, ETH_ALEN
);
646 memcpy(rah
->ar_sip
, ah
->ar_tip
, 4);
647 memcpy(rah
->ar_tha
, ah
->ar_sha
, ETH_ALEN
);
648 memcpy(rah
->ar_tip
, ah
->ar_sip
, 4);
649 slirp_output(arp_reply
, sizeof(arp_reply
));
653 /* reply to request of client mac address ? */
654 if (!memcmp(client_ethaddr
, zero_ethaddr
, ETH_ALEN
) &&
655 !memcmp(ah
->ar_sip
, &client_ipaddr
.s_addr
, 4)) {
656 memcpy(client_ethaddr
, ah
->ar_sha
, ETH_ALEN
);
664 void slirp_input(const uint8_t *pkt
, int pkt_len
)
669 if (pkt_len
< ETH_HLEN
)
672 proto
= ntohs(*(uint16_t *)(pkt
+ 12));
675 arp_input(pkt
, pkt_len
);
681 /* Note: we add to align the IP header */
682 if (M_FREEROOM(m
) < pkt_len
+ 2) {
683 m_inc(m
, pkt_len
+ 2);
685 m
->m_len
= pkt_len
+ 2;
686 memcpy(m
->m_data
+ 2, pkt
, pkt_len
);
688 m
->m_data
+= 2 + ETH_HLEN
;
689 m
->m_len
-= 2 + ETH_HLEN
;
698 /* output the IP packet to the ethernet device */
699 void if_encap(const uint8_t *ip_data
, int ip_data_len
)
702 struct ethhdr
*eh
= (struct ethhdr
*)buf
;
704 if (ip_data_len
+ ETH_HLEN
> sizeof(buf
))
707 if (!memcmp(client_ethaddr
, zero_ethaddr
, ETH_ALEN
)) {
708 uint8_t arp_req
[ETH_HLEN
+ sizeof(struct arphdr
)];
709 struct ethhdr
*reh
= (struct ethhdr
*)arp_req
;
710 struct arphdr
*rah
= (struct arphdr
*)(arp_req
+ ETH_HLEN
);
711 const struct ip
*iph
= (const struct ip
*)ip_data
;
713 /* If the client addr is not known, there is no point in
714 sending the packet to it. Normally the sender should have
715 done an ARP request to get its MAC address. Here we do it
716 in place of sending the packet and we hope that the sender
717 will retry sending its packet. */
718 memset(reh
->h_dest
, 0xff, ETH_ALEN
);
719 memcpy(reh
->h_source
, special_ethaddr
, ETH_ALEN
- 1);
720 reh
->h_source
[5] = CTL_ALIAS
;
721 reh
->h_proto
= htons(ETH_P_ARP
);
722 rah
->ar_hrd
= htons(1);
723 rah
->ar_pro
= htons(ETH_P_IP
);
724 rah
->ar_hln
= ETH_ALEN
;
726 rah
->ar_op
= htons(ARPOP_REQUEST
);
728 memcpy(rah
->ar_sha
, special_ethaddr
, ETH_ALEN
- 1);
729 rah
->ar_sha
[5] = CTL_ALIAS
;
731 memcpy(rah
->ar_sip
, &alias_addr
, 4);
732 /* target hw addr (none) */
733 memset(rah
->ar_tha
, 0, ETH_ALEN
);
735 memcpy(rah
->ar_tip
, &iph
->ip_dst
, 4);
736 client_ipaddr
= iph
->ip_dst
;
737 slirp_output(arp_req
, sizeof(arp_req
));
739 memcpy(eh
->h_dest
, client_ethaddr
, ETH_ALEN
);
740 memcpy(eh
->h_source
, special_ethaddr
, ETH_ALEN
- 1);
741 /* XXX: not correct */
742 eh
->h_source
[5] = CTL_ALIAS
;
743 eh
->h_proto
= htons(ETH_P_IP
);
744 memcpy(buf
+ sizeof(struct ethhdr
), ip_data
, ip_data_len
);
745 slirp_output(buf
, ip_data_len
+ ETH_HLEN
);
749 /* Unlistens a redirection
751 * Return value: number of redirs removed */
752 int slirp_redir_rm(int is_udp
, int host_port
)
755 struct socket
*head
= (is_udp
? &udb
: &tcb
);
756 int fport
= htons(host_port
);
760 for (so
= head
->so_next
; so
!= head
; so
= so
->so_next
) {
761 if (so
->so_fport
== fport
) {
772 int slirp_redir(int is_udp
, int host_port
,
773 struct in_addr guest_addr
, int guest_port
)
776 if (!udp_listen(htons(host_port
), guest_addr
.s_addr
,
777 htons(guest_port
), 0))
780 if (!solisten(htons(host_port
), guest_addr
.s_addr
,
781 htons(guest_port
), 0))
787 int slirp_add_exec(int do_pty
, const void *args
, int addr_low_byte
,
790 return add_exec(&exec_list
, do_pty
, (char *)args
,
791 addr_low_byte
, htons(guest_port
));
794 ssize_t
slirp_send(struct socket
*so
, const void *buf
, size_t len
, int flags
)
796 if (so
->s
== -1 && so
->extra
) {
797 qemu_chr_write(so
->extra
, buf
, len
);
801 return send(so
->s
, buf
, len
, flags
);
804 static struct socket
*slirp_find_ctl_socket(int addr_low_byte
, int guest_port
)
808 for (so
= tcb
.so_next
; so
!= &tcb
; so
= so
->so_next
) {
809 if ((so
->so_faddr
.s_addr
& htonl(0xffffff00)) ==
811 && (ntohl(so
->so_faddr
.s_addr
) & 0xff) ==
813 && htons(so
->so_fport
) == guest_port
)
820 size_t slirp_socket_can_recv(int addr_low_byte
, int guest_port
)
828 so
= slirp_find_ctl_socket(addr_low_byte
, guest_port
);
830 if (!so
|| so
->so_state
& SS_NOFDREF
)
833 if (!CONN_CANFRCV(so
) || so
->so_snd
.sb_cc
>= (so
->so_snd
.sb_datalen
/2))
836 return sopreprbuf(so
, iov
, NULL
);
839 void slirp_socket_recv(int addr_low_byte
, int guest_port
, const uint8_t *buf
,
843 struct socket
*so
= slirp_find_ctl_socket(addr_low_byte
, guest_port
);
848 ret
= soreadbuf(so
, (const char *)buf
, size
);
851 tcp_output(sototcpcb(so
));
854 static void slirp_tcp_save(QEMUFile
*f
, struct tcpcb
*tp
)
858 qemu_put_sbe16(f
, tp
->t_state
);
859 for (i
= 0; i
< TCPT_NTIMERS
; i
++)
860 qemu_put_sbe16(f
, tp
->t_timer
[i
]);
861 qemu_put_sbe16(f
, tp
->t_rxtshift
);
862 qemu_put_sbe16(f
, tp
->t_rxtcur
);
863 qemu_put_sbe16(f
, tp
->t_dupacks
);
864 qemu_put_be16(f
, tp
->t_maxseg
);
865 qemu_put_sbyte(f
, tp
->t_force
);
866 qemu_put_be16(f
, tp
->t_flags
);
867 qemu_put_be32(f
, tp
->snd_una
);
868 qemu_put_be32(f
, tp
->snd_nxt
);
869 qemu_put_be32(f
, tp
->snd_up
);
870 qemu_put_be32(f
, tp
->snd_wl1
);
871 qemu_put_be32(f
, tp
->snd_wl2
);
872 qemu_put_be32(f
, tp
->iss
);
873 qemu_put_be32(f
, tp
->snd_wnd
);
874 qemu_put_be32(f
, tp
->rcv_wnd
);
875 qemu_put_be32(f
, tp
->rcv_nxt
);
876 qemu_put_be32(f
, tp
->rcv_up
);
877 qemu_put_be32(f
, tp
->irs
);
878 qemu_put_be32(f
, tp
->rcv_adv
);
879 qemu_put_be32(f
, tp
->snd_max
);
880 qemu_put_be32(f
, tp
->snd_cwnd
);
881 qemu_put_be32(f
, tp
->snd_ssthresh
);
882 qemu_put_sbe16(f
, tp
->t_idle
);
883 qemu_put_sbe16(f
, tp
->t_rtt
);
884 qemu_put_be32(f
, tp
->t_rtseq
);
885 qemu_put_sbe16(f
, tp
->t_srtt
);
886 qemu_put_sbe16(f
, tp
->t_rttvar
);
887 qemu_put_be16(f
, tp
->t_rttmin
);
888 qemu_put_be32(f
, tp
->max_sndwnd
);
889 qemu_put_byte(f
, tp
->t_oobflags
);
890 qemu_put_byte(f
, tp
->t_iobc
);
891 qemu_put_sbe16(f
, tp
->t_softerror
);
892 qemu_put_byte(f
, tp
->snd_scale
);
893 qemu_put_byte(f
, tp
->rcv_scale
);
894 qemu_put_byte(f
, tp
->request_r_scale
);
895 qemu_put_byte(f
, tp
->requested_s_scale
);
896 qemu_put_be32(f
, tp
->ts_recent
);
897 qemu_put_be32(f
, tp
->ts_recent_age
);
898 qemu_put_be32(f
, tp
->last_ack_sent
);
901 static void slirp_sbuf_save(QEMUFile
*f
, struct sbuf
*sbuf
)
905 qemu_put_be32(f
, sbuf
->sb_cc
);
906 qemu_put_be32(f
, sbuf
->sb_datalen
);
907 off
= (uint32_t)(sbuf
->sb_wptr
- sbuf
->sb_data
);
908 qemu_put_sbe32(f
, off
);
909 off
= (uint32_t)(sbuf
->sb_rptr
- sbuf
->sb_data
);
910 qemu_put_sbe32(f
, off
);
911 qemu_put_buffer(f
, (unsigned char*)sbuf
->sb_data
, sbuf
->sb_datalen
);
914 static void slirp_socket_save(QEMUFile
*f
, struct socket
*so
)
916 qemu_put_be32(f
, so
->so_urgc
);
917 qemu_put_be32(f
, so
->so_faddr
.s_addr
);
918 qemu_put_be32(f
, so
->so_laddr
.s_addr
);
919 qemu_put_be16(f
, so
->so_fport
);
920 qemu_put_be16(f
, so
->so_lport
);
921 qemu_put_byte(f
, so
->so_iptos
);
922 qemu_put_byte(f
, so
->so_emu
);
923 qemu_put_byte(f
, so
->so_type
);
924 qemu_put_be32(f
, so
->so_state
);
925 slirp_sbuf_save(f
, &so
->so_rcv
);
926 slirp_sbuf_save(f
, &so
->so_snd
);
927 slirp_tcp_save(f
, so
->so_tcpcb
);
930 static void slirp_state_save(QEMUFile
*f
, void *opaque
)
932 struct ex_list
*ex_ptr
;
934 for (ex_ptr
= exec_list
; ex_ptr
; ex_ptr
= ex_ptr
->ex_next
)
935 if (ex_ptr
->ex_pty
== 3) {
937 so
= slirp_find_ctl_socket(ex_ptr
->ex_addr
, ntohs(ex_ptr
->ex_fport
));
941 qemu_put_byte(f
, 42);
942 slirp_socket_save(f
, so
);
947 static void slirp_tcp_load(QEMUFile
*f
, struct tcpcb
*tp
)
951 tp
->t_state
= qemu_get_sbe16(f
);
952 for (i
= 0; i
< TCPT_NTIMERS
; i
++)
953 tp
->t_timer
[i
] = qemu_get_sbe16(f
);
954 tp
->t_rxtshift
= qemu_get_sbe16(f
);
955 tp
->t_rxtcur
= qemu_get_sbe16(f
);
956 tp
->t_dupacks
= qemu_get_sbe16(f
);
957 tp
->t_maxseg
= qemu_get_be16(f
);
958 tp
->t_force
= qemu_get_sbyte(f
);
959 tp
->t_flags
= qemu_get_be16(f
);
960 tp
->snd_una
= qemu_get_be32(f
);
961 tp
->snd_nxt
= qemu_get_be32(f
);
962 tp
->snd_up
= qemu_get_be32(f
);
963 tp
->snd_wl1
= qemu_get_be32(f
);
964 tp
->snd_wl2
= qemu_get_be32(f
);
965 tp
->iss
= qemu_get_be32(f
);
966 tp
->snd_wnd
= qemu_get_be32(f
);
967 tp
->rcv_wnd
= qemu_get_be32(f
);
968 tp
->rcv_nxt
= qemu_get_be32(f
);
969 tp
->rcv_up
= qemu_get_be32(f
);
970 tp
->irs
= qemu_get_be32(f
);
971 tp
->rcv_adv
= qemu_get_be32(f
);
972 tp
->snd_max
= qemu_get_be32(f
);
973 tp
->snd_cwnd
= qemu_get_be32(f
);
974 tp
->snd_ssthresh
= qemu_get_be32(f
);
975 tp
->t_idle
= qemu_get_sbe16(f
);
976 tp
->t_rtt
= qemu_get_sbe16(f
);
977 tp
->t_rtseq
= qemu_get_be32(f
);
978 tp
->t_srtt
= qemu_get_sbe16(f
);
979 tp
->t_rttvar
= qemu_get_sbe16(f
);
980 tp
->t_rttmin
= qemu_get_be16(f
);
981 tp
->max_sndwnd
= qemu_get_be32(f
);
982 tp
->t_oobflags
= qemu_get_byte(f
);
983 tp
->t_iobc
= qemu_get_byte(f
);
984 tp
->t_softerror
= qemu_get_sbe16(f
);
985 tp
->snd_scale
= qemu_get_byte(f
);
986 tp
->rcv_scale
= qemu_get_byte(f
);
987 tp
->request_r_scale
= qemu_get_byte(f
);
988 tp
->requested_s_scale
= qemu_get_byte(f
);
989 tp
->ts_recent
= qemu_get_be32(f
);
990 tp
->ts_recent_age
= qemu_get_be32(f
);
991 tp
->last_ack_sent
= qemu_get_be32(f
);
995 static int slirp_sbuf_load(QEMUFile
*f
, struct sbuf
*sbuf
)
997 uint32_t off
, sb_cc
, sb_datalen
;
999 sb_cc
= qemu_get_be32(f
);
1000 sb_datalen
= qemu_get_be32(f
);
1002 sbreserve(sbuf
, sb_datalen
);
1004 if (sbuf
->sb_datalen
!= sb_datalen
)
1007 sbuf
->sb_cc
= sb_cc
;
1009 off
= qemu_get_sbe32(f
);
1010 sbuf
->sb_wptr
= sbuf
->sb_data
+ off
;
1011 off
= qemu_get_sbe32(f
);
1012 sbuf
->sb_rptr
= sbuf
->sb_data
+ off
;
1013 qemu_get_buffer(f
, (unsigned char*)sbuf
->sb_data
, sbuf
->sb_datalen
);
1018 static int slirp_socket_load(QEMUFile
*f
, struct socket
*so
)
1020 if (tcp_attach(so
) < 0)
1023 so
->so_urgc
= qemu_get_be32(f
);
1024 so
->so_faddr
.s_addr
= qemu_get_be32(f
);
1025 so
->so_laddr
.s_addr
= qemu_get_be32(f
);
1026 so
->so_fport
= qemu_get_be16(f
);
1027 so
->so_lport
= qemu_get_be16(f
);
1028 so
->so_iptos
= qemu_get_byte(f
);
1029 so
->so_emu
= qemu_get_byte(f
);
1030 so
->so_type
= qemu_get_byte(f
);
1031 so
->so_state
= qemu_get_be32(f
);
1032 if (slirp_sbuf_load(f
, &so
->so_rcv
) < 0)
1034 if (slirp_sbuf_load(f
, &so
->so_snd
) < 0)
1036 slirp_tcp_load(f
, so
->so_tcpcb
);
1041 static int slirp_state_load(QEMUFile
*f
, void *opaque
, int version_id
)
1043 struct ex_list
*ex_ptr
;
1046 while ((r
= qemu_get_byte(f
))) {
1048 struct socket
*so
= socreate();
1053 ret
= slirp_socket_load(f
, so
);
1058 if ((so
->so_faddr
.s_addr
& htonl(0xffffff00)) != special_addr
.s_addr
)
1061 for (ex_ptr
= exec_list
; ex_ptr
; ex_ptr
= ex_ptr
->ex_next
)
1062 if (ex_ptr
->ex_pty
== 3 &&
1063 (ntohl(so
->so_faddr
.s_addr
) & 0xff) == ex_ptr
->ex_addr
&&
1064 so
->so_fport
== ex_ptr
->ex_fport
)
1070 so
->extra
= (void *)ex_ptr
->ex_exec
;