Statistics
| Branch: | Revision:

root / slirp / udp.c @ aeed97c4

History | View | Annotate | Download (17.1 kB)

1 f0cbd3ec bellard
/*
2 f0cbd3ec bellard
 * Copyright (c) 1982, 1986, 1988, 1990, 1993
3 f0cbd3ec bellard
 *        The Regents of the University of California.  All rights reserved.
4 f0cbd3ec bellard
 *
5 f0cbd3ec bellard
 * Redistribution and use in source and binary forms, with or without
6 f0cbd3ec bellard
 * modification, are permitted provided that the following conditions
7 f0cbd3ec bellard
 * are met:
8 f0cbd3ec bellard
 * 1. Redistributions of source code must retain the above copyright
9 f0cbd3ec bellard
 *    notice, this list of conditions and the following disclaimer.
10 f0cbd3ec bellard
 * 2. Redistributions in binary form must reproduce the above copyright
11 f0cbd3ec bellard
 *    notice, this list of conditions and the following disclaimer in the
12 f0cbd3ec bellard
 *    documentation and/or other materials provided with the distribution.
13 2f5f8996 aliguori
 * 3. Neither the name of the University nor the names of its contributors
14 f0cbd3ec bellard
 *    may be used to endorse or promote products derived from this software
15 f0cbd3ec bellard
 *    without specific prior written permission.
16 f0cbd3ec bellard
 *
17 f0cbd3ec bellard
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 f0cbd3ec bellard
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 f0cbd3ec bellard
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 f0cbd3ec bellard
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 f0cbd3ec bellard
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 f0cbd3ec bellard
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 f0cbd3ec bellard
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 f0cbd3ec bellard
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 f0cbd3ec bellard
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 f0cbd3ec bellard
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 f0cbd3ec bellard
 * SUCH DAMAGE.
28 f0cbd3ec bellard
 *
29 f0cbd3ec bellard
 *        @(#)udp_usrreq.c        8.4 (Berkeley) 1/21/94
30 f0cbd3ec bellard
 * udp_usrreq.c,v 1.4 1994/10/02 17:48:45 phk Exp
31 f0cbd3ec bellard
 */
32 f0cbd3ec bellard
33 f0cbd3ec bellard
/*
34 f0cbd3ec bellard
 * Changes and additions relating to SLiRP
35 f0cbd3ec bellard
 * Copyright (c) 1995 Danny Gasparovski.
36 5fafdf24 ths
 *
37 5fafdf24 ths
 * Please read the file COPYRIGHT for the
38 f0cbd3ec bellard
 * terms and conditions of the copyright.
39 f0cbd3ec bellard
 */
40 f0cbd3ec bellard
41 f0cbd3ec bellard
#include <slirp.h>
42 f0cbd3ec bellard
#include "ip_icmp.h"
43 f0cbd3ec bellard
44 31a60e22 blueswir1
#ifdef LOG_ENABLED
45 f0cbd3ec bellard
struct udpstat udpstat;
46 31a60e22 blueswir1
#endif
47 f0cbd3ec bellard
48 f0cbd3ec bellard
struct socket udb;
49 f0cbd3ec bellard
50 9634d903 blueswir1
static u_int8_t udp_tos(struct socket *so);
51 9634d903 blueswir1
static void udp_emu(struct socket *so, struct mbuf *m);
52 9634d903 blueswir1
53 f0cbd3ec bellard
/*
54 f0cbd3ec bellard
 * UDP protocol implementation.
55 f0cbd3ec bellard
 * Per RFC 768, August, 1980.
56 f0cbd3ec bellard
 */
57 f0cbd3ec bellard
#ifndef        COMPAT_42
58 9634d903 blueswir1
#define UDPCKSUM 1
59 f0cbd3ec bellard
#else
60 9634d903 blueswir1
#define UDPCKSUM 0 /* XXX */
61 f0cbd3ec bellard
#endif
62 f0cbd3ec bellard
63 f0cbd3ec bellard
struct        socket *udp_last_so = &udb;
64 f0cbd3ec bellard
65 f0cbd3ec bellard
void
66 aeed97c4 blueswir1
udp_init(void)
67 f0cbd3ec bellard
{
68 f0cbd3ec bellard
        udb.so_next = udb.so_prev = &udb;
69 f0cbd3ec bellard
}
70 5fafdf24 ths
/* m->m_data  points at ip packet header
71 5fafdf24 ths
 * m->m_len   length ip packet
72 f0cbd3ec bellard
 * ip->ip_len length data (IPDU)
73 f0cbd3ec bellard
 */
74 f0cbd3ec bellard
void
75 aeed97c4 blueswir1
udp_input(register struct mbuf *m, int iphlen)
76 f0cbd3ec bellard
{
77 f0cbd3ec bellard
        register struct ip *ip;
78 f0cbd3ec bellard
        register struct udphdr *uh;
79 f0cbd3ec bellard
/*        struct mbuf *opts = 0;*/
80 f0cbd3ec bellard
        int len;
81 5fafdf24 ths
        struct ip save_ip;
82 f0cbd3ec bellard
        struct socket *so;
83 5fafdf24 ths
84 f0cbd3ec bellard
        DEBUG_CALL("udp_input");
85 f0cbd3ec bellard
        DEBUG_ARG("m = %lx", (long)m);
86 f0cbd3ec bellard
        DEBUG_ARG("iphlen = %d", iphlen);
87 5fafdf24 ths
88 31a60e22 blueswir1
        STAT(udpstat.udps_ipackets++);
89 f0cbd3ec bellard
90 f0cbd3ec bellard
        /*
91 f0cbd3ec bellard
         * Strip IP options, if any; should skip this,
92 f0cbd3ec bellard
         * make available to user, and use on returned packets,
93 f0cbd3ec bellard
         * but we don't yet have a way to check the checksum
94 f0cbd3ec bellard
         * with options still present.
95 f0cbd3ec bellard
         */
96 f0cbd3ec bellard
        if(iphlen > sizeof(struct ip)) {
97 f0cbd3ec bellard
                ip_stripoptions(m, (struct mbuf *)0);
98 f0cbd3ec bellard
                iphlen = sizeof(struct ip);
99 f0cbd3ec bellard
        }
100 f0cbd3ec bellard
101 f0cbd3ec bellard
        /*
102 f0cbd3ec bellard
         * Get IP and UDP header together in first mbuf.
103 f0cbd3ec bellard
         */
104 f0cbd3ec bellard
        ip = mtod(m, struct ip *);
105 f0cbd3ec bellard
        uh = (struct udphdr *)((caddr_t)ip + iphlen);
106 f0cbd3ec bellard
107 f0cbd3ec bellard
        /*
108 f0cbd3ec bellard
         * Make mbuf data length reflect UDP length.
109 f0cbd3ec bellard
         * If not enough data to reflect UDP length, drop.
110 f0cbd3ec bellard
         */
111 f0cbd3ec bellard
        len = ntohs((u_int16_t)uh->uh_ulen);
112 f0cbd3ec bellard
113 f0cbd3ec bellard
        if (ip->ip_len != len) {
114 f0cbd3ec bellard
                if (len > ip->ip_len) {
115 31a60e22 blueswir1
                        STAT(udpstat.udps_badlen++);
116 f0cbd3ec bellard
                        goto bad;
117 f0cbd3ec bellard
                }
118 f0cbd3ec bellard
                m_adj(m, len - ip->ip_len);
119 f0cbd3ec bellard
                ip->ip_len = len;
120 f0cbd3ec bellard
        }
121 5fafdf24 ths
122 f0cbd3ec bellard
        /*
123 f0cbd3ec bellard
         * Save a copy of the IP header in case we want restore it
124 f0cbd3ec bellard
         * for sending an ICMP error message in response.
125 f0cbd3ec bellard
         */
126 5fafdf24 ths
        save_ip = *ip;
127 f0cbd3ec bellard
        save_ip.ip_len+= iphlen;         /* tcp_input subtracts this */
128 f0cbd3ec bellard
129 f0cbd3ec bellard
        /*
130 f0cbd3ec bellard
         * Checksum extended UDP header and data.
131 f0cbd3ec bellard
         */
132 9634d903 blueswir1
        if (UDPCKSUM && uh->uh_sum) {
133 429d0a3d blueswir1
      memset(&((struct ipovly *)ip)->ih_mbuf, 0, sizeof(struct mbuf_ptr));
134 f0cbd3ec bellard
          ((struct ipovly *)ip)->ih_x1 = 0;
135 f0cbd3ec bellard
          ((struct ipovly *)ip)->ih_len = uh->uh_ulen;
136 f0cbd3ec bellard
          /* keep uh_sum for ICMP reply
137 5fafdf24 ths
           * uh->uh_sum = cksum(m, len + sizeof (struct ip));
138 5fafdf24 ths
           * if (uh->uh_sum) {
139 f0cbd3ec bellard
           */
140 f0cbd3ec bellard
          if(cksum(m, len + sizeof(struct ip))) {
141 31a60e22 blueswir1
            STAT(udpstat.udps_badsum++);
142 f0cbd3ec bellard
            goto bad;
143 f0cbd3ec bellard
          }
144 f0cbd3ec bellard
        }
145 f0cbd3ec bellard
146 f0cbd3ec bellard
        /*
147 f0cbd3ec bellard
         *  handle DHCP/BOOTP
148 f0cbd3ec bellard
         */
149 f0cbd3ec bellard
        if (ntohs(uh->uh_dport) == BOOTP_SERVER) {
150 f0cbd3ec bellard
            bootp_input(m);
151 f0cbd3ec bellard
            goto bad;
152 f0cbd3ec bellard
        }
153 f0cbd3ec bellard
154 a9ba3a85 aliguori
        if (slirp_restrict)
155 a9ba3a85 aliguori
            goto bad;
156 a9ba3a85 aliguori
157 c7f74643 bellard
        /*
158 c7f74643 bellard
         *  handle TFTP
159 c7f74643 bellard
         */
160 c7f74643 bellard
        if (ntohs(uh->uh_dport) == TFTP_SERVER) {
161 c7f74643 bellard
            tftp_input(m);
162 c7f74643 bellard
            goto bad;
163 c7f74643 bellard
        }
164 c7f74643 bellard
165 f0cbd3ec bellard
        /*
166 f0cbd3ec bellard
         * Locate pcb for datagram.
167 f0cbd3ec bellard
         */
168 f0cbd3ec bellard
        so = udp_last_so;
169 f0cbd3ec bellard
        if (so->so_lport != uh->uh_sport ||
170 f0cbd3ec bellard
            so->so_laddr.s_addr != ip->ip_src.s_addr) {
171 f0cbd3ec bellard
                struct socket *tmp;
172 3b46e624 ths
173 f0cbd3ec bellard
                for (tmp = udb.so_next; tmp != &udb; tmp = tmp->so_next) {
174 f0cbd3ec bellard
                        if (tmp->so_lport == uh->uh_sport &&
175 f0cbd3ec bellard
                            tmp->so_laddr.s_addr == ip->ip_src.s_addr) {
176 f0cbd3ec bellard
                                tmp->so_faddr.s_addr = ip->ip_dst.s_addr;
177 f0cbd3ec bellard
                                tmp->so_fport = uh->uh_dport;
178 f0cbd3ec bellard
                                so = tmp;
179 f0cbd3ec bellard
                                break;
180 f0cbd3ec bellard
                        }
181 f0cbd3ec bellard
                }
182 f0cbd3ec bellard
                if (tmp == &udb) {
183 f0cbd3ec bellard
                  so = NULL;
184 f0cbd3ec bellard
                } else {
185 31a60e22 blueswir1
                  STAT(udpstat.udpps_pcbcachemiss++);
186 f0cbd3ec bellard
                  udp_last_so = so;
187 f0cbd3ec bellard
                }
188 f0cbd3ec bellard
        }
189 5fafdf24 ths
190 f0cbd3ec bellard
        if (so == NULL) {
191 f0cbd3ec bellard
          /*
192 f0cbd3ec bellard
           * If there's no socket for this packet,
193 f0cbd3ec bellard
           * create one
194 f0cbd3ec bellard
           */
195 f0cbd3ec bellard
          if ((so = socreate()) == NULL) goto bad;
196 f0cbd3ec bellard
          if(udp_attach(so) == -1) {
197 5fafdf24 ths
            DEBUG_MISC((dfd," udp_attach errno = %d-%s\n",
198 f0cbd3ec bellard
                        errno,strerror(errno)));
199 f0cbd3ec bellard
            sofree(so);
200 f0cbd3ec bellard
            goto bad;
201 f0cbd3ec bellard
          }
202 3b46e624 ths
203 f0cbd3ec bellard
          /*
204 f0cbd3ec bellard
           * Setup fields
205 f0cbd3ec bellard
           */
206 f0cbd3ec bellard
          /* udp_last_so = so; */
207 f0cbd3ec bellard
          so->so_laddr = ip->ip_src;
208 f0cbd3ec bellard
          so->so_lport = uh->uh_sport;
209 3b46e624 ths
210 f0cbd3ec bellard
          if ((so->so_iptos = udp_tos(so)) == 0)
211 f0cbd3ec bellard
            so->so_iptos = ip->ip_tos;
212 3b46e624 ths
213 f0cbd3ec bellard
          /*
214 f0cbd3ec bellard
           * XXXXX Here, check if it's in udpexec_list,
215 f0cbd3ec bellard
           * and if it is, do the fork_exec() etc.
216 f0cbd3ec bellard
           */
217 f0cbd3ec bellard
        }
218 f0cbd3ec bellard
219 54fd9cdf ths
        so->so_faddr = ip->ip_dst; /* XXX */
220 54fd9cdf ths
        so->so_fport = uh->uh_dport; /* XXX */
221 54fd9cdf ths
222 f0cbd3ec bellard
        iphlen += sizeof(struct udphdr);
223 f0cbd3ec bellard
        m->m_len -= iphlen;
224 f0cbd3ec bellard
        m->m_data += iphlen;
225 f0cbd3ec bellard
226 f0cbd3ec bellard
        /*
227 f0cbd3ec bellard
         * Now we sendto() the packet.
228 f0cbd3ec bellard
         */
229 f0cbd3ec bellard
        if (so->so_emu)
230 f0cbd3ec bellard
           udp_emu(so, m);
231 f0cbd3ec bellard
232 f0cbd3ec bellard
        if(sosendto(so,m) == -1) {
233 f0cbd3ec bellard
          m->m_len += iphlen;
234 f0cbd3ec bellard
          m->m_data -= iphlen;
235 f0cbd3ec bellard
          *ip=save_ip;
236 f0cbd3ec bellard
          DEBUG_MISC((dfd,"udp tx errno = %d-%s\n",errno,strerror(errno)));
237 3b46e624 ths
          icmp_error(m, ICMP_UNREACH,ICMP_UNREACH_NET, 0,strerror(errno));
238 f0cbd3ec bellard
        }
239 f0cbd3ec bellard
240 f0cbd3ec bellard
        m_free(so->so_m);   /* used for ICMP if error on sorecvfrom */
241 f0cbd3ec bellard
242 f0cbd3ec bellard
        /* restore the orig mbuf packet */
243 f0cbd3ec bellard
        m->m_len += iphlen;
244 f0cbd3ec bellard
        m->m_data -= iphlen;
245 f0cbd3ec bellard
        *ip=save_ip;
246 f0cbd3ec bellard
        so->so_m=m;         /* ICMP backup */
247 f0cbd3ec bellard
248 f0cbd3ec bellard
        return;
249 f0cbd3ec bellard
bad:
250 f0cbd3ec bellard
        m_freem(m);
251 f0cbd3ec bellard
        /* if (opts) m_freem(opts); */
252 f0cbd3ec bellard
        return;
253 f0cbd3ec bellard
}
254 f0cbd3ec bellard
255 5fafdf24 ths
int udp_output2(struct socket *so, struct mbuf *m,
256 f0cbd3ec bellard
                struct sockaddr_in *saddr, struct sockaddr_in *daddr,
257 f0cbd3ec bellard
                int iptos)
258 f0cbd3ec bellard
{
259 f0cbd3ec bellard
        register struct udpiphdr *ui;
260 f0cbd3ec bellard
        int error = 0;
261 f0cbd3ec bellard
262 f0cbd3ec bellard
        DEBUG_CALL("udp_output");
263 f0cbd3ec bellard
        DEBUG_ARG("so = %lx", (long)so);
264 f0cbd3ec bellard
        DEBUG_ARG("m = %lx", (long)m);
265 f0cbd3ec bellard
        DEBUG_ARG("saddr = %lx", (long)saddr->sin_addr.s_addr);
266 f0cbd3ec bellard
        DEBUG_ARG("daddr = %lx", (long)daddr->sin_addr.s_addr);
267 f0cbd3ec bellard
268 f0cbd3ec bellard
        /*
269 f0cbd3ec bellard
         * Adjust for header
270 f0cbd3ec bellard
         */
271 f0cbd3ec bellard
        m->m_data -= sizeof(struct udpiphdr);
272 f0cbd3ec bellard
        m->m_len += sizeof(struct udpiphdr);
273 5fafdf24 ths
274 f0cbd3ec bellard
        /*
275 f0cbd3ec bellard
         * Fill in mbuf with extended UDP header
276 f0cbd3ec bellard
         * and addresses and length put into network format.
277 f0cbd3ec bellard
         */
278 f0cbd3ec bellard
        ui = mtod(m, struct udpiphdr *);
279 429d0a3d blueswir1
    memset(&ui->ui_i.ih_mbuf, 0 , sizeof(struct mbuf_ptr));
280 f0cbd3ec bellard
        ui->ui_x1 = 0;
281 f0cbd3ec bellard
        ui->ui_pr = IPPROTO_UDP;
282 f0cbd3ec bellard
        ui->ui_len = htons(m->m_len - sizeof(struct ip)); /* + sizeof (struct udphdr)); */
283 f0cbd3ec bellard
        /* XXXXX Check for from-one-location sockets, or from-any-location sockets */
284 f0cbd3ec bellard
        ui->ui_src = saddr->sin_addr;
285 f0cbd3ec bellard
        ui->ui_dst = daddr->sin_addr;
286 f0cbd3ec bellard
        ui->ui_sport = saddr->sin_port;
287 f0cbd3ec bellard
        ui->ui_dport = daddr->sin_port;
288 f0cbd3ec bellard
        ui->ui_ulen = ui->ui_len;
289 f0cbd3ec bellard
290 f0cbd3ec bellard
        /*
291 f0cbd3ec bellard
         * Stuff checksum and output datagram.
292 f0cbd3ec bellard
         */
293 f0cbd3ec bellard
        ui->ui_sum = 0;
294 9634d903 blueswir1
        if (UDPCKSUM) {
295 f0cbd3ec bellard
            if ((ui->ui_sum = cksum(m, /* sizeof (struct udpiphdr) + */ m->m_len)) == 0)
296 f0cbd3ec bellard
                ui->ui_sum = 0xffff;
297 f0cbd3ec bellard
        }
298 f0cbd3ec bellard
        ((struct ip *)ui)->ip_len = m->m_len;
299 f0cbd3ec bellard
300 9634d903 blueswir1
        ((struct ip *)ui)->ip_ttl = IPDEFTTL;
301 f0cbd3ec bellard
        ((struct ip *)ui)->ip_tos = iptos;
302 5fafdf24 ths
303 31a60e22 blueswir1
        STAT(udpstat.udps_opackets++);
304 5fafdf24 ths
305 f0cbd3ec bellard
        error = ip_output(so, m);
306 5fafdf24 ths
307 f0cbd3ec bellard
        return (error);
308 f0cbd3ec bellard
}
309 f0cbd3ec bellard
310 5fafdf24 ths
int udp_output(struct socket *so, struct mbuf *m,
311 f0cbd3ec bellard
               struct sockaddr_in *addr)
312 f0cbd3ec bellard
313 f0cbd3ec bellard
{
314 f0cbd3ec bellard
    struct sockaddr_in saddr, daddr;
315 f0cbd3ec bellard
316 f0cbd3ec bellard
    saddr = *addr;
317 f3ae0704 balrog
    if ((so->so_faddr.s_addr & htonl(0xffffff00)) == special_addr.s_addr) {
318 c904d61f bellard
        if ((so->so_faddr.s_addr & htonl(0x000000ff)) == htonl(0xff))
319 c904d61f bellard
            saddr.sin_addr.s_addr = alias_addr.s_addr;
320 f3ae0704 balrog
        else if (addr->sin_addr.s_addr == loopback_addr.s_addr ||
321 242acf3a balrog
                 (ntohl(so->so_faddr.s_addr) & 0xff) != CTL_ALIAS)
322 f3ae0704 balrog
            saddr.sin_addr.s_addr = so->so_faddr.s_addr;
323 c904d61f bellard
    }
324 f0cbd3ec bellard
    daddr.sin_addr = so->so_laddr;
325 f0cbd3ec bellard
    daddr.sin_port = so->so_lport;
326 3b46e624 ths
327 f0cbd3ec bellard
    return udp_output2(so, m, &saddr, &daddr, so->so_iptos);
328 f0cbd3ec bellard
}
329 f0cbd3ec bellard
330 f0cbd3ec bellard
int
331 aeed97c4 blueswir1
udp_attach(struct socket *so)
332 f0cbd3ec bellard
{
333 f0cbd3ec bellard
  struct sockaddr_in addr;
334 5fafdf24 ths
335 f0cbd3ec bellard
  if((so->s = socket(AF_INET,SOCK_DGRAM,0)) != -1) {
336 f0cbd3ec bellard
    /*
337 f0cbd3ec bellard
     * Here, we bind() the socket.  Although not really needed
338 f0cbd3ec bellard
     * (sendto() on an unbound socket will bind it), it's done
339 f0cbd3ec bellard
     * here so that emulation of ytalk etc. don't have to do it
340 f0cbd3ec bellard
     */
341 f0cbd3ec bellard
    addr.sin_family = AF_INET;
342 f0cbd3ec bellard
    addr.sin_port = 0;
343 f0cbd3ec bellard
    addr.sin_addr.s_addr = INADDR_ANY;
344 f0cbd3ec bellard
    if(bind(so->s, (struct sockaddr *)&addr, sizeof(addr))<0) {
345 f0cbd3ec bellard
      int lasterrno=errno;
346 379ff53d bellard
      closesocket(so->s);
347 f0cbd3ec bellard
      so->s=-1;
348 02d2c54c bellard
#ifdef _WIN32
349 02d2c54c bellard
      WSASetLastError(lasterrno);
350 02d2c54c bellard
#else
351 f0cbd3ec bellard
      errno=lasterrno;
352 02d2c54c bellard
#endif
353 f0cbd3ec bellard
    } else {
354 f0cbd3ec bellard
      /* success, insert in queue */
355 f0cbd3ec bellard
      so->so_expire = curtime + SO_EXPIRE;
356 f0cbd3ec bellard
      insque(so,&udb);
357 f0cbd3ec bellard
    }
358 f0cbd3ec bellard
  }
359 f0cbd3ec bellard
  return(so->s);
360 f0cbd3ec bellard
}
361 f0cbd3ec bellard
362 f0cbd3ec bellard
void
363 aeed97c4 blueswir1
udp_detach(struct socket *so)
364 f0cbd3ec bellard
{
365 379ff53d bellard
        closesocket(so->s);
366 f0cbd3ec bellard
        /* if (so->so_m) m_free(so->so_m);    done by sofree */
367 f0cbd3ec bellard
368 f0cbd3ec bellard
        sofree(so);
369 f0cbd3ec bellard
}
370 f0cbd3ec bellard
371 9634d903 blueswir1
static const struct tos_t udptos[] = {
372 f0cbd3ec bellard
        {0, 53, IPTOS_LOWDELAY, 0},                        /* DNS */
373 f0cbd3ec bellard
        {517, 517, IPTOS_LOWDELAY, EMU_TALK},        /* talk */
374 f0cbd3ec bellard
        {518, 518, IPTOS_LOWDELAY, EMU_NTALK},        /* ntalk */
375 f0cbd3ec bellard
        {0, 7648, IPTOS_LOWDELAY, EMU_CUSEEME},        /* Cu-Seeme */
376 f0cbd3ec bellard
        {0, 0, 0, 0}
377 f0cbd3ec bellard
};
378 f0cbd3ec bellard
379 9634d903 blueswir1
static u_int8_t
380 9634d903 blueswir1
udp_tos(struct socket *so)
381 f0cbd3ec bellard
{
382 f0cbd3ec bellard
        int i = 0;
383 5fafdf24 ths
384 f0cbd3ec bellard
        while(udptos[i].tos) {
385 f0cbd3ec bellard
                if ((udptos[i].fport && ntohs(so->so_fport) == udptos[i].fport) ||
386 f0cbd3ec bellard
                    (udptos[i].lport && ntohs(so->so_lport) == udptos[i].lport)) {
387 f0cbd3ec bellard
                            so->so_emu = udptos[i].emu;
388 f0cbd3ec bellard
                        return udptos[i].tos;
389 f0cbd3ec bellard
                }
390 f0cbd3ec bellard
                i++;
391 f0cbd3ec bellard
        }
392 5fafdf24 ths
393 f0cbd3ec bellard
        return 0;
394 f0cbd3ec bellard
}
395 f0cbd3ec bellard
396 f0cbd3ec bellard
#ifdef EMULATE_TALK
397 f0cbd3ec bellard
#include "talkd.h"
398 f0cbd3ec bellard
#endif
399 f0cbd3ec bellard
400 f0cbd3ec bellard
/*
401 f0cbd3ec bellard
 * Here, talk/ytalk/ntalk requests must be emulated
402 f0cbd3ec bellard
 */
403 9634d903 blueswir1
static void
404 9634d903 blueswir1
udp_emu(struct socket *so, struct mbuf *m)
405 f0cbd3ec bellard
{
406 f0cbd3ec bellard
        struct sockaddr_in addr;
407 242acf3a balrog
        socklen_t addrlen = sizeof(addr);
408 f0cbd3ec bellard
#ifdef EMULATE_TALK
409 f0cbd3ec bellard
        CTL_MSG_OLD *omsg;
410 f0cbd3ec bellard
        CTL_MSG *nmsg;
411 f0cbd3ec bellard
        char buff[sizeof(CTL_MSG)];
412 f0cbd3ec bellard
        u_char type;
413 5fafdf24 ths
414 f0cbd3ec bellard
struct talk_request {
415 f0cbd3ec bellard
        struct talk_request *next;
416 f0cbd3ec bellard
        struct socket *udp_so;
417 f0cbd3ec bellard
        struct socket *tcp_so;
418 f0cbd3ec bellard
} *req;
419 5fafdf24 ths
420 5fafdf24 ths
        static struct talk_request *req_tbl = 0;
421 5fafdf24 ths
422 f0cbd3ec bellard
#endif
423 5fafdf24 ths
424 f0cbd3ec bellard
struct cu_header {
425 101c5935 bellard
        uint16_t        d_family;                // destination family
426 101c5935 bellard
        uint16_t        d_port;                        // destination port
427 101c5935 bellard
        uint32_t        d_addr;                        // destination address
428 101c5935 bellard
        uint16_t        s_family;                // source family
429 101c5935 bellard
        uint16_t        s_port;                        // source port
430 51a36cb2 bellard
        uint32_t        so_addr;                // source address
431 101c5935 bellard
        uint32_t        seqn;                        // sequence number
432 101c5935 bellard
        uint16_t        message;                // message
433 101c5935 bellard
        uint16_t        data_type;                // data type
434 101c5935 bellard
        uint16_t        pkt_len;                // packet length
435 f0cbd3ec bellard
} *cu_head;
436 f0cbd3ec bellard
437 f0cbd3ec bellard
        switch(so->so_emu) {
438 f0cbd3ec bellard
439 f0cbd3ec bellard
#ifdef EMULATE_TALK
440 f0cbd3ec bellard
         case EMU_TALK:
441 f0cbd3ec bellard
         case EMU_NTALK:
442 f0cbd3ec bellard
                /*
443 f0cbd3ec bellard
                 * Talk emulation. We always change the ctl_addr to get
444 f0cbd3ec bellard
                 * some answers from the daemon. When an ANNOUNCE comes,
445 f0cbd3ec bellard
                 * we send LEAVE_INVITE to the local daemons. Also when a
446 f0cbd3ec bellard
                 * DELETE comes, we send copies to the local daemons.
447 f0cbd3ec bellard
                 */
448 f0cbd3ec bellard
                if (getsockname(so->s, (struct sockaddr *)&addr, &addrlen) < 0)
449 f0cbd3ec bellard
                        return;
450 3b46e624 ths
451 f0cbd3ec bellard
#define        IS_OLD        (so->so_emu == EMU_TALK)
452 f0cbd3ec bellard
453 f0cbd3ec bellard
#define COPY_MSG(dest, src) { dest->type = src->type; \
454 f0cbd3ec bellard
                              dest->id_num = src->id_num; \
455 f0cbd3ec bellard
                              dest->pid = src->pid; \
456 f0cbd3ec bellard
                              dest->addr = src->addr; \
457 f0cbd3ec bellard
                              dest->ctl_addr = src->ctl_addr; \
458 f0cbd3ec bellard
                              memcpy(&dest->l_name, &src->l_name, NAME_SIZE_OLD); \
459 f0cbd3ec bellard
                              memcpy(&dest->r_name, &src->r_name, NAME_SIZE_OLD); \
460 f0cbd3ec bellard
                               memcpy(&dest->r_tty, &src->r_tty, TTY_SIZE); }
461 f0cbd3ec bellard
462 f0cbd3ec bellard
#define OTOSIN(ptr, field) ((struct sockaddr_in *)&ptr->field)
463 f0cbd3ec bellard
/* old_sockaddr to sockaddr_in */
464 f0cbd3ec bellard
465 f0cbd3ec bellard
466 f0cbd3ec bellard
                if (IS_OLD) {                  /* old talk */
467 f0cbd3ec bellard
                        omsg = mtod(m, CTL_MSG_OLD*);
468 f0cbd3ec bellard
                        nmsg = (CTL_MSG *) buff;
469 f0cbd3ec bellard
                        type = omsg->type;
470 f0cbd3ec bellard
                        OTOSIN(omsg, ctl_addr)->sin_port = addr.sin_port;
471 f0cbd3ec bellard
                        OTOSIN(omsg, ctl_addr)->sin_addr = our_addr;
472 be15b141 blueswir1
                        pstrcpy(omsg->l_name, NAME_SIZE_OLD, getlogin());
473 5fafdf24 ths
                } else {                /* new talk */
474 f0cbd3ec bellard
                        omsg = (CTL_MSG_OLD *) buff;
475 f0cbd3ec bellard
                        nmsg = mtod(m, CTL_MSG *);
476 f0cbd3ec bellard
                        type = nmsg->type;
477 f0cbd3ec bellard
                        OTOSIN(nmsg, ctl_addr)->sin_port = addr.sin_port;
478 f0cbd3ec bellard
                        OTOSIN(nmsg, ctl_addr)->sin_addr = our_addr;
479 be15b141 blueswir1
                        pstrcpy(nmsg->l_name, NAME_SIZE_OLD, getlogin());
480 f0cbd3ec bellard
                }
481 3b46e624 ths
482 5fafdf24 ths
                if (type == LOOK_UP)
483 f0cbd3ec bellard
                        return;                /* for LOOK_UP this is enough */
484 3b46e624 ths
485 f0cbd3ec bellard
                if (IS_OLD) {                /* make a copy of the message */
486 f0cbd3ec bellard
                        COPY_MSG(nmsg, omsg);
487 f0cbd3ec bellard
                        nmsg->vers = 1;
488 f0cbd3ec bellard
                        nmsg->answer = 0;
489 f0cbd3ec bellard
                } else
490 f0cbd3ec bellard
                        COPY_MSG(omsg, nmsg);
491 f0cbd3ec bellard
492 f0cbd3ec bellard
                /*
493 f0cbd3ec bellard
                 * If if is an ANNOUNCE message, we go through the
494 f0cbd3ec bellard
                 * request table to see if a tcp port has already
495 f0cbd3ec bellard
                 * been redirected for this socket. If not, we solisten()
496 f0cbd3ec bellard
                 * a new socket and add this entry to the table.
497 f0cbd3ec bellard
                 * The port number of the tcp socket and our IP
498 f0cbd3ec bellard
                 * are put to the addr field of the message structures.
499 f0cbd3ec bellard
                 * Then a LEAVE_INVITE is sent to both local daemon
500 f0cbd3ec bellard
                 * ports, 517 and 518. This is why we have two copies
501 f0cbd3ec bellard
                 * of the message, one in old talk and one in new talk
502 f0cbd3ec bellard
                 * format.
503 5fafdf24 ths
                 */
504 f0cbd3ec bellard
505 f0cbd3ec bellard
                if (type == ANNOUNCE) {
506 f0cbd3ec bellard
                        int s;
507 f0cbd3ec bellard
                        u_short temp_port;
508 3b46e624 ths
509 f0cbd3ec bellard
                        for(req = req_tbl; req; req = req->next)
510 f0cbd3ec bellard
                                if (so == req->udp_so)
511 f0cbd3ec bellard
                                        break;          /* found it */
512 3b46e624 ths
513 f0cbd3ec bellard
                        if (!req) {        /* no entry for so, create new */
514 f0cbd3ec bellard
                                req = (struct talk_request *)
515 f0cbd3ec bellard
                                        malloc(sizeof(struct talk_request));
516 f0cbd3ec bellard
                                req->udp_so = so;
517 3b46e624 ths
                                req->tcp_so = solisten(0,
518 5fafdf24 ths
                                        OTOSIN(omsg, addr)->sin_addr.s_addr,
519 f0cbd3ec bellard
                                        OTOSIN(omsg, addr)->sin_port,
520 f0cbd3ec bellard
                                        SS_FACCEPTONCE);
521 f0cbd3ec bellard
                                req->next = req_tbl;
522 f0cbd3ec bellard
                                req_tbl = req;
523 3b46e624 ths
                        }
524 3b46e624 ths
525 f0cbd3ec bellard
                        /* replace port number in addr field */
526 f0cbd3ec bellard
                        addrlen = sizeof(addr);
527 5fafdf24 ths
                        getsockname(req->tcp_so->s,
528 f0cbd3ec bellard
                                        (struct sockaddr *) &addr,
529 3b46e624 ths
                                        &addrlen);
530 f0cbd3ec bellard
                        OTOSIN(omsg, addr)->sin_port = addr.sin_port;
531 f0cbd3ec bellard
                        OTOSIN(omsg, addr)->sin_addr = our_addr;
532 f0cbd3ec bellard
                        OTOSIN(nmsg, addr)->sin_port = addr.sin_port;
533 3b46e624 ths
                        OTOSIN(nmsg, addr)->sin_addr = our_addr;
534 3b46e624 ths
535 f0cbd3ec bellard
                        /* send LEAVE_INVITEs */
536 f0cbd3ec bellard
                        temp_port = OTOSIN(omsg, ctl_addr)->sin_port;
537 f0cbd3ec bellard
                        OTOSIN(omsg, ctl_addr)->sin_port = 0;
538 f0cbd3ec bellard
                        OTOSIN(nmsg, ctl_addr)->sin_port = 0;
539 3b46e624 ths
                        omsg->type = nmsg->type = LEAVE_INVITE;
540 3b46e624 ths
541 f0cbd3ec bellard
                        s = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
542 f0cbd3ec bellard
                        addr.sin_addr = our_addr;
543 f0cbd3ec bellard
                        addr.sin_family = AF_INET;
544 f0cbd3ec bellard
                        addr.sin_port = htons(517);
545 5fafdf24 ths
                        sendto(s, (char *)omsg, sizeof(*omsg), 0,
546 f0cbd3ec bellard
                                (struct sockaddr *)&addr, sizeof(addr));
547 f0cbd3ec bellard
                        addr.sin_port = htons(518);
548 f0cbd3ec bellard
                        sendto(s, (char *)nmsg, sizeof(*nmsg), 0,
549 f0cbd3ec bellard
                                (struct sockaddr *) &addr, sizeof(addr));
550 379ff53d bellard
                        closesocket(s) ;
551 f0cbd3ec bellard
552 5fafdf24 ths
                        omsg->type = nmsg->type = ANNOUNCE;
553 f0cbd3ec bellard
                        OTOSIN(omsg, ctl_addr)->sin_port = temp_port;
554 f0cbd3ec bellard
                        OTOSIN(nmsg, ctl_addr)->sin_port = temp_port;
555 f0cbd3ec bellard
                }
556 3b46e624 ths
557 5fafdf24 ths
                /*
558 f0cbd3ec bellard
                 * If it is a DELETE message, we send a copy to the
559 f0cbd3ec bellard
                 * local daemons. Then we delete the entry corresponding
560 f0cbd3ec bellard
                 * to our socket from the request table.
561 f0cbd3ec bellard
                 */
562 3b46e624 ths
563 f0cbd3ec bellard
                if (type == DELETE) {
564 f0cbd3ec bellard
                        struct talk_request *temp_req, *req_next;
565 f0cbd3ec bellard
                        int s;
566 f0cbd3ec bellard
                        u_short temp_port;
567 3b46e624 ths
568 f0cbd3ec bellard
                        temp_port = OTOSIN(omsg, ctl_addr)->sin_port;
569 f0cbd3ec bellard
                        OTOSIN(omsg, ctl_addr)->sin_port = 0;
570 f0cbd3ec bellard
                        OTOSIN(nmsg, ctl_addr)->sin_port = 0;
571 3b46e624 ths
572 f0cbd3ec bellard
                        s = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
573 f0cbd3ec bellard
                        addr.sin_addr = our_addr;
574 f0cbd3ec bellard
                        addr.sin_family = AF_INET;
575 f0cbd3ec bellard
                        addr.sin_port = htons(517);
576 f0cbd3ec bellard
                        sendto(s, (char *)omsg, sizeof(*omsg), 0,
577 f0cbd3ec bellard
                                (struct sockaddr *)&addr, sizeof(addr));
578 f0cbd3ec bellard
                        addr.sin_port = htons(518);
579 f0cbd3ec bellard
                        sendto(s, (char *)nmsg, sizeof(*nmsg), 0,
580 f0cbd3ec bellard
                                (struct sockaddr *)&addr, sizeof(addr));
581 379ff53d bellard
                        closesocket(s);
582 3b46e624 ths
583 f0cbd3ec bellard
                        OTOSIN(omsg, ctl_addr)->sin_port = temp_port;
584 f0cbd3ec bellard
                        OTOSIN(nmsg, ctl_addr)->sin_port = temp_port;
585 f0cbd3ec bellard
586 f0cbd3ec bellard
                        /* delete table entry */
587 f0cbd3ec bellard
                        if (so == req_tbl->udp_so) {
588 f0cbd3ec bellard
                                temp_req = req_tbl;
589 f0cbd3ec bellard
                                req_tbl = req_tbl->next;
590 f0cbd3ec bellard
                                free(temp_req);
591 f0cbd3ec bellard
                        } else {
592 f0cbd3ec bellard
                                temp_req = req_tbl;
593 f0cbd3ec bellard
                                for(req = req_tbl->next; req; req = req_next) {
594 f0cbd3ec bellard
                                        req_next = req->next;
595 f0cbd3ec bellard
                                        if (so == req->udp_so) {
596 f0cbd3ec bellard
                                                temp_req->next = req_next;
597 f0cbd3ec bellard
                                                free(req);
598 f0cbd3ec bellard
                                                break;
599 f0cbd3ec bellard
                                        } else {
600 f0cbd3ec bellard
                                                temp_req = req;
601 f0cbd3ec bellard
                                        }
602 f0cbd3ec bellard
                                }
603 f0cbd3ec bellard
                        }
604 f0cbd3ec bellard
                }
605 3b46e624 ths
606 3b46e624 ths
                return;
607 f0cbd3ec bellard
#endif
608 3b46e624 ths
609 5fafdf24 ths
        case EMU_CUSEEME:
610 5fafdf24 ths
611 f0cbd3ec bellard
                /*
612 f0cbd3ec bellard
                 * Cu-SeeMe emulation.
613 f0cbd3ec bellard
                 * Hopefully the packet is more that 16 bytes long. We don't
614 f0cbd3ec bellard
                 * do any other tests, just replace the address and port
615 f0cbd3ec bellard
                 * fields.
616 5fafdf24 ths
                 */
617 f0cbd3ec bellard
                if (m->m_len >= sizeof (*cu_head)) {
618 f0cbd3ec bellard
                        if (getsockname(so->s, (struct sockaddr *)&addr, &addrlen) < 0)
619 f0cbd3ec bellard
                                return;
620 f0cbd3ec bellard
                        cu_head = mtod(m, struct cu_header *);
621 101c5935 bellard
                        cu_head->s_port = addr.sin_port;
622 51a36cb2 bellard
                        cu_head->so_addr = our_addr.s_addr;
623 f0cbd3ec bellard
                }
624 3b46e624 ths
625 f0cbd3ec bellard
                return;
626 f0cbd3ec bellard
        }
627 f0cbd3ec bellard
}
628 f0cbd3ec bellard
629 f0cbd3ec bellard
struct socket *
630 aeed97c4 blueswir1
udp_listen(u_int port, u_int32_t laddr, u_int lport, int flags)
631 f0cbd3ec bellard
{
632 f0cbd3ec bellard
        struct sockaddr_in addr;
633 f0cbd3ec bellard
        struct socket *so;
634 242acf3a balrog
        socklen_t addrlen = sizeof(struct sockaddr_in), opt = 1;
635 5fafdf24 ths
636 f0cbd3ec bellard
        if ((so = socreate()) == NULL) {
637 f0cbd3ec bellard
                free(so);
638 f0cbd3ec bellard
                return NULL;
639 f0cbd3ec bellard
        }
640 f0cbd3ec bellard
        so->s = socket(AF_INET,SOCK_DGRAM,0);
641 f0cbd3ec bellard
        so->so_expire = curtime + SO_EXPIRE;
642 f0cbd3ec bellard
        insque(so,&udb);
643 f0cbd3ec bellard
644 f0cbd3ec bellard
        addr.sin_family = AF_INET;
645 f0cbd3ec bellard
        addr.sin_addr.s_addr = INADDR_ANY;
646 f0cbd3ec bellard
        addr.sin_port = port;
647 f0cbd3ec bellard
648 f0cbd3ec bellard
        if (bind(so->s,(struct sockaddr *)&addr, addrlen) < 0) {
649 f0cbd3ec bellard
                udp_detach(so);
650 f0cbd3ec bellard
                return NULL;
651 f0cbd3ec bellard
        }
652 f0cbd3ec bellard
        setsockopt(so->s,SOL_SOCKET,SO_REUSEADDR,(char *)&opt,sizeof(int));
653 f0cbd3ec bellard
/*        setsockopt(so->s,SOL_SOCKET,SO_OOBINLINE,(char *)&opt,sizeof(int)); */
654 5fafdf24 ths
655 f0cbd3ec bellard
        getsockname(so->s,(struct sockaddr *)&addr,&addrlen);
656 f0cbd3ec bellard
        so->so_fport = addr.sin_port;
657 f0cbd3ec bellard
        if (addr.sin_addr.s_addr == 0 || addr.sin_addr.s_addr == loopback_addr.s_addr)
658 8dbca8dd bellard
           so->so_faddr = alias_addr;
659 f0cbd3ec bellard
        else
660 f0cbd3ec bellard
           so->so_faddr = addr.sin_addr;
661 5fafdf24 ths
662 f0cbd3ec bellard
        so->so_lport = lport;
663 f0cbd3ec bellard
        so->so_laddr.s_addr = laddr;
664 f0cbd3ec bellard
        if (flags != SS_FACCEPTONCE)
665 f0cbd3ec bellard
           so->so_expire = 0;
666 5fafdf24 ths
667 f0cbd3ec bellard
        so->so_state = SS_ISFCONNECTED;
668 5fafdf24 ths
669 f0cbd3ec bellard
        return so;
670 f0cbd3ec bellard
}