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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <slirp.h>
|
|
|
|
#include "ip_icmp.h"
|
|
|
|
|
|
|
|
struct socket udb;
|
|
|
|
|
2007-10-27 03:01:16 +08:00
|
|
|
static u_int8_t udp_tos(struct socket *so);
|
|
|
|
static void udp_emu(struct socket *so, struct mbuf *m);
|
|
|
|
|
2004-04-22 08:10:48 +08:00
|
|
|
struct socket *udp_last_so = &udb;
|
|
|
|
|
|
|
|
void
|
2009-04-18 15:32:41 +08:00
|
|
|
udp_init(void)
|
2004-04-22 08:10:48 +08:00
|
|
|
{
|
|
|
|
udb.so_next = udb.so_prev = &udb;
|
|
|
|
}
|
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
|
|
|
{
|
|
|
|
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;
|
2007-09-17 05:08:06 +08:00
|
|
|
|
2004-04-22 08:10:48 +08:00
|
|
|
DEBUG_CALL("udp_input");
|
|
|
|
DEBUG_ARG("m = %lx", (long)m);
|
|
|
|
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 *);
|
|
|
|
uh = (struct udphdr *)((caddr_t)ip + iphlen);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Make mbuf data length reflect UDP length.
|
|
|
|
* If not enough data to reflect UDP length, drop.
|
|
|
|
*/
|
|
|
|
len = ntohs((u_int16_t)uh->uh_ulen);
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* handle DHCP/BOOTP
|
|
|
|
*/
|
|
|
|
if (ntohs(uh->uh_dport) == BOOTP_SERVER) {
|
|
|
|
bootp_input(m);
|
|
|
|
goto bad;
|
|
|
|
}
|
|
|
|
|
2009-01-09 03:24:00 +08:00
|
|
|
if (slirp_restrict)
|
|
|
|
goto bad;
|
|
|
|
|
2004-08-25 05:57:12 +08:00
|
|
|
/*
|
|
|
|
* handle TFTP
|
|
|
|
*/
|
|
|
|
if (ntohs(uh->uh_dport) == TFTP_SERVER) {
|
|
|
|
tftp_input(m);
|
|
|
|
goto bad;
|
|
|
|
}
|
|
|
|
|
2004-04-22 08:10:48 +08:00
|
|
|
/*
|
|
|
|
* Locate pcb for datagram.
|
|
|
|
*/
|
|
|
|
so = udp_last_so;
|
|
|
|
if (so->so_lport != uh->uh_sport ||
|
|
|
|
so->so_laddr.s_addr != ip->ip_src.s_addr) {
|
|
|
|
struct socket *tmp;
|
2007-09-17 16:09:54 +08:00
|
|
|
|
2004-04-22 08:10:48 +08:00
|
|
|
for (tmp = udb.so_next; tmp != &udb; tmp = tmp->so_next) {
|
|
|
|
if (tmp->so_lport == uh->uh_sport &&
|
|
|
|
tmp->so_laddr.s_addr == ip->ip_src.s_addr) {
|
|
|
|
so = tmp;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (tmp == &udb) {
|
|
|
|
so = NULL;
|
|
|
|
} else {
|
|
|
|
udp_last_so = so;
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
|
*/
|
|
|
|
if ((so = socreate()) == NULL) goto bad;
|
|
|
|
if(udp_attach(so) == -1) {
|
2007-09-17 05:08:06 +08:00
|
|
|
DEBUG_MISC((dfd," udp_attach errno = %d-%s\n",
|
2004-04-22 08:10:48 +08:00
|
|
|
errno,strerror(errno)));
|
|
|
|
sofree(so);
|
|
|
|
goto bad;
|
|
|
|
}
|
2007-09-17 16:09:54 +08:00
|
|
|
|
2004-04-22 08:10:48 +08:00
|
|
|
/*
|
|
|
|
* Setup fields
|
|
|
|
*/
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
|
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 (so->so_emu)
|
|
|
|
udp_emu(so, m);
|
|
|
|
|
|
|
|
if(sosendto(so,m) == -1) {
|
|
|
|
m->m_len += iphlen;
|
|
|
|
m->m_data -= iphlen;
|
|
|
|
*ip=save_ip;
|
|
|
|
DEBUG_MISC((dfd,"udp tx errno = %d-%s\n",errno,strerror(errno)));
|
2007-09-17 16:09:54 +08:00
|
|
|
icmp_error(m, ICMP_UNREACH,ICMP_UNREACH_NET, 0,strerror(errno));
|
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:
|
|
|
|
m_freem(m);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2007-09-17 05:08:06 +08:00
|
|
|
int udp_output2(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");
|
|
|
|
DEBUG_ARG("so = %lx", (long)so);
|
|
|
|
DEBUG_ARG("m = %lx", (long)m);
|
|
|
|
DEBUG_ARG("saddr = %lx", (long)saddr->sin_addr.s_addr);
|
|
|
|
DEBUG_ARG("daddr = %lx", (long)daddr->sin_addr.s_addr);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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);
|
|
|
|
}
|
|
|
|
|
2007-09-17 05:08:06 +08:00
|
|
|
int udp_output(struct socket *so, struct mbuf *m,
|
2004-04-22 08:10:48 +08:00
|
|
|
struct sockaddr_in *addr)
|
|
|
|
|
|
|
|
{
|
|
|
|
struct sockaddr_in saddr, daddr;
|
|
|
|
|
|
|
|
saddr = *addr;
|
2009-06-24 20:42:28 +08:00
|
|
|
if ((so->so_faddr.s_addr & vnetwork_mask.s_addr) == vnetwork_addr.s_addr) {
|
|
|
|
if ((so->so_faddr.s_addr & ~vnetwork_mask.s_addr) ==
|
|
|
|
~vnetwork_mask.s_addr) {
|
|
|
|
saddr.sin_addr = vhost_addr;
|
|
|
|
} else if (addr->sin_addr.s_addr == loopback_addr.s_addr ||
|
|
|
|
so->so_faddr.s_addr != vhost_addr.s_addr) {
|
|
|
|
saddr.sin_addr = so->so_faddr;
|
|
|
|
}
|
2006-05-04 04:53:46 +08:00
|
|
|
}
|
2004-04-22 08:10:48 +08:00
|
|
|
daddr.sin_addr = so->so_laddr;
|
|
|
|
daddr.sin_port = so->so_lport;
|
2007-09-17 16:09:54 +08:00
|
|
|
|
2004-04-22 08:10:48 +08:00
|
|
|
return udp_output2(so, m, &saddr, &daddr, so->so_iptos);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2009-04-18 15:32:41 +08:00
|
|
|
udp_attach(struct socket *so)
|
2004-04-22 08:10:48 +08:00
|
|
|
{
|
|
|
|
struct sockaddr_in addr;
|
2007-09-17 05:08:06 +08:00
|
|
|
|
2004-04-22 08:10:48 +08:00
|
|
|
if((so->s = socket(AF_INET,SOCK_DGRAM,0)) != -1) {
|
|
|
|
/*
|
|
|
|
* Here, we bind() the socket. Although not really needed
|
|
|
|
* (sendto() on an unbound socket will bind it), it's done
|
|
|
|
* here so that emulation of ytalk etc. don't have to do it
|
|
|
|
*/
|
|
|
|
addr.sin_family = AF_INET;
|
|
|
|
addr.sin_port = 0;
|
|
|
|
addr.sin_addr.s_addr = INADDR_ANY;
|
|
|
|
if(bind(so->s, (struct sockaddr *)&addr, sizeof(addr))<0) {
|
|
|
|
int lasterrno=errno;
|
2004-07-13 06:33:07 +08:00
|
|
|
closesocket(so->s);
|
2004-04-22 08:10:48 +08:00
|
|
|
so->s=-1;
|
2004-10-08 07:27:35 +08:00
|
|
|
#ifdef _WIN32
|
|
|
|
WSASetLastError(lasterrno);
|
|
|
|
#else
|
2004-04-22 08:10:48 +08:00
|
|
|
errno=lasterrno;
|
2004-10-08 07:27:35 +08:00
|
|
|
#endif
|
2004-04-22 08:10:48 +08:00
|
|
|
} else {
|
|
|
|
/* success, insert in queue */
|
|
|
|
so->so_expire = curtime + SO_EXPIRE;
|
|
|
|
insque(so,&udb);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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
|
|
|
{
|
2004-07-13 06:33:07 +08:00
|
|
|
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 */
|
|
|
|
{517, 517, IPTOS_LOWDELAY, EMU_TALK}, /* talk */
|
|
|
|
{518, 518, IPTOS_LOWDELAY, EMU_NTALK}, /* ntalk */
|
|
|
|
{0, 7648, IPTOS_LOWDELAY, EMU_CUSEEME}, /* Cu-Seeme */
|
|
|
|
{0, 0, 0, 0}
|
|
|
|
};
|
|
|
|
|
2007-10-27 03:01:16 +08:00
|
|
|
static u_int8_t
|
|
|
|
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)) {
|
|
|
|
so->so_emu = udptos[i].emu;
|
|
|
|
return udptos[i].tos;
|
|
|
|
}
|
|
|
|
i++;
|
|
|
|
}
|
2007-09-17 05:08:06 +08:00
|
|
|
|
2004-04-22 08:10:48 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef EMULATE_TALK
|
|
|
|
#include "talkd.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Here, talk/ytalk/ntalk requests must be emulated
|
|
|
|
*/
|
2007-10-27 03:01:16 +08:00
|
|
|
static void
|
|
|
|
udp_emu(struct socket *so, struct mbuf *m)
|
2004-04-22 08:10:48 +08:00
|
|
|
{
|
|
|
|
struct sockaddr_in addr;
|
2008-05-10 09:49:53 +08:00
|
|
|
socklen_t addrlen = sizeof(addr);
|
2004-04-22 08:10:48 +08:00
|
|
|
#ifdef EMULATE_TALK
|
|
|
|
CTL_MSG_OLD *omsg;
|
|
|
|
CTL_MSG *nmsg;
|
|
|
|
char buff[sizeof(CTL_MSG)];
|
|
|
|
u_char type;
|
2007-09-17 05:08:06 +08:00
|
|
|
|
2004-04-22 08:10:48 +08:00
|
|
|
struct talk_request {
|
|
|
|
struct talk_request *next;
|
|
|
|
struct socket *udp_so;
|
|
|
|
struct socket *tcp_so;
|
|
|
|
} *req;
|
2007-09-17 05:08:06 +08:00
|
|
|
|
|
|
|
static struct talk_request *req_tbl = 0;
|
|
|
|
|
2004-04-22 08:10:48 +08:00
|
|
|
#endif
|
2007-09-17 05:08:06 +08:00
|
|
|
|
2004-04-22 08:10:48 +08:00
|
|
|
struct cu_header {
|
2005-06-06 01:11:42 +08:00
|
|
|
uint16_t d_family; // destination family
|
|
|
|
uint16_t d_port; // destination port
|
|
|
|
uint32_t d_addr; // destination address
|
|
|
|
uint16_t s_family; // source family
|
|
|
|
uint16_t s_port; // source port
|
2005-07-04 01:08:43 +08:00
|
|
|
uint32_t so_addr; // source address
|
2005-06-06 01:11:42 +08:00
|
|
|
uint32_t seqn; // sequence number
|
|
|
|
uint16_t message; // message
|
|
|
|
uint16_t data_type; // data type
|
|
|
|
uint16_t pkt_len; // packet length
|
2004-04-22 08:10:48 +08:00
|
|
|
} *cu_head;
|
|
|
|
|
|
|
|
switch(so->so_emu) {
|
|
|
|
|
|
|
|
#ifdef EMULATE_TALK
|
|
|
|
case EMU_TALK:
|
|
|
|
case EMU_NTALK:
|
|
|
|
/*
|
|
|
|
* Talk emulation. We always change the ctl_addr to get
|
|
|
|
* some answers from the daemon. When an ANNOUNCE comes,
|
|
|
|
* we send LEAVE_INVITE to the local daemons. Also when a
|
|
|
|
* DELETE comes, we send copies to the local daemons.
|
|
|
|
*/
|
|
|
|
if (getsockname(so->s, (struct sockaddr *)&addr, &addrlen) < 0)
|
|
|
|
return;
|
2007-09-17 16:09:54 +08:00
|
|
|
|
2004-04-22 08:10:48 +08:00
|
|
|
#define IS_OLD (so->so_emu == EMU_TALK)
|
|
|
|
|
|
|
|
#define COPY_MSG(dest, src) { dest->type = src->type; \
|
|
|
|
dest->id_num = src->id_num; \
|
|
|
|
dest->pid = src->pid; \
|
|
|
|
dest->addr = src->addr; \
|
|
|
|
dest->ctl_addr = src->ctl_addr; \
|
|
|
|
memcpy(&dest->l_name, &src->l_name, NAME_SIZE_OLD); \
|
|
|
|
memcpy(&dest->r_name, &src->r_name, NAME_SIZE_OLD); \
|
|
|
|
memcpy(&dest->r_tty, &src->r_tty, TTY_SIZE); }
|
|
|
|
|
|
|
|
#define OTOSIN(ptr, field) ((struct sockaddr_in *)&ptr->field)
|
|
|
|
/* old_sockaddr to sockaddr_in */
|
|
|
|
|
|
|
|
|
|
|
|
if (IS_OLD) { /* old talk */
|
|
|
|
omsg = mtod(m, CTL_MSG_OLD*);
|
|
|
|
nmsg = (CTL_MSG *) buff;
|
|
|
|
type = omsg->type;
|
|
|
|
OTOSIN(omsg, ctl_addr)->sin_port = addr.sin_port;
|
|
|
|
OTOSIN(omsg, ctl_addr)->sin_addr = our_addr;
|
2008-10-25 19:21:28 +08:00
|
|
|
pstrcpy(omsg->l_name, NAME_SIZE_OLD, getlogin());
|
2007-09-17 05:08:06 +08:00
|
|
|
} else { /* new talk */
|
2004-04-22 08:10:48 +08:00
|
|
|
omsg = (CTL_MSG_OLD *) buff;
|
|
|
|
nmsg = mtod(m, CTL_MSG *);
|
|
|
|
type = nmsg->type;
|
|
|
|
OTOSIN(nmsg, ctl_addr)->sin_port = addr.sin_port;
|
|
|
|
OTOSIN(nmsg, ctl_addr)->sin_addr = our_addr;
|
2008-10-25 19:21:28 +08:00
|
|
|
pstrcpy(nmsg->l_name, NAME_SIZE_OLD, getlogin());
|
2004-04-22 08:10:48 +08:00
|
|
|
}
|
2007-09-17 16:09:54 +08:00
|
|
|
|
2007-09-17 05:08:06 +08:00
|
|
|
if (type == LOOK_UP)
|
2004-04-22 08:10:48 +08:00
|
|
|
return; /* for LOOK_UP this is enough */
|
2007-09-17 16:09:54 +08:00
|
|
|
|
2004-04-22 08:10:48 +08:00
|
|
|
if (IS_OLD) { /* make a copy of the message */
|
|
|
|
COPY_MSG(nmsg, omsg);
|
|
|
|
nmsg->vers = 1;
|
|
|
|
nmsg->answer = 0;
|
|
|
|
} else
|
|
|
|
COPY_MSG(omsg, nmsg);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If if is an ANNOUNCE message, we go through the
|
|
|
|
* request table to see if a tcp port has already
|
|
|
|
* been redirected for this socket. If not, we solisten()
|
|
|
|
* a new socket and add this entry to the table.
|
|
|
|
* The port number of the tcp socket and our IP
|
|
|
|
* are put to the addr field of the message structures.
|
|
|
|
* Then a LEAVE_INVITE is sent to both local daemon
|
|
|
|
* ports, 517 and 518. This is why we have two copies
|
|
|
|
* of the message, one in old talk and one in new talk
|
|
|
|
* format.
|
2007-09-17 05:08:06 +08:00
|
|
|
*/
|
2004-04-22 08:10:48 +08:00
|
|
|
|
|
|
|
if (type == ANNOUNCE) {
|
|
|
|
int s;
|
|
|
|
u_short temp_port;
|
2007-09-17 16:09:54 +08:00
|
|
|
|
2004-04-22 08:10:48 +08:00
|
|
|
for(req = req_tbl; req; req = req->next)
|
|
|
|
if (so == req->udp_so)
|
|
|
|
break; /* found it */
|
2007-09-17 16:09:54 +08:00
|
|
|
|
2004-04-22 08:10:48 +08:00
|
|
|
if (!req) { /* no entry for so, create new */
|
|
|
|
req = (struct talk_request *)
|
|
|
|
malloc(sizeof(struct talk_request));
|
|
|
|
req->udp_so = so;
|
2007-09-17 16:09:54 +08:00
|
|
|
req->tcp_so = solisten(0,
|
2007-09-17 05:08:06 +08:00
|
|
|
OTOSIN(omsg, addr)->sin_addr.s_addr,
|
2004-04-22 08:10:48 +08:00
|
|
|
OTOSIN(omsg, addr)->sin_port,
|
|
|
|
SS_FACCEPTONCE);
|
|
|
|
req->next = req_tbl;
|
|
|
|
req_tbl = req;
|
2007-09-17 16:09:54 +08:00
|
|
|
}
|
|
|
|
|
2004-04-22 08:10:48 +08:00
|
|
|
/* replace port number in addr field */
|
|
|
|
addrlen = sizeof(addr);
|
2007-09-17 05:08:06 +08:00
|
|
|
getsockname(req->tcp_so->s,
|
2004-04-22 08:10:48 +08:00
|
|
|
(struct sockaddr *) &addr,
|
2007-09-17 16:09:54 +08:00
|
|
|
&addrlen);
|
2004-04-22 08:10:48 +08:00
|
|
|
OTOSIN(omsg, addr)->sin_port = addr.sin_port;
|
|
|
|
OTOSIN(omsg, addr)->sin_addr = our_addr;
|
|
|
|
OTOSIN(nmsg, addr)->sin_port = addr.sin_port;
|
2007-09-17 16:09:54 +08:00
|
|
|
OTOSIN(nmsg, addr)->sin_addr = our_addr;
|
|
|
|
|
2004-04-22 08:10:48 +08:00
|
|
|
/* send LEAVE_INVITEs */
|
|
|
|
temp_port = OTOSIN(omsg, ctl_addr)->sin_port;
|
|
|
|
OTOSIN(omsg, ctl_addr)->sin_port = 0;
|
|
|
|
OTOSIN(nmsg, ctl_addr)->sin_port = 0;
|
2007-09-17 16:09:54 +08:00
|
|
|
omsg->type = nmsg->type = LEAVE_INVITE;
|
|
|
|
|
2004-04-22 08:10:48 +08:00
|
|
|
s = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
|
|
|
|
addr.sin_addr = our_addr;
|
|
|
|
addr.sin_family = AF_INET;
|
|
|
|
addr.sin_port = htons(517);
|
2007-09-17 05:08:06 +08:00
|
|
|
sendto(s, (char *)omsg, sizeof(*omsg), 0,
|
2004-04-22 08:10:48 +08:00
|
|
|
(struct sockaddr *)&addr, sizeof(addr));
|
|
|
|
addr.sin_port = htons(518);
|
|
|
|
sendto(s, (char *)nmsg, sizeof(*nmsg), 0,
|
|
|
|
(struct sockaddr *) &addr, sizeof(addr));
|
2004-07-13 06:33:07 +08:00
|
|
|
closesocket(s) ;
|
2004-04-22 08:10:48 +08:00
|
|
|
|
2007-09-17 05:08:06 +08:00
|
|
|
omsg->type = nmsg->type = ANNOUNCE;
|
2004-04-22 08:10:48 +08:00
|
|
|
OTOSIN(omsg, ctl_addr)->sin_port = temp_port;
|
|
|
|
OTOSIN(nmsg, ctl_addr)->sin_port = temp_port;
|
|
|
|
}
|
2007-09-17 16:09:54 +08:00
|
|
|
|
2007-09-17 05:08:06 +08:00
|
|
|
/*
|
2004-04-22 08:10:48 +08:00
|
|
|
* If it is a DELETE message, we send a copy to the
|
|
|
|
* local daemons. Then we delete the entry corresponding
|
|
|
|
* to our socket from the request table.
|
|
|
|
*/
|
2007-09-17 16:09:54 +08:00
|
|
|
|
2004-04-22 08:10:48 +08:00
|
|
|
if (type == DELETE) {
|
|
|
|
struct talk_request *temp_req, *req_next;
|
|
|
|
int s;
|
|
|
|
u_short temp_port;
|
2007-09-17 16:09:54 +08:00
|
|
|
|
2004-04-22 08:10:48 +08:00
|
|
|
temp_port = OTOSIN(omsg, ctl_addr)->sin_port;
|
|
|
|
OTOSIN(omsg, ctl_addr)->sin_port = 0;
|
|
|
|
OTOSIN(nmsg, ctl_addr)->sin_port = 0;
|
2007-09-17 16:09:54 +08:00
|
|
|
|
2004-04-22 08:10:48 +08:00
|
|
|
s = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
|
|
|
|
addr.sin_addr = our_addr;
|
|
|
|
addr.sin_family = AF_INET;
|
|
|
|
addr.sin_port = htons(517);
|
|
|
|
sendto(s, (char *)omsg, sizeof(*omsg), 0,
|
|
|
|
(struct sockaddr *)&addr, sizeof(addr));
|
|
|
|
addr.sin_port = htons(518);
|
|
|
|
sendto(s, (char *)nmsg, sizeof(*nmsg), 0,
|
|
|
|
(struct sockaddr *)&addr, sizeof(addr));
|
2004-07-13 06:33:07 +08:00
|
|
|
closesocket(s);
|
2007-09-17 16:09:54 +08:00
|
|
|
|
2004-04-22 08:10:48 +08:00
|
|
|
OTOSIN(omsg, ctl_addr)->sin_port = temp_port;
|
|
|
|
OTOSIN(nmsg, ctl_addr)->sin_port = temp_port;
|
|
|
|
|
|
|
|
/* delete table entry */
|
|
|
|
if (so == req_tbl->udp_so) {
|
|
|
|
temp_req = req_tbl;
|
|
|
|
req_tbl = req_tbl->next;
|
|
|
|
free(temp_req);
|
|
|
|
} else {
|
|
|
|
temp_req = req_tbl;
|
|
|
|
for(req = req_tbl->next; req; req = req_next) {
|
|
|
|
req_next = req->next;
|
|
|
|
if (so == req->udp_so) {
|
|
|
|
temp_req->next = req_next;
|
|
|
|
free(req);
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
temp_req = req;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-09-17 16:09:54 +08:00
|
|
|
|
|
|
|
return;
|
2004-04-22 08:10:48 +08:00
|
|
|
#endif
|
2007-09-17 16:09:54 +08:00
|
|
|
|
2007-09-17 05:08:06 +08:00
|
|
|
case EMU_CUSEEME:
|
|
|
|
|
2004-04-22 08:10:48 +08:00
|
|
|
/*
|
|
|
|
* Cu-SeeMe emulation.
|
|
|
|
* Hopefully the packet is more that 16 bytes long. We don't
|
|
|
|
* do any other tests, just replace the address and port
|
|
|
|
* fields.
|
2007-09-17 05:08:06 +08:00
|
|
|
*/
|
2004-04-22 08:10:48 +08:00
|
|
|
if (m->m_len >= sizeof (*cu_head)) {
|
|
|
|
if (getsockname(so->s, (struct sockaddr *)&addr, &addrlen) < 0)
|
|
|
|
return;
|
|
|
|
cu_head = mtod(m, struct cu_header *);
|
2005-06-06 01:11:42 +08:00
|
|
|
cu_head->s_port = addr.sin_port;
|
2005-07-04 01:08:43 +08:00
|
|
|
cu_head->so_addr = our_addr.s_addr;
|
2004-04-22 08:10:48 +08:00
|
|
|
}
|
2007-09-17 16:09:54 +08:00
|
|
|
|
2004-04-22 08:10:48 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct socket *
|
2009-06-24 20:42:28 +08:00
|
|
|
udp_listen(u_int32_t haddr, u_int hport, u_int32_t laddr, u_int lport,
|
|
|
|
int flags)
|
2004-04-22 08:10:48 +08:00
|
|
|
{
|
|
|
|
struct sockaddr_in addr;
|
|
|
|
struct socket *so;
|
2008-05-10 09:49:53 +08:00
|
|
|
socklen_t addrlen = sizeof(struct sockaddr_in), opt = 1;
|
2007-09-17 05:08:06 +08:00
|
|
|
|
2004-04-22 08:10:48 +08:00
|
|
|
if ((so = socreate()) == NULL) {
|
|
|
|
free(so);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
so->s = socket(AF_INET,SOCK_DGRAM,0);
|
|
|
|
so->so_expire = curtime + SO_EXPIRE;
|
|
|
|
insque(so,&udb);
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
setsockopt(so->s,SOL_SOCKET,SO_REUSEADDR,(char *)&opt,sizeof(int));
|
2007-09-17 05:08:06 +08:00
|
|
|
|
2004-04-22 08:10:48 +08:00
|
|
|
getsockname(so->s,(struct sockaddr *)&addr,&addrlen);
|
|
|
|
so->so_fport = addr.sin_port;
|
2009-06-24 20:42:28 +08:00
|
|
|
if (addr.sin_addr.s_addr == 0 ||
|
|
|
|
addr.sin_addr.s_addr == loopback_addr.s_addr) {
|
|
|
|
so->so_faddr = vhost_addr;
|
|
|
|
} else {
|
2004-04-22 08:10:48 +08:00
|
|
|
so->so_faddr = addr.sin_addr;
|
2009-06-24 20:42:28 +08:00
|
|
|
}
|
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;
|
|
|
|
}
|