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