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