1 /*-
2  * Copyright (c) 1998 Brian Somers <[email protected]>
3  *                    with the aid of code written by
4  *                    Junichi SATOH <[email protected]> 1996, 1997.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31 
32 #include <stdio.h>
33 #include <sys/types.h>
34 #include <netinet/in_systm.h>
35 #include <netinet/in.h>
36 #include <netinet/ip.h>
37 #include <netinet/udp.h>
38 
39 #include "alias_local.h"
40 
41 /* CU-SeeMe Data Header */
42 struct cu_header {
43 	u_int16_t	dest_family;
44 	u_int16_t	dest_port;
45 	u_int32_t	dest_addr;
46 	int16_t		family;
47 	u_int16_t	port;
48 	u_int32_t	addr;
49 	u_int32_t	seq;
50 	u_int16_t	msg;
51 	u_int16_t	data_type;
52 	u_int16_t	packet_len;
53 };
54 
55 /* Open Continue Header */
56 struct oc_header {
57 	u_int16_t	client_count;	/* Number of client info structs */
58 	u_int32_t	seq_no;
59 	char		user_name [20];
60 	char		reserved  [4];	/* flags, version stuff, etc */
61 };
62 
63 /* client info structures */
64 struct client_info {
65 	u_int32_t	address;/* Client address */
66 	char		reserved  [8];	/* Flags, pruning bitfield, packet
67 					 * counts etc */
68 };
69 
70 void
71 AliasHandleCUSeeMeOut(struct libalias *la, struct ip *pip, struct alias_link *lnk)
72 {
73 	struct udphdr *ud = ip_next(pip);
74 
75 	if (ntohs(ud->uh_ulen) - sizeof(struct udphdr) >= sizeof(struct cu_header)) {
76 		struct cu_header *cu;
77 		struct alias_link *cu_lnk;
78 
79 		cu = udp_next(ud);
80 		if (cu->addr)
81 			cu->addr = (u_int32_t) GetAliasAddress(lnk).s_addr;
82 
83 		cu_lnk = FindUdpTcpOut(la, pip->ip_src, GetDestAddress(lnk),
84 		    ud->uh_dport, 0, IPPROTO_UDP, 1);
85 
86 #ifndef NO_FW_PUNCH
87 		if (cu_lnk)
88 			PunchFWHole(cu_lnk);
89 #endif
90 	}
91 }
92 
93 void
94 AliasHandleCUSeeMeIn(struct libalias *la, struct ip *pip, struct in_addr original_addr)
95 {
96 	struct in_addr alias_addr;
97 	struct udphdr *ud;
98 	struct cu_header *cu;
99 	struct oc_header *oc;
100 	struct client_info *ci;
101 	char *end;
102 	int i;
103 
104 	(void)la;
105 	alias_addr.s_addr = pip->ip_dst.s_addr;
106 	ud = ip_next(pip);
107 	cu = udp_next(ud);
108 	oc = (struct oc_header *)(cu + 1);
109 	ci = (struct client_info *)(oc + 1);
110 	end = (char *)ud + ntohs(ud->uh_ulen);
111 
112 	if ((char *)oc <= end) {
113 		if (cu->dest_addr)
114 			cu->dest_addr = (u_int32_t) original_addr.s_addr;
115 		if (ntohs(cu->data_type) == 101)
116 			/* Find and change our address */
117 			for (i = 0; (char *)(ci + 1) <= end && i < oc->client_count; i++, ci++)
118 				if (ci->address == (u_int32_t) alias_addr.s_addr) {
119 					ci->address = (u_int32_t) original_addr.s_addr;
120 					break;
121 				}
122 	}
123 }
124