Fri Jul 3 20:35:36 2009

Asterisk developer's documentation


chan_ooh323.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2004-2005 by Objective Systems, Inc.
00003  *
00004  * This software is furnished under an open source license and may be 
00005  * used and copied only in accordance with the terms of this license. 
00006  * The text of the license may generally be found in the root 
00007  * directory of this installation in the COPYING file.  It 
00008  * can also be viewed online at the following URL:
00009  *
00010  *   http://www.obj-sys.com/open/license.html
00011  *
00012  * Any redistributions of this file including modified versions must 
00013  * maintain this copyright notice.
00014  *
00015  *****************************************************************************/
00016 
00017 
00018 #include "chan_ooh323.h"
00019 
00020 /* Defaults */
00021 #define DEFAULT_CONTEXT "default"
00022 #define DEFAULT_H323ID "Asterisk PBX"
00023 #define DEFAULT_LOGFILE "/var/log/asterisk/h323_log"
00024 #define DEFAULT_H323ACCNT "ast_h323"
00025 
00026 /* Flags */
00027 #define H323_SILENCESUPPRESSION (1<<0)
00028 #define H323_GKROUTED           (1<<1)
00029 #define H323_TUNNELING          (1<<2)
00030 #define H323_FASTSTART          (1<<3)
00031 #define H323_OUTGOING           (1<<4)
00032 #define H323_ALREADYGONE        (1<<5)
00033 #define H323_NEEDDESTROY        (1<<6)
00034 #define H323_DISABLEGK          (1<<7)
00035 
00036 /* Channel description */
00037 static const char type[] = "OOH323";
00038 static const char tdesc[] = "Objective Systems H323 Channel Driver";
00039 static const char config[] = "ooh323.conf";
00040 
00041 
00042 /* Channel Definition */
00043 static struct ast_channel *ooh323_request(const char *type, int format, 
00044                                         void *data, int *cause);
00045 static int ooh323_digit_begin(struct ast_channel *ast, char digit);
00046 static int ooh323_digit_end(struct ast_channel *ast, char digit, unsigned int duration);
00047 static int ooh323_call(struct ast_channel *ast, char *dest, int timeout);
00048 static int ooh323_hangup(struct ast_channel *ast);
00049 static int ooh323_answer(struct ast_channel *ast);
00050 static struct ast_frame *ooh323_read(struct ast_channel *ast);
00051 static int ooh323_write(struct ast_channel *ast, struct ast_frame *f);
00052 static int ooh323_indicate(struct ast_channel *ast, int condition, const void *data, size_t datalen);
00053 static int ooh323_fixup
00054    (struct ast_channel *oldchan, struct ast_channel *newchan);
00055 
00056 static enum ast_rtp_get_result ooh323_get_rtp_peer(struct ast_channel *chan, struct ast_rtp **rtp);
00057 static enum ast_rtp_get_result ooh323_get_vrtp_peer(struct ast_channel *chan, struct ast_rtp **rtp);
00058 static int ooh323_set_rtp_peer(struct ast_channel *chan, struct ast_rtp *rtp, 
00059                              struct ast_rtp *vrtp, int codecs, int nat_active);
00060 
00061 static void print_codec_to_cli(int fd, struct ast_codec_pref *pref);
00062 static void ast_ooh323c_exit();
00063 
00064 
00065 static const struct ast_channel_tech ooh323_tech = {
00066    .type = type,
00067    .description = tdesc,
00068    .capabilities = -1,
00069    .requester = ooh323_request,
00070    .send_digit_begin = ooh323_digit_begin,
00071    .send_digit_end = ooh323_digit_end,
00072    .call = ooh323_call,
00073    .hangup = ooh323_hangup,
00074    .answer = ooh323_answer,
00075    .read = ooh323_read,
00076    .write = ooh323_write,
00077    .exception = ooh323_read,
00078    .indicate = ooh323_indicate,
00079    .fixup = ooh323_fixup,
00080    .send_html = 0,
00081    .bridge = ast_rtp_bridge,
00082 };
00083 
00084 static struct ast_rtp_protocol ooh323_rtp = {
00085    .type = type,
00086    .get_rtp_info = ooh323_get_rtp_peer,
00087    .get_vrtp_info = ooh323_get_vrtp_peer,
00088    .set_rtp_peer = ooh323_set_rtp_peer
00089 };
00090 
00091 /* H.323 channel private structure */
00092 static struct ooh323_pvt {
00093    ast_mutex_t lock;    /* Channel private lock */
00094    struct ast_rtp *rtp;
00095    struct ast_rtp *vrtp; /* Placeholder for now */
00096    struct ast_channel *owner; /* Master Channel */
00097    time_t lastrtptx;
00098    time_t lastrtprx;
00099    unsigned int flags;
00100    unsigned int call_reference;
00101    char *callToken;
00102    char *username;
00103    char *host;
00104    char *callerid_name;
00105    char *callerid_num;
00106    char caller_h323id[AST_MAX_EXTENSION];
00107    char caller_dialedDigits[AST_MAX_EXTENSION];
00108    char caller_email[AST_MAX_EXTENSION];
00109    char caller_url[256];
00110    char callee_h323id[AST_MAX_EXTENSION];
00111    char callee_dialedDigits[AST_MAX_EXTENSION];
00112    char callee_email[AST_MAX_EXTENSION];
00113    char callee_url[AST_MAX_EXTENSION];
00114  
00115    int port;
00116    int readformat;    /* negotiated read format */
00117    int writeformat;  /* negotiated write format */
00118    int capability;
00119    struct ast_codec_pref prefs;
00120    int dtmfmode;
00121    char exten[AST_MAX_EXTENSION];   /* Requested extension */
00122    char context[AST_MAX_EXTENSION]; /* Context where to start */
00123    char accountcode[256];  /* Account code */
00124    int nat;
00125    int amaflags;
00126    struct ast_dsp *vad;
00127    struct ooh323_pvt *next;   /* Next entity */
00128 } *iflist = NULL;
00129 
00130 /* Protect the channel/interface list (ooh323_pvt) */
00131 AST_MUTEX_DEFINE_STATIC(iflock);
00132 
00133 /* Profile of H.323 user registered with PBX*/
00134 struct ooh323_user{
00135    ast_mutex_t lock;
00136    char        name[256];
00137    char        context[AST_MAX_EXTENSION];
00138    int         incominglimit;
00139    unsigned    inUse;
00140    char        accountcode[20];
00141    int         amaflags;
00142    int         capability;
00143    struct ast_codec_pref prefs;
00144    int         dtmfmode;
00145    int         rtptimeout;
00146    int         mUseIP;        /* Use IP address or H323-ID to search user */
00147    char        mIP[20];
00148    struct ooh323_user *next;
00149 };
00150 
00151 /* Profile of valid asterisk peers */
00152 struct ooh323_peer{
00153    ast_mutex_t lock;
00154    char        name[256];
00155    unsigned    outgoinglimit;
00156    unsigned    outUse;
00157    int         capability;
00158    struct ast_codec_pref prefs;
00159    char        accountcode[20];
00160    int         amaflags;
00161    int         dtmfmode;
00162    int         mFriend;    /* indicates defined as friend */
00163    char        ip[20];
00164    int         port;
00165    char        *h323id;    /* H323-ID alias, which asterisk will register with gk to reach this peer*/
00166    char        *email;     /* Email alias, which asterisk will register with gk to reach this peer*/
00167    char        *url;       /* url alias, which asterisk will register with gk to reach this peer*/
00168    char        *e164;      /* e164 alias, which asterisk will register with gk to reach this peer*/
00169    int         rtptimeout;
00170    struct ooh323_peer *next;
00171 };
00172 
00173 
00174 /* List of H.323 users known to PBX */
00175 static struct ast_user_list {
00176    struct ooh323_user *users;
00177    ast_mutex_t lock;
00178 } userl;
00179 
00180 static struct ast_peer_list {
00181    struct ooh323_peer *peers;
00182   ast_mutex_t lock;
00183 }peerl;
00184 
00185 /* Mutex to protect H.323 reload process */
00186 static int h323_reloading = 0;
00187 AST_MUTEX_DEFINE_STATIC(h323_reload_lock);
00188 
00189 /* Mutex to protect usage counter */
00190 static int usecnt =0;
00191 AST_MUTEX_DEFINE_STATIC(usecnt_lock);
00192 
00193 AST_MUTEX_DEFINE_STATIC(ooh323c_cmd_lock);
00194 
00195 /* stack callbacks */
00196 int onAlerting(ooCallData *call);
00197 int onNewCallCreated(ooCallData *call);
00198 int onCallEstablished(ooCallData *call);
00199 int onCallCleared(ooCallData *call);
00200 
00201 static char gLogFile[256] = DEFAULT_LOGFILE;
00202 static int  gPort = 1720;
00203 static char gIP[20];
00204 static char gCallerID[AST_MAX_EXTENSION] = DEFAULT_H323ID;
00205 static struct ooAliases *gAliasList;
00206 static int  gCapability = AST_FORMAT_ULAW;
00207 static struct ast_codec_pref gPrefs;
00208 static int  gDTMFMode = H323_DTMF_RFC2833;
00209 static char gGatekeeper[100];
00210 static enum RasGatekeeperMode gRasGkMode = RasNoGatekeeper;
00211 
00212 static int  gIsGateway = 0;
00213 static int  gFastStart = 1;
00214 static int  gTunneling = 1;
00215 static int  gMediaWaitForConnect = 0;
00216 static int  gTOS = 0;
00217 static int  gRTPTimeout = 60;
00218 static char gAccountcode[80] = DEFAULT_H323ACCNT;
00219 static int  gAMAFLAGS;
00220 static char gContext[AST_MAX_EXTENSION] = DEFAULT_CONTEXT;
00221 static int  gIncomingLimit = 4;
00222 static int  gOutgoingLimit = 4;
00223 OOBOOL gH323Debug = FALSE;
00224 
00225 static struct ooh323_config
00226 {
00227    int  mTCPPortStart;
00228    int  mTCPPortEnd;
00229 }ooconfig;
00230 
00231 /** Asterisk RTP stuff*/
00232 static struct sched_context *sched;
00233 static struct io_context *io;
00234 
00235 /* Protect the monitoring thread, so only one process can kill or start it, 
00236    and not when it's doing something critical. */
00237 AST_MUTEX_DEFINE_STATIC(monlock);
00238 
00239 
00240 /* This is the thread for the monitor which checks for input on the channels
00241    which are not currently in use.  */
00242 static pthread_t monitor_thread = AST_PTHREADT_NULL;
00243 
00244 
00245 static struct ast_channel *ooh323_new(struct ooh323_pvt *i, int state,
00246                                              const char *host) 
00247 {
00248    struct ast_channel *ch = NULL;
00249    int fmt;
00250    if(gH323Debug)
00251       ast_verbose("---   ooh323_new - %s\n", host);
00252 
00253 
00254    /* Don't hold a h323 pvt lock while we allocate a channel */
00255    ast_mutex_unlock(&i->lock);
00256    ch = ast_channel_alloc(1, state, i->callerid_num, i->callerid_name, i->accountcode, i->exten, i->context, i->amaflags, "OOH323/%s-%04x", host, rand() & 0xffff);
00257    ast_mutex_lock(&i->lock);
00258 
00259    if(ch)
00260    {
00261       ast_mutex_lock(&ch->lock);
00262       ch->tech = &ooh323_tech;
00263 
00264       ch->nativeformats = i->capability;
00265 
00266 
00267       fmt = ast_best_codec(ch->nativeformats);
00268 
00269       ch->fds[0] = ast_rtp_fd(i->rtp);
00270       ch->fds[1] = ast_rtcp_fd(i->rtp);
00271 
00272       if (state == AST_STATE_RING)
00273          ch->rings = 1;
00274 
00275       ch->adsicpe = AST_ADSI_UNAVAILABLE;
00276       ch->writeformat = fmt;
00277       ch->rawwriteformat = fmt;
00278       ch->readformat = fmt;
00279       ch->rawreadformat = fmt;
00280       ch->tech_pvt = i;
00281       i->owner = ch;
00282 
00283       /* Allocate dsp for in-band DTMF support */
00284       if(i->dtmfmode & H323_DTMF_INBAND) {
00285          i->vad = ast_dsp_new();
00286          ast_dsp_set_features(i->vad, DSP_FEATURE_DTMF_DETECT);
00287       }
00288 
00289       ast_mutex_lock(&usecnt_lock);
00290       usecnt++;
00291       ast_mutex_unlock(&usecnt_lock);
00292 
00293       /* Notify the module monitors that use count for resource has changed*/
00294       ast_update_use_count();
00295 
00296       strncpy(ch->context, i->context, sizeof(ch->context)-1);
00297       ch->context[sizeof(ch->context)-1] = '\0';
00298       strncpy(ch->exten, i->exten, sizeof(ch->exten)-1);
00299       ch->exten[sizeof(ch->exten)-1] = '\0';
00300 
00301       ch->priority = 1;
00302       if (i->callerid_name)
00303       {
00304          ch->cid.cid_name = strdup(i->callerid_name);
00305       }
00306       if(i->callerid_num){
00307 
00308          ch->cid.cid_num = strdup(i->callerid_num);
00309       }
00310 
00311       if(!ast_test_flag(i, H323_OUTGOING))
00312       {
00313       
00314          if (!ast_strlen_zero(i->caller_h323id)) {
00315             pbx_builtin_setvar_helper(ch, "_CALLER_H323ID", i->caller_h323id);
00316 
00317          }
00318          if (!ast_strlen_zero(i->caller_dialedDigits)) {
00319             pbx_builtin_setvar_helper(ch, "_CALLER_H323DIALEDDIGITS", 
00320                                                        i->caller_dialedDigits);
00321          }
00322          if (!ast_strlen_zero(i->caller_email)) {
00323             pbx_builtin_setvar_helper(ch, "_CALLER_H323EMAIL", 
00324                                                               i->caller_email);
00325          }
00326          if (!ast_strlen_zero(i->caller_url)) {
00327             pbx_builtin_setvar_helper(ch, "_CALLER_H323URL", i->caller_url);
00328          }
00329       }
00330 
00331       if (!ast_strlen_zero(i->accountcode))
00332          ast_string_field_set(ch, accountcode, i->accountcode);
00333       
00334       if (i->amaflags)
00335          ch->amaflags = i->amaflags;
00336 
00337       ast_setstate(ch, state);
00338       if (state != AST_STATE_DOWN) {
00339          if (ast_pbx_start(ch)) {
00340             ast_log(LOG_WARNING, "Unable to start PBX on %s\n", ch->name);
00341             ast_mutex_unlock(&ch->lock);
00342             ast_hangup(ch);
00343             ch = NULL;
00344          }
00345       }
00346    } else
00347       ast_log(LOG_WARNING, "Unable to allocate channel structure\n");
00348 
00349 
00350    if(ch)   ast_mutex_unlock(&ch->lock);
00351 
00352    if(gH323Debug)
00353       ast_verbose("+++   h323_new\n");
00354 
00355    return ch;
00356 }
00357 
00358 
00359 
00360 static struct ooh323_pvt *ooh323_alloc(int callref, char *callToken) 
00361 {
00362    struct ooh323_pvt *pvt = NULL;
00363    struct in_addr ipAddr;
00364    if(gH323Debug)
00365       ast_verbose("---   ooh323_alloc\n");
00366 
00367    pvt = (struct ooh323_pvt*) malloc(sizeof(struct ooh323_pvt));
00368    if(!pvt)
00369    {
00370       ast_log(LOG_ERROR, "Couldn't allocate private ooh323 structure\n");
00371       return NULL;
00372    }
00373 
00374    memset(pvt, 0, sizeof(struct ooh323_pvt));
00375    ast_mutex_init(&pvt->lock);
00376    ast_mutex_lock(&pvt->lock);
00377 
00378 
00379    pvt->caller_h323id[0]='\0';
00380    pvt->caller_dialedDigits[0]='\0';
00381    pvt->caller_email[0]='\0';
00382    pvt->caller_url[0]='\0';
00383    pvt->vrtp = NULL;   
00384    pvt->callee_h323id[0]='\0';
00385    pvt->callee_dialedDigits[0]='\0';
00386    pvt->callee_email[0] = '\0';
00387    pvt->callee_url[0] = '\0';
00388 
00389    pvt->host = NULL;
00390    pvt->callToken = NULL;
00391    pvt->port = 0;
00392    pvt->vad = NULL;
00393    pvt->next = NULL;
00394    pvt->owner = NULL;
00395    pvt->callerid_name = NULL;
00396    pvt->callerid_num = NULL;
00397    pvt->flags = 0;
00398    if(!inet_aton(gIP, &ipAddr))
00399    {
00400       ast_log(LOG_ERROR, "Invalid OOH323 driver ip address\n");
00401       ast_mutex_unlock(&pvt->lock);
00402       ast_mutex_destroy(&pvt->lock);
00403       free(pvt);
00404       return NULL;
00405    }
00406    
00407 
00408    pvt->rtp = ast_rtp_new_with_bindaddr(sched, io, 1, 0, ipAddr);
00409 
00410    if (!pvt->rtp) 
00411    {
00412       ast_log(LOG_WARNING, "Unable to create RTP session: %s\n", 
00413               strerror(errno));
00414       ast_mutex_unlock(&pvt->lock);
00415       ast_mutex_destroy(&pvt->lock);
00416       free(pvt);
00417       return NULL;
00418    }
00419  
00420    ast_rtp_settos(pvt->rtp, gTOS);
00421 
00422 
00423    pvt->call_reference = callref;
00424    if(callToken)
00425       pvt->callToken = strdup(callToken);
00426 
00427    /* whether to use gk for this call */
00428    if(gRasGkMode == RasNoGatekeeper)
00429       OO_SETFLAG(pvt->flags, H323_DISABLEGK);
00430 
00431    pvt->dtmfmode = gDTMFMode;
00432    strncpy(pvt->context, gContext, sizeof(pvt->context)-1);
00433    pvt->exten[0]='\0';
00434    strncpy(pvt->accountcode, gAccountcode, sizeof(pvt->accountcode)-1);
00435    pvt->amaflags = gAMAFLAGS;
00436    pvt->capability = gCapability;
00437    memcpy(&pvt->prefs, &gPrefs, sizeof(struct ast_codec_pref));
00438 
00439 
00440   ast_mutex_unlock(&pvt->lock); 
00441   /* Add to interface list */
00442   ast_mutex_lock(&iflock);
00443   pvt->next = iflist;
00444   iflist = pvt;
00445   ast_mutex_unlock(&iflock);
00446 
00447   if(gH323Debug)
00448     ast_verbose("+++   ooh323_alloc\n");
00449 
00450   return pvt;
00451 }
00452 
00453 
00454 /*
00455    Possible data values - peername, exten/peername, exten@ip
00456  */
00457 static struct ast_channel *ooh323_request(const char *type, int format, 
00458                                         void *data, int *cause)
00459 
00460 {
00461    struct ast_channel *chan=NULL;
00462    struct ooh323_pvt *p = NULL;
00463    struct ooh323_peer *peer = NULL;
00464    char *dest = NULL; 
00465    char *ext = NULL;
00466    char tmp[256];
00467    char formats[512];
00468    int oldformat;
00469    char *sport = NULL;
00470    int port = 0;
00471 
00472    if(gH323Debug)
00473       ast_verbose("---   ooh323_request - data %s format %s\n", (char*)data,  
00474                               ast_getformatname_multiple(formats,512,format) );
00475 
00476    oldformat = format;
00477    format &= ((AST_FORMAT_MAX_AUDIO << 1) - 1);
00478    if (!format) {
00479       ast_log(LOG_NOTICE, "Asked to get a channel of unsupported format "
00480                           "'%d'\n", format);
00481       return NULL;
00482    }
00483 
00484    p = ooh323_alloc(0,0); /* Initial callRef is zero */
00485 
00486    if(!p)
00487    {
00488       ast_log(LOG_WARNING, "Unable to build pvt data for '%s'\n", (char*)data);
00489       return NULL;
00490    }
00491    ast_mutex_lock(&p->lock);
00492 
00493    /* This is an outgoing call, since ooh323_request is called */
00494    ast_set_flag(p, H323_OUTGOING);
00495 
00496 
00497    strncpy(tmp, (char*)data, sizeof(tmp)-1);
00498 
00499    dest = strchr(tmp, '/');
00500 
00501    if(dest)
00502    {  
00503       *dest = '\0';
00504       dest++;
00505       ext = tmp;
00506    }
00507    else if((dest = strchr(tmp, '@'))) {
00508       *dest = '\0';
00509       dest++;
00510       ext = tmp;
00511    }
00512    else {
00513       dest = tmp;
00514       ext = NULL;
00515    }
00516    
00517    /*if((sport = strchr(dest, ':'))) {
00518       *sport = '\0';
00519       sport++;
00520       port = atoi(sport);
00521    }*/
00522 
00523    if(dest) {
00524       peer = find_peer(dest, port);
00525    }
00526    else{
00527       ast_log(LOG_ERROR, "Destination format is not supported\n");
00528       return NULL;
00529    }
00530 
00531    if(peer)
00532    {
00533       p->username = strdup(peer->name);
00534       p->host = strdup(peer->ip);
00535       p->port = peer->port;
00536       /* Disable gk as we are going to call a known peer*/
00537       OO_SETFLAG(p->flags, H323_DISABLEGK);
00538 
00539       if(ext)
00540          strncpy(p->exten, ext, sizeof(p->exten)-1);
00541 
00542       if(peer->capability & format)
00543          p->capability = peer->capability & format;
00544       else{
00545          p->capability = peer->capability;
00546       }
00547       memcpy(&p->prefs, &peer->prefs, sizeof(struct ast_codec_pref));
00548       p->dtmfmode = peer->dtmfmode;
00549       strncpy(p->accountcode, peer->accountcode, sizeof(p->accountcode)-1);
00550       p->amaflags = peer->amaflags;
00551    }
00552    else {
00553       p->dtmfmode = gDTMFMode;
00554       p->capability = gCapability;
00555 
00556       memcpy(&p->prefs, &gPrefs, sizeof(struct ast_codec_pref));
00557       p->username = strdup(dest);
00558      
00559       p->host = strdup(dest);
00560       if(port > 0) {
00561          p->port = port;
00562       }
00563       if(ext) {
00564          strncpy(p->exten, ext, sizeof(p->exten)-1);
00565       }
00566    }
00567 
00568 
00569    chan = ooh323_new(p, AST_STATE_DOWN, p->username);
00570    
00571    ast_mutex_unlock(&p->lock);
00572 
00573    if (!chan)
00574    {
00575       ast_mutex_lock(&iflock);
00576       ooh323_destroy(p);
00577       ast_mutex_unlock(&iflock);
00578    }
00579 
00580    restart_monitor();
00581    if(gH323Debug)
00582       ast_verbose("+++   ooh323_request\n");
00583 
00584    return chan;
00585 
00586 }
00587 
00588 
00589 static struct ooh323_pvt* find_call(ooCallData *call)
00590 {
00591    struct ooh323_pvt *p=NULL;
00592 
00593    if(gH323Debug)
00594       ast_verbose("---   find_call\n");
00595 
00596    p = iflist;
00597    ast_mutex_lock(&iflock);
00598 
00599    while(p)
00600    {
00601       if(p->callToken && !strcmp(p->callToken, call->callToken))
00602       {
00603          break;
00604       }
00605       p = p->next;
00606       
00607    }
00608    ast_mutex_unlock(&iflock);
00609 
00610    if(gH323Debug)
00611       ast_verbose("+++   find_call\n");
00612 
00613    return p;
00614 }
00615 
00616 struct ooh323_user *find_user(const char * name, const char* ip)
00617 {
00618    struct ooh323_user *user=NULL;
00619 
00620    if(gH323Debug)
00621       ast_verbose("---   find_user \n");
00622 
00623    user = userl.users;
00624    ast_mutex_lock(&userl.lock);
00625    while(user)
00626    {
00627       if(ip && user->mUseIP && !strcmp(user->mIP, ip)) {
00628          break;
00629       }
00630       if(name && !strcmp(user->name, name)) {
00631          break;
00632       }
00633       user = user->next;
00634    }
00635    ast_mutex_unlock(&userl.lock);
00636 
00637    if(gH323Debug)
00638       ast_verbose("+++   find_user\n");
00639 
00640    return user;      
00641 }
00642 
00643 struct ooh323_peer *find_friend(const char *name, int port)
00644 {
00645    struct ooh323_peer *peer=NULL;  
00646 
00647    if(gH323Debug)
00648       ast_verbose("---   find_friend \"%s\"\n", name);
00649 
00650 
00651    peer = peerl.peers;
00652    ast_mutex_lock(&peerl.lock);
00653    while(peer)
00654    {
00655       if(gH323Debug) {
00656          ast_verbose("      comparing with \"%s\"\n", peer->ip);
00657       }
00658       if(!strcmp(peer->ip, name)) {
00659          if(port > 0 && peer->port == port) { break; }
00660          else if (port <= 0) { break; }
00661       }
00662       peer = peer->next;
00663    }
00664    ast_mutex_unlock(&peerl.lock);
00665 
00666    if(gH323Debug) {
00667       if(peer) {
00668          ast_verbose("      found matching friend\n");
00669       }
00670       ast_verbose("+++   find_friend \"%s\"\n", name);
00671    }
00672 
00673    return peer;      
00674 }
00675 
00676 
00677 struct ooh323_peer *find_peer(const char * name, int port)
00678 {
00679    struct ooh323_peer *peer=NULL;  
00680 
00681    if(gH323Debug)
00682       ast_verbose("---   find_peer \"%s\"\n", name);
00683 
00684 
00685    peer = peerl.peers;
00686    ast_mutex_lock(&peerl.lock);
00687    while(peer)
00688    {
00689       if(gH323Debug) {
00690          ast_verbose("      comparing with \"%s\"\n", peer->ip);
00691       }
00692       if(!strcasecmp(peer->name, name))
00693          break;
00694       if(peer->h323id && !strcasecmp(peer->h323id, name))
00695          break;
00696       if(peer->e164 && !strcasecmp(peer->e164, name))
00697          break;
00698       /*
00699       if(!strcmp(peer->ip, name)) {
00700          if(port > 0 && peer->port == port) { break; }
00701          else if (port <= 0) { break; }
00702       }
00703       */
00704       peer = peer->next;
00705    }
00706    ast_mutex_unlock(&peerl.lock);
00707 
00708    if(gH323Debug) {
00709       if(peer) {
00710          ast_verbose("      found matching peer\n");
00711       }
00712       ast_verbose("+++   find_peer \"%s\"\n", name);
00713    }
00714 
00715    return peer;      
00716 }
00717 
00718 static int ooh323_digit_begin(struct ast_channel *chan, char digit)
00719 {
00720    char dtmf[2];
00721    struct ooh323_pvt *p = (struct ooh323_pvt *) chan->tech_pvt;
00722    
00723    if(gH323Debug)
00724       ast_verbose("---   ooh323_digit_begin\n");
00725 
00726    if(!p){
00727       ast_log(LOG_ERROR, "No private structure for call\n");
00728       return -1;
00729    }
00730    ast_mutex_lock(&p->lock);
00731    if (p->rtp && (p->dtmfmode & H323_DTMF_RFC2833)) {
00732       ast_rtp_senddigit_begin(p->rtp, digit);
00733    }
00734    else if (((p->dtmfmode & H323_DTMF_Q931) ||
00735                    (p->dtmfmode & H323_DTMF_H245ALPHANUMERIC) ||
00736                    (p->dtmfmode & H323_DTMF_H245SIGNAL))){
00737       dtmf[0]= digit;
00738       dtmf[1]='\0';
00739       ast_mutex_lock(&ooh323c_cmd_lock);
00740       ooSendDTMFDigit(p->callToken, dtmf);
00741       ast_mutex_unlock(&ooh323c_cmd_lock);
00742    }
00743    ast_mutex_unlock(&p->lock);
00744    if(gH323Debug)
00745       ast_verbose("+++   ooh323_digit_begin\n");
00746 
00747    return 0;
00748 }
00749 
00750 static int ooh323_digit_end(struct ast_channel *chan, char digit, unsigned int duration)
00751 {
00752    struct ooh323_pvt *p = (struct ooh323_pvt *) chan->tech_pvt;
00753 
00754    if(gH323Debug)
00755       ast_verbose("---   ooh323_digit_end\n");
00756 
00757    if(!p){
00758       ast_log(LOG_ERROR, "No private structure for call\n");
00759       return -1;
00760    }
00761    ast_mutex_lock(&p->lock);
00762    if (p->rtp && (p->dtmfmode & H323_DTMF_RFC2833)) 
00763       ast_rtp_senddigit_end(p->rtp, digit);
00764 
00765    ast_mutex_unlock(&p->lock);
00766    if(gH323Debug)
00767       ast_verbose("+++   ooh323_digit_end\n");
00768 
00769    return 0;
00770 }
00771 
00772 
00773 static int ooh323_call(struct ast_channel *ast, char *dest, int timeout)
00774 {
00775    struct ooh323_pvt *p = ast->tech_pvt;
00776    char destination[256];
00777    int res=0;
00778    const char *val=NULL;
00779    ooCallOptions opts = {
00780       .fastStart = TRUE,
00781       .tunneling = TRUE,
00782       .disableGk = TRUE,
00783       .callMode = OO_CALLMODE_AUDIOCALL
00784    };
00785    if(gH323Debug)
00786       ast_verbose("---   ooh323_call- %s\n", dest);
00787 
00788 
00789    if ((ast->_state != AST_STATE_DOWN) && (ast->_state != AST_STATE_RESERVED)) 
00790    {
00791       ast_log(LOG_WARNING, "ooh323_call called on %s, neither down nor "
00792                            "reserved\n", ast->name);
00793       return -1;
00794    }
00795    ast_mutex_lock(&p->lock);
00796    ast_set_flag(p, H323_OUTGOING);
00797    if (ast->cid.cid_num) {
00798      if(p->callerid_num)   free(p->callerid_num);
00799       p->callerid_num = strdup(ast->cid.cid_num);
00800    }
00801 
00802    if(ast->cid.cid_name) {
00803       if(p->callerid_name)   free(p->callerid_name);
00804       p->callerid_name = strdup(ast->cid.cid_name);
00805    }
00806    else{
00807       ast->cid.cid_name = strdup(gCallerID);
00808       if(p->callerid_name)   free(p->callerid_name);
00809       p->callerid_name = strdup(ast->cid.cid_name);
00810    }
00811 
00812    /* Retrieve vars */
00813 
00814 
00815    val = pbx_builtin_getvar_helper(ast, "CALLER_H323ID");
00816    if(val){
00817      strncpy(p->caller_h323id, val, sizeof(p->caller_h323id)-1);
00818    }
00819    
00820    val = pbx_builtin_getvar_helper(ast, "CALLER_H323DIALEDDIGITS");
00821    if(val){
00822       strncpy(p->caller_dialedDigits, val, sizeof(p->caller_dialedDigits)-1);
00823       if(!p->callerid_num)   p->callerid_num = strdup(val);
00824      
00825    }
00826 
00827    val = pbx_builtin_getvar_helper(ast, "CALLER_H323EMAIL");
00828    if(val)
00829      strncpy(p->caller_email, val, sizeof(p->caller_email)-1);
00830    
00831    val = pbx_builtin_getvar_helper(ast, "CALLER_H323URL");
00832    if(val)
00833      strncpy(p->caller_url, val, sizeof(p->caller_url)-1);  
00834 
00835 
00836    p->callToken = (char*)malloc(AST_MAX_EXTENSION);
00837    if(!p->callToken)
00838    {
00839       ast_mutex_unlock(&p->lock);
00840       ast_log(LOG_ERROR, "Failed to allocate memory for callToken\n");
00841       return -1; /* TODO: need to clean/hangup?? */
00842    }      
00843 
00844    if(p->host && p->port != 0)
00845       sprintf(destination, "%s:%d", p->host, p->port);
00846    else if(p->host)
00847       sprintf(destination, "%s", p->host);
00848    else
00849      strncpy(destination, dest, sizeof(destination)-1);
00850 
00851    destination[sizeof(destination)-1]='\0';
00852 
00853    ast_mutex_lock(&ooh323c_cmd_lock);
00854    if(OO_TESTFLAG(p->flags, H323_DISABLEGK))
00855       res = ooMakeCall(destination, p->callToken, AST_MAX_EXTENSION, &opts);
00856    else
00857       res = ooMakeCall(destination, p->callToken, AST_MAX_EXTENSION, NULL);
00858    ast_mutex_unlock(&ooh323c_cmd_lock);
00859 
00860    ast_mutex_unlock(&p->lock);
00861    if(res != OO_OK)
00862    {
00863       ast_log(LOG_ERROR,"Failed to make call\n");
00864       return -1; /* ToDO: cleanup */
00865    }
00866    if(gH323Debug)
00867       ast_verbose("+++   ooh323_call\n");
00868 
00869   return 0;
00870 }
00871 
00872 static int ooh323_hangup(struct ast_channel *ast)
00873 {
00874    struct ooh323_pvt *p = ast->tech_pvt;
00875 
00876    if(gH323Debug)
00877       ast_verbose("---   ooh323_hangup\n");
00878 
00879    if(p)
00880    {
00881       ast_mutex_lock(&p->lock);
00882 
00883       if(gH323Debug)
00884    ast_verbose("    hanging %s\n", p->username);
00885       ast->tech_pvt = NULL; 
00886       if(!ast_test_flag(p, H323_ALREADYGONE)) {
00887          ast_mutex_lock(&ooh323c_cmd_lock);
00888          ooHangCall(p->callToken, 
00889              ooh323_convert_hangupcause_asteriskToH323(p->owner->hangupcause));
00890          ast_mutex_unlock(&ooh323c_cmd_lock);
00891          ast_set_flag(p, H323_ALREADYGONE);
00892          /* ast_mutex_unlock(&p->lock); */
00893       }
00894       else {
00895          ast_set_flag(p, H323_NEEDDESTROY);
00896       }
00897       /* detach channel here */
00898       if(p->owner)
00899       {
00900          p->owner->tech_pvt = NULL;
00901          p->owner = NULL;
00902       }
00903 
00904       ast_mutex_unlock(&p->lock);
00905       ast_mutex_lock(&usecnt_lock);
00906       usecnt--;
00907       ast_mutex_unlock(&usecnt_lock);
00908 
00909       /* Notify the module monitors that use count for resource has changed */
00910       ast_update_use_count();
00911      
00912    }
00913 
00914 
00915    else {
00916       ast_log(LOG_ERROR, "No call to hangup\n" );
00917       return -1;
00918    }
00919    
00920    if(gH323Debug)
00921       ast_verbose("+++   ooh323_hangup\n");
00922 
00923   return 0;
00924 }
00925 
00926 static int ooh323_answer(struct ast_channel *ast)
00927 {
00928    struct ooh323_pvt *p = ast->tech_pvt;
00929 
00930    if(gH323Debug)
00931       ast_verbose("--- ooh323_answer\n");
00932 
00933    ast_mutex_lock(&p->lock);
00934    if(ast->_state != AST_STATE_UP)
00935    {
00936       ast_mutex_lock(&ast->lock);
00937       ast_setstate(ast, AST_STATE_UP);
00938       if (option_debug)
00939          ast_log(LOG_DEBUG, "ooh323_answer(%s)\n", ast->name);
00940       ast_mutex_unlock(&ast->lock);
00941       ast_mutex_lock(&ooh323c_cmd_lock);
00942       ooAnswerCall(p->callToken);
00943       ast_mutex_unlock(&ooh323c_cmd_lock);
00944    }
00945    ast_mutex_unlock(&p->lock);
00946 
00947    if(gH323Debug)
00948       ast_verbose("+++ ooh323_answer\n");
00949 
00950   return 0;
00951 }
00952 
00953 static struct ast_frame *ooh323_read(struct ast_channel *ast)
00954 {
00955    struct ast_frame *fr;
00956    static struct ast_frame null_frame = { AST_FRAME_NULL, };
00957    struct ooh323_pvt *p = ast->tech_pvt;
00958 
00959    ast_mutex_lock(&p->lock);
00960    if(p->rtp)
00961       fr = ooh323_rtp_read(ast, p);
00962    else
00963       fr = &null_frame;
00964    /* time(&p->lastrtprx); */
00965    ast_mutex_unlock(&p->lock);
00966    return fr;
00967 }
00968 
00969 static int ooh323_write(struct ast_channel *ast, struct ast_frame *f)
00970 {
00971    struct ooh323_pvt *p = ast->tech_pvt;
00972    int res = 0;
00973 
00974    if(f->frametype == AST_FRAME_VOICE)
00975    {
00976       if(!(f->subclass & ast->nativeformats))
00977       {
00978          ast_log(LOG_WARNING, "Asked to transmit frame type %d, while native "
00979                              "formats is %d (read/write = %d/%d)\n",
00980                            f->subclass, ast->nativeformats, ast->readformat,
00981                               ast->writeformat);
00982          return 0;
00983       }
00984       if(p){
00985          ast_mutex_lock(&p->lock);
00986          if(p->rtp)
00987             res = ast_rtp_write(p->rtp, f);
00988          ast_mutex_unlock(&p->lock);
00989       }
00990    }
00991    else if (f->frametype == AST_FRAME_IMAGE) {
00992       return 0;
00993    }
00994    else {
00995       ast_log(LOG_WARNING, "Can't send %d type frames with SIP write\n", 
00996                             f->frametype);
00997       return 0;
00998    }
00999 
01000    return res;
01001 }
01002 
01003 static int ooh323_indicate(struct ast_channel *ast, int condition, const void *data, size_t datalen)
01004 {
01005 
01006    struct ooh323_pvt *p = (struct ooh323_pvt *) ast->tech_pvt;
01007    char *callToken = (char *)NULL;
01008 
01009    ast_mutex_lock(&p->lock);
01010    callToken = (p->callToken ? strdup(p->callToken) : NULL);
01011    ast_mutex_unlock(&p->lock);
01012 
01013    if(!callToken)
01014    {
01015       if(gH323Debug)
01016          ast_verbose("   ooh323_indicate - No callToken\n");
01017       return -1;
01018    }
01019 
01020    if(gH323Debug)
01021       ast_verbose("----- ooh323_indicate %d on call %s\n", condition, callToken);
01022     
01023 
01024    switch(condition) {
01025       case AST_CONTROL_CONGESTION:
01026          if(!ast_test_flag(p, H323_ALREADYGONE)){
01027             ast_mutex_lock(&ooh323c_cmd_lock);
01028             ooHangCall(callToken, OO_REASON_LOCAL_CONGESTED);
01029             ast_mutex_unlock(&ooh323c_cmd_lock);
01030             ast_set_flag(p, H323_ALREADYGONE);
01031          }
01032          break;
01033       case AST_CONTROL_BUSY:
01034          if(!ast_test_flag(p, H323_ALREADYGONE)){
01035             ast_mutex_lock(&ooh323c_cmd_lock);
01036             ooHangCall(callToken, OO_REASON_LOCAL_BUSY);
01037             ast_mutex_unlock(&ooh323c_cmd_lock);
01038             ast_set_flag(p, H323_ALREADYGONE);
01039          }
01040          break;
01041       case AST_CONTROL_HOLD:
01042          ast_moh_start(ast, data, NULL);      
01043          break;
01044       case AST_CONTROL_UNHOLD:
01045         ast_moh_stop(ast);
01046          break;
01047       case AST_CONTROL_PROCEEDING:
01048       case AST_CONTROL_RINGING:
01049       case AST_CONTROL_PROGRESS:
01050       case -1:
01051          break;
01052       default:
01053          ast_log(LOG_WARNING,"Don't know how to indicate condition %d on %s\n",
01054                               condition, callToken);
01055    }
01056 
01057    if(gH323Debug)
01058       ast_verbose("++++  ooh323_indicate %d on %s\n", condition, callToken);
01059 
01060 
01061    return -1;
01062 }
01063 
01064 static int ooh323_fixup
01065    (struct ast_channel *oldchan, struct ast_channel *newchan)
01066 {
01067    struct ooh323_pvt *p = newchan->tech_pvt;
01068 
01069    if(gH323Debug)
01070       ast_verbose("--- ooh323c ooh323_fixup\n");
01071 
01072    ast_mutex_lock(&p->lock);
01073    if (p->owner != oldchan) {
01074       ast_log(LOG_WARNING, "Old channel wasn't %p but was %p\n", oldchan, p->owner);
01075       ast_mutex_unlock(&p->lock);
01076       return -1;
01077    }
01078 
01079    if (p->owner == oldchan) {
01080       p->owner = newchan;
01081    }
01082    else {
01083       p->owner = oldchan;
01084    }
01085 
01086    ast_mutex_unlock(&p->lock);
01087 
01088    if(gH323Debug)
01089       ast_verbose("+++ ooh323c ooh323_fixup \n");
01090 
01091    return 0;
01092 }
01093 
01094 
01095 void ooh323_set_write_format(ooCallData *call, int fmt)
01096 {
01097 #if 0
01098    struct ooh323_pvt *p = NULL;
01099    char formats[512];
01100 #ifdef print_debug
01101    printf("---   ooh323_update_writeformat %s\n", 
01102                               ast_getformatname_multiple(formats,512, fmt));
01103 #endif
01104    
01105    p = find_call(call);
01106    if(!p)
01107    {
01108       ast_log(LOG_ERROR, "No matching call found for %s\n", call->callToken);
01109       return;
01110    }
01111 
01112    ast_mutex_lock(&p->lock);
01113 
01114    p->writeformat = fmt;
01115    ast_mutex_unlock(&p->lock);
01116 
01117    if(p->owner){
01118      printf("Writeformat before update %s\n", 
01119               ast_getformatname_multiple(formats,512, p->owner->writeformat));
01120      ast_set_write_format(p->owner, fmt);
01121    }
01122    else
01123      ast_log(LOG_ERROR, "No owner found\n");
01124 
01125 
01126 #ifdef print_debug
01127   printf("+++   ooh323_update_writeformat\n");
01128 #endif
01129 #endif
01130 }
01131 
01132 
01133 void ooh323_set_read_format(ooCallData *call, int fmt)
01134 {
01135 #if 0
01136    struct ooh323_pvt *p = NULL;
01137    char formats[512];
01138 #ifdef print_debug
01139    printf("---   ooh323_update_readformat %s\n", 
01140                                  ast_getformatname_multiple(formats,512, fmt));
01141 #endif
01142 
01143    p = find_call(call);
01144    if(!p)
01145    {
01146       ast_log(LOG_ERROR, "No matching call found for %s\n", call->callToken);
01147       return;
01148    }
01149 
01150    ast_mutex_lock(&p->lock);
01151    p->readformat = fmt;
01152    ast_mutex_unlock(&p->lock);
01153    ast_set_read_format(p->owner, fmt);   
01154    
01155 #ifdef print_debug
01156   printf("+++   ooh323_update_readformat\n");
01157 #endif
01158 #endif
01159 }
01160 
01161 int onAlerting(ooCallData *call)
01162 {
01163    struct ooh323_pvt *p=NULL;
01164    struct ast_channel *c = NULL;
01165 
01166    if(gH323Debug)
01167       ast_verbose("--- onAlerting %s\n", call->callToken);
01168 
01169    p = find_call(call);
01170 
01171    if(!p)
01172    {
01173       ast_log(LOG_ERROR, "No matching call found\n");
01174       return -1;
01175    }  
01176    ast_mutex_lock(&p->lock);
01177    if(!ast_test_flag(p, H323_OUTGOING))
01178    {
01179       c = ooh323_new(p, AST_STATE_RING, p->username);
01180       if(!c)
01181       {
01182          ast_mutex_unlock(&p->lock);
01183          ast_log(LOG_ERROR, "Could not create ast_channel\n");
01184          return -1;
01185       }
01186       ast_mutex_unlock(&p->lock);
01187    }
01188    else {
01189       if (!p->owner) {
01190          ast_mutex_unlock(&p->lock);
01191          ast_log(LOG_ERROR, "Channel has no owner\n");
01192          return 0;
01193       }
01194       c = p->owner;
01195       ast_mutex_unlock(&p->lock);
01196       ast_mutex_lock(&c->lock);
01197       ast_setstate(c, AST_STATE_RINGING);
01198       ast_mutex_unlock(&c->lock);
01199       ast_queue_control(c, AST_CONTROL_RINGING);
01200    }
01201    
01202 
01203    if(gH323Debug)
01204       ast_verbose("+++ onAlerting %s\n", call->callToken);
01205 
01206    return OO_OK;
01207 }
01208 
01209 /**
01210   * Callback for sending digits from H.323 up to asterisk
01211   *
01212   */
01213 int ooh323_onReceivedDigit(OOH323CallData *call, const char* digit)
01214 {
01215    struct ooh323_pvt *p=NULL;
01216    struct ast_frame f;
01217    int res;
01218 
01219    ast_log(LOG_DEBUG, "Received Digit: %c\n", digit);
01220    p = find_call(call);
01221    if(!p) {
01222       ast_log(LOG_ERROR, "Failed to find a matching call.\n");
01223       return -1;
01224    }
01225    if(!p->owner) {
01226       ast_log(LOG_ERROR, "Channel has no owner\n");
01227       return -1;
01228    }
01229    ast_mutex_lock(&p->lock);
01230    memset(&f, 0, sizeof(f));
01231    f.frametype = AST_FRAME_DTMF;
01232    f.subclass = digit[0];
01233    f.datalen = 0;
01234    f.samples = 800;
01235    f.offset = 0;
01236    f.data = NULL;
01237    f.mallocd = 0;
01238    f.src = "SEND_DIGIT";
01239    ast_mutex_unlock(&p->lock);
01240    res = ast_queue_frame(p->owner, &f);
01241    return res;
01242 }
01243 
01244 int ooh323_onReceivedSetup(ooCallData *call, Q931Message *pmsg)
01245 {
01246    struct ooh323_pvt *p=NULL;
01247    struct ooh323_user *user=NULL;
01248    ooAliases *alias=NULL;
01249    char *at=NULL;
01250    char number [OO_MAX_NUMBER_LENGTH];
01251 
01252    if(gH323Debug)
01253       ast_verbose("---   ooh323_onReceivedSetup %s\n", call->callToken);
01254 
01255 
01256    p = ooh323_alloc(call->callReference, call->callToken);
01257    if(!p)
01258    {
01259       ast_log(LOG_ERROR, "Failed to create a new call.\n");
01260       return -1;
01261    }
01262    ast_mutex_lock(&p->lock);
01263    ast_clear_flag(p, H323_OUTGOING);
01264   
01265 
01266    if(call->remoteDisplayName){
01267       p->callerid_name = strdup(call->remoteDisplayName);
01268    }
01269 
01270    if(ooCallGetCallingPartyNumber(call, number, OO_MAX_NUMBER_LENGTH) == OO_OK)
01271    {
01272       p->callerid_num = strdup(number);
01273    }
01274 
01275    if(call->remoteAliases)
01276    {
01277       alias = call->remoteAliases;         
01278       while(alias)
01279       {
01280          if(alias->type == T_H225AliasAddress_h323_ID)
01281          {
01282             if(!p->callerid_name){
01283                p->callerid_name = strdup(alias->value);
01284             }
01285             strncpy(p->caller_h323id, alias->value, sizeof(p->caller_h323id)-1);
01286          }
01287          else if(alias->type == T_H225AliasAddress_dialedDigits)
01288          {
01289             if(!p->callerid_num)
01290                p->callerid_num = strdup(alias->value);
01291             strncpy(p->caller_dialedDigits, alias->value, 
01292                                              sizeof(p->caller_dialedDigits)-1);
01293          }
01294          else if(alias->type == T_H225AliasAddress_email_ID)
01295          {
01296             strncpy(p->caller_email, alias->value, sizeof(p->caller_email)-1);
01297          }
01298          else if(alias->type == T_H225AliasAddress_url_ID)
01299          {
01300             strncpy(p->caller_url, alias->value, sizeof(p->caller_url)-1);
01301          }
01302          alias = alias->next;
01303       }
01304    }
01305 
01306    number[0]='\0';
01307    if(ooCallGetCalledPartyNumber(call, number, OO_MAX_NUMBER_LENGTH)== OO_OK)
01308    {
01309       strncpy(p->exten, number, sizeof(p->exten)-1);
01310    }
01311    else{
01312       update_our_aliases(call, p);
01313       if(!ast_strlen_zero(p->callee_dialedDigits)){
01314          strncpy(p->exten, p->callee_dialedDigits, sizeof(p->exten)-1);
01315       }
01316       else if(!ast_strlen_zero(p->callee_h323id)){
01317          strncpy(p->exten, p->callee_h323id, sizeof(p->exten)-1);
01318       }
01319       else if(!ast_strlen_zero(p->callee_email)){
01320          strncpy(p->exten, p->callee_email, sizeof(p->exten)-1);
01321          if((at=strchr(p->exten, '@'))) {
01322             *at='\0';
01323          }
01324       }
01325    }
01326 
01327    /* if no extension found, set to default 's' */
01328    if(ast_strlen_zero(p->exten))
01329    {     
01330       p->exten[0]='s';
01331       p->exten[1]='\0';
01332    }
01333 
01334    if(!p->callerid_name)
01335    {
01336       p->callerid_name = strdup(call->remoteIP);
01337    }
01338    
01339    if(p->callerid_name)
01340    {
01341       user = find_user(p->callerid_name, call->remoteIP);
01342       if(user)
01343       {
01344          ast_mutex_lock(&user->lock);
01345          p->username = strdup(user->name);
01346          strncpy(p->context, user->context, sizeof(p->context)-1);
01347          strncpy(p->accountcode, user->accountcode, sizeof(p->accountcode)-1);
01348          p->amaflags = user->amaflags;
01349          p->capability = user->capability;
01350          memcpy(&p->prefs, &user->prefs, sizeof(struct ast_codec_pref));
01351          p->dtmfmode = user->dtmfmode;
01352          /* Since, call is coming from a pbx user, no need to use gk */
01353          OO_SETFLAG(p->flags, H323_DISABLEGK);
01354          OO_SETFLAG(call->flags, OO_M_DISABLEGK);
01355          ast_mutex_unlock(&user->lock);
01356       }
01357    }
01358 
01359 
01360    ooh323c_set_capability_for_call(call, &p->prefs, p->capability,p->dtmfmode);
01361    configure_local_rtp(p, call);
01362 
01363    ast_mutex_unlock(&p->lock);
01364 
01365    if(gH323Debug)
01366       ast_verbose("+++   ooh323_onReceivedSetup - Determined context %s, "
01367                   "extension %s\n", p->context, p->exten);
01368 
01369    return OO_OK;
01370 }
01371 
01372 
01373 
01374 int onNewCallCreated(ooCallData *call)
01375 {
01376    struct ooh323_pvt *p = NULL;
01377    int i=0;
01378 
01379    if(gH323Debug)
01380       ast_verbose("---   onNewCallCreated %s\n", call->callToken);
01381 
01382    if(!strcmp(call->callType, "outgoing"))
01383    {
01384       p = find_call(call);
01385       if(!p)
01386       {
01387          ast_log(LOG_ERROR, "No matching call found for outgoing call\n");
01388          return -1;
01389       }
01390       ast_mutex_lock(&p->lock);
01391       if(p->callerid_name){
01392          ooCallSetCallerId(call, p->callerid_name);
01393       }
01394       if(p->callerid_num)
01395       {
01396          i=0;
01397          while(*(p->callerid_num+i) != '\0')
01398          {
01399             if(!isdigit(*(p->callerid_num+i))) { break; }
01400             i++;
01401          }
01402          if(*(p->callerid_num+i) == '\0')
01403             ooCallSetCallingPartyNumber(call, p->callerid_num);
01404          else{
01405             if(!p->callerid_name)
01406                ooCallSetCallerId(call, p->callerid_num);
01407          }
01408       }
01409       
01410       if(!ast_strlen_zero(p->caller_h323id))
01411          ooCallAddAliasH323ID(call, p->caller_h323id);
01412 
01413       if(!ast_strlen_zero(p->caller_dialedDigits)){
01414          if(gH323Debug) {
01415             ast_verbose("Setting dialed digits %s\n", p->caller_dialedDigits);
01416          }
01417          ooCallAddAliasDialedDigits(call, p->caller_dialedDigits);
01418       }
01419       else if(p->callerid_num) {
01420          if(ooIsDailedDigit(p->callerid_num)) {
01421             if(gH323Debug) {
01422                ast_verbose("setting callid number %s\n", p->callerid_num);
01423             }
01424             ooCallAddAliasDialedDigits(call, p->callerid_num);
01425          }
01426          else if(ast_strlen_zero(p->caller_h323id)) {
01427             ooCallAddAliasH323ID(call, p->callerid_num);
01428          }
01429       }
01430   
01431 
01432       if(!ast_strlen_zero(p->exten))  
01433       {
01434          if(ooIsDailedDigit(p->exten)) {
01435             ooCallSetCalledPartyNumber(call, p->exten);
01436             ooCallAddRemoteAliasDialedDigits(call, p->exten);
01437          }
01438          else {
01439            ooCallAddRemoteAliasH323ID(call, p->exten);
01440          }
01441       }
01442 
01443       if(gH323Debug)
01444       {
01445          char prefsBuf[256];
01446          ast_codec_pref_string(&p->prefs, prefsBuf, 256);
01447          ast_verbose(" Outgoing call %s(%s) - Codec prefs - %s\n", 
01448                     p->username?p->username:"NULL", call->callToken, prefsBuf);
01449       }
01450 
01451       ooh323c_set_capability_for_call(call, &p->prefs, p->capability, 
01452                                      p->dtmfmode);
01453    
01454       configure_local_rtp(p, call);
01455       ast_mutex_unlock(&p->lock);
01456    }
01457    if(gH323Debug)
01458       ast_verbose("+++   onNewCallCreated %s\n", call->callToken);
01459 
01460    return OO_OK;
01461 }
01462 
01463 int onCallEstablished(ooCallData *call)
01464 {
01465   struct ooh323_pvt *p = NULL;
01466 
01467    if(gH323Debug)
01468       ast_verbose("---   onCallEstablished %s\n", call->callToken);
01469 
01470    p = find_call(call);
01471    if(!p) {
01472       ast_log(LOG_ERROR, "Failed to find a matching call.\n");
01473       return -1;
01474    }
01475    ast_mutex_lock(&p->lock);
01476    if (!p->owner) {
01477       ast_mutex_unlock(&p->lock);
01478       ast_log(LOG_ERROR, "Channel has no owner\n");
01479       return -1;
01480    }
01481    
01482    while(ast_mutex_trylock(&p->owner->lock))
01483    {
01484       ast_log(LOG_DEBUG,"Failed to grab lock, trying again\n");
01485       ast_mutex_unlock(&p->lock);
01486       usleep(1);
01487       ast_mutex_lock(&p->lock);
01488    }    
01489    if(p->owner->_state != AST_STATE_UP)
01490    {
01491       ast_setstate(p->owner, AST_STATE_UP);
01492    }
01493    ast_mutex_unlock(&p->owner->lock);
01494    if(ast_test_flag(p, H323_OUTGOING)) {
01495       struct ast_channel* c = p->owner;
01496       ast_mutex_unlock(&p->lock);
01497       ast_queue_control(c, AST_CONTROL_ANSWER);
01498    }
01499    else { ast_mutex_unlock(&p->lock); }
01500 
01501    if(gH323Debug)
01502       ast_verbose("+++   onCallEstablished %s\n", call->callToken);
01503 
01504   return OO_OK;
01505 }
01506 
01507 int onCallCleared(ooCallData *call)
01508 {
01509    struct ooh323_pvt *p = NULL;
01510    int ownerLock = 0;
01511 
01512    if(gH323Debug)
01513       ast_verbose("---   onCallCleared %s \n", call->callToken);
01514 
01515    p = find_call(call); 
01516    if (!p) { return 0; }
01517    ast_mutex_lock(&p->lock);
01518   
01519    while(p->owner) {
01520       if(ast_mutex_trylock(&p->owner->lock)) {
01521          ooTrace(OOTRCLVLINFO, "Failed to grab lock, trying again\n");
01522          ast_log(LOG_DEBUG,"Failed to grab lock, trying again\n");
01523          ast_mutex_unlock(&p->lock);
01524          usleep(1);
01525          ast_mutex_lock(&p->lock);
01526       }
01527       else {
01528          ownerLock = 1; break;
01529       }
01530    }
01531 
01532    if(ownerLock) {
01533       if(!ast_test_flag(p, H323_ALREADYGONE)) { 
01534 
01535          /* NOTE: Channel is not detached yet */
01536          ast_set_flag(p, H323_ALREADYGONE);
01537          p->owner->hangupcause = 
01538             ooh323_convert_hangupcause_h323ToAsterisk(call->callEndReason);
01539          p->owner->_softhangup |= AST_SOFTHANGUP_DEV;
01540          ast_mutex_unlock(&p->owner->lock);
01541          ast_queue_hangup(p->owner);
01542          ast_mutex_unlock(&p->lock);
01543          return OO_OK;
01544       }
01545       ast_mutex_unlock(&p->owner->lock);
01546    }
01547    ast_set_flag(p, H323_NEEDDESTROY);
01548    ast_mutex_unlock(&p->lock);
01549 
01550    if(gH323Debug)
01551       ast_verbose("+++   onCallCleared\n");
01552 
01553    return OO_OK;
01554 }
01555 
01556 static void ooh323_delete_user(struct ooh323_user *user)
01557 {
01558    struct ooh323_user *prev=NULL, *cur=NULL;
01559 
01560    if(gH323Debug)
01561       ast_verbose("---   ooh323_delete_user\n");
01562 
01563    if(user)
01564    {   
01565       cur = userl.users;
01566       ast_mutex_lock(&userl.lock);
01567       while(cur)
01568       {
01569          if(cur==user) break;
01570          prev = cur;
01571          cur = cur->next;
01572       }
01573 
01574       if(cur)
01575       {
01576          if(prev)
01577             prev->next = cur->next;
01578          else
01579             userl.users = cur->next;
01580       }
01581       ast_mutex_unlock(&userl.lock);
01582 
01583       free(user);
01584    }  
01585 
01586    if(gH323Debug)
01587       ast_verbose("+++   ooh323_delete_user\n");
01588 
01589 }
01590 
01591 void ooh323_delete_peer(struct ooh323_peer *peer)
01592 {
01593    struct ooh323_peer *prev=NULL, *cur=NULL;
01594 
01595    if(gH323Debug)
01596       ast_verbose("---   ooh323_delete_peer\n");
01597 
01598    if(peer)
01599    {   
01600       cur = peerl.peers;
01601       ast_mutex_lock(&peerl.lock);
01602       while(cur)
01603       {
01604          if(cur==peer) break;
01605          prev = cur;
01606          cur = cur->next;
01607       }
01608 
01609       if(cur)
01610       {
01611          if(prev)
01612             prev->next = cur->next;
01613          else
01614             peerl.peers = cur->next;
01615       }
01616       ast_mutex_unlock(&peerl.lock);
01617 
01618       if(peer->h323id)   free(peer->h323id);
01619       if(peer->email)    free(peer->email);
01620       if(peer->url)      free(peer->url);
01621       if(peer->e164)     free(peer->e164);
01622 
01623       free(peer);
01624    }  
01625 
01626    if(gH323Debug)
01627       ast_verbose("+++   ooh323_delete_peer\n");
01628 
01629 }
01630 
01631 
01632 
01633 static struct ooh323_user *build_user(const char *name, struct ast_variable *v)
01634 {
01635    struct ooh323_user *user=NULL;
01636 
01637    if(gH323Debug)
01638       ast_verbose("---   build_user\n");
01639 
01640    user = (struct ooh323_user *)malloc(sizeof(struct ooh323_user));
01641    if (user) {
01642       memset(user, 0, sizeof(struct ooh323_user));
01643       ast_mutex_init(&user->lock);
01644       strncpy(user->name, name, sizeof(user->name)-1);
01645       user->capability = gCapability;
01646       memcpy(&user->prefs, &gPrefs, sizeof(struct ast_codec_pref));
01647       user->rtptimeout = gRTPTimeout;
01648       user->dtmfmode = gDTMFMode;
01649       /* set default context */
01650       strncpy(user->context, gContext, sizeof(user->context)-1);
01651       strncpy(user->accountcode, gAccountcode, sizeof(user->accountcode)-1);
01652       user->amaflags = gAMAFLAGS;
01653 
01654       while(v) {
01655          if (!strcasecmp(v->name, "context")) {
01656             strncpy(user->context, v->value, sizeof(user->context) - 1);
01657          }
01658          else if (!strcasecmp(v->name, "incominglimit")) {
01659             user->incominglimit = atoi(v->value);
01660             if (user->incominglimit < 0)
01661                user->incominglimit = 0;
01662          }
01663          else if (!strcasecmp(v->name, "accountcode")) {
01664             strncpy(user->accountcode, v->value, sizeof(user->accountcode)-1);
01665          }
01666          else if (!strcasecmp(v->name, "rtptimeout")) {
01667             user->rtptimeout = atoi(v->value);
01668             if(user->rtptimeout < 0)
01669                user->rtptimeout = gRTPTimeout;
01670          }
01671          else if (!strcasecmp(v->name, "disallow")) {
01672             ast_parse_allow_disallow(&user->prefs, &user->capability, 
01673                                      v->value, 0);
01674          }
01675          else if (!strcasecmp(v->name, "allow")) {
01676             const char* tcodecs = v->value;
01677             if(!strcasecmp(v->value, "all")) {
01678                tcodecs = "ulaw,alaw,g729,g723,gsm";
01679             }
01680             ast_parse_allow_disallow(&user->prefs, &user->capability, 
01681                                      tcodecs, 1);
01682          }
01683          else if (!strcasecmp(v->name, "amaflags")) {
01684             user->amaflags = ast_cdr_amaflags2int(v->value);
01685          }
01686          else if (!strcasecmp(v->name, "ip")) {
01687             strncpy(user->mIP, v->value, sizeof(user->mIP)-1);
01688             user->mUseIP = 1;
01689          }
01690          else if (!strcasecmp(v->name, "dtmfmode")) {
01691             if(!strcasecmp(v->value, "rfc2833"))
01692                user->dtmfmode = H323_DTMF_RFC2833;
01693             else if(!strcasecmp(v->value, "q931keypad"))
01694                user->dtmfmode = H323_DTMF_Q931;
01695             else if(!strcasecmp(v->value, "h245alphanumeric"))
01696                user->dtmfmode = H323_DTMF_H245ALPHANUMERIC;
01697             else if(!strcasecmp(v->value, "h245signal"))
01698                user->dtmfmode = H323_DTMF_H245SIGNAL;
01699          } 
01700          v = v->next;
01701       }
01702    }
01703 
01704    if(gH323Debug)
01705       ast_verbose("+++   build_user\n");
01706 
01707    return user;
01708 }
01709 
01710 static struct ooh323_peer *build_peer(const char *name, struct ast_variable *v, int friend_type)
01711 {
01712    struct ooh323_peer *peer=NULL;
01713 
01714    if(gH323Debug)
01715       ast_verbose("---   build_peer\n");
01716 
01717    peer = (struct ooh323_peer *)malloc(sizeof(struct ooh323_peer));
01718    if (peer) {
01719       memset(peer, 0, sizeof(struct ooh323_peer));
01720       ast_mutex_init(&peer->lock);
01721       strncpy(peer->name, name, sizeof(peer->name)-1);
01722       peer->capability = gCapability;
01723       memcpy(&peer->prefs, &gPrefs, sizeof(struct ast_codec_pref));
01724       peer->rtptimeout = gRTPTimeout;
01725       strncpy(peer->accountcode, gAccountcode, sizeof(peer->accountcode)-1);
01726       peer->amaflags = gAMAFLAGS;
01727       peer->dtmfmode = gDTMFMode;
01728       if(0 == friend_type) {
01729          peer->mFriend = 1;
01730       }
01731 
01732       while(v) {
01733          if (!strcasecmp(v->name, "h323id")) {
01734             peer->h323id = (char*)malloc(strlen(v->value)+1);
01735             if(!peer->h323id)
01736             {
01737                ast_log(LOG_ERROR, "Could not allocate memory for h323id of "
01738                                   "peer %s\n", name);
01739                ooh323_delete_peer(peer);
01740                return NULL;
01741             }
01742             strcpy(peer->h323id, v->value);
01743          } 
01744          else if (!strcasecmp(v->name, "e164")) {
01745             peer->e164 = (char*)malloc(strlen(v->value)+1);
01746             if(!peer->e164)
01747             {
01748                ast_log(LOG_ERROR, "Could not allocate memory for e164 of "
01749                                   "peer %s\n", name);
01750                ooh323_delete_peer(peer);
01751                return NULL;
01752             }
01753             strcpy(peer->e164, v->value);
01754          } 
01755          else  if (!strcasecmp(v->name, "email")) {
01756             peer->email = (char*)malloc(strlen(v->value)+1);
01757             if(!peer->email)
01758             {
01759                ast_log(LOG_ERROR, "Could not allocate memory for email of "
01760                                   "peer %s\n", name);
01761                ooh323_delete_peer(peer);
01762                return NULL;
01763             }
01764             strcpy(peer->email, v->value);
01765          } 
01766          else if (!strcasecmp(v->name, "url")) {
01767             peer->url = (char*)malloc(strlen(v->value)+1);
01768             if(!peer->url)
01769             {
01770                ast_log(LOG_ERROR, "Could not allocate memory for h323id of "
01771                                   "peer %s\n", name);
01772                ooh323_delete_peer(peer);
01773                return NULL;
01774             }
01775             strcpy(peer->url, v->value);
01776          }
01777          else if (!strcasecmp(v->name, "port")) {
01778             peer->port = atoi(v->value);
01779          } 
01780          else if (!strcasecmp(v->name, "ip")) {
01781             strncpy(peer->ip, v->value, sizeof(peer->ip)-1);
01782          } 
01783          else if (!strcasecmp(v->name, "outgoinglimit")) {
01784             peer->outgoinglimit = atoi(v->value);
01785             if (peer->outgoinglimit < 0)
01786                peer->outgoinglimit = 0;
01787          }
01788          else if (!strcasecmp(v->name, "accountcode")) {
01789             strncpy(peer->accountcode, v->value, sizeof(peer->accountcode)-1);
01790          }
01791          else if (!strcasecmp(v->name, "rtptimeout")) {
01792             peer->rtptimeout = atoi(v->value);
01793             if(peer->rtptimeout < 0)
01794                peer->rtptimeout = gRTPTimeout;
01795          }
01796          else if (!strcasecmp(v->name, "disallow")) {
01797             ast_parse_allow_disallow(&peer->prefs, &peer->capability, 
01798                                      v->value, 0); 
01799          }
01800          else if (!strcasecmp(v->name, "allow")) {
01801             const char* tcodecs = v->value;
01802             if(!strcasecmp(v->value, "all")) {
01803                tcodecs = "ulaw,alaw,g729,g723,gsm";
01804             }
01805             ast_parse_allow_disallow(&peer->prefs, &peer->capability, 
01806                                      tcodecs, 1);             
01807          }
01808          else if (!strcasecmp(v->name,  "amaflags")) {
01809             peer->amaflags = ast_cdr_amaflags2int(v->value);
01810          }
01811          else if (!strcasecmp(v->name, "dtmfmode")) {
01812             if(!strcasecmp(v->value, "rfc2833"))
01813                peer->dtmfmode = H323_DTMF_RFC2833;
01814             else if(!strcasecmp(v->value, "q931keypad"))
01815                peer->dtmfmode = H323_DTMF_Q931;
01816             else if(!strcasecmp(v->value, "h245alphanumeric"))
01817                peer->dtmfmode = H323_DTMF_H245ALPHANUMERIC;
01818             else if(!strcasecmp(v->value, "h245signal"))
01819                peer->dtmfmode = H323_DTMF_H245SIGNAL;
01820          }
01821          v = v->next;
01822       }
01823    }
01824 
01825    if(gH323Debug)
01826       ast_verbose("+++   build_peer\n");
01827 
01828    return peer;
01829 }
01830 
01831 static int ooh323_do_reload(void)
01832 {
01833    if(gH323Debug) {
01834       ast_verbose("---   ooh323_do_reload\n");
01835    }
01836 
01837    delete_users();
01838    delete_peers();
01839    reload_config();
01840 
01841    if(gH323Debug) {
01842       ast_verbose("+++   ooh323_do_reload\n");
01843    }
01844 
01845    return 0;
01846 }
01847 
01848 /*--- h323_reload: Force reload of module from cli ---*/
01849 static int ooh323_reload(int fd, int argc, char *argv[])
01850 {
01851 
01852    if(gH323Debug)
01853       ast_verbose("---   ooh323_reload\n");
01854 
01855    ast_mutex_lock(&h323_reload_lock);
01856    if (h323_reloading) {
01857       ast_verbose("Previous OOH323 reload not yet done\n");
01858    } 
01859    else {
01860       h323_reloading = 1;
01861    }
01862    ast_mutex_unlock(&h323_reload_lock);
01863    restart_monitor();
01864 
01865    if(gH323Debug)
01866       ast_verbose("+++   ooh323_reload\n");
01867 
01868    return 0;
01869 }
01870 
01871 static int reload(void *mod)
01872 {
01873    return ooh323_reload(0, 0, NULL);
01874 }
01875 
01876 int reload_config(void)
01877 {
01878    int format;
01879    struct ooAliases  *pNewAlias = NULL;
01880    struct ast_config *cfg;
01881    struct ast_variable *v;
01882    struct ooh323_user *user = NULL;
01883    struct ooh323_peer *peer = NULL;
01884    char *cat;
01885    const char *utype;
01886 
01887    if(gH323Debug)
01888       ast_verbose("---   reload_config\n");
01889 
01890    cfg = ast_config_load((char*)config);
01891 
01892    /* We *must* have a config file otherwise stop immediately */
01893    if (!cfg) {
01894       ast_log(LOG_NOTICE, "Unable to load config %s, OOH323 disabled\n",config);
01895       return 1;
01896    }
01897 
01898    /* Inintialize everything to default */
01899    strcpy(gLogFile, DEFAULT_LOGFILE);
01900    gPort = 1720;
01901    gIP[0]='\0';
01902    strcpy(gCallerID, DEFAULT_H323ID);
01903    gCapability = AST_FORMAT_ULAW;
01904    memset(&gPrefs, 0, sizeof(struct ast_codec_pref));
01905    gDTMFMode = H323_DTMF_RFC2833;
01906    gRasGkMode = RasNoGatekeeper;
01907    gGatekeeper[0] = '\0';
01908    gRTPTimeout = 60;
01909    strcpy(gAccountcode, DEFAULT_H323ACCNT);
01910    gFastStart = 1;
01911    gTunneling = 1;
01912    gTOS = 0;
01913    strcpy(gContext, DEFAULT_CONTEXT);
01914    gAliasList = NULL;
01915    gMediaWaitForConnect = 0;
01916    ooconfig.mTCPPortStart = 12030;
01917    ooconfig.mTCPPortEnd = 12230;
01918 
01919    v = ast_variable_browse(cfg, "general");
01920    while(v) {
01921    
01922       if (!strcasecmp(v->name, "port")) {
01923          gPort = (int)strtol(v->value, NULL, 10);
01924       } 
01925       else if (!strcasecmp(v->name, "bindaddr")) {
01926          strncpy(gIP, v->value, sizeof(gIP)-1);
01927       } 
01928       else if (!strcasecmp(v->name, "h225portrange")) {
01929          char* endlimit = 0;
01930          char temp[512];
01931          strncpy(temp, v->value, sizeof(temp) - 1);   
01932          /* char *temp = ast_strdupa(v->value); */
01933          endlimit = strchr(temp, ',');
01934          if (endlimit) {
01935             *endlimit = '\0';
01936             endlimit++;
01937             ooconfig.mTCPPortStart = atoi(temp);
01938             ooconfig.mTCPPortEnd = atoi(endlimit);
01939 
01940             if(ooH323EpSetTCPPortRange(ooconfig.mTCPPortStart, 
01941                                        ooconfig.mTCPPortEnd) == OO_FAILED) {
01942                ast_log(LOG_ERROR, "h225portrange: Failed to set range\n");
01943             }
01944          } 
01945          else {
01946             ast_log(LOG_ERROR, "h225portrange: Invalid format, separate port range with \",\"\n");
01947          }
01948       }
01949       else if (!strcasecmp(v->name, "gateway")) {
01950          gIsGateway = ast_true(v->value);
01951       } 
01952       else if (!strcasecmp(v->name, "faststart")) {
01953          gFastStart = ast_true(v->value);
01954          if(gFastStart)
01955             ooH323EpEnableFastStart();
01956          else
01957             ooH323EpDisableFastStart();
01958       } 
01959       else if (!strcasecmp(v->name, "mediawaitforconnect")) {
01960          gMediaWaitForConnect = ast_true(v->value);
01961          if(gMediaWaitForConnect)
01962             ooH323EpEnableMediaWaitForConnect();
01963          else 
01964             ooH323EpDisableMediaWaitForConnect();
01965       } 
01966       else if (!strcasecmp(v->name, "h245tunneling")) {
01967          gTunneling = ast_true(v->value);
01968          if(gTunneling)
01969             ooH323EpEnableH245Tunneling();
01970          else
01971             ooH323EpDisableH245Tunneling();
01972       }
01973       else if (!strcasecmp(v->name, "h323id")) {
01974          pNewAlias = malloc(sizeof(struct ooAliases));
01975          if(!pNewAlias){
01976             ast_log(LOG_ERROR, "Failed to allocate memory for h323id alias\n");
01977             return 1;
01978          }
01979          pNewAlias->type =  T_H225AliasAddress_h323_ID;
01980          pNewAlias->value = strdup(v->value);
01981          pNewAlias->next = gAliasList;
01982          gAliasList = pNewAlias;
01983          pNewAlias = NULL;
01984       }
01985       else if (!strcasecmp(v->name, "e164")) {
01986          pNewAlias = malloc(sizeof(struct ooAliases));
01987          if(!pNewAlias){
01988             ast_log(LOG_ERROR, "Failed to allocate memory for e164 alias\n");
01989             return 1;
01990          }
01991          pNewAlias->type =  T_H225AliasAddress_dialedDigits;
01992          pNewAlias->value = strdup(v->value);
01993          pNewAlias->next = gAliasList;
01994          gAliasList = pNewAlias;
01995          pNewAlias = NULL;
01996       }
01997       else if (!strcasecmp(v->name, "email")) {
01998          pNewAlias = malloc(sizeof(struct ooAliases));
01999          if(!pNewAlias){
02000             ast_log(LOG_ERROR, "Failed to allocate memory for email alias\n");
02001             return 1;
02002          }
02003          pNewAlias->type =  T_H225AliasAddress_email_ID;
02004          pNewAlias->value = strdup(v->value);
02005          pNewAlias->next = gAliasList;
02006          gAliasList = pNewAlias;
02007          pNewAlias = NULL;
02008       }
02009       else if (!strcasecmp(v->name, "callerid")) {
02010          strncpy(gCallerID, v->value, sizeof(gCallerID)-1);
02011       }
02012       else if (!strcasecmp(v->name, "incominglimit")) {
02013          gIncomingLimit = atoi(v->value);
02014       }
02015       else if (!strcasecmp(v->name, "outgoinglimit")) {
02016          gOutgoingLimit = atoi(v->value);
02017       }
02018       else if (!strcasecmp(v->name, "gatekeeper")) {
02019          if (!strcasecmp(v->value, "DISABLE")) {
02020             gRasGkMode = RasNoGatekeeper;
02021          } 
02022          else if (!strcasecmp(v->value, "DISCOVER")) {
02023             gRasGkMode = RasDiscoverGatekeeper;
02024          } 
02025          else {
02026             gRasGkMode = RasUseSpecificGatekeeper;
02027             strncpy(gGatekeeper, v->value, sizeof(gGatekeeper)-1);
02028          }
02029       }
02030       else if (!strcasecmp(v->name, "logfile")) {
02031          strncpy(gLogFile, v->value, sizeof(gLogFile)-1);
02032       }
02033       else if (!strcasecmp(v->name, "context")) {
02034          strncpy(gContext, v->value, sizeof(gContext)-1);
02035          ast_verbose(VERBOSE_PREFIX_3 "  == Setting default context to %s\n", 
02036                                                       gContext);
02037       }
02038       else if (!strcasecmp(v->name, "rtptimeout")) {
02039          gRTPTimeout = atoi(v->value);
02040          if(gRTPTimeout <= 0)
02041             gRTPTimeout = 60;
02042       }
02043       else if (!strcasecmp(v->name, "tos")) {
02044          if (sscanf(v->value, "%i", &format) == 1)
02045             gTOS = format & 0xff;
02046          else if (!strcasecmp(v->value, "lowdelay"))
02047             gTOS = IPTOS_LOWDELAY;
02048          else if (!strcasecmp(v->value, "throughput"))
02049             gTOS = IPTOS_THROUGHPUT;
02050          else if (!strcasecmp(v->value, "reliability"))
02051             gTOS = IPTOS_RELIABILITY;
02052          else if (!strcasecmp(v->value, "mincost"))
02053             gTOS = IPTOS_MINCOST;
02054          else if (!strcasecmp(v->value, "none"))
02055             gTOS = 0;
02056          else
02057             ast_log(LOG_WARNING, "Invalid tos value at line %d, should be "
02058                                  "'lowdelay', 'throughput', 'reliability', "
02059                                  "'mincost', or 'none'\n", v->lineno);
02060       }
02061       else if(!strcasecmp(v->name, "amaflags")) {
02062          gAMAFLAGS = ast_cdr_amaflags2int(v->value);
02063       }
02064       else if(!strcasecmp(v->name, "accountcode")) {
02065          strncpy(gAccountcode, v->value, sizeof(gAccountcode)-1);
02066       }
02067       else if (!strcasecmp(v->name, "disallow")) {
02068          ast_parse_allow_disallow(&gPrefs, &gCapability, v->value, 0);
02069       }
02070       else if (!strcasecmp(v->name, "allow")) {
02071          const char* tcodecs = v->value;
02072          if(!strcasecmp(v->value, "all")) {
02073             tcodecs = "ulaw,alaw,g729,g723,gsm";
02074          }
02075          ast_parse_allow_disallow(&gPrefs, &gCapability, tcodecs, 1);
02076       }
02077       else if (!strcasecmp(v->name, "dtmfmode")) {
02078          if (!strcasecmp(v->value, "inband"))
02079             gDTMFMode=H323_DTMF_INBAND;
02080          else if (!strcasecmp(v->value, "rfc2833"))
02081             gDTMFMode = H323_DTMF_RFC2833;
02082          else if (!strcasecmp(v->value, "q931keypad"))
02083             gDTMFMode = H323_DTMF_Q931;
02084          else if (!strcasecmp(v->value, "h245alphanumeric"))
02085             gDTMFMode = H323_DTMF_H245ALPHANUMERIC;
02086          else if (!strcasecmp(v->value, "h245signal"))
02087             gDTMFMode = H323_DTMF_H245SIGNAL;
02088          else {
02089             ast_log(LOG_WARNING, "Unknown dtmf mode '%s', using rfc2833\n", 
02090                                                                     v->value);
02091             gDTMFMode = H323_DTMF_RFC2833;
02092          }
02093       }  
02094       v = v->next;
02095    }
02096    
02097    cat = ast_category_browse(cfg, NULL);
02098    while(cat) 
02099    {
02100       if(strcasecmp(cat, "general")) 
02101       {
02102          int friend_type = 0;
02103          utype = ast_variable_retrieve(cfg, cat, "type");
02104          if(utype)
02105          {
02106             friend_type = strcasecmp(utype, "friend");
02107             if(!strcmp(utype, "user") || 0 == friend_type)
02108             {
02109                user = build_user(cat, ast_variable_browse(cfg, cat));
02110                if (user) 
02111                {
02112                   ast_mutex_lock(&userl.lock);
02113                   user->next = userl.users;
02114                   userl.users = user;
02115                   ast_mutex_unlock(&userl.lock);
02116                }
02117                else {
02118                   ast_log(LOG_WARNING, "Failed to build user %s\n", cat);
02119                }
02120             }
02121             if(!strcasecmp(utype, "peer") || 0 == friend_type)
02122             {
02123                peer = build_peer(cat, ast_variable_browse(cfg, cat), friend_type);
02124                if(peer)
02125                {
02126                   ast_mutex_lock(&peerl.lock);
02127                   peer->next = peerl.peers;
02128                   peerl.peers = peer;
02129                   ast_mutex_unlock(&peerl.lock);
02130                }
02131                else {
02132                   ast_log(LOG_WARNING, "Failed to build peer %s\n", cat);
02133                }
02134             }
02135          }
02136       }
02137       cat = ast_category_browse(cfg, cat);
02138    }
02139    ast_config_destroy(cfg);
02140 
02141 
02142    /* Determine ip address if neccessary */
02143    if(!strlen(gIP))
02144    {
02145       ooGetLocalIPAddress(gIP);
02146       if(!strcmp(gIP, "127.0.0.1"))
02147       {
02148          ast_log(LOG_NOTICE, "Failed to determine local ip address. Please "
02149                             "specify it in ooh323.conf. OOH323 Disabled\n");
02150          return 1;
02151       }
02152    }
02153 
02154    if(gH323Debug)
02155       ast_verbose("+++   reload_config\n");
02156 
02157    return 0;
02158 
02159 }
02160 
02161 
02162 static int ooh323_show_peer(int fd, int argc, char *argv[])
02163 {
02164    struct ooh323_peer *prev=NULL, *peer=NULL;
02165    char formats[512];
02166    char ip_port[30];
02167    if(argc !=  4)
02168       return RESULT_SHOWUSAGE;
02169  
02170  
02171    ast_mutex_lock(&peerl.lock);
02172    peer = peerl.peers;
02173    while(peer)
02174    {
02175       ast_mutex_lock(&peer->lock);
02176       if(!strcmp(peer->name, argv[3]))
02177          break;
02178       else{
02179          prev = peer;
02180          peer = peer->next;
02181          ast_mutex_unlock(&prev->lock);
02182       }
02183    }
02184 
02185    if(peer)
02186    {
02187       sprintf(ip_port, "%s:%d", peer->ip, peer->port);
02188       ast_cli(fd, "%-15.15s%s\n", "Name: ", peer->name);
02189       ast_cli(fd, "%-15.15s%s", "Format Prefs: ", "(");
02190       print_codec_to_cli(fd, &peer->prefs);
02191       ast_cli(fd, ")\n");
02192       ast_cli(fd, "%-15.15s", "DTMF Mode: ");
02193       if(peer->dtmfmode & H323_DTMF_RFC2833)
02194          ast_cli(fd, "%s\n", "rfc2833");
02195       else if(peer->dtmfmode & H323_DTMF_Q931)
02196          ast_cli(fd, "%s\n", "q931keypad");
02197       else if(peer->dtmfmode & H323_DTMF_H245ALPHANUMERIC)
02198          ast_cli(fd, "%s\n", "h245alphanumeric");
02199       else if(peer->dtmfmode & H323_DTMF_H245SIGNAL)
02200          ast_cli(fd, "%s\n", "h245signal");
02201       else
02202          ast_cli(fd, "%s\n", "unknown");
02203       ast_cli(fd, "%-15.15s%s\n", "AccountCode: ", peer->accountcode);
02204       ast_cli(fd, "%-15.15s%s\n", "AMA flags: ", 
02205                                             ast_cdr_flags2str(peer->amaflags));
02206       ast_cli(fd, "%-15.15s%s\n", "IP:Port: ", ip_port);
02207       ast_cli(fd, "%-15.15s%d\n", "OutgoingLimit: ", peer->outgoinglimit);
02208       ast_cli(fd, "%-15.15s%d\n", "rtptimeout: ", peer->rtptimeout);
02209       ast_mutex_unlock(&peer->lock);
02210    }
02211    else{
02212      ast_cli(fd, "Peer %s not found\n", argv[3]);
02213      ast_cli(fd, "\n");
02214    }
02215    ast_mutex_unlock(&peerl.lock);
02216 
02217    return RESULT_SUCCESS;
02218 }
02219 
02220 static int ooh323_show_peers(int fd, int argc, char *argv[])
02221 {
02222    struct ooh323_peer *prev = NULL, *peer=NULL;
02223    char formats[512];
02224    char ip_port[30];
02225 #define FORMAT  "%-15.15s  %-15.15s  %-23.23s  %-s\n"
02226 
02227    if (argc != 3)
02228       return RESULT_SHOWUSAGE;
02229 
02230    ast_cli(fd, FORMAT, "Name", "Accountcode", "ip:port", "Formats");
02231    
02232    ast_mutex_lock(&peerl.lock);
02233    peer = peerl.peers;
02234    while(peer)
02235    {
02236      ast_mutex_lock(&peer->lock);
02237      sprintf(ip_port, "%s:%d", peer->ip, peer->port);
02238      ast_cli(fd, FORMAT, peer->name, 
02239                  peer->accountcode,
02240                  ip_port,
02241                  ast_getformatname_multiple(formats,512,peer->capability));
02242      prev = peer;
02243      peer = peer->next;
02244      ast_mutex_unlock(&prev->lock);
02245 
02246    }
02247    ast_mutex_unlock(&peerl.lock);
02248 #undef FORMAT
02249    return RESULT_SUCCESS;
02250 }
02251 
02252 /*! \brief Print codec list from preference to CLI/manager */
02253 static void print_codec_to_cli(int fd, struct ast_codec_pref *pref)
02254 {
02255    int x, codec;
02256 
02257    for(x = 0; x < 32 ; x++) {
02258       codec = ast_codec_pref_index(pref, x);
02259       if (!codec)
02260          break;
02261       ast_cli(fd, "%s", ast_getformatname(codec));
02262       ast_cli(fd, ":%d", pref->framing[x]);
02263       if (x < 31 && ast_codec_pref_index(pref, x + 1))
02264          ast_cli(fd, ",");
02265       }
02266    if (!x)
02267       ast_cli(fd, "none");
02268 }
02269 
02270 static int ooh323_show_user(int fd, int argc, char *argv[])
02271 {
02272    struct ooh323_user *prev=NULL, *user=NULL;
02273    char formats[512];
02274 
02275    if(argc !=  4)
02276       return RESULT_SHOWUSAGE;
02277  
02278  
02279    ast_mutex_lock(&userl.lock);
02280    user = userl.users;
02281    while(user)
02282    {
02283       ast_mutex_lock(&user->lock);
02284       if(!strcmp(user->name, argv[3])) {
02285          break;
02286       }
02287       else {
02288          prev = user;
02289          user = user->next;
02290          ast_mutex_unlock(&prev->lock);
02291       }
02292    }
02293 
02294    if(user)
02295    {
02296       ast_cli(fd, "%-15.15s%s\n", "Name: ", user->name);
02297       ast_cli(fd, "%-15.15s%s", "Format Prefs: ", "(");
02298       print_codec_to_cli(fd, &user->prefs);
02299       ast_cli(fd, ")\n");
02300       ast_cli(fd, "%-15.15s", "DTMF Mode: ");
02301       if(user->dtmfmode & H323_DTMF_RFC2833)
02302          ast_cli(fd, "%s\n", "rfc2833");
02303       else if(user->dtmfmode & H323_DTMF_Q931)
02304          ast_cli(fd, "%s\n", "q931keypad");
02305       else if(user->dtmfmode & H323_DTMF_H245ALPHANUMERIC)
02306          ast_cli(fd, "%s\n", "h245alphanumeric");
02307       else if(user->dtmfmode & H323_DTMF_H245SIGNAL)
02308          ast_cli(fd, "%s\n", "h245signal");
02309       else
02310          ast_cli(fd, "%s\n", "unknown");
02311       ast_cli(fd, "%-15.15s%s\n", "AccountCode: ", user->accountcode);
02312       ast_cli(fd, "%-15.15s%s\n", "AMA flags: ", 
02313                                             ast_cdr_flags2str(user->amaflags));
02314       ast_cli(fd, "%-15.15s%s\n", "Context: ", user->context);
02315       ast_cli(fd, "%-15.15s%d\n", "IncomingLimit: ", user->incominglimit);
02316       ast_cli(fd, "%-15.15s%d\n", "rtptimeout: ", user->rtptimeout);
02317       ast_mutex_unlock(&user->lock);
02318    }
02319    else{
02320      ast_cli(fd, "User %s not found\n", argv[3]);
02321      ast_cli(fd, "\n");
02322    }
02323    ast_mutex_unlock(&userl.lock);
02324 
02325    return RESULT_SUCCESS;
02326 }
02327 
02328 static int ooh323_show_users(int fd, int argc, char *argv[])
02329 {
02330    struct ooh323_user *prev = NULL, *user=NULL;
02331    char formats[512];
02332 #define FORMAT1  "%-15.15s  %-15.15s  %-15.15s  %-s\n"
02333 
02334    if (argc != 3)
02335       return RESULT_SHOWUSAGE;
02336 
02337    ast_cli(fd, FORMAT1, "Username", "Accountcode", "Context", "Formats");
02338    
02339    ast_mutex_lock(&userl.lock);
02340    user = userl.users;
02341    while(user)
02342    {
02343      ast_mutex_lock(&user->lock);
02344      ast_cli(fd, FORMAT1, user->name, 
02345                  user->accountcode, user->context,
02346                  ast_getformatname_multiple(formats,512,user->capability));
02347      prev = user;
02348      user = user->next;
02349      ast_mutex_unlock(&prev->lock);
02350 
02351    }
02352    ast_mutex_unlock(&userl.lock);
02353 #undef FORMAT1
02354    return RESULT_SUCCESS;
02355 
02356 }
02357 
02358 static int ooh323_do_debug(int fd, int argc, char *argv[])
02359 {
02360    if (argc != 2)
02361       return RESULT_SHOWUSAGE;
02362 
02363    gH323Debug = TRUE;
02364    ast_cli(fd, "OOH323 Debugging Enabled\n");
02365    return RESULT_SUCCESS;
02366 }
02367 
02368 static int ooh323_no_debug(int fd, int argc, char *argv[])
02369 
02370 {
02371    if (argc != 3)
02372       return RESULT_SHOWUSAGE;
02373    gH323Debug = FALSE;
02374    ast_cli(fd, "OOH323 Debugging Disabled\n");
02375    return RESULT_SUCCESS;
02376 }
02377 
02378 static int ooh323_show_channels(int fd, int argc, char *argv[])
02379 {
02380    return RESULT_SUCCESS;
02381 }
02382 
02383 static int ooh323_show_config(int fd, int argc, char *argv[])
02384 {
02385    char value[512];
02386    ooAliases *pAlias = NULL, *pAliasNext=NULL;;
02387 
02388    ast_cli(fd, "\nObjective Open H.323 Channel Driver's Config:\n");
02389    sprintf(value, "%s:%d", gIP, gPort);
02390    ast_cli(fd, "%-20s%s\n", "IP:Port: ", value);
02391    ast_cli(fd, "%-20s%d-%d\n", "H.225 port range: ", 
02392       ooconfig.mTCPPortStart, ooconfig.mTCPPortEnd);
02393    ast_cli(fd, "%-20s%s\n", "FastStart", gFastStart?"yes":"no");
02394    ast_cli(fd, "%-20s%s\n", "Tunneling", gTunneling?"yes":"no");
02395    ast_cli(fd, "%-20s%s\n", "CallerId", gCallerID);
02396    ast_cli(fd, "%-20s%s\n", "MediaWaitForConnect", 
02397       gMediaWaitForConnect?"yes":"no");
02398 
02399 #if (0)
02400    extern OOH323EndPoint gH323ep;
02401    ast_cli(fd, "%-20s%s\n", "FASTSTART", 
02402       (OO_TESTFLAG(gH323ep.flags, OO_M_FASTSTART) != 0) ? "yes" : "no");
02403    ast_cli(fd, "%-20s%s\n", "TUNNELING", 
02404       (OO_TESTFLAG(gH323ep.flags, OO_M_TUNNELING) != 0) ? "yes" : "no");
02405    ast_cli(fd, "%-20s%s\n", "MEDIAWAITFORCONN",
02406       (OO_TESTFLAG(gH323ep.flags, OO_M_MEDIAWAITFORCONN) != 0) ? "yes" : "no");
02407 #endif
02408 
02409    if(gRasGkMode == RasNoGatekeeper)
02410       sprintf(value, "%s", "No Gatekeeper");
02411    else if(gRasGkMode == RasDiscoverGatekeeper)
02412       sprintf(value, "%s", "Discover");
02413    else
02414       sprintf(value, "%s", gGatekeeper);
02415 
02416    ast_cli(fd,  "%-20s%s\n", "Gatekeeper:", value);
02417 
02418    ast_cli(fd,  "%-20s%s\n", "H.323 LogFile:", gLogFile);   
02419 
02420    ast_cli(fd,  "%-20s%s\n", "Context:", gContext);
02421    
02422    ast_cli(fd,  "%-20s%s\n", "Capability:", 
02423            ast_getformatname_multiple(value,512,gCapability));
02424 
02425    ast_cli(fd, "%-20s", "DTMF Mode: ");
02426    if(gDTMFMode & H323_DTMF_RFC2833)
02427       ast_cli(fd, "%s\n", "rfc2833");
02428    else if (gDTMFMode & H323_DTMF_Q931)
02429       ast_cli(fd, "%s\n", "q931keypad");
02430    else if(gDTMFMode & H323_DTMF_H245ALPHANUMERIC)
02431       ast_cli(fd, "%s\n", "h245alphanumeric");
02432    else if(gDTMFMode & H323_DTMF_H245SIGNAL)
02433       ast_cli(fd, "%s\n", "h245signal");
02434    else
02435       ast_cli(fd, "%s\n", "unknown");
02436 
02437    ast_cli(fd, "%-20s%s\n", "AccountCode: ", gAccountcode);
02438 
02439    ast_cli(fd, "%-20s%s\n", "AMA flags: ", ast_cdr_flags2str(gAMAFLAGS));
02440 
02441    pAlias = gAliasList;
02442    if(pAlias)
02443    {
02444      ast_cli(fd, "%-20s\n", "Aliases: ");
02445    }
02446    while(pAlias)
02447    {
02448       pAliasNext = pAlias->next;
02449       if(pAliasNext)
02450       {
02451          ast_cli(fd,"\t%-30s\t%-30s\n",pAlias->value, pAliasNext->value);
02452          pAlias = pAliasNext->next;
02453       }
02454       else{
02455          ast_cli(fd,"\t%-30s\n",pAlias->value);
02456          pAlias = pAlias->next;
02457       }
02458    }         
02459    return RESULT_SUCCESS;
02460 }
02461 
02462 static char show_config_usage[]=
02463 "Usage: ooh323 show config\n"
02464 "       Shows global configuration of H.323 channel driver\n";
02465 
02466 static char debug_usage[] = 
02467 "Usage: ooh323 debug\n"
02468 "       Enables debugging of OOH323 channel driver\n";
02469 
02470 static struct ast_cli_entry  cli_debug =
02471    { { "ooh323", "debug", NULL }, ooh323_do_debug, "Enable OOH323 debugging", debug_usage };
02472 
02473 static char no_debug_usage[] = 
02474 "Usage: ooh323 no debug\n"
02475 "       Disables debugging of OOH323 channel driver\n";
02476 
02477 static struct ast_cli_entry  cli_no_debug =
02478    { { "ooh323", "no", "debug", NULL }, ooh323_no_debug, "Disable OOH323 debugging", no_debug_usage };
02479 
02480 static char show_users_usage[] = 
02481 "Usage: ooh323 show users \n"
02482 "       Lists all known OOH323 users.\n";
02483 
02484 static struct ast_cli_entry  cli_show_users = 
02485    { { "ooh323", "show", "users", NULL }, ooh323_show_users, "Show defined OOH323 users", show_users_usage };
02486 
02487 static char show_user_usage[] = 
02488 "Usage: ooh323 show user <name> \n"
02489 "       List details of specific OOH323 user.\n";
02490 
02491 static struct ast_cli_entry  cli_show_user =
02492    { { "ooh323", "show", "user", NULL }, ooh323_show_user, "Show details on specific OOH323 user", show_user_usage };
02493 
02494 static char show_peers_usage[] = 
02495 "Usage: ooh323 show peers \n"
02496 "       Lists all known OOH323 peers.\n";
02497 
02498 static struct ast_cli_entry  cli_show_peers = 
02499    { { "ooh323", "show", "peers", NULL }, ooh323_show_peers, "Show defined OOH323 peers", show_peers_usage };
02500 
02501 static char show_peer_usage[] = 
02502 "Usage: ooh323 show peer <name> \n"
02503 "       List details of specific OOH323 peer.\n";
02504 
02505 static struct ast_cli_entry  cli_show_peer =
02506    { { "ooh323", "show", "peer", NULL }, ooh323_show_peer, "Show details on specific OOH323 peer", show_peer_usage };
02507 
02508 static struct ast_cli_entry cli_show_config =
02509    { { "ooh323", "show", "config", NULL }, ooh323_show_config, "Show details on global configuration of H.323 channel driver", show_config_usage };         
02510 
02511 
02512 
02513 
02514 static int load_module(void)
02515 {
02516    int res;
02517    struct ooAliases * pNewAlias=NULL;
02518    struct ooh323_peer *peer=NULL;
02519    OOH225MsgCallbacks h225Callbacks = {0, 0, 0, 0};
02520 
02521    OOH323CALLBACKS h323Callbacks ={
02522      .onNewCallCreated = onNewCallCreated,
02523      .onAlerting = onAlerting,
02524      .onIncomingCall = NULL,
02525      .onOutgoingCall = NULL,
02526      .onCallEstablished = onCallEstablished,
02527      .onCallCleared = onCallCleared,
02528      .openLogicalChannels=NULL,
02529      .onReceivedDTMF = &ooh323_onReceivedDigit
02530    };
02531 
02532    ast_log(LOG_NOTICE, 
02533       "---------------------------------------------------------------------------------\n"
02534       "---  ******* IMPORTANT NOTE ***********\n"
02535       "---\n"
02536       "---  This module is currently unsupported.  Use it at your own risk.\n"
02537       "---\n"
02538       "---------------------------------------------------------------------------------\n");
02539 
02540    h225Callbacks.onReceivedSetup = &ooh323_onReceivedSetup;
02541 
02542    userl.users = NULL;
02543    ast_mutex_init(&userl.lock);
02544    peerl.peers = NULL;
02545    ast_mutex_init(&peerl.lock);
02546          
02547    /* ast_register_atexit(&ast_ooh323c_exit); */
02548 
02549    sched = sched_context_create();
02550    if (!sched) {
02551       ast_log(LOG_WARNING, "Unable to create schedule context\n");
02552    }
02553    io = io_context_create();
02554    if (!io) {
02555       ast_log(LOG_WARNING, "Unable to create I/O context\n");
02556    }
02557 
02558 
02559    res = reload_config();
02560    if (!res) {
02561       /* Make sure we can register our OOH323 channel type */
02562       if (ast_channel_register(&ooh323_tech)) {
02563          ast_log(LOG_ERROR, "Unable to register channel class %s\n", type);
02564          return 0;
02565       }
02566       ast_rtp_proto_register(&ooh323_rtp);
02567       ast_cli_register(&cli_show_users);
02568       ast_cli_register(&cli_show_user);
02569       ast_cli_register(&cli_show_peers);
02570       ast_cli_register(&cli_show_peer);
02571       ast_cli_register(&cli_show_config);
02572       ast_cli_register(&cli_debug);
02573       ast_cli_register(&cli_no_debug);
02574 
02575        /* fire up the H.323 Endpoint */       
02576       if(OO_OK != ooH323EpInitialize(OO_CALLMODE_AUDIOCALL, gLogFile))
02577       {
02578          ast_log(LOG_ERROR, "Failed to initialize OOH323 endpoint-"
02579                             "OOH323 Disabled\n");
02580          return 1;
02581       }
02582 
02583       if(gIsGateway)
02584          ooH323EpSetAsGateway();
02585 
02586       ooH323EpDisableAutoAnswer();
02587       ooH323EpSetH225MsgCallbacks(h225Callbacks);
02588       ooH323EpSetTraceLevel(OOTRCLVLDBGC);
02589       ooH323EpSetLocalAddress(gIP, gPort);
02590       ooH323EpSetCallerID(gCallerID);
02591  
02592       /* Set aliases if any */
02593       pNewAlias = gAliasList;
02594       while(pNewAlias)
02595       {
02596          switch(pNewAlias->type)
02597          {
02598          case T_H225AliasAddress_h323_ID:
02599             ooH323EpAddAliasH323ID(pNewAlias->value);
02600             break;
02601          case T_H225AliasAddress_dialedDigits:   
02602             ooH323EpAddAliasDialedDigits(pNewAlias->value);
02603             break;
02604          case T_H225AliasAddress_email_ID:   
02605             ooH323EpAddAliasEmailID(pNewAlias->value);
02606             break;
02607          default:
02608             ;
02609          }
02610          pNewAlias = pNewAlias->next;
02611       }
02612 
02613       ast_mutex_lock(&peerl.lock);
02614       peer = peerl.peers;
02615       while(peer)
02616       {
02617          if(peer->h323id) ooH323EpAddAliasH323ID(peer->h323id);
02618          if(peer->email)  ooH323EpAddAliasEmailID(peer->email);
02619          if(peer->e164)   ooH323EpAddAliasDialedDigits(peer->e164);
02620          if(peer->url)    ooH323EpAddAliasURLID(peer->url);
02621          peer = peer->next;
02622       }
02623       ast_mutex_unlock(&peerl.lock);
02624    
02625 
02626       if(gMediaWaitForConnect)
02627          ooH323EpEnableMediaWaitForConnect();
02628       else 
02629          ooH323EpDisableMediaWaitForConnect();
02630 
02631       /* Fast start and tunneling options */
02632       if(gFastStart)
02633          ooH323EpEnableFastStart();
02634       else
02635          ooH323EpDisableFastStart();
02636 
02637       if(!gTunneling)
02638          ooH323EpDisableH245Tunneling();
02639 
02640       /* Gatekeeper */
02641       if(gRasGkMode == RasUseSpecificGatekeeper)
02642          ooGkClientInit(gRasGkMode, gGatekeeper, 0);
02643       else if(gRasGkMode == RasDiscoverGatekeeper)
02644          ooGkClientInit(gRasGkMode, 0, 0);
02645 
02646       /* Register callbacks */
02647       ooH323EpSetH323Callbacks(h323Callbacks);
02648 
02649       /* Add endpoint capabilities */
02650       if(ooh323c_set_capability(&gPrefs, gCapability, gDTMFMode)<0) {
02651          ast_log(LOG_ERROR, "Capabilities failure for OOH323. OOH323 Disabled.\n");
02652          return 1;
02653       }
02654 
02655       /* Create H.323 listener */
02656       if(ooCreateH323Listener()!= OO_OK)
02657       {
02658          ast_log(LOG_ERROR, "OOH323 Listener Creation failure. "
02659                             "OOH323 DISABLED\n");
02660       
02661          ooH323EpDestroy();
02662          return 1;
02663       }
02664       
02665       if(ooh323c_start_stack_thread()  <0)
02666       {
02667          ast_log(LOG_ERROR, "Failed to start OOH323 stack thread. "
02668                             "OOH323 DISABLED\n");
02669          ooH323EpDestroy();
02670          return 1;
02671       }
02672       /* And start the monitor for the first time */
02673       restart_monitor();
02674    }
02675 
02676    return 0;
02677 }
02678 
02679 
02680 static void *do_monitor(void *data)
02681 {
02682    int res;
02683    int reloading;
02684    struct ooh323_pvt *h323 = NULL;
02685    time_t t;
02686 
02687    for(;;) {
02688       struct ooh323_pvt *h323_next;
02689       /* Check for a reload request */
02690       ast_mutex_lock(&h323_reload_lock);
02691       reloading = h323_reloading;
02692       h323_reloading = 0;
02693       ast_mutex_unlock(&h323_reload_lock);
02694       if (reloading) {
02695          if (option_verbose > 0) {
02696             ast_verbose(VERBOSE_PREFIX_1 "Reloading H.323\n");
02697          }
02698          ooh323_do_reload();
02699       }
02700 
02701       /* Check for interfaces needing to be killed */
02702       ast_mutex_lock(&iflock);
02703       time(&t);
02704       h323 = iflist;
02705       while(h323) {
02706          h323_next = h323->next;
02707 
02708          /*TODO: Need to add rtptimeout keepalive support */
02709          if(ast_test_flag(h323, H323_NEEDDESTROY)) {
02710             ooh323_destroy (h323);
02711          }
02712          h323 = h323_next;
02713       }
02714       ast_mutex_unlock(&iflock);
02715       pthread_testcancel();
02716 
02717       /* Wait for sched or io */
02718       res = ast_sched_wait(sched);
02719       if ((res < 0) || (res > 1000)) {
02720          res = 1000;
02721       }
02722       res = ast_io_wait(io, res);
02723       pthread_testcancel();
02724       ast_mutex_lock(&monlock);
02725       if (res >= 0) {
02726          ast_sched_runq(sched);
02727       }
02728       ast_mutex_unlock(&monlock);
02729    }
02730    /* Never reached */
02731    return NULL;
02732 }
02733 
02734 int restart_monitor(void)
02735 {
02736    pthread_attr_t attr;
02737 
02738    /* If we're supposed to be stopped -- stay stopped */
02739    if (monitor_thread == AST_PTHREADT_STOP)
02740       return 0;
02741    if (ast_mutex_lock(&monlock)) {
02742       ast_log(LOG_WARNING, "Unable to lock monitor\n");
02743       return -1;
02744    }
02745    if (monitor_thread == pthread_self()) {
02746       ast_mutex_unlock(&monlock);
02747       ast_log(LOG_WARNING, "Cannot kill myself\n");
02748       return -1;
02749    }
02750    if (monitor_thread != AST_PTHREADT_NULL) {
02751       /* Wake up the thread */
02752       pthread_kill(monitor_thread, SIGURG);
02753    } 
02754    else {
02755       pthread_attr_init(&attr);
02756       pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
02757       /* Start a new monitor */
02758       if (ast_pthread_create(&monitor_thread, &attr, do_monitor, NULL) < 0) {
02759          ast_mutex_unlock(&monlock);
02760          ast_log(LOG_ERROR, "Unable to start monitor thread.\n");
02761          return -1;
02762       }
02763    }
02764    ast_mutex_unlock(&monlock);
02765    return 0;
02766 }
02767 
02768 
02769 
02770 int ooh323_destroy(struct ooh323_pvt *p)
02771 {
02772    /* NOTE: Assumes iflock already acquired */
02773    struct ooh323_pvt *prev=NULL,*cur = NULL;
02774 
02775 
02776    if(gH323Debug)
02777    {
02778       ast_verbose("---   ooh323_destroy \n");
02779 
02780       if(p)
02781          ast_verbose(" Destroying %s\n", p->username);
02782    }
02783 
02784    cur = iflist;
02785    while(cur)
02786    {
02787       if(cur == p) { break; }
02788       prev = cur;
02789       cur = cur->next;
02790    }
02791 
02792    if(cur)
02793    {
02794       ast_mutex_lock(&cur->lock);
02795       if(prev)
02796          prev->next = cur->next;
02797       else
02798          iflist = cur->next;
02799 
02800       if(cur->callToken) {
02801          free(cur->callToken);
02802          cur->callToken = 0;
02803       }
02804 
02805       if(cur->username) {
02806          free(cur->username);
02807          cur->username = 0;
02808       }
02809 
02810       if(cur->host) {
02811          free(cur->host);
02812          cur->host = 0;
02813       }
02814 
02815       if(cur->callerid_name) {
02816          free(cur->callerid_name);
02817          cur->callerid_name = 0;
02818       }
02819       
02820       if(cur->callerid_num) {
02821          free(cur->callerid_num);
02822          cur->callerid_num = 0;
02823       }
02824 
02825 
02826       if (cur->rtp) {
02827          ast_rtp_destroy(cur->rtp);
02828          cur->rtp = 0;
02829       }
02830    
02831       /* Unlink us from the owner if we have one */
02832       if (cur->owner) {
02833          while(ast_mutex_trylock(&cur->owner->lock))
02834          {
02835             ast_log(LOG_DEBUG,"Failed to grab lock, trying again\n");
02836             ast_mutex_unlock(&cur->lock);
02837             usleep(1);
02838             ast_mutex_lock(&cur->lock);
02839          }           
02840          ast_log(LOG_DEBUG, "Detaching from %s\n", cur->owner->name);
02841          cur->owner->tech_pvt = NULL;
02842          ast_mutex_unlock(&cur->owner->lock);
02843          cur->owner = NULL;
02844       }
02845   
02846       if (cur->vad) {
02847          ast_dsp_free(cur->vad);
02848          cur->vad = NULL;
02849       }
02850       ast_mutex_unlock(&cur->lock);
02851       ast_mutex_destroy(&cur->lock);
02852 
02853       free(cur);
02854    }
02855 
02856    if(gH323Debug)
02857       ast_verbose("+++   ooh323_destroy\n");
02858 
02859    return 0;
02860 }
02861 
02862 int delete_peers()
02863 {
02864    struct ooh323_peer *cur=NULL, *prev=NULL;
02865    ast_mutex_lock(&peerl.lock);
02866    cur = peerl.peers;
02867    while(cur)
02868    {
02869       prev = cur;
02870       cur = cur->next;
02871 
02872       ast_mutex_destroy(&prev->lock);
02873       if(prev->h323id)   free(prev->h323id);
02874       if(prev->email)    free(prev->email);
02875       if(prev->url)      free(prev->url);
02876       if(prev->e164)     free(prev->e164);
02877       free(prev);
02878 
02879       if(cur == peerl.peers) { break; }
02880    }
02881    peerl.peers = NULL;
02882    ast_mutex_unlock(&peerl.lock);
02883    return 0;
02884 }
02885 
02886 int delete_users()
02887 {
02888    struct ooh323_user *cur=NULL, *prev=NULL;
02889    ast_mutex_lock(&userl.lock);
02890    cur = userl.users;
02891    while(cur)
02892    {
02893       prev = cur;
02894       cur = cur->next;
02895       ast_mutex_destroy(&prev->lock);
02896       free(prev);
02897       if(cur == userl.users) { break; }
02898    }
02899    userl.users = NULL;
02900    ast_mutex_unlock(&userl.lock);
02901    return 0;
02902 }
02903   
02904 static int unload_module(void)
02905 {
02906    struct ooh323_pvt *p;
02907    struct ooAliases *cur=NULL, *prev=NULL;
02908 
02909    if(gH323Debug) {
02910       ast_verbose("--- ooh323  unload_module \n");
02911    }
02912    /* First, take us out of the channel loop */
02913    ast_cli_unregister(&cli_show_users);
02914    ast_cli_unregister(&cli_show_user);
02915    ast_cli_unregister(&cli_show_peers);
02916    ast_cli_unregister(&cli_show_peer);
02917    ast_cli_unregister(&cli_show_config);
02918    ast_cli_unregister(&cli_debug);
02919    ast_cli_unregister(&cli_no_debug);
02920    ast_rtp_proto_unregister(&ooh323_rtp);
02921    ast_channel_unregister(&ooh323_tech);
02922    /* ast_unregister_atexit(&ast_ooh323c_exit); */
02923 
02924    if(gH323Debug) {
02925       ast_verbose("  unload_module - hanging up all interfaces\n");
02926    }
02927    if (!ast_mutex_lock(&iflock)) {
02928       /* Hangup all interfaces if they have an owner */
02929       p = iflist;
02930       while(p) {
02931          if (p->owner) {
02932             ast_softhangup(p->owner, AST_SOFTHANGUP_APPUNLOAD);
02933          }
02934          p = p->next;
02935       }
02936       iflist = NULL;
02937       ast_mutex_unlock(&iflock);
02938    } 
02939    else {
02940       ast_log(LOG_WARNING, "Unable to lock the interface list\n");
02941       return -1;
02942    }
02943 
02944 
02945    if(gH323Debug) {
02946       ast_verbose("  unload_module - stopping monitor thread\n");
02947    }  
02948    if (monitor_thread != AST_PTHREADT_NULL)
02949    {
02950      if(!ast_mutex_lock(&monlock)) {
02951         if (monitor_thread && (monitor_thread != AST_PTHREADT_STOP)) {
02952             pthread_cancel(monitor_thread);
02953             pthread_kill(monitor_thread, SIGURG);
02954             pthread_join(monitor_thread, NULL);
02955          }
02956          monitor_thread = AST_PTHREADT_STOP;
02957          ast_mutex_unlock(&monlock);
02958       } 
02959       else {
02960          ast_log(LOG_WARNING, "Unable to lock the monitor\n");
02961          return -1;
02962       }
02963    }
02964 
02965 
02966    if(gH323Debug) {
02967      ast_verbose("   unload_module - stopping stack thread\n");
02968    }
02969    ooh323c_stop_stack_thread();
02970 
02971 
02972    if(gH323Debug) {
02973       ast_verbose("  unload_module - freeing up memory used by interfaces\n");
02974    }
02975    if (!ast_mutex_lock(&iflock)) {
02976       struct ooh323_pvt *pl;
02977 
02978       /* Destroy all the interfaces and free their memory */
02979       p = iflist;
02980       while(p) {
02981          pl = p;
02982          p = p->next;
02983          /* Free associated memory */
02984          ooh323_destroy(pl);
02985       }
02986       iflist = NULL;
02987       ast_mutex_unlock(&iflock);
02988    } 
02989    else {
02990       ast_log(LOG_WARNING, "Unable to lock the interface list\n");
02991       return -1;
02992    }
02993  
02994 
02995    if(gH323Debug) {
02996       ast_verbose("  unload_module - deleting users\n");
02997    }
02998    delete_users();
02999 
03000 
03001    if(gH323Debug) {
03002       ast_verbose("  unload_module - deleting peers\n");
03003    }
03004    delete_peers();
03005 
03006 
03007    if(gH323Debug) {
03008       ast_verbose("  unload_module - Freeing up alias list\n");
03009    }
03010    cur = gAliasList;
03011    while(cur)
03012    {
03013      prev = cur;
03014      cur = cur->next;
03015      free(prev->value);
03016      free(prev);
03017    }
03018    gAliasList = NULL;
03019 
03020 
03021    if(gH323Debug) {
03022       ast_verbose("   unload_module- destroying OOH323 endpoint \n");
03023    }
03024    ooH323EpDestroy();
03025 
03026    if(gH323Debug) {
03027       ast_verbose("+++ ooh323  unload_module \n");   
03028    }
03029 
03030    return 0;
03031 }
03032 
03033 
03034 
03035 static enum ast_rtp_get_result ooh323_get_rtp_peer(struct ast_channel *chan, struct ast_rtp **rtp)
03036 {
03037    struct ooh323_pvt *p = NULL;
03038    enum ast_rtp_get_result res = AST_RTP_TRY_PARTIAL;
03039 
03040    if (!(p = (struct ooh323_pvt *) chan->tech_pvt))
03041    return AST_RTP_GET_FAILED;
03042 
03043    *rtp = p->rtp;
03044 
03045    if (!(p->rtp)) {
03046    return AST_RTP_GET_FAILED;
03047    }
03048    res = AST_RTP_TRY_NATIVE;
03049 
03050    return res;
03051 }
03052 
03053 static enum ast_rtp_get_result ooh323_get_vrtp_peer(struct ast_channel *chan, struct ast_rtp **rtp)
03054 {
03055    struct ooh323_pvt *p = NULL;
03056    enum ast_rtp_get_result res = AST_RTP_TRY_PARTIAL;
03057 
03058    if (!(p = (struct ooh323_pvt *) chan->tech_pvt))
03059         return AST_RTP_GET_FAILED;
03060 
03061    *rtp = p->vrtp;
03062 
03063    if (!(p->rtp)) {
03064         return AST_RTP_GET_FAILED;
03065    }
03066    res = AST_RTP_TRY_NATIVE;
03067 
03068    return res;
03069 }
03070 
03071 
03072 int ooh323_update_capPrefsOrderForCall
03073    (ooCallData *call, struct ast_codec_pref *prefs)
03074 {
03075    int i=0;
03076    int codec = ast_codec_pref_index(prefs, i);
03077 
03078    ooResetCapPrefs(call);
03079    while(codec)
03080    {
03081       ooAppendCapToCapPrefs(call, ooh323_convertAsteriskCapToH323Cap(codec));
03082       codec = ast_codec_pref_index(prefs, ++i);
03083    }
03084 
03085    return 0;
03086 }
03087 
03088 
03089 int ooh323_convertAsteriskCapToH323Cap(int cap)
03090 {
03091    char formats[512];
03092    switch (cap) {
03093       case AST_FORMAT_ULAW:
03094          return OO_G711ULAW64K;
03095       case AST_FORMAT_ALAW:
03096          return OO_G711ALAW64K;
03097       case AST_FORMAT_GSM:
03098          return OO_GSMFULLRATE;
03099       case AST_FORMAT_G729A:
03100          return OO_G729A;
03101       case AST_FORMAT_G723_1:
03102          return OO_G7231;
03103       case AST_FORMAT_H263:
03104          return OO_H263VIDEO;
03105       default:
03106          ast_log(LOG_NOTICE, "Don't know how to deal with mode %s\n", 
03107                         ast_getformatname_multiple(formats,512,cap));
03108          return -1;
03109    }
03110 }
03111 
03112 static int ooh323_set_rtp_peer
03113    (struct ast_channel *chan, struct ast_rtp *rtp, struct ast_rtp *vrtp, 
03114     int codecs, int nat_active)
03115 {
03116    /* XXX Deal with Video */
03117    struct ooh323_pvt *p;
03118    struct sockaddr_in them;
03119    struct sockaddr_in us;
03120    int mode;
03121 
03122    if(gH323Debug)
03123       ast_verbose("---   ooh323_set_peer - %s\n", chan->name);
03124 
03125    if (!rtp) {
03126       return 0;
03127    }
03128 
03129    mode = ooh323_convertAsteriskCapToH323Cap(chan->writeformat); 
03130    p = (struct ooh323_pvt *) chan->tech_pvt;
03131    if (!p) {
03132       ast_log(LOG_ERROR, "No Private Structure, this is bad\n");
03133       return -1;
03134    }
03135    ast_rtp_get_peer(rtp, &them);
03136    ast_rtp_get_us(rtp, &us);
03137    return 0;
03138 }
03139 
03140 
03141 
03142 
03143 int configure_local_rtp(struct ooh323_pvt *p, ooCallData *call)
03144 {
03145    struct sockaddr_in us;
03146    ooMediaInfo mediaInfo;
03147    int x, format=0;     
03148 
03149    if(gH323Debug)
03150       ast_verbose("---   configure_local_rtp\n");
03151 
03152    if (p->rtp){
03153    ast_rtp_codec_setpref(p->rtp, &p->prefs);
03154    }
03155 
03156    /* figure out our local RTP port and tell the H.323 stack about it*/
03157    ast_rtp_get_us(p->rtp, &us);
03158 
03159    strncpy(mediaInfo.lMediaIP, ast_inet_ntoa(us.sin_addr), sizeof(mediaInfo.lMediaIP)-1);
03160    mediaInfo.lMediaPort = ntohs(us.sin_port);
03161    mediaInfo.lMediaCntrlPort = mediaInfo.lMediaPort +1;
03162    for(x=0; 0 != (format=ast_codec_pref_index(&p->prefs, x)); x++)
03163    {
03164       strcpy(mediaInfo.dir, "transmit");
03165       mediaInfo.cap = ooh323_convertAsteriskCapToH323Cap(format);
03166       ooAddMediaInfo(call, mediaInfo);
03167       strcpy(mediaInfo.dir, "receive");
03168       ooAddMediaInfo(call, mediaInfo);
03169       if(mediaInfo.cap == OO_G729A) {
03170          strcpy(mediaInfo.dir, "transmit");
03171          mediaInfo.cap = OO_G729;
03172          ooAddMediaInfo(call, mediaInfo);
03173          strcpy(mediaInfo.dir, "receive");
03174          ooAddMediaInfo(call, mediaInfo);
03175       }
03176    }
03177 
03178    if(gH323Debug)
03179       ast_verbose("+++   configure_local_rtp\n");
03180 
03181    return 1;
03182 }
03183 
03184 void setup_rtp_connection(ooCallData *call, const char *remoteIp, 
03185                           int remotePort)
03186 {
03187    struct ooh323_pvt *p = NULL;
03188    struct sockaddr_in them;
03189 
03190    if(gH323Debug)
03191       ast_verbose("---   setup_rtp_connection\n");
03192 
03193    /* Find the call or allocate a private structure if call not found */
03194    p = find_call(call); 
03195 
03196    if (!p) {
03197       ast_log(LOG_ERROR, "Something is wrong: rtp\n");
03198       return;
03199    }
03200 
03201    them.sin_family = AF_INET;
03202    them.sin_addr.s_addr = inet_addr(remoteIp); /* only works for IPv4 */
03203    them.sin_port = htons(remotePort);
03204    ast_rtp_set_peer(p->rtp, &them);
03205 
03206    if(gH323Debug)
03207       ast_verbose("+++   setup_rtp_connection\n");
03208 
03209    return;
03210 }
03211 
03212 void close_rtp_connection(ooCallData *call)
03213 {
03214    struct ooh323_pvt *p = NULL;
03215 
03216    if(gH323Debug)
03217       ast_verbose("---   close_rtp_connection\n");
03218 
03219    p = find_call(call);
03220    if(!p)
03221    {
03222       ast_log(LOG_ERROR, "Couldn't find matching call to close rtp "
03223                          "connection\n");
03224       return;
03225    }
03226    ast_mutex_lock(&p->lock);
03227    if(p->rtp){
03228       ast_rtp_stop(p->rtp);
03229    }
03230    ast_mutex_unlock(&p->lock);
03231 
03232    if(gH323Debug)
03233       ast_verbose("+++   close_rtp_connection\n");
03234 
03235    return;
03236 }
03237 
03238 
03239 int update_our_aliases(ooCallData *call, struct ooh323_pvt *p)
03240 {
03241    int updated = -1;
03242    ooAliases *psAlias=NULL;
03243    
03244    if(!call->ourAliases)
03245       return updated;
03246    psAlias = call->ourAliases;
03247    while(psAlias)
03248    {
03249       if(psAlias->type == T_H225AliasAddress_h323_ID)
03250       {
03251          strncpy(p->callee_h323id, psAlias->value, sizeof(p->callee_h323id)-1);
03252          updated = 1;
03253       }
03254       if(psAlias->type == T_H225AliasAddress_dialedDigits)
03255       {
03256          strncpy(p->callee_dialedDigits, psAlias->value, 
03257                                         sizeof(p->callee_dialedDigits)-1);
03258          updated = 1;
03259       }
03260       if(psAlias->type == T_H225AliasAddress_url_ID)
03261       {
03262          strncpy(p->callee_url, psAlias->value, sizeof(p->callee_url)-1);
03263          updated = 1;
03264       }
03265       if(psAlias->type == T_H225AliasAddress_email_ID)
03266       {
03267          strncpy(p->callee_email, psAlias->value, sizeof(p->callee_email)-1);
03268          updated = 1;
03269       }
03270       psAlias = psAlias->next;
03271    }
03272    return updated;
03273 }
03274 
03275 struct ast_frame *ooh323_rtp_read
03276                              (struct ast_channel *ast, struct ooh323_pvt *p)
03277 {
03278    /* Retrieve audio/etc from channel.  Assumes p->lock is already held. */
03279    struct ast_frame *f;
03280    static struct ast_frame null_frame = { AST_FRAME_NULL, };
03281    switch(ast->fdno) {
03282       case 0:
03283          f = ast_rtp_read(p->rtp);   /* RTP Audio */
03284          break;
03285       case 1:
03286          f = ast_rtcp_read(p->rtp);   /* RTCP Control Channel */
03287          break;
03288       case 2:
03289          f = ast_rtp_read(p->vrtp);   /* RTP Video */
03290          break;
03291       case 3:
03292          f = ast_rtcp_read(p->vrtp);   /* RTCP Control Channel for video */
03293          break;
03294       default:
03295          f = &null_frame;
03296    }
03297    /* Don't send RFC2833 if we're not supposed to */
03298    if (f && (f->frametype == AST_FRAME_DTMF) && 
03299        !(p->dtmfmode & H323_DTMF_RFC2833))
03300       return &null_frame;
03301    if (p->owner) 
03302    {
03303       /* We already hold the channel lock */
03304       if (f->frametype == AST_FRAME_VOICE) 
03305       {
03306          if (f->subclass != p->owner->nativeformats) 
03307          {
03308             ast_log(LOG_DEBUG, "Oooh, format changed to %d\n", f->subclass);
03309             p->owner->nativeformats = f->subclass;
03310             ast_set_read_format(p->owner, p->owner->readformat);
03311             ast_set_write_format(p->owner, p->owner->writeformat);
03312          }
03313          if ((p->dtmfmode & H323_DTMF_INBAND) && p->vad) 
03314          {
03315             f = ast_dsp_process(p->owner, p->vad, f);
03316             if (f && (f->frametype == AST_FRAME_DTMF)) 
03317                ast_log(LOG_DEBUG, "* Detected inband DTMF '%c'\n",f->subclass);
03318          }
03319       }
03320    }
03321    return f;
03322 }
03323 
03324 
03325 int ooh323_convert_hangupcause_asteriskToH323(int cause)
03326 {
03327    switch(cause)
03328    {
03329       case AST_CAUSE_CALL_REJECTED:
03330          return OO_REASON_REMOTE_REJECTED;
03331       case AST_CAUSE_UNALLOCATED:
03332          return OO_REASON_NOUSER;
03333       case AST_CAUSE_BUSY:
03334          return OO_REASON_REMOTE_BUSY;
03335       case AST_CAUSE_BEARERCAPABILITY_NOTAVAIL:
03336          return OO_REASON_NOCOMMON_CAPABILITIES;
03337       case AST_CAUSE_CONGESTION:
03338          return OO_REASON_REMOTE_BUSY;
03339       case AST_CAUSE_NO_ANSWER:
03340          return OO_REASON_REMOTE_NOANSWER;
03341       case AST_CAUSE_NORMAL:
03342          return OO_REASON_REMOTE_CLEARED;
03343       case AST_CAUSE_FAILURE:
03344       default:
03345          return OO_REASON_UNKNOWN;
03346    }
03347 
03348    return 0;
03349 
03350 
03351 }
03352 
03353 int ooh323_convert_hangupcause_h323ToAsterisk(int cause)
03354 {
03355    switch(cause) {
03356       case OO_REASON_REMOTE_REJECTED:
03357          return AST_CAUSE_CALL_REJECTED;
03358       case OO_REASON_NOUSER: 
03359          return AST_CAUSE_UNALLOCATED;
03360       case OO_REASON_REMOTE_BUSY:
03361       case OO_REASON_LOCAL_BUSY:
03362          return AST_CAUSE_BUSY;
03363       case OO_REASON_NOCOMMON_CAPABILITIES:   /* No codecs approved */
03364          return AST_CAUSE_BEARERCAPABILITY_NOTAVAIL;
03365       case OO_REASON_REMOTE_CONGESTED:
03366       case OO_REASON_LOCAL_CONGESTED:
03367          return AST_CAUSE_CONGESTION;
03368       case OO_REASON_REMOTE_NOANSWER:
03369          return AST_CAUSE_NO_ANSWER;
03370       case OO_REASON_UNKNOWN: 
03371       case OO_REASON_INVALIDMESSAGE:
03372       case OO_REASON_TRANSPORTFAILURE:
03373          return AST_CAUSE_FAILURE;
03374       case OO_REASON_REMOTE_CLEARED:
03375          return AST_CAUSE_NORMAL;
03376       default:
03377          return AST_CAUSE_NORMAL;
03378    }
03379    /* Never reached */
03380    return 0;
03381 }
03382 
03383 #if 0
03384 void ast_ooh323c_exit()
03385 {
03386    ooGkClientDestroy();
03387 }
03388 #endif
03389 
03390 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Objective Systems H323 Channel");

Generated on Fri Jul 3 20:35:36 2009 for Asterisk - the Open Source PBX by  doxygen 1.5.9