int main(int argc, char **argv) { CS_CONTEXT *ctx; CS_CONNECTION *conn; int ret = 1; read_login_info(); if (cs_ctx_alloc(CS_VERSION_100, &ctx) != CS_SUCCEED) { fprintf(stderr, "Context Alloc failed!\n"); return ret; } if (ct_init(ctx, CS_VERSION_100) != CS_SUCCEED) { fprintf(stderr, "Library Init failed!\n"); return ret; } if (ct_con_alloc(ctx, &conn) != CS_SUCCEED) { fprintf(stderr, "Connect Alloc failed!\n"); return ret; } if (ct_con_props(conn, CS_SET, CS_USERNAME, (CS_VOID*) "sa", CS_NULLTERM, NULL) != CS_SUCCEED) { fprintf(stderr, "ct_con_props() SET USERNAME failed!\n"); return ret; } if (ct_con_props(conn, CS_SET, CS_PASSWORD, (CS_VOID*) "invalid", CS_NULLTERM, NULL) != CS_SUCCEED) { fprintf(stderr, "ct_con_props() SET PASSWORD failed!\n"); return ret; } if (ct_connect(conn, SERVER, CS_NULLTERM) != CS_FAIL) { fprintf(stderr, "Connection succeeded??\n"); return ret; } if (ct_cancel(conn, NULL, CS_CANCEL_ALL) != CS_SUCCEED) { fprintf(stderr, "ct_cancel() failed!\n"); return ret; } if (ct_close(conn, CS_UNUSED) != CS_SUCCEED) { fprintf(stderr, "ct_close() failed!\n"); return ret; } if (ct_con_drop(conn) != CS_SUCCEED) { fprintf(stderr, "ct_con_drop() failed!\n"); return ret; } if (ct_exit(ctx, CS_UNUSED) != CS_SUCCEED) { fprintf(stderr, "ct_exit() failed!\n"); return ret; } if (cs_ctx_drop(ctx) != CS_SUCCEED) { fprintf(stderr, "cs_ctx_drop() failed!\n"); return ret; } fprintf(stdout, "Test succeeded\n"); return 0; }
CAMLprim value mltds_ct_connect(value connection, value servername) { CAMLparam2(connection, servername); retval_inspect( "ct_connect", ct_connect(connection_ptr(connection), String_val(servername), string_length(servername))); CAMLreturn(Val_unit); }
static VALUE connection_Initialize(VALUE self, VALUE connection_hash) { TDS_Connection* conn; char *servername = NULL; char *username = NULL; char *password = NULL; char *confile = NULL; char *charset = NULL; VALUE temp; VALUE errors; CS_RETCODE ret; Data_Get_Struct(self, TDS_Connection, conn); cs_ctx_alloc(CS_VERSION_100, &conn->context); ct_init(conn->context, CS_VERSION_100); ct_con_alloc(conn->context, &conn->connection); // conn->context->msg_handler = connection_handle_message; // conn->context->err_handler = connection_handle_message; /* now let's get the connection parameters */ temp = rb_hash_aref(connection_hash, ID2SYM(rb_intern("username"))); username = value_to_cstr(temp); temp = rb_hash_aref(connection_hash, ID2SYM(rb_intern("password"))); password = value_to_cstr(temp); temp = rb_hash_aref(connection_hash, ID2SYM(rb_intern("servername"))); servername = value_to_cstr(temp); temp = rb_hash_aref(connection_hash, ID2SYM(rb_intern("charset"))); charset = value_to_cstr(temp); if(charset==NULL) { charset = strdup("ISO-8859-1"); } /* validate parameters */ if (!servername) { rb_raise(rb_eArgError, "You must specify a servername"); return Qnil; } if (!username) { rb_raise(rb_eArgError, "No username specified"); return Qnil; } if (!password) { password = strdup(""); } // printf("*** servername='%s', username='******' password='******'\n", servername, username, password); ct_con_props(conn->connection, CS_SET, CS_USERNAME, username, CS_NULLTERM, NULL); ct_con_props(conn->connection, CS_SET, CS_PASSWORD, password, CS_NULLTERM, NULL); /* Try to open a connection */ ret = ct_connect(conn->connection, servername, CS_NULLTERM); /* free up all the memory */ if (username) { free(username); username = NULL; } if (password) { free(password); password = NULL; } if (servername) { free(servername); } if(ret!=CS_SUCCEED) { rb_raise(rb_eIOError, "Connection failed"); return Qnil; } rb_iv_set(self, "@messages", rb_ary_new()); errors = rb_ary_new(); rb_iv_set(self, "@errors", errors); return Qnil; }
CS_RETCODE try_ctlogin(CS_CONTEXT ** ctx, CS_CONNECTION ** conn, CS_COMMAND ** cmd, int verbose) { CS_RETCODE ret; char query[30]; TDSCONTEXT *tds_ctx; /* read login information from PWD file */ ret = read_login_info(); if (ret != CS_SUCCEED) { if (verbose) { fprintf(stderr, "read_login_info() failed!\n"); } return ret; } ret = cs_ctx_alloc(CS_VERSION_100, ctx); if (ret != CS_SUCCEED) { if (verbose) { fprintf(stderr, "Context Alloc failed!\n"); } return ret; } /* Force default date format, some tests rely on it */ tds_ctx = (TDSCONTEXT *) (*ctx)->tds_ctx; if (tds_ctx && tds_ctx->locale && tds_ctx->locale->date_fmt) { free(tds_ctx->locale->date_fmt); tds_ctx->locale->date_fmt = tds_strdup("%b %d %Y %I:%M%p"); } ret = ct_init(*ctx, CS_VERSION_100); if (ret != CS_SUCCEED) { if (verbose) { fprintf(stderr, "Library Init failed!\n"); } return ret; } ret = ct_con_alloc(*ctx, conn); if (ret != CS_SUCCEED) { if (verbose) { fprintf(stderr, "Connect Alloc failed!\n"); } return ret; } ret = ct_con_props(*conn, CS_SET, CS_USERNAME, USER, CS_NULLTERM, NULL); if (ret != CS_SUCCEED) { if (verbose) { fprintf(stderr, "ct_con_props() SET USERNAME failed!\n"); } return ret; } ret = ct_con_props(*conn, CS_SET, CS_PASSWORD, PASSWORD, CS_NULLTERM, NULL); if (ret != CS_SUCCEED) { if (verbose) { fprintf(stderr, "ct_con_props() SET PASSWORD failed!\n"); } return ret; } ret = ct_connect(*conn, SERVER, CS_NULLTERM); if (ret != CS_SUCCEED) { if (verbose) { fprintf(stderr, "Connection failed!\n"); } return ret; } ret = ct_cmd_alloc(*conn, cmd); if (ret != CS_SUCCEED) { if (verbose) { fprintf(stderr, "Command Alloc failed!\n"); } return ret; } strcpy(query, "use "); strncat(query, DATABASE, 20); ret = run_command(*cmd, query); if (ret != CS_SUCCEED) return ret; return CS_SUCCEED; }
CS_RETCODE continue_logging_in(CS_CONTEXT ** ctx, CS_CONNECTION ** conn, CS_COMMAND ** cmd, int verbose) { CS_RETCODE ret; char query[30]; #ifdef TDS_STATIC_CAST TDSCONTEXT *tds_ctx; #endif ret = cs_ctx_alloc(CS_VERSION_100, ctx); if (ret != CS_SUCCEED) { if (verbose) { fprintf(stderr, "Context Alloc failed!\n"); } return ret; } #ifdef TDS_STATIC_CAST /* Force default date format, some tests rely on it */ tds_ctx = (TDSCONTEXT *) (*ctx)->tds_ctx; if (tds_ctx && tds_ctx->locale && tds_ctx->locale->date_fmt) { free(tds_ctx->locale->date_fmt); tds_ctx->locale->date_fmt = strdup("%b %d %Y %I:%M%p"); } #endif ret = ct_init(*ctx, CS_VERSION_100); if (ret != CS_SUCCEED) { if (verbose) { fprintf(stderr, "Library Init failed!\n"); } return ret; } if ((ret = ct_callback(*ctx, NULL, CS_SET, CS_CLIENTMSG_CB, (CS_VOID*) clientmsg_cb)) != CS_SUCCEED) { fprintf(stderr, "ct_callback() failed\n"); return ret; } if ((ret = ct_callback(*ctx, NULL, CS_SET, CS_SERVERMSG_CB, servermsg_cb)) != CS_SUCCEED) { fprintf(stderr, "ct_callback() failed\n"); return ret; } ret = ct_con_alloc(*ctx, conn); if (ret != CS_SUCCEED) { if (verbose) { fprintf(stderr, "Connect Alloc failed!\n"); } return ret; } ret = ct_con_props(*conn, CS_SET, CS_USERNAME, USER, CS_NULLTERM, NULL); if (ret != CS_SUCCEED) { if (verbose) { fprintf(stderr, "ct_con_props() SET USERNAME failed!\n"); } return ret; } ret = ct_con_props(*conn, CS_SET, CS_PASSWORD, PASSWORD, CS_NULLTERM, NULL); if (ret != CS_SUCCEED) { if (verbose) { fprintf(stderr, "ct_con_props() SET PASSWORD failed!\n"); } return ret; } printf("connecting as %s to %s.%s\n", USER, SERVER, DATABASE); ret = ct_connect(*conn, SERVER, CS_NULLTERM); if (ret != CS_SUCCEED) { if (verbose) { fprintf(stderr, "Connection failed!\n"); } return ret; } ret = ct_cmd_alloc(*conn, cmd); if (ret != CS_SUCCEED) { if (verbose) { fprintf(stderr, "Command Alloc failed!\n"); } return ret; } strcpy(query, "use "); strncat(query, DATABASE, 20); ret = run_command(*cmd, query); if (ret != CS_SUCCEED) return ret; return CS_SUCCEED; }
CS_RETCODE SybConnection::connect_(const char *app, const char *addr, const char *user, const char *pwd, const char *db) { CS_RETCODE retcode; CS_BOOL hafailover = CS_TRUE; // Allocate a connection structure. retcode = ct_con_alloc(sContext_, &conn_); if (retcode != CS_SUCCEED) { SysLogger::error("connect_: ct_con_alloc() failed"); return retcode; } // If a appname is defined, set the CS_APPNAME property. if (app != NULL) { retcode = ct_con_props(conn_, CS_SET, CS_APPNAME, (CS_VOID*)app, CS_NULLTERM, NULL); if (retcode != CS_SUCCEED) { SysLogger::error("connect_: ct_con_props(CS_APPNAME) failed"); ct_con_drop(conn_); conn_ = NULL; return retcode; } } // If a servername is defined, set the CS_SERVERNAME property. if (addr != NULL) { retcode = ct_con_props(conn_, CS_SET, CS_SERVERADDR, (CS_VOID*)addr, CS_NULLTERM, NULL); if (retcode != CS_SUCCEED) { SysLogger::error("connect_: ct_con_props(CS_SERVERADDR) failed"); ct_con_drop(conn_); conn_ = NULL; return retcode; } } // If a username is defined, set the CS_USERNAME property. if (user != NULL) { retcode = ct_con_props(conn_, CS_SET, CS_USERNAME, (CS_VOID*)user, CS_NULLTERM, NULL); if (retcode != CS_SUCCEED) { SysLogger::error("connect_: ct_con_props(CS_USERNAME) failed"); ct_con_drop(conn_); conn_ = NULL; return retcode; } } // If a password is defined, set the CS_PASSWORD property. if (pwd != NULL) { retcode = ct_con_props(conn_, CS_SET, CS_PASSWORD, (CS_VOID*)pwd, CS_NULLTERM, NULL); if (retcode != CS_SUCCEED) { SysLogger::error("connect_: ct_con_props(CS_PASSWORD) failed"); ct_con_drop(conn_); conn_ = NULL; return retcode; } } // Set the CS_HAFAILOVER property. retcode = ct_con_props(conn_, CS_SET, CS_HAFAILOVER, &hafailover, CS_UNUSED, NULL); if (retcode != CS_SUCCEED) { SysLogger::error("connect_: ct_con_props(CS_HAFAILOVER) failed"); ct_con_drop(conn_); conn_ = NULL; return retcode; } // Connect to the server. retcode = ct_connect(conn_, NULL, CS_UNUSED); if (retcode != CS_SUCCEED) { SysLogger::error("connect_: ct_connect() failed"); ct_con_drop(conn_); conn_ = NULL; return retcode; } return CS_SUCCEED; }
static int ct_session_connect (transport_endpoint_cfg_t * tep) { session_endpoint_cfg_t *sep_ext; session_endpoint_t *sep; app_worker_t *app_wrk; session_handle_t lh; application_t *app; app_listener_t *al; u32 table_index; session_t *ll; u8 fib_proto; sep_ext = (session_endpoint_cfg_t *) tep; sep = (session_endpoint_t *) tep; app_wrk = app_worker_get (sep_ext->app_wrk_index); app = application_get (app_wrk->app_index); sep->transport_proto = sep_ext->original_tp; table_index = application_local_session_table (app); lh = session_lookup_local_endpoint (table_index, sep); if (lh == SESSION_DROP_HANDLE) return VNET_API_ERROR_APP_CONNECT_FILTERED; if (lh == SESSION_INVALID_HANDLE) goto global_scope; ll = listen_session_get_from_handle (lh); al = app_listener_get_w_session (ll); /* * Break loop if rule in local table points to connecting app. This * can happen if client is a generic proxy. Route connect through * global table instead. */ if (al->app_index == app->app_index) goto global_scope; return ct_connect (app_wrk, ll, sep_ext); /* * If nothing found, check the global scope for locally attached * destinations. Make sure first that we're allowed to. */ global_scope: if (session_endpoint_is_local (sep)) return VNET_API_ERROR_SESSION_CONNECT; if (!application_has_global_scope (app)) return VNET_API_ERROR_APP_CONNECT_SCOPE; fib_proto = session_endpoint_fib_proto (sep); table_index = application_session_table (app, fib_proto); ll = session_lookup_listener_wildcard (table_index, sep); if (ll) return ct_connect (app_wrk, ll, sep_ext); /* Failed to connect but no error */ return 1; }