Statistics
| Branch: | Revision:

root / slirp / udp.c @ f0cbd3ec

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