alias_cuseeme.c revision 145932
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 145932 2005-05-05 21:55:17Z glebius $");
3184195Sdillon
32145921Sglebius#ifdef _KERNEL
33145921Sglebius#include <sys/param.h>
34145921Sglebius#else
35145921Sglebius#include <sys/types.h>
36124621Sphk#include <stdio.h>
37145921Sglebius#endif
38145921Sglebius
3937131Sbrian#include <netinet/in_systm.h>
4037131Sbrian#include <netinet/in.h>
4137131Sbrian#include <netinet/ip.h>
4237131Sbrian#include <netinet/udp.h>
4337131Sbrian
44145921Sglebius#ifdef _KERNEL
45145932Sglebius#include <netinet/libalias/alias.h>
46145921Sglebius#include <netinet/libalias/alias_local.h>
47145921Sglebius#else
4837131Sbrian#include "alias_local.h"
49145921Sglebius#endif
5037131Sbrian
5137131Sbrian/* CU-SeeMe Data Header */
5237131Sbrianstruct cu_header {
53127094Sdes	u_int16_t	dest_family;
54127094Sdes	u_int16_t	dest_port;
55127094Sdes	u_int32_t	dest_addr;
56127094Sdes	int16_t		family;
57127094Sdes	u_int16_t	port;
58127094Sdes	u_int32_t	addr;
59127094Sdes	u_int32_t	seq;
60127094Sdes	u_int16_t	msg;
61127094Sdes	u_int16_t	data_type;
62127094Sdes	u_int16_t	packet_len;
6337131Sbrian};
6437131Sbrian
6537131Sbrian/* Open Continue Header */
6637131Sbrianstruct oc_header {
67127094Sdes	u_int16_t	client_count;	/* Number of client info structs */
68127094Sdes	u_int32_t	seq_no;
69127094Sdes	char		user_name [20];
70127094Sdes	char		reserved  [4];	/* flags, version stuff, etc */
7137131Sbrian};
7237131Sbrian
7337131Sbrian/* client info structures */
7437131Sbrianstruct client_info {
75127094Sdes	u_int32_t	address;/* Client address */
76127094Sdes	char		reserved  [8];	/* Flags, pruning bitfield, packet
77127094Sdes					 * counts etc */
7837131Sbrian};
7937131Sbrian
8037131Sbrianvoid
81131614SdesAliasHandleCUSeeMeOut(struct libalias *la, struct ip *pip, struct alias_link *lnk)
8237131Sbrian{
83131699Sdes	struct udphdr *ud = ip_next(pip);
8437131Sbrian
85127094Sdes	if (ntohs(ud->uh_ulen) - sizeof(struct udphdr) >= sizeof(struct cu_header)) {
86127094Sdes		struct cu_header *cu;
87131614Sdes		struct alias_link *cu_lnk;
8837131Sbrian
89131699Sdes		cu = udp_next(ud);
90127094Sdes		if (cu->addr)
91131614Sdes			cu->addr = (u_int32_t) GetAliasAddress(lnk).s_addr;
9237131Sbrian
93131614Sdes		cu_lnk = FindUdpTcpOut(la, pip->ip_src, GetDestAddress(lnk),
94127094Sdes		    ud->uh_dport, 0, IPPROTO_UDP, 1);
9599207Sbrian
9637131Sbrian#ifndef NO_FW_PUNCH
97131614Sdes		if (cu_lnk)
98131614Sdes			PunchFWHole(cu_lnk);
9937131Sbrian#endif
100127094Sdes	}
10137131Sbrian}
10237131Sbrian
10337131Sbrianvoid
104124621SphkAliasHandleCUSeeMeIn(struct libalias *la, struct ip *pip, struct in_addr original_addr)
10537131Sbrian{
106127094Sdes	struct in_addr alias_addr;
107127094Sdes	struct udphdr *ud;
108127094Sdes	struct cu_header *cu;
109127094Sdes	struct oc_header *oc;
110127094Sdes	struct client_info *ci;
111127094Sdes	char *end;
112127094Sdes	int i;
11337131Sbrian
114131614Sdes	(void)la;
115127094Sdes	alias_addr.s_addr = pip->ip_dst.s_addr;
116131699Sdes	ud = ip_next(pip);
117131699Sdes	cu = udp_next(ud);
118127094Sdes	oc = (struct oc_header *)(cu + 1);
119127094Sdes	ci = (struct client_info *)(oc + 1);
120127094Sdes	end = (char *)ud + ntohs(ud->uh_ulen);
12137131Sbrian
122127094Sdes	if ((char *)oc <= end) {
123127094Sdes		if (cu->dest_addr)
124127094Sdes			cu->dest_addr = (u_int32_t) original_addr.s_addr;
125127094Sdes		if (ntohs(cu->data_type) == 101)
126127094Sdes			/* Find and change our address */
127127094Sdes			for (i = 0; (char *)(ci + 1) <= end && i < oc->client_count; i++, ci++)
128127094Sdes				if (ci->address == (u_int32_t) alias_addr.s_addr) {
129127094Sdes					ci->address = (u_int32_t) original_addr.s_addr;
130127094Sdes					break;
131127094Sdes				}
132127094Sdes	}
13337131Sbrian}
134