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