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 */
419 interface = interface_first;
422 ifport = interface->ifport;
425 if (ifport->portnum == portnum)
427 ifport = ifport->next;
431 interface = interface->next;
433 /* not found, we return -1 */
436 response->am[0].u.x.block = -1;
437 response->am[0].u.x.error = 1;
438 SPRINT(response->am[0].u.x.message, "Port %d does not exist.", portnum);
443 if (!ifport->mISDNport)
445 /* not loaded anyway */
448 response->am[0].u.x.block = 2;
452 /* try loading interface */
453 ifport->block = block;
456 /* port cannot load */
457 if (ifport->block >= 2)
459 response->am[0].u.x.block = 2;
460 response->am[0].u.x.error = 1;
461 SPRINT(response->am[0].u.x.message, "Port %d will not load.", portnum);
466 response->am[0].u.x.block = ifport->block;
470 /* if we shall unload interface */
473 mISDNport_close(ifport->mISDNport);
474 ifport->mISDNport = 0;
479 /* port new blocking state */
480 ifport->block = response->am[0].u.x.block = block;
483 /* attach to response chain */
484 *responsep = response;
485 responsep = &response->next;
493 int admin_release(struct admin_queue **responsep, char *message)
496 struct admin_queue *response; /* response pointer */
497 class EndpointAppPBX *apppbx;
499 /* create state response */
500 response = (struct admin_queue *)MALLOC(sizeof(struct admin_queue)+sizeof(admin_message));
504 response->am[0].message = ADMIN_RESPONSE_CMD_RELEASE;
507 apppbx = apppbx_first;
510 if (apppbx->ea_endpoint->ep_serial == id)
512 apppbx = apppbx->next;
516 response->am[0].u.x.error = -EINVAL;
517 SPRINT(response->am[0].u.x.message, "Given endpoint %d doesn't exist.", id);
521 apppbx->e_callback = 0;
522 apppbx->release(RELEASE_ALL, LOCATION_PRIVATE_LOCAL, CAUSE_NORMAL, LOCATION_PRIVATE_LOCAL, CAUSE_NORMAL);
525 /* attach to response chain */
526 *responsep = response;
527 responsep = &response->next;
535 int admin_call(struct admin_list *admin, struct admin_message *msg)
537 class Endpoint *epoint;
538 class EndpointAppPBX *apppbx;
540 if (!(epoint = new Endpoint(0, 0)))
541 FATAL("No memory for Endpoint instance\n");
542 if (!(epoint->ep_app = apppbx = new DEFAULT_ENDPOINT_APP(epoint, 1))) // outgoing
543 FATAL("No memory for Endpoint Application instance\n");
544 apppbx->e_adminid = admin->sockserial;
545 admin->epointid = epoint->ep_serial;
546 SCPY(apppbx->e_callerinfo.id, nationalize_callerinfo(msg->u.call.callerid, &apppbx->e_callerinfo.ntype, options.national, options.international));
547 if (msg->u.call.present)
548 apppbx->e_callerinfo.present = INFO_PRESENT_ALLOWED;
550 apppbx->e_callerinfo.present = INFO_PRESENT_RESTRICTED;
551 apppbx->e_callerinfo.screen = INFO_SCREEN_NETWORK;
553 //printf("hh=%d\n", apppbx->e_capainfo.hlc);
555 apppbx->e_capainfo.bearer_capa = msg->u.call.bc_capa;
556 apppbx->e_capainfo.bearer_mode = msg->u.call.bc_mode;
557 apppbx->e_capainfo.bearer_info1 = msg->u.call.bc_info1;
558 apppbx->e_capainfo.hlc = msg->u.call.hlc;
559 apppbx->e_capainfo.exthlc = msg->u.call.exthlc;
560 SCPY(apppbx->e_dialinginfo.id, msg->u.call.dialing);
561 SCPY(apppbx->e_dialinginfo.interfaces, msg->u.call.interface);
562 apppbx->e_dialinginfo.sending_complete = 1;
564 apppbx->new_state(PORT_STATE_OUT_SETUP);
571 * this function is called for response whenever a call state changes.
573 void admin_call_response(int adminid, int message, const char *connected, int cause, int location, int notify)
575 struct admin_list *admin;
576 struct admin_queue *response, **responsep; /* response pointer */
578 /* searching for admin id
579 * maybe there is no admin instance, because the calling port was not
580 * initiated by admin_call */
584 if (adminid == admin->sockserial)
591 /* seek to end of response list */
592 response = admin->response;
593 responsep = &admin->response;
596 responsep = &response->next;
597 response = response->next;
600 /* create state response */
601 response = (struct admin_queue *)MALLOC(sizeof(struct admin_queue)+sizeof(admin_message));
605 response->am[0].message = message;
606 // printf("MESSAGE: %d\n", message);
608 SCPY(response->am[0].u.call.callerid, connected);
609 response->am[0].u.call.cause = cause;
610 response->am[0].u.call.location = location;
611 response->am[0].u.call.notify = notify;
613 /* attach to response chain */
614 *responsep = response;
615 responsep = &response->next;
620 * send data to the remote socket join instance
622 int admin_message_to_join(struct admin_msg *msg, struct admin_list *admin)
625 struct admin_list *temp;
628 if (msg->type == MESSAGE_HELLO)
630 if (admin->remote_name[0])
632 PERROR("Remote application repeats hello message.\n");
635 /* look for second application */
639 if (!strcmp(temp->remote_name, msg->param.hello.application))
645 PERROR("Remote application connects twice??? (ignoring)\n");
648 /* set remote socket instance */
649 SCPY(admin->remote_name, msg->param.hello.application);
657 "REMOTE APP registers");
658 add_trace("app", "name", "%s", admin->remote_name);
663 /* check we have no application name */
664 if (!admin->remote_name[0])
666 PERROR("Remote application did not send us a hello message.\n");
671 if (msg->type == MESSAGE_NEWREF)
673 /* create new join instance */
674 join = new JoinRemote(0, admin->remote_name, admin->sock); // must have no serial, because no endpoint is connected
677 FATAL("No memory for remote join instance\n");
684 * no ref given for *_ack */
685 if (msg->type == MESSAGE_BCHANNEL)
686 if (msg->param.bchannel.type == BCHANNEL_ASSIGN_ACK
687 || msg->param.bchannel.type == BCHANNEL_REMOVE_ACK
688 || msg->param.bchannel.type == BCHANNEL_RELEASE)
690 /* no ref, but address */
691 message_bchannel_from_remote(NULL, msg->param.bchannel.type, msg->param.bchannel.handle);
698 PERROR("Remote application did not send us a valid ref with a message.\n");
702 /* find join instance */
706 if (join->j_serial == msg->ref)
712 PDEBUG(DEBUG_LOG, "No join found with serial %d. (May have been already released.)\n", msg->ref);
716 /* check application */
717 if (join->j_type != JOIN_TYPE_REMOTE)
719 PERROR("Ref %d does not belong to a remote join instance.\n", msg->ref);
722 if (admin->sock != ((class JoinRemote *)join)->j_remote_id)
724 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);
729 ((class JoinRemote *)join)->message_remote(msg->type, &msg->param);
736 * this function is called for every message to remote socket
738 int admin_message_from_join(int remote_id, unsigned int ref, int message_type, union parameter *param)
740 struct admin_list *admin;
741 struct admin_queue **responsep; /* response pointer */
743 /* searching for admin id
744 * maybe there is no given remote application
749 if (admin->remote_name[0] && admin->sock==remote_id)
753 /* no given remote application connected */
757 /* seek to end of response list */
758 responsep = &admin->response;
761 responsep = &(*responsep)->next;
764 /* create state response */
765 *responsep = (struct admin_queue *)MALLOC(sizeof(struct admin_queue)+sizeof(admin_message));
767 (*responsep)->num = 1;
770 (*responsep)->am[0].message = ADMIN_MESSAGE;
771 (*responsep)->am[0].u.msg.type = message_type;
772 (*responsep)->am[0].u.msg.ref = ref;
773 memcpy(&(*responsep)->am[0].u.msg.param, param, sizeof(union parameter));
782 int admin_state(struct admin_queue **responsep)
786 class EndpointAppPBX *apppbx;
789 struct interface *interface;
790 struct interface_port *ifport;
791 struct mISDNport *mISDNport;
795 struct admin_queue *response;
796 struct admin_list *admin;
798 /* create state response */
799 response = (struct admin_queue *)MALLOC(sizeof(struct admin_queue)+sizeof(admin_message));
803 response->am[0].message = ADMIN_RESPONSE_STATE;
805 SCPY(response->am[0].u.s.version_string, VERSION_STRING);
807 memcpy(&response->am[0].u.s.tm, now_tm, sizeof(struct tm));
809 SCPY(response->am[0].u.s.logfile, options.log);
810 /* interface count */
812 interface = interface_first;
815 ifport = interface->ifport;
819 ifport = ifport->next;
821 interface = interface->next;
823 response->am[0].u.s.interfaces = i;
824 /* remote connection count */
829 if (admin->remote_name[0])
833 response->am[0].u.s.remotes = i;
842 response->am[0].u.s.joins = i;
844 apppbx = apppbx_first;
849 apppbx = apppbx->next;
851 response->am[0].u.s.epoints = i;
860 response->am[0].u.s.ports = i;
861 /* attach to response chain */
862 *responsep = response;
863 responsep = &response->next;
865 /* create response for all instances */
866 num = (response->am[0].u.s.interfaces)
867 + (response->am[0].u.s.remotes)
868 + (response->am[0].u.s.joins)
869 + (response->am[0].u.s.epoints)
870 + (response->am[0].u.s.ports);
873 response = (struct admin_queue *)MALLOC(sizeof(admin_queue)+(num*sizeof(admin_message)));
876 *responsep = response;
877 responsep = &response->next;
878 interface = interface_first;
882 ifport = interface->ifport;
886 response->am[num].message = ADMIN_RESPONSE_S_INTERFACE;
888 SCPY(response->am[num].u.i.interface_name, interface->name);
890 response->am[num].u.i.portnum = ifport->portnum;
892 SCPY(response->am[num].u.i.portname, ifport->portname);
894 response->am[num].u.i.extension = interface->extension;
896 response->am[num].u.i.block = ifport->block;
897 if (ifport->mISDNport)
899 mISDNport = ifport->mISDNport;
902 response->am[num].u.i.ptp = mISDNport->ptp;
904 response->am[num].u.i.l1hold = mISDNport->l1hold;
906 response->am[num].u.i.l2hold = mISDNport->l2hold;
908 response->am[num].u.i.ntmode = mISDNport->ntmode;
910 response->am[num].u.i.pri = mISDNport->pri;
912 response->am[num].u.i.use = mISDNport->use;
914 response->am[num].u.i.l1link = mISDNport->l1link;
916 response->am[num].u.i.l2link = mISDNport->l2link;
918 response->am[num].u.i.los = mISDNport->los;
920 response->am[num].u.i.ais = mISDNport->ais;
922 response->am[num].u.i.rdi = mISDNport->rdi;
924 response->am[num].u.i.slip_tx = mISDNport->slip_tx;
925 response->am[num].u.i.slip_rx = mISDNport->slip_rx;
927 response->am[num].u.i.channels = mISDNport->b_num;
931 while(i < mISDNport->b_num)
933 response->am[num].u.i.busy[i] = mISDNport->b_state[i];
934 if (mISDNport->b_port[i])
935 response->am[num].u.i.port[i] = mISDNport->b_port[i]->p_serial;
936 response->am[num].u.i.mode[i] = mISDNport->b_mode[i];
942 ifport = ifport->next;
944 interface = interface->next;
947 /* create response for all remotes */
951 if (admin->remote_name[0])
954 response->am[num].message = ADMIN_RESPONSE_S_REMOTE;
956 SCPY(response->am[num].u.r.name, admin->remote_name);
963 /* create response for all joins */
968 response->am[num].message = ADMIN_RESPONSE_S_JOIN;
970 response->am[num].u.j.serial = join->j_serial;
972 if (join->j_type == JOIN_TYPE_PBX)
973 response->am[num].u.j.partyline = ((class JoinPBX *)join)->j_partyline;
974 /* remote application */
975 if (join->j_type == JOIN_TYPE_REMOTE)
976 SCPY(response->am[num].u.j.remote, ((class JoinRemote *)join)->j_remote_name);
982 /* create response for all endpoint */
983 apppbx = apppbx_first;
987 response->am[num].message = ADMIN_RESPONSE_S_EPOINT;
989 response->am[num].u.e.serial = apppbx->ea_endpoint->ep_serial;
991 response->am[num].u.e.join = apppbx->ea_endpoint->ep_join_id;
992 /* rx notification */
993 response->am[num].u.e.rx_state = apppbx->e_rx_state;
994 /* tx notification */
995 response->am[num].u.e.tx_state = apppbx->e_tx_state;
997 switch(apppbx->e_state)
999 case EPOINT_STATE_IN_SETUP:
1000 response->am[num].u.e.state = ADMIN_STATE_IN_SETUP;
1002 case EPOINT_STATE_OUT_SETUP:
1003 response->am[num].u.e.state = ADMIN_STATE_OUT_SETUP;
1005 case EPOINT_STATE_IN_OVERLAP:
1006 response->am[num].u.e.state = ADMIN_STATE_IN_OVERLAP;
1008 case EPOINT_STATE_OUT_OVERLAP:
1009 response->am[num].u.e.state = ADMIN_STATE_OUT_OVERLAP;
1011 case EPOINT_STATE_IN_PROCEEDING:
1012 response->am[num].u.e.state = ADMIN_STATE_IN_PROCEEDING;
1014 case EPOINT_STATE_OUT_PROCEEDING:
1015 response->am[num].u.e.state = ADMIN_STATE_OUT_PROCEEDING;
1017 case EPOINT_STATE_IN_ALERTING:
1018 response->am[num].u.e.state = ADMIN_STATE_IN_ALERTING;
1020 case EPOINT_STATE_OUT_ALERTING:
1021 response->am[num].u.e.state = ADMIN_STATE_OUT_ALERTING;
1023 case EPOINT_STATE_CONNECT:
1024 response->am[num].u.e.state = ADMIN_STATE_CONNECT;
1026 case EPOINT_STATE_IN_DISCONNECT:
1027 response->am[num].u.e.state = ADMIN_STATE_IN_DISCONNECT;
1029 case EPOINT_STATE_OUT_DISCONNECT:
1030 response->am[num].u.e.state = ADMIN_STATE_OUT_DISCONNECT;
1033 response->am[num].u.e.state = ADMIN_STATE_IDLE;
1036 SCPY(response->am[num].u.e.terminal, apppbx->e_ext.number);
1038 SCPY(response->am[num].u.e.callerid, apppbx->e_callerinfo.id);
1040 SCPY(response->am[num].u.e.dialing, apppbx->e_dialinginfo.id);
1042 if (apppbx->e_action)
1043 SCPY(response->am[num].u.e.action, action_defs[apppbx->e_action->index].name);
1044 // if (apppbx->e_action)
1045 // printf("action=%s\n",action_defs[apppbx->e_action->index].name);
1047 response->am[num].u.e.park = apppbx->ea_endpoint->ep_park;
1048 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))
1049 memcpy(response->am[num].u.e.park_callid, apppbx->ea_endpoint->ep_park_callid, apppbx->ea_endpoint->ep_park_len);
1050 response->am[num].u.e.park_len = apppbx->ea_endpoint->ep_park_len;
1052 if (apppbx->e_crypt == CRYPT_ON)
1053 response->am[num].u.e.crypt = 1;
1055 apppbx = apppbx->next;
1059 /* create response for all ports */
1064 response->am[num].message = ADMIN_RESPONSE_S_PORT;
1066 response->am[num].u.p.serial = port->p_serial;
1068 SCPY(response->am[num].u.p.name, port->p_name);
1070 response->am[num].u.p.epoint = ACTIVE_EPOINT(port->p_epointlist);
1072 switch(port->p_state)
1074 case PORT_STATE_IN_SETUP:
1075 response->am[num].u.p.state = ADMIN_STATE_IN_SETUP;
1077 case PORT_STATE_OUT_SETUP:
1078 response->am[num].u.p.state = ADMIN_STATE_OUT_SETUP;
1080 case PORT_STATE_IN_OVERLAP:
1081 response->am[num].u.p.state = ADMIN_STATE_IN_OVERLAP;
1083 case PORT_STATE_OUT_OVERLAP:
1084 response->am[num].u.p.state = ADMIN_STATE_OUT_OVERLAP;
1086 case PORT_STATE_IN_PROCEEDING:
1087 response->am[num].u.p.state = ADMIN_STATE_IN_PROCEEDING;
1089 case PORT_STATE_OUT_PROCEEDING:
1090 response->am[num].u.p.state = ADMIN_STATE_OUT_PROCEEDING;
1092 case PORT_STATE_IN_ALERTING:
1093 response->am[num].u.p.state = ADMIN_STATE_IN_ALERTING;
1095 case PORT_STATE_OUT_ALERTING:
1096 response->am[num].u.p.state = ADMIN_STATE_OUT_ALERTING;
1098 case PORT_STATE_CONNECT:
1099 response->am[num].u.p.state = ADMIN_STATE_CONNECT;
1101 case PORT_STATE_IN_DISCONNECT:
1102 response->am[num].u.p.state = ADMIN_STATE_IN_DISCONNECT;
1104 case PORT_STATE_OUT_DISCONNECT:
1105 response->am[num].u.p.state = ADMIN_STATE_OUT_DISCONNECT;
1108 response->am[num].u.p.state = ADMIN_STATE_IDLE;
1111 if ((port->p_type&PORT_CLASS_mISDN_MASK) == PORT_CLASS_mISDN_DSS1)
1113 response->am[num].u.p.isdn = 1;
1114 pdss1 = (class Pdss1 *)port;
1115 response->am[num].u.p.isdn_chan = pdss1->p_m_b_channel;
1116 response->am[num].u.p.isdn_hold = pdss1->p_m_hold;
1117 response->am[num].u.p.isdn_ces = pdss1->p_m_d_ces;
1126 int sockserial = 1; // must start with 1, because 0 is used if no serial is set
1128 * handle admin socket (non blocking)
1130 int admin_handle(void)
1132 struct admin_list *admin, **adminp;
1134 struct admin_message msg;
1137 socklen_t sock_len = sizeof(sock_address);
1138 unsigned int on = 1;
1139 int work = 0; /* if work was done */
1140 struct Endpoint *epoint;
1145 /* check for new incoming connections */
1146 if ((new_sock = accept(sock, (struct sockaddr *)&sock_address, &sock_len)) >= 0)
1149 /* insert new socket */
1150 admin = (struct admin_list *)MALLOC(sizeof(struct admin_list));
1151 if (ioctl(new_sock, FIONBIO, (unsigned char *)(&on)) >= 0)
1154 // PERROR("DEBUG incoming socket %d, serial=%d\n", new_sock, sockserial);
1157 admin->sockserial = sockserial++;
1158 admin->next = admin_first;
1159 admin_first = admin;
1160 admin->sock = new_sock;
1163 FREE(admin, sizeof(struct admin_list));
1167 if (errno != EWOULDBLOCK)
1169 PERROR("Failed to accept connection from socket \"%s\". (errno=%d) Closing socket.\n", sock_address.sun_path, errno);
1175 /* loop all current socket connections */
1176 admin = admin_first;
1177 adminp = &admin_first;
1181 len = read(admin->sock, &msg, sizeof(msg));
1184 if (errno != EWOULDBLOCK)
1188 PDEBUG(DEBUG_LOG, "Broken pipe on socket %d. (errno=%d).\n", admin->sock, errno);
1189 *adminp = admin->next;
1190 free_connection(admin);
1198 //PERROR("DEBUG socket %d got data. serial=%d\n", admin->sock, admin->sockserial);
1203 /*release endpoint if exists */
1204 if (admin->epointid)
1206 epoint = find_epoint_id(admin->epointid);
1209 ((class DEFAULT_ENDPOINT_APP *)epoint->ep_app)->
1210 release(RELEASE_ALL, LOCATION_PRIVATE_LOCAL, CAUSE_NORMAL, LOCATION_PRIVATE_LOCAL, CAUSE_NORMAL);
1215 //PERROR("DEBUG socket %d closed by remote.\n", admin->sock);
1216 *adminp = admin->next;
1217 free_connection(admin);
1219 //PERROR("DEBUG (admin_first=%x)\n", admin_first);
1222 if (len != sizeof(msg))
1224 PERROR("Short/long read on socket %d. (len=%d != size=%d).\n", admin->sock, len, sizeof(msg));
1225 *adminp = admin->next;
1226 free_connection(admin);
1230 /* process socket command */
1231 if (admin->response && msg.message != ADMIN_MESSAGE)
1233 PERROR("Data from socket %d while sending response.\n", admin->sock);
1234 *adminp = admin->next;
1235 free_connection(admin);
1239 switch (msg.message)
1241 case ADMIN_REQUEST_CMD_INTERFACE:
1242 if (admin_interface(&admin->response) < 0)
1244 PERROR("Failed to create dial response for socket %d.\n", admin->sock);
1245 goto response_error;
1249 case ADMIN_REQUEST_CMD_ROUTE:
1250 if (admin_route(&admin->response) < 0)
1252 PERROR("Failed to create dial response for socket %d.\n", admin->sock);
1253 goto response_error;
1257 case ADMIN_REQUEST_CMD_DIAL:
1258 if (admin_dial(&admin->response, msg.u.x.message) < 0)
1260 PERROR("Failed to create dial response for socket %d.\n", admin->sock);
1261 goto response_error;
1265 case ADMIN_REQUEST_CMD_RELEASE:
1266 if (admin_release(&admin->response, msg.u.x.message) < 0)
1268 PERROR("Failed to create release response for socket %d.\n", admin->sock);
1269 goto response_error;
1273 case ADMIN_REQUEST_STATE:
1274 if (admin_state(&admin->response) < 0)
1276 PERROR("Failed to create state response for socket %d.\n", admin->sock);
1277 goto response_error;
1281 case ADMIN_TRACE_REQUEST:
1282 if (admin_trace(admin, &msg.u.trace_req) < 0)
1284 PERROR("Failed to create trace response for socket %d.\n", admin->sock);
1285 goto response_error;
1289 case ADMIN_REQUEST_CMD_BLOCK:
1290 if (admin_block(&admin->response, msg.u.x.portnum, msg.u.x.block) < 0)
1292 PERROR("Failed to create block response for socket %d.\n", admin->sock);
1293 goto response_error;
1298 if (admin_message_to_join(&msg.u.msg, admin) < 0)
1300 PERROR("Failed to deliver message for socket %d.\n", admin->sock);
1301 goto response_error;
1306 struct admin_queue *response;
1308 response = admin->response;
1311 printf("%c", '0'+response->am[0].message);
1312 response=response->next;
1319 case ADMIN_CALL_SETUP:
1320 if (admin_call(admin, &msg) < 0)
1322 PERROR("Failed to create call for socket %d.\n", admin->sock);
1324 *adminp = admin->next;
1325 free_connection(admin);
1332 PERROR("Invalid message %d from socket %d.\n", msg.message, admin->sock);
1333 *adminp = admin->next;
1334 free_connection(admin);
1340 if (admin->response)
1343 //PERROR("DEBUG socket %d sending data.\n", admin->sock);
1344 len = write(admin->sock, ((unsigned char *)(admin->response->am))+admin->response->offset, sizeof(struct admin_message)*(admin->response->num)-admin->response->offset);
1347 if (errno != EWOULDBLOCK)
1357 if (len < (int)(sizeof(struct admin_message)*(admin->response->num) - admin->response->offset))
1359 admin->response->offset+=len;
1363 temp = admin->response;
1364 admin->response = admin->response->next;
1369 /* done with socket instance */
1371 adminp = &admin->next;
1372 admin = admin->next;