Merge from vendor branch AWK:
[dragonfly.git] / contrib / ipfilter / ip_raudio_pxy.c
1 /*
2  * $Id: ip_raudio_pxy.c,v 1.7.2.8 2002/01/13 04:58:29 darrenr Exp $
3  */
4 #if SOLARIS && defined(_KERNEL)
5 extern  kmutex_t        ipf_rw;
6 #endif
7
8 #define IPF_RAUDIO_PROXY
9
10
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 *));
15
16 static  frentry_t       raudiofr;
17
18
19 /*
20  * Real Audio application proxy initialization.
21  */
22 int ippr_raudio_init()
23 {
24         bzero((char *)&raudiofr, sizeof(raudiofr));
25         raudiofr.fr_ref = 1;
26         raudiofr.fr_flags = FR_INQUE|FR_PASS|FR_QUICK|FR_KEEPSTATE;
27         return 0;
28 }
29
30
31 /*
32  * Setup for a new proxy to handle Real Audio.
33  */
34 int ippr_raudio_new(fin, ip, aps, nat)
35 fr_info_t *fin;
36 ip_t *ip;
37 ap_session_t *aps;
38 nat_t *nat;
39 {
40         raudio_t *rap;
41
42
43         KMALLOCS(aps->aps_data, void *, sizeof(raudio_t));
44         if (aps->aps_data == NULL)
45                 return -1;
46
47         bzero(aps->aps_data, sizeof(raudio_t));
48         rap = aps->aps_data;
49         aps->aps_psiz = sizeof(raudio_t);
50         rap->rap_mode = RAP_M_TCP;      /* default is for TCP */
51         return 0;
52 }
53
54
55
56 int ippr_raudio_out(fin, ip, aps, nat)
57 fr_info_t *fin;
58 ip_t *ip;
59 ap_session_t *aps;
60 nat_t *nat;
61 {
62         raudio_t *rap = aps->aps_data;
63         unsigned char membuf[512 + 1], *s;
64         u_short id = 0;
65         int off, dlen;
66         tcphdr_t *tcp;
67         int len = 0;
68         mb_t *m;
69 #if     SOLARIS
70         mb_t *m1;
71 #endif
72
73         /*
74          * If we've already processed the start messages, then nothing left
75          * for the proxy to do.
76          */
77         if (rap->rap_eos == 1)
78                 return 0;
79
80         tcp = (tcphdr_t *)fin->fin_dp;
81         off = fin->fin_hlen + (tcp->th_off << 2);
82         bzero(membuf, sizeof(membuf));
83 #if     SOLARIS
84         m = fin->fin_qfm;
85
86         dlen = msgdsize(m) - off;
87         if (dlen <= 0)
88                 return 0;
89         dlen = MIN(sizeof(membuf), dlen);
90         copyout_mblk(m, off, dlen, (char *)membuf);
91 #else
92         m = *(mb_t **)fin->fin_mp;
93
94         dlen = mbufchainlen(m) - off;
95         if (dlen <= 0)
96                 return 0;
97         dlen = MIN(sizeof(membuf), dlen);
98         m_copydata(m, off, dlen, (char *)membuf);
99 #endif
100         /*
101          * In all the startup parsing, ensure that we don't go outside
102          * the packet buffer boundary.
103          */
104         /*
105          * Look for the start of connection "PNA" string if not seen yet.
106          */
107         if (rap->rap_seenpna == 0) {
108                 s = (u_char *)memstr("PNA", (char *)membuf, 3, dlen);
109                 if (s == NULL)
110                         return 0;
111                 s += 3;
112                 rap->rap_seenpna = 1;
113         } else
114                 s = membuf;
115
116         /*
117          * Directly after the PNA will be the version number of this
118          * connection.
119          */
120         if (rap->rap_seenpna == 1 && rap->rap_seenver == 0) {
121                 if ((s + 1) - membuf < dlen) {
122                         rap->rap_version = (*s << 8) | *(s + 1);
123                         s += 2;
124                         rap->rap_seenver = 1;
125                 } else
126                         return 0;
127         }
128
129         /*
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.
132          */
133         while ((rap->rap_eos == 0) && ((s + 1) - membuf < dlen)) {
134                 if (rap->rap_gotid == 0) {
135                         id = (*s << 8) | *(s + 1);
136                         s += 2;
137                         rap->rap_gotid = 1;
138                         if (id == RA_ID_END) {
139                                 rap->rap_eos = 1;
140                                 break;
141                         }
142                 } else if (rap->rap_gotlen == 0) {
143                         len = (*s << 8) | *(s + 1);
144                         s += 2;
145                         rap->rap_gotlen = 1;
146                 }
147
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);
156                         }
157                         s += len;
158                         rap->rap_gotlen = 0;
159                         rap->rap_gotid = 0;
160                 }
161         }
162         return 0;
163 }
164
165
166 int ippr_raudio_in(fin, ip, aps, nat)
167 fr_info_t *fin;
168 ip_t *ip;
169 ap_session_t *aps;
170 nat_t *nat;
171 {
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;
177         int a1, a2, a3, a4;
178         u_short sp, dp;
179         fr_info_t fi;
180         tcp_seq seq;
181         nat_t *ipn;
182         u_char swp;
183         mb_t *m;
184 #if     SOLARIS
185         mb_t *m1;
186 #endif
187
188         /*
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.
192          */
193         if (rap->rap_sdone != 0)
194                 return 0;
195
196         tcp = (tcphdr_t *)fin->fin_dp;
197         off = fin->fin_hlen + (tcp->th_off << 2);
198         m = *(mb_t **)fin->fin_mp;
199
200 #if     SOLARIS
201         m = fin->fin_qfm;
202
203         dlen = msgdsize(m) - off;
204         if (dlen <= 0)
205                 return 0;
206         bzero(membuf, sizeof(membuf));
207         clen = MIN(sizeof(membuf), dlen);
208         copyout_mblk(m, off, clen, (char *)membuf);
209 #else
210         dlen = mbufchainlen(m) - off;
211         if (dlen <= 0)
212                 return 0;
213         bzero(membuf, sizeof(membuf));
214         clen = MIN(sizeof(membuf), dlen);
215         m_copydata(m, off, clen, (char *)membuf);
216 #endif
217
218         seq = ntohl(tcp->th_seq);
219         /*
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.
222          */
223         if (rap->rap_sseq == 0) {
224                 s = (u_char *)memstr("PNA", (char *)membuf, 3, clen);
225                 if (s == NULL)
226                         return 0;
227                 a1 = s - membuf;
228                 dlen -= a1;
229                 a1 = 0;
230                 rap->rap_sseq = seq;
231                 a2 = MIN(dlen, sizeof(rap->rap_svr));
232         } else if (seq <= rap->rap_sseq + sizeof(rap->rap_svr)) {
233                 /*
234                  * seq # which is the start of data and from that the offset
235                  * into the buffer array.
236                  */
237                 a1 = seq - rap->rap_sseq;
238                 a2 = MIN(dlen, sizeof(rap->rap_svr));
239                 a2 -= a1;
240                 s = membuf;
241         } else
242                 return 0;
243
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++;
247         }
248
249         if ((rap->rap_sbf != 0x7ffff) || (!rap->rap_eos))       /* 19 bits */
250                 return 0;
251         rap->rap_sdone = 1;
252
253         s = (u_char *)rap->rap_svr + 11;
254         if (((*s << 8) | *(s + 1)) == RA_ID_ROBUST) {
255                 s += 2;
256                 rap->rap_srport = (*s << 8) | *(s + 1);
257         }
258
259         swp = ip->ip_p;
260         swa = ip->ip_src;
261         swb = ip->ip_dst;
262
263         ip->ip_p = IPPROTO_UDP;
264         ip->ip_src = nat->nat_inip;
265         ip->ip_dst = nat->nat_oip;
266
267         bcopy((char *)fin, (char *)&fi, sizeof(fi));
268         bzero((char *)tcp2, sizeof(*tcp2));
269         tcp2->th_off = 5;
270         fi.fin_dp = (char *)tcp2;
271         fi.fin_fr = &raudiofr;
272         fi.fin_dlen = sizeof(*tcp2);
273         tcp2->th_win = htons(8192);
274         slen = ip->ip_len;
275         ip->ip_len = fin->fin_hlen + sizeof(*tcp);
276
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);
283                 fi.fin_data[0] = dp;
284                 fi.fin_data[1] = sp;
285                 fi.fin_out = 0;
286                 ipn = nat_new(&fi, ip, nat->nat_ptr, NULL,
287                               IPN_UDP | (sp ? 0 : FI_W_SPORT), NAT_OUTBOUND);
288                 if (ipn != NULL) {
289                         ipn->nat_age = fr_defnatage;
290                         (void) fr_addstate(ip, &fi, NULL,
291                                            FI_IGNOREPKT|FI_NORULE|
292                                            (sp ? 0 : FI_W_SPORT));
293                 }
294         }
295
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 */
300                 fi.fin_data[0] = sp;
301                 fi.fin_data[1] = 0;
302                 fi.fin_out = 1;
303                 ipn = nat_new(&fi, ip, nat->nat_ptr, NULL, IPN_UDP|FI_W_DPORT,
304                               NAT_OUTBOUND);
305                 if (ipn != NULL) {
306                         ipn->nat_age = fr_defnatage;
307                         (void) fr_addstate(ip, &fi, NULL,
308                                            FI_W_DPORT|FI_IGNOREPKT|FI_NORULE);
309                 }
310         }
311
312         ip->ip_p = swp;
313         ip->ip_len = slen;
314         ip->ip_src = swa;
315         ip->ip_dst = swb;
316         return 0;
317 }