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