1 /*****************************************************************************\
3 ** Linux Call Router **
5 **---------------------------------------------------------------------------**
6 ** Copyright: Andreas Eversberg **
8 ** Socket link server **
10 \*****************************************************************************/
13 #include <sys/socket.h>
18 char *socket_name = SOCKET_NAME;
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 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)
48 PERROR("Failed to bind admin socket to \"%s\". (errno=%d)\n", sock_address.sun_path, errno);
51 if (listen(sock, 5) < 0)
57 PERROR("Failed to listen to socket \"%s\". (errno=%d)\n", sock_address.sun_path, errno);
60 if (ioctl(sock, FIONBIO, (unsigned char *)(&on)) < 0)
66 PERROR("Failed to set socket \"%s\" into non-blocking mode. (errno=%d)\n", sock_address.sun_path, 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])
96 "REMOTE APP release");
97 add_trace("app", "name", "%s", admin->remote_name);
99 /* release all exported channels */
100 mISDNport = mISDNport_first;
104 ii = mISDNport->b_num;
107 if (mISDNport->b_remote_id[i] == admin->sock)
109 mISDNport->b_state[i] = B_STATE_IDLE;
110 mISDNport->b_timer[i] = 0;
111 mISDNport->b_remote_id[i] = 0;
112 mISDNport->b_remote_ref[i] = 0;
116 mISDNport = mISDNport->next;
122 joinnext = join->next;
123 if (join->j_type==JOIN_TYPE_REMOTE) if (((class JoinRemote *)join)->j_remote_id == admin->sock)
125 memset(¶m, 0, sizeof(param));
126 param.disconnectinfo.cause = CAUSE_OUTOFORDER;
127 param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
128 ((class JoinRemote *)join)->message_remote(MESSAGE_RELEASE, ¶m);
129 /* join is now destroyed, so we go to next join */
135 if (admin->sock >= 0)
140 // printf("new\n", response);
141 response = admin->response;
145 // printf("%x\n", response);
146 temp = response->next;
149 response = (struct admin_queue *)temp;
151 // printf("new2\n", response);
153 // printf("new3\n", response);
159 * cleanup admin socket
161 void admin_cleanup(void)
163 struct admin_list *admin, *next;;
170 free_connection(admin);
183 * do interface reload
185 int admin_interface(struct admin_queue **responsep)
187 struct admin_queue *response; /* response pointer */
191 if (read_interfaces())
194 free_interfaces(interface_first);
195 interface_first = interface_newlist;
196 interface_newlist = NULL;
199 err_txt = interface_error;
202 /* create state response */
203 response = (struct admin_queue *)MALLOC(sizeof(struct admin_queue)+sizeof(admin_message));
207 response->am[0].message = ADMIN_RESPONSE_CMD_INTERFACE;
209 response->am[0].u.x.error = err;
211 SCPY(response->am[0].u.x.message, err_txt);
212 /* attach to response chain */
213 *responsep = response;
214 responsep = &response->next;
223 int admin_route(struct admin_queue **responsep)
225 struct route_ruleset *ruleset_new;
226 struct admin_queue *response; /* response pointer */
227 char err_txt[256] = "";
232 class EndpointAppPBX *apppbx;
236 apppbx = apppbx_first;
240 apppbx = apppbx->next;
244 SPRINT(err_txt, "Cannot reload routing, because %d endpoints active\n", n);
249 if (!(ruleset_new = ruleset_parse()))
251 SPRINT(err_txt, ruleset_error);
255 ruleset_free(ruleset_first);
256 ruleset_first = ruleset_new;
257 ruleset_main = getrulesetbyname("main");
260 SPRINT(err_txt, "Ruleset reloaded, but rule 'main' not found.\n");
263 apppbx = apppbx_first;
266 if (apppbx->e_action)
268 switch(apppbx->e_action->index)
270 case ACTION_INTERNAL:
271 apppbx->e_action = &action_internal;
273 case ACTION_EXTERNAL:
274 apppbx->e_action = &action_external;
277 apppbx->e_action = &action_remote;
279 case ACTION_VBOX_RECORD:
280 apppbx->e_action = &action_vbox;
282 case ACTION_PARTYLINE:
283 apppbx->e_action = &action_partyline;
288 } else if (apppbx->e_state != EPOINT_STATE_CONNECT)
291 apppbx->e_callback = 0;
292 apppbx->e_action = NULL;
293 apppbx->release(RELEASE_ALL, LOCATION_PRIVATE_LOCAL, CAUSE_NORMAL, LOCATION_PRIVATE_LOCAL, CAUSE_NORMAL);
296 numberrize_callerinfo(apppbx->e_callerinfo.id, apppbx->e_callerinfo.ntype, options.national, options.international),
297 apppbx->e_dialinginfo.id,
300 apppbx->ea_endpoint->ep_serial,
301 "KICK (reload routing)");
305 apppbx->e_action_timeout = 0;
306 apppbx->e_rule = NULL;
307 apppbx->e_ruleset = NULL;
309 apppbx = apppbx->next;
313 /* create state response */
314 response = (struct admin_queue *)MALLOC(sizeof(struct admin_queue)+sizeof(admin_message));
318 response->am[0].message = ADMIN_RESPONSE_CMD_ROUTE;
320 response->am[0].u.x.error = err;
322 SCPY(response->am[0].u.x.message, err_txt);
323 /* attach to response chain */
324 *responsep = response;
325 responsep = &response->next;
334 int admin_dial(struct admin_queue **responsep, char *message)
336 struct extension ext; /* temporary extension's settings */
337 struct admin_queue *response; /* response pointer */
338 char *p; /* pointer to dialing digits */
340 /* create state response */
341 response = (struct admin_queue *)MALLOC(sizeof(struct admin_queue)+sizeof(admin_message));
345 response->am[0].message = ADMIN_RESPONSE_CMD_DIAL;
347 /* process request */
348 if (!(p = strchr(message,':')))
350 response->am[0].u.x.error = -EINVAL;
351 SPRINT(response->am[0].u.x.message, "no seperator ':' in message to seperate number from extension");
356 /* modify extension */
357 if (!read_extension(&ext, message))
359 response->am[0].u.x.error = -EINVAL;
360 SPRINT(response->am[0].u.x.message, "extension doesn't exist");
364 write_extension(&ext, message);
367 /* attach to response chain */
368 *responsep = response;
369 responsep = &response->next;
377 int admin_trace(struct admin_list *admin, struct admin_trace_req *trace)
379 memcpy(&admin->trace, trace, sizeof(struct admin_trace_req));
387 * 0 = make port available
388 * 1 = make port administratively blocked
390 * the result is returned:
391 * 0 = port is now available
392 * 1 = port is now blocked
393 * 2 = port cannot be loaded or has been unloaded
394 * -1 = port doesn't exist
396 int admin_block(struct admin_queue **responsep, int portnum, int block)
398 struct admin_queue *response; /* response pointer */
399 struct interface *interface;
400 struct interface_port *ifport;
402 /* create block response */
403 response = (struct admin_queue *)MALLOC(sizeof(struct admin_queue)+sizeof(admin_message));
407 response->am[0].message = ADMIN_RESPONSE_CMD_BLOCK;
408 response->am[0].u.x.portnum = portnum;
410 /* search for port */
411 interface = interface_first;
414 ifport = interface->ifport;
417 if (ifport->portnum == portnum)
419 ifport = ifport->next;
423 interface = interface->next;
425 /* not found, we return -1 */
428 response->am[0].u.x.block = -1;
429 response->am[0].u.x.error = 1;
430 SPRINT(response->am[0].u.x.message, "Port %d does not exist.", portnum);
435 if (!ifport->mISDNport)
437 /* not loaded anyway */
440 response->am[0].u.x.block = 2;
444 /* try loading interface */
445 ifport->block = block;
448 /* port cannot load */
449 if (ifport->block >= 2)
451 response->am[0].u.x.block = 2;
452 response->am[0].u.x.error = 1;
453 SPRINT(response->am[0].u.x.message, "Port %d will not load.", portnum);
458 response->am[0].u.x.block = ifport->block;
462 /* if we shall unload interface */
465 mISDNport_close(ifport->mISDNport);
466 ifport->mISDNport = 0;
471 /* port new blocking state */
472 ifport->block = response->am[0].u.x.block = block;
475 /* attach to response chain */
476 *responsep = response;
477 responsep = &response->next;
485 int admin_release(struct admin_queue **responsep, char *message)
488 struct admin_queue *response; /* response pointer */
489 class EndpointAppPBX *apppbx;
491 /* create state response */
492 response = (struct admin_queue *)MALLOC(sizeof(struct admin_queue)+sizeof(admin_message));
496 response->am[0].message = ADMIN_RESPONSE_CMD_RELEASE;
499 apppbx = apppbx_first;
502 if (apppbx->ea_endpoint->ep_serial == id)
504 apppbx = apppbx->next;
508 response->am[0].u.x.error = -EINVAL;
509 SPRINT(response->am[0].u.x.message, "Given endpoint %d doesn't exist.", id);
513 apppbx->e_callback = 0;
514 apppbx->release(RELEASE_ALL, LOCATION_PRIVATE_LOCAL, CAUSE_NORMAL, LOCATION_PRIVATE_LOCAL, CAUSE_NORMAL);
517 /* attach to response chain */
518 *responsep = response;
519 responsep = &response->next;
527 int admin_call(struct admin_list *admin, struct admin_message *msg)
529 class Endpoint *epoint;
530 class EndpointAppPBX *apppbx;
532 if (!(epoint = new Endpoint(0, 0)))
533 FATAL("No memory for Endpoint instance\n");
534 if (!(epoint->ep_app = apppbx = new DEFAULT_ENDPOINT_APP(epoint, 1))) // outgoing
535 FATAL("No memory for Endpoint Application instance\n");
536 apppbx->e_adminid = admin->sockserial;
537 admin->epointid = epoint->ep_serial;
538 SCPY(apppbx->e_callerinfo.id, nationalize_callerinfo(msg->u.call.callerid, &apppbx->e_callerinfo.ntype, options.national, options.international));
539 if (msg->u.call.present)
540 apppbx->e_callerinfo.present = INFO_PRESENT_ALLOWED;
542 apppbx->e_callerinfo.present = INFO_PRESENT_RESTRICTED;
543 apppbx->e_callerinfo.screen = INFO_SCREEN_NETWORK;
545 //printf("hh=%d\n", apppbx->e_capainfo.hlc);
547 apppbx->e_capainfo.bearer_capa = msg->u.call.bc_capa;
548 apppbx->e_capainfo.bearer_mode = msg->u.call.bc_mode;
549 apppbx->e_capainfo.bearer_info1 = msg->u.call.bc_info1;
550 apppbx->e_capainfo.hlc = msg->u.call.hlc;
551 apppbx->e_capainfo.exthlc = msg->u.call.exthlc;
552 SCPY(apppbx->e_dialinginfo.id, msg->u.call.dialing);
553 SCPY(apppbx->e_dialinginfo.interfaces, msg->u.call.interface);
554 apppbx->e_dialinginfo.sending_complete = 1;
556 apppbx->new_state(PORT_STATE_OUT_SETUP);
563 * this function is called for response whenever a call state changes.
565 void admin_call_response(int adminid, int message, char *connected, int cause, int location, int notify)
567 struct admin_list *admin;
568 struct admin_queue *response, **responsep; /* response pointer */
570 /* searching for admin id
571 * maybe there is no admin instance, because the calling port was not
572 * initiated by admin_call */
576 if (adminid == admin->sockserial)
583 /* seek to end of response list */
584 response = admin->response;
585 responsep = &admin->response;
588 responsep = &response->next;
589 response = response->next;
592 /* create state response */
593 response = (struct admin_queue *)MALLOC(sizeof(struct admin_queue)+sizeof(admin_message));
597 response->am[0].message = message;
598 // printf("MESSAGE: %d\n", message);
600 SCPY(response->am[0].u.call.callerid, connected);
601 response->am[0].u.call.cause = cause;
602 response->am[0].u.call.location = location;
603 response->am[0].u.call.notify = notify;
605 /* attach to response chain */
606 *responsep = response;
607 responsep = &response->next;
612 * send data to the remote socket join instance
614 int admin_message_to_join(struct admin_msg *msg, struct admin_list *admin)
617 struct admin_list *temp;
620 if (msg->type == MESSAGE_HELLO)
622 if (admin->remote_name[0])
624 PERROR("Remote application repeats hello message.\n");
627 /* look for second application */
631 if (!strcmp(temp->remote_name, msg->param.hello.application))
637 PERROR("Remote application connects twice??? (ignoring)\n");
640 /* set remote socket instance */
641 SCPY(admin->remote_name, msg->param.hello.application);
649 "REMOTE APP registers");
650 add_trace("app", "name", "%s", admin->remote_name);
655 /* check we have no application name */
656 if (!admin->remote_name[0])
658 PERROR("Remote application did not send us a hello message.\n");
663 if (msg->type == MESSAGE_NEWREF)
665 /* create new join instance */
666 join = new JoinRemote(0, admin->remote_name, admin->sock); // must have no serial, because no endpoint is connected
669 FATAL("No memory for remote join instance\n");
676 * no ref given for *_ack */
677 if (msg->type == MESSAGE_BCHANNEL)
678 if (msg->param.bchannel.type == BCHANNEL_ASSIGN_ACK
679 || msg->param.bchannel.type == BCHANNEL_REMOVE_ACK
680 || msg->param.bchannel.type == BCHANNEL_RELEASE)
682 /* no ref, but address */
683 message_bchannel_from_remote(NULL, msg->param.bchannel.type, msg->param.bchannel.handle);
690 PERROR("Remote application did not send us a valid ref with a message.\n");
694 /* find join instance */
698 if (join->j_serial == msg->ref)
704 PERROR("No join found with serial %d.\n", msg->ref);
708 /* check application */
709 if (join->j_type != JOIN_TYPE_REMOTE)
711 PERROR("Ref %d does not belong to a remote join instance.\n", msg->ref);
714 if (admin->sock != ((class JoinRemote *)join)->j_remote_id)
716 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);
721 ((class JoinRemote *)join)->message_remote(msg->type, &msg->param);
728 * this function is called for every message to remote socket
730 int admin_message_from_join(int remote_id, unsigned int ref, int message_type, union parameter *param)
732 struct admin_list *admin;
733 struct admin_queue **responsep; /* response pointer */
735 /* searching for admin id
736 * maybe there is no given remote application
741 if (admin->remote_name[0] && admin->sock==remote_id)
745 /* no given remote application connected */
749 /* seek to end of response list */
750 responsep = &admin->response;
753 responsep = &(*responsep)->next;
756 /* create state response */
757 *responsep = (struct admin_queue *)MALLOC(sizeof(struct admin_queue)+sizeof(admin_message));
759 (*responsep)->num = 1;
762 (*responsep)->am[0].message = ADMIN_MESSAGE;
763 (*responsep)->am[0].u.msg.type = message_type;
764 (*responsep)->am[0].u.msg.ref = ref;
765 memcpy(&(*responsep)->am[0].u.msg.param, param, sizeof(union parameter));
774 int admin_state(struct admin_queue **responsep)
778 class EndpointAppPBX *apppbx;
781 struct interface *interface;
782 struct interface_port *ifport;
783 struct mISDNport *mISDNport;
787 struct admin_queue *response;
788 struct admin_list *admin;
790 /* create state response */
791 response = (struct admin_queue *)MALLOC(sizeof(struct admin_queue)+sizeof(admin_message));
795 response->am[0].message = ADMIN_RESPONSE_STATE;
797 SCPY(response->am[0].u.s.version_string, VERSION_STRING);
799 memcpy(&response->am[0].u.s.tm, now_tm, sizeof(struct tm));
801 SCPY(response->am[0].u.s.logfile, options.log);
802 /* interface count */
804 interface = interface_first;
807 ifport = interface->ifport;
811 ifport = ifport->next;
813 interface = interface->next;
815 response->am[0].u.s.interfaces = i;
816 /* remote connection count */
821 if (admin->remote_name[0])
825 response->am[0].u.s.remotes = i;
834 response->am[0].u.s.joins = i;
836 apppbx = apppbx_first;
841 apppbx = apppbx->next;
843 response->am[0].u.s.epoints = i;
852 response->am[0].u.s.ports = i;
853 /* attach to response chain */
854 *responsep = response;
855 responsep = &response->next;
857 /* create response for all instances */
858 num = (response->am[0].u.s.interfaces)
859 + (response->am[0].u.s.remotes)
860 + (response->am[0].u.s.joins)
861 + (response->am[0].u.s.epoints)
862 + (response->am[0].u.s.ports);
865 response = (struct admin_queue *)MALLOC(sizeof(admin_queue)+(num*sizeof(admin_message)));
868 *responsep = response;
869 responsep = &response->next;
870 interface = interface_first;
874 ifport = interface->ifport;
878 response->am[num].message = ADMIN_RESPONSE_S_INTERFACE;
880 SCPY(response->am[num].u.i.interface_name, interface->name);
882 response->am[num].u.i.portnum = ifport->portnum;
884 response->am[num].u.i.extension = interface->extension;
886 response->am[num].u.i.block = ifport->block;
887 if (ifport->mISDNport)
889 mISDNport = ifport->mISDNport;
892 response->am[num].u.i.ptp = mISDNport->ptp;
894 response->am[num].u.i.ntmode = mISDNport->ntmode;
896 response->am[num].u.i.pri = mISDNport->pri;
898 response->am[num].u.i.use = mISDNport->use;
900 response->am[num].u.i.l1link = mISDNport->l1link;
902 response->am[num].u.i.l2link = mISDNport->l2link;
904 response->am[num].u.i.los = mISDNport->los;
906 response->am[num].u.i.ais = mISDNport->ais;
908 response->am[num].u.i.rdi = mISDNport->rdi;
910 response->am[num].u.i.slip_tx = mISDNport->slip_tx;
911 response->am[num].u.i.slip_rx = mISDNport->slip_rx;
913 response->am[num].u.i.channels = mISDNport->b_num;
917 while(i < mISDNport->b_num)
919 response->am[num].u.i.busy[i] = mISDNport->b_state[i];
920 if (mISDNport->b_port[i])
921 response->am[num].u.i.port[i] = mISDNport->b_port[i]->p_serial;
927 ifport = ifport->next;
929 interface = interface->next;
932 /* create response for all remotes */
936 if (admin->remote_name[0])
939 response->am[num].message = ADMIN_RESPONSE_S_REMOTE;
941 SCPY(response->am[num].u.r.name, admin->remote_name);
948 /* create response for all joins */
953 response->am[num].message = ADMIN_RESPONSE_S_JOIN;
955 response->am[num].u.j.serial = join->j_serial;
957 if (join->j_type == JOIN_TYPE_PBX)
958 response->am[num].u.j.partyline = ((class JoinPBX *)join)->j_partyline;
959 /* remote application */
960 if (join->j_type == JOIN_TYPE_REMOTE)
961 SCPY(response->am[num].u.j.remote, ((class JoinRemote *)join)->j_remote_name);
967 /* create response for all endpoint */
968 apppbx = apppbx_first;
972 response->am[num].message = ADMIN_RESPONSE_S_EPOINT;
974 response->am[num].u.e.serial = apppbx->ea_endpoint->ep_serial;
976 response->am[num].u.e.join = apppbx->ea_endpoint->ep_join_id;
977 /* rx notification */
978 response->am[num].u.e.rx_state = apppbx->e_rx_state;
979 /* tx notification */
980 response->am[num].u.e.tx_state = apppbx->e_tx_state;
982 switch(apppbx->e_state)
984 case EPOINT_STATE_IN_SETUP:
985 response->am[num].u.e.state = ADMIN_STATE_IN_SETUP;
987 case EPOINT_STATE_OUT_SETUP:
988 response->am[num].u.e.state = ADMIN_STATE_OUT_SETUP;
990 case EPOINT_STATE_IN_OVERLAP:
991 response->am[num].u.e.state = ADMIN_STATE_IN_OVERLAP;
993 case EPOINT_STATE_OUT_OVERLAP:
994 response->am[num].u.e.state = ADMIN_STATE_OUT_OVERLAP;
996 case EPOINT_STATE_IN_PROCEEDING:
997 response->am[num].u.e.state = ADMIN_STATE_IN_PROCEEDING;
999 case EPOINT_STATE_OUT_PROCEEDING:
1000 response->am[num].u.e.state = ADMIN_STATE_OUT_PROCEEDING;
1002 case EPOINT_STATE_IN_ALERTING:
1003 response->am[num].u.e.state = ADMIN_STATE_IN_ALERTING;
1005 case EPOINT_STATE_OUT_ALERTING:
1006 response->am[num].u.e.state = ADMIN_STATE_OUT_ALERTING;
1008 case EPOINT_STATE_CONNECT:
1009 response->am[num].u.e.state = ADMIN_STATE_CONNECT;
1011 case EPOINT_STATE_IN_DISCONNECT:
1012 response->am[num].u.e.state = ADMIN_STATE_IN_DISCONNECT;
1014 case EPOINT_STATE_OUT_DISCONNECT:
1015 response->am[num].u.e.state = ADMIN_STATE_OUT_DISCONNECT;
1018 response->am[num].u.e.state = ADMIN_STATE_IDLE;
1021 SCPY(response->am[num].u.e.terminal, apppbx->e_ext.number);
1023 SCPY(response->am[num].u.e.callerid, apppbx->e_callerinfo.id);
1025 SCPY(response->am[num].u.e.dialing, apppbx->e_dialinginfo.id);
1027 if (apppbx->e_action)
1028 SCPY(response->am[num].u.e.action, action_defs[apppbx->e_action->index].name);
1029 // if (apppbx->e_action)
1030 // printf("action=%s\n",action_defs[apppbx->e_action->index].name);
1032 response->am[num].u.e.park = apppbx->ea_endpoint->ep_park;
1033 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))
1034 memcpy(response->am[num].u.e.park_callid, apppbx->ea_endpoint->ep_park_callid, apppbx->ea_endpoint->ep_park_len);
1035 response->am[num].u.e.park_len = apppbx->ea_endpoint->ep_park_len;
1037 if (apppbx->e_crypt == CRYPT_ON)
1038 response->am[num].u.e.crypt = 1;
1040 apppbx = apppbx->next;
1044 /* create response for all ports */
1049 response->am[num].message = ADMIN_RESPONSE_S_PORT;
1051 response->am[num].u.p.serial = port->p_serial;
1053 SCPY(response->am[num].u.p.name, port->p_name);
1055 response->am[num].u.p.epoint = ACTIVE_EPOINT(port->p_epointlist);
1057 switch(port->p_state)
1059 case PORT_STATE_IN_SETUP:
1060 response->am[num].u.p.state = ADMIN_STATE_IN_SETUP;
1062 case PORT_STATE_OUT_SETUP:
1063 response->am[num].u.p.state = ADMIN_STATE_OUT_SETUP;
1065 case PORT_STATE_IN_OVERLAP:
1066 response->am[num].u.p.state = ADMIN_STATE_IN_OVERLAP;
1068 case PORT_STATE_OUT_OVERLAP:
1069 response->am[num].u.p.state = ADMIN_STATE_OUT_OVERLAP;
1071 case PORT_STATE_IN_PROCEEDING:
1072 response->am[num].u.p.state = ADMIN_STATE_IN_PROCEEDING;
1074 case PORT_STATE_OUT_PROCEEDING:
1075 response->am[num].u.p.state = ADMIN_STATE_OUT_PROCEEDING;
1077 case PORT_STATE_IN_ALERTING:
1078 response->am[num].u.p.state = ADMIN_STATE_IN_ALERTING;
1080 case PORT_STATE_OUT_ALERTING:
1081 response->am[num].u.p.state = ADMIN_STATE_OUT_ALERTING;
1083 case PORT_STATE_CONNECT:
1084 response->am[num].u.p.state = ADMIN_STATE_CONNECT;
1086 case PORT_STATE_IN_DISCONNECT:
1087 response->am[num].u.p.state = ADMIN_STATE_IN_DISCONNECT;
1089 case PORT_STATE_OUT_DISCONNECT:
1090 response->am[num].u.p.state = ADMIN_STATE_OUT_DISCONNECT;
1093 response->am[num].u.p.state = ADMIN_STATE_IDLE;
1096 if ((port->p_type&PORT_CLASS_mISDN_MASK) == PORT_CLASS_mISDN_DSS1)
1098 response->am[num].u.p.isdn = 1;
1099 pdss1 = (class Pdss1 *)port;
1100 response->am[num].u.p.isdn_chan = pdss1->p_m_b_channel;
1101 response->am[num].u.p.isdn_hold = pdss1->p_m_hold;
1102 response->am[num].u.p.isdn_ces = pdss1->p_m_d_ces;
1111 int sockserial = 1; // must start with 1, because 0 is used if no serial is set
1113 * handle admin socket (non blocking)
1115 int admin_handle(void)
1117 struct admin_list *admin, **adminp;
1119 struct admin_message msg;
1122 socklen_t sock_len = sizeof(sock_address);
1123 unsigned int on = 1;
1124 int work = 0; /* if work was done */
1125 struct Endpoint *epoint;
1130 /* check for new incoming connections */
1131 if ((new_sock = accept(sock, (struct sockaddr *)&sock_address, &sock_len)) >= 0)
1134 /* insert new socket */
1135 admin = (struct admin_list *)MALLOC(sizeof(struct admin_list));
1136 if (ioctl(new_sock, FIONBIO, (unsigned char *)(&on)) >= 0)
1139 // PERROR("DEBUG incoming socket %d, serial=%d\n", new_sock, sockserial);
1142 admin->sockserial = sockserial++;
1143 admin->next = admin_first;
1144 admin_first = admin;
1145 admin->sock = new_sock;
1148 FREE(admin, sizeof(struct admin_list));
1152 if (errno != EWOULDBLOCK)
1154 PERROR("Failed to accept connection from socket \"%s\". (errno=%d) Closing socket.\n", sock_address.sun_path, errno);
1160 /* loop all current socket connections */
1161 admin = admin_first;
1162 adminp = &admin_first;
1166 len = read(admin->sock, &msg, sizeof(msg));
1169 if (errno != EWOULDBLOCK)
1173 printf("Broken pipe on socket %d. (errno=%d).\n", admin->sock, errno);
1174 PDEBUG(DEBUG_LOG, "Broken pipe on socket %d. (errno=%d).\n", admin->sock, errno);
1175 *adminp = admin->next;
1176 free_connection(admin);
1184 //PERROR("DEBUG socket %d got data. serial=%d\n", admin->sock, admin->sockserial);
1189 /*release endpoint if exists */
1190 if (admin->epointid)
1192 epoint = find_epoint_id(admin->epointid);
1195 ((class DEFAULT_ENDPOINT_APP *)epoint->ep_app)->
1196 release(RELEASE_ALL, LOCATION_PRIVATE_LOCAL, CAUSE_NORMAL, LOCATION_PRIVATE_LOCAL, CAUSE_NORMAL);
1201 //PERROR("DEBUG socket %d closed by remote.\n", admin->sock);
1202 *adminp = admin->next;
1203 free_connection(admin);
1205 //PERROR("DEBUG (admin_first=%x)\n", admin_first);
1208 if (len != sizeof(msg))
1210 PERROR("Short/long read on socket %d. (len=%d != size=%d).\n", admin->sock, len, sizeof(msg));
1211 *adminp = admin->next;
1212 free_connection(admin);
1216 /* process socket command */
1217 if (admin->response && msg.message != ADMIN_MESSAGE)
1219 PERROR("Data from socket %d while sending response.\n", admin->sock);
1220 *adminp = admin->next;
1221 free_connection(admin);
1225 switch (msg.message)
1227 case ADMIN_REQUEST_CMD_INTERFACE:
1228 if (admin_interface(&admin->response) < 0)
1230 PERROR("Failed to create dial response for socket %d.\n", admin->sock);
1231 goto response_error;
1235 case ADMIN_REQUEST_CMD_ROUTE:
1236 if (admin_route(&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_DIAL:
1244 if (admin_dial(&admin->response, msg.u.x.message) < 0)
1246 PERROR("Failed to create dial response for socket %d.\n", admin->sock);
1247 goto response_error;
1251 case ADMIN_REQUEST_CMD_RELEASE:
1252 if (admin_release(&admin->response, msg.u.x.message) < 0)
1254 PERROR("Failed to create release response for socket %d.\n", admin->sock);
1255 goto response_error;
1259 case ADMIN_REQUEST_STATE:
1260 if (admin_state(&admin->response) < 0)
1262 PERROR("Failed to create state response for socket %d.\n", admin->sock);
1263 goto response_error;
1267 case ADMIN_TRACE_REQUEST:
1268 if (admin_trace(admin, &msg.u.trace_req) < 0)
1270 PERROR("Failed to create trace response for socket %d.\n", admin->sock);
1271 goto response_error;
1275 case ADMIN_REQUEST_CMD_BLOCK:
1276 if (admin_block(&admin->response, msg.u.x.portnum, msg.u.x.block) < 0)
1278 PERROR("Failed to create block response for socket %d.\n", admin->sock);
1279 goto response_error;
1284 if (admin_message_to_join(&msg.u.msg, admin) < 0)
1286 PERROR("Failed to deliver message for socket %d.\n", admin->sock);
1287 goto response_error;
1292 struct admin_queue *response;
1294 response = admin->response;
1297 printf("%c", '0'+response->am[0].message);
1298 response=response->next;
1305 case ADMIN_CALL_SETUP:
1306 if (admin_call(admin, &msg) < 0)
1308 PERROR("Failed to create call for socket %d.\n", admin->sock);
1310 *adminp = admin->next;
1311 free_connection(admin);
1318 PERROR("Invalid message %d from socket %d.\n", msg.message, admin->sock);
1319 *adminp = admin->next;
1320 free_connection(admin);
1326 if (admin->response)
1329 //PERROR("DEBUG socket %d sending data.\n", admin->sock);
1330 len = write(admin->sock, ((unsigned char *)(admin->response->am))+admin->response->offset, sizeof(struct admin_message)*(admin->response->num)-admin->response->offset);
1333 if (errno != EWOULDBLOCK)
1343 if (len < (int)(sizeof(struct admin_message)*(admin->response->num) - admin->response->offset))
1345 admin->response->offset+=len;
1349 temp = admin->response;
1350 admin->response = admin->response->next;
1355 /* done with socket instance */
1357 adminp = &admin->next;
1358 admin = admin->next;