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