Wed Oct 28 15:48:22 2009

Asterisk developer's documentation


astosp.h File Reference

OSP support (Open Settlement Protocol). More...

#include "asterisk/channel.h"
#include <netinet/in.h>
#include <time.h>

Include dependency graph for astosp.h:

This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  ast_osp_result

Functions

int ast_osp_lookup (struct ast_channel *chan, char *provider, char *extension, char *callerid, struct ast_osp_result *result)
int ast_osp_next (struct ast_osp_result *result, int cause)
int ast_osp_terminate (int handle, int cause, time_t start, time_t duration)
int ast_osp_validate (char *provider, char *token, int *handle, unsigned int *timeout, char *callerid, struct in_addr addr, char *extension)


Detailed Description

OSP support (Open Settlement Protocol).

Definition in file astosp.h.


Function Documentation

int ast_osp_lookup ( struct ast_channel chan,
char *  provider,
char *  extension,
char *  callerid,
struct ast_osp_result result 
)

Definition at line 517 of file res_osp.c.

References ast_autoservice_start(), ast_autoservice_stop(), ast_base64encode(), ast_callerid_parse(), ast_channel_cmpwhentohangup(), ast_channel_setwhentohangup(), ast_isphonenumber(), ast_log(), ast_mutex_lock(), ast_mutex_unlock(), ast_shrink_phone_number(), ast_osp_result::dest, osp_provider::handle, ast_osp_result::handle, LOG_DEBUG, LOG_NOTICE, LOG_WARNING, n, osp_provider::name, osp_provider::next, ast_osp_result::numresults, OSP_MAX, pbx_builtin_getvar_helper(), osp_provider::source, ast_osp_result::tech, and ast_osp_result::token.

Referenced by osplookup_exec().

00518 {
00519    int cres;
00520    int res = 0;
00521    int counts;
00522    int tokenlen;
00523    unsigned int dummy=0;
00524    unsigned int timelimit;
00525    unsigned int callidlen;
00526    char callidstr[OSPC_CALLID_MAXSIZE] = "";
00527    struct osp_provider *osp;
00528    char source[OSP_MAX] = ""; /* Same length as osp->source */
00529    char callednum[2048]="";
00530    char callingnum[2048]="";
00531    char destination[2048]="";
00532    char token[2000];
00533    char tmp[256]="", *l, *n;
00534    OSPE_DEST_PROT prot;
00535    OSPE_DEST_OSP_ENABLED ospenabled;
00536    char *devinfo = NULL;
00537 
00538    result->handle = -1;
00539    result->numresults = 0;
00540    result->tech[0] = '\0';
00541    result->dest[0] = '\0';
00542    result->token[0] = '\0';
00543 
00544    if (!provider || !strlen(provider))
00545       provider = "default";
00546 
00547    if (!callerid)
00548       callerid = "";
00549    ast_copy_string(tmp, callerid, sizeof(tmp));
00550    ast_callerid_parse(tmp, &n, &l);
00551    if (!l)
00552       l = "";
00553    else {
00554       ast_shrink_phone_number(l);
00555       if (!ast_isphonenumber(l))
00556          l = "";
00557    }
00558    callerid = l;
00559 
00560    if (chan) {
00561       cres = ast_autoservice_start(chan);
00562       if (cres < 0)
00563          return cres;
00564    }
00565    ast_mutex_lock(&osplock);
00566    osp = providers;
00567    while(osp) {
00568       if (!strcasecmp(osp->name, provider)) {
00569          if (OSPPTransactionNew(osp->handle, &result->handle)) {
00570             ast_log(LOG_WARNING, "Unable to create OSP Transaction handle!\n");
00571          } else {
00572             ast_copy_string(source, osp->source, sizeof(source));
00573             res = 1;
00574          }
00575          break;
00576       }
00577       osp = osp->next;
00578    }
00579    ast_mutex_unlock(&osplock);
00580    if (res) {
00581       res = 0;
00582       /* No more than 10 back */
00583       counts = 10;
00584       dummy = 0;
00585       devinfo = pbx_builtin_getvar_helper (chan, "OSPPEER");
00586       if (!devinfo) {
00587          devinfo = "";
00588       }
00589       if (!OSPPTransactionRequestAuthorisation(result->handle, source, devinfo, 
00590            callerid,OSPC_E164, extension, OSPC_E164, NULL, 0, NULL, NULL, &counts, &dummy, NULL)) {
00591          if (counts) {
00592             tokenlen = sizeof(token);
00593             result->numresults = counts - 1;
00594             callidlen = sizeof(callidstr);
00595             if (!OSPPTransactionGetFirstDestination(result->handle, 0, NULL, NULL, &timelimit, &callidlen, callidstr, 
00596                sizeof(callednum), callednum, sizeof(callingnum), callingnum, sizeof(destination), destination, 0, NULL, &tokenlen, token)) {
00597                ast_log(LOG_DEBUG, "Got destination '%s' and called: '%s' calling: '%s' for '%s' (provider '%s')\n",
00598                   destination, callednum, callingnum, extension, provider);
00599                /* Only support OSP server with only one duration limit */
00600                if (ast_channel_cmpwhentohangup (chan, timelimit) < 0) {
00601                   ast_channel_setwhentohangup (chan, timelimit);  
00602                }
00603                do {
00604                   if (!OSPPTransactionIsDestOSPEnabled (result->handle, &ospenabled) && (ospenabled == OSPE_OSP_FALSE)) {
00605                      result->token[0] = 0;
00606                   }
00607                   else {
00608                      ast_base64encode(result->token, token, tokenlen, sizeof(result->token) - 1);
00609                   }
00610                   if ((strlen(destination) > 2) && !OSPPTransactionGetDestProtocol(result->handle, &prot)) {
00611                      res = 1;
00612                      /* Strip leading and trailing brackets */
00613                      destination[strlen(destination) - 1] = '\0';
00614                      switch(prot) {
00615                      case OSPE_DEST_PROT_H323_SETUP:
00616                         ast_copy_string(result->tech, "H323", sizeof(result->tech));
00617                         snprintf(result->dest, sizeof(result->dest), "%s@%s", callednum, destination + 1);
00618                         break;
00619                      case OSPE_DEST_PROT_SIP:
00620                         ast_copy_string(result->tech, "SIP", sizeof(result->tech));
00621                         snprintf(result->dest, sizeof(result->dest), "%s@%s", callednum, destination + 1);
00622                         break;
00623                      case OSPE_DEST_PROT_IAX:
00624                         ast_copy_string(result->tech, "IAX", sizeof(result->tech));
00625                         snprintf(result->dest, sizeof(result->dest), "%s@%s", callednum, destination + 1);
00626                         break;
00627                      default:
00628                         ast_log(LOG_DEBUG, "Unknown destination protocol '%d', skipping...\n", prot);
00629                         res = 0;
00630                      }
00631                      if (!res && result->numresults) {
00632                         result->numresults--;
00633                         callidlen = sizeof(callidstr);
00634                         if (OSPPTransactionGetNextDestination(result->handle, OSPC_FAIL_INCOMPATIBLE_DEST, 0, NULL, NULL, &timelimit, &callidlen, callidstr, 
00635                               sizeof(callednum), callednum, sizeof(callingnum), callingnum, sizeof(destination), destination, 0, NULL, &tokenlen, token)) {
00636                               break;
00637                         }
00638                      }
00639                   } else {
00640                      ast_log(LOG_DEBUG, "Missing destination protocol\n");
00641                      break;
00642                   }
00643                } while(!res && result->numresults);
00644             }
00645          }
00646          
00647       }
00648       if (!res) {
00649          OSPPTransactionDelete(result->handle);
00650          result->handle = -1;
00651       }
00652       
00653    }
00654    if (!osp) 
00655       ast_log(LOG_NOTICE, "OSP Provider '%s' does not exist!\n", provider);
00656    if (chan) {
00657       cres = ast_autoservice_stop(chan);
00658       if (cres < 0)
00659          return cres;
00660    }
00661    return res;
00662 }

int ast_osp_next ( struct ast_osp_result result,
int  cause 
)

Definition at line 664 of file res_osp.c.

References ast_base64encode(), ast_log(), ast_osp_result::dest, ast_osp_result::handle, LOG_DEBUG, ast_osp_result::numresults, ast_osp_result::tech, and ast_osp_result::token.

Referenced by ospnext_exec().

00665 {
00666    int res = 0;
00667    int tokenlen;
00668    unsigned int dummy=0;
00669    unsigned int timelimit;
00670    unsigned int callidlen;
00671    char callidstr[OSPC_CALLID_MAXSIZE] = "";
00672    char callednum[2048]="";
00673    char callingnum[2048]="";
00674    char destination[2048]="";
00675    char token[2000];
00676    OSPE_DEST_PROT prot;
00677    OSPE_DEST_OSP_ENABLED ospenabled;
00678 
00679    result->tech[0] = '\0';
00680    result->dest[0] = '\0';
00681    result->token[0] = '\0';
00682 
00683    if (result->handle > -1) {
00684       dummy = 0;
00685       if (result->numresults) {
00686          tokenlen = sizeof(token);
00687          while(!res && result->numresults) {
00688             result->numresults--;
00689             callidlen = sizeof(callidstr);
00690             if (!OSPPTransactionGetNextDestination(result->handle, OSPC_FAIL_INCOMPATIBLE_DEST, 0, NULL, NULL, &timelimit, &callidlen, callidstr, 
00691                            sizeof(callednum), callednum, sizeof(callingnum), callingnum, sizeof(destination), destination, 0, NULL, &tokenlen, token)) {
00692                if (!OSPPTransactionIsDestOSPEnabled (result->handle, &ospenabled) && (ospenabled == OSPE_OSP_FALSE)) {
00693                   result->token[0] = 0;
00694                }
00695                else {
00696                   ast_base64encode(result->token, token, tokenlen, sizeof(result->token) - 1);
00697                }
00698                if ((strlen(destination) > 2) && !OSPPTransactionGetDestProtocol(result->handle, &prot)) {
00699                   res = 1;
00700                   /* Strip leading and trailing brackets */
00701                   destination[strlen(destination) - 1] = '\0';
00702                   switch(prot) {
00703                   case OSPE_DEST_PROT_H323_SETUP:
00704                      ast_copy_string(result->tech, "H323", sizeof(result->tech));
00705                      snprintf(result->dest, sizeof(result->dest), "%s@%s", callednum, destination + 1);
00706                      break;
00707                   case OSPE_DEST_PROT_SIP:
00708                      ast_copy_string(result->tech, "SIP", sizeof(result->tech));
00709                      snprintf(result->dest, sizeof(result->dest), "%s@%s", callednum, destination + 1);
00710                      break;
00711                   case OSPE_DEST_PROT_IAX:
00712                      ast_copy_string(result->tech, "IAX", sizeof(result->tech));
00713                      snprintf(result->dest, sizeof(result->dest), "%s@%s", callednum, destination + 1);
00714                      break;
00715                   default:
00716                      ast_log(LOG_DEBUG, "Unknown destination protocol '%d', skipping...\n", prot);
00717                      res = 0;
00718                   }
00719                } else {
00720                   ast_log(LOG_DEBUG, "Missing destination protocol\n");
00721                   break;
00722                }
00723             }
00724          }
00725          
00726       }
00727       if (!res) {
00728          OSPPTransactionDelete(result->handle);
00729          result->handle = -1;
00730       }
00731       
00732    }
00733    return res;
00734 }

int ast_osp_terminate ( int  handle,
int  cause,
time_t  start,
time_t  duration 
)

Definition at line 755 of file res_osp.c.

References ast_log(), cause2reason(), LOG_DEBUG, and LOG_WARNING.

Referenced by ospfinished_exec(), and sip_hangup().

00756 {
00757    unsigned int dummy = 0;
00758    int res = -1;
00759    enum OSPEFAILREASON reason;
00760 
00761    time_t endTime = 0;
00762    time_t alertTime = 0;
00763    time_t connectTime = 0;
00764    unsigned isPddInfoPresent = 0;
00765    unsigned pdd = 0;
00766    unsigned releaseSource = 0;
00767    unsigned char *confId = "";
00768    
00769    reason = cause2reason(cause);
00770    if (OSPPTransactionRecordFailure(handle, reason))
00771       ast_log(LOG_WARNING, "Failed to record call termination for handle %d\n", handle);
00772    else if (OSPPTransactionReportUsage(handle, duration, start,
00773                 endTime,alertTime,connectTime,isPddInfoPresent,pdd,releaseSource,confId,
00774                       0, 0, 0, 0, &dummy, NULL))
00775       ast_log(LOG_WARNING, "Failed to report duration for handle %d\n", handle);
00776    else {
00777       ast_log(LOG_DEBUG, "Completed recording handle %d\n", handle);
00778       OSPPTransactionDelete(handle);
00779       res = 0;
00780    }
00781    return res;
00782 }

int ast_osp_validate ( char *  provider,
char *  token,
int *  handle,
unsigned int *  timeout,
char *  callerid,
struct in_addr  addr,
char *  extension 
)

Definition at line 456 of file res_osp.c.

References ast_base64decode(), ast_callerid_parse(), ast_inet_ntoa(), ast_isphonenumber(), ast_log(), ast_mutex_lock(), ast_mutex_unlock(), ast_shrink_phone_number(), ast_strdupa, osp_provider::handle, LOG_DEBUG, LOG_WARNING, n, osp_provider::name, osp_provider::next, OSP_MAX, and osp_provider::source.

00457 {
00458    char tmp[256]="", *l, *n;
00459    char iabuf[INET_ADDRSTRLEN];
00460    char source[OSP_MAX] = ""; /* Same length as osp->source */
00461    char *token2;
00462    int tokenlen;
00463    struct osp_provider *osp;
00464    int res = 0;
00465    unsigned int authorised, dummy;
00466 
00467    if (!provider || !strlen(provider))
00468       provider = "default";
00469 
00470    token2 = ast_strdupa(token);
00471    if (!token2)
00472       return -1;
00473    tokenlen = ast_base64decode(token2, token, strlen(token));
00474    *handle = -1;
00475    if (!callerid)
00476       callerid = "";
00477    ast_copy_string(tmp, callerid, sizeof(tmp));
00478    ast_callerid_parse(tmp, &n, &l);
00479    if (!l)
00480       l = "";
00481    else {
00482       ast_shrink_phone_number(l);
00483       if (!ast_isphonenumber(l))
00484          l = "";
00485    }
00486    callerid = l;
00487    ast_mutex_lock(&osplock);
00488    ast_inet_ntoa(iabuf, sizeof(iabuf), addr);
00489    osp = providers;
00490    while(osp) {
00491       if (!strcasecmp(osp->name, provider)) {
00492          if (OSPPTransactionNew(osp->handle, handle)) {
00493             ast_log(LOG_WARNING, "Unable to create OSP Transaction handle!\n");
00494          } else {
00495             ast_copy_string(source, osp->source, sizeof(source));
00496             res = 1;
00497          }
00498          break;
00499       }
00500       osp = osp->next;
00501    }
00502    ast_mutex_unlock(&osplock);
00503    if (res) {
00504       res = 0;
00505       dummy = 0;
00506       if (!OSPPTransactionValidateAuthorisation(*handle, iabuf, source, NULL, NULL, 
00507          callerid, OSPC_E164, extension, OSPC_E164, 0, "", tokenlen, token2, &authorised, timelimit, &dummy, NULL, tokenformat)) {
00508          if (authorised) {
00509             ast_log(LOG_DEBUG, "Validated token for '%s' from '%s@%s'\n", extension, callerid, iabuf);
00510             res = 1;
00511          }
00512       }
00513    }
00514    return res; 
00515 }


Generated on Wed Oct 28 15:48:22 2009 for Asterisk - the Open Source PBX by  doxygen 1.5.6