added exporting/importing bchannel stacks to the remote application
[lcr.git] / admin_server.c
index 6292e12..6b0647b 100644 (file)
@@ -96,18 +96,18 @@ void free_connection(struct admin_list *admin)
        class Join *join, *joinnext;
 
        /* free remote joins */
-       if (admin->remote[0])
+       if (admin->remote_name[0])
        {
                join = join_first;
                while(join)
                {
                        joinnext = join->next;
-                       if (join->j_type==JOIN_TYPE_REMOTE && !strcmp(((class JoinRemote *)join)->j_remote, admin->remote))
+                       if (join->j_type==JOIN_TYPE_REMOTE) if (((class JoinRemote *)join)->j_remote_id == admin->sock)
                        {
                                memset(&param, 0, sizeof(param));
                                param.disconnectinfo.cause = CAUSE_OUTOFORDER;
                                param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
-                               ((class JoinRemote *)join)->message_remote(0, MESSAGE_RELEASE, &param);
+                               ((class JoinRemote *)join)->message_remote(MESSAGE_RELEASE, &param);
                                /* join is now destroyed, so we go to next join */
                        }
                        join = joinnext;
@@ -511,7 +511,7 @@ int admin_call(struct admin_list *admin, struct admin_message *msg)
        class Endpoint          *epoint;
        class EndpointAppPBX    *apppbx;
 
-       if (!(epoint = new Endpoint(0, 0, 0)))
+       if (!(epoint = new Endpoint(0, 0)))
                FATAL("No memory for Endpoint instance\n");
        if (!(epoint->ep_app = apppbx = new DEFAULT_ENDPOINT_APP(epoint)))
                FATAL("No memory for Endpoint Application instance\n");
@@ -593,7 +593,7 @@ void admin_call_response(int adminid, int message, char *connected, int cause, i
 /*
  * send data to the remote socket join instance
  */
-int admin_message_to_join(struct admin_msg *msg, char *remote)
+int admin_message_to_join(struct admin_msg *msg, char *remote_name, int sock_id)
 {
        class Join                      *join;
        struct admin_list               *admin;
@@ -601,7 +601,7 @@ int admin_message_to_join(struct admin_msg *msg, char *remote)
        /* hello message */
        if (msg->type == MESSAGE_HELLO)
        {
-               if (remote[0])
+               if (remote_name[0])
                {
                        PERROR("Remote application repeats hello message.\n");
                        return(-1);
@@ -610,7 +610,7 @@ int admin_message_to_join(struct admin_msg *msg, char *remote)
                admin = admin_first;
                while(admin)
                {
-                       if (!strcmp(admin->remote, msg->param.hello.application))
+                       if (!strcmp(admin->remote_name, msg->param.hello.application))
                                break;
                        admin = admin->next;
                }
@@ -619,13 +619,13 @@ int admin_message_to_join(struct admin_msg *msg, char *remote)
                        PERROR("Remote application connects twice??? (ignoring)\n");
                        return(-1);
                }
-               /* set asterisk socket instance */
-               SCPY(remote, msg->param.hello.application);
+               /* set remote socket instance */
+               SCPY(remote_name, msg->param.hello.application);
                return(0);
        }
 
-       /* check we already have no application name */
-       if (!remote[0])
+       /* check we have no application name */
+       if (remote_name[0])
        {
                PERROR("Remote application did not send us a hello message.\n");
                return(-1);
@@ -635,12 +635,23 @@ int admin_message_to_join(struct admin_msg *msg, char *remote)
        if (msg->type == MESSAGE_NEWREF)
        {
                /* create new join instance */
-               join = new JoinRemote(0, remote); // must have no serial, because no endpoint is connected
+               join = new JoinRemote(0, remote_name, sock_id); // must have no serial, because no endpoint is connected
                if (!join)
                        FATAL("No memory for remote join instance\n");
                return(0);
        }
 
+       /* bchannel message
+        * no ref given for *_ack */
+       if (msg->type == MESSAGE_BCHANNEL)
+       if (msg->param.bchannel.type == BCHANNEL_ASSIGN_ACK
+        || msg->param.bchannel.type == BCHANNEL_REMOVE_ACK)
+       {
+               /* no ref, but address */
+               message_bchannel_from_join(NULL, msg->param.bchannel.type, msg->param.bchannel.addr);
+               return(0);
+       }
+       
        /* check for ref */
        if (!msg->ref)
        {
@@ -656,6 +667,11 @@ int admin_message_to_join(struct admin_msg *msg, char *remote)
                        break;
                join = join->next;
        }
+       if (!join)
+       {
+               PERROR("No join found with serial %d.\n", msg->ref);
+               return(-1);
+       }
 
        /* check application */
        if (join->j_type != JOIN_TYPE_REMOTE)
@@ -663,14 +679,14 @@ int admin_message_to_join(struct admin_msg *msg, char *remote)
                PERROR("Ref %d does not belong to a remote join instance.\n", msg->ref);
                return(-1);
        }
-       if (!!strcmp(remote, ((class JoinRemote *)join)->j_remote))
+       if (sock_id != ((class JoinRemote *)join)->j_remote_id)
        {
-               PERROR("Ref %d belongs to remote application %s, but not to sending application %s.\n", msg->ref, ((class JoinRemote *)join)->j_remote, remote);
+               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);
                return(-1);
        }
 
        /* send message */
-       ((class JoinRemote *)join)->message_remote(msg->ref, msg->type, &msg->param);
+       ((class JoinRemote *)join)->message_remote(msg->type, &msg->param);
 
        return(0);
 }
@@ -679,7 +695,7 @@ int admin_message_to_join(struct admin_msg *msg, char *remote)
 /*
  * this function is called for every message to remote socket
  */
-int admin_message_from_join(char *remote, unsigned long ref, int message_type, union parameter *param)
+int admin_message_from_join(int remote_id, unsigned long ref, int message_type, union parameter *param)
 {
        struct admin_list       *admin;
        struct admin_queue      *response, **responsep; /* response pointer */
@@ -690,7 +706,7 @@ int admin_message_from_join(char *remote, unsigned long ref, int message_type, u
        admin = admin_first;
        while(admin)
        {
-               if (admin->remote[0] && !strcmp(admin->remote, remote))
+               if (admin->remote_name[0] && admin->sock==remote_id)
                        break;
                admin = admin->next;
        }
@@ -775,7 +791,7 @@ int admin_state(struct admin_queue **responsep)
        admin = admin_first;
        while(admin)
        {
-               if (admin->remote[0])
+               if (admin->remote_name[0])
                        i++;
                admin = admin->next;
        }
@@ -877,12 +893,12 @@ int admin_state(struct admin_queue **responsep)
        admin = admin_first;
        while(admin)
        {
-               if (admin->remote[0])
+               if (admin->remote_name[0])
                {
                        /* message */
                        response->am[num].message = ADMIN_RESPONSE_S_REMOTE;
                        /* name */
-                       SCPY(response->am[num].u.r.name, admin->remote);
+                       SCPY(response->am[num].u.r.name, admin->remote_name);
                        /* */
                        num++;
                }
@@ -902,7 +918,7 @@ int admin_state(struct admin_queue **responsep)
                        response->am[num].u.j.partyline = ((class JoinPBX *)join)->j_partyline;
                /* remote application */
                if (join->j_type == JOIN_TYPE_REMOTE)
-                       SCPY(response->am[num].u.j.remote, ((class JoinRemote *)join)->j_remote);
+                       SCPY(response->am[num].u.j.remote, ((class JoinRemote *)join)->j_remote_name);
                /* */
                join = join->next;
                num++;
@@ -1225,7 +1241,7 @@ int admin_handle(void)
                        break;
 
                        case ADMIN_MESSAGE:
-                       if (admin_message_to_join(&msg.u.msg, admin->remote) < 0)
+                       if (admin_message_to_join(&msg.u.msg, admin->remote_name, admin->sock) < 0)
                        {
                                PERROR("Failed to deliver message for socket %d.\n", admin->sock);
                                goto response_error;