fills mydbf with the corresponding db module callbacks * returns 0 on success, -1 on error * on error mydbf will contain only 0s */
| 189 | * returns 0 on success, -1 on error |
| 190 | * on error mydbf will contain only 0s */ |
| 191 | int db_bind_mod(const str* mod, db_func_t* mydbf) |
| 192 | { |
| 193 | char *name, *tmp, *p; |
| 194 | int len; |
| 195 | db_func_t dbf; |
| 196 | db_bind_api_f dbind; |
| 197 | |
| 198 | if (!mod || !mod->s) { |
| 199 | LM_CRIT("null database module name\n"); |
| 200 | return -1; |
| 201 | } |
| 202 | if (mydbf==0) { |
| 203 | LM_CRIT("null dbf parameter\n"); |
| 204 | return -1; |
| 205 | } |
| 206 | if (mod->len > MAX_URL_LENGTH) |
| 207 | { |
| 208 | LM_ERR("SQL URL too long\n"); |
| 209 | return 0; |
| 210 | } |
| 211 | // add the prefix |
| 212 | name = pkg_malloc(mod->len + 4); |
| 213 | if (!name) { |
| 214 | LM_ERR("no private memory left\n"); |
| 215 | return -1; |
| 216 | } |
| 217 | memcpy(name, "db_", 3); |
| 218 | memcpy(name+3, mod->s, mod->len); |
| 219 | name[mod->len+3] = 0; |
| 220 | |
| 221 | /* for safety we initialize mydbf with 0 (this will cause |
| 222 | * a segfault immediately if someone tries to call a function |
| 223 | * from it without checking the return code from bind_dbmod */ |
| 224 | memset((void*)mydbf, 0, sizeof(db_func_t)); |
| 225 | |
| 226 | p = strchr(name, ':'); |
| 227 | if (p) { |
| 228 | len = p - name; |
| 229 | tmp = (char*)pkg_malloc(len + 4); |
| 230 | if (!tmp) { |
| 231 | LM_ERR("no private memory left\n"); |
| 232 | pkg_free(name); |
| 233 | return -1; |
| 234 | } |
| 235 | memcpy(tmp, name, len); |
| 236 | tmp[len] = '\0'; |
| 237 | pkg_free(name); |
| 238 | } else { |
| 239 | tmp = name; |
| 240 | } |
| 241 | |
| 242 | dbind = (db_bind_api_f)find_mod_export(tmp, "db_bind_api", 0); |
| 243 | if(dbind != NULL) |
| 244 | { |
| 245 | LM_DBG("using db bind api for %s\n", tmp); |
| 246 | if(dbind(mod, &dbf)<0) |
| 247 | { |
| 248 | LM_ERR("db_bind_api returned error for module %s\n", tmp); |
no test coverage detected