2 * $Id: ip_raudio_pxy.c,v 1.7.2.8 2002/01/13 04:58:29 darrenr Exp $
4 #if SOLARIS && defined(_KERNEL)
5 extern kmutex_t ipf_rw;
8 #define IPF_RAUDIO_PROXY
11 int ippr_raudio_init __P((void));
12 int ippr_raudio_new __P((fr_info_t *, ip_t *, ap_session_t *, nat_t *));
13 int ippr_raudio_in __P((fr_info_t *, ip_t *, ap_session_t *, nat_t *));
14 int ippr_raudio_out __P((fr_info_t *, ip_t *, ap_session_t *, nat_t *));
16 static frentry_t raudiofr;
20 * Real Audio application proxy initialization.
22 int ippr_raudio_init()
24 bzero((char *)&raudiofr, sizeof(raudiofr));
26 raudiofr.fr_flags = FR_INQUE|FR_PASS|FR_QUICK|FR_KEEPSTATE;
32 * Setup for a new proxy to handle Real Audio.
34 int ippr_raudio_new(fin, ip, aps, nat)
43 KMALLOCS(aps->aps_data, void *, sizeof(raudio_t));
44 if (aps->aps_data == NULL)
47 bzero(aps->aps_data, sizeof(raudio_t));
49 aps->aps_psiz = sizeof(raudio_t);
50 rap->rap_mode = RAP_M_TCP; /* default is for TCP */
56 int ippr_raudio_out(fin, ip, aps, nat)
62 raudio_t *rap = aps->aps_data;
63 unsigned char membuf[512 + 1], *s;
74 * If we've already processed the start messages, then nothing left
75 * for the proxy to do.
77 if (rap->rap_eos == 1)
80 tcp = (tcphdr_t *)fin->fin_dp;
81 off = fin->fin_hlen + (tcp->th_off << 2);
82 bzero(membuf, sizeof(membuf));
86 dlen = msgdsize(m) - off;
89 dlen = MIN(sizeof(membuf), dlen);
90 copyout_mblk(m, off, dlen, (char *)membuf);
92 m = *(mb_t **)fin->fin_mp;
94 dlen = mbufchainlen(m) - off;
97 dlen = MIN(sizeof(membuf), dlen);
98 m_copydata(m, off, dlen, (char *)membuf);
101 * In all the startup parsing, ensure that we don't go outside
102 * the packet buffer boundary.
105 * Look for the start of connection "PNA" string if not seen yet.
107 if (rap->rap_seenpna == 0) {
108 s = (u_char *)memstr("PNA", (char *)membuf, 3, dlen);
112 rap->rap_seenpna = 1;
117 * Directly after the PNA will be the version number of this
120 if (rap->rap_seenpna == 1 && rap->rap_seenver == 0) {
121 if ((s + 1) - membuf < dlen) {
122 rap->rap_version = (*s << 8) | *(s + 1);
124 rap->rap_seenver = 1;
130 * Now that we've been past the PNA and version number, we're into the
131 * startup messages block. This ends when a message with an ID of 0.
133 while ((rap->rap_eos == 0) && ((s + 1) - membuf < dlen)) {
134 if (rap->rap_gotid == 0) {
135 id = (*s << 8) | *(s + 1);
138 if (id == RA_ID_END) {
142 } else if (rap->rap_gotlen == 0) {
143 len = (*s << 8) | *(s + 1);
148 if (rap->rap_gotid == 1 && rap->rap_gotlen == 1) {
149 if (id == RA_ID_UDP) {
150 rap->rap_mode &= ~RAP_M_TCP;
151 rap->rap_mode |= RAP_M_UDP;
152 rap->rap_plport = (*s << 8) | *(s + 1);
153 } else if (id == RA_ID_ROBUST) {
154 rap->rap_mode |= RAP_M_ROBUST;
155 rap->rap_prport = (*s << 8) | *(s + 1);
166 int ippr_raudio_in(fin, ip, aps, nat)
172 unsigned char membuf[IPF_MAXPORTLEN + 1], *s;
173 tcphdr_t *tcp, tcph, *tcp2 = &tcph;
174 raudio_t *rap = aps->aps_data;
175 int off, dlen, slen, clen;
176 struct in_addr swa, swb;
189 * Wait until we've seen the end of the start messages and even then
190 * only proceed further if we're using UDP. If they want to use TCP
191 * then data is sent back on the same channel that is already open.
193 if (rap->rap_sdone != 0)
196 tcp = (tcphdr_t *)fin->fin_dp;
197 off = fin->fin_hlen + (tcp->th_off << 2);
198 m = *(mb_t **)fin->fin_mp;
203 dlen = msgdsize(m) - off;
206 bzero(membuf, sizeof(membuf));
207 clen = MIN(sizeof(membuf), dlen);
208 copyout_mblk(m, off, clen, (char *)membuf);
210 dlen = mbufchainlen(m) - off;
213 bzero(membuf, sizeof(membuf));
214 clen = MIN(sizeof(membuf), dlen);
215 m_copydata(m, off, clen, (char *)membuf);
218 seq = ntohl(tcp->th_seq);
220 * Check to see if the data in this packet is of interest to us.
221 * We only care for the first 19 bytes coming back from the server.
223 if (rap->rap_sseq == 0) {
224 s = (u_char *)memstr("PNA", (char *)membuf, 3, clen);
231 a2 = MIN(dlen, sizeof(rap->rap_svr));
232 } else if (seq <= rap->rap_sseq + sizeof(rap->rap_svr)) {
234 * seq # which is the start of data and from that the offset
235 * into the buffer array.
237 a1 = seq - rap->rap_sseq;
238 a2 = MIN(dlen, sizeof(rap->rap_svr));
244 for (a3 = a1, a4 = a2; (a4 > 0) && (a3 < 19) && (a3 >= 0); a4--,a3++) {
245 rap->rap_sbf |= (1 << a3);
246 rap->rap_svr[a3] = *s++;
249 if ((rap->rap_sbf != 0x7ffff) || (!rap->rap_eos)) /* 19 bits */
253 s = (u_char *)rap->rap_svr + 11;
254 if (((*s << 8) | *(s + 1)) == RA_ID_ROBUST) {
256 rap->rap_srport = (*s << 8) | *(s + 1);
263 ip->ip_p = IPPROTO_UDP;
264 ip->ip_src = nat->nat_inip;
265 ip->ip_dst = nat->nat_oip;
267 bcopy((char *)fin, (char *)&fi, sizeof(fi));
268 bzero((char *)tcp2, sizeof(*tcp2));
270 fi.fin_dp = (char *)tcp2;
271 fi.fin_fr = &raudiofr;
272 fi.fin_dlen = sizeof(*tcp2);
273 tcp2->th_win = htons(8192);
275 ip->ip_len = fin->fin_hlen + sizeof(*tcp);
277 if (((rap->rap_mode & RAP_M_UDP_ROBUST) == RAP_M_UDP_ROBUST) &&
278 (rap->rap_srport != 0)) {
279 dp = rap->rap_srport;
280 sp = rap->rap_prport;
281 tcp2->th_sport = htons(sp);
282 tcp2->th_dport = htons(dp);
286 ipn = nat_new(&fi, ip, nat->nat_ptr, NULL,
287 IPN_UDP | (sp ? 0 : FI_W_SPORT), NAT_OUTBOUND);
289 ipn->nat_age = fr_defnatage;
290 (void) fr_addstate(ip, &fi, NULL,
291 FI_IGNOREPKT|FI_NORULE|
292 (sp ? 0 : FI_W_SPORT));
296 if ((rap->rap_mode & RAP_M_UDP) == RAP_M_UDP) {
297 sp = rap->rap_plport;
298 tcp2->th_sport = htons(sp);
299 tcp2->th_dport = 0; /* XXX - don't specify remote port */
303 ipn = nat_new(&fi, ip, nat->nat_ptr, NULL, IPN_UDP|FI_W_DPORT,
306 ipn->nat_age = fr_defnatage;
307 (void) fr_addstate(ip, &fi, NULL,
308 FI_W_DPORT|FI_IGNOREPKT|FI_NORULE);