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