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 EndpointAppPBX(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;
851 ifport = ifport->next;
853 interface = interface->next;
855 response->am[0].u.s.interfaces = i;
856 /* remote connection count */
860 if (admin->remote_name[0])
864 response->am[0].u.s.remotes = i;
872 response->am[0].u.s.joins = i;
874 apppbx = apppbx_first;
878 apppbx = apppbx->next;
880 response->am[0].u.s.epoints = i;
888 response->am[0].u.s.ports = i;
889 /* attach to response chain */
890 *responsep = response;
891 responsep = &response->next;
893 /* create response for all instances */
894 num = (response->am[0].u.s.interfaces)
895 + (response->am[0].u.s.remotes)
896 + (response->am[0].u.s.joins)
897 + (response->am[0].u.s.epoints)
898 + (response->am[0].u.s.ports);
901 response = (struct admin_queue *)MALLOC(sizeof(admin_queue)+(num*sizeof(admin_message)));
904 *responsep = response;
905 responsep = &response->next;
906 interface = interface_first;
909 ifport = interface->ifport;
912 response->am[num].message = ADMIN_RESPONSE_S_INTERFACE;
914 SCPY(response->am[num].u.i.interface_name, interface->name);
916 response->am[num].u.i.portnum = -100; /* indicate: no ifport */
918 response->am[num].u.i.extension = interface->extension;
924 response->am[num].message = ADMIN_RESPONSE_S_INTERFACE;
926 SCPY(response->am[num].u.i.interface_name, interface->name);
928 response->am[num].u.i.portnum = ifport->portnum;
930 SCPY(response->am[num].u.i.portname, ifport->portname);
932 response->am[num].u.i.extension = interface->extension;
934 response->am[num].u.i.block = ifport->block;
935 if (ifport->mISDNport) {
936 mISDNport = ifport->mISDNport;
939 response->am[num].u.i.ptp = mISDNport->ptp;
941 response->am[num].u.i.l1hold = mISDNport->l1hold;
943 response->am[num].u.i.l2hold = mISDNport->l2hold;
945 response->am[num].u.i.ntmode = mISDNport->ntmode;
947 response->am[num].u.i.pri = mISDNport->pri;
949 response->am[num].u.i.use = mISDNport->use;
951 response->am[num].u.i.l1link = mISDNport->l1link;
953 response->am[num].u.i.l2link = mISDNport->l2link;
954 memcpy(response->am[num].u.i.l2mask, mISDNport->l2mask, 16);
956 response->am[num].u.i.los = mISDNport->los;
958 response->am[num].u.i.ais = mISDNport->ais;
960 response->am[num].u.i.rdi = mISDNport->rdi;
962 response->am[num].u.i.slip_tx = mISDNport->slip_tx;
963 response->am[num].u.i.slip_rx = mISDNport->slip_rx;
965 response->am[num].u.i.channels = mISDNport->b_num;
966 /* channel selection */
967 selchannel = ifport->out_channel;
968 if (ifport->channel_force)
969 SCAT(response->am[num].u.i.out_channel, "force");
971 if (response->am[num].u.i.out_channel[0])
972 SCAT(response->am[num].u.i.out_channel, ",");
973 switch (selchannel->channel) {
975 SCAT(response->am[num].u.i.out_channel, "no");
978 SCAT(response->am[num].u.i.out_channel, "any");
981 SCAT(response->am[num].u.i.out_channel, "free");
984 SPRINT(strchr(response->am[num].u.i.out_channel, '\0'), "%d", selchannel->channel);
986 selchannel = selchannel->next;
988 selchannel = ifport->in_channel;
990 switch (selchannel->channel) {
992 SCAT(response->am[num].u.i.in_channel, "free");
995 SPRINT(strchr(response->am[num].u.i.in_channel, '\0'), "%d", selchannel->channel);
997 selchannel = selchannel->next;
1002 while(i < mISDNport->b_num) {
1003 response->am[num].u.i.busy[i] = mISDNport->b_state[i];
1004 if (mISDNport->b_port[i])
1005 response->am[num].u.i.port[i] = mISDNport->b_port[i]->p_serial;
1006 response->am[num].u.i.mode[i] = mISDNport->b_mode[i];
1012 ifport = ifport->next;
1014 interface = interface->next;
1017 /* create response for all remotes */
1018 admin = admin_first;
1020 if (admin->remote_name[0]) {
1022 response->am[num].message = ADMIN_RESPONSE_S_REMOTE;
1024 SCPY(response->am[num].u.r.name, admin->remote_name);
1028 admin = admin->next;
1031 /* create response for all joins */
1035 response->am[num].message = ADMIN_RESPONSE_S_JOIN;
1037 response->am[num].u.j.serial = join->j_serial;
1039 if (join->j_type == JOIN_TYPE_PBX)
1040 response->am[num].u.j.partyline = ((class JoinPBX *)join)->j_partyline;
1041 /* remote application */
1042 if (join->j_type == JOIN_TYPE_REMOTE)
1043 SCPY(response->am[num].u.j.remote, ((class JoinRemote *)join)->j_remote_name);
1049 /* create response for all endpoint */
1050 apppbx = apppbx_first;
1053 response->am[num].message = ADMIN_RESPONSE_S_EPOINT;
1055 response->am[num].u.e.serial = apppbx->ea_endpoint->ep_serial;
1057 response->am[num].u.e.join = apppbx->ea_endpoint->ep_join_id;
1058 /* rx notification */
1059 response->am[num].u.e.rx_state = apppbx->e_rx_state;
1060 /* tx notification */
1061 response->am[num].u.e.tx_state = apppbx->e_tx_state;
1063 switch(apppbx->e_state) {
1064 case EPOINT_STATE_IN_SETUP:
1065 response->am[num].u.e.state = ADMIN_STATE_IN_SETUP;
1067 case EPOINT_STATE_OUT_SETUP:
1068 response->am[num].u.e.state = ADMIN_STATE_OUT_SETUP;
1070 case EPOINT_STATE_IN_OVERLAP:
1071 response->am[num].u.e.state = ADMIN_STATE_IN_OVERLAP;
1073 case EPOINT_STATE_OUT_OVERLAP:
1074 response->am[num].u.e.state = ADMIN_STATE_OUT_OVERLAP;
1076 case EPOINT_STATE_IN_PROCEEDING:
1077 response->am[num].u.e.state = ADMIN_STATE_IN_PROCEEDING;
1079 case EPOINT_STATE_OUT_PROCEEDING:
1080 response->am[num].u.e.state = ADMIN_STATE_OUT_PROCEEDING;
1082 case EPOINT_STATE_IN_ALERTING:
1083 response->am[num].u.e.state = ADMIN_STATE_IN_ALERTING;
1085 case EPOINT_STATE_OUT_ALERTING:
1086 response->am[num].u.e.state = ADMIN_STATE_OUT_ALERTING;
1088 case EPOINT_STATE_CONNECT:
1089 response->am[num].u.e.state = ADMIN_STATE_CONNECT;
1091 case EPOINT_STATE_IN_DISCONNECT:
1092 response->am[num].u.e.state = ADMIN_STATE_IN_DISCONNECT;
1094 case EPOINT_STATE_OUT_DISCONNECT:
1095 response->am[num].u.e.state = ADMIN_STATE_OUT_DISCONNECT;
1098 response->am[num].u.e.state = ADMIN_STATE_IDLE;
1101 SCPY(response->am[num].u.e.terminal, apppbx->e_ext.number);
1103 SCPY(response->am[num].u.e.callerid, apppbx->e_callerinfo.id);
1105 SCPY(response->am[num].u.e.dialing, apppbx->e_dialinginfo.id);
1107 if (apppbx->e_action)
1108 SCPY(response->am[num].u.e.action, action_defs[apppbx->e_action->index].name);
1109 // if (apppbx->e_action)
1110 // printf("action=%s\n",action_defs[apppbx->e_action->index].name);
1112 response->am[num].u.e.park = apppbx->ea_endpoint->ep_park;
1113 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))
1114 memcpy(response->am[num].u.e.park_callid, apppbx->ea_endpoint->ep_park_callid, apppbx->ea_endpoint->ep_park_len);
1115 response->am[num].u.e.park_len = apppbx->ea_endpoint->ep_park_len;
1117 if (apppbx->e_crypt == CRYPT_ON)
1118 response->am[num].u.e.crypt = 1;
1120 apppbx = apppbx->next;
1124 /* create response for all ports */
1128 response->am[num].message = ADMIN_RESPONSE_S_PORT;
1130 response->am[num].u.p.serial = port->p_serial;
1132 SCPY(response->am[num].u.p.name, port->p_name);
1134 response->am[num].u.p.epoint = ACTIVE_EPOINT(port->p_epointlist);
1136 switch(port->p_state) {
1137 case PORT_STATE_IN_SETUP:
1138 response->am[num].u.p.state = ADMIN_STATE_IN_SETUP;
1140 case PORT_STATE_OUT_SETUP:
1141 response->am[num].u.p.state = ADMIN_STATE_OUT_SETUP;
1143 case PORT_STATE_IN_OVERLAP:
1144 response->am[num].u.p.state = ADMIN_STATE_IN_OVERLAP;
1146 case PORT_STATE_OUT_OVERLAP:
1147 response->am[num].u.p.state = ADMIN_STATE_OUT_OVERLAP;
1149 case PORT_STATE_IN_PROCEEDING:
1150 response->am[num].u.p.state = ADMIN_STATE_IN_PROCEEDING;
1152 case PORT_STATE_OUT_PROCEEDING:
1153 response->am[num].u.p.state = ADMIN_STATE_OUT_PROCEEDING;
1155 case PORT_STATE_IN_ALERTING:
1156 response->am[num].u.p.state = ADMIN_STATE_IN_ALERTING;
1158 case PORT_STATE_OUT_ALERTING:
1159 response->am[num].u.p.state = ADMIN_STATE_OUT_ALERTING;
1161 case PORT_STATE_CONNECT:
1162 response->am[num].u.p.state = ADMIN_STATE_CONNECT;
1164 case PORT_STATE_IN_DISCONNECT:
1165 response->am[num].u.p.state = ADMIN_STATE_IN_DISCONNECT;
1167 case PORT_STATE_OUT_DISCONNECT:
1168 response->am[num].u.p.state = ADMIN_STATE_OUT_DISCONNECT;
1170 case PORT_STATE_RELEASE:
1171 response->am[num].u.p.state = ADMIN_STATE_RELEASE;
1174 response->am[num].u.p.state = ADMIN_STATE_IDLE;
1177 if ((port->p_type & PORT_CLASS_mISDN_MASK) == PORT_CLASS_DSS1) {
1178 response->am[num].u.p.isdn = 1;
1179 pdss1 = (class Pdss1 *)port;
1180 response->am[num].u.p.isdn_chan = pdss1->p_m_b_channel;
1181 response->am[num].u.p.isdn_hold = pdss1->p_m_hold;
1182 response->am[num].u.p.isdn_ces = pdss1->p_m_d_ces;
1191 int sockserial = 1; // must start with 1, because 0 is used if no serial is set
1193 * handle admin socket (non blocking)
1195 int admin_handle_con(struct lcr_fd *fd, unsigned int what, void *instance, int index);
1197 int admin_handle(struct lcr_fd *fd, unsigned int what, void *instance, int index)
1200 socklen_t sock_len = sizeof(sock_address);
1201 struct admin_list *admin;
1203 /* check for new incoming connections */
1204 if ((new_sock = accept(sock, (struct sockaddr *)&sock_address, &sock_len)) >= 0) {
1205 /* insert new socket */
1206 admin = (struct admin_list *)MALLOC(sizeof(struct admin_list));
1209 admin->sockserial = sockserial++;
1210 admin->next = admin_first;
1211 admin_first = admin;
1212 admin->sock = new_sock;
1213 admin->fd.fd = new_sock;
1214 register_fd(&admin->fd, LCR_FD_READ | LCR_FD_EXCEPT, admin_handle_con, admin, 0);
1216 if (errno != EWOULDBLOCK) {
1217 PERROR("Failed to accept connection from socket \"%s\". (errno=%d) Closing socket.\n", sock_address.sun_path, errno);
1226 int admin_handle_con(struct lcr_fd *fd, unsigned int what, void *instance, int index)
1228 struct admin_list *admin = (struct admin_list *)instance;
1230 struct admin_message msg;
1232 struct Endpoint *epoint;
1234 if ((what & LCR_FD_READ)) {
1236 len = read(admin->sock, &msg, sizeof(msg));
1239 PDEBUG(DEBUG_LOG, "Broken pipe on socket %d. (errno=%d).\n", admin->sock, errno);
1240 free_connection(admin);
1246 /*release endpoint if exists */
1247 if (admin->epointid) {
1248 epoint = find_epoint_id(admin->epointid);
1249 if (epoint && epoint->ep_app_type == EAPP_TYPE_PBX) {
1250 ((class EndpointAppPBX *)epoint->ep_app)->
1251 release(RELEASE_ALL, LOCATION_PRIVATE_LOCAL, CAUSE_NORMAL, LOCATION_PRIVATE_LOCAL, CAUSE_NORMAL, 0);
1255 free_connection(admin);
1258 if (len != sizeof(msg)) {
1259 PERROR("Short/long read on socket %d. (len=%d != size=%d).\n", admin->sock, len, sizeof(msg));
1260 free_connection(admin);
1263 /* process socket command */
1264 if (admin->response && msg.message != ADMIN_MESSAGE) {
1265 PERROR("Data from socket %d while sending response.\n", admin->sock);
1266 free_connection(admin);
1269 switch (msg.message) {
1270 case ADMIN_REQUEST_CMD_INTERFACE:
1271 if (admin_interface(&admin->response) < 0) {
1272 PERROR("Failed to create dial response for socket %d.\n", admin->sock);
1273 goto response_error;
1275 admin->fd.when |= LCR_FD_WRITE;
1278 case ADMIN_REQUEST_CMD_ROUTE:
1279 if (admin_route(&admin->response) < 0) {
1280 PERROR("Failed to create dial response for socket %d.\n", admin->sock);
1281 goto response_error;
1283 admin->fd.when |= LCR_FD_WRITE;
1286 case ADMIN_REQUEST_CMD_DIAL:
1287 if (admin_dial(&admin->response, msg.u.x.message) < 0) {
1288 PERROR("Failed to create dial response for socket %d.\n", admin->sock);
1289 goto response_error;
1291 admin->fd.when |= LCR_FD_WRITE;
1294 case ADMIN_REQUEST_CMD_RELEASE:
1295 if (admin_release(&admin->response, msg.u.x.message) < 0) {
1296 PERROR("Failed to create release response for socket %d.\n", admin->sock);
1297 goto response_error;
1299 admin->fd.when |= LCR_FD_WRITE;
1302 case ADMIN_REQUEST_STATE:
1303 if (admin_state(&admin->response) < 0) {
1304 PERROR("Failed to create state response for socket %d.\n", admin->sock);
1305 goto response_error;
1307 admin->fd.when |= LCR_FD_WRITE;
1310 case ADMIN_TRACE_REQUEST:
1311 if (admin_trace(admin, &msg.u.trace_req) < 0) {
1312 PERROR("Failed to create trace response for socket %d.\n", admin->sock);
1313 goto response_error;
1315 admin->fd.when |= LCR_FD_WRITE;
1318 case ADMIN_REQUEST_CMD_BLOCK:
1319 if (admin_block(&admin->response, msg.u.x.portnum, msg.u.x.block) < 0) {
1320 PERROR("Failed to create block response for socket %d.\n", admin->sock);
1321 goto response_error;
1323 admin->fd.when |= LCR_FD_WRITE;
1327 if (admin_message_to_lcr(&msg.u.msg, admin) < 0) {
1328 PERROR("Failed to deliver message for socket %d.\n", admin->sock);
1329 goto response_error;
1333 case ADMIN_CALL_SETUP:
1334 if (admin_call(admin, &msg) < 0) {
1335 PERROR("Failed to create call for socket %d.\n", admin->sock);
1337 free_connection(admin);
1343 PERROR("Invalid message %d from socket %d.\n", msg.message, admin->sock);
1344 free_connection(admin);
1349 if ((what & LCR_FD_WRITE)) {
1351 if (admin->response) {
1352 len = write(admin->sock, ((unsigned char *)(admin->response->am))+admin->response->offset, sizeof(struct admin_message)*(admin->response->num)-admin->response->offset);
1358 if (len < (int)(sizeof(struct admin_message)*(admin->response->num) - admin->response->offset)) {
1359 admin->response->offset+=len;
1362 temp = admin->response;
1363 admin->response = admin->response->next;
1368 admin->fd.when &= ~LCR_FD_WRITE;
1374 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)
1376 union parameter param;
1378 memset(¶m, 0, sizeof(union parameter));
1379 param.bchannel.isloopback = isloopback;
1380 param.bchannel.type = type;
1381 param.bchannel.handle = handle;
1382 param.bchannel.tx_gain = tx_gain;
1383 param.bchannel.rx_gain = rx_gain;
1385 SCPY(param.bchannel.pipeline, pipeline);
1387 memcpy(param.bchannel.crypt, crypt, crypt_len);
1388 param.bchannel.crypt_type = crypt_type;
1389 if (admin_message_from_lcr(remote_id, ref, MESSAGE_BCHANNEL, ¶m)<0) {
1390 PERROR("No socket with remote id %d found, this happens, if the socket is closed before all bchannels are imported.\n", remote_id);