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);
79 * also releases all remote joins
81 void free_connection(struct admin_list *admin)
83 struct admin_queue *response;
85 union parameter param;
86 class Join *join, *joinnext;
87 struct mISDNport *mISDNport;
89 struct admin_list **adminp;
90 class Port *port, *portnext;
91 class Premote *remote;
93 /* free remote joins */
94 if (admin->remote_name[0]) {
102 "REMOTE APP release");
103 add_trace("app", "name", "%s", admin->remote_name);
105 /* release all exported channels */
106 mISDNport = mISDNport_first;
109 ii = mISDNport->b_num;
111 if (mISDNport->b_remote_id[i] == admin->sock) {
112 mISDNport->b_state[i] = B_STATE_IDLE;
113 unsched_timer(&mISDNport->b_timer[i]);
114 mISDNport->b_remote_id[i] = 0;
115 mISDNport->b_remote_ref[i] = 0;
119 mISDNport = mISDNport->next;
124 joinnext = join->next;
125 if (join->j_type==JOIN_TYPE_REMOTE) if (((class JoinRemote *)join)->j_remote_id == admin->sock) {
126 memset(¶m, 0, sizeof(param));
127 param.disconnectinfo.cause = CAUSE_OUTOFORDER;
128 param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
129 ((class JoinRemote *)join)->message_remote(MESSAGE_RELEASE, ¶m);
130 /* join is now destroyed, so we go to next join */
134 /* release remote port */
137 portnext = port->next;
138 if ((port->p_type & PORT_CLASS_MASK) == PORT_CLASS_REMOTE) {
139 remote = (class Premote *) port;
140 if (remote->p_m_r_remote_id == admin->sock) {
141 memset(¶m, 0, sizeof(param));
142 param.disconnectinfo.cause = CAUSE_OUTOFORDER;
143 param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
144 remote->message_remote(MESSAGE_RELEASE, ¶m);
145 /* port is now destroyed, so we go to next join */
152 if (admin->sock >= 0) {
153 unregister_fd(&admin->fd);
157 response = admin->response;
159 temp = response->next;
162 response = (struct admin_queue *)temp;
165 adminp = &admin_first;
167 if (*adminp == admin)
169 adminp = &((*adminp)->next);
172 *adminp = (*adminp)->next;
180 * cleanup admin socket
182 void admin_cleanup(void)
184 struct admin_list *admin, *next;;
189 free_connection(admin);
194 unregister_fd(&admin_fd);
204 * do interface reload
206 int admin_interface(struct admin_queue **responsep)
208 struct admin_queue *response; /* response pointer */
209 const char *err_txt = "";
212 if (read_interfaces()) {
214 free_interfaces(interface_first);
215 interface_first = interface_newlist;
216 interface_newlist = NULL;
218 err_txt = interface_error;
221 /* create state response */
222 response = (struct admin_queue *)MALLOC(sizeof(struct admin_queue)+sizeof(admin_message));
226 response->am[0].message = ADMIN_RESPONSE_CMD_INTERFACE;
228 response->am[0].u.x.error = err;
230 SCPY(response->am[0].u.x.message, err_txt);
231 /* attach to response chain */
232 *responsep = response;
233 responsep = &response->next;
241 int admin_route(struct admin_queue **responsep)
243 struct route_ruleset *ruleset_new;
244 struct admin_queue *response; /* response pointer */
245 char err_txt[256] = "";
250 class EndpointAppPBX *apppbx;
254 apppbx = apppbx_first;
257 apppbx = apppbx->next;
260 SPRINT(err_txt, "Cannot reload routing, because %d endpoints active\n", n);
265 if (!(ruleset_new = ruleset_parse())) {
266 SPRINT(err_txt, ruleset_error);
270 ruleset_free(ruleset_first);
271 ruleset_first = ruleset_new;
272 ruleset_main = getrulesetbyname("main");
274 SPRINT(err_txt, "Ruleset reloaded, but rule 'main' not found.\n");
277 apppbx = apppbx_first;
279 if (apppbx->e_action) {
280 switch(apppbx->e_action->index) {
281 case ACTION_INTERNAL:
282 apppbx->e_action = &action_internal;
284 case ACTION_EXTERNAL:
285 apppbx->e_action = &action_external;
288 apppbx->e_action = &action_remote;
290 case ACTION_VBOX_RECORD:
291 apppbx->e_action = &action_vbox;
293 case ACTION_PARTYLINE:
294 apppbx->e_action = &action_partyline;
299 } else if (apppbx->e_state != EPOINT_STATE_CONNECT) {
301 unsched_timer(&apppbx->e_callback_timeout);
302 apppbx->e_action = NULL;
303 apppbx->release(RELEASE_ALL, LOCATION_PRIVATE_LOCAL, CAUSE_NORMAL, LOCATION_PRIVATE_LOCAL, CAUSE_NORMAL, 0);
306 numberrize_callerinfo(apppbx->e_callerinfo.id, apppbx->e_callerinfo.ntype, options.national, options.international),
307 apppbx->e_dialinginfo.id,
310 apppbx->ea_endpoint->ep_serial,
311 "KICK (reload routing)");
315 unsched_timer(&apppbx->e_action_timeout);
316 apppbx->e_rule = NULL;
317 apppbx->e_ruleset = NULL;
319 apppbx = apppbx->next;
323 /* create state response */
324 response = (struct admin_queue *)MALLOC(sizeof(struct admin_queue)+sizeof(admin_message));
328 response->am[0].message = ADMIN_RESPONSE_CMD_ROUTE;
330 response->am[0].u.x.error = err;
332 SCPY(response->am[0].u.x.message, err_txt);
333 /* attach to response chain */
334 *responsep = response;
335 responsep = &response->next;
343 int admin_dial(struct admin_queue **responsep, char *message)
345 struct extension ext; /* temporary extension's settings */
346 struct admin_queue *response; /* response pointer */
347 char *p; /* pointer to dialing digits */
349 /* create state response */
350 response = (struct admin_queue *)MALLOC(sizeof(struct admin_queue)+sizeof(admin_message));
354 response->am[0].message = ADMIN_RESPONSE_CMD_DIAL;
356 /* process request */
357 if (!(p = strchr(message,':'))) {
358 response->am[0].u.x.error = -EINVAL;
359 SPRINT(response->am[0].u.x.message, "no seperator ':' in message to seperate number from extension");
364 /* modify extension */
365 if (!read_extension(&ext, message)) {
366 response->am[0].u.x.error = -EINVAL;
367 SPRINT(response->am[0].u.x.message, "extension doesn't exist");
371 write_extension(&ext, message);
374 /* attach to response chain */
375 *responsep = response;
376 responsep = &response->next;
384 int admin_trace(struct admin_list *admin, struct admin_trace_req *trace)
386 memcpy(&admin->trace, trace, sizeof(struct admin_trace_req));
394 * 0 = make port available
395 * 1 = make port administratively blocked
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
403 int admin_block(struct admin_queue **responsep, int portnum, int block)
405 struct admin_queue *response; /* response pointer */
406 struct interface *interface;
407 struct interface_port *ifport;
409 /* create block response */
410 response = (struct admin_queue *)MALLOC(sizeof(struct admin_queue)+sizeof(admin_message));
414 response->am[0].message = ADMIN_RESPONSE_CMD_BLOCK;
415 response->am[0].u.x.portnum = portnum;
417 /* search for port */
419 interface = interface_first;
421 ifport = interface->ifport;
423 if (ifport->portnum == portnum)
425 ifport = ifport->next;
429 interface = interface->next;
431 /* not found, we return -1 */
433 response->am[0].u.x.block = -1;
434 response->am[0].u.x.error = 1;
435 SPRINT(response->am[0].u.x.message, "Port %d does not exist.", portnum);
440 if (!ifport->mISDNport) {
441 /* not loaded anyway */
443 response->am[0].u.x.block = 2;
447 /* try loading interface */
448 ifport->block = block;
451 /* port cannot load */
452 if (ifport->block >= 2) {
453 response->am[0].u.x.block = 2;
454 response->am[0].u.x.error = 1;
455 SPRINT(response->am[0].u.x.message, "Port %d will not load.", portnum);
460 response->am[0].u.x.block = ifport->block;
464 /* if we shall unload interface */
466 mISDNport_close(ifport->mISDNport);
467 ifport->mISDNport = 0;
472 /* port new blocking state */
473 ifport->block = response->am[0].u.x.block = block;
476 /* attach to response chain */
477 *responsep = response;
478 responsep = &response->next;
486 int admin_release(struct admin_queue **responsep, char *message)
489 struct admin_queue *response; /* response pointer */
490 class EndpointAppPBX *apppbx;
492 /* create state response */
493 response = (struct admin_queue *)MALLOC(sizeof(struct admin_queue)+sizeof(admin_message));
497 response->am[0].message = ADMIN_RESPONSE_CMD_RELEASE;
500 apppbx = apppbx_first;
502 if (apppbx->ea_endpoint->ep_serial == id)
504 apppbx = apppbx->next;
507 response->am[0].u.x.error = -EINVAL;
508 SPRINT(response->am[0].u.x.message, "Given endpoint %d doesn't exist.", id);
512 unsched_timer(&apppbx->e_callback_timeout);
513 apppbx->release(RELEASE_ALL, LOCATION_PRIVATE_LOCAL, CAUSE_NORMAL, LOCATION_PRIVATE_LOCAL, CAUSE_NORMAL, 0);
516 /* attach to response chain */
517 *responsep = response;
518 responsep = &response->next;
526 int admin_call(struct admin_list *admin, struct admin_message *msg)
528 class Endpoint *epoint;
529 class EndpointAppPBX *apppbx;
531 if (!(epoint = new Endpoint(0, 0)))
532 FATAL("No memory for Endpoint instance\n");
533 if (!(epoint->ep_app = apppbx = new DEFAULT_ENDPOINT_APP(epoint, 1))) // outgoing
534 FATAL("No memory for Endpoint Application instance\n");
535 apppbx->e_adminid = admin->sockserial;
536 admin->epointid = epoint->ep_serial;
537 SCPY(apppbx->e_callerinfo.id, nationalize_callerinfo(msg->u.call.callerid, &apppbx->e_callerinfo.ntype, options.national, options.international));
538 if (msg->u.call.present)
539 apppbx->e_callerinfo.present = INFO_PRESENT_ALLOWED;
541 apppbx->e_callerinfo.present = INFO_PRESENT_RESTRICTED;
542 apppbx->e_callerinfo.screen = INFO_SCREEN_NETWORK;
545 apppbx->e_capainfo.bearer_capa = msg->u.call.bc_capa;
546 apppbx->e_capainfo.bearer_mode = msg->u.call.bc_mode;
547 apppbx->e_capainfo.bearer_info1 = msg->u.call.bc_info1;
548 apppbx->e_capainfo.hlc = msg->u.call.hlc;
549 apppbx->e_capainfo.exthlc = msg->u.call.exthlc;
550 SCPY(apppbx->e_dialinginfo.id, msg->u.call.dialing);
551 SCPY(apppbx->e_dialinginfo.interfaces, msg->u.call.interface);
552 apppbx->e_dialinginfo.sending_complete = 1;
554 apppbx->new_state(PORT_STATE_OUT_SETUP);
555 apppbx->out_setup(0);
561 * this function is called for response whenever a call state changes.
563 void admin_call_response(int adminid, int message, const char *connected, int cause, int location, int notify_progress)
565 struct admin_list *admin;
566 struct admin_queue *response, **responsep; /* response pointer */
568 /* searching for admin id
569 * maybe there is no admin instance, because the calling port was not
570 * initiated by admin_call */
573 if (adminid == admin->sockserial)
580 /* seek to end of response list */
581 response = admin->response;
582 responsep = &admin->response;
584 responsep = &response->next;
585 response = response->next;
588 /* create state response */
589 response = (struct admin_queue *)MALLOC(sizeof(struct admin_queue)+sizeof(admin_message));
593 response->am[0].message = message;
595 SCPY(response->am[0].u.call.callerid, connected);
596 response->am[0].u.call.cause = cause;
597 response->am[0].u.call.location = location;
598 response->am[0].u.call.notify_progress = notify_progress;
600 /* attach to response chain */
601 *responsep = response;
602 responsep = &response->next;
603 admin->fd.when |= LCR_FD_WRITE;
608 * send data to the remote socket join instance
610 int admin_message_to_lcr(struct admin_msg *msg, struct admin_list *admin)
612 struct mISDNport *mISDNport;
614 class JoinRemote *joinremote = NULL; /* make GCC happy */
616 class Premote *remote = NULL; /* make GCC happy */
617 struct admin_list *temp;
620 if (msg->type == MESSAGE_HELLO) {
621 if (admin->remote_name[0]) {
622 PERROR("Remote application repeats hello message.\n");
625 /* look for second application */
628 if (!strcmp(temp->remote_name, msg->param.hello.application))
633 PERROR("Remote application connects twice??? (ignoring)\n");
636 /* set remote socket instance */
637 SCPY(admin->remote_name, msg->param.hello.application);
645 "REMOTE APP registers");
646 add_trace("app", "name", "%s", admin->remote_name);
651 /* check we have no application name */
652 if (!admin->remote_name[0]) {
653 PERROR("Remote application did not send us a hello message.\n");
657 /* new join. the reply (NEWREF assignment) is sent from constructor */
658 if (msg->type == MESSAGE_NEWREF) {
659 if (msg->param.newref.mode) {
661 /* find remote port */
662 mISDNport = mISDNport_first;
664 if (mISDNport->ifport->remote && !strcmp(mISDNport->ifport->remote_app, admin->remote_name))
666 mISDNport = mISDNport->next;
669 union parameter param;
671 /* create new join instance */
672 join = joinremote = new JoinRemote(0, admin->remote_name, admin->sock); // must have no serial, because no endpoint is connected
674 FATAL("No memory for remote join instance\n");
677 memset(¶m, 0, sizeof(union parameter));
678 param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
679 param.disconnectinfo.cause = CAUSE_RESSOURCEUNAVAIL;
680 admin_message_from_lcr(joinremote->j_remote_id, joinremote->j_remote_ref, MESSAGE_RELEASE, ¶m);
683 /* creating port object, transparent until setup with hdlc */
684 SPRINT(name, "%s-%s-in", mISDNport->ifport->interface->name, mISDNport->ifport->remote_app);
685 if (!(remote = new Premote(PORT_TYPE_REMOTE_IN, mISDNport, name, NULL, 0, 0, B_MODE_TRANSPARENT, admin->sock)))
687 FATAL("Cannot create Port instance.\n");
689 /* create new join instance */
690 join = new JoinRemote(0, admin->remote_name, admin->sock); // must have no serial, because no endpoint is connected
692 FATAL("No memory for remote join instance\n");
700 * no ref given for *_ack */
701 if (msg->type == MESSAGE_BCHANNEL)
702 if (msg->param.bchannel.type == BCHANNEL_ASSIGN_ACK
703 || msg->param.bchannel.type == BCHANNEL_REMOVE_ACK
704 || msg->param.bchannel.type == BCHANNEL_RELEASE) {
705 /* no ref, but address */
706 message_bchannel_from_remote(NULL, msg->param.bchannel.type, msg->param.bchannel.handle);
712 PERROR("Remote application did not send us a valid ref with a message.\n");
716 /* find join instance */
719 if (join->j_type == JOIN_TYPE_REMOTE) {
720 joinremote = (class JoinRemote *)join;
721 if (joinremote->j_remote_ref == msg->ref)
727 if (admin->sock != joinremote->j_remote_id) {
728 PERROR("Ref %d belongs to remote application %s, but not to sending application %s.\n", msg->ref, joinremote->j_remote_name, admin->remote_name);
732 joinremote->message_remote(msg->type, &msg->param);
737 /* find port instance */
740 if ((port->p_type & PORT_CLASS_mISDN_MASK) == PORT_CLASS_REMOTE) {
741 remote = (class Premote *) port;
742 if (remote->p_m_r_ref == msg->ref)
748 if (admin->sock != remote->p_m_r_remote_id) {
749 PERROR("Ref %d belongs to remote application %s, but not to sending application %s.\n", msg->ref, remote->p_m_r_remote_app, admin->remote_name);
754 remote->message_remote(msg->type, &msg->param);
759 PDEBUG(DEBUG_LOG, "No remote instance found with ref %d. (May have been already released.)\n", msg->ref);
766 * this function is called for every message to remote socket
768 int admin_message_from_lcr(int remote_id, unsigned int ref, int message_type, union parameter *param)
770 struct admin_list *admin;
771 struct admin_queue **responsep; /* response pointer */
773 /* searching for admin id
774 * maybe there is no given remote application
778 if (admin->remote_name[0] && admin->sock==remote_id)
782 /* no given remote application connected */
786 /* seek to end of response list */
787 responsep = &admin->response;
789 responsep = &(*responsep)->next;
792 /* create state response */
793 *responsep = (struct admin_queue *)MALLOC(sizeof(struct admin_queue)+sizeof(admin_message));
795 (*responsep)->num = 1;
798 (*responsep)->am[0].message = ADMIN_MESSAGE;
799 (*responsep)->am[0].u.msg.type = message_type;
800 (*responsep)->am[0].u.msg.ref = ref;
801 memcpy(&(*responsep)->am[0].u.msg.param, param, sizeof(union parameter));
802 admin->fd.when |= LCR_FD_WRITE;
810 int admin_state(struct admin_queue **responsep)
813 class EndpointAppPBX *apppbx;
816 struct interface *interface;
817 struct interface_port *ifport;
818 struct mISDNport *mISDNport;
819 struct select_channel *selchannel;
823 struct admin_queue *response;
824 struct admin_list *admin;
828 /* create state response */
829 response = (struct admin_queue *)MALLOC(sizeof(struct admin_queue)+sizeof(admin_message));
833 response->am[0].message = ADMIN_RESPONSE_STATE;
835 SCPY(response->am[0].u.s.version_string, VERSION_STRING);
838 now_tm = localtime(&now);
839 memcpy(&response->am[0].u.s.tm, now_tm, sizeof(struct tm));
841 SCPY(response->am[0].u.s.logfile, options.log);
842 /* interface count */
844 interface = interface_first;
846 ifport = interface->ifport;
849 ifport = ifport->next;
851 interface = interface->next;
853 response->am[0].u.s.interfaces = i;
854 /* remote connection count */
858 if (admin->remote_name[0])
862 response->am[0].u.s.remotes = i;
870 response->am[0].u.s.joins = i;
872 apppbx = apppbx_first;
876 apppbx = apppbx->next;
878 response->am[0].u.s.epoints = i;
886 response->am[0].u.s.ports = i;
887 /* attach to response chain */
888 *responsep = response;
889 responsep = &response->next;
891 /* create response for all instances */
892 num = (response->am[0].u.s.interfaces)
893 + (response->am[0].u.s.remotes)
894 + (response->am[0].u.s.joins)
895 + (response->am[0].u.s.epoints)
896 + (response->am[0].u.s.ports);
899 response = (struct admin_queue *)MALLOC(sizeof(admin_queue)+(num*sizeof(admin_message)));
902 *responsep = response;
903 responsep = &response->next;
904 interface = interface_first;
907 ifport = interface->ifport;
910 response->am[num].message = ADMIN_RESPONSE_S_INTERFACE;
912 SCPY(response->am[num].u.i.interface_name, interface->name);
914 response->am[num].u.i.portnum = ifport->portnum;
916 SCPY(response->am[num].u.i.portname, ifport->portname);
918 response->am[num].u.i.extension = interface->extension;
920 response->am[num].u.i.block = ifport->block;
921 if (ifport->mISDNport) {
922 mISDNport = ifport->mISDNport;
925 response->am[num].u.i.ptp = mISDNport->ptp;
927 response->am[num].u.i.l1hold = mISDNport->l1hold;
929 response->am[num].u.i.l2hold = mISDNport->l2hold;
931 response->am[num].u.i.ntmode = mISDNport->ntmode;
933 response->am[num].u.i.pri = mISDNport->pri;
935 response->am[num].u.i.use = mISDNport->use;
937 response->am[num].u.i.l1link = mISDNport->l1link;
939 response->am[num].u.i.l2link = mISDNport->l2link;
940 memcpy(response->am[num].u.i.l2mask, mISDNport->l2mask, 16);
942 response->am[num].u.i.los = mISDNport->los;
944 response->am[num].u.i.ais = mISDNport->ais;
946 response->am[num].u.i.rdi = mISDNport->rdi;
948 response->am[num].u.i.slip_tx = mISDNport->slip_tx;
949 response->am[num].u.i.slip_rx = mISDNport->slip_rx;
951 response->am[num].u.i.channels = mISDNport->b_num;
952 /* channel selection */
953 selchannel = ifport->out_channel;
954 if (ifport->channel_force)
955 SCAT(response->am[num].u.i.out_channel, "force");
957 if (response->am[num].u.i.out_channel[0])
958 SCAT(response->am[num].u.i.out_channel, ",");
959 switch (selchannel->channel) {
961 SCAT(response->am[num].u.i.out_channel, "no");
964 SCAT(response->am[num].u.i.out_channel, "any");
967 SCAT(response->am[num].u.i.out_channel, "free");
970 SPRINT(strchr(response->am[num].u.i.out_channel, '\0'), "%d", selchannel->channel);
972 selchannel = selchannel->next;
974 selchannel = ifport->in_channel;
976 switch (selchannel->channel) {
978 SCAT(response->am[num].u.i.in_channel, "free");
981 SPRINT(strchr(response->am[num].u.i.in_channel, '\0'), "%d", selchannel->channel);
983 selchannel = selchannel->next;
988 while(i < mISDNport->b_num) {
989 response->am[num].u.i.busy[i] = mISDNport->b_state[i];
990 if (mISDNport->b_port[i])
991 response->am[num].u.i.port[i] = mISDNport->b_port[i]->p_serial;
992 response->am[num].u.i.mode[i] = mISDNport->b_mode[i];
998 ifport = ifport->next;
1000 interface = interface->next;
1003 /* create response for all remotes */
1004 admin = admin_first;
1006 if (admin->remote_name[0]) {
1008 response->am[num].message = ADMIN_RESPONSE_S_REMOTE;
1010 SCPY(response->am[num].u.r.name, admin->remote_name);
1014 admin = admin->next;
1017 /* create response for all joins */
1021 response->am[num].message = ADMIN_RESPONSE_S_JOIN;
1023 response->am[num].u.j.serial = join->j_serial;
1025 if (join->j_type == JOIN_TYPE_PBX)
1026 response->am[num].u.j.partyline = ((class JoinPBX *)join)->j_partyline;
1027 /* remote application */
1028 if (join->j_type == JOIN_TYPE_REMOTE)
1029 SCPY(response->am[num].u.j.remote, ((class JoinRemote *)join)->j_remote_name);
1035 /* create response for all endpoint */
1036 apppbx = apppbx_first;
1039 response->am[num].message = ADMIN_RESPONSE_S_EPOINT;
1041 response->am[num].u.e.serial = apppbx->ea_endpoint->ep_serial;
1043 response->am[num].u.e.join = apppbx->ea_endpoint->ep_join_id;
1044 /* rx notification */
1045 response->am[num].u.e.rx_state = apppbx->e_rx_state;
1046 /* tx notification */
1047 response->am[num].u.e.tx_state = apppbx->e_tx_state;
1049 switch(apppbx->e_state) {
1050 case EPOINT_STATE_IN_SETUP:
1051 response->am[num].u.e.state = ADMIN_STATE_IN_SETUP;
1053 case EPOINT_STATE_OUT_SETUP:
1054 response->am[num].u.e.state = ADMIN_STATE_OUT_SETUP;
1056 case EPOINT_STATE_IN_OVERLAP:
1057 response->am[num].u.e.state = ADMIN_STATE_IN_OVERLAP;
1059 case EPOINT_STATE_OUT_OVERLAP:
1060 response->am[num].u.e.state = ADMIN_STATE_OUT_OVERLAP;
1062 case EPOINT_STATE_IN_PROCEEDING:
1063 response->am[num].u.e.state = ADMIN_STATE_IN_PROCEEDING;
1065 case EPOINT_STATE_OUT_PROCEEDING:
1066 response->am[num].u.e.state = ADMIN_STATE_OUT_PROCEEDING;
1068 case EPOINT_STATE_IN_ALERTING:
1069 response->am[num].u.e.state = ADMIN_STATE_IN_ALERTING;
1071 case EPOINT_STATE_OUT_ALERTING:
1072 response->am[num].u.e.state = ADMIN_STATE_OUT_ALERTING;
1074 case EPOINT_STATE_CONNECT:
1075 response->am[num].u.e.state = ADMIN_STATE_CONNECT;
1077 case EPOINT_STATE_IN_DISCONNECT:
1078 response->am[num].u.e.state = ADMIN_STATE_IN_DISCONNECT;
1080 case EPOINT_STATE_OUT_DISCONNECT:
1081 response->am[num].u.e.state = ADMIN_STATE_OUT_DISCONNECT;
1084 response->am[num].u.e.state = ADMIN_STATE_IDLE;
1087 SCPY(response->am[num].u.e.terminal, apppbx->e_ext.number);
1089 SCPY(response->am[num].u.e.callerid, apppbx->e_callerinfo.id);
1091 SCPY(response->am[num].u.e.dialing, apppbx->e_dialinginfo.id);
1093 if (apppbx->e_action)
1094 SCPY(response->am[num].u.e.action, action_defs[apppbx->e_action->index].name);
1095 // if (apppbx->e_action)
1096 // printf("action=%s\n",action_defs[apppbx->e_action->index].name);
1098 response->am[num].u.e.park = apppbx->ea_endpoint->ep_park;
1099 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))
1100 memcpy(response->am[num].u.e.park_callid, apppbx->ea_endpoint->ep_park_callid, apppbx->ea_endpoint->ep_park_len);
1101 response->am[num].u.e.park_len = apppbx->ea_endpoint->ep_park_len;
1103 if (apppbx->e_crypt == CRYPT_ON)
1104 response->am[num].u.e.crypt = 1;
1106 apppbx = apppbx->next;
1110 /* create response for all ports */
1114 response->am[num].message = ADMIN_RESPONSE_S_PORT;
1116 response->am[num].u.p.serial = port->p_serial;
1118 SCPY(response->am[num].u.p.name, port->p_name);
1120 response->am[num].u.p.epoint = ACTIVE_EPOINT(port->p_epointlist);
1122 switch(port->p_state) {
1123 case PORT_STATE_IN_SETUP:
1124 response->am[num].u.p.state = ADMIN_STATE_IN_SETUP;
1126 case PORT_STATE_OUT_SETUP:
1127 response->am[num].u.p.state = ADMIN_STATE_OUT_SETUP;
1129 case PORT_STATE_IN_OVERLAP:
1130 response->am[num].u.p.state = ADMIN_STATE_IN_OVERLAP;
1132 case PORT_STATE_OUT_OVERLAP:
1133 response->am[num].u.p.state = ADMIN_STATE_OUT_OVERLAP;
1135 case PORT_STATE_IN_PROCEEDING:
1136 response->am[num].u.p.state = ADMIN_STATE_IN_PROCEEDING;
1138 case PORT_STATE_OUT_PROCEEDING:
1139 response->am[num].u.p.state = ADMIN_STATE_OUT_PROCEEDING;
1141 case PORT_STATE_IN_ALERTING:
1142 response->am[num].u.p.state = ADMIN_STATE_IN_ALERTING;
1144 case PORT_STATE_OUT_ALERTING:
1145 response->am[num].u.p.state = ADMIN_STATE_OUT_ALERTING;
1147 case PORT_STATE_CONNECT:
1148 response->am[num].u.p.state = ADMIN_STATE_CONNECT;
1150 case PORT_STATE_IN_DISCONNECT:
1151 response->am[num].u.p.state = ADMIN_STATE_IN_DISCONNECT;
1153 case PORT_STATE_OUT_DISCONNECT:
1154 response->am[num].u.p.state = ADMIN_STATE_OUT_DISCONNECT;
1156 case PORT_STATE_RELEASE:
1157 response->am[num].u.p.state = ADMIN_STATE_RELEASE;
1160 response->am[num].u.p.state = ADMIN_STATE_IDLE;
1163 if ((port->p_type & PORT_CLASS_mISDN_MASK) == PORT_CLASS_DSS1) {
1164 response->am[num].u.p.isdn = 1;
1165 pdss1 = (class Pdss1 *)port;
1166 response->am[num].u.p.isdn_chan = pdss1->p_m_b_channel;
1167 response->am[num].u.p.isdn_hold = pdss1->p_m_hold;
1168 response->am[num].u.p.isdn_ces = pdss1->p_m_d_ces;
1177 int sockserial = 1; // must start with 1, because 0 is used if no serial is set
1179 * handle admin socket (non blocking)
1181 int admin_handle_con(struct lcr_fd *fd, unsigned int what, void *instance, int index);
1183 int admin_handle(struct lcr_fd *fd, unsigned int what, void *instance, int index)
1186 socklen_t sock_len = sizeof(sock_address);
1187 struct admin_list *admin;
1189 /* check for new incoming connections */
1190 if ((new_sock = accept(sock, (struct sockaddr *)&sock_address, &sock_len)) >= 0) {
1191 /* insert new socket */
1192 admin = (struct admin_list *)MALLOC(sizeof(struct admin_list));
1195 admin->sockserial = sockserial++;
1196 admin->next = admin_first;
1197 admin_first = admin;
1198 admin->sock = new_sock;
1199 admin->fd.fd = new_sock;
1200 register_fd(&admin->fd, LCR_FD_READ | LCR_FD_EXCEPT, admin_handle_con, admin, 0);
1202 if (errno != EWOULDBLOCK) {
1203 PERROR("Failed to accept connection from socket \"%s\". (errno=%d) Closing socket.\n", sock_address.sun_path, errno);
1212 int admin_handle_con(struct lcr_fd *fd, unsigned int what, void *instance, int index)
1214 struct admin_list *admin = (struct admin_list *)instance;
1216 struct admin_message msg;
1218 struct Endpoint *epoint;
1220 if ((what & LCR_FD_READ)) {
1222 len = read(admin->sock, &msg, sizeof(msg));
1225 PDEBUG(DEBUG_LOG, "Broken pipe on socket %d. (errno=%d).\n", admin->sock, errno);
1226 free_connection(admin);
1232 /*release endpoint if exists */
1233 if (admin->epointid) {
1234 epoint = find_epoint_id(admin->epointid);
1236 ((class DEFAULT_ENDPOINT_APP *)epoint->ep_app)->
1237 release(RELEASE_ALL, LOCATION_PRIVATE_LOCAL, CAUSE_NORMAL, LOCATION_PRIVATE_LOCAL, CAUSE_NORMAL, 0);
1241 free_connection(admin);
1244 if (len != sizeof(msg)) {
1245 PERROR("Short/long read on socket %d. (len=%d != size=%d).\n", admin->sock, len, sizeof(msg));
1246 free_connection(admin);
1249 /* process socket command */
1250 if (admin->response && msg.message != ADMIN_MESSAGE) {
1251 PERROR("Data from socket %d while sending response.\n", admin->sock);
1252 free_connection(admin);
1255 switch (msg.message) {
1256 case ADMIN_REQUEST_CMD_INTERFACE:
1257 if (admin_interface(&admin->response) < 0) {
1258 PERROR("Failed to create dial response for socket %d.\n", admin->sock);
1259 goto response_error;
1261 admin->fd.when |= LCR_FD_WRITE;
1264 case ADMIN_REQUEST_CMD_ROUTE:
1265 if (admin_route(&admin->response) < 0) {
1266 PERROR("Failed to create dial response for socket %d.\n", admin->sock);
1267 goto response_error;
1269 admin->fd.when |= LCR_FD_WRITE;
1272 case ADMIN_REQUEST_CMD_DIAL:
1273 if (admin_dial(&admin->response, msg.u.x.message) < 0) {
1274 PERROR("Failed to create dial response for socket %d.\n", admin->sock);
1275 goto response_error;
1277 admin->fd.when |= LCR_FD_WRITE;
1280 case ADMIN_REQUEST_CMD_RELEASE:
1281 if (admin_release(&admin->response, msg.u.x.message) < 0) {
1282 PERROR("Failed to create release response for socket %d.\n", admin->sock);
1283 goto response_error;
1285 admin->fd.when |= LCR_FD_WRITE;
1288 case ADMIN_REQUEST_STATE:
1289 if (admin_state(&admin->response) < 0) {
1290 PERROR("Failed to create state response for socket %d.\n", admin->sock);
1291 goto response_error;
1293 admin->fd.when |= LCR_FD_WRITE;
1296 case ADMIN_TRACE_REQUEST:
1297 if (admin_trace(admin, &msg.u.trace_req) < 0) {
1298 PERROR("Failed to create trace response for socket %d.\n", admin->sock);
1299 goto response_error;
1301 admin->fd.when |= LCR_FD_WRITE;
1304 case ADMIN_REQUEST_CMD_BLOCK:
1305 if (admin_block(&admin->response, msg.u.x.portnum, msg.u.x.block) < 0) {
1306 PERROR("Failed to create block response for socket %d.\n", admin->sock);
1307 goto response_error;
1309 admin->fd.when |= LCR_FD_WRITE;
1313 if (admin_message_to_lcr(&msg.u.msg, admin) < 0) {
1314 PERROR("Failed to deliver message for socket %d.\n", admin->sock);
1315 goto response_error;
1319 case ADMIN_CALL_SETUP:
1320 if (admin_call(admin, &msg) < 0) {
1321 PERROR("Failed to create call for socket %d.\n", admin->sock);
1323 free_connection(admin);
1329 PERROR("Invalid message %d from socket %d.\n", msg.message, admin->sock);
1330 free_connection(admin);
1335 if ((what & LCR_FD_WRITE)) {
1337 if (admin->response) {
1338 len = write(admin->sock, ((unsigned char *)(admin->response->am))+admin->response->offset, sizeof(struct admin_message)*(admin->response->num)-admin->response->offset);
1344 if (len < (int)(sizeof(struct admin_message)*(admin->response->num) - admin->response->offset)) {
1345 admin->response->offset+=len;
1348 temp = admin->response;
1349 admin->response = admin->response->next;
1354 admin->fd.when &= ~LCR_FD_WRITE;
1360 void message_bchannel_to_remote(unsigned int remote_id, unsigned int ref, int type, unsigned int handle, int tx_gain, int rx_gain, char *pipeline, unsigned char *crypt, int crypt_len, int crypt_type, int isloopback)
1362 union parameter param;
1364 memset(¶m, 0, sizeof(union parameter));
1365 param.bchannel.isloopback = isloopback;
1366 param.bchannel.type = type;
1367 param.bchannel.handle = handle;
1368 param.bchannel.tx_gain = tx_gain;
1369 param.bchannel.rx_gain = rx_gain;
1371 SCPY(param.bchannel.pipeline, pipeline);
1373 memcpy(param.bchannel.crypt, crypt, crypt_len);
1374 param.bchannel.crypt_type = crypt_type;
1375 if (admin_message_from_lcr(remote_id, ref, MESSAGE_BCHANNEL, ¶m)<0) {
1376 PERROR("No socket with remote id %d found, this happens, if the socket is closed before all bchannels are imported.\n", remote_id);