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