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;
917 memcpy(response->am[num].u.i.l2mask, mISDNport->l2mask, 16);
919 response->am[num].u.i.los = mISDNport->los;
921 response->am[num].u.i.ais = mISDNport->ais;
923 response->am[num].u.i.rdi = mISDNport->rdi;
925 response->am[num].u.i.slip_tx = mISDNport->slip_tx;
926 response->am[num].u.i.slip_rx = mISDNport->slip_rx;
928 response->am[num].u.i.channels = mISDNport->b_num;
932 while(i < mISDNport->b_num)
934 response->am[num].u.i.busy[i] = mISDNport->b_state[i];
935 if (mISDNport->b_port[i])
936 response->am[num].u.i.port[i] = mISDNport->b_port[i]->p_serial;
937 response->am[num].u.i.mode[i] = mISDNport->b_mode[i];
943 ifport = ifport->next;
945 interface = interface->next;
948 /* create response for all remotes */
952 if (admin->remote_name[0])
955 response->am[num].message = ADMIN_RESPONSE_S_REMOTE;
957 SCPY(response->am[num].u.r.name, admin->remote_name);
964 /* create response for all joins */
969 response->am[num].message = ADMIN_RESPONSE_S_JOIN;
971 response->am[num].u.j.serial = join->j_serial;
973 if (join->j_type == JOIN_TYPE_PBX)
974 response->am[num].u.j.partyline = ((class JoinPBX *)join)->j_partyline;
975 /* remote application */
976 if (join->j_type == JOIN_TYPE_REMOTE)
977 SCPY(response->am[num].u.j.remote, ((class JoinRemote *)join)->j_remote_name);
983 /* create response for all endpoint */
984 apppbx = apppbx_first;
988 response->am[num].message = ADMIN_RESPONSE_S_EPOINT;
990 response->am[num].u.e.serial = apppbx->ea_endpoint->ep_serial;
992 response->am[num].u.e.join = apppbx->ea_endpoint->ep_join_id;
993 /* rx notification */
994 response->am[num].u.e.rx_state = apppbx->e_rx_state;
995 /* tx notification */
996 response->am[num].u.e.tx_state = apppbx->e_tx_state;
998 switch(apppbx->e_state)
1000 case EPOINT_STATE_IN_SETUP:
1001 response->am[num].u.e.state = ADMIN_STATE_IN_SETUP;
1003 case EPOINT_STATE_OUT_SETUP:
1004 response->am[num].u.e.state = ADMIN_STATE_OUT_SETUP;
1006 case EPOINT_STATE_IN_OVERLAP:
1007 response->am[num].u.e.state = ADMIN_STATE_IN_OVERLAP;
1009 case EPOINT_STATE_OUT_OVERLAP:
1010 response->am[num].u.e.state = ADMIN_STATE_OUT_OVERLAP;
1012 case EPOINT_STATE_IN_PROCEEDING:
1013 response->am[num].u.e.state = ADMIN_STATE_IN_PROCEEDING;
1015 case EPOINT_STATE_OUT_PROCEEDING:
1016 response->am[num].u.e.state = ADMIN_STATE_OUT_PROCEEDING;
1018 case EPOINT_STATE_IN_ALERTING:
1019 response->am[num].u.e.state = ADMIN_STATE_IN_ALERTING;
1021 case EPOINT_STATE_OUT_ALERTING:
1022 response->am[num].u.e.state = ADMIN_STATE_OUT_ALERTING;
1024 case EPOINT_STATE_CONNECT:
1025 response->am[num].u.e.state = ADMIN_STATE_CONNECT;
1027 case EPOINT_STATE_IN_DISCONNECT:
1028 response->am[num].u.e.state = ADMIN_STATE_IN_DISCONNECT;
1030 case EPOINT_STATE_OUT_DISCONNECT:
1031 response->am[num].u.e.state = ADMIN_STATE_OUT_DISCONNECT;
1034 response->am[num].u.e.state = ADMIN_STATE_IDLE;
1037 SCPY(response->am[num].u.e.terminal, apppbx->e_ext.number);
1039 SCPY(response->am[num].u.e.callerid, apppbx->e_callerinfo.id);
1041 SCPY(response->am[num].u.e.dialing, apppbx->e_dialinginfo.id);
1043 if (apppbx->e_action)
1044 SCPY(response->am[num].u.e.action, action_defs[apppbx->e_action->index].name);
1045 // if (apppbx->e_action)
1046 // printf("action=%s\n",action_defs[apppbx->e_action->index].name);
1048 response->am[num].u.e.park = apppbx->ea_endpoint->ep_park;
1049 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))
1050 memcpy(response->am[num].u.e.park_callid, apppbx->ea_endpoint->ep_park_callid, apppbx->ea_endpoint->ep_park_len);
1051 response->am[num].u.e.park_len = apppbx->ea_endpoint->ep_park_len;
1053 if (apppbx->e_crypt == CRYPT_ON)
1054 response->am[num].u.e.crypt = 1;
1056 apppbx = apppbx->next;
1060 /* create response for all ports */
1065 response->am[num].message = ADMIN_RESPONSE_S_PORT;
1067 response->am[num].u.p.serial = port->p_serial;
1069 SCPY(response->am[num].u.p.name, port->p_name);
1071 response->am[num].u.p.epoint = ACTIVE_EPOINT(port->p_epointlist);
1073 switch(port->p_state)
1075 case PORT_STATE_IN_SETUP:
1076 response->am[num].u.p.state = ADMIN_STATE_IN_SETUP;
1078 case PORT_STATE_OUT_SETUP:
1079 response->am[num].u.p.state = ADMIN_STATE_OUT_SETUP;
1081 case PORT_STATE_IN_OVERLAP:
1082 response->am[num].u.p.state = ADMIN_STATE_IN_OVERLAP;
1084 case PORT_STATE_OUT_OVERLAP:
1085 response->am[num].u.p.state = ADMIN_STATE_OUT_OVERLAP;
1087 case PORT_STATE_IN_PROCEEDING:
1088 response->am[num].u.p.state = ADMIN_STATE_IN_PROCEEDING;
1090 case PORT_STATE_OUT_PROCEEDING:
1091 response->am[num].u.p.state = ADMIN_STATE_OUT_PROCEEDING;
1093 case PORT_STATE_IN_ALERTING:
1094 response->am[num].u.p.state = ADMIN_STATE_IN_ALERTING;
1096 case PORT_STATE_OUT_ALERTING:
1097 response->am[num].u.p.state = ADMIN_STATE_OUT_ALERTING;
1099 case PORT_STATE_CONNECT:
1100 response->am[num].u.p.state = ADMIN_STATE_CONNECT;
1102 case PORT_STATE_IN_DISCONNECT:
1103 response->am[num].u.p.state = ADMIN_STATE_IN_DISCONNECT;
1105 case PORT_STATE_OUT_DISCONNECT:
1106 response->am[num].u.p.state = ADMIN_STATE_OUT_DISCONNECT;
1109 response->am[num].u.p.state = ADMIN_STATE_IDLE;
1112 if ((port->p_type&PORT_CLASS_mISDN_MASK) == PORT_CLASS_mISDN_DSS1)
1114 response->am[num].u.p.isdn = 1;
1115 pdss1 = (class Pdss1 *)port;
1116 response->am[num].u.p.isdn_chan = pdss1->p_m_b_channel;
1117 response->am[num].u.p.isdn_hold = pdss1->p_m_hold;
1118 response->am[num].u.p.isdn_ces = pdss1->p_m_d_ces;
1127 int sockserial = 1; // must start with 1, because 0 is used if no serial is set
1129 * handle admin socket (non blocking)
1131 int admin_handle(void)
1133 struct admin_list *admin, **adminp;
1135 struct admin_message msg;
1138 socklen_t sock_len = sizeof(sock_address);
1139 unsigned int on = 1;
1140 int work = 0; /* if work was done */
1141 struct Endpoint *epoint;
1146 /* check for new incoming connections */
1147 if ((new_sock = accept(sock, (struct sockaddr *)&sock_address, &sock_len)) >= 0)
1150 /* insert new socket */
1151 admin = (struct admin_list *)MALLOC(sizeof(struct admin_list));
1152 if (ioctl(new_sock, FIONBIO, (unsigned char *)(&on)) >= 0)
1155 // PERROR("DEBUG incoming socket %d, serial=%d\n", new_sock, sockserial);
1158 admin->sockserial = sockserial++;
1159 admin->next = admin_first;
1160 admin_first = admin;
1161 admin->sock = new_sock;
1164 FREE(admin, sizeof(struct admin_list));
1168 if (errno != EWOULDBLOCK)
1170 PERROR("Failed to accept connection from socket \"%s\". (errno=%d) Closing socket.\n", sock_address.sun_path, errno);
1176 /* loop all current socket connections */
1177 admin = admin_first;
1178 adminp = &admin_first;
1182 len = read(admin->sock, &msg, sizeof(msg));
1185 if (errno != EWOULDBLOCK)
1189 PDEBUG(DEBUG_LOG, "Broken pipe on socket %d. (errno=%d).\n", admin->sock, errno);
1190 *adminp = admin->next;
1191 free_connection(admin);
1199 //PERROR("DEBUG socket %d got data. serial=%d\n", admin->sock, admin->sockserial);
1204 /*release endpoint if exists */
1205 if (admin->epointid)
1207 epoint = find_epoint_id(admin->epointid);
1210 ((class DEFAULT_ENDPOINT_APP *)epoint->ep_app)->
1211 release(RELEASE_ALL, LOCATION_PRIVATE_LOCAL, CAUSE_NORMAL, LOCATION_PRIVATE_LOCAL, CAUSE_NORMAL);
1216 //PERROR("DEBUG socket %d closed by remote.\n", admin->sock);
1217 *adminp = admin->next;
1218 free_connection(admin);
1220 //PERROR("DEBUG (admin_first=%x)\n", admin_first);
1223 if (len != sizeof(msg))
1225 PERROR("Short/long read on socket %d. (len=%d != size=%d).\n", admin->sock, len, sizeof(msg));
1226 *adminp = admin->next;
1227 free_connection(admin);
1231 /* process socket command */
1232 if (admin->response && msg.message != ADMIN_MESSAGE)
1234 PERROR("Data from socket %d while sending response.\n", admin->sock);
1235 *adminp = admin->next;
1236 free_connection(admin);
1240 switch (msg.message)
1242 case ADMIN_REQUEST_CMD_INTERFACE:
1243 if (admin_interface(&admin->response) < 0)
1245 PERROR("Failed to create dial response for socket %d.\n", admin->sock);
1246 goto response_error;
1250 case ADMIN_REQUEST_CMD_ROUTE:
1251 if (admin_route(&admin->response) < 0)
1253 PERROR("Failed to create dial response for socket %d.\n", admin->sock);
1254 goto response_error;
1258 case ADMIN_REQUEST_CMD_DIAL:
1259 if (admin_dial(&admin->response, msg.u.x.message) < 0)
1261 PERROR("Failed to create dial response for socket %d.\n", admin->sock);
1262 goto response_error;
1266 case ADMIN_REQUEST_CMD_RELEASE:
1267 if (admin_release(&admin->response, msg.u.x.message) < 0)
1269 PERROR("Failed to create release response for socket %d.\n", admin->sock);
1270 goto response_error;
1274 case ADMIN_REQUEST_STATE:
1275 if (admin_state(&admin->response) < 0)
1277 PERROR("Failed to create state response for socket %d.\n", admin->sock);
1278 goto response_error;
1282 case ADMIN_TRACE_REQUEST:
1283 if (admin_trace(admin, &msg.u.trace_req) < 0)
1285 PERROR("Failed to create trace response for socket %d.\n", admin->sock);
1286 goto response_error;
1290 case ADMIN_REQUEST_CMD_BLOCK:
1291 if (admin_block(&admin->response, msg.u.x.portnum, msg.u.x.block) < 0)
1293 PERROR("Failed to create block response for socket %d.\n", admin->sock);
1294 goto response_error;
1299 if (admin_message_to_join(&msg.u.msg, admin) < 0)
1301 PERROR("Failed to deliver message for socket %d.\n", admin->sock);
1302 goto response_error;
1307 struct admin_queue *response;
1309 response = admin->response;
1312 printf("%c", '0'+response->am[0].message);
1313 response=response->next;
1320 case ADMIN_CALL_SETUP:
1321 if (admin_call(admin, &msg) < 0)
1323 PERROR("Failed to create call for socket %d.\n", admin->sock);
1325 *adminp = admin->next;
1326 free_connection(admin);
1333 PERROR("Invalid message %d from socket %d.\n", msg.message, admin->sock);
1334 *adminp = admin->next;
1335 free_connection(admin);
1341 if (admin->response)
1344 //PERROR("DEBUG socket %d sending data.\n", admin->sock);
1345 len = write(admin->sock, ((unsigned char *)(admin->response->am))+admin->response->offset, sizeof(struct admin_message)*(admin->response->num)-admin->response->offset);
1348 if (errno != EWOULDBLOCK)
1358 if (len < (int)(sizeof(struct admin_message)*(admin->response->num) - admin->response->offset))
1360 admin->response->offset+=len;
1364 temp = admin->response;
1365 admin->response = admin->response->next;
1370 /* done with socket instance */
1372 adminp = &admin->next;
1373 admin = admin->next;