1 /*****************************************************************************\
3 ** Linux Call Router **
5 **---------------------------------------------------------------------------**
6 ** Copyright: Andreas Eversberg **
8 ** Socket link server **
10 \*****************************************************************************/
13 #include <sys/socket.h>
18 char socket_name[128];
20 struct sockaddr_un sock_address;
22 struct admin_list *admin_first = NULL;
23 static struct lcr_fd admin_fd;
25 int admin_handle(struct lcr_fd *fd, unsigned int what, void *instance, int index);
28 * initialize admin socket
32 /* open and bind socket */
33 if ((sock = socket(PF_UNIX, SOCK_STREAM, 0)) < 0) {
34 PERROR("Failed to create admin socket. (errno=%d)\n", errno);
38 memset(&sock_address, 0, sizeof(sock_address));
39 SPRINT(socket_name, SOCKET_NAME, options.lock);
40 sock_address.sun_family = AF_UNIX;
41 UCPY(sock_address.sun_path, socket_name);
43 if (bind(sock, (struct sockaddr *)(&sock_address), SUN_LEN(&sock_address)) < 0) {
48 PERROR("Failed to bind admin socket to \"%s\". (errno=%d)\n", sock_address.sun_path, errno);
51 if (listen(sock, 5) < 0) {
56 PERROR("Failed to listen to socket \"%s\". (errno=%d)\n", sock_address.sun_path, errno);
59 memset(&admin_fd, 0, sizeof(admin_fd));
61 register_fd(&admin_fd, LCR_FD_READ | LCR_FD_EXCEPT, admin_handle, NULL, 0);
62 if (chmod(socket_name, options.socketrights) < 0) {
63 PERROR("Failed to change socket rights to %d. (errno=%d)\n", options.socketrights, errno);
65 if (chown(socket_name, options.socketuser, options.socketgroup) < 0) {
66 PERROR("Failed to change socket user/group to %d/%d. (errno=%d)\n", options.socketuser, options.socketgroup, errno);
75 * also releases all remote joins
77 void free_connection(struct admin_list *admin)
79 struct admin_queue *response;
81 union parameter param;
82 class Join *join, *joinnext;
83 struct mISDNport *mISDNport;
85 struct admin_list **adminp;
87 /* free remote joins */
88 if (admin->remote_name[0]) {
96 "REMOTE APP release");
97 add_trace("app", "name", "%s", admin->remote_name);
99 /* release all exported channels */
100 mISDNport = mISDNport_first;
103 ii = mISDNport->b_num;
105 if (mISDNport->b_remote_id[i] == admin->sock) {
106 mISDNport->b_state[i] = B_STATE_IDLE;
107 unsched_timer(&mISDNport->b_timer[i]);
108 mISDNport->b_remote_id[i] = 0;
109 mISDNport->b_remote_ref[i] = 0;
113 mISDNport = mISDNport->next;
118 joinnext = join->next;
119 if (join->j_type==JOIN_TYPE_REMOTE) if (((class JoinRemote *)join)->j_remote_id == admin->sock) {
120 memset(¶m, 0, sizeof(param));
121 param.disconnectinfo.cause = CAUSE_OUTOFORDER;
122 param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
123 ((class JoinRemote *)join)->message_remote(MESSAGE_RELEASE, ¶m);
124 /* join is now destroyed, so we go to next join */
130 if (admin->sock >= 0) {
131 unregister_fd(&admin->fd);
135 response = admin->response;
137 temp = response->next;
140 response = (struct admin_queue *)temp;
143 adminp = &admin_first;
145 if (*adminp == admin)
147 adminp = &((*adminp)->next);
150 *adminp = (*adminp)->next;
158 * cleanup admin socket
160 void admin_cleanup(void)
162 struct admin_list *admin, *next;;
167 free_connection(admin);
172 unregister_fd(&admin_fd);
182 * do interface reload
184 int admin_interface(struct admin_queue **responsep)
186 struct admin_queue *response; /* response pointer */
187 const char *err_txt = "";
190 if (read_interfaces()) {
192 free_interfaces(interface_first);
193 interface_first = interface_newlist;
194 interface_newlist = NULL;
196 err_txt = interface_error;
199 /* create state response */
200 response = (struct admin_queue *)MALLOC(sizeof(struct admin_queue)+sizeof(admin_message));
204 response->am[0].message = ADMIN_RESPONSE_CMD_INTERFACE;
206 response->am[0].u.x.error = err;
208 SCPY(response->am[0].u.x.message, err_txt);
209 /* attach to response chain */
210 *responsep = response;
211 responsep = &response->next;
219 int admin_route(struct admin_queue **responsep)
221 struct route_ruleset *ruleset_new;
222 struct admin_queue *response; /* response pointer */
223 char err_txt[256] = "";
228 class EndpointAppPBX *apppbx;
232 apppbx = apppbx_first;
235 apppbx = apppbx->next;
238 SPRINT(err_txt, "Cannot reload routing, because %d endpoints active\n", n);
243 if (!(ruleset_new = ruleset_parse())) {
244 SPRINT(err_txt, ruleset_error);
248 ruleset_free(ruleset_first);
249 ruleset_first = ruleset_new;
250 ruleset_main = getrulesetbyname("main");
252 SPRINT(err_txt, "Ruleset reloaded, but rule 'main' not found.\n");
255 apppbx = apppbx_first;
257 if (apppbx->e_action) {
258 switch(apppbx->e_action->index) {
259 case ACTION_INTERNAL:
260 apppbx->e_action = &action_internal;
262 case ACTION_EXTERNAL:
263 apppbx->e_action = &action_external;
266 apppbx->e_action = &action_remote;
268 case ACTION_VBOX_RECORD:
269 apppbx->e_action = &action_vbox;
271 case ACTION_PARTYLINE:
272 apppbx->e_action = &action_partyline;
277 } else if (apppbx->e_state != EPOINT_STATE_CONNECT) {
279 unsched_timer(&apppbx->e_callback_timeout);
280 apppbx->e_action = NULL;
281 apppbx->release(RELEASE_ALL, LOCATION_PRIVATE_LOCAL, CAUSE_NORMAL, LOCATION_PRIVATE_LOCAL, CAUSE_NORMAL, 0);
284 numberrize_callerinfo(apppbx->e_callerinfo.id, apppbx->e_callerinfo.ntype, options.national, options.international),
285 apppbx->e_dialinginfo.id,
288 apppbx->ea_endpoint->ep_serial,
289 "KICK (reload routing)");
293 unsched_timer(&apppbx->e_action_timeout);
294 apppbx->e_rule = NULL;
295 apppbx->e_ruleset = NULL;
297 apppbx = apppbx->next;
301 /* create state response */
302 response = (struct admin_queue *)MALLOC(sizeof(struct admin_queue)+sizeof(admin_message));
306 response->am[0].message = ADMIN_RESPONSE_CMD_ROUTE;
308 response->am[0].u.x.error = err;
310 SCPY(response->am[0].u.x.message, err_txt);
311 /* attach to response chain */
312 *responsep = response;
313 responsep = &response->next;
321 int admin_dial(struct admin_queue **responsep, char *message)
323 struct extension ext; /* temporary extension's settings */
324 struct admin_queue *response; /* response pointer */
325 char *p; /* pointer to dialing digits */
327 /* create state response */
328 response = (struct admin_queue *)MALLOC(sizeof(struct admin_queue)+sizeof(admin_message));
332 response->am[0].message = ADMIN_RESPONSE_CMD_DIAL;
334 /* process request */
335 if (!(p = strchr(message,':'))) {
336 response->am[0].u.x.error = -EINVAL;
337 SPRINT(response->am[0].u.x.message, "no seperator ':' in message to seperate number from extension");
342 /* modify extension */
343 if (!read_extension(&ext, message)) {
344 response->am[0].u.x.error = -EINVAL;
345 SPRINT(response->am[0].u.x.message, "extension doesn't exist");
349 write_extension(&ext, message);
352 /* attach to response chain */
353 *responsep = response;
354 responsep = &response->next;
362 int admin_trace(struct admin_list *admin, struct admin_trace_req *trace)
364 memcpy(&admin->trace, trace, sizeof(struct admin_trace_req));
372 * 0 = make port available
373 * 1 = make port administratively blocked
375 * the result is returned:
376 * 0 = port is now available
377 * 1 = port is now blocked
378 * 2 = port cannot be loaded or has been unloaded
379 * -1 = port doesn't exist
381 int admin_block(struct admin_queue **responsep, int portnum, int block)
383 struct admin_queue *response; /* response pointer */
384 struct interface *interface;
385 struct interface_port *ifport;
387 /* create block response */
388 response = (struct admin_queue *)MALLOC(sizeof(struct admin_queue)+sizeof(admin_message));
392 response->am[0].message = ADMIN_RESPONSE_CMD_BLOCK;
393 response->am[0].u.x.portnum = portnum;
395 /* search for port */
397 interface = interface_first;
399 ifport = interface->ifport;
401 if (ifport->portnum == portnum)
403 ifport = ifport->next;
407 interface = interface->next;
409 /* not found, we return -1 */
411 response->am[0].u.x.block = -1;
412 response->am[0].u.x.error = 1;
413 SPRINT(response->am[0].u.x.message, "Port %d does not exist.", portnum);
418 if (!ifport->mISDNport) {
419 /* not loaded anyway */
421 response->am[0].u.x.block = 2;
425 /* try loading interface */
426 ifport->block = block;
429 /* port cannot load */
430 if (ifport->block >= 2) {
431 response->am[0].u.x.block = 2;
432 response->am[0].u.x.error = 1;
433 SPRINT(response->am[0].u.x.message, "Port %d will not load.", portnum);
438 response->am[0].u.x.block = ifport->block;
442 /* if we shall unload interface */
444 mISDNport_close(ifport->mISDNport);
445 ifport->mISDNport = 0;
450 /* port new blocking state */
451 ifport->block = response->am[0].u.x.block = block;
454 /* attach to response chain */
455 *responsep = response;
456 responsep = &response->next;
464 int admin_release(struct admin_queue **responsep, char *message)
467 struct admin_queue *response; /* response pointer */
468 class EndpointAppPBX *apppbx;
470 /* create state response */
471 response = (struct admin_queue *)MALLOC(sizeof(struct admin_queue)+sizeof(admin_message));
475 response->am[0].message = ADMIN_RESPONSE_CMD_RELEASE;
478 apppbx = apppbx_first;
480 if (apppbx->ea_endpoint->ep_serial == id)
482 apppbx = apppbx->next;
485 response->am[0].u.x.error = -EINVAL;
486 SPRINT(response->am[0].u.x.message, "Given endpoint %d doesn't exist.", id);
490 unsched_timer(&apppbx->e_callback_timeout);
491 apppbx->release(RELEASE_ALL, LOCATION_PRIVATE_LOCAL, CAUSE_NORMAL, LOCATION_PRIVATE_LOCAL, CAUSE_NORMAL, 0);
494 /* attach to response chain */
495 *responsep = response;
496 responsep = &response->next;
504 int admin_call(struct admin_list *admin, struct admin_message *msg)
506 class Endpoint *epoint;
507 class EndpointAppPBX *apppbx;
509 if (!(epoint = new Endpoint(0, 0)))
510 FATAL("No memory for Endpoint instance\n");
511 if (!(epoint->ep_app = apppbx = new DEFAULT_ENDPOINT_APP(epoint, 1))) // outgoing
512 FATAL("No memory for Endpoint Application instance\n");
513 apppbx->e_adminid = admin->sockserial;
514 admin->epointid = epoint->ep_serial;
515 SCPY(apppbx->e_callerinfo.id, nationalize_callerinfo(msg->u.call.callerid, &apppbx->e_callerinfo.ntype, options.national, options.international));
516 if (msg->u.call.present)
517 apppbx->e_callerinfo.present = INFO_PRESENT_ALLOWED;
519 apppbx->e_callerinfo.present = INFO_PRESENT_RESTRICTED;
520 apppbx->e_callerinfo.screen = INFO_SCREEN_NETWORK;
523 apppbx->e_capainfo.bearer_capa = msg->u.call.bc_capa;
524 apppbx->e_capainfo.bearer_mode = msg->u.call.bc_mode;
525 apppbx->e_capainfo.bearer_info1 = msg->u.call.bc_info1;
526 apppbx->e_capainfo.hlc = msg->u.call.hlc;
527 apppbx->e_capainfo.exthlc = msg->u.call.exthlc;
528 SCPY(apppbx->e_dialinginfo.id, msg->u.call.dialing);
529 SCPY(apppbx->e_dialinginfo.interfaces, msg->u.call.interface);
530 apppbx->e_dialinginfo.sending_complete = 1;
532 apppbx->new_state(PORT_STATE_OUT_SETUP);
533 apppbx->out_setup(0);
539 * this function is called for response whenever a call state changes.
541 void admin_call_response(int adminid, int message, const char *connected, int cause, int location, int notify_progress)
543 struct admin_list *admin;
544 struct admin_queue *response, **responsep; /* response pointer */
546 /* searching for admin id
547 * maybe there is no admin instance, because the calling port was not
548 * initiated by admin_call */
551 if (adminid == admin->sockserial)
558 /* seek to end of response list */
559 response = admin->response;
560 responsep = &admin->response;
562 responsep = &response->next;
563 response = response->next;
566 /* create state response */
567 response = (struct admin_queue *)MALLOC(sizeof(struct admin_queue)+sizeof(admin_message));
571 response->am[0].message = message;
573 SCPY(response->am[0].u.call.callerid, connected);
574 response->am[0].u.call.cause = cause;
575 response->am[0].u.call.location = location;
576 response->am[0].u.call.notify_progress = notify_progress;
578 /* attach to response chain */
579 *responsep = response;
580 responsep = &response->next;
581 admin->fd.when |= LCR_FD_WRITE;
586 * send data to the remote socket join instance
588 int admin_message_to_join(struct admin_msg *msg, struct admin_list *admin)
591 struct admin_list *temp;
594 if (msg->type == MESSAGE_HELLO) {
595 if (admin->remote_name[0]) {
596 PERROR("Remote application repeats hello message.\n");
599 /* look for second application */
602 if (!strcmp(temp->remote_name, msg->param.hello.application))
607 PERROR("Remote application connects twice??? (ignoring)\n");
610 /* set remote socket instance */
611 SCPY(admin->remote_name, msg->param.hello.application);
619 "REMOTE APP registers");
620 add_trace("app", "name", "%s", admin->remote_name);
625 /* check we have no application name */
626 if (!admin->remote_name[0]) {
627 PERROR("Remote application did not send us a hello message.\n");
632 if (msg->type == MESSAGE_NEWREF) {
633 /* create new join instance */
634 join = new JoinRemote(0, admin->remote_name, admin->sock); // must have no serial, because no endpoint is connected
636 FATAL("No memory for remote join instance\n");
643 * no ref given for *_ack */
644 if (msg->type == MESSAGE_BCHANNEL)
645 if (msg->param.bchannel.type == BCHANNEL_ASSIGN_ACK
646 || msg->param.bchannel.type == BCHANNEL_REMOVE_ACK
647 || msg->param.bchannel.type == BCHANNEL_RELEASE) {
648 /* no ref, but address */
649 message_bchannel_from_remote(NULL, msg->param.bchannel.type, msg->param.bchannel.handle);
655 PERROR("Remote application did not send us a valid ref with a message.\n");
659 /* find join instance */
662 if (join->j_serial == msg->ref)
667 PDEBUG(DEBUG_LOG, "No join found with serial %d. (May have been already released.)\n", msg->ref);
671 /* check application */
672 if (join->j_type != JOIN_TYPE_REMOTE) {
673 PERROR("Ref %d does not belong to a remote join instance.\n", msg->ref);
676 if (admin->sock != ((class JoinRemote *)join)->j_remote_id) {
677 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);
682 ((class JoinRemote *)join)->message_remote(msg->type, &msg->param);
689 * this function is called for every message to remote socket
691 int admin_message_from_join(int remote_id, unsigned int ref, int message_type, union parameter *param)
693 struct admin_list *admin;
694 struct admin_queue **responsep; /* response pointer */
696 /* searching for admin id
697 * maybe there is no given remote application
701 if (admin->remote_name[0] && admin->sock==remote_id)
705 /* no given remote application connected */
709 /* seek to end of response list */
710 responsep = &admin->response;
712 responsep = &(*responsep)->next;
715 /* create state response */
716 *responsep = (struct admin_queue *)MALLOC(sizeof(struct admin_queue)+sizeof(admin_message));
718 (*responsep)->num = 1;
721 (*responsep)->am[0].message = ADMIN_MESSAGE;
722 (*responsep)->am[0].u.msg.type = message_type;
723 (*responsep)->am[0].u.msg.ref = ref;
724 memcpy(&(*responsep)->am[0].u.msg.param, param, sizeof(union parameter));
725 admin->fd.when |= LCR_FD_WRITE;
733 int admin_state(struct admin_queue **responsep)
736 class EndpointAppPBX *apppbx;
739 struct interface *interface;
740 struct interface_port *ifport;
741 struct mISDNport *mISDNport;
745 struct admin_queue *response;
746 struct admin_list *admin;
750 /* create state response */
751 response = (struct admin_queue *)MALLOC(sizeof(struct admin_queue)+sizeof(admin_message));
755 response->am[0].message = ADMIN_RESPONSE_STATE;
757 SCPY(response->am[0].u.s.version_string, VERSION_STRING);
760 now_tm = localtime(&now);
761 memcpy(&response->am[0].u.s.tm, now_tm, sizeof(struct tm));
763 SCPY(response->am[0].u.s.logfile, options.log);
764 /* interface count */
766 interface = interface_first;
768 ifport = interface->ifport;
771 ifport = ifport->next;
773 interface = interface->next;
775 response->am[0].u.s.interfaces = i;
776 /* remote connection count */
780 if (admin->remote_name[0])
784 response->am[0].u.s.remotes = i;
792 response->am[0].u.s.joins = i;
794 apppbx = apppbx_first;
798 apppbx = apppbx->next;
800 response->am[0].u.s.epoints = i;
808 response->am[0].u.s.ports = i;
809 /* attach to response chain */
810 *responsep = response;
811 responsep = &response->next;
813 /* create response for all instances */
814 num = (response->am[0].u.s.interfaces)
815 + (response->am[0].u.s.remotes)
816 + (response->am[0].u.s.joins)
817 + (response->am[0].u.s.epoints)
818 + (response->am[0].u.s.ports);
821 response = (struct admin_queue *)MALLOC(sizeof(admin_queue)+(num*sizeof(admin_message)));
824 *responsep = response;
825 responsep = &response->next;
826 interface = interface_first;
829 ifport = interface->ifport;
832 response->am[num].message = ADMIN_RESPONSE_S_INTERFACE;
834 SCPY(response->am[num].u.i.interface_name, interface->name);
836 response->am[num].u.i.portnum = ifport->portnum;
838 SCPY(response->am[num].u.i.portname, ifport->portname);
840 response->am[num].u.i.extension = interface->extension;
842 response->am[num].u.i.block = ifport->block;
843 if (ifport->mISDNport) {
844 mISDNport = ifport->mISDNport;
847 response->am[num].u.i.ptp = mISDNport->ptp;
849 response->am[num].u.i.l1hold = mISDNport->l1hold;
851 response->am[num].u.i.l2hold = mISDNport->l2hold;
853 response->am[num].u.i.ntmode = mISDNport->ntmode;
855 response->am[num].u.i.pri = mISDNport->pri;
857 response->am[num].u.i.use = mISDNport->use;
859 response->am[num].u.i.l1link = mISDNport->l1link;
861 response->am[num].u.i.l2link = mISDNport->l2link;
862 memcpy(response->am[num].u.i.l2mask, mISDNport->l2mask, 16);
864 response->am[num].u.i.los = mISDNport->los;
866 response->am[num].u.i.ais = mISDNport->ais;
868 response->am[num].u.i.rdi = mISDNport->rdi;
870 response->am[num].u.i.slip_tx = mISDNport->slip_tx;
871 response->am[num].u.i.slip_rx = mISDNport->slip_rx;
873 response->am[num].u.i.channels = mISDNport->b_num;
877 while(i < mISDNport->b_num) {
878 response->am[num].u.i.busy[i] = mISDNport->b_state[i];
879 if (mISDNport->b_port[i])
880 response->am[num].u.i.port[i] = mISDNport->b_port[i]->p_serial;
881 response->am[num].u.i.mode[i] = mISDNport->b_mode[i];
887 ifport = ifport->next;
889 interface = interface->next;
892 /* create response for all remotes */
895 if (admin->remote_name[0]) {
897 response->am[num].message = ADMIN_RESPONSE_S_REMOTE;
899 SCPY(response->am[num].u.r.name, admin->remote_name);
906 /* create response for all joins */
910 response->am[num].message = ADMIN_RESPONSE_S_JOIN;
912 response->am[num].u.j.serial = join->j_serial;
914 if (join->j_type == JOIN_TYPE_PBX)
915 response->am[num].u.j.partyline = ((class JoinPBX *)join)->j_partyline;
916 /* remote application */
917 if (join->j_type == JOIN_TYPE_REMOTE)
918 SCPY(response->am[num].u.j.remote, ((class JoinRemote *)join)->j_remote_name);
924 /* create response for all endpoint */
925 apppbx = apppbx_first;
928 response->am[num].message = ADMIN_RESPONSE_S_EPOINT;
930 response->am[num].u.e.serial = apppbx->ea_endpoint->ep_serial;
932 response->am[num].u.e.join = apppbx->ea_endpoint->ep_join_id;
933 /* rx notification */
934 response->am[num].u.e.rx_state = apppbx->e_rx_state;
935 /* tx notification */
936 response->am[num].u.e.tx_state = apppbx->e_tx_state;
938 switch(apppbx->e_state) {
939 case EPOINT_STATE_IN_SETUP:
940 response->am[num].u.e.state = ADMIN_STATE_IN_SETUP;
942 case EPOINT_STATE_OUT_SETUP:
943 response->am[num].u.e.state = ADMIN_STATE_OUT_SETUP;
945 case EPOINT_STATE_IN_OVERLAP:
946 response->am[num].u.e.state = ADMIN_STATE_IN_OVERLAP;
948 case EPOINT_STATE_OUT_OVERLAP:
949 response->am[num].u.e.state = ADMIN_STATE_OUT_OVERLAP;
951 case EPOINT_STATE_IN_PROCEEDING:
952 response->am[num].u.e.state = ADMIN_STATE_IN_PROCEEDING;
954 case EPOINT_STATE_OUT_PROCEEDING:
955 response->am[num].u.e.state = ADMIN_STATE_OUT_PROCEEDING;
957 case EPOINT_STATE_IN_ALERTING:
958 response->am[num].u.e.state = ADMIN_STATE_IN_ALERTING;
960 case EPOINT_STATE_OUT_ALERTING:
961 response->am[num].u.e.state = ADMIN_STATE_OUT_ALERTING;
963 case EPOINT_STATE_CONNECT:
964 response->am[num].u.e.state = ADMIN_STATE_CONNECT;
966 case EPOINT_STATE_IN_DISCONNECT:
967 response->am[num].u.e.state = ADMIN_STATE_IN_DISCONNECT;
969 case EPOINT_STATE_OUT_DISCONNECT:
970 response->am[num].u.e.state = ADMIN_STATE_OUT_DISCONNECT;
973 response->am[num].u.e.state = ADMIN_STATE_IDLE;
976 SCPY(response->am[num].u.e.terminal, apppbx->e_ext.number);
978 SCPY(response->am[num].u.e.callerid, apppbx->e_callerinfo.id);
980 SCPY(response->am[num].u.e.dialing, apppbx->e_dialinginfo.id);
982 if (apppbx->e_action)
983 SCPY(response->am[num].u.e.action, action_defs[apppbx->e_action->index].name);
984 // if (apppbx->e_action)
985 // printf("action=%s\n",action_defs[apppbx->e_action->index].name);
987 response->am[num].u.e.park = apppbx->ea_endpoint->ep_park;
988 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))
989 memcpy(response->am[num].u.e.park_callid, apppbx->ea_endpoint->ep_park_callid, apppbx->ea_endpoint->ep_park_len);
990 response->am[num].u.e.park_len = apppbx->ea_endpoint->ep_park_len;
992 if (apppbx->e_crypt == CRYPT_ON)
993 response->am[num].u.e.crypt = 1;
995 apppbx = apppbx->next;
999 /* create response for all ports */
1003 response->am[num].message = ADMIN_RESPONSE_S_PORT;
1005 response->am[num].u.p.serial = port->p_serial;
1007 SCPY(response->am[num].u.p.name, port->p_name);
1009 response->am[num].u.p.epoint = ACTIVE_EPOINT(port->p_epointlist);
1011 switch(port->p_state) {
1012 case PORT_STATE_IN_SETUP:
1013 response->am[num].u.p.state = ADMIN_STATE_IN_SETUP;
1015 case PORT_STATE_OUT_SETUP:
1016 response->am[num].u.p.state = ADMIN_STATE_OUT_SETUP;
1018 case PORT_STATE_IN_OVERLAP:
1019 response->am[num].u.p.state = ADMIN_STATE_IN_OVERLAP;
1021 case PORT_STATE_OUT_OVERLAP:
1022 response->am[num].u.p.state = ADMIN_STATE_OUT_OVERLAP;
1024 case PORT_STATE_IN_PROCEEDING:
1025 response->am[num].u.p.state = ADMIN_STATE_IN_PROCEEDING;
1027 case PORT_STATE_OUT_PROCEEDING:
1028 response->am[num].u.p.state = ADMIN_STATE_OUT_PROCEEDING;
1030 case PORT_STATE_IN_ALERTING:
1031 response->am[num].u.p.state = ADMIN_STATE_IN_ALERTING;
1033 case PORT_STATE_OUT_ALERTING:
1034 response->am[num].u.p.state = ADMIN_STATE_OUT_ALERTING;
1036 case PORT_STATE_CONNECT:
1037 response->am[num].u.p.state = ADMIN_STATE_CONNECT;
1039 case PORT_STATE_IN_DISCONNECT:
1040 response->am[num].u.p.state = ADMIN_STATE_IN_DISCONNECT;
1042 case PORT_STATE_OUT_DISCONNECT:
1043 response->am[num].u.p.state = ADMIN_STATE_OUT_DISCONNECT;
1045 case PORT_STATE_RELEASE:
1046 response->am[num].u.p.state = ADMIN_STATE_RELEASE;
1049 response->am[num].u.p.state = ADMIN_STATE_IDLE;
1052 if ((port->p_type & PORT_CLASS_mISDN_MASK) == PORT_CLASS_DSS1) {
1053 response->am[num].u.p.isdn = 1;
1054 pdss1 = (class Pdss1 *)port;
1055 response->am[num].u.p.isdn_chan = pdss1->p_m_b_channel;
1056 response->am[num].u.p.isdn_hold = pdss1->p_m_hold;
1057 response->am[num].u.p.isdn_ces = pdss1->p_m_d_ces;
1066 int sockserial = 1; // must start with 1, because 0 is used if no serial is set
1068 * handle admin socket (non blocking)
1070 int admin_handle_con(struct lcr_fd *fd, unsigned int what, void *instance, int index);
1072 int admin_handle(struct lcr_fd *fd, unsigned int what, void *instance, int index)
1075 socklen_t sock_len = sizeof(sock_address);
1076 struct admin_list *admin;
1078 /* check for new incoming connections */
1079 if ((new_sock = accept(sock, (struct sockaddr *)&sock_address, &sock_len)) >= 0) {
1080 /* insert new socket */
1081 admin = (struct admin_list *)MALLOC(sizeof(struct admin_list));
1084 admin->sockserial = sockserial++;
1085 admin->next = admin_first;
1086 admin_first = admin;
1087 admin->sock = new_sock;
1088 admin->fd.fd = new_sock;
1089 register_fd(&admin->fd, LCR_FD_READ | LCR_FD_EXCEPT, admin_handle_con, admin, 0);
1091 if (errno != EWOULDBLOCK) {
1092 PERROR("Failed to accept connection from socket \"%s\". (errno=%d) Closing socket.\n", sock_address.sun_path, errno);
1101 int admin_handle_con(struct lcr_fd *fd, unsigned int what, void *instance, int index)
1103 struct admin_list *admin = (struct admin_list *)instance;
1105 struct admin_message msg;
1107 struct Endpoint *epoint;
1109 if ((what & LCR_FD_READ)) {
1111 len = read(admin->sock, &msg, sizeof(msg));
1114 PDEBUG(DEBUG_LOG, "Broken pipe on socket %d. (errno=%d).\n", admin->sock, errno);
1115 free_connection(admin);
1121 /*release endpoint if exists */
1122 if (admin->epointid) {
1123 epoint = find_epoint_id(admin->epointid);
1125 ((class DEFAULT_ENDPOINT_APP *)epoint->ep_app)->
1126 release(RELEASE_ALL, LOCATION_PRIVATE_LOCAL, CAUSE_NORMAL, LOCATION_PRIVATE_LOCAL, CAUSE_NORMAL, 0);
1130 free_connection(admin);
1133 if (len != sizeof(msg)) {
1134 PERROR("Short/long read on socket %d. (len=%d != size=%d).\n", admin->sock, len, sizeof(msg));
1135 free_connection(admin);
1138 /* process socket command */
1139 if (admin->response && msg.message != ADMIN_MESSAGE) {
1140 PERROR("Data from socket %d while sending response.\n", admin->sock);
1141 free_connection(admin);
1144 switch (msg.message) {
1145 case ADMIN_REQUEST_CMD_INTERFACE:
1146 if (admin_interface(&admin->response) < 0) {
1147 PERROR("Failed to create dial response for socket %d.\n", admin->sock);
1148 goto response_error;
1150 admin->fd.when |= LCR_FD_WRITE;
1153 case ADMIN_REQUEST_CMD_ROUTE:
1154 if (admin_route(&admin->response) < 0) {
1155 PERROR("Failed to create dial response for socket %d.\n", admin->sock);
1156 goto response_error;
1158 admin->fd.when |= LCR_FD_WRITE;
1161 case ADMIN_REQUEST_CMD_DIAL:
1162 if (admin_dial(&admin->response, msg.u.x.message) < 0) {
1163 PERROR("Failed to create dial response for socket %d.\n", admin->sock);
1164 goto response_error;
1166 admin->fd.when |= LCR_FD_WRITE;
1169 case ADMIN_REQUEST_CMD_RELEASE:
1170 if (admin_release(&admin->response, msg.u.x.message) < 0) {
1171 PERROR("Failed to create release response for socket %d.\n", admin->sock);
1172 goto response_error;
1174 admin->fd.when |= LCR_FD_WRITE;
1177 case ADMIN_REQUEST_STATE:
1178 if (admin_state(&admin->response) < 0) {
1179 PERROR("Failed to create state response for socket %d.\n", admin->sock);
1180 goto response_error;
1182 admin->fd.when |= LCR_FD_WRITE;
1185 case ADMIN_TRACE_REQUEST:
1186 if (admin_trace(admin, &msg.u.trace_req) < 0) {
1187 PERROR("Failed to create trace response for socket %d.\n", admin->sock);
1188 goto response_error;
1190 admin->fd.when |= LCR_FD_WRITE;
1193 case ADMIN_REQUEST_CMD_BLOCK:
1194 if (admin_block(&admin->response, msg.u.x.portnum, msg.u.x.block) < 0) {
1195 PERROR("Failed to create block response for socket %d.\n", admin->sock);
1196 goto response_error;
1198 admin->fd.when |= LCR_FD_WRITE;
1202 if (admin_message_to_join(&msg.u.msg, admin) < 0) {
1203 PERROR("Failed to deliver message for socket %d.\n", admin->sock);
1204 goto response_error;
1208 case ADMIN_CALL_SETUP:
1209 if (admin_call(admin, &msg) < 0) {
1210 PERROR("Failed to create call for socket %d.\n", admin->sock);
1212 free_connection(admin);
1218 PERROR("Invalid message %d from socket %d.\n", msg.message, admin->sock);
1219 free_connection(admin);
1224 if ((what & LCR_FD_WRITE)) {
1226 if (admin->response) {
1227 len = write(admin->sock, ((unsigned char *)(admin->response->am))+admin->response->offset, sizeof(struct admin_message)*(admin->response->num)-admin->response->offset);
1233 if (len < (int)(sizeof(struct admin_message)*(admin->response->num) - admin->response->offset)) {
1234 admin->response->offset+=len;
1237 temp = admin->response;
1238 admin->response = admin->response->next;
1243 admin->fd.when &= ~LCR_FD_WRITE;