mirror of
https://github.com/Motorhead1991/qemu.git
synced 2025-08-08 10:13:56 -06:00
slirp: Factorizing address translation
This patch factorizes some duplicate code into a new function, sotranslate_out(). This function perform the address translation when a packet is transmitted to the host network. If the packet is destinated to the host, the loopback address is used, and if the packet is destinated to the virtual DNS, the real DNS address is used. This code is just a copy of the existent, but factorized and ready to manage the IPv6 case. On the same model, the major part of udp_output() code is moved into a new sotranslate_in(). This function is directly used in sorecvfrom(), like sotranslate_out() in sosendto(). udp_output() becoming useless, it is removed and udp_output2() is renamed into udp_output(). This adds consistency with the udp6_output() function introduced by further patches. Lastly, this factorizes some duplicate code into sotranslate_accept(), which performs the address translation when a connection is established on the host for port forwarding: if it comes from localhost, the host virtual address is used instead. This prepares for IPv6 support. Signed-off-by: Guillaume Subiron <maethor@subiron.org> Signed-off-by: Samuel Thibault <samuel.thibault@ens-lyon.org> Reviewed-by: Thomas Huth <thuth@redhat.com> Signed-off-by: Jason Wang <jasowang@redhat.com>
This commit is contained in:
parent
eae303ff23
commit
5379229a27
8 changed files with 115 additions and 102 deletions
37
slirp/udp.c
37
slirp/udp.c
|
@ -236,7 +236,7 @@ bad:
|
|||
m_free(m);
|
||||
}
|
||||
|
||||
int udp_output2(struct socket *so, struct mbuf *m,
|
||||
int udp_output(struct socket *so, struct mbuf *m,
|
||||
struct sockaddr_in *saddr, struct sockaddr_in *daddr,
|
||||
int iptos)
|
||||
{
|
||||
|
@ -287,31 +287,6 @@ int udp_output2(struct socket *so, struct mbuf *m,
|
|||
return (error);
|
||||
}
|
||||
|
||||
int udp_output(struct socket *so, struct mbuf *m,
|
||||
struct sockaddr_in *addr)
|
||||
|
||||
{
|
||||
Slirp *slirp = so->slirp;
|
||||
struct sockaddr_in saddr, daddr;
|
||||
|
||||
saddr = *addr;
|
||||
if ((so->so_faddr.s_addr & slirp->vnetwork_mask.s_addr) ==
|
||||
slirp->vnetwork_addr.s_addr) {
|
||||
uint32_t inv_mask = ~slirp->vnetwork_mask.s_addr;
|
||||
|
||||
if ((so->so_faddr.s_addr & inv_mask) == inv_mask) {
|
||||
saddr.sin_addr = slirp->vhost_addr;
|
||||
} else if (addr->sin_addr.s_addr == loopback_addr.s_addr ||
|
||||
so->so_faddr.s_addr != slirp->vhost_addr.s_addr) {
|
||||
saddr.sin_addr = so->so_faddr;
|
||||
}
|
||||
}
|
||||
daddr.sin_addr = so->so_laddr;
|
||||
daddr.sin_port = so->so_lport;
|
||||
|
||||
return udp_output2(so, m, &saddr, &daddr, so->so_iptos);
|
||||
}
|
||||
|
||||
int
|
||||
udp_attach(struct socket *so)
|
||||
{
|
||||
|
@ -378,14 +353,8 @@ udp_listen(Slirp *slirp, uint32_t haddr, u_int hport, uint32_t laddr,
|
|||
socket_set_fast_reuse(so->s);
|
||||
|
||||
getsockname(so->s,(struct sockaddr *)&addr,&addrlen);
|
||||
so->so_ffamily = AF_INET;
|
||||
so->so_fport = addr.sin_port;
|
||||
if (addr.sin_addr.s_addr == 0 ||
|
||||
addr.sin_addr.s_addr == loopback_addr.s_addr) {
|
||||
so->so_faddr = slirp->vhost_addr;
|
||||
} else {
|
||||
so->so_faddr = addr.sin_addr;
|
||||
}
|
||||
so->fhost.sin = addr;
|
||||
sotranslate_accept(so);
|
||||
so->so_lfamily = AF_INET;
|
||||
so->so_lport = lport;
|
||||
so->so_laddr.s_addr = laddr;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue