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