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])
90 joinnext = join->next;
91 if (join->j_type==JOIN_TYPE_REMOTE) if (((class JoinRemote *)join)->j_remote_id == admin->sock)
93 memset(¶m, 0, sizeof(param));
94 param.disconnectinfo.cause = CAUSE_OUTOFORDER;
95 param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
96 ((class JoinRemote *)join)->message_remote(MESSAGE_RELEASE, ¶m);
97 /* join is now destroyed, so we go to next join */
103 if (admin->sock >= 0)
108 // printf("new\n", response);
109 response = admin->response;
113 // printf("%x\n", response);
114 temp = response->next;
117 response = (struct admin_queue *)temp;
119 // printf("new2\n", response);
121 // printf("new3\n", response);
127 * cleanup admin socket
129 void admin_cleanup(void)
131 struct admin_list *admin, *next;;
138 free_connection(admin);
151 * do interface reload
153 int admin_interface(struct admin_queue **responsep)
155 struct admin_queue *response; /* response pointer */
159 if (read_interfaces())
162 free_interfaces(interface_first);
163 interface_first = interface_newlist;
164 interface_newlist = NULL;
167 err_txt = interface_error;
170 /* create state response */
171 response = (struct admin_queue *)MALLOC(sizeof(struct admin_queue)+sizeof(admin_message));
175 response->am[0].message = ADMIN_RESPONSE_CMD_INTERFACE;
177 response->am[0].u.x.error = err;
179 SCPY(response->am[0].u.x.message, err_txt);
180 /* attach to response chain */
181 *responsep = response;
182 responsep = &response->next;
191 int admin_route(struct admin_queue **responsep)
193 struct route_ruleset *ruleset_new;
194 struct admin_queue *response; /* response pointer */
195 char err_txt[256] = "";
200 class EndpointAppPBX *apppbx;
204 apppbx = apppbx_first;
208 apppbx = apppbx->next;
212 SPRINT(err_txt, "Cannot reload routing, because %d endpoints active\n", n);
217 if (!(ruleset_new = ruleset_parse()))
219 SPRINT(err_txt, ruleset_error);
223 ruleset_free(ruleset_first);
224 ruleset_first = ruleset_new;
225 ruleset_main = getrulesetbyname("main");
228 SPRINT(err_txt, "Ruleset reloaded, but rule 'main' not found.\n");
231 apppbx = apppbx_first;
234 if (apppbx->e_action)
236 switch(apppbx->e_action->index)
238 case ACTION_INTERNAL:
239 apppbx->e_action = &action_internal;
241 case ACTION_EXTERNAL:
242 apppbx->e_action = &action_external;
245 apppbx->e_action = &action_remote;
247 case ACTION_VBOX_RECORD:
248 apppbx->e_action = &action_vbox;
250 case ACTION_PARTYLINE:
251 apppbx->e_action = &action_partyline;
256 } else if (apppbx->e_state != EPOINT_STATE_CONNECT)
259 apppbx->e_callback = 0;
260 apppbx->e_action = NULL;
261 apppbx->release(RELEASE_ALL, LOCATION_PRIVATE_LOCAL, CAUSE_NORMAL, LOCATION_PRIVATE_LOCAL, CAUSE_NORMAL);
264 numberrize_callerinfo(apppbx->e_callerinfo.id, apppbx->e_callerinfo.ntype, options.national, options.international),
265 apppbx->e_dialinginfo.id,
268 apppbx->ea_endpoint->ep_serial,
269 "KICK (reload routing)");
273 apppbx->e_action_timeout = 0;
274 apppbx->e_rule = NULL;
275 apppbx->e_ruleset = NULL;
277 apppbx = apppbx->next;
281 /* create state response */
282 response = (struct admin_queue *)MALLOC(sizeof(struct admin_queue)+sizeof(admin_message));
286 response->am[0].message = ADMIN_RESPONSE_CMD_ROUTE;
288 response->am[0].u.x.error = err;
290 SCPY(response->am[0].u.x.message, err_txt);
291 /* attach to response chain */
292 *responsep = response;
293 responsep = &response->next;
302 int admin_dial(struct admin_queue **responsep, char *message)
304 struct extension ext; /* temporary extension's settings */
305 struct admin_queue *response; /* response pointer */
306 char *p; /* pointer to dialing digits */
308 /* create state response */
309 response = (struct admin_queue *)MALLOC(sizeof(struct admin_queue)+sizeof(admin_message));
313 response->am[0].message = ADMIN_RESPONSE_CMD_DIAL;
315 /* process request */
316 if (!(p = strchr(message,':')))
318 response->am[0].u.x.error = -EINVAL;
319 SPRINT(response->am[0].u.x.message, "no seperator ':' in message to seperate number from extension");
324 /* modify extension */
325 if (!read_extension(&ext, message))
327 response->am[0].u.x.error = -EINVAL;
328 SPRINT(response->am[0].u.x.message, "extension doesn't exist");
332 write_extension(&ext, message);
335 /* attach to response chain */
336 *responsep = response;
337 responsep = &response->next;
345 int admin_trace(struct admin_list *admin, struct admin_trace_req *trace)
347 memcpy(&admin->trace, trace, sizeof(struct admin_trace_req));
355 * 0 = make port available
356 * 1 = make port administratively blocked
358 * the result is returned:
359 * 0 = port is now available
360 * 1 = port is now blocked
361 * 2 = port cannot be loaded or has been unloaded
362 * -1 = port doesn't exist
364 int admin_block(struct admin_queue **responsep, int portnum, int block)
366 struct admin_queue *response; /* response pointer */
367 struct interface *interface;
368 struct interface_port *ifport;
370 /* create block response */
371 response = (struct admin_queue *)MALLOC(sizeof(struct admin_queue)+sizeof(admin_message));
375 response->am[0].message = ADMIN_RESPONSE_CMD_BLOCK;
376 response->am[0].u.x.portnum = portnum;
378 /* search for port */
379 interface = interface_first;
382 ifport = interface->ifport;
385 if (ifport->portnum == portnum)
387 ifport = ifport->next;
391 interface = interface->next;
393 /* not found, we return -1 */
396 response->am[0].u.x.block = -1;
397 response->am[0].u.x.error = 1;
398 SPRINT(response->am[0].u.x.message, "Port %d does not exist.", portnum);
403 if (!ifport->mISDNport)
405 /* not loaded anyway */
408 response->am[0].u.x.block = 2;
412 /* try loading interface */
413 ifport->block = block;
416 /* port cannot load */
417 if (ifport->block >= 2)
419 response->am[0].u.x.block = 2;
420 response->am[0].u.x.error = 1;
421 SPRINT(response->am[0].u.x.message, "Port %d will not load.", portnum);
426 response->am[0].u.x.block = ifport->block;
430 /* if we shall unload interface */
433 mISDNport_close(ifport->mISDNport);
434 ifport->mISDNport = 0;
439 /* port new blocking state */
440 ifport->block = response->am[0].u.x.block = block;
443 /* attach to response chain */
444 *responsep = response;
445 responsep = &response->next;
453 int admin_release(struct admin_queue **responsep, char *message)
456 struct admin_queue *response; /* response pointer */
457 class EndpointAppPBX *apppbx;
459 /* create state response */
460 response = (struct admin_queue *)MALLOC(sizeof(struct admin_queue)+sizeof(admin_message));
464 response->am[0].message = ADMIN_RESPONSE_CMD_RELEASE;
467 apppbx = apppbx_first;
470 if (apppbx->ea_endpoint->ep_serial == id)
472 apppbx = apppbx->next;
476 response->am[0].u.x.error = -EINVAL;
477 SPRINT(response->am[0].u.x.message, "Given endpoint %d doesn't exist.", id);
481 apppbx->e_callback = 0;
482 apppbx->release(RELEASE_ALL, LOCATION_PRIVATE_LOCAL, CAUSE_NORMAL, LOCATION_PRIVATE_LOCAL, CAUSE_NORMAL);
485 /* attach to response chain */
486 *responsep = response;
487 responsep = &response->next;
495 int admin_call(struct admin_list *admin, struct admin_message *msg)
497 class Endpoint *epoint;
498 class EndpointAppPBX *apppbx;
500 if (!(epoint = new Endpoint(0, 0)))
501 FATAL("No memory for Endpoint instance\n");
502 if (!(epoint->ep_app = apppbx = new DEFAULT_ENDPOINT_APP(epoint, 1))) // outgoing
503 FATAL("No memory for Endpoint Application instance\n");
504 apppbx->e_adminid = admin->sockserial;
505 admin->epointid = epoint->ep_serial;
506 SCPY(apppbx->e_callerinfo.id, nationalize_callerinfo(msg->u.call.callerid, &apppbx->e_callerinfo.ntype, options.national, options.international));
507 if (msg->u.call.present)
508 apppbx->e_callerinfo.present = INFO_PRESENT_ALLOWED;
510 apppbx->e_callerinfo.present = INFO_PRESENT_RESTRICTED;
511 apppbx->e_callerinfo.screen = INFO_SCREEN_NETWORK;
513 //printf("hh=%d\n", apppbx->e_capainfo.hlc);
515 apppbx->e_capainfo.bearer_capa = msg->u.call.bc_capa;
516 apppbx->e_capainfo.bearer_mode = msg->u.call.bc_mode;
517 apppbx->e_capainfo.bearer_info1 = msg->u.call.bc_info1;
518 apppbx->e_capainfo.hlc = msg->u.call.hlc;
519 apppbx->e_capainfo.exthlc = msg->u.call.exthlc;
520 SCPY(apppbx->e_dialinginfo.id, msg->u.call.dialing);
521 SCPY(apppbx->e_dialinginfo.interfaces, msg->u.call.interface);
522 apppbx->e_dialinginfo.sending_complete = 1;
524 apppbx->new_state(PORT_STATE_OUT_SETUP);
531 * this function is called for response whenever a call state changes.
533 void admin_call_response(int adminid, int message, char *connected, int cause, int location, int notify)
535 struct admin_list *admin;
536 struct admin_queue *response, **responsep; /* response pointer */
538 /* searching for admin id
539 * maybe there is no admin instance, because the calling port was not
540 * initiated by admin_call */
544 if (adminid == admin->sockserial)
551 /* seek to end of response list */
552 response = admin->response;
553 responsep = &admin->response;
556 responsep = &response->next;
557 response = response->next;
560 /* create state response */
561 response = (struct admin_queue *)MALLOC(sizeof(struct admin_queue)+sizeof(admin_message));
565 response->am[0].message = message;
566 // printf("MESSAGE: %d\n", message);
568 SCPY(response->am[0].u.call.callerid, connected);
569 response->am[0].u.call.cause = cause;
570 response->am[0].u.call.location = location;
571 response->am[0].u.call.notify = notify;
573 /* attach to response chain */
574 *responsep = response;
575 responsep = &response->next;
580 * send data to the remote socket join instance
582 int admin_message_to_join(struct admin_msg *msg, char *remote_name, int sock_id)
585 struct admin_list *admin;
588 if (msg->type == MESSAGE_HELLO)
592 PERROR("Remote application repeats hello message.\n");
595 /* look for second application */
599 if (!strcmp(admin->remote_name, msg->param.hello.application))
605 PERROR("Remote application connects twice??? (ignoring)\n");
608 /* set remote socket instance */
609 SCPY(remote_name, msg->param.hello.application);
613 /* check we have no application name */
616 PERROR("Remote application did not send us a hello message.\n");
621 if (msg->type == MESSAGE_NEWREF)
623 /* create new join instance */
624 join = new JoinRemote(0, remote_name, sock_id); // must have no serial, because no endpoint is connected
626 FATAL("No memory for remote join instance\n");
631 * no ref given for *_ack */
632 if (msg->type == MESSAGE_BCHANNEL)
633 if (msg->param.bchannel.type == BCHANNEL_ASSIGN_ACK
634 || msg->param.bchannel.type == BCHANNEL_REMOVE_ACK)
636 /* no ref, but address */
637 message_bchannel_from_join(NULL, msg->param.bchannel.type, msg->param.bchannel.handle);
644 PERROR("Remote application did not send us a valid ref with a message.\n");
648 /* find join instance */
652 if (join->j_serial == msg->ref)
658 PERROR("No join found with serial %d.\n", msg->ref);
662 /* check application */
663 if (join->j_type != JOIN_TYPE_REMOTE)
665 PERROR("Ref %d does not belong to a remote join instance.\n", msg->ref);
668 if (sock_id != ((class JoinRemote *)join)->j_remote_id)
670 PERROR("Ref %d belongs to remote application %s, but not to sending application %s.\n", msg->ref, ((class JoinRemote *)join)->j_remote_name, remote_name);
675 ((class JoinRemote *)join)->message_remote(msg->type, &msg->param);
682 * this function is called for every message to remote socket
684 int admin_message_from_join(int remote_id, unsigned long ref, int message_type, union parameter *param)
686 struct admin_list *admin;
687 struct admin_queue *response, **responsep; /* response pointer */
689 /* searching for admin id
690 * maybe there is no given remote application
695 if (admin->remote_name[0] && admin->sock==remote_id)
699 /* no given remote application connected */
703 /* seek to end of response list */
704 response = admin->response;
705 responsep = &admin->response;
708 responsep = &response->next;
709 response = response->next;
712 /* create state response */
713 response = (struct admin_queue *)MALLOC(sizeof(struct admin_queue)+sizeof(admin_message));
718 response->am[0].u.msg.type = message_type;
719 response->am[0].u.msg.ref = ref;
720 memcpy(&response->am[0].u.msg.param, param, sizeof(union parameter));
722 /* attach to response chain */
723 *responsep = response;
724 responsep = &response->next;
733 int admin_state(struct admin_queue **responsep)
737 class EndpointAppPBX *apppbx;
740 struct interface *interface;
741 struct interface_port *ifport;
742 struct mISDNport *mISDNport;
746 struct admin_queue *response;
747 struct admin_list *admin;
749 /* create state response */
750 response = (struct admin_queue *)MALLOC(sizeof(struct admin_queue)+sizeof(admin_message));
754 response->am[0].message = ADMIN_RESPONSE_STATE;
756 SCPY(response->am[0].u.s.version_string, VERSION_STRING);
758 memcpy(&response->am[0].u.s.tm, now_tm, sizeof(struct tm));
760 SCPY(response->am[0].u.s.logfile, options.log);
761 /* interface count */
763 interface = interface_first;
766 ifport = interface->ifport;
770 ifport = ifport->next;
772 interface = interface->next;
774 response->am[0].u.s.interfaces = i;
775 /* remote connection count */
780 if (admin->remote_name[0])
784 response->am[0].u.s.remotes = i;
793 response->am[0].u.s.joins = i;
795 apppbx = apppbx_first;
800 apppbx = apppbx->next;
802 response->am[0].u.s.epoints = i;
811 response->am[0].u.s.ports = i;
812 /* attach to response chain */
813 *responsep = response;
814 responsep = &response->next;
816 /* create response for all interfaces */
817 num = (response->am[0].u.s.interfaces)+(response->am[0].u.s.joins)+(response->am[0].u.s.epoints)+(response->am[0].u.s.ports);
820 response = (struct admin_queue *)MALLOC(sizeof(admin_queue)+(num*sizeof(admin_message)));
823 *responsep = response;
824 responsep = &response->next;
825 interface = interface_first;
829 ifport = interface->ifport;
833 response->am[num].message = ADMIN_RESPONSE_S_INTERFACE;
835 SCPY(response->am[num].u.i.interface_name, interface->name);
837 response->am[num].u.i.portnum = ifport->portnum;
839 response->am[num].u.i.extension = interface->extension;
841 response->am[num].u.i.block = ifport->block;
842 if (ifport->mISDNport)
844 mISDNport = ifport->mISDNport;
847 response->am[num].u.i.ptp = mISDNport->ptp;
849 response->am[num].u.i.ntmode = mISDNport->ntmode;
851 response->am[num].u.i.pri = mISDNport->pri;
853 response->am[num].u.i.use = mISDNport->use;
855 response->am[num].u.i.l1link = mISDNport->l1link;
857 response->am[num].u.i.l2link = mISDNport->l2link;
859 response->am[num].u.i.los = mISDNport->los;
861 response->am[num].u.i.ais = mISDNport->ais;
863 response->am[num].u.i.rdi = mISDNport->rdi;
865 response->am[num].u.i.slip_tx = mISDNport->slip_tx;
866 response->am[num].u.i.slip_rx = mISDNport->slip_rx;
868 response->am[num].u.i.channels = mISDNport->b_num;
872 while(i < mISDNport->b_num)
874 response->am[num].u.i.busy[i] = mISDNport->b_state[i];
875 if (mISDNport->b_port[i])
876 response->am[num].u.i.port[i] = mISDNport->b_port[i]->p_serial;
882 ifport = ifport->next;
884 interface = interface->next;
887 /* create response for all remotes */
891 if (admin->remote_name[0])
894 response->am[num].message = ADMIN_RESPONSE_S_REMOTE;
896 SCPY(response->am[num].u.r.name, admin->remote_name);
903 /* create response for all joins */
908 response->am[num].message = ADMIN_RESPONSE_S_JOIN;
910 response->am[num].u.j.serial = join->j_serial;
912 if (join->j_type == JOIN_TYPE_PBX)
913 response->am[num].u.j.partyline = ((class JoinPBX *)join)->j_partyline;
914 /* remote application */
915 if (join->j_type == JOIN_TYPE_REMOTE)
916 SCPY(response->am[num].u.j.remote, ((class JoinRemote *)join)->j_remote_name);
922 /* create response for all endpoint */
923 apppbx = apppbx_first;
927 response->am[num].message = ADMIN_RESPONSE_S_EPOINT;
929 response->am[num].u.e.serial = apppbx->ea_endpoint->ep_serial;
931 response->am[num].u.e.join = apppbx->ea_endpoint->ep_join_id;
932 /* rx notification */
933 response->am[num].u.e.rx_state = apppbx->e_rx_state;
934 /* tx notification */
935 response->am[num].u.e.tx_state = apppbx->e_tx_state;
937 switch(apppbx->e_state)
939 case EPOINT_STATE_IN_SETUP:
940 response->am[num].u.e.state = ADMIN_STATE_IN_SETUP;
942 case EPOINT_STATE_OUT_SETUP:
943 response->am[num].u.e.state = ADMIN_STATE_OUT_SETUP;
945 case EPOINT_STATE_IN_OVERLAP:
946 response->am[num].u.e.state = ADMIN_STATE_IN_OVERLAP;
948 case EPOINT_STATE_OUT_OVERLAP:
949 response->am[num].u.e.state = ADMIN_STATE_OUT_OVERLAP;
951 case EPOINT_STATE_IN_PROCEEDING:
952 response->am[num].u.e.state = ADMIN_STATE_IN_PROCEEDING;
954 case EPOINT_STATE_OUT_PROCEEDING:
955 response->am[num].u.e.state = ADMIN_STATE_OUT_PROCEEDING;
957 case EPOINT_STATE_IN_ALERTING:
958 response->am[num].u.e.state = ADMIN_STATE_IN_ALERTING;
960 case EPOINT_STATE_OUT_ALERTING:
961 response->am[num].u.e.state = ADMIN_STATE_OUT_ALERTING;
963 case EPOINT_STATE_CONNECT:
964 response->am[num].u.e.state = ADMIN_STATE_CONNECT;
966 case EPOINT_STATE_IN_DISCONNECT:
967 response->am[num].u.e.state = ADMIN_STATE_IN_DISCONNECT;
969 case EPOINT_STATE_OUT_DISCONNECT:
970 response->am[num].u.e.state = ADMIN_STATE_OUT_DISCONNECT;
973 response->am[num].u.e.state = ADMIN_STATE_IDLE;
976 SCPY(response->am[num].u.e.terminal, apppbx->e_ext.number);
978 SCPY(response->am[num].u.e.callerid, apppbx->e_callerinfo.id);
980 SCPY(response->am[num].u.e.dialing, apppbx->e_dialinginfo.id);
982 if (apppbx->e_action)
983 SCPY(response->am[num].u.e.action, action_defs[apppbx->e_action->index].name);
984 // if (apppbx->e_action)
985 // printf("action=%s\n",action_defs[apppbx->e_action->index].name);
987 response->am[num].u.e.park = apppbx->ea_endpoint->ep_park;
988 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))
989 memcpy(response->am[num].u.e.park_callid, apppbx->ea_endpoint->ep_park_callid, apppbx->ea_endpoint->ep_park_len);
990 response->am[num].u.e.park_len = apppbx->ea_endpoint->ep_park_len;
992 if (apppbx->e_crypt == CRYPT_ON)
993 response->am[num].u.e.crypt = 1;
995 apppbx = apppbx->next;
999 /* create response for all ports */
1004 response->am[num].message = ADMIN_RESPONSE_S_PORT;
1006 response->am[num].u.p.serial = port->p_serial;
1008 SCPY(response->am[num].u.p.name, port->p_name);
1010 response->am[num].u.p.epoint = ACTIVE_EPOINT(port->p_epointlist);
1012 switch(port->p_state)
1014 case PORT_STATE_IN_SETUP:
1015 response->am[num].u.p.state = ADMIN_STATE_IN_SETUP;
1017 case PORT_STATE_OUT_SETUP:
1018 response->am[num].u.p.state = ADMIN_STATE_OUT_SETUP;
1020 case PORT_STATE_IN_OVERLAP:
1021 response->am[num].u.p.state = ADMIN_STATE_IN_OVERLAP;
1023 case PORT_STATE_OUT_OVERLAP:
1024 response->am[num].u.p.state = ADMIN_STATE_OUT_OVERLAP;
1026 case PORT_STATE_IN_PROCEEDING:
1027 response->am[num].u.p.state = ADMIN_STATE_IN_PROCEEDING;
1029 case PORT_STATE_OUT_PROCEEDING:
1030 response->am[num].u.p.state = ADMIN_STATE_OUT_PROCEEDING;
1032 case PORT_STATE_IN_ALERTING:
1033 response->am[num].u.p.state = ADMIN_STATE_IN_ALERTING;
1035 case PORT_STATE_OUT_ALERTING:
1036 response->am[num].u.p.state = ADMIN_STATE_OUT_ALERTING;
1038 case PORT_STATE_CONNECT:
1039 response->am[num].u.p.state = ADMIN_STATE_CONNECT;
1041 case PORT_STATE_IN_DISCONNECT:
1042 response->am[num].u.p.state = ADMIN_STATE_IN_DISCONNECT;
1044 case PORT_STATE_OUT_DISCONNECT:
1045 response->am[num].u.p.state = ADMIN_STATE_OUT_DISCONNECT;
1048 response->am[num].u.p.state = ADMIN_STATE_IDLE;
1051 if ((port->p_type&PORT_CLASS_mISDN_MASK) == PORT_CLASS_mISDN_DSS1)
1053 response->am[num].u.p.isdn = 1;
1054 pdss1 = (class Pdss1 *)port;
1055 response->am[num].u.p.isdn_chan = pdss1->p_m_b_channel;
1056 response->am[num].u.p.isdn_hold = pdss1->p_m_hold;
1057 response->am[num].u.p.isdn_ces = pdss1->p_m_d_ces;
1066 int sockserial = 1; // must start with 1, because 0 is used if no serial is set
1068 * handle admin socket (non blocking)
1070 int admin_handle(void)
1072 struct admin_list *admin, **adminp;
1074 struct admin_message msg;
1077 socklen_t sock_len = sizeof(sock_address);
1078 unsigned long on = 1;
1079 int work = 0; /* if work was done */
1080 struct Endpoint *epoint;
1085 /* check for new incoming connections */
1086 if ((new_sock = accept(sock, (struct sockaddr *)&sock_address, &sock_len)) >= 0)
1089 /* insert new socket */
1090 admin = (struct admin_list *)MALLOC(sizeof(struct admin_list));
1091 if (ioctl(new_sock, FIONBIO, (unsigned char *)(&on)) >= 0)
1094 // PERROR("DEBUG incoming socket %d, serial=%d\n", new_sock, sockserial);
1097 admin->sockserial = sockserial++;
1098 admin->next = admin_first;
1099 admin_first = admin;
1100 admin->sock = new_sock;
1103 FREE(admin, sizeof(struct admin_list));
1107 if (errno != EWOULDBLOCK)
1109 PERROR("Failed to accept connection from socket \"%s\". (errno=%d) Closing socket.\n", sock_address.sun_path, errno);
1115 /* loop all current socket connections */
1116 admin = admin_first;
1117 adminp = &admin_first;
1121 len = read(admin->sock, &msg, sizeof(msg));
1124 if (errno != EWOULDBLOCK)
1128 printf("Broken pipe on socket %d. (errno=%d).\n", admin->sock, errno);
1129 PDEBUG(DEBUG_LOG, "Broken pipe on socket %d. (errno=%d).\n", admin->sock, errno);
1130 *adminp = admin->next;
1131 free_connection(admin);
1139 //PERROR("DEBUG socket %d got data. serial=%d\n", admin->sock, admin->sockserial);
1144 /*release endpoint if exists */
1145 if (admin->epointid)
1147 epoint = find_epoint_id(admin->epointid);
1150 ((class DEFAULT_ENDPOINT_APP *)epoint->ep_app)->
1151 release(RELEASE_ALL, LOCATION_PRIVATE_LOCAL, CAUSE_NORMAL, LOCATION_PRIVATE_LOCAL, CAUSE_NORMAL);
1156 //PERROR("DEBUG socket %d closed by remote.\n", admin->sock);
1157 *adminp = admin->next;
1158 free_connection(admin);
1160 //PERROR("DEBUG (admin_first=%x)\n", admin_first);
1163 if (len != sizeof(msg))
1165 PERROR("Short/long read on socket %d. (len=%d != size=%d).\n", admin->sock, len, sizeof(msg));
1166 *adminp = admin->next;
1167 free_connection(admin);
1171 /* process socket command */
1172 if (admin->response)
1174 PERROR("Data from socket %d while sending response.\n", admin->sock);
1175 *adminp = admin->next;
1176 free_connection(admin);
1180 switch (msg.message)
1182 case ADMIN_REQUEST_CMD_INTERFACE:
1183 if (admin_interface(&admin->response) < 0)
1185 PERROR("Failed to create dial response for socket %d.\n", admin->sock);
1186 goto response_error;
1190 case ADMIN_REQUEST_CMD_ROUTE:
1191 if (admin_route(&admin->response) < 0)
1193 PERROR("Failed to create dial response for socket %d.\n", admin->sock);
1194 goto response_error;
1198 case ADMIN_REQUEST_CMD_DIAL:
1199 if (admin_dial(&admin->response, msg.u.x.message) < 0)
1201 PERROR("Failed to create dial response for socket %d.\n", admin->sock);
1202 goto response_error;
1206 case ADMIN_REQUEST_CMD_RELEASE:
1207 if (admin_release(&admin->response, msg.u.x.message) < 0)
1209 PERROR("Failed to create release response for socket %d.\n", admin->sock);
1210 goto response_error;
1214 case ADMIN_REQUEST_STATE:
1215 if (admin_state(&admin->response) < 0)
1217 PERROR("Failed to create state response for socket %d.\n", admin->sock);
1218 goto response_error;
1222 case ADMIN_TRACE_REQUEST:
1223 if (admin_trace(admin, &msg.u.trace_req) < 0)
1225 PERROR("Failed to create trace response for socket %d.\n", admin->sock);
1226 goto response_error;
1230 case ADMIN_REQUEST_CMD_BLOCK:
1231 if (admin_block(&admin->response, msg.u.x.portnum, msg.u.x.block) < 0)
1233 PERROR("Failed to create block response for socket %d.\n", admin->sock);
1234 goto response_error;
1239 if (admin_message_to_join(&msg.u.msg, admin->remote_name, admin->sock) < 0)
1241 PERROR("Failed to deliver message for socket %d.\n", admin->sock);
1242 goto response_error;
1247 struct admin_queue *response;
1249 response = admin->response;
1252 printf("%c", '0'+response->am[0].message);
1253 response=response->next;
1260 case ADMIN_CALL_SETUP:
1261 if (admin_call(admin, &msg) < 0)
1263 PERROR("Failed to create call for socket %d.\n", admin->sock);
1265 *adminp = admin->next;
1266 free_connection(admin);
1273 PERROR("Invalid message %d from socket %d.\n", msg.message, admin->sock);
1274 *adminp = admin->next;
1275 free_connection(admin);
1281 if (admin->response)
1284 //PERROR("DEBUG socket %d sending data.\n", admin->sock);
1285 len = write(admin->sock, ((unsigned char *)(admin->response->am))+admin->response->offset, sizeof(struct admin_message)*(admin->response->num)-admin->response->offset);
1288 if (errno != EWOULDBLOCK)
1298 if (len < (int)(sizeof(struct admin_message)*(admin->response->num)-admin->response->offset))
1300 admin->response->offset+=len;
1304 temp = admin->response;
1305 admin->response = admin->response->next;
1310 /* done with socket instance */
1312 adminp = &admin->next;
1313 admin = admin->next;