1 /*****************************************************************************\
3 ** Linux Call Router **
5 **---------------------------------------------------------------------------**
6 ** Copyright: Andreas Eversberg **
8 ** Socket link server **
10 \*****************************************************************************/
13 #include <sys/socket.h>
19 char socket_name[128];
21 struct sockaddr_un sock_address;
23 struct admin_list *admin_first = NULL;
24 static struct lcr_fd admin_fd;
26 int admin_handle(struct lcr_fd *fd, unsigned int what, void *instance, int index);
29 * initialize admin socket
33 /* open and bind socket */
34 if ((sock = socket(PF_UNIX, SOCK_STREAM, 0)) < 0) {
35 PERROR("Failed to create admin socket. (errno=%d)\n", errno);
39 memset(&sock_address, 0, sizeof(sock_address));
40 SPRINT(socket_name, SOCKET_NAME, options.lock);
41 sock_address.sun_family = AF_UNIX;
42 UCPY(sock_address.sun_path, socket_name);
44 if (bind(sock, (struct sockaddr *)(&sock_address), SUN_LEN(&sock_address)) < 0) {
49 PERROR("Failed to bind admin socket to \"%s\". (errno=%d)\n", sock_address.sun_path, errno);
52 if (listen(sock, 5) < 0) {
57 PERROR("Failed to listen to socket \"%s\". (errno=%d)\n", sock_address.sun_path, errno);
60 memset(&admin_fd, 0, sizeof(admin_fd));
62 register_fd(&admin_fd, LCR_FD_READ | LCR_FD_EXCEPT, admin_handle, NULL, 0);
63 if (chmod(socket_name, options.socketrights) < 0) {
64 PERROR("Failed to change socket rights to %d. (errno=%d)\n", options.socketrights, errno);
66 if (chown(socket_name, options.socketuser, options.socketgroup) < 0) {
67 PERROR("Failed to change socket user/group to %d/%d. (errno=%d)\n", options.socketuser, options.socketgroup, errno);
76 * also releases all remote joins
78 void free_connection(struct admin_list *admin)
80 struct admin_queue *response;
82 union parameter param;
83 class Join *join, *joinnext;
84 struct mISDNport *mISDNport;
86 struct admin_list **adminp;
87 class Port *port, *portnext;
88 class Premote *remote;
90 /* free remote joins */
91 if (admin->remote_name[0]) {
99 "REMOTE APP release");
100 add_trace("app", "name", "%s", admin->remote_name);
102 /* release all exported channels */
103 mISDNport = mISDNport_first;
106 ii = mISDNport->b_num;
108 if (mISDNport->b_remote_id[i] == admin->sock) {
109 mISDNport->b_state[i] = B_STATE_IDLE;
110 unsched_timer(&mISDNport->b_timer[i]);
111 mISDNport->b_remote_id[i] = 0;
112 mISDNport->b_remote_ref[i] = 0;
116 mISDNport = mISDNport->next;
121 joinnext = join->next;
122 if (join->j_type==JOIN_TYPE_REMOTE) if (((class JoinRemote *)join)->j_remote_id == admin->sock) {
123 memset(¶m, 0, sizeof(param));
124 param.disconnectinfo.cause = CAUSE_OUTOFORDER;
125 param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
126 ((class JoinRemote *)join)->message_remote(MESSAGE_RELEASE, ¶m);
127 /* join is now destroyed, so we go to next join */
131 /* release remote port */
134 portnext = port->next;
135 if ((port->p_type & PORT_CLASS_MASK) == PORT_CLASS_REMOTE) {
136 remote = (class Premote *) port;
137 if (remote->p_m_r_remote_id == admin->sock) {
138 memset(¶m, 0, sizeof(param));
139 param.disconnectinfo.cause = CAUSE_OUTOFORDER;
140 param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
141 remote->message_remote(MESSAGE_RELEASE, ¶m);
142 /* port is now destroyed, so we go to next join */
149 if (admin->sock >= 0) {
150 unregister_fd(&admin->fd);
154 response = admin->response;
156 temp = response->next;
159 response = (struct admin_queue *)temp;
162 adminp = &admin_first;
164 if (*adminp == admin)
166 adminp = &((*adminp)->next);
169 *adminp = (*adminp)->next;
177 * cleanup admin socket
179 void admin_cleanup(void)
181 struct admin_list *admin, *next;;
186 free_connection(admin);
191 unregister_fd(&admin_fd);
201 * do interface reload
203 int admin_interface(struct admin_queue **responsep)
205 struct admin_queue *response; /* response pointer */
206 const char *err_txt = "";
209 if (read_interfaces()) {
211 free_interfaces(interface_first);
212 interface_first = interface_newlist;
213 interface_newlist = NULL;
215 err_txt = interface_error;
218 /* create state response */
219 response = (struct admin_queue *)MALLOC(sizeof(struct admin_queue)+sizeof(admin_message));
223 response->am[0].message = ADMIN_RESPONSE_CMD_INTERFACE;
225 response->am[0].u.x.error = err;
227 SCPY(response->am[0].u.x.message, err_txt);
228 /* attach to response chain */
229 *responsep = response;
230 responsep = &response->next;
238 int admin_route(struct admin_queue **responsep)
240 struct route_ruleset *ruleset_new;
241 struct admin_queue *response; /* response pointer */
242 char err_txt[256] = "";
247 class EndpointAppPBX *apppbx;
251 apppbx = apppbx_first;
254 apppbx = apppbx->next;
257 SPRINT(err_txt, "Cannot reload routing, because %d endpoints active\n", n);
262 if (!(ruleset_new = ruleset_parse())) {
263 SPRINT(err_txt, ruleset_error);
267 ruleset_free(ruleset_first);
268 ruleset_first = ruleset_new;
269 ruleset_main = getrulesetbyname("main");
271 SPRINT(err_txt, "Ruleset reloaded, but rule 'main' not found.\n");
274 apppbx = apppbx_first;
276 if (apppbx->e_action) {
277 switch(apppbx->e_action->index) {
278 case ACTION_INTERNAL:
279 apppbx->e_action = &action_internal;
281 case ACTION_EXTERNAL:
282 apppbx->e_action = &action_external;
285 apppbx->e_action = &action_remote;
287 case ACTION_VBOX_RECORD:
288 apppbx->e_action = &action_vbox;
290 case ACTION_PARTYLINE:
291 apppbx->e_action = &action_partyline;
296 } else if (apppbx->e_state != EPOINT_STATE_CONNECT) {
298 unsched_timer(&apppbx->e_callback_timeout);
299 apppbx->e_action = NULL;
300 apppbx->release(RELEASE_ALL, LOCATION_PRIVATE_LOCAL, CAUSE_NORMAL, LOCATION_PRIVATE_LOCAL, CAUSE_NORMAL, 0);
303 numberrize_callerinfo(apppbx->e_callerinfo.id, apppbx->e_callerinfo.ntype, options.national, options.international),
304 apppbx->e_dialinginfo.id,
307 apppbx->ea_endpoint->ep_serial,
308 "KICK (reload routing)");
312 unsched_timer(&apppbx->e_action_timeout);
313 apppbx->e_rule = NULL;
314 apppbx->e_ruleset = NULL;
316 apppbx = apppbx->next;
320 /* create state response */
321 response = (struct admin_queue *)MALLOC(sizeof(struct admin_queue)+sizeof(admin_message));
325 response->am[0].message = ADMIN_RESPONSE_CMD_ROUTE;
327 response->am[0].u.x.error = err;
329 SCPY(response->am[0].u.x.message, err_txt);
330 /* attach to response chain */
331 *responsep = response;
332 responsep = &response->next;
340 int admin_dial(struct admin_queue **responsep, char *message)
342 struct extension ext; /* temporary extension's settings */
343 struct admin_queue *response; /* response pointer */
344 char *p; /* pointer to dialing digits */
346 /* create state response */
347 response = (struct admin_queue *)MALLOC(sizeof(struct admin_queue)+sizeof(admin_message));
351 response->am[0].message = ADMIN_RESPONSE_CMD_DIAL;
353 /* process request */
354 if (!(p = strchr(message,':'))) {
355 response->am[0].u.x.error = -EINVAL;
356 SPRINT(response->am[0].u.x.message, "no seperator ':' in message to seperate number from extension");
361 /* modify extension */
362 if (!read_extension(&ext, message)) {
363 response->am[0].u.x.error = -EINVAL;
364 SPRINT(response->am[0].u.x.message, "extension doesn't exist");
368 write_extension(&ext, message);
371 /* attach to response chain */
372 *responsep = response;
373 responsep = &response->next;
381 int admin_trace(struct admin_list *admin, struct admin_trace_req *trace)
383 memcpy(&admin->trace, trace, sizeof(struct admin_trace_req));
391 * 0 = make port available
392 * 1 = make port administratively blocked
394 * the result is returned:
395 * 0 = port is now available
396 * 1 = port is now blocked
397 * 2 = port cannot be loaded or has been unloaded
398 * -1 = port doesn't exist
400 int admin_block(struct admin_queue **responsep, int portnum, int block)
402 struct admin_queue *response; /* response pointer */
403 struct interface *interface;
404 struct interface_port *ifport;
406 /* create block response */
407 response = (struct admin_queue *)MALLOC(sizeof(struct admin_queue)+sizeof(admin_message));
411 response->am[0].message = ADMIN_RESPONSE_CMD_BLOCK;
412 response->am[0].u.x.portnum = portnum;
414 /* search for port */
416 interface = interface_first;
418 ifport = interface->ifport;
420 if (ifport->portnum == portnum)
422 ifport = ifport->next;
426 interface = interface->next;
428 /* not found, we return -1 */
430 response->am[0].u.x.block = -1;
431 response->am[0].u.x.error = 1;
432 SPRINT(response->am[0].u.x.message, "Port %d does not exist.", portnum);
437 if (!ifport->mISDNport) {
438 /* not loaded anyway */
440 response->am[0].u.x.block = 2;
444 /* try loading interface */
445 ifport->block = block;
448 /* port cannot load */
449 if (ifport->block >= 2) {
450 response->am[0].u.x.block = 2;
451 response->am[0].u.x.error = 1;
452 SPRINT(response->am[0].u.x.message, "Port %d will not load.", portnum);
457 response->am[0].u.x.block = ifport->block;
461 /* if we shall unload interface */
463 mISDNport_close(ifport->mISDNport);
464 ifport->mISDNport = 0;
469 /* port new blocking state */
470 ifport->block = response->am[0].u.x.block = block;
473 /* attach to response chain */
474 *responsep = response;
475 responsep = &response->next;
483 int admin_release(struct admin_queue **responsep, char *message)
486 struct admin_queue *response; /* response pointer */
487 class EndpointAppPBX *apppbx;
489 /* create state response */
490 response = (struct admin_queue *)MALLOC(sizeof(struct admin_queue)+sizeof(admin_message));
494 response->am[0].message = ADMIN_RESPONSE_CMD_RELEASE;
497 apppbx = apppbx_first;
499 if (apppbx->ea_endpoint->ep_serial == id)
501 apppbx = apppbx->next;
504 response->am[0].u.x.error = -EINVAL;
505 SPRINT(response->am[0].u.x.message, "Given endpoint %d doesn't exist.", id);
509 unsched_timer(&apppbx->e_callback_timeout);
510 apppbx->release(RELEASE_ALL, LOCATION_PRIVATE_LOCAL, CAUSE_NORMAL, LOCATION_PRIVATE_LOCAL, CAUSE_NORMAL, 0);
513 /* attach to response chain */
514 *responsep = response;
515 responsep = &response->next;
523 int admin_call(struct admin_list *admin, struct admin_message *msg)
525 class Endpoint *epoint;
526 class EndpointAppPBX *apppbx;
528 if (!(epoint = new Endpoint(0, 0)))
529 FATAL("No memory for Endpoint instance\n");
530 if (!(epoint->ep_app = apppbx = new DEFAULT_ENDPOINT_APP(epoint, 1))) // outgoing
531 FATAL("No memory for Endpoint Application instance\n");
532 apppbx->e_adminid = admin->sockserial;
533 admin->epointid = epoint->ep_serial;
534 SCPY(apppbx->e_callerinfo.id, nationalize_callerinfo(msg->u.call.callerid, &apppbx->e_callerinfo.ntype, options.national, options.international));
535 if (msg->u.call.present)
536 apppbx->e_callerinfo.present = INFO_PRESENT_ALLOWED;
538 apppbx->e_callerinfo.present = INFO_PRESENT_RESTRICTED;
539 apppbx->e_callerinfo.screen = INFO_SCREEN_NETWORK;
542 apppbx->e_capainfo.bearer_capa = msg->u.call.bc_capa;
543 apppbx->e_capainfo.bearer_mode = msg->u.call.bc_mode;
544 apppbx->e_capainfo.bearer_info1 = msg->u.call.bc_info1;
545 apppbx->e_capainfo.hlc = msg->u.call.hlc;
546 apppbx->e_capainfo.exthlc = msg->u.call.exthlc;
547 SCPY(apppbx->e_dialinginfo.id, msg->u.call.dialing);
548 SCPY(apppbx->e_dialinginfo.interfaces, msg->u.call.interface);
549 apppbx->e_dialinginfo.sending_complete = 1;
551 apppbx->new_state(PORT_STATE_OUT_SETUP);
552 apppbx->out_setup(0);
558 * this function is called for response whenever a call state changes.
560 void admin_call_response(int adminid, int message, const char *connected, int cause, int location, int notify_progress)
562 struct admin_list *admin;
563 struct admin_queue *response, **responsep; /* response pointer */
565 /* searching for admin id
566 * maybe there is no admin instance, because the calling port was not
567 * initiated by admin_call */
570 if (adminid == admin->sockserial)
577 /* seek to end of response list */
578 response = admin->response;
579 responsep = &admin->response;
581 responsep = &response->next;
582 response = response->next;
585 /* create state response */
586 response = (struct admin_queue *)MALLOC(sizeof(struct admin_queue)+sizeof(admin_message));
590 response->am[0].message = message;
592 SCPY(response->am[0].u.call.callerid, connected);
593 response->am[0].u.call.cause = cause;
594 response->am[0].u.call.location = location;
595 response->am[0].u.call.notify_progress = notify_progress;
597 /* attach to response chain */
598 *responsep = response;
599 responsep = &response->next;
600 admin->fd.when |= LCR_FD_WRITE;
605 * send data to the remote socket join instance
607 int admin_message_to_lcr(struct admin_msg *msg, struct admin_list *admin)
609 struct mISDNport *mISDNport;
611 class JoinRemote *joinremote = NULL; /* make GCC happy */
613 class Premote *remote = NULL; /* make GCC happy */
614 struct admin_list *temp;
617 if (msg->type == MESSAGE_HELLO) {
618 if (admin->remote_name[0]) {
619 PERROR("Remote application repeats hello message.\n");
622 /* look for second application */
625 if (!strcmp(temp->remote_name, msg->param.hello.application))
630 PERROR("Remote application connects twice??? (ignoring)\n");
633 /* set remote socket instance */
634 SCPY(admin->remote_name, msg->param.hello.application);
642 "REMOTE APP registers");
643 add_trace("app", "name", "%s", admin->remote_name);
648 /* check we have no application name */
649 if (!admin->remote_name[0]) {
650 PERROR("Remote application did not send us a hello message.\n");
654 /* new join. the reply (NEWREF assignment) is sent from constructor */
655 if (msg->type == MESSAGE_NEWREF) {
656 if (msg->param.newref.mode) {
658 /* find remote port */
659 mISDNport = mISDNport_first;
661 if (mISDNport->ifport->remote && !strcmp(mISDNport->ifport->remote_app, admin->remote_name))
663 mISDNport = mISDNport->next;
666 union parameter param;
668 /* create new join instance */
669 join = joinremote = new JoinRemote(0, admin->remote_name, admin->sock); // must have no serial, because no endpoint is connected
671 FATAL("No memory for remote join instance\n");
674 memset(¶m, 0, sizeof(union parameter));
675 param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
676 param.disconnectinfo.cause = CAUSE_RESSOURCEUNAVAIL;
677 admin_message_from_lcr(joinremote->j_remote_id, joinremote->j_remote_ref, MESSAGE_RELEASE, ¶m);
680 /* creating port object, transparent until setup with hdlc */
681 SPRINT(name, "%s-%s-in", mISDNport->ifport->interface->name, mISDNport->ifport->remote_app);
682 if (!(remote = new Premote(PORT_TYPE_REMOTE_IN, mISDNport, name, NULL, 0, 0, B_MODE_TRANSPARENT, admin->sock)))
684 FATAL("Cannot create Port instance.\n");
686 /* create new join instance */
687 join = new JoinRemote(0, admin->remote_name, admin->sock); // must have no serial, because no endpoint is connected
689 FATAL("No memory for remote join instance\n");
697 * no ref given for *_ack */
698 if (msg->type == MESSAGE_BCHANNEL)
699 if (msg->param.bchannel.type == BCHANNEL_ASSIGN_ACK
700 || msg->param.bchannel.type == BCHANNEL_REMOVE_ACK
701 || msg->param.bchannel.type == BCHANNEL_RELEASE) {
702 /* no ref, but address */
703 message_bchannel_from_remote(NULL, msg->param.bchannel.type, msg->param.bchannel.handle);
709 PERROR("Remote application did not send us a valid ref with a message.\n");
713 /* find join instance */
716 if (join->j_type == JOIN_TYPE_REMOTE) {
717 joinremote = (class JoinRemote *)join;
718 if (joinremote->j_remote_ref == msg->ref)
724 if (admin->sock != joinremote->j_remote_id) {
725 PERROR("Ref %d belongs to remote application %s, but not to sending application %s.\n", msg->ref, joinremote->j_remote_name, admin->remote_name);
729 joinremote->message_remote(msg->type, &msg->param);
734 /* find port instance */
737 if ((port->p_type & PORT_CLASS_mISDN_MASK) == PORT_CLASS_REMOTE) {
738 remote = (class Premote *) port;
739 if (remote->p_m_r_ref == msg->ref)
745 if (admin->sock != remote->p_m_r_remote_id) {
746 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);
751 remote->message_remote(msg->type, &msg->param);
756 PDEBUG(DEBUG_LOG, "No remote instance found with ref %d. (May have been already released.)\n", msg->ref);
763 * this function is called for every message to remote socket
765 int admin_message_from_lcr(int remote_id, unsigned int ref, int message_type, union parameter *param)
767 struct admin_list *admin;
768 struct admin_queue **responsep; /* response pointer */
770 /* searching for admin id
771 * maybe there is no given remote application
775 if (admin->remote_name[0] && admin->sock==remote_id)
779 /* no given remote application connected */
783 /* seek to end of response list */
784 responsep = &admin->response;
786 responsep = &(*responsep)->next;
789 /* create state response */
790 *responsep = (struct admin_queue *)MALLOC(sizeof(struct admin_queue)+sizeof(admin_message));
792 (*responsep)->num = 1;
795 (*responsep)->am[0].message = ADMIN_MESSAGE;
796 (*responsep)->am[0].u.msg.type = message_type;
797 (*responsep)->am[0].u.msg.ref = ref;
798 memcpy(&(*responsep)->am[0].u.msg.param, param, sizeof(union parameter));
799 admin->fd.when |= LCR_FD_WRITE;
807 int admin_state(struct admin_queue **responsep)
810 class EndpointAppPBX *apppbx;
813 struct interface *interface;
814 struct interface_port *ifport;
815 struct mISDNport *mISDNport;
816 struct select_channel *selchannel;
820 struct admin_queue *response;
821 struct admin_list *admin;
825 /* create state response */
826 response = (struct admin_queue *)MALLOC(sizeof(struct admin_queue)+sizeof(admin_message));
830 response->am[0].message = ADMIN_RESPONSE_STATE;
832 SCPY(response->am[0].u.s.version_string, VERSION_STRING);
835 now_tm = localtime(&now);
836 memcpy(&response->am[0].u.s.tm, now_tm, sizeof(struct tm));
838 SCPY(response->am[0].u.s.logfile, options.log);
839 /* interface count */
841 interface = interface_first;
843 ifport = interface->ifport;
846 ifport = ifport->next;
848 interface = interface->next;
850 response->am[0].u.s.interfaces = i;
851 /* remote connection count */
855 if (admin->remote_name[0])
859 response->am[0].u.s.remotes = i;
867 response->am[0].u.s.joins = i;
869 apppbx = apppbx_first;
873 apppbx = apppbx->next;
875 response->am[0].u.s.epoints = i;
883 response->am[0].u.s.ports = i;
884 /* attach to response chain */
885 *responsep = response;
886 responsep = &response->next;
888 /* create response for all instances */
889 num = (response->am[0].u.s.interfaces)
890 + (response->am[0].u.s.remotes)
891 + (response->am[0].u.s.joins)
892 + (response->am[0].u.s.epoints)
893 + (response->am[0].u.s.ports);
896 response = (struct admin_queue *)MALLOC(sizeof(admin_queue)+(num*sizeof(admin_message)));
899 *responsep = response;
900 responsep = &response->next;
901 interface = interface_first;
904 ifport = interface->ifport;
907 response->am[num].message = ADMIN_RESPONSE_S_INTERFACE;
909 SCPY(response->am[num].u.i.interface_name, interface->name);
911 response->am[num].u.i.portnum = ifport->portnum;
913 SCPY(response->am[num].u.i.portname, ifport->portname);
915 response->am[num].u.i.extension = interface->extension;
917 response->am[num].u.i.block = ifport->block;
918 if (ifport->mISDNport) {
919 mISDNport = ifport->mISDNport;
922 response->am[num].u.i.ptp = mISDNport->ptp;
924 response->am[num].u.i.l1hold = mISDNport->l1hold;
926 response->am[num].u.i.l2hold = mISDNport->l2hold;
928 response->am[num].u.i.ntmode = mISDNport->ntmode;
930 response->am[num].u.i.pri = mISDNport->pri;
932 response->am[num].u.i.use = mISDNport->use;
934 response->am[num].u.i.l1link = mISDNport->l1link;
936 response->am[num].u.i.l2link = mISDNport->l2link;
937 memcpy(response->am[num].u.i.l2mask, mISDNport->l2mask, 16);
939 response->am[num].u.i.los = mISDNport->los;
941 response->am[num].u.i.ais = mISDNport->ais;
943 response->am[num].u.i.rdi = mISDNport->rdi;
945 response->am[num].u.i.slip_tx = mISDNport->slip_tx;
946 response->am[num].u.i.slip_rx = mISDNport->slip_rx;
948 response->am[num].u.i.channels = mISDNport->b_num;
949 /* channel selection */
950 selchannel = ifport->out_channel;
951 if (ifport->channel_force)
952 SCAT(response->am[num].u.i.out_channel, "force");
954 if (response->am[num].u.i.out_channel[0])
955 SCAT(response->am[num].u.i.out_channel, ",");
956 switch (selchannel->channel) {
958 SCAT(response->am[num].u.i.out_channel, "no");
961 SCAT(response->am[num].u.i.out_channel, "any");
964 SCAT(response->am[num].u.i.out_channel, "free");
967 SPRINT(strchr(response->am[num].u.i.out_channel, '\0'), "%d", selchannel->channel);
969 selchannel = selchannel->next;
971 selchannel = ifport->in_channel;
973 switch (selchannel->channel) {
975 SCAT(response->am[num].u.i.in_channel, "free");
978 SPRINT(strchr(response->am[num].u.i.in_channel, '\0'), "%d", selchannel->channel);
980 selchannel = selchannel->next;
985 while(i < mISDNport->b_num) {
986 response->am[num].u.i.busy[i] = mISDNport->b_state[i];
987 if (mISDNport->b_port[i])
988 response->am[num].u.i.port[i] = mISDNport->b_port[i]->p_serial;
989 response->am[num].u.i.mode[i] = mISDNport->b_mode[i];
995 ifport = ifport->next;
997 interface = interface->next;
1000 /* create response for all remotes */
1001 admin = admin_first;
1003 if (admin->remote_name[0]) {
1005 response->am[num].message = ADMIN_RESPONSE_S_REMOTE;
1007 SCPY(response->am[num].u.r.name, admin->remote_name);
1011 admin = admin->next;
1014 /* create response for all joins */
1018 response->am[num].message = ADMIN_RESPONSE_S_JOIN;
1020 response->am[num].u.j.serial = join->j_serial;
1022 if (join->j_type == JOIN_TYPE_PBX)
1023 response->am[num].u.j.partyline = ((class JoinPBX *)join)->j_partyline;
1024 /* remote application */
1025 if (join->j_type == JOIN_TYPE_REMOTE)
1026 SCPY(response->am[num].u.j.remote, ((class JoinRemote *)join)->j_remote_name);
1032 /* create response for all endpoint */
1033 apppbx = apppbx_first;
1036 response->am[num].message = ADMIN_RESPONSE_S_EPOINT;
1038 response->am[num].u.e.serial = apppbx->ea_endpoint->ep_serial;
1040 response->am[num].u.e.join = apppbx->ea_endpoint->ep_join_id;
1041 /* rx notification */
1042 response->am[num].u.e.rx_state = apppbx->e_rx_state;
1043 /* tx notification */
1044 response->am[num].u.e.tx_state = apppbx->e_tx_state;
1046 switch(apppbx->e_state) {
1047 case EPOINT_STATE_IN_SETUP:
1048 response->am[num].u.e.state = ADMIN_STATE_IN_SETUP;
1050 case EPOINT_STATE_OUT_SETUP:
1051 response->am[num].u.e.state = ADMIN_STATE_OUT_SETUP;
1053 case EPOINT_STATE_IN_OVERLAP:
1054 response->am[num].u.e.state = ADMIN_STATE_IN_OVERLAP;
1056 case EPOINT_STATE_OUT_OVERLAP:
1057 response->am[num].u.e.state = ADMIN_STATE_OUT_OVERLAP;
1059 case EPOINT_STATE_IN_PROCEEDING:
1060 response->am[num].u.e.state = ADMIN_STATE_IN_PROCEEDING;
1062 case EPOINT_STATE_OUT_PROCEEDING:
1063 response->am[num].u.e.state = ADMIN_STATE_OUT_PROCEEDING;
1065 case EPOINT_STATE_IN_ALERTING:
1066 response->am[num].u.e.state = ADMIN_STATE_IN_ALERTING;
1068 case EPOINT_STATE_OUT_ALERTING:
1069 response->am[num].u.e.state = ADMIN_STATE_OUT_ALERTING;
1071 case EPOINT_STATE_CONNECT:
1072 response->am[num].u.e.state = ADMIN_STATE_CONNECT;
1074 case EPOINT_STATE_IN_DISCONNECT:
1075 response->am[num].u.e.state = ADMIN_STATE_IN_DISCONNECT;
1077 case EPOINT_STATE_OUT_DISCONNECT:
1078 response->am[num].u.e.state = ADMIN_STATE_OUT_DISCONNECT;
1081 response->am[num].u.e.state = ADMIN_STATE_IDLE;
1084 SCPY(response->am[num].u.e.terminal, apppbx->e_ext.number);
1086 SCPY(response->am[num].u.e.callerid, apppbx->e_callerinfo.id);
1088 SCPY(response->am[num].u.e.dialing, apppbx->e_dialinginfo.id);
1090 if (apppbx->e_action)
1091 SCPY(response->am[num].u.e.action, action_defs[apppbx->e_action->index].name);
1092 // if (apppbx->e_action)
1093 // printf("action=%s\n",action_defs[apppbx->e_action->index].name);
1095 response->am[num].u.e.park = apppbx->ea_endpoint->ep_park;
1096 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))
1097 memcpy(response->am[num].u.e.park_callid, apppbx->ea_endpoint->ep_park_callid, apppbx->ea_endpoint->ep_park_len);
1098 response->am[num].u.e.park_len = apppbx->ea_endpoint->ep_park_len;
1100 if (apppbx->e_crypt == CRYPT_ON)
1101 response->am[num].u.e.crypt = 1;
1103 apppbx = apppbx->next;
1107 /* create response for all ports */
1111 response->am[num].message = ADMIN_RESPONSE_S_PORT;
1113 response->am[num].u.p.serial = port->p_serial;
1115 SCPY(response->am[num].u.p.name, port->p_name);
1117 response->am[num].u.p.epoint = ACTIVE_EPOINT(port->p_epointlist);
1119 switch(port->p_state) {
1120 case PORT_STATE_IN_SETUP:
1121 response->am[num].u.p.state = ADMIN_STATE_IN_SETUP;
1123 case PORT_STATE_OUT_SETUP:
1124 response->am[num].u.p.state = ADMIN_STATE_OUT_SETUP;
1126 case PORT_STATE_IN_OVERLAP:
1127 response->am[num].u.p.state = ADMIN_STATE_IN_OVERLAP;
1129 case PORT_STATE_OUT_OVERLAP:
1130 response->am[num].u.p.state = ADMIN_STATE_OUT_OVERLAP;
1132 case PORT_STATE_IN_PROCEEDING:
1133 response->am[num].u.p.state = ADMIN_STATE_IN_PROCEEDING;
1135 case PORT_STATE_OUT_PROCEEDING:
1136 response->am[num].u.p.state = ADMIN_STATE_OUT_PROCEEDING;
1138 case PORT_STATE_IN_ALERTING:
1139 response->am[num].u.p.state = ADMIN_STATE_IN_ALERTING;
1141 case PORT_STATE_OUT_ALERTING:
1142 response->am[num].u.p.state = ADMIN_STATE_OUT_ALERTING;
1144 case PORT_STATE_CONNECT:
1145 response->am[num].u.p.state = ADMIN_STATE_CONNECT;
1147 case PORT_STATE_IN_DISCONNECT:
1148 response->am[num].u.p.state = ADMIN_STATE_IN_DISCONNECT;
1150 case PORT_STATE_OUT_DISCONNECT:
1151 response->am[num].u.p.state = ADMIN_STATE_OUT_DISCONNECT;
1153 case PORT_STATE_RELEASE:
1154 response->am[num].u.p.state = ADMIN_STATE_RELEASE;
1157 response->am[num].u.p.state = ADMIN_STATE_IDLE;
1160 if ((port->p_type & PORT_CLASS_mISDN_MASK) == PORT_CLASS_DSS1) {
1161 response->am[num].u.p.isdn = 1;
1162 pdss1 = (class Pdss1 *)port;
1163 response->am[num].u.p.isdn_chan = pdss1->p_m_b_channel;
1164 response->am[num].u.p.isdn_hold = pdss1->p_m_hold;
1165 response->am[num].u.p.isdn_ces = pdss1->p_m_d_ces;
1174 int sockserial = 1; // must start with 1, because 0 is used if no serial is set
1176 * handle admin socket (non blocking)
1178 int admin_handle_con(struct lcr_fd *fd, unsigned int what, void *instance, int index);
1180 int admin_handle(struct lcr_fd *fd, unsigned int what, void *instance, int index)
1183 socklen_t sock_len = sizeof(sock_address);
1184 struct admin_list *admin;
1186 /* check for new incoming connections */
1187 if ((new_sock = accept(sock, (struct sockaddr *)&sock_address, &sock_len)) >= 0) {
1188 /* insert new socket */
1189 admin = (struct admin_list *)MALLOC(sizeof(struct admin_list));
1192 admin->sockserial = sockserial++;
1193 admin->next = admin_first;
1194 admin_first = admin;
1195 admin->sock = new_sock;
1196 admin->fd.fd = new_sock;
1197 register_fd(&admin->fd, LCR_FD_READ | LCR_FD_EXCEPT, admin_handle_con, admin, 0);
1199 if (errno != EWOULDBLOCK) {
1200 PERROR("Failed to accept connection from socket \"%s\". (errno=%d) Closing socket.\n", sock_address.sun_path, errno);
1209 int admin_handle_con(struct lcr_fd *fd, unsigned int what, void *instance, int index)
1211 struct admin_list *admin = (struct admin_list *)instance;
1213 struct admin_message msg;
1215 struct Endpoint *epoint;
1217 if ((what & LCR_FD_READ)) {
1219 len = read(admin->sock, &msg, sizeof(msg));
1222 PDEBUG(DEBUG_LOG, "Broken pipe on socket %d. (errno=%d).\n", admin->sock, errno);
1223 free_connection(admin);
1229 /*release endpoint if exists */
1230 if (admin->epointid) {
1231 epoint = find_epoint_id(admin->epointid);
1233 ((class DEFAULT_ENDPOINT_APP *)epoint->ep_app)->
1234 release(RELEASE_ALL, LOCATION_PRIVATE_LOCAL, CAUSE_NORMAL, LOCATION_PRIVATE_LOCAL, CAUSE_NORMAL, 0);
1238 free_connection(admin);
1241 if (len != sizeof(msg)) {
1242 PERROR("Short/long read on socket %d. (len=%d != size=%d).\n", admin->sock, len, sizeof(msg));
1243 free_connection(admin);
1246 /* process socket command */
1247 if (admin->response && msg.message != ADMIN_MESSAGE) {
1248 PERROR("Data from socket %d while sending response.\n", admin->sock);
1249 free_connection(admin);
1252 switch (msg.message) {
1253 case ADMIN_REQUEST_CMD_INTERFACE:
1254 if (admin_interface(&admin->response) < 0) {
1255 PERROR("Failed to create dial response for socket %d.\n", admin->sock);
1256 goto response_error;
1258 admin->fd.when |= LCR_FD_WRITE;
1261 case ADMIN_REQUEST_CMD_ROUTE:
1262 if (admin_route(&admin->response) < 0) {
1263 PERROR("Failed to create dial response for socket %d.\n", admin->sock);
1264 goto response_error;
1266 admin->fd.when |= LCR_FD_WRITE;
1269 case ADMIN_REQUEST_CMD_DIAL:
1270 if (admin_dial(&admin->response, msg.u.x.message) < 0) {
1271 PERROR("Failed to create dial response for socket %d.\n", admin->sock);
1272 goto response_error;
1274 admin->fd.when |= LCR_FD_WRITE;
1277 case ADMIN_REQUEST_CMD_RELEASE:
1278 if (admin_release(&admin->response, msg.u.x.message) < 0) {
1279 PERROR("Failed to create release response for socket %d.\n", admin->sock);
1280 goto response_error;
1282 admin->fd.when |= LCR_FD_WRITE;
1285 case ADMIN_REQUEST_STATE:
1286 if (admin_state(&admin->response) < 0) {
1287 PERROR("Failed to create state response for socket %d.\n", admin->sock);
1288 goto response_error;
1290 admin->fd.when |= LCR_FD_WRITE;
1293 case ADMIN_TRACE_REQUEST:
1294 if (admin_trace(admin, &msg.u.trace_req) < 0) {
1295 PERROR("Failed to create trace response for socket %d.\n", admin->sock);
1296 goto response_error;
1298 admin->fd.when |= LCR_FD_WRITE;
1301 case ADMIN_REQUEST_CMD_BLOCK:
1302 if (admin_block(&admin->response, msg.u.x.portnum, msg.u.x.block) < 0) {
1303 PERROR("Failed to create block response for socket %d.\n", admin->sock);
1304 goto response_error;
1306 admin->fd.when |= LCR_FD_WRITE;
1310 if (admin_message_to_lcr(&msg.u.msg, admin) < 0) {
1311 PERROR("Failed to deliver message for socket %d.\n", admin->sock);
1312 goto response_error;
1316 case ADMIN_CALL_SETUP:
1317 if (admin_call(admin, &msg) < 0) {
1318 PERROR("Failed to create call for socket %d.\n", admin->sock);
1320 free_connection(admin);
1326 PERROR("Invalid message %d from socket %d.\n", msg.message, admin->sock);
1327 free_connection(admin);
1332 if ((what & LCR_FD_WRITE)) {
1334 if (admin->response) {
1335 len = write(admin->sock, ((unsigned char *)(admin->response->am))+admin->response->offset, sizeof(struct admin_message)*(admin->response->num)-admin->response->offset);
1341 if (len < (int)(sizeof(struct admin_message)*(admin->response->num) - admin->response->offset)) {
1342 admin->response->offset+=len;
1345 temp = admin->response;
1346 admin->response = admin->response->next;
1351 admin->fd.when &= ~LCR_FD_WRITE;
1357 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)
1359 union parameter param;
1361 memset(¶m, 0, sizeof(union parameter));
1362 param.bchannel.isloopback = isloopback;
1363 param.bchannel.type = type;
1364 param.bchannel.handle = handle;
1365 param.bchannel.tx_gain = tx_gain;
1366 param.bchannel.rx_gain = rx_gain;
1368 SCPY(param.bchannel.pipeline, pipeline);
1370 memcpy(param.bchannel.crypt, crypt, crypt_len);
1371 param.bchannel.crypt_type = crypt_type;
1372 if (admin_message_from_lcr(remote_id, ref, MESSAGE_BCHANNEL, ¶m)<0) {
1373 PERROR("No socket with remote id %d found, this happens, if the socket is closed before all bchannels are imported.\n", remote_id);