1 /*****************************************************************************\
3 ** Linux Call Router **
5 **---------------------------------------------------------------------------**
6 ** Copyright: Andreas Eversberg **
8 ** Socket link server **
10 \*****************************************************************************/
13 #include <sys/socket.h>
16 #ifdef PACKAGE_VERSION
17 #undef PACKAGE_VERSION
22 char socket_name[128];
24 struct sockaddr_un sock_address;
26 struct admin_list *admin_first = NULL;
27 static struct lcr_fd admin_fd;
29 int admin_handle(struct lcr_fd *fd, unsigned int what, void *instance, int index);
32 * initialize admin socket
36 /* open and bind socket */
37 if ((sock = socket(PF_UNIX, SOCK_STREAM, 0)) < 0) {
38 PERROR("Failed to create admin socket. (errno=%d)\n", errno);
42 memset(&sock_address, 0, sizeof(sock_address));
43 SPRINT(socket_name, SOCKET_NAME, options.lock);
44 sock_address.sun_family = AF_UNIX;
45 UCPY(sock_address.sun_path, socket_name);
47 if (bind(sock, (struct sockaddr *)(&sock_address), SUN_LEN(&sock_address)) < 0) {
52 PERROR("Failed to bind admin socket to \"%s\". (errno=%d)\n", sock_address.sun_path, errno);
55 if (listen(sock, 5) < 0) {
60 PERROR("Failed to listen to socket \"%s\". (errno=%d)\n", sock_address.sun_path, errno);
63 memset(&admin_fd, 0, sizeof(admin_fd));
65 register_fd(&admin_fd, LCR_FD_READ | LCR_FD_EXCEPT, admin_handle, NULL, 0);
66 if (chmod(socket_name, options.socketrights) < 0) {
67 PERROR("Failed to change socket rights to %d. (errno=%d)\n", options.socketrights, errno);
69 if (chown(socket_name, options.socketuser, options.socketgroup) < 0) {
70 PERROR("Failed to change socket user/group to %d/%d. (errno=%d)\n", options.socketuser, options.socketgroup, errno);
80 void free_connection(struct admin_list *admin)
82 struct admin_queue *response;
84 union parameter param;
85 class Port *port, *portnext;
86 class Premote *remote;
87 struct admin_list **adminp;
89 /* free remote ports */
90 if (admin->remote_name[0]) {
98 "REMOTE APP release");
99 add_trace("app", "name", "%s", admin->remote_name);
101 /* release remote port */
104 portnext = port->next;
105 if ((port->p_type & PORT_CLASS_MASK) == PORT_CLASS_REMOTE) {
106 remote = (class Premote *) port;
107 if (remote->p_r_remote_id == admin->sock) {
108 memset(¶m, 0, sizeof(param));
109 param.disconnectinfo.cause = CAUSE_OUTOFORDER;
110 param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
111 remote->message_remote(MESSAGE_RELEASE, ¶m);
112 /* port is now destroyed, so we go to next join */
119 if (admin->sock >= 0) {
120 unregister_fd(&admin->fd);
124 response = admin->response;
126 temp = response->next;
129 response = (struct admin_queue *)temp;
132 adminp = &admin_first;
134 if (*adminp == admin)
136 adminp = &((*adminp)->next);
139 *adminp = (*adminp)->next;
147 * cleanup admin socket
149 void admin_cleanup(void)
151 struct admin_list *admin, *next;;
156 free_connection(admin);
161 unregister_fd(&admin_fd);
171 * do interface reload
173 int admin_interface(struct admin_queue **responsep)
175 struct admin_queue *response; /* response pointer */
176 const char *err_txt = "";
179 if (read_interfaces()) {
181 free_interfaces(interface_first);
182 interface_first = interface_newlist;
183 interface_newlist = NULL;
185 err_txt = interface_error;
188 /* create state response */
189 response = (struct admin_queue *)MALLOC(sizeof(struct admin_queue)+sizeof(admin_message));
193 response->am[0].message = ADMIN_RESPONSE_CMD_INTERFACE;
195 response->am[0].u.x.error = err;
197 SCPY(response->am[0].u.x.message, err_txt);
198 /* attach to response chain */
199 *responsep = response;
200 responsep = &response->next;
208 int admin_route(struct admin_queue **responsep)
210 struct route_ruleset *ruleset_new;
211 struct admin_queue *response; /* response pointer */
212 char err_txt[256] = "";
217 class EndpointAppPBX *apppbx;
221 apppbx = apppbx_first;
224 apppbx = apppbx->next;
227 SPRINT(err_txt, "Cannot reload routing, because %d endpoints active\n", n);
232 if (!(ruleset_new = ruleset_parse())) {
233 SPRINT(err_txt, ruleset_error);
237 ruleset_free(ruleset_first);
238 ruleset_first = ruleset_new;
239 ruleset_main = getrulesetbyname("main");
241 SPRINT(err_txt, "Ruleset reloaded, but rule 'main' not found.\n");
244 apppbx = apppbx_first;
246 if (apppbx->e_action) {
247 switch(apppbx->e_action->index) {
248 case ACTION_INTERNAL:
249 apppbx->e_action = &action_internal;
251 case ACTION_EXTERNAL:
252 apppbx->e_action = &action_external;
254 case ACTION_VBOX_RECORD:
255 apppbx->e_action = &action_vbox;
257 case ACTION_PARTYLINE:
258 apppbx->e_action = &action_partyline;
263 } else if (apppbx->e_state != EPOINT_STATE_CONNECT) {
265 unsched_timer(&apppbx->e_callback_timeout);
266 apppbx->e_action = NULL;
267 apppbx->release(RELEASE_ALL, LOCATION_PRIVATE_LOCAL, CAUSE_NORMAL, LOCATION_PRIVATE_LOCAL, CAUSE_NORMAL, 0);
270 numberrize_callerinfo(apppbx->e_callerinfo.id, apppbx->e_callerinfo.ntype, options.national, options.international),
271 apppbx->e_dialinginfo.id,
274 apppbx->ea_endpoint->ep_serial,
275 "KICK (reload routing)");
279 unsched_timer(&apppbx->e_action_timeout);
280 apppbx->e_rule = NULL;
281 apppbx->e_ruleset = NULL;
283 apppbx = apppbx->next;
287 /* create state response */
288 response = (struct admin_queue *)MALLOC(sizeof(struct admin_queue)+sizeof(admin_message));
292 response->am[0].message = ADMIN_RESPONSE_CMD_ROUTE;
294 response->am[0].u.x.error = err;
296 SCPY(response->am[0].u.x.message, err_txt);
297 /* attach to response chain */
298 *responsep = response;
299 responsep = &response->next;
307 int admin_dial(struct admin_queue **responsep, char *message)
309 struct extension ext; /* temporary extension's settings */
310 struct admin_queue *response; /* response pointer */
311 char *p; /* pointer to dialing digits */
313 /* create state response */
314 response = (struct admin_queue *)MALLOC(sizeof(struct admin_queue)+sizeof(admin_message));
318 response->am[0].message = ADMIN_RESPONSE_CMD_DIAL;
320 /* process request */
321 if (!(p = strchr(message,':'))) {
322 response->am[0].u.x.error = -EINVAL;
323 SPRINT(response->am[0].u.x.message, "no seperator ':' in message to seperate number from extension");
328 /* modify extension */
329 if (!read_extension(&ext, message)) {
330 response->am[0].u.x.error = -EINVAL;
331 SPRINT(response->am[0].u.x.message, "extension doesn't exist");
335 write_extension(&ext, message);
338 /* attach to response chain */
339 *responsep = response;
340 responsep = &response->next;
348 int admin_trace(struct admin_list *admin, struct admin_trace_req *trace)
350 memcpy(&admin->trace, trace, sizeof(struct admin_trace_req));
358 * 0 = make port available
359 * 1 = make port administratively blocked
361 * the result is returned:
362 * 0 = port is now available
363 * 1 = port is now blocked
364 * 2 = port cannot be loaded or has been unloaded
365 * -1 = port doesn't exist
367 int admin_block(struct admin_queue **responsep, int portnum, int block)
369 struct admin_queue *response; /* response pointer */
370 struct interface *interface;
371 struct interface_port *ifport;
373 /* create block response */
374 response = (struct admin_queue *)MALLOC(sizeof(struct admin_queue)+sizeof(admin_message));
378 response->am[0].message = ADMIN_RESPONSE_CMD_BLOCK;
379 response->am[0].u.x.portnum = portnum;
381 /* search for port */
383 interface = interface_first;
385 ifport = interface->ifport;
387 if (ifport->portnum == portnum)
389 ifport = ifport->next;
393 interface = interface->next;
395 /* not found, we return -1 */
397 response->am[0].u.x.block = -1;
398 response->am[0].u.x.error = 1;
399 SPRINT(response->am[0].u.x.message, "Port %d does not exist.", portnum);
405 if (!ifport->mISDNport) {
406 /* not loaded anyway */
408 response->am[0].u.x.block = 2;
412 /* try loading interface */
413 ifport->block = block;
414 load_mISDN_port(ifport);
416 /* port cannot load */
417 if (ifport->block >= 2) {
418 response->am[0].u.x.block = 2;
419 response->am[0].u.x.error = 1;
420 SPRINT(response->am[0].u.x.message, "Port %d will not load.", portnum);
425 response->am[0].u.x.block = ifport->block;
429 /* if we shall unload interface */
431 mISDNport_close(ifport->mISDNport);
432 ifport->mISDNport = 0;
438 /* port new blocking state */
439 ifport->block = response->am[0].u.x.block = block;
442 /* attach to response chain */
443 *responsep = response;
444 responsep = &response->next;
452 int admin_release(struct admin_queue **responsep, char *message)
455 struct admin_queue *response; /* response pointer */
456 class EndpointAppPBX *apppbx;
458 /* create state response */
459 response = (struct admin_queue *)MALLOC(sizeof(struct admin_queue)+sizeof(admin_message));
463 response->am[0].message = ADMIN_RESPONSE_CMD_RELEASE;
466 apppbx = apppbx_first;
468 if (apppbx->ea_endpoint->ep_serial == id)
470 apppbx = apppbx->next;
473 response->am[0].u.x.error = -EINVAL;
474 SPRINT(response->am[0].u.x.message, "Given endpoint %d doesn't exist.", id);
478 unsched_timer(&apppbx->e_callback_timeout);
479 apppbx->release(RELEASE_ALL, LOCATION_PRIVATE_LOCAL, CAUSE_NORMAL, LOCATION_PRIVATE_LOCAL, CAUSE_NORMAL, 0);
482 /* attach to response chain */
483 *responsep = response;
484 responsep = &response->next;
492 int admin_call(struct admin_list *admin, struct admin_message *msg)
494 class Endpoint *epoint;
495 class EndpointAppPBX *apppbx;
497 if (!(epoint = new Endpoint(0, 0)))
498 FATAL("No memory for Endpoint instance\n");
499 if (!(epoint->ep_app = apppbx = new EndpointAppPBX(epoint, 1))) // outgoing
500 FATAL("No memory for Endpoint Application instance\n");
501 apppbx->e_adminid = admin->sockserial;
502 admin->epointid = epoint->ep_serial;
503 SCPY(apppbx->e_callerinfo.id, nationalize_callerinfo(msg->u.call.callerid, &apppbx->e_callerinfo.ntype, options.national, options.international));
504 if (msg->u.call.present)
505 apppbx->e_callerinfo.present = INFO_PRESENT_ALLOWED;
507 apppbx->e_callerinfo.present = INFO_PRESENT_RESTRICTED;
508 apppbx->e_callerinfo.screen = INFO_SCREEN_NETWORK;
511 apppbx->e_capainfo.bearer_capa = msg->u.call.bc_capa;
512 apppbx->e_capainfo.bearer_mode = msg->u.call.bc_mode;
513 apppbx->e_capainfo.bearer_info1 = msg->u.call.bc_info1;
514 apppbx->e_capainfo.hlc = msg->u.call.hlc;
515 apppbx->e_capainfo.exthlc = msg->u.call.exthlc;
516 SCPY(apppbx->e_dialinginfo.id, msg->u.call.dialing);
517 SCPY(apppbx->e_dialinginfo.interfaces, msg->u.call.interface);
518 apppbx->e_dialinginfo.sending_complete = 1;
520 apppbx->new_state(PORT_STATE_OUT_SETUP);
521 apppbx->out_setup(0);
527 * this function is called for response whenever a call state changes.
529 void admin_call_response(int adminid, int message, const char *connected, int cause, int location, int notify_progress)
531 struct admin_list *admin;
532 struct admin_queue *response, **responsep; /* response pointer */
534 /* searching for admin id
535 * maybe there is no admin instance, because the calling port was not
536 * initiated by admin_call */
539 if (adminid == admin->sockserial)
546 /* seek to end of response list */
547 response = admin->response;
548 responsep = &admin->response;
550 responsep = &response->next;
551 response = response->next;
554 /* create state response */
555 response = (struct admin_queue *)MALLOC(sizeof(struct admin_queue)+sizeof(admin_message));
559 response->am[0].message = message;
561 SCPY(response->am[0].u.call.callerid, connected);
562 response->am[0].u.call.cause = cause;
563 response->am[0].u.call.location = location;
564 response->am[0].u.call.notify_progress = notify_progress;
566 /* attach to response chain */
567 *responsep = response;
568 responsep = &response->next;
569 admin->fd.when |= LCR_FD_WRITE;
574 * send data to the remote socket join instance
576 int admin_message_to_lcr(struct admin_msg *msg, struct admin_list *admin)
579 class Premote *remote = NULL; /* make GCC happy */
580 struct interface *interface;
581 struct admin_list *temp;
584 if (msg->type == MESSAGE_HELLO) {
585 if (admin->remote_name[0]) {
586 PERROR("Remote application repeats hello message.\n");
589 /* look for second application */
592 if (!strcmp(temp->remote_name, msg->param.hello.application))
597 PERROR("Remote application connects twice??? (ignoring)\n");
600 /* set remote socket instance */
601 SCPY(admin->remote_name, msg->param.hello.application);
609 "REMOTE APP registers");
610 add_trace("app", "name", "%s", admin->remote_name);
615 /* check we have no application name */
616 if (!admin->remote_name[0]) {
617 PERROR("Remote application did not send us a hello message.\n");
621 /* new remote instance. the reply (NEWREF assignment) is sent from constructor */
622 if (msg->type == MESSAGE_NEWREF) {
624 /* find remote port */
625 interface = interface_first;
627 /* interface must match the remote application */
628 if (interface->remote && !strcmp(interface->remote_app, admin->remote_name)) {
629 /* interface must match the name, if given */
630 if (!msg->param.newref.interface[0] || !strcmp(msg->param.newref.interface, interface->name))
633 interface = interface->next;
636 union parameter param;
637 unsigned int ref = new_remote++;
646 "REMOTE APP illegal interface");
647 add_trace("app", "name", "%s", admin->remote_name);
648 add_trace("interface", "name", "%s", msg->param.newref.interface);
650 memset(¶m, 0, sizeof(union parameter));
651 param.newref.direction = 0;
652 admin_message_from_lcr(admin->sock, ref, MESSAGE_NEWREF, ¶m);
653 memset(¶m, 0, sizeof(union parameter));
654 param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
655 param.disconnectinfo.cause = CAUSE_RESSOURCEUNAVAIL;
656 admin_message_from_lcr(admin->sock, ref, MESSAGE_RELEASE, ¶m);
659 /* creating port object, transparent until setup with hdlc */
660 SPRINT(name, "%s-%s-in", interface->name, interface->remote_app);
661 if (!(remote = new Premote(PORT_TYPE_REMOTE_IN, name, NULL, interface, admin->sock)))
663 FATAL("Cannot create Port instance.\n");
669 PERROR("Remote application did not send us a valid ref with a message.\n");
673 /* find port instance */
676 if ((port->p_type & PORT_CLASS_mISDN_MASK) == PORT_CLASS_REMOTE) {
677 remote = (class Premote *) port;
678 if (remote->p_r_ref == msg->ref)
684 if (admin->sock != remote->p_r_remote_id) {
685 PERROR("Ref %d belongs to remote application %s, but not to sending application %s.\n", msg->ref, remote->p_r_remote_app, admin->remote_name);
690 remote->message_remote(msg->type, &msg->param);
695 PDEBUG(DEBUG_LOG, "No remote instance found with ref %d. (May have been already released.)\n", msg->ref);
702 * this function is called for every message to remote socket
704 int admin_message_from_lcr(int remote_id, unsigned int ref, int message_type, union parameter *param)
706 struct admin_list *admin;
707 struct admin_queue **responsep; /* response pointer */
709 /* searching for admin id
710 * maybe there is no given remote application
714 if (admin->remote_name[0] && admin->sock==remote_id)
718 /* no given remote application connected */
722 /* seek to end of response list */
723 responsep = &admin->response;
725 responsep = &(*responsep)->next;
728 /* create state response */
729 *responsep = (struct admin_queue *)MALLOC(sizeof(struct admin_queue)+sizeof(admin_message));
731 (*responsep)->num = 1;
734 (*responsep)->am[0].message = ADMIN_MESSAGE;
735 (*responsep)->am[0].u.msg.type = message_type;
736 (*responsep)->am[0].u.msg.ref = ref;
737 memcpy(&(*responsep)->am[0].u.msg.param, param, sizeof(union parameter));
738 admin->fd.when |= LCR_FD_WRITE;
746 int admin_state(struct admin_queue **responsep)
749 class EndpointAppPBX *apppbx;
753 struct mISDNport *mISDNport;
754 struct select_channel *selchannel;
756 struct interface *interface;
757 struct interface_port *ifport;
760 struct admin_queue *response;
761 struct admin_list *admin;
765 /* create state response */
766 response = (struct admin_queue *)MALLOC(sizeof(struct admin_queue)+sizeof(admin_message));
770 response->am[0].message = ADMIN_RESPONSE_STATE;
772 SCPY(response->am[0].u.s.version_string, VERSION_STRING);
775 now_tm = localtime(&now);
776 memcpy(&response->am[0].u.s.tm, now_tm, sizeof(struct tm));
778 SCPY(response->am[0].u.s.logfile, options.log);
779 /* interface count */
781 interface = interface_first;
783 ifport = interface->ifport;
788 ifport = ifport->next;
790 interface = interface->next;
792 response->am[0].u.s.interfaces = i;
793 /* remote connection count */
797 if (admin->remote_name[0])
801 response->am[0].u.s.remotes = i;
809 response->am[0].u.s.joins = i;
811 apppbx = apppbx_first;
815 apppbx = apppbx->next;
817 response->am[0].u.s.epoints = i;
825 response->am[0].u.s.ports = i;
826 /* attach to response chain */
827 *responsep = response;
828 responsep = &response->next;
830 /* create response for all instances */
831 num = (response->am[0].u.s.interfaces)
832 + (response->am[0].u.s.remotes)
833 + (response->am[0].u.s.joins)
834 + (response->am[0].u.s.epoints)
835 + (response->am[0].u.s.ports);
838 response = (struct admin_queue *)MALLOC(sizeof(admin_queue)+(num*sizeof(admin_message)));
841 *responsep = response;
842 responsep = &response->next;
843 interface = interface_first;
846 ifport = interface->ifport;
849 response->am[num].message = ADMIN_RESPONSE_S_INTERFACE;
851 SCPY(response->am[num].u.i.interface_name, interface->name);
853 response->am[num].u.i.portnum = -100; /* indicate: no ifport */
855 response->am[num].u.i.extension = interface->extension;
861 response->am[num].message = ADMIN_RESPONSE_S_INTERFACE;
863 SCPY(response->am[num].u.i.interface_name, interface->name);
865 response->am[num].u.i.portnum = ifport->portnum;
867 SCPY(response->am[num].u.i.portname, ifport->portname);
869 response->am[num].u.i.extension = interface->extension;
871 response->am[num].u.i.block = ifport->block;
873 if (ifport->mISDNport) {
874 mISDNport = ifport->mISDNport;
877 response->am[num].u.i.ptp = mISDNport->ptp;
879 response->am[num].u.i.l1hold = mISDNport->l1hold;
881 response->am[num].u.i.l2hold = mISDNport->l2hold;
883 response->am[num].u.i.ntmode = mISDNport->ntmode;
885 response->am[num].u.i.pri = mISDNport->pri;
887 response->am[num].u.i.use = mISDNport->use;
889 response->am[num].u.i.l1link = mISDNport->l1link;
891 response->am[num].u.i.l2link = mISDNport->l2link;
892 memcpy(response->am[num].u.i.l2mask, mISDNport->l2mask, 16);
894 response->am[num].u.i.los = mISDNport->los;
896 response->am[num].u.i.ais = mISDNport->ais;
898 response->am[num].u.i.rdi = mISDNport->rdi;
900 response->am[num].u.i.slip_tx = mISDNport->slip_tx;
901 response->am[num].u.i.slip_rx = mISDNport->slip_rx;
903 response->am[num].u.i.channels = mISDNport->b_num;
904 /* channel selection */
905 selchannel = ifport->out_channel;
906 if (ifport->channel_force)
907 SCAT(response->am[num].u.i.out_channel, "force");
909 if (response->am[num].u.i.out_channel[0])
910 SCAT(response->am[num].u.i.out_channel, ",");
911 switch (selchannel->channel) {
913 SCAT(response->am[num].u.i.out_channel, "no");
916 SCAT(response->am[num].u.i.out_channel, "any");
919 SCAT(response->am[num].u.i.out_channel, "free");
922 SPRINT(strchr(response->am[num].u.i.out_channel, '\0'), "%d", selchannel->channel);
924 selchannel = selchannel->next;
926 selchannel = ifport->in_channel;
928 switch (selchannel->channel) {
930 SCAT(response->am[num].u.i.in_channel, "free");
933 SPRINT(strchr(response->am[num].u.i.in_channel, '\0'), "%d", selchannel->channel);
935 selchannel = selchannel->next;
939 while(i < mISDNport->b_num) {
940 response->am[num].u.i.busy[i] = mISDNport->b_state[i];
941 if (mISDNport->b_port[i])
942 response->am[num].u.i.port[i] = mISDNport->b_port[i]->p_serial;
943 response->am[num].u.i.mode[i] = mISDNport->b_mode[i];
950 ifport = ifport->next;
952 interface = interface->next;
955 /* create response for all remotes */
958 if (admin->remote_name[0]) {
960 response->am[num].message = ADMIN_RESPONSE_S_REMOTE;
962 SCPY(response->am[num].u.r.name, admin->remote_name);
969 /* create response for all joins */
973 response->am[num].message = ADMIN_RESPONSE_S_JOIN;
975 response->am[num].u.j.serial = join->j_serial;
977 if (join->j_type == JOIN_TYPE_PBX) {
978 response->am[num].u.j.partyline = ((class JoinPBX *)join)->j_partyline;
979 response->am[num].u.j.threepty = ((class JoinPBX *)join)->j_3pty;
986 /* create response for all endpoint */
987 apppbx = apppbx_first;
990 response->am[num].message = ADMIN_RESPONSE_S_EPOINT;
992 response->am[num].u.e.serial = apppbx->ea_endpoint->ep_serial;
994 response->am[num].u.e.join = apppbx->ea_endpoint->ep_join_id;
995 /* rx notification */
996 response->am[num].u.e.rx_state = apppbx->e_rx_state;
997 /* tx notification */
998 response->am[num].u.e.tx_state = apppbx->e_tx_state;
1000 switch(apppbx->e_state) {
1001 case EPOINT_STATE_IN_SETUP:
1002 response->am[num].u.e.state = ADMIN_STATE_IN_SETUP;
1004 case EPOINT_STATE_OUT_SETUP:
1005 response->am[num].u.e.state = ADMIN_STATE_OUT_SETUP;
1007 case EPOINT_STATE_IN_OVERLAP:
1008 response->am[num].u.e.state = ADMIN_STATE_IN_OVERLAP;
1010 case EPOINT_STATE_OUT_OVERLAP:
1011 response->am[num].u.e.state = ADMIN_STATE_OUT_OVERLAP;
1013 case EPOINT_STATE_IN_PROCEEDING:
1014 response->am[num].u.e.state = ADMIN_STATE_IN_PROCEEDING;
1016 case EPOINT_STATE_OUT_PROCEEDING:
1017 response->am[num].u.e.state = ADMIN_STATE_OUT_PROCEEDING;
1019 case EPOINT_STATE_IN_ALERTING:
1020 response->am[num].u.e.state = ADMIN_STATE_IN_ALERTING;
1022 case EPOINT_STATE_OUT_ALERTING:
1023 response->am[num].u.e.state = ADMIN_STATE_OUT_ALERTING;
1025 case EPOINT_STATE_CONNECT:
1026 response->am[num].u.e.state = ADMIN_STATE_CONNECT;
1028 case EPOINT_STATE_IN_DISCONNECT:
1029 response->am[num].u.e.state = ADMIN_STATE_IN_DISCONNECT;
1031 case EPOINT_STATE_OUT_DISCONNECT:
1032 response->am[num].u.e.state = ADMIN_STATE_OUT_DISCONNECT;
1035 response->am[num].u.e.state = ADMIN_STATE_IDLE;
1038 SCPY(response->am[num].u.e.terminal, apppbx->e_ext.number);
1040 SCPY(response->am[num].u.e.callerid, apppbx->e_callerinfo.id);
1042 SCPY(response->am[num].u.e.dialing, apppbx->e_dialinginfo.id);
1044 if (apppbx->e_action)
1045 SCPY(response->am[num].u.e.action, action_defs[apppbx->e_action->index].name);
1046 // if (apppbx->e_action)
1047 // printf("action=%s\n",action_defs[apppbx->e_action->index].name);
1049 response->am[num].u.e.park = apppbx->ea_endpoint->ep_park;
1050 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))
1051 memcpy(response->am[num].u.e.park_callid, apppbx->ea_endpoint->ep_park_callid, apppbx->ea_endpoint->ep_park_len);
1052 response->am[num].u.e.park_len = apppbx->ea_endpoint->ep_park_len;
1055 if (apppbx->e_crypt == CRYPT_ON)
1056 response->am[num].u.e.crypt = 1;
1059 apppbx = apppbx->next;
1063 /* create response for all ports */
1067 response->am[num].message = ADMIN_RESPONSE_S_PORT;
1069 response->am[num].u.p.serial = port->p_serial;
1071 SCPY(response->am[num].u.p.name, port->p_name);
1073 response->am[num].u.p.epoint = ACTIVE_EPOINT(port->p_epointlist);
1075 switch(port->p_state) {
1076 case PORT_STATE_IN_SETUP:
1077 response->am[num].u.p.state = ADMIN_STATE_IN_SETUP;
1079 case PORT_STATE_OUT_SETUP:
1080 response->am[num].u.p.state = ADMIN_STATE_OUT_SETUP;
1082 case PORT_STATE_IN_OVERLAP:
1083 response->am[num].u.p.state = ADMIN_STATE_IN_OVERLAP;
1085 case PORT_STATE_OUT_OVERLAP:
1086 response->am[num].u.p.state = ADMIN_STATE_OUT_OVERLAP;
1088 case PORT_STATE_IN_PROCEEDING:
1089 response->am[num].u.p.state = ADMIN_STATE_IN_PROCEEDING;
1091 case PORT_STATE_OUT_PROCEEDING:
1092 response->am[num].u.p.state = ADMIN_STATE_OUT_PROCEEDING;
1094 case PORT_STATE_IN_ALERTING:
1095 response->am[num].u.p.state = ADMIN_STATE_IN_ALERTING;
1097 case PORT_STATE_OUT_ALERTING:
1098 response->am[num].u.p.state = ADMIN_STATE_OUT_ALERTING;
1100 case PORT_STATE_CONNECT:
1101 response->am[num].u.p.state = ADMIN_STATE_CONNECT;
1103 case PORT_STATE_IN_DISCONNECT:
1104 response->am[num].u.p.state = ADMIN_STATE_IN_DISCONNECT;
1106 case PORT_STATE_OUT_DISCONNECT:
1107 response->am[num].u.p.state = ADMIN_STATE_OUT_DISCONNECT;
1109 case PORT_STATE_RELEASE:
1110 response->am[num].u.p.state = ADMIN_STATE_RELEASE;
1113 response->am[num].u.p.state = ADMIN_STATE_IDLE;
1117 if ((port->p_type & PORT_CLASS_mISDN_MASK) == PORT_CLASS_DSS1) {
1118 response->am[num].u.p.isdn = 1;
1119 pdss1 = (class Pdss1 *)port;
1120 response->am[num].u.p.isdn_chan = pdss1->p_m_b_channel;
1121 response->am[num].u.p.isdn_hold = pdss1->p_m_hold;
1122 response->am[num].u.p.isdn_ces = pdss1->p_m_d_ces;
1132 int sockserial = 1; // must start with 1, because 0 is used if no serial is set
1134 * handle admin socket (non blocking)
1136 int admin_handle_con(struct lcr_fd *fd, unsigned int what, void *instance, int index);
1138 int admin_handle(struct lcr_fd *fd, unsigned int what, void *instance, int index)
1141 socklen_t sock_len = sizeof(sock_address);
1142 struct admin_list *admin;
1144 /* check for new incoming connections */
1145 if ((new_sock = accept(sock, (struct sockaddr *)&sock_address, &sock_len)) >= 0) {
1146 /* insert new socket */
1147 admin = (struct admin_list *)MALLOC(sizeof(struct admin_list));
1150 admin->sockserial = sockserial++;
1151 admin->next = admin_first;
1152 admin_first = admin;
1153 admin->sock = new_sock;
1154 admin->fd.fd = new_sock;
1155 register_fd(&admin->fd, LCR_FD_READ | LCR_FD_EXCEPT, admin_handle_con, admin, 0);
1157 if (errno != EWOULDBLOCK) {
1158 PERROR("Failed to accept connection from socket \"%s\". (errno=%d) Closing socket.\n", sock_address.sun_path, errno);
1167 int admin_handle_con(struct lcr_fd *fd, unsigned int what, void *instance, int index)
1169 struct admin_list *admin = (struct admin_list *)instance;
1171 struct admin_message msg;
1173 struct Endpoint *epoint;
1175 if ((what & LCR_FD_READ)) {
1177 len = read(admin->sock, &msg, sizeof(msg));
1180 PDEBUG(DEBUG_LOG, "Broken pipe on socket %d. (errno=%d).\n", admin->sock, errno);
1182 /*release endpoint if exists */
1183 if (admin->epointid) {
1184 epoint = find_epoint_id(admin->epointid);
1185 if (epoint && epoint->ep_app_type == EAPP_TYPE_PBX) {
1186 ((class EndpointAppPBX *)epoint->ep_app)->
1187 release(RELEASE_ALL, LOCATION_PRIVATE_LOCAL, CAUSE_NORMAL, LOCATION_PRIVATE_LOCAL, CAUSE_NORMAL, 0);
1191 free_connection(admin);
1196 if (len != sizeof(msg)) {
1197 PERROR("Short/long read on socket %d. (len=%d != size=%d).\n", admin->sock, len, sizeof(msg));
1200 /* process socket command */
1201 if (admin->response && msg.message != ADMIN_MESSAGE) {
1202 PERROR("Data from socket %d while sending response.\n", admin->sock);
1205 switch (msg.message) {
1206 case ADMIN_REQUEST_CMD_INTERFACE:
1207 if (admin_interface(&admin->response) < 0) {
1208 PERROR("Failed to create dial response for socket %d.\n", admin->sock);
1209 goto response_error;
1211 admin->fd.when |= LCR_FD_WRITE;
1214 case ADMIN_REQUEST_CMD_ROUTE:
1215 if (admin_route(&admin->response) < 0) {
1216 PERROR("Failed to create dial response for socket %d.\n", admin->sock);
1217 goto response_error;
1219 admin->fd.when |= LCR_FD_WRITE;
1222 case ADMIN_REQUEST_CMD_DIAL:
1223 if (admin_dial(&admin->response, msg.u.x.message) < 0) {
1224 PERROR("Failed to create dial response for socket %d.\n", admin->sock);
1225 goto response_error;
1227 admin->fd.when |= LCR_FD_WRITE;
1230 case ADMIN_REQUEST_CMD_RELEASE:
1231 if (admin_release(&admin->response, msg.u.x.message) < 0) {
1232 PERROR("Failed to create release response for socket %d.\n", admin->sock);
1233 goto response_error;
1235 admin->fd.when |= LCR_FD_WRITE;
1238 case ADMIN_REQUEST_STATE:
1239 if (admin_state(&admin->response) < 0) {
1240 PERROR("Failed to create state response for socket %d.\n", admin->sock);
1241 goto response_error;
1243 admin->fd.when |= LCR_FD_WRITE;
1246 case ADMIN_TRACE_REQUEST:
1247 if (admin_trace(admin, &msg.u.trace_req) < 0) {
1248 PERROR("Failed to create trace response for socket %d.\n", admin->sock);
1249 goto response_error;
1251 admin->fd.when |= LCR_FD_WRITE;
1254 case ADMIN_REQUEST_CMD_BLOCK:
1255 if (admin_block(&admin->response, msg.u.x.portnum, msg.u.x.block) < 0) {
1256 PERROR("Failed to create block response for socket %d.\n", admin->sock);
1257 goto response_error;
1259 admin->fd.when |= LCR_FD_WRITE;
1263 if (admin_message_to_lcr(&msg.u.msg, admin) < 0) {
1264 PERROR("Failed to deliver message for socket %d.\n", admin->sock);
1265 goto response_error;
1269 case ADMIN_CALL_SETUP:
1270 if (admin_call(admin, &msg) < 0) {
1271 PERROR("Failed to create call for socket %d.\n", admin->sock);
1278 PERROR("Invalid message %d from socket %d.\n", msg.message, admin->sock);
1283 if ((what & LCR_FD_WRITE)) {
1285 if (admin->response) {
1286 len = write(admin->sock, ((unsigned char *)(admin->response->am))+admin->response->offset, sizeof(struct admin_message)*(admin->response->num)-admin->response->offset);
1292 if (len < (int)(sizeof(struct admin_message)*(admin->response->num) - admin->response->offset)) {
1293 admin->response->offset+=len;
1296 temp = admin->response;
1297 admin->response = admin->response->next;
1302 admin->fd.when &= ~LCR_FD_WRITE;