f1275a6a0ce33d66e2c72df1b727462d4fff845e
[lcr.git] / admin_server.c
1 /*****************************************************************************\
2 **                                                                           **
3 ** Linux Call Router                                                         **
4 **                                                                           **
5 **---------------------------------------------------------------------------**
6 ** Copyright: Andreas Eversberg                                              **
7 **                                                                           **
8 ** Socket link server                                                        **
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_first = 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 remote 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 Join *join, *joinnext;
97
98         /* free remote joins */
99         if (admin->remote_name[0])
100         {
101                 join = join_first;
102                 while(join)
103                 {
104                         joinnext = join->next;
105                         if (join->j_type==JOIN_TYPE_REMOTE) if (((class JoinRemote *)join)->j_remote_id == admin->sock)
106                         {
107                                 memset(&param, 0, sizeof(param));
108                                 param.disconnectinfo.cause = CAUSE_OUTOFORDER;
109                                 param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
110                                 ((class JoinRemote *)join)->message_remote(0, MESSAGE_RELEASE, &param);
111                                 /* join is now destroyed, so we go to next join */
112                         }
113                         join = joinnext;
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_first;
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_REMOTE:
259                                 apppbx->e_action = &action_remote;
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_first;
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 remote socket join instance
595  */
596 int admin_message_to_join(struct admin_msg *msg, char *remote_name, int sock_id)
597 {
598         class Join                      *join;
599         struct admin_list               *admin;
600
601         /* hello message */
602         if (msg->type == MESSAGE_HELLO)
603         {
604                 if (remote_name[0])
605                 {
606                         PERROR("Remote application repeats hello message.\n");
607                         return(-1);
608                 }
609                 /* look for second application */
610                 admin = admin_first;
611                 while(admin)
612                 {
613                         if (!strcmp(admin->remote_name, msg->param.hello.application))
614                                 break;
615                         admin = admin->next;
616                 }
617                 if (admin)
618                 {
619                         PERROR("Remote application connects twice??? (ignoring)\n");
620                         return(-1);
621                 }
622                 /* set remote socket instance */
623                 SCPY(remote_name, msg->param.hello.application);
624                 return(0);
625         }
626
627         /* check we have no application name */
628         if (remote_name[0])
629         {
630                 PERROR("Remote application did not send us a hello message.\n");
631                 return(-1);
632         }
633
634         /* new join */
635         if (msg->type == MESSAGE_NEWREF)
636         {
637                 /* create new join instance */
638                 join = new JoinRemote(0, remote_name, sock_id); // must have no serial, because no endpoint is connected
639                 if (!join)
640                         FATAL("No memory for remote join instance\n");
641                 return(0);
642         }
643
644         /* check for ref */
645         if (!msg->ref)
646         {
647                 PERROR("Remote application did not send us a valid ref with a message.\n");
648                 return(-1);
649         }
650
651         /* find join instance */
652         join = join_first;
653         while(join)
654         {
655                 if (join->j_serial == msg->ref)
656                         break;
657                 join = join->next;
658         }
659         if (!join)
660         {
661                 PERROR("No join found with serial %d.\n", msg->ref);
662                 return(-1);
663         }
664
665         /* check application */
666         if (join->j_type != JOIN_TYPE_REMOTE)
667         {
668                 PERROR("Ref %d does not belong to a remote join instance.\n", msg->ref);
669                 return(-1);
670         }
671         if (sock_id != ((class JoinRemote *)join)->j_remote_id)
672         {
673                 PERROR("Ref %d belongs to remote application %s, but not to sending application %s.\n", msg->ref, ((class JoinRemote *)join)->j_remote_name, remote_name);
674                 return(-1);
675         }
676
677         /* send message */
678         ((class JoinRemote *)join)->message_remote(msg->ref, msg->type, &msg->param);
679
680         return(0);
681 }
682
683
684 /*
685  * this function is called for every message to remote socket
686  */
687 int admin_message_from_join(int remote_id, unsigned long ref, int message_type, union parameter *param)
688 {
689         struct admin_list       *admin;
690         struct admin_queue      *response, **responsep; /* response pointer */
691
692         /* searching for admin id
693          * maybe there is no given remote application
694          */
695         admin = admin_first;
696         while(admin)
697         {
698                 if (admin->remote_name[0] && admin->sock==remote_id)
699                         break;
700                 admin = admin->next;
701         }
702         /* no given remote application connected */
703         if (!admin)
704                 return(-1);
705
706         /* seek to end of response list */
707         response = admin->response;
708         responsep = &admin->response;
709         while(response)
710         {
711                 responsep = &response->next;
712                 response = response->next;
713         }
714
715         /* create state response */
716         response = (struct admin_queue *)MALLOC(sizeof(struct admin_queue)+sizeof(admin_message));
717         memuse++;
718         response->num = 1;
719
720         /* message */
721         response->am[0].u.msg.type = message_type;
722         response->am[0].u.msg.ref = ref;
723         memcpy(&response->am[0].u.msg.param, param, sizeof(union parameter));
724
725         /* attach to response chain */
726         *responsep = response;
727         responsep = &response->next;
728
729         return(0);
730 }
731
732
733 /*
734  * do state debugging
735  */
736 int admin_state(struct admin_queue **responsep)
737 {
738
739         class Port              *port;
740         class EndpointAppPBX    *apppbx;
741         class Join              *join;
742         class Pdss1             *pdss1;
743         struct interface        *interface;
744         struct interface_port   *ifport;
745         struct mISDNport        *mISDNport;
746         int                     i;
747         int                     num;
748         int                     anybusy;
749         struct admin_queue      *response;
750         struct admin_list       *admin;
751
752         /* create state response */
753         response = (struct admin_queue *)MALLOC(sizeof(struct admin_queue)+sizeof(admin_message));
754         memuse++;
755         response->num = 1;
756         /* message */
757         response->am[0].message = ADMIN_RESPONSE_STATE;
758         /* version */
759         SCPY(response->am[0].u.s.version_string, VERSION_STRING);
760         /* time */
761         memcpy(&response->am[0].u.s.tm, now_tm, sizeof(struct tm));
762         /* log file */
763         SCPY(response->am[0].u.s.logfile, options.log);
764         /* interface count */
765         i = 0;
766         interface = interface_first;
767         while(interface)
768         {
769                 ifport = interface->ifport;
770                 while(ifport)
771                 {
772                         i++;
773                         ifport = ifport->next;
774                 }
775                 interface = interface->next;
776         }
777         response->am[0].u.s.interfaces = i;
778         /* remote connection count */
779         i = 0;
780         admin = admin_first;
781         while(admin)
782         {
783                 if (admin->remote_name[0])
784                         i++;
785                 admin = admin->next;
786         }
787         response->am[0].u.s.remotes = i;
788         /* join count */
789         join = join_first;
790         i = 0;
791         while(join)
792         {
793                 i++;
794                 join = join->next;
795         }
796         response->am[0].u.s.joins = i;
797         /* apppbx count */
798         apppbx = apppbx_first;
799         i = 0;
800         while(apppbx)
801         {
802                 i++;
803                 apppbx = apppbx->next;
804         }
805         response->am[0].u.s.epoints = i;
806         /* port count */
807         i = 0;
808         port = port_first;
809         while(port)
810         {
811                 i++;
812                 port = port->next;
813         }
814         response->am[0].u.s.ports = i;
815         /* attach to response chain */
816         *responsep = response;
817         responsep = &response->next;
818
819         /* create response for all interfaces */
820         num = (response->am[0].u.s.interfaces)+(response->am[0].u.s.joins)+(response->am[0].u.s.epoints)+(response->am[0].u.s.ports);
821         if (num == 0)
822                 return(0);
823         response = (struct admin_queue *)MALLOC(sizeof(admin_queue)+(num*sizeof(admin_message)));
824         memuse++;
825         response->num = num;
826         *responsep = response;
827         responsep = &response->next;
828         interface = interface_first;
829         num = 0;
830         while(interface)
831         {
832                 ifport = interface->ifport;
833                 while(ifport)
834                 {
835                         /* message */
836                         response->am[num].message = ADMIN_RESPONSE_S_INTERFACE;
837                         /* interface */
838                         SCPY(response->am[num].u.i.interface_name, interface->name);
839                         /* portnum */
840                         response->am[num].u.i.portnum = ifport->portnum;
841                         /* iftype */
842                         response->am[num].u.i.extension = interface->extension;
843                         /* block */
844                         response->am[num].u.i.block = ifport->block;
845                         if (ifport->mISDNport)
846                         {
847                                 mISDNport = ifport->mISDNport;
848
849                                 /* ptp */
850                                 response->am[num].u.i.ptp = mISDNport->ptp;
851                                 /* ntmode */
852                                 response->am[num].u.i.ntmode = mISDNport->ntmode;
853                                 /* pri */
854                                 response->am[num].u.i.pri = mISDNport->pri;
855                                 /* use */
856                                 response->am[num].u.i.use = mISDNport->use;
857                                 /* l1link */
858                                 response->am[num].u.i.l1link = mISDNport->l1link;
859                                 /* l2link */
860                                 response->am[num].u.i.l2link = mISDNport->l2link;
861                                 /* channels */
862                                 response->am[num].u.i.channels = mISDNport->b_num;
863                                 /* channel info */
864                                 i = 0;
865                                 anybusy = 0;
866                                 while(i < mISDNport->b_num)
867                                 {
868                                         response->am[num].u.i.busy[i] = mISDNport->b_state[i];
869                                         if (mISDNport->b_port[i])
870                                                 response->am[num].u.i.port[i] = mISDNport->b_port[i]->p_serial;
871                                         i++;
872                                 }
873                         }
874                         num++;
875
876                         ifport = ifport->next;
877                 }
878                 interface = interface->next;
879         }
880
881         /* create response for all remotes */
882         admin = admin_first;
883         while(admin)
884         {
885                 if (admin->remote_name[0])
886                 {
887                         /* message */
888                         response->am[num].message = ADMIN_RESPONSE_S_REMOTE;
889                         /* name */
890                         SCPY(response->am[num].u.r.name, admin->remote_name);
891                         /* */
892                         num++;
893                 }
894                 admin = admin->next;
895         }
896
897         /* create response for all joins */
898         join = join_first;
899         while(join)
900         {
901                 /* message */
902                 response->am[num].message = ADMIN_RESPONSE_S_JOIN;
903                 /* serial */
904                 response->am[num].u.j.serial = join->j_serial;
905                 /* partyline */
906                 if (join->j_type == JOIN_TYPE_PBX)
907                         response->am[num].u.j.partyline = ((class JoinPBX *)join)->j_partyline;
908                 /* remote application */
909                 if (join->j_type == JOIN_TYPE_REMOTE)
910                         SCPY(response->am[num].u.j.remote, ((class JoinRemote *)join)->j_remote_name);
911                 /* */
912                 join = join->next;
913                 num++;
914         }
915
916         /* create response for all endpoint */
917         apppbx = apppbx_first;
918         while(apppbx)
919         {
920                 /* message */
921                 response->am[num].message = ADMIN_RESPONSE_S_EPOINT;
922                 /* serial */
923                 response->am[num].u.e.serial = apppbx->ea_endpoint->ep_serial;
924                 /* join */
925                 response->am[num].u.e.join = apppbx->ea_endpoint->ep_join_id;
926                 /* rx notification */
927                 response->am[num].u.e.rx_state = apppbx->e_rx_state;
928                 /* tx notification */
929                 response->am[num].u.e.tx_state = apppbx->e_tx_state;
930                 /* state */
931                 switch(apppbx->e_state)
932                 {
933                         case EPOINT_STATE_IN_SETUP:
934                         response->am[num].u.e.state = ADMIN_STATE_IN_SETUP;
935                         break;
936                         case EPOINT_STATE_OUT_SETUP:
937                         response->am[num].u.e.state = ADMIN_STATE_OUT_SETUP;
938                         break;
939                         case EPOINT_STATE_IN_OVERLAP:
940                         response->am[num].u.e.state = ADMIN_STATE_IN_OVERLAP;
941                         break;
942                         case EPOINT_STATE_OUT_OVERLAP:
943                         response->am[num].u.e.state = ADMIN_STATE_OUT_OVERLAP;
944                         break;
945                         case EPOINT_STATE_IN_PROCEEDING:
946                         response->am[num].u.e.state = ADMIN_STATE_IN_PROCEEDING;
947                         break;
948                         case EPOINT_STATE_OUT_PROCEEDING:
949                         response->am[num].u.e.state = ADMIN_STATE_OUT_PROCEEDING;
950                         break;
951                         case EPOINT_STATE_IN_ALERTING:
952                         response->am[num].u.e.state = ADMIN_STATE_IN_ALERTING;
953                         break;
954                         case EPOINT_STATE_OUT_ALERTING:
955                         response->am[num].u.e.state = ADMIN_STATE_OUT_ALERTING;
956                         break;
957                         case EPOINT_STATE_CONNECT:
958                         response->am[num].u.e.state = ADMIN_STATE_CONNECT;
959                         break;
960                         case EPOINT_STATE_IN_DISCONNECT:
961                         response->am[num].u.e.state = ADMIN_STATE_IN_DISCONNECT;
962                         break;
963                         case EPOINT_STATE_OUT_DISCONNECT:
964                         response->am[num].u.e.state = ADMIN_STATE_OUT_DISCONNECT;
965                         break;
966                         default:
967                         response->am[num].u.e.state = ADMIN_STATE_IDLE;
968                 }
969                 /* terminal */
970                 SCPY(response->am[num].u.e.terminal, apppbx->e_ext.number);
971                 /* callerid */
972                 SCPY(response->am[num].u.e.callerid, apppbx->e_callerinfo.id);
973                 /* dialing */
974                 SCPY(response->am[num].u.e.dialing, apppbx->e_dialinginfo.id);
975                 /* action string */
976                 if (apppbx->e_action)
977                         SCPY(response->am[num].u.e.action, action_defs[apppbx->e_action->index].name);
978 //              if (apppbx->e_action)
979 //              printf("action=%s\n",action_defs[apppbx->e_action->index].name);
980                 /* park */
981                 response->am[num].u.e.park = apppbx->ea_endpoint->ep_park;
982                 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))
983                         memcpy(response->am[num].u.e.park_callid, apppbx->ea_endpoint->ep_park_callid, apppbx->ea_endpoint->ep_park_len);
984                 response->am[num].u.e.park_len = apppbx->ea_endpoint->ep_park_len;
985                 /* crypt */
986                 if (apppbx->e_crypt == CRYPT_ON)
987                         response->am[num].u.e.crypt = 1;
988                 /* */
989                 apppbx = apppbx->next;
990                 num++;
991         }
992
993         /* create response for all ports */
994         port = port_first;
995         while(port)
996         {
997                 /* message */
998                 response->am[num].message = ADMIN_RESPONSE_S_PORT;
999                 /* serial */
1000                 response->am[num].u.p.serial = port->p_serial;
1001                 /* name */
1002                 SCPY(response->am[num].u.p.name, port->p_name);
1003                 /* epoint */
1004                 response->am[num].u.p.epoint = ACTIVE_EPOINT(port->p_epointlist);
1005                 /* state */
1006                 switch(port->p_state)
1007                 {
1008                         case PORT_STATE_IN_SETUP:
1009                         response->am[num].u.p.state = ADMIN_STATE_IN_SETUP;
1010                         break;
1011                         case PORT_STATE_OUT_SETUP:
1012                         response->am[num].u.p.state = ADMIN_STATE_OUT_SETUP;
1013                         break;
1014                         case PORT_STATE_IN_OVERLAP:
1015                         response->am[num].u.p.state = ADMIN_STATE_IN_OVERLAP;
1016                         break;
1017                         case PORT_STATE_OUT_OVERLAP:
1018                         response->am[num].u.p.state = ADMIN_STATE_OUT_OVERLAP;
1019                         break;
1020                         case PORT_STATE_IN_PROCEEDING:
1021                         response->am[num].u.p.state = ADMIN_STATE_IN_PROCEEDING;
1022                         break;
1023                         case PORT_STATE_OUT_PROCEEDING:
1024                         response->am[num].u.p.state = ADMIN_STATE_OUT_PROCEEDING;
1025                         break;
1026                         case PORT_STATE_IN_ALERTING:
1027                         response->am[num].u.p.state = ADMIN_STATE_IN_ALERTING;
1028                         break;
1029                         case PORT_STATE_OUT_ALERTING:
1030                         response->am[num].u.p.state = ADMIN_STATE_OUT_ALERTING;
1031                         break;
1032                         case PORT_STATE_CONNECT:
1033                         response->am[num].u.p.state = ADMIN_STATE_CONNECT;
1034                         break;
1035                         case PORT_STATE_IN_DISCONNECT:
1036                         response->am[num].u.p.state = ADMIN_STATE_IN_DISCONNECT;
1037                         break;
1038                         case PORT_STATE_OUT_DISCONNECT:
1039                         response->am[num].u.p.state = ADMIN_STATE_OUT_DISCONNECT;
1040                         break;
1041                         default:
1042                         response->am[num].u.p.state = ADMIN_STATE_IDLE;
1043                 }
1044                 /* isdn */
1045                 if ((port->p_type&PORT_CLASS_mISDN_MASK) == PORT_CLASS_mISDN_DSS1)
1046                 {
1047                         response->am[num].u.p.isdn = 1;
1048                         pdss1 = (class Pdss1 *)port;
1049                         response->am[num].u.p.isdn_chan = pdss1->p_m_b_channel;
1050                         response->am[num].u.p.isdn_hold = pdss1->p_m_hold;
1051                         response->am[num].u.p.isdn_ces = pdss1->p_m_d_ces;
1052                 }
1053                 /* */
1054                 port = port->next;
1055                 num++;
1056         }
1057         return(0);
1058 }
1059
1060 int sockserial = 1; // must start with 1, because 0 is used if no serial is set
1061 /*
1062  * handle admin socket (non blocking)
1063  */
1064 int admin_handle(void)
1065 {
1066         struct admin_list       *admin, **adminp;
1067         void                    *temp;
1068         struct admin_message    msg;
1069         int                     len;
1070         int                     new_sock;
1071         socklen_t               sock_len = sizeof(sock_address);
1072         unsigned long           on = 1;
1073         int                     work = 0; /* if work was done */
1074         struct Endpoint         *epoint;
1075
1076         if (sock < 0)
1077                 return(0);
1078
1079         /* check for new incomming connections */
1080         if ((new_sock = accept(sock, (struct sockaddr *)&sock_address, &sock_len)) >= 0)
1081         {
1082                 work = 1;
1083                 /* insert new socket */
1084                 admin = (struct admin_list *)MALLOC(sizeof(struct admin_list));
1085                 if (ioctl(new_sock, FIONBIO, (unsigned char *)(&on)) >= 0)
1086                 {
1087 //#warning
1088 //      PERROR("DEBUG incomming socket %d, serial=%d\n", new_sock, sockserial);
1089                         memuse++;
1090                         fhuse++;
1091                         admin->sockserial = sockserial++;
1092                         admin->next = admin_first;
1093                         admin_first = admin;
1094                         admin->sock = new_sock;
1095                 } else {
1096                         close(new_sock);
1097                         FREE(admin, sizeof(struct admin_list));
1098                 }
1099         } else
1100         {
1101                 if (errno != EWOULDBLOCK)
1102                 {
1103                         PERROR("Failed to accept connection from socket \"%s\". (errno=%d) Closing socket.\n", sock_address.sun_path, errno);
1104                         admin_cleanup();
1105                         return(1);
1106                 }
1107         }
1108
1109         /* loop all current socket connections */
1110         admin = admin_first;
1111         adminp = &admin_first;
1112         while(admin)
1113         {
1114                 /* read command */
1115                 len = read(admin->sock, &msg, sizeof(msg));
1116                 if (len < 0)
1117                 {
1118                         if (errno != EWOULDBLOCK)
1119                         {
1120                                 work = 1;
1121                                 brokenpipe:
1122                                 printf("Broken pipe on socket %d. (errno=%d).\n", admin->sock, errno);
1123                                 PDEBUG(DEBUG_LOG, "Broken pipe on socket %d. (errno=%d).\n", admin->sock, errno);
1124                                 *adminp = admin->next;
1125                                 free_connection(admin);
1126                                 admin = *adminp;
1127                                 continue;
1128                         }
1129                         goto send_data;
1130                 }
1131                 work = 1;
1132 //#warning
1133 //PERROR("DEBUG socket %d got data. serial=%d\n", admin->sock, admin->sockserial);
1134                 if (len == 0)
1135                 {
1136                         end:
1137
1138                         /*release endpoint if exists */
1139                         if (admin->epointid)
1140                         {
1141                                 epoint = find_epoint_id(admin->epointid);
1142                                 if (epoint)
1143                                 {
1144                                         ((class DEFAULT_ENDPOINT_APP *)epoint->ep_app)->
1145                                                 release(RELEASE_ALL, CAUSE_NORMAL, LOCATION_PRIVATE_LOCAL, 0, 0);
1146                                 }
1147                         }
1148
1149 //#warning
1150 //PERROR("DEBUG socket %d closed by remote.\n", admin->sock);
1151                         *adminp = admin->next;
1152                         free_connection(admin);
1153                         admin = *adminp;
1154 //PERROR("DEBUG (admin_first=%x)\n", admin_first);
1155                         continue;
1156                 }
1157                 if (len != sizeof(msg))
1158                 {
1159                         PERROR("Short/long read on socket %d. (len=%d != size=%d).\n", admin->sock, len, sizeof(msg));
1160                         *adminp = admin->next;
1161                         free_connection(admin);
1162                         admin = *adminp;
1163                         continue;
1164                 }
1165                 /* process socket command */
1166                 if (admin->response)
1167                 {
1168                         PERROR("Data from socket %d while sending response.\n", admin->sock);
1169                         *adminp = admin->next;
1170                         free_connection(admin);
1171                         admin = *adminp;
1172                         continue;
1173                 }
1174                 switch (msg.message)
1175                 {
1176                         case ADMIN_REQUEST_CMD_INTERFACE:
1177                         if (admin_interface(&admin->response) < 0)
1178                         {
1179                                 PERROR("Failed to create dial response for socket %d.\n", admin->sock);
1180                                 goto response_error;
1181                         }
1182                         break;
1183
1184                         case ADMIN_REQUEST_CMD_ROUTE:
1185                         if (admin_route(&admin->response) < 0)
1186                         {
1187                                 PERROR("Failed to create dial response for socket %d.\n", admin->sock);
1188                                 goto response_error;
1189                         }
1190                         break;
1191
1192                         case ADMIN_REQUEST_CMD_DIAL:
1193                         if (admin_dial(&admin->response, msg.u.x.message) < 0)
1194                         {
1195                                 PERROR("Failed to create dial response for socket %d.\n", admin->sock);
1196                                 goto response_error;
1197                         }
1198                         break;
1199
1200                         case ADMIN_REQUEST_CMD_RELEASE:
1201                         if (admin_release(&admin->response, msg.u.x.message) < 0)
1202                         {
1203                                 PERROR("Failed to create release response for socket %d.\n", admin->sock);
1204                                 goto response_error;
1205                         }
1206                         break;
1207
1208                         case ADMIN_REQUEST_STATE:
1209                         if (admin_state(&admin->response) < 0)
1210                         {
1211                                 PERROR("Failed to create state response for socket %d.\n", admin->sock);
1212                                 goto response_error;
1213                         }
1214                         break;
1215
1216                         case ADMIN_TRACE_REQUEST:
1217                         if (admin_trace(admin, &msg.u.trace_req) < 0)
1218                         {
1219                                 PERROR("Failed to create trace response for socket %d.\n", admin->sock);
1220                                 goto response_error;
1221                         }
1222                         break;
1223
1224                         case ADMIN_REQUEST_CMD_BLOCK:
1225                         if (admin_block(&admin->response, msg.u.x.portnum, msg.u.x.block) < 0)
1226                         {
1227                                 PERROR("Failed to create block response for socket %d.\n", admin->sock);
1228                                 goto response_error;
1229                         }
1230                         break;
1231
1232                         case ADMIN_MESSAGE:
1233                         if (admin_message_to_join(&msg.u.msg, admin->remote_name, admin->sock) < 0)
1234                         {
1235                                 PERROR("Failed to deliver message for socket %d.\n", admin->sock);
1236                                 goto response_error;
1237                         }
1238 #if 0
1239 #warning DEBUGGING
1240 {
1241         struct admin_queue      *response;
1242         printf("Chain: ");
1243         response = admin->response;
1244         while(response)
1245         {
1246                 printf("%c", '0'+response->am[0].message);
1247                 response=response->next;
1248         }
1249         printf("\n");
1250 }
1251 #endif
1252                         break;
1253
1254                         case ADMIN_CALL_SETUP:
1255                         if (admin_call(admin, &msg) < 0)
1256                         {
1257                                 PERROR("Failed to create call for socket %d.\n", admin->sock);
1258                                 response_error:
1259                                 *adminp = admin->next;
1260                                 free_connection(admin);
1261                                 admin = *adminp;
1262                                 continue;
1263                         }
1264                         break;
1265
1266                         default:
1267                         PERROR("Invalid message %d from socket %d.\n", msg.message, admin->sock);
1268                         *adminp = admin->next;
1269                         free_connection(admin);
1270                         admin = *adminp;
1271                         continue;
1272                 }
1273                 /* write queue */
1274                 send_data:
1275                 if (admin->response)
1276                 {
1277 //#warning
1278 //PERROR("DEBUG socket %d sending data.\n", admin->sock);
1279                         len = write(admin->sock, ((unsigned char *)(admin->response->am))+admin->response->offset, sizeof(struct admin_message)*(admin->response->num)-admin->response->offset);
1280                         if (len < 0)
1281                         {
1282                                 if (errno != EWOULDBLOCK)
1283                                 {
1284                                         work = 1;
1285                                         goto brokenpipe;
1286                                 }
1287                                 goto next;
1288                         }
1289                         work = 1;
1290                         if (len == 0)
1291                                 goto end;
1292                         if (len < (int)(sizeof(struct admin_message)*(admin->response->num)-admin->response->offset))
1293                         {
1294                                 admin->response->offset+=len;
1295                                 goto next;
1296                         } else
1297                         {
1298                                 temp = admin->response;
1299                                 admin->response = admin->response->next;
1300                                 FREE(temp, 0);
1301                                 memuse--;
1302                         }
1303                 }
1304                 /* done with socket instance */
1305                 next:
1306                 adminp = &admin->next;
1307                 admin = admin->next;
1308         }
1309
1310         return(work);
1311 }
1312