Пример #1
0
int bdb_ufield_db2bdb(bdb_uval_p v, db_val_t* _v)
{
	char	*s;

	VAL_NULL(&(v->v)) = VAL_NULL(_v);
	VAL_TYPE(&(v->v)) = VAL_TYPE(_v);

	if (!VAL_NULL(&(v->v))) {
		switch (VAL_TYPE(_v)) {
		case DB_INT:
			VAL_INT(&(v->v)) = VAL_INT(_v);
			break;
		case DB_FLOAT:
			VAL_FLOAT(&(v->v)) = VAL_FLOAT(_v);
			break;
		case DB_DATETIME:
			VAL_TIME(&(v->v)) = VAL_TIME(_v);
			break;
		case DB_BLOB:
			s = pkg_malloc(VAL_BLOB(_v).len);
			memcpy(s, VAL_BLOB(_v).s, VAL_BLOB(_v).len);
			VAL_BLOB(&(v->v)).s = s;
			VAL_BLOB(&(v->v)).len = VAL_BLOB(_v).len;
			break;
		case DB_DOUBLE:
			VAL_DOUBLE(&(v->v)) = VAL_DOUBLE(_v);
			break;
		case DB_STRING:
			VAL_STR(&(v->v)).len = strlen(VAL_STRING(_v)) + 1;
			s = pkg_malloc(VAL_STR(&(v->v)).len);
			strcpy(s, VAL_STRING(_v));
			VAL_STRING(&(v->v)) = s;
			break;
		case DB_STR:
			s = pkg_malloc(VAL_STR(_v).len);
			memcpy(s, VAL_STR(_v).s, VAL_STR(_v).len);
			VAL_STR(&(v->v)).s = s;
			VAL_STR(&(v->v)).len = VAL_STR(_v).len;
			break;
		case DB_BITMAP:
			VAL_BITMAP(&(v->v)) = VAL_BITMAP(_v);
			break;
		default:
			LOG(L_ERR, "BDB:bdb_ufield_db2bdb: unknown column type: %0X\n", VAL_TYPE(_v));
			return -1;
			break;
		}
	}

	return 0;
};
Пример #2
0
int bdb_set_key(bdb_row_p _r, bdb_val_p _v)
{
	/* NULL is not allowed for primary key */
	if (VAL_NULL(&(_v->v))) {
		LOG(L_ERR, "BDB:bdb_set_key: NULL is not allowed for primary key\n");
		return -1;
	}

	switch (VAL_TYPE(&(_v->v))) {
	case DB_INT:
		_r->key.data = &VAL_INT(&(_v->v));
		_r->key.size = sizeof(VAL_INT(&(_v->v)));
		break;
	case DB_FLOAT:
		_r->key.data = &VAL_FLOAT(&(_v->v));
		_r->key.size = sizeof(VAL_FLOAT(&(_v->v)));
		break;
	case DB_DATETIME:
		_r->key.data = &VAL_TIME(&(_v->v));
		_r->key.size = sizeof(VAL_TIME(&(_v->v)));
		break;
	case DB_BLOB:
		_r->key.data = VAL_BLOB(&(_v->v)).s;
		_r->key.size = VAL_BLOB(&(_v->v)).len;
		break;
	case DB_DOUBLE:
		_r->key.data = &VAL_DOUBLE(&(_v->v));
		_r->key.size = sizeof(VAL_DOUBLE(&(_v->v)));
		break;
	case DB_STRING:
		_r->key.data = (void *)VAL_STRING(&(_v->v));
		_r->key.size = strlen(VAL_STRING(&(_v->v))) + 1;
		break;
	case DB_STR:
		_r->key.data = VAL_STR(&(_v->v)).s;
		_r->key.size = VAL_STR(&(_v->v)).len;
		break;
	case DB_BITMAP:
		_r->key.data = &VAL_BITMAP(&(_v->v));
		_r->key.size = sizeof(VAL_BITMAP(&(_v->v)));
		break;
	default:
		LOG(L_ERR, "BDB:bdb_set_skey: unknown column type: %0X\n", VAL_TYPE(&(_v->v)));
		return -1;
		break;
	}

	return 0;
};
Пример #3
0
void bdb_set_skey(bdb_srow_p _r, bdb_sval_p _v)
{
	/* NULL is not allowed for primary key */
	if (VAL_NULL(&(_v->v)))
		return;

	switch (VAL_TYPE(&(_v->v))) {
	case DB_INT:
		_r->key.data = &VAL_INT(&(_v->v));
		_r->key.size = sizeof(VAL_INT(&(_v->v)));
		break;
	case DB_FLOAT:
		_r->key.data = &VAL_FLOAT(&(_v->v));
		_r->key.size = sizeof(VAL_FLOAT(&(_v->v)));
		break;
	case DB_DATETIME:
		_r->key.data = &VAL_TIME(&(_v->v));
		_r->key.size = sizeof(VAL_TIME(&(_v->v)));
		break;
	case DB_BLOB:
		_r->key.data = VAL_BLOB(&(_v->v)).s;
		_r->key.size = VAL_BLOB(&(_v->v)).len;
		break;
	case DB_DOUBLE:
		_r->key.data = &VAL_DOUBLE(&(_v->v));
		_r->key.size = sizeof(VAL_DOUBLE(&(_v->v)));
		break;
	case DB_STRING:
		_r->key.data = (void *)VAL_STRING(&(_v->v));
		_r->key.size = strlen(VAL_STRING(&(_v->v))) + 1;
		break;
	case DB_STR:
		_r->key.data = VAL_STR(&(_v->v)).s;
		_r->key.size = VAL_STR(&(_v->v)).len;
		break;
	case DB_BITMAP:
		_r->key.data = &VAL_BITMAP(&(_v->v));
		_r->key.size = sizeof(VAL_BITMAP(&(_v->v)));
		break;
	default:
		LOG(L_ERR, "BDB:bdb_set_skey: unknown column type: %0X\n", VAL_TYPE(&(_v->v)));
		break;
	}

#ifdef BDB_EXTRA_DEBUG
	LOG(L_NOTICE, "BDB:bdb_set_skey: use key '%.*s' (%d bytes)\n", _r->key.size, (char *)_r->key.data, _r->key.size);
#endif
};
Пример #4
0
static int db_sqlite_bind_values(sqlite3_stmt* stmt, const db_val_t* v, const int n)
{
	int i, ret;

	if (n>0 && v) {
		for (i=0; i<n; i++) {
			if (VAL_NULL(v+i)) {
				ret=sqlite3_bind_null(stmt, i+1);
				goto check_ret;
			}


			switch(VAL_TYPE(v+i)) {
				/* every param has '+1' index because in sqlite the leftmost
				 * parameter has index '1' */
				case DB_INT:
					ret=sqlite3_bind_int(stmt, i+1, VAL_INT(v+i));
					break;
				case DB_BIGINT:
					ret=sqlite3_bind_int64(stmt, i+1, VAL_BIGINT(v+i));
					break;
				case DB_DOUBLE:
					ret=sqlite3_bind_double(stmt, i+1, VAL_DOUBLE(v+i));
					break;
				case DB_STRING:
					ret=sqlite3_bind_text(stmt, i+1, VAL_STRING(v+i),
											strlen(VAL_STRING(v+i)), SQLITE_STATIC);
					break;
				case DB_STR:
					ret=sqlite3_bind_text(stmt, i+1, VAL_STR(v+i).s,
											VAL_STR(v+i).len, SQLITE_STATIC);
					break;
				case DB_DATETIME:
					ret=sqlite3_bind_int64(stmt, i+1, (long int)VAL_TIME(v+i));
					break;
				case DB_BLOB:
					ret=sqlite3_bind_blob(stmt, i+1, (void*)VAL_BLOB(v+i).s,
											VAL_BLOB(v+i).len, SQLITE_STATIC);
					break;
				case DB_BITMAP:
					ret=sqlite3_bind_int(stmt, i+1, (int)VAL_BITMAP(v+i));
					break;
				default:
					LM_BUG("invalid db type\n");
					return 1;
			}

check_ret:
			if (ret != SQLITE_OK) {
				return ret;
			}
		}
	}

	return SQLITE_OK;
}
Пример #5
0
inline SV *valdata(db_val_t* val) {
	SV *data = &PL_sv_undef;
	const char* stringval;

	switch(VAL_TYPE(val)) {
		case DB1_INT:
			data = newSViv(VAL_INT(val));
			break;

		case DB1_BIGINT:
			LM_ERR("BIGINT not supported");
			data = &PL_sv_undef;
			break;

		case DB1_DOUBLE:
			data = newSVnv(VAL_DOUBLE(val));
			break;

		case DB1_STRING:
			stringval = VAL_STRING(val);
			if (strlen(stringval) > 0)
				data = newSVpv(stringval, strlen(stringval));
			else
				data = &PL_sv_undef;
			break;

		case DB1_STR:
			if (VAL_STR(val).len > 0)
				data = newSVpv(VAL_STR(val).s, VAL_STR(val).len);
			else
				data = &PL_sv_undef;
			break;

		case DB1_DATETIME:
			data = newSViv((unsigned int)VAL_TIME(val));
			break;

		case DB1_BLOB:
			if (VAL_BLOB(val).len > 0)
				data = newSVpv(VAL_BLOB(val).s,
						VAL_BLOB(val).len);
			else
				data = &PL_sv_undef;
			break;

		case DB1_BITMAP:
			data = newSViv(VAL_BITMAP(val));
			break;
	}

	return data;
}
Пример #6
0
/*
 * Get and convert columns from a result. Define handlers and buffers
 */
static int get_columns(ora_con_t* con, db_res_t* _r, OCIStmt* _c, dmap_t* _d)
{
	OCIParam *param;
	size_t tsz;
	ub4 i, n;
	sword status;

	status = OCIAttrGet(_c, OCI_HTYPE_STMT, &n, NULL, OCI_ATTR_PARAM_COUNT,
		con->errhp);

	if (status != OCI_SUCCESS) {
		LM_ERR("driver: %s\n", db_oracle_error(con, status));
		return -1;
	}

	if (!n) {
		LM_ERR("no columns\n");
		return -2;
	}

	if (n >= MAX_DEF_HANDLES) {
		LM_ERR("too many res. Rebuild with MAX_DEF_HANDLES >= %u\n", n);
		return -3;
	}

	if (db_allocate_columns(_r, n) != 0) {
		LM_ERR("could not allocate columns\n");
		return -4;
	}
	for (i = 0; i < n; ++i)
		memset(RES_NAMES(_r)[i], 0, sizeof(db_key_t));

	RES_COL_N(_r) = n;

	tsz = 0;
	memset(_d->defh, 0, sizeof(_d->defh[0]) * n);
	for (i = 0; i < n; i++) {
		ub4 len;
		ub2 dtype;

		status = OCIParamGet(_c, OCI_HTYPE_STMT, con->errhp,
			(dvoid**)(dvoid*)&param, i+1);
		if (status != OCI_SUCCESS) goto ora_err;

		{
			text* name;
			status = OCIAttrGet(param, OCI_DTYPE_PARAM,
				(dvoid**)(dvoid*)&name,	&len, OCI_ATTR_NAME,
				con->errhp);
			if (status != OCI_SUCCESS) goto ora_err;
			RES_NAMES(_r)[i]->s = (char*)pkg_malloc(len+1);
			if (!RES_NAMES(_r)[i]->s) {
				db_free_columns(_r);
				LM_ERR("no private memory left\n");
				return -5;
			}
			RES_NAMES(_r)[i]->len = len;
			memcpy(RES_NAMES(_r)[i]->s, name, len);
			RES_NAMES(_r)[i]->s[len] = '\0';
		}

		status = OCIAttrGet(param, OCI_DTYPE_PARAM,
			(dvoid**)(dvoid*)&dtype, NULL, OCI_ATTR_DATA_TYPE,
			con->errhp);
		if (status != OCI_SUCCESS) goto ora_err;

		switch (dtype) {
		case SQLT_UIN:		/* unsigned integer */
set_bitmap:
			LM_DBG("use DB_BITMAP type\n");
			RES_TYPES(_r)[i] = DB_BITMAP;
			len = sizeof(VAL_BITMAP((db_val_t*)NULL));
			break;

		case SQLT_INT:		/* (ORANET TYPE) integer */
set_int:
			LM_DBG("use DB_INT result type\n");
			RES_TYPES(_r)[i] = DB_INT;
			len = sizeof(VAL_INT((db_val_t*)NULL));
			break;

		case SQLT_LNG:		/* long */
		case SQLT_VNU:		/* NUM with preceding length byte */
		case SQLT_NUM:		/* (ORANET TYPE) oracle numeric */
			len = 0; /* PRECISION is ub1 */
			status = OCIAttrGet(param, OCI_DTYPE_PARAM,
				(dvoid**)(dvoid*)&len, NULL, OCI_ATTR_PRECISION,
				con->errhp);
			if (status != OCI_SUCCESS) goto ora_err;
			if (len <= 11) {
				sb1 sc;
				status = OCIAttrGet(param, OCI_DTYPE_PARAM,
					(dvoid**)(dvoid*)&sc, NULL,
					OCI_ATTR_SCALE, con->errhp);
				if (status != OCI_SUCCESS) goto ora_err;
				if (!sc) {
					dtype = SQLT_INT;
					if (len != 11) goto set_int;
					dtype = SQLT_UIN;
					goto set_bitmap;
				}
			}
			LM_DBG("use DB_BIGINT result type\n");
			RES_TYPES(_r)[i] = DB_BIGINT;
			len = sizeof(VAL_BIGINT((db_val_t*)NULL));
			dtype = SQLT_NUM;
			break;

		case SQLT_FLT:		/* (ORANET TYPE) Floating point number */
		case SQLT_BFLOAT:       /* Native Binary float*/
		case SQLT_BDOUBLE:	/* NAtive binary double */
		case SQLT_IBFLOAT:	/* binary float canonical */
		case SQLT_IBDOUBLE:	/* binary double canonical */
		case SQLT_PDN:		/* (ORANET TYPE) Packed Decimal Numeric */
			LM_DBG("use DB_DOUBLE result type\n");
			RES_TYPES(_r)[i] = DB_DOUBLE;
			len = sizeof(VAL_DOUBLE((db_val_t*)NULL));
			dtype = SQLT_FLT;
			break;

//		case SQLT_TIME:		/* TIME */
//		case SQLT_TIME_TZ:	/* TIME WITH TIME ZONE */
		case SQLT_DATE:		/* ANSI Date */
		case SQLT_DAT:		/* date in oracle format */
		case SQLT_ODT:		/* OCIDate type */
		case SQLT_TIMESTAMP:	/* TIMESTAMP */
		case SQLT_TIMESTAMP_TZ:	/* TIMESTAMP WITH TIME ZONE */
		case SQLT_TIMESTAMP_LTZ:/* TIMESTAMP WITH LOCAL TZ */
//		case SQLT_INTERVAL_YM:	/* INTERVAL YEAR TO MONTH */
//		case SQLT_INTERVAL_DS:	/* INTERVAL DAY TO SECOND */
			LM_DBG("use DB_DATETIME result type\n");
			RES_TYPES(_r)[i] = DB_DATETIME;
			len = sizeof(OCIDate);
			dtype = SQLT_ODT;
			break;

		case SQLT_CLOB:		/* character lob */
		case SQLT_BLOB:		/* binary lob */
//		case SQLT_BFILEE:	/* binary file lob */
//		case SQLT_CFILEE:	/* character file lob */
//		case SQLT_BIN:		/* binary data(DTYBIN) */
//		case SQLT_LBI:		/* long binary */
			LM_DBG("use DB_BLOB result type\n");
			RES_TYPES(_r)[i] = DB_BLOB;
			goto dyn_str;

		case SQLT_CHR:		/* (ORANET TYPE) character string */
		case SQLT_STR:		/* zero terminated string */
		case SQLT_VST:		/* OCIString type */
		case SQLT_VCS:		/* Variable character string */
		case SQLT_AFC:		/* Ansi fixed char */
		case SQLT_AVC:		/* Ansi Var char */
//		case SQLT_RID:		/* rowid */
			LM_DBG("use DB_STR result type\n");
			RES_TYPES(_r)[i] = DB_STR;
dyn_str:
			dtype = SQLT_CHR;
			len = 0; /* DATA_SIZE is ub2 */
			status = OCIAttrGet(param, OCI_DTYPE_PARAM,
				(dvoid**)(dvoid*)&len, NULL, OCI_ATTR_DATA_SIZE,
				con->errhp);
			if (status != OCI_SUCCESS) goto ora_err;
			if (len >= 4000) {
				LM_DBG("use DB_BLOB result type\n");
				RES_TYPES(_r)[i] = DB_BLOB;
			}
			++len;
			break;

		default:
			LM_ERR("unsupported datatype %d\n", dtype);
			goto stop_load;
		}
		_d->ilen[i] = (ub2)len;
		_d->pv[i].v = st_buf + tsz;
		tsz += len;
		status = OCIDefineByPos(_c, &_d->defh[i], con->errhp, i+1,
			_d->pv[i].v, len, dtype, &_d->ind[i],
			&_d->len[i], NULL, OCI_DEFAULT);
		if (status != OCI_SUCCESS) goto ora_err;
	}

#if STATIC_BUF_LEN < 65536
#error
#endif
	if (tsz > 65536) {
		LM_ERR("Row size exceed 65K. IOB's are not supported\n");
		goto stop_load;
	}
	return 0;

ora_err:
	LM_ERR("driver: %s\n", db_oracle_error(con, status));
stop_load:
	db_free_columns(_r);
	return -6;
}
Пример #7
0
/*
 * Convert data fron db format to internal format
 */
static int convert_row(db_res_t* _res, db_row_t* _r, dmap_t* _d)
{
	unsigned i, n = RES_COL_N(_res);

	ROW_N(_r) = n;

	for (i = 0; i < n; i++) {
		static const str dummy_string = {"", 0};

		db_val_t* v = &ROW_VALUES(_r)[i];
		db_type_t t = RES_TYPES(_res)[i];

		if (_d->ind[i] == -1) {
			/* Initialize the string pointers to a dummy empty
			 * string so that we do not crash when the NULL flag
			 * is set but the module does not check it properly
			 */
			VAL_STRING(v) = dummy_string.s;
			VAL_STR(v) = dummy_string;
			VAL_BLOB(v) = dummy_string;
			VAL_TYPE(v) = t;
			VAL_NULL(v) = 1;
			continue;
		}

		if (_d->ind[i])
			LM_WARN("truncated value in DB\n");

		VAL_TYPE(v) = t;
		switch (t) {
		case DB_INT:
			VAL_INT(v) = *_d->pv[i].i;
			break;

		case DB_BIGINT:
			VAL_BIGINT(v) = *_d->pv[i].i;
			break;

		case DB_BITMAP:
			VAL_BITMAP(v) = *_d->pv[i].i;
			break;

		case DB_DOUBLE:
			VAL_DOUBLE(v) = *_d->pv[i].f;
			break;

		case DB_DATETIME:
			{
				struct tm tm;
				memset(&tm, 0, sizeof(tm));
				OCIDateGetTime(_d->pv[i].o, &tm.tm_hour,
					&tm.tm_min, &tm.tm_sec);
				OCIDateGetDate(_d->pv[i].o, &tm.tm_year,
					&tm.tm_mon, &tm.tm_mday);
				if (tm.tm_mon)
					--tm.tm_mon;
				if (tm.tm_year >= 1900)
					tm.tm_year -= 1900;
				VAL_TIME(v) = mktime(&tm);
			}
			break;

		case DB_STR:
		case DB_BLOB:
		case DB_STRING:
			{
				size_t len = _d->len[i];
				char *pstr = pkg_malloc(len+1);

				if (pstr == NULL)
					return -1;

				memcpy(pstr, _d->pv[i].c, len);
				pstr[len] = '\0';
				VAL_FREE(v) = 1;
				if (t == DB_STR) {
					VAL_STR(v).s = pstr;
					VAL_STR(v).len = len;
				} else if (t == DB_BLOB) {
					VAL_BLOB(v).s = pstr;
					VAL_BLOB(v).len = len;
				} else {
					VAL_STRING(v) = pstr;
				}
			}
			break;

		default:
			LM_ERR("unknown type mapping (%u)\n", t);
			return -2;
		}
	}

	return 0;
}
Пример #8
0
/*
 * Insert a row into specified table
 * h: structure representing database connection
 * k: key names
 * v: values of the keys
 * n: number of key=value pairs
 */
int flat_db_insert(const db_con_t* h, const db_key_t* k, const db_val_t* v,
		const int n)
{
	FILE* f;
	int i;
	int auxl;
	str aux;
	char * begin = flat_iov_buf.s;

	if (local_timestamp < *flat_rotate) {
		flat_rotate_logs();
		local_timestamp = *flat_rotate;
	}

	if ( !h || !CON_TAIL(h) || (f=CON_FILE(h))==NULL ) {
		LM_ERR("uninitialized connection\n");
		return -1;
	}

	if (flat_prepare_iovec(n) < 0) {
		LM_ERR("cannot insert row\n");
		return -1;
	}

	FLAT_LOCK(f);

	for(i = 0; i < n; i++) {
		if (VAL_NULL(v + i)) {
			FLAT_SET_STR(i, "");
			FLAT_SET_LEN(i, 0);
			continue;
		}
		FLAT_SET_STR(i, FLAT_BUF);
		switch(VAL_TYPE(v + i)) {
		case DB_INT:
			/* guess this is 20 */
			FLAT_ALLOC(20);
			FLAT_PRINTF("%d", VAL_INT(v+i), i);
			break;

		case DB_DOUBLE:
			/* guess there are max 20 digits */
			FLAT_ALLOC(40);
			FLAT_PRINTF("%f", VAL_DOUBLE(v+i), i);
			break;

		case DB_BIGINT:
			/* guess there are max 20 digits */
			FLAT_ALLOC(40);
			FLAT_PRINTF("%llu", VAL_BIGINT(v+i), i);
			break;

		case DB_STRING:
			auxl = strlen(VAL_STRING(v + i));
			FLAT_ALLOC(auxl * 4);
			FLAT_COPY(i, VAL_STRING(v + i), auxl);
			break;

		case DB_STR:
			FLAT_ALLOC(VAL_STR(v + i).len * 4);
			FLAT_COPY(i, VAL_STR(v + i).s, VAL_STR(v + i).len);
			break;

		case DB_DATETIME:
			/* guess this is 20 */
			FLAT_ALLOC(20);
			FLAT_PRINTF("%lu", VAL_TIME(v+i), i);
			break;

		case DB_BLOB:
			auxl = VAL_BLOB(v+i).len;
			/* the maximum size is 4l - if all chars were not printable */
			FLAT_ALLOC(4 * auxl);
			FLAT_COPY(i, VAL_BLOB(v+i).s, auxl);
			break;

		case DB_BITMAP:
			/* guess this is 20 */
			FLAT_ALLOC(20);
			FLAT_PRINTF("%u", VAL_BITMAP(v+i), i);
			break;
		}
	}
	/* reorder pointers in case they were altered by (re)allocation */
	if (flat_iov_buf.s != begin && flat_iov_buf.len) {
		FLAT_RESET();
		for (i = 0; i < n; i++) {
			if (!VAL_NULL(v + i)) {
				FLAT_SET_STR(i, FLAT_BUF);
				FLAT_INC(FLAT_GET_LEN(i));
			}
		}
	}

	do {
		auxl = writev(fileno(f), flat_iov, 2 * n);
	} while (auxl < 0 && errno == EINTR);

	if (auxl < 0) {
		LM_ERR("unable to write to file: %s - %d\n", strerror(errno), errno);
		return -1;
	}

	/* XXX does this make sense any more? */
	if (flat_flush && fflush(f) < 0) {
		LM_ERR("cannot flush buffer: %s - %d\n", strerror(errno), errno);
	}
	FLAT_UNLOCK(f);


	return 0;
}
Пример #9
0
static inline int get_all_db_ucontacts(void *buf, int len, unsigned int flags,
								unsigned int part_idx, unsigned int part_max)
{
	static char query_buf[512];
	static str query_str;

	struct socket_info *sock;
	unsigned int dbflags;
	db_res_t* res = NULL;
	db_row_t *row;
	dlist_t *dom;
	char *p, *p1;
	char now_s[25];
	int now_len;
	int port, proto, p_len, p1_len;
	str host;
	int i;
	void *cp;
	int shortage, needed;

	cp = buf;
	shortage = 0;
	/* Reserve space for terminating 0000 */
	len -= sizeof(p_len);

	/* get the current time in DB format */
	now_len = 25;
	if (db_time2str( time(0), now_s, &now_len)!=0) {
		LM_ERR("failed to print now time\n");
		return -1;
	}

	for (dom = root; dom!=NULL ; dom=dom->next) {
		/* build query */
		i = snprintf( query_buf, sizeof(query_buf), "select %.*s, %.*s, %.*s,"
#ifdef ORACLE_USRLOC
			" %.*s, %.*s from %s where %.*s > %.*s and "
			"bitand(%.*s, %d) = %d and mod(id, %u) = %u",
#else
			" %.*s, %.*s from %s where %.*s > %.*s and %.*s & %d = %d and "
			"id %% %u = %u",
#endif
			received_col.len, received_col.s,
			contact_col.len, contact_col.s,
			sock_col.len, sock_col.s,
			cflags_col.len, cflags_col.s,
			path_col.len, path_col.s,
			dom->d->name->s,
			expires_col.len, expires_col.s,
			now_len, now_s,
			cflags_col.len, cflags_col.s,
			flags, flags, part_max, part_idx);
		if ( i>=sizeof(query_buf) ) {
			LM_ERR("DB query too long\n");
			return -1;
		}
		query_str.s = query_buf;
		query_str.len = i;
		if ( ul_dbf.raw_query( ul_dbh, &query_str, &res)<0 ) {
			LM_ERR("raw_query failed\n");
			return -1;
		}
		if( RES_ROW_N(res)==0 ) {
			ul_dbf.free_result(ul_dbh, res);
			continue;
		}

		for(i = 0; i < RES_ROW_N(res); i++) {
			row = RES_ROWS(res) + i;

			/* received */
			p = (char*)VAL_STRING(ROW_VALUES(row));
			if ( VAL_NULL(ROW_VALUES(row)) || p==0 || p[0]==0 ) {
				/* contact */
				p = (char*)VAL_STRING(ROW_VALUES(row)+1);
				if (VAL_NULL(ROW_VALUES(row)+1) || p==0 || p[0]==0) {
					LM_ERR("empty contact -> skipping\n");
					continue;
				}
			}
			p_len = strlen(p);

			/* path */
			p1 = (char*)VAL_STRING(ROW_VALUES(row)+4);
			if (VAL_NULL(ROW_VALUES(row)+4) || p1==0 || p1[0]==0){
				p1 = NULL;
				p1_len = 0;
			} else {
				p1_len = strlen(p1);
			}

			needed = (int)(sizeof(p_len)+p_len+sizeof(sock)+sizeof(dbflags)+
				sizeof(p1_len)+p1_len);
			if (len < needed) {
				shortage += needed ;
				continue;
			}

			/* write received/contact */
			memcpy(cp, &p_len, sizeof(p_len));
			cp = (char*)cp + sizeof(p_len);
			memcpy(cp, p, p_len);
			cp = (char*)cp + p_len;

			/* sock */
			p  = (char*)VAL_STRING(ROW_VALUES(row) + 2);
			if (VAL_NULL(ROW_VALUES(row)+2) || p==0 || p[0]==0){
				sock = 0;
			} else {
				if (parse_phostport( p, strlen(p), &host.s, &host.len,
				&port, &proto)!=0) {
					LM_ERR("bad socket <%s>...ignoring\n", p);
					sock = 0;
				} else {
					sock = grep_sock_info( &host, (unsigned short)port, proto);
					if (sock==0) {
						LM_DBG("non-local socket <%s>...ignoring\n", p);
					}
				}
			}

			/* flags */
			dbflags = VAL_BITMAP(ROW_VALUES(row) + 3);

			/* write sock and flags */
			memcpy(cp, &sock, sizeof(sock));
			cp = (char*)cp + sizeof(sock);
			memcpy(cp, &dbflags, sizeof(dbflags));
			cp = (char*)cp + sizeof(dbflags);

			/* write path */
			memcpy(cp, &p1_len, sizeof(p1_len));
			cp = (char*)cp + sizeof(p1_len);
			memcpy(cp, p1, p1_len);
			cp = (char*)cp + p1_len;

			len -= needed;
		} /* row cycle */

		ul_dbf.free_result(ul_dbh, res);
	} /* domain cycle */

	/* len < 0 is possible, if size of the buffer < sizeof(c->c.len) */
	if (len >= 0)
		memset(cp, 0, sizeof(p_len));

	/* Shouldn't happen */
	if (shortage > 0 && len > shortage) {
		abort();
	}

	shortage -= len;

	return shortage > 0 ? shortage : 0;
}
Пример #10
0
/*!
 * \brief Get all contacts from the database, in partitions if wanted
 * \see get_all_ucontacts
 * \param buf target buffer
 * \param len length of buffer
 * \param flags contact flags
 * \param part_idx part index
 * \param part_max maximal part
 * \param options options
 * \return 0 on success, positive if buffer size was not sufficient, negative on failure
 */
static inline int get_all_db_ucontacts(void *buf, int len, unsigned int flags,
								unsigned int part_idx, unsigned int part_max,
								int options)
{
	struct socket_info *sock;
	unsigned int dbflags;
	db1_res_t* res = NULL;
	db_row_t *row;
	dlist_t *dom;
	int port, proto;
	char *p;
	str addr;
	str recv;
	str path;
	str ruid;
	str host;
	unsigned int aorhash;
	int i;
	void *cp;
	int shortage, needed;
	db_key_t keys1[4]; /* where */
	db_val_t vals1[4];
	db_op_t  ops1[4];
	db_key_t keys2[6]; /* select */
	int n[2] = {2,6}; /* number of dynamic values used on key1/key2 */

	cp = buf;
	shortage = 0;
	/* Reserve space for terminating 0000 */
	len -= sizeof(addr.len);

	aorhash = 0;

	/* select fields */
	keys2[0] = &received_col;
	keys2[1] = &contact_col;
	keys2[2] = &sock_col;
	keys2[3] = &cflags_col;
	keys2[4] = &path_col;
	keys2[5] = &ruid_col;

	/* where fields */
	keys1[0] = &expires_col;
	ops1[0] = OP_GT;
	vals1[0].nul = 0;
	UL_DB_EXPIRES_SET(&vals1[0], time(0));

	keys1[1] = &partition_col;
	ops1[1] = OP_EQ;
	vals1[1].type = DB1_INT;
	vals1[1].nul = 0;
	if(_ul_max_partition>0)
		vals1[1].val.int_val = part_idx;
	else
		vals1[1].val.int_val = 0;

	if (flags & nat_bflag) {
		keys1[n[0]] = &keepalive_col;
		ops1[n[0]] = OP_EQ;
		vals1[n[0]].type = DB1_INT;
		vals1[n[0]].nul = 0;
		vals1[n[0]].val.int_val = 1;
		n[0]++;
	}
	if(options&GAU_OPT_SERVER_ID) {
		keys1[n[0]] = &srv_id_col;
		ops1[n[0]] = OP_EQ;
		vals1[n[0]].type = DB1_INT;
		vals1[n[0]].nul = 0;
		vals1[n[0]].val.int_val = server_id;
		n[0]++;
	}

	/* TODO: use part_idx and part_max on keys1 */

	for (dom = root; dom!=NULL ; dom=dom->next) {
		if (ul_dbf.use_table(ul_dbh, dom->d->name) < 0) {
			LM_ERR("sql use_table failed\n");
			return -1;
		}
		if (ul_dbf.query(ul_dbh, keys1, ops1, vals1, keys2,
							n[0], n[1], NULL, &res) <0 ) {
			LM_ERR("query error\n");
			return -1;
		}
		if( RES_ROW_N(res)==0 ) {
			ul_dbf.free_result(ul_dbh, res);
			continue;
		}

		for(i = 0; i < RES_ROW_N(res); i++) {
			row = RES_ROWS(res) + i;


			/* received */
			recv.s = (char*)VAL_STRING(ROW_VALUES(row));
			if ( VAL_NULL(ROW_VALUES(row)) || recv.s==0 || recv.s[0]==0 ) {
				recv.s = NULL;
				recv.len = 0;
			}
			else {
				recv.len = strlen(recv.s);
			}

			/* contact */
			addr.s = (char*)VAL_STRING(ROW_VALUES(row)+1);
			if (VAL_NULL(ROW_VALUES(row)+1) || addr.s==0 || addr.s[0]==0) {
				LM_ERR("empty contact -> skipping\n");
				continue;
			}
			else {
				addr.len = strlen(addr.s);
			}

			/* path */
			path.s = (char*)VAL_STRING(ROW_VALUES(row)+4);
			if (VAL_NULL(ROW_VALUES(row)+4) || path.s==0 || path.s[0]==0){
				path.s = NULL;
				path.len = 0;
			} else {
				path.len = strlen(path.s);
			}

			/* ruid */
			ruid.s = (char*)VAL_STRING(ROW_VALUES(row)+5);
			if (VAL_NULL(ROW_VALUES(row)+5) || ruid.s==0 || ruid.s[0]==0){
				ruid.s = NULL;
				ruid.len = 0;
			} else {
				ruid.len = strlen(ruid.s);
			}

			needed = (int)(sizeof(addr.len) + addr.len
					+ sizeof(recv.len) + recv.len
					+ sizeof(sock) + sizeof(dbflags)
					+ sizeof(path.len) + path.len
					+ sizeof(ruid.len) + ruid.len
					+ sizeof(aorhash));
			if (len < needed) {
				shortage += needed ;
				continue;
			}

			/* write contact */
			memcpy(cp, &addr.len, sizeof(addr.len));
			cp = (char*)cp + sizeof(addr.len);
			memcpy(cp, addr.s, addr.len);
			cp = (char*)cp + addr.len;

			/* write received */
			memcpy(cp, &recv.len, sizeof(recv.len));
			cp = (char*)cp + sizeof(recv.len);
			/* copy received only if exist */
			if(recv.len){
				memcpy(cp, recv.s, recv.len);
				cp = (char*)cp + recv.len;
			}

			/* sock */
			p  = (char*)VAL_STRING(ROW_VALUES(row) + 2);
			if (VAL_NULL(ROW_VALUES(row)+2) || p==0 || p[0]==0){
				sock = 0;
			} else {
				if (parse_phostport( p, &host.s, &host.len,
				&port, &proto)!=0) {
					LM_ERR("bad socket <%s>...set to 0\n", p);
					sock = 0;
				} else {
					sock = grep_sock_info( &host, (unsigned short)port, proto);
					if (sock==0) {
						LM_DBG("non-local socket <%s>...set to 0\n", p);
					}
				}
			}

			/* flags */
			dbflags = VAL_BITMAP(ROW_VALUES(row) + 3);

			/* write sock and flags */
			memcpy(cp, &sock, sizeof(sock));
			cp = (char*)cp + sizeof(sock);
			memcpy(cp, &dbflags, sizeof(dbflags));
			cp = (char*)cp + sizeof(dbflags);

			/* write path */
			memcpy(cp, &path.len, sizeof(path.len));
			cp = (char*)cp + sizeof(path.len);
			/* copy path only if exist */
			if(path.len){
				memcpy(cp, path.s, path.len);
				cp = (char*)cp + path.len;
			}

			/* write ruid */
			memcpy(cp, &ruid.len, sizeof(ruid.len));
			cp = (char*)cp + sizeof(ruid.len);
			/* copy ruid only if exist */
			if(ruid.len){
				memcpy(cp, ruid.s, ruid.len);
				cp = (char*)cp + ruid.len;
			}
			/* aorhash not used for db-only records, but it is added
			 * (as 0) to match the struct used for mem records */
			memcpy(cp, &aorhash, sizeof(aorhash));
			cp = (char*)cp + sizeof(aorhash);

			len -= needed;
		} /* row cycle */

		ul_dbf.free_result(ul_dbh, res);
	} /* domain cycle */

	/* len < 0 is possible, if size of the buffer < sizeof(c->c.len) */
	if (len >= 0)
		memset(cp, 0, sizeof(addr.len));

	/* Shouldn't happen */
	if (shortage > 0 && len > shortage) {
		abort();
	}

	shortage -= len;

	return shortage > 0 ? shortage : 0;
}
Пример #11
0
/*
 * Used when converting values to be used in a DB query
 */
int db_mysql_val2str(const db_con_t* _c, const db_val_t* _v, char* _s, int* _len)
{
	int l;
	char* old_s;

	if (!_c || !_v || !_s || !_len || !*_len) {
		LM_ERR("invalid parameter value\n");
		return -1;
	}

	if (VAL_NULL(_v)) {
		if (*_len < sizeof("NULL")) {
			LM_ERR("buffer too small\n");
			return -1;
		}
		*_len = snprintf(_s, *_len, "NULL");
		return 0;
	}
	
	switch(VAL_TYPE(_v)) {
	case DB_INT:
		if (db_int2str(VAL_INT(_v), _s, _len) < 0) {
			LM_ERR("error while converting string to int\n");
			return -2;
		} else {
			return 0;
		}
		break;

	case DB_BIGINT:
		if (db_bigint2str(VAL_BIGINT(_v), _s, _len) < 0) {
			LM_ERR("error while converting bigint to string\n");
			return -2;
		} else {
			return 0;
		}
		break;


	case DB_BITMAP:
		if (db_int2str(VAL_BITMAP(_v), _s, _len) < 0) {
			LM_ERR("error while converting string to int\n");
			return -3;
		} else {
			return 0;
		}
		break;

	case DB_DOUBLE:
		if (db_double2str(VAL_DOUBLE(_v), _s, _len) < 0) {
			LM_ERR("error while converting string to double\n");
			return -4;
		} else {
			return 0;
		}
		break;

	case DB_STRING:
		l = strlen(VAL_STRING(_v));
		if (*_len < (l * 2 + 3)) {
			LM_ERR("destination buffer too short\n");
			return -5;
		} else {
			old_s = _s;
			*_s++ = '\'';
			_s += mysql_real_escape_string(CON_CONNECTION(_c), _s, VAL_STRING(_v), l);
			*_s++ = '\'';
			*_s = '\0'; /* FIXME */
			*_len = _s - old_s;
			return 0;
		}
		break;

	case DB_STR:
		if (*_len < (VAL_STR(_v).len * 2 + 3)) {
			LM_ERR("destination buffer too short\n");
			return -6;
		} else {
			old_s = _s;
			*_s++ = '\'';
			_s += mysql_real_escape_string(CON_CONNECTION(_c), _s, VAL_STR(_v).s, VAL_STR(_v).len);
			*_s++ = '\'';
			*_s = '\0';
			*_len = _s - old_s;
			return 0;
		}
		break;

	case DB_DATETIME:
		if (db_time2str(VAL_TIME(_v), _s, _len) < 0) {
			LM_ERR("error while converting string to time_t\n");
			return -7;
		} else {
			return 0;
		}
		break;

	case DB_BLOB:
		l = VAL_BLOB(_v).len;
		if (*_len < (l * 2 + 3)) {
			LM_ERR("destination buffer too short\n");
			return -8;
		} else {
			old_s = _s;
			*_s++ = '\'';
			_s += mysql_real_escape_string(CON_CONNECTION(_c), _s, VAL_STR(_v).s, l);
			*_s++ = '\'';
			*_s = '\0';
			*_len = _s - old_s;
			return 0;
		}			
		break;

	default:
		LM_DBG("unknown data type\n");
		return -9;
	}
	/*return -8; --not reached*/
}
Пример #12
0
/*
 * Used when converting result from a query
 */
int val2str(MYSQL* _c, db_val_t* _v, char* _s, int* _len)
{
	int l;
	char* old_s;

	if (!_c || !_v || !_s || !_len || !*_len) {
		LOG(L_ERR, "val2str: Invalid parameter value\n");
		return -1;
	}

	if (VAL_NULL(_v)) {
		if (*_len < sizeof("NULL")) {
			LOG(L_ERR, "val2str: Buffer too small\n");
			return -1;
		}
		*_len = snprintf(_s, *_len, "NULL");
		return 0;
	}
	
	switch(VAL_TYPE(_v)) {
	case DB_INT:
		if (int2str(VAL_INT(_v), _s, _len) < 0) {
			LOG(L_ERR, "val2str: Error while converting string to int\n");
			return -2;
		} else {
			return 0;
		}
		break;

	case DB_BITMAP:
		if (int2str(VAL_BITMAP(_v), _s, _len) < 0) {
			LOG(L_ERR, "val2str: Error while converting string to int\n");
			return -3;
		} else {
			return 0;
		}
		break;

	case DB_DOUBLE:
		if (double2str(VAL_DOUBLE(_v), _s, _len) < 0) {
			LOG(L_ERR, "val2str: Error while converting string to double\n");
			return -4;
		} else {
			return 0;
		}
		break;

	case DB_STRING:
		l = strlen(VAL_STRING(_v));
		if (*_len < (l * 2 + 3)) {
			LOG(L_ERR, "val2str: Destination buffer too short\n");
			return -5;
		} else {
			old_s = _s;
			*_s++ = '\'';
			_s += mysql_real_escape_string(_c, _s, VAL_STRING(_v), l);
			*_s++ = '\'';
			*_s = '\0'; /* FIXME */
			*_len = _s - old_s;
			return 0;
		}
		break;

	case DB_STR:
		l = VAL_STR(_v).len;
		if (*_len < (l * 2 + 3)) {
			LOG(L_ERR, "val2str: Destination buffer too short\n");
			return -6;
		} else {
			old_s = _s;
			*_s++ = '\'';
			_s += mysql_real_escape_string(_c, _s, VAL_STR(_v).s, l);
			*_s++ = '\'';
			*_s = '\0';
			*_len = _s - old_s;
			return 0;
		}
		break;

	case DB_DATETIME:
		if (time2str(VAL_TIME(_v), _s, _len) < 0) {
			LOG(L_ERR, "val2str: Error while converting string to time_t\n");
			return -7;
		} else {
			return 0;
		}
		break;

	case DB_BLOB:
		l = VAL_BLOB(_v).len;
		if (*_len < (l * 2 + 3)) {
			LOG(L_ERR, "val2str: Destination buffer too short\n");
			return -8;
		} else {
			old_s = _s;
			*_s++ = '\'';
			_s += mysql_escape_string(_s, VAL_STR(_v).s, l);
			*_s++ = '\'';
			*_s = '\0';
			*_len = _s - old_s;
			return 0;
		}			
		break;

	default:
		DBG("val2str: Unknown data type\n");
		return -9;
	}
	/*return -8; --not reached*/
}
Пример #13
0
int bdb_sfield_db2bdb(bdb_sval_p v, db_val_t* _v, db_op_t _op)
{
	char	*s;

	VAL_NULL(&(v->v)) = VAL_NULL(_v);
	VAL_TYPE(&(v->v)) = VAL_TYPE(_v);

	if (!VAL_NULL(&(v->v))) {
		switch (VAL_TYPE(_v)) {
		case DB_INT:
			VAL_INT(&(v->v)) = VAL_INT(_v);
			break;
		case DB_FLOAT:
			VAL_FLOAT(&(v->v)) = VAL_FLOAT(_v);
			break;
		case DB_DATETIME:
			VAL_TIME(&(v->v)) = VAL_TIME(_v);
			break;
		case DB_BLOB:
			s = pkg_malloc(VAL_BLOB(_v).len);
			memcpy(s, VAL_BLOB(_v).s, VAL_BLOB(_v).len);
			VAL_BLOB(&(v->v)).s = s;
			VAL_BLOB(&(v->v)).len = VAL_BLOB(_v).len;
			break;
		case DB_DOUBLE:
			VAL_DOUBLE(&(v->v)) = VAL_DOUBLE(_v);
			break;
		case DB_STRING:
			VAL_STR(&(v->v)).len = strlen(VAL_STRING(_v)) + 1;
			s = pkg_malloc(VAL_STR(&(v->v)).len);
			strcpy(s, VAL_STRING(_v));
			VAL_STRING(&(v->v)) = s;
			break;
		case DB_STR:
			s = pkg_malloc(VAL_STR(_v).len);
			memcpy(s, VAL_STR(_v).s, VAL_STR(_v).len);
			VAL_STR(&(v->v)).s = s;
			VAL_STR(&(v->v)).len = VAL_STR(_v).len;
			break;
		case DB_BITMAP:
			VAL_BITMAP(&(v->v)) = VAL_BITMAP(_v);
			break;
		default:
			LOG(L_ERR, "BDB:bdb_sfield_db2bdb: unknown column type: %0X\n", VAL_TYPE(_v));
			return -1;
			break;
		}

		if (!_op || !strcmp(_op, OP_EQ)) {
			v->op = BDB_OP_EQ;
		} else if (!strcmp(_op, OP_LT)) {
			v->op = BDB_OP_LT;
		} else if (!strcmp(_op, OP_GT)) {
			v->op = BDB_OP_GT;
		} else if (!strcmp(_op, OP_LEQ)) {
			v->op = BDB_OP_LEQ;
		} else if (!strcmp(_op, OP_GEQ)) {
			v->op = BDB_OP_GEQ;
		} else {
			LOG(L_ERR, "BDB:sbdb_field_db2bdb: unknown operator: %s\n", _op);
			return -1;
		}
	}

	return 0;
};
Пример #14
0
/*
 * Send an SQL query to the server
 */
static int db_sqlite_submit_query(const db1_con_t* _h, const str* _s)
{
	struct sqlite_connection *conn = CON_SQLITE(_h);
	sqlite3_stmt *stmt;
	const db_val_t *val;
	int rc, i;

	LM_DBG("submit_query: %.*s\n", _s->len, _s->s);

	rc = sqlite3_prepare_v2(conn->conn, _s->s, _s->len, &stmt, NULL);
	if (rc != SQLITE_OK) {
		LM_ERR("failed to prepare statement: %s\n",
			sqlite3_errmsg(conn->conn));
		return -1;
	}
	conn->stmt = stmt;

	for (i = 1; i <= conn->bindpos; i++) {
		val = conn->bindarg[i-1];
		if (VAL_NULL(val)) {
			rc = sqlite3_bind_null(stmt, i);
		} else switch (VAL_TYPE(val)) {
		case DB1_INT:
			rc = sqlite3_bind_int(stmt, i, VAL_INT(val));
			break;
		case DB1_BIGINT:
			rc = sqlite3_bind_int64(stmt, i, VAL_BIGINT(val));
			break;
		case DB1_DOUBLE:
			rc = sqlite3_bind_double(stmt, i, VAL_DOUBLE(val));
			break;
		case DB1_STRING:
			rc = sqlite3_bind_text(stmt, i,
				VAL_STRING(val), -1, NULL);
			break;
		case DB1_STR:
			rc = sqlite3_bind_text(stmt, i,
				VAL_STR(val).s, VAL_STR(val).len, NULL);
			break;
		case DB1_DATETIME:
			rc = sqlite3_bind_double(stmt, i, timet_to_sqlite(VAL_TIME(val)));
			break;
		case DB1_BLOB:
			rc = sqlite3_bind_blob(stmt, i,
				VAL_BLOB(val).s, VAL_BLOB(val).len,
				NULL);
			break;
		case DB1_BITMAP:
			rc = sqlite3_bind_int(stmt, i, VAL_BITMAP(val));
			break;
		default:
			LM_ERR("unknown bind value type %d\n", VAL_TYPE(val));
			return -1;
		}
		if (rc != SQLITE_OK) {
			LM_ERR("Parameter bind failed: %s\n",
				sqlite3_errmsg(conn->conn));
			return -1;
		}
	}

	return 0;
}
Пример #15
0
/*!
 * \brief Convert database values into ucontact_info
 *
 * Convert database values into ucontact_info,
 * expects 12 rows (contact, expirs, q, callid, cseq, flags,
 * ua, received, path, socket, methods, last_modified)
 * \param vals database values
 * \param contact contact
 * \return pointer to the ucontact_info on success, 0 on failure
 */
static inline ucontact_info_t* dbrow2info( db_val_t *vals, str *contact)
{
    static ucontact_info_t ci;
    static str callid, ua, received, host, path;
    int port, proto;
    char *p;

    memset( &ci, 0, sizeof(ucontact_info_t));

    contact->s = (char*)VAL_STRING(vals);
    if (VAL_NULL(vals) || contact->s==0 || contact->s[0]==0) {
        LM_CRIT("bad contact\n");
        return 0;
    }
    contact->len = strlen(contact->s);

    if (VAL_NULL(vals+1)) {
        LM_CRIT("empty expire\n");
        return 0;
    }
    ci.expires = VAL_TIME(vals+1);

    if (VAL_NULL(vals+2)) {
        LM_CRIT("empty q\n");
        return 0;
    }
    ci.q = double2q(VAL_DOUBLE(vals+2));

    if (VAL_NULL(vals+4)) {
        LM_CRIT("empty cseq_nr\n");
        return 0;
    }
    ci.cseq = VAL_INT(vals+4);

    callid.s = (char*)VAL_STRING(vals+3);
    if (VAL_NULL(vals+3) || !callid.s || !callid.s[0]) {
        LM_CRIT("bad callid\n");
        return 0;
    }
    callid.len  = strlen(callid.s);
    ci.callid = &callid;

    if (VAL_NULL(vals+5)) {
        LM_CRIT("empty flag\n");
        return 0;
    }
    ci.flags  = VAL_BITMAP(vals+5);

    if (VAL_NULL(vals+6)) {
        LM_CRIT("empty cflag\n");
        return 0;
    }
    ci.cflags  = VAL_BITMAP(vals+6);

    ua.s  = (char*)VAL_STRING(vals+7);
    if (VAL_NULL(vals+7) || !ua.s || !ua.s[0]) {
        ua.s = 0;
        ua.len = 0;
    } else {
        ua.len = strlen(ua.s);
    }
    ci.user_agent = &ua;

    received.s  = (char*)VAL_STRING(vals+8);
    if (VAL_NULL(vals+8) || !received.s || !received.s[0]) {
        received.len = 0;
        received.s = 0;
    } else {
        received.len = strlen(received.s);
    }
    ci.received = received;

    path.s  = (char*)VAL_STRING(vals+9);
    if (VAL_NULL(vals+9) || !path.s || !path.s[0]) {
        path.len = 0;
        path.s = 0;
    } else {
        path.len = strlen(path.s);
    }
    ci.path= &path;

    /* socket name */
    p  = (char*)VAL_STRING(vals+10);
    if (VAL_NULL(vals+10) || p==0 || p[0]==0) {
        ci.sock = 0;
    } else {
        if (parse_phostport( p, &host.s, &host.len,
                             &port, &proto)!=0) {
            LM_ERR("bad socket <%s>\n", p);
            return 0;
        }
        ci.sock = grep_sock_info( &host, (unsigned short)port, proto);
        if (ci.sock==0) {
            LM_INFO("non-local socket <%s>...ignoring\n", p);
        }
    }

    /* supported methods */
    if (VAL_NULL(vals+11)) {
        ci.methods = ALL_METHODS;
    } else {
        ci.methods = VAL_BITMAP(vals+11);
    }

    /* last modified time */
    if (!VAL_NULL(vals+12)) {
        ci.last_modified = VAL_TIME(vals+12);
    }

    /* record internal uid */
    if (!VAL_NULL(vals+13)) {
        ci.ruid.s = (char*)VAL_STRING(vals+13);
        ci.ruid.len = strlen(ci.ruid.s);
    }

    /* sip instance */
    if (!VAL_NULL(vals+14)) {
        ci.instance.s = (char*)VAL_STRING(vals+14);
        ci.instance.len = strlen(ci.instance.s);
    }

    /* reg-id */
    if (!VAL_NULL(vals+15)) {
        ci.reg_id = VAL_UINT(vals+15);
    }

    return &ci;
}
Пример #16
0
/*!
 * \brief Get all contacts from the database, in partitions if wanted
 * \see get_all_ucontacts
 * \param buf target buffer
 * \param len length of buffer
 * \param flags contact flags
 * \param part_idx part index
 * \param part_max maximal part
 * \return 0 on success, positive if buffer size was not sufficient, negative on failure
 */
static inline int get_all_db_ucontacts(void *buf, int len, unsigned int flags,
								unsigned int part_idx, unsigned int part_max)
{
	static char query_buf[512];
	static str query_str;

	struct socket_info *sock;
	unsigned int dbflags;
	db1_res_t* res = NULL;
	db_row_t *row;
	dlist_t *dom;
	char now_s[25];
	int now_len;
	int port, proto;
	char *p;
	str addr;
	str path;
	str ruid;
	str host;
	unsigned int aorhash;
	int i;
	void *cp;
	int shortage, needed;

	if(ul_dbf.raw_query==NULL) {
		LM_WARN("DB raw query support is required, but not implemented\n");
		return -1;
	}

	cp = buf;
	shortage = 0;
	/* Reserve space for terminating 0000 */
	len -= sizeof(addr.len);

	/* get the current time in DB format */
	now_len = 25;
	if (db_time2str( time(0), now_s, &now_len)!=0) {
		LM_ERR("failed to print now time\n");
		return -1;
	}
	aorhash = 0;

	for (dom = root; dom!=NULL ; dom=dom->next) {
		/* build query */
		i = snprintf( query_buf, sizeof(query_buf), "select %.*s, %.*s, %.*s,"
			" %.*s, %.*s, %.*s from %s where %.*s > %.*s and"
#ifdef ORACLE_USRLOC
			" bitand(%.*s, %d) = %d and mod(id, %u) = %u",
#else
			" %.*s & %d = %d and id %% %u = %u",
#endif
			received_col.len, received_col.s,
			contact_col.len, contact_col.s,
			sock_col.len, sock_col.s,
			cflags_col.len, cflags_col.s,
			path_col.len, path_col.s,
			ruid_col.len, ruid_col.s,
			dom->d->name->s,
			expires_col.len, expires_col.s,
			now_len, now_s,
			cflags_col.len, cflags_col.s,
			flags, flags, part_max, part_idx);
		if ( i>=sizeof(query_buf) ) {
			LM_ERR("DB query too long\n");
			return -1;
		}
		query_str.s = query_buf;
		query_str.len = i;
		if ( ul_dbf.raw_query( ul_dbh, &query_str, &res)<0 ) {
			LM_ERR("raw_query failed\n");
			return -1;
		}
		if( RES_ROW_N(res)==0 ) {
			ul_dbf.free_result(ul_dbh, res);
			continue;
		}

		for(i = 0; i < RES_ROW_N(res); i++) {
			row = RES_ROWS(res) + i;

			/* received */
			addr.s = (char*)VAL_STRING(ROW_VALUES(row));
			if ( VAL_NULL(ROW_VALUES(row)) || addr.s==0 || addr.s[0]==0 ) {
				/* contact */
				addr.s = (char*)VAL_STRING(ROW_VALUES(row)+1);
				if (VAL_NULL(ROW_VALUES(row)+1) || addr.s==0 || addr.s[0]==0) {
					LM_ERR("empty contact -> skipping\n");
					continue;
				}
			}
			addr.len = strlen(addr.s);

			/* path */
			path.s = (char*)VAL_STRING(ROW_VALUES(row)+4);
			if (VAL_NULL(ROW_VALUES(row)+4) || path.s==0 || path.s[0]==0){
				path.s = NULL;
				path.len = 0;
			} else {
				path.len = strlen(path.s);
			}

			/* ruid */
			ruid.s = (char*)VAL_STRING(ROW_VALUES(row)+5);
			if (VAL_NULL(ROW_VALUES(row)+5) || ruid.s==0 || ruid.s[0]==0){
				ruid.s = NULL;
				ruid.len = 0;
			} else {
				ruid.len = strlen(ruid.s);
			}

			needed = (int)(sizeof(addr.len) + addr.len
					+ sizeof(sock) + sizeof(dbflags)
					+ sizeof(path.len) + path.len
					+ sizeof(ruid.len) + ruid.len
					+ sizeof(aorhash));
			if (len < needed) {
				shortage += needed ;
				continue;
			}

			/* write received/contact */
			memcpy(cp, &addr.len, sizeof(addr.len));
			cp = (char*)cp + sizeof(addr.len);
			memcpy(cp, addr.s, addr.len);
			cp = (char*)cp + addr.len;

			/* sock */
			p  = (char*)VAL_STRING(ROW_VALUES(row) + 2);
			if (VAL_NULL(ROW_VALUES(row)+2) || p==0 || p[0]==0){
				sock = 0;
			} else {
				if (parse_phostport( p, &host.s, &host.len,
				&port, &proto)!=0) {
					LM_ERR("bad socket <%s>...set to 0\n", p);
					sock = 0;
				} else {
					sock = grep_sock_info( &host, (unsigned short)port, proto);
					if (sock==0) {
						LM_DBG("non-local socket <%s>...set to 0\n", p);
					}
				}
			}

			/* flags */
			dbflags = VAL_BITMAP(ROW_VALUES(row) + 3);

			/* write sock and flags */
			memcpy(cp, &sock, sizeof(sock));
			cp = (char*)cp + sizeof(sock);
			memcpy(cp, &dbflags, sizeof(dbflags));
			cp = (char*)cp + sizeof(dbflags);

			/* write path */
			memcpy(cp, &path.len, sizeof(path.len));
			cp = (char*)cp + sizeof(path.len);
			/* copy path only if exist */
			if(path.len){
				memcpy(cp, path.s, path.len);
				cp = (char*)cp + path.len;
			}

			/* write ruid */
			memcpy(cp, &ruid.len, sizeof(ruid.len));
			cp = (char*)cp + sizeof(ruid.len);
			/* copy ruid only if exist */
			if(ruid.len){
				memcpy(cp, ruid.s, ruid.len);
				cp = (char*)cp + ruid.len;
			}
			/* aorhash not used for db-only records, but it is added
			 * (as 0) to match the struct used for mem records */
			memcpy(cp, &aorhash, sizeof(aorhash));
			cp = (char*)cp + sizeof(aorhash);

			len -= needed;
		} /* row cycle */

		ul_dbf.free_result(ul_dbh, res);
	} /* domain cycle */

	/* len < 0 is possible, if size of the buffer < sizeof(c->c.len) */
	if (len >= 0)
		memset(cp, 0, sizeof(addr.len));

	/* Shouldn't happen */
	if (shortage > 0 && len > shortage) {
		abort();
	}

	shortage -= len;

	return shortage > 0 ? shortage : 0;
}
Пример #17
0
int preload_udomain(db_con_t* _c, udomain_t* _d)
{
	char b[256];
	db_key_t columns[11];
	db_res_t* res;
	db_row_t* row;
	int i, cseq;
	unsigned int flags;
	struct socket_info* sock;
	str uid, contact, callid, ua, received, instance, aor;
	str* rec;
	time_t expires;
	qvalue_t q;

	urecord_t* r;
	ucontact_t* c;

	columns[0] = uid_col.s;
	columns[1] = contact_col.s;
	columns[2] = expires_col.s;
	columns[3] = q_col.s;
	columns[4] = callid_col.s;
	columns[5] = cseq_col.s;
	columns[6] = flags_col.s;
	columns[7] = user_agent_col.s;
	columns[8] = received_col.s;
	columns[9] = instance_col.s;
	columns[10] = aor_col.s;
	
	memcpy(b, _d->name->s, _d->name->len);
	b[_d->name->len] = '\0';

	if (ul_dbf.use_table(_c, b) < 0) {
		LOG(L_ERR, "preload_udomain(): Error in use_table\n");
		return -1;
	}

	if (ul_dbf.query(_c, 0, 0, 0, columns, 0, 11, 0, &res) < 0) {
		LOG(L_ERR, "preload_udomain(): Error while doing db_query\n");
		return -1;
	}

	if (RES_ROW_N(res) == 0) {
		DBG("preload_udomain(): Table is empty\n");
		ul_dbf.free_result(_c, res);
		return 0;
	}

	lock_udomain(_d);

	for(i = 0; i < RES_ROW_N(res); i++) {
		row = RES_ROWS(res) + i;
		
		uid.s      = (char*)VAL_STRING(ROW_VALUES(row));
		if (uid.s == 0) {
			LOG(L_CRIT, "preload_udomain: ERROR: bad uid "
							"record in table %s\n", b);
			LOG(L_CRIT, "preload_udomain: ERROR: skipping...\n");
			continue;
		} else {
			uid.len = strlen(uid.s);
		}

		contact.s = (char*)VAL_STRING(ROW_VALUES(row) + 1);
		if (contact.s == 0) {
			LOG(L_CRIT, "preload_udomain: ERROR: bad contact "
							"record in table %s\n", b);
			LOG(L_CRIT, "preload_udomain: ERROR: for username %.*s\n",
							uid.len, uid.s);
			LOG(L_CRIT, "preload_udomain: ERROR: skipping...\n");
			continue;
		} else {
			contact.len = strlen(contact.s);
		}
		expires     = VAL_TIME  (ROW_VALUES(row) + 2);
		q           = double2q(VAL_DOUBLE(ROW_VALUES(row) + 3));
		cseq        = VAL_INT   (ROW_VALUES(row) + 5);
		callid.s    = (char*)VAL_STRING(ROW_VALUES(row) + 4);
		if (callid.s == 0) {
			LOG(L_CRIT, "preload_udomain: ERROR: bad callid record in"
							" table %s\n", b);
			LOG(L_CRIT, "preload_udomain: ERROR: for username %.*s,"
							" contact %.*s\n",
							uid.len, uid.s, contact.len, contact.s);
			LOG(L_CRIT, "preload_udomain: ERROR: skipping...\n");
			continue;
		} else {
			callid.len  = strlen(callid.s);
		}

		flags  = VAL_BITMAP(ROW_VALUES(row) + 6);

		ua.s  = (char*)VAL_STRING(ROW_VALUES(row) + 7);
		if (ua.s) {
			ua.len = strlen(ua.s);
		} else {
			ua.len = 0;
		}

		if (!VAL_NULL(ROW_VALUES(row) + 8)) {
			received.s  = (char*)VAL_STRING(ROW_VALUES(row) + 8);
			if (received.s) {
				received.len = strlen(received.s);
				rec = &received;

				sock = find_socket(&received);
			} else {
				received.len = 0;
				rec = 0;
				sock = 0;
			}
		} else {
			received.s = 0;
			received.len = 0;
			rec = 0;
			sock = 0;
		}

		if (!VAL_NULL(ROW_VALUES(row) + 9)) {
			instance.s  = (char*)VAL_STRING(ROW_VALUES(row) + 9);
			if (instance.s) {
				instance.len = strlen(instance.s);
			} else {
				instance.len = 0;
			}
		} else {
			instance.s = 0;
			instance.len = 0;
		}

		if (!VAL_NULL(ROW_VALUES(row) + 10)) {
			aor.s  = (char*)VAL_STRING(ROW_VALUES(row) + 10);
			if (aor.s) {
				aor.len = strlen(aor.s);
			} else {
				aor.len = 0;
			}
		} else {
			aor.s = 0;
			aor.len = 0;
		}

		if (get_urecord(_d, &uid, &r) > 0) {
			if (mem_insert_urecord(_d, &uid, &r) < 0) {
				LOG(L_ERR, "preload_udomain(): Can't create a record\n");
				ul_dbf.free_result(_c, res);
				unlock_udomain(_d);
				return -2;
			}
		}
		
		if (mem_insert_ucontact(r, &aor, &contact, expires, q, &callid, cseq, flags, &c, &ua, rec, sock, &instance) < 0) {
			LOG(L_ERR, "preload_udomain(): Error while inserting contact\n");
			ul_dbf.free_result(_c, res);
			unlock_udomain(_d);
			return -3;
		}

		db_read_reg_avps(_c, c);

		     /* We have to do this, because insert_ucontact sets state to CS_NEW
		      * and we have the contact in the database already
			  * we also store zombies in database so we have to restore
			  * the correct state
		      */
		c->state = CS_SYNC;
	}

	ul_dbf.free_result(_c, res);
	unlock_udomain(_d);
	return 0;
}
Пример #18
0
/*
 * Used when converting values to be used in a DB query
 */
int db_sqlite_val2str(const db_con_t* _c, const db_val_t* _v, char* _s, int* _len)
{
	int l;

	if (!_c || !_v || !_s || !_len || !*_len) {
		LM_ERR("invalid parameter value\n");
		return -1;
	}

	if (VAL_NULL(_v)) {
		if (*_len < sizeof("NULL")) {
			LM_ERR("buffer too small\n");
			return -1;
		}
		*_len = snprintf(_s, *_len, "NULL");
		return 0;
	}

	switch(VAL_TYPE(_v)) {
	case DB_INT:
		if (db_int2str(VAL_INT(_v), _s, _len) < 0) {
			LM_ERR("error while converting string to int\n");
			return -2;
		} else {
			return 0;
		}
		break;

	case DB_BIGINT:
		if (db_bigint2str(VAL_BIGINT(_v), _s, _len) < 0) {
			LM_ERR("error while converting bigint to string\n");
			return -2;
		} else {
			return 0;
		}
		break;


	case DB_BITMAP:
		if (db_int2str(VAL_BITMAP(_v), _s, _len) < 0) {
			LM_ERR("error while converting string to int\n");
			return -3;
		} else {
			return 0;
		}
		break;

	case DB_DOUBLE:
		if (db_double2str(VAL_DOUBLE(_v), _s, _len) < 0) {
			LM_ERR("error while converting string to double\n");
			return -4;
		} else {
			return 0;
		}
		break;

	case DB_STRING:
		/* TODO check escpaing */
		l = strlen(VAL_STRING(_v));
		if (*_len < l )
		{	LM_ERR("Destination buffer too short for string\n");
			return -4;
		}
		else
		{	LM_DBG("Converted string to string\n");
			strncpy(_s, VAL_STRING(_v) , l);
			_s[l] = 0;
			*_len = l+1; /* count the 0 also */
			return 0;
		}
		break;

	case DB_STR:
		/* TODO check escpaing */
		l = VAL_STR(_v).len;
		if (*_len < l)
		{
			LM_ERR("Destination buffer too short for str\n");
			return -5;
		}
		else
		{
			LM_DBG("Converted str to string\n");
			strncpy(_s, VAL_STR(_v).s , l);
			*_len = l;
			return 0;
		}
		break;


		break;

	case DB_DATETIME:
		if (db_time2str(VAL_TIME(_v), _s, _len) < 0) {
			LM_ERR("error while converting string to time_t\n");
			return -7;
		} else {
			return 0;
		}
		break;

	case DB_BLOB:
		/* TODO check escpaing */

		l = VAL_BLOB(_v).len;
		if (*_len < l)
		{
			LM_ERR("Destination buffer too short for blob\n");
			return -7;
		}
		else
		{
			strncpy(_s, VAL_BLOB(_v).s , l);
			LM_DBG("Converting BLOB [%.*s]\n", l,_s);
			*_len = l;
			return 0;
		}
		break;

	default:
		LM_DBG("unknown data type\n");
		return -9;
	}
}
Пример #19
0
int db_mysql_val2bind(const db_val_t* v, MYSQL_BIND *binds, unsigned int i)
{
	struct tm *t;
	MYSQL_TIME *mt;

	if (VAL_NULL(v)) {
		*(binds[i].is_null) = 1;
		*(binds[i].length) = 0;
		binds[i].buffer= NULL;
		switch(VAL_TYPE(v)) {
			case DB_INT:
				binds[i].buffer_type= MYSQL_TYPE_LONG; break;
			case DB_BIGINT:
				binds[i].buffer_type= MYSQL_TYPE_LONGLONG; break;
			case DB_BITMAP:
				binds[i].buffer_type= MYSQL_TYPE_LONG; break;
			case DB_DOUBLE:
				binds[i].buffer_type= MYSQL_TYPE_DOUBLE; break;
			case DB_STRING:
				binds[i].buffer_type= MYSQL_TYPE_STRING; break;
			case DB_STR:
				binds[i].buffer_type= MYSQL_TYPE_STRING; break;
			case DB_DATETIME:
				binds[i].buffer_type= MYSQL_TYPE_DATETIME; break;
			case DB_BLOB:
				binds[i].buffer_type= MYSQL_TYPE_BLOB; break;
			default:
				LM_ERR("unknown NULL data type (%d)\n",VAL_TYPE(v));
				return -10;
		}
		return 0;
	} else {
		*(binds[i].is_null) = 0;
	}

	switch(VAL_TYPE(v)) {
		case DB_INT:
			binds[i].buffer_type= MYSQL_TYPE_LONG;
			binds[i].buffer= (char*)&(VAL_INT(v));
			*binds[i].length= sizeof(VAL_INT(v));
			break;

		case DB_BIGINT:
			binds[i].buffer_type= MYSQL_TYPE_LONGLONG;
			binds[i].buffer= (char*)&(VAL_BIGINT(v));
			*binds[i].length= sizeof(VAL_BIGINT(v));
			break;


		case DB_BITMAP:
			binds[i].buffer_type= MYSQL_TYPE_LONG;
			binds[i].buffer= (char*)&(VAL_BITMAP(v));
			*binds[i].length= sizeof(VAL_BITMAP(v));
			break;

		case DB_DOUBLE:
			binds[i].buffer_type= MYSQL_TYPE_DOUBLE;
			binds[i].buffer= (char*)&(VAL_DOUBLE(v));
			*binds[i].length= sizeof(VAL_DOUBLE(v));
			break;

		case DB_STRING:
			binds[i].buffer_type= MYSQL_TYPE_STRING;
			binds[i].buffer= (char*)VAL_STRING(v);
			*binds[i].length= strlen(VAL_STRING(v));
			break;

		case DB_STR:
			binds[i].buffer_type= MYSQL_TYPE_STRING;
			binds[i].buffer= VAL_STR(v).s;
			*binds[i].length= VAL_STR(v).len;
			break;

		case DB_DATETIME:
			binds[i].buffer_type= MYSQL_TYPE_DATETIME;
			t = localtime( &VAL_TIME(v) );
			mt = (MYSQL_TIME*)binds[i].buffer;
			mt->year = 1900 + t->tm_year;
			mt->month = (t->tm_mon)+1;
			mt->day = t->tm_mday;
			mt->hour = t->tm_hour;
			mt->minute = t->tm_min;
			mt->second = t->tm_sec;
			*binds[i].length= sizeof(MYSQL_TIME);
			break;

		case DB_BLOB:
			binds[i].buffer_type= MYSQL_TYPE_BLOB;
			binds[i].buffer= VAL_BLOB(v).s;
			*binds[i].length= VAL_BLOB(v).len;
			break;

		default:
			LM_ERR("unknown data type (%d)\n",VAL_TYPE(v));
			return -9;
	}

	LM_DBG("added val (%d): len=%ld; type=%d; is_null=%d\n", i,
		*(binds[i].length), binds[i].buffer_type, *(binds[i].is_null));

	return 0;
}
Пример #20
0
/*
 * Insert a row into specified table
 * h: structure representing database connection
 * k: key names
 * v: values of the keys
 * n: number of key=value pairs
 */
int flat_db_insert(db_con_t* h, db_key_t* k, db_val_t* v, int n)
{
	FILE* f;
	int i;
	char delims[4], *s;
	size_t len;

	f = CON_FILE(h);
	if (!f) {
		LOG(L_CRIT, "BUG: flat_db_insert: Uninitialized connection\n");
		return -1;
	}

	if (local_timestamp < *flat_rotate) {
		flat_rotate_logs();
		local_timestamp = *flat_rotate;
	}

	for(i = 0; i < n; i++) {

		if (!VAL_NULL(v + i)) {   // TODO: how to distinguish NULL from empty
			switch(VAL_TYPE(v + i)) {
				case DB_INT:
					fprintf(f, "%d", VAL_INT(v + i));
					break;

				case DB_FLOAT:
					fprintf(f, "%f", VAL_FLOAT(v + i));
					break;

				case DB_DOUBLE:
					fprintf(f, "%f", VAL_DOUBLE(v + i));
					break;

				case DB_STRING: {
					s = (char*) VAL_STRING(v + i);
					delims[0] = flat_delimiter[0];
					delims[1] = flat_record_delimiter[0];
					delims[2] = flat_escape[0];
					delims[3] = '\0';
					while (*s) {
						len = strcspn(s, delims);
						fprintf(f, "%.*s", (int)len, s);
						s+= len;
						if (*s) {
							fprintf(f, "%c%c", flat_escape[0], *s);
							s++;
						}
					}
					break;
				}
				case DB_STR:
				case DB_BLOB:
					if (VAL_TYPE(v + i) == DB_STR) {
						s = VAL_STR(v + i).s;
						len = VAL_STR(v + i).len;
					}
					else {
						s = VAL_BLOB(v + i).s;
						len = VAL_BLOB(v + i).len;
					}
					while (len > 0) {
						char *c;
						for (c = s; len > 0 && *c != flat_delimiter[0] && *c != flat_record_delimiter[0] && *c != flat_escape[0]; c++, len--);
						fprintf(f, "%.*s", (int)(c-s), s);
						s = c;
						if (len > 0) {
							fprintf(f, "%c%c", flat_escape[0], *s);
							s++;
							len--;
						}
					}
					break;

				case DB_DATETIME:
					fprintf(f, "%u", (unsigned int)VAL_TIME(v + i));
					break;

				case DB_BITMAP:
					fprintf(f, "%u", VAL_BITMAP(v + i));
					break;
			}
		}
		if (i < (n - 1)) {
			fprintf(f, "%c", flat_delimiter[0]);
		}
	}

	fprintf(f, "%c", flat_record_delimiter[0]);

	if (flat_flush) {
		fflush(f);
	}

	return 0;
}
Пример #21
0
/*
 * Called after val2str to realy binding
 */
int db_oracle_val2bind(bmap_t* _m, const db_val_t* _v, OCIDate* _o)
{
	if (VAL_NULL(_v)) {
		_m->addr = NULL;
		_m->size = 0;
		_m->type = SQLT_NON;
		return 0;
	}

	switch (VAL_TYPE(_v)) {
	case DB1_INT:
		_m->addr = (int*)&VAL_INT(_v);
		_m->size = sizeof(VAL_INT(_v));
		_m->type = SQLT_INT;
		break;

	case DB1_BIGINT:
		LM_ERR("BIGINT not supported");
		return -1;

	case DB1_BITMAP:
		_m->addr = (unsigned*)&VAL_BITMAP(_v);
		_m->size = sizeof(VAL_BITMAP(_v));
		_m->type = SQLT_UIN;
		break;

	case DB1_DOUBLE:
		_m->addr = (double*)&VAL_DOUBLE(_v);
		_m->size = sizeof(VAL_DOUBLE(_v));
		_m->type = SQLT_FLT;
		break;

	case DB1_STRING:
		_m->addr = (char*)VAL_STRING(_v);
		_m->size = strlen(VAL_STRING(_v))+1;
		_m->type = SQLT_STR;
		break;

	case DB1_STR:
		{
			unsigned len = VAL_STR(_v).len;
			char *estr, *pstr = VAL_STR(_v).s;

			estr = (char*)memchr(pstr, 0, len);
			if (estr) {
				LM_WARN("truncate STR len from %u to: '%s'\n",
					len, pstr);
				len = (unsigned)(estr - pstr) + 1;
			}
			_m->size = len;
			_m->addr = pstr;
			_m->type = SQLT_CHR;
		}
		break;

	case DB1_DATETIME:
		{
			struct tm* tm = localtime(&VAL_TIME(_v));
			if (tm->tm_sec == 60)
				--tm->tm_sec;
			OCIDateSetDate(_o, (ub2)(tm->tm_year + 1900),
				(ub1)(tm->tm_mon + 1), (ub1)tm->tm_mday);
			OCIDateSetTime(_o, (ub1)tm->tm_hour, (ub1)tm->tm_min,
				(ub1)tm->tm_sec);
			_m->addr = _o;
			_m->size = sizeof(*_o);
			_m->type = SQLT_ODT;
		}
		break;

	case DB1_BLOB:
		_m->addr = VAL_BLOB(_v).s;
		_m->size = VAL_BLOB(_v).len;
		_m->type = SQLT_CLOB;
		break;

	default:
	    LM_ERR("unknown data type\n");
	    return -1;
	}
	return 0;
}
Пример #22
0
/*
 * Insert a row into specified table
 * h: structure representing database connection
 * k: key names
 * v: values of the keys
 * n: number of key=value pairs
 */
int flat_db_insert(const db1_con_t* h, const db_key_t* k, const db_val_t* v,
		const int n)
{
	FILE* f;
	int i;
	int l;
	char *s, *p;

	if (km_local_timestamp < *km_flat_rotate) {
		flat_rotate_logs();
		km_local_timestamp = *km_flat_rotate;
	}

	f = CON_FILE(h);
	if (!f) {
		LM_ERR("uninitialized connection\n");
		return -1;
	}

	for(i = 0; i < n; i++) {
		switch(VAL_TYPE(v + i)) {
		case DB1_INT:
			fprintf(f, "%d", VAL_INT(v + i));
			break;

		case DB1_BIGINT:
			LM_ERR("BIGINT not supported");
			return -1;

		case DB1_DOUBLE:
			fprintf(f, "%f", VAL_DOUBLE(v + i));
			break;

		case DB1_STRING:
			fprintf(f, "%s", VAL_STRING(v + i));
			break;

		case DB1_STR:
			fprintf(f, "%.*s", VAL_STR(v + i).len, VAL_STR(v + i).s);
			break;

		case DB1_DATETIME:
			fprintf(f, "%u", (unsigned int)VAL_TIME(v + i));
			break;

		case DB1_BLOB:
			l = VAL_BLOB(v+i).len;
			s = p = VAL_BLOB(v+i).s;
			while (l--) {
				if ( !(isprint((int)*s) && *s != '\\' && *s != '|')) {
					fprintf(f,"%.*s\\x%02X",(int)(s-p),p,(*s & 0xff));
					p = s+1;
				}
				++s;
			}
			if (p!=s)
				fprintf(f,"%.*s",(int)(s-p),p);
			break;

		case DB1_BITMAP:
			fprintf(f, "%u", VAL_BITMAP(v + i));
			break;
		}

		if (i < (n - 1)) {
			fprintf(f, "%c", *km_flat_delimiter);
		}
	}

	fprintf(f, "\n");

	if (flat_flush) {
		fflush(f);
	}

	return 0;
}
Пример #23
0
/*
 * Used when converting result from a query
 */
int db_postgres_val2str(const db_con_t* _con, const db_val_t* _v,
														char* _s, int* _len)
{
	int l, ret;
	int pgret;
	char *tmp_s;
	size_t tmp_len;
	char* old_s;

	if ((!_v) || (!_s) || (!_len) || (!*_len)) {
		LM_ERR("invalid parameter value\n");
		return -1;
	}

	if (VAL_NULL(_v)) {
		if ( *_len < (l=(int)sizeof("NULL")-1)) {
			LM_ERR("buffer too short to print NULL\n");
			return -1;
		}
		memcpy(_s, "NULL", l);
		*_len = l;
		return 0;
	}

	switch(VAL_TYPE(_v)) {
	case DB_INT:
		if (db_int2str(VAL_INT(_v), _s, _len) < 0) {
			LM_ERR("failed to convert string to int\n");
			return -2;
		} else {
			return 0;
		}
		break;

	case DB_BIGINT:
		if (db_bigint2str(VAL_BIGINT(_v), _s, _len) < 0) {
			LM_ERR("failed to convert string to big int\n");
			return -2;
		} else {
			return 0;
		}
		break;

	case DB_BITMAP:
		if (db_int2str(VAL_BITMAP(_v), _s, _len) < 0) {
			LM_ERR("failed to convert string to int\n");
			return -3;
		} else {
			return 0;
		}
		break;

	case DB_DOUBLE:
		if (db_double2str(VAL_DOUBLE(_v), _s, _len) < 0) {
			LM_ERR("failed to convert string to double\n");
			return -3;
		} else {
			return 0;
		}
		break;

	case DB_STRING:
		l = strlen(VAL_STRING(_v));
		if (*_len < (l * 2 + 3)) {
			LM_ERR("destination STRING buffer too short (have %d, need %d)\n",
			       *_len, l * 2 + 3);
			return -4;
		} else {
			old_s = _s;
			*_s++ = '\'';
			ret = PQescapeStringConn(CON_CONNECTION(_con), _s, VAL_STRING(_v),
					l, &pgret);
			if(pgret!=0)
			{
				LM_ERR("PQescapeStringConn failed\n");
				return -4;
			}
			LM_DBG("PQescapeStringConn: in: %d chars,"
				" out: %d chars\n", l, ret);
			_s += ret;
			*_s++ = '\'';
			*_s = '\0'; /* FIXME */
			*_len = _s - old_s;
			return 0;
		}
		break;

	case DB_STR:
		l = VAL_STR(_v).len;
		if (*_len < (l * 2 + 3)) {
			LM_ERR("destination STR buffer too short (have %d, need %d)\n",
			       *_len, l * 2 + 3);
			return -5;
		} else {
			old_s = _s;
			*_s++ = '\'';
			ret = PQescapeStringConn(CON_CONNECTION(_con), _s, VAL_STRING(_v),
					l, &pgret);
			if(pgret!=0)
			{
				LM_ERR("PQescapeStringConn failed \n");
				return -5;
			}
	        LM_DBG("PQescapeStringConn: in: %d chars, out: %d chars\n", l, ret);
			_s += ret;
			*_s++ = '\'';
			*_s = '\0'; /* FIXME */
			*_len = _s - old_s;
			return 0;
		}
		break;

	case DB_DATETIME:
		if (db_time2str(VAL_TIME(_v), _s, _len) < 0) {
			LM_ERR("failed to convert string to time_t\n");
			return -6;
		} else {
			return 0;
		}
		break;

	case DB_BLOB:
		l = VAL_BLOB(_v).len;
		/* this estimation is not always correct, thus we need to check later again */
		if (*_len < (l * 2 + 3)) {
			LM_ERR("destination BLOB buffer too short (have %d, need %d)\n",
			       *_len, l * 2 + 3);
			return -7;
		} else {
			*_s++ = '\'';
			tmp_s = (char*)PQescapeByteaConn(CON_CONNECTION(_con), (unsigned char*)VAL_STRING(_v),
					(size_t)l, (size_t*)&tmp_len);
			if(tmp_s==NULL)
			{
				LM_ERR("PQescapeBytea failed\n");
				return -7;
			}
			if (tmp_len > *_len) {
				LM_ERR("escaped result too long\n");
				return -7;
			}
			memcpy(_s, tmp_s, tmp_len);
			PQfreemem(tmp_s);
			tmp_len = strlen(_s);
			*(_s + tmp_len) = '\'';
			*(_s + tmp_len + 1) = '\0';
			*_len = tmp_len + 2;
			return 0;
		}
		break;

	default:
		LM_DBG("unknown data type\n");
		return -7;
	}
}
int srdb1_encode_kv(int tupsize,const db_key_t* _k, const db_op_t* _op, const db_val_t* _v,
				const int _n, ei_x_buff *argbuf) {
	int i;
	struct tm* tt;
	time_t t_t;

	if(_k) {
	    ei_x_encode_list_header(argbuf, _n);
	    for(i = 0; i < _n; i++) {
		db_val_t *vv;
		ei_x_encode_tuple_header(argbuf, tupsize);
		ei_x_encode_atom_len(argbuf,_k[i]->s,_k[i]->len);
		if(tupsize == 3 ) {
		    if (_op) {
			ei_x_encode_atom(argbuf,_op[i]);
		    } else {
			ei_x_encode_atom(argbuf,"=");
		    }
		}
		vv=&(_v[i]);
		if (VAL_NULL(vv)) {
		    ei_x_encode_atom(argbuf,"undefined");
		} else {
		    switch(VAL_TYPE(vv)) {
			case DB1_INT:
			    ei_x_encode_ulong(argbuf, VAL_INT(vv));
			    break;
			case DB1_BIGINT:
			    ei_x_encode_longlong(argbuf, VAL_BIGINT(vv));
			    break;
			case DB1_DOUBLE:
			    ei_x_encode_double(argbuf, VAL_DOUBLE(vv));
			    break;
			case DB1_STRING:
			    ei_x_encode_string(argbuf,VAL_STRING(vv));
			    break;
			case DB1_STR:
			    ei_x_encode_string_len(argbuf,VAL_STR(vv).s,VAL_STR(vv).len);
			    break;
			case DB1_DATETIME:
			    t_t=VAL_TIME(vv);
			    tt= localtime(&t_t);
			    ei_x_encode_tuple_header(argbuf, 2);
			    ei_x_encode_tuple_header(argbuf, 3);
			    ei_x_encode_long(argbuf, tt->tm_year + 1900);
			    ei_x_encode_long(argbuf, tt->tm_mon +1);
			    ei_x_encode_long(argbuf, tt->tm_mday);
			    ei_x_encode_tuple_header(argbuf, 3);
			    ei_x_encode_long(argbuf, tt->tm_hour);
			    ei_x_encode_long(argbuf, tt->tm_min);
			    ei_x_encode_long(argbuf, tt->tm_sec);
			    break;
			case DB1_BLOB:
			    ei_x_encode_binary(argbuf,VAL_BLOB(vv).s,VAL_BLOB(vv).len);
			    break;
			case DB1_BITMAP:
			    ei_x_encode_ulong(argbuf,VAL_BITMAP(vv));
			    break;
		    }
		}
	    }
	    ei_x_encode_empty_list(argbuf);
	} else {
	    ei_x_encode_list_header(argbuf, 0);
	}
	return 0;
}