Merged revisions 328428-328429 via svnmerge from
[asterisk/asterisk.git] / addons / ooh323c / src / ooGkClient.c
index e5c6361..33d7df0 100644 (file)
@@ -22,6 +22,7 @@
  */
 #include "asterisk.h"
 #include "asterisk/lock.h"
+#include "asterisk/netsock2.h"
 
 #include "ooGkClient.h"
 #include "ootypes.h"
@@ -261,7 +262,7 @@ int ooGkClientCreateChannel(ooGkClient *pGkClient)
    int ret=0;
    OOIPADDR ipaddrs;
    /* Create socket */
-   if((ret=ooSocketCreateUDP(&pGkClient->rasSocket))!=ASN_OK)
+   if((ret=ooSocketCreateUDP(&pGkClient->rasSocket, 4))!=ASN_OK)
    {
       OOTRACEERR1("Failed to create RAS socket\n");
       pGkClient->state = GkClientFailed;
@@ -269,7 +270,7 @@ int ooGkClientCreateChannel(ooGkClient *pGkClient)
    }
    if(pGkClient->localRASPort)
    {
-      ret= ooSocketStrToAddr (pGkClient->localRASIP, &ipaddrs);
+      inet_pton(AF_INET, pGkClient->localRASIP, &ipaddrs);
       if( (ret=ooSocketBind( pGkClient->rasSocket, ipaddrs, 
            pGkClient->localRASPort))!=ASN_OK ) 
       {
@@ -295,7 +296,7 @@ int ooGkClientCreateChannel(ooGkClient *pGkClient)
       OOTRACEDBGA1("Determining ip address for RAS channel "
                    "multihomed mode. \n");
       ret = ooSocketGetIpAndPort(pGkClient->rasSocket, pGkClient->localRASIP, 
-                                 20, &pGkClient->localRASPort);
+                                 20, &pGkClient->localRASPort, NULL);
       if(ret != ASN_OK)
       {
          OOTRACEERR1("Error:Failed to retrieve local ip and port from "
@@ -666,7 +667,7 @@ int ooGkClientSendGRQ(ooGkClient *pGkClient)
    }
 
  
-   ooSocketConvertIpToNwAddr(pGkClient->localRASIP, pRasAddress->ip.data);
+   inet_pton(AF_INET, pGkClient->localRASIP, pRasAddress->ip.data);
 
    pRasAddress->ip.numocts = 4;
    pRasAddress->port = pGkClient->localRASPort;
@@ -686,7 +687,7 @@ int ooGkClientSendGRQ(ooGkClient *pGkClient)
 
    pGkReq->m.endpointAliasPresent=TRUE;
    if(OO_OK != ooPopulateAliasList(&pGkClient->msgCtxt, gH323ep.aliases, 
-                                      &pGkReq->endpointAlias))
+                                      &pGkReq->endpointAlias, 0))
    {
       OOTRACEERR1("Error Failed to fill alias information for GRQ message\n");
       memReset(&pGkClient->msgCtxt);
@@ -823,10 +824,9 @@ int ooGkClientHandleGatekeeperConfirm
              sizeof(ASN116BITCHAR)* pGkClient->gkId.nchars);
    }
    else{
-      OOTRACEERR1("ERROR:No Gatekeeper ID present in received GKConfirmed "
+      OOTRACEINFO1("ERROR:No Gatekeeper ID present in received GKConfirmed "
                   "message\n");
-      OOTRACEINFO1("Ignoring message and will retransmit GRQ after timeout\n");
-      return OO_FAILED;
+      pGkClient->gkId.nchars = 0;
    }
    
    /* Extract Gatekeeper's RAS address */
@@ -933,7 +933,7 @@ int ooGkClientSendRRQ(ooGkClient *pGkClient, ASN1BOOL keepAlive)
    }
    pTransportAddress->t = T_H225TransportAddress_ipAddress;
    pTransportAddress->u.ipAddress = pIpAddress;
-   ooSocketConvertIpToNwAddr(pGkClient->localRASIP, pIpAddress->ip.data);
+   inet_pton(AF_INET, pGkClient->localRASIP, pIpAddress->ip.data);
    pIpAddress->ip.numocts = 4;
    pIpAddress->port = gH323ep.listenPort;
    
@@ -961,7 +961,7 @@ int ooGkClientSendRRQ(ooGkClient *pGkClient, ASN1BOOL keepAlive)
    pTransportAddress->t = T_H225TransportAddress_ipAddress;
    pTransportAddress->u.ipAddress = pIpAddress;
    
-   ooSocketConvertIpToNwAddr(pGkClient->localRASIP, pIpAddress->ip.data);
+   inet_pton(AF_INET, pGkClient->localRASIP, pIpAddress->ip.data);
 
    pIpAddress->ip.numocts = 4;
    pIpAddress->port = pGkClient->localRASPort;
@@ -1010,7 +1010,7 @@ int ooGkClientSendRRQ(ooGkClient *pGkClient, ASN1BOOL keepAlive)
 
    pRegReq->m.terminalAliasPresent=TRUE;
    if(OO_OK != ooPopulateAliasList(pctxt, gH323ep.aliases, 
-                                     &pRegReq->terminalAlias)) {
+                                     &pRegReq->terminalAlias, 0)) {
      OOTRACEERR1("Error filling alias for RRQ\n");
      memReset(pctxt); 
      pGkClient->state = GkClientFailed;
@@ -1018,21 +1018,23 @@ int ooGkClientSendRRQ(ooGkClient *pGkClient, ASN1BOOL keepAlive)
      return OO_FAILED;
    }
    
-   pRegReq->m.gatekeeperIdentifierPresent=TRUE;
-   pRegReq->gatekeeperIdentifier.nchars = pGkClient->gkId.nchars;
-   pRegReq->gatekeeperIdentifier.data = (ASN116BITCHAR*)memAlloc
+   if (pGkClient->gkId.nchars) {
+    pRegReq->m.gatekeeperIdentifierPresent=TRUE;
+    pRegReq->gatekeeperIdentifier.nchars = pGkClient->gkId.nchars;
+    pRegReq->gatekeeperIdentifier.data = (ASN116BITCHAR*)memAlloc
                          (pctxt, pGkClient->gkId.nchars*sizeof(ASN116BITCHAR));
-   if(!pRegReq->gatekeeperIdentifier.data)
-   {
+    if(!pRegReq->gatekeeperIdentifier.data)
+    {
       OOTRACEERR1("Error: Failed to allocate memory for GKIdentifier in RRQ "
                    "message.\n");
       memReset(pctxt);
       pGkClient->state = GkClientFailed;
       ast_mutex_unlock(&pGkClient->Lock);
       return OO_FAILED;
-   }
-   memcpy(pRegReq->gatekeeperIdentifier.data, pGkClient->gkId.data, 
+    }
+    memcpy(pRegReq->gatekeeperIdentifier.data, pGkClient->gkId.data, 
                                 pGkClient->gkId.nchars*sizeof(ASN116BITCHAR));
+   }
    
    ooGkClientFillVendor(pGkClient, &pRegReq->endpointVendor);
    
@@ -1149,7 +1151,26 @@ int ooGkClientHandleRegistrationConfirm
    memcpy(pGkClient->endpointId.data, 
           pRegistrationConfirm->endpointIdentifier.data,
           sizeof(ASN116BITCHAR)*pGkClient->endpointId.nchars);
+
+   /* Extract GK Identifier */
    
+   if(pRegistrationConfirm->m.gatekeeperIdentifierPresent && pGkClient->gkId.nchars == 0)
+   {
+      pGkClient->gkId.nchars = pRegistrationConfirm->gatekeeperIdentifier.nchars;
+      pGkClient->gkId.data = (ASN116BITCHAR*)memAlloc(&pGkClient->ctxt,
+                              sizeof(ASN116BITCHAR)*pGkClient->gkId.nchars);
+      if(!pGkClient->gkId.data)
+      {
+         OOTRACEERR1("Error:Failed to allocate memory for GK ID data\n");
+         pGkClient->state = GkClientFailed;
+         return OO_FAILED;
+      }
+
+      memcpy(pGkClient->gkId.data, 
+             pRegistrationConfirm->gatekeeperIdentifier.data,
+             sizeof(ASN116BITCHAR)* pGkClient->gkId.nchars);
+   }
+
    /* Extract CallSignalling Address */
    for(i=0; i<(int)pRegistrationConfirm->callSignalAddress.count; i++)
    {
@@ -1190,8 +1211,11 @@ int ooGkClientHandleRegistrationConfirm
 
       if(pGkClient->regTimeout > DEFAULT_TTL_OFFSET)
          regTTL = pGkClient->regTimeout - DEFAULT_TTL_OFFSET;
-      else
-         regTTL = pGkClient->regTimeout;
+      else {
+         regTTL = pGkClient->regTimeout - 1; /* -1 due to some ops expire us few earlier */
+        if (regTTL <= 0)
+               regTTL = 1;
+      }
 
       cbData = (ooGkClientTimerCb*) memAlloc
                                 (&pGkClient->ctxt, sizeof(ooGkClientTimerCb));
@@ -1239,7 +1263,6 @@ int ooGkClientHandleRegistrationConfirm
          memFreePtr(&pGkClient->ctxt, pTimer->cbData);
          ooTimerDelete(&pGkClient->ctxt, &pGkClient->timerList, pTimer);
          OOTRACEDBGA1("Deleted RRQ Timer.\n");
-         break;
       }
    }
    pGkClient->state = GkClientRegistered;
@@ -1410,7 +1433,7 @@ int ooGkClientSendURQ(ooGkClient *pGkClient, ooAliases *aliases)
    }
    pTransportAddress->t = T_H225TransportAddress_ipAddress;
    pTransportAddress->u.ipAddress = pIpAddress;
-   ooSocketConvertIpToNwAddr(pGkClient->localRASIP, pIpAddress->ip.data);
+   inet_pton(AF_INET, pGkClient->localRASIP, pIpAddress->ip.data);
    pIpAddress->ip.numocts = 4;
    pIpAddress->port = gH323ep.listenPort;
    
@@ -1437,27 +1460,29 @@ int ooGkClientSendURQ(ooGkClient *pGkClient, ooAliases *aliases)
           sizeof(ASN116BITCHAR)*pGkClient->endpointId.nchars);
 
    /* Populate gatekeeper identifier */
-   pUnregReq->m.gatekeeperIdentifierPresent = TRUE;
-   pUnregReq->gatekeeperIdentifier.nchars = pGkClient->gkId.nchars;
-   pUnregReq->gatekeeperIdentifier.data = (ASN116BITCHAR*)memAlloc(pctxt,
+   if (pGkClient->gkId.nchars) {
+    pUnregReq->m.gatekeeperIdentifierPresent = TRUE;
+    pUnregReq->gatekeeperIdentifier.nchars = pGkClient->gkId.nchars;
+    pUnregReq->gatekeeperIdentifier.data = (ASN116BITCHAR*)memAlloc(pctxt,
                                  sizeof(ASN116BITCHAR)*pGkClient->gkId.nchars);
-   if(!pUnregReq->gatekeeperIdentifier.data)
-   {
+    if(!pUnregReq->gatekeeperIdentifier.data)
+    {
       OOTRACEERR1("Error:Failed to allocate memory for GKID of URQ message\n");
       memReset(pctxt);
       pGkClient->state = GkClientFailed;
       ast_mutex_unlock(&pGkClient->Lock);
       return OO_FAILED;
-   }
-   memcpy((void*)pUnregReq->gatekeeperIdentifier.data, 
+    }
+    memcpy((void*)pUnregReq->gatekeeperIdentifier.data, 
           (void*)pGkClient->gkId.data, 
           sizeof(ASN116BITCHAR)*pGkClient->gkId.nchars);   
+   }
 
    /* Check whether specific aliases are to be unregistered*/
    if(aliases)
    {
       pUnregReq->m.endpointAliasPresent = TRUE;
-      ooPopulateAliasList(pctxt, aliases, &pUnregReq->endpointAlias);
+      ooPopulateAliasList(pctxt, aliases, &pUnregReq->endpointAlias, 0);
    }
 
   
@@ -1482,8 +1507,10 @@ int ooGkClientSendURQ(ooGkClient *pGkClient, ooAliases *aliases)
 int ooGkClientHandleUnregistrationRequest
    (ooGkClient *pGkClient, H225UnregistrationRequest * punregistrationRequest)
 {
-   int iRet=0;
-
+   int iRet=0, x;
+   OOTimer *pTimer = NULL;
+   DListNode *pNode = NULL;
    /* Lets first send unregistration confirm message back to gatekeeper*/
    ooGkClientSendUnregistrationConfirm(pGkClient, 
                                       punregistrationRequest->requestSeqNum);
@@ -1505,6 +1532,24 @@ int ooGkClientHandleUnregistrationRequest
       pGkClient->rrqRetries = 0;
       pGkClient->state = GkClientDiscovered;
 
+
+      /* delete the corresponding RRQ & REG timers */
+       pNode = NULL;
+       for(x=0; x<pGkClient->timerList.count; x++) {
+               pNode =  dListFindByIndex(&pGkClient->timerList, x);
+               pTimer = (OOTimer*)pNode->data;
+               if(((ooGkClientTimerCb*)pTimer->cbData)->timerType & OO_RRQ_TIMER) {
+                       memFreePtr(&pGkClient->ctxt, pTimer->cbData);
+                       ooTimerDelete(&pGkClient->ctxt, &pGkClient->timerList, pTimer);
+                       OOTRACEDBGA1("Deleted RRQ Timer.\n");
+               }
+               if(((ooGkClientTimerCb*)pTimer->cbData)->timerType & OO_REG_TIMER) {
+                       memFreePtr(&pGkClient->ctxt, pTimer->cbData);
+                       ooTimerDelete(&pGkClient->ctxt, &pGkClient->timerList, pTimer);
+                       OOTRACEDBGA1("Deleted REG Timer.\n");
+               }
+       }
+
       iRet = ooGkClientSendRRQ(pGkClient, 0); 
       if(iRet != OO_OK)
       {
@@ -1623,14 +1668,14 @@ int ooGkClientSendAdmissionRequest
       ast_mutex_unlock(&pGkClient->Lock);
       return OO_FAILED;
    }
-   ooSocketConvertIpToNwAddr(pGkClient->localRASIP, pIpAddressLocal->ip.data);
+   inet_pton(AF_INET, pGkClient->localRASIP, pIpAddressLocal->ip.data);
 
    pIpAddressLocal->ip.numocts = 4;
    pIpAddressLocal->port = gH323ep.listenPort;
 
    if(!ooUtilsIsStrEmpty(call->remoteIP))
    {
-      ooSocketConvertIpToNwAddr(call->remoteIP, pIpAddressRemote->ip.data);
+      inet_pton(AF_INET, call->remoteIP, pIpAddressRemote->ip.data);
       pIpAddressRemote->ip.numocts = 4;
       pIpAddressRemote->port = call->remotePort;
    }
@@ -1716,7 +1761,7 @@ int ooGkClientSendAdmissionRequest
    {
       pAdmReq->m.destinationInfoPresent = 1;
       if(OO_OK != ooPopulateAliasList(&pGkClient->msgCtxt, destAliases,
-                                      &pAdmReq->destinationInfo))
+                      &pAdmReq->destinationInfo, T_H225AliasAddress_dialedDigits))
       {
          OOTRACEERR1("Error:Failed to populate destination aliases - "
                     "ARQ message\n");
@@ -1731,7 +1776,7 @@ int ooGkClientSendAdmissionRequest
    if(srcAliases)
    {
       iRet = ooPopulateAliasList(&pGkClient->msgCtxt, srcAliases,
-                                                          &pAdmReq->srcInfo);
+                              &pAdmReq->srcInfo, 0);
       if(OO_OK != iRet)
       {
          OOTRACEERR1("Error:Failed to populate source aliases -ARQ message\n");
@@ -1766,21 +1811,23 @@ int ooGkClientSendAdmissionRequest
                                              sizeof(H225CallIdentifier));
 
    /* Populate Gatekeeper Id */
-   pAdmReq->m.gatekeeperIdentifierPresent = TRUE;
-   pAdmReq->gatekeeperIdentifier.nchars = pGkClient->gkId.nchars;
-   pAdmReq->gatekeeperIdentifier.data = (ASN116BITCHAR*)memAlloc(pctxt,
+   if (pGkClient->gkId.nchars) {
+    pAdmReq->m.gatekeeperIdentifierPresent = TRUE;
+    pAdmReq->gatekeeperIdentifier.nchars = pGkClient->gkId.nchars;
+    pAdmReq->gatekeeperIdentifier.data = (ASN116BITCHAR*)memAlloc(pctxt,
                                  sizeof(ASN116BITCHAR)*pGkClient->gkId.nchars);
-   if(!pAdmReq->gatekeeperIdentifier.data)
-   {
+    if(!pAdmReq->gatekeeperIdentifier.data)
+    {
       OOTRACEERR1("Error:Failed to allocate memory for GKID of ARQ message\n");
       memReset(pctxt);
       pGkClient->state = GkClientFailed;
       ast_mutex_unlock(&pGkClient->Lock);
       return OO_FAILED;
-   }
-   memcpy((void*)pAdmReq->gatekeeperIdentifier.data, 
+    }
+    memcpy((void*)pAdmReq->gatekeeperIdentifier.data, 
           (void*)pGkClient->gkId.data, 
           sizeof(ASN116BITCHAR)*pGkClient->gkId.nchars);
+   }
 
    pAdmReq->m.willSupplyUUIEsPresent = 1;
    pAdmReq->willSupplyUUIEs = FALSE;
@@ -2027,7 +2074,7 @@ int ooGkClientHandleAdmissionReject
                 "(%s, %s)\n", pAdmissionReject->rejectReason.t, call->callType,
                  call->callToken);
    
-   call->callState = OO_CALL_CLEAR;
+   call->callState = OO_CALL_CLEARED;
 
    switch(pAdmissionReject->rejectReason.t)
    {
@@ -2129,7 +2176,7 @@ int ooGkClientSendIRR
       ast_mutex_unlock(&pGkClient->Lock);
       return OO_FAILED;
    }
-   ooSocketConvertIpToNwAddr(pGkClient->localRASIP, pIpAddressLocal->ip.data);
+   inet_pton(AF_INET, pGkClient->localRASIP, pIpAddressLocal->ip.data);
 
    pIpAddressLocal->ip.numocts = 4;
    pIpAddressLocal->port = gH323ep.listenPort;
@@ -2160,7 +2207,7 @@ int ooGkClientSendIRR
 
    pIpRasAddress->ip.numocts = 4;
    pIpRasAddress->port = pGkClient->localRASPort;
-   ooSocketConvertIpToNwAddr(pGkClient->localRASIP, pIpRasAddress->ip.data);
+   inet_pton(AF_INET, pGkClient->localRASIP, pIpRasAddress->ip.data);
 
    pIRR->rasAddress.u.ipAddress = pIpRasAddress;
    pIRR->rasAddress.t=T_H225TransportAddress_ipAddress; /* IPv4 address */
@@ -2204,10 +2251,10 @@ int ooGkClientSendIRR
    if(srcAliases)
    {
       iRet = ooPopulateAliasList(&pGkClient->msgCtxt, srcAliases,
-                                                          &pIRR->endpointAlias);
+                             &pIRR->endpointAlias, T_H225AliasAddress_h323_ID);
       if(OO_OK != iRet)
       {
-         OOTRACEERR1("Error:Failed to populate source aliases -ARQ message\n");
+         OOTRACEERR1("Error:Failed to populate source aliases -IRR message\n");
          memReset(pctxt);
          pGkClient->state = GkClientFailed;
         ast_mutex_unlock(&pGkClient->Lock);
@@ -2259,11 +2306,11 @@ int ooGkClientSendIRR
       return OO_FAILED;
    }
    pLocalAddr->ip.numocts = 4;
-   ooSocketConvertIpToNwAddr(call->localIP, pLocalAddr->ip.data);
+   inet_pton(AF_INET, call->localIP, pLocalAddr->ip.data);
    pLocalAddr->port = (call->pH225Channel->port) ? call->pH225Channel->port : gH323ep.listenPort;
 
    pRemoteAddr->ip.numocts = 4;
-   ooSocketConvertIpToNwAddr(call->remoteIP, pRemoteAddr->ip.data);
+   inet_pton(AF_INET, call->remoteIP, pRemoteAddr->ip.data);
    pRemoteAddr->port = call->remotePort;
 
    perCallInfo->callSignaling.m.sendAddressPresent = TRUE;
@@ -2416,20 +2463,22 @@ int ooGkClientSendDisengageRequest(ooGkClient *pGkClient, OOH323CallData *call)
    pDRQ->m.callIdentifierPresent = 1;
    memcpy((void*)&pDRQ->callIdentifier, (void*)&call->callIdentifier,
                                              sizeof(H225CallIdentifier));
-   pDRQ->m.gatekeeperIdentifierPresent = 1;
-   pDRQ->gatekeeperIdentifier.nchars = pGkClient->gkId.nchars;
-   pDRQ->gatekeeperIdentifier.data = (ASN116BITCHAR*)memAlloc
+   if (pGkClient->gkId.nchars) {
+    pDRQ->m.gatekeeperIdentifierPresent = 1;
+    pDRQ->gatekeeperIdentifier.nchars = pGkClient->gkId.nchars;
+    pDRQ->gatekeeperIdentifier.data = (ASN116BITCHAR*)memAlloc
                        (pctxt, pGkClient->gkId.nchars*sizeof(ASN116BITCHAR));
-   if(!pDRQ->gatekeeperIdentifier.data)
-   {
+    if(!pDRQ->gatekeeperIdentifier.data)
+    {
       OOTRACEERR1("Error:Failed to allocate memory for GKId in DRQ.\n");
       memReset(pctxt);
       pGkClient->state = GkClientFailed;
       ast_mutex_unlock(&pGkClient->Lock);
       return OO_FAILED;
-   }
-   memcpy(pDRQ->gatekeeperIdentifier.data, pGkClient->gkId.data, 
+    }
+    memcpy(pDRQ->gatekeeperIdentifier.data, pGkClient->gkId.data, 
                                 pGkClient->gkId.nchars*sizeof(ASN116BITCHAR));
+   }
 
    pDRQ->m.terminationCausePresent = 1;
    pDRQ->terminationCause.t = T_H225CallTerminationCause_releaseCompleteCauseIE;