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