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;
88 /* free remote joins */
89 if (admin->remote_name[0]) {
97 "REMOTE APP release");
98 add_trace("app", "name", "%s", admin->remote_name);
100 /* release all exported channels */
101 mISDNport = mISDNport_first;
104 ii = mISDNport->b_num;
106 if (mISDNport->b_remote_id[i] == admin->sock) {
107 mISDNport->b_state[i] = B_STATE_IDLE;
108 unsched_timer(&mISDNport->b_timer[i]);
109 mISDNport->b_remote_id[i] = 0;
110 mISDNport->b_remote_ref[i] = 0;
114 mISDNport = mISDNport->next;
119 joinnext = join->next;
120 if (join->j_type==JOIN_TYPE_REMOTE) if (((class JoinRemote *)join)->j_remote_id == admin->sock) {
121 memset(¶m, 0, sizeof(param));
122 param.disconnectinfo.cause = CAUSE_OUTOFORDER;
123 param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
124 ((class JoinRemote *)join)->message_remote(MESSAGE_RELEASE, ¶m);
125 /* join is now destroyed, so we go to next join */
131 if (admin->sock >= 0) {
132 unregister_fd(&admin->fd);
136 response = admin->response;
138 temp = response->next;
141 response = (struct admin_queue *)temp;
144 adminp = &admin_first;
146 if (*adminp == admin)
148 adminp = &((*adminp)->next);
151 *adminp = (*adminp)->next;
159 * cleanup admin socket
161 void admin_cleanup(void)
163 struct admin_list *admin, *next;;
168 free_connection(admin);
173 unregister_fd(&admin_fd);
183 * do interface reload
185 int admin_interface(struct admin_queue **responsep)
187 struct admin_queue *response; /* response pointer */
188 const char *err_txt = "";
191 if (read_interfaces()) {
193 free_interfaces(interface_first);
194 interface_first = interface_newlist;
195 interface_newlist = NULL;
197 err_txt = interface_error;
200 /* create state response */
201 response = (struct admin_queue *)MALLOC(sizeof(struct admin_queue)+sizeof(admin_message));
205 response->am[0].message = ADMIN_RESPONSE_CMD_INTERFACE;
207 response->am[0].u.x.error = err;
209 SCPY(response->am[0].u.x.message, err_txt);
210 /* attach to response chain */
211 *responsep = response;
212 responsep = &response->next;
220 int admin_route(struct admin_queue **responsep)
222 struct route_ruleset *ruleset_new;
223 struct admin_queue *response; /* response pointer */
224 char err_txt[256] = "";
229 class EndpointAppPBX *apppbx;
233 apppbx = apppbx_first;
236 apppbx = apppbx->next;
239 SPRINT(err_txt, "Cannot reload routing, because %d endpoints active\n", n);
244 if (!(ruleset_new = ruleset_parse())) {
245 SPRINT(err_txt, ruleset_error);
249 ruleset_free(ruleset_first);
250 ruleset_first = ruleset_new;
251 ruleset_main = getrulesetbyname("main");
253 SPRINT(err_txt, "Ruleset reloaded, but rule 'main' not found.\n");
256 apppbx = apppbx_first;
258 if (apppbx->e_action) {
259 switch(apppbx->e_action->index) {
260 case ACTION_INTERNAL:
261 apppbx->e_action = &action_internal;
263 case ACTION_EXTERNAL:
264 apppbx->e_action = &action_external;
267 apppbx->e_action = &action_remote;
269 case ACTION_VBOX_RECORD:
270 apppbx->e_action = &action_vbox;
272 case ACTION_PARTYLINE:
273 apppbx->e_action = &action_partyline;
278 } else if (apppbx->e_state != EPOINT_STATE_CONNECT) {
280 unsched_timer(&apppbx->e_callback_timeout);
281 apppbx->e_action = NULL;
282 apppbx->release(RELEASE_ALL, LOCATION_PRIVATE_LOCAL, CAUSE_NORMAL, LOCATION_PRIVATE_LOCAL, CAUSE_NORMAL, 0);
285 numberrize_callerinfo(apppbx->e_callerinfo.id, apppbx->e_callerinfo.ntype, options.national, options.international),
286 apppbx->e_dialinginfo.id,
289 apppbx->ea_endpoint->ep_serial,
290 "KICK (reload routing)");
294 unsched_timer(&apppbx->e_action_timeout);
295 apppbx->e_rule = NULL;
296 apppbx->e_ruleset = NULL;
298 apppbx = apppbx->next;
302 /* create state response */
303 response = (struct admin_queue *)MALLOC(sizeof(struct admin_queue)+sizeof(admin_message));
307 response->am[0].message = ADMIN_RESPONSE_CMD_ROUTE;
309 response->am[0].u.x.error = err;
311 SCPY(response->am[0].u.x.message, err_txt);
312 /* attach to response chain */
313 *responsep = response;
314 responsep = &response->next;
322 int admin_dial(struct admin_queue **responsep, char *message)
324 struct extension ext; /* temporary extension's settings */
325 struct admin_queue *response; /* response pointer */
326 char *p; /* pointer to dialing digits */
328 /* create state response */
329 response = (struct admin_queue *)MALLOC(sizeof(struct admin_queue)+sizeof(admin_message));
333 response->am[0].message = ADMIN_RESPONSE_CMD_DIAL;
335 /* process request */
336 if (!(p = strchr(message,':'))) {
337 response->am[0].u.x.error = -EINVAL;
338 SPRINT(response->am[0].u.x.message, "no seperator ':' in message to seperate number from extension");
343 /* modify extension */
344 if (!read_extension(&ext, message)) {
345 response->am[0].u.x.error = -EINVAL;
346 SPRINT(response->am[0].u.x.message, "extension doesn't exist");
350 write_extension(&ext, message);
353 /* attach to response chain */
354 *responsep = response;
355 responsep = &response->next;
363 int admin_trace(struct admin_list *admin, struct admin_trace_req *trace)
365 memcpy(&admin->trace, trace, sizeof(struct admin_trace_req));
373 * 0 = make port available
374 * 1 = make port administratively blocked
376 * the result is returned:
377 * 0 = port is now available
378 * 1 = port is now blocked
379 * 2 = port cannot be loaded or has been unloaded
380 * -1 = port doesn't exist
382 int admin_block(struct admin_queue **responsep, int portnum, int block)
384 struct admin_queue *response; /* response pointer */
385 struct interface *interface;
386 struct interface_port *ifport;
388 /* create block response */
389 response = (struct admin_queue *)MALLOC(sizeof(struct admin_queue)+sizeof(admin_message));
393 response->am[0].message = ADMIN_RESPONSE_CMD_BLOCK;
394 response->am[0].u.x.portnum = portnum;
396 /* search for port */
398 interface = interface_first;
400 ifport = interface->ifport;
402 if (ifport->portnum == portnum)
404 ifport = ifport->next;
408 interface = interface->next;
410 /* not found, we return -1 */
412 response->am[0].u.x.block = -1;
413 response->am[0].u.x.error = 1;
414 SPRINT(response->am[0].u.x.message, "Port %d does not exist.", portnum);
419 if (!ifport->mISDNport) {
420 /* not loaded anyway */
422 response->am[0].u.x.block = 2;
426 /* try loading interface */
427 ifport->block = block;
430 /* port cannot load */
431 if (ifport->block >= 2) {
432 response->am[0].u.x.block = 2;
433 response->am[0].u.x.error = 1;
434 SPRINT(response->am[0].u.x.message, "Port %d will not load.", portnum);
439 response->am[0].u.x.block = ifport->block;
443 /* if we shall unload interface */
445 mISDNport_close(ifport->mISDNport);
446 ifport->mISDNport = 0;
451 /* port new blocking state */
452 ifport->block = response->am[0].u.x.block = block;
455 /* attach to response chain */
456 *responsep = response;
457 responsep = &response->next;
465 int admin_release(struct admin_queue **responsep, char *message)
468 struct admin_queue *response; /* response pointer */
469 class EndpointAppPBX *apppbx;
471 /* create state response */
472 response = (struct admin_queue *)MALLOC(sizeof(struct admin_queue)+sizeof(admin_message));
476 response->am[0].message = ADMIN_RESPONSE_CMD_RELEASE;
479 apppbx = apppbx_first;
481 if (apppbx->ea_endpoint->ep_serial == id)
483 apppbx = apppbx->next;
486 response->am[0].u.x.error = -EINVAL;
487 SPRINT(response->am[0].u.x.message, "Given endpoint %d doesn't exist.", id);
491 unsched_timer(&apppbx->e_callback_timeout);
492 apppbx->release(RELEASE_ALL, LOCATION_PRIVATE_LOCAL, CAUSE_NORMAL, LOCATION_PRIVATE_LOCAL, CAUSE_NORMAL, 0);
495 /* attach to response chain */
496 *responsep = response;
497 responsep = &response->next;
505 int admin_call(struct admin_list *admin, struct admin_message *msg)
507 class Endpoint *epoint;
508 class EndpointAppPBX *apppbx;
510 if (!(epoint = new Endpoint(0, 0)))
511 FATAL("No memory for Endpoint instance\n");
512 if (!(epoint->ep_app = apppbx = new DEFAULT_ENDPOINT_APP(epoint, 1))) // outgoing
513 FATAL("No memory for Endpoint Application instance\n");
514 apppbx->e_adminid = admin->sockserial;
515 admin->epointid = epoint->ep_serial;
516 SCPY(apppbx->e_callerinfo.id, nationalize_callerinfo(msg->u.call.callerid, &apppbx->e_callerinfo.ntype, options.national, options.international));
517 if (msg->u.call.present)
518 apppbx->e_callerinfo.present = INFO_PRESENT_ALLOWED;
520 apppbx->e_callerinfo.present = INFO_PRESENT_RESTRICTED;
521 apppbx->e_callerinfo.screen = INFO_SCREEN_NETWORK;
524 apppbx->e_capainfo.bearer_capa = msg->u.call.bc_capa;
525 apppbx->e_capainfo.bearer_mode = msg->u.call.bc_mode;
526 apppbx->e_capainfo.bearer_info1 = msg->u.call.bc_info1;
527 apppbx->e_capainfo.hlc = msg->u.call.hlc;
528 apppbx->e_capainfo.exthlc = msg->u.call.exthlc;
529 SCPY(apppbx->e_dialinginfo.id, msg->u.call.dialing);
530 SCPY(apppbx->e_dialinginfo.interfaces, msg->u.call.interface);
531 apppbx->e_dialinginfo.sending_complete = 1;
533 apppbx->new_state(PORT_STATE_OUT_SETUP);
534 apppbx->out_setup(0);
540 * this function is called for response whenever a call state changes.
542 void admin_call_response(int adminid, int message, const char *connected, int cause, int location, int notify_progress)
544 struct admin_list *admin;
545 struct admin_queue *response, **responsep; /* response pointer */
547 /* searching for admin id
548 * maybe there is no admin instance, because the calling port was not
549 * initiated by admin_call */
552 if (adminid == admin->sockserial)
559 /* seek to end of response list */
560 response = admin->response;
561 responsep = &admin->response;
563 responsep = &response->next;
564 response = response->next;
567 /* create state response */
568 response = (struct admin_queue *)MALLOC(sizeof(struct admin_queue)+sizeof(admin_message));
572 response->am[0].message = message;
574 SCPY(response->am[0].u.call.callerid, connected);
575 response->am[0].u.call.cause = cause;
576 response->am[0].u.call.location = location;
577 response->am[0].u.call.notify_progress = notify_progress;
579 /* attach to response chain */
580 *responsep = response;
581 responsep = &response->next;
582 admin->fd.when |= LCR_FD_WRITE;
587 * send data to the remote socket join instance
589 int admin_message_to_join(struct admin_msg *msg, struct admin_list *admin)
592 struct admin_list *temp;
595 if (msg->type == MESSAGE_HELLO) {
596 if (admin->remote_name[0]) {
597 PERROR("Remote application repeats hello message.\n");
600 /* look for second application */
603 if (!strcmp(temp->remote_name, msg->param.hello.application))
608 PERROR("Remote application connects twice??? (ignoring)\n");
611 /* set remote socket instance */
612 SCPY(admin->remote_name, msg->param.hello.application);
620 "REMOTE APP registers");
621 add_trace("app", "name", "%s", admin->remote_name);
626 /* check we have no application name */
627 if (!admin->remote_name[0]) {
628 PERROR("Remote application did not send us a hello message.\n");
633 if (msg->type == MESSAGE_NEWREF) {
634 /* create new join instance */
635 join = new JoinRemote(0, admin->remote_name, admin->sock); // must have no serial, because no endpoint is connected
637 FATAL("No memory for remote join instance\n");
644 * no ref given for *_ack */
645 if (msg->type == MESSAGE_BCHANNEL)
646 if (msg->param.bchannel.type == BCHANNEL_ASSIGN_ACK
647 || msg->param.bchannel.type == BCHANNEL_REMOVE_ACK
648 || msg->param.bchannel.type == BCHANNEL_RELEASE) {
649 /* no ref, but address */
650 message_bchannel_from_remote(NULL, msg->param.bchannel.type, msg->param.bchannel.handle);
656 PERROR("Remote application did not send us a valid ref with a message.\n");
660 /* find join instance */
663 if (join->j_serial == msg->ref)
668 PDEBUG(DEBUG_LOG, "No join found with serial %d. (May have been already released.)\n", msg->ref);
672 /* check application */
673 if (join->j_type != JOIN_TYPE_REMOTE) {
674 PERROR("Ref %d does not belong to a remote join instance.\n", msg->ref);
677 if (admin->sock != ((class JoinRemote *)join)->j_remote_id) {
678 PERROR("Ref %d belongs to remote application %s, but not to sending application %s.\n", msg->ref, ((class JoinRemote *)join)->j_remote_name, admin->remote_name);
683 ((class JoinRemote *)join)->message_remote(msg->type, &msg->param);
690 * this function is called for every message to remote socket
692 int admin_message_from_join(int remote_id, unsigned int ref, int message_type, union parameter *param)
694 struct admin_list *admin;
695 struct admin_queue **responsep; /* response pointer */
697 /* searching for admin id
698 * maybe there is no given remote application
702 if (admin->remote_name[0] && admin->sock==remote_id)
706 /* no given remote application connected */
710 /* seek to end of response list */
711 responsep = &admin->response;
713 responsep = &(*responsep)->next;
716 /* create state response */
717 *responsep = (struct admin_queue *)MALLOC(sizeof(struct admin_queue)+sizeof(admin_message));
719 (*responsep)->num = 1;
722 (*responsep)->am[0].message = ADMIN_MESSAGE;
723 (*responsep)->am[0].u.msg.type = message_type;
724 (*responsep)->am[0].u.msg.ref = ref;
725 memcpy(&(*responsep)->am[0].u.msg.param, param, sizeof(union parameter));
726 admin->fd.when |= LCR_FD_WRITE;
734 int admin_state(struct admin_queue **responsep)
737 class EndpointAppPBX *apppbx;
740 struct interface *interface;
741 struct interface_port *ifport;
742 struct mISDNport *mISDNport;
743 struct select_channel *selchannel;
747 struct admin_queue *response;
748 struct admin_list *admin;
752 /* create state response */
753 response = (struct admin_queue *)MALLOC(sizeof(struct admin_queue)+sizeof(admin_message));
757 response->am[0].message = ADMIN_RESPONSE_STATE;
759 SCPY(response->am[0].u.s.version_string, VERSION_STRING);
762 now_tm = localtime(&now);
763 memcpy(&response->am[0].u.s.tm, now_tm, sizeof(struct tm));
765 SCPY(response->am[0].u.s.logfile, options.log);
766 /* interface count */
768 interface = interface_first;
770 ifport = interface->ifport;
773 ifport = ifport->next;
775 interface = interface->next;
777 response->am[0].u.s.interfaces = i;
778 /* remote connection count */
782 if (admin->remote_name[0])
786 response->am[0].u.s.remotes = i;
794 response->am[0].u.s.joins = i;
796 apppbx = apppbx_first;
800 apppbx = apppbx->next;
802 response->am[0].u.s.epoints = i;
810 response->am[0].u.s.ports = i;
811 /* attach to response chain */
812 *responsep = response;
813 responsep = &response->next;
815 /* create response for all instances */
816 num = (response->am[0].u.s.interfaces)
817 + (response->am[0].u.s.remotes)
818 + (response->am[0].u.s.joins)
819 + (response->am[0].u.s.epoints)
820 + (response->am[0].u.s.ports);
823 response = (struct admin_queue *)MALLOC(sizeof(admin_queue)+(num*sizeof(admin_message)));
826 *responsep = response;
827 responsep = &response->next;
828 interface = interface_first;
831 ifport = interface->ifport;
834 response->am[num].message = ADMIN_RESPONSE_S_INTERFACE;
836 SCPY(response->am[num].u.i.interface_name, interface->name);
838 response->am[num].u.i.portnum = ifport->portnum;
840 SCPY(response->am[num].u.i.portname, ifport->portname);
842 response->am[num].u.i.extension = interface->extension;
844 response->am[num].u.i.block = ifport->block;
845 if (ifport->mISDNport) {
846 mISDNport = ifport->mISDNport;
849 response->am[num].u.i.ptp = mISDNport->ptp;
851 response->am[num].u.i.l1hold = mISDNport->l1hold;
853 response->am[num].u.i.l2hold = mISDNport->l2hold;
855 response->am[num].u.i.ntmode = mISDNport->ntmode;
857 response->am[num].u.i.pri = mISDNport->pri;
859 response->am[num].u.i.use = mISDNport->use;
861 response->am[num].u.i.l1link = mISDNport->l1link;
863 response->am[num].u.i.l2link = mISDNport->l2link;
864 memcpy(response->am[num].u.i.l2mask, mISDNport->l2mask, 16);
866 response->am[num].u.i.los = mISDNport->los;
868 response->am[num].u.i.ais = mISDNport->ais;
870 response->am[num].u.i.rdi = mISDNport->rdi;
872 response->am[num].u.i.slip_tx = mISDNport->slip_tx;
873 response->am[num].u.i.slip_rx = mISDNport->slip_rx;
875 response->am[num].u.i.channels = mISDNport->b_num;
876 /* channel selection */
877 selchannel = ifport->out_channel;
878 if (ifport->channel_force)
879 SCAT(response->am[num].u.i.out_channel, "force");
881 if (response->am[num].u.i.out_channel[0])
882 SCAT(response->am[num].u.i.out_channel, ",");
883 switch (selchannel->channel) {
885 SCAT(response->am[num].u.i.out_channel, "no");
888 SCAT(response->am[num].u.i.out_channel, "any");
891 SCAT(response->am[num].u.i.out_channel, "free");
894 SPRINT(strchr(response->am[num].u.i.out_channel, '\0'), "%d", selchannel->channel);
896 selchannel = selchannel->next;
898 selchannel = ifport->in_channel;
900 switch (selchannel->channel) {
902 SCAT(response->am[num].u.i.in_channel, "free");
905 SPRINT(strchr(response->am[num].u.i.in_channel, '\0'), "%d", selchannel->channel);
907 selchannel = selchannel->next;
912 while(i < mISDNport->b_num) {
913 response->am[num].u.i.busy[i] = mISDNport->b_state[i];
914 if (mISDNport->b_port[i])
915 response->am[num].u.i.port[i] = mISDNport->b_port[i]->p_serial;
916 response->am[num].u.i.mode[i] = mISDNport->b_mode[i];
922 ifport = ifport->next;
924 interface = interface->next;
927 /* create response for all remotes */
930 if (admin->remote_name[0]) {
932 response->am[num].message = ADMIN_RESPONSE_S_REMOTE;
934 SCPY(response->am[num].u.r.name, admin->remote_name);
941 /* create response for all joins */
945 response->am[num].message = ADMIN_RESPONSE_S_JOIN;
947 response->am[num].u.j.serial = join->j_serial;
949 if (join->j_type == JOIN_TYPE_PBX)
950 response->am[num].u.j.partyline = ((class JoinPBX *)join)->j_partyline;
951 /* remote application */
952 if (join->j_type == JOIN_TYPE_REMOTE)
953 SCPY(response->am[num].u.j.remote, ((class JoinRemote *)join)->j_remote_name);
959 /* create response for all endpoint */
960 apppbx = apppbx_first;
963 response->am[num].message = ADMIN_RESPONSE_S_EPOINT;
965 response->am[num].u.e.serial = apppbx->ea_endpoint->ep_serial;
967 response->am[num].u.e.join = apppbx->ea_endpoint->ep_join_id;
968 /* rx notification */
969 response->am[num].u.e.rx_state = apppbx->e_rx_state;
970 /* tx notification */
971 response->am[num].u.e.tx_state = apppbx->e_tx_state;
973 switch(apppbx->e_state) {
974 case EPOINT_STATE_IN_SETUP:
975 response->am[num].u.e.state = ADMIN_STATE_IN_SETUP;
977 case EPOINT_STATE_OUT_SETUP:
978 response->am[num].u.e.state = ADMIN_STATE_OUT_SETUP;
980 case EPOINT_STATE_IN_OVERLAP:
981 response->am[num].u.e.state = ADMIN_STATE_IN_OVERLAP;
983 case EPOINT_STATE_OUT_OVERLAP:
984 response->am[num].u.e.state = ADMIN_STATE_OUT_OVERLAP;
986 case EPOINT_STATE_IN_PROCEEDING:
987 response->am[num].u.e.state = ADMIN_STATE_IN_PROCEEDING;
989 case EPOINT_STATE_OUT_PROCEEDING:
990 response->am[num].u.e.state = ADMIN_STATE_OUT_PROCEEDING;
992 case EPOINT_STATE_IN_ALERTING:
993 response->am[num].u.e.state = ADMIN_STATE_IN_ALERTING;
995 case EPOINT_STATE_OUT_ALERTING:
996 response->am[num].u.e.state = ADMIN_STATE_OUT_ALERTING;
998 case EPOINT_STATE_CONNECT:
999 response->am[num].u.e.state = ADMIN_STATE_CONNECT;
1001 case EPOINT_STATE_IN_DISCONNECT:
1002 response->am[num].u.e.state = ADMIN_STATE_IN_DISCONNECT;
1004 case EPOINT_STATE_OUT_DISCONNECT:
1005 response->am[num].u.e.state = ADMIN_STATE_OUT_DISCONNECT;
1008 response->am[num].u.e.state = ADMIN_STATE_IDLE;
1011 SCPY(response->am[num].u.e.terminal, apppbx->e_ext.number);
1013 SCPY(response->am[num].u.e.callerid, apppbx->e_callerinfo.id);
1015 SCPY(response->am[num].u.e.dialing, apppbx->e_dialinginfo.id);
1017 if (apppbx->e_action)
1018 SCPY(response->am[num].u.e.action, action_defs[apppbx->e_action->index].name);
1019 // if (apppbx->e_action)
1020 // printf("action=%s\n",action_defs[apppbx->e_action->index].name);
1022 response->am[num].u.e.park = apppbx->ea_endpoint->ep_park;
1023 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))
1024 memcpy(response->am[num].u.e.park_callid, apppbx->ea_endpoint->ep_park_callid, apppbx->ea_endpoint->ep_park_len);
1025 response->am[num].u.e.park_len = apppbx->ea_endpoint->ep_park_len;
1027 if (apppbx->e_crypt == CRYPT_ON)
1028 response->am[num].u.e.crypt = 1;
1030 apppbx = apppbx->next;
1034 /* create response for all ports */
1038 response->am[num].message = ADMIN_RESPONSE_S_PORT;
1040 response->am[num].u.p.serial = port->p_serial;
1042 SCPY(response->am[num].u.p.name, port->p_name);
1044 response->am[num].u.p.epoint = ACTIVE_EPOINT(port->p_epointlist);
1046 switch(port->p_state) {
1047 case PORT_STATE_IN_SETUP:
1048 response->am[num].u.p.state = ADMIN_STATE_IN_SETUP;
1050 case PORT_STATE_OUT_SETUP:
1051 response->am[num].u.p.state = ADMIN_STATE_OUT_SETUP;
1053 case PORT_STATE_IN_OVERLAP:
1054 response->am[num].u.p.state = ADMIN_STATE_IN_OVERLAP;
1056 case PORT_STATE_OUT_OVERLAP:
1057 response->am[num].u.p.state = ADMIN_STATE_OUT_OVERLAP;
1059 case PORT_STATE_IN_PROCEEDING:
1060 response->am[num].u.p.state = ADMIN_STATE_IN_PROCEEDING;
1062 case PORT_STATE_OUT_PROCEEDING:
1063 response->am[num].u.p.state = ADMIN_STATE_OUT_PROCEEDING;
1065 case PORT_STATE_IN_ALERTING:
1066 response->am[num].u.p.state = ADMIN_STATE_IN_ALERTING;
1068 case PORT_STATE_OUT_ALERTING:
1069 response->am[num].u.p.state = ADMIN_STATE_OUT_ALERTING;
1071 case PORT_STATE_CONNECT:
1072 response->am[num].u.p.state = ADMIN_STATE_CONNECT;
1074 case PORT_STATE_IN_DISCONNECT:
1075 response->am[num].u.p.state = ADMIN_STATE_IN_DISCONNECT;
1077 case PORT_STATE_OUT_DISCONNECT:
1078 response->am[num].u.p.state = ADMIN_STATE_OUT_DISCONNECT;
1080 case PORT_STATE_RELEASE:
1081 response->am[num].u.p.state = ADMIN_STATE_RELEASE;
1084 response->am[num].u.p.state = ADMIN_STATE_IDLE;
1087 if ((port->p_type & PORT_CLASS_mISDN_MASK) == PORT_CLASS_DSS1) {
1088 response->am[num].u.p.isdn = 1;
1089 pdss1 = (class Pdss1 *)port;
1090 response->am[num].u.p.isdn_chan = pdss1->p_m_b_channel;
1091 response->am[num].u.p.isdn_hold = pdss1->p_m_hold;
1092 response->am[num].u.p.isdn_ces = pdss1->p_m_d_ces;
1101 int sockserial = 1; // must start with 1, because 0 is used if no serial is set
1103 * handle admin socket (non blocking)
1105 int admin_handle_con(struct lcr_fd *fd, unsigned int what, void *instance, int index);
1107 int admin_handle(struct lcr_fd *fd, unsigned int what, void *instance, int index)
1110 socklen_t sock_len = sizeof(sock_address);
1111 struct admin_list *admin;
1113 /* check for new incoming connections */
1114 if ((new_sock = accept(sock, (struct sockaddr *)&sock_address, &sock_len)) >= 0) {
1115 /* insert new socket */
1116 admin = (struct admin_list *)MALLOC(sizeof(struct admin_list));
1119 admin->sockserial = sockserial++;
1120 admin->next = admin_first;
1121 admin_first = admin;
1122 admin->sock = new_sock;
1123 admin->fd.fd = new_sock;
1124 register_fd(&admin->fd, LCR_FD_READ | LCR_FD_EXCEPT, admin_handle_con, admin, 0);
1126 if (errno != EWOULDBLOCK) {
1127 PERROR("Failed to accept connection from socket \"%s\". (errno=%d) Closing socket.\n", sock_address.sun_path, errno);
1136 int admin_handle_con(struct lcr_fd *fd, unsigned int what, void *instance, int index)
1138 struct admin_list *admin = (struct admin_list *)instance;
1140 struct admin_message msg;
1142 struct Endpoint *epoint;
1144 if ((what & LCR_FD_READ)) {
1146 len = read(admin->sock, &msg, sizeof(msg));
1149 PDEBUG(DEBUG_LOG, "Broken pipe on socket %d. (errno=%d).\n", admin->sock, errno);
1150 free_connection(admin);
1156 /*release endpoint if exists */
1157 if (admin->epointid) {
1158 epoint = find_epoint_id(admin->epointid);
1160 ((class DEFAULT_ENDPOINT_APP *)epoint->ep_app)->
1161 release(RELEASE_ALL, LOCATION_PRIVATE_LOCAL, CAUSE_NORMAL, LOCATION_PRIVATE_LOCAL, CAUSE_NORMAL, 0);
1165 free_connection(admin);
1168 if (len != sizeof(msg)) {
1169 PERROR("Short/long read on socket %d. (len=%d != size=%d).\n", admin->sock, len, sizeof(msg));
1170 free_connection(admin);
1173 /* process socket command */
1174 if (admin->response && msg.message != ADMIN_MESSAGE) {
1175 PERROR("Data from socket %d while sending response.\n", admin->sock);
1176 free_connection(admin);
1179 switch (msg.message) {
1180 case ADMIN_REQUEST_CMD_INTERFACE:
1181 if (admin_interface(&admin->response) < 0) {
1182 PERROR("Failed to create dial response for socket %d.\n", admin->sock);
1183 goto response_error;
1185 admin->fd.when |= LCR_FD_WRITE;
1188 case ADMIN_REQUEST_CMD_ROUTE:
1189 if (admin_route(&admin->response) < 0) {
1190 PERROR("Failed to create dial response for socket %d.\n", admin->sock);
1191 goto response_error;
1193 admin->fd.when |= LCR_FD_WRITE;
1196 case ADMIN_REQUEST_CMD_DIAL:
1197 if (admin_dial(&admin->response, msg.u.x.message) < 0) {
1198 PERROR("Failed to create dial response for socket %d.\n", admin->sock);
1199 goto response_error;
1201 admin->fd.when |= LCR_FD_WRITE;
1204 case ADMIN_REQUEST_CMD_RELEASE:
1205 if (admin_release(&admin->response, msg.u.x.message) < 0) {
1206 PERROR("Failed to create release response for socket %d.\n", admin->sock);
1207 goto response_error;
1209 admin->fd.when |= LCR_FD_WRITE;
1212 case ADMIN_REQUEST_STATE:
1213 if (admin_state(&admin->response) < 0) {
1214 PERROR("Failed to create state response for socket %d.\n", admin->sock);
1215 goto response_error;
1217 admin->fd.when |= LCR_FD_WRITE;
1220 case ADMIN_TRACE_REQUEST:
1221 if (admin_trace(admin, &msg.u.trace_req) < 0) {
1222 PERROR("Failed to create trace response for socket %d.\n", admin->sock);
1223 goto response_error;
1225 admin->fd.when |= LCR_FD_WRITE;
1228 case ADMIN_REQUEST_CMD_BLOCK:
1229 if (admin_block(&admin->response, msg.u.x.portnum, msg.u.x.block) < 0) {
1230 PERROR("Failed to create block response for socket %d.\n", admin->sock);
1231 goto response_error;
1233 admin->fd.when |= LCR_FD_WRITE;
1237 if (admin_message_to_join(&msg.u.msg, admin) < 0) {
1238 PERROR("Failed to deliver message for socket %d.\n", admin->sock);
1239 goto response_error;
1243 case ADMIN_CALL_SETUP:
1244 if (admin_call(admin, &msg) < 0) {
1245 PERROR("Failed to create call for socket %d.\n", admin->sock);
1247 free_connection(admin);
1253 PERROR("Invalid message %d from socket %d.\n", msg.message, admin->sock);
1254 free_connection(admin);
1259 if ((what & LCR_FD_WRITE)) {
1261 if (admin->response) {
1262 len = write(admin->sock, ((unsigned char *)(admin->response->am))+admin->response->offset, sizeof(struct admin_message)*(admin->response->num)-admin->response->offset);
1268 if (len < (int)(sizeof(struct admin_message)*(admin->response->num) - admin->response->offset)) {
1269 admin->response->offset+=len;
1272 temp = admin->response;
1273 admin->response = admin->response->next;
1278 admin->fd.when &= ~LCR_FD_WRITE;