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