alias_cuseeme.c revision 127094
1240116Smarcel/*-
2240116Smarcel * Copyright (c) 1998 Brian Somers <brian@Awfulhak.org>
3240116Smarcel *                    with the aid of code written by
4240116Smarcel *                    Junichi SATOH <junichi@astec.co.jp> 1996, 1997.
5240116Smarcel * All rights reserved.
6240116Smarcel *
7240116Smarcel * Redistribution and use in source and binary forms, with or without
8240116Smarcel * modification, are permitted provided that the following conditions
9240116Smarcel * are met:
10240116Smarcel * 1. Redistributions of source code must retain the above copyright
11240116Smarcel *    notice, this list of conditions and the following disclaimer.
12240116Smarcel * 2. Redistributions in binary form must reproduce the above copyright
13240116Smarcel *    notice, this list of conditions and the following disclaimer in the
14240116Smarcel *    documentation and/or other materials provided with the distribution.
15240116Smarcel *
16240116Smarcel * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17240116Smarcel * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18240116Smarcel * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19240116Smarcel * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20240116Smarcel * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21240116Smarcel * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22240116Smarcel * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23240116Smarcel * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24240116Smarcel * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25240116Smarcel * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26240116Smarcel * SUCH DAMAGE.
27240116Smarcel */
28240116Smarcel
29240116Smarcel#include <sys/cdefs.h>
30240116Smarcel__FBSDID("$FreeBSD: head/sys/netinet/libalias/alias_cuseeme.c 127094 2004-03-16 21:30:41Z des $");
31240116Smarcel
32240116Smarcel#include <stdio.h>
33240116Smarcel#include <sys/types.h>
34240116Smarcel#include <netinet/in_systm.h>
35240116Smarcel#include <netinet/in.h>
36240116Smarcel#include <netinet/ip.h>
37240116Smarcel#include <netinet/udp.h>
38240116Smarcel
39240116Smarcel#include "alias_local.h"
40240116Smarcel
41240116Smarcel/* CU-SeeMe Data Header */
42240116Smarcelstruct cu_header {
43240116Smarcel	u_int16_t	dest_family;
44240116Smarcel	u_int16_t	dest_port;
45240116Smarcel	u_int32_t	dest_addr;
46240116Smarcel	int16_t		family;
47240116Smarcel	u_int16_t	port;
48240116Smarcel	u_int32_t	addr;
49240116Smarcel	u_int32_t	seq;
50240116Smarcel	u_int16_t	msg;
51240116Smarcel	u_int16_t	data_type;
52240116Smarcel	u_int16_t	packet_len;
53240116Smarcel};
54240116Smarcel
55240116Smarcel/* Open Continue Header */
56240116Smarcelstruct oc_header {
57240116Smarcel	u_int16_t	client_count;	/* Number of client info structs */
58240116Smarcel	u_int32_t	seq_no;
59240116Smarcel	char		user_name [20];
60240116Smarcel	char		reserved  [4];	/* flags, version stuff, etc */
61240116Smarcel};
62240116Smarcel
63240116Smarcel/* client info structures */
64240116Smarcelstruct client_info {
65240116Smarcel	u_int32_t	address;/* Client address */
66240116Smarcel	char		reserved  [8];	/* Flags, pruning bitfield, packet
67240116Smarcel					 * counts etc */
68240116Smarcel};
69240116Smarcel
70240116Smarcelvoid
71240116SmarcelAliasHandleCUSeeMeOut(struct libalias *la, struct ip *pip, struct alias_link *link)
72240116Smarcel{
73240116Smarcel	struct udphdr *ud;
74240116Smarcel
75240116Smarcel	ud = (struct udphdr *)((char *)pip + (pip->ip_hl << 2));
76240116Smarcel	if (ntohs(ud->uh_ulen) - sizeof(struct udphdr) >= sizeof(struct cu_header)) {
77240116Smarcel		struct cu_header *cu;
78240116Smarcel		struct alias_link *cu_link;
79240116Smarcel
80240116Smarcel		cu = (struct cu_header *)(ud + 1);
81240116Smarcel		if (cu->addr)
82240116Smarcel			cu->addr = (u_int32_t) GetAliasAddress(link).s_addr;
83240116Smarcel
84240116Smarcel		cu_link = FindUdpTcpOut(la, pip->ip_src, GetDestAddress(link),
85240116Smarcel		    ud->uh_dport, 0, IPPROTO_UDP, 1);
86240116Smarcel
87240116Smarcel#ifndef NO_FW_PUNCH
88240116Smarcel		if (cu_link)
89240116Smarcel			PunchFWHole(cu_link);
90240116Smarcel#endif
91240116Smarcel	}
92240116Smarcel}
93240116Smarcel
94240116Smarcelvoid
95240116SmarcelAliasHandleCUSeeMeIn(struct libalias *la, struct ip *pip, struct in_addr original_addr)
96240116Smarcel{
97240116Smarcel	struct in_addr alias_addr;
98240116Smarcel	struct udphdr *ud;
99240116Smarcel	struct cu_header *cu;
100240116Smarcel	struct oc_header *oc;
101240116Smarcel	struct client_info *ci;
102240116Smarcel	char *end;
103240116Smarcel	int i;
104240116Smarcel
105240116Smarcel	alias_addr.s_addr = pip->ip_dst.s_addr;
106240116Smarcel	ud = (struct udphdr *)((char *)pip + (pip->ip_hl << 2));
107240116Smarcel	cu = (struct cu_header *)(ud + 1);
108240116Smarcel	oc = (struct oc_header *)(cu + 1);
109240116Smarcel	ci = (struct client_info *)(oc + 1);
110240116Smarcel	end = (char *)ud + ntohs(ud->uh_ulen);
111240116Smarcel
112240116Smarcel	if ((char *)oc <= end) {
113240116Smarcel		if (cu->dest_addr)
114240116Smarcel			cu->dest_addr = (u_int32_t) original_addr.s_addr;
115240116Smarcel		if (ntohs(cu->data_type) == 101)
116240116Smarcel			/* Find and change our address */
117240116Smarcel			for (i = 0; (char *)(ci + 1) <= end && i < oc->client_count; i++, ci++)
118240116Smarcel				if (ci->address == (u_int32_t) alias_addr.s_addr) {
119240116Smarcel					ci->address = (u_int32_t) original_addr.s_addr;
120240116Smarcel					break;
121240116Smarcel				}
122240116Smarcel	}
123240116Smarcel}
124240116Smarcel