1 /*****************************************************************************\
3 ** Linux Call Router **
5 **---------------------------------------------------------------------------**
6 ** Copyright: Andreas Eversberg **
8 ** Socket link server **
10 \*****************************************************************************/
15 #include <sys/types.h>
16 //#include <sys/stat.h>
20 #include <sys/ioctl.h>
21 //#include <sys/file.h>
23 //#include <sys/mman.h>
24 //#include <sys/resource.h>
25 #include <sys/socket.h>
31 char *socket_name = SOCKET_NAME;
33 struct sockaddr_un sock_address;
35 struct admin_list *admin_first = NULL;
38 * initialize admin socket
44 /* open and bind socket */
45 if ((sock = socket(PF_UNIX, SOCK_STREAM, 0)) < 0)
47 PERROR("Failed to create admin socket. (errno=%d)\n", errno);
51 memset(&sock_address, 0, sizeof(sock_address));
52 sock_address.sun_family = AF_UNIX;
53 UCPY(sock_address.sun_path, socket_name);
55 if (bind(sock, (struct sockaddr *)(&sock_address), SUN_LEN(&sock_address)) < 0)
61 PERROR("Failed to bind admin socket to \"%s\". (errno=%d)\n", sock_address.sun_path, errno);
64 if (listen(sock, 5) < 0)
70 PERROR("Failed to listen to socket \"%s\". (errno=%d)\n", sock_address.sun_path, errno);
73 if (ioctl(sock, FIONBIO, (unsigned char *)(&on)) < 0)
79 PERROR("Failed to set socket \"%s\" into non-blocking mode. (errno=%d)\n", sock_address.sun_path, errno);
88 * also releases all remote joins
90 void free_connection(struct admin_list *admin)
92 struct admin_queue *response;
94 union parameter param;
95 class Join *join, *joinnext;
97 /* free remote joins */
98 if (admin->remote_name[0])
103 joinnext = join->next;
104 if (join->j_type==JOIN_TYPE_REMOTE) if (((class JoinRemote *)join)->j_remote_id == admin->sock)
106 memset(¶m, 0, sizeof(param));
107 param.disconnectinfo.cause = CAUSE_OUTOFORDER;
108 param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
109 ((class JoinRemote *)join)->message_remote(MESSAGE_RELEASE, ¶m);
110 /* join is now destroyed, so we go to next join */
116 if (admin->sock >= 0)
121 // printf("new\n", response);
122 response = admin->response;
126 // printf("%x\n", response);
127 temp = response->next;
130 response = (struct admin_queue *)temp;
132 // printf("new2\n", response);
134 // printf("new3\n", response);
140 * cleanup admin socket
142 void admin_cleanup(void)
144 struct admin_list *admin, *next;;
151 free_connection(admin);
164 * do interface reload
166 int admin_interface(struct admin_queue **responsep)
168 struct admin_queue *response; /* response pointer */
172 if (read_interfaces())
175 free_interfaces(interface_first);
176 interface_first = interface_newlist;
177 interface_newlist = NULL;
180 err_txt = interface_error;
183 /* create state response */
184 response = (struct admin_queue *)MALLOC(sizeof(struct admin_queue)+sizeof(admin_message));
188 response->am[0].message = ADMIN_RESPONSE_CMD_INTERFACE;
190 response->am[0].u.x.error = err;
192 SCPY(response->am[0].u.x.message, err_txt);
193 /* attach to response chain */
194 *responsep = response;
195 responsep = &response->next;
204 int admin_route(struct admin_queue **responsep)
206 struct route_ruleset *ruleset_new;
207 struct admin_queue *response; /* response pointer */
208 char err_txt[256] = "";
213 class EndpointAppPBX *apppbx;
217 apppbx = apppbx_first;
221 apppbx = apppbx->next;
225 SPRINT(err_txt, "Cannot reload routing, because %d endpoints active\n", n);
230 if (!(ruleset_new = ruleset_parse()))
232 SPRINT(err_txt, ruleset_error);
236 ruleset_free(ruleset_first);
237 ruleset_first = ruleset_new;
238 ruleset_main = getrulesetbyname("main");
241 SPRINT(err_txt, "Ruleset reloaded, but rule 'main' not found.\n");
244 apppbx = apppbx_first;
247 if (apppbx->e_action)
249 switch(apppbx->e_action->index)
251 case ACTION_INTERNAL:
252 apppbx->e_action = &action_internal;
254 case ACTION_EXTERNAL:
255 apppbx->e_action = &action_external;
258 apppbx->e_action = &action_remote;
260 case ACTION_VBOX_RECORD:
261 apppbx->e_action = &action_vbox;
263 case ACTION_PARTYLINE:
264 apppbx->e_action = &action_partyline;
269 } else if (apppbx->e_state != EPOINT_STATE_CONNECT)
272 apppbx->e_callback = 0;
273 apppbx->e_action = NULL;
274 apppbx->release(RELEASE_ALL, LOCATION_PRIVATE_LOCAL, CAUSE_NORMAL, LOCATION_PRIVATE_LOCAL, CAUSE_NORMAL);
277 numberrize_callerinfo(apppbx->e_callerinfo.id, apppbx->e_callerinfo.ntype),
278 apppbx->e_dialinginfo.id,
281 apppbx->ea_endpoint->ep_serial,
282 "KICK (reload routing)");
286 apppbx->e_action_timeout = 0;
287 apppbx->e_rule = NULL;
288 apppbx->e_ruleset = NULL;
290 apppbx = apppbx->next;
294 /* create state response */
295 response = (struct admin_queue *)MALLOC(sizeof(struct admin_queue)+sizeof(admin_message));
299 response->am[0].message = ADMIN_RESPONSE_CMD_ROUTE;
301 response->am[0].u.x.error = err;
303 SCPY(response->am[0].u.x.message, err_txt);
304 /* attach to response chain */
305 *responsep = response;
306 responsep = &response->next;
315 int admin_dial(struct admin_queue **responsep, char *message)
317 struct extension ext; /* temporary extension's settings */
318 struct admin_queue *response; /* response pointer */
319 char *p; /* pointer to dialing digits */
321 /* create state response */
322 response = (struct admin_queue *)MALLOC(sizeof(struct admin_queue)+sizeof(admin_message));
326 response->am[0].message = ADMIN_RESPONSE_CMD_DIAL;
328 /* process request */
329 if (!(p = strchr(message,':')))
331 response->am[0].u.x.error = -EINVAL;
332 SPRINT(response->am[0].u.x.message, "no seperator ':' in message to seperate number from extension");
337 /* modify extension */
338 if (!read_extension(&ext, message))
340 response->am[0].u.x.error = -EINVAL;
341 SPRINT(response->am[0].u.x.message, "extension doesn't exist");
345 write_extension(&ext, message);
348 /* attach to response chain */
349 *responsep = response;
350 responsep = &response->next;
358 int admin_trace(struct admin_list *admin, struct admin_trace_req *trace)
360 memcpy(&admin->trace, trace, sizeof(struct admin_trace_req));
368 * 0 = make port available
369 * 1 = make port administratively blocked
371 * the result is returned:
372 * 0 = port is now available
373 * 1 = port is now blocked
374 * 2 = port cannot be loaded or has been unloaded
375 * -1 = port doesn't exist
377 int admin_block(struct admin_queue **responsep, int portnum, int block)
379 struct admin_queue *response; /* response pointer */
380 struct interface *interface;
381 struct interface_port *ifport;
383 /* create block response */
384 response = (struct admin_queue *)MALLOC(sizeof(struct admin_queue)+sizeof(admin_message));
388 response->am[0].message = ADMIN_RESPONSE_CMD_BLOCK;
389 response->am[0].u.x.portnum = portnum;
391 /* search for port */
392 interface = interface_first;
395 ifport = interface->ifport;
398 if (ifport->portnum == portnum)
400 ifport = ifport->next;
404 interface = interface->next;
406 /* not found, we return -1 */
409 response->am[0].u.x.block = -1;
410 response->am[0].u.x.error = 1;
411 SPRINT(response->am[0].u.x.message, "Port %d does not exist.", portnum);
416 if (!ifport->mISDNport)
418 /* not loaded anyway */
421 response->am[0].u.x.block = 2;
425 /* try loading interface */
426 ifport->block = block;
429 /* port cannot load */
430 if (ifport->block >= 2)
432 response->am[0].u.x.block = 2;
433 response->am[0].u.x.error = 1;
434 SPRINT(response->am[0].u.x.message, "Port %d will not load.", portnum);
439 response->am[0].u.x.block = ifport->block;
443 /* if we shall unload interface */
446 mISDNport_close(ifport->mISDNport);
447 ifport->mISDNport = 0;
452 /* port new blocking state */
453 ifport->block = response->am[0].u.x.block = block;
456 /* attach to response chain */
457 *responsep = response;
458 responsep = &response->next;
466 int admin_release(struct admin_queue **responsep, char *message)
469 struct admin_queue *response; /* response pointer */
470 class EndpointAppPBX *apppbx;
472 /* create state response */
473 response = (struct admin_queue *)MALLOC(sizeof(struct admin_queue)+sizeof(admin_message));
477 response->am[0].message = ADMIN_RESPONSE_CMD_RELEASE;
480 apppbx = apppbx_first;
483 if (apppbx->ea_endpoint->ep_serial == id)
485 apppbx = apppbx->next;
489 response->am[0].u.x.error = -EINVAL;
490 SPRINT(response->am[0].u.x.message, "Given endpoint %d doesn't exist.", id);
494 apppbx->e_callback = 0;
495 apppbx->release(RELEASE_ALL, LOCATION_PRIVATE_LOCAL, CAUSE_NORMAL, LOCATION_PRIVATE_LOCAL, CAUSE_NORMAL);
498 /* attach to response chain */
499 *responsep = response;
500 responsep = &response->next;
508 int admin_call(struct admin_list *admin, struct admin_message *msg)
510 class Endpoint *epoint;
511 class EndpointAppPBX *apppbx;
513 if (!(epoint = new Endpoint(0, 0)))
514 FATAL("No memory for Endpoint instance\n");
515 if (!(epoint->ep_app = apppbx = new DEFAULT_ENDPOINT_APP(epoint, 1))) // outgoing
516 FATAL("No memory for Endpoint Application instance\n");
517 apppbx->e_adminid = admin->sockserial;
518 admin->epointid = epoint->ep_serial;
519 SCPY(apppbx->e_callerinfo.id, nationalize_callerinfo(msg->u.call.callerid, &apppbx->e_callerinfo.ntype));
520 if (msg->u.call.present)
521 apppbx->e_callerinfo.present = INFO_PRESENT_ALLOWED;
523 apppbx->e_callerinfo.present = INFO_PRESENT_RESTRICTED;
524 apppbx->e_callerinfo.screen = INFO_SCREEN_NETWORK;
526 //printf("hh=%d\n", apppbx->e_capainfo.hlc);
528 apppbx->e_capainfo.bearer_capa = msg->u.call.bc_capa;
529 apppbx->e_capainfo.bearer_mode = msg->u.call.bc_mode;
530 apppbx->e_capainfo.bearer_info1 = msg->u.call.bc_info1;
531 apppbx->e_capainfo.hlc = msg->u.call.hlc;
532 apppbx->e_capainfo.exthlc = msg->u.call.exthlc;
533 SCPY(apppbx->e_dialinginfo.id, msg->u.call.dialing);
534 SCPY(apppbx->e_dialinginfo.interfaces, msg->u.call.interface);
535 apppbx->e_dialinginfo.sending_complete = 1;
537 apppbx->new_state(PORT_STATE_OUT_SETUP);
544 * this function is called for response whenever a call state changes.
546 void admin_call_response(int adminid, int message, char *connected, int cause, int location, int notify)
548 struct admin_list *admin;
549 struct admin_queue *response, **responsep; /* response pointer */
551 /* searching for admin id
552 * maybe there is no admin instance, because the calling port was not
553 * initiated by admin_call */
557 if (adminid == admin->sockserial)
564 /* seek to end of response list */
565 response = admin->response;
566 responsep = &admin->response;
569 responsep = &response->next;
570 response = response->next;
573 /* create state response */
574 response = (struct admin_queue *)MALLOC(sizeof(struct admin_queue)+sizeof(admin_message));
578 response->am[0].message = message;
579 // printf("MESSAGE: %d\n", message);
581 SCPY(response->am[0].u.call.callerid, connected);
582 response->am[0].u.call.cause = cause;
583 response->am[0].u.call.location = location;
584 response->am[0].u.call.notify = notify;
586 /* attach to response chain */
587 *responsep = response;
588 responsep = &response->next;
593 * send data to the remote socket join instance
595 int admin_message_to_join(struct admin_msg *msg, char *remote_name, int sock_id)
598 struct admin_list *admin;
601 if (msg->type == MESSAGE_HELLO)
605 PERROR("Remote application repeats hello message.\n");
608 /* look for second application */
612 if (!strcmp(admin->remote_name, msg->param.hello.application))
618 PERROR("Remote application connects twice??? (ignoring)\n");
621 /* set remote socket instance */
622 SCPY(remote_name, msg->param.hello.application);
626 /* check we have no application name */
629 PERROR("Remote application did not send us a hello message.\n");
634 if (msg->type == MESSAGE_NEWREF)
636 /* create new join instance */
637 join = new JoinRemote(0, remote_name, sock_id); // must have no serial, because no endpoint is connected
639 FATAL("No memory for remote join instance\n");
644 * no ref given for *_ack */
645 if (msg->type == MESSAGE_BCHANNEL)
646 if (msg->param.bchannel.type == BCHANNEL_ASSIGN_ACK
647 || msg->param.bchannel.type == BCHANNEL_REMOVE_ACK)
649 /* no ref, but address */
650 message_bchannel_from_join(NULL, msg->param.bchannel.type, msg->param.bchannel.handle);
657 PERROR("Remote application did not send us a valid ref with a message.\n");
661 /* find join instance */
665 if (join->j_serial == msg->ref)
671 PERROR("No join found with serial %d.\n", msg->ref);
675 /* check application */
676 if (join->j_type != JOIN_TYPE_REMOTE)
678 PERROR("Ref %d does not belong to a remote join instance.\n", msg->ref);
681 if (sock_id != ((class JoinRemote *)join)->j_remote_id)
683 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);
688 ((class JoinRemote *)join)->message_remote(msg->type, &msg->param);
695 * this function is called for every message to remote socket
697 int admin_message_from_join(int remote_id, unsigned long ref, int message_type, union parameter *param)
699 struct admin_list *admin;
700 struct admin_queue *response, **responsep; /* response pointer */
702 /* searching for admin id
703 * maybe there is no given remote application
708 if (admin->remote_name[0] && admin->sock==remote_id)
712 /* no given remote application connected */
716 /* seek to end of response list */
717 response = admin->response;
718 responsep = &admin->response;
721 responsep = &response->next;
722 response = response->next;
725 /* create state response */
726 response = (struct admin_queue *)MALLOC(sizeof(struct admin_queue)+sizeof(admin_message));
731 response->am[0].u.msg.type = message_type;
732 response->am[0].u.msg.ref = ref;
733 memcpy(&response->am[0].u.msg.param, param, sizeof(union parameter));
735 /* attach to response chain */
736 *responsep = response;
737 responsep = &response->next;
746 int admin_state(struct admin_queue **responsep)
750 class EndpointAppPBX *apppbx;
753 struct interface *interface;
754 struct interface_port *ifport;
755 struct mISDNport *mISDNport;
759 struct admin_queue *response;
760 struct admin_list *admin;
762 /* create state response */
763 response = (struct admin_queue *)MALLOC(sizeof(struct admin_queue)+sizeof(admin_message));
767 response->am[0].message = ADMIN_RESPONSE_STATE;
769 SCPY(response->am[0].u.s.version_string, VERSION_STRING);
771 memcpy(&response->am[0].u.s.tm, now_tm, sizeof(struct tm));
773 SCPY(response->am[0].u.s.logfile, options.log);
774 /* interface count */
776 interface = interface_first;
779 ifport = interface->ifport;
783 ifport = ifport->next;
785 interface = interface->next;
787 response->am[0].u.s.interfaces = i;
788 /* remote connection count */
793 if (admin->remote_name[0])
797 response->am[0].u.s.remotes = i;
806 response->am[0].u.s.joins = i;
808 apppbx = apppbx_first;
813 apppbx = apppbx->next;
815 response->am[0].u.s.epoints = i;
824 response->am[0].u.s.ports = i;
825 /* attach to response chain */
826 *responsep = response;
827 responsep = &response->next;
829 /* create response for all interfaces */
830 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);
833 response = (struct admin_queue *)MALLOC(sizeof(admin_queue)+(num*sizeof(admin_message)));
836 *responsep = response;
837 responsep = &response->next;
838 interface = interface_first;
842 ifport = interface->ifport;
846 response->am[num].message = ADMIN_RESPONSE_S_INTERFACE;
848 SCPY(response->am[num].u.i.interface_name, interface->name);
850 response->am[num].u.i.portnum = ifport->portnum;
852 response->am[num].u.i.extension = interface->extension;
854 response->am[num].u.i.block = ifport->block;
855 if (ifport->mISDNport)
857 mISDNport = ifport->mISDNport;
860 response->am[num].u.i.ptp = mISDNport->ptp;
862 response->am[num].u.i.ntmode = mISDNport->ntmode;
864 response->am[num].u.i.pri = mISDNport->pri;
866 response->am[num].u.i.use = mISDNport->use;
868 response->am[num].u.i.l1link = mISDNport->l1link;
870 response->am[num].u.i.l2link = mISDNport->l2link;
872 response->am[num].u.i.channels = mISDNport->b_num;
876 while(i < mISDNport->b_num)
878 response->am[num].u.i.busy[i] = mISDNport->b_state[i];
879 if (mISDNport->b_port[i])
880 response->am[num].u.i.port[i] = mISDNport->b_port[i]->p_serial;
886 ifport = ifport->next;
888 interface = interface->next;
891 /* create response for all remotes */
895 if (admin->remote_name[0])
898 response->am[num].message = ADMIN_RESPONSE_S_REMOTE;
900 SCPY(response->am[num].u.r.name, admin->remote_name);
907 /* create response for all joins */
912 response->am[num].message = ADMIN_RESPONSE_S_JOIN;
914 response->am[num].u.j.serial = join->j_serial;
916 if (join->j_type == JOIN_TYPE_PBX)
917 response->am[num].u.j.partyline = ((class JoinPBX *)join)->j_partyline;
918 /* remote application */
919 if (join->j_type == JOIN_TYPE_REMOTE)
920 SCPY(response->am[num].u.j.remote, ((class JoinRemote *)join)->j_remote_name);
926 /* create response for all endpoint */
927 apppbx = apppbx_first;
931 response->am[num].message = ADMIN_RESPONSE_S_EPOINT;
933 response->am[num].u.e.serial = apppbx->ea_endpoint->ep_serial;
935 response->am[num].u.e.join = apppbx->ea_endpoint->ep_join_id;
936 /* rx notification */
937 response->am[num].u.e.rx_state = apppbx->e_rx_state;
938 /* tx notification */
939 response->am[num].u.e.tx_state = apppbx->e_tx_state;
941 switch(apppbx->e_state)
943 case EPOINT_STATE_IN_SETUP:
944 response->am[num].u.e.state = ADMIN_STATE_IN_SETUP;
946 case EPOINT_STATE_OUT_SETUP:
947 response->am[num].u.e.state = ADMIN_STATE_OUT_SETUP;
949 case EPOINT_STATE_IN_OVERLAP:
950 response->am[num].u.e.state = ADMIN_STATE_IN_OVERLAP;
952 case EPOINT_STATE_OUT_OVERLAP:
953 response->am[num].u.e.state = ADMIN_STATE_OUT_OVERLAP;
955 case EPOINT_STATE_IN_PROCEEDING:
956 response->am[num].u.e.state = ADMIN_STATE_IN_PROCEEDING;
958 case EPOINT_STATE_OUT_PROCEEDING:
959 response->am[num].u.e.state = ADMIN_STATE_OUT_PROCEEDING;
961 case EPOINT_STATE_IN_ALERTING:
962 response->am[num].u.e.state = ADMIN_STATE_IN_ALERTING;
964 case EPOINT_STATE_OUT_ALERTING:
965 response->am[num].u.e.state = ADMIN_STATE_OUT_ALERTING;
967 case EPOINT_STATE_CONNECT:
968 response->am[num].u.e.state = ADMIN_STATE_CONNECT;
970 case EPOINT_STATE_IN_DISCONNECT:
971 response->am[num].u.e.state = ADMIN_STATE_IN_DISCONNECT;
973 case EPOINT_STATE_OUT_DISCONNECT:
974 response->am[num].u.e.state = ADMIN_STATE_OUT_DISCONNECT;
977 response->am[num].u.e.state = ADMIN_STATE_IDLE;
980 SCPY(response->am[num].u.e.terminal, apppbx->e_ext.number);
982 SCPY(response->am[num].u.e.callerid, apppbx->e_callerinfo.id);
984 SCPY(response->am[num].u.e.dialing, apppbx->e_dialinginfo.id);
986 if (apppbx->e_action)
987 SCPY(response->am[num].u.e.action, action_defs[apppbx->e_action->index].name);
988 // if (apppbx->e_action)
989 // printf("action=%s\n",action_defs[apppbx->e_action->index].name);
991 response->am[num].u.e.park = apppbx->ea_endpoint->ep_park;
992 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))
993 memcpy(response->am[num].u.e.park_callid, apppbx->ea_endpoint->ep_park_callid, apppbx->ea_endpoint->ep_park_len);
994 response->am[num].u.e.park_len = apppbx->ea_endpoint->ep_park_len;
996 if (apppbx->e_crypt == CRYPT_ON)
997 response->am[num].u.e.crypt = 1;
999 apppbx = apppbx->next;
1003 /* create response for all ports */
1008 response->am[num].message = ADMIN_RESPONSE_S_PORT;
1010 response->am[num].u.p.serial = port->p_serial;
1012 SCPY(response->am[num].u.p.name, port->p_name);
1014 response->am[num].u.p.epoint = ACTIVE_EPOINT(port->p_epointlist);
1016 switch(port->p_state)
1018 case PORT_STATE_IN_SETUP:
1019 response->am[num].u.p.state = ADMIN_STATE_IN_SETUP;
1021 case PORT_STATE_OUT_SETUP:
1022 response->am[num].u.p.state = ADMIN_STATE_OUT_SETUP;
1024 case PORT_STATE_IN_OVERLAP:
1025 response->am[num].u.p.state = ADMIN_STATE_IN_OVERLAP;
1027 case PORT_STATE_OUT_OVERLAP:
1028 response->am[num].u.p.state = ADMIN_STATE_OUT_OVERLAP;
1030 case PORT_STATE_IN_PROCEEDING:
1031 response->am[num].u.p.state = ADMIN_STATE_IN_PROCEEDING;
1033 case PORT_STATE_OUT_PROCEEDING:
1034 response->am[num].u.p.state = ADMIN_STATE_OUT_PROCEEDING;
1036 case PORT_STATE_IN_ALERTING:
1037 response->am[num].u.p.state = ADMIN_STATE_IN_ALERTING;
1039 case PORT_STATE_OUT_ALERTING:
1040 response->am[num].u.p.state = ADMIN_STATE_OUT_ALERTING;
1042 case PORT_STATE_CONNECT:
1043 response->am[num].u.p.state = ADMIN_STATE_CONNECT;
1045 case PORT_STATE_IN_DISCONNECT:
1046 response->am[num].u.p.state = ADMIN_STATE_IN_DISCONNECT;
1048 case PORT_STATE_OUT_DISCONNECT:
1049 response->am[num].u.p.state = ADMIN_STATE_OUT_DISCONNECT;
1052 response->am[num].u.p.state = ADMIN_STATE_IDLE;
1055 if ((port->p_type&PORT_CLASS_mISDN_MASK) == PORT_CLASS_mISDN_DSS1)
1057 response->am[num].u.p.isdn = 1;
1058 pdss1 = (class Pdss1 *)port;
1059 response->am[num].u.p.isdn_chan = pdss1->p_m_b_channel;
1060 response->am[num].u.p.isdn_hold = pdss1->p_m_hold;
1061 response->am[num].u.p.isdn_ces = pdss1->p_m_d_ces;
1070 int sockserial = 1; // must start with 1, because 0 is used if no serial is set
1072 * handle admin socket (non blocking)
1074 int admin_handle(void)
1076 struct admin_list *admin, **adminp;
1078 struct admin_message msg;
1081 socklen_t sock_len = sizeof(sock_address);
1082 unsigned long on = 1;
1083 int work = 0; /* if work was done */
1084 struct Endpoint *epoint;
1089 /* check for new incoming connections */
1090 if ((new_sock = accept(sock, (struct sockaddr *)&sock_address, &sock_len)) >= 0)
1093 /* insert new socket */
1094 admin = (struct admin_list *)MALLOC(sizeof(struct admin_list));
1095 if (ioctl(new_sock, FIONBIO, (unsigned char *)(&on)) >= 0)
1098 // PERROR("DEBUG incoming socket %d, serial=%d\n", new_sock, sockserial);
1101 admin->sockserial = sockserial++;
1102 admin->next = admin_first;
1103 admin_first = admin;
1104 admin->sock = new_sock;
1107 FREE(admin, sizeof(struct admin_list));
1111 if (errno != EWOULDBLOCK)
1113 PERROR("Failed to accept connection from socket \"%s\". (errno=%d) Closing socket.\n", sock_address.sun_path, errno);
1119 /* loop all current socket connections */
1120 admin = admin_first;
1121 adminp = &admin_first;
1125 len = read(admin->sock, &msg, sizeof(msg));
1128 if (errno != EWOULDBLOCK)
1132 printf("Broken pipe on socket %d. (errno=%d).\n", admin->sock, errno);
1133 PDEBUG(DEBUG_LOG, "Broken pipe on socket %d. (errno=%d).\n", admin->sock, errno);
1134 *adminp = admin->next;
1135 free_connection(admin);
1143 //PERROR("DEBUG socket %d got data. serial=%d\n", admin->sock, admin->sockserial);
1148 /*release endpoint if exists */
1149 if (admin->epointid)
1151 epoint = find_epoint_id(admin->epointid);
1154 ((class DEFAULT_ENDPOINT_APP *)epoint->ep_app)->
1155 release(RELEASE_ALL, LOCATION_PRIVATE_LOCAL, CAUSE_NORMAL, LOCATION_PRIVATE_LOCAL, CAUSE_NORMAL);
1160 //PERROR("DEBUG socket %d closed by remote.\n", admin->sock);
1161 *adminp = admin->next;
1162 free_connection(admin);
1164 //PERROR("DEBUG (admin_first=%x)\n", admin_first);
1167 if (len != sizeof(msg))
1169 PERROR("Short/long read on socket %d. (len=%d != size=%d).\n", admin->sock, len, sizeof(msg));
1170 *adminp = admin->next;
1171 free_connection(admin);
1175 /* process socket command */
1176 if (admin->response)
1178 PERROR("Data from socket %d while sending response.\n", admin->sock);
1179 *adminp = admin->next;
1180 free_connection(admin);
1184 switch (msg.message)
1186 case ADMIN_REQUEST_CMD_INTERFACE:
1187 if (admin_interface(&admin->response) < 0)
1189 PERROR("Failed to create dial response for socket %d.\n", admin->sock);
1190 goto response_error;
1194 case ADMIN_REQUEST_CMD_ROUTE:
1195 if (admin_route(&admin->response) < 0)
1197 PERROR("Failed to create dial response for socket %d.\n", admin->sock);
1198 goto response_error;
1202 case ADMIN_REQUEST_CMD_DIAL:
1203 if (admin_dial(&admin->response, msg.u.x.message) < 0)
1205 PERROR("Failed to create dial response for socket %d.\n", admin->sock);
1206 goto response_error;
1210 case ADMIN_REQUEST_CMD_RELEASE:
1211 if (admin_release(&admin->response, msg.u.x.message) < 0)
1213 PERROR("Failed to create release response for socket %d.\n", admin->sock);
1214 goto response_error;
1218 case ADMIN_REQUEST_STATE:
1219 if (admin_state(&admin->response) < 0)
1221 PERROR("Failed to create state response for socket %d.\n", admin->sock);
1222 goto response_error;
1226 case ADMIN_TRACE_REQUEST:
1227 if (admin_trace(admin, &msg.u.trace_req) < 0)
1229 PERROR("Failed to create trace response for socket %d.\n", admin->sock);
1230 goto response_error;
1234 case ADMIN_REQUEST_CMD_BLOCK:
1235 if (admin_block(&admin->response, msg.u.x.portnum, msg.u.x.block) < 0)
1237 PERROR("Failed to create block response for socket %d.\n", admin->sock);
1238 goto response_error;
1243 if (admin_message_to_join(&msg.u.msg, admin->remote_name, admin->sock) < 0)
1245 PERROR("Failed to deliver message for socket %d.\n", admin->sock);
1246 goto response_error;
1251 struct admin_queue *response;
1253 response = admin->response;
1256 printf("%c", '0'+response->am[0].message);
1257 response=response->next;
1264 case ADMIN_CALL_SETUP:
1265 if (admin_call(admin, &msg) < 0)
1267 PERROR("Failed to create call for socket %d.\n", admin->sock);
1269 *adminp = admin->next;
1270 free_connection(admin);
1277 PERROR("Invalid message %d from socket %d.\n", msg.message, admin->sock);
1278 *adminp = admin->next;
1279 free_connection(admin);
1285 if (admin->response)
1288 //PERROR("DEBUG socket %d sending data.\n", admin->sock);
1289 len = write(admin->sock, ((unsigned char *)(admin->response->am))+admin->response->offset, sizeof(struct admin_message)*(admin->response->num)-admin->response->offset);
1292 if (errno != EWOULDBLOCK)
1302 if (len < (int)(sizeof(struct admin_message)*(admin->response->num)-admin->response->offset))
1304 admin->response->offset+=len;
1308 temp = admin->response;
1309 admin->response = admin->response->next;
1314 /* done with socket instance */
1316 adminp = &admin->next;
1317 admin = admin->next;