1 /*
2 * Redirector.cc --
3 *
4 * FIXME: This file needs a description here.
5 *
6 * Copyright (c) 1997-2002 The Regents of the University of California.
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions are met:
11 *
12 * A. Redistributions of source code must retain the above copyright notice,
13 * this list of conditions and the following disclaimer.
14 * B. Redistributions in binary form must reproduce the above copyright notice,
15 * this list of conditions and the following disclaimer in the documentation
16 * and/or other materials provided with the distribution.
17 * C. Neither the names of the copyright holders nor the names of its
18 * contributors may be used to endorse or promote products derived from this
19 * software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS
22 * IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
23 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE
25 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 * POSSIBILITY OF SUCH DAMAGE.
32 */
33
34 #ifndef lint
35 static const char rcsid[] =
36 "@(#) $Header: /usr/mash/src/repository/mash/mash-1/tgw/Redirector.cc,v 1.5 2003/11/19 19:20:45 aswan Exp $ (UCB)";
37 #endif
38
39 #include "tclcl.h"
40 #include "misc/sys-time.h"
41 #include "rtp/inet.h"
42 #include "rtp/session-rtp.h"
43
44 // This class forwards data from one session to another.
45 // Note: it only forwards in one direction (isn't a bridge)
46 // used with tgw. For a bridge version, look at Reflector
47
48 class Redirector : public TclObject, public SessionHandler {
49 public:
50 Redirector(int mtu);
51 ~Redirector();
52 virtual int command(int argc, const char*const* argv);
53 virtual void recv(DataHandler*);
54 protected:
55 int mtu_;
56 DataHandler* sinks_;
57 DataHandler* source_;
58 int forward_all;
59 u_long srcid[50];
60 u_char* pktbuf_;
61
62 /* FIXME */
63 virtual void recv(CtrlHandler*) {}
64 virtual void announce(CtrlHandler*) {}
65 };
66
67 static class RedirectorClass : public TclClass {
68 public:
69 RedirectorClass() : TclClass("Redirector") {}
70 TclObject* create(int, const char*const* argv) {
71 return (new Redirector(atoi(argv[4])));
72 }
73 } Redirector_class;
74
75 Redirector::Redirector(int mtu)
76 :mtu_(mtu), sinks_(0), source_(0), forward_all(0)
77 {
78 for (int x=0;x<50;x++) {
79 srcid[x]=0;
80 }
81 pktbuf_ = new u_char[2 * mtu];
82 }
83
84 Redirector::~Redirector()
85 {
86 DataHandler* p = sinks_;
87 while(p != 0) {
88 DataHandler* t = p->next;
89 delete p;
90 p = t;
91 }
92 if (source_ != 0)
93 delete source_;
94
95 delete pktbuf_;
96 }
97
98 int Redirector::command(int argc, const char*const* argv)
99 {
100 if (argc == 2) {
101 if (strcmp(argv[1], "add-all-sources") == 0) {
102 forward_all = 1;
103 return (TCL_OK);
104 } else if (strcmp(argv[1], "remove-all-sources") == 0) {
105 forward_all = 0;
106 return (TCL_OK);
107 }
108 } else if (argc == 3) {
109 if (strcmp(argv[1], "add-sink") == 0) {
110 Network* n = (Network*)TclObject::lookup(argv[2]);
111 DataHandler* dh = new DataHandler;
112 dh->manager(this);
113 dh->net(n);
114 dh->next = sinks_;
115 sinks_ = dh;
116 return (TCL_OK);
117 } else if (strcmp(argv[1], "add-source-net") == 0) {
118 Network* n = (Network*)TclObject::lookup(argv[2]);
119 DataHandler* dh = new DataHandler;
120 dh->manager(this);
121 dh->net(n);
122 source_ = dh;
123 return (TCL_OK);
124 } else if (strcmp(argv[1], "add-source") == 0) {
125 int next = 0;
126 while ((srcid[next] != 0) && (next < 50))
127 next++;
128 if (next >= 50)
129 printf("more then 50 active sources, increase buffer size!\n");
130 srcid[next] = strtoul(argv[2],NULL,10);
131 return (TCL_OK);
132 } else if (strcmp(argv[1], "remove-source") == 0) {
133 u_long toFind = strtoul(argv[2],NULL,10);
134 int next = 0;
135 while ((srcid[next] != toFind) && (next < 50))
136 next++;
137 if (next >= 50)
138 printf("can't find that source to remove!\n");
139 else
140 srcid[next] = 0;
141 return (TCL_OK);
142 }
143 }
144
145 return (TclObject::command(argc, argv));
146 }
147
148 void Redirector::recv(DataHandler* dh)
149 {
150
151 u_int32_t addr;
152 int port;
153 int cc = dh->recv(pktbuf_, 2 * mtu_, addr, port);
154 if (cc <= 0)
155 return;
156 if (dh != source_)
157 return;
158
159 rtphdr* rh = (rtphdr*)pktbuf_;
160 u_long recv_srcid = ntohl(rh->rh_ssrc);
161
162 DataHandler* p;
163 for (int temp=0;temp<50;temp++) {
164 if ((recv_srcid == srcid[temp]) || (forward_all == 1)) {
165 for (p = sinks_; p != 0; p = p->next) {
166 p->send(pktbuf_, cc);
167 }
168 return;
169 }
170 }
171 }
172
This page was automatically generated by the
LXR engine.
Visit the LXR main site for more
information.