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