Fix: chan_lcr must use right context attribute for Asterisk version >= 11
[lcr.git] / gsm.cpp
1 /*****************************************************************************\
2 **                                                                           **
3 ** LCR                                                                       **
4 **                                                                           **
5 **---------------------------------------------------------------------------**
6 ** Copyright: Andreas Eversberg                                              **
7 **            MNCC-Interface: Harald Welte                                   **
8 **                                                                           **
9 ** mISDN gsm                                                                 **
10 **                                                                           **
11 \*****************************************************************************/ 
12
13 #include "main.h"
14 #include "mncc.h"
15
16 #ifndef _GNU_SOURCE
17 #define _GNU_SOURCE
18 #endif
19 extern "C" {
20 #include "gsm_audio.h"
21 }
22
23 #include <assert.h>
24
25 #define SOCKET_RETRY_TIMER      5
26
27 //struct lcr_gsm *gsm = NULL;
28
29 int new_callref = 1;
30
31 /* names of MNCC-SAP */
32 static const struct _value_string {
33         int msg_type;
34         const char *name;
35 } mncc_names[] = {
36         { 0,                    "New call ref" },
37         { 1,                    "Codec negotiation" },
38         { MNCC_SETUP_REQ,       "MNCC_SETUP_REQ" },
39         { MNCC_SETUP_IND,       "MNCC_SETUP_IND" },
40         { MNCC_SETUP_RSP,       "MNCC_SETUP_RSP" },
41         { MNCC_SETUP_CNF,       "MNCC_SETUP_CNF" },
42         { MNCC_SETUP_COMPL_REQ, "MNCC_SETUP_COMPL_REQ" },
43         { MNCC_SETUP_COMPL_IND, "MNCC_SETUP_COMPL_IND" },
44         { MNCC_CALL_CONF_IND,   "MNCC_CALL_CONF_IND" },
45         { MNCC_CALL_PROC_REQ,   "MNCC_CALL_PROC_REQ" },
46         { MNCC_PROGRESS_REQ,    "MNCC_PROGRESS_REQ" },
47         { MNCC_ALERT_REQ,       "MNCC_ALERT_REQ" },
48         { MNCC_ALERT_IND,       "MNCC_ALERT_IND" },
49         { MNCC_NOTIFY_REQ,      "MNCC_NOTIFY_REQ" },
50         { MNCC_NOTIFY_IND,      "MNCC_NOTIFY_IND" },
51         { MNCC_DISC_REQ,        "MNCC_DISC_REQ" },
52         { MNCC_DISC_IND,        "MNCC_DISC_IND" },
53         { MNCC_REL_REQ,         "MNCC_REL_REQ" },
54         { MNCC_REL_IND,         "MNCC_REL_IND" },
55         { MNCC_REL_CNF,         "MNCC_REL_CNF" },
56         { MNCC_FACILITY_REQ,    "MNCC_FACILITY_REQ" },
57         { MNCC_FACILITY_IND,    "MNCC_FACILITY_IND" },
58         { MNCC_START_DTMF_IND,  "MNCC_START_DTMF_IND" },
59         { MNCC_START_DTMF_RSP,  "MNCC_START_DTMF_RSP" },
60         { MNCC_START_DTMF_REJ,  "MNCC_START_DTMF_REJ" },
61         { MNCC_STOP_DTMF_IND,   "MNCC_STOP_DTMF_IND" },
62         { MNCC_STOP_DTMF_RSP,   "MNCC_STOP_DTMF_RSP" },
63         { MNCC_MODIFY_REQ,      "MNCC_MODIFY_REQ" },
64         { MNCC_MODIFY_IND,      "MNCC_MODIFY_IND" },
65         { MNCC_MODIFY_RSP,      "MNCC_MODIFY_RSP" },
66         { MNCC_MODIFY_CNF,      "MNCC_MODIFY_CNF" },
67         { MNCC_MODIFY_REJ,      "MNCC_MODIFY_REJ" },
68         { MNCC_HOLD_IND,        "MNCC_HOLD_IND" },
69         { MNCC_HOLD_CNF,        "MNCC_HOLD_CNF" },
70         { MNCC_HOLD_REJ,        "MNCC_HOLD_REJ" },
71         { MNCC_RETRIEVE_IND,    "MNCC_RETRIEVE_IND" },
72         { MNCC_RETRIEVE_CNF,    "MNCC_RETRIEVE_CNF" },
73         { MNCC_RETRIEVE_REJ,    "MNCC_RETRIEVE_REJ" },
74         { MNCC_USERINFO_REQ,    "MNCC_USERINFO_REQ" },
75         { MNCC_USERINFO_IND,    "MNCC_USERINFO_IND" },
76         { MNCC_REJ_REQ,         "MNCC_REJ_REQ" },
77         { MNCC_REJ_IND,         "MNCC_REJ_IND" },
78         { MNCC_PROGRESS_IND,    "MNCC_PROGRESS_IND" },
79         { MNCC_CALL_PROC_IND,   "MNCC_CALL_PROC_IND" },
80         { MNCC_CALL_CONF_REQ,   "MNCC_CALL_CONF_REQ" },
81         { MNCC_START_DTMF_REQ,  "MNCC_START_DTMF_REQ" },
82         { MNCC_STOP_DTMF_REQ,   "MNCC_STOP_DTMF_REQ" },
83         { MNCC_HOLD_REQ,        "MNCC_HOLD_REQ " },
84         { MNCC_RETRIEVE_REQ,    "MNCC_RETRIEVE_REQ" },
85         { MNCC_LCHAN_MODIFY,    "MNCC_LCHAN_MODIFY" },
86         { 0,                    NULL }
87 };
88
89 const char *mncc_name(int value)
90 {
91         int i = 0;
92
93         while (mncc_names[i].name) {
94                 if (mncc_names[i].msg_type == value)
95                         return mncc_names[i].name;
96                 i++;
97         }
98         return "unknown";
99 }
100
101 static int mncc_send(struct lcr_gsm *lcr_gsm, int msg_type, void *data);
102
103 /*
104  * create and send mncc message
105  */
106 struct gsm_mncc *create_mncc(int msg_type, unsigned int callref)
107 {
108         struct gsm_mncc *mncc;
109
110         mncc = (struct gsm_mncc *)MALLOC(sizeof(struct gsm_mncc));
111         mncc->msg_type = msg_type;
112         mncc->callref = callref;
113         return (mncc);
114 }
115 int send_and_free_mncc(struct lcr_gsm *lcr_gsm, unsigned int msg_type, void *data)
116 {
117         int ret = 0;
118
119         if (lcr_gsm) {
120                 ret = mncc_send(lcr_gsm, msg_type, data);
121         }
122         free(data);
123
124         return ret;
125 }
126
127 void Pgsm::send_mncc_rtp_connect(void)
128 {
129         struct gsm_mncc_rtp *nrtp;
130
131         nrtp = (struct gsm_mncc_rtp *) create_mncc(MNCC_RTP_CONNECT, p_g_callref);
132         nrtp->ip = p_g_rtp_ip_remote;
133         nrtp->port = p_g_rtp_port_remote;
134         switch (p_g_media_type) {
135         case MEDIA_TYPE_GSM:
136                 nrtp->payload_msg_type = GSM_TCHF_FRAME;
137                 break;
138         case MEDIA_TYPE_GSM_EFR:
139                 nrtp->payload_msg_type = GSM_TCHF_FRAME_EFR;
140                 break;
141         case MEDIA_TYPE_AMR:
142                 nrtp->payload_msg_type = GSM_TCH_FRAME_AMR;
143                 break;
144         case MEDIA_TYPE_GSM_HR:
145                 nrtp->payload_msg_type = GSM_TCHH_FRAME;
146                 break;
147         }
148         nrtp->payload_type = p_g_payload_type;
149         PDEBUG(DEBUG_GSM, "sending MNCC RTP connect with payload_msg_type=%x, payload_type=%d\n", nrtp->payload_msg_type, nrtp->payload_type);
150         send_and_free_mncc(p_g_lcr_gsm, nrtp->msg_type, nrtp);
151 }
152
153 static int delete_event(struct lcr_work *work, void *instance, int index);
154
155 /*
156  * constructor
157  */
158 Pgsm::Pgsm(int type, char *portname, struct port_settings *settings, struct interface *interface) : Port(type, portname, settings, interface)
159 {
160         p_g_tones = 0;
161         if (interface->is_tones == IS_YES)
162                 p_g_tones = 1;
163         p_g_earlyb = 0;
164         if (interface->is_earlyb == IS_YES)
165                 p_g_earlyb = 1;
166         p_g_rtp_bridge = 0;
167         if (interface->rtp_bridge)
168                 p_g_rtp_bridge = 1;
169         p_g_rtp_payloads = 0;
170         memset(&p_g_samples, 0, sizeof(p_g_samples));
171         p_callerinfo.itype = (interface->extension)?INFO_ITYPE_ISDN_EXTENSION:INFO_ITYPE_ISDN;
172         memset(&p_g_delete, 0, sizeof(p_g_delete));
173         add_work(&p_g_delete, delete_event, this, 0);
174         p_g_lcr_gsm = NULL;
175         p_g_callref = 0;
176         p_g_mode = 0;
177         p_g_gsm_b_sock = -1;
178         p_g_gsm_b_index = -1;
179         p_g_gsm_b_active = 0;
180         p_g_notify_pending = NULL;
181         p_g_setup_pending = NULL;
182         p_g_connect_pending = NULL;
183         p_g_decoder = gsm_audio_create();
184         p_g_encoder = gsm_audio_create();
185         if (!p_g_encoder || !p_g_decoder) {
186                 PERROR("Failed to create GSM audio codec instance\n");
187                 trigger_work(&p_g_delete);
188         }
189         p_g_rxpos = 0;
190         p_g_tch_connected = 0;
191         p_g_media_type = 0;
192
193         PDEBUG(DEBUG_GSM, "Created new GSMPort(%s).\n", portname);
194 }
195
196 /*
197  * destructor
198  */
199 Pgsm::~Pgsm()
200 {
201         PDEBUG(DEBUG_GSM, "Destroyed GSM process(%s).\n", p_name);
202
203         del_work(&p_g_delete);
204
205         /* remove queued message */
206         if (p_g_notify_pending)
207                 message_free(p_g_notify_pending);
208         if (p_g_setup_pending)
209                 message_free(p_g_setup_pending);
210         if (p_g_connect_pending)
211                 message_free(p_g_connect_pending);
212
213         /* close codec */
214         if (p_g_encoder)
215                 gsm_audio_destroy(p_g_encoder);
216         if (p_g_decoder)
217                 gsm_audio_destroy(p_g_decoder);
218 }
219
220
221 /* receive encoded frame from gsm */
222 void Pgsm::frame_receive(void *arg)
223 {
224         struct gsm_data_frame *frame = (struct gsm_data_frame *)arg;
225         unsigned char data[160];
226         int i;
227
228         if (!p_g_decoder)
229                 return;
230
231         if (frame->msg_type != GSM_BAD_FRAME) {
232                 if ((frame->data[0]>>4) != 0xd)
233                         PERROR("received GSM frame with wrong magig 0x%x\n", frame->data[0]>>4);
234         
235                 /* decode */
236                 gsm_audio_decode(p_g_decoder, frame->data, p_g_samples);
237                 for (i = 0; i < 160; i++) {
238                         data[i] = audio_s16_to_law[p_g_samples[i] & 0xffff];
239                 }
240         } else if (p_echotest) {
241                 /* beep on bad frame */
242                 for (i = 0; i < 160; i++) {
243                         if ((i & 3) > 2)
244                                 p_g_samples[i] = 15000;
245                         else
246                                 p_g_samples[i] = -15000;
247                         data[i] = audio_s16_to_law[p_g_samples[i] & 0xffff];
248                 }
249         } else {
250                 /* repeat on bad frame */
251                 for (i = 0; i < 160; i++) {
252                         p_g_samples[i] = (p_g_samples[i] * 14) >> 4;
253                         data[i] = audio_s16_to_law[p_g_samples[i] & 0xffff];
254                 }
255         }
256
257         /* local echo */
258         if (p_echotest)
259                 bridge_rx(data, 160);
260
261         /* send to remote*/
262         bridge_tx(data, 160);
263 }
264
265 /* send traffic to gsm */
266 int Pgsm::bridge_rx(unsigned char *data, int len)
267 {
268         if (p_tone_name[0])
269                 return -EINVAL;
270
271         return audio_send(data, len);
272 }
273
274 int Pgsm::audio_send(unsigned char *data, int len)
275 {
276         unsigned char frame[33];
277
278         /* encoder init failed */
279         if (!p_g_encoder)
280                 return -EINVAL;
281
282         /* (currently) not connected, so don't flood tch! */
283         if (!p_g_tch_connected)
284                 return -EINVAL;
285
286         /* write to rx buffer */
287         while(len--) {
288                 p_g_rxdata[p_g_rxpos++] = audio_law_to_s32[*data++];
289                 if (p_g_rxpos == 160) {
290                         p_g_rxpos = 0;
291
292                         /* encode data */
293                         gsm_audio_encode(p_g_encoder, p_g_rxdata, frame);
294                         frame_send(frame);
295                 }
296         }
297
298         return 0;
299 }
300
301 void Pgsm::frame_send(void *_frame)
302 {
303         unsigned char buffer[sizeof(struct gsm_data_frame) + 33];
304         struct gsm_data_frame *frame = (struct gsm_data_frame *)buffer;
305         
306         frame->msg_type = GSM_TCHF_FRAME;
307         frame->callref = p_g_callref;
308         memcpy(frame->data, _frame, 33);
309
310         if (p_g_lcr_gsm) {
311                 mncc_send(p_g_lcr_gsm, frame->msg_type, frame);
312         }
313 }
314
315 /*
316  * create trace
317  */
318 void gsm_trace_header(const char *interface_name, class Pgsm *port, unsigned int msg_type, int direction)
319 {
320         char msgtext[64];
321         struct interface *interface = interface_first;
322
323         interface = getinterfacebyname(interface_name);
324         if (!interface)
325                 return;
326
327         /* select message and primitive text */
328         SCPY(msgtext, mncc_name(msg_type));
329
330         /* add direction */
331         if (port) {
332                 switch(port->p_type) {
333                 case PORT_TYPE_GSM_BS_OUT:
334                 case PORT_TYPE_GSM_BS_IN:
335                         SCAT(msgtext, " LCR<->BSC");
336                         break;
337                 case PORT_TYPE_GSM_MS_OUT:
338                 case PORT_TYPE_GSM_MS_IN:
339                         SCAT(msgtext, " LCR<->MS");
340                         break;
341                 }
342         } else
343                 SCAT(msgtext, " ----");
344
345         /* init trace with given values */
346         start_trace(-1,
347                     interface,
348                     port?numberrize_callerinfo(port->p_callerinfo.id, port->p_callerinfo.ntype, options.national, options.international):NULL,
349                     port?port->p_dialinginfo.id:NULL,
350                     direction,
351                     CATEGORY_CH,
352                     port?port->p_serial:0,
353                     msgtext);
354 }
355
356 /* modify lchan to given payload type */
357 void Pgsm::modify_lchan(int media_type)
358 {
359         struct gsm_mncc *mode;
360
361         /* already modified to that payload type */
362         if (p_g_media_type == media_type)
363                 return;
364
365         p_g_media_type = media_type;
366         gsm_trace_header(p_interface_name, this, MNCC_LCHAN_MODIFY, DIRECTION_OUT);
367         mode = create_mncc(MNCC_LCHAN_MODIFY, p_g_callref);
368         switch (media_type) {
369         case MEDIA_TYPE_GSM_EFR:
370                 add_trace("speech", "version", "EFR given");
371                 mode->lchan_mode = 0x21; /* GSM V2 */
372                 break;
373         case MEDIA_TYPE_AMR:
374                 add_trace("speech", "version", "AMR given");
375                 mode->lchan_mode = 0x41; /* GSM V3 */
376                 break;
377         default:
378                 add_trace("speech", "version", "Full/Half Rate given");
379                 mode->lchan_mode = 0x01; /* GSM V1 */
380         }
381         mode->lchan_type = 0x02; /* FIXME: unused */
382         add_trace("mode", NULL, "0x%02x", mode->lchan_mode);
383         end_trace();
384         send_and_free_mncc(p_g_lcr_gsm, mode->msg_type, mode);
385 }
386
387 /* CALL PROCEEDING INDICATION (from network) */
388 void Pgsm::call_proc_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
389 {
390         struct lcr_msg *message;
391         struct gsm_mncc *frame;
392
393         gsm_trace_header(p_interface_name, this, msg_type, DIRECTION_IN);
394         end_trace();
395
396         message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_PROCEEDING);
397         message_put(message);
398
399         new_state(PORT_STATE_OUT_PROCEEDING);
400
401         if (p_g_earlyb && !p_g_tch_connected) { /* only if ... */
402                 gsm_trace_header(p_interface_name, this, MNCC_FRAME_RECV, DIRECTION_OUT);
403                 end_trace();
404                 frame = create_mncc(MNCC_FRAME_RECV, p_g_callref);
405                 send_and_free_mncc(p_g_lcr_gsm, frame->msg_type, frame);
406                 p_g_tch_connected = 1;
407         }
408 }
409
410 /* ALERTING INDICATION */
411 void Pgsm::alert_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
412 {
413         struct lcr_msg *message;
414         struct gsm_mncc *frame;
415
416         gsm_trace_header(p_interface_name, this, msg_type, DIRECTION_IN);
417         end_trace();
418
419         message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_ALERTING);
420         message_put(message);
421
422         new_state(PORT_STATE_OUT_ALERTING);
423
424         if (p_g_earlyb && !p_g_tch_connected) { /* only if ... */
425                 gsm_trace_header(p_interface_name, this, MNCC_FRAME_RECV, DIRECTION_OUT);
426                 end_trace();
427                 frame = create_mncc(MNCC_FRAME_RECV, p_g_callref);
428                 send_and_free_mncc(p_g_lcr_gsm, frame->msg_type, frame);
429                 p_g_tch_connected = 1;
430         }
431 }
432
433 /* CONNECT INDICATION */
434 void Pgsm::setup_cnf(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
435 {
436         struct gsm_mncc *resp, *frame;
437         struct lcr_msg *message;
438
439         SCPY(p_connectinfo.id, mncc->connected.number);
440         SCPY(p_connectinfo.imsi, mncc->imsi);
441         p_connectinfo.present = INFO_PRESENT_ALLOWED;
442         p_connectinfo.screen = INFO_SCREEN_NETWORK;
443         p_connectinfo.ntype = INFO_NTYPE_UNKNOWN;
444         SCPY(p_connectinfo.interface, p_interface_name);
445
446         gsm_trace_header(p_interface_name, this, msg_type, DIRECTION_IN);
447         if (p_connectinfo.id[0])
448                 add_trace("connect", "number", "%s", p_connectinfo.id);
449         else if (mncc->imsi[0])
450                 SPRINT(p_connectinfo.id, "imsi-%s", p_connectinfo.imsi);
451         if (mncc->imsi[0])
452                 add_trace("connect", "imsi", "%s", p_connectinfo.imsi);
453         end_trace();
454
455         /* send resp */
456         gsm_trace_header(p_interface_name, this, MNCC_SETUP_COMPL_REQ, DIRECTION_OUT);
457         resp = create_mncc(MNCC_SETUP_COMPL_REQ, p_g_callref);
458         end_trace();
459         send_and_free_mncc(p_g_lcr_gsm, resp->msg_type, resp);
460
461         new_state(PORT_STATE_CONNECT);
462
463         if (!p_g_tch_connected) { /* only if ... */
464                 gsm_trace_header(p_interface_name, this, MNCC_FRAME_RECV, DIRECTION_OUT);
465                 end_trace();
466                 frame = create_mncc(MNCC_FRAME_RECV, p_g_callref);
467                 send_and_free_mncc(p_g_lcr_gsm, frame->msg_type, frame);
468                 p_g_tch_connected = 1;
469         }
470
471         message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_CONNECT);
472         memcpy(&message->param.connectinfo, &p_connectinfo, sizeof(struct connect_info));
473
474         /* if we have a bridge, but not yet modified, the phone accepts out requested payload.
475          * we force the first in list */
476         if (p_g_rtp_bridge) {
477                 if (!p_g_media_type) {
478                         /* modify to first given type */
479                         modify_lchan(p_g_rtp_media_types[0]);
480                         /* also set payload type */
481                         p_g_payload_type = p_g_rtp_payload_types[0];
482                 }
483                 message->param.connectinfo.rtpinfo.media_types[0] = p_g_media_type;
484                 message->param.connectinfo.rtpinfo.payload_types[0] = p_g_payload_type;
485                 message->param.connectinfo.rtpinfo.payloads = 1;
486         }
487
488         if (p_g_rtp_bridge) {
489                 struct gsm_mncc_rtp *rtp;
490
491                 PDEBUG(DEBUG_GSM, "Request RTP peer info, before forwarding connect msg\n");
492                 p_g_connect_pending = message;
493                 rtp = (struct gsm_mncc_rtp *) create_mncc(MNCC_RTP_CREATE, p_g_callref);
494                 send_and_free_mncc(p_g_lcr_gsm, rtp->msg_type, rtp);
495         } else
496                 message_put(message);
497 }
498
499 /* CONNECT ACK INDICATION */
500 void Pgsm::setup_compl_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
501 {
502         struct gsm_mncc *frame;
503
504         gsm_trace_header(p_interface_name, this, msg_type, DIRECTION_IN);
505         end_trace();
506
507         new_state(PORT_STATE_CONNECT);
508
509         if (!p_g_tch_connected) { /* only if ... */
510                 gsm_trace_header(p_interface_name, this, MNCC_FRAME_RECV, DIRECTION_OUT);
511                 end_trace();
512                 frame = create_mncc(MNCC_FRAME_RECV, p_g_callref);
513                 send_and_free_mncc(p_g_lcr_gsm, frame->msg_type, frame);
514                 p_g_tch_connected = 1;
515         }
516 }
517
518 /* DISCONNECT INDICATION */
519 void Pgsm::disc_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
520 {
521         struct lcr_msg *message;
522         int cause = 16, location = 0;
523         struct gsm_mncc *resp;
524
525         gsm_trace_header(p_interface_name, this, msg_type, DIRECTION_IN);
526         if (mncc->fields & MNCC_F_CAUSE) {
527                 location = mncc->cause.location;
528                 cause = mncc->cause.value;
529                 add_trace("cause", "coding", "%d", mncc->cause.coding);
530                 add_trace("cause", "location", "%d", location);
531                 add_trace("cause", "value", "%d", cause);
532         }
533         end_trace();
534
535         /* send release */
536         resp = create_mncc(MNCC_REL_REQ, p_g_callref);
537         gsm_trace_header(p_interface_name, this, MNCC_REL_REQ, DIRECTION_OUT);
538 #if 0
539         resp->fields |= MNCC_F_CAUSE;
540         resp->cause.coding = 3;
541         resp->cause.location = 1;
542         resp->cause.value = cause;
543         add_trace("cause", "coding", "%d", resp->cause.coding);
544         add_trace("cause", "location", "%d", resp->cause.location);
545         add_trace("cause", "value", "%d", resp->cause.value);
546 #endif
547         end_trace();
548         send_and_free_mncc(p_g_lcr_gsm, resp->msg_type, resp);
549
550         /* sending release to endpoint */
551         while(p_epointlist) {
552                 message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
553                 message->param.disconnectinfo.cause = cause;
554                 message->param.disconnectinfo.location = location;
555                 message_put(message);
556                 /* remove epoint */
557                 free_epointlist(p_epointlist);
558         }
559         new_state(PORT_STATE_RELEASE);
560         trigger_work(&p_g_delete);
561 }
562
563 /* CC_RELEASE INDICATION */
564 void Pgsm::rel_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
565 {
566         int location = 0, cause = 16;
567         struct lcr_msg *message;
568
569         gsm_trace_header(p_interface_name, this, msg_type, DIRECTION_IN);
570         if (mncc->fields & MNCC_F_CAUSE) {
571                 location = mncc->cause.location;
572                 cause = mncc->cause.value;
573                 add_trace("cause", "coding", "%d", mncc->cause.coding);
574                 add_trace("cause", "location", "%d", mncc->cause.location);
575                 add_trace("cause", "value", "%d", mncc->cause.value);
576         }
577         end_trace();
578
579         /* sending release to endpoint */
580         while(p_epointlist) {
581                 message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
582                 message->param.disconnectinfo.cause = cause;
583                 message->param.disconnectinfo.location = location;
584                 message_put(message);
585                 /* remove epoint */
586                 free_epointlist(p_epointlist);
587         }
588         new_state(PORT_STATE_RELEASE);
589         trigger_work(&p_g_delete);
590 }
591
592 /* NOTIFY INDICATION */
593 void Pgsm::notify_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
594 {
595         struct lcr_msg *message;
596
597         gsm_trace_header(p_interface_name, this, msg_type, DIRECTION_IN);
598         add_trace("notify", NULL, "%d", mncc->notify);
599         end_trace();
600
601         message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_NOTIFY);
602         message->param.notifyinfo.notify = mncc->notify;
603         message_put(message);
604 }
605
606 /* MESSAGE_NOTIFY */
607 void Pgsm::message_notify(unsigned int epoint_id, int message_id, union parameter *param)
608 {
609         struct gsm_mncc *mncc;
610         int notify;
611
612 //      printf("if = %d\n", param->notifyinfo.notify);
613         if (param->notifyinfo.notify>INFO_NOTIFY_NONE) {
614                 notify = param->notifyinfo.notify & 0x7f;
615                 if (p_state!=PORT_STATE_CONNECT /*&& p_state!=PORT_STATE_IN_PROCEEDING*/ && p_state!=PORT_STATE_IN_ALERTING) {
616                         /* queue notification */
617                         if (p_g_notify_pending)
618                                 message_free(p_g_notify_pending);
619                         p_g_notify_pending = message_create(ACTIVE_EPOINT(p_epointlist), p_serial, EPOINT_TO_PORT, message_id);
620                         memcpy(&p_g_notify_pending->param, param, sizeof(union parameter));
621                 } else {
622                         /* sending notification */
623                         gsm_trace_header(p_interface_name, this, MNCC_NOTIFY_REQ, DIRECTION_OUT);
624                         add_trace("notify", NULL, "%d", notify);
625                         end_trace();
626                         mncc = create_mncc(MNCC_NOTIFY_REQ, p_g_callref);
627                         mncc->notify = notify;
628                         send_and_free_mncc(p_g_lcr_gsm, mncc->msg_type, mncc);
629                 }
630         }
631 }
632
633 /* RTP create indication */
634 void Pgsm::rtp_create_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
635 {
636         struct gsm_mncc_rtp *rtp = (struct gsm_mncc_rtp *) mncc;
637
638         /* send queued setup, as we received remote RTP info */
639         if (p_g_setup_pending) {
640                 struct lcr_msg *message;
641
642                 message = p_g_setup_pending;
643                 PDEBUG(DEBUG_GSM, "Got RTP peer info (%08x,%d) forwarding setup\n", rtp->ip, rtp->port);
644                 message->param.setup.rtpinfo.ip = rtp->ip;
645                 message->param.setup.rtpinfo.port = rtp->port;
646                 message_put(message);
647                 p_g_setup_pending = NULL;
648         }
649         if (p_g_connect_pending) {
650                 PDEBUG(DEBUG_GSM, "Got RTP peer info (%08x,%d) connecting RTP... \n", rtp->ip, rtp->port);
651                 send_mncc_rtp_connect();
652         }
653 }
654
655 /* RTP connect indication */
656 void Pgsm::rtp_connect_ind(unsigned int msg_type, unsigned int callref, struct gsm_mncc *mncc)
657 {
658         struct lcr_msg *message;
659         struct gsm_mncc_rtp *rtp = (struct gsm_mncc_rtp *) mncc;
660
661         if (p_g_connect_pending) {
662                 message = p_g_connect_pending;
663                 PDEBUG(DEBUG_GSM, "Got RTP peer info (%08x,%d) forwarding connect\n", rtp->ip, rtp->port);
664                 message->param.connectinfo.rtpinfo.ip = rtp->ip;
665                 message->param.connectinfo.rtpinfo.port = rtp->port;
666                 message_put(message);
667                 p_g_connect_pending = NULL;
668         }
669 }
670
671 /* MESSAGE_PROGRESS */
672 void Pgsm::message_progress(unsigned int epoint_id, int message_id, union parameter *param)
673 {
674         if (param->progressinfo.progress == 8) {
675                 PDEBUG(DEBUG_GSM, "Remote provides tones for us\n");
676                 p_g_tones = 1;
677         }
678
679         if (param->progressinfo.rtpinfo.port) {
680                 PDEBUG(DEBUG_GSM, "PROGRESS with RTP peer info, sent to BSC (%08x,%d) with media %d, pt %d\n", param->progressinfo.rtpinfo.ip, param->progressinfo.rtpinfo.port, param->progressinfo.rtpinfo.media_types[0], param->progressinfo.rtpinfo.payload_types[0]);
681
682                 /* modify channel to givne type, also sets media type */
683                 modify_lchan(param->progressinfo.rtpinfo.media_types[0]);
684
685                 /* connect RTP */
686                 p_g_rtp_ip_remote = param->progressinfo.rtpinfo.ip;
687                 p_g_rtp_port_remote = param->progressinfo.rtpinfo.port;
688                 /* p_g_media_type is already set by modify_lchan() */
689                 p_g_payload_type = param->progressinfo.rtpinfo.payload_types[0];
690                 send_mncc_rtp_connect();
691         }
692 }
693
694 /* MESSAGE_ALERTING */
695 void Pgsm::message_alerting(unsigned int epoint_id, int message_id, union parameter *param)
696 {
697         struct gsm_mncc *mncc;
698
699         /* send alert */
700         gsm_trace_header(p_interface_name, this, MNCC_ALERT_REQ, DIRECTION_OUT);
701         mncc = create_mncc(MNCC_ALERT_REQ, p_g_callref);
702         if (p_g_tones) {
703                 mncc->fields |= MNCC_F_PROGRESS;
704                 mncc->progress.coding = 3; /* GSM */
705                 mncc->progress.location = 1;
706                 mncc->progress.descr = 8;
707                 add_trace("progress", "coding", "%d", mncc->progress.coding);
708                 add_trace("progress", "location", "%d", mncc->progress.location);
709                 add_trace("progress", "descr", "%d", mncc->progress.descr);
710         }
711         end_trace();
712         send_and_free_mncc(p_g_lcr_gsm, mncc->msg_type, mncc);
713
714         new_state(PORT_STATE_IN_ALERTING);
715
716         if (p_g_tones && !p_g_tch_connected) { /* only if ... */
717                 gsm_trace_header(p_interface_name, this, MNCC_FRAME_RECV, DIRECTION_OUT);
718                 end_trace();
719                 mncc = create_mncc(MNCC_FRAME_RECV, p_g_callref);
720                 send_and_free_mncc(p_g_lcr_gsm, mncc->msg_type, mncc);
721                 p_g_tch_connected = 1;
722         }
723 }
724
725 /* MESSAGE_CONNECT */
726 void Pgsm::message_connect(unsigned int epoint_id, int message_id, union parameter *param)
727 {
728         struct gsm_mncc *mncc;
729
730         /* copy connected information */
731         memcpy(&p_connectinfo, &param->connectinfo, sizeof(p_connectinfo));
732         /* screen outgoing caller id */
733         do_screen(1, p_connectinfo.id, sizeof(p_connectinfo.id), &p_connectinfo.ntype, &p_connectinfo.present, p_interface_name);
734
735         /* send connect */
736         mncc = create_mncc(MNCC_SETUP_RSP, p_g_callref);
737         gsm_trace_header(p_interface_name, this, MNCC_SETUP_RSP, DIRECTION_OUT);
738         /* caller information */
739         mncc->fields |= MNCC_F_CONNECTED;
740         mncc->connected.plan = 1;
741         switch (p_callerinfo.ntype) {
742                 case INFO_NTYPE_UNKNOWN:
743                 mncc->connected.type = 0x0;
744                 break;
745                 case INFO_NTYPE_INTERNATIONAL:
746                 mncc->connected.type = 0x1;
747                 break;
748                 case INFO_NTYPE_NATIONAL:
749                 mncc->connected.type = 0x2;
750                 break;
751                 case INFO_NTYPE_SUBSCRIBER:
752                 mncc->connected.type = 0x4;
753                 break;
754                 default: /* INFO_NTYPE_NOTPRESENT */
755                 mncc->fields &= ~MNCC_F_CONNECTED;
756                 break;
757         }
758         switch (p_callerinfo.screen) {
759                 case INFO_SCREEN_USER:
760                 mncc->connected.screen = 0;
761                 break;
762                 default: /* INFO_SCREEN_NETWORK */
763                 mncc->connected.screen = 3;
764                 break;
765         }
766         switch (p_callerinfo.present) {
767                 case INFO_PRESENT_ALLOWED:
768                 mncc->connected.present = 0;
769                 break;
770                 case INFO_PRESENT_RESTRICTED:
771                 mncc->connected.present = 1;
772                 break;
773                 default: /* INFO_PRESENT_NOTAVAIL */
774                 mncc->connected.present = 2;
775                 break;
776         }
777         if (mncc->fields & MNCC_F_CONNECTED) {
778                 SCPY(mncc->connected.number, p_connectinfo.id);
779                 add_trace("connected", "type", "%d", mncc->connected.type);
780                 add_trace("connected", "plan", "%d", mncc->connected.plan);
781                 add_trace("connected", "present", "%d", mncc->connected.present);
782                 add_trace("connected", "screen", "%d", mncc->connected.screen);
783                 add_trace("connected", "number", "%s", mncc->connected.number);
784         }
785         end_trace();
786         send_and_free_mncc(p_g_lcr_gsm, mncc->msg_type, mncc);
787
788         new_state(PORT_STATE_CONNECT_WAITING);
789
790         if (param->connectinfo.rtpinfo.port) {
791                 PDEBUG(DEBUG_GSM, "CONNECT with RTP peer info, sent to BSC (%08x,%d)\n", param->connectinfo.rtpinfo.ip, param->connectinfo.rtpinfo.port);
792
793                 /* modify channel to givne type, also sets media type */
794                 modify_lchan(param->connectinfo.rtpinfo.media_types[0]);
795
796                 /* connect RTP */
797                 p_g_rtp_ip_remote = param->connectinfo.rtpinfo.ip;
798                 p_g_rtp_port_remote = param->connectinfo.rtpinfo.port;
799                 /* p_g_media_type is already set by modify_lchan() */
800                 p_g_payload_type = param->connectinfo.rtpinfo.payload_types[0];
801                 send_mncc_rtp_connect();
802         }
803 }
804
805 /* MESSAGE_DISCONNECT */
806 void Pgsm::message_disconnect(unsigned int epoint_id, int message_id, union parameter *param)
807 {
808         struct gsm_mncc *mncc;
809
810         /* send disconnect */
811         mncc = create_mncc(MNCC_DISC_REQ, p_g_callref);
812         gsm_trace_header(p_interface_name, this, MNCC_DISC_REQ, DIRECTION_OUT);
813         if (p_g_tones) {
814                 mncc->fields |= MNCC_F_PROGRESS;
815                 mncc->progress.coding = 3; /* GSM */
816                 mncc->progress.location = 1;
817                 mncc->progress.descr = 8;
818                 add_trace("progress", "coding", "%d", mncc->progress.coding);
819                 add_trace("progress", "location", "%d", mncc->progress.location);
820                 add_trace("progress", "descr", "%d", mncc->progress.descr);
821         }
822         mncc->fields |= MNCC_F_CAUSE;
823         mncc->cause.coding = 3;
824         mncc->cause.location = param->disconnectinfo.location;
825         mncc->cause.value = param->disconnectinfo.cause;
826         add_trace("cause", "coding", "%d", mncc->cause.coding);
827         add_trace("cause", "location", "%d", mncc->cause.location);
828         add_trace("cause", "value", "%d", mncc->cause.value);
829         end_trace();
830         send_and_free_mncc(p_g_lcr_gsm, mncc->msg_type, mncc);
831
832         new_state(PORT_STATE_OUT_DISCONNECT);
833
834         if (p_g_tones && !p_g_tch_connected) { /* only if ... */
835                 gsm_trace_header(p_interface_name, this, MNCC_FRAME_RECV, DIRECTION_OUT);
836                 end_trace();
837                 mncc = create_mncc(MNCC_FRAME_RECV, p_g_callref);
838                 send_and_free_mncc(p_g_lcr_gsm, mncc->msg_type, mncc);
839                 p_g_tch_connected = 1;
840         }
841 }
842
843
844 /* MESSAGE_RELEASE */
845 void Pgsm::message_release(unsigned int epoint_id, int message_id, union parameter *param)
846 {
847         struct gsm_mncc *mncc;
848
849         /* send release */
850         mncc = create_mncc(MNCC_REL_REQ, p_g_callref);
851         gsm_trace_header(p_interface_name, this, MNCC_REL_REQ, DIRECTION_OUT);
852         mncc->fields |= MNCC_F_CAUSE;
853         mncc->cause.coding = 3;
854         mncc->cause.location = param->disconnectinfo.location;
855         mncc->cause.value = param->disconnectinfo.cause;
856         add_trace("cause", "coding", "%d", mncc->cause.coding);
857         add_trace("cause", "location", "%d", mncc->cause.location);
858         add_trace("cause", "value", "%d", mncc->cause.value);
859         end_trace();
860         send_and_free_mncc(p_g_lcr_gsm, mncc->msg_type, mncc);
861
862         new_state(PORT_STATE_RELEASE);
863         trigger_work(&p_g_delete);
864         return;
865 }
866
867 /*
868  * endpoint sends messages to the port
869  */
870 int Pgsm::message_epoint(unsigned int epoint_id, int message_id, union parameter *param)
871 {
872         int ret = 0;
873
874         if (Port::message_epoint(epoint_id, message_id, param))
875                 return 1;
876
877         switch(message_id) {
878                 case MESSAGE_NOTIFY: /* display and notifications */
879                 ret = 1;
880                 message_notify(epoint_id, message_id, param);
881                 break;
882
883 //              case MESSAGE_FACILITY: /* facility message */
884 //              message_facility(epoint_id, message_id, param);
885 //              break;
886
887                 case MESSAGE_PROCEEDING: /* message not handles */
888                 ret = 1;
889                 break;
890
891                 case MESSAGE_PROGRESS:
892                 ret = 1;
893                 message_progress(epoint_id, message_id, param);
894                 break;
895
896                 case MESSAGE_ALERTING: /* call of endpoint is ringing */
897                 ret = 1;
898                 if (p_state!=PORT_STATE_IN_PROCEEDING)
899                         break;
900                 message_alerting(epoint_id, message_id, param);
901                 if (p_g_notify_pending) {
902                         /* send pending notify message during connect */
903                         message_notify(ACTIVE_EPOINT(p_epointlist), p_g_notify_pending->type, &p_g_notify_pending->param);
904                         message_free(p_g_notify_pending);
905                         p_g_notify_pending = NULL;
906                 }
907                 break;
908
909                 case MESSAGE_CONNECT: /* call of endpoint is connected */
910                 ret = 1;
911                 if (p_state!=PORT_STATE_IN_PROCEEDING
912                  && p_state!=PORT_STATE_IN_ALERTING)
913                         break;
914                 message_connect(epoint_id, message_id, param);
915                 if (p_g_notify_pending) {
916                         /* send pending notify message during connect */
917                         message_notify(ACTIVE_EPOINT(p_epointlist), p_g_notify_pending->type, &p_g_notify_pending->param);
918                         message_free(p_g_notify_pending);
919                         p_g_notify_pending = NULL;
920                 }
921                 break;
922
923                 case MESSAGE_DISCONNECT: /* call has been disconnected */
924                 ret = 1;
925                 if (p_state!=PORT_STATE_IN_PROCEEDING
926                  && p_state!=PORT_STATE_IN_ALERTING
927                  && p_state!=PORT_STATE_OUT_SETUP
928                  && p_state!=PORT_STATE_OUT_OVERLAP
929                  && p_state!=PORT_STATE_OUT_PROCEEDING
930                  && p_state!=PORT_STATE_OUT_ALERTING
931                  && p_state!=PORT_STATE_CONNECT
932                  && p_state!=PORT_STATE_CONNECT_WAITING)
933                         break;
934                 message_disconnect(epoint_id, message_id, param);
935                 break;
936
937                 case MESSAGE_RELEASE: /* release isdn port */
938                 ret = 1;
939                 if (p_state==PORT_STATE_RELEASE)
940                         break;
941                 message_release(epoint_id, message_id, param);
942                 break;
943
944         }
945
946         return ret;
947 }
948
949 /* deletes only if l3id is release, otherwhise it will be triggered then */
950 static int delete_event(struct lcr_work *work, void *instance, int index)
951 {
952         class Pgsm *gsmport = (class Pgsm *)instance;
953
954         delete gsmport;
955
956         return 0;
957 }
958
959 int gsm_exit(int rc)
960 {
961         return(rc);
962 }
963
964 int gsm_init(void)
965 {
966         /* seed the PRNG */
967         srand(time(NULL));
968
969         return 0;
970 }
971
972 /*
973  * MNCC interface
974  */
975
976 static int mncc_q_enqueue(struct lcr_gsm *lcr_gsm, struct gsm_mncc *mncc, unsigned int len)
977 {
978         struct mncc_q_entry *qe;
979
980         qe = (struct mncc_q_entry *) MALLOC(sizeof(*qe)+sizeof(*mncc)+len);
981         if (!qe)
982                 return -ENOMEM;
983
984         qe->next = NULL;
985         qe->len = len;
986         memcpy(qe->data, mncc, len);
987
988         /* in case of empty list ... */
989         if (!lcr_gsm->mncc_q_hd && !lcr_gsm->mncc_q_tail) {
990                 /* the list head and tail both point to the new qe */
991                 lcr_gsm->mncc_q_hd = lcr_gsm->mncc_q_tail = qe;
992         } else {
993                 /* append to tail of list */
994                 lcr_gsm->mncc_q_tail->next = qe;
995                 lcr_gsm->mncc_q_tail = qe;
996         }
997
998         lcr_gsm->mncc_lfd.when |= LCR_FD_WRITE;
999
1000         return 0;
1001 }
1002
1003 static struct mncc_q_entry *mncc_q_dequeue(struct lcr_gsm *lcr_gsm)
1004 {
1005         struct mncc_q_entry *qe = lcr_gsm->mncc_q_hd;
1006         if (!qe)
1007                 return NULL;
1008
1009         /* dequeue the successfully sent message */
1010         lcr_gsm->mncc_q_hd = qe->next;
1011         if (!qe)
1012                 return NULL;
1013         if (qe == lcr_gsm->mncc_q_tail)
1014                 lcr_gsm->mncc_q_tail = NULL;
1015
1016         return qe;
1017 }
1018
1019 /* routine called by LCR code if it wants to send a message to OpenBSC */
1020 static int mncc_send(struct lcr_gsm *lcr_gsm, int msg_type, void *data)
1021 {
1022         int len = 0;
1023
1024         /* FIXME: the caller should provide this */
1025         switch (msg_type) {
1026         case GSM_TCHF_FRAME:
1027                 len = sizeof(struct gsm_data_frame) + 33;
1028                 break;
1029         default:
1030                 len = sizeof(struct gsm_mncc);
1031                 break;
1032         }
1033                 
1034         return mncc_q_enqueue(lcr_gsm, (struct gsm_mncc *)data, len);
1035 }
1036
1037 /* close MNCC socket */
1038 static int mncc_fd_close(struct lcr_gsm *lcr_gsm, struct lcr_fd *lfd)
1039 {
1040         class Port *port;
1041         class Pgsm *pgsm = NULL;
1042         struct lcr_msg *message;
1043
1044         PERROR("Lost MNCC socket, retrying in %u seconds\n", SOCKET_RETRY_TIMER);
1045         close(lfd->fd);
1046         unregister_fd(lfd);
1047         lfd->fd = -1;
1048
1049         /* free all the calls that were running through the MNCC interface */
1050         port = port_first;
1051         while(port) {
1052                 if ((port->p_type & PORT_CLASS_mISDN_MASK) == PORT_CLASS_GSM) {
1053                         pgsm = (class Pgsm *)port;
1054                         if (pgsm->p_g_lcr_gsm == lcr_gsm) {
1055                                 message = message_create(pgsm->p_serial, ACTIVE_EPOINT(pgsm->p_epointlist), PORT_TO_EPOINT, MESSAGE_RELEASE);
1056                                 message->param.disconnectinfo.cause = 27; // temp. unavail.
1057                                 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
1058                                 message_put(message);
1059                                 pgsm->new_state(PORT_STATE_RELEASE);
1060                                 trigger_work(&pgsm->p_g_delete);
1061                         }
1062                 }
1063                 port = port->next;
1064         }
1065
1066         /* flush the queue */
1067         while (mncc_q_dequeue(lcr_gsm))
1068                 ;
1069
1070         /* start the re-connect timer */
1071         schedule_timer(&lcr_gsm->socket_retry, SOCKET_RETRY_TIMER, 0);
1072
1073         return 0;
1074 }
1075
1076 /* write to OpenBSC via MNCC socket */
1077 static int mncc_fd_write(struct lcr_fd *lfd, void *inst, int idx)
1078 {
1079         struct lcr_gsm *lcr_gsm = (struct lcr_gsm *) inst;
1080         struct mncc_q_entry *qe, *qe2;
1081         int rc;
1082
1083         while (1) {
1084                 qe = lcr_gsm->mncc_q_hd;
1085                 if (!qe) {
1086                         lfd->when &= ~LCR_FD_WRITE;
1087                         break;
1088                 }
1089                 rc = write(lfd->fd, qe->data, qe->len);
1090                 if (rc == 0)
1091                         return mncc_fd_close(lcr_gsm, lfd);
1092                 if (rc < 0)
1093                         return rc;
1094                 if (rc < (int)qe->len)
1095                         return -1;
1096                 /* dequeue the successfully sent message */
1097                 qe2 = mncc_q_dequeue(lcr_gsm);
1098                 assert(qe == qe2);
1099                 free(qe);
1100         }
1101         return 0;
1102 }
1103
1104 /* read from OpenBSC via MNCC socket */
1105 static int mncc_fd_read(struct lcr_fd *lfd, void *inst, int idx)
1106 {
1107         struct lcr_gsm *lcr_gsm = (struct lcr_gsm *) inst;
1108         int rc;
1109         static char buf[sizeof(struct gsm_mncc)+1024];
1110         struct gsm_mncc *mncc_prim = (struct gsm_mncc *) buf;
1111
1112         memset(buf, 0, sizeof(buf));
1113         rc = recv(lfd->fd, buf, sizeof(buf), 0);
1114         if (rc == 0)
1115                 return mncc_fd_close(lcr_gsm, lfd);
1116         if (rc < 0)
1117                 return rc;
1118
1119         /* Hand the MNCC message into LCR */
1120         switch (lcr_gsm->type) {
1121 #ifdef WITH_GSM_BS
1122         case LCR_GSM_TYPE_NETWORK:
1123                 return message_bsc(lcr_gsm, mncc_prim->msg_type, mncc_prim);
1124 #endif
1125 #ifdef WITH_GSM_MS
1126         case LCR_GSM_TYPE_MS:
1127                 return message_ms(lcr_gsm, mncc_prim->msg_type, mncc_prim);
1128 #endif
1129         default:
1130                 return 0;
1131         }
1132 }
1133
1134 /* file descriptor callback if we can read or write form MNCC socket */
1135 static int mncc_fd_cb(struct lcr_fd *lfd, unsigned int what, void *inst, int idx)
1136 {
1137         int rc = 0;
1138
1139         if (what & LCR_FD_READ)
1140                 rc = mncc_fd_read(lfd, inst, idx);
1141         if (rc < 0)
1142                 return rc;
1143
1144         if (what & LCR_FD_WRITE)
1145                 rc = mncc_fd_write(lfd, inst, idx);
1146
1147         return rc;
1148 }
1149
1150 int mncc_socket_retry_cb(struct lcr_timer *timer, void *inst, int index)
1151 {
1152         struct lcr_gsm *lcr_gsm = (struct lcr_gsm *) inst;
1153         int fd, rc;
1154
1155         lcr_gsm->mncc_lfd.fd = -1;
1156
1157         fd = socket(PF_UNIX, SOCK_SEQPACKET, 0);
1158         if (fd < 0) {
1159                 PERROR("Cannot create SEQPACKET socket, giving up!\n");
1160                 return fd;
1161         }
1162
1163         rc = connect(fd, (struct sockaddr *) &lcr_gsm->sun,
1164                      sizeof(lcr_gsm->sun));
1165         if (rc < 0) {
1166                 PERROR("Could not connect to MNCC socket %s, "
1167                         "retrying in %u seconds\n", lcr_gsm->sun.sun_path,
1168                         SOCKET_RETRY_TIMER);
1169                 close(fd);
1170                 schedule_timer(&lcr_gsm->socket_retry, SOCKET_RETRY_TIMER, 0);
1171         } else {
1172                 PDEBUG(DEBUG_GSM, "Connected to MNCC socket %s!\n", lcr_gsm->sun.sun_path);
1173                 lcr_gsm->mncc_lfd.fd = fd;
1174                 register_fd(&lcr_gsm->mncc_lfd, LCR_FD_READ, &mncc_fd_cb, lcr_gsm, 0);
1175         }
1176
1177         return 0;
1178 }
1179