Statistics
| Branch: | Revision:

root / slirp / udp.c @ afc7df11

History | View | Annotate | Download (16.7 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 f0cbd3ec bellard
 * 
41 f0cbd3ec bellard
 * 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 f0cbd3ec bellard
/* m->m_data  points at ip packet header 
70 f0cbd3ec bellard
 * 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 f0cbd3ec bellard
        struct ip save_ip; 
83 f0cbd3ec bellard
        struct socket *so;
84 f0cbd3ec bellard
        
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 f0cbd3ec bellard
        
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 f0cbd3ec bellard
        
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 f0cbd3ec bellard
        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 f0cbd3ec bellard
           * uh->uh_sum = cksum(m, len + sizeof (struct ip)); 
140 f0cbd3ec bellard
           * 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 f0cbd3ec bellard
                
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 f0cbd3ec bellard
        
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 f0cbd3ec bellard
            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 f0cbd3ec bellard
          
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 f0cbd3ec bellard
          so->so_faddr = ip->ip_dst; /* XXX */
209 f0cbd3ec bellard
          so->so_fport = uh->uh_dport; /* XXX */
210 f0cbd3ec bellard
          
211 f0cbd3ec bellard
          if ((so->so_iptos = udp_tos(so)) == 0)
212 f0cbd3ec bellard
            so->so_iptos = ip->ip_tos;
213 f0cbd3ec bellard
          
214 f0cbd3ec bellard
          /*
215 f0cbd3ec bellard
           * XXXXX Here, check if it's in udpexec_list,
216 f0cbd3ec bellard
           * and if it is, do the fork_exec() etc.
217 f0cbd3ec bellard
           */
218 f0cbd3ec bellard
        }
219 f0cbd3ec bellard
220 f0cbd3ec bellard
        iphlen += sizeof(struct udphdr);
221 f0cbd3ec bellard
        m->m_len -= iphlen;
222 f0cbd3ec bellard
        m->m_data += iphlen;
223 f0cbd3ec bellard
224 f0cbd3ec bellard
        /*
225 f0cbd3ec bellard
         * Now we sendto() the packet.
226 f0cbd3ec bellard
         */
227 f0cbd3ec bellard
        if (so->so_emu)
228 f0cbd3ec bellard
           udp_emu(so, m);
229 f0cbd3ec bellard
230 f0cbd3ec bellard
        if(sosendto(so,m) == -1) {
231 f0cbd3ec bellard
          m->m_len += iphlen;
232 f0cbd3ec bellard
          m->m_data -= iphlen;
233 f0cbd3ec bellard
          *ip=save_ip;
234 f0cbd3ec bellard
          DEBUG_MISC((dfd,"udp tx errno = %d-%s\n",errno,strerror(errno)));
235 f0cbd3ec bellard
          icmp_error(m, ICMP_UNREACH,ICMP_UNREACH_NET, 0,strerror(errno));  
236 f0cbd3ec bellard
        }
237 f0cbd3ec bellard
238 f0cbd3ec bellard
        m_free(so->so_m);   /* used for ICMP if error on sorecvfrom */
239 f0cbd3ec bellard
240 f0cbd3ec bellard
        /* restore the orig mbuf packet */
241 f0cbd3ec bellard
        m->m_len += iphlen;
242 f0cbd3ec bellard
        m->m_data -= iphlen;
243 f0cbd3ec bellard
        *ip=save_ip;
244 f0cbd3ec bellard
        so->so_m=m;         /* ICMP backup */
245 f0cbd3ec bellard
246 f0cbd3ec bellard
        return;
247 f0cbd3ec bellard
bad:
248 f0cbd3ec bellard
        m_freem(m);
249 f0cbd3ec bellard
        /* if (opts) m_freem(opts); */
250 f0cbd3ec bellard
        return;
251 f0cbd3ec bellard
}
252 f0cbd3ec bellard
253 f0cbd3ec bellard
int udp_output2(struct socket *so, struct mbuf *m, 
254 f0cbd3ec bellard
                struct sockaddr_in *saddr, struct sockaddr_in *daddr,
255 f0cbd3ec bellard
                int iptos)
256 f0cbd3ec bellard
{
257 f0cbd3ec bellard
        register struct udpiphdr *ui;
258 f0cbd3ec bellard
        int error = 0;
259 f0cbd3ec bellard
260 f0cbd3ec bellard
        DEBUG_CALL("udp_output");
261 f0cbd3ec bellard
        DEBUG_ARG("so = %lx", (long)so);
262 f0cbd3ec bellard
        DEBUG_ARG("m = %lx", (long)m);
263 f0cbd3ec bellard
        DEBUG_ARG("saddr = %lx", (long)saddr->sin_addr.s_addr);
264 f0cbd3ec bellard
        DEBUG_ARG("daddr = %lx", (long)daddr->sin_addr.s_addr);
265 f0cbd3ec bellard
266 f0cbd3ec bellard
        /*
267 f0cbd3ec bellard
         * Adjust for header
268 f0cbd3ec bellard
         */
269 f0cbd3ec bellard
        m->m_data -= sizeof(struct udpiphdr);
270 f0cbd3ec bellard
        m->m_len += sizeof(struct udpiphdr);
271 f0cbd3ec bellard
        
272 f0cbd3ec bellard
        /*
273 f0cbd3ec bellard
         * Fill in mbuf with extended UDP header
274 f0cbd3ec bellard
         * and addresses and length put into network format.
275 f0cbd3ec bellard
         */
276 f0cbd3ec bellard
        ui = mtod(m, struct udpiphdr *);
277 f0cbd3ec bellard
        ui->ui_next = ui->ui_prev = 0;
278 f0cbd3ec bellard
        ui->ui_x1 = 0;
279 f0cbd3ec bellard
        ui->ui_pr = IPPROTO_UDP;
280 f0cbd3ec bellard
        ui->ui_len = htons(m->m_len - sizeof(struct ip)); /* + sizeof (struct udphdr)); */
281 f0cbd3ec bellard
        /* XXXXX Check for from-one-location sockets, or from-any-location sockets */
282 f0cbd3ec bellard
        ui->ui_src = saddr->sin_addr;
283 f0cbd3ec bellard
        ui->ui_dst = daddr->sin_addr;
284 f0cbd3ec bellard
        ui->ui_sport = saddr->sin_port;
285 f0cbd3ec bellard
        ui->ui_dport = daddr->sin_port;
286 f0cbd3ec bellard
        ui->ui_ulen = ui->ui_len;
287 f0cbd3ec bellard
288 f0cbd3ec bellard
        /*
289 f0cbd3ec bellard
         * Stuff checksum and output datagram.
290 f0cbd3ec bellard
         */
291 f0cbd3ec bellard
        ui->ui_sum = 0;
292 f0cbd3ec bellard
        if (udpcksum) {
293 f0cbd3ec bellard
            if ((ui->ui_sum = cksum(m, /* sizeof (struct udpiphdr) + */ m->m_len)) == 0)
294 f0cbd3ec bellard
                ui->ui_sum = 0xffff;
295 f0cbd3ec bellard
        }
296 f0cbd3ec bellard
        ((struct ip *)ui)->ip_len = m->m_len;
297 f0cbd3ec bellard
298 f0cbd3ec bellard
        ((struct ip *)ui)->ip_ttl = ip_defttl;
299 f0cbd3ec bellard
        ((struct ip *)ui)->ip_tos = iptos;
300 f0cbd3ec bellard
        
301 f0cbd3ec bellard
        udpstat.udps_opackets++;
302 f0cbd3ec bellard
        
303 f0cbd3ec bellard
        error = ip_output(so, m);
304 f0cbd3ec bellard
        
305 f0cbd3ec bellard
        return (error);
306 f0cbd3ec bellard
}
307 f0cbd3ec bellard
308 f0cbd3ec bellard
int udp_output(struct socket *so, struct mbuf *m, 
309 f0cbd3ec bellard
               struct sockaddr_in *addr)
310 f0cbd3ec bellard
311 f0cbd3ec bellard
{
312 f0cbd3ec bellard
    struct sockaddr_in saddr, daddr;
313 f0cbd3ec bellard
314 f0cbd3ec bellard
    saddr = *addr;
315 f0cbd3ec bellard
    if ((so->so_faddr.s_addr & htonl(0xffffff00)) == special_addr.s_addr)
316 f0cbd3ec bellard
        saddr.sin_addr.s_addr = so->so_faddr.s_addr;
317 f0cbd3ec bellard
    daddr.sin_addr = so->so_laddr;
318 f0cbd3ec bellard
    daddr.sin_port = so->so_lport;
319 f0cbd3ec bellard
    
320 f0cbd3ec bellard
    return udp_output2(so, m, &saddr, &daddr, so->so_iptos);
321 f0cbd3ec bellard
}
322 f0cbd3ec bellard
323 f0cbd3ec bellard
int
324 f0cbd3ec bellard
udp_attach(so)
325 f0cbd3ec bellard
     struct socket *so;
326 f0cbd3ec bellard
{
327 f0cbd3ec bellard
  struct sockaddr_in addr;
328 f0cbd3ec bellard
        
329 f0cbd3ec bellard
  if((so->s = socket(AF_INET,SOCK_DGRAM,0)) != -1) {
330 f0cbd3ec bellard
    /*
331 f0cbd3ec bellard
     * Here, we bind() the socket.  Although not really needed
332 f0cbd3ec bellard
     * (sendto() on an unbound socket will bind it), it's done
333 f0cbd3ec bellard
     * here so that emulation of ytalk etc. don't have to do it
334 f0cbd3ec bellard
     */
335 f0cbd3ec bellard
    addr.sin_family = AF_INET;
336 f0cbd3ec bellard
    addr.sin_port = 0;
337 f0cbd3ec bellard
    addr.sin_addr.s_addr = INADDR_ANY;
338 f0cbd3ec bellard
    if(bind(so->s, (struct sockaddr *)&addr, sizeof(addr))<0) {
339 f0cbd3ec bellard
      int lasterrno=errno;
340 379ff53d bellard
      closesocket(so->s);
341 f0cbd3ec bellard
      so->s=-1;
342 02d2c54c bellard
#ifdef _WIN32
343 02d2c54c bellard
      WSASetLastError(lasterrno);
344 02d2c54c bellard
#else
345 f0cbd3ec bellard
      errno=lasterrno;
346 02d2c54c bellard
#endif
347 f0cbd3ec bellard
    } else {
348 f0cbd3ec bellard
      /* success, insert in queue */
349 f0cbd3ec bellard
      so->so_expire = curtime + SO_EXPIRE;
350 f0cbd3ec bellard
      insque(so,&udb);
351 f0cbd3ec bellard
    }
352 f0cbd3ec bellard
  }
353 f0cbd3ec bellard
  return(so->s);
354 f0cbd3ec bellard
}
355 f0cbd3ec bellard
356 f0cbd3ec bellard
void
357 f0cbd3ec bellard
udp_detach(so)
358 f0cbd3ec bellard
        struct socket *so;
359 f0cbd3ec bellard
{
360 379ff53d bellard
        closesocket(so->s);
361 f0cbd3ec bellard
        /* if (so->so_m) m_free(so->so_m);    done by sofree */
362 f0cbd3ec bellard
363 f0cbd3ec bellard
        sofree(so);
364 f0cbd3ec bellard
}
365 f0cbd3ec bellard
366 f0cbd3ec bellard
struct tos_t udptos[] = {
367 f0cbd3ec bellard
        {0, 53, IPTOS_LOWDELAY, 0},                        /* DNS */
368 f0cbd3ec bellard
        {517, 517, IPTOS_LOWDELAY, EMU_TALK},        /* talk */
369 f0cbd3ec bellard
        {518, 518, IPTOS_LOWDELAY, EMU_NTALK},        /* ntalk */
370 f0cbd3ec bellard
        {0, 7648, IPTOS_LOWDELAY, EMU_CUSEEME},        /* Cu-Seeme */
371 f0cbd3ec bellard
        {0, 0, 0, 0}
372 f0cbd3ec bellard
};
373 f0cbd3ec bellard
374 f0cbd3ec bellard
u_int8_t
375 f0cbd3ec bellard
udp_tos(so)
376 f0cbd3ec bellard
        struct socket *so;
377 f0cbd3ec bellard
{
378 f0cbd3ec bellard
        int i = 0;
379 f0cbd3ec bellard
        
380 f0cbd3ec bellard
        while(udptos[i].tos) {
381 f0cbd3ec bellard
                if ((udptos[i].fport && ntohs(so->so_fport) == udptos[i].fport) ||
382 f0cbd3ec bellard
                    (udptos[i].lport && ntohs(so->so_lport) == udptos[i].lport)) {
383 f0cbd3ec bellard
                            so->so_emu = udptos[i].emu;
384 f0cbd3ec bellard
                        return udptos[i].tos;
385 f0cbd3ec bellard
                }
386 f0cbd3ec bellard
                i++;
387 f0cbd3ec bellard
        }
388 f0cbd3ec bellard
        
389 f0cbd3ec bellard
        return 0;
390 f0cbd3ec bellard
}
391 f0cbd3ec bellard
392 f0cbd3ec bellard
#ifdef EMULATE_TALK
393 f0cbd3ec bellard
#include "talkd.h"
394 f0cbd3ec bellard
#endif
395 f0cbd3ec bellard
396 f0cbd3ec bellard
/*
397 f0cbd3ec bellard
 * Here, talk/ytalk/ntalk requests must be emulated
398 f0cbd3ec bellard
 */
399 f0cbd3ec bellard
void
400 f0cbd3ec bellard
udp_emu(so, m)
401 f0cbd3ec bellard
        struct socket *so;
402 f0cbd3ec bellard
        struct mbuf *m;
403 f0cbd3ec bellard
{
404 f0cbd3ec bellard
        struct sockaddr_in addr;
405 f0cbd3ec bellard
        int addrlen = sizeof(addr);
406 f0cbd3ec bellard
#ifdef EMULATE_TALK
407 f0cbd3ec bellard
        CTL_MSG_OLD *omsg;
408 f0cbd3ec bellard
        CTL_MSG *nmsg;
409 f0cbd3ec bellard
        char buff[sizeof(CTL_MSG)];
410 f0cbd3ec bellard
        u_char type;
411 f0cbd3ec bellard
        
412 f0cbd3ec bellard
struct talk_request {
413 f0cbd3ec bellard
        struct talk_request *next;
414 f0cbd3ec bellard
        struct socket *udp_so;
415 f0cbd3ec bellard
        struct socket *tcp_so;
416 f0cbd3ec bellard
} *req;
417 f0cbd3ec bellard
        
418 f0cbd3ec bellard
        static struct talk_request *req_tbl = 0;        
419 f0cbd3ec bellard
        
420 f0cbd3ec bellard
#endif
421 f0cbd3ec bellard
        
422 f0cbd3ec bellard
struct cu_header {
423 f0cbd3ec bellard
        char         dest[8];
424 f0cbd3ec bellard
        short         family;
425 f0cbd3ec bellard
        u_short        port;
426 f0cbd3ec bellard
        u_long        addr;
427 f0cbd3ec bellard
} *cu_head;
428 f0cbd3ec bellard
429 f0cbd3ec bellard
        switch(so->so_emu) {
430 f0cbd3ec bellard
431 f0cbd3ec bellard
#ifdef EMULATE_TALK
432 f0cbd3ec bellard
         case EMU_TALK:
433 f0cbd3ec bellard
         case EMU_NTALK:
434 f0cbd3ec bellard
                /*
435 f0cbd3ec bellard
                 * Talk emulation. We always change the ctl_addr to get
436 f0cbd3ec bellard
                 * some answers from the daemon. When an ANNOUNCE comes,
437 f0cbd3ec bellard
                 * we send LEAVE_INVITE to the local daemons. Also when a
438 f0cbd3ec bellard
                 * DELETE comes, we send copies to the local daemons.
439 f0cbd3ec bellard
                 */
440 f0cbd3ec bellard
                if (getsockname(so->s, (struct sockaddr *)&addr, &addrlen) < 0)
441 f0cbd3ec bellard
                        return;
442 f0cbd3ec bellard
                
443 f0cbd3ec bellard
#define        IS_OLD        (so->so_emu == EMU_TALK)
444 f0cbd3ec bellard
445 f0cbd3ec bellard
#define COPY_MSG(dest, src) { dest->type = src->type; \
446 f0cbd3ec bellard
                              dest->id_num = src->id_num; \
447 f0cbd3ec bellard
                              dest->pid = src->pid; \
448 f0cbd3ec bellard
                              dest->addr = src->addr; \
449 f0cbd3ec bellard
                              dest->ctl_addr = src->ctl_addr; \
450 f0cbd3ec bellard
                              memcpy(&dest->l_name, &src->l_name, NAME_SIZE_OLD); \
451 f0cbd3ec bellard
                              memcpy(&dest->r_name, &src->r_name, NAME_SIZE_OLD); \
452 f0cbd3ec bellard
                               memcpy(&dest->r_tty, &src->r_tty, TTY_SIZE); }
453 f0cbd3ec bellard
454 f0cbd3ec bellard
#define OTOSIN(ptr, field) ((struct sockaddr_in *)&ptr->field)
455 f0cbd3ec bellard
/* old_sockaddr to sockaddr_in */
456 f0cbd3ec bellard
457 f0cbd3ec bellard
458 f0cbd3ec bellard
                if (IS_OLD) {                  /* old talk */
459 f0cbd3ec bellard
                        omsg = mtod(m, CTL_MSG_OLD*);
460 f0cbd3ec bellard
                        nmsg = (CTL_MSG *) buff;
461 f0cbd3ec bellard
                        type = omsg->type;
462 f0cbd3ec bellard
                        OTOSIN(omsg, ctl_addr)->sin_port = addr.sin_port;
463 f0cbd3ec bellard
                        OTOSIN(omsg, ctl_addr)->sin_addr = our_addr;
464 f0cbd3ec bellard
                        strncpy(omsg->l_name, getlogin(), NAME_SIZE_OLD);
465 f0cbd3ec bellard
                } else {                /* new talk */        
466 f0cbd3ec bellard
                        omsg = (CTL_MSG_OLD *) buff;
467 f0cbd3ec bellard
                        nmsg = mtod(m, CTL_MSG *);
468 f0cbd3ec bellard
                        type = nmsg->type;
469 f0cbd3ec bellard
                        OTOSIN(nmsg, ctl_addr)->sin_port = addr.sin_port;
470 f0cbd3ec bellard
                        OTOSIN(nmsg, ctl_addr)->sin_addr = our_addr;
471 f0cbd3ec bellard
                        strncpy(nmsg->l_name, getlogin(), NAME_SIZE_OLD);
472 f0cbd3ec bellard
                }
473 f0cbd3ec bellard
                
474 f0cbd3ec bellard
                if (type == LOOK_UP) 
475 f0cbd3ec bellard
                        return;                /* for LOOK_UP this is enough */
476 f0cbd3ec bellard
                        
477 f0cbd3ec bellard
                if (IS_OLD) {                /* make a copy of the message */
478 f0cbd3ec bellard
                        COPY_MSG(nmsg, omsg);
479 f0cbd3ec bellard
                        nmsg->vers = 1;
480 f0cbd3ec bellard
                        nmsg->answer = 0;
481 f0cbd3ec bellard
                } else
482 f0cbd3ec bellard
                        COPY_MSG(omsg, nmsg);
483 f0cbd3ec bellard
484 f0cbd3ec bellard
                /*
485 f0cbd3ec bellard
                 * If if is an ANNOUNCE message, we go through the
486 f0cbd3ec bellard
                 * request table to see if a tcp port has already
487 f0cbd3ec bellard
                 * been redirected for this socket. If not, we solisten()
488 f0cbd3ec bellard
                 * a new socket and add this entry to the table.
489 f0cbd3ec bellard
                 * The port number of the tcp socket and our IP
490 f0cbd3ec bellard
                 * are put to the addr field of the message structures.
491 f0cbd3ec bellard
                 * Then a LEAVE_INVITE is sent to both local daemon
492 f0cbd3ec bellard
                 * ports, 517 and 518. This is why we have two copies
493 f0cbd3ec bellard
                 * of the message, one in old talk and one in new talk
494 f0cbd3ec bellard
                 * format.
495 f0cbd3ec bellard
                 */ 
496 f0cbd3ec bellard
497 f0cbd3ec bellard
                if (type == ANNOUNCE) {
498 f0cbd3ec bellard
                        int s;
499 f0cbd3ec bellard
                        u_short temp_port;
500 f0cbd3ec bellard
                        
501 f0cbd3ec bellard
                        for(req = req_tbl; req; req = req->next)
502 f0cbd3ec bellard
                                if (so == req->udp_so)
503 f0cbd3ec bellard
                                        break;          /* found it */
504 f0cbd3ec bellard
                                        
505 f0cbd3ec bellard
                        if (!req) {        /* no entry for so, create new */
506 f0cbd3ec bellard
                                req = (struct talk_request *)
507 f0cbd3ec bellard
                                        malloc(sizeof(struct talk_request));
508 f0cbd3ec bellard
                                req->udp_so = so;
509 f0cbd3ec bellard
                                req->tcp_so = solisten(0,                
510 f0cbd3ec bellard
                                        OTOSIN(omsg, addr)->sin_addr.s_addr,        
511 f0cbd3ec bellard
                                        OTOSIN(omsg, addr)->sin_port,
512 f0cbd3ec bellard
                                        SS_FACCEPTONCE);
513 f0cbd3ec bellard
                                req->next = req_tbl;
514 f0cbd3ec bellard
                                req_tbl = req;
515 f0cbd3ec bellard
                        }                        
516 f0cbd3ec bellard
                        
517 f0cbd3ec bellard
                        /* replace port number in addr field */
518 f0cbd3ec bellard
                        addrlen = sizeof(addr);
519 f0cbd3ec bellard
                        getsockname(req->tcp_so->s, 
520 f0cbd3ec bellard
                                        (struct sockaddr *) &addr,
521 f0cbd3ec bellard
                                        &addrlen);                
522 f0cbd3ec bellard
                        OTOSIN(omsg, addr)->sin_port = addr.sin_port;
523 f0cbd3ec bellard
                        OTOSIN(omsg, addr)->sin_addr = our_addr;
524 f0cbd3ec bellard
                        OTOSIN(nmsg, addr)->sin_port = addr.sin_port;
525 f0cbd3ec bellard
                        OTOSIN(nmsg, addr)->sin_addr = our_addr;                
526 f0cbd3ec bellard
                        
527 f0cbd3ec bellard
                        /* send LEAVE_INVITEs */
528 f0cbd3ec bellard
                        temp_port = OTOSIN(omsg, ctl_addr)->sin_port;
529 f0cbd3ec bellard
                        OTOSIN(omsg, ctl_addr)->sin_port = 0;
530 f0cbd3ec bellard
                        OTOSIN(nmsg, ctl_addr)->sin_port = 0;
531 f0cbd3ec bellard
                        omsg->type = nmsg->type = LEAVE_INVITE;                        
532 f0cbd3ec bellard
                        
533 f0cbd3ec bellard
                        s = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
534 f0cbd3ec bellard
                        addr.sin_addr = our_addr;
535 f0cbd3ec bellard
                        addr.sin_family = AF_INET;
536 f0cbd3ec bellard
                        addr.sin_port = htons(517);
537 f0cbd3ec bellard
                        sendto(s, (char *)omsg, sizeof(*omsg), 0, 
538 f0cbd3ec bellard
                                (struct sockaddr *)&addr, sizeof(addr));
539 f0cbd3ec bellard
                        addr.sin_port = htons(518);
540 f0cbd3ec bellard
                        sendto(s, (char *)nmsg, sizeof(*nmsg), 0,
541 f0cbd3ec bellard
                                (struct sockaddr *) &addr, sizeof(addr));
542 379ff53d bellard
                        closesocket(s) ;
543 f0cbd3ec bellard
544 f0cbd3ec bellard
                        omsg->type = nmsg->type = ANNOUNCE; 
545 f0cbd3ec bellard
                        OTOSIN(omsg, ctl_addr)->sin_port = temp_port;
546 f0cbd3ec bellard
                        OTOSIN(nmsg, ctl_addr)->sin_port = temp_port;
547 f0cbd3ec bellard
                }
548 f0cbd3ec bellard
                
549 f0cbd3ec bellard
                /*        
550 f0cbd3ec bellard
                 * If it is a DELETE message, we send a copy to the
551 f0cbd3ec bellard
                 * local daemons. Then we delete the entry corresponding
552 f0cbd3ec bellard
                 * to our socket from the request table.
553 f0cbd3ec bellard
                 */
554 f0cbd3ec bellard
                
555 f0cbd3ec bellard
                if (type == DELETE) {
556 f0cbd3ec bellard
                        struct talk_request *temp_req, *req_next;
557 f0cbd3ec bellard
                        int s;
558 f0cbd3ec bellard
                        u_short temp_port;
559 f0cbd3ec bellard
                        
560 f0cbd3ec bellard
                        temp_port = OTOSIN(omsg, ctl_addr)->sin_port;
561 f0cbd3ec bellard
                        OTOSIN(omsg, ctl_addr)->sin_port = 0;
562 f0cbd3ec bellard
                        OTOSIN(nmsg, ctl_addr)->sin_port = 0;
563 f0cbd3ec bellard
                        
564 f0cbd3ec bellard
                        s = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
565 f0cbd3ec bellard
                        addr.sin_addr = our_addr;
566 f0cbd3ec bellard
                        addr.sin_family = AF_INET;
567 f0cbd3ec bellard
                        addr.sin_port = htons(517);
568 f0cbd3ec bellard
                        sendto(s, (char *)omsg, sizeof(*omsg), 0,
569 f0cbd3ec bellard
                                (struct sockaddr *)&addr, sizeof(addr));
570 f0cbd3ec bellard
                        addr.sin_port = htons(518);
571 f0cbd3ec bellard
                        sendto(s, (char *)nmsg, sizeof(*nmsg), 0,
572 f0cbd3ec bellard
                                (struct sockaddr *)&addr, sizeof(addr));
573 379ff53d bellard
                        closesocket(s);
574 f0cbd3ec bellard
                        
575 f0cbd3ec bellard
                        OTOSIN(omsg, ctl_addr)->sin_port = temp_port;
576 f0cbd3ec bellard
                        OTOSIN(nmsg, ctl_addr)->sin_port = temp_port;
577 f0cbd3ec bellard
578 f0cbd3ec bellard
                        /* delete table entry */
579 f0cbd3ec bellard
                        if (so == req_tbl->udp_so) {
580 f0cbd3ec bellard
                                temp_req = req_tbl;
581 f0cbd3ec bellard
                                req_tbl = req_tbl->next;
582 f0cbd3ec bellard
                                free(temp_req);
583 f0cbd3ec bellard
                        } else {
584 f0cbd3ec bellard
                                temp_req = req_tbl;
585 f0cbd3ec bellard
                                for(req = req_tbl->next; req; req = req_next) {
586 f0cbd3ec bellard
                                        req_next = req->next;
587 f0cbd3ec bellard
                                        if (so == req->udp_so) {
588 f0cbd3ec bellard
                                                temp_req->next = req_next;
589 f0cbd3ec bellard
                                                free(req);
590 f0cbd3ec bellard
                                                break;
591 f0cbd3ec bellard
                                        } else {
592 f0cbd3ec bellard
                                                temp_req = req;
593 f0cbd3ec bellard
                                        }
594 f0cbd3ec bellard
                                }
595 f0cbd3ec bellard
                        }
596 f0cbd3ec bellard
                }
597 f0cbd3ec bellard
                
598 f0cbd3ec bellard
                return;                
599 f0cbd3ec bellard
#endif
600 f0cbd3ec bellard
                
601 f0cbd3ec bellard
        case EMU_CUSEEME:
602 f0cbd3ec bellard
        
603 f0cbd3ec bellard
                /*
604 f0cbd3ec bellard
                 * Cu-SeeMe emulation.
605 f0cbd3ec bellard
                 * Hopefully the packet is more that 16 bytes long. We don't
606 f0cbd3ec bellard
                 * do any other tests, just replace the address and port
607 f0cbd3ec bellard
                 * fields.
608 f0cbd3ec bellard
                 */ 
609 f0cbd3ec bellard
                if (m->m_len >= sizeof (*cu_head)) {
610 f0cbd3ec bellard
                        if (getsockname(so->s, (struct sockaddr *)&addr, &addrlen) < 0)
611 f0cbd3ec bellard
                                return;
612 f0cbd3ec bellard
                        cu_head = mtod(m, struct cu_header *);
613 f0cbd3ec bellard
                        cu_head->port = addr.sin_port;
614 f0cbd3ec bellard
                        cu_head->addr = (u_long) our_addr.s_addr;
615 f0cbd3ec bellard
                }
616 f0cbd3ec bellard
                
617 f0cbd3ec bellard
                return;
618 f0cbd3ec bellard
        }
619 f0cbd3ec bellard
}
620 f0cbd3ec bellard
621 f0cbd3ec bellard
struct socket *
622 f0cbd3ec bellard
udp_listen(port, laddr, lport, flags)
623 f0cbd3ec bellard
        u_int port;
624 f0cbd3ec bellard
        u_int32_t laddr;
625 f0cbd3ec bellard
        u_int lport;
626 f0cbd3ec bellard
        int flags;
627 f0cbd3ec bellard
{
628 f0cbd3ec bellard
        struct sockaddr_in addr;
629 f0cbd3ec bellard
        struct socket *so;
630 f0cbd3ec bellard
        int addrlen = sizeof(struct sockaddr_in), opt = 1;
631 f0cbd3ec bellard
        
632 f0cbd3ec bellard
        if ((so = socreate()) == NULL) {
633 f0cbd3ec bellard
                free(so);
634 f0cbd3ec bellard
                return NULL;
635 f0cbd3ec bellard
        }
636 f0cbd3ec bellard
        so->s = socket(AF_INET,SOCK_DGRAM,0);
637 f0cbd3ec bellard
        so->so_expire = curtime + SO_EXPIRE;
638 f0cbd3ec bellard
        insque(so,&udb);
639 f0cbd3ec bellard
640 f0cbd3ec bellard
        addr.sin_family = AF_INET;
641 f0cbd3ec bellard
        addr.sin_addr.s_addr = INADDR_ANY;
642 f0cbd3ec bellard
        addr.sin_port = port;
643 f0cbd3ec bellard
644 f0cbd3ec bellard
        if (bind(so->s,(struct sockaddr *)&addr, addrlen) < 0) {
645 f0cbd3ec bellard
                udp_detach(so);
646 f0cbd3ec bellard
                return NULL;
647 f0cbd3ec bellard
        }
648 f0cbd3ec bellard
        setsockopt(so->s,SOL_SOCKET,SO_REUSEADDR,(char *)&opt,sizeof(int));
649 f0cbd3ec bellard
/*        setsockopt(so->s,SOL_SOCKET,SO_OOBINLINE,(char *)&opt,sizeof(int)); */
650 f0cbd3ec bellard
        
651 f0cbd3ec bellard
        getsockname(so->s,(struct sockaddr *)&addr,&addrlen);
652 f0cbd3ec bellard
        so->so_fport = addr.sin_port;
653 f0cbd3ec bellard
        if (addr.sin_addr.s_addr == 0 || addr.sin_addr.s_addr == loopback_addr.s_addr)
654 f0cbd3ec bellard
           so->so_faddr = our_addr;
655 f0cbd3ec bellard
        else
656 f0cbd3ec bellard
           so->so_faddr = addr.sin_addr;
657 f0cbd3ec bellard
        
658 f0cbd3ec bellard
        so->so_lport = lport;
659 f0cbd3ec bellard
        so->so_laddr.s_addr = laddr;
660 f0cbd3ec bellard
        if (flags != SS_FACCEPTONCE)
661 f0cbd3ec bellard
           so->so_expire = 0;
662 f0cbd3ec bellard
        
663 f0cbd3ec bellard
        so->so_state = SS_ISFCONNECTED;
664 f0cbd3ec bellard
        
665 f0cbd3ec bellard
        return so;
666 f0cbd3ec bellard
}