1 /*****************************************************************************\
3 ** Linux Call Router **
5 **---------------------------------------------------------------------------**
6 ** Copyright: Andreas Eversberg **
8 ** Socket link server **
10 \*****************************************************************************/
13 #include <sys/socket.h>
16 #ifdef PACKAGE_VERSION
17 #undef PACKAGE_VERSION
22 char socket_name[128];
24 struct sockaddr_un sock_address;
26 struct admin_list *admin_first = NULL;
27 static struct lcr_fd admin_fd;
29 int admin_handle(struct lcr_fd *fd, unsigned int what, void *instance, int index);
32 * initialize admin socket
36 /* open and bind socket */
37 if ((sock = socket(PF_UNIX, SOCK_STREAM, 0)) < 0) {
38 PERROR("Failed to create admin socket. (errno=%d)\n", errno);
42 memset(&sock_address, 0, sizeof(sock_address));
43 SPRINT(socket_name, SOCKET_NAME, options.lock);
44 sock_address.sun_family = AF_UNIX;
45 UCPY(sock_address.sun_path, socket_name);
47 if (bind(sock, (struct sockaddr *)(&sock_address), SUN_LEN(&sock_address)) < 0) {
52 PERROR("Failed to bind admin socket to \"%s\". (errno=%d)\n", sock_address.sun_path, errno);
55 if (listen(sock, 5) < 0) {
60 PERROR("Failed to listen to socket \"%s\". (errno=%d)\n", sock_address.sun_path, errno);
63 memset(&admin_fd, 0, sizeof(admin_fd));
65 register_fd(&admin_fd, LCR_FD_READ | LCR_FD_EXCEPT, admin_handle, NULL, 0);
66 if (chmod(socket_name, options.socketrights) < 0) {
67 PERROR("Failed to change socket rights to %d. (errno=%d)\n", options.socketrights, errno);
69 if (chown(socket_name, options.socketuser, options.socketgroup) < 0) {
70 PERROR("Failed to change socket user/group to %d/%d. (errno=%d)\n", options.socketuser, options.socketgroup, errno);
80 void free_connection(struct admin_list *admin)
82 struct admin_queue *response;
84 union parameter param;
85 class Port *port, *portnext;
86 class Premote *remote;
87 struct admin_list **adminp;
89 /* free remote ports */
90 if (admin->remote_name[0]) {
98 "REMOTE APP release");
99 add_trace("app", "name", "%s", admin->remote_name);
101 /* release remote port */
104 portnext = port->next;
105 if ((port->p_type & PORT_CLASS_MASK) == PORT_CLASS_REMOTE) {
106 remote = (class Premote *) port;
107 if (remote->p_r_remote_id == admin->sock) {
108 memset(¶m, 0, sizeof(param));
109 param.disconnectinfo.cause = CAUSE_OUTOFORDER;
110 param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
111 remote->message_remote(MESSAGE_RELEASE, ¶m);
112 /* port is now destroyed, so we go to next join */
119 if (admin->sock >= 0) {
120 unregister_fd(&admin->fd);
124 response = admin->response;
126 temp = response->next;
129 response = (struct admin_queue *)temp;
132 adminp = &admin_first;
134 if (*adminp == admin)
136 adminp = &((*adminp)->next);
139 *adminp = (*adminp)->next;
147 * cleanup admin socket
149 void admin_cleanup(void)
151 struct admin_list *admin, *next;;
156 free_connection(admin);
161 unregister_fd(&admin_fd);
171 * do interface reload
173 int admin_interface(struct admin_queue **responsep)
175 struct admin_queue *response; /* response pointer */
176 const char *err_txt = "";
179 if (read_interfaces()) {
181 free_interfaces(interface_first);
182 interface_first = interface_newlist;
183 interface_newlist = NULL;
185 err_txt = interface_error;
188 /* create state response */
189 response = (struct admin_queue *)MALLOC(sizeof(struct admin_queue)+sizeof(admin_message));
193 response->am[0].message = ADMIN_RESPONSE_CMD_INTERFACE;
195 response->am[0].u.x.error = err;
197 SCPY(response->am[0].u.x.message, err_txt);
198 /* attach to response chain */
199 *responsep = response;
200 responsep = &response->next;
208 int admin_route(struct admin_queue **responsep)
210 struct route_ruleset *ruleset_new;
211 struct admin_queue *response; /* response pointer */
212 char err_txt[256] = "";
217 class EndpointAppPBX *apppbx;
221 apppbx = apppbx_first;
224 apppbx = apppbx->next;
227 SPRINT(err_txt, "Cannot reload routing, because %d endpoints active\n", n);
232 if (!(ruleset_new = ruleset_parse())) {
233 SPRINT(err_txt, ruleset_error);
237 ruleset_free(ruleset_first);
238 ruleset_first = ruleset_new;
239 ruleset_main = getrulesetbyname("main");
241 SPRINT(err_txt, "Ruleset reloaded, but rule 'main' not found.\n");
244 apppbx = apppbx_first;
246 if (apppbx->e_action) {
247 switch(apppbx->e_action->index) {
248 case ACTION_INTERNAL:
249 apppbx->e_action = &action_internal;
251 case ACTION_EXTERNAL:
252 apppbx->e_action = &action_external;
254 case ACTION_VBOX_RECORD:
255 apppbx->e_action = &action_vbox;
257 case ACTION_PARTYLINE:
258 apppbx->e_action = &action_partyline;
263 } else if (apppbx->e_state != EPOINT_STATE_CONNECT) {
265 unsched_timer(&apppbx->e_callback_timeout);
266 apppbx->e_action = NULL;
267 apppbx->release(RELEASE_ALL, LOCATION_PRIVATE_LOCAL, CAUSE_NORMAL, LOCATION_PRIVATE_LOCAL, CAUSE_NORMAL, 0);
270 numberrize_callerinfo(apppbx->e_callerinfo.id, apppbx->e_callerinfo.ntype, options.national, options.international),
271 apppbx->e_dialinginfo.id,
274 apppbx->ea_endpoint->ep_serial,
275 "KICK (reload routing)");
279 unsched_timer(&apppbx->e_action_timeout);
280 apppbx->e_rule = NULL;
281 apppbx->e_ruleset = NULL;
283 apppbx = apppbx->next;
287 /* create state response */
288 response = (struct admin_queue *)MALLOC(sizeof(struct admin_queue)+sizeof(admin_message));
292 response->am[0].message = ADMIN_RESPONSE_CMD_ROUTE;
294 response->am[0].u.x.error = err;
296 SCPY(response->am[0].u.x.message, err_txt);
297 /* attach to response chain */
298 *responsep = response;
299 responsep = &response->next;
307 int admin_dial(struct admin_queue **responsep, char *message)
309 struct extension ext; /* temporary extension's settings */
310 struct admin_queue *response; /* response pointer */
311 char *p; /* pointer to dialing digits */
313 /* create state response */
314 response = (struct admin_queue *)MALLOC(sizeof(struct admin_queue)+sizeof(admin_message));
318 response->am[0].message = ADMIN_RESPONSE_CMD_DIAL;
320 /* process request */
321 if (!(p = strchr(message,':'))) {
322 response->am[0].u.x.error = -EINVAL;
323 SPRINT(response->am[0].u.x.message, "no seperator ':' in message to seperate number from extension");
328 /* modify extension */
329 if (!read_extension(&ext, message)) {
330 response->am[0].u.x.error = -EINVAL;
331 SPRINT(response->am[0].u.x.message, "extension doesn't exist");
335 write_extension(&ext, message);
338 /* attach to response chain */
339 *responsep = response;
340 responsep = &response->next;
348 int admin_trace(struct admin_list *admin, struct admin_trace_req *trace)
350 memcpy(&admin->trace, trace, sizeof(struct admin_trace_req));
358 * 0 = make port available
359 * 1 = make port administratively blocked
361 * the result is returned:
362 * 0 = port is now available
363 * 1 = port is now blocked
364 * 2 = port cannot be loaded or has been unloaded
365 * -1 = port doesn't exist
367 int admin_block(struct admin_queue **responsep, int portnum, int block)
369 struct admin_queue *response; /* response pointer */
370 struct interface *interface;
371 struct interface_port *ifport;
373 /* create block response */
374 response = (struct admin_queue *)MALLOC(sizeof(struct admin_queue)+sizeof(admin_message));
378 response->am[0].message = ADMIN_RESPONSE_CMD_BLOCK;
379 response->am[0].u.x.portnum = portnum;
381 /* search for port */
383 interface = interface_first;
385 ifport = interface->ifport;
387 if (ifport->portnum == portnum)
389 ifport = ifport->next;
393 interface = interface->next;
395 /* not found, we return -1 */
397 response->am[0].u.x.block = -1;
398 response->am[0].u.x.error = 1;
399 SPRINT(response->am[0].u.x.message, "Port %d does not exist.", portnum);
405 if (!ifport->mISDNport) {
406 /* not loaded anyway */
408 response->am[0].u.x.block = 2;
412 /* try loading interface */
413 ifport->block = block;
414 load_mISDN_port(ifport);
416 /* port cannot load */
417 if (ifport->block >= 2) {
418 response->am[0].u.x.block = 2;
419 response->am[0].u.x.error = 1;
420 SPRINT(response->am[0].u.x.message, "Port %d will not load.", portnum);
425 response->am[0].u.x.block = ifport->block;
429 /* if we shall unload interface */
431 mISDNport_close(ifport->mISDNport);
432 ifport->mISDNport = 0;
438 /* port new blocking state */
439 ifport->block = response->am[0].u.x.block = block;
442 /* attach to response chain */
443 *responsep = response;
444 responsep = &response->next;
452 int admin_release(struct admin_queue **responsep, char *message)
455 struct admin_queue *response; /* response pointer */
456 class EndpointAppPBX *apppbx;
458 /* create state response */
459 response = (struct admin_queue *)MALLOC(sizeof(struct admin_queue)+sizeof(admin_message));
463 response->am[0].message = ADMIN_RESPONSE_CMD_RELEASE;
466 apppbx = apppbx_first;
468 if (apppbx->ea_endpoint->ep_serial == id)
470 apppbx = apppbx->next;
473 response->am[0].u.x.error = -EINVAL;
474 SPRINT(response->am[0].u.x.message, "Given endpoint %d doesn't exist.", id);
478 unsched_timer(&apppbx->e_callback_timeout);
479 apppbx->release(RELEASE_ALL, LOCATION_PRIVATE_LOCAL, CAUSE_NORMAL, LOCATION_PRIVATE_LOCAL, CAUSE_NORMAL, 0);
482 /* attach to response chain */
483 *responsep = response;
484 responsep = &response->next;
492 int admin_call(struct admin_list *admin, struct admin_message *msg)
494 class Endpoint *epoint;
495 class EndpointAppPBX *apppbx;
497 if (!(epoint = new Endpoint(0, 0)))
498 FATAL("No memory for Endpoint instance\n");
499 if (!(epoint->ep_app = apppbx = new EndpointAppPBX(epoint, 1))) // outgoing
500 FATAL("No memory for Endpoint Application instance\n");
501 apppbx->e_adminid = admin->sockserial;
502 admin->epointid = epoint->ep_serial;
503 SCPY(apppbx->e_callerinfo.id, nationalize_callerinfo(msg->u.call.callerid, &apppbx->e_callerinfo.ntype, options.national, options.international));
504 if (msg->u.call.present)
505 apppbx->e_callerinfo.present = INFO_PRESENT_ALLOWED;
507 apppbx->e_callerinfo.present = INFO_PRESENT_RESTRICTED;
508 apppbx->e_callerinfo.screen = INFO_SCREEN_NETWORK;
511 apppbx->e_capainfo.bearer_capa = msg->u.call.bc_capa;
512 apppbx->e_capainfo.bearer_mode = msg->u.call.bc_mode;
513 apppbx->e_capainfo.bearer_info1 = msg->u.call.bc_info1;
514 apppbx->e_capainfo.hlc = msg->u.call.hlc;
515 apppbx->e_capainfo.exthlc = msg->u.call.exthlc;
516 SCPY(apppbx->e_dialinginfo.id, msg->u.call.dialing);
517 SCPY(apppbx->e_dialinginfo.interfaces, msg->u.call.interface);
518 apppbx->e_dialinginfo.sending_complete = 1;
520 apppbx->new_state(PORT_STATE_OUT_SETUP);
521 apppbx->out_setup(0);
527 * this function is called for response whenever a call state changes.
529 void admin_call_response(int adminid, int message, const char *connected, int cause, int location, int notify_progress)
531 struct admin_list *admin;
532 struct admin_queue *response, **responsep; /* response pointer */
534 /* searching for admin id
535 * maybe there is no admin instance, because the calling port was not
536 * initiated by admin_call */
539 if (adminid == admin->sockserial)
546 /* seek to end of response list */
547 response = admin->response;
548 responsep = &admin->response;
550 responsep = &response->next;
551 response = response->next;
554 /* create state response */
555 response = (struct admin_queue *)MALLOC(sizeof(struct admin_queue)+sizeof(admin_message));
559 response->am[0].message = message;
561 SCPY(response->am[0].u.call.callerid, connected);
562 response->am[0].u.call.cause = cause;
563 response->am[0].u.call.location = location;
564 response->am[0].u.call.notify_progress = notify_progress;
566 /* attach to response chain */
567 *responsep = response;
568 responsep = &response->next;
569 admin->fd.when |= LCR_FD_WRITE;
574 * send data to the remote socket join instance
576 int admin_message_to_lcr(struct admin_msg *msg, struct admin_list *admin)
579 class Premote *remote = NULL; /* make GCC happy */
580 struct interface *interface;
581 struct admin_list *temp;
584 if (msg->type == MESSAGE_HELLO) {
585 if (admin->remote_name[0]) {
586 PERROR("Remote application repeats hello message.\n");
589 /* look for second application */
592 if (!strcmp(temp->remote_name, msg->param.hello.application))
597 PERROR("Remote application connects twice??? (ignoring)\n");
600 /* set remote socket instance */
601 SCPY(admin->remote_name, msg->param.hello.application);
609 "REMOTE APP registers");
610 add_trace("app", "name", "%s", admin->remote_name);
615 /* check we have no application name */
616 if (!admin->remote_name[0]) {
617 PERROR("Remote application did not send us a hello message.\n");
621 /* new join. the reply (NEWREF assignment) is sent from constructor */
622 if (msg->type == MESSAGE_NEWREF) {
624 /* find remote port */
625 interface = interface_first;
627 if (interface->remote && !strcmp(interface->remote_app, admin->remote_name))
629 interface = interface->next;
632 union parameter param;
634 memset(¶m, 0, sizeof(union parameter));
635 param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
636 param.disconnectinfo.cause = CAUSE_RESSOURCEUNAVAIL;
637 admin_message_from_lcr(admin->sock, 0, MESSAGE_RELEASE, ¶m);
640 /* creating port object, transparent until setup with hdlc */
641 SPRINT(name, "%s-%s-in", interface->name, interface->remote_app);
642 if (!(remote = new Premote(PORT_TYPE_REMOTE_IN, name, NULL, interface, admin->sock)))
644 FATAL("Cannot create Port instance.\n");
650 PERROR("Remote application did not send us a valid ref with a message.\n");
654 /* find port instance */
657 if ((port->p_type & PORT_CLASS_mISDN_MASK) == PORT_CLASS_REMOTE) {
658 remote = (class Premote *) port;
659 if (remote->p_r_ref == msg->ref)
665 if (admin->sock != remote->p_r_remote_id) {
666 PERROR("Ref %d belongs to remote application %s, but not to sending application %s.\n", msg->ref, remote->p_r_remote_app, admin->remote_name);
671 remote->message_remote(msg->type, &msg->param);
676 PDEBUG(DEBUG_LOG, "No remote instance found with ref %d. (May have been already released.)\n", msg->ref);
683 * this function is called for every message to remote socket
685 int admin_message_from_lcr(int remote_id, unsigned int ref, int message_type, union parameter *param)
687 struct admin_list *admin;
688 struct admin_queue **responsep; /* response pointer */
690 /* searching for admin id
691 * maybe there is no given remote application
695 if (admin->remote_name[0] && admin->sock==remote_id)
699 /* no given remote application connected */
703 /* seek to end of response list */
704 responsep = &admin->response;
706 responsep = &(*responsep)->next;
709 /* create state response */
710 *responsep = (struct admin_queue *)MALLOC(sizeof(struct admin_queue)+sizeof(admin_message));
712 (*responsep)->num = 1;
715 (*responsep)->am[0].message = ADMIN_MESSAGE;
716 (*responsep)->am[0].u.msg.type = message_type;
717 (*responsep)->am[0].u.msg.ref = ref;
718 memcpy(&(*responsep)->am[0].u.msg.param, param, sizeof(union parameter));
719 admin->fd.when |= LCR_FD_WRITE;
727 int admin_state(struct admin_queue **responsep)
730 class EndpointAppPBX *apppbx;
734 struct mISDNport *mISDNport;
735 struct select_channel *selchannel;
738 struct interface *interface;
739 struct interface_port *ifport;
742 struct admin_queue *response;
743 struct admin_list *admin;
747 /* create state response */
748 response = (struct admin_queue *)MALLOC(sizeof(struct admin_queue)+sizeof(admin_message));
752 response->am[0].message = ADMIN_RESPONSE_STATE;
754 SCPY(response->am[0].u.s.version_string, VERSION_STRING);
757 now_tm = localtime(&now);
758 memcpy(&response->am[0].u.s.tm, now_tm, sizeof(struct tm));
760 SCPY(response->am[0].u.s.logfile, options.log);
761 /* interface count */
763 interface = interface_first;
765 ifport = interface->ifport;
770 ifport = ifport->next;
772 interface = interface->next;
774 response->am[0].u.s.interfaces = i;
775 /* remote connection count */
779 if (admin->remote_name[0])
783 response->am[0].u.s.remotes = i;
791 response->am[0].u.s.joins = i;
793 apppbx = apppbx_first;
797 apppbx = apppbx->next;
799 response->am[0].u.s.epoints = i;
807 response->am[0].u.s.ports = i;
808 /* attach to response chain */
809 *responsep = response;
810 responsep = &response->next;
812 /* create response for all instances */
813 num = (response->am[0].u.s.interfaces)
814 + (response->am[0].u.s.remotes)
815 + (response->am[0].u.s.joins)
816 + (response->am[0].u.s.epoints)
817 + (response->am[0].u.s.ports);
820 response = (struct admin_queue *)MALLOC(sizeof(admin_queue)+(num*sizeof(admin_message)));
823 *responsep = response;
824 responsep = &response->next;
825 interface = interface_first;
828 ifport = interface->ifport;
831 response->am[num].message = ADMIN_RESPONSE_S_INTERFACE;
833 SCPY(response->am[num].u.i.interface_name, interface->name);
835 response->am[num].u.i.portnum = -100; /* indicate: no ifport */
837 response->am[num].u.i.extension = interface->extension;
843 response->am[num].message = ADMIN_RESPONSE_S_INTERFACE;
845 SCPY(response->am[num].u.i.interface_name, interface->name);
847 response->am[num].u.i.portnum = ifport->portnum;
849 SCPY(response->am[num].u.i.portname, ifport->portname);
851 response->am[num].u.i.extension = interface->extension;
853 response->am[num].u.i.block = ifport->block;
855 if (ifport->mISDNport) {
856 mISDNport = ifport->mISDNport;
859 response->am[num].u.i.ptp = mISDNport->ptp;
861 response->am[num].u.i.l1hold = mISDNport->l1hold;
863 response->am[num].u.i.l2hold = mISDNport->l2hold;
865 response->am[num].u.i.ntmode = mISDNport->ntmode;
867 response->am[num].u.i.pri = mISDNport->pri;
869 response->am[num].u.i.use = mISDNport->use;
871 response->am[num].u.i.l1link = mISDNport->l1link;
873 response->am[num].u.i.l2link = mISDNport->l2link;
874 memcpy(response->am[num].u.i.l2mask, mISDNport->l2mask, 16);
876 response->am[num].u.i.los = mISDNport->los;
878 response->am[num].u.i.ais = mISDNport->ais;
880 response->am[num].u.i.rdi = mISDNport->rdi;
882 response->am[num].u.i.slip_tx = mISDNport->slip_tx;
883 response->am[num].u.i.slip_rx = mISDNport->slip_rx;
885 response->am[num].u.i.channels = mISDNport->b_num;
886 /* channel selection */
887 selchannel = ifport->out_channel;
888 if (ifport->channel_force)
889 SCAT(response->am[num].u.i.out_channel, "force");
891 if (response->am[num].u.i.out_channel[0])
892 SCAT(response->am[num].u.i.out_channel, ",");
893 switch (selchannel->channel) {
895 SCAT(response->am[num].u.i.out_channel, "no");
898 SCAT(response->am[num].u.i.out_channel, "any");
901 SCAT(response->am[num].u.i.out_channel, "free");
904 SPRINT(strchr(response->am[num].u.i.out_channel, '\0'), "%d", selchannel->channel);
906 selchannel = selchannel->next;
908 selchannel = ifport->in_channel;
910 switch (selchannel->channel) {
912 SCAT(response->am[num].u.i.in_channel, "free");
915 SPRINT(strchr(response->am[num].u.i.in_channel, '\0'), "%d", selchannel->channel);
917 selchannel = selchannel->next;
922 while(i < mISDNport->b_num) {
923 response->am[num].u.i.busy[i] = mISDNport->b_state[i];
924 if (mISDNport->b_port[i])
925 response->am[num].u.i.port[i] = mISDNport->b_port[i]->p_serial;
926 response->am[num].u.i.mode[i] = mISDNport->b_mode[i];
933 ifport = ifport->next;
935 interface = interface->next;
938 /* create response for all remotes */
941 if (admin->remote_name[0]) {
943 response->am[num].message = ADMIN_RESPONSE_S_REMOTE;
945 SCPY(response->am[num].u.r.name, admin->remote_name);
952 /* create response for all joins */
956 response->am[num].message = ADMIN_RESPONSE_S_JOIN;
958 response->am[num].u.j.serial = join->j_serial;
960 if (join->j_type == JOIN_TYPE_PBX) {
961 response->am[num].u.j.partyline = ((class JoinPBX *)join)->j_partyline;
962 response->am[num].u.j.threepty = ((class JoinPBX *)join)->j_3pty;
969 /* create response for all endpoint */
970 apppbx = apppbx_first;
973 response->am[num].message = ADMIN_RESPONSE_S_EPOINT;
975 response->am[num].u.e.serial = apppbx->ea_endpoint->ep_serial;
977 response->am[num].u.e.join = apppbx->ea_endpoint->ep_join_id;
978 /* rx notification */
979 response->am[num].u.e.rx_state = apppbx->e_rx_state;
980 /* tx notification */
981 response->am[num].u.e.tx_state = apppbx->e_tx_state;
983 switch(apppbx->e_state) {
984 case EPOINT_STATE_IN_SETUP:
985 response->am[num].u.e.state = ADMIN_STATE_IN_SETUP;
987 case EPOINT_STATE_OUT_SETUP:
988 response->am[num].u.e.state = ADMIN_STATE_OUT_SETUP;
990 case EPOINT_STATE_IN_OVERLAP:
991 response->am[num].u.e.state = ADMIN_STATE_IN_OVERLAP;
993 case EPOINT_STATE_OUT_OVERLAP:
994 response->am[num].u.e.state = ADMIN_STATE_OUT_OVERLAP;
996 case EPOINT_STATE_IN_PROCEEDING:
997 response->am[num].u.e.state = ADMIN_STATE_IN_PROCEEDING;
999 case EPOINT_STATE_OUT_PROCEEDING:
1000 response->am[num].u.e.state = ADMIN_STATE_OUT_PROCEEDING;
1002 case EPOINT_STATE_IN_ALERTING:
1003 response->am[num].u.e.state = ADMIN_STATE_IN_ALERTING;
1005 case EPOINT_STATE_OUT_ALERTING:
1006 response->am[num].u.e.state = ADMIN_STATE_OUT_ALERTING;
1008 case EPOINT_STATE_CONNECT:
1009 response->am[num].u.e.state = ADMIN_STATE_CONNECT;
1011 case EPOINT_STATE_IN_DISCONNECT:
1012 response->am[num].u.e.state = ADMIN_STATE_IN_DISCONNECT;
1014 case EPOINT_STATE_OUT_DISCONNECT:
1015 response->am[num].u.e.state = ADMIN_STATE_OUT_DISCONNECT;
1018 response->am[num].u.e.state = ADMIN_STATE_IDLE;
1021 SCPY(response->am[num].u.e.terminal, apppbx->e_ext.number);
1023 SCPY(response->am[num].u.e.callerid, apppbx->e_callerinfo.id);
1025 SCPY(response->am[num].u.e.dialing, apppbx->e_dialinginfo.id);
1027 if (apppbx->e_action)
1028 SCPY(response->am[num].u.e.action, action_defs[apppbx->e_action->index].name);
1029 // if (apppbx->e_action)
1030 // printf("action=%s\n",action_defs[apppbx->e_action->index].name);
1032 response->am[num].u.e.park = apppbx->ea_endpoint->ep_park;
1033 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))
1034 memcpy(response->am[num].u.e.park_callid, apppbx->ea_endpoint->ep_park_callid, apppbx->ea_endpoint->ep_park_len);
1035 response->am[num].u.e.park_len = apppbx->ea_endpoint->ep_park_len;
1038 if (apppbx->e_crypt == CRYPT_ON)
1039 response->am[num].u.e.crypt = 1;
1042 apppbx = apppbx->next;
1046 /* create response for all ports */
1050 response->am[num].message = ADMIN_RESPONSE_S_PORT;
1052 response->am[num].u.p.serial = port->p_serial;
1054 SCPY(response->am[num].u.p.name, port->p_name);
1056 response->am[num].u.p.epoint = ACTIVE_EPOINT(port->p_epointlist);
1058 switch(port->p_state) {
1059 case PORT_STATE_IN_SETUP:
1060 response->am[num].u.p.state = ADMIN_STATE_IN_SETUP;
1062 case PORT_STATE_OUT_SETUP:
1063 response->am[num].u.p.state = ADMIN_STATE_OUT_SETUP;
1065 case PORT_STATE_IN_OVERLAP:
1066 response->am[num].u.p.state = ADMIN_STATE_IN_OVERLAP;
1068 case PORT_STATE_OUT_OVERLAP:
1069 response->am[num].u.p.state = ADMIN_STATE_OUT_OVERLAP;
1071 case PORT_STATE_IN_PROCEEDING:
1072 response->am[num].u.p.state = ADMIN_STATE_IN_PROCEEDING;
1074 case PORT_STATE_OUT_PROCEEDING:
1075 response->am[num].u.p.state = ADMIN_STATE_OUT_PROCEEDING;
1077 case PORT_STATE_IN_ALERTING:
1078 response->am[num].u.p.state = ADMIN_STATE_IN_ALERTING;
1080 case PORT_STATE_OUT_ALERTING:
1081 response->am[num].u.p.state = ADMIN_STATE_OUT_ALERTING;
1083 case PORT_STATE_CONNECT:
1084 response->am[num].u.p.state = ADMIN_STATE_CONNECT;
1086 case PORT_STATE_IN_DISCONNECT:
1087 response->am[num].u.p.state = ADMIN_STATE_IN_DISCONNECT;
1089 case PORT_STATE_OUT_DISCONNECT:
1090 response->am[num].u.p.state = ADMIN_STATE_OUT_DISCONNECT;
1092 case PORT_STATE_RELEASE:
1093 response->am[num].u.p.state = ADMIN_STATE_RELEASE;
1096 response->am[num].u.p.state = ADMIN_STATE_IDLE;
1100 if ((port->p_type & PORT_CLASS_mISDN_MASK) == PORT_CLASS_DSS1) {
1101 response->am[num].u.p.isdn = 1;
1102 pdss1 = (class Pdss1 *)port;
1103 response->am[num].u.p.isdn_chan = pdss1->p_m_b_channel;
1104 response->am[num].u.p.isdn_hold = pdss1->p_m_hold;
1105 response->am[num].u.p.isdn_ces = pdss1->p_m_d_ces;
1115 int sockserial = 1; // must start with 1, because 0 is used if no serial is set
1117 * handle admin socket (non blocking)
1119 int admin_handle_con(struct lcr_fd *fd, unsigned int what, void *instance, int index);
1121 int admin_handle(struct lcr_fd *fd, unsigned int what, void *instance, int index)
1124 socklen_t sock_len = sizeof(sock_address);
1125 struct admin_list *admin;
1127 /* check for new incoming connections */
1128 if ((new_sock = accept(sock, (struct sockaddr *)&sock_address, &sock_len)) >= 0) {
1129 /* insert new socket */
1130 admin = (struct admin_list *)MALLOC(sizeof(struct admin_list));
1133 admin->sockserial = sockserial++;
1134 admin->next = admin_first;
1135 admin_first = admin;
1136 admin->sock = new_sock;
1137 admin->fd.fd = new_sock;
1138 register_fd(&admin->fd, LCR_FD_READ | LCR_FD_EXCEPT, admin_handle_con, admin, 0);
1140 if (errno != EWOULDBLOCK) {
1141 PERROR("Failed to accept connection from socket \"%s\". (errno=%d) Closing socket.\n", sock_address.sun_path, errno);
1150 int admin_handle_con(struct lcr_fd *fd, unsigned int what, void *instance, int index)
1152 struct admin_list *admin = (struct admin_list *)instance;
1154 struct admin_message msg;
1156 struct Endpoint *epoint;
1158 if ((what & LCR_FD_READ)) {
1160 len = read(admin->sock, &msg, sizeof(msg));
1163 PDEBUG(DEBUG_LOG, "Broken pipe on socket %d. (errno=%d).\n", admin->sock, errno);
1165 /*release endpoint if exists */
1166 if (admin->epointid) {
1167 epoint = find_epoint_id(admin->epointid);
1168 if (epoint && epoint->ep_app_type == EAPP_TYPE_PBX) {
1169 ((class EndpointAppPBX *)epoint->ep_app)->
1170 release(RELEASE_ALL, LOCATION_PRIVATE_LOCAL, CAUSE_NORMAL, LOCATION_PRIVATE_LOCAL, CAUSE_NORMAL, 0);
1174 free_connection(admin);
1179 if (len != sizeof(msg)) {
1180 PERROR("Short/long read on socket %d. (len=%d != size=%d).\n", admin->sock, len, sizeof(msg));
1183 /* process socket command */
1184 if (admin->response && msg.message != ADMIN_MESSAGE) {
1185 PERROR("Data from socket %d while sending response.\n", admin->sock);
1188 switch (msg.message) {
1189 case ADMIN_REQUEST_CMD_INTERFACE:
1190 if (admin_interface(&admin->response) < 0) {
1191 PERROR("Failed to create dial response for socket %d.\n", admin->sock);
1192 goto response_error;
1194 admin->fd.when |= LCR_FD_WRITE;
1197 case ADMIN_REQUEST_CMD_ROUTE:
1198 if (admin_route(&admin->response) < 0) {
1199 PERROR("Failed to create dial response for socket %d.\n", admin->sock);
1200 goto response_error;
1202 admin->fd.when |= LCR_FD_WRITE;
1205 case ADMIN_REQUEST_CMD_DIAL:
1206 if (admin_dial(&admin->response, msg.u.x.message) < 0) {
1207 PERROR("Failed to create dial response for socket %d.\n", admin->sock);
1208 goto response_error;
1210 admin->fd.when |= LCR_FD_WRITE;
1213 case ADMIN_REQUEST_CMD_RELEASE:
1214 if (admin_release(&admin->response, msg.u.x.message) < 0) {
1215 PERROR("Failed to create release response for socket %d.\n", admin->sock);
1216 goto response_error;
1218 admin->fd.when |= LCR_FD_WRITE;
1221 case ADMIN_REQUEST_STATE:
1222 if (admin_state(&admin->response) < 0) {
1223 PERROR("Failed to create state response for socket %d.\n", admin->sock);
1224 goto response_error;
1226 admin->fd.when |= LCR_FD_WRITE;
1229 case ADMIN_TRACE_REQUEST:
1230 if (admin_trace(admin, &msg.u.trace_req) < 0) {
1231 PERROR("Failed to create trace response for socket %d.\n", admin->sock);
1232 goto response_error;
1234 admin->fd.when |= LCR_FD_WRITE;
1237 case ADMIN_REQUEST_CMD_BLOCK:
1238 if (admin_block(&admin->response, msg.u.x.portnum, msg.u.x.block) < 0) {
1239 PERROR("Failed to create block response for socket %d.\n", admin->sock);
1240 goto response_error;
1242 admin->fd.when |= LCR_FD_WRITE;
1246 if (admin_message_to_lcr(&msg.u.msg, admin) < 0) {
1247 PERROR("Failed to deliver message for socket %d.\n", admin->sock);
1248 goto response_error;
1252 case ADMIN_CALL_SETUP:
1253 if (admin_call(admin, &msg) < 0) {
1254 PERROR("Failed to create call for socket %d.\n", admin->sock);
1261 PERROR("Invalid message %d from socket %d.\n", msg.message, admin->sock);
1266 if ((what & LCR_FD_WRITE)) {
1268 if (admin->response) {
1269 len = write(admin->sock, ((unsigned char *)(admin->response->am))+admin->response->offset, sizeof(struct admin_message)*(admin->response->num)-admin->response->offset);
1275 if (len < (int)(sizeof(struct admin_message)*(admin->response->num) - admin->response->offset)) {
1276 admin->response->offset+=len;
1279 temp = admin->response;
1280 admin->response = admin->response->next;
1285 admin->fd.when &= ~LCR_FD_WRITE;