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>
21 #include <sys/ioctl.h>
22 //#include <sys/file.h>
24 //#include <sys/mman.h>
25 //#include <sys/resource.h>
26 #include <sys/socket.h>
32 char *socket_name = SOCKET_NAME;
34 struct sockaddr_un sock_address;
36 struct admin_list *admin_first = NULL;
39 * initialize admin socket
45 /* open and bind socket */
46 if ((sock = socket(PF_UNIX, SOCK_STREAM, 0)) < 0)
48 PERROR("Failed to create admin socket. (errno=%d)\n", errno);
52 memset(&sock_address, 0, sizeof(sock_address));
53 sock_address.sun_family = AF_UNIX;
54 UCPY(sock_address.sun_path, socket_name);
56 if (bind(sock, (struct sockaddr *)(&sock_address), SUN_LEN(&sock_address)) < 0)
62 PERROR("Failed to bind admin socket to \"%s\". (errno=%d)\n", sock_address.sun_path, errno);
65 if (listen(sock, 5) < 0)
71 PERROR("Failed to listen to socket \"%s\". (errno=%d)\n", sock_address.sun_path, errno);
74 if (ioctl(sock, FIONBIO, (unsigned char *)(&on)) < 0)
80 PERROR("Failed to set socket \"%s\" into non-blocking mode. (errno=%d)\n", sock_address.sun_path, errno);
89 * also releases all remote joins
91 void free_connection(struct admin_list *admin)
93 struct admin_queue *response;
95 union parameter param;
96 class Join *join, *joinnext;
98 /* free remote joins */
99 if (admin->remote_name[0])
104 joinnext = join->next;
105 if (join->j_type==JOIN_TYPE_REMOTE) if (((class JoinRemote *)join)->j_remote_id == admin->sock)
107 memset(¶m, 0, sizeof(param));
108 param.disconnectinfo.cause = CAUSE_OUTOFORDER;
109 param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
110 ((class JoinRemote *)join)->message_remote(MESSAGE_RELEASE, ¶m);
111 /* join is now destroyed, so we go to next join */
117 if (admin->sock >= 0)
122 // printf("new\n", response);
123 response = admin->response;
127 // printf("%x\n", response);
128 temp = response->next;
131 response = (struct admin_queue *)temp;
133 // printf("new2\n", response);
135 // printf("new3\n", response);
141 * cleanup admin socket
143 void admin_cleanup(void)
145 struct admin_list *admin, *next;;
152 free_connection(admin);
165 * do interface reload
167 int admin_interface(struct admin_queue **responsep)
169 struct admin_queue *response; /* response pointer */
173 if (read_interfaces())
176 free_interfaces(interface_first);
177 interface_first = interface_newlist;
178 interface_newlist = NULL;
181 err_txt = interface_error;
184 /* create state response */
185 response = (struct admin_queue *)MALLOC(sizeof(struct admin_queue)+sizeof(admin_message));
189 response->am[0].message = ADMIN_RESPONSE_CMD_INTERFACE;
191 response->am[0].u.x.error = err;
193 SCPY(response->am[0].u.x.message, err_txt);
194 /* attach to response chain */
195 *responsep = response;
196 responsep = &response->next;
205 int admin_route(struct admin_queue **responsep)
207 struct route_ruleset *ruleset_new;
208 struct admin_queue *response; /* response pointer */
209 char err_txt[256] = "";
214 class EndpointAppPBX *apppbx;
218 apppbx = apppbx_first;
222 apppbx = apppbx->next;
226 SPRINT(err_txt, "Cannot reload routing, because %d endpoints active\n", n);
231 if (!(ruleset_new = ruleset_parse()))
233 SPRINT(err_txt, ruleset_error);
237 ruleset_free(ruleset_first);
238 ruleset_first = ruleset_new;
239 ruleset_main = getrulesetbyname("main");
242 SPRINT(err_txt, "Ruleset reloaded, but rule 'main' not found.\n");
245 apppbx = apppbx_first;
248 if (apppbx->e_action)
250 switch(apppbx->e_action->index)
252 case ACTION_INTERNAL:
253 apppbx->e_action = &action_internal;
255 case ACTION_EXTERNAL:
256 apppbx->e_action = &action_external;
259 apppbx->e_action = &action_remote;
261 case ACTION_VBOX_RECORD:
262 apppbx->e_action = &action_vbox;
264 case ACTION_PARTYLINE:
265 apppbx->e_action = &action_partyline;
270 } else if (apppbx->e_state != EPOINT_STATE_CONNECT)
273 apppbx->e_callback = 0;
274 apppbx->e_action = NULL;
275 apppbx->release(RELEASE_ALL, LOCATION_PRIVATE_LOCAL, CAUSE_NORMAL, LOCATION_PRIVATE_LOCAL, CAUSE_NORMAL);
278 numberrize_callerinfo(apppbx->e_callerinfo.id, apppbx->e_callerinfo.ntype),
279 apppbx->e_dialinginfo.id,
282 apppbx->ea_endpoint->ep_serial,
283 "KICK (reload routing)");
287 apppbx->e_action_timeout = NULL;
288 apppbx->e_rule = NULL;
289 apppbx->e_ruleset = NULL;
291 apppbx = apppbx->next;
295 /* create state response */
296 response = (struct admin_queue *)MALLOC(sizeof(struct admin_queue)+sizeof(admin_message));
300 response->am[0].message = ADMIN_RESPONSE_CMD_ROUTE;
302 response->am[0].u.x.error = err;
304 SCPY(response->am[0].u.x.message, err_txt);
305 /* attach to response chain */
306 *responsep = response;
307 responsep = &response->next;
316 int admin_dial(struct admin_queue **responsep, char *message)
318 struct extension ext; /* temporary extension's settings */
319 struct admin_queue *response; /* response pointer */
320 char *p; /* pointer to dialing digits */
322 /* create state response */
323 response = (struct admin_queue *)MALLOC(sizeof(struct admin_queue)+sizeof(admin_message));
327 response->am[0].message = ADMIN_RESPONSE_CMD_DIAL;
329 /* process request */
330 if (!(p = strchr(message,':')))
332 response->am[0].u.x.error = -EINVAL;
333 SPRINT(response->am[0].u.x.message, "no seperator ':' in message to seperate number from extension");
338 /* modify extension */
339 if (!read_extension(&ext, message))
341 response->am[0].u.x.error = -EINVAL;
342 SPRINT(response->am[0].u.x.message, "extension doesn't exist");
346 write_extension(&ext, message);
349 /* attach to response chain */
350 *responsep = response;
351 responsep = &response->next;
359 int admin_trace(struct admin_list *admin, struct admin_trace_req *trace)
361 memcpy(&admin->trace, trace, sizeof(struct admin_trace_req));
369 * 0 = make port available
370 * 1 = make port administratively blocked
372 * the result is returned:
373 * 0 = port is now available
374 * 1 = port is now blocked
375 * 2 = port cannot be loaded or has been unloaded
376 * -1 = port doesn't exist
378 int admin_block(struct admin_queue **responsep, int portnum, int block)
380 struct admin_queue *response; /* response pointer */
381 struct interface *interface;
382 struct interface_port *ifport;
384 /* create block response */
385 response = (struct admin_queue *)MALLOC(sizeof(struct admin_queue)+sizeof(admin_message));
389 response->am[0].message = ADMIN_RESPONSE_CMD_BLOCK;
390 response->am[0].u.x.portnum = portnum;
392 /* search for port */
393 interface = interface_first;
396 ifport = interface->ifport;
399 if (ifport->portnum == portnum)
401 ifport = ifport->next;
405 interface = interface->next;
407 /* not found, we return -1 */
410 response->am[0].u.x.block = -1;
411 response->am[0].u.x.error = 1;
412 SPRINT(response->am[0].u.x.message, "Port %d does not exist.", portnum);
417 if (!ifport->mISDNport)
419 /* not loaded anyway */
422 response->am[0].u.x.block = 2;
426 /* try loading interface */
427 ifport->block = block;
430 /* port cannot load */
431 if (ifport->block >= 2)
433 response->am[0].u.x.block = 2;
434 response->am[0].u.x.error = 1;
435 SPRINT(response->am[0].u.x.message, "Port %d will not load.", portnum);
440 response->am[0].u.x.block = ifport->block;
444 /* if we shall unload interface */
447 mISDNport_close(ifport->mISDNport);
448 ifport->mISDNport = 0;
453 /* port new blocking state */
454 ifport->block = response->am[0].u.x.block = block;
457 /* attach to response chain */
458 *responsep = response;
459 responsep = &response->next;
467 int admin_release(struct admin_queue **responsep, char *message)
470 struct admin_queue *response; /* response pointer */
471 class EndpointAppPBX *apppbx;
473 /* create state response */
474 response = (struct admin_queue *)MALLOC(sizeof(struct admin_queue)+sizeof(admin_message));
478 response->am[0].message = ADMIN_RESPONSE_CMD_RELEASE;
481 apppbx = apppbx_first;
484 if (apppbx->ea_endpoint->ep_serial == id)
486 apppbx = apppbx->next;
490 response->am[0].u.x.error = -EINVAL;
491 SPRINT(response->am[0].u.x.message, "Given endpoint %d doesn't exist.", id);
495 apppbx->e_callback = 0;
496 apppbx->release(RELEASE_ALL, LOCATION_PRIVATE_LOCAL, CAUSE_NORMAL, LOCATION_PRIVATE_LOCAL, CAUSE_NORMAL);
499 /* attach to response chain */
500 *responsep = response;
501 responsep = &response->next;
509 int admin_call(struct admin_list *admin, struct admin_message *msg)
511 class Endpoint *epoint;
512 class EndpointAppPBX *apppbx;
514 if (!(epoint = new Endpoint(0, 0)))
515 FATAL("No memory for Endpoint instance\n");
516 if (!(epoint->ep_app = apppbx = new DEFAULT_ENDPOINT_APP(epoint, 1))) // outgoing
517 FATAL("No memory for Endpoint Application instance\n");
518 apppbx->e_adminid = admin->sockserial;
519 admin->epointid = epoint->ep_serial;
520 SCPY(apppbx->e_callerinfo.id, nationalize_callerinfo(msg->u.call.callerid, &apppbx->e_callerinfo.ntype));
521 if (msg->u.call.present)
522 apppbx->e_callerinfo.present = INFO_PRESENT_ALLOWED;
524 apppbx->e_callerinfo.present = INFO_PRESENT_RESTRICTED;
525 apppbx->e_callerinfo.screen = INFO_SCREEN_NETWORK;
527 //printf("hh=%d\n", apppbx->e_capainfo.hlc);
529 apppbx->e_capainfo.bearer_capa = msg->u.call.bc_capa;
530 apppbx->e_capainfo.bearer_mode = msg->u.call.bc_mode;
531 apppbx->e_capainfo.bearer_info1 = msg->u.call.bc_info1;
532 apppbx->e_capainfo.hlc = msg->u.call.hlc;
533 apppbx->e_capainfo.exthlc = msg->u.call.exthlc;
534 SCPY(apppbx->e_dialinginfo.id, msg->u.call.dialing);
535 SCPY(apppbx->e_dialinginfo.interfaces, msg->u.call.interface);
536 apppbx->e_dialinginfo.sending_complete = 1;
538 apppbx->new_state(PORT_STATE_OUT_SETUP);
545 * this function is called for response whenever a call state changes.
547 void admin_call_response(int adminid, int message, char *connected, int cause, int location, int notify)
549 struct admin_list *admin;
550 struct admin_queue *response, **responsep; /* response pointer */
552 /* searching for admin id
553 * maybe there is no admin instance, because the calling port was not
554 * initiated by admin_call */
558 if (adminid == admin->sockserial)
565 /* seek to end of response list */
566 response = admin->response;
567 responsep = &admin->response;
570 responsep = &response->next;
571 response = response->next;
574 /* create state response */
575 response = (struct admin_queue *)MALLOC(sizeof(struct admin_queue)+sizeof(admin_message));
579 response->am[0].message = message;
580 // printf("MESSAGE: %d\n", message);
582 SCPY(response->am[0].u.call.callerid, connected);
583 response->am[0].u.call.cause = cause;
584 response->am[0].u.call.location = location;
585 response->am[0].u.call.notify = notify;
587 /* attach to response chain */
588 *responsep = response;
589 responsep = &response->next;
594 * send data to the remote socket join instance
596 int admin_message_to_join(struct admin_msg *msg, char *remote_name, int sock_id)
599 struct admin_list *admin;
602 if (msg->type == MESSAGE_HELLO)
606 PERROR("Remote application repeats hello message.\n");
609 /* look for second application */
613 if (!strcmp(admin->remote_name, msg->param.hello.application))
619 PERROR("Remote application connects twice??? (ignoring)\n");
622 /* set remote socket instance */
623 SCPY(remote_name, msg->param.hello.application);
627 /* check we have no application name */
630 PERROR("Remote application did not send us a hello message.\n");
635 if (msg->type == MESSAGE_NEWREF)
637 /* create new join instance */
638 join = new JoinRemote(0, remote_name, sock_id); // must have no serial, because no endpoint is connected
640 FATAL("No memory for remote join instance\n");
645 * no ref given for *_ack */
646 if (msg->type == MESSAGE_BCHANNEL)
647 if (msg->param.bchannel.type == BCHANNEL_ASSIGN_ACK
648 || msg->param.bchannel.type == BCHANNEL_REMOVE_ACK)
650 /* no ref, but address */
651 message_bchannel_from_join(NULL, msg->param.bchannel.type, msg->param.bchannel.handle);
658 PERROR("Remote application did not send us a valid ref with a message.\n");
662 /* find join instance */
666 if (join->j_serial == msg->ref)
672 PERROR("No join found with serial %d.\n", msg->ref);
676 /* check application */
677 if (join->j_type != JOIN_TYPE_REMOTE)
679 PERROR("Ref %d does not belong to a remote join instance.\n", msg->ref);
682 if (sock_id != ((class JoinRemote *)join)->j_remote_id)
684 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);
689 ((class JoinRemote *)join)->message_remote(msg->type, &msg->param);
696 * this function is called for every message to remote socket
698 int admin_message_from_join(int remote_id, unsigned long ref, int message_type, union parameter *param)
700 struct admin_list *admin;
701 struct admin_queue *response, **responsep; /* response pointer */
703 /* searching for admin id
704 * maybe there is no given remote application
709 if (admin->remote_name[0] && admin->sock==remote_id)
713 /* no given remote application connected */
717 /* seek to end of response list */
718 response = admin->response;
719 responsep = &admin->response;
722 responsep = &response->next;
723 response = response->next;
726 /* create state response */
727 response = (struct admin_queue *)MALLOC(sizeof(struct admin_queue)+sizeof(admin_message));
732 response->am[0].u.msg.type = message_type;
733 response->am[0].u.msg.ref = ref;
734 memcpy(&response->am[0].u.msg.param, param, sizeof(union parameter));
736 /* attach to response chain */
737 *responsep = response;
738 responsep = &response->next;
747 int admin_state(struct admin_queue **responsep)
751 class EndpointAppPBX *apppbx;
754 struct interface *interface;
755 struct interface_port *ifport;
756 struct mISDNport *mISDNport;
760 struct admin_queue *response;
761 struct admin_list *admin;
763 /* create state response */
764 response = (struct admin_queue *)MALLOC(sizeof(struct admin_queue)+sizeof(admin_message));
768 response->am[0].message = ADMIN_RESPONSE_STATE;
770 SCPY(response->am[0].u.s.version_string, VERSION_STRING);
772 memcpy(&response->am[0].u.s.tm, now_tm, sizeof(struct tm));
774 SCPY(response->am[0].u.s.logfile, options.log);
775 /* interface count */
777 interface = interface_first;
780 ifport = interface->ifport;
784 ifport = ifport->next;
786 interface = interface->next;
788 response->am[0].u.s.interfaces = i;
789 /* remote connection count */
794 if (admin->remote_name[0])
798 response->am[0].u.s.remotes = i;
807 response->am[0].u.s.joins = i;
809 apppbx = apppbx_first;
814 apppbx = apppbx->next;
816 response->am[0].u.s.epoints = i;
825 response->am[0].u.s.ports = i;
826 /* attach to response chain */
827 *responsep = response;
828 responsep = &response->next;
830 /* create response for all interfaces */
831 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);
834 response = (struct admin_queue *)MALLOC(sizeof(admin_queue)+(num*sizeof(admin_message)));
837 *responsep = response;
838 responsep = &response->next;
839 interface = interface_first;
843 ifport = interface->ifport;
847 response->am[num].message = ADMIN_RESPONSE_S_INTERFACE;
849 SCPY(response->am[num].u.i.interface_name, interface->name);
851 response->am[num].u.i.portnum = ifport->portnum;
853 response->am[num].u.i.extension = interface->extension;
855 response->am[num].u.i.block = ifport->block;
856 if (ifport->mISDNport)
858 mISDNport = ifport->mISDNport;
861 response->am[num].u.i.ptp = mISDNport->ptp;
863 response->am[num].u.i.ntmode = mISDNport->ntmode;
865 response->am[num].u.i.pri = mISDNport->pri;
867 response->am[num].u.i.use = mISDNport->use;
869 response->am[num].u.i.l1link = mISDNport->l1link;
871 response->am[num].u.i.l2link = mISDNport->l2link;
873 response->am[num].u.i.channels = mISDNport->b_num;
877 while(i < mISDNport->b_num)
879 response->am[num].u.i.busy[i] = mISDNport->b_state[i];
880 if (mISDNport->b_port[i])
881 response->am[num].u.i.port[i] = mISDNport->b_port[i]->p_serial;
887 ifport = ifport->next;
889 interface = interface->next;
892 /* create response for all remotes */
896 if (admin->remote_name[0])
899 response->am[num].message = ADMIN_RESPONSE_S_REMOTE;
901 SCPY(response->am[num].u.r.name, admin->remote_name);
908 /* create response for all joins */
913 response->am[num].message = ADMIN_RESPONSE_S_JOIN;
915 response->am[num].u.j.serial = join->j_serial;
917 if (join->j_type == JOIN_TYPE_PBX)
918 response->am[num].u.j.partyline = ((class JoinPBX *)join)->j_partyline;
919 /* remote application */
920 if (join->j_type == JOIN_TYPE_REMOTE)
921 SCPY(response->am[num].u.j.remote, ((class JoinRemote *)join)->j_remote_name);
927 /* create response for all endpoint */
928 apppbx = apppbx_first;
932 response->am[num].message = ADMIN_RESPONSE_S_EPOINT;
934 response->am[num].u.e.serial = apppbx->ea_endpoint->ep_serial;
936 response->am[num].u.e.join = apppbx->ea_endpoint->ep_join_id;
937 /* rx notification */
938 response->am[num].u.e.rx_state = apppbx->e_rx_state;
939 /* tx notification */
940 response->am[num].u.e.tx_state = apppbx->e_tx_state;
942 switch(apppbx->e_state)
944 case EPOINT_STATE_IN_SETUP:
945 response->am[num].u.e.state = ADMIN_STATE_IN_SETUP;
947 case EPOINT_STATE_OUT_SETUP:
948 response->am[num].u.e.state = ADMIN_STATE_OUT_SETUP;
950 case EPOINT_STATE_IN_OVERLAP:
951 response->am[num].u.e.state = ADMIN_STATE_IN_OVERLAP;
953 case EPOINT_STATE_OUT_OVERLAP:
954 response->am[num].u.e.state = ADMIN_STATE_OUT_OVERLAP;
956 case EPOINT_STATE_IN_PROCEEDING:
957 response->am[num].u.e.state = ADMIN_STATE_IN_PROCEEDING;
959 case EPOINT_STATE_OUT_PROCEEDING:
960 response->am[num].u.e.state = ADMIN_STATE_OUT_PROCEEDING;
962 case EPOINT_STATE_IN_ALERTING:
963 response->am[num].u.e.state = ADMIN_STATE_IN_ALERTING;
965 case EPOINT_STATE_OUT_ALERTING:
966 response->am[num].u.e.state = ADMIN_STATE_OUT_ALERTING;
968 case EPOINT_STATE_CONNECT:
969 response->am[num].u.e.state = ADMIN_STATE_CONNECT;
971 case EPOINT_STATE_IN_DISCONNECT:
972 response->am[num].u.e.state = ADMIN_STATE_IN_DISCONNECT;
974 case EPOINT_STATE_OUT_DISCONNECT:
975 response->am[num].u.e.state = ADMIN_STATE_OUT_DISCONNECT;
978 response->am[num].u.e.state = ADMIN_STATE_IDLE;
981 SCPY(response->am[num].u.e.terminal, apppbx->e_ext.number);
983 SCPY(response->am[num].u.e.callerid, apppbx->e_callerinfo.id);
985 SCPY(response->am[num].u.e.dialing, apppbx->e_dialinginfo.id);
987 if (apppbx->e_action)
988 SCPY(response->am[num].u.e.action, action_defs[apppbx->e_action->index].name);
989 // if (apppbx->e_action)
990 // printf("action=%s\n",action_defs[apppbx->e_action->index].name);
992 response->am[num].u.e.park = apppbx->ea_endpoint->ep_park;
993 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))
994 memcpy(response->am[num].u.e.park_callid, apppbx->ea_endpoint->ep_park_callid, apppbx->ea_endpoint->ep_park_len);
995 response->am[num].u.e.park_len = apppbx->ea_endpoint->ep_park_len;
997 if (apppbx->e_crypt == CRYPT_ON)
998 response->am[num].u.e.crypt = 1;
1000 apppbx = apppbx->next;
1004 /* create response for all ports */
1009 response->am[num].message = ADMIN_RESPONSE_S_PORT;
1011 response->am[num].u.p.serial = port->p_serial;
1013 SCPY(response->am[num].u.p.name, port->p_name);
1015 response->am[num].u.p.epoint = ACTIVE_EPOINT(port->p_epointlist);
1017 switch(port->p_state)
1019 case PORT_STATE_IN_SETUP:
1020 response->am[num].u.p.state = ADMIN_STATE_IN_SETUP;
1022 case PORT_STATE_OUT_SETUP:
1023 response->am[num].u.p.state = ADMIN_STATE_OUT_SETUP;
1025 case PORT_STATE_IN_OVERLAP:
1026 response->am[num].u.p.state = ADMIN_STATE_IN_OVERLAP;
1028 case PORT_STATE_OUT_OVERLAP:
1029 response->am[num].u.p.state = ADMIN_STATE_OUT_OVERLAP;
1031 case PORT_STATE_IN_PROCEEDING:
1032 response->am[num].u.p.state = ADMIN_STATE_IN_PROCEEDING;
1034 case PORT_STATE_OUT_PROCEEDING:
1035 response->am[num].u.p.state = ADMIN_STATE_OUT_PROCEEDING;
1037 case PORT_STATE_IN_ALERTING:
1038 response->am[num].u.p.state = ADMIN_STATE_IN_ALERTING;
1040 case PORT_STATE_OUT_ALERTING:
1041 response->am[num].u.p.state = ADMIN_STATE_OUT_ALERTING;
1043 case PORT_STATE_CONNECT:
1044 response->am[num].u.p.state = ADMIN_STATE_CONNECT;
1046 case PORT_STATE_IN_DISCONNECT:
1047 response->am[num].u.p.state = ADMIN_STATE_IN_DISCONNECT;
1049 case PORT_STATE_OUT_DISCONNECT:
1050 response->am[num].u.p.state = ADMIN_STATE_OUT_DISCONNECT;
1053 response->am[num].u.p.state = ADMIN_STATE_IDLE;
1056 if ((port->p_type&PORT_CLASS_mISDN_MASK) == PORT_CLASS_mISDN_DSS1)
1058 response->am[num].u.p.isdn = 1;
1059 pdss1 = (class Pdss1 *)port;
1060 response->am[num].u.p.isdn_chan = pdss1->p_m_b_channel;
1061 response->am[num].u.p.isdn_hold = pdss1->p_m_hold;
1062 response->am[num].u.p.isdn_ces = pdss1->p_m_d_ces;
1071 int sockserial = 1; // must start with 1, because 0 is used if no serial is set
1073 * handle admin socket (non blocking)
1075 int admin_handle(void)
1077 struct admin_list *admin, **adminp;
1079 struct admin_message msg;
1082 socklen_t sock_len = sizeof(sock_address);
1083 unsigned long on = 1;
1084 int work = 0; /* if work was done */
1085 struct Endpoint *epoint;
1090 /* check for new incoming connections */
1091 if ((new_sock = accept(sock, (struct sockaddr *)&sock_address, &sock_len)) >= 0)
1094 /* insert new socket */
1095 admin = (struct admin_list *)MALLOC(sizeof(struct admin_list));
1096 if (ioctl(new_sock, FIONBIO, (unsigned char *)(&on)) >= 0)
1099 // PERROR("DEBUG incoming socket %d, serial=%d\n", new_sock, sockserial);
1102 admin->sockserial = sockserial++;
1103 admin->next = admin_first;
1104 admin_first = admin;
1105 admin->sock = new_sock;
1108 FREE(admin, sizeof(struct admin_list));
1112 if (errno != EWOULDBLOCK)
1114 PERROR("Failed to accept connection from socket \"%s\". (errno=%d) Closing socket.\n", sock_address.sun_path, errno);
1120 /* loop all current socket connections */
1121 admin = admin_first;
1122 adminp = &admin_first;
1126 len = read(admin->sock, &msg, sizeof(msg));
1129 if (errno != EWOULDBLOCK)
1133 printf("Broken pipe on socket %d. (errno=%d).\n", admin->sock, errno);
1134 PDEBUG(DEBUG_LOG, "Broken pipe on socket %d. (errno=%d).\n", admin->sock, errno);
1135 *adminp = admin->next;
1136 free_connection(admin);
1144 //PERROR("DEBUG socket %d got data. serial=%d\n", admin->sock, admin->sockserial);
1149 /*release endpoint if exists */
1150 if (admin->epointid)
1152 epoint = find_epoint_id(admin->epointid);
1155 ((class DEFAULT_ENDPOINT_APP *)epoint->ep_app)->
1156 release(RELEASE_ALL, LOCATION_PRIVATE_LOCAL, CAUSE_NORMAL, LOCATION_PRIVATE_LOCAL, CAUSE_NORMAL);
1161 //PERROR("DEBUG socket %d closed by remote.\n", admin->sock);
1162 *adminp = admin->next;
1163 free_connection(admin);
1165 //PERROR("DEBUG (admin_first=%x)\n", admin_first);
1168 if (len != sizeof(msg))
1170 PERROR("Short/long read on socket %d. (len=%d != size=%d).\n", admin->sock, len, sizeof(msg));
1171 *adminp = admin->next;
1172 free_connection(admin);
1176 /* process socket command */
1177 if (admin->response)
1179 PERROR("Data from socket %d while sending response.\n", admin->sock);
1180 *adminp = admin->next;
1181 free_connection(admin);
1185 switch (msg.message)
1187 case ADMIN_REQUEST_CMD_INTERFACE:
1188 if (admin_interface(&admin->response) < 0)
1190 PERROR("Failed to create dial response for socket %d.\n", admin->sock);
1191 goto response_error;
1195 case ADMIN_REQUEST_CMD_ROUTE:
1196 if (admin_route(&admin->response) < 0)
1198 PERROR("Failed to create dial response for socket %d.\n", admin->sock);
1199 goto response_error;
1203 case ADMIN_REQUEST_CMD_DIAL:
1204 if (admin_dial(&admin->response, msg.u.x.message) < 0)
1206 PERROR("Failed to create dial response for socket %d.\n", admin->sock);
1207 goto response_error;
1211 case ADMIN_REQUEST_CMD_RELEASE:
1212 if (admin_release(&admin->response, msg.u.x.message) < 0)
1214 PERROR("Failed to create release response for socket %d.\n", admin->sock);
1215 goto response_error;
1219 case ADMIN_REQUEST_STATE:
1220 if (admin_state(&admin->response) < 0)
1222 PERROR("Failed to create state response for socket %d.\n", admin->sock);
1223 goto response_error;
1227 case ADMIN_TRACE_REQUEST:
1228 if (admin_trace(admin, &msg.u.trace_req) < 0)
1230 PERROR("Failed to create trace response for socket %d.\n", admin->sock);
1231 goto response_error;
1235 case ADMIN_REQUEST_CMD_BLOCK:
1236 if (admin_block(&admin->response, msg.u.x.portnum, msg.u.x.block) < 0)
1238 PERROR("Failed to create block response for socket %d.\n", admin->sock);
1239 goto response_error;
1244 if (admin_message_to_join(&msg.u.msg, admin->remote_name, admin->sock) < 0)
1246 PERROR("Failed to deliver message for socket %d.\n", admin->sock);
1247 goto response_error;
1252 struct admin_queue *response;
1254 response = admin->response;
1257 printf("%c", '0'+response->am[0].message);
1258 response=response->next;
1265 case ADMIN_CALL_SETUP:
1266 if (admin_call(admin, &msg) < 0)
1268 PERROR("Failed to create call for socket %d.\n", admin->sock);
1270 *adminp = admin->next;
1271 free_connection(admin);
1278 PERROR("Invalid message %d from socket %d.\n", msg.message, admin->sock);
1279 *adminp = admin->next;
1280 free_connection(admin);
1286 if (admin->response)
1289 //PERROR("DEBUG socket %d sending data.\n", admin->sock);
1290 len = write(admin->sock, ((unsigned char *)(admin->response->am))+admin->response->offset, sizeof(struct admin_message)*(admin->response->num)-admin->response->offset);
1293 if (errno != EWOULDBLOCK)
1303 if (len < (int)(sizeof(struct admin_message)*(admin->response->num)-admin->response->offset))
1305 admin->response->offset+=len;
1309 temp = admin->response;
1310 admin->response = admin->response->next;
1315 /* done with socket instance */
1317 adminp = &admin->next;
1318 admin = admin->next;