Statistics
| Branch: | Revision:

root / slirp / udp.c @ 5fafdf24

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