2004-04-22 08:10:48 +08:00
|
|
|
/*
|
|
|
|
* Copyright (c) 1982, 1986, 1988, 1990, 1993
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
Remove the advertising clause from the slirp license
According to the FSF, the 4-clause BSD license, which slirp is covered under,
is not compatible with the GPL or LGPL[1].
[1] http://www.fsf.org/licensing/licenses/index_html#GPLIncompatibleLicenses
There are three declared copyright holders in slirp that use the 4-clause
BSD license, the Regents of UC Berkley, Danny Gasparovski, and Kelly Price.
Below are the appropriate permissions to remove the advertise clause from slirp
from each party.
Special thanks go to Richard Fontana from Red Hat for contacting all of the
necessary authors to resolve this issue!
Regents of UC Berkley:
From ftp://ftp.cs.berkeley.edu/pub/4bsd/README.Impt.License.Change
July 22, 1999
To All Licensees, Distributors of Any Version of BSD:
As you know, certain of the Berkeley Software Distribution ("BSD") source
code files require that further distributions of products containing all or
portions of the software, acknowledge within their advertising materials
that such products contain software developed by UC Berkeley and its
contributors.
Specifically, the provision reads:
" * 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors."
Effective immediately, licensees and distributors are no longer required to
include the acknowledgement within advertising materials. Accordingly, the
foregoing paragraph of those BSD Unix files containing it is hereby deleted
in its entirety.
William Hoskins
Director, Office of Technology Licensing
University of California, Berkeley
Danny Gasparovski:
Subject: RE: Slirp license
Date: Thu, 8 Jan 2009 10:51:00 +1100
From: "Gasparovski, Daniel" <Daniel.Gasparovski@ato.gov.au>
To: "Richard Fontana" <rfontana@redhat.com>
Hi Richard,
I have no objection to having Slirp code in QEMU be licensed under the
3-clause BSD license.
Thanks for taking the effort to consult me about this.
Dan ...
Kelly Price:
Date: Thu, 8 Jan 2009 19:38:56 -0500
From: "Kelly Price" <strredwolf@gmail.com>
To: "Richard Fontana" <rfontana@redhat.com>
Subject: Re: Slirp license
Thanks for contacting me, Richard. I'm glad you were able to find
Dan, as I've been "keeping the light on" for Slirp. I have no use for
it now, and I have little time for it (now holding onto Keenspot's
Comic Genesis and having a regular US state government position). If
Dan would like to return to the project, I'd love to give it back to
him.
As for copyright, I don't own all of it. Dan does, so I will defer to
him. Any of my patches I will gladly license to the 3-part BSD
license. My interest in re-licensing was because we didn't have ready
info to contact Dan. If Dan would like to port Slirp back out of
QEMU, a lot of us 64-bit users would be grateful.
Feel free to share this email address with Dan. I will be glad to
effect a transfer of the project to him and Mr. Bellard of the QEMU
project.
Signed-off-by: Anthony Liguori <aliguori@us.ibm.com>
git-svn-id: svn://svn.savannah.nongnu.org/qemu/trunk@6451 c046a42c-6fe2-441c-8c8c-71466251a162
2009-01-27 03:37:41 +08:00
|
|
|
* 3. Neither the name of the University nor the names of its contributors
|
2004-04-22 08:10:48 +08:00
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
* @(#)udp_usrreq.c 8.4 (Berkeley) 1/21/94
|
|
|
|
* udp_usrreq.c,v 1.4 1994/10/02 17:48:45 phk Exp
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Changes and additions relating to SLiRP
|
|
|
|
* Copyright (c) 1995 Danny Gasparovski.
|
2007-09-17 05:08:06 +08:00
|
|
|
*
|
|
|
|
* Please read the file COPYRIGHT for the
|
2004-04-22 08:10:48 +08:00
|
|
|
* terms and conditions of the copyright.
|
|
|
|
*/
|
|
|
|
|
2016-06-23 01:11:19 +08:00
|
|
|
#include "slirp.h"
|
2004-04-22 08:10:48 +08:00
|
|
|
#include "ip_icmp.h"
|
|
|
|
|
2010-07-23 04:15:23 +08:00
|
|
|
static uint8_t udp_tos(struct socket *so);
|
2007-10-27 03:01:16 +08:00
|
|
|
|
2004-04-22 08:10:48 +08:00
|
|
|
void
|
2009-06-24 20:42:31 +08:00
|
|
|
udp_init(Slirp *slirp)
|
2004-04-22 08:10:48 +08:00
|
|
|
{
|
2009-06-24 20:42:31 +08:00
|
|
|
slirp->udb.so_next = slirp->udb.so_prev = &slirp->udb;
|
|
|
|
slirp->udp_last_so = &slirp->udb;
|
2004-04-22 08:10:48 +08:00
|
|
|
}
|
2012-03-01 02:14:23 +08:00
|
|
|
|
|
|
|
void udp_cleanup(Slirp *slirp)
|
|
|
|
{
|
|
|
|
while (slirp->udb.so_next != &slirp->udb) {
|
|
|
|
udp_detach(slirp->udb.so_next);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-09-17 05:08:06 +08:00
|
|
|
/* m->m_data points at ip packet header
|
|
|
|
* m->m_len length ip packet
|
2004-04-22 08:10:48 +08:00
|
|
|
* ip->ip_len length data (IPDU)
|
|
|
|
*/
|
|
|
|
void
|
2009-04-18 15:32:41 +08:00
|
|
|
udp_input(register struct mbuf *m, int iphlen)
|
2004-04-22 08:10:48 +08:00
|
|
|
{
|
2009-06-24 20:42:31 +08:00
|
|
|
Slirp *slirp = m->slirp;
|
2004-04-22 08:10:48 +08:00
|
|
|
register struct ip *ip;
|
|
|
|
register struct udphdr *uh;
|
|
|
|
int len;
|
2007-09-17 05:08:06 +08:00
|
|
|
struct ip save_ip;
|
2004-04-22 08:10:48 +08:00
|
|
|
struct socket *so;
|
2015-12-20 05:25:01 +08:00
|
|
|
struct sockaddr_storage lhost;
|
|
|
|
struct sockaddr_in *lhost4;
|
2007-09-17 05:08:06 +08:00
|
|
|
|
2004-04-22 08:10:48 +08:00
|
|
|
DEBUG_CALL("udp_input");
|
2015-08-29 15:12:35 +08:00
|
|
|
DEBUG_ARG("m = %p", m);
|
2004-04-22 08:10:48 +08:00
|
|
|
DEBUG_ARG("iphlen = %d", iphlen);
|
2007-09-17 05:08:06 +08:00
|
|
|
|
2004-04-22 08:10:48 +08:00
|
|
|
/*
|
|
|
|
* Strip IP options, if any; should skip this,
|
|
|
|
* make available to user, and use on returned packets,
|
|
|
|
* but we don't yet have a way to check the checksum
|
|
|
|
* with options still present.
|
|
|
|
*/
|
|
|
|
if(iphlen > sizeof(struct ip)) {
|
|
|
|
ip_stripoptions(m, (struct mbuf *)0);
|
|
|
|
iphlen = sizeof(struct ip);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Get IP and UDP header together in first mbuf.
|
|
|
|
*/
|
|
|
|
ip = mtod(m, struct ip *);
|
2019-01-17 19:43:53 +08:00
|
|
|
uh = (struct udphdr *)((char *)ip + iphlen);
|
2004-04-22 08:10:48 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Make mbuf data length reflect UDP length.
|
|
|
|
* If not enough data to reflect UDP length, drop.
|
|
|
|
*/
|
2010-07-23 04:15:23 +08:00
|
|
|
len = ntohs((uint16_t)uh->uh_ulen);
|
2004-04-22 08:10:48 +08:00
|
|
|
|
|
|
|
if (ip->ip_len != len) {
|
|
|
|
if (len > ip->ip_len) {
|
|
|
|
goto bad;
|
|
|
|
}
|
|
|
|
m_adj(m, len - ip->ip_len);
|
|
|
|
ip->ip_len = len;
|
|
|
|
}
|
2007-09-17 05:08:06 +08:00
|
|
|
|
2004-04-22 08:10:48 +08:00
|
|
|
/*
|
|
|
|
* Save a copy of the IP header in case we want restore it
|
|
|
|
* for sending an ICMP error message in response.
|
|
|
|
*/
|
2007-09-17 05:08:06 +08:00
|
|
|
save_ip = *ip;
|
2004-04-22 08:10:48 +08:00
|
|
|
save_ip.ip_len+= iphlen; /* tcp_input subtracts this */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Checksum extended UDP header and data.
|
|
|
|
*/
|
2009-06-24 20:42:29 +08:00
|
|
|
if (uh->uh_sum) {
|
2009-01-14 03:48:42 +08:00
|
|
|
memset(&((struct ipovly *)ip)->ih_mbuf, 0, sizeof(struct mbuf_ptr));
|
2004-04-22 08:10:48 +08:00
|
|
|
((struct ipovly *)ip)->ih_x1 = 0;
|
|
|
|
((struct ipovly *)ip)->ih_len = uh->uh_ulen;
|
|
|
|
if(cksum(m, len + sizeof(struct ip))) {
|
|
|
|
goto bad;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-15 17:31:23 +08:00
|
|
|
lhost.ss_family = AF_INET;
|
|
|
|
lhost4 = (struct sockaddr_in *) &lhost;
|
|
|
|
lhost4->sin_addr = ip->ip_src;
|
|
|
|
lhost4->sin_port = uh->uh_sport;
|
|
|
|
|
2004-04-22 08:10:48 +08:00
|
|
|
/*
|
|
|
|
* handle DHCP/BOOTP
|
|
|
|
*/
|
2011-07-20 18:20:15 +08:00
|
|
|
if (ntohs(uh->uh_dport) == BOOTP_SERVER &&
|
|
|
|
(ip->ip_dst.s_addr == slirp->vhost_addr.s_addr ||
|
|
|
|
ip->ip_dst.s_addr == 0xffffffff)) {
|
|
|
|
bootp_input(m);
|
|
|
|
goto bad;
|
|
|
|
}
|
2004-04-22 08:10:48 +08:00
|
|
|
|
2004-08-25 05:57:12 +08:00
|
|
|
/*
|
|
|
|
* handle TFTP
|
|
|
|
*/
|
2011-07-20 18:20:15 +08:00
|
|
|
if (ntohs(uh->uh_dport) == TFTP_SERVER &&
|
|
|
|
ip->ip_dst.s_addr == slirp->vhost_addr.s_addr) {
|
2016-03-15 17:31:23 +08:00
|
|
|
m->m_data += iphlen;
|
|
|
|
m->m_len -= iphlen;
|
|
|
|
tftp_input(&lhost, m);
|
|
|
|
m->m_data -= iphlen;
|
|
|
|
m->m_len += iphlen;
|
2004-08-25 05:57:12 +08:00
|
|
|
goto bad;
|
|
|
|
}
|
|
|
|
|
2011-07-20 18:20:13 +08:00
|
|
|
if (slirp->restricted) {
|
|
|
|
goto bad;
|
|
|
|
}
|
|
|
|
|
2004-04-22 08:10:48 +08:00
|
|
|
/*
|
|
|
|
* Locate pcb for datagram.
|
|
|
|
*/
|
2015-12-20 05:25:01 +08:00
|
|
|
so = solookup(&slirp->udp_last_so, &slirp->udb, &lhost, NULL);
|
2007-09-17 05:08:06 +08:00
|
|
|
|
2004-04-22 08:10:48 +08:00
|
|
|
if (so == NULL) {
|
|
|
|
/*
|
|
|
|
* If there's no socket for this packet,
|
|
|
|
* create one
|
|
|
|
*/
|
2009-06-24 20:42:31 +08:00
|
|
|
so = socreate(slirp);
|
2015-12-20 05:25:02 +08:00
|
|
|
if (udp_attach(so, AF_INET) == -1) {
|
2018-11-22 06:06:41 +08:00
|
|
|
DEBUG_MISC(" udp_attach errno = %d-%s", errno, strerror(errno));
|
2004-04-22 08:10:48 +08:00
|
|
|
sofree(so);
|
|
|
|
goto bad;
|
|
|
|
}
|
2007-09-17 16:09:54 +08:00
|
|
|
|
2004-04-22 08:10:48 +08:00
|
|
|
/*
|
|
|
|
* Setup fields
|
|
|
|
*/
|
2015-12-20 05:24:58 +08:00
|
|
|
so->so_lfamily = AF_INET;
|
2004-04-22 08:10:48 +08:00
|
|
|
so->so_laddr = ip->ip_src;
|
|
|
|
so->so_lport = uh->uh_sport;
|
2007-09-17 16:09:54 +08:00
|
|
|
|
2004-04-22 08:10:48 +08:00
|
|
|
if ((so->so_iptos = udp_tos(so)) == 0)
|
|
|
|
so->so_iptos = ip->ip_tos;
|
2007-09-17 16:09:54 +08:00
|
|
|
|
2004-04-22 08:10:48 +08:00
|
|
|
/*
|
|
|
|
* XXXXX Here, check if it's in udpexec_list,
|
|
|
|
* and if it is, do the fork_exec() etc.
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
|
2015-12-20 05:24:58 +08:00
|
|
|
so->so_ffamily = AF_INET;
|
2007-01-18 06:47:40 +08:00
|
|
|
so->so_faddr = ip->ip_dst; /* XXX */
|
|
|
|
so->so_fport = uh->uh_dport; /* XXX */
|
|
|
|
|
2004-04-22 08:10:48 +08:00
|
|
|
iphlen += sizeof(struct udphdr);
|
|
|
|
m->m_len -= iphlen;
|
|
|
|
m->m_data += iphlen;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Now we sendto() the packet.
|
|
|
|
*/
|
|
|
|
if(sosendto(so,m) == -1) {
|
|
|
|
m->m_len += iphlen;
|
|
|
|
m->m_data -= iphlen;
|
|
|
|
*ip=save_ip;
|
2018-11-22 06:06:41 +08:00
|
|
|
DEBUG_MISC("udp tx errno = %d-%s", errno, strerror(errno));
|
2016-03-15 17:31:19 +08:00
|
|
|
icmp_send_error(m, ICMP_UNREACH, ICMP_UNREACH_NET, 0,
|
|
|
|
strerror(errno));
|
2015-12-20 05:24:55 +08:00
|
|
|
goto bad;
|
2004-04-22 08:10:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
m_free(so->so_m); /* used for ICMP if error on sorecvfrom */
|
|
|
|
|
|
|
|
/* restore the orig mbuf packet */
|
|
|
|
m->m_len += iphlen;
|
|
|
|
m->m_data -= iphlen;
|
|
|
|
*ip=save_ip;
|
|
|
|
so->so_m=m; /* ICMP backup */
|
|
|
|
|
|
|
|
return;
|
|
|
|
bad:
|
2011-07-20 18:20:16 +08:00
|
|
|
m_free(m);
|
2004-04-22 08:10:48 +08: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>
2015-12-20 05:24:59 +08:00
|
|
|
int udp_output(struct socket *so, struct mbuf *m,
|
2004-04-22 08:10:48 +08:00
|
|
|
struct sockaddr_in *saddr, struct sockaddr_in *daddr,
|
|
|
|
int iptos)
|
|
|
|
{
|
|
|
|
register struct udpiphdr *ui;
|
|
|
|
int error = 0;
|
|
|
|
|
|
|
|
DEBUG_CALL("udp_output");
|
2015-08-29 15:12:35 +08:00
|
|
|
DEBUG_ARG("so = %p", so);
|
|
|
|
DEBUG_ARG("m = %p", m);
|
2018-02-01 17:35:45 +08:00
|
|
|
DEBUG_ARG("saddr = %s", inet_ntoa(saddr->sin_addr));
|
|
|
|
DEBUG_ARG("daddr = %s", inet_ntoa(daddr->sin_addr));
|
2004-04-22 08:10:48 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Adjust for header
|
|
|
|
*/
|
|
|
|
m->m_data -= sizeof(struct udpiphdr);
|
|
|
|
m->m_len += sizeof(struct udpiphdr);
|
2007-09-17 05:08:06 +08:00
|
|
|
|
2004-04-22 08:10:48 +08:00
|
|
|
/*
|
|
|
|
* Fill in mbuf with extended UDP header
|
|
|
|
* and addresses and length put into network format.
|
|
|
|
*/
|
|
|
|
ui = mtod(m, struct udpiphdr *);
|
2009-01-14 03:48:42 +08:00
|
|
|
memset(&ui->ui_i.ih_mbuf, 0 , sizeof(struct mbuf_ptr));
|
2004-04-22 08:10:48 +08:00
|
|
|
ui->ui_x1 = 0;
|
|
|
|
ui->ui_pr = IPPROTO_UDP;
|
2009-06-24 20:42:29 +08:00
|
|
|
ui->ui_len = htons(m->m_len - sizeof(struct ip));
|
2004-04-22 08:10:48 +08:00
|
|
|
/* XXXXX Check for from-one-location sockets, or from-any-location sockets */
|
|
|
|
ui->ui_src = saddr->sin_addr;
|
|
|
|
ui->ui_dst = daddr->sin_addr;
|
|
|
|
ui->ui_sport = saddr->sin_port;
|
|
|
|
ui->ui_dport = daddr->sin_port;
|
|
|
|
ui->ui_ulen = ui->ui_len;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Stuff checksum and output datagram.
|
|
|
|
*/
|
|
|
|
ui->ui_sum = 0;
|
2009-06-24 20:42:29 +08:00
|
|
|
if ((ui->ui_sum = cksum(m, m->m_len)) == 0)
|
2004-04-22 08:10:48 +08:00
|
|
|
ui->ui_sum = 0xffff;
|
|
|
|
((struct ip *)ui)->ip_len = m->m_len;
|
|
|
|
|
2007-10-27 03:01:16 +08:00
|
|
|
((struct ip *)ui)->ip_ttl = IPDEFTTL;
|
2004-04-22 08:10:48 +08:00
|
|
|
((struct ip *)ui)->ip_tos = iptos;
|
2007-09-17 05:08:06 +08:00
|
|
|
|
2004-04-22 08:10:48 +08:00
|
|
|
error = ip_output(so, m);
|
2007-09-17 05:08:06 +08:00
|
|
|
|
2004-04-22 08:10:48 +08:00
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2015-12-20 05:25:02 +08:00
|
|
|
udp_attach(struct socket *so, unsigned short af)
|
2004-04-22 08:10:48 +08:00
|
|
|
{
|
2019-01-17 19:43:40 +08:00
|
|
|
so->s = slirp_socket(af, SOCK_DGRAM, 0);
|
2015-12-20 05:25:02 +08:00
|
|
|
if (so->s != -1) {
|
2009-07-23 08:53:15 +08:00
|
|
|
so->so_expire = curtime + SO_EXPIRE;
|
|
|
|
insque(so, &so->slirp->udb);
|
2004-04-22 08:10:48 +08:00
|
|
|
}
|
|
|
|
return(so->s);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2009-04-18 15:32:41 +08:00
|
|
|
udp_detach(struct socket *so)
|
2004-04-22 08:10:48 +08:00
|
|
|
{
|
2019-01-17 19:43:58 +08:00
|
|
|
so->slirp->cb->unregister_poll_fd(so->s, so->slirp->opaque);
|
2019-01-17 19:43:40 +08:00
|
|
|
slirp_closesocket(so->s);
|
2004-04-22 08:10:48 +08:00
|
|
|
sofree(so);
|
|
|
|
}
|
|
|
|
|
2007-10-27 03:01:16 +08:00
|
|
|
static const struct tos_t udptos[] = {
|
2004-04-22 08:10:48 +08:00
|
|
|
{0, 53, IPTOS_LOWDELAY, 0}, /* DNS */
|
|
|
|
{0, 0, 0, 0}
|
|
|
|
};
|
|
|
|
|
2010-07-23 04:15:23 +08:00
|
|
|
static uint8_t
|
2007-10-27 03:01:16 +08:00
|
|
|
udp_tos(struct socket *so)
|
2004-04-22 08:10:48 +08:00
|
|
|
{
|
|
|
|
int i = 0;
|
2007-09-17 05:08:06 +08:00
|
|
|
|
2004-04-22 08:10:48 +08:00
|
|
|
while(udptos[i].tos) {
|
|
|
|
if ((udptos[i].fport && ntohs(so->so_fport) == udptos[i].fport) ||
|
|
|
|
(udptos[i].lport && ntohs(so->so_lport) == udptos[i].lport)) {
|
2018-12-14 06:37:36 +08:00
|
|
|
so->so_emu = udptos[i].emu;
|
2004-04-22 08:10:48 +08:00
|
|
|
return udptos[i].tos;
|
|
|
|
}
|
|
|
|
i++;
|
|
|
|
}
|
2007-09-17 05:08:06 +08:00
|
|
|
|
2004-04-22 08:10:48 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct socket *
|
2019-01-17 19:43:53 +08:00
|
|
|
udp_listen(Slirp *slirp, uint32_t haddr, unsigned hport, uint32_t laddr,
|
|
|
|
unsigned lport, int flags)
|
2004-04-22 08:10:48 +08:00
|
|
|
{
|
|
|
|
struct sockaddr_in addr;
|
|
|
|
struct socket *so;
|
2013-10-02 18:23:15 +08:00
|
|
|
socklen_t addrlen = sizeof(struct sockaddr_in);
|
2007-09-17 05:08:06 +08:00
|
|
|
|
2009-06-24 20:42:31 +08:00
|
|
|
so = socreate(slirp);
|
2019-01-17 19:43:40 +08:00
|
|
|
so->s = slirp_socket(AF_INET,SOCK_DGRAM,0);
|
2017-02-05 07:08:33 +08:00
|
|
|
if (so->s < 0) {
|
|
|
|
sofree(so);
|
|
|
|
return NULL;
|
|
|
|
}
|
2004-04-22 08:10:48 +08:00
|
|
|
so->so_expire = curtime + SO_EXPIRE;
|
2009-06-24 20:42:31 +08:00
|
|
|
insque(so, &slirp->udb);
|
2004-04-22 08:10:48 +08:00
|
|
|
|
|
|
|
addr.sin_family = AF_INET;
|
2009-06-24 20:42:28 +08:00
|
|
|
addr.sin_addr.s_addr = haddr;
|
|
|
|
addr.sin_port = hport;
|
2004-04-22 08:10:48 +08:00
|
|
|
|
|
|
|
if (bind(so->s,(struct sockaddr *)&addr, addrlen) < 0) {
|
|
|
|
udp_detach(so);
|
|
|
|
return NULL;
|
|
|
|
}
|
2019-01-17 19:43:40 +08:00
|
|
|
slirp_socket_set_fast_reuse(so->s);
|
2007-09-17 05:08:06 +08:00
|
|
|
|
2004-04-22 08:10:48 +08:00
|
|
|
getsockname(so->s,(struct sockaddr *)&addr,&addrlen);
|
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>
2015-12-20 05:24:59 +08:00
|
|
|
so->fhost.sin = addr;
|
|
|
|
sotranslate_accept(so);
|
2015-12-20 05:24:58 +08:00
|
|
|
so->so_lfamily = AF_INET;
|
2004-04-22 08:10:48 +08:00
|
|
|
so->so_lport = lport;
|
|
|
|
so->so_laddr.s_addr = laddr;
|
|
|
|
if (flags != SS_FACCEPTONCE)
|
|
|
|
so->so_expire = 0;
|
2007-09-17 05:08:06 +08:00
|
|
|
|
2009-06-24 20:42:29 +08:00
|
|
|
so->so_state &= SS_PERSISTENT_MASK;
|
2009-06-24 20:42:29 +08:00
|
|
|
so->so_state |= SS_ISFCONNECTED | flags;
|
2007-09-17 05:08:06 +08:00
|
|
|
|
2004-04-22 08:10:48 +08:00
|
|
|
return so;
|
|
|
|
}
|