Example #1
0
	bool OpenDB()
	{
		CloseDB();

		TDSCONNECTION* conn = NULL;

		login = tds_alloc_login();
		tds_set_app(login, "TSQL");
		tds_set_library(login,"TDS-Library");
		tds_set_host(login, "");
		tds_set_server(login, host.host.c_str());
		tds_set_server_addr(login, host.host.c_str());
		tds_set_user(login, host.user.c_str());
		tds_set_passwd(login, host.pass.c_str());
		tds_set_port(login, host.port);
		tds_set_packet(login, 512);

		context = tds_alloc_context(this);
		context->msg_handler = HandleMessage;
		context->err_handler = HandleError;

		sock = tds_alloc_socket(context, 512);
		tds_set_parent(sock, NULL);

		conn = tds_read_config_info(NULL, login, context->locale);

		if (tds_connect(sock, conn) == TDS_SUCCEED)
		{
			tds_free_connection(conn);
			return 1;
		}
		tds_free_connection(conn);
		return 0;
	}
Example #2
0
/*
 * pool_mbr_login open a single pool login, to be call at init time or
 * to reconnect.
 */
static TDSSOCKET *
pool_mbr_login(TDS_POOL * pool)
{
	TDSCONTEXT *context;
	TDSLOGIN *login;
	TDSSOCKET *tds;
	TDSCONNECTION *connection;
	int rc;
	char *query;
	char hostname[MAXHOSTNAMELEN];

	login = tds_alloc_login();
	tds_set_passwd(login, pool->password);
	tds_set_user(login, pool->user);
	tds_set_app(login, "tdspool");
#if HAVE_GETHOSTNAME
	if (gethostname(hostname, MAXHOSTNAMELEN) < 0)
#endif
		tds_strlcpy(hostname, "tdspool", MAXHOSTNAMELEN);
	tds_set_host(login, hostname);
	tds_set_library(login, "TDS-Library");
	tds_set_server(login, pool->server);
	tds_set_client_charset(login, "iso_1");
	tds_set_language(login, "us_english");
	tds_set_packet(login, 512);
	context = tds_alloc_context(NULL);
	tds = tds_alloc_socket(context, 512);
	connection = tds_read_config_info(tds, login, context->locale);
	if (!connection || tds_connect_and_login(tds, connection) != TDS_SUCCEED) {
		tds_free_socket(tds);
		tds_free_connection(connection);
		/* what to do? */
		fprintf(stderr, "Could not open connection to server %s\n", pool->server);
		return NULL;
	}
	tds_free_connection(connection);
	/*
	 * FIXME -- tds_connect_and_login no longer preallocates the in_buf need to 
	 * do something like what tds_read_packet does
	 */
	tds->in_buf = (unsigned char *) calloc(BLOCKSIZ, 1);

	if (pool->database && strlen(pool->database)) {
		query = (char *) malloc(strlen(pool->database) + 5);
		sprintf(query, "use %s", pool->database);
		rc = tds_submit_query(tds, query);
		free(query);
		if (rc != TDS_SUCCEED) {
			fprintf(stderr, "changing database failed\n");
			return NULL;
		}

		if (tds_process_simple_query(tds) != TDS_SUCCEED)
			return NULL;
	}


	return tds;
}
Example #3
0
int
try_tds_login(TDSLOGIN ** login, TDSSOCKET ** tds, const char *appname, int verbose)
{
	TDSCONNECTINFO *connect_info;

	if (verbose) {
		fprintf(stdout, "Entered tds_try_login()\n");
	}
	if (!login) {
		fprintf(stderr, "Invalid TDSLOGIN**\n");
		return TDS_FAIL;
	}
	if (!tds) {
		fprintf(stderr, "Invalid TDSSOCKET**\n");
		return TDS_FAIL;
	}

	if (verbose) {
		fprintf(stdout, "Trying read_login_info()\n");
	}
	read_login_info();

	if (verbose) {
		fprintf(stdout, "Setting login parameters\n");
	}
	*login = tds_alloc_login();
	if (!*login) {
		fprintf(stderr, "tds_alloc_login() failed.\n");
		return TDS_FAIL;
	}
	tds_set_passwd(*login, PASSWORD);
	tds_set_user(*login, USER);
	tds_set_app(*login, appname);
	tds_set_host(*login, "myhost");
	tds_set_library(*login, "TDS-Library");
	tds_set_server(*login, SERVER);
	tds_set_client_charset(*login, CHARSET);
	tds_set_language(*login, "us_english");

	if (verbose) {
		fprintf(stdout, "Connecting to database\n");
	}
	test_context = tds_alloc_context();
	*tds = tds_alloc_socket(test_context, 512);
	tds_set_parent(*tds, NULL);
	connect_info = tds_read_config_info(NULL, *login, test_context->locale);
	if (!connect_info || tds_connect(*tds, connect_info) == TDS_FAIL) {
		if (connect_info) {
			*tds = NULL;
			tds_free_connect(connect_info);
		}
		fprintf(stderr, "tds_connect() failed\n");
		return TDS_FAIL;
	}
	tds_free_connect(connect_info);

	return TDS_SUCCEED;
}
Example #4
0
/*
 * pool_mbr_login open a single pool login, to be call at init time or
 * to reconnect.
 */
static TDSSOCKET *
pool_mbr_login(TDS_POOL * pool)
{
	TDSCONTEXT *context;
	TDSLOGIN *login;
	TDSSOCKET *tds;
	TDSLOGIN *connection;
	char hostname[MAXHOSTNAMELEN];

	login = tds_alloc_login(1);
	if (gethostname(hostname, MAXHOSTNAMELEN) < 0)
		strlcpy(hostname, "tdspool", MAXHOSTNAMELEN);
	if (!tds_set_passwd(login, pool->password)
	    || !tds_set_user(login, pool->user)
	    || !tds_set_app(login, "tdspool")
	    || !tds_set_host(login, hostname)
	    || !tds_set_library(login, "TDS-Library")
	    || !tds_set_server(login, pool->server)
	    || !tds_set_client_charset(login, "iso_1")
	    || !tds_set_language(login, "us_english")) {
		tds_free_login(login);
		return NULL;
	}
	if (pool->database && strlen(pool->database)) {
		if (!tds_dstr_copy(&login->database, pool->database)) {
			tds_free_login(login);
			return NULL;
		}
	}
	context = tds_alloc_context(NULL);
	tds = tds_alloc_socket(context, 512);
	connection = tds_read_config_info(tds, login, context->locale);
	if (!connection || TDS_FAILED(tds_connect_and_login(tds, connection))) {
		tds_free_socket(tds);
		tds_free_login(connection);
		/* what to do? */
		fprintf(stderr, "Could not open connection to server %s\n", pool->server);
		return NULL;
	}
	tds_free_login(connection);

	if (pool->database && strlen(pool->database)) {
		if (strcasecmp(tds->conn->env.database, pool->database) != 0) {
			fprintf(stderr, "changing database failed\n");
			return NULL;
		}
	}

	return tds;
}
Example #5
0
int
try_tds_login(TDSLOGIN ** login, TDSSOCKET ** tds, const char *appname, int verbose)
{
	TDSLOGIN *connection;
    const char* base_appname;

    base_appname = strrchr(appname, '/');
    if (base_appname == NULL) {
        base_appname = appname;
    } else {
        ++base_appname;
    }

	if (verbose) {
		fprintf(stdout, "Entered tds_try_login()\n");
	}
	if (!login) {
		fprintf(stderr, "Invalid TDSLOGIN**\n");
		return TDS_FAIL;
	}
	if (!tds) {
		fprintf(stderr, "Invalid TDSSOCKET**\n");
		return TDS_FAIL;
	}

	if (verbose) {
		fprintf(stdout, "Trying read_login_info()\n");
	}
	read_login_info();

	if (verbose) {
		fprintf(stdout, "Setting login parameters\n");
	}
	*login = tds_alloc_login(1);
	if (!*login) {
		fprintf(stderr, "tds_alloc_login() failed.\n");
		return TDS_FAIL;
	}
	tds_set_passwd(*login, PASSWORD);
	tds_set_user(*login, USER);
    tds_set_app(*login, base_appname);
	tds_set_host(*login, "myhost");
	tds_set_library(*login, "TDS-Library");
	tds_set_server(*login, SERVER);
	tds_set_client_charset(*login, CHARSET);
	tds_set_language(*login, "us_english");

	if (verbose) {
		fprintf(stdout, "Connecting to database\n");
	}
	test_context = tds_alloc_context(NULL);
	*tds = tds_alloc_socket(test_context, 512);
	tds_set_parent(*tds, NULL);
	connection = tds_read_config_info(*tds, *login, test_context->locale);
	if (!connection || tds_connect_and_login(*tds, connection) != TDS_SUCCESS) {
		if (connection) {
			tds_free_socket(*tds);
			*tds = NULL;
			tds_free_login(connection);
		}
		fprintf(stderr, "tds_connect_and_login() failed\n");
		return TDS_FAIL;
	}
	tds_free_login(connection);

	return TDS_SUCCESS;
}
Example #6
0
static int mssql_connect(void)
{
#if (defined(FREETDS_0_63) || defined(FREETDS_0_64))
	TDSCONNECTION *connection = NULL;
#else
	TDSCONNECTINFO *connection = NULL;
#endif
	char query[128];

	/* Connect to M$SQL Server */
	if (!(login = tds_alloc_login()))
	{
		ast_log(LOG_ERROR, "tds_alloc_login() failed.\n");
		return -1;
	}
	
	tds_set_server(login, hostname);
	tds_set_user(login, dbuser);
	tds_set_passwd(login, password);
	tds_set_app(login, "TSQL");
	tds_set_library(login, "TDS-Library");
#ifndef FREETDS_PRE_0_62
	tds_set_client_charset(login, charset);
#endif
	tds_set_language(login, language);
	tds_set_packet(login, 512);
	tds_set_version(login, 7, 0);

#ifdef FREETDS_0_64
	if (!(context = tds_alloc_context(NULL)))
#else
	if (!(context = tds_alloc_context()))
#endif
	{
		ast_log(LOG_ERROR, "tds_alloc_context() failed.\n");
		goto connect_fail;
	}

	if (!(tds = tds_alloc_socket(context, 512))) {
		ast_log(LOG_ERROR, "tds_alloc_socket() failed.\n");
		goto connect_fail;
	}

	tds_set_parent(tds, NULL);
	connection = tds_read_config_info(tds, login, context->locale);
	if (!connection)
	{
		ast_log(LOG_ERROR, "tds_read_config() failed.\n");
		goto connect_fail;
	}

	if (tds_connect(tds, connection) == TDS_FAIL)
	{
		ast_log(LOG_ERROR, "Failed to connect to MSSQL server.\n");
		tds = NULL;	/* freed by tds_connect() on error */
#if (defined(FREETDS_0_63) || defined(FREETDS_0_64))
		tds_free_connection(connection);
#else
		tds_free_connect(connection);
#endif
		connection = NULL;
		goto connect_fail;
	}
#if (defined(FREETDS_0_63) || defined(FREETDS_0_64))
	tds_free_connection(connection);
#else
	tds_free_connect(connection);
#endif
	connection = NULL;

	sprintf(query, "USE %s", dbname);
#ifdef FREETDS_PRE_0_62
	if ((tds_submit_query(tds, query) != TDS_SUCCEED) || (tds_process_simple_query(tds, &result_type) != TDS_SUCCEED || result_type != TDS_CMD_SUCCEED))
#else
	if ((tds_submit_query(tds, query) != TDS_SUCCEED) || (tds_process_simple_query(tds) != TDS_SUCCEED))
#endif
	{
		ast_log(LOG_ERROR, "Could not change database (%s)\n", dbname);
		goto connect_fail;
	}

	connected = 1;
	return 0;

connect_fail:
	mssql_disconnect();
	return -1;
}
Example #7
0
// Connect to the server
bool TdsDatabaseLayer::Connect()
{
	m_pLogin = tds_alloc_login();
	if (m_pLogin == NULL)
  {
		//fprintf(stderr, "tds_alloc_login() failed.\n");
    SetError(DATABASE_LAYER_ERROR, wxT("Failed to allocate login"));
    // Reset the variables so there are not mistaken as initialized
    m_pDatabase = NULL;
    m_pLogin = NULL;
    if (m_pContext)
    {
      tds_free_context(m_pContext);
      m_pContext = NULL;
    }
    ThrowDatabaseException();
		return false;
	}
  wxCharBuffer serverBuffer = ConvertToUnicodeStream(m_strServer);
  tds_set_server(m_pLogin, serverBuffer);
  wxCharBuffer loginBuffer = ConvertToUnicodeStream(m_strLogin);
	tds_set_user(m_pLogin, loginBuffer);
  wxCharBuffer passwordBuffer = ConvertToUnicodeStream(m_strPassword);
	tds_set_passwd(m_pLogin, passwordBuffer);
	tds_set_app(m_pLogin, "DatabaseLayer");
	tds_set_library(m_pLogin, "TDS-Library");
	tds_set_client_charset(m_pLogin, "UTF-8");
  switch (m_nTdsVersion)
  {
    case TDS_42:
      tds_set_version(m_pLogin, 4, 2);
      break;
    case TDS_46:
      tds_set_version(m_pLogin, 4, 6);
      break;
    case TDS_50:
      tds_set_version(m_pLogin, 5, 0);
      break;
    case TDS_70:
      tds_set_version(m_pLogin, 7, 0);
      break;
    case TDS_80:
      tds_set_version(m_pLogin, 8, 0);
      break;
    default:
      tds_set_version(m_pLogin, 0, 0);
      break;
  };
  
	m_pContext = tds_alloc_context(NULL);
  if (m_pContext == NULL)
  {
    //fprintf(stderr, "tds_alloc_context() failed.\n");
    SetError(DATABASE_LAYER_ERROR, wxT("Failed to allocate context"));
    // Reset the variables so there are not mistaken as initialized
    m_pDatabase = NULL;
    if (m_pLogin)
    {
      tds_free_login(m_pLogin);
      m_pLogin = NULL;
    }
    if (m_pContext)
    {
      tds_free_context(m_pContext);
      m_pContext = NULL;
    }
    ThrowDatabaseException();
    return false;
  }
	m_pContext->msg_handler = tsql_handle_message;
	m_pContext->err_handler = tsql_handle_error;

  // Add the context (and this databaselayer) from the lookup map
  //  used by the error handler
  TdsDatabaseLayer::AddTdsLayer(m_pContext, this);

  m_pDatabase = tds_alloc_socket(m_pContext, 512);
  if (m_pDatabase == NULL)
  {
    //fprintf(stderr, "tds_alloc_socket() failed.\n");
    SetError(DATABASE_LAYER_ERROR, wxT("Failed to allocate socket"));
    // Reset the variables so there are not mistaken as initialized
    m_pDatabase = NULL;
    if (m_pLogin)
    {
      tds_free_login(m_pLogin);
      m_pLogin = NULL;
    }
    if (m_pContext)
    {
      tds_free_context(m_pContext);
      m_pContext = NULL;
    }
    ThrowDatabaseException();
    return false;
  }
	tds_set_parent(m_pDatabase, NULL);

	TDSCONNECTION* pConnection = tds_read_config_info(NULL, m_pLogin, m_pContext->locale);
	if (!pConnection || tds_connect(m_pDatabase, pConnection) == TDS_FAIL)
  {
		if (pConnection)
    {
			tds_free_socket(m_pDatabase);
			//m_pDatabase = NULL;
			tds_free_connection(pConnection);
		}
		//fprintf(stderr, "tds_connect() failed\n");
    if (GetErrorCode() == DATABASE_LAYER_OK)
    {
      SetError(DATABASE_LAYER_ERROR, wxT("Database connection failed"));
    }
    // Reset the variables so there are not mistaken as initialized
    m_pDatabase = NULL;
    if (m_pLogin)
    {
      tds_free_login(m_pLogin);
      m_pLogin = NULL;
    }
    if (m_pContext)
    {
      tds_free_context(m_pContext);
      m_pContext = NULL;
    }
    ThrowDatabaseException();
		return false;
	}
	tds_free_connection(pConnection);

  return true;
}