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;
84 /* free remote joins */
85 if (admin->remote_name[0])
94 "REMOTE APP release");
95 add_trace("app", "name", "%s", admin->remote_name);
100 joinnext = join->next;
101 if (join->j_type==JOIN_TYPE_REMOTE) if (((class JoinRemote *)join)->j_remote_id == admin->sock)
103 memset(¶m, 0, sizeof(param));
104 param.disconnectinfo.cause = CAUSE_OUTOFORDER;
105 param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
106 ((class JoinRemote *)join)->message_remote(MESSAGE_RELEASE, ¶m);
107 /* join is now destroyed, so we go to next join */
113 if (admin->sock >= 0)
118 // printf("new\n", response);
119 response = admin->response;
123 // printf("%x\n", response);
124 temp = response->next;
127 response = (struct admin_queue *)temp;
129 // printf("new2\n", response);
131 // printf("new3\n", response);
137 * cleanup admin socket
139 void admin_cleanup(void)
141 struct admin_list *admin, *next;;
148 free_connection(admin);
161 * do interface reload
163 int admin_interface(struct admin_queue **responsep)
165 struct admin_queue *response; /* response pointer */
169 if (read_interfaces())
172 free_interfaces(interface_first);
173 interface_first = interface_newlist;
174 interface_newlist = NULL;
177 err_txt = interface_error;
180 /* create state response */
181 response = (struct admin_queue *)MALLOC(sizeof(struct admin_queue)+sizeof(admin_message));
185 response->am[0].message = ADMIN_RESPONSE_CMD_INTERFACE;
187 response->am[0].u.x.error = err;
189 SCPY(response->am[0].u.x.message, err_txt);
190 /* attach to response chain */
191 *responsep = response;
192 responsep = &response->next;
201 int admin_route(struct admin_queue **responsep)
203 struct route_ruleset *ruleset_new;
204 struct admin_queue *response; /* response pointer */
205 char err_txt[256] = "";
210 class EndpointAppPBX *apppbx;
214 apppbx = apppbx_first;
218 apppbx = apppbx->next;
222 SPRINT(err_txt, "Cannot reload routing, because %d endpoints active\n", n);
227 if (!(ruleset_new = ruleset_parse()))
229 SPRINT(err_txt, ruleset_error);
233 ruleset_free(ruleset_first);
234 ruleset_first = ruleset_new;
235 ruleset_main = getrulesetbyname("main");
238 SPRINT(err_txt, "Ruleset reloaded, but rule 'main' not found.\n");
241 apppbx = apppbx_first;
244 if (apppbx->e_action)
246 switch(apppbx->e_action->index)
248 case ACTION_INTERNAL:
249 apppbx->e_action = &action_internal;
251 case ACTION_EXTERNAL:
252 apppbx->e_action = &action_external;
255 apppbx->e_action = &action_remote;
257 case ACTION_VBOX_RECORD:
258 apppbx->e_action = &action_vbox;
260 case ACTION_PARTYLINE:
261 apppbx->e_action = &action_partyline;
266 } else if (apppbx->e_state != EPOINT_STATE_CONNECT)
269 apppbx->e_callback = 0;
270 apppbx->e_action = NULL;
271 apppbx->release(RELEASE_ALL, LOCATION_PRIVATE_LOCAL, CAUSE_NORMAL, LOCATION_PRIVATE_LOCAL, CAUSE_NORMAL);
274 numberrize_callerinfo(apppbx->e_callerinfo.id, apppbx->e_callerinfo.ntype, options.national, options.international),
275 apppbx->e_dialinginfo.id,
278 apppbx->ea_endpoint->ep_serial,
279 "KICK (reload routing)");
283 apppbx->e_action_timeout = 0;
284 apppbx->e_rule = NULL;
285 apppbx->e_ruleset = NULL;
287 apppbx = apppbx->next;
291 /* create state response */
292 response = (struct admin_queue *)MALLOC(sizeof(struct admin_queue)+sizeof(admin_message));
296 response->am[0].message = ADMIN_RESPONSE_CMD_ROUTE;
298 response->am[0].u.x.error = err;
300 SCPY(response->am[0].u.x.message, err_txt);
301 /* attach to response chain */
302 *responsep = response;
303 responsep = &response->next;
312 int admin_dial(struct admin_queue **responsep, char *message)
314 struct extension ext; /* temporary extension's settings */
315 struct admin_queue *response; /* response pointer */
316 char *p; /* pointer to dialing digits */
318 /* create state response */
319 response = (struct admin_queue *)MALLOC(sizeof(struct admin_queue)+sizeof(admin_message));
323 response->am[0].message = ADMIN_RESPONSE_CMD_DIAL;
325 /* process request */
326 if (!(p = strchr(message,':')))
328 response->am[0].u.x.error = -EINVAL;
329 SPRINT(response->am[0].u.x.message, "no seperator ':' in message to seperate number from extension");
334 /* modify extension */
335 if (!read_extension(&ext, message))
337 response->am[0].u.x.error = -EINVAL;
338 SPRINT(response->am[0].u.x.message, "extension doesn't exist");
342 write_extension(&ext, message);
345 /* attach to response chain */
346 *responsep = response;
347 responsep = &response->next;
355 int admin_trace(struct admin_list *admin, struct admin_trace_req *trace)
357 memcpy(&admin->trace, trace, sizeof(struct admin_trace_req));
365 * 0 = make port available
366 * 1 = make port administratively blocked
368 * the result is returned:
369 * 0 = port is now available
370 * 1 = port is now blocked
371 * 2 = port cannot be loaded or has been unloaded
372 * -1 = port doesn't exist
374 int admin_block(struct admin_queue **responsep, int portnum, int block)
376 struct admin_queue *response; /* response pointer */
377 struct interface *interface;
378 struct interface_port *ifport;
380 /* create block response */
381 response = (struct admin_queue *)MALLOC(sizeof(struct admin_queue)+sizeof(admin_message));
385 response->am[0].message = ADMIN_RESPONSE_CMD_BLOCK;
386 response->am[0].u.x.portnum = portnum;
388 /* search for port */
389 interface = interface_first;
392 ifport = interface->ifport;
395 if (ifport->portnum == portnum)
397 ifport = ifport->next;
401 interface = interface->next;
403 /* not found, we return -1 */
406 response->am[0].u.x.block = -1;
407 response->am[0].u.x.error = 1;
408 SPRINT(response->am[0].u.x.message, "Port %d does not exist.", portnum);
413 if (!ifport->mISDNport)
415 /* not loaded anyway */
418 response->am[0].u.x.block = 2;
422 /* try loading interface */
423 ifport->block = block;
426 /* port cannot load */
427 if (ifport->block >= 2)
429 response->am[0].u.x.block = 2;
430 response->am[0].u.x.error = 1;
431 SPRINT(response->am[0].u.x.message, "Port %d will not load.", portnum);
436 response->am[0].u.x.block = ifport->block;
440 /* if we shall unload interface */
443 mISDNport_close(ifport->mISDNport);
444 ifport->mISDNport = 0;
449 /* port new blocking state */
450 ifport->block = response->am[0].u.x.block = block;
453 /* attach to response chain */
454 *responsep = response;
455 responsep = &response->next;
463 int admin_release(struct admin_queue **responsep, char *message)
466 struct admin_queue *response; /* response pointer */
467 class EndpointAppPBX *apppbx;
469 /* create state response */
470 response = (struct admin_queue *)MALLOC(sizeof(struct admin_queue)+sizeof(admin_message));
474 response->am[0].message = ADMIN_RESPONSE_CMD_RELEASE;
477 apppbx = apppbx_first;
480 if (apppbx->ea_endpoint->ep_serial == id)
482 apppbx = apppbx->next;
486 response->am[0].u.x.error = -EINVAL;
487 SPRINT(response->am[0].u.x.message, "Given endpoint %d doesn't exist.", id);
491 apppbx->e_callback = 0;
492 apppbx->release(RELEASE_ALL, LOCATION_PRIVATE_LOCAL, CAUSE_NORMAL, LOCATION_PRIVATE_LOCAL, CAUSE_NORMAL);
495 /* attach to response chain */
496 *responsep = response;
497 responsep = &response->next;
505 int admin_call(struct admin_list *admin, struct admin_message *msg)
507 class Endpoint *epoint;
508 class EndpointAppPBX *apppbx;
510 if (!(epoint = new Endpoint(0, 0)))
511 FATAL("No memory for Endpoint instance\n");
512 if (!(epoint->ep_app = apppbx = new DEFAULT_ENDPOINT_APP(epoint, 1))) // outgoing
513 FATAL("No memory for Endpoint Application instance\n");
514 apppbx->e_adminid = admin->sockserial;
515 admin->epointid = epoint->ep_serial;
516 SCPY(apppbx->e_callerinfo.id, nationalize_callerinfo(msg->u.call.callerid, &apppbx->e_callerinfo.ntype, options.national, options.international));
517 if (msg->u.call.present)
518 apppbx->e_callerinfo.present = INFO_PRESENT_ALLOWED;
520 apppbx->e_callerinfo.present = INFO_PRESENT_RESTRICTED;
521 apppbx->e_callerinfo.screen = INFO_SCREEN_NETWORK;
523 //printf("hh=%d\n", apppbx->e_capainfo.hlc);
525 apppbx->e_capainfo.bearer_capa = msg->u.call.bc_capa;
526 apppbx->e_capainfo.bearer_mode = msg->u.call.bc_mode;
527 apppbx->e_capainfo.bearer_info1 = msg->u.call.bc_info1;
528 apppbx->e_capainfo.hlc = msg->u.call.hlc;
529 apppbx->e_capainfo.exthlc = msg->u.call.exthlc;
530 SCPY(apppbx->e_dialinginfo.id, msg->u.call.dialing);
531 SCPY(apppbx->e_dialinginfo.interfaces, msg->u.call.interface);
532 apppbx->e_dialinginfo.sending_complete = 1;
534 apppbx->new_state(PORT_STATE_OUT_SETUP);
541 * this function is called for response whenever a call state changes.
543 void admin_call_response(int adminid, int message, char *connected, int cause, int location, int notify)
545 struct admin_list *admin;
546 struct admin_queue *response, **responsep; /* response pointer */
548 /* searching for admin id
549 * maybe there is no admin instance, because the calling port was not
550 * initiated by admin_call */
554 if (adminid == admin->sockserial)
561 /* seek to end of response list */
562 response = admin->response;
563 responsep = &admin->response;
566 responsep = &response->next;
567 response = response->next;
570 /* create state response */
571 response = (struct admin_queue *)MALLOC(sizeof(struct admin_queue)+sizeof(admin_message));
575 response->am[0].message = message;
576 // printf("MESSAGE: %d\n", message);
578 SCPY(response->am[0].u.call.callerid, connected);
579 response->am[0].u.call.cause = cause;
580 response->am[0].u.call.location = location;
581 response->am[0].u.call.notify = notify;
583 /* attach to response chain */
584 *responsep = response;
585 responsep = &response->next;
590 * send data to the remote socket join instance
592 int admin_message_to_join(struct admin_msg *msg, struct admin_list *admin)
595 struct admin_list *temp;
598 if (msg->type == MESSAGE_HELLO)
600 if (admin->remote_name[0])
602 PERROR("Remote application repeats hello message.\n");
605 /* look for second application */
609 if (!strcmp(temp->remote_name, msg->param.hello.application))
615 PERROR("Remote application connects twice??? (ignoring)\n");
618 /* set remote socket instance */
619 SCPY(admin->remote_name, msg->param.hello.application);
627 "REMOTE APP registers");
628 add_trace("app", "name", "%s", admin->remote_name);
633 /* check we have no application name */
634 if (!admin->remote_name[0])
636 PERROR("Remote application did not send us a hello message.\n");
641 if (msg->type == MESSAGE_NEWREF)
643 /* create new join instance */
644 join = new JoinRemote(0, admin->remote_name, admin->sock); // must have no serial, because no endpoint is connected
647 FATAL("No memory for remote join instance\n");
654 * no ref given for *_ack */
655 if (msg->type == MESSAGE_BCHANNEL)
656 if (msg->param.bchannel.type == BCHANNEL_ASSIGN_ACK
657 || msg->param.bchannel.type == BCHANNEL_REMOVE_ACK
658 || msg->param.bchannel.type == BCHANNEL_RELEASE)
660 /* no ref, but address */
661 message_bchannel_from_remote(NULL, msg->param.bchannel.type, msg->param.bchannel.handle);
668 PERROR("Remote application did not send us a valid ref with a message.\n");
672 /* find join instance */
676 if (join->j_serial == msg->ref)
682 PERROR("No join found with serial %d.\n", msg->ref);
686 /* check application */
687 if (join->j_type != JOIN_TYPE_REMOTE)
689 PERROR("Ref %d does not belong to a remote join instance.\n", msg->ref);
692 if (admin->sock != ((class JoinRemote *)join)->j_remote_id)
694 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);
699 ((class JoinRemote *)join)->message_remote(msg->type, &msg->param);
706 * this function is called for every message to remote socket
708 int admin_message_from_join(int remote_id, unsigned long ref, int message_type, union parameter *param)
710 struct admin_list *admin;
711 struct admin_queue **responsep; /* response pointer */
713 /* searching for admin id
714 * maybe there is no given remote application
719 if (admin->remote_name[0] && admin->sock==remote_id)
723 /* no given remote application connected */
727 /* seek to end of response list */
728 responsep = &admin->response;
731 responsep = &(*responsep)->next;
734 /* create state response */
735 *responsep = (struct admin_queue *)MALLOC(sizeof(struct admin_queue)+sizeof(admin_message));
737 (*responsep)->num = 1;
740 (*responsep)->am[0].message = ADMIN_MESSAGE;
741 (*responsep)->am[0].u.msg.type = message_type;
742 (*responsep)->am[0].u.msg.ref = ref;
743 memcpy(&(*responsep)->am[0].u.msg.param, param, sizeof(union parameter));
752 int admin_state(struct admin_queue **responsep)
756 class EndpointAppPBX *apppbx;
759 struct interface *interface;
760 struct interface_port *ifport;
761 struct mISDNport *mISDNport;
765 struct admin_queue *response;
766 struct admin_list *admin;
768 /* create state response */
769 response = (struct admin_queue *)MALLOC(sizeof(struct admin_queue)+sizeof(admin_message));
773 response->am[0].message = ADMIN_RESPONSE_STATE;
775 SCPY(response->am[0].u.s.version_string, VERSION_STRING);
777 memcpy(&response->am[0].u.s.tm, now_tm, sizeof(struct tm));
779 SCPY(response->am[0].u.s.logfile, options.log);
780 /* interface count */
782 interface = interface_first;
785 ifport = interface->ifport;
789 ifport = ifport->next;
791 interface = interface->next;
793 response->am[0].u.s.interfaces = i;
794 /* remote connection count */
799 if (admin->remote_name[0])
803 response->am[0].u.s.remotes = i;
812 response->am[0].u.s.joins = i;
814 apppbx = apppbx_first;
819 apppbx = apppbx->next;
821 response->am[0].u.s.epoints = i;
830 response->am[0].u.s.ports = i;
831 /* attach to response chain */
832 *responsep = response;
833 responsep = &response->next;
835 /* create response for all instances */
836 num = (response->am[0].u.s.interfaces)
837 + (response->am[0].u.s.remotes)
838 + (response->am[0].u.s.joins)
839 + (response->am[0].u.s.epoints)
840 + (response->am[0].u.s.ports);
843 response = (struct admin_queue *)MALLOC(sizeof(admin_queue)+(num*sizeof(admin_message)));
846 *responsep = response;
847 responsep = &response->next;
848 interface = interface_first;
852 ifport = interface->ifport;
856 response->am[num].message = ADMIN_RESPONSE_S_INTERFACE;
858 SCPY(response->am[num].u.i.interface_name, interface->name);
860 response->am[num].u.i.portnum = ifport->portnum;
862 response->am[num].u.i.extension = interface->extension;
864 response->am[num].u.i.block = ifport->block;
865 if (ifport->mISDNport)
867 mISDNport = ifport->mISDNport;
870 response->am[num].u.i.ptp = mISDNport->ptp;
872 response->am[num].u.i.ntmode = mISDNport->ntmode;
874 response->am[num].u.i.pri = mISDNport->pri;
876 response->am[num].u.i.use = mISDNport->use;
878 response->am[num].u.i.l1link = mISDNport->l1link;
880 response->am[num].u.i.l2link = mISDNport->l2link;
882 response->am[num].u.i.los = mISDNport->los;
884 response->am[num].u.i.ais = mISDNport->ais;
886 response->am[num].u.i.rdi = mISDNport->rdi;
888 response->am[num].u.i.slip_tx = mISDNport->slip_tx;
889 response->am[num].u.i.slip_rx = mISDNport->slip_rx;
891 response->am[num].u.i.channels = mISDNport->b_num;
895 while(i < mISDNport->b_num)
897 response->am[num].u.i.busy[i] = mISDNport->b_state[i];
898 if (mISDNport->b_port[i])
899 response->am[num].u.i.port[i] = mISDNport->b_port[i]->p_serial;
905 ifport = ifport->next;
907 interface = interface->next;
910 /* create response for all remotes */
914 if (admin->remote_name[0])
917 response->am[num].message = ADMIN_RESPONSE_S_REMOTE;
919 SCPY(response->am[num].u.r.name, admin->remote_name);
926 /* create response for all joins */
931 response->am[num].message = ADMIN_RESPONSE_S_JOIN;
933 response->am[num].u.j.serial = join->j_serial;
935 if (join->j_type == JOIN_TYPE_PBX)
936 response->am[num].u.j.partyline = ((class JoinPBX *)join)->j_partyline;
937 /* remote application */
938 if (join->j_type == JOIN_TYPE_REMOTE)
939 SCPY(response->am[num].u.j.remote, ((class JoinRemote *)join)->j_remote_name);
945 /* create response for all endpoint */
946 apppbx = apppbx_first;
950 response->am[num].message = ADMIN_RESPONSE_S_EPOINT;
952 response->am[num].u.e.serial = apppbx->ea_endpoint->ep_serial;
954 response->am[num].u.e.join = apppbx->ea_endpoint->ep_join_id;
955 /* rx notification */
956 response->am[num].u.e.rx_state = apppbx->e_rx_state;
957 /* tx notification */
958 response->am[num].u.e.tx_state = apppbx->e_tx_state;
960 switch(apppbx->e_state)
962 case EPOINT_STATE_IN_SETUP:
963 response->am[num].u.e.state = ADMIN_STATE_IN_SETUP;
965 case EPOINT_STATE_OUT_SETUP:
966 response->am[num].u.e.state = ADMIN_STATE_OUT_SETUP;
968 case EPOINT_STATE_IN_OVERLAP:
969 response->am[num].u.e.state = ADMIN_STATE_IN_OVERLAP;
971 case EPOINT_STATE_OUT_OVERLAP:
972 response->am[num].u.e.state = ADMIN_STATE_OUT_OVERLAP;
974 case EPOINT_STATE_IN_PROCEEDING:
975 response->am[num].u.e.state = ADMIN_STATE_IN_PROCEEDING;
977 case EPOINT_STATE_OUT_PROCEEDING:
978 response->am[num].u.e.state = ADMIN_STATE_OUT_PROCEEDING;
980 case EPOINT_STATE_IN_ALERTING:
981 response->am[num].u.e.state = ADMIN_STATE_IN_ALERTING;
983 case EPOINT_STATE_OUT_ALERTING:
984 response->am[num].u.e.state = ADMIN_STATE_OUT_ALERTING;
986 case EPOINT_STATE_CONNECT:
987 response->am[num].u.e.state = ADMIN_STATE_CONNECT;
989 case EPOINT_STATE_IN_DISCONNECT:
990 response->am[num].u.e.state = ADMIN_STATE_IN_DISCONNECT;
992 case EPOINT_STATE_OUT_DISCONNECT:
993 response->am[num].u.e.state = ADMIN_STATE_OUT_DISCONNECT;
996 response->am[num].u.e.state = ADMIN_STATE_IDLE;
999 SCPY(response->am[num].u.e.terminal, apppbx->e_ext.number);
1001 SCPY(response->am[num].u.e.callerid, apppbx->e_callerinfo.id);
1003 SCPY(response->am[num].u.e.dialing, apppbx->e_dialinginfo.id);
1005 if (apppbx->e_action)
1006 SCPY(response->am[num].u.e.action, action_defs[apppbx->e_action->index].name);
1007 // if (apppbx->e_action)
1008 // printf("action=%s\n",action_defs[apppbx->e_action->index].name);
1010 response->am[num].u.e.park = apppbx->ea_endpoint->ep_park;
1011 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))
1012 memcpy(response->am[num].u.e.park_callid, apppbx->ea_endpoint->ep_park_callid, apppbx->ea_endpoint->ep_park_len);
1013 response->am[num].u.e.park_len = apppbx->ea_endpoint->ep_park_len;
1015 if (apppbx->e_crypt == CRYPT_ON)
1016 response->am[num].u.e.crypt = 1;
1018 apppbx = apppbx->next;
1022 /* create response for all ports */
1027 response->am[num].message = ADMIN_RESPONSE_S_PORT;
1029 response->am[num].u.p.serial = port->p_serial;
1031 SCPY(response->am[num].u.p.name, port->p_name);
1033 response->am[num].u.p.epoint = ACTIVE_EPOINT(port->p_epointlist);
1035 switch(port->p_state)
1037 case PORT_STATE_IN_SETUP:
1038 response->am[num].u.p.state = ADMIN_STATE_IN_SETUP;
1040 case PORT_STATE_OUT_SETUP:
1041 response->am[num].u.p.state = ADMIN_STATE_OUT_SETUP;
1043 case PORT_STATE_IN_OVERLAP:
1044 response->am[num].u.p.state = ADMIN_STATE_IN_OVERLAP;
1046 case PORT_STATE_OUT_OVERLAP:
1047 response->am[num].u.p.state = ADMIN_STATE_OUT_OVERLAP;
1049 case PORT_STATE_IN_PROCEEDING:
1050 response->am[num].u.p.state = ADMIN_STATE_IN_PROCEEDING;
1052 case PORT_STATE_OUT_PROCEEDING:
1053 response->am[num].u.p.state = ADMIN_STATE_OUT_PROCEEDING;
1055 case PORT_STATE_IN_ALERTING:
1056 response->am[num].u.p.state = ADMIN_STATE_IN_ALERTING;
1058 case PORT_STATE_OUT_ALERTING:
1059 response->am[num].u.p.state = ADMIN_STATE_OUT_ALERTING;
1061 case PORT_STATE_CONNECT:
1062 response->am[num].u.p.state = ADMIN_STATE_CONNECT;
1064 case PORT_STATE_IN_DISCONNECT:
1065 response->am[num].u.p.state = ADMIN_STATE_IN_DISCONNECT;
1067 case PORT_STATE_OUT_DISCONNECT:
1068 response->am[num].u.p.state = ADMIN_STATE_OUT_DISCONNECT;
1071 response->am[num].u.p.state = ADMIN_STATE_IDLE;
1074 if ((port->p_type&PORT_CLASS_mISDN_MASK) == PORT_CLASS_mISDN_DSS1)
1076 response->am[num].u.p.isdn = 1;
1077 pdss1 = (class Pdss1 *)port;
1078 response->am[num].u.p.isdn_chan = pdss1->p_m_b_channel;
1079 response->am[num].u.p.isdn_hold = pdss1->p_m_hold;
1080 response->am[num].u.p.isdn_ces = pdss1->p_m_d_ces;
1089 int sockserial = 1; // must start with 1, because 0 is used if no serial is set
1091 * handle admin socket (non blocking)
1093 int admin_handle(void)
1095 struct admin_list *admin, **adminp;
1097 struct admin_message msg;
1100 socklen_t sock_len = sizeof(sock_address);
1101 unsigned long on = 1;
1102 int work = 0; /* if work was done */
1103 struct Endpoint *epoint;
1108 /* check for new incoming connections */
1109 if ((new_sock = accept(sock, (struct sockaddr *)&sock_address, &sock_len)) >= 0)
1112 /* insert new socket */
1113 admin = (struct admin_list *)MALLOC(sizeof(struct admin_list));
1114 if (ioctl(new_sock, FIONBIO, (unsigned char *)(&on)) >= 0)
1117 // PERROR("DEBUG incoming socket %d, serial=%d\n", new_sock, sockserial);
1120 admin->sockserial = sockserial++;
1121 admin->next = admin_first;
1122 admin_first = admin;
1123 admin->sock = new_sock;
1126 FREE(admin, sizeof(struct admin_list));
1130 if (errno != EWOULDBLOCK)
1132 PERROR("Failed to accept connection from socket \"%s\". (errno=%d) Closing socket.\n", sock_address.sun_path, errno);
1138 /* loop all current socket connections */
1139 admin = admin_first;
1140 adminp = &admin_first;
1144 len = read(admin->sock, &msg, sizeof(msg));
1147 if (errno != EWOULDBLOCK)
1151 printf("Broken pipe on socket %d. (errno=%d).\n", admin->sock, errno);
1152 PDEBUG(DEBUG_LOG, "Broken pipe on socket %d. (errno=%d).\n", admin->sock, errno);
1153 *adminp = admin->next;
1154 free_connection(admin);
1162 //PERROR("DEBUG socket %d got data. serial=%d\n", admin->sock, admin->sockserial);
1167 /*release endpoint if exists */
1168 if (admin->epointid)
1170 epoint = find_epoint_id(admin->epointid);
1173 ((class DEFAULT_ENDPOINT_APP *)epoint->ep_app)->
1174 release(RELEASE_ALL, LOCATION_PRIVATE_LOCAL, CAUSE_NORMAL, LOCATION_PRIVATE_LOCAL, CAUSE_NORMAL);
1179 //PERROR("DEBUG socket %d closed by remote.\n", admin->sock);
1180 *adminp = admin->next;
1181 free_connection(admin);
1183 //PERROR("DEBUG (admin_first=%x)\n", admin_first);
1186 if (len != sizeof(msg))
1188 PERROR("Short/long read on socket %d. (len=%d != size=%d).\n", admin->sock, len, sizeof(msg));
1189 *adminp = admin->next;
1190 free_connection(admin);
1194 /* process socket command */
1195 if (admin->response && msg.message != ADMIN_MESSAGE)
1197 PERROR("Data from socket %d while sending response.\n", admin->sock);
1198 *adminp = admin->next;
1199 free_connection(admin);
1203 switch (msg.message)
1205 case ADMIN_REQUEST_CMD_INTERFACE:
1206 if (admin_interface(&admin->response) < 0)
1208 PERROR("Failed to create dial response for socket %d.\n", admin->sock);
1209 goto response_error;
1213 case ADMIN_REQUEST_CMD_ROUTE:
1214 if (admin_route(&admin->response) < 0)
1216 PERROR("Failed to create dial response for socket %d.\n", admin->sock);
1217 goto response_error;
1221 case ADMIN_REQUEST_CMD_DIAL:
1222 if (admin_dial(&admin->response, msg.u.x.message) < 0)
1224 PERROR("Failed to create dial response for socket %d.\n", admin->sock);
1225 goto response_error;
1229 case ADMIN_REQUEST_CMD_RELEASE:
1230 if (admin_release(&admin->response, msg.u.x.message) < 0)
1232 PERROR("Failed to create release response for socket %d.\n", admin->sock);
1233 goto response_error;
1237 case ADMIN_REQUEST_STATE:
1238 if (admin_state(&admin->response) < 0)
1240 PERROR("Failed to create state response for socket %d.\n", admin->sock);
1241 goto response_error;
1245 case ADMIN_TRACE_REQUEST:
1246 if (admin_trace(admin, &msg.u.trace_req) < 0)
1248 PERROR("Failed to create trace response for socket %d.\n", admin->sock);
1249 goto response_error;
1253 case ADMIN_REQUEST_CMD_BLOCK:
1254 if (admin_block(&admin->response, msg.u.x.portnum, msg.u.x.block) < 0)
1256 PERROR("Failed to create block response for socket %d.\n", admin->sock);
1257 goto response_error;
1262 if (admin_message_to_join(&msg.u.msg, admin) < 0)
1264 PERROR("Failed to deliver message for socket %d.\n", admin->sock);
1265 goto response_error;
1270 struct admin_queue *response;
1272 response = admin->response;
1275 printf("%c", '0'+response->am[0].message);
1276 response=response->next;
1283 case ADMIN_CALL_SETUP:
1284 if (admin_call(admin, &msg) < 0)
1286 PERROR("Failed to create call for socket %d.\n", admin->sock);
1288 *adminp = admin->next;
1289 free_connection(admin);
1296 PERROR("Invalid message %d from socket %d.\n", msg.message, admin->sock);
1297 *adminp = admin->next;
1298 free_connection(admin);
1304 if (admin->response)
1307 //PERROR("DEBUG socket %d sending data.\n", admin->sock);
1308 len = write(admin->sock, ((unsigned char *)(admin->response->am))+admin->response->offset, sizeof(struct admin_message)*(admin->response->num)-admin->response->offset);
1311 if (errno != EWOULDBLOCK)
1321 if (len < (int)(sizeof(struct admin_message)*(admin->response->num) - admin->response->offset))
1323 admin->response->offset+=len;
1327 temp = admin->response;
1328 admin->response = admin->response->next;
1333 /* done with socket instance */
1335 adminp = &admin->next;
1336 admin = admin->next;