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