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)
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)
49 PERROR("Failed to bind admin socket to \"%s\". (errno=%d)\n", sock_address.sun_path, errno);
52 if (listen(sock, 5) < 0)
58 PERROR("Failed to listen to socket \"%s\". (errno=%d)\n", sock_address.sun_path, errno);
61 if (ioctl(sock, FIONBIO, (unsigned char *)(&on)) < 0)
67 PERROR("Failed to set socket \"%s\" into non-blocking mode. (errno=%d)\n", sock_address.sun_path, errno);
70 if (chmod(socket_name, options.socketrights) < 0)
72 PERROR("Failed to change socket rigts to %d. (errno=%d)\n", options.socketrights, errno);
80 * also releases all remote joins
82 void free_connection(struct admin_list *admin)
84 struct admin_queue *response;
86 union parameter param;
87 class Join *join, *joinnext;
88 struct mISDNport *mISDNport;
91 /* free remote joins */
92 if (admin->remote_name[0])
101 "REMOTE APP release");
102 add_trace("app", "name", "%s", admin->remote_name);
104 /* release all exported channels */
105 mISDNport = mISDNport_first;
109 ii = mISDNport->b_num;
112 if (mISDNport->b_remote_id[i] == admin->sock)
114 mISDNport->b_state[i] = B_STATE_IDLE;
115 mISDNport->b_timer[i] = 0;
116 mISDNport->b_remote_id[i] = 0;
117 mISDNport->b_remote_ref[i] = 0;
121 mISDNport = mISDNport->next;
127 joinnext = join->next;
128 if (join->j_type==JOIN_TYPE_REMOTE) if (((class JoinRemote *)join)->j_remote_id == admin->sock)
130 memset(¶m, 0, sizeof(param));
131 param.disconnectinfo.cause = CAUSE_OUTOFORDER;
132 param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
133 ((class JoinRemote *)join)->message_remote(MESSAGE_RELEASE, ¶m);
134 /* join is now destroyed, so we go to next join */
140 if (admin->sock >= 0)
145 // printf("new\n", response);
146 response = admin->response;
150 // printf("%x\n", response);
151 temp = response->next;
154 response = (struct admin_queue *)temp;
156 // printf("new2\n", response);
158 // printf("new3\n", response);
164 * cleanup admin socket
166 void admin_cleanup(void)
168 struct admin_list *admin, *next;;
175 free_connection(admin);
190 * do interface reload
192 int admin_interface(struct admin_queue **responsep)
194 struct admin_queue *response; /* response pointer */
195 const char *err_txt = "";
198 if (read_interfaces())
201 free_interfaces(interface_first);
202 interface_first = interface_newlist;
203 interface_newlist = NULL;
206 err_txt = interface_error;
209 /* create state response */
210 response = (struct admin_queue *)MALLOC(sizeof(struct admin_queue)+sizeof(admin_message));
214 response->am[0].message = ADMIN_RESPONSE_CMD_INTERFACE;
216 response->am[0].u.x.error = err;
218 SCPY(response->am[0].u.x.message, err_txt);
219 /* attach to response chain */
220 *responsep = response;
221 responsep = &response->next;
230 int admin_route(struct admin_queue **responsep)
232 struct route_ruleset *ruleset_new;
233 struct admin_queue *response; /* response pointer */
234 char err_txt[256] = "";
239 class EndpointAppPBX *apppbx;
243 apppbx = apppbx_first;
247 apppbx = apppbx->next;
251 SPRINT(err_txt, "Cannot reload routing, because %d endpoints active\n", n);
256 if (!(ruleset_new = ruleset_parse()))
258 SPRINT(err_txt, ruleset_error);
262 ruleset_free(ruleset_first);
263 ruleset_first = ruleset_new;
264 ruleset_main = getrulesetbyname("main");
267 SPRINT(err_txt, "Ruleset reloaded, but rule 'main' not found.\n");
270 apppbx = apppbx_first;
273 if (apppbx->e_action)
275 switch(apppbx->e_action->index)
277 case ACTION_INTERNAL:
278 apppbx->e_action = &action_internal;
280 case ACTION_EXTERNAL:
281 apppbx->e_action = &action_external;
284 apppbx->e_action = &action_remote;
286 case ACTION_VBOX_RECORD:
287 apppbx->e_action = &action_vbox;
289 case ACTION_PARTYLINE:
290 apppbx->e_action = &action_partyline;
295 } else if (apppbx->e_state != EPOINT_STATE_CONNECT)
298 apppbx->e_callback = 0;
299 apppbx->e_action = NULL;
300 apppbx->release(RELEASE_ALL, LOCATION_PRIVATE_LOCAL, CAUSE_NORMAL, LOCATION_PRIVATE_LOCAL, CAUSE_NORMAL);
303 numberrize_callerinfo(apppbx->e_callerinfo.id, apppbx->e_callerinfo.ntype, options.national, options.international),
304 apppbx->e_dialinginfo.id,
307 apppbx->ea_endpoint->ep_serial,
308 "KICK (reload routing)");
312 apppbx->e_action_timeout = 0;
313 apppbx->e_rule = NULL;
314 apppbx->e_ruleset = NULL;
316 apppbx = apppbx->next;
320 /* create state response */
321 response = (struct admin_queue *)MALLOC(sizeof(struct admin_queue)+sizeof(admin_message));
325 response->am[0].message = ADMIN_RESPONSE_CMD_ROUTE;
327 response->am[0].u.x.error = err;
329 SCPY(response->am[0].u.x.message, err_txt);
330 /* attach to response chain */
331 *responsep = response;
332 responsep = &response->next;
341 int admin_dial(struct admin_queue **responsep, char *message)
343 struct extension ext; /* temporary extension's settings */
344 struct admin_queue *response; /* response pointer */
345 char *p; /* pointer to dialing digits */
347 /* create state response */
348 response = (struct admin_queue *)MALLOC(sizeof(struct admin_queue)+sizeof(admin_message));
352 response->am[0].message = ADMIN_RESPONSE_CMD_DIAL;
354 /* process request */
355 if (!(p = strchr(message,':')))
357 response->am[0].u.x.error = -EINVAL;
358 SPRINT(response->am[0].u.x.message, "no seperator ':' in message to seperate number from extension");
363 /* modify extension */
364 if (!read_extension(&ext, message))
366 response->am[0].u.x.error = -EINVAL;
367 SPRINT(response->am[0].u.x.message, "extension doesn't exist");
371 write_extension(&ext, message);
374 /* attach to response chain */
375 *responsep = response;
376 responsep = &response->next;
384 int admin_trace(struct admin_list *admin, struct admin_trace_req *trace)
386 memcpy(&admin->trace, trace, sizeof(struct admin_trace_req));
394 * 0 = make port available
395 * 1 = make port administratively blocked
397 * the result is returned:
398 * 0 = port is now available
399 * 1 = port is now blocked
400 * 2 = port cannot be loaded or has been unloaded
401 * -1 = port doesn't exist
403 int admin_block(struct admin_queue **responsep, int portnum, int block)
405 struct admin_queue *response; /* response pointer */
406 struct interface *interface;
407 struct interface_port *ifport;
409 /* create block response */
410 response = (struct admin_queue *)MALLOC(sizeof(struct admin_queue)+sizeof(admin_message));
414 response->am[0].message = ADMIN_RESPONSE_CMD_BLOCK;
415 response->am[0].u.x.portnum = portnum;
417 /* search for port */
418 interface = interface_first;
421 ifport = interface->ifport;
424 if (ifport->portnum == portnum)
426 ifport = ifport->next;
430 interface = interface->next;
432 /* not found, we return -1 */
435 response->am[0].u.x.block = -1;
436 response->am[0].u.x.error = 1;
437 SPRINT(response->am[0].u.x.message, "Port %d does not exist.", portnum);
442 if (!ifport->mISDNport)
444 /* not loaded anyway */
447 response->am[0].u.x.block = 2;
451 /* try loading interface */
452 ifport->block = block;
455 /* port cannot load */
456 if (ifport->block >= 2)
458 response->am[0].u.x.block = 2;
459 response->am[0].u.x.error = 1;
460 SPRINT(response->am[0].u.x.message, "Port %d will not load.", portnum);
465 response->am[0].u.x.block = ifport->block;
469 /* if we shall unload interface */
472 mISDNport_close(ifport->mISDNport);
473 ifport->mISDNport = 0;
478 /* port new blocking state */
479 ifport->block = response->am[0].u.x.block = block;
482 /* attach to response chain */
483 *responsep = response;
484 responsep = &response->next;
492 int admin_release(struct admin_queue **responsep, char *message)
495 struct admin_queue *response; /* response pointer */
496 class EndpointAppPBX *apppbx;
498 /* create state response */
499 response = (struct admin_queue *)MALLOC(sizeof(struct admin_queue)+sizeof(admin_message));
503 response->am[0].message = ADMIN_RESPONSE_CMD_RELEASE;
506 apppbx = apppbx_first;
509 if (apppbx->ea_endpoint->ep_serial == id)
511 apppbx = apppbx->next;
515 response->am[0].u.x.error = -EINVAL;
516 SPRINT(response->am[0].u.x.message, "Given endpoint %d doesn't exist.", id);
520 apppbx->e_callback = 0;
521 apppbx->release(RELEASE_ALL, LOCATION_PRIVATE_LOCAL, CAUSE_NORMAL, LOCATION_PRIVATE_LOCAL, CAUSE_NORMAL);
524 /* attach to response chain */
525 *responsep = response;
526 responsep = &response->next;
534 int admin_call(struct admin_list *admin, struct admin_message *msg)
536 class Endpoint *epoint;
537 class EndpointAppPBX *apppbx;
539 if (!(epoint = new Endpoint(0, 0)))
540 FATAL("No memory for Endpoint instance\n");
541 if (!(epoint->ep_app = apppbx = new DEFAULT_ENDPOINT_APP(epoint, 1))) // outgoing
542 FATAL("No memory for Endpoint Application instance\n");
543 apppbx->e_adminid = admin->sockserial;
544 admin->epointid = epoint->ep_serial;
545 SCPY(apppbx->e_callerinfo.id, nationalize_callerinfo(msg->u.call.callerid, &apppbx->e_callerinfo.ntype, options.national, options.international));
546 if (msg->u.call.present)
547 apppbx->e_callerinfo.present = INFO_PRESENT_ALLOWED;
549 apppbx->e_callerinfo.present = INFO_PRESENT_RESTRICTED;
550 apppbx->e_callerinfo.screen = INFO_SCREEN_NETWORK;
552 //printf("hh=%d\n", apppbx->e_capainfo.hlc);
554 apppbx->e_capainfo.bearer_capa = msg->u.call.bc_capa;
555 apppbx->e_capainfo.bearer_mode = msg->u.call.bc_mode;
556 apppbx->e_capainfo.bearer_info1 = msg->u.call.bc_info1;
557 apppbx->e_capainfo.hlc = msg->u.call.hlc;
558 apppbx->e_capainfo.exthlc = msg->u.call.exthlc;
559 SCPY(apppbx->e_dialinginfo.id, msg->u.call.dialing);
560 SCPY(apppbx->e_dialinginfo.interfaces, msg->u.call.interface);
561 apppbx->e_dialinginfo.sending_complete = 1;
563 apppbx->new_state(PORT_STATE_OUT_SETUP);
570 * this function is called for response whenever a call state changes.
572 void admin_call_response(int adminid, int message, const char *connected, int cause, int location, int notify)
574 struct admin_list *admin;
575 struct admin_queue *response, **responsep; /* response pointer */
577 /* searching for admin id
578 * maybe there is no admin instance, because the calling port was not
579 * initiated by admin_call */
583 if (adminid == admin->sockserial)
590 /* seek to end of response list */
591 response = admin->response;
592 responsep = &admin->response;
595 responsep = &response->next;
596 response = response->next;
599 /* create state response */
600 response = (struct admin_queue *)MALLOC(sizeof(struct admin_queue)+sizeof(admin_message));
604 response->am[0].message = message;
605 // printf("MESSAGE: %d\n", message);
607 SCPY(response->am[0].u.call.callerid, connected);
608 response->am[0].u.call.cause = cause;
609 response->am[0].u.call.location = location;
610 response->am[0].u.call.notify = notify;
612 /* attach to response chain */
613 *responsep = response;
614 responsep = &response->next;
619 * send data to the remote socket join instance
621 int admin_message_to_join(struct admin_msg *msg, struct admin_list *admin)
624 struct admin_list *temp;
627 if (msg->type == MESSAGE_HELLO)
629 if (admin->remote_name[0])
631 PERROR("Remote application repeats hello message.\n");
634 /* look for second application */
638 if (!strcmp(temp->remote_name, msg->param.hello.application))
644 PERROR("Remote application connects twice??? (ignoring)\n");
647 /* set remote socket instance */
648 SCPY(admin->remote_name, msg->param.hello.application);
656 "REMOTE APP registers");
657 add_trace("app", "name", "%s", admin->remote_name);
662 /* check we have no application name */
663 if (!admin->remote_name[0])
665 PERROR("Remote application did not send us a hello message.\n");
670 if (msg->type == MESSAGE_NEWREF)
672 /* create new join instance */
673 join = new JoinRemote(0, admin->remote_name, admin->sock); // must have no serial, because no endpoint is connected
676 FATAL("No memory for remote join instance\n");
683 * no ref given for *_ack */
684 if (msg->type == MESSAGE_BCHANNEL)
685 if (msg->param.bchannel.type == BCHANNEL_ASSIGN_ACK
686 || msg->param.bchannel.type == BCHANNEL_REMOVE_ACK
687 || msg->param.bchannel.type == BCHANNEL_RELEASE)
689 /* no ref, but address */
690 message_bchannel_from_remote(NULL, msg->param.bchannel.type, msg->param.bchannel.handle);
697 PERROR("Remote application did not send us a valid ref with a message.\n");
701 /* find join instance */
705 if (join->j_serial == msg->ref)
711 PDEBUG(DEBUG_LOG, "No join found with serial %d. (May have been already released.)\n", msg->ref);
715 /* check application */
716 if (join->j_type != JOIN_TYPE_REMOTE)
718 PERROR("Ref %d does not belong to a remote join instance.\n", msg->ref);
721 if (admin->sock != ((class JoinRemote *)join)->j_remote_id)
723 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);
728 ((class JoinRemote *)join)->message_remote(msg->type, &msg->param);
735 * this function is called for every message to remote socket
737 int admin_message_from_join(int remote_id, unsigned int ref, int message_type, union parameter *param)
739 struct admin_list *admin;
740 struct admin_queue **responsep; /* response pointer */
742 /* searching for admin id
743 * maybe there is no given remote application
748 if (admin->remote_name[0] && admin->sock==remote_id)
752 /* no given remote application connected */
756 /* seek to end of response list */
757 responsep = &admin->response;
760 responsep = &(*responsep)->next;
763 /* create state response */
764 *responsep = (struct admin_queue *)MALLOC(sizeof(struct admin_queue)+sizeof(admin_message));
766 (*responsep)->num = 1;
769 (*responsep)->am[0].message = ADMIN_MESSAGE;
770 (*responsep)->am[0].u.msg.type = message_type;
771 (*responsep)->am[0].u.msg.ref = ref;
772 memcpy(&(*responsep)->am[0].u.msg.param, param, sizeof(union parameter));
781 int admin_state(struct admin_queue **responsep)
785 class EndpointAppPBX *apppbx;
788 struct interface *interface;
789 struct interface_port *ifport;
790 struct mISDNport *mISDNport;
794 struct admin_queue *response;
795 struct admin_list *admin;
797 /* create state response */
798 response = (struct admin_queue *)MALLOC(sizeof(struct admin_queue)+sizeof(admin_message));
802 response->am[0].message = ADMIN_RESPONSE_STATE;
804 SCPY(response->am[0].u.s.version_string, VERSION_STRING);
806 memcpy(&response->am[0].u.s.tm, now_tm, sizeof(struct tm));
808 SCPY(response->am[0].u.s.logfile, options.log);
809 /* interface count */
811 interface = interface_first;
814 ifport = interface->ifport;
818 ifport = ifport->next;
820 interface = interface->next;
822 response->am[0].u.s.interfaces = i;
823 /* remote connection count */
828 if (admin->remote_name[0])
832 response->am[0].u.s.remotes = i;
841 response->am[0].u.s.joins = i;
843 apppbx = apppbx_first;
848 apppbx = apppbx->next;
850 response->am[0].u.s.epoints = i;
859 response->am[0].u.s.ports = i;
860 /* attach to response chain */
861 *responsep = response;
862 responsep = &response->next;
864 /* create response for all instances */
865 num = (response->am[0].u.s.interfaces)
866 + (response->am[0].u.s.remotes)
867 + (response->am[0].u.s.joins)
868 + (response->am[0].u.s.epoints)
869 + (response->am[0].u.s.ports);
872 response = (struct admin_queue *)MALLOC(sizeof(admin_queue)+(num*sizeof(admin_message)));
875 *responsep = response;
876 responsep = &response->next;
877 interface = interface_first;
881 ifport = interface->ifport;
885 response->am[num].message = ADMIN_RESPONSE_S_INTERFACE;
887 SCPY(response->am[num].u.i.interface_name, interface->name);
889 response->am[num].u.i.portnum = ifport->portnum;
891 response->am[num].u.i.extension = interface->extension;
893 response->am[num].u.i.block = ifport->block;
894 if (ifport->mISDNport)
896 mISDNport = ifport->mISDNport;
899 response->am[num].u.i.ptp = mISDNport->ptp;
901 response->am[num].u.i.ntmode = mISDNport->ntmode;
903 response->am[num].u.i.pri = mISDNport->pri;
905 response->am[num].u.i.use = mISDNport->use;
907 response->am[num].u.i.l1link = mISDNport->l1link;
909 response->am[num].u.i.l2link = mISDNport->l2link;
911 response->am[num].u.i.los = mISDNport->los;
913 response->am[num].u.i.ais = mISDNport->ais;
915 response->am[num].u.i.rdi = mISDNport->rdi;
917 response->am[num].u.i.slip_tx = mISDNport->slip_tx;
918 response->am[num].u.i.slip_rx = mISDNport->slip_rx;
920 response->am[num].u.i.channels = mISDNport->b_num;
924 while(i < mISDNport->b_num)
926 response->am[num].u.i.busy[i] = mISDNport->b_state[i];
927 if (mISDNport->b_port[i])
928 response->am[num].u.i.port[i] = mISDNport->b_port[i]->p_serial;
929 response->am[num].u.i.mode[i] = mISDNport->b_mode[i];
935 ifport = ifport->next;
937 interface = interface->next;
940 /* create response for all remotes */
944 if (admin->remote_name[0])
947 response->am[num].message = ADMIN_RESPONSE_S_REMOTE;
949 SCPY(response->am[num].u.r.name, admin->remote_name);
956 /* create response for all joins */
961 response->am[num].message = ADMIN_RESPONSE_S_JOIN;
963 response->am[num].u.j.serial = join->j_serial;
965 if (join->j_type == JOIN_TYPE_PBX)
966 response->am[num].u.j.partyline = ((class JoinPBX *)join)->j_partyline;
967 /* remote application */
968 if (join->j_type == JOIN_TYPE_REMOTE)
969 SCPY(response->am[num].u.j.remote, ((class JoinRemote *)join)->j_remote_name);
975 /* create response for all endpoint */
976 apppbx = apppbx_first;
980 response->am[num].message = ADMIN_RESPONSE_S_EPOINT;
982 response->am[num].u.e.serial = apppbx->ea_endpoint->ep_serial;
984 response->am[num].u.e.join = apppbx->ea_endpoint->ep_join_id;
985 /* rx notification */
986 response->am[num].u.e.rx_state = apppbx->e_rx_state;
987 /* tx notification */
988 response->am[num].u.e.tx_state = apppbx->e_tx_state;
990 switch(apppbx->e_state)
992 case EPOINT_STATE_IN_SETUP:
993 response->am[num].u.e.state = ADMIN_STATE_IN_SETUP;
995 case EPOINT_STATE_OUT_SETUP:
996 response->am[num].u.e.state = ADMIN_STATE_OUT_SETUP;
998 case EPOINT_STATE_IN_OVERLAP:
999 response->am[num].u.e.state = ADMIN_STATE_IN_OVERLAP;
1001 case EPOINT_STATE_OUT_OVERLAP:
1002 response->am[num].u.e.state = ADMIN_STATE_OUT_OVERLAP;
1004 case EPOINT_STATE_IN_PROCEEDING:
1005 response->am[num].u.e.state = ADMIN_STATE_IN_PROCEEDING;
1007 case EPOINT_STATE_OUT_PROCEEDING:
1008 response->am[num].u.e.state = ADMIN_STATE_OUT_PROCEEDING;
1010 case EPOINT_STATE_IN_ALERTING:
1011 response->am[num].u.e.state = ADMIN_STATE_IN_ALERTING;
1013 case EPOINT_STATE_OUT_ALERTING:
1014 response->am[num].u.e.state = ADMIN_STATE_OUT_ALERTING;
1016 case EPOINT_STATE_CONNECT:
1017 response->am[num].u.e.state = ADMIN_STATE_CONNECT;
1019 case EPOINT_STATE_IN_DISCONNECT:
1020 response->am[num].u.e.state = ADMIN_STATE_IN_DISCONNECT;
1022 case EPOINT_STATE_OUT_DISCONNECT:
1023 response->am[num].u.e.state = ADMIN_STATE_OUT_DISCONNECT;
1026 response->am[num].u.e.state = ADMIN_STATE_IDLE;
1029 SCPY(response->am[num].u.e.terminal, apppbx->e_ext.number);
1031 SCPY(response->am[num].u.e.callerid, apppbx->e_callerinfo.id);
1033 SCPY(response->am[num].u.e.dialing, apppbx->e_dialinginfo.id);
1035 if (apppbx->e_action)
1036 SCPY(response->am[num].u.e.action, action_defs[apppbx->e_action->index].name);
1037 // if (apppbx->e_action)
1038 // printf("action=%s\n",action_defs[apppbx->e_action->index].name);
1040 response->am[num].u.e.park = apppbx->ea_endpoint->ep_park;
1041 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))
1042 memcpy(response->am[num].u.e.park_callid, apppbx->ea_endpoint->ep_park_callid, apppbx->ea_endpoint->ep_park_len);
1043 response->am[num].u.e.park_len = apppbx->ea_endpoint->ep_park_len;
1045 if (apppbx->e_crypt == CRYPT_ON)
1046 response->am[num].u.e.crypt = 1;
1048 apppbx = apppbx->next;
1052 /* create response for all ports */
1057 response->am[num].message = ADMIN_RESPONSE_S_PORT;
1059 response->am[num].u.p.serial = port->p_serial;
1061 SCPY(response->am[num].u.p.name, port->p_name);
1063 response->am[num].u.p.epoint = ACTIVE_EPOINT(port->p_epointlist);
1065 switch(port->p_state)
1067 case PORT_STATE_IN_SETUP:
1068 response->am[num].u.p.state = ADMIN_STATE_IN_SETUP;
1070 case PORT_STATE_OUT_SETUP:
1071 response->am[num].u.p.state = ADMIN_STATE_OUT_SETUP;
1073 case PORT_STATE_IN_OVERLAP:
1074 response->am[num].u.p.state = ADMIN_STATE_IN_OVERLAP;
1076 case PORT_STATE_OUT_OVERLAP:
1077 response->am[num].u.p.state = ADMIN_STATE_OUT_OVERLAP;
1079 case PORT_STATE_IN_PROCEEDING:
1080 response->am[num].u.p.state = ADMIN_STATE_IN_PROCEEDING;
1082 case PORT_STATE_OUT_PROCEEDING:
1083 response->am[num].u.p.state = ADMIN_STATE_OUT_PROCEEDING;
1085 case PORT_STATE_IN_ALERTING:
1086 response->am[num].u.p.state = ADMIN_STATE_IN_ALERTING;
1088 case PORT_STATE_OUT_ALERTING:
1089 response->am[num].u.p.state = ADMIN_STATE_OUT_ALERTING;
1091 case PORT_STATE_CONNECT:
1092 response->am[num].u.p.state = ADMIN_STATE_CONNECT;
1094 case PORT_STATE_IN_DISCONNECT:
1095 response->am[num].u.p.state = ADMIN_STATE_IN_DISCONNECT;
1097 case PORT_STATE_OUT_DISCONNECT:
1098 response->am[num].u.p.state = ADMIN_STATE_OUT_DISCONNECT;
1101 response->am[num].u.p.state = ADMIN_STATE_IDLE;
1104 if ((port->p_type&PORT_CLASS_mISDN_MASK) == PORT_CLASS_mISDN_DSS1)
1106 response->am[num].u.p.isdn = 1;
1107 pdss1 = (class Pdss1 *)port;
1108 response->am[num].u.p.isdn_chan = pdss1->p_m_b_channel;
1109 response->am[num].u.p.isdn_hold = pdss1->p_m_hold;
1110 response->am[num].u.p.isdn_ces = pdss1->p_m_d_ces;
1119 int sockserial = 1; // must start with 1, because 0 is used if no serial is set
1121 * handle admin socket (non blocking)
1123 int admin_handle(void)
1125 struct admin_list *admin, **adminp;
1127 struct admin_message msg;
1130 socklen_t sock_len = sizeof(sock_address);
1131 unsigned int on = 1;
1132 int work = 0; /* if work was done */
1133 struct Endpoint *epoint;
1138 /* check for new incoming connections */
1139 if ((new_sock = accept(sock, (struct sockaddr *)&sock_address, &sock_len)) >= 0)
1142 /* insert new socket */
1143 admin = (struct admin_list *)MALLOC(sizeof(struct admin_list));
1144 if (ioctl(new_sock, FIONBIO, (unsigned char *)(&on)) >= 0)
1147 // PERROR("DEBUG incoming socket %d, serial=%d\n", new_sock, sockserial);
1150 admin->sockserial = sockserial++;
1151 admin->next = admin_first;
1152 admin_first = admin;
1153 admin->sock = new_sock;
1156 FREE(admin, sizeof(struct admin_list));
1160 if (errno != EWOULDBLOCK)
1162 PERROR("Failed to accept connection from socket \"%s\". (errno=%d) Closing socket.\n", sock_address.sun_path, errno);
1168 /* loop all current socket connections */
1169 admin = admin_first;
1170 adminp = &admin_first;
1174 len = read(admin->sock, &msg, sizeof(msg));
1177 if (errno != EWOULDBLOCK)
1181 printf("Broken pipe on socket %d. (errno=%d).\n", admin->sock, errno);
1182 PDEBUG(DEBUG_LOG, "Broken pipe on socket %d. (errno=%d).\n", admin->sock, errno);
1183 *adminp = admin->next;
1184 free_connection(admin);
1192 //PERROR("DEBUG socket %d got data. serial=%d\n", admin->sock, admin->sockserial);
1197 /*release endpoint if exists */
1198 if (admin->epointid)
1200 epoint = find_epoint_id(admin->epointid);
1203 ((class DEFAULT_ENDPOINT_APP *)epoint->ep_app)->
1204 release(RELEASE_ALL, LOCATION_PRIVATE_LOCAL, CAUSE_NORMAL, LOCATION_PRIVATE_LOCAL, CAUSE_NORMAL);
1209 //PERROR("DEBUG socket %d closed by remote.\n", admin->sock);
1210 *adminp = admin->next;
1211 free_connection(admin);
1213 //PERROR("DEBUG (admin_first=%x)\n", admin_first);
1216 if (len != sizeof(msg))
1218 PERROR("Short/long read on socket %d. (len=%d != size=%d).\n", admin->sock, len, sizeof(msg));
1219 *adminp = admin->next;
1220 free_connection(admin);
1224 /* process socket command */
1225 if (admin->response && msg.message != ADMIN_MESSAGE)
1227 PERROR("Data from socket %d while sending response.\n", admin->sock);
1228 *adminp = admin->next;
1229 free_connection(admin);
1233 switch (msg.message)
1235 case ADMIN_REQUEST_CMD_INTERFACE:
1236 if (admin_interface(&admin->response) < 0)
1238 PERROR("Failed to create dial response for socket %d.\n", admin->sock);
1239 goto response_error;
1243 case ADMIN_REQUEST_CMD_ROUTE:
1244 if (admin_route(&admin->response) < 0)
1246 PERROR("Failed to create dial response for socket %d.\n", admin->sock);
1247 goto response_error;
1251 case ADMIN_REQUEST_CMD_DIAL:
1252 if (admin_dial(&admin->response, msg.u.x.message) < 0)
1254 PERROR("Failed to create dial response for socket %d.\n", admin->sock);
1255 goto response_error;
1259 case ADMIN_REQUEST_CMD_RELEASE:
1260 if (admin_release(&admin->response, msg.u.x.message) < 0)
1262 PERROR("Failed to create release response for socket %d.\n", admin->sock);
1263 goto response_error;
1267 case ADMIN_REQUEST_STATE:
1268 if (admin_state(&admin->response) < 0)
1270 PERROR("Failed to create state response for socket %d.\n", admin->sock);
1271 goto response_error;
1275 case ADMIN_TRACE_REQUEST:
1276 if (admin_trace(admin, &msg.u.trace_req) < 0)
1278 PERROR("Failed to create trace response for socket %d.\n", admin->sock);
1279 goto response_error;
1283 case ADMIN_REQUEST_CMD_BLOCK:
1284 if (admin_block(&admin->response, msg.u.x.portnum, msg.u.x.block) < 0)
1286 PERROR("Failed to create block response for socket %d.\n", admin->sock);
1287 goto response_error;
1292 if (admin_message_to_join(&msg.u.msg, admin) < 0)
1294 PERROR("Failed to deliver message for socket %d.\n", admin->sock);
1295 goto response_error;
1300 struct admin_queue *response;
1302 response = admin->response;
1305 printf("%c", '0'+response->am[0].message);
1306 response=response->next;
1313 case ADMIN_CALL_SETUP:
1314 if (admin_call(admin, &msg) < 0)
1316 PERROR("Failed to create call for socket %d.\n", admin->sock);
1318 *adminp = admin->next;
1319 free_connection(admin);
1326 PERROR("Invalid message %d from socket %d.\n", msg.message, admin->sock);
1327 *adminp = admin->next;
1328 free_connection(admin);
1334 if (admin->response)
1337 //PERROR("DEBUG socket %d sending data.\n", admin->sock);
1338 len = write(admin->sock, ((unsigned char *)(admin->response->am))+admin->response->offset, sizeof(struct admin_message)*(admin->response->num)-admin->response->offset);
1341 if (errno != EWOULDBLOCK)
1351 if (len < (int)(sizeof(struct admin_message)*(admin->response->num) - admin->response->offset))
1353 admin->response->offset+=len;
1357 temp = admin->response;
1358 admin->response = admin->response->next;
1363 /* done with socket instance */
1365 adminp = &admin->next;
1366 admin = admin->next;