[asterisk-commits] branch oej/strictrouting r18751 - in
/team/oej/strictrouting: ./ channels/ do...
asterisk-commits at lists.digium.com
asterisk-commits at lists.digium.com
Sun Apr 9 22:30:30 MST 2006
Author: oej
Date: Mon Apr 10 00:30:25 2006
New Revision: 18751
URL: http://svn.digium.com/view/asterisk?rev=18751&view=rev
Log:
Reset, resolve, go
Modified:
team/oej/strictrouting/ (props changed)
team/oej/strictrouting/channels/chan_sip.c
team/oej/strictrouting/doc/CODING-GUIDELINES
team/oej/strictrouting/res/res_config_pgsql.c
Propchange: team/oej/strictrouting/
------------------------------------------------------------------------------
automerge = http://edvina.net/training/
Propchange: team/oej/strictrouting/
------------------------------------------------------------------------------
--- svnmerge-integrated (original)
+++ svnmerge-integrated Mon Apr 10 00:30:25 2006
@@ -1,1 +1,1 @@
-/trunk:1-18662
+/trunk:1-18749
Modified: team/oej/strictrouting/channels/chan_sip.c
URL: http://svn.digium.com/view/asterisk/team/oej/strictrouting/channels/chan_sip.c?rev=18751&r1=18750&r2=18751&view=diff
==============================================================================
--- team/oej/strictrouting/channels/chan_sip.c (original)
+++ team/oej/strictrouting/channels/chan_sip.c Mon Apr 10 00:30:25 2006
@@ -151,6 +151,7 @@
#define SIP_MAX_LINES 64 /*!< Max amount of lines in SIP attachment (like SDP) */
#define SIP_MAX_PACKET 4096 /*!< Also from RFC 3261 (2543), should sub headers tho */
+#define INITIAL_CSEQ 101 /*!< our initial sip sequence number */
static const char desc[] = "Session Initiation Protocol (SIP)";
static const char config[] = "sip.conf";
@@ -1144,7 +1145,7 @@
if (localaddr && externip.sin_addr.s_addr &&
ast_apply_ha(localaddr, &theirs)) {
- if (externexpire && (time(NULL) >= externexpire)) {
+ if (externexpire && time(NULL) >= externexpire) {
struct ast_hostent ahp;
struct hostent *hp;
@@ -1446,9 +1447,9 @@
/*! \brief Pretend to ack all packets */
static int __sip_pretend_ack(struct sip_pvt *p)
{
- struct sip_pkt *cur=NULL;
-
- while(p->packets) {
+ struct sip_pkt *cur = NULL;
+
+ while (p->packets) {
if (cur == p->packets) {
ast_log(LOG_WARNING, "Have a packet that doesn't want to give up! %s\n", sip_methods[cur->method].text);
return -1;
@@ -1687,7 +1688,7 @@
else
speerobjs--;
clear_realm_authentication(peer->auth);
- peer->auth = (struct sip_auth *) NULL;
+ peer->auth = NULL;
if (peer->dnsmgr)
ast_dnsmgr_release(peer->dnsmgr);
free(peer);
@@ -1746,14 +1747,14 @@
if (!newpeername) { /* Did not find peer in realtime */
ast_log(LOG_WARNING, "Cannot Determine peer name ip=%s\n", iabuf);
ast_variables_destroy(var);
- return (struct sip_peer *) NULL;
+ return NULL;
}
/* Peer found in realtime, now build it in memory */
peer = build_peer(newpeername, var, !ast_test_flag(&global_flags[1], SIP_PAGE2_RTCACHEFRIENDS));
if (!peer) {
ast_variables_destroy(var);
- return (struct sip_peer *) NULL;
+ return NULL;
}
if (ast_test_flag(&global_flags[1], SIP_PAGE2_RTCACHEFRIENDS)) {
@@ -1868,11 +1869,9 @@
* realtime storage (defined in extconfig.conf) */
static struct sip_user *find_user(const char *name, int realtime)
{
- struct sip_user *u = NULL;
- u = ASTOBJ_CONTAINER_FIND(&userl,name);
- if (!u && realtime) {
+ struct sip_user *u = ASTOBJ_CONTAINER_FIND(&userl, name);
+ if (!u && realtime)
u = realtime_user(name);
- }
return u;
}
@@ -2131,7 +2130,7 @@
/* Remove link from peer to subscription of MWI */
if (p->relatedpeer && p->relatedpeer->mwipvt)
- p->relatedpeer->mwipvt = (struct sip_pvt *) NULL;
+ p->relatedpeer->mwipvt = NULL;
if (dumphistory)
sip_dump_history(p);
@@ -3173,8 +3172,7 @@
p->branch = ast_random();
make_our_tag(p->tag, sizeof(p->tag));
- /* Start with 101 instead of 1 */
- p->ocseq = 101;
+ p->ocseq = INITIAL_CSEQ;
if (sip_methods[intended_method].need_rtp) {
p->rtp = ast_rtp_new_with_bindaddr(sched, io, 1, 0, bindaddr.sin_addr);
@@ -3382,7 +3380,7 @@
reg->refresh = default_expiry;
reg->portno = porta ? atoi(porta) : 0;
reg->callid_valid = FALSE;
- reg->ocseq = 101;
+ reg->ocseq = INITIAL_CSEQ;
ASTOBJ_CONTAINER_LINK(®l, reg); /* Add the new registry entry to the list */
ASTOBJ_UNREF(reg,sip_registry_destroy);
return 0;
@@ -3874,12 +3872,9 @@
/*! \brief Copy one header field from one request to another */
static int copy_header(struct sip_request *req, struct sip_request *orig, char *field)
{
- char *tmp;
- tmp = get_header(orig, field);
- if (!ast_strlen_zero(tmp)) {
- /* Add what we're responding to */
+ const char *tmp = get_header(orig, field);
+ if (!ast_strlen_zero(tmp)) /* Add what we're responding to */
return add_header(req, field, tmp);
- }
ast_log(LOG_NOTICE, "No field '%s' present to copy\n", field);
return -1;
}
@@ -3887,17 +3882,15 @@
/*! \brief Copy all headers from one request to another */
static int copy_all_header(struct sip_request *req, struct sip_request *orig, char *field)
{
- char *tmp;
int start = 0;
int copied = 0;
for (;;) {
- tmp = __get_header(orig, field, &start);
- if (!ast_strlen_zero(tmp)) {
- /* Add what we're responding to */
- add_header(req, field, tmp);
- copied++;
- } else
+ const char *tmp = __get_header(orig, field, &start);
+ if (ast_strlen_zero(tmp))
break;
+ /* Add what we're responding to */
+ add_header(req, field, tmp);
+ copied++;
}
return copied ? 0 : -1;
}
@@ -3910,14 +3903,17 @@
Received: RFC 3261, rport RFC 3581 */
static int copy_via_headers(struct sip_pvt *p, struct sip_request *req, struct sip_request *orig, char *field)
{
- char tmp[256], *oh, *end;
+ char tmp[256], *end;
int start = 0;
int copied = 0;
char iabuf[INET_ADDRSTRLEN];
for (;;) {
- oh = __get_header(orig, field, &start);
- if (!ast_strlen_zero(oh)) {
+ const char *oh = __get_header(orig, field, &start);
+ if (ast_strlen_zero(oh))
+ break;
+
+ /* XXX reindent next block */
if (!copied) { /* Only check for empty rport in topmost via header */
char *rport;
char new[256];
@@ -3956,8 +3952,6 @@
add_header(req, field, oh);
}
copied++;
- } else
- break;
}
if (!copied) {
ast_log(LOG_NOTICE, "No header field '%s' present to copy\n", field);
@@ -4040,7 +4034,8 @@
if (maddr) {
maddr += 6;
hn = strspn(maddr, "0123456789.") + 1;
- if (hn > sizeof(hostname)) hn = sizeof(hostname);
+ if (hn > sizeof(hostname))
+ hn = sizeof(hostname);
ast_copy_string(hostname, maddr, hn);
}
@@ -4164,7 +4159,7 @@
}
/* Check for strict or loose router */
- if (ast_test_flag(p, SIP_STRICTROUTING)) {
+ if (ast_test_flag(&p->flags[0], SIP_STRICTROUTING)) {
if (option_debug) {
ast_log(LOG_DEBUG, "Strict routing enforced for session %s\n", p->callid);
add_header(req, "X-Asterisk-routing", "Strict routing enforced on this session");
@@ -4201,10 +4196,7 @@
add_header(req, "Via", p->via);
if (p->route) {
set_destination(p, p->route->hop);
- if (ast_test_flag(p, SIP_STRICTROUTING))
- add_route(req, p->route->next); /* Start with second hop */
- else
- add_route(req, p->route); /* Start with first hop */
+ add_route(req, ast_test_flag(&p->flags[0], SIP_STRICTROUTING) ? p->route->next : p->route);
}
ot = get_header(orig, "To");
@@ -4294,9 +4286,8 @@
{
char tmpdat[256];
struct tm tm;
- time_t t;
-
- time(&t);
+ time_t t = time(NULL);
+
gmtime_r(&t, &tm);
strftime(tmpdat, sizeof(tmpdat), "%a, %d %b %Y %T GMT", &tm);
add_header(req, "Date", tmpdat);
@@ -5031,7 +5022,7 @@
add_header(&req, "Allow", ALLOWED_METHODS);
if (p->options && p->options->addsipheaders ) {
struct ast_channel *ast;
- struct varshead *headp = (struct varshead *) NULL;
+ struct varshead *headp = NULL;
const struct ast_var_t *current;
ast = p->owner; /* The owner channel */
@@ -5513,12 +5504,10 @@
if (!ast_strlen_zero(r->authuser)) {
ast_string_field_set(p, peername, r->authuser);
ast_string_field_set(p, authname, r->authuser);
- } else {
- if (!ast_strlen_zero(r->username)) {
- ast_string_field_set(p, peername, r->username);
- ast_string_field_set(p, authname, r->username);
- ast_string_field_set(p, fromuser, r->username);
- }
+ } else if (!ast_strlen_zero(r->username)) {
+ ast_string_field_set(p, peername, r->username);
+ ast_string_field_set(p, authname, r->username);
+ ast_string_field_set(p, fromuser, r->username);
}
if (!ast_strlen_zero(r->username))
ast_string_field_set(p, username, r->username);
@@ -5659,12 +5648,10 @@
} else
of += 4;
/* Get just the username part */
- if ((c = strchr(dest, '@'))) {
+ if ((c = strchr(dest, '@')))
c = NULL;
- } else if ((c = strchr(of, '@'))) {
- *c = '\0';
- c++;
- }
+ else if ((c = strchr(of, '@')))
+ *c++ = '\0';
if (c) {
snprintf(referto, sizeof(referto), "<sip:%s@%s>", dest, c);
} else {
@@ -5908,6 +5895,7 @@
contact += 4;
/* Ditch arguments */
+ /* XXX this code is replicated also shortly below */
host = strchr(contact, ';');
if (host)
*host = '\0';
@@ -5918,13 +5906,11 @@
host = contact;
c = NULL;
} else {
- *host = '\0';
- host++;
+ *host++ = '\0';
}
pt = strchr(host, ':');
if (pt) {
- *pt = '\0';
- pt++;
+ *pt++ = '\0';
port = atoi(pt);
} else
port = DEFAULT_SIP_PORT;
@@ -5993,9 +5979,8 @@
*/
if (ast_strlen_zero(c) && ast_strlen_zero(expires)) {
/* If we have an active registration, tell them when the registration is going to expire */
- if ((p->expire > -1) && !ast_strlen_zero(p->fullcontact)) {
+ if (p->expire > -1 && !ast_strlen_zero(p->fullcontact))
pvt->expiry = ast_sched_when(sched, p->expire);
- }
return PARSE_REGISTER_QUERY;
} else if (!strcasecmp(c, "*") || !expiry) { /* Unregister this peer */
/* This means remove all registrations and return OK */
@@ -6035,14 +6020,11 @@
if (!n) {
n = c;
c = NULL;
- } else {
- *n = '\0';
- n++;
- }
+ } else
+ *n++ = '\0';
pt = strchr(n, ':');
if (pt) {
- *pt = '\0';
- pt++;
+ *pt++ = '\0';
port = atoi(pt);
} else
port = DEFAULT_SIP_PORT;
@@ -6074,10 +6056,8 @@
expiry = max_expiry;
if (expiry < min_expiry)
expiry = min_expiry;
- if (!ast_test_flag(&p->flags[0], SIP_REALTIME))
- p->expire = ast_sched_add(sched, (expiry + 10) * 1000, expire_register, p);
- else
- p->expire = -1;
+ p->expire = ast_test_flag(&p->flags[0], SIP_REALTIME) ? -1 :
+ ast_sched_add(sched, (expiry + 10) * 1000, expire_register, p);
pvt->expiry = expiry;
snprintf(data, sizeof(data), "%s:%d:%d:%s:%s", ast_inet_ntoa(iabuf, sizeof(iabuf), p->addr.sin_addr), ntohs(p->addr.sin_port), expiry, p->username, p->fullcontact);
if (!ast_test_flag(&p->flags[1], SIP_PAGE2_RT_FROMCONTACT))
@@ -6149,7 +6129,8 @@
p->route_persistant = backwards;
/* We build up head, then assign it to p->route when we're done */
- head = NULL; tail = head;
+ head = NULL;
+ tail = head;
/* 1st we pass through all the hops in any Record-Route headers */
for (;;) {
/* Each Record-Route header */
@@ -6191,7 +6172,7 @@
/* Only append the contact if we are dealing with a strict router */
if (!head || (!ast_strlen_zero(head->hop) && strstr(head->hop,";lr") == NULL) ) {
- ast_set_flag(p, SIP_STRICTROUTING);
+ ast_set_flag(&p->flags[0], SIP_STRICTROUTING);
/* 2nd append the Contact: if there is one */
/* Can be multiple Contact headers, comma separated values - we just take the first */
contact = get_header(req, "Contact");
@@ -6625,14 +6606,12 @@
}
/* Skip any options */
- if ((a = strchr(uri, ';'))) {
+ if ((a = strchr(uri, ';')))
*a = '\0';
- }
/* Get the target domain */
if ((a = strchr(uri, '@'))) {
- *a = '\0';
- a++;
+ *a++ = '\0';
colon = strchr(a, ':'); /* Remove :port */
if (colon)
*colon = '\0';
@@ -6729,10 +6708,9 @@
req = outgoing_req;
- if (!req) {
+ if (!req)
req = &sip_pvt->initreq;
- }
-
+
if (!( (p_refer_to = get_header(req, "Refer-To")) && (h_refer_to = ast_strdupa(p_refer_to)) )) {
ast_log(LOG_WARNING, "No Refer-To Header That's illegal\n");
return -1;
@@ -6838,11 +6816,11 @@
ast_string_field_set(sip_pvt, refer_to, refer_to);
if (referred_by)
ast_string_field_set(sip_pvt, referred_by, referred_by);
- if (h_contact) {
+ if (h_contact)
ast_string_field_set(sip_pvt, refer_contact, h_contact);
- }
sip_pvt->refer_call = NULL;
- if ((chan = sip_pvt->owner) && (peer = ast_bridged_channel(sip_pvt->owner))) {
+ chan = sip_pvt->owner;
+ if (chan && (peer = ast_bridged_channel(chan)) != NULL) {
pbx_builtin_setvar_helper(chan, "BLINDTRANSFER", peer->name);
pbx_builtin_setvar_helper(peer, "BLINDTRANSFER", chan->name);
}
@@ -6858,16 +6836,13 @@
static int get_also_info(struct sip_pvt *p, struct sip_request *oreq)
{
char tmp[256], *c, *a;
- struct sip_request *req;
+ struct sip_request *req = oreq;
- req = oreq;
if (!req)
req = &p->initreq;
ast_copy_string(tmp, get_header(req, "Also"), sizeof(tmp));
c = get_in_brackets(tmp);
-
-
if (strncmp(c, "sip:", 4)) {
ast_log(LOG_WARNING, "Huh? Not a SIP header (%s)?\n", c);
return -1;
@@ -6878,9 +6853,9 @@
if ((a = strchr(c, ';')))
*a = '\0';
- if (sip_debug_test_pvt(p)) {
+ if (sip_debug_test_pvt(p))
ast_verbose("Looking for %s in %s\n", c, p->context);
- }
+
if (ast_exists_extension(NULL, p->context, c, 1, NULL)) {
/* This is an unsupervised transfer */
if (option_debug)
@@ -6954,7 +6929,7 @@
int bytes = 0;
int maxbytes = outputsize - 1;
- if (!end || (end == input))
+ if (!end || end == input)
return NULL;
/* move away from "<" */
@@ -6975,17 +6950,15 @@
/* clear the empty characters in the begining*/
input = ast_skip_blanks(input);
/* clear the empty characters in the end */
- while(*end && (*end < 33) && end > input)
+ while(*end && *end < 33 && end > input)
end--;
if (end >= input) {
bytes = (int) (end - input) + 2;
/* protect the output buffer */
- if (bytes > maxbytes) {
+ if (bytes > maxbytes)
bytes = maxbytes;
- }
ast_copy_string(output, input, bytes);
- }
- else
+ } else
return NULL;
}
return output;
@@ -7045,7 +7018,7 @@
/* Terminate URI */
t = uri;
- while(*t && (*t > 32) && (*t != ';'))
+ while (*t && *t > 32 && *t != ';')
t++;
*t = '\0';
of = get_header(req, "From");
@@ -8507,13 +8480,10 @@
ASTOBJ_CONTAINER_TRAVERSE(&peerl, !result, do {
/* locking of the object is not required because only the name and flags are being compared */
- if (!strncasecmp(word, iterator->name, wordlen)) {
- if (flags2 && !ast_test_flag(&iterator->flags[1], flags2))
- continue;
- if (++which > state) {
- result = ast_strdup(iterator->name);
- }
- }
+ if (!strncasecmp(word, iterator->name, wordlen) &&
+ ast_test_flag(&iterator->flags[1], flags2) &&
+ ++which > state)
+ result = ast_strdup(iterator->name);
} while(0) );
return result;
}
@@ -8735,7 +8705,6 @@
if (!x)
ast_log(LOG_DEBUG, "Call '%s' has no history\n", dialog->callid);
ast_log(LOG_DEBUG, "\n---------- END SIP HISTORY for '%s' \n", dialog->callid);
-
}
@@ -8847,9 +8816,9 @@
if (p)
port = atoi(p);
hp = ast_gethostbyname(arg, &ahp);
- if (hp == NULL) {
+ if (hp == NULL)
return RESULT_SHOWUSAGE;
- }
+
debugaddr.sin_family = AF_INET;
memcpy(&debugaddr.sin_addr, hp->h_addr, sizeof(debugaddr.sin_addr));
debugaddr.sin_port = htons(port);
@@ -9134,7 +9103,7 @@
const char *username;
const char *secret;
const char *md5secret;
- struct sip_auth *auth = (struct sip_auth *) NULL; /* Realm authentication */
+ struct sip_auth *auth = NULL; /* Realm authentication */
if (!ast_strlen_zero(p->domain))
ast_copy_string(uri, p->domain, sizeof(uri));
@@ -10849,17 +10818,14 @@
/* Unlock locks so ast_hangup can do its magic */
ast_mutex_unlock(&p->lock);
c->hangupcause = AST_CAUSE_CALL_REJECTED;
- ast_hangup(c);
- ast_mutex_lock(&p->lock);
- c = NULL;
} else {
ast_mutex_unlock(&p->lock);
ast_setstate(c, AST_STATE_DOWN);
c->hangupcause = AST_CAUSE_NORMAL_CLEARING;
- ast_hangup(c);
- ast_mutex_lock(&p->lock);
- c = NULL;
}
+ ast_hangup(c);
+ ast_mutex_lock(&p->lock);
+ c = NULL;
}
break;
case AST_STATE_RING:
@@ -12004,8 +11970,7 @@
ast_copy_string(tmp, dest, sizeof(tmp));
host = strchr(tmp, '@');
if (host) {
- *host = '\0';
- host++;
+ *host++ = '\0';
ext = tmp;
} else {
ext = strchr(tmp, '/');
@@ -12152,10 +12117,7 @@
else if (strcasecmp(v->value, "never"))
ast_set_flag(&flags[0], SIP_PROG_INBAND_NO);
} else if (!strcasecmp(v->name, "allowguest")) {
- if (ast_true(v->value))
- global_allowguest = 1;
- else
- global_allowguest = 0;
+ global_allowguest = ast_true(v->value) ? 1 : 0;
} else if (!strcasecmp(v->name, "promiscredir")) {
ast_set_flag(&mask[0], SIP_PROMISCREDIR);
ast_set2_flag(&flags[0], ast_true(v->value), SIP_PROMISCREDIR);
@@ -12363,8 +12325,7 @@
} else if (!strcasecmp(v->name, "setvar")) {
varname = ast_strdupa(v->value);
if (varname && (varval = strchr(varname,'='))) {
- *varval = '\0';
- varval++;
+ *varval++ = '\0';
if ((tmpvar = ast_variable_new(varname, varval))) {
tmpvar->next = user->chanvars;
user->chanvars = tmpvar;
Modified: team/oej/strictrouting/doc/CODING-GUIDELINES
URL: http://svn.digium.com/view/asterisk/team/oej/strictrouting/doc/CODING-GUIDELINES?rev=18751&r1=18750&r2=18751&view=diff
==============================================================================
--- team/oej/strictrouting/doc/CODING-GUIDELINES (original)
+++ team/oej/strictrouting/doc/CODING-GUIDELINES Mon Apr 10 00:30:25 2006
@@ -77,7 +77,7 @@
Roughly, Asterisk code formatting guidelines are generally equivalent to the
following:
-# indent -i4 -ts4 -br -brs -cdw -lp -ce -nbfda -npcs -nprs -npsl -nbbo -saf -sai -saw -cs -ln90 foo.c
+# indent -i4 -ts4 -br -brs -cdw -lp -ce -nbfda -npcs -nprs -npsl -nbbo -saf -sai -saw -cs -l90 foo.c
this means in verbose:
-i4: indent level 4
Modified: team/oej/strictrouting/res/res_config_pgsql.c
URL: http://svn.digium.com/view/asterisk/team/oej/strictrouting/res/res_config_pgsql.c?rev=18751&r1=18750&r2=18751&view=diff
==============================================================================
--- team/oej/strictrouting/res/res_config_pgsql.c (original)
+++ team/oej/strictrouting/res/res_config_pgsql.c Mon Apr 10 00:30:25 2006
@@ -24,7 +24,7 @@
#include <stdlib.h>
#include <string.h>
-#include <libpq-fe.h> /* PostgreSQL */
+#include <libpq-fe.h> /* PostgreSQL */
#include "asterisk.h"
@@ -44,16 +44,20 @@
static char *res_config_pgsql_desc = "Postgresql RealTime Configuration Driver";
AST_MUTEX_DEFINE_STATIC(pgsql_lock);
+
#define RES_CONFIG_PGSQL_CONF "res_pgsql.conf"
-PGconn* pgsqlConn = NULL;
+
+PGconn *pgsqlConn = NULL;
+
#define MAX_DB_OPTION_SIZE 64
-static char dbhost[MAX_DB_OPTION_SIZE]="";
-static char dbuser[MAX_DB_OPTION_SIZE]="";
-static char dbpass[MAX_DB_OPTION_SIZE]="";
-static char dbname[MAX_DB_OPTION_SIZE]="";
-static char dbsock[MAX_DB_OPTION_SIZE]="";
-static int dbport=5432;
-static time_t connect_time=0;
+
+static char dbhost[MAX_DB_OPTION_SIZE] = "";
+static char dbuser[MAX_DB_OPTION_SIZE] = "";
+static char dbpass[MAX_DB_OPTION_SIZE] = "";
+static char dbname[MAX_DB_OPTION_SIZE] = "";
+static char dbsock[MAX_DB_OPTION_SIZE] = "";
+static int dbport = 5432;
+static time_t connect_time = 0;
static int parse_config(void);
static int pgsql_reconnect(const char *database);
@@ -62,12 +66,14 @@
LOCAL_USER_DECL;
static char cli_realtime_pgsql_status_usage[] =
-"Usage: realtime pgsql status\n"
-" Shows connection information for the Postgresql RealTime driver\n";
+ "Usage: realtime pgsql status\n"
+ " Shows connection information for the Postgresql RealTime driver\n";
static struct ast_cli_entry cli_realtime_pgsql_status = {
- { "realtime", "pgsql", "status", NULL }, realtime_pgsql_status,
- "Shows connection information for the Postgresql RealTime driver", cli_realtime_pgsql_status_usage, NULL };
+ { "realtime", "pgsql", "status", NULL }, realtime_pgsql_status,
+ "Shows connection information for the Postgresql RealTime driver",
+ cli_realtime_pgsql_status_usage, NULL
+ };
static struct ast_variable *realtime_pgsql(const char *database, const char *table, va_list ap)
{
@@ -78,9 +84,9 @@
char *chunk;
char *op;
const char *newparam, *newval;
- struct ast_variable *var=NULL, *prev=NULL;
-
- if(!table) {
+ struct ast_variable *var = NULL, *prev = NULL;
+
+ if (!table) {
ast_log(LOG_WARNING, "Postgresql RealTime: No table specified.\n");
return NULL;
}
@@ -88,97 +94,102 @@
/* Get the first parameter and first value in our list of passed paramater/value pairs */
newparam = va_arg(ap, const char *);
newval = va_arg(ap, const char *);
- if(!newparam || !newval) {
- ast_log(LOG_WARNING, "Postgresql RealTime: Realtime retrieval requires at least 1 parameter and 1 value to search on.\n");
- if (pgsqlConn)
- {
- PQfinish(pgsqlConn);
- pgsqlConn=NULL;
- };
+ if (!newparam || !newval) {
+ ast_log(LOG_WARNING,
+ "Postgresql RealTime: Realtime retrieval requires at least 1 parameter and 1 value to search on.\n");
+ if (pgsqlConn) {
+ PQfinish(pgsqlConn);
+ pgsqlConn = NULL;
+ };
return NULL;
}
/* Create the first part of the query using the first parameter/value pairs we just extracted
If there is only 1 set, then we have our query. Otherwise, loop thru the list and concat */
- op = strchr(newparam, ' ')?"":" =";
-
- snprintf(sql, sizeof(sql), "SELECT * FROM %s WHERE %s%s '%s'", table, newparam, op, newval);
- while((newparam = va_arg(ap, const char *))) {
+ op = strchr(newparam, ' ') ? "" : " =";
+
+ snprintf(sql, sizeof(sql), "SELECT * FROM %s WHERE %s%s '%s'", table, newparam, op,
+ newval);
+ while ((newparam = va_arg(ap, const char *))) {
newval = va_arg(ap, const char *);
- if(!strchr(newparam, ' ')) op = " ="; else op = "";
- snprintf(sql + strlen(sql), sizeof(sql) - strlen(sql), " AND %s%s '%s'", newparam, op, newval);
+ if (!strchr(newparam, ' '))
+ op = " =";
+ else
+ op = "";
+ snprintf(sql + strlen(sql), sizeof(sql) - strlen(sql), " AND %s%s '%s'", newparam,
+ op, newval);
}
va_end(ap);
/* We now have our complete statement; Lets connect to the server and execute it. */
ast_mutex_lock(&pgsql_lock);
- if(!pgsql_reconnect(database)) {
+ if (!pgsql_reconnect(database)) {
ast_mutex_unlock(&pgsql_lock);
return NULL;
}
-
- if(!(result=PQexec(pgsqlConn, sql))) {
- ast_log(LOG_WARNING, "Postgresql RealTime: Failed to query database. Check debug for more info.\n");
+
+ if (!(result = PQexec(pgsqlConn, sql))) {
+ ast_log(LOG_WARNING,
+ "Postgresql RealTime: Failed to query database. Check debug for more info.\n");
ast_log(LOG_DEBUG, "Postgresql RealTime: Query: %s\n", sql);
- ast_log(LOG_DEBUG, "Postgresql RealTime: Query Failed because: %s\n", PQerrorMessage(pgsqlConn));
+ ast_log(LOG_DEBUG, "Postgresql RealTime: Query Failed because: %s\n",
+ PQerrorMessage(pgsqlConn));
ast_mutex_unlock(&pgsql_lock);
return NULL;
- }
- else {
- ExecStatusType result_status=PQresultStatus(result);
- if (result_status!=PGRES_COMMAND_OK
- && result_status!=PGRES_TUPLES_OK
- && result_status!=PGRES_NONFATAL_ERROR)
- {
- ast_log(LOG_WARNING, "Postgresql RealTime: Failed to query database. Check debug for more info.\n");
- ast_log(LOG_DEBUG, "Postgresql RealTime: Query: %s\n", sql);
- ast_log(LOG_DEBUG, "Postgresql RealTime: Query Failed because: %s (%s)\n",
- PQresultErrorMessage(result),PQresStatus(result_status));
- ast_mutex_unlock(&pgsql_lock);
- return NULL;
- }
- }
-
+ } else {
+ ExecStatusType result_status = PQresultStatus(result);
+ if (result_status != PGRES_COMMAND_OK
+ && result_status != PGRES_TUPLES_OK
+ && result_status != PGRES_NONFATAL_ERROR) {
+ ast_log(LOG_WARNING,
+ "Postgresql RealTime: Failed to query database. Check debug for more info.\n");
+ ast_log(LOG_DEBUG, "Postgresql RealTime: Query: %s\n", sql);
+ ast_log(LOG_DEBUG, "Postgresql RealTime: Query Failed because: %s (%s)\n",
+ PQresultErrorMessage(result), PQresStatus(result_status));
+ ast_mutex_unlock(&pgsql_lock);
+ return NULL;
+ }
+ }
+
ast_log(LOG_DEBUG, "1Postgresql RealTime: Result=%p Query: %s\n", result, sql);
- if((num_rows=PQntuples(result))>0) {
- int i = 0;
- int rowIndex=0;
- int numFields = PQnfields(result);
- char** fieldnames=NULL;
-
- ast_log(LOG_DEBUG, "Postgresql RealTime: Found %d rows.\n", num_rows);
-
- fieldnames=malloc(numFields*sizeof(char*));
- if (!fieldnames) {
- /* If I can't alloc memory at this point, why bother doing anything else? */
- ast_log(LOG_WARNING, "Out of memory!\n");
- ast_mutex_unlock(&pgsql_lock);
- PQclear(result);
- return NULL;
- }
- for(i = 0; i < numFields; i++)
- fieldnames[i]=PQfname(result,i);
- for(rowIndex = 0; rowIndex < num_rows; rowIndex++)
- for(i = 0; i < numFields; i++) {
- stringp = PQgetvalue(result,rowIndex,i);
- while(stringp) {
- chunk = strsep(&stringp, ";");
- if(chunk && !ast_strlen_zero(ast_strip(chunk))) {
- if(prev) {
- prev->next = ast_variable_new(fieldnames[i], chunk);
- if (prev->next) {
- prev = prev->next;
- }
- } else {
- prev = var = ast_variable_new(fieldnames[i], chunk);
- }
- }
- }
- }
- free(fieldnames);
- } else {
- ast_log(LOG_WARNING, "Postgresql RealTime: Could not find any rows in table %s.\n", table);
+ if ((num_rows = PQntuples(result)) > 0) {
+ int i = 0;
+ int rowIndex = 0;
+ int numFields = PQnfields(result);
+ char **fieldnames = NULL;
+
+ ast_log(LOG_DEBUG, "Postgresql RealTime: Found %d rows.\n", num_rows);
+
+ if (!(fieldnames = ast_calloc(1, numFields * sizeof(char *)))) {
+ ast_mutex_unlock(&pgsql_lock);
+ PQclear(result);
+ return NULL;
+ }
+ for (i = 0; i < numFields; i++)
+ fieldnames[i] = PQfname(result, i);
+ for (rowIndex = 0; rowIndex < num_rows; rowIndex++) {
+ for (i = 0; i < numFields; i++) {
+ stringp = PQgetvalue(result, rowIndex, i);
+ while (stringp) {
+ chunk = strsep(&stringp, ";");
+ if (chunk && !ast_strlen_zero(ast_strip(chunk))) {
+ if (prev) {
+ prev->next = ast_variable_new(fieldnames[i], chunk);
+ if (prev->next) {
+ prev = prev->next;
+ }
+ } else {
+ prev = var = ast_variable_new(fieldnames[i], chunk);
+ }
+ }
+ }
+ }
+ }
+ free(fieldnames);
+ } else {
+ ast_log(LOG_WARNING,
+ "Postgresql RealTime: Could not find any rows in table %s.\n", table);
}
ast_mutex_unlock(&pgsql_lock);
@@ -198,55 +209,59 @@
char *op;
const char *newparam, *newval;
struct ast_realloca ra;
- struct ast_variable *var=NULL;
+ struct ast_variable *var = NULL;
struct ast_config *cfg = NULL;
struct ast_category *cat = NULL;
- if(!table) {
+ if (!table) {
ast_log(LOG_WARNING, "Postgresql RealTime: No table specified.\n");
return NULL;
}
-
+
memset(&ra, 0, sizeof(ra));
- cfg = ast_config_new();
- if (!cfg) {
- /* If I can't alloc memory at this point, why bother doing anything else? */
- ast_log(LOG_WARNING, "Out of memory!\n");
- return NULL;
- }
+ if (!(cfg = ast_config_new()))
+ return NULL;
/* Get the first parameter and first value in our list of passed paramater/value pairs */
newparam = va_arg(ap, const char *);
newval = va_arg(ap, const char *);
- if(!newparam || !newval) {
- ast_log(LOG_WARNING, "Postgresql RealTime: Realtime retrieval requires at least 1 parameter and 1 value to search on.\n");
- if (pgsqlConn)
- {
- PQfinish(pgsqlConn);
- pgsqlConn=NULL;
- };
+ if (!newparam || !newval) {
+ ast_log(LOG_WARNING,
+ "Postgresql RealTime: Realtime retrieval requires at least 1 parameter and 1 value to search on.\n");
+ if (pgsqlConn) {
+ PQfinish(pgsqlConn);
+ pgsqlConn = NULL;
+ };
return NULL;
}
initfield = ast_strdupa(newparam);
- if(initfield && (op = strchr(initfield, ' '))) {
+ if (initfield && (op = strchr(initfield, ' '))) {
*op = '\0';
}
/* Create the first part of the query using the first parameter/value pairs we just extracted
If there is only 1 set, then we have our query. Otherwise, loop thru the list and concat */
- if(!strchr(newparam, ' ')) op = " ="; else op = "";
-
- snprintf(sql, sizeof(sql), "SELECT * FROM %s WHERE %s%s '%s'", table, newparam, op, newval);
- while((newparam = va_arg(ap, const char *))) {
+ if (!strchr(newparam, ' '))
+ op = " =";
+ else
+ op = "";
+
+ snprintf(sql, sizeof(sql), "SELECT * FROM %s WHERE %s%s '%s'", table, newparam, op,
+ newval);
+ while ((newparam = va_arg(ap, const char *))) {
newval = va_arg(ap, const char *);
- if(!strchr(newparam, ' ')) op = " ="; else op = "";
- snprintf(sql + strlen(sql), sizeof(sql) - strlen(sql), " AND %s%s '%s'", newparam, op, newval);
- }
-
- if(initfield) {
+ if (!strchr(newparam, ' '))
+ op = " =";
+ else
+ op = "";
+ snprintf(sql + strlen(sql), sizeof(sql) - strlen(sql), " AND %s%s '%s'", newparam,
+ op, newval);
+ }
+
+ if (initfield) {
snprintf(sql + strlen(sql), sizeof(sql) - strlen(sql), " ORDER BY %s", initfield);
}
@@ -254,80 +269,75 @@
/* We now have our complete statement; Lets connect to the server and execute it. */
ast_mutex_lock(&pgsql_lock);
- if(!pgsql_reconnect(database)) {
+ if (!pgsql_reconnect(database)) {
ast_mutex_unlock(&pgsql_lock);
return NULL;
}
- if(!(result=PQexec(pgsqlConn, sql))) {
- ast_log(LOG_WARNING, "Postgresql RealTime: Failed to query database. Check debug for more info.\n");
+ if (!(result = PQexec(pgsqlConn, sql))) {
+ ast_log(LOG_WARNING,
+ "Postgresql RealTime: Failed to query database. Check debug for more info.\n");
ast_log(LOG_DEBUG, "Postgresql RealTime: Query: %s\n", sql);
- ast_log(LOG_DEBUG, "Postgresql RealTime: Query Failed because: %s\n", PQerrorMessage(pgsqlConn));
+ ast_log(LOG_DEBUG, "Postgresql RealTime: Query Failed because: %s\n",
+ PQerrorMessage(pgsqlConn));
ast_mutex_unlock(&pgsql_lock);
return NULL;
- }
- else {
- ExecStatusType result_status=PQresultStatus(result);
- if (result_status!=PGRES_COMMAND_OK
- && result_status!=PGRES_TUPLES_OK
- && result_status!=PGRES_NONFATAL_ERROR)
- {
- ast_log(LOG_WARNING, "Postgresql RealTime: Failed to query database. Check debug for more info.\n");
- ast_log(LOG_DEBUG, "Postgresql RealTime: Query: %s\n", sql);
- ast_log(LOG_DEBUG, "Postgresql RealTime: Query Failed because: %s (%s)\n",
- PQresultErrorMessage(result),PQresStatus(result_status));
- ast_mutex_unlock(&pgsql_lock);
- return NULL;
- }
+ } else {
+ ExecStatusType result_status = PQresultStatus(result);
+ if (result_status != PGRES_COMMAND_OK
+ && result_status != PGRES_TUPLES_OK
+ && result_status != PGRES_NONFATAL_ERROR) {
+ ast_log(LOG_WARNING,
+ "Postgresql RealTime: Failed to query database. Check debug for more info.\n");
+ ast_log(LOG_DEBUG, "Postgresql RealTime: Query: %s\n", sql);
+ ast_log(LOG_DEBUG, "Postgresql RealTime: Query Failed because: %s (%s)\n",
+ PQresultErrorMessage(result), PQresStatus(result_status));
+ ast_mutex_unlock(&pgsql_lock);
+ return NULL;
+ }
}
ast_log(LOG_DEBUG, "2Postgresql RealTime: Result=%p Query: %s\n", result, sql);
- if((num_rows=PQntuples(result))>0) {
- int numFields = PQnfields(result);
- int i = 0;
- int rowIndex=0;
- char** fieldnames=NULL;
-
- ast_log(LOG_DEBUG, "Postgresql RealTime: Found %d rows.\n", num_rows);
-
- fieldnames=malloc(numFields*sizeof(char*));
- if (!fieldnames) {
- /* If I can't alloc memory at this point, why bother doing anything else? */
- ast_log(LOG_WARNING, "Out of memory!\n");
- ast_mutex_unlock(&pgsql_lock);
- PQclear(result);
- return NULL;
- }
- for(i = 0; i < numFields; i++)
- fieldnames[i]=PQfname(result,i);
-
- for(rowIndex=0;rowIndex<num_rows;rowIndex++)
- {
- var = NULL;
- cat = ast_category_new("");
- if(!cat) {
- ast_log(LOG_WARNING, "Out of memory!\n");
- continue;
- }
- for(i = 0; i < numFields; i++) {
- stringp = PQgetvalue(result,rowIndex,i);
- while(stringp) {
- chunk = strsep(&stringp, ";");
- if(chunk && !ast_strlen_zero(ast_strip(chunk))) {
- if(initfield && !strcmp(initfield, fieldnames[i])) {
- ast_category_rename(cat, chunk);
- }
- var = ast_variable_new(fieldnames[i], chunk);
- ast_variable_append(cat, var);
- }
- }
- }
- ast_category_append(cfg, cat);
- }
- free(fieldnames);
- } else {
- ast_log(LOG_WARNING, "Postgresql RealTime: Could not find any rows in table %s.\n", table);
+ if ((num_rows = PQntuples(result)) > 0) {
+ int numFields = PQnfields(result);
+ int i = 0;
+ int rowIndex = 0;
+ char **fieldnames = NULL;
+
+ ast_log(LOG_DEBUG, "Postgresql RealTime: Found %d rows.\n", num_rows);
+
+ if (!(fieldnames = ast_calloc(1, numFields * sizeof(char *)))) {
+ ast_mutex_unlock(&pgsql_lock);
+ PQclear(result);
+ return NULL;
+ }
+ for (i = 0; i < numFields; i++)
+ fieldnames[i] = PQfname(result, i);
+
+ for (rowIndex = 0; rowIndex < num_rows; rowIndex++) {
+ var = NULL;
+ if (!(cat = ast_category_new("")))
+ continue;
+ for (i = 0; i < numFields; i++) {
+ stringp = PQgetvalue(result, rowIndex, i);
+ while (stringp) {
+ chunk = strsep(&stringp, ";");
+ if (chunk && !ast_strlen_zero(ast_strip(chunk))) {
+ if (initfield && !strcmp(initfield, fieldnames[i])) {
+ ast_category_rename(cat, chunk);
+ }
+ var = ast_variable_new(fieldnames[i], chunk);
+ ast_variable_append(cat, var);
+ }
+ }
+ }
+ ast_category_append(cfg, cat);
+ }
+ free(fieldnames);
+ } else {
+ ast_log(LOG_WARNING,
+ "Postgresql RealTime: Could not find any rows in table %s.\n", table);
}
ast_mutex_unlock(&pgsql_lock);
@@ -336,91 +346,97 @@
return cfg;
}
-static int update_pgsql(const char *database, const char *table, const char *keyfield, const char *lookup, va_list ap)
-{
- PGresult* result = NULL;
+static int update_pgsql(const char *database, const char *table, const char *keyfield,
+ const char *lookup, va_list ap)
+{
+ PGresult *result = NULL;
int numrows = 0;
char sql[256];
const char *newparam, *newval;
- if(!table) {
+ if (!table) {
ast_log(LOG_WARNING, "Postgresql RealTime: No table specified.\n");
- return -1;
+ return -1;
}
/* Get the first parameter and first value in our list of passed paramater/value pairs */
newparam = va_arg(ap, const char *);
newval = va_arg(ap, const char *);
- if(!newparam || !newval) {
- ast_log(LOG_WARNING, "Postgresql RealTime: Realtime retrieval requires at least 1 parameter and 1 value to search on.\n");
- if (pgsqlConn)
- {
- PQfinish(pgsqlConn);
- pgsqlConn=NULL;
- };
- return -1;
+ if (!newparam || !newval) {
+ ast_log(LOG_WARNING,
+ "Postgresql RealTime: Realtime retrieval requires at least 1 parameter and 1 value to search on.\n");
+ if (pgsqlConn) {
+ PQfinish(pgsqlConn);
+ pgsqlConn = NULL;
+ };
+ return -1;
}
/* Create the first part of the query using the first parameter/value pairs we just extracted
If there is only 1 set, then we have our query. Otherwise, loop thru the list and concat */
snprintf(sql, sizeof(sql), "UPDATE %s SET %s = '%s'", table, newparam, newval);
- while((newparam = va_arg(ap, const char *))) {
+ while ((newparam = va_arg(ap, const char *))) {
newval = va_arg(ap, const char *);
- snprintf(sql + strlen(sql), sizeof(sql) - strlen(sql), ", %s = '%s'", newparam, newval);
+ snprintf(sql + strlen(sql), sizeof(sql) - strlen(sql), ", %s = '%s'", newparam,
+ newval);
}
va_end(ap);
- snprintf(sql + strlen(sql), sizeof(sql) - strlen(sql), " WHERE %s = '%s'", keyfield, lookup);
-
- ast_log(LOG_DEBUG,"Postgresql RealTime: Update SQL: %s\n", sql);
+ snprintf(sql + strlen(sql), sizeof(sql) - strlen(sql), " WHERE %s = '%s'", keyfield,
+ lookup);
+
+ ast_log(LOG_DEBUG, "Postgresql RealTime: Update SQL: %s\n", sql);
/* We now have our complete statement; Lets connect to the server and execute it. */
ast_mutex_lock(&pgsql_lock);
- if(!pgsql_reconnect(database)) {
+ if (!pgsql_reconnect(database)) {
ast_mutex_unlock(&pgsql_lock);
return -1;
}
- if(!(result=PQexec(pgsqlConn, sql))) {
- ast_log(LOG_WARNING, "Postgresql RealTime: Failed to query database. Check debug for more info.\n");
+ if (!(result = PQexec(pgsqlConn, sql))) {
+ ast_log(LOG_WARNING,
+ "Postgresql RealTime: Failed to query database. Check debug for more info.\n");
ast_log(LOG_DEBUG, "Postgresql RealTime: Query: %s\n", sql);
- ast_log(LOG_DEBUG, "Postgresql RealTime: Query Failed because: %s\n", PQerrorMessage(pgsqlConn));
+ ast_log(LOG_DEBUG, "Postgresql RealTime: Query Failed because: %s\n",
+ PQerrorMessage(pgsqlConn));
ast_mutex_unlock(&pgsql_lock);
return -1;
- }
- else {
- ExecStatusType result_status=PQresultStatus(result);
- if (result_status!=PGRES_COMMAND_OK
- && result_status!=PGRES_TUPLES_OK
- && result_status!=PGRES_NONFATAL_ERROR)
- {
- ast_log(LOG_WARNING, "Postgresql RealTime: Failed to query database. Check debug for more info.\n");
- ast_log(LOG_DEBUG, "Postgresql RealTime: Query: %s\n", sql);
- ast_log(LOG_DEBUG, "Postgresql RealTime: Query Failed because: %s (%s)\n",
- PQresultErrorMessage(result),PQresStatus(result_status));
- ast_mutex_unlock(&pgsql_lock);
- return -1;
- }
+ } else {
+ ExecStatusType result_status = PQresultStatus(result);
+ if (result_status != PGRES_COMMAND_OK
+ && result_status != PGRES_TUPLES_OK
+ && result_status != PGRES_NONFATAL_ERROR) {
+ ast_log(LOG_WARNING,
+ "Postgresql RealTime: Failed to query database. Check debug for more info.\n");
+ ast_log(LOG_DEBUG, "Postgresql RealTime: Query: %s\n", sql);
+ ast_log(LOG_DEBUG, "Postgresql RealTime: Query Failed because: %s (%s)\n",
+ PQresultErrorMessage(result), PQresStatus(result_status));
+ ast_mutex_unlock(&pgsql_lock);
+ return -1;
+ }
}
numrows = atoi(PQcmdTuples(result));
ast_mutex_unlock(&pgsql_lock);
- ast_log(LOG_DEBUG,"Postgresql RealTime: Updated %d rows on table: %s\n", numrows, table);
+ ast_log(LOG_DEBUG, "Postgresql RealTime: Updated %d rows on table: %s\n", numrows,
+ table);
/* From http://dev.pgsql.com/doc/pgsql/en/pgsql-affected-rows.html
* An integer greater than zero indicates the number of rows affected
* Zero indicates that no records were updated
* -1 indicates that the query returned an error (although, if the query failed, it should have been caught above.)
- */
-
- if(numrows >= 0)
- return (int)numrows;
+ */
+
+ if (numrows >= 0)
+ return (int) numrows;
return -1;
}
-static struct ast_config *config_pgsql(const char *database, const char *table, const char *file, struct ast_config *cfg)
+static struct ast_config *config_pgsql(const char *database, const char *table,
+ const char *file, struct ast_config *cfg)
{
PGresult *result = NULL;
long num_rows;
@@ -432,70 +448,69 @@
last[0] = '\0';
- if(!file || !strcmp(file, RES_CONFIG_PGSQL_CONF)) {
+ if (!file || !strcmp(file, RES_CONFIG_PGSQL_CONF)) {
ast_log(LOG_WARNING, "Postgresql RealTime: Cannot configure myself.\n");
return NULL;
}
- snprintf(sql, sizeof(sql), "SELECT category, var_name, var_val, cat_metric FROM %s WHERE filename='%s' and commented=0 ORDER BY filename, cat_metric desc, var_metric asc, category, var_name, var_val, id", table, file);
+ snprintf(sql, sizeof(sql),
+ "SELECT category, var_name, var_val, cat_metric FROM %s WHERE filename='%s' and commented=0 ORDER BY filename, cat_metric desc, var_metric asc, category, var_name, var_val, id",
[... 468 lines stripped ...]
More information about the asterisk-commits
mailing list