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