Added GSM IMSI dialing by using dialing "imsi-<number>".
[lcr.git] / socket_server.c
1 /*****************************************************************************\
2 **                                                                           **
3 ** Linux Call Router                                                         **
4 **                                                                           **
5 **---------------------------------------------------------------------------**
6 ** Copyright: Andreas Eversberg                                              **
7 **                                                                           **
8 ** Socket link server                                                        **
9 **                                                                           **
10 \*****************************************************************************/
11
12 #include "main.h"
13 #include <sys/socket.h>
14 #include <sys/un.h>
15 #include <curses.h>
16
17
18 char socket_name[128];
19 int sock = -1;
20 struct sockaddr_un sock_address;
21
22 struct admin_list *admin_first = NULL;
23
24 /*
25  * initialize admin socket 
26  */
27 int admin_init(void)
28 {
29         unsigned int on = 1;
30
31         /* open and bind socket */
32         if ((sock = socket(PF_UNIX, SOCK_STREAM, 0)) < 0)
33         {
34                 PERROR("Failed to create admin socket. (errno=%d)\n", errno);
35                 return(-1);
36         }
37         fhuse++;
38         memset(&sock_address, 0, sizeof(sock_address));
39         SPRINT(socket_name, SOCKET_NAME, options.lock);
40         sock_address.sun_family = AF_UNIX;
41         UCPY(sock_address.sun_path, socket_name);
42         unlink(socket_name);
43         if (bind(sock, (struct sockaddr *)(&sock_address), SUN_LEN(&sock_address)) < 0)
44         {
45                 close(sock);
46                 unlink(socket_name);
47                 fhuse--;
48                 sock = -1;
49                 PERROR("Failed to bind admin socket to \"%s\". (errno=%d)\n", sock_address.sun_path, errno);
50                 return(-1);
51         }
52         if (listen(sock, 5) < 0)
53         {
54                 close(sock);
55                 unlink(socket_name);
56                 fhuse--;
57                 sock = -1;
58                 PERROR("Failed to listen to socket \"%s\". (errno=%d)\n", sock_address.sun_path, errno);
59                 return(-1);
60         }
61         if (ioctl(sock, FIONBIO, (unsigned char *)(&on)) < 0)
62         {
63                 close(sock);
64                 unlink(socket_name);
65                 fhuse--;
66                 sock = -1;
67                 PERROR("Failed to set socket \"%s\" into non-blocking mode. (errno=%d)\n", sock_address.sun_path, errno);
68                 return(-1);
69         }
70         if (chmod(socket_name, options.socketrights) < 0)
71         {
72                 PERROR("Failed to change socket rigts to %d. (errno=%d)\n", options.socketrights, errno);
73         }
74         return(0);
75 }
76
77
78 /*
79  * free connection
80  * also releases all remote joins
81  */
82 void free_connection(struct admin_list *admin)
83 {
84         struct admin_queue *response;
85         void *temp;
86         union parameter param;
87         class Join *join, *joinnext;
88         struct mISDNport *mISDNport;
89         int i, ii;
90
91         /* free remote joins */
92         if (admin->remote_name[0])
93         {
94                 start_trace(-1,
95                         NULL,
96                         NULL,
97                         NULL,
98                         DIRECTION_NONE,
99                         0,
100                         0,
101                         "REMOTE APP release");
102                 add_trace("app", "name", "%s", admin->remote_name);
103                 end_trace();
104                 /* release all exported channels */
105                 mISDNport = mISDNport_first;
106                 while(mISDNport)
107                 {
108                         i = 0;
109                         ii = mISDNport->b_num;
110                         while(i < ii)
111                         {
112                                 if (mISDNport->b_remote_id[i] == admin->sock)
113                                 {
114                                         mISDNport->b_state[i] = B_STATE_IDLE;
115                                         mISDNport->b_timer[i] = 0;
116                                         mISDNport->b_remote_id[i] = 0;
117                                         mISDNport->b_remote_ref[i] = 0;
118                                 }
119                                 i++;
120                         }
121                         mISDNport = mISDNport->next;
122                 }
123                 /* release join */
124                 join = join_first;
125                 while(join)
126                 {
127                         joinnext = join->next;
128                         if (join->j_type==JOIN_TYPE_REMOTE) if (((class JoinRemote *)join)->j_remote_id == admin->sock)
129                         {
130                                 memset(&param, 0, sizeof(param));
131                                 param.disconnectinfo.cause = CAUSE_OUTOFORDER;
132                                 param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
133                                 ((class JoinRemote *)join)->message_remote(MESSAGE_RELEASE, &param);
134                                 /* join is now destroyed, so we go to next join */
135                         }
136                         join = joinnext;
137                 }
138         }
139
140         if (admin->sock >= 0)
141         {
142                 close(admin->sock);
143                 fhuse--;
144         }
145 //      printf("new\n", response);
146         response = admin->response;
147         while (response)
148         {
149 //#warning
150 //      printf("%x\n", response);
151                 temp = response->next;
152                 FREE(response, 0);
153                 memuse--;
154                 response = (struct admin_queue *)temp;
155         }
156 //      printf("new2\n", response);
157         FREE(admin, 0);
158 //      printf("new3\n", response);
159         memuse--;
160 }
161
162
163 /*
164  * cleanup admin socket 
165  */
166 void admin_cleanup(void)
167 {
168         struct admin_list *admin, *next;;
169
170         admin = admin_first;
171         while(admin)
172         {
173 //printf("clean\n");
174                 next = admin->next;
175                 free_connection(admin);
176                 admin = next;
177         }
178
179         if (sock >= 0)
180         {
181                 close(sock);
182                 fhuse--;
183         }
184
185         unlink(socket_name);
186 }
187
188
189 /*
190  * do interface reload
191  */
192 int admin_interface(struct admin_queue **responsep)
193 {
194         struct admin_queue      *response;      /* response pointer */
195         const char              *err_txt = "";
196         int                     err = 0;
197
198         if (read_interfaces())
199         {
200                 relink_interfaces();
201                 free_interfaces(interface_first);
202                 interface_first = interface_newlist;
203                 interface_newlist = NULL;
204         } else
205         {
206                 err_txt = interface_error;
207                 err = -1;
208         }
209         /* create state response */
210         response = (struct admin_queue *)MALLOC(sizeof(struct admin_queue)+sizeof(admin_message));
211         memuse++;
212         response->num = 1;
213         /* message */
214         response->am[0].message = ADMIN_RESPONSE_CMD_INTERFACE;
215         /* error */
216         response->am[0].u.x.error = err;
217         /* message */
218         SCPY(response->am[0].u.x.message, err_txt);
219         /* attach to response chain */
220         *responsep = response;
221         responsep = &response->next;
222
223         return(0);
224 }
225
226
227 /*
228  * do route reload
229  */
230 int admin_route(struct admin_queue **responsep)
231 {
232         struct route_ruleset    *ruleset_new;
233         struct admin_queue      *response;      /* response pointer */
234         char                    err_txt[256] = "";
235         int                     err = 0;
236 #if 0
237         int                     n;
238 #endif
239         class EndpointAppPBX    *apppbx;
240
241 #if 0
242         n = 0;
243         apppbx = apppbx_first;
244         while(apppbx)
245         {
246                 n++;
247                 apppbx = apppbx->next;
248         }
249         if (apppbx_first)
250         {
251                 SPRINT(err_txt, "Cannot reload routing, because %d endpoints active\n", n);
252                 err = -1;
253                 goto response;
254         }
255 #endif
256         if (!(ruleset_new = ruleset_parse()))
257         {
258                 SPRINT(err_txt, ruleset_error);
259                 err = -1;
260                 goto response;
261         }
262         ruleset_free(ruleset_first);
263         ruleset_first = ruleset_new;
264         ruleset_main = getrulesetbyname("main");
265         if (!ruleset_main)
266         {
267                 SPRINT(err_txt, "Ruleset reloaded, but rule 'main' not found.\n");
268                 err = -1;
269         }
270         apppbx = apppbx_first;
271         while(apppbx)
272         {
273                 if (apppbx->e_action)
274                 {
275                         switch(apppbx->e_action->index)
276                         {
277                                 case ACTION_INTERNAL:
278                                 apppbx->e_action = &action_internal;
279                                 break;
280                                 case ACTION_EXTERNAL:
281                                 apppbx->e_action = &action_external;
282                                 break;
283                                 case ACTION_REMOTE:
284                                 apppbx->e_action = &action_remote;
285                                 break;
286                                 case ACTION_VBOX_RECORD:
287                                 apppbx->e_action = &action_vbox;
288                                 break;
289                                 case ACTION_PARTYLINE:
290                                 apppbx->e_action = &action_partyline;
291                                 break;
292                                 default:
293                                 goto release;
294                         }
295                 } else if (apppbx->e_state != EPOINT_STATE_CONNECT)
296                 {
297                         release:
298                         apppbx->e_callback = 0;
299                         apppbx->e_action = NULL;
300                         apppbx->release(RELEASE_ALL, LOCATION_PRIVATE_LOCAL, CAUSE_NORMAL, LOCATION_PRIVATE_LOCAL, CAUSE_NORMAL);
301                         start_trace(-1,
302                                 NULL,
303                                 numberrize_callerinfo(apppbx->e_callerinfo.id, apppbx->e_callerinfo.ntype, options.national, options.international),
304                                 apppbx->e_dialinginfo.id,
305                                 DIRECTION_NONE,
306                                 CATEGORY_EP,
307                                 apppbx->ea_endpoint->ep_serial,
308                                 "KICK (reload routing)");
309                         end_trace();
310                 }
311
312                 apppbx->e_action_timeout = 0;
313                 apppbx->e_rule = NULL;
314                 apppbx->e_ruleset = NULL;
315
316                 apppbx = apppbx->next;
317         }
318
319         response:
320         /* create state response */
321         response = (struct admin_queue *)MALLOC(sizeof(struct admin_queue)+sizeof(admin_message));
322         memuse++;
323         response->num = 1;
324         /* message */
325         response->am[0].message = ADMIN_RESPONSE_CMD_ROUTE;
326         /* error */
327         response->am[0].u.x.error = err;
328         /* message */
329         SCPY(response->am[0].u.x.message, err_txt);
330         /* attach to response chain */
331         *responsep = response;
332         responsep = &response->next;
333
334         return(0);
335 }
336
337
338 /*
339  * do dialing
340  */
341 int admin_dial(struct admin_queue **responsep, char *message)
342 {
343         struct extension        ext;            /* temporary extension's settings */
344         struct admin_queue      *response;      /* response pointer */
345         char                    *p;             /* pointer to dialing digits */
346
347         /* create state response */
348         response = (struct admin_queue *)MALLOC(sizeof(struct admin_queue)+sizeof(admin_message));
349         memuse++;
350         response->num = 1;
351         /* message */
352         response->am[0].message = ADMIN_RESPONSE_CMD_DIAL;
353
354         /* process request */
355         if (!(p = strchr(message,':')))
356         {
357                 response->am[0].u.x.error = -EINVAL;
358                 SPRINT(response->am[0].u.x.message, "no seperator ':' in message to seperate number from extension");
359                 goto out;
360         }
361         *p++ = 0;
362
363         /* modify extension */
364         if (!read_extension(&ext, message))
365         {
366                 response->am[0].u.x.error = -EINVAL;
367                 SPRINT(response->am[0].u.x.message, "extension doesn't exist");
368                 goto out;
369         }
370         SCPY(ext.next, p);
371         write_extension(&ext, message);
372
373         out:
374         /* attach to response chain */
375         *responsep = response;
376         responsep = &response->next;
377         return(0);
378 }
379
380
381 /*
382  * do tracing
383  */
384 int admin_trace(struct admin_list *admin, struct admin_trace_req *trace)
385 {
386         memcpy(&admin->trace, trace, sizeof(struct admin_trace_req));
387         return(0);
388 }
389
390
391 /*
392  * do blocking
393  * 
394  * 0 = make port available
395  * 1 = make port administratively blocked
396  * 2 = unload port
397  * the result is returned:
398  * 0 = port is now available
399  * 1 = port is now blocked
400  * 2 = port cannot be loaded or has been unloaded
401  * -1 = port doesn't exist
402  */
403 int admin_block(struct admin_queue **responsep, int portnum, int block)
404 {
405         struct admin_queue      *response;      /* response pointer */
406         struct interface        *interface;
407         struct interface_port   *ifport;
408
409         /* create block response */
410         response = (struct admin_queue *)MALLOC(sizeof(struct admin_queue)+sizeof(admin_message));
411         memuse++;
412         response->num = 1;
413         /* message */
414         response->am[0].message = ADMIN_RESPONSE_CMD_BLOCK;
415         response->am[0].u.x.portnum = portnum;
416
417         /* search for port */
418         ifport = NULL;
419         interface = interface_first;
420         while(interface)
421         {
422                 ifport = interface->ifport;
423                 while(ifport)
424                 {
425                         if (ifport->portnum == portnum)
426                                 break;
427                         ifport = ifport->next;
428                 }
429                 if (ifport)
430                         break;
431                 interface = interface->next;
432         }
433         /* not found, we return -1 */
434         if (!ifport)
435         {
436                 response->am[0].u.x.block = -1;
437                 response->am[0].u.x.error = 1;
438                 SPRINT(response->am[0].u.x.message, "Port %d does not exist.", portnum);
439                 goto out;
440         }
441
442         /* no interface */
443         if (!ifport->mISDNport)
444         {
445                 /* not loaded anyway */
446                 if (block >= 2)
447                 {
448                         response->am[0].u.x.block = 2;
449                         goto out;
450                 }
451
452                 /* try loading interface */
453                 ifport->block = block;
454                 load_port(ifport);
455
456                 /* port cannot load */
457                 if (ifport->block >= 2)
458                 {
459                         response->am[0].u.x.block = 2;
460                         response->am[0].u.x.error = 1;
461                         SPRINT(response->am[0].u.x.message, "Port %d will not load.", portnum);
462                         goto out;
463                 }
464
465                 /* port loaded */
466                 response->am[0].u.x.block = ifport->block;
467                 goto out;
468         }
469
470         /* if we shall unload interface */
471         if (block >= 2)
472         {
473                 mISDNport_close(ifport->mISDNport);
474                 ifport->mISDNport = 0;
475                 ifport->block = 2;
476                 goto out;
477         }
478         
479         /* port new blocking state */
480         ifport->block = response->am[0].u.x.block = block;
481
482         out:
483         /* attach to response chain */
484         *responsep = response;
485         responsep = &response->next;
486         return(0);
487 }
488
489
490 /*
491  * do release
492  */
493 int admin_release(struct admin_queue **responsep, char *message)
494 {
495         unsigned int            id;
496         struct admin_queue      *response;      /* response pointer */
497         class EndpointAppPBX    *apppbx;
498
499         /* create state response */
500         response = (struct admin_queue *)MALLOC(sizeof(struct admin_queue)+sizeof(admin_message));
501         memuse++;
502         response->num = 1;
503         /* message */
504         response->am[0].message = ADMIN_RESPONSE_CMD_RELEASE;
505
506         id = atoi(message);
507         apppbx = apppbx_first;
508         while(apppbx)
509         {
510                 if (apppbx->ea_endpoint->ep_serial == id)
511                         break;
512                 apppbx = apppbx->next;
513         }
514         if (!apppbx)
515         {
516                 response->am[0].u.x.error = -EINVAL;
517                 SPRINT(response->am[0].u.x.message, "Given endpoint %d doesn't exist.", id);
518                 goto out;
519         }
520
521         apppbx->e_callback = 0;
522         apppbx->release(RELEASE_ALL, LOCATION_PRIVATE_LOCAL, CAUSE_NORMAL, LOCATION_PRIVATE_LOCAL, CAUSE_NORMAL);
523
524         out:
525         /* attach to response chain */
526         *responsep = response;
527         responsep = &response->next;
528         return(0);
529 }
530
531
532 /*
533  * do call
534  */
535 int admin_call(struct admin_list *admin, struct admin_message *msg)
536 {
537         class Endpoint          *epoint;
538         class EndpointAppPBX    *apppbx;
539
540         if (!(epoint = new Endpoint(0, 0)))
541                 FATAL("No memory for Endpoint instance\n");
542         if (!(epoint->ep_app = apppbx = new DEFAULT_ENDPOINT_APP(epoint, 1))) // outgoing
543                 FATAL("No memory for Endpoint Application instance\n");
544         apppbx->e_adminid = admin->sockserial;
545         admin->epointid = epoint->ep_serial;
546         SCPY(apppbx->e_callerinfo.id, nationalize_callerinfo(msg->u.call.callerid, &apppbx->e_callerinfo.ntype, options.national, options.international));
547         if (msg->u.call.present)
548                 apppbx->e_callerinfo.present = INFO_PRESENT_ALLOWED;
549         else
550                 apppbx->e_callerinfo.present = INFO_PRESENT_RESTRICTED;
551         apppbx->e_callerinfo.screen = INFO_SCREEN_NETWORK;
552
553 //printf("hh=%d\n", apppbx->e_capainfo.hlc);
554
555         apppbx->e_capainfo.bearer_capa = msg->u.call.bc_capa;
556         apppbx->e_capainfo.bearer_mode = msg->u.call.bc_mode;
557         apppbx->e_capainfo.bearer_info1 = msg->u.call.bc_info1;
558         apppbx->e_capainfo.hlc = msg->u.call.hlc;
559         apppbx->e_capainfo.exthlc = msg->u.call.exthlc;
560         SCPY(apppbx->e_dialinginfo.id, msg->u.call.dialing);
561         SCPY(apppbx->e_dialinginfo.interfaces, msg->u.call.interface);
562         apppbx->e_dialinginfo.sending_complete = 1;
563
564         apppbx->new_state(PORT_STATE_OUT_SETUP);
565         apppbx->out_setup();
566         return(0);
567 }
568
569
570 /*
571  * this function is called for response whenever a call state changes.
572  */
573 void admin_call_response(int adminid, int message, const char *connected, int cause, int location, int notify)
574 {
575         struct admin_list       *admin;
576         struct admin_queue      *response, **responsep; /* response pointer */
577
578         /* searching for admin id
579          * maybe there is no admin instance, because the calling port was not
580          * initiated by admin_call */
581         admin = admin_first;
582         while(admin)
583         {
584                 if (adminid == admin->sockserial)
585                         break;
586                 admin = admin->next;
587         }
588         if (!admin)
589                 return;
590
591         /* seek to end of response list */
592         response = admin->response;
593         responsep = &admin->response;
594         while(response)
595         {
596                 responsep = &response->next;
597                 response = response->next;
598         }
599
600         /* create state response */
601         response = (struct admin_queue *)MALLOC(sizeof(struct admin_queue)+sizeof(admin_message));
602         memuse++;
603         response->num = 1;
604         /* message */
605         response->am[0].message = message;
606 //      printf("MESSAGE: %d\n", message);
607
608         SCPY(response->am[0].u.call.callerid, connected);
609         response->am[0].u.call.cause = cause;
610         response->am[0].u.call.location = location;
611         response->am[0].u.call.notify = notify;
612
613         /* attach to response chain */
614         *responsep = response;
615         responsep = &response->next;
616 }
617
618
619 /*
620  * send data to the remote socket join instance
621  */
622 int admin_message_to_join(struct admin_msg *msg, struct admin_list *admin)
623 {
624         class Join                      *join;
625         struct admin_list               *temp;
626
627         /* hello message */
628         if (msg->type == MESSAGE_HELLO)
629         {
630                 if (admin->remote_name[0])
631                 {
632                         PERROR("Remote application repeats hello message.\n");
633                         return(-1);
634                 }
635                 /* look for second application */
636                 temp = admin_first;
637                 while(temp)
638                 {
639                         if (!strcmp(temp->remote_name, msg->param.hello.application))
640                                 break;
641                         temp = temp->next;
642                 }
643                 if (temp)
644                 {
645                         PERROR("Remote application connects twice??? (ignoring)\n");
646                         return(-1);
647                 }
648                 /* set remote socket instance */
649                 SCPY(admin->remote_name, msg->param.hello.application);
650                 start_trace(-1,
651                         NULL,
652                         NULL,
653                         NULL,
654                         DIRECTION_NONE,
655                         0,
656                         0,
657                         "REMOTE APP registers");
658                 add_trace("app", "name", "%s", admin->remote_name);
659                 end_trace();
660                 return(0);
661         }
662
663         /* check we have no application name */
664         if (!admin->remote_name[0])
665         {
666                 PERROR("Remote application did not send us a hello message.\n");
667                 return(-1);
668         }
669
670         /* new join */
671         if (msg->type == MESSAGE_NEWREF)
672         {
673                 /* create new join instance */
674                 join = new JoinRemote(0, admin->remote_name, admin->sock); // must have no serial, because no endpoint is connected
675                 if (!join)
676                 {
677                         FATAL("No memory for remote join instance\n");
678                         return(-1);
679                 }
680                 return(0);
681         }
682
683         /* bchannel message
684          * no ref given for *_ack */
685         if (msg->type == MESSAGE_BCHANNEL)
686         if (msg->param.bchannel.type == BCHANNEL_ASSIGN_ACK
687          || msg->param.bchannel.type == BCHANNEL_REMOVE_ACK
688          || msg->param.bchannel.type == BCHANNEL_RELEASE)
689         {
690                 /* no ref, but address */
691                 message_bchannel_from_remote(NULL, msg->param.bchannel.type, msg->param.bchannel.handle);
692                 return(0);
693         }
694         
695         /* check for ref */
696         if (!msg->ref)
697         {
698                 PERROR("Remote application did not send us a valid ref with a message.\n");
699                 return(-1);
700         }
701
702         /* find join instance */
703         join = join_first;
704         while(join)
705         {
706                 if (join->j_serial == msg->ref)
707                         break;
708                 join = join->next;
709         }
710         if (!join)
711         {
712                 PDEBUG(DEBUG_LOG, "No join found with serial %d. (May have been already released.)\n", msg->ref);
713                 return(0);
714         }
715
716         /* check application */
717         if (join->j_type != JOIN_TYPE_REMOTE)
718         {
719                 PERROR("Ref %d does not belong to a remote join instance.\n", msg->ref);
720                 return(-1);
721         }
722         if (admin->sock != ((class JoinRemote *)join)->j_remote_id)
723         {
724                 PERROR("Ref %d belongs to remote application %s, but not to sending application %s.\n", msg->ref, ((class JoinRemote *)join)->j_remote_name, admin->remote_name);
725                 return(-1);
726         }
727
728         /* send message */
729         ((class JoinRemote *)join)->message_remote(msg->type, &msg->param);
730
731         return(0);
732 }
733
734
735 /*
736  * this function is called for every message to remote socket
737  */
738 int admin_message_from_join(int remote_id, unsigned int ref, int message_type, union parameter *param)
739 {
740         struct admin_list       *admin;
741         struct admin_queue      **responsep;    /* response pointer */
742
743         /* searching for admin id
744          * maybe there is no given remote application
745          */
746         admin = admin_first;
747         while(admin)
748         {
749                 if (admin->remote_name[0] && admin->sock==remote_id)
750                         break;
751                 admin = admin->next;
752         }
753         /* no given remote application connected */
754         if (!admin)
755                 return(-1);
756
757         /* seek to end of response list */
758         responsep = &admin->response;
759         while(*responsep)
760         {
761                 responsep = &(*responsep)->next;
762         }
763
764         /* create state response */
765         *responsep = (struct admin_queue *)MALLOC(sizeof(struct admin_queue)+sizeof(admin_message));
766         memuse++;
767         (*responsep)->num = 1;
768
769         /* message */
770         (*responsep)->am[0].message = ADMIN_MESSAGE;
771         (*responsep)->am[0].u.msg.type = message_type;
772         (*responsep)->am[0].u.msg.ref = ref;
773         memcpy(&(*responsep)->am[0].u.msg.param, param, sizeof(union parameter));
774
775         return(0);
776 }
777
778
779 /*
780  * do state debugging
781  */
782 int admin_state(struct admin_queue **responsep)
783 {
784
785         class Port              *port;
786         class EndpointAppPBX    *apppbx;
787         class Join              *join;
788         class Pdss1             *pdss1;
789         struct interface        *interface;
790         struct interface_port   *ifport;
791         struct mISDNport        *mISDNport;
792         int                     i;
793         int                     num;
794         int                     anybusy;
795         struct admin_queue      *response;
796         struct admin_list       *admin;
797
798         /* create state response */
799         response = (struct admin_queue *)MALLOC(sizeof(struct admin_queue)+sizeof(admin_message));
800         memuse++;
801         response->num = 1;
802         /* message */
803         response->am[0].message = ADMIN_RESPONSE_STATE;
804         /* version */
805         SCPY(response->am[0].u.s.version_string, VERSION_STRING);
806         /* time */
807         memcpy(&response->am[0].u.s.tm, now_tm, sizeof(struct tm));
808         /* log file */
809         SCPY(response->am[0].u.s.logfile, options.log);
810         /* interface count */
811         i = 0;
812         interface = interface_first;
813         while(interface)
814         {
815                 ifport = interface->ifport;
816                 while(ifport)
817                 {
818                         i++;
819                         ifport = ifport->next;
820                 }
821                 interface = interface->next;
822         }
823         response->am[0].u.s.interfaces = i;
824         /* remote connection count */
825         i = 0;
826         admin = admin_first;
827         while(admin)
828         {
829                 if (admin->remote_name[0])
830                         i++;
831                 admin = admin->next;
832         }
833         response->am[0].u.s.remotes = i;
834         /* join count */
835         join = join_first;
836         i = 0;
837         while(join)
838         {
839                 i++;
840                 join = join->next;
841         }
842         response->am[0].u.s.joins = i;
843         /* apppbx count */
844         apppbx = apppbx_first;
845         i = 0;
846         while(apppbx)
847         {
848                 i++;
849                 apppbx = apppbx->next;
850         }
851         response->am[0].u.s.epoints = i;
852         /* port count */
853         i = 0;
854         port = port_first;
855         while(port)
856         {
857                 i++;
858                 port = port->next;
859         }
860         response->am[0].u.s.ports = i;
861         /* attach to response chain */
862         *responsep = response;
863         responsep = &response->next;
864
865         /* create response for all instances */
866         num = (response->am[0].u.s.interfaces)
867             + (response->am[0].u.s.remotes)
868             + (response->am[0].u.s.joins)
869             + (response->am[0].u.s.epoints)
870             + (response->am[0].u.s.ports);
871         if (num == 0)
872                 return(0);
873         response = (struct admin_queue *)MALLOC(sizeof(admin_queue)+(num*sizeof(admin_message)));
874         memuse++;
875         response->num = num;
876         *responsep = response;
877         responsep = &response->next;
878         interface = interface_first;
879         num = 0;
880         while(interface)
881         {
882                 ifport = interface->ifport;
883                 while(ifport)
884                 {
885                         /* message */
886                         response->am[num].message = ADMIN_RESPONSE_S_INTERFACE;
887                         /* interface */
888                         SCPY(response->am[num].u.i.interface_name, interface->name);
889                         /* portnum */
890                         response->am[num].u.i.portnum = ifport->portnum;
891                         /* portname */
892                         SCPY(response->am[num].u.i.portname, ifport->portname);
893                         /* iftype */
894                         response->am[num].u.i.extension = interface->extension;
895                         /* block */
896                         response->am[num].u.i.block = ifport->block;
897                         if (ifport->mISDNport)
898                         {
899                                 mISDNport = ifport->mISDNport;
900
901                                 /* ptp */
902                                 response->am[num].u.i.ptp = mISDNport->ptp;
903                                 /* l1hold */
904                                 response->am[num].u.i.l1hold = mISDNport->l1hold;
905                                 /* l2hold */
906                                 response->am[num].u.i.l2hold = mISDNport->l2hold;
907                                 /* ntmode */
908                                 response->am[num].u.i.ntmode = mISDNport->ntmode;
909                                 /* pri */
910                                 response->am[num].u.i.pri = mISDNport->pri;
911                                 /* use */
912                                 response->am[num].u.i.use = mISDNport->use;
913                                 /* l1link */
914                                 response->am[num].u.i.l1link = mISDNport->l1link;
915                                 /* l2link */
916                                 response->am[num].u.i.l2link = mISDNport->l2link;
917                                 memcpy(response->am[num].u.i.l2mask, mISDNport->l2mask, 16);
918                                 /* los */
919                                 response->am[num].u.i.los = mISDNport->los;
920                                 /* ais */
921                                 response->am[num].u.i.ais = mISDNport->ais;
922                                 /* rdi */
923                                 response->am[num].u.i.rdi = mISDNport->rdi;
924                                 /* slip */
925                                 response->am[num].u.i.slip_tx = mISDNport->slip_tx;
926                                 response->am[num].u.i.slip_rx = mISDNport->slip_rx;
927                                 /* channels */
928                                 response->am[num].u.i.channels = mISDNport->b_num;
929                                 /* channel info */
930                                 i = 0;
931                                 anybusy = 0;
932                                 while(i < mISDNport->b_num)
933                                 {
934                                         response->am[num].u.i.busy[i] = mISDNport->b_state[i];
935                                         if (mISDNport->b_port[i])
936                                                 response->am[num].u.i.port[i] = mISDNport->b_port[i]->p_serial;
937                                         response->am[num].u.i.mode[i] = mISDNport->b_mode[i];
938                                         i++;
939                                 }
940                         }
941                         num++;
942
943                         ifport = ifport->next;
944                 }
945                 interface = interface->next;
946         }
947
948         /* create response for all remotes */
949         admin = admin_first;
950         while(admin)
951         {
952                 if (admin->remote_name[0])
953                 {
954                         /* message */
955                         response->am[num].message = ADMIN_RESPONSE_S_REMOTE;
956                         /* name */
957                         SCPY(response->am[num].u.r.name, admin->remote_name);
958                         /* */
959                         num++;
960                 }
961                 admin = admin->next;
962         }
963
964         /* create response for all joins */
965         join = join_first;
966         while(join)
967         {
968                 /* message */
969                 response->am[num].message = ADMIN_RESPONSE_S_JOIN;
970                 /* serial */
971                 response->am[num].u.j.serial = join->j_serial;
972                 /* partyline */
973                 if (join->j_type == JOIN_TYPE_PBX)
974                         response->am[num].u.j.partyline = ((class JoinPBX *)join)->j_partyline;
975                 /* remote application */
976                 if (join->j_type == JOIN_TYPE_REMOTE)
977                         SCPY(response->am[num].u.j.remote, ((class JoinRemote *)join)->j_remote_name);
978                 /* */
979                 join = join->next;
980                 num++;
981         }
982
983         /* create response for all endpoint */
984         apppbx = apppbx_first;
985         while(apppbx)
986         {
987                 /* message */
988                 response->am[num].message = ADMIN_RESPONSE_S_EPOINT;
989                 /* serial */
990                 response->am[num].u.e.serial = apppbx->ea_endpoint->ep_serial;
991                 /* join */
992                 response->am[num].u.e.join = apppbx->ea_endpoint->ep_join_id;
993                 /* rx notification */
994                 response->am[num].u.e.rx_state = apppbx->e_rx_state;
995                 /* tx notification */
996                 response->am[num].u.e.tx_state = apppbx->e_tx_state;
997                 /* state */
998                 switch(apppbx->e_state)
999                 {
1000                         case EPOINT_STATE_IN_SETUP:
1001                         response->am[num].u.e.state = ADMIN_STATE_IN_SETUP;
1002                         break;
1003                         case EPOINT_STATE_OUT_SETUP:
1004                         response->am[num].u.e.state = ADMIN_STATE_OUT_SETUP;
1005                         break;
1006                         case EPOINT_STATE_IN_OVERLAP:
1007                         response->am[num].u.e.state = ADMIN_STATE_IN_OVERLAP;
1008                         break;
1009                         case EPOINT_STATE_OUT_OVERLAP:
1010                         response->am[num].u.e.state = ADMIN_STATE_OUT_OVERLAP;
1011                         break;
1012                         case EPOINT_STATE_IN_PROCEEDING:
1013                         response->am[num].u.e.state = ADMIN_STATE_IN_PROCEEDING;
1014                         break;
1015                         case EPOINT_STATE_OUT_PROCEEDING:
1016                         response->am[num].u.e.state = ADMIN_STATE_OUT_PROCEEDING;
1017                         break;
1018                         case EPOINT_STATE_IN_ALERTING:
1019                         response->am[num].u.e.state = ADMIN_STATE_IN_ALERTING;
1020                         break;
1021                         case EPOINT_STATE_OUT_ALERTING:
1022                         response->am[num].u.e.state = ADMIN_STATE_OUT_ALERTING;
1023                         break;
1024                         case EPOINT_STATE_CONNECT:
1025                         response->am[num].u.e.state = ADMIN_STATE_CONNECT;
1026                         break;
1027                         case EPOINT_STATE_IN_DISCONNECT:
1028                         response->am[num].u.e.state = ADMIN_STATE_IN_DISCONNECT;
1029                         break;
1030                         case EPOINT_STATE_OUT_DISCONNECT:
1031                         response->am[num].u.e.state = ADMIN_STATE_OUT_DISCONNECT;
1032                         break;
1033                         default:
1034                         response->am[num].u.e.state = ADMIN_STATE_IDLE;
1035                 }
1036                 /* terminal */
1037                 SCPY(response->am[num].u.e.terminal, apppbx->e_ext.number);
1038                 /* callerid */
1039                 SCPY(response->am[num].u.e.callerid, apppbx->e_callerinfo.id);
1040                 /* dialing */
1041                 SCPY(response->am[num].u.e.dialing, apppbx->e_dialinginfo.id);
1042                 /* action string */
1043                 if (apppbx->e_action)
1044                         SCPY(response->am[num].u.e.action, action_defs[apppbx->e_action->index].name);
1045 //              if (apppbx->e_action)
1046 //              printf("action=%s\n",action_defs[apppbx->e_action->index].name);
1047                 /* park */
1048                 response->am[num].u.e.park = apppbx->ea_endpoint->ep_park;
1049                 if (apppbx->ea_endpoint->ep_park && apppbx->ea_endpoint->ep_park_len && apppbx->ea_endpoint->ep_park_len<=(int)sizeof(response->am[num].u.e.park_callid))
1050                         memcpy(response->am[num].u.e.park_callid, apppbx->ea_endpoint->ep_park_callid, apppbx->ea_endpoint->ep_park_len);
1051                 response->am[num].u.e.park_len = apppbx->ea_endpoint->ep_park_len;
1052                 /* crypt */
1053                 if (apppbx->e_crypt == CRYPT_ON)
1054                         response->am[num].u.e.crypt = 1;
1055                 /* */
1056                 apppbx = apppbx->next;
1057                 num++;
1058         }
1059
1060         /* create response for all ports */
1061         port = port_first;
1062         while(port)
1063         {
1064                 /* message */
1065                 response->am[num].message = ADMIN_RESPONSE_S_PORT;
1066                 /* serial */
1067                 response->am[num].u.p.serial = port->p_serial;
1068                 /* name */
1069                 SCPY(response->am[num].u.p.name, port->p_name);
1070                 /* epoint */
1071                 response->am[num].u.p.epoint = ACTIVE_EPOINT(port->p_epointlist);
1072                 /* state */
1073                 switch(port->p_state)
1074                 {
1075                         case PORT_STATE_IN_SETUP:
1076                         response->am[num].u.p.state = ADMIN_STATE_IN_SETUP;
1077                         break;
1078                         case PORT_STATE_OUT_SETUP:
1079                         response->am[num].u.p.state = ADMIN_STATE_OUT_SETUP;
1080                         break;
1081                         case PORT_STATE_IN_OVERLAP:
1082                         response->am[num].u.p.state = ADMIN_STATE_IN_OVERLAP;
1083                         break;
1084                         case PORT_STATE_OUT_OVERLAP:
1085                         response->am[num].u.p.state = ADMIN_STATE_OUT_OVERLAP;
1086                         break;
1087                         case PORT_STATE_IN_PROCEEDING:
1088                         response->am[num].u.p.state = ADMIN_STATE_IN_PROCEEDING;
1089                         break;
1090                         case PORT_STATE_OUT_PROCEEDING:
1091                         response->am[num].u.p.state = ADMIN_STATE_OUT_PROCEEDING;
1092                         break;
1093                         case PORT_STATE_IN_ALERTING:
1094                         response->am[num].u.p.state = ADMIN_STATE_IN_ALERTING;
1095                         break;
1096                         case PORT_STATE_OUT_ALERTING:
1097                         response->am[num].u.p.state = ADMIN_STATE_OUT_ALERTING;
1098                         break;
1099                         case PORT_STATE_CONNECT:
1100                         response->am[num].u.p.state = ADMIN_STATE_CONNECT;
1101                         break;
1102                         case PORT_STATE_IN_DISCONNECT:
1103                         response->am[num].u.p.state = ADMIN_STATE_IN_DISCONNECT;
1104                         break;
1105                         case PORT_STATE_OUT_DISCONNECT:
1106                         response->am[num].u.p.state = ADMIN_STATE_OUT_DISCONNECT;
1107                         break;
1108                         default:
1109                         response->am[num].u.p.state = ADMIN_STATE_IDLE;
1110                 }
1111                 /* isdn */
1112                 if ((port->p_type&PORT_CLASS_mISDN_MASK) == PORT_CLASS_mISDN_DSS1)
1113                 {
1114                         response->am[num].u.p.isdn = 1;
1115                         pdss1 = (class Pdss1 *)port;
1116                         response->am[num].u.p.isdn_chan = pdss1->p_m_b_channel;
1117                         response->am[num].u.p.isdn_hold = pdss1->p_m_hold;
1118                         response->am[num].u.p.isdn_ces = pdss1->p_m_d_ces;
1119                 }
1120                 /* */
1121                 port = port->next;
1122                 num++;
1123         }
1124         return(0);
1125 }
1126
1127 int sockserial = 1; // must start with 1, because 0 is used if no serial is set
1128 /*
1129  * handle admin socket (non blocking)
1130  */
1131 int admin_handle(void)
1132 {
1133         struct admin_list       *admin, **adminp;
1134         void                    *temp;
1135         struct admin_message    msg;
1136         int                     len;
1137         int                     new_sock;
1138         socklen_t               sock_len = sizeof(sock_address);
1139         unsigned int            on = 1;
1140         int                     work = 0; /* if work was done */
1141         struct Endpoint         *epoint;
1142
1143         if (sock < 0)
1144                 return(0);
1145
1146         /* check for new incoming connections */
1147         if ((new_sock = accept(sock, (struct sockaddr *)&sock_address, &sock_len)) >= 0)
1148         {
1149                 work = 1;
1150                 /* insert new socket */
1151                 admin = (struct admin_list *)MALLOC(sizeof(struct admin_list));
1152                 if (ioctl(new_sock, FIONBIO, (unsigned char *)(&on)) >= 0)
1153                 {
1154 //#warning
1155 //      PERROR("DEBUG incoming socket %d, serial=%d\n", new_sock, sockserial);
1156                         memuse++;
1157                         fhuse++;
1158                         admin->sockserial = sockserial++;
1159                         admin->next = admin_first;
1160                         admin_first = admin;
1161                         admin->sock = new_sock;
1162                 } else {
1163                         close(new_sock);
1164                         FREE(admin, sizeof(struct admin_list));
1165                 }
1166         } else
1167         {
1168                 if (errno != EWOULDBLOCK)
1169                 {
1170                         PERROR("Failed to accept connection from socket \"%s\". (errno=%d) Closing socket.\n", sock_address.sun_path, errno);
1171                         admin_cleanup();
1172                         return(1);
1173                 }
1174         }
1175
1176         /* loop all current socket connections */
1177         admin = admin_first;
1178         adminp = &admin_first;
1179         while(admin)
1180         {
1181                 /* read command */
1182                 len = read(admin->sock, &msg, sizeof(msg));
1183                 if (len < 0)
1184                 {
1185                         if (errno != EWOULDBLOCK)
1186                         {
1187                                 work = 1;
1188                                 brokenpipe:
1189                                 PDEBUG(DEBUG_LOG, "Broken pipe on socket %d. (errno=%d).\n", admin->sock, errno);
1190                                 *adminp = admin->next;
1191                                 free_connection(admin);
1192                                 admin = *adminp;
1193                                 continue;
1194                         }
1195                         goto send_data;
1196                 }
1197                 work = 1;
1198 //#warning
1199 //PERROR("DEBUG socket %d got data. serial=%d\n", admin->sock, admin->sockserial);
1200                 if (len == 0)
1201                 {
1202                         end:
1203
1204                         /*release endpoint if exists */
1205                         if (admin->epointid)
1206                         {
1207                                 epoint = find_epoint_id(admin->epointid);
1208                                 if (epoint)
1209                                 {
1210                                         ((class DEFAULT_ENDPOINT_APP *)epoint->ep_app)->
1211                                                 release(RELEASE_ALL, LOCATION_PRIVATE_LOCAL, CAUSE_NORMAL, LOCATION_PRIVATE_LOCAL, CAUSE_NORMAL);
1212                                 }
1213                         }
1214
1215 //#warning
1216 //PERROR("DEBUG socket %d closed by remote.\n", admin->sock);
1217                         *adminp = admin->next;
1218                         free_connection(admin);
1219                         admin = *adminp;
1220 //PERROR("DEBUG (admin_first=%x)\n", admin_first);
1221                         continue;
1222                 }
1223                 if (len != sizeof(msg))
1224                 {
1225                         PERROR("Short/long read on socket %d. (len=%d != size=%d).\n", admin->sock, len, sizeof(msg));
1226                         *adminp = admin->next;
1227                         free_connection(admin);
1228                         admin = *adminp;
1229                         continue;
1230                 }
1231                 /* process socket command */
1232                 if (admin->response && msg.message != ADMIN_MESSAGE)
1233                 {
1234                         PERROR("Data from socket %d while sending response.\n", admin->sock);
1235                         *adminp = admin->next;
1236                         free_connection(admin);
1237                         admin = *adminp;
1238                         continue;
1239                 }
1240                 switch (msg.message)
1241                 {
1242                         case ADMIN_REQUEST_CMD_INTERFACE:
1243                         if (admin_interface(&admin->response) < 0)
1244                         {
1245                                 PERROR("Failed to create dial response for socket %d.\n", admin->sock);
1246                                 goto response_error;
1247                         }
1248                         break;
1249
1250                         case ADMIN_REQUEST_CMD_ROUTE:
1251                         if (admin_route(&admin->response) < 0)
1252                         {
1253                                 PERROR("Failed to create dial response for socket %d.\n", admin->sock);
1254                                 goto response_error;
1255                         }
1256                         break;
1257
1258                         case ADMIN_REQUEST_CMD_DIAL:
1259                         if (admin_dial(&admin->response, msg.u.x.message) < 0)
1260                         {
1261                                 PERROR("Failed to create dial response for socket %d.\n", admin->sock);
1262                                 goto response_error;
1263                         }
1264                         break;
1265
1266                         case ADMIN_REQUEST_CMD_RELEASE:
1267                         if (admin_release(&admin->response, msg.u.x.message) < 0)
1268                         {
1269                                 PERROR("Failed to create release response for socket %d.\n", admin->sock);
1270                                 goto response_error;
1271                         }
1272                         break;
1273
1274                         case ADMIN_REQUEST_STATE:
1275                         if (admin_state(&admin->response) < 0)
1276                         {
1277                                 PERROR("Failed to create state response for socket %d.\n", admin->sock);
1278                                 goto response_error;
1279                         }
1280                         break;
1281
1282                         case ADMIN_TRACE_REQUEST:
1283                         if (admin_trace(admin, &msg.u.trace_req) < 0)
1284                         {
1285                                 PERROR("Failed to create trace response for socket %d.\n", admin->sock);
1286                                 goto response_error;
1287                         }
1288                         break;
1289
1290                         case ADMIN_REQUEST_CMD_BLOCK:
1291                         if (admin_block(&admin->response, msg.u.x.portnum, msg.u.x.block) < 0)
1292                         {
1293                                 PERROR("Failed to create block response for socket %d.\n", admin->sock);
1294                                 goto response_error;
1295                         }
1296                         break;
1297
1298                         case ADMIN_MESSAGE:
1299                         if (admin_message_to_join(&msg.u.msg, admin) < 0)
1300                         {
1301                                 PERROR("Failed to deliver message for socket %d.\n", admin->sock);
1302                                 goto response_error;
1303                         }
1304 #if 0
1305 #warning DEBUGGING
1306 {
1307         struct admin_queue      *response;
1308         printf("Chain: ");
1309         response = admin->response;
1310         while(response)
1311         {
1312                 printf("%c", '0'+response->am[0].message);
1313                 response=response->next;
1314         }
1315         printf("\n");
1316 }
1317 #endif
1318                         break;
1319
1320                         case ADMIN_CALL_SETUP:
1321                         if (admin_call(admin, &msg) < 0)
1322                         {
1323                                 PERROR("Failed to create call for socket %d.\n", admin->sock);
1324                                 response_error:
1325                                 *adminp = admin->next;
1326                                 free_connection(admin);
1327                                 admin = *adminp;
1328                                 continue;
1329                         }
1330                         break;
1331
1332                         default:
1333                         PERROR("Invalid message %d from socket %d.\n", msg.message, admin->sock);
1334                         *adminp = admin->next;
1335                         free_connection(admin);
1336                         admin = *adminp;
1337                         continue;
1338                 }
1339                 /* write queue */
1340                 send_data:
1341                 if (admin->response)
1342                 {
1343 //#warning
1344 //PERROR("DEBUG socket %d sending data.\n", admin->sock);
1345                         len = write(admin->sock, ((unsigned char *)(admin->response->am))+admin->response->offset, sizeof(struct admin_message)*(admin->response->num)-admin->response->offset);
1346                         if (len < 0)
1347                         {
1348                                 if (errno != EWOULDBLOCK)
1349                                 {
1350                                         work = 1;
1351                                         goto brokenpipe;
1352                                 }
1353                                 goto next;
1354                         }
1355                         work = 1;
1356                         if (len == 0)
1357                                 goto end;
1358                         if (len < (int)(sizeof(struct admin_message)*(admin->response->num) - admin->response->offset))
1359                         {
1360                                 admin->response->offset+=len;
1361                                 goto next;
1362                         } else
1363                         {
1364                                 temp = admin->response;
1365                                 admin->response = admin->response->next;
1366                                 FREE(temp, 0);
1367                                 memuse--;
1368                         }
1369                 }
1370                 /* done with socket instance */
1371                 next:
1372                 adminp = &admin->next;
1373                 admin = admin->next;
1374         }
1375
1376         return(work);
1377 }
1378