]> Git Repo - qemu.git/blame - slirp/udp.c
slirp: Drop link_up checks from if_output and slirp_socket_can_recv
[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
f0cbd3ec
FB
44struct socket udb;
45
9634d903
BS
46static u_int8_t udp_tos(struct socket *so);
47static void udp_emu(struct socket *so, struct mbuf *m);
48
f0cbd3ec
FB
49struct socket *udp_last_so = &udb;
50
51void
aeed97c4 52udp_init(void)
f0cbd3ec
FB
53{
54 udb.so_next = udb.so_prev = &udb;
55}
5fafdf24
TS
56/* m->m_data points at ip packet header
57 * m->m_len length ip packet
f0cbd3ec
FB
58 * ip->ip_len length data (IPDU)
59 */
60void
aeed97c4 61udp_input(register struct mbuf *m, int iphlen)
f0cbd3ec
FB
62{
63 register struct ip *ip;
64 register struct udphdr *uh;
f0cbd3ec 65 int len;
5fafdf24 66 struct ip save_ip;
f0cbd3ec 67 struct socket *so;
5fafdf24 68
f0cbd3ec
FB
69 DEBUG_CALL("udp_input");
70 DEBUG_ARG("m = %lx", (long)m);
71 DEBUG_ARG("iphlen = %d", iphlen);
5fafdf24 72
f0cbd3ec
FB
73 /*
74 * Strip IP options, if any; should skip this,
75 * make available to user, and use on returned packets,
76 * but we don't yet have a way to check the checksum
77 * with options still present.
78 */
79 if(iphlen > sizeof(struct ip)) {
80 ip_stripoptions(m, (struct mbuf *)0);
81 iphlen = sizeof(struct ip);
82 }
83
84 /*
85 * Get IP and UDP header together in first mbuf.
86 */
87 ip = mtod(m, struct ip *);
88 uh = (struct udphdr *)((caddr_t)ip + iphlen);
89
90 /*
91 * Make mbuf data length reflect UDP length.
92 * If not enough data to reflect UDP length, drop.
93 */
94 len = ntohs((u_int16_t)uh->uh_ulen);
95
96 if (ip->ip_len != len) {
97 if (len > ip->ip_len) {
f0cbd3ec
FB
98 goto bad;
99 }
100 m_adj(m, len - ip->ip_len);
101 ip->ip_len = len;
102 }
5fafdf24 103
f0cbd3ec
FB
104 /*
105 * Save a copy of the IP header in case we want restore it
106 * for sending an ICMP error message in response.
107 */
5fafdf24 108 save_ip = *ip;
f0cbd3ec
FB
109 save_ip.ip_len+= iphlen; /* tcp_input subtracts this */
110
111 /*
112 * Checksum extended UDP header and data.
113 */
0d62c4cf 114 if (uh->uh_sum) {
429d0a3d 115 memset(&((struct ipovly *)ip)->ih_mbuf, 0, sizeof(struct mbuf_ptr));
f0cbd3ec
FB
116 ((struct ipovly *)ip)->ih_x1 = 0;
117 ((struct ipovly *)ip)->ih_len = uh->uh_ulen;
f0cbd3ec 118 if(cksum(m, len + sizeof(struct ip))) {
f0cbd3ec
FB
119 goto bad;
120 }
121 }
122
123 /*
124 * handle DHCP/BOOTP
125 */
126 if (ntohs(uh->uh_dport) == BOOTP_SERVER) {
127 bootp_input(m);
128 goto bad;
129 }
130
a9ba3a85
AL
131 if (slirp_restrict)
132 goto bad;
133
c7f74643
FB
134 /*
135 * handle TFTP
136 */
137 if (ntohs(uh->uh_dport) == TFTP_SERVER) {
138 tftp_input(m);
139 goto bad;
140 }
141
f0cbd3ec
FB
142 /*
143 * Locate pcb for datagram.
144 */
145 so = udp_last_so;
146 if (so->so_lport != uh->uh_sport ||
147 so->so_laddr.s_addr != ip->ip_src.s_addr) {
148 struct socket *tmp;
3b46e624 149
f0cbd3ec
FB
150 for (tmp = udb.so_next; tmp != &udb; tmp = tmp->so_next) {
151 if (tmp->so_lport == uh->uh_sport &&
152 tmp->so_laddr.s_addr == ip->ip_src.s_addr) {
f0cbd3ec
FB
153 so = tmp;
154 break;
155 }
156 }
157 if (tmp == &udb) {
158 so = NULL;
159 } else {
f0cbd3ec
FB
160 udp_last_so = so;
161 }
162 }
5fafdf24 163
f0cbd3ec
FB
164 if (so == NULL) {
165 /*
166 * If there's no socket for this packet,
167 * create one
168 */
169 if ((so = socreate()) == NULL) goto bad;
170 if(udp_attach(so) == -1) {
5fafdf24 171 DEBUG_MISC((dfd," udp_attach errno = %d-%s\n",
f0cbd3ec
FB
172 errno,strerror(errno)));
173 sofree(so);
174 goto bad;
175 }
3b46e624 176
f0cbd3ec
FB
177 /*
178 * Setup fields
179 */
f0cbd3ec
FB
180 so->so_laddr = ip->ip_src;
181 so->so_lport = uh->uh_sport;
3b46e624 182
f0cbd3ec
FB
183 if ((so->so_iptos = udp_tos(so)) == 0)
184 so->so_iptos = ip->ip_tos;
3b46e624 185
f0cbd3ec
FB
186 /*
187 * XXXXX Here, check if it's in udpexec_list,
188 * and if it is, do the fork_exec() etc.
189 */
190 }
191
54fd9cdf
TS
192 so->so_faddr = ip->ip_dst; /* XXX */
193 so->so_fport = uh->uh_dport; /* XXX */
194
f0cbd3ec
FB
195 iphlen += sizeof(struct udphdr);
196 m->m_len -= iphlen;
197 m->m_data += iphlen;
198
199 /*
200 * Now we sendto() the packet.
201 */
202 if (so->so_emu)
203 udp_emu(so, m);
204
205 if(sosendto(so,m) == -1) {
206 m->m_len += iphlen;
207 m->m_data -= iphlen;
208 *ip=save_ip;
209 DEBUG_MISC((dfd,"udp tx errno = %d-%s\n",errno,strerror(errno)));
3b46e624 210 icmp_error(m, ICMP_UNREACH,ICMP_UNREACH_NET, 0,strerror(errno));
f0cbd3ec
FB
211 }
212
213 m_free(so->so_m); /* used for ICMP if error on sorecvfrom */
214
215 /* restore the orig mbuf packet */
216 m->m_len += iphlen;
217 m->m_data -= iphlen;
218 *ip=save_ip;
219 so->so_m=m; /* ICMP backup */
220
221 return;
222bad:
223 m_freem(m);
f0cbd3ec
FB
224 return;
225}
226
5fafdf24 227int udp_output2(struct socket *so, struct mbuf *m,
f0cbd3ec
FB
228 struct sockaddr_in *saddr, struct sockaddr_in *daddr,
229 int iptos)
230{
231 register struct udpiphdr *ui;
232 int error = 0;
233
234 DEBUG_CALL("udp_output");
235 DEBUG_ARG("so = %lx", (long)so);
236 DEBUG_ARG("m = %lx", (long)m);
237 DEBUG_ARG("saddr = %lx", (long)saddr->sin_addr.s_addr);
238 DEBUG_ARG("daddr = %lx", (long)daddr->sin_addr.s_addr);
239
240 /*
241 * Adjust for header
242 */
243 m->m_data -= sizeof(struct udpiphdr);
244 m->m_len += sizeof(struct udpiphdr);
5fafdf24 245
f0cbd3ec
FB
246 /*
247 * Fill in mbuf with extended UDP header
248 * and addresses and length put into network format.
249 */
250 ui = mtod(m, struct udpiphdr *);
429d0a3d 251 memset(&ui->ui_i.ih_mbuf, 0 , sizeof(struct mbuf_ptr));
f0cbd3ec
FB
252 ui->ui_x1 = 0;
253 ui->ui_pr = IPPROTO_UDP;
0d62c4cf 254 ui->ui_len = htons(m->m_len - sizeof(struct ip));
f0cbd3ec
FB
255 /* XXXXX Check for from-one-location sockets, or from-any-location sockets */
256 ui->ui_src = saddr->sin_addr;
257 ui->ui_dst = daddr->sin_addr;
258 ui->ui_sport = saddr->sin_port;
259 ui->ui_dport = daddr->sin_port;
260 ui->ui_ulen = ui->ui_len;
261
262 /*
263 * Stuff checksum and output datagram.
264 */
265 ui->ui_sum = 0;
0d62c4cf 266 if ((ui->ui_sum = cksum(m, m->m_len)) == 0)
f0cbd3ec 267 ui->ui_sum = 0xffff;
f0cbd3ec
FB
268 ((struct ip *)ui)->ip_len = m->m_len;
269
9634d903 270 ((struct ip *)ui)->ip_ttl = IPDEFTTL;
f0cbd3ec 271 ((struct ip *)ui)->ip_tos = iptos;
5fafdf24 272
f0cbd3ec 273 error = ip_output(so, m);
5fafdf24 274
f0cbd3ec
FB
275 return (error);
276}
277
5fafdf24 278int udp_output(struct socket *so, struct mbuf *m,
f0cbd3ec
FB
279 struct sockaddr_in *addr)
280
281{
282 struct sockaddr_in saddr, daddr;
283
284 saddr = *addr;
a13a4126
JK
285 if ((so->so_faddr.s_addr & vnetwork_mask.s_addr) == vnetwork_addr.s_addr) {
286 if ((so->so_faddr.s_addr & ~vnetwork_mask.s_addr) ==
287 ~vnetwork_mask.s_addr) {
288 saddr.sin_addr = vhost_addr;
289 } else if (addr->sin_addr.s_addr == loopback_addr.s_addr ||
290 so->so_faddr.s_addr != vhost_addr.s_addr) {
291 saddr.sin_addr = so->so_faddr;
292 }
c904d61f 293 }
f0cbd3ec
FB
294 daddr.sin_addr = so->so_laddr;
295 daddr.sin_port = so->so_lport;
3b46e624 296
f0cbd3ec
FB
297 return udp_output2(so, m, &saddr, &daddr, so->so_iptos);
298}
299
300int
aeed97c4 301udp_attach(struct socket *so)
f0cbd3ec
FB
302{
303 struct sockaddr_in addr;
5fafdf24 304
f0cbd3ec
FB
305 if((so->s = socket(AF_INET,SOCK_DGRAM,0)) != -1) {
306 /*
307 * Here, we bind() the socket. Although not really needed
308 * (sendto() on an unbound socket will bind it), it's done
309 * here so that emulation of ytalk etc. don't have to do it
310 */
311 addr.sin_family = AF_INET;
312 addr.sin_port = 0;
313 addr.sin_addr.s_addr = INADDR_ANY;
314 if(bind(so->s, (struct sockaddr *)&addr, sizeof(addr))<0) {
315 int lasterrno=errno;
379ff53d 316 closesocket(so->s);
f0cbd3ec 317 so->s=-1;
02d2c54c
FB
318#ifdef _WIN32
319 WSASetLastError(lasterrno);
320#else
f0cbd3ec 321 errno=lasterrno;
02d2c54c 322#endif
f0cbd3ec
FB
323 } else {
324 /* success, insert in queue */
325 so->so_expire = curtime + SO_EXPIRE;
326 insque(so,&udb);
327 }
328 }
329 return(so->s);
330}
331
332void
aeed97c4 333udp_detach(struct socket *so)
f0cbd3ec 334{
379ff53d 335 closesocket(so->s);
f0cbd3ec
FB
336 sofree(so);
337}
338
9634d903 339static const struct tos_t udptos[] = {
f0cbd3ec
FB
340 {0, 53, IPTOS_LOWDELAY, 0}, /* DNS */
341 {517, 517, IPTOS_LOWDELAY, EMU_TALK}, /* talk */
342 {518, 518, IPTOS_LOWDELAY, EMU_NTALK}, /* ntalk */
343 {0, 7648, IPTOS_LOWDELAY, EMU_CUSEEME}, /* Cu-Seeme */
344 {0, 0, 0, 0}
345};
346
9634d903
BS
347static u_int8_t
348udp_tos(struct socket *so)
f0cbd3ec
FB
349{
350 int i = 0;
5fafdf24 351
f0cbd3ec
FB
352 while(udptos[i].tos) {
353 if ((udptos[i].fport && ntohs(so->so_fport) == udptos[i].fport) ||
354 (udptos[i].lport && ntohs(so->so_lport) == udptos[i].lport)) {
355 so->so_emu = udptos[i].emu;
356 return udptos[i].tos;
357 }
358 i++;
359 }
5fafdf24 360
f0cbd3ec
FB
361 return 0;
362}
363
364#ifdef EMULATE_TALK
365#include "talkd.h"
366#endif
367
368/*
369 * Here, talk/ytalk/ntalk requests must be emulated
370 */
9634d903
BS
371static void
372udp_emu(struct socket *so, struct mbuf *m)
f0cbd3ec
FB
373{
374 struct sockaddr_in addr;
242acf3a 375 socklen_t addrlen = sizeof(addr);
f0cbd3ec
FB
376#ifdef EMULATE_TALK
377 CTL_MSG_OLD *omsg;
378 CTL_MSG *nmsg;
379 char buff[sizeof(CTL_MSG)];
380 u_char type;
5fafdf24 381
f0cbd3ec
FB
382struct talk_request {
383 struct talk_request *next;
384 struct socket *udp_so;
385 struct socket *tcp_so;
386} *req;
5fafdf24
TS
387
388 static struct talk_request *req_tbl = 0;
389
f0cbd3ec 390#endif
5fafdf24 391
f0cbd3ec 392struct cu_header {
101c5935
FB
393 uint16_t d_family; // destination family
394 uint16_t d_port; // destination port
395 uint32_t d_addr; // destination address
396 uint16_t s_family; // source family
397 uint16_t s_port; // source port
51a36cb2 398 uint32_t so_addr; // source address
101c5935
FB
399 uint32_t seqn; // sequence number
400 uint16_t message; // message
401 uint16_t data_type; // data type
402 uint16_t pkt_len; // packet length
f0cbd3ec
FB
403} *cu_head;
404
405 switch(so->so_emu) {
406
407#ifdef EMULATE_TALK
408 case EMU_TALK:
409 case EMU_NTALK:
410 /*
411 * Talk emulation. We always change the ctl_addr to get
412 * some answers from the daemon. When an ANNOUNCE comes,
413 * we send LEAVE_INVITE to the local daemons. Also when a
414 * DELETE comes, we send copies to the local daemons.
415 */
416 if (getsockname(so->s, (struct sockaddr *)&addr, &addrlen) < 0)
417 return;
3b46e624 418
f0cbd3ec
FB
419#define IS_OLD (so->so_emu == EMU_TALK)
420
421#define COPY_MSG(dest, src) { dest->type = src->type; \
422 dest->id_num = src->id_num; \
423 dest->pid = src->pid; \
424 dest->addr = src->addr; \
425 dest->ctl_addr = src->ctl_addr; \
426 memcpy(&dest->l_name, &src->l_name, NAME_SIZE_OLD); \
427 memcpy(&dest->r_name, &src->r_name, NAME_SIZE_OLD); \
428 memcpy(&dest->r_tty, &src->r_tty, TTY_SIZE); }
429
430#define OTOSIN(ptr, field) ((struct sockaddr_in *)&ptr->field)
431/* old_sockaddr to sockaddr_in */
432
433
434 if (IS_OLD) { /* old talk */
435 omsg = mtod(m, CTL_MSG_OLD*);
436 nmsg = (CTL_MSG *) buff;
437 type = omsg->type;
438 OTOSIN(omsg, ctl_addr)->sin_port = addr.sin_port;
439 OTOSIN(omsg, ctl_addr)->sin_addr = our_addr;
be15b141 440 pstrcpy(omsg->l_name, NAME_SIZE_OLD, getlogin());
5fafdf24 441 } else { /* new talk */
f0cbd3ec
FB
442 omsg = (CTL_MSG_OLD *) buff;
443 nmsg = mtod(m, CTL_MSG *);
444 type = nmsg->type;
445 OTOSIN(nmsg, ctl_addr)->sin_port = addr.sin_port;
446 OTOSIN(nmsg, ctl_addr)->sin_addr = our_addr;
be15b141 447 pstrcpy(nmsg->l_name, NAME_SIZE_OLD, getlogin());
f0cbd3ec 448 }
3b46e624 449
5fafdf24 450 if (type == LOOK_UP)
f0cbd3ec 451 return; /* for LOOK_UP this is enough */
3b46e624 452
f0cbd3ec
FB
453 if (IS_OLD) { /* make a copy of the message */
454 COPY_MSG(nmsg, omsg);
455 nmsg->vers = 1;
456 nmsg->answer = 0;
457 } else
458 COPY_MSG(omsg, nmsg);
459
460 /*
461 * If if is an ANNOUNCE message, we go through the
462 * request table to see if a tcp port has already
463 * been redirected for this socket. If not, we solisten()
464 * a new socket and add this entry to the table.
465 * The port number of the tcp socket and our IP
466 * are put to the addr field of the message structures.
467 * Then a LEAVE_INVITE is sent to both local daemon
468 * ports, 517 and 518. This is why we have two copies
469 * of the message, one in old talk and one in new talk
470 * format.
5fafdf24 471 */
f0cbd3ec
FB
472
473 if (type == ANNOUNCE) {
474 int s;
475 u_short temp_port;
3b46e624 476
f0cbd3ec
FB
477 for(req = req_tbl; req; req = req->next)
478 if (so == req->udp_so)
479 break; /* found it */
3b46e624 480
f0cbd3ec
FB
481 if (!req) { /* no entry for so, create new */
482 req = (struct talk_request *)
483 malloc(sizeof(struct talk_request));
484 req->udp_so = so;
3b46e624 485 req->tcp_so = solisten(0,
5fafdf24 486 OTOSIN(omsg, addr)->sin_addr.s_addr,
f0cbd3ec
FB
487 OTOSIN(omsg, addr)->sin_port,
488 SS_FACCEPTONCE);
489 req->next = req_tbl;
490 req_tbl = req;
3b46e624
TS
491 }
492
f0cbd3ec
FB
493 /* replace port number in addr field */
494 addrlen = sizeof(addr);
5fafdf24 495 getsockname(req->tcp_so->s,
f0cbd3ec 496 (struct sockaddr *) &addr,
3b46e624 497 &addrlen);
f0cbd3ec
FB
498 OTOSIN(omsg, addr)->sin_port = addr.sin_port;
499 OTOSIN(omsg, addr)->sin_addr = our_addr;
500 OTOSIN(nmsg, addr)->sin_port = addr.sin_port;
3b46e624
TS
501 OTOSIN(nmsg, addr)->sin_addr = our_addr;
502
f0cbd3ec
FB
503 /* send LEAVE_INVITEs */
504 temp_port = OTOSIN(omsg, ctl_addr)->sin_port;
505 OTOSIN(omsg, ctl_addr)->sin_port = 0;
506 OTOSIN(nmsg, ctl_addr)->sin_port = 0;
3b46e624
TS
507 omsg->type = nmsg->type = LEAVE_INVITE;
508
f0cbd3ec
FB
509 s = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
510 addr.sin_addr = our_addr;
511 addr.sin_family = AF_INET;
512 addr.sin_port = htons(517);
5fafdf24 513 sendto(s, (char *)omsg, sizeof(*omsg), 0,
f0cbd3ec
FB
514 (struct sockaddr *)&addr, sizeof(addr));
515 addr.sin_port = htons(518);
516 sendto(s, (char *)nmsg, sizeof(*nmsg), 0,
517 (struct sockaddr *) &addr, sizeof(addr));
379ff53d 518 closesocket(s) ;
f0cbd3ec 519
5fafdf24 520 omsg->type = nmsg->type = ANNOUNCE;
f0cbd3ec
FB
521 OTOSIN(omsg, ctl_addr)->sin_port = temp_port;
522 OTOSIN(nmsg, ctl_addr)->sin_port = temp_port;
523 }
3b46e624 524
5fafdf24 525 /*
f0cbd3ec
FB
526 * If it is a DELETE message, we send a copy to the
527 * local daemons. Then we delete the entry corresponding
528 * to our socket from the request table.
529 */
3b46e624 530
f0cbd3ec
FB
531 if (type == DELETE) {
532 struct talk_request *temp_req, *req_next;
533 int s;
534 u_short temp_port;
3b46e624 535
f0cbd3ec
FB
536 temp_port = OTOSIN(omsg, ctl_addr)->sin_port;
537 OTOSIN(omsg, ctl_addr)->sin_port = 0;
538 OTOSIN(nmsg, ctl_addr)->sin_port = 0;
3b46e624 539
f0cbd3ec
FB
540 s = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
541 addr.sin_addr = our_addr;
542 addr.sin_family = AF_INET;
543 addr.sin_port = htons(517);
544 sendto(s, (char *)omsg, sizeof(*omsg), 0,
545 (struct sockaddr *)&addr, sizeof(addr));
546 addr.sin_port = htons(518);
547 sendto(s, (char *)nmsg, sizeof(*nmsg), 0,
548 (struct sockaddr *)&addr, sizeof(addr));
379ff53d 549 closesocket(s);
3b46e624 550
f0cbd3ec
FB
551 OTOSIN(omsg, ctl_addr)->sin_port = temp_port;
552 OTOSIN(nmsg, ctl_addr)->sin_port = temp_port;
553
554 /* delete table entry */
555 if (so == req_tbl->udp_so) {
556 temp_req = req_tbl;
557 req_tbl = req_tbl->next;
558 free(temp_req);
559 } else {
560 temp_req = req_tbl;
561 for(req = req_tbl->next; req; req = req_next) {
562 req_next = req->next;
563 if (so == req->udp_so) {
564 temp_req->next = req_next;
565 free(req);
566 break;
567 } else {
568 temp_req = req;
569 }
570 }
571 }
572 }
3b46e624
TS
573
574 return;
f0cbd3ec 575#endif
3b46e624 576
5fafdf24
TS
577 case EMU_CUSEEME:
578
f0cbd3ec
FB
579 /*
580 * Cu-SeeMe emulation.
581 * Hopefully the packet is more that 16 bytes long. We don't
582 * do any other tests, just replace the address and port
583 * fields.
5fafdf24 584 */
f0cbd3ec
FB
585 if (m->m_len >= sizeof (*cu_head)) {
586 if (getsockname(so->s, (struct sockaddr *)&addr, &addrlen) < 0)
587 return;
588 cu_head = mtod(m, struct cu_header *);
101c5935 589 cu_head->s_port = addr.sin_port;
51a36cb2 590 cu_head->so_addr = our_addr.s_addr;
f0cbd3ec 591 }
3b46e624 592
f0cbd3ec
FB
593 return;
594 }
595}
596
597struct socket *
3c6a0580
JK
598udp_listen(u_int32_t haddr, u_int hport, u_int32_t laddr, u_int lport,
599 int flags)
f0cbd3ec
FB
600{
601 struct sockaddr_in addr;
602 struct socket *so;
242acf3a 603 socklen_t addrlen = sizeof(struct sockaddr_in), opt = 1;
5fafdf24 604
f0cbd3ec
FB
605 if ((so = socreate()) == NULL) {
606 free(so);
607 return NULL;
608 }
609 so->s = socket(AF_INET,SOCK_DGRAM,0);
610 so->so_expire = curtime + SO_EXPIRE;
611 insque(so,&udb);
612
613 addr.sin_family = AF_INET;
3c6a0580
JK
614 addr.sin_addr.s_addr = haddr;
615 addr.sin_port = hport;
f0cbd3ec
FB
616
617 if (bind(so->s,(struct sockaddr *)&addr, addrlen) < 0) {
618 udp_detach(so);
619 return NULL;
620 }
621 setsockopt(so->s,SOL_SOCKET,SO_REUSEADDR,(char *)&opt,sizeof(int));
5fafdf24 622
f0cbd3ec
FB
623 getsockname(so->s,(struct sockaddr *)&addr,&addrlen);
624 so->so_fport = addr.sin_port;
a13a4126
JK
625 if (addr.sin_addr.s_addr == 0 ||
626 addr.sin_addr.s_addr == loopback_addr.s_addr) {
627 so->so_faddr = vhost_addr;
628 } else {
f0cbd3ec 629 so->so_faddr = addr.sin_addr;
a13a4126 630 }
f0cbd3ec
FB
631 so->so_lport = lport;
632 so->so_laddr.s_addr = laddr;
633 if (flags != SS_FACCEPTONCE)
634 so->so_expire = 0;
5fafdf24 635
f932b6ce 636 so->so_state &= SS_PERSISTENT_MASK;
6dd5ffb6 637 so->so_state |= SS_ISFCONNECTED | flags;
5fafdf24 638
f0cbd3ec
FB
639 return so;
640}
This page took 0.389879 seconds and 4 git commands to generate.