alias_cuseeme.c revision 66157
1/*-
2 * Copyright (c) 1998 Brian Somers <brian@Awfulhak.org>
3 *                    with the aid of code written by
4 *                    Junichi SATOH <junichi@astec.co.jp> 1996, 1997.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 *
28 * $FreeBSD: head/sys/netinet/libalias/alias_cuseeme.c 66157 2000-09-21 06:52:59Z ru $
29 */
30
31#include <sys/types.h>
32#include <netinet/in_systm.h>
33#include <netinet/in.h>
34#include <netinet/ip.h>
35#include <netinet/udp.h>
36
37#include "alias_local.h"
38
39/* CU-SeeMe Data Header */
40struct cu_header {
41    u_int16_t dest_family;
42    u_int16_t dest_port;
43    u_int32_t  dest_addr;
44    int16_t family;
45    u_int16_t port;
46    u_int32_t addr;
47    u_int32_t seq;
48    u_int16_t msg;
49    u_int16_t data_type;
50    u_int16_t packet_len;
51};
52
53/* Open Continue Header */
54struct oc_header {
55    u_int16_t client_count;    /* Number of client info structs */
56    u_int32_t seq_no;
57    char user_name[20];
58    char reserved[4];        /* flags, version stuff, etc */
59};
60
61/* client info structures */
62struct client_info {
63    u_int32_t address;          /* Client address */
64    char reserved[8];        /* Flags, pruning bitfield, packet counts etc */
65};
66
67void
68AliasHandleCUSeeMeOut(struct ip *pip, struct alias_link *link)
69{
70  struct udphdr *ud;
71
72  ud = (struct udphdr *)((char *)pip + (pip->ip_hl << 2));
73  if (ntohs(ud->uh_ulen) - sizeof(struct udphdr) >= sizeof(struct cu_header)) {
74    struct cu_header *cu;
75    struct alias_link *cu_link;
76
77    cu = (struct cu_header *)(ud + 1);
78    if (cu->addr)
79      cu->addr = (u_int32_t)GetAliasAddress(link).s_addr;
80
81    cu_link = FindUdpTcpOut(pip->ip_src, GetDestAddress(link),
82                            ud->uh_dport, 0, IPPROTO_UDP);
83
84#ifndef NO_FW_PUNCH
85    if (cu_link)
86        PunchFWHole(cu_link);
87#endif
88  }
89}
90
91void
92AliasHandleCUSeeMeIn(struct ip *pip, struct in_addr original_addr)
93{
94  struct in_addr alias_addr;
95  struct udphdr *ud;
96  struct cu_header *cu;
97  struct oc_header *oc;
98  struct client_info *ci;
99  char *end;
100  int i;
101
102  alias_addr.s_addr = pip->ip_dst.s_addr;
103  ud = (struct udphdr *)((char *)pip + (pip->ip_hl << 2));
104  cu = (struct cu_header *)(ud + 1);
105  oc = (struct oc_header *)(cu + 1);
106  ci = (struct client_info *)(oc + 1);
107  end = (char *)ud + ntohs(ud->uh_ulen);
108
109  if ((char *)oc <= end) {
110    if(cu->dest_addr)
111      cu->dest_addr = (u_int32_t)original_addr.s_addr;
112    if(ntohs(cu->data_type) == 101)
113      /* Find and change our address */
114      for(i = 0; (char *)(ci + 1) <= end && i < oc->client_count; i++, ci++)
115        if(ci->address == (u_int32_t)alias_addr.s_addr) {
116          ci->address = (u_int32_t)original_addr.s_addr;
117          break;
118        }
119  }
120}
121