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;
25 * initialize admin socket
31 /* open and bind socket */
32 if ((sock = socket(PF_UNIX, SOCK_STREAM, 0)) < 0) {
33 PERROR("Failed to create admin socket. (errno=%d)\n", errno);
37 memset(&sock_address, 0, sizeof(sock_address));
38 SPRINT(socket_name, SOCKET_NAME, options.lock);
39 sock_address.sun_family = AF_UNIX;
40 UCPY(sock_address.sun_path, socket_name);
42 if (bind(sock, (struct sockaddr *)(&sock_address), SUN_LEN(&sock_address)) < 0) {
47 PERROR("Failed to bind admin socket to \"%s\". (errno=%d)\n", sock_address.sun_path, errno);
50 if (listen(sock, 5) < 0) {
55 PERROR("Failed to listen to socket \"%s\". (errno=%d)\n", sock_address.sun_path, errno);
58 if (ioctl(sock, FIONBIO, (unsigned char *)(&on)) < 0) {
63 PERROR("Failed to set socket \"%s\" into non-blocking mode. (errno=%d)\n", sock_address.sun_path, errno);
66 if (chmod(socket_name, options.socketrights) < 0) {
67 PERROR("Failed to change socket rigts to %d. (errno=%d)\n", options.socketrights, 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;
86 /* free remote joins */
87 if (admin->remote_name[0]) {
95 "REMOTE APP release");
96 add_trace("app", "name", "%s", admin->remote_name);
98 /* release all exported channels */
99 mISDNport = mISDNport_first;
102 ii = mISDNport->b_num;
104 if (mISDNport->b_remote_id[i] == admin->sock) {
105 mISDNport->b_state[i] = B_STATE_IDLE;
106 mISDNport->b_timer[i] = 0;
107 mISDNport->b_remote_id[i] = 0;
108 mISDNport->b_remote_ref[i] = 0;
112 mISDNport = mISDNport->next;
117 joinnext = join->next;
118 if (join->j_type==JOIN_TYPE_REMOTE) if (((class JoinRemote *)join)->j_remote_id == admin->sock) {
119 memset(¶m, 0, sizeof(param));
120 param.disconnectinfo.cause = CAUSE_OUTOFORDER;
121 param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
122 ((class JoinRemote *)join)->message_remote(MESSAGE_RELEASE, ¶m);
123 /* join is now destroyed, so we go to next join */
129 if (admin->sock >= 0) {
133 // printf("new\n", response);
134 response = admin->response;
137 // printf("%x\n", response);
138 temp = response->next;
141 response = (struct admin_queue *)temp;
143 // printf("new2\n", response);
145 // printf("new3\n", response);
151 * cleanup admin socket
153 void admin_cleanup(void)
155 struct admin_list *admin, *next;;
161 free_connection(admin);
175 * do interface reload
177 int admin_interface(struct admin_queue **responsep)
179 struct admin_queue *response; /* response pointer */
180 const char *err_txt = "";
183 if (read_interfaces()) {
185 free_interfaces(interface_first);
186 interface_first = interface_newlist;
187 interface_newlist = NULL;
189 err_txt = interface_error;
192 /* create state response */
193 response = (struct admin_queue *)MALLOC(sizeof(struct admin_queue)+sizeof(admin_message));
197 response->am[0].message = ADMIN_RESPONSE_CMD_INTERFACE;
199 response->am[0].u.x.error = err;
201 SCPY(response->am[0].u.x.message, err_txt);
202 /* attach to response chain */
203 *responsep = response;
204 responsep = &response->next;
213 int admin_route(struct admin_queue **responsep)
215 struct route_ruleset *ruleset_new;
216 struct admin_queue *response; /* response pointer */
217 char err_txt[256] = "";
222 class EndpointAppPBX *apppbx;
226 apppbx = apppbx_first;
229 apppbx = apppbx->next;
232 SPRINT(err_txt, "Cannot reload routing, because %d endpoints active\n", n);
237 if (!(ruleset_new = ruleset_parse())) {
238 SPRINT(err_txt, ruleset_error);
242 ruleset_free(ruleset_first);
243 ruleset_first = ruleset_new;
244 ruleset_main = getrulesetbyname("main");
246 SPRINT(err_txt, "Ruleset reloaded, but rule 'main' not found.\n");
249 apppbx = apppbx_first;
251 if (apppbx->e_action) {
252 switch(apppbx->e_action->index) {
253 case ACTION_INTERNAL:
254 apppbx->e_action = &action_internal;
256 case ACTION_EXTERNAL:
257 apppbx->e_action = &action_external;
260 apppbx->e_action = &action_remote;
262 case ACTION_VBOX_RECORD:
263 apppbx->e_action = &action_vbox;
265 case ACTION_PARTYLINE:
266 apppbx->e_action = &action_partyline;
271 } else if (apppbx->e_state != EPOINT_STATE_CONNECT) {
273 apppbx->e_callback = 0;
274 apppbx->e_action = NULL;
275 apppbx->release(RELEASE_ALL, LOCATION_PRIVATE_LOCAL, CAUSE_NORMAL, LOCATION_PRIVATE_LOCAL, CAUSE_NORMAL);
278 numberrize_callerinfo(apppbx->e_callerinfo.id, apppbx->e_callerinfo.ntype, options.national, options.international),
279 apppbx->e_dialinginfo.id,
282 apppbx->ea_endpoint->ep_serial,
283 "KICK (reload routing)");
287 apppbx->e_action_timeout = 0;
288 apppbx->e_rule = NULL;
289 apppbx->e_ruleset = NULL;
291 apppbx = apppbx->next;
295 /* create state response */
296 response = (struct admin_queue *)MALLOC(sizeof(struct admin_queue)+sizeof(admin_message));
300 response->am[0].message = ADMIN_RESPONSE_CMD_ROUTE;
302 response->am[0].u.x.error = err;
304 SCPY(response->am[0].u.x.message, err_txt);
305 /* attach to response chain */
306 *responsep = response;
307 responsep = &response->next;
316 int admin_dial(struct admin_queue **responsep, char *message)
318 struct extension ext; /* temporary extension's settings */
319 struct admin_queue *response; /* response pointer */
320 char *p; /* pointer to dialing digits */
322 /* create state response */
323 response = (struct admin_queue *)MALLOC(sizeof(struct admin_queue)+sizeof(admin_message));
327 response->am[0].message = ADMIN_RESPONSE_CMD_DIAL;
329 /* process request */
330 if (!(p = strchr(message,':'))) {
331 response->am[0].u.x.error = -EINVAL;
332 SPRINT(response->am[0].u.x.message, "no seperator ':' in message to seperate number from extension");
337 /* modify extension */
338 if (!read_extension(&ext, message)) {
339 response->am[0].u.x.error = -EINVAL;
340 SPRINT(response->am[0].u.x.message, "extension doesn't exist");
344 write_extension(&ext, message);
347 /* attach to response chain */
348 *responsep = response;
349 responsep = &response->next;
357 int admin_trace(struct admin_list *admin, struct admin_trace_req *trace)
359 memcpy(&admin->trace, trace, sizeof(struct admin_trace_req));
367 * 0 = make port available
368 * 1 = make port administratively blocked
370 * the result is returned:
371 * 0 = port is now available
372 * 1 = port is now blocked
373 * 2 = port cannot be loaded or has been unloaded
374 * -1 = port doesn't exist
376 int admin_block(struct admin_queue **responsep, int portnum, int block)
378 struct admin_queue *response; /* response pointer */
379 struct interface *interface;
380 struct interface_port *ifport;
382 /* create block response */
383 response = (struct admin_queue *)MALLOC(sizeof(struct admin_queue)+sizeof(admin_message));
387 response->am[0].message = ADMIN_RESPONSE_CMD_BLOCK;
388 response->am[0].u.x.portnum = portnum;
390 /* search for port */
392 interface = interface_first;
394 ifport = interface->ifport;
396 if (ifport->portnum == portnum)
398 ifport = ifport->next;
402 interface = interface->next;
404 /* not found, we return -1 */
406 response->am[0].u.x.block = -1;
407 response->am[0].u.x.error = 1;
408 SPRINT(response->am[0].u.x.message, "Port %d does not exist.", portnum);
413 if (!ifport->mISDNport) {
414 /* not loaded anyway */
416 response->am[0].u.x.block = 2;
420 /* try loading interface */
421 ifport->block = block;
424 /* port cannot load */
425 if (ifport->block >= 2) {
426 response->am[0].u.x.block = 2;
427 response->am[0].u.x.error = 1;
428 SPRINT(response->am[0].u.x.message, "Port %d will not load.", portnum);
433 response->am[0].u.x.block = ifport->block;
437 /* if we shall unload interface */
439 mISDNport_close(ifport->mISDNport);
440 ifport->mISDNport = 0;
445 /* port new blocking state */
446 ifport->block = response->am[0].u.x.block = block;
449 /* attach to response chain */
450 *responsep = response;
451 responsep = &response->next;
459 int admin_release(struct admin_queue **responsep, char *message)
462 struct admin_queue *response; /* response pointer */
463 class EndpointAppPBX *apppbx;
465 /* create state response */
466 response = (struct admin_queue *)MALLOC(sizeof(struct admin_queue)+sizeof(admin_message));
470 response->am[0].message = ADMIN_RESPONSE_CMD_RELEASE;
473 apppbx = apppbx_first;
475 if (apppbx->ea_endpoint->ep_serial == id)
477 apppbx = apppbx->next;
480 response->am[0].u.x.error = -EINVAL;
481 SPRINT(response->am[0].u.x.message, "Given endpoint %d doesn't exist.", id);
485 apppbx->e_callback = 0;
486 apppbx->release(RELEASE_ALL, LOCATION_PRIVATE_LOCAL, CAUSE_NORMAL, LOCATION_PRIVATE_LOCAL, CAUSE_NORMAL);
489 /* attach to response chain */
490 *responsep = response;
491 responsep = &response->next;
499 int admin_call(struct admin_list *admin, struct admin_message *msg)
501 class Endpoint *epoint;
502 class EndpointAppPBX *apppbx;
504 if (!(epoint = new Endpoint(0, 0)))
505 FATAL("No memory for Endpoint instance\n");
506 if (!(epoint->ep_app = apppbx = new DEFAULT_ENDPOINT_APP(epoint, 1))) // outgoing
507 FATAL("No memory for Endpoint Application instance\n");
508 apppbx->e_adminid = admin->sockserial;
509 admin->epointid = epoint->ep_serial;
510 SCPY(apppbx->e_callerinfo.id, nationalize_callerinfo(msg->u.call.callerid, &apppbx->e_callerinfo.ntype, options.national, options.international));
511 if (msg->u.call.present)
512 apppbx->e_callerinfo.present = INFO_PRESENT_ALLOWED;
514 apppbx->e_callerinfo.present = INFO_PRESENT_RESTRICTED;
515 apppbx->e_callerinfo.screen = INFO_SCREEN_NETWORK;
517 //printf("hh=%d\n", apppbx->e_capainfo.hlc);
519 apppbx->e_capainfo.bearer_capa = msg->u.call.bc_capa;
520 apppbx->e_capainfo.bearer_mode = msg->u.call.bc_mode;
521 apppbx->e_capainfo.bearer_info1 = msg->u.call.bc_info1;
522 apppbx->e_capainfo.hlc = msg->u.call.hlc;
523 apppbx->e_capainfo.exthlc = msg->u.call.exthlc;
524 SCPY(apppbx->e_dialinginfo.id, msg->u.call.dialing);
525 SCPY(apppbx->e_dialinginfo.interfaces, msg->u.call.interface);
526 apppbx->e_dialinginfo.sending_complete = 1;
528 apppbx->new_state(PORT_STATE_OUT_SETUP);
535 * this function is called for response whenever a call state changes.
537 void admin_call_response(int adminid, int message, const char *connected, int cause, int location, int notify)
539 struct admin_list *admin;
540 struct admin_queue *response, **responsep; /* response pointer */
542 /* searching for admin id
543 * maybe there is no admin instance, because the calling port was not
544 * initiated by admin_call */
547 if (adminid == admin->sockserial)
554 /* seek to end of response list */
555 response = admin->response;
556 responsep = &admin->response;
558 responsep = &response->next;
559 response = response->next;
562 /* create state response */
563 response = (struct admin_queue *)MALLOC(sizeof(struct admin_queue)+sizeof(admin_message));
567 response->am[0].message = message;
568 // printf("MESSAGE: %d\n", message);
570 SCPY(response->am[0].u.call.callerid, connected);
571 response->am[0].u.call.cause = cause;
572 response->am[0].u.call.location = location;
573 response->am[0].u.call.notify = notify;
575 /* attach to response chain */
576 *responsep = response;
577 responsep = &response->next;
582 * send data to the remote socket join instance
584 int admin_message_to_join(struct admin_msg *msg, struct admin_list *admin)
587 struct admin_list *temp;
590 if (msg->type == MESSAGE_HELLO) {
591 if (admin->remote_name[0]) {
592 PERROR("Remote application repeats hello message.\n");
595 /* look for second application */
598 if (!strcmp(temp->remote_name, msg->param.hello.application))
603 PERROR("Remote application connects twice??? (ignoring)\n");
606 /* set remote socket instance */
607 SCPY(admin->remote_name, msg->param.hello.application);
615 "REMOTE APP registers");
616 add_trace("app", "name", "%s", admin->remote_name);
621 /* check we have no application name */
622 if (!admin->remote_name[0]) {
623 PERROR("Remote application did not send us a hello message.\n");
628 if (msg->type == MESSAGE_NEWREF) {
629 /* create new join instance */
630 join = new JoinRemote(0, admin->remote_name, admin->sock); // must have no serial, because no endpoint is connected
632 FATAL("No memory for remote join instance\n");
639 * no ref given for *_ack */
640 if (msg->type == MESSAGE_BCHANNEL)
641 if (msg->param.bchannel.type == BCHANNEL_ASSIGN_ACK
642 || msg->param.bchannel.type == BCHANNEL_REMOVE_ACK
643 || msg->param.bchannel.type == BCHANNEL_RELEASE) {
644 /* no ref, but address */
645 message_bchannel_from_remote(NULL, msg->param.bchannel.type, msg->param.bchannel.handle);
651 PERROR("Remote application did not send us a valid ref with a message.\n");
655 /* find join instance */
658 if (join->j_serial == msg->ref)
663 PDEBUG(DEBUG_LOG, "No join found with serial %d. (May have been already released.)\n", msg->ref);
667 /* check application */
668 if (join->j_type != JOIN_TYPE_REMOTE) {
669 PERROR("Ref %d does not belong to a remote join instance.\n", msg->ref);
672 if (admin->sock != ((class JoinRemote *)join)->j_remote_id) {
673 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);
678 ((class JoinRemote *)join)->message_remote(msg->type, &msg->param);
685 * this function is called for every message to remote socket
687 int admin_message_from_join(int remote_id, unsigned int ref, int message_type, union parameter *param)
689 struct admin_list *admin;
690 struct admin_queue **responsep; /* response pointer */
692 /* searching for admin id
693 * maybe there is no given remote application
697 if (admin->remote_name[0] && admin->sock==remote_id)
701 /* no given remote application connected */
705 /* seek to end of response list */
706 responsep = &admin->response;
708 responsep = &(*responsep)->next;
711 /* create state response */
712 *responsep = (struct admin_queue *)MALLOC(sizeof(struct admin_queue)+sizeof(admin_message));
714 (*responsep)->num = 1;
717 (*responsep)->am[0].message = ADMIN_MESSAGE;
718 (*responsep)->am[0].u.msg.type = message_type;
719 (*responsep)->am[0].u.msg.ref = ref;
720 memcpy(&(*responsep)->am[0].u.msg.param, param, sizeof(union parameter));
729 int admin_state(struct admin_queue **responsep)
733 class EndpointAppPBX *apppbx;
736 struct interface *interface;
737 struct interface_port *ifport;
738 struct mISDNport *mISDNport;
742 struct admin_queue *response;
743 struct admin_list *admin;
745 /* create state response */
746 response = (struct admin_queue *)MALLOC(sizeof(struct admin_queue)+sizeof(admin_message));
750 response->am[0].message = ADMIN_RESPONSE_STATE;
752 SCPY(response->am[0].u.s.version_string, VERSION_STRING);
754 memcpy(&response->am[0].u.s.tm, now_tm, sizeof(struct tm));
756 SCPY(response->am[0].u.s.logfile, options.log);
757 /* interface count */
759 interface = interface_first;
761 ifport = interface->ifport;
764 ifport = ifport->next;
766 interface = interface->next;
768 response->am[0].u.s.interfaces = i;
769 /* remote connection count */
773 if (admin->remote_name[0])
777 response->am[0].u.s.remotes = i;
785 response->am[0].u.s.joins = i;
787 apppbx = apppbx_first;
791 apppbx = apppbx->next;
793 response->am[0].u.s.epoints = i;
801 response->am[0].u.s.ports = i;
802 /* attach to response chain */
803 *responsep = response;
804 responsep = &response->next;
806 /* create response for all instances */
807 num = (response->am[0].u.s.interfaces)
808 + (response->am[0].u.s.remotes)
809 + (response->am[0].u.s.joins)
810 + (response->am[0].u.s.epoints)
811 + (response->am[0].u.s.ports);
814 response = (struct admin_queue *)MALLOC(sizeof(admin_queue)+(num*sizeof(admin_message)));
817 *responsep = response;
818 responsep = &response->next;
819 interface = interface_first;
822 ifport = interface->ifport;
825 response->am[num].message = ADMIN_RESPONSE_S_INTERFACE;
827 SCPY(response->am[num].u.i.interface_name, interface->name);
829 response->am[num].u.i.portnum = ifport->portnum;
831 SCPY(response->am[num].u.i.portname, ifport->portname);
833 response->am[num].u.i.extension = interface->extension;
835 response->am[num].u.i.block = ifport->block;
836 if (ifport->mISDNport) {
837 mISDNport = ifport->mISDNport;
840 response->am[num].u.i.ptp = mISDNport->ptp;
842 response->am[num].u.i.l1hold = mISDNport->l1hold;
844 response->am[num].u.i.l2hold = mISDNport->l2hold;
846 response->am[num].u.i.ntmode = mISDNport->ntmode;
848 response->am[num].u.i.pri = mISDNport->pri;
850 response->am[num].u.i.use = mISDNport->use;
852 response->am[num].u.i.l1link = mISDNport->l1link;
854 response->am[num].u.i.l2link = mISDNport->l2link;
855 memcpy(response->am[num].u.i.l2mask, mISDNport->l2mask, 16);
857 response->am[num].u.i.los = mISDNport->los;
859 response->am[num].u.i.ais = mISDNport->ais;
861 response->am[num].u.i.rdi = mISDNport->rdi;
863 response->am[num].u.i.slip_tx = mISDNport->slip_tx;
864 response->am[num].u.i.slip_rx = mISDNport->slip_rx;
866 response->am[num].u.i.channels = mISDNport->b_num;
870 while(i < mISDNport->b_num) {
871 response->am[num].u.i.busy[i] = mISDNport->b_state[i];
872 if (mISDNport->b_port[i])
873 response->am[num].u.i.port[i] = mISDNport->b_port[i]->p_serial;
874 response->am[num].u.i.mode[i] = mISDNport->b_mode[i];
880 ifport = ifport->next;
882 interface = interface->next;
885 /* create response for all remotes */
888 if (admin->remote_name[0]) {
890 response->am[num].message = ADMIN_RESPONSE_S_REMOTE;
892 SCPY(response->am[num].u.r.name, admin->remote_name);
899 /* create response for all joins */
903 response->am[num].message = ADMIN_RESPONSE_S_JOIN;
905 response->am[num].u.j.serial = join->j_serial;
907 if (join->j_type == JOIN_TYPE_PBX)
908 response->am[num].u.j.partyline = ((class JoinPBX *)join)->j_partyline;
909 /* remote application */
910 if (join->j_type == JOIN_TYPE_REMOTE)
911 SCPY(response->am[num].u.j.remote, ((class JoinRemote *)join)->j_remote_name);
917 /* create response for all endpoint */
918 apppbx = apppbx_first;
921 response->am[num].message = ADMIN_RESPONSE_S_EPOINT;
923 response->am[num].u.e.serial = apppbx->ea_endpoint->ep_serial;
925 response->am[num].u.e.join = apppbx->ea_endpoint->ep_join_id;
926 /* rx notification */
927 response->am[num].u.e.rx_state = apppbx->e_rx_state;
928 /* tx notification */
929 response->am[num].u.e.tx_state = apppbx->e_tx_state;
931 switch(apppbx->e_state) {
932 case EPOINT_STATE_IN_SETUP:
933 response->am[num].u.e.state = ADMIN_STATE_IN_SETUP;
935 case EPOINT_STATE_OUT_SETUP:
936 response->am[num].u.e.state = ADMIN_STATE_OUT_SETUP;
938 case EPOINT_STATE_IN_OVERLAP:
939 response->am[num].u.e.state = ADMIN_STATE_IN_OVERLAP;
941 case EPOINT_STATE_OUT_OVERLAP:
942 response->am[num].u.e.state = ADMIN_STATE_OUT_OVERLAP;
944 case EPOINT_STATE_IN_PROCEEDING:
945 response->am[num].u.e.state = ADMIN_STATE_IN_PROCEEDING;
947 case EPOINT_STATE_OUT_PROCEEDING:
948 response->am[num].u.e.state = ADMIN_STATE_OUT_PROCEEDING;
950 case EPOINT_STATE_IN_ALERTING:
951 response->am[num].u.e.state = ADMIN_STATE_IN_ALERTING;
953 case EPOINT_STATE_OUT_ALERTING:
954 response->am[num].u.e.state = ADMIN_STATE_OUT_ALERTING;
956 case EPOINT_STATE_CONNECT:
957 response->am[num].u.e.state = ADMIN_STATE_CONNECT;
959 case EPOINT_STATE_IN_DISCONNECT:
960 response->am[num].u.e.state = ADMIN_STATE_IN_DISCONNECT;
962 case EPOINT_STATE_OUT_DISCONNECT:
963 response->am[num].u.e.state = ADMIN_STATE_OUT_DISCONNECT;
966 response->am[num].u.e.state = ADMIN_STATE_IDLE;
969 SCPY(response->am[num].u.e.terminal, apppbx->e_ext.number);
971 SCPY(response->am[num].u.e.callerid, apppbx->e_callerinfo.id);
973 SCPY(response->am[num].u.e.dialing, apppbx->e_dialinginfo.id);
975 if (apppbx->e_action)
976 SCPY(response->am[num].u.e.action, action_defs[apppbx->e_action->index].name);
977 // if (apppbx->e_action)
978 // printf("action=%s\n",action_defs[apppbx->e_action->index].name);
980 response->am[num].u.e.park = apppbx->ea_endpoint->ep_park;
981 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))
982 memcpy(response->am[num].u.e.park_callid, apppbx->ea_endpoint->ep_park_callid, apppbx->ea_endpoint->ep_park_len);
983 response->am[num].u.e.park_len = apppbx->ea_endpoint->ep_park_len;
985 if (apppbx->e_crypt == CRYPT_ON)
986 response->am[num].u.e.crypt = 1;
988 apppbx = apppbx->next;
992 /* create response for all ports */
996 response->am[num].message = ADMIN_RESPONSE_S_PORT;
998 response->am[num].u.p.serial = port->p_serial;
1000 SCPY(response->am[num].u.p.name, port->p_name);
1002 response->am[num].u.p.epoint = ACTIVE_EPOINT(port->p_epointlist);
1004 switch(port->p_state) {
1005 case PORT_STATE_IN_SETUP:
1006 response->am[num].u.p.state = ADMIN_STATE_IN_SETUP;
1008 case PORT_STATE_OUT_SETUP:
1009 response->am[num].u.p.state = ADMIN_STATE_OUT_SETUP;
1011 case PORT_STATE_IN_OVERLAP:
1012 response->am[num].u.p.state = ADMIN_STATE_IN_OVERLAP;
1014 case PORT_STATE_OUT_OVERLAP:
1015 response->am[num].u.p.state = ADMIN_STATE_OUT_OVERLAP;
1017 case PORT_STATE_IN_PROCEEDING:
1018 response->am[num].u.p.state = ADMIN_STATE_IN_PROCEEDING;
1020 case PORT_STATE_OUT_PROCEEDING:
1021 response->am[num].u.p.state = ADMIN_STATE_OUT_PROCEEDING;
1023 case PORT_STATE_IN_ALERTING:
1024 response->am[num].u.p.state = ADMIN_STATE_IN_ALERTING;
1026 case PORT_STATE_OUT_ALERTING:
1027 response->am[num].u.p.state = ADMIN_STATE_OUT_ALERTING;
1029 case PORT_STATE_CONNECT:
1030 response->am[num].u.p.state = ADMIN_STATE_CONNECT;
1032 case PORT_STATE_IN_DISCONNECT:
1033 response->am[num].u.p.state = ADMIN_STATE_IN_DISCONNECT;
1035 case PORT_STATE_OUT_DISCONNECT:
1036 response->am[num].u.p.state = ADMIN_STATE_OUT_DISCONNECT;
1039 response->am[num].u.p.state = ADMIN_STATE_IDLE;
1042 if ((port->p_type&PORT_CLASS_mISDN_MASK) == PORT_CLASS_mISDN_DSS1) {
1043 response->am[num].u.p.isdn = 1;
1044 pdss1 = (class Pdss1 *)port;
1045 response->am[num].u.p.isdn_chan = pdss1->p_m_b_channel;
1046 response->am[num].u.p.isdn_hold = pdss1->p_m_hold;
1047 response->am[num].u.p.isdn_ces = pdss1->p_m_d_ces;
1056 int sockserial = 1; // must start with 1, because 0 is used if no serial is set
1058 * handle admin socket (non blocking)
1060 int admin_handle(void)
1062 struct admin_list *admin, **adminp;
1064 struct admin_message msg;
1067 socklen_t sock_len = sizeof(sock_address);
1068 unsigned int on = 1;
1069 int work = 0; /* if work was done */
1070 struct Endpoint *epoint;
1075 /* check for new incoming connections */
1076 if ((new_sock = accept(sock, (struct sockaddr *)&sock_address, &sock_len)) >= 0) {
1078 /* insert new socket */
1079 admin = (struct admin_list *)MALLOC(sizeof(struct admin_list));
1080 if (ioctl(new_sock, FIONBIO, (unsigned char *)(&on)) >= 0) {
1082 // PERROR("DEBUG incoming socket %d, serial=%d\n", new_sock, sockserial);
1085 admin->sockserial = sockserial++;
1086 admin->next = admin_first;
1087 admin_first = admin;
1088 admin->sock = new_sock;
1091 FREE(admin, sizeof(struct admin_list));
1094 if (errno != EWOULDBLOCK) {
1095 PERROR("Failed to accept connection from socket \"%s\". (errno=%d) Closing socket.\n", sock_address.sun_path, errno);
1101 /* loop all current socket connections */
1102 admin = admin_first;
1103 adminp = &admin_first;
1106 len = read(admin->sock, &msg, sizeof(msg));
1108 if (errno != EWOULDBLOCK) {
1111 PDEBUG(DEBUG_LOG, "Broken pipe on socket %d. (errno=%d).\n", admin->sock, errno);
1112 *adminp = admin->next;
1113 free_connection(admin);
1121 //PERROR("DEBUG socket %d got data. serial=%d\n", admin->sock, admin->sockserial);
1125 /*release endpoint if exists */
1126 if (admin->epointid) {
1127 epoint = find_epoint_id(admin->epointid);
1129 ((class DEFAULT_ENDPOINT_APP *)epoint->ep_app)->
1130 release(RELEASE_ALL, LOCATION_PRIVATE_LOCAL, CAUSE_NORMAL, LOCATION_PRIVATE_LOCAL, CAUSE_NORMAL);
1135 //PERROR("DEBUG socket %d closed by remote.\n", admin->sock);
1136 *adminp = admin->next;
1137 free_connection(admin);
1139 //PERROR("DEBUG (admin_first=%x)\n", admin_first);
1142 if (len != sizeof(msg)) {
1143 PERROR("Short/long read on socket %d. (len=%d != size=%d).\n", admin->sock, len, sizeof(msg));
1144 *adminp = admin->next;
1145 free_connection(admin);
1149 /* process socket command */
1150 if (admin->response && msg.message != ADMIN_MESSAGE) {
1151 PERROR("Data from socket %d while sending response.\n", admin->sock);
1152 *adminp = admin->next;
1153 free_connection(admin);
1157 switch (msg.message) {
1158 case ADMIN_REQUEST_CMD_INTERFACE:
1159 if (admin_interface(&admin->response) < 0) {
1160 PERROR("Failed to create dial response for socket %d.\n", admin->sock);
1161 goto response_error;
1165 case ADMIN_REQUEST_CMD_ROUTE:
1166 if (admin_route(&admin->response) < 0) {
1167 PERROR("Failed to create dial response for socket %d.\n", admin->sock);
1168 goto response_error;
1172 case ADMIN_REQUEST_CMD_DIAL:
1173 if (admin_dial(&admin->response, msg.u.x.message) < 0) {
1174 PERROR("Failed to create dial response for socket %d.\n", admin->sock);
1175 goto response_error;
1179 case ADMIN_REQUEST_CMD_RELEASE:
1180 if (admin_release(&admin->response, msg.u.x.message) < 0) {
1181 PERROR("Failed to create release response for socket %d.\n", admin->sock);
1182 goto response_error;
1186 case ADMIN_REQUEST_STATE:
1187 if (admin_state(&admin->response) < 0) {
1188 PERROR("Failed to create state response for socket %d.\n", admin->sock);
1189 goto response_error;
1193 case ADMIN_TRACE_REQUEST:
1194 if (admin_trace(admin, &msg.u.trace_req) < 0) {
1195 PERROR("Failed to create trace response for socket %d.\n", admin->sock);
1196 goto response_error;
1200 case ADMIN_REQUEST_CMD_BLOCK:
1201 if (admin_block(&admin->response, msg.u.x.portnum, msg.u.x.block) < 0) {
1202 PERROR("Failed to create block response for socket %d.\n", admin->sock);
1203 goto response_error;
1208 if (admin_message_to_join(&msg.u.msg, admin) < 0) {
1209 PERROR("Failed to deliver message for socket %d.\n", admin->sock);
1210 goto response_error;
1215 struct admin_queue *response;
1217 response = admin->response;
1219 printf("%c", '0'+response->am[0].message);
1220 response=response->next;
1227 case ADMIN_CALL_SETUP:
1228 if (admin_call(admin, &msg) < 0) {
1229 PERROR("Failed to create call for socket %d.\n", admin->sock);
1231 *adminp = admin->next;
1232 free_connection(admin);
1239 PERROR("Invalid message %d from socket %d.\n", msg.message, admin->sock);
1240 *adminp = admin->next;
1241 free_connection(admin);
1247 if (admin->response) {
1249 //PERROR("DEBUG socket %d sending data.\n", admin->sock);
1250 len = write(admin->sock, ((unsigned char *)(admin->response->am))+admin->response->offset, sizeof(struct admin_message)*(admin->response->num)-admin->response->offset);
1252 if (errno != EWOULDBLOCK) {
1261 if (len < (int)(sizeof(struct admin_message)*(admin->response->num) - admin->response->offset)) {
1262 admin->response->offset+=len;
1265 temp = admin->response;
1266 admin->response = admin->response->next;
1271 /* done with socket instance */
1273 adminp = &admin->next;
1274 admin = admin->next;