2 * $FreeBSD: src/sys/contrib/ipfilter/netinet/ip_raudio_pxy.c,v 1.7.2.3 2002/04/27 17:37:12 darrenr Exp $
3 * $DragonFly: src/sys/contrib/ipfilter/netinet/ip_raudio_pxy.c,v 1.2 2003/06/17 04:28:20 dillon Exp $
5 #if SOLARIS && defined(_KERNEL)
6 extern kmutex_t ipf_rw;
9 #define IPF_RAUDIO_PROXY
12 int ippr_raudio_init __P((void));
13 int ippr_raudio_new __P((fr_info_t *, ip_t *, ap_session_t *, nat_t *));
14 int ippr_raudio_in __P((fr_info_t *, ip_t *, ap_session_t *, nat_t *));
15 int ippr_raudio_out __P((fr_info_t *, ip_t *, ap_session_t *, nat_t *));
17 static frentry_t raudiofr;
21 * Real Audio application proxy initialization.
23 int ippr_raudio_init()
25 bzero((char *)&raudiofr, sizeof(raudiofr));
27 raudiofr.fr_flags = FR_INQUE|FR_PASS|FR_QUICK|FR_KEEPSTATE;
33 * Setup for a new proxy to handle Real Audio.
35 int ippr_raudio_new(fin, ip, aps, nat)
44 KMALLOCS(aps->aps_data, void *, sizeof(raudio_t));
45 if (aps->aps_data == NULL)
48 bzero(aps->aps_data, sizeof(raudio_t));
50 aps->aps_psiz = sizeof(raudio_t);
51 rap->rap_mode = RAP_M_TCP; /* default is for TCP */
57 int ippr_raudio_out(fin, ip, aps, nat)
63 raudio_t *rap = aps->aps_data;
64 unsigned char membuf[512 + 1], *s;
75 * If we've already processed the start messages, then nothing left
76 * for the proxy to do.
78 if (rap->rap_eos == 1)
81 tcp = (tcphdr_t *)fin->fin_dp;
82 off = fin->fin_hlen + (tcp->th_off << 2);
83 bzero(membuf, sizeof(membuf));
87 dlen = msgdsize(m) - off;
90 dlen = MIN(sizeof(membuf), dlen);
91 copyout_mblk(m, off, dlen, (char *)membuf);
93 m = *(mb_t **)fin->fin_mp;
95 dlen = mbufchainlen(m) - off;
98 dlen = MIN(sizeof(membuf), dlen);
99 m_copydata(m, off, dlen, (char *)membuf);
102 * In all the startup parsing, ensure that we don't go outside
103 * the packet buffer boundary.
106 * Look for the start of connection "PNA" string if not seen yet.
108 if (rap->rap_seenpna == 0) {
109 s = (u_char *)memstr("PNA", (char *)membuf, 3, dlen);
113 rap->rap_seenpna = 1;
118 * Directly after the PNA will be the version number of this
121 if (rap->rap_seenpna == 1 && rap->rap_seenver == 0) {
122 if ((s + 1) - membuf < dlen) {
123 rap->rap_version = (*s << 8) | *(s + 1);
125 rap->rap_seenver = 1;
131 * Now that we've been past the PNA and version number, we're into the
132 * startup messages block. This ends when a message with an ID of 0.
134 while ((rap->rap_eos == 0) && ((s + 1) - membuf < dlen)) {
135 if (rap->rap_gotid == 0) {
136 id = (*s << 8) | *(s + 1);
139 if (id == RA_ID_END) {
143 } else if (rap->rap_gotlen == 0) {
144 len = (*s << 8) | *(s + 1);
149 if (rap->rap_gotid == 1 && rap->rap_gotlen == 1) {
150 if (id == RA_ID_UDP) {
151 rap->rap_mode &= ~RAP_M_TCP;
152 rap->rap_mode |= RAP_M_UDP;
153 rap->rap_plport = (*s << 8) | *(s + 1);
154 } else if (id == RA_ID_ROBUST) {
155 rap->rap_mode |= RAP_M_ROBUST;
156 rap->rap_prport = (*s << 8) | *(s + 1);
167 int ippr_raudio_in(fin, ip, aps, nat)
173 unsigned char membuf[IPF_MAXPORTLEN + 1], *s;
174 tcphdr_t *tcp, tcph, *tcp2 = &tcph;
175 raudio_t *rap = aps->aps_data;
176 int off, dlen, slen, clen;
177 struct in_addr swa, swb;
190 * Wait until we've seen the end of the start messages and even then
191 * only proceed further if we're using UDP. If they want to use TCP
192 * then data is sent back on the same channel that is already open.
194 if (rap->rap_sdone != 0)
197 tcp = (tcphdr_t *)fin->fin_dp;
198 off = fin->fin_hlen + (tcp->th_off << 2);
199 m = *(mb_t **)fin->fin_mp;
204 dlen = msgdsize(m) - off;
207 bzero(membuf, sizeof(membuf));
208 clen = MIN(sizeof(membuf), dlen);
209 copyout_mblk(m, off, clen, (char *)membuf);
211 dlen = mbufchainlen(m) - off;
214 bzero(membuf, sizeof(membuf));
215 clen = MIN(sizeof(membuf), dlen);
216 m_copydata(m, off, clen, (char *)membuf);
219 seq = ntohl(tcp->th_seq);
221 * Check to see if the data in this packet is of interest to us.
222 * We only care for the first 19 bytes coming back from the server.
224 if (rap->rap_sseq == 0) {
225 s = (u_char *)memstr("PNA", (char *)membuf, 3, clen);
232 a2 = MIN(dlen, sizeof(rap->rap_svr));
233 } else if (seq <= rap->rap_sseq + sizeof(rap->rap_svr)) {
235 * seq # which is the start of data and from that the offset
236 * into the buffer array.
238 a1 = seq - rap->rap_sseq;
239 a2 = MIN(dlen, sizeof(rap->rap_svr));
245 for (a3 = a1, a4 = a2; (a4 > 0) && (a3 < 19) && (a3 >= 0); a4--,a3++) {
246 rap->rap_sbf |= (1 << a3);
247 rap->rap_svr[a3] = *s++;
250 if ((rap->rap_sbf != 0x7ffff) || (!rap->rap_eos)) /* 19 bits */
254 s = (u_char *)rap->rap_svr + 11;
255 if (((*s << 8) | *(s + 1)) == RA_ID_ROBUST) {
257 rap->rap_srport = (*s << 8) | *(s + 1);
264 ip->ip_p = IPPROTO_UDP;
265 ip->ip_src = nat->nat_inip;
266 ip->ip_dst = nat->nat_oip;
268 bcopy((char *)fin, (char *)&fi, sizeof(fi));
269 bzero((char *)tcp2, sizeof(*tcp2));
271 fi.fin_dp = (char *)tcp2;
272 fi.fin_fr = &raudiofr;
273 fi.fin_dlen = sizeof(*tcp2);
274 tcp2->th_win = htons(8192);
276 ip->ip_len = fin->fin_hlen + sizeof(*tcp);
278 if (((rap->rap_mode & RAP_M_UDP_ROBUST) == RAP_M_UDP_ROBUST) &&
279 (rap->rap_srport != 0)) {
280 dp = rap->rap_srport;
281 sp = rap->rap_prport;
282 tcp2->th_sport = htons(sp);
283 tcp2->th_dport = htons(dp);
287 ipn = nat_new(&fi, ip, nat->nat_ptr, NULL,
288 IPN_UDP | (sp ? 0 : FI_W_SPORT), NAT_OUTBOUND);
290 ipn->nat_age = fr_defnatage;
291 (void) fr_addstate(ip, &fi, NULL,
292 FI_IGNOREPKT|FI_NORULE|
293 (sp ? 0 : FI_W_SPORT));
297 if ((rap->rap_mode & RAP_M_UDP) == RAP_M_UDP) {
298 sp = rap->rap_plport;
299 tcp2->th_sport = htons(sp);
300 tcp2->th_dport = 0; /* XXX - don't specify remote port */
304 ipn = nat_new(&fi, ip, nat->nat_ptr, NULL, IPN_UDP|FI_W_DPORT,
307 ipn->nat_age = fr_defnatage;
308 (void) fr_addstate(ip, &fi, NULL,
309 FI_W_DPORT|FI_IGNOREPKT|FI_NORULE);