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;
86 union parameter param;
87 class Join *join, *joinnext;
88 struct mISDNport *mISDNport;
90 class Port *port, *portnext;
91 class Premote *remote;
93 struct admin_list **adminp;
95 /* free remote joins */
96 if (admin->remote_name[0]) {
104 "REMOTE APP release");
105 add_trace("app", "name", "%s", admin->remote_name);
108 /* release all exported channels */
109 mISDNport = mISDNport_first;
112 ii = mISDNport->b_num;
114 if (mISDNport->b_remote_id[i] == admin->sock) {
115 mISDNport->b_state[i] = B_STATE_IDLE;
116 unsched_timer(&mISDNport->b_timer[i]);
117 mISDNport->b_remote_id[i] = 0;
118 mISDNport->b_remote_ref[i] = 0;
122 mISDNport = mISDNport->next;
129 joinnext = join->next;
130 if (join->j_type==JOIN_TYPE_REMOTE) if (((class JoinRemote *)join)->j_remote_id == admin->sock) {
131 memset(¶m, 0, sizeof(param));
132 param.disconnectinfo.cause = CAUSE_OUTOFORDER;
133 param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
134 ((class JoinRemote *)join)->message_remote(MESSAGE_RELEASE, ¶m);
135 /* join is now destroyed, so we go to next join */
139 /* release remote port */
142 portnext = port->next;
143 if ((port->p_type & PORT_CLASS_MASK) == PORT_CLASS_REMOTE) {
144 remote = (class Premote *) port;
145 if (remote->p_m_r_remote_id == admin->sock) {
146 memset(¶m, 0, sizeof(param));
147 param.disconnectinfo.cause = CAUSE_OUTOFORDER;
148 param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
149 remote->message_remote(MESSAGE_RELEASE, ¶m);
150 /* port is now destroyed, so we go to next join */
158 if (admin->sock >= 0) {
159 unregister_fd(&admin->fd);
163 response = admin->response;
165 temp = response->next;
168 response = (struct admin_queue *)temp;
171 adminp = &admin_first;
173 if (*adminp == admin)
175 adminp = &((*adminp)->next);
178 *adminp = (*adminp)->next;
186 * cleanup admin socket
188 void admin_cleanup(void)
190 struct admin_list *admin, *next;;
195 free_connection(admin);
200 unregister_fd(&admin_fd);
210 * do interface reload
212 int admin_interface(struct admin_queue **responsep)
214 struct admin_queue *response; /* response pointer */
215 const char *err_txt = "";
218 if (read_interfaces()) {
220 free_interfaces(interface_first);
221 interface_first = interface_newlist;
222 interface_newlist = NULL;
224 err_txt = interface_error;
227 /* create state response */
228 response = (struct admin_queue *)MALLOC(sizeof(struct admin_queue)+sizeof(admin_message));
232 response->am[0].message = ADMIN_RESPONSE_CMD_INTERFACE;
234 response->am[0].u.x.error = err;
236 SCPY(response->am[0].u.x.message, err_txt);
237 /* attach to response chain */
238 *responsep = response;
239 responsep = &response->next;
247 int admin_route(struct admin_queue **responsep)
249 struct route_ruleset *ruleset_new;
250 struct admin_queue *response; /* response pointer */
251 char err_txt[256] = "";
256 class EndpointAppPBX *apppbx;
260 apppbx = apppbx_first;
263 apppbx = apppbx->next;
266 SPRINT(err_txt, "Cannot reload routing, because %d endpoints active\n", n);
271 if (!(ruleset_new = ruleset_parse())) {
272 SPRINT(err_txt, ruleset_error);
276 ruleset_free(ruleset_first);
277 ruleset_first = ruleset_new;
278 ruleset_main = getrulesetbyname("main");
280 SPRINT(err_txt, "Ruleset reloaded, but rule 'main' not found.\n");
283 apppbx = apppbx_first;
285 if (apppbx->e_action) {
286 switch(apppbx->e_action->index) {
287 case ACTION_INTERNAL:
288 apppbx->e_action = &action_internal;
290 case ACTION_EXTERNAL:
291 apppbx->e_action = &action_external;
294 apppbx->e_action = &action_remote;
296 case ACTION_VBOX_RECORD:
297 apppbx->e_action = &action_vbox;
299 case ACTION_PARTYLINE:
300 apppbx->e_action = &action_partyline;
305 } else if (apppbx->e_state != EPOINT_STATE_CONNECT) {
307 unsched_timer(&apppbx->e_callback_timeout);
308 apppbx->e_action = NULL;
309 apppbx->release(RELEASE_ALL, LOCATION_PRIVATE_LOCAL, CAUSE_NORMAL, LOCATION_PRIVATE_LOCAL, CAUSE_NORMAL, 0);
312 numberrize_callerinfo(apppbx->e_callerinfo.id, apppbx->e_callerinfo.ntype, options.national, options.international),
313 apppbx->e_dialinginfo.id,
316 apppbx->ea_endpoint->ep_serial,
317 "KICK (reload routing)");
321 unsched_timer(&apppbx->e_action_timeout);
322 apppbx->e_rule = NULL;
323 apppbx->e_ruleset = NULL;
325 apppbx = apppbx->next;
329 /* create state response */
330 response = (struct admin_queue *)MALLOC(sizeof(struct admin_queue)+sizeof(admin_message));
334 response->am[0].message = ADMIN_RESPONSE_CMD_ROUTE;
336 response->am[0].u.x.error = err;
338 SCPY(response->am[0].u.x.message, err_txt);
339 /* attach to response chain */
340 *responsep = response;
341 responsep = &response->next;
349 int admin_dial(struct admin_queue **responsep, char *message)
351 struct extension ext; /* temporary extension's settings */
352 struct admin_queue *response; /* response pointer */
353 char *p; /* pointer to dialing digits */
355 /* create state response */
356 response = (struct admin_queue *)MALLOC(sizeof(struct admin_queue)+sizeof(admin_message));
360 response->am[0].message = ADMIN_RESPONSE_CMD_DIAL;
362 /* process request */
363 if (!(p = strchr(message,':'))) {
364 response->am[0].u.x.error = -EINVAL;
365 SPRINT(response->am[0].u.x.message, "no seperator ':' in message to seperate number from extension");
370 /* modify extension */
371 if (!read_extension(&ext, message)) {
372 response->am[0].u.x.error = -EINVAL;
373 SPRINT(response->am[0].u.x.message, "extension doesn't exist");
377 write_extension(&ext, message);
380 /* attach to response chain */
381 *responsep = response;
382 responsep = &response->next;
390 int admin_trace(struct admin_list *admin, struct admin_trace_req *trace)
392 memcpy(&admin->trace, trace, sizeof(struct admin_trace_req));
400 * 0 = make port available
401 * 1 = make port administratively blocked
403 * the result is returned:
404 * 0 = port is now available
405 * 1 = port is now blocked
406 * 2 = port cannot be loaded or has been unloaded
407 * -1 = port doesn't exist
409 int admin_block(struct admin_queue **responsep, int portnum, int block)
411 struct admin_queue *response; /* response pointer */
412 struct interface *interface;
413 struct interface_port *ifport;
415 /* create block response */
416 response = (struct admin_queue *)MALLOC(sizeof(struct admin_queue)+sizeof(admin_message));
420 response->am[0].message = ADMIN_RESPONSE_CMD_BLOCK;
421 response->am[0].u.x.portnum = portnum;
423 /* search for port */
425 interface = interface_first;
427 ifport = interface->ifport;
429 if (ifport->portnum == portnum)
431 ifport = ifport->next;
435 interface = interface->next;
437 /* not found, we return -1 */
439 response->am[0].u.x.block = -1;
440 response->am[0].u.x.error = 1;
441 SPRINT(response->am[0].u.x.message, "Port %d does not exist.", portnum);
447 if (!ifport->mISDNport) {
448 /* not loaded anyway */
450 response->am[0].u.x.block = 2;
454 /* try loading interface */
455 ifport->block = block;
456 load_mISDN_port(ifport);
458 /* port cannot load */
459 if (ifport->block >= 2) {
460 response->am[0].u.x.block = 2;
461 response->am[0].u.x.error = 1;
462 SPRINT(response->am[0].u.x.message, "Port %d will not load.", portnum);
467 response->am[0].u.x.block = ifport->block;
471 /* if we shall unload interface */
473 mISDNport_close(ifport->mISDNport);
474 ifport->mISDNport = 0;
480 /* port new blocking state */
481 ifport->block = response->am[0].u.x.block = block;
484 /* attach to response chain */
485 *responsep = response;
486 responsep = &response->next;
494 int admin_release(struct admin_queue **responsep, char *message)
497 struct admin_queue *response; /* response pointer */
498 class EndpointAppPBX *apppbx;
500 /* create state response */
501 response = (struct admin_queue *)MALLOC(sizeof(struct admin_queue)+sizeof(admin_message));
505 response->am[0].message = ADMIN_RESPONSE_CMD_RELEASE;
508 apppbx = apppbx_first;
510 if (apppbx->ea_endpoint->ep_serial == id)
512 apppbx = apppbx->next;
515 response->am[0].u.x.error = -EINVAL;
516 SPRINT(response->am[0].u.x.message, "Given endpoint %d doesn't exist.", id);
520 unsched_timer(&apppbx->e_callback_timeout);
521 apppbx->release(RELEASE_ALL, LOCATION_PRIVATE_LOCAL, CAUSE_NORMAL, LOCATION_PRIVATE_LOCAL, CAUSE_NORMAL, 0);
524 /* attach to response chain */
525 *responsep = response;
526 responsep = &response->next;
534 int admin_call(struct admin_list *admin, struct admin_message *msg)
536 class Endpoint *epoint;
537 class EndpointAppPBX *apppbx;
539 if (!(epoint = new Endpoint(0, 0)))
540 FATAL("No memory for Endpoint instance\n");
541 if (!(epoint->ep_app = apppbx = new EndpointAppPBX(epoint, 1))) // outgoing
542 FATAL("No memory for Endpoint Application instance\n");
543 apppbx->e_adminid = admin->sockserial;
544 admin->epointid = epoint->ep_serial;
545 SCPY(apppbx->e_callerinfo.id, nationalize_callerinfo(msg->u.call.callerid, &apppbx->e_callerinfo.ntype, options.national, options.international));
546 if (msg->u.call.present)
547 apppbx->e_callerinfo.present = INFO_PRESENT_ALLOWED;
549 apppbx->e_callerinfo.present = INFO_PRESENT_RESTRICTED;
550 apppbx->e_callerinfo.screen = INFO_SCREEN_NETWORK;
553 apppbx->e_capainfo.bearer_capa = msg->u.call.bc_capa;
554 apppbx->e_capainfo.bearer_mode = msg->u.call.bc_mode;
555 apppbx->e_capainfo.bearer_info1 = msg->u.call.bc_info1;
556 apppbx->e_capainfo.hlc = msg->u.call.hlc;
557 apppbx->e_capainfo.exthlc = msg->u.call.exthlc;
558 SCPY(apppbx->e_dialinginfo.id, msg->u.call.dialing);
559 SCPY(apppbx->e_dialinginfo.interfaces, msg->u.call.interface);
560 apppbx->e_dialinginfo.sending_complete = 1;
562 apppbx->new_state(PORT_STATE_OUT_SETUP);
563 apppbx->out_setup(0);
569 * this function is called for response whenever a call state changes.
571 void admin_call_response(int adminid, int message, const char *connected, int cause, int location, int notify_progress)
573 struct admin_list *admin;
574 struct admin_queue *response, **responsep; /* response pointer */
576 /* searching for admin id
577 * maybe there is no admin instance, because the calling port was not
578 * initiated by admin_call */
581 if (adminid == admin->sockserial)
588 /* seek to end of response list */
589 response = admin->response;
590 responsep = &admin->response;
592 responsep = &response->next;
593 response = response->next;
596 /* create state response */
597 response = (struct admin_queue *)MALLOC(sizeof(struct admin_queue)+sizeof(admin_message));
601 response->am[0].message = message;
603 SCPY(response->am[0].u.call.callerid, connected);
604 response->am[0].u.call.cause = cause;
605 response->am[0].u.call.location = location;
606 response->am[0].u.call.notify_progress = notify_progress;
608 /* attach to response chain */
609 *responsep = response;
610 responsep = &response->next;
611 admin->fd.when |= LCR_FD_WRITE;
616 * send data to the remote socket join instance
618 int admin_message_to_lcr(struct admin_msg *msg, struct admin_list *admin)
621 struct mISDNport *mISDNport;
623 class Premote *remote = NULL; /* make GCC happy */
625 class JoinRemote *joinremote = NULL; /* make GCC happy */
627 struct admin_list *temp;
630 if (msg->type == MESSAGE_HELLO) {
631 if (admin->remote_name[0]) {
632 PERROR("Remote application repeats hello message.\n");
635 /* look for second application */
638 if (!strcmp(temp->remote_name, msg->param.hello.application))
643 PERROR("Remote application connects twice??? (ignoring)\n");
646 /* set remote socket instance */
647 SCPY(admin->remote_name, msg->param.hello.application);
655 "REMOTE APP registers");
656 add_trace("app", "name", "%s", admin->remote_name);
661 /* check we have no application name */
662 if (!admin->remote_name[0]) {
663 PERROR("Remote application did not send us a hello message.\n");
668 /* new join. the reply (NEWREF assignment) is sent from constructor */
669 if (msg->type == MESSAGE_NEWREF) {
670 if (msg->param.newref.mode) {
672 /* find remote port */
673 mISDNport = mISDNport_first;
675 if (mISDNport->ifport->remote && !strcmp(mISDNport->ifport->remote_app, admin->remote_name))
677 mISDNport = mISDNport->next;
680 union parameter param;
682 /* create new join instance */
683 join = joinremote = new JoinRemote(0, admin->remote_name, admin->sock); // must have no serial, because no endpoint is connected
685 FATAL("No memory for remote join instance\n");
688 memset(¶m, 0, sizeof(union parameter));
689 param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
690 param.disconnectinfo.cause = CAUSE_RESSOURCEUNAVAIL;
691 admin_message_from_lcr(joinremote->j_remote_id, joinremote->j_remote_ref, MESSAGE_RELEASE, ¶m);
694 /* creating port object, transparent until setup with hdlc */
695 SPRINT(name, "%s-%s-in", mISDNport->ifport->interface->name, mISDNport->ifport->remote_app);
696 if (!(remote = new Premote(PORT_TYPE_REMOTE_IN, mISDNport, name, NULL, 0, 0, B_MODE_TRANSPARENT, admin->sock)))
698 FATAL("Cannot create Port instance.\n");
700 /* create new join instance */
701 join = new JoinRemote(0, admin->remote_name, admin->sock); // must have no serial, because no endpoint is connected
703 FATAL("No memory for remote join instance\n");
713 * no ref given for *_ack */
714 if (msg->type == MESSAGE_BCHANNEL)
715 if (msg->param.bchannel.type == BCHANNEL_ASSIGN_ACK
716 || msg->param.bchannel.type == BCHANNEL_REMOVE_ACK
717 || msg->param.bchannel.type == BCHANNEL_RELEASE) {
718 /* no ref, but address */
719 message_bchannel_from_remote(NULL, msg->param.bchannel.type, msg->param.bchannel.handle);
726 PERROR("Remote application did not send us a valid ref with a message.\n");
731 /* find join instance */
734 if (join->j_type == JOIN_TYPE_REMOTE) {
735 joinremote = (class JoinRemote *)join;
736 if (joinremote->j_remote_ref == msg->ref)
742 if (admin->sock != joinremote->j_remote_id) {
743 PERROR("Ref %d belongs to remote application %s, but not to sending application %s.\n", msg->ref, joinremote->j_remote_name, admin->remote_name);
747 joinremote->message_remote(msg->type, &msg->param);
752 /* find port instance */
755 if ((port->p_type & PORT_CLASS_mISDN_MASK) == PORT_CLASS_REMOTE) {
756 remote = (class Premote *) port;
757 if (remote->p_m_r_ref == msg->ref)
763 if (admin->sock != remote->p_m_r_remote_id) {
764 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);
769 remote->message_remote(msg->type, &msg->param);
775 PDEBUG(DEBUG_LOG, "No remote instance found with ref %d. (May have been already released.)\n", msg->ref);
782 * this function is called for every message to remote socket
784 int admin_message_from_lcr(int remote_id, unsigned int ref, int message_type, union parameter *param)
786 struct admin_list *admin;
787 struct admin_queue **responsep; /* response pointer */
789 /* searching for admin id
790 * maybe there is no given remote application
794 if (admin->remote_name[0] && admin->sock==remote_id)
798 /* no given remote application connected */
802 /* seek to end of response list */
803 responsep = &admin->response;
805 responsep = &(*responsep)->next;
808 /* create state response */
809 *responsep = (struct admin_queue *)MALLOC(sizeof(struct admin_queue)+sizeof(admin_message));
811 (*responsep)->num = 1;
814 (*responsep)->am[0].message = ADMIN_MESSAGE;
815 (*responsep)->am[0].u.msg.type = message_type;
816 (*responsep)->am[0].u.msg.ref = ref;
817 memcpy(&(*responsep)->am[0].u.msg.param, param, sizeof(union parameter));
818 admin->fd.when |= LCR_FD_WRITE;
826 int admin_state(struct admin_queue **responsep)
829 class EndpointAppPBX *apppbx;
833 struct mISDNport *mISDNport;
834 struct select_channel *selchannel;
837 struct interface *interface;
838 struct interface_port *ifport;
841 struct admin_queue *response;
842 struct admin_list *admin;
846 /* create state response */
847 response = (struct admin_queue *)MALLOC(sizeof(struct admin_queue)+sizeof(admin_message));
851 response->am[0].message = ADMIN_RESPONSE_STATE;
853 SCPY(response->am[0].u.s.version_string, VERSION_STRING);
856 now_tm = localtime(&now);
857 memcpy(&response->am[0].u.s.tm, now_tm, sizeof(struct tm));
859 SCPY(response->am[0].u.s.logfile, options.log);
860 /* interface count */
862 interface = interface_first;
864 ifport = interface->ifport;
869 ifport = ifport->next;
871 interface = interface->next;
873 response->am[0].u.s.interfaces = i;
874 /* remote connection count */
878 if (admin->remote_name[0])
882 response->am[0].u.s.remotes = i;
890 response->am[0].u.s.joins = i;
892 apppbx = apppbx_first;
896 apppbx = apppbx->next;
898 response->am[0].u.s.epoints = i;
906 response->am[0].u.s.ports = i;
907 /* attach to response chain */
908 *responsep = response;
909 responsep = &response->next;
911 /* create response for all instances */
912 num = (response->am[0].u.s.interfaces)
913 + (response->am[0].u.s.remotes)
914 + (response->am[0].u.s.joins)
915 + (response->am[0].u.s.epoints)
916 + (response->am[0].u.s.ports);
919 response = (struct admin_queue *)MALLOC(sizeof(admin_queue)+(num*sizeof(admin_message)));
922 *responsep = response;
923 responsep = &response->next;
924 interface = interface_first;
927 ifport = interface->ifport;
930 response->am[num].message = ADMIN_RESPONSE_S_INTERFACE;
932 SCPY(response->am[num].u.i.interface_name, interface->name);
934 response->am[num].u.i.portnum = -100; /* indicate: no ifport */
936 response->am[num].u.i.extension = interface->extension;
942 response->am[num].message = ADMIN_RESPONSE_S_INTERFACE;
944 SCPY(response->am[num].u.i.interface_name, interface->name);
946 response->am[num].u.i.portnum = ifport->portnum;
948 SCPY(response->am[num].u.i.portname, ifport->portname);
950 response->am[num].u.i.extension = interface->extension;
952 response->am[num].u.i.block = ifport->block;
954 if (ifport->mISDNport) {
955 mISDNport = ifport->mISDNport;
958 response->am[num].u.i.ptp = mISDNport->ptp;
960 response->am[num].u.i.l1hold = mISDNport->l1hold;
962 response->am[num].u.i.l2hold = mISDNport->l2hold;
964 response->am[num].u.i.ntmode = mISDNport->ntmode;
966 response->am[num].u.i.pri = mISDNport->pri;
968 response->am[num].u.i.use = mISDNport->use;
970 response->am[num].u.i.l1link = mISDNport->l1link;
972 response->am[num].u.i.l2link = mISDNport->l2link;
973 memcpy(response->am[num].u.i.l2mask, mISDNport->l2mask, 16);
975 response->am[num].u.i.los = mISDNport->los;
977 response->am[num].u.i.ais = mISDNport->ais;
979 response->am[num].u.i.rdi = mISDNport->rdi;
981 response->am[num].u.i.slip_tx = mISDNport->slip_tx;
982 response->am[num].u.i.slip_rx = mISDNport->slip_rx;
984 response->am[num].u.i.channels = mISDNport->b_num;
985 /* channel selection */
986 selchannel = ifport->out_channel;
987 if (ifport->channel_force)
988 SCAT(response->am[num].u.i.out_channel, "force");
990 if (response->am[num].u.i.out_channel[0])
991 SCAT(response->am[num].u.i.out_channel, ",");
992 switch (selchannel->channel) {
994 SCAT(response->am[num].u.i.out_channel, "no");
997 SCAT(response->am[num].u.i.out_channel, "any");
1000 SCAT(response->am[num].u.i.out_channel, "free");
1003 SPRINT(strchr(response->am[num].u.i.out_channel, '\0'), "%d", selchannel->channel);
1005 selchannel = selchannel->next;
1007 selchannel = ifport->in_channel;
1008 while (selchannel) {
1009 switch (selchannel->channel) {
1011 SCAT(response->am[num].u.i.in_channel, "free");
1014 SPRINT(strchr(response->am[num].u.i.in_channel, '\0'), "%d", selchannel->channel);
1016 selchannel = selchannel->next;
1021 while(i < mISDNport->b_num) {
1022 response->am[num].u.i.busy[i] = mISDNport->b_state[i];
1023 if (mISDNport->b_port[i])
1024 response->am[num].u.i.port[i] = mISDNport->b_port[i]->p_serial;
1025 response->am[num].u.i.mode[i] = mISDNport->b_mode[i];
1032 ifport = ifport->next;
1034 interface = interface->next;
1037 /* create response for all remotes */
1038 admin = admin_first;
1040 if (admin->remote_name[0]) {
1042 response->am[num].message = ADMIN_RESPONSE_S_REMOTE;
1044 SCPY(response->am[num].u.r.name, admin->remote_name);
1048 admin = admin->next;
1051 /* create response for all joins */
1055 response->am[num].message = ADMIN_RESPONSE_S_JOIN;
1057 response->am[num].u.j.serial = join->j_serial;
1059 if (join->j_type == JOIN_TYPE_PBX)
1060 response->am[num].u.j.partyline = ((class JoinPBX *)join)->j_partyline;
1061 /* remote application */
1062 if (join->j_type == JOIN_TYPE_REMOTE)
1063 SCPY(response->am[num].u.j.remote, ((class JoinRemote *)join)->j_remote_name);
1069 /* create response for all endpoint */
1070 apppbx = apppbx_first;
1073 response->am[num].message = ADMIN_RESPONSE_S_EPOINT;
1075 response->am[num].u.e.serial = apppbx->ea_endpoint->ep_serial;
1077 response->am[num].u.e.join = apppbx->ea_endpoint->ep_join_id;
1078 /* rx notification */
1079 response->am[num].u.e.rx_state = apppbx->e_rx_state;
1080 /* tx notification */
1081 response->am[num].u.e.tx_state = apppbx->e_tx_state;
1083 switch(apppbx->e_state) {
1084 case EPOINT_STATE_IN_SETUP:
1085 response->am[num].u.e.state = ADMIN_STATE_IN_SETUP;
1087 case EPOINT_STATE_OUT_SETUP:
1088 response->am[num].u.e.state = ADMIN_STATE_OUT_SETUP;
1090 case EPOINT_STATE_IN_OVERLAP:
1091 response->am[num].u.e.state = ADMIN_STATE_IN_OVERLAP;
1093 case EPOINT_STATE_OUT_OVERLAP:
1094 response->am[num].u.e.state = ADMIN_STATE_OUT_OVERLAP;
1096 case EPOINT_STATE_IN_PROCEEDING:
1097 response->am[num].u.e.state = ADMIN_STATE_IN_PROCEEDING;
1099 case EPOINT_STATE_OUT_PROCEEDING:
1100 response->am[num].u.e.state = ADMIN_STATE_OUT_PROCEEDING;
1102 case EPOINT_STATE_IN_ALERTING:
1103 response->am[num].u.e.state = ADMIN_STATE_IN_ALERTING;
1105 case EPOINT_STATE_OUT_ALERTING:
1106 response->am[num].u.e.state = ADMIN_STATE_OUT_ALERTING;
1108 case EPOINT_STATE_CONNECT:
1109 response->am[num].u.e.state = ADMIN_STATE_CONNECT;
1111 case EPOINT_STATE_IN_DISCONNECT:
1112 response->am[num].u.e.state = ADMIN_STATE_IN_DISCONNECT;
1114 case EPOINT_STATE_OUT_DISCONNECT:
1115 response->am[num].u.e.state = ADMIN_STATE_OUT_DISCONNECT;
1118 response->am[num].u.e.state = ADMIN_STATE_IDLE;
1121 SCPY(response->am[num].u.e.terminal, apppbx->e_ext.number);
1123 SCPY(response->am[num].u.e.callerid, apppbx->e_callerinfo.id);
1125 SCPY(response->am[num].u.e.dialing, apppbx->e_dialinginfo.id);
1127 if (apppbx->e_action)
1128 SCPY(response->am[num].u.e.action, action_defs[apppbx->e_action->index].name);
1129 // if (apppbx->e_action)
1130 // printf("action=%s\n",action_defs[apppbx->e_action->index].name);
1132 response->am[num].u.e.park = apppbx->ea_endpoint->ep_park;
1133 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))
1134 memcpy(response->am[num].u.e.park_callid, apppbx->ea_endpoint->ep_park_callid, apppbx->ea_endpoint->ep_park_len);
1135 response->am[num].u.e.park_len = apppbx->ea_endpoint->ep_park_len;
1138 if (apppbx->e_crypt == CRYPT_ON)
1139 response->am[num].u.e.crypt = 1;
1142 apppbx = apppbx->next;
1146 /* create response for all ports */
1150 response->am[num].message = ADMIN_RESPONSE_S_PORT;
1152 response->am[num].u.p.serial = port->p_serial;
1154 SCPY(response->am[num].u.p.name, port->p_name);
1156 response->am[num].u.p.epoint = ACTIVE_EPOINT(port->p_epointlist);
1158 switch(port->p_state) {
1159 case PORT_STATE_IN_SETUP:
1160 response->am[num].u.p.state = ADMIN_STATE_IN_SETUP;
1162 case PORT_STATE_OUT_SETUP:
1163 response->am[num].u.p.state = ADMIN_STATE_OUT_SETUP;
1165 case PORT_STATE_IN_OVERLAP:
1166 response->am[num].u.p.state = ADMIN_STATE_IN_OVERLAP;
1168 case PORT_STATE_OUT_OVERLAP:
1169 response->am[num].u.p.state = ADMIN_STATE_OUT_OVERLAP;
1171 case PORT_STATE_IN_PROCEEDING:
1172 response->am[num].u.p.state = ADMIN_STATE_IN_PROCEEDING;
1174 case PORT_STATE_OUT_PROCEEDING:
1175 response->am[num].u.p.state = ADMIN_STATE_OUT_PROCEEDING;
1177 case PORT_STATE_IN_ALERTING:
1178 response->am[num].u.p.state = ADMIN_STATE_IN_ALERTING;
1180 case PORT_STATE_OUT_ALERTING:
1181 response->am[num].u.p.state = ADMIN_STATE_OUT_ALERTING;
1183 case PORT_STATE_CONNECT:
1184 response->am[num].u.p.state = ADMIN_STATE_CONNECT;
1186 case PORT_STATE_IN_DISCONNECT:
1187 response->am[num].u.p.state = ADMIN_STATE_IN_DISCONNECT;
1189 case PORT_STATE_OUT_DISCONNECT:
1190 response->am[num].u.p.state = ADMIN_STATE_OUT_DISCONNECT;
1192 case PORT_STATE_RELEASE:
1193 response->am[num].u.p.state = ADMIN_STATE_RELEASE;
1196 response->am[num].u.p.state = ADMIN_STATE_IDLE;
1200 if ((port->p_type & PORT_CLASS_mISDN_MASK) == PORT_CLASS_DSS1) {
1201 response->am[num].u.p.isdn = 1;
1202 pdss1 = (class Pdss1 *)port;
1203 response->am[num].u.p.isdn_chan = pdss1->p_m_b_channel;
1204 response->am[num].u.p.isdn_hold = pdss1->p_m_hold;
1205 response->am[num].u.p.isdn_ces = pdss1->p_m_d_ces;
1215 int sockserial = 1; // must start with 1, because 0 is used if no serial is set
1217 * handle admin socket (non blocking)
1219 int admin_handle_con(struct lcr_fd *fd, unsigned int what, void *instance, int index);
1221 int admin_handle(struct lcr_fd *fd, unsigned int what, void *instance, int index)
1224 socklen_t sock_len = sizeof(sock_address);
1225 struct admin_list *admin;
1227 /* check for new incoming connections */
1228 if ((new_sock = accept(sock, (struct sockaddr *)&sock_address, &sock_len)) >= 0) {
1229 /* insert new socket */
1230 admin = (struct admin_list *)MALLOC(sizeof(struct admin_list));
1233 admin->sockserial = sockserial++;
1234 admin->next = admin_first;
1235 admin_first = admin;
1236 admin->sock = new_sock;
1237 admin->fd.fd = new_sock;
1238 register_fd(&admin->fd, LCR_FD_READ | LCR_FD_EXCEPT, admin_handle_con, admin, 0);
1240 if (errno != EWOULDBLOCK) {
1241 PERROR("Failed to accept connection from socket \"%s\". (errno=%d) Closing socket.\n", sock_address.sun_path, errno);
1250 int admin_handle_con(struct lcr_fd *fd, unsigned int what, void *instance, int index)
1252 struct admin_list *admin = (struct admin_list *)instance;
1254 struct admin_message msg;
1256 struct Endpoint *epoint;
1258 if ((what & LCR_FD_READ)) {
1260 len = read(admin->sock, &msg, sizeof(msg));
1263 PDEBUG(DEBUG_LOG, "Broken pipe on socket %d. (errno=%d).\n", admin->sock, errno);
1265 /*release endpoint if exists */
1266 if (admin->epointid) {
1267 epoint = find_epoint_id(admin->epointid);
1268 if (epoint && epoint->ep_app_type == EAPP_TYPE_PBX) {
1269 ((class EndpointAppPBX *)epoint->ep_app)->
1270 release(RELEASE_ALL, LOCATION_PRIVATE_LOCAL, CAUSE_NORMAL, LOCATION_PRIVATE_LOCAL, CAUSE_NORMAL, 0);
1274 free_connection(admin);
1279 if (len != sizeof(msg)) {
1280 PERROR("Short/long read on socket %d. (len=%d != size=%d).\n", admin->sock, len, sizeof(msg));
1283 /* process socket command */
1284 if (admin->response && msg.message != ADMIN_MESSAGE) {
1285 PERROR("Data from socket %d while sending response.\n", admin->sock);
1288 switch (msg.message) {
1289 case ADMIN_REQUEST_CMD_INTERFACE:
1290 if (admin_interface(&admin->response) < 0) {
1291 PERROR("Failed to create dial response for socket %d.\n", admin->sock);
1292 goto response_error;
1294 admin->fd.when |= LCR_FD_WRITE;
1297 case ADMIN_REQUEST_CMD_ROUTE:
1298 if (admin_route(&admin->response) < 0) {
1299 PERROR("Failed to create dial response for socket %d.\n", admin->sock);
1300 goto response_error;
1302 admin->fd.when |= LCR_FD_WRITE;
1305 case ADMIN_REQUEST_CMD_DIAL:
1306 if (admin_dial(&admin->response, msg.u.x.message) < 0) {
1307 PERROR("Failed to create dial response for socket %d.\n", admin->sock);
1308 goto response_error;
1310 admin->fd.when |= LCR_FD_WRITE;
1313 case ADMIN_REQUEST_CMD_RELEASE:
1314 if (admin_release(&admin->response, msg.u.x.message) < 0) {
1315 PERROR("Failed to create release response for socket %d.\n", admin->sock);
1316 goto response_error;
1318 admin->fd.when |= LCR_FD_WRITE;
1321 case ADMIN_REQUEST_STATE:
1322 if (admin_state(&admin->response) < 0) {
1323 PERROR("Failed to create state response for socket %d.\n", admin->sock);
1324 goto response_error;
1326 admin->fd.when |= LCR_FD_WRITE;
1329 case ADMIN_TRACE_REQUEST:
1330 if (admin_trace(admin, &msg.u.trace_req) < 0) {
1331 PERROR("Failed to create trace response for socket %d.\n", admin->sock);
1332 goto response_error;
1334 admin->fd.when |= LCR_FD_WRITE;
1337 case ADMIN_REQUEST_CMD_BLOCK:
1338 if (admin_block(&admin->response, msg.u.x.portnum, msg.u.x.block) < 0) {
1339 PERROR("Failed to create block response for socket %d.\n", admin->sock);
1340 goto response_error;
1342 admin->fd.when |= LCR_FD_WRITE;
1346 if (admin_message_to_lcr(&msg.u.msg, admin) < 0) {
1347 PERROR("Failed to deliver message for socket %d.\n", admin->sock);
1348 goto response_error;
1352 case ADMIN_CALL_SETUP:
1353 if (admin_call(admin, &msg) < 0) {
1354 PERROR("Failed to create call for socket %d.\n", admin->sock);
1361 PERROR("Invalid message %d from socket %d.\n", msg.message, admin->sock);
1366 if ((what & LCR_FD_WRITE)) {
1368 if (admin->response) {
1369 len = write(admin->sock, ((unsigned char *)(admin->response->am))+admin->response->offset, sizeof(struct admin_message)*(admin->response->num)-admin->response->offset);
1375 if (len < (int)(sizeof(struct admin_message)*(admin->response->num) - admin->response->offset)) {
1376 admin->response->offset+=len;
1379 temp = admin->response;
1380 admin->response = admin->response->next;
1385 admin->fd.when &= ~LCR_FD_WRITE;
1391 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)
1393 union parameter param;
1395 memset(¶m, 0, sizeof(union parameter));
1396 param.bchannel.isloopback = isloopback;
1397 param.bchannel.type = type;
1398 param.bchannel.handle = handle;
1399 param.bchannel.tx_gain = tx_gain;
1400 param.bchannel.rx_gain = rx_gain;
1402 SCPY(param.bchannel.pipeline, pipeline);
1404 memcpy(param.bchannel.crypt, crypt, crypt_len);
1405 param.bchannel.crypt_type = crypt_type;
1406 if (admin_message_from_lcr(remote_id, ref, MESSAGE_BCHANNEL, ¶m)<0) {
1407 PERROR("No socket with remote id %d found, this happens, if the socket is closed before all bchannels are imported.\n", remote_id);