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