alias_cuseeme.c revision 127094
137131Sbrian/*-
237131Sbrian * Copyright (c) 1998 Brian Somers <brian@Awfulhak.org>
337131Sbrian *                    with the aid of code written by
437131Sbrian *                    Junichi SATOH <junichi@astec.co.jp> 1996, 1997.
537131Sbrian * All rights reserved.
637131Sbrian *
737131Sbrian * Redistribution and use in source and binary forms, with or without
837131Sbrian * modification, are permitted provided that the following conditions
937131Sbrian * are met:
1037131Sbrian * 1. Redistributions of source code must retain the above copyright
1137131Sbrian *    notice, this list of conditions and the following disclaimer.
1237131Sbrian * 2. Redistributions in binary form must reproduce the above copyright
1337131Sbrian *    notice, this list of conditions and the following disclaimer in the
1437131Sbrian *    documentation and/or other materials provided with the distribution.
1537131Sbrian *
1637131Sbrian * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1737131Sbrian * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1837131Sbrian * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1937131Sbrian * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2037131Sbrian * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2137131Sbrian * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2237131Sbrian * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2337131Sbrian * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2437131Sbrian * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2537131Sbrian * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2637131Sbrian * SUCH DAMAGE.
2737131Sbrian */
2837131Sbrian
2984195Sdillon#include <sys/cdefs.h>
3084195Sdillon__FBSDID("$FreeBSD: head/sys/netinet/libalias/alias_cuseeme.c 127094 2004-03-16 21:30:41Z des $");
3184195Sdillon
32124621Sphk#include <stdio.h>
3337131Sbrian#include <sys/types.h>
3437131Sbrian#include <netinet/in_systm.h>
3537131Sbrian#include <netinet/in.h>
3637131Sbrian#include <netinet/ip.h>
3737131Sbrian#include <netinet/udp.h>
3837131Sbrian
3937131Sbrian#include "alias_local.h"
4037131Sbrian
4137131Sbrian/* CU-SeeMe Data Header */
4237131Sbrianstruct cu_header {
43127094Sdes	u_int16_t	dest_family;
44127094Sdes	u_int16_t	dest_port;
45127094Sdes	u_int32_t	dest_addr;
46127094Sdes	int16_t		family;
47127094Sdes	u_int16_t	port;
48127094Sdes	u_int32_t	addr;
49127094Sdes	u_int32_t	seq;
50127094Sdes	u_int16_t	msg;
51127094Sdes	u_int16_t	data_type;
52127094Sdes	u_int16_t	packet_len;
5337131Sbrian};
5437131Sbrian
5537131Sbrian/* Open Continue Header */
5637131Sbrianstruct oc_header {
57127094Sdes	u_int16_t	client_count;	/* Number of client info structs */
58127094Sdes	u_int32_t	seq_no;
59127094Sdes	char		user_name [20];
60127094Sdes	char		reserved  [4];	/* flags, version stuff, etc */
6137131Sbrian};
6237131Sbrian
6337131Sbrian/* client info structures */
6437131Sbrianstruct client_info {
65127094Sdes	u_int32_t	address;/* Client address */
66127094Sdes	char		reserved  [8];	/* Flags, pruning bitfield, packet
67127094Sdes					 * counts etc */
6837131Sbrian};
6937131Sbrian
7037131Sbrianvoid
71124621SphkAliasHandleCUSeeMeOut(struct libalias *la, struct ip *pip, struct alias_link *link)
7237131Sbrian{
73127094Sdes	struct udphdr *ud;
7437131Sbrian
75127094Sdes	ud = (struct udphdr *)((char *)pip + (pip->ip_hl << 2));
76127094Sdes	if (ntohs(ud->uh_ulen) - sizeof(struct udphdr) >= sizeof(struct cu_header)) {
77127094Sdes		struct cu_header *cu;
78127094Sdes		struct alias_link *cu_link;
7937131Sbrian
80127094Sdes		cu = (struct cu_header *)(ud + 1);
81127094Sdes		if (cu->addr)
82127094Sdes			cu->addr = (u_int32_t) GetAliasAddress(link).s_addr;
8337131Sbrian
84127094Sdes		cu_link = FindUdpTcpOut(la, pip->ip_src, GetDestAddress(link),
85127094Sdes		    ud->uh_dport, 0, IPPROTO_UDP, 1);
8699207Sbrian
8737131Sbrian#ifndef NO_FW_PUNCH
88127094Sdes		if (cu_link)
89127094Sdes			PunchFWHole(cu_link);
9037131Sbrian#endif
91127094Sdes	}
9237131Sbrian}
9337131Sbrian
9437131Sbrianvoid
95124621SphkAliasHandleCUSeeMeIn(struct libalias *la, struct ip *pip, struct in_addr original_addr)
9637131Sbrian{
97127094Sdes	struct in_addr alias_addr;
98127094Sdes	struct udphdr *ud;
99127094Sdes	struct cu_header *cu;
100127094Sdes	struct oc_header *oc;
101127094Sdes	struct client_info *ci;
102127094Sdes	char *end;
103127094Sdes	int i;
10437131Sbrian
105127094Sdes	alias_addr.s_addr = pip->ip_dst.s_addr;
106127094Sdes	ud = (struct udphdr *)((char *)pip + (pip->ip_hl << 2));
107127094Sdes	cu = (struct cu_header *)(ud + 1);
108127094Sdes	oc = (struct oc_header *)(cu + 1);
109127094Sdes	ci = (struct client_info *)(oc + 1);
110127094Sdes	end = (char *)ud + ntohs(ud->uh_ulen);
11137131Sbrian
112127094Sdes	if ((char *)oc <= end) {
113127094Sdes		if (cu->dest_addr)
114127094Sdes			cu->dest_addr = (u_int32_t) original_addr.s_addr;
115127094Sdes		if (ntohs(cu->data_type) == 101)
116127094Sdes			/* Find and change our address */
117127094Sdes			for (i = 0; (char *)(ci + 1) <= end && i < oc->client_count; i++, ci++)
118127094Sdes				if (ci->address == (u_int32_t) alias_addr.s_addr) {
119127094Sdes					ci->address = (u_int32_t) original_addr.s_addr;
120127094Sdes					break;
121127094Sdes				}
122127094Sdes	}
12337131Sbrian}
124