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