7f6e7ecfb895c949d43c1746ec8f2ac6df9ed2a3
[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                                 /* ntmode */
904                                 response->am[num].u.i.ntmode = mISDNport->ntmode;
905                                 /* pri */
906                                 response->am[num].u.i.pri = mISDNport->pri;
907                                 /* use */
908                                 response->am[num].u.i.use = mISDNport->use;
909                                 /* l1link */
910                                 response->am[num].u.i.l1link = mISDNport->l1link;
911                                 /* l2link */
912                                 response->am[num].u.i.l2link = mISDNport->l2link;
913                                 /* los */
914                                 response->am[num].u.i.los = mISDNport->los;
915                                 /* ais */
916                                 response->am[num].u.i.ais = mISDNport->ais;
917                                 /* rdi */
918                                 response->am[num].u.i.rdi = mISDNport->rdi;
919                                 /* slip */
920                                 response->am[num].u.i.slip_tx = mISDNport->slip_tx;
921                                 response->am[num].u.i.slip_rx = mISDNport->slip_rx;
922                                 /* channels */
923                                 response->am[num].u.i.channels = mISDNport->b_num;
924                                 /* channel info */
925                                 i = 0;
926                                 anybusy = 0;
927                                 while(i < mISDNport->b_num)
928                                 {
929                                         response->am[num].u.i.busy[i] = mISDNport->b_state[i];
930                                         if (mISDNport->b_port[i])
931                                                 response->am[num].u.i.port[i] = mISDNport->b_port[i]->p_serial;
932                                         response->am[num].u.i.mode[i] = mISDNport->b_mode[i];
933                                         i++;
934                                 }
935                         }
936                         num++;
937
938                         ifport = ifport->next;
939                 }
940                 interface = interface->next;
941         }
942
943         /* create response for all remotes */
944         admin = admin_first;
945         while(admin)
946         {
947                 if (admin->remote_name[0])
948                 {
949                         /* message */
950                         response->am[num].message = ADMIN_RESPONSE_S_REMOTE;
951                         /* name */
952                         SCPY(response->am[num].u.r.name, admin->remote_name);
953                         /* */
954                         num++;
955                 }
956                 admin = admin->next;
957         }
958
959         /* create response for all joins */
960         join = join_first;
961         while(join)
962         {
963                 /* message */
964                 response->am[num].message = ADMIN_RESPONSE_S_JOIN;
965                 /* serial */
966                 response->am[num].u.j.serial = join->j_serial;
967                 /* partyline */
968                 if (join->j_type == JOIN_TYPE_PBX)
969                         response->am[num].u.j.partyline = ((class JoinPBX *)join)->j_partyline;
970                 /* remote application */
971                 if (join->j_type == JOIN_TYPE_REMOTE)
972                         SCPY(response->am[num].u.j.remote, ((class JoinRemote *)join)->j_remote_name);
973                 /* */
974                 join = join->next;
975                 num++;
976         }
977
978         /* create response for all endpoint */
979         apppbx = apppbx_first;
980         while(apppbx)
981         {
982                 /* message */
983                 response->am[num].message = ADMIN_RESPONSE_S_EPOINT;
984                 /* serial */
985                 response->am[num].u.e.serial = apppbx->ea_endpoint->ep_serial;
986                 /* join */
987                 response->am[num].u.e.join = apppbx->ea_endpoint->ep_join_id;
988                 /* rx notification */
989                 response->am[num].u.e.rx_state = apppbx->e_rx_state;
990                 /* tx notification */
991                 response->am[num].u.e.tx_state = apppbx->e_tx_state;
992                 /* state */
993                 switch(apppbx->e_state)
994                 {
995                         case EPOINT_STATE_IN_SETUP:
996                         response->am[num].u.e.state = ADMIN_STATE_IN_SETUP;
997                         break;
998                         case EPOINT_STATE_OUT_SETUP:
999                         response->am[num].u.e.state = ADMIN_STATE_OUT_SETUP;
1000                         break;
1001                         case EPOINT_STATE_IN_OVERLAP:
1002                         response->am[num].u.e.state = ADMIN_STATE_IN_OVERLAP;
1003                         break;
1004                         case EPOINT_STATE_OUT_OVERLAP:
1005                         response->am[num].u.e.state = ADMIN_STATE_OUT_OVERLAP;
1006                         break;
1007                         case EPOINT_STATE_IN_PROCEEDING:
1008                         response->am[num].u.e.state = ADMIN_STATE_IN_PROCEEDING;
1009                         break;
1010                         case EPOINT_STATE_OUT_PROCEEDING:
1011                         response->am[num].u.e.state = ADMIN_STATE_OUT_PROCEEDING;
1012                         break;
1013                         case EPOINT_STATE_IN_ALERTING:
1014                         response->am[num].u.e.state = ADMIN_STATE_IN_ALERTING;
1015                         break;
1016                         case EPOINT_STATE_OUT_ALERTING:
1017                         response->am[num].u.e.state = ADMIN_STATE_OUT_ALERTING;
1018                         break;
1019                         case EPOINT_STATE_CONNECT:
1020                         response->am[num].u.e.state = ADMIN_STATE_CONNECT;
1021                         break;
1022                         case EPOINT_STATE_IN_DISCONNECT:
1023                         response->am[num].u.e.state = ADMIN_STATE_IN_DISCONNECT;
1024                         break;
1025                         case EPOINT_STATE_OUT_DISCONNECT:
1026                         response->am[num].u.e.state = ADMIN_STATE_OUT_DISCONNECT;
1027                         break;
1028                         default:
1029                         response->am[num].u.e.state = ADMIN_STATE_IDLE;
1030                 }
1031                 /* terminal */
1032                 SCPY(response->am[num].u.e.terminal, apppbx->e_ext.number);
1033                 /* callerid */
1034                 SCPY(response->am[num].u.e.callerid, apppbx->e_callerinfo.id);
1035                 /* dialing */
1036                 SCPY(response->am[num].u.e.dialing, apppbx->e_dialinginfo.id);
1037                 /* action string */
1038                 if (apppbx->e_action)
1039                         SCPY(response->am[num].u.e.action, action_defs[apppbx->e_action->index].name);
1040 //              if (apppbx->e_action)
1041 //              printf("action=%s\n",action_defs[apppbx->e_action->index].name);
1042                 /* park */
1043                 response->am[num].u.e.park = apppbx->ea_endpoint->ep_park;
1044                 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))
1045                         memcpy(response->am[num].u.e.park_callid, apppbx->ea_endpoint->ep_park_callid, apppbx->ea_endpoint->ep_park_len);
1046                 response->am[num].u.e.park_len = apppbx->ea_endpoint->ep_park_len;
1047                 /* crypt */
1048                 if (apppbx->e_crypt == CRYPT_ON)
1049                         response->am[num].u.e.crypt = 1;
1050                 /* */
1051                 apppbx = apppbx->next;
1052                 num++;
1053         }
1054
1055         /* create response for all ports */
1056         port = port_first;
1057         while(port)
1058         {
1059                 /* message */
1060                 response->am[num].message = ADMIN_RESPONSE_S_PORT;
1061                 /* serial */
1062                 response->am[num].u.p.serial = port->p_serial;
1063                 /* name */
1064                 SCPY(response->am[num].u.p.name, port->p_name);
1065                 /* epoint */
1066                 response->am[num].u.p.epoint = ACTIVE_EPOINT(port->p_epointlist);
1067                 /* state */
1068                 switch(port->p_state)
1069                 {
1070                         case PORT_STATE_IN_SETUP:
1071                         response->am[num].u.p.state = ADMIN_STATE_IN_SETUP;
1072                         break;
1073                         case PORT_STATE_OUT_SETUP:
1074                         response->am[num].u.p.state = ADMIN_STATE_OUT_SETUP;
1075                         break;
1076                         case PORT_STATE_IN_OVERLAP:
1077                         response->am[num].u.p.state = ADMIN_STATE_IN_OVERLAP;
1078                         break;
1079                         case PORT_STATE_OUT_OVERLAP:
1080                         response->am[num].u.p.state = ADMIN_STATE_OUT_OVERLAP;
1081                         break;
1082                         case PORT_STATE_IN_PROCEEDING:
1083                         response->am[num].u.p.state = ADMIN_STATE_IN_PROCEEDING;
1084                         break;
1085                         case PORT_STATE_OUT_PROCEEDING:
1086                         response->am[num].u.p.state = ADMIN_STATE_OUT_PROCEEDING;
1087                         break;
1088                         case PORT_STATE_IN_ALERTING:
1089                         response->am[num].u.p.state = ADMIN_STATE_IN_ALERTING;
1090                         break;
1091                         case PORT_STATE_OUT_ALERTING:
1092                         response->am[num].u.p.state = ADMIN_STATE_OUT_ALERTING;
1093                         break;
1094                         case PORT_STATE_CONNECT:
1095                         response->am[num].u.p.state = ADMIN_STATE_CONNECT;
1096                         break;
1097                         case PORT_STATE_IN_DISCONNECT:
1098                         response->am[num].u.p.state = ADMIN_STATE_IN_DISCONNECT;
1099                         break;
1100                         case PORT_STATE_OUT_DISCONNECT:
1101                         response->am[num].u.p.state = ADMIN_STATE_OUT_DISCONNECT;
1102                         break;
1103                         default:
1104                         response->am[num].u.p.state = ADMIN_STATE_IDLE;
1105                 }
1106                 /* isdn */
1107                 if ((port->p_type&PORT_CLASS_mISDN_MASK) == PORT_CLASS_mISDN_DSS1)
1108                 {
1109                         response->am[num].u.p.isdn = 1;
1110                         pdss1 = (class Pdss1 *)port;
1111                         response->am[num].u.p.isdn_chan = pdss1->p_m_b_channel;
1112                         response->am[num].u.p.isdn_hold = pdss1->p_m_hold;
1113                         response->am[num].u.p.isdn_ces = pdss1->p_m_d_ces;
1114                 }
1115                 /* */
1116                 port = port->next;
1117                 num++;
1118         }
1119         return(0);
1120 }
1121
1122 int sockserial = 1; // must start with 1, because 0 is used if no serial is set
1123 /*
1124  * handle admin socket (non blocking)
1125  */
1126 int admin_handle(void)
1127 {
1128         struct admin_list       *admin, **adminp;
1129         void                    *temp;
1130         struct admin_message    msg;
1131         int                     len;
1132         int                     new_sock;
1133         socklen_t               sock_len = sizeof(sock_address);
1134         unsigned int            on = 1;
1135         int                     work = 0; /* if work was done */
1136         struct Endpoint         *epoint;
1137
1138         if (sock < 0)
1139                 return(0);
1140
1141         /* check for new incoming connections */
1142         if ((new_sock = accept(sock, (struct sockaddr *)&sock_address, &sock_len)) >= 0)
1143         {
1144                 work = 1;
1145                 /* insert new socket */
1146                 admin = (struct admin_list *)MALLOC(sizeof(struct admin_list));
1147                 if (ioctl(new_sock, FIONBIO, (unsigned char *)(&on)) >= 0)
1148                 {
1149 //#warning
1150 //      PERROR("DEBUG incoming socket %d, serial=%d\n", new_sock, sockserial);
1151                         memuse++;
1152                         fhuse++;
1153                         admin->sockserial = sockserial++;
1154                         admin->next = admin_first;
1155                         admin_first = admin;
1156                         admin->sock = new_sock;
1157                 } else {
1158                         close(new_sock);
1159                         FREE(admin, sizeof(struct admin_list));
1160                 }
1161         } else
1162         {
1163                 if (errno != EWOULDBLOCK)
1164                 {
1165                         PERROR("Failed to accept connection from socket \"%s\". (errno=%d) Closing socket.\n", sock_address.sun_path, errno);
1166                         admin_cleanup();
1167                         return(1);
1168                 }
1169         }
1170
1171         /* loop all current socket connections */
1172         admin = admin_first;
1173         adminp = &admin_first;
1174         while(admin)
1175         {
1176                 /* read command */
1177                 len = read(admin->sock, &msg, sizeof(msg));
1178                 if (len < 0)
1179                 {
1180                         if (errno != EWOULDBLOCK)
1181                         {
1182                                 work = 1;
1183                                 brokenpipe:
1184                                 PDEBUG(DEBUG_LOG, "Broken pipe on socket %d. (errno=%d).\n", admin->sock, errno);
1185                                 *adminp = admin->next;
1186                                 free_connection(admin);
1187                                 admin = *adminp;
1188                                 continue;
1189                         }
1190                         goto send_data;
1191                 }
1192                 work = 1;
1193 //#warning
1194 //PERROR("DEBUG socket %d got data. serial=%d\n", admin->sock, admin->sockserial);
1195                 if (len == 0)
1196                 {
1197                         end:
1198
1199                         /*release endpoint if exists */
1200                         if (admin->epointid)
1201                         {
1202                                 epoint = find_epoint_id(admin->epointid);
1203                                 if (epoint)
1204                                 {
1205                                         ((class DEFAULT_ENDPOINT_APP *)epoint->ep_app)->
1206                                                 release(RELEASE_ALL, LOCATION_PRIVATE_LOCAL, CAUSE_NORMAL, LOCATION_PRIVATE_LOCAL, CAUSE_NORMAL);
1207                                 }
1208                         }
1209
1210 //#warning
1211 //PERROR("DEBUG socket %d closed by remote.\n", admin->sock);
1212                         *adminp = admin->next;
1213                         free_connection(admin);
1214                         admin = *adminp;
1215 //PERROR("DEBUG (admin_first=%x)\n", admin_first);
1216                         continue;
1217                 }
1218                 if (len != sizeof(msg))
1219                 {
1220                         PERROR("Short/long read on socket %d. (len=%d != size=%d).\n", admin->sock, len, sizeof(msg));
1221                         *adminp = admin->next;
1222                         free_connection(admin);
1223                         admin = *adminp;
1224                         continue;
1225                 }
1226                 /* process socket command */
1227                 if (admin->response && msg.message != ADMIN_MESSAGE)
1228                 {
1229                         PERROR("Data from socket %d while sending response.\n", admin->sock);
1230                         *adminp = admin->next;
1231                         free_connection(admin);
1232                         admin = *adminp;
1233                         continue;
1234                 }
1235                 switch (msg.message)
1236                 {
1237                         case ADMIN_REQUEST_CMD_INTERFACE:
1238                         if (admin_interface(&admin->response) < 0)
1239                         {
1240                                 PERROR("Failed to create dial response for socket %d.\n", admin->sock);
1241                                 goto response_error;
1242                         }
1243                         break;
1244
1245                         case ADMIN_REQUEST_CMD_ROUTE:
1246                         if (admin_route(&admin->response) < 0)
1247                         {
1248                                 PERROR("Failed to create dial response for socket %d.\n", admin->sock);
1249                                 goto response_error;
1250                         }
1251                         break;
1252
1253                         case ADMIN_REQUEST_CMD_DIAL:
1254                         if (admin_dial(&admin->response, msg.u.x.message) < 0)
1255                         {
1256                                 PERROR("Failed to create dial response for socket %d.\n", admin->sock);
1257                                 goto response_error;
1258                         }
1259                         break;
1260
1261                         case ADMIN_REQUEST_CMD_RELEASE:
1262                         if (admin_release(&admin->response, msg.u.x.message) < 0)
1263                         {
1264                                 PERROR("Failed to create release response for socket %d.\n", admin->sock);
1265                                 goto response_error;
1266                         }
1267                         break;
1268
1269                         case ADMIN_REQUEST_STATE:
1270                         if (admin_state(&admin->response) < 0)
1271                         {
1272                                 PERROR("Failed to create state response for socket %d.\n", admin->sock);
1273                                 goto response_error;
1274                         }
1275                         break;
1276
1277                         case ADMIN_TRACE_REQUEST:
1278                         if (admin_trace(admin, &msg.u.trace_req) < 0)
1279                         {
1280                                 PERROR("Failed to create trace response for socket %d.\n", admin->sock);
1281                                 goto response_error;
1282                         }
1283                         break;
1284
1285                         case ADMIN_REQUEST_CMD_BLOCK:
1286                         if (admin_block(&admin->response, msg.u.x.portnum, msg.u.x.block) < 0)
1287                         {
1288                                 PERROR("Failed to create block response for socket %d.\n", admin->sock);
1289                                 goto response_error;
1290                         }
1291                         break;
1292
1293                         case ADMIN_MESSAGE:
1294                         if (admin_message_to_join(&msg.u.msg, admin) < 0)
1295                         {
1296                                 PERROR("Failed to deliver message for socket %d.\n", admin->sock);
1297                                 goto response_error;
1298                         }
1299 #if 0
1300 #warning DEBUGGING
1301 {
1302         struct admin_queue      *response;
1303         printf("Chain: ");
1304         response = admin->response;
1305         while(response)
1306         {
1307                 printf("%c", '0'+response->am[0].message);
1308                 response=response->next;
1309         }
1310         printf("\n");
1311 }
1312 #endif
1313                         break;
1314
1315                         case ADMIN_CALL_SETUP:
1316                         if (admin_call(admin, &msg) < 0)
1317                         {
1318                                 PERROR("Failed to create call for socket %d.\n", admin->sock);
1319                                 response_error:
1320                                 *adminp = admin->next;
1321                                 free_connection(admin);
1322                                 admin = *adminp;
1323                                 continue;
1324                         }
1325                         break;
1326
1327                         default:
1328                         PERROR("Invalid message %d from socket %d.\n", msg.message, admin->sock);
1329                         *adminp = admin->next;
1330                         free_connection(admin);
1331                         admin = *adminp;
1332                         continue;
1333                 }
1334                 /* write queue */
1335                 send_data:
1336                 if (admin->response)
1337                 {
1338 //#warning
1339 //PERROR("DEBUG socket %d sending data.\n", admin->sock);
1340                         len = write(admin->sock, ((unsigned char *)(admin->response->am))+admin->response->offset, sizeof(struct admin_message)*(admin->response->num)-admin->response->offset);
1341                         if (len < 0)
1342                         {
1343                                 if (errno != EWOULDBLOCK)
1344                                 {
1345                                         work = 1;
1346                                         goto brokenpipe;
1347                                 }
1348                                 goto next;
1349                         }
1350                         work = 1;
1351                         if (len == 0)
1352                                 goto end;
1353                         if (len < (int)(sizeof(struct admin_message)*(admin->response->num) - admin->response->offset))
1354                         {
1355                                 admin->response->offset+=len;
1356                                 goto next;
1357                         } else
1358                         {
1359                                 temp = admin->response;
1360                                 admin->response = admin->response->next;
1361                                 FREE(temp, 0);
1362                                 memuse--;
1363                         }
1364                 }
1365                 /* done with socket instance */
1366                 next:
1367                 adminp = &admin->next;
1368                 admin = admin->next;
1369         }
1370
1371         return(work);
1372 }
1373