]> git.saurik.com Git - apple/network_cmds.git/blob - alias/alias_cuseeme.c
cb7478abf5f44aeaab08364cb06e73367d9d5875
[apple/network_cmds.git] / alias / alias_cuseeme.c
1 /*
2 * Copyright (c) 2000-2002 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23 /*-
24 * Copyright (c) 1998 Brian Somers <brian@Awfulhak.org>
25 * with the aid of code written by
26 * Junichi SATOH <junichi@astec.co.jp> 1996, 1997.
27 * All rights reserved.
28 *
29 * Redistribution and use in source and binary forms, with or without
30 * modification, are permitted provided that the following conditions
31 * are met:
32 * 1. Redistributions of source code must retain the above copyright
33 * notice, this list of conditions and the following disclaimer.
34 * 2. Redistributions in binary form must reproduce the above copyright
35 * notice, this list of conditions and the following disclaimer in the
36 * documentation and/or other materials provided with the distribution.
37 *
38 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
39 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
40 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
41 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
42 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
43 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
44 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
45 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
46 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
47 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48 * SUCH DAMAGE.
49 *
50 * Based upon:
51 * $FreeBSD: src/lib/libalias/alias_cuseeme.c,v 1.2.2.2 2000/10/31 08:48:21 ru Exp $
52 */
53
54 #include <sys/types.h>
55 #include <netinet/in_systm.h>
56 #include <netinet/in.h>
57 #include <netinet/ip.h>
58 #include <netinet/udp.h>
59
60 #include "alias_local.h"
61
62 /* CU-SeeMe Data Header */
63 struct cu_header {
64 u_int16_t dest_family;
65 u_int16_t dest_port;
66 u_int32_t dest_addr;
67 int16_t family;
68 u_int16_t port;
69 u_int32_t addr;
70 u_int32_t seq;
71 u_int16_t msg;
72 u_int16_t data_type;
73 u_int16_t packet_len;
74 };
75
76 /* Open Continue Header */
77 struct oc_header {
78 u_int16_t client_count; /* Number of client info structs */
79 u_int32_t seq_no;
80 char user_name[20];
81 char reserved[4]; /* flags, version stuff, etc */
82 };
83
84 /* client info structures */
85 struct client_info {
86 u_int32_t address; /* Client address */
87 char reserved[8]; /* Flags, pruning bitfield, packet counts etc */
88 };
89
90 void
91 AliasHandleCUSeeMeOut(struct ip *pip, struct alias_link *link)
92 {
93 struct udphdr *ud;
94
95 ud = (struct udphdr *)((char *)pip + (pip->ip_hl << 2));
96 if (ntohs(ud->uh_ulen) - sizeof(struct udphdr) >= sizeof(struct cu_header)) {
97 struct cu_header *cu;
98 struct alias_link *cu_link;
99
100 cu = (struct cu_header *)(ud + 1);
101 if (cu->addr)
102 cu->addr = (u_int32_t)GetAliasAddress(link).s_addr;
103
104 cu_link = FindUdpTcpOut(pip->ip_src, GetDestAddress(link),
105 ud->uh_dport, 0, IPPROTO_UDP, 1);
106
107 #ifndef NO_FW_PUNCH
108 if (cu_link)
109 PunchFWHole(cu_link);
110 #endif
111 }
112 }
113
114 void
115 AliasHandleCUSeeMeIn(struct ip *pip, struct in_addr original_addr)
116 {
117 struct in_addr alias_addr;
118 struct udphdr *ud;
119 struct cu_header *cu;
120 struct oc_header *oc;
121 struct client_info *ci;
122 char *end;
123 int i;
124
125 alias_addr.s_addr = pip->ip_dst.s_addr;
126 ud = (struct udphdr *)((char *)pip + (pip->ip_hl << 2));
127 cu = (struct cu_header *)(ud + 1);
128 oc = (struct oc_header *)(cu + 1);
129 ci = (struct client_info *)(oc + 1);
130 end = (char *)ud + ntohs(ud->uh_ulen);
131
132 if ((char *)oc <= end) {
133 if(cu->dest_addr)
134 cu->dest_addr = (u_int32_t)original_addr.s_addr;
135 if(ntohs(cu->data_type) == 101)
136 /* Find and change our address */
137 for(i = 0; (char *)(ci + 1) <= end && i < oc->client_count; i++, ci++)
138 if(ci->address == (u_int32_t)alias_addr.s_addr) {
139 ci->address = (u_int32_t)original_addr.s_addr;
140 break;
141 }
142 }
143 }