Corrected error print, if mISDN_dsp.ko module cannot be loaded.
[lcr.git] / bchannel.c
1 /*****************************************************************************\
2 **                                                                           **
3 ** Linux Call Router                                                         **
4 **                                                                           **
5 **---------------------------------------------------------------------------**
6 ** Copyright: Andreas Eversberg                                              **
7 **                                                                           **
8 ** mISDN channel handlin for remote application                              **
9 **                                                                           **
10 \*****************************************************************************/ 
11
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <unistd.h>
15 #include <string.h>
16 #include <poll.h>
17 #include <errno.h>
18 #include <sys/ioctl.h>
19 #include <sys/types.h>
20 #include <sys/stat.h>
21 #include <fcntl.h>
22 #include <netinet/udp.h>
23 #include <netinet/in.h>
24 #include <netdb.h>
25 #include <sys/socket.h>
26 #include <mISDNif.h>
27
28 #define AF_COMPATIBILITY_FUNC 1
29 #define MISDN_OLD_AF_COMPATIBILITY 1
30 #include <compat_af_isdn.h>
31
32 #include <asterisk/frame.h>
33
34
35 #include "extension.h"
36 #include "message.h"
37 #include "lcrsocket.h"
38 #include "cause.h"
39 #include "bchannel.h"
40 #include "chan_lcr.h"
41 #include "callerid.h"
42
43
44 #ifndef ISDN_PID_L4_B_USER
45 #define ISDN_PID_L4_B_USER 0x440000ff
46 #endif
47
48 pid_t   bchannel_pid;
49
50 enum {
51         BSTATE_IDLE,
52         BSTATE_ACTIVATING,
53         BSTATE_ACTIVE,
54         BSTATE_DEACTIVATING,
55 };
56
57
58 int bchannel_initialize(void)
59 {
60         init_af_isdn();
61
62         return(0);
63 }
64
65 void bchannel_deinitialize(void)
66 {
67 }
68
69 /*
70  * send control information to the channel (dsp-module)
71  */
72 static void ph_control(int sock, unsigned int c1, unsigned int c2, char *trace_name, int trace_value, int b_mode)
73 {
74         unsigned char buffer[MISDN_HEADER_LEN+sizeof(int)+sizeof(int)];
75         struct mISDNhead *ctrl = (struct mISDNhead *)buffer;
76         unsigned int *d = (unsigned int *)(buffer+MISDN_HEADER_LEN);
77         int ret;
78
79         if (b_mode != 0 && b_mode != 2)
80                 return;
81
82         CDEBUG(NULL, NULL, "Sending PH_CONTROL %s %x,%x\n", trace_name, c1, c2);
83         ctrl->prim = PH_CONTROL_REQ;
84         ctrl->id = 0;
85         *d++ = c1;
86         *d++ = c2;
87         ret = sendto(sock, buffer, MISDN_HEADER_LEN+sizeof(int)*2, 0, NULL, 0);
88         if (ret < 0)
89                 CERROR(NULL, NULL, "Failed to send to socket %d\n", sock);
90 }
91
92 static void ph_control_block(int sock, unsigned int c1, void *c2, int c2_len, char *trace_name, int trace_value, int b_mode)
93 {
94         unsigned char buffer[MISDN_HEADER_LEN+sizeof(int)+c2_len];
95         struct mISDNhead *ctrl = (struct mISDNhead *)buffer;
96         unsigned int *d = (unsigned int *)(buffer+MISDN_HEADER_LEN);
97         int ret;
98
99         if (b_mode != 0 && b_mode != 2)
100                 return;
101
102         CDEBUG(NULL, NULL, "Sending PH_CONTROL (block) %s %x\n", trace_name, c1);
103         ctrl->prim = PH_CONTROL_REQ;
104         ctrl->id = 0;
105         *d++ = c1;
106         memcpy(d, c2, c2_len);
107         ret = sendto(sock, buffer, MISDN_HEADER_LEN+sizeof(int)+c2_len, 0, NULL, 0);
108         if (ret < 0)
109                 CERROR(NULL, NULL, "Failed to send to socket %d\n", sock);
110 }
111
112
113 /*
114  * create stack
115  */
116 int bchannel_create(struct bchannel *bchannel, int mode)
117 {
118         int ret;
119         unsigned int on = 1;
120         struct sockaddr_mISDN addr;
121
122         if (bchannel->b_sock > -1)
123         {
124                 CERROR(bchannel->call, NULL, "Socket already created for handle 0x%x\n", bchannel->handle);
125                 return(0);
126         }
127
128         /* open socket */
129         bchannel->b_mode = mode;
130         switch(bchannel->b_mode)
131         {
132                 case 0:
133                 CDEBUG(bchannel->call, NULL, "Open DSP audio\n");
134                 bchannel->b_sock = socket(PF_ISDN, SOCK_DGRAM, ISDN_P_B_L2DSP);
135                 break;
136                 case 1:
137                 CDEBUG(bchannel->call, NULL, "Open audio\n");
138                 bchannel->b_sock = socket(PF_ISDN, SOCK_DGRAM, ISDN_P_B_RAW);
139                 break;
140                 case 2:
141                 CDEBUG(bchannel->call, NULL, "Open DSP HDLC\n");
142                 bchannel->b_sock = socket(PF_ISDN, SOCK_DGRAM, ISDN_P_B_L2DSPHDLC);
143                 break;
144                 case 3:
145                 CDEBUG(bchannel->call, NULL, "Open HDLC\n");
146                 bchannel->b_sock = socket(PF_ISDN, SOCK_DGRAM, ISDN_P_B_HDLC);
147                 break;
148         }
149         if (bchannel->b_sock < 0)
150         {
151                 CERROR(bchannel->call, NULL, "Failed to open bchannel-socket for handle 0x%x with mISDN-DSP layer. Did you load mISDN_dsp.ko?\n", bchannel->handle);
152                 return(0);
153         }
154         
155         /* set nonblocking io */
156         ret = ioctl(bchannel->b_sock, FIONBIO, &on);
157         if (ret < 0)
158         {
159                 CERROR(bchannel->call, NULL, "Failed to set bchannel-socket handle 0x%x into nonblocking IO\n", bchannel->handle);
160                 close(bchannel->b_sock);
161                 bchannel->b_sock = -1;
162                 return(0);
163         }
164
165         /* bind socket to bchannel */
166         addr.family = AF_ISDN;
167         addr.dev = (bchannel->handle>>8);
168         addr.channel = bchannel->handle & 0xff;
169         ret = bind(bchannel->b_sock, (struct sockaddr *)&addr, sizeof(addr));
170         if (ret < 0)
171         {
172                 CERROR(bchannel->call, NULL, "Failed to bind bchannel-socket for handle 0x%x with mISDN-DSP layer. (port %d, channel %d) Did you load mISDN_dsp.ko?\n", bchannel->handle, addr.dev, addr.channel);
173                 close(bchannel->b_sock);
174                 bchannel->b_sock = -1;
175                 return(0);
176         }
177         return(1);
178 }
179
180
181 /*
182  * activate / deactivate request
183  */
184 void bchannel_activate(struct bchannel *bchannel, int activate)
185 {
186         struct mISDNhead act;
187         int ret;
188
189         /* activate bchannel */
190         CDEBUG(bchannel->call, NULL, "%sActivating B-channel.\n", activate?"":"De-");
191         switch(bchannel->b_mode)
192         {
193                 case 0:
194                 case 2:
195                 act.prim = (activate)?DL_ESTABLISH_REQ:DL_RELEASE_REQ; 
196                 break;
197                 case 1:
198                 case 3:
199                 act.prim = (activate)?PH_ACTIVATE_REQ:PH_DEACTIVATE_REQ; 
200                 break;
201         }
202         act.id = 0;
203         ret = sendto(bchannel->b_sock, &act, MISDN_HEADER_LEN, 0, NULL, 0);
204         if (ret < 0)
205                 CERROR(bchannel->call, NULL, "Failed to send to socket %d\n", bchannel->b_sock);
206
207         bchannel->b_state = (activate)?BSTATE_ACTIVATING:BSTATE_DEACTIVATING;
208         bchannel->rebuffer_usage = 0;
209 }
210
211
212 /*
213  * set features
214  */
215 static void bchannel_activated(struct bchannel *bchannel)
216 {
217         int sock;
218
219         sock = bchannel->b_sock;
220
221         /* set dsp features */
222         if (bchannel->b_txdata)
223                 ph_control(sock, (bchannel->b_txdata)?DSP_TXDATA_ON:DSP_TXDATA_OFF, 0, "DSP-TXDATA", bchannel->b_txdata, bchannel->b_mode);
224         if (bchannel->b_delay && bchannel->b_mode == 0)
225                 ph_control(sock, DSP_DELAY, bchannel->b_delay, "DSP-DELAY", bchannel->b_delay, bchannel->b_mode);
226         if (bchannel->b_tx_dejitter && bchannel->b_mode == 0)
227                 ph_control(sock, (bchannel->b_tx_dejitter)?DSP_TX_DEJITTER:DSP_TX_DEJ_OFF, 0, "DSP-TX_DEJITTER", bchannel->b_tx_dejitter, bchannel->b_mode);
228         if (bchannel->b_tx_gain && bchannel->b_mode == 0)
229                 ph_control(sock, DSP_VOL_CHANGE_TX, bchannel->b_tx_gain, "DSP-TX_GAIN", bchannel->b_tx_gain, bchannel->b_mode);
230         if (bchannel->b_rx_gain && bchannel->b_mode == 0)
231                 ph_control(sock, DSP_VOL_CHANGE_RX, bchannel->b_rx_gain, "DSP-RX_GAIN", bchannel->b_rx_gain, bchannel->b_mode);
232         if (bchannel->b_pipeline[0] && bchannel->b_mode == 0)
233                 ph_control_block(sock, DSP_PIPELINE_CFG, bchannel->b_pipeline, strlen(bchannel->b_pipeline)+1, "DSP-PIPELINE", 0, bchannel->b_mode);
234         if (bchannel->b_conf)
235                 ph_control(sock, DSP_CONF_JOIN, bchannel->b_conf, "DSP-CONF", bchannel->b_conf, bchannel->b_mode);
236         if (bchannel->b_echo)
237                 ph_control(sock, DSP_ECHO_ON, 0, "DSP-ECHO", 1, bchannel->b_mode);
238         if (bchannel->b_tone && bchannel->b_mode == 0)
239                 ph_control(sock, DSP_TONE_PATT_ON, bchannel->b_tone, "DSP-TONE", bchannel->b_tone, bchannel->b_mode);
240         if (bchannel->b_rxoff)
241                 ph_control(sock, DSP_RECEIVE_OFF, 0, "DSP-RXOFF", 1, bchannel->b_mode);
242 //      if (bchannel->b_txmix && bchannel->b_mode == 0)
243 //              ph_control(sock, DSP_MIX_ON, 0, "DSP-MIX", 1, bchannel->b_mode);
244         if (bchannel->b_dtmf && bchannel->b_mode == 0)
245                 ph_control(sock, DTMF_TONE_START, 0, "DSP-DTMF", 1, bchannel->b_mode);
246         if (bchannel->b_bf_len && bchannel->b_mode == 0)
247                 ph_control_block(sock, DSP_BF_ENABLE_KEY, bchannel->b_bf_key, bchannel->b_bf_len, "DSP-CRYPT", bchannel->b_bf_len, bchannel->b_mode);
248         if (bchannel->b_conf)
249                 ph_control(sock, DSP_CONF_JOIN, bchannel->b_conf, "DSP-CONF", bchannel->b_conf, bchannel->b_mode);
250
251         bchannel->b_state = BSTATE_ACTIVE;
252 }
253
254 /*
255  * destroy stack
256  */
257 void bchannel_destroy(struct bchannel *bchannel)
258 {
259         if (bchannel->b_sock > -1)
260         {
261                 close(bchannel->b_sock);
262                 bchannel->b_sock = -1;
263                 bchannel->rebuffer_usage = 0;
264         }
265         bchannel->b_state = BSTATE_IDLE;
266 }
267
268
269 /*
270  * whenever we get audio data from bchannel, we process it here
271  */
272 static void bchannel_receive(struct bchannel *bchannel, unsigned char *buffer, int len)
273 {
274         struct mISDNhead *hh = (struct mISDNhead *)buffer;
275         unsigned char *data = buffer + MISDN_HEADER_LEN;
276         unsigned int cont = *((unsigned int *)data);
277         unsigned char *d;
278         int i;
279         struct bchannel *remote_bchannel;
280         int ret;
281
282         if (hh->prim == PH_CONTROL_IND)
283         {
284                 /* non dsp -> ignore ph_control */
285                 if (bchannel->b_mode == 1 || bchannel->b_mode == 3)
286                         return;
287                 if (len < 4)
288                 {
289                         CERROR(bchannel->call, NULL, "SHORT READ OF PH_CONTROL INDICATION\n");
290                         return;
291                 }
292                 if ((cont&(~DTMF_TONE_MASK)) == DTMF_TONE_VAL)
293                 {
294                         if (bchannel->call)
295                                 lcr_in_dtmf(bchannel->call, cont & DTMF_TONE_MASK);
296                         return;
297                 }
298                 switch(cont)
299                 {
300                         case DSP_BF_REJECT:
301                         CERROR(bchannel->call, NULL, "Blowfish crypt rejected.\n");
302                         break;
303
304                         case DSP_BF_ACCEPT:
305                         CDEBUG(bchannel->call, NULL, "Blowfish crypt enabled.\n");
306                         break;
307
308                         default:
309                         CDEBUG(bchannel->call, NULL, "Unhandled bchannel control 0x%x.\n", cont);
310                 }
311                 return;
312         }
313         if (hh->prim == PH_DATA_REQ)
314         {
315                 if (!bchannel->b_txdata)
316                 {
317                         /* if tx is off, it may happen that fifos send us pending informations, we just ignore them */
318                         CDEBUG(bchannel->call, NULL, "ignoring tx data, because 'txdata' is turned off\n");
319                         return;
320                 }
321                 return;
322         }
323         if (hh->prim != PH_DATA_IND && hh->prim != DL_DATA_IND)
324         {
325                 CERROR(bchannel->call, NULL, "Bchannel received unknown primitve: 0x%lx\n", hh->prim);
326                 return;
327         }
328         /* if calls are bridged, but not via dsp (no b_conf), forward here */
329         if (!bchannel->b_conf
330          && bchannel->call
331          && bchannel->call->bridge_call
332          && bchannel->call->bridge_call->bchannel) {
333                 remote_bchannel = bchannel->call->bridge_call->bchannel;
334 #if 0
335                 int i = 0;
336                 char string[4096] = "";
337                 while(i < len) {
338                         sprintf(string+strlen(string), " %02x", data[i]);
339                         i++;
340                 }
341                 CDEBUG(remote_bchannel->call, NULL, "Forwarding packet%s\n", string);
342 #endif
343                 hh->prim = PH_DATA_REQ;
344                 hh->id = 0;
345                 ret = sendto(remote_bchannel->b_sock, buffer, MISDN_HEADER_LEN+len, 0, NULL, 0);
346                 if (ret < 0)
347                         CERROR(remote_bchannel->call, NULL, "Failed to send to socket %d\n", bchannel->b_sock);
348                 return;
349         }
350         /* calls will not process any audio data unless
351          * the call is connected OR interface features audio during call setup.
352          */
353
354         /* if rx is off, it may happen that fifos send us pending informations, we just ignore them */
355         if (bchannel->b_rxoff)
356         {
357                 CDEBUG(bchannel->call, NULL, "ignoring data, because rx is turned off\n");
358                 return;
359         }
360
361         if (!bchannel->call)
362         {
363                 CDEBUG(bchannel->call, NULL, "ignoring data, because no call associated with bchannel\n");
364                 return;
365         }
366         if (!bchannel->call->audiopath)
367         {
368                 /* return, because we have no audio from port */
369                 return;
370         }
371
372         if (bchannel->call->pipe[1] < 0)
373         {
374                 /* nobody there */
375                 return;
376         }
377
378         /* if no hdlc */
379         if (bchannel->b_mode == 0 || bchannel->b_mode == 1)
380         {
381                 d = data;
382                 for (i = 0; i < len; i++) {
383                         *d = flip_bits[*d];
384                         d++;
385                 }
386         }
387
388         /* no hdlc and rebuffer */
389         if (bchannel->call->rebuffer && !bchannel->call->hdlc) {
390                 int u = bchannel->rebuffer_usage;
391                 unsigned char * b = bchannel->rebuffer;
392                 int l = len;
393                 int fd = bchannel->call->pipe[1];
394
395                 d = data;
396
397                 if (u > 0) {
398                         if (u + l >= 160) {
399                                 memcpy(b + u, d, 160 - u);
400                                 d += 160 - u;
401                                 l -= 160 - u;
402                                 u = 0;
403                                 if (write(fd, b, 160) < 0)
404                                         goto errout;
405                         } else {
406                                 memcpy(b + u, d, l);
407                                 u += l;
408                                 l = 0;
409                         }
410                 }
411
412                 while (l >= 160) {
413                         if (write(fd, d, 160) < 0)
414                                 goto errout;
415                         d += 160;
416                         l -= 160;
417                 }
418
419                 if (l > 0) {
420                         memcpy(b, d, l);
421                 } 
422                 bchannel->rebuffer_usage = u + l;
423         } else {
424                 len = write(bchannel->call->pipe[1], data, len);
425                 if (len < 0)
426                         goto errout;
427         }
428
429         return;
430  errout:
431         close(bchannel->call->pipe[1]);
432         bchannel->call->pipe[1] = -1;
433         bchannel->rebuffer_usage = 0;
434         CDEBUG(bchannel->call, NULL, "broken pipe on bchannel pipe\n");
435 }
436
437
438 /*
439  * transmit data to bchannel
440  */
441 void bchannel_transmit(struct bchannel *bchannel, unsigned char *data, int len)
442 {
443         unsigned char buff[1024 + MISDN_HEADER_LEN], *p = buff + MISDN_HEADER_LEN;
444         struct mISDNhead *frm = (struct mISDNhead *)buff;
445         int ret;
446         int i;
447
448         if (bchannel->b_state != BSTATE_ACTIVE)
449                 return;
450         if (len > 1024 || len < 1)
451                 return;
452         switch(bchannel->b_mode)
453         {
454         case 0:
455                 for (i = 0; i < len; i++)
456                         *p++ = flip_bits[*data++];
457                 frm->prim = DL_DATA_REQ;
458                 break;
459         case 1:
460                 for (i = 0; i < len; i++)
461                         *p++ = flip_bits[*data++];
462                 frm->prim = PH_DATA_REQ;
463                 break;
464         case 2:
465                 memcpy(p, data, len);
466                 frm->prim = DL_DATA_REQ;
467                 break;
468         case 3:
469                 memcpy(p, data, len);
470                 frm->prim = PH_DATA_REQ;
471                 break;
472         }
473         frm->id = 0;
474         ret = sendto(bchannel->b_sock, buff, MISDN_HEADER_LEN+len, 0, NULL, 0);
475         if (ret < 0)
476                 CERROR(bchannel->call, NULL, "Failed to send to socket %d\n", bchannel->b_sock);
477 }
478
479
480 /*
481  * join bchannel
482  */
483 void bchannel_join(struct bchannel *bchannel, unsigned short id)
484 {
485         int sock;
486
487         sock = bchannel->b_sock;
488         if (id) {
489                 bchannel->b_conf = (id<<16) + bchannel_pid;
490                 bchannel->b_rxoff = 1;
491         } else {
492                 bchannel->b_conf = 0;
493                 bchannel->b_rxoff = 0;
494         }
495         if (bchannel->b_state == BSTATE_ACTIVE)
496         {
497                 ph_control(sock, DSP_RECEIVE_OFF, bchannel->b_rxoff, "DSP-RX_OFF", bchannel->b_conf, bchannel->b_mode);
498                 ph_control(sock, DSP_CONF_JOIN, bchannel->b_conf, "DSP-CONF", bchannel->b_conf, bchannel->b_mode);
499         }
500 }
501
502
503 /*
504  * dtmf bchannel
505  */
506 void bchannel_dtmf(struct bchannel *bchannel, int on)
507 {
508         int sock;
509
510         sock = bchannel->b_sock;
511         bchannel->b_dtmf = 1;
512         if (bchannel->b_state == BSTATE_ACTIVE && bchannel->b_mode == 0)
513                 ph_control(sock, on?DTMF_TONE_START:DTMF_TONE_STOP, 0, "DSP-DTMF", 1, bchannel->b_mode);
514 }
515
516
517 /*
518  * blowfish bchannel
519  */
520 void bchannel_blowfish(struct bchannel *bchannel, unsigned char *key, int len)
521 {
522         int sock;
523
524         sock = bchannel->b_sock;
525         memcpy(bchannel->b_bf_key, key, len);
526         bchannel->b_bf_len = len;
527         if (bchannel->b_state == BSTATE_ACTIVE)
528                 ph_control_block(sock, DSP_BF_ENABLE_KEY, bchannel->b_bf_key, bchannel->b_bf_len, "DSP-CRYPT", bchannel->b_bf_len, bchannel->b_mode);
529 }
530
531
532 /*
533  * pipeline bchannel
534  */
535 void bchannel_pipeline(struct bchannel *bchannel, char *pipeline)
536 {
537         int sock;
538
539         sock = bchannel->b_sock;
540         strncpy(bchannel->b_pipeline, pipeline, sizeof(bchannel->b_pipeline)-1);
541         if (bchannel->b_state == BSTATE_ACTIVE)
542                 ph_control_block(sock, DSP_PIPELINE_CFG, bchannel->b_pipeline, strlen(bchannel->b_pipeline)+1, "DSP-PIPELINE", 0, bchannel->b_mode);
543 }
544
545
546 /*
547  * gain bchannel
548  */
549 void bchannel_gain(struct bchannel *bchannel, int gain, int tx)
550 {
551         int sock;
552
553         sock = bchannel->b_sock;
554         if (tx)
555                 bchannel->b_tx_gain = gain;
556         else
557                 bchannel->b_rx_gain = gain;
558         if (bchannel->b_state == BSTATE_ACTIVE && bchannel->b_mode == 0)
559                 ph_control(sock, (tx)?DSP_VOL_CHANGE_TX:DSP_VOL_CHANGE_RX, gain, (tx)?"DSP-TX_GAIN":"DSP-RX_GAIN", gain, bchannel->b_mode);
560 }
561
562
563 /*
564  * main loop for processing messages from mISDN
565  */
566 int bchannel_handle(void)
567 {
568         int ret, work = 0;
569         struct bchannel *bchannel;
570         unsigned char buffer[2048+MISDN_HEADER_LEN];
571         struct mISDNhead *hh = (struct mISDNhead *)buffer;
572
573         /* process all bchannels */
574         bchannel = bchannel_first;
575         while(bchannel)
576         {
577                 /* handle message from bchannel */
578                 if (bchannel->b_sock > -1)
579                 {
580                         ret = recv(bchannel->b_sock, buffer, sizeof(buffer), 0);
581                         if (ret >= (int)MISDN_HEADER_LEN)
582                         {
583                                 work = 1;
584                                 switch(hh->prim)
585                                 {
586                                         /* we don't care about confirms, we use rx data to sync tx */
587                                         case PH_DATA_CNF:
588                                         break;
589
590                                         /* we receive audio data, we respond to it AND we send tones */
591                                         case PH_DATA_IND:
592                                         case PH_DATA_REQ:
593                                         case DL_DATA_IND:
594                                         case PH_CONTROL_IND:
595                                         bchannel_receive(bchannel, buffer, ret-MISDN_HEADER_LEN);
596                                         break;
597
598                                         case PH_ACTIVATE_IND:
599                                         case DL_ESTABLISH_IND:
600                                         case PH_ACTIVATE_CNF:
601                                         case DL_ESTABLISH_CNF:
602                                         CDEBUG(bchannel->call, NULL, "DL_ESTABLISH confirm: bchannel is now activated (socket %d).\n", bchannel->b_sock);
603                                         bchannel_activated(bchannel);
604                                         break;
605
606                                         case PH_DEACTIVATE_IND:
607                                         case DL_RELEASE_IND:
608                                         case PH_DEACTIVATE_CNF:
609                                         case DL_RELEASE_CNF:
610                                         CDEBUG(bchannel->call, NULL, "DL_RELEASE confirm: bchannel is now de-activated (socket %d).\n", bchannel->b_sock);
611 //                                      bchannel_deactivated(bchannel);
612                                         break;
613
614                                         default:
615                                         CERROR(bchannel->call, NULL, "child message not handled: prim(0x%x) socket(%d) data len(%d)\n", hh->prim, bchannel->b_sock, ret - MISDN_HEADER_LEN);
616                                 }
617                         } else
618                         {
619                                 if (ret < 0 && errno != EWOULDBLOCK)
620                                         CERROR(bchannel->call, NULL, "Read from socket %d failed with return code %d\n", bchannel->b_sock, ret);
621                         }
622                 }
623                 bchannel = bchannel->next;
624         }
625
626         /* if we received at least one b-frame, we will return 1 */
627         return(work);
628 }
629
630
631 /*
632  * bchannel channel handling
633  */
634 struct bchannel *bchannel_first = NULL;
635 struct bchannel *find_bchannel_handle(unsigned int handle)
636 {
637         struct bchannel *bchannel = bchannel_first;
638
639         while(bchannel)
640         {
641                 if (bchannel->handle == handle)
642                         break;
643                 bchannel = bchannel->next;
644         }
645         return(bchannel);
646 }
647
648 #if 0
649 struct bchannel *find_bchannel_ref(unsigned int ref)
650 {
651         struct bchannel *bchannel = bchannel_first;
652
653         while(bchannel)
654         {
655                 if (bchannel->ref == ref)
656                         break;
657                 bchannel = bchannel->next;
658         }
659         return(bchannel);
660 }
661 #endif
662
663 struct bchannel *alloc_bchannel(unsigned int handle)
664 {
665         struct bchannel **bchannelp = &bchannel_first;
666
667         while(*bchannelp)
668                 bchannelp = &((*bchannelp)->next);
669
670         *bchannelp = (struct bchannel *)calloc(1, sizeof(struct bchannel));
671         if (!*bchannelp)
672                 return(NULL);
673         (*bchannelp)->handle = handle;
674         (*bchannelp)->b_state = BSTATE_IDLE;
675         (*bchannelp)->b_sock = -1;
676                 
677         return(*bchannelp);
678 }
679
680 void free_bchannel(struct bchannel *bchannel)
681 {
682         struct bchannel **temp = &bchannel_first;
683
684         while(*temp)
685         {
686                 if (*temp == bchannel)
687                 {
688                         *temp = (*temp)->next;
689                         if (bchannel->b_sock > -1)
690                                 bchannel_destroy(bchannel);
691                         if (bchannel->call)
692                         {
693                                 if (bchannel->call->bchannel)
694                                         bchannel->call->bchannel = NULL;
695                         }
696                         free(bchannel);
697                         return;
698                 }
699                 temp = &((*temp)->next);
700         }
701 }
702
703