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;
746 struct admin_queue *response;
747 struct admin_list *admin;
751 /* create state response */
752 response = (struct admin_queue *)MALLOC(sizeof(struct admin_queue)+sizeof(admin_message));
756 response->am[0].message = ADMIN_RESPONSE_STATE;
758 SCPY(response->am[0].u.s.version_string, VERSION_STRING);
761 now_tm = localtime(&now);
762 memcpy(&response->am[0].u.s.tm, now_tm, sizeof(struct tm));
764 SCPY(response->am[0].u.s.logfile, options.log);
765 /* interface count */
767 interface = interface_first;
769 ifport = interface->ifport;
772 ifport = ifport->next;
774 interface = interface->next;
776 response->am[0].u.s.interfaces = i;
777 /* remote connection count */
781 if (admin->remote_name[0])
785 response->am[0].u.s.remotes = i;
793 response->am[0].u.s.joins = i;
795 apppbx = apppbx_first;
799 apppbx = apppbx->next;
801 response->am[0].u.s.epoints = i;
809 response->am[0].u.s.ports = i;
810 /* attach to response chain */
811 *responsep = response;
812 responsep = &response->next;
814 /* create response for all instances */
815 num = (response->am[0].u.s.interfaces)
816 + (response->am[0].u.s.remotes)
817 + (response->am[0].u.s.joins)
818 + (response->am[0].u.s.epoints)
819 + (response->am[0].u.s.ports);
822 response = (struct admin_queue *)MALLOC(sizeof(admin_queue)+(num*sizeof(admin_message)));
825 *responsep = response;
826 responsep = &response->next;
827 interface = interface_first;
830 ifport = interface->ifport;
833 response->am[num].message = ADMIN_RESPONSE_S_INTERFACE;
835 SCPY(response->am[num].u.i.interface_name, interface->name);
837 response->am[num].u.i.portnum = ifport->portnum;
839 SCPY(response->am[num].u.i.portname, ifport->portname);
841 response->am[num].u.i.extension = interface->extension;
843 response->am[num].u.i.block = ifport->block;
844 if (ifport->mISDNport) {
845 mISDNport = ifport->mISDNport;
848 response->am[num].u.i.ptp = mISDNport->ptp;
850 response->am[num].u.i.l1hold = mISDNport->l1hold;
852 response->am[num].u.i.l2hold = mISDNport->l2hold;
854 response->am[num].u.i.ntmode = mISDNport->ntmode;
856 response->am[num].u.i.pri = mISDNport->pri;
858 response->am[num].u.i.use = mISDNport->use;
860 response->am[num].u.i.l1link = mISDNport->l1link;
862 response->am[num].u.i.l2link = mISDNport->l2link;
863 memcpy(response->am[num].u.i.l2mask, mISDNport->l2mask, 16);
865 response->am[num].u.i.los = mISDNport->los;
867 response->am[num].u.i.ais = mISDNport->ais;
869 response->am[num].u.i.rdi = mISDNport->rdi;
871 response->am[num].u.i.slip_tx = mISDNport->slip_tx;
872 response->am[num].u.i.slip_rx = mISDNport->slip_rx;
874 response->am[num].u.i.channels = mISDNport->b_num;
878 while(i < mISDNport->b_num) {
879 response->am[num].u.i.busy[i] = mISDNport->b_state[i];
880 if (mISDNport->b_port[i])
881 response->am[num].u.i.port[i] = mISDNport->b_port[i]->p_serial;
882 response->am[num].u.i.mode[i] = mISDNport->b_mode[i];
888 ifport = ifport->next;
890 interface = interface->next;
893 /* create response for all remotes */
896 if (admin->remote_name[0]) {
898 response->am[num].message = ADMIN_RESPONSE_S_REMOTE;
900 SCPY(response->am[num].u.r.name, admin->remote_name);
907 /* create response for all joins */
911 response->am[num].message = ADMIN_RESPONSE_S_JOIN;
913 response->am[num].u.j.serial = join->j_serial;
915 if (join->j_type == JOIN_TYPE_PBX)
916 response->am[num].u.j.partyline = ((class JoinPBX *)join)->j_partyline;
917 /* remote application */
918 if (join->j_type == JOIN_TYPE_REMOTE)
919 SCPY(response->am[num].u.j.remote, ((class JoinRemote *)join)->j_remote_name);
925 /* create response for all endpoint */
926 apppbx = apppbx_first;
929 response->am[num].message = ADMIN_RESPONSE_S_EPOINT;
931 response->am[num].u.e.serial = apppbx->ea_endpoint->ep_serial;
933 response->am[num].u.e.join = apppbx->ea_endpoint->ep_join_id;
934 /* rx notification */
935 response->am[num].u.e.rx_state = apppbx->e_rx_state;
936 /* tx notification */
937 response->am[num].u.e.tx_state = apppbx->e_tx_state;
939 switch(apppbx->e_state) {
940 case EPOINT_STATE_IN_SETUP:
941 response->am[num].u.e.state = ADMIN_STATE_IN_SETUP;
943 case EPOINT_STATE_OUT_SETUP:
944 response->am[num].u.e.state = ADMIN_STATE_OUT_SETUP;
946 case EPOINT_STATE_IN_OVERLAP:
947 response->am[num].u.e.state = ADMIN_STATE_IN_OVERLAP;
949 case EPOINT_STATE_OUT_OVERLAP:
950 response->am[num].u.e.state = ADMIN_STATE_OUT_OVERLAP;
952 case EPOINT_STATE_IN_PROCEEDING:
953 response->am[num].u.e.state = ADMIN_STATE_IN_PROCEEDING;
955 case EPOINT_STATE_OUT_PROCEEDING:
956 response->am[num].u.e.state = ADMIN_STATE_OUT_PROCEEDING;
958 case EPOINT_STATE_IN_ALERTING:
959 response->am[num].u.e.state = ADMIN_STATE_IN_ALERTING;
961 case EPOINT_STATE_OUT_ALERTING:
962 response->am[num].u.e.state = ADMIN_STATE_OUT_ALERTING;
964 case EPOINT_STATE_CONNECT:
965 response->am[num].u.e.state = ADMIN_STATE_CONNECT;
967 case EPOINT_STATE_IN_DISCONNECT:
968 response->am[num].u.e.state = ADMIN_STATE_IN_DISCONNECT;
970 case EPOINT_STATE_OUT_DISCONNECT:
971 response->am[num].u.e.state = ADMIN_STATE_OUT_DISCONNECT;
974 response->am[num].u.e.state = ADMIN_STATE_IDLE;
977 SCPY(response->am[num].u.e.terminal, apppbx->e_ext.number);
979 SCPY(response->am[num].u.e.callerid, apppbx->e_callerinfo.id);
981 SCPY(response->am[num].u.e.dialing, apppbx->e_dialinginfo.id);
983 if (apppbx->e_action)
984 SCPY(response->am[num].u.e.action, action_defs[apppbx->e_action->index].name);
985 // if (apppbx->e_action)
986 // printf("action=%s\n",action_defs[apppbx->e_action->index].name);
988 response->am[num].u.e.park = apppbx->ea_endpoint->ep_park;
989 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))
990 memcpy(response->am[num].u.e.park_callid, apppbx->ea_endpoint->ep_park_callid, apppbx->ea_endpoint->ep_park_len);
991 response->am[num].u.e.park_len = apppbx->ea_endpoint->ep_park_len;
993 if (apppbx->e_crypt == CRYPT_ON)
994 response->am[num].u.e.crypt = 1;
996 apppbx = apppbx->next;
1000 /* create response for all ports */
1004 response->am[num].message = ADMIN_RESPONSE_S_PORT;
1006 response->am[num].u.p.serial = port->p_serial;
1008 SCPY(response->am[num].u.p.name, port->p_name);
1010 response->am[num].u.p.epoint = ACTIVE_EPOINT(port->p_epointlist);
1012 switch(port->p_state) {
1013 case PORT_STATE_IN_SETUP:
1014 response->am[num].u.p.state = ADMIN_STATE_IN_SETUP;
1016 case PORT_STATE_OUT_SETUP:
1017 response->am[num].u.p.state = ADMIN_STATE_OUT_SETUP;
1019 case PORT_STATE_IN_OVERLAP:
1020 response->am[num].u.p.state = ADMIN_STATE_IN_OVERLAP;
1022 case PORT_STATE_OUT_OVERLAP:
1023 response->am[num].u.p.state = ADMIN_STATE_OUT_OVERLAP;
1025 case PORT_STATE_IN_PROCEEDING:
1026 response->am[num].u.p.state = ADMIN_STATE_IN_PROCEEDING;
1028 case PORT_STATE_OUT_PROCEEDING:
1029 response->am[num].u.p.state = ADMIN_STATE_OUT_PROCEEDING;
1031 case PORT_STATE_IN_ALERTING:
1032 response->am[num].u.p.state = ADMIN_STATE_IN_ALERTING;
1034 case PORT_STATE_OUT_ALERTING:
1035 response->am[num].u.p.state = ADMIN_STATE_OUT_ALERTING;
1037 case PORT_STATE_CONNECT:
1038 response->am[num].u.p.state = ADMIN_STATE_CONNECT;
1040 case PORT_STATE_IN_DISCONNECT:
1041 response->am[num].u.p.state = ADMIN_STATE_IN_DISCONNECT;
1043 case PORT_STATE_OUT_DISCONNECT:
1044 response->am[num].u.p.state = ADMIN_STATE_OUT_DISCONNECT;
1046 case PORT_STATE_RELEASE:
1047 response->am[num].u.p.state = ADMIN_STATE_RELEASE;
1050 response->am[num].u.p.state = ADMIN_STATE_IDLE;
1053 if ((port->p_type & PORT_CLASS_mISDN_MASK) == PORT_CLASS_DSS1) {
1054 response->am[num].u.p.isdn = 1;
1055 pdss1 = (class Pdss1 *)port;
1056 response->am[num].u.p.isdn_chan = pdss1->p_m_b_channel;
1057 response->am[num].u.p.isdn_hold = pdss1->p_m_hold;
1058 response->am[num].u.p.isdn_ces = pdss1->p_m_d_ces;
1067 int sockserial = 1; // must start with 1, because 0 is used if no serial is set
1069 * handle admin socket (non blocking)
1071 int admin_handle_con(struct lcr_fd *fd, unsigned int what, void *instance, int index);
1073 int admin_handle(struct lcr_fd *fd, unsigned int what, void *instance, int index)
1076 socklen_t sock_len = sizeof(sock_address);
1077 struct admin_list *admin;
1079 /* check for new incoming connections */
1080 if ((new_sock = accept(sock, (struct sockaddr *)&sock_address, &sock_len)) >= 0) {
1081 /* insert new socket */
1082 admin = (struct admin_list *)MALLOC(sizeof(struct admin_list));
1085 admin->sockserial = sockserial++;
1086 admin->next = admin_first;
1087 admin_first = admin;
1088 admin->sock = new_sock;
1089 admin->fd.fd = new_sock;
1090 register_fd(&admin->fd, LCR_FD_READ | LCR_FD_EXCEPT, admin_handle_con, admin, 0);
1092 if (errno != EWOULDBLOCK) {
1093 PERROR("Failed to accept connection from socket \"%s\". (errno=%d) Closing socket.\n", sock_address.sun_path, errno);
1102 int admin_handle_con(struct lcr_fd *fd, unsigned int what, void *instance, int index)
1104 struct admin_list *admin = (struct admin_list *)instance;
1106 struct admin_message msg;
1108 struct Endpoint *epoint;
1110 if ((what & LCR_FD_READ)) {
1112 len = read(admin->sock, &msg, sizeof(msg));
1115 PDEBUG(DEBUG_LOG, "Broken pipe on socket %d. (errno=%d).\n", admin->sock, errno);
1116 free_connection(admin);
1122 /*release endpoint if exists */
1123 if (admin->epointid) {
1124 epoint = find_epoint_id(admin->epointid);
1126 ((class DEFAULT_ENDPOINT_APP *)epoint->ep_app)->
1127 release(RELEASE_ALL, LOCATION_PRIVATE_LOCAL, CAUSE_NORMAL, LOCATION_PRIVATE_LOCAL, CAUSE_NORMAL, 0);
1131 free_connection(admin);
1134 if (len != sizeof(msg)) {
1135 PERROR("Short/long read on socket %d. (len=%d != size=%d).\n", admin->sock, len, sizeof(msg));
1136 free_connection(admin);
1139 /* process socket command */
1140 if (admin->response && msg.message != ADMIN_MESSAGE) {
1141 PERROR("Data from socket %d while sending response.\n", admin->sock);
1142 free_connection(admin);
1145 switch (msg.message) {
1146 case ADMIN_REQUEST_CMD_INTERFACE:
1147 if (admin_interface(&admin->response) < 0) {
1148 PERROR("Failed to create dial response for socket %d.\n", admin->sock);
1149 goto response_error;
1151 admin->fd.when |= LCR_FD_WRITE;
1154 case ADMIN_REQUEST_CMD_ROUTE:
1155 if (admin_route(&admin->response) < 0) {
1156 PERROR("Failed to create dial response for socket %d.\n", admin->sock);
1157 goto response_error;
1159 admin->fd.when |= LCR_FD_WRITE;
1162 case ADMIN_REQUEST_CMD_DIAL:
1163 if (admin_dial(&admin->response, msg.u.x.message) < 0) {
1164 PERROR("Failed to create dial response for socket %d.\n", admin->sock);
1165 goto response_error;
1167 admin->fd.when |= LCR_FD_WRITE;
1170 case ADMIN_REQUEST_CMD_RELEASE:
1171 if (admin_release(&admin->response, msg.u.x.message) < 0) {
1172 PERROR("Failed to create release response for socket %d.\n", admin->sock);
1173 goto response_error;
1175 admin->fd.when |= LCR_FD_WRITE;
1178 case ADMIN_REQUEST_STATE:
1179 if (admin_state(&admin->response) < 0) {
1180 PERROR("Failed to create state response for socket %d.\n", admin->sock);
1181 goto response_error;
1183 admin->fd.when |= LCR_FD_WRITE;
1186 case ADMIN_TRACE_REQUEST:
1187 if (admin_trace(admin, &msg.u.trace_req) < 0) {
1188 PERROR("Failed to create trace response for socket %d.\n", admin->sock);
1189 goto response_error;
1191 admin->fd.when |= LCR_FD_WRITE;
1194 case ADMIN_REQUEST_CMD_BLOCK:
1195 if (admin_block(&admin->response, msg.u.x.portnum, msg.u.x.block) < 0) {
1196 PERROR("Failed to create block response for socket %d.\n", admin->sock);
1197 goto response_error;
1199 admin->fd.when |= LCR_FD_WRITE;
1203 if (admin_message_to_join(&msg.u.msg, admin) < 0) {
1204 PERROR("Failed to deliver message for socket %d.\n", admin->sock);
1205 goto response_error;
1209 case ADMIN_CALL_SETUP:
1210 if (admin_call(admin, &msg) < 0) {
1211 PERROR("Failed to create call for socket %d.\n", admin->sock);
1213 free_connection(admin);
1219 PERROR("Invalid message %d from socket %d.\n", msg.message, admin->sock);
1220 free_connection(admin);
1225 if ((what & LCR_FD_WRITE)) {
1227 if (admin->response) {
1228 len = write(admin->sock, ((unsigned char *)(admin->response->am))+admin->response->offset, sizeof(struct admin_message)*(admin->response->num)-admin->response->offset);
1234 if (len < (int)(sizeof(struct admin_message)*(admin->response->num) - admin->response->offset)) {
1235 admin->response->offset+=len;
1238 temp = admin->response;
1239 admin->response = admin->response->next;
1244 admin->fd.when &= ~LCR_FD_WRITE;