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