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