Index: chan_sip.c =================================================================== RCS file: /usr/cvsroot/asterisk/channels/chan_sip.c,v retrieving revision 1.411 diff -u -r1.411 chan_sip.c --- chan_sip.c 14 Jun 2004 21:47:08 -0000 1.411 +++ chan_sip.c 15 Jun 2004 16:41:15 -0000 @@ -11,6 +11,7 @@ * the GNU General Public License */ + #include #include #include @@ -380,6 +381,9 @@ int outgoinglimit; int restrictcid; struct ast_ha *ha; +#ifdef MYSQL_FRIENDS + int temponly; +#endif /* MYSQL_FRIENDS */ struct sip_user *next; }; @@ -853,6 +857,84 @@ #ifdef MYSQL_FRIENDS +/* Ehud Gavron 08-Jun-2004: */ +/* The Mysql stuff works great for peers but not for users. */ +/* Unfortunately multi-line phones (e.g. cisco 7960) and many */ +/* SIP users behind the same NAT gateway need users. So.... */ +/* */ +/* mysql_update_user is not needed */ +/*--- mysql_host: Get user from database ---*/ +static struct sip_user *mysql_user(char *user) +{ + struct sip_user *u; + int success = 0; + u = malloc(sizeof(struct sip_user)); + memset(u, 0, sizeof(struct sip_user)); + if (mysql && (!user || (strlen(user) < 128))) { + char query[512]; + char *name = NULL; + int numfields, x; + time_t regseconds, nowtime; + MYSQL_RES *result; + MYSQL_FIELD *fields; + MYSQL_ROW rowval; + if (user) { + name = alloca(strlen(user) * 2 + 1); + mysql_real_escape_string(mysql, name, user, strlen(user)); + } + + snprintf(query, sizeof(query), "SELECT name, secret, context, username, ipaddr, port, regseconds, callerid, restrictcid FROM sipfriends WHERE name=\"%s\"", name); + + + ast_mutex_lock(&mysqllock); + mysql_query(mysql, query); + if ((result = mysql_store_result(mysql))) { + + if ((rowval = mysql_fetch_row(result))) { + numfields = mysql_num_fields(result); + fields = mysql_fetch_fields(result); + success = 1; + for (x=0;xsecret, rowval[x], sizeof(u->secret)); + } else if (!strcasecmp(fields[x].name, "name")) { + strncpy(u->name, rowval[x], sizeof(u->name) - 1); + } else if (!strcasecmp(fields[x].name, "context")) { + strncpy(u->context, rowval[x], sizeof(u->context) - 1); + } else if (!strcasecmp(fields[x].name, "username")) { + strncpy(u->name, rowval[x], sizeof(u->name) - 1); + } else if (!strcasecmp(fields[x].name, "regseconds")) { + if (sscanf(rowval[x], "%li", ®seconds) != 1) + regseconds = 0; + } else if (!strcasecmp(fields[x].name, "restrictcid")) { + u->restrictcid = 1; + } else if (!strcasecmp(fields[x].name, "callerid")) { + strncpy(u->callerid, rowval[x], sizeof(u->callerid) - 1); + u->hascallerid=1; + } + } + } + time(&nowtime); + } + mysql_free_result(result); + result = NULL; + } + ast_mutex_unlock(&mysqllock); + } + if (!success) { + free(u); + u = NULL; + } else { + u->capability = capability; + u->nat = globalnat; + u->dtmfmode = globaldtmfmode; + u->insecure = 1; + u->temponly = 1; + } + return u; +} + /*--- mysql_update_peer: Update peer from database ---*/ /* This function adds registration state to database */ static void mysql_update_peer(char *peer, struct sockaddr_in *sin, char *username, int expiry) @@ -902,9 +984,12 @@ snprintf(query, sizeof(query), "SELECT name, secret, context, username, ipaddr, port, regseconds FROM sipfriends WHERE ipaddr=\"%s\" AND port=\"%d\"", inet_ntoa(sin->sin_addr), ntohs(sin->sin_port)); else snprintf(query, sizeof(query), "SELECT name, secret, context, username, ipaddr, port, regseconds FROM sipfriends WHERE name=\"%s\"", name); + + ast_mutex_lock(&mysqllock); mysql_query(mysql, query); if ((result = mysql_store_result(mysql))) { + if ((rowval = mysql_fetch_row(result))) { numfields = mysql_num_fields(result); fields = mysql_fetch_fields(result); @@ -952,7 +1037,6 @@ p->insecure = 1; p->expire = -1; p->temponly = 1; - } return p; } @@ -1008,7 +1092,6 @@ static struct sip_user *find_user(char *name) { struct sip_user *u = NULL; - u = userl.users; while(u) { if (!strcasecmp(u->name, name)) { @@ -1016,6 +1099,11 @@ } u = u->next; } +#ifdef MYSQL_FRIENDS + if (!u) { + u = mysql_user(name); + } +#endif /* MYSQL_FRIENDS */ return(u); } @@ -4833,6 +4921,9 @@ if (user && sip_debug_test_addr(sin)) ast_verbose("Found user '%s', but fails host access\n", user->name); user = NULL; + } + if (user->temponly) { + free(user); } ast_mutex_unlock(&userl.lock); if (!user) {